returns normally.
@return true = ok, false = error.
*/
boolean write_v5d_header() throws IOException {
int vr, time, filler, maxnl;
RandomAccessFile f;
boolean newfile;
if (FileFormat != 0) {
System.err.println("Error: " +
"v5d library can't write comp5d format files.");
return false;
}
f = FileDesc;
if (!v5dVerifyStruct()) return false;
// Determine if we're writing to a new file
newfile = (FirstGridPos == 0);
// compute grid sizes
SumGridSizes = 0;
for (vr=0; vr<NumVars; vr++) {
GridSize[vr] = 8 * Nl[vr] + v5dSizeofGrid(0, vr);
SumGridSizes += GridSize[vr];
}
// set file pointer to start of file
f.seek(0);
CurPos = 0;
// Write the tagged header info
// ID
if (!write_tag(TAG_ID, 0, newfile)) return false;
// File Version
if (!write_tag(TAG_VERSION, 10, newfile)) return false;
f.write(FILE_VERSION.getBytes(), 0, 10);
// Number of timesteps
if (!write_tag(TAG_NUMTIMES, 4, newfile)) return false;
f.writeInt(NumTimes);
// Number of variables
if (!write_tag(TAG_NUMVARS, 4, newfile)) return false;
f.writeInt(NumVars);
// Names of variables
for (vr=0; vr<NumVars; vr++) {
if (!write_tag(TAG_VARNAME, 14, newfile)) return false;
f.writeInt(vr);
for (int q=0; q<10; q++) f.writeByte((byte) VarName[vr][q]);
}
// Physical Units
for (vr=0; vr<NumVars; vr++) {
if (!write_tag(TAG_UNITS, 24, newfile)) return false;
f.writeInt(vr);
for (int q=0; q<20; q++) f.writeByte((byte) Units[vr][q]);
}
// Date and time of each timestep
for (time=0; time<NumTimes; time++) {
if (!write_tag(TAG_TIME, 8, newfile)) return false;
f.writeInt(time);
f.writeInt(TimeStamp[time]);
if (!write_tag(TAG_DATE, 8, newfile)) return false;
f.writeInt(time);
f.writeInt(DateStamp[time]);
}
// Number of rows
if (!write_tag(TAG_NR, 4, newfile)) return false;
f.writeInt(Nr);
// Number of columns
if (!write_tag(TAG_NC, 4, newfile)) return false;
f.writeInt(Nc);
// Number of levels, compute maxnl
maxnl = 0;
for (vr=0; vr<NumVars; vr++) {
if (!write_tag(TAG_NL_VAR, 8, newfile)) return false;
f.writeInt(vr);
f.writeInt(Nl[vr]);
if (!write_tag(TAG_LOWLEV_VAR, 8, newfile)) return false;
f.writeInt(vr);
f.writeInt(LowLev[vr]);
if (Nl[vr] + LowLev[vr] > maxnl) maxnl = Nl[vr]+LowLev[vr];
}
// Min/Max values
for (vr=0; vr<NumVars; vr++) {
if (!write_tag(TAG_MINVAL, 8, newfile)) return false;
f.writeInt(vr);
f.writeFloat(MinVal[vr]);
if (!write_tag(TAG_MAXVAL, 8, newfile)) return false;
f.writeInt(vr);
f.writeFloat(MaxVal[vr]);
}
// Compress mode
if (!write_tag(TAG_COMPRESS, 4, newfile)) return false;
f.writeInt(CompressMode);
// Vertical Coordinate System
if (!write_tag(TAG_VERTICAL_SYSTEM, 4, newfile)) return false;
f.writeInt(VerticalSystem);
if (!write_tag(TAG_VERT_ARGS, 4+4*MAXVERTARGS, newfile)) return false;
f.writeInt(MAXVERTARGS);
for (int q=0; q<MAXVERTARGS; q++) f.writeFloat(VertArgs[q]);
// Map Projection
if (!write_tag(TAG_PROJECTION, 4, newfile)) return false;
f.writeInt(Projection);
if (!write_tag(TAG_PROJ_ARGS, 4+4*MAXPROJARGS, newfile)) return false;
f.writeInt(MAXPROJARGS);
for (int q=0; q<MAXPROJARGS; q++) f.writeFloat(ProjArgs[q]);
// write END tag
if (newfile) {
// We're writing to a brand new file.
// Reserve 10000 bytes for future header growth.
if (!write_tag(TAG_END, 10000, newfile)) return false;
f.skipBytes(10000);
// Let file pointer indicate where first grid is stored
FirstGridPos = (int) f.getFilePointer();
}
else {
// we're rewriting a header
filler = FirstGridPos - (int) f.getFilePointer();
if (!write_tag(TAG_END, filler - 8, newfile)) return false;
}
return true;
}