*/
boolean read_v5d_header() throws IOException, BadFormException {
boolean end_of_header = false;
int id;
int idlen, vr, numargs;
RandomAccessFile f;
f = FileDesc;
int order = f.BIG_ENDIAN;
// first try to read the header id, check the endianness
while (true) {
f.seek(0);
f.order(order);
id = f.readInt();
idlen = f.readInt();
if (id == TAG_ID && idlen == 0) {
// this is a v5d file
FileFormat = 0;
break;
}
else if (id >= 0x80808080 && id <= 0x80808083) {
// this is an old COMP* file
FileFormat = id;
return read_comp_header();
}
else if (order == f.BIG_ENDIAN) {
order = f.LITTLE_ENDIAN;
continue; // try again
} else {
// unknown file type
System.out.println("unknown file type");
return false;
}
}
// default
CompressMode = 1;
while (!end_of_header) {
int tag, length;
int i, time, nl, lev;
tag = f.readInt();
length = f.readInt();
switch (tag) {
case TAG_VERSION:
V5Dassert(length == 10);
byte[] b = new byte[10];
f.read(b, 0, 10);
int index = 10;
for (int q=0; q<10; q++) {
if (b[q] == 0) {
index = q;
break;
}
}
FileVersion = new String(b, 0, index);
// Check if reading a file made by a future version of Vis5D
if (FileVersion.compareTo(FILE_VERSION) > 0) {
System.err.println("Warning: Trying to read a version " +
FileVersion + " file, you should upgrade Vis5D.");
}
break;
case TAG_NUMTIMES:
V5Dassert(length == 4);
NumTimes = f.readInt();
break;
case TAG_NUMVARS:
V5Dassert(length == 4);
NumVars = f.readInt();
break;
case TAG_VARNAME:
// 1 int + 10 char
V5Dassert(length == 14);
vr = f.readInt();
for (int q=0; q<10; q++) VarName[vr][q] = (char) f.readByte();
break;
case TAG_NR:
// Number of rows for all variables
V5Dassert(length == 4);
Nr = f.readInt();
break;
case TAG_NC:
// Number of columns for all variables
V5Dassert(length == 4);
Nc = f.readInt();
break;
case TAG_NL:
// Number of levels for all variables
V5Dassert(length == 4);
nl = f.readInt();
for (i=0; i<NumVars; i++) {
Nl[i] = nl;
}
break;
case TAG_NL_VAR:
// Number of levels for one variable
V5Dassert(length == 8);
vr = f.readInt();
Nl[vr] = f.readInt();
break;
case TAG_LOWLEV_VAR:
// Lowest level for one variable
V5Dassert(length == 8);
vr = f.readInt();
LowLev[vr] = f.readInt();
break;
case TAG_TIME:
// Time stamp for 1 timestep
V5Dassert(length == 8);
time = f.readInt();
TimeStamp[time] = f.readInt();
break;
case TAG_DATE:
// Date stamp for 1 timestep
V5Dassert(length == 8);
time = f.readInt();
DateStamp[time] = f.readInt();
break;
case TAG_MINVAL:
// Minimum value for a variable
V5Dassert(length == 8);
vr = f.readInt();
MinVal[vr] = f.readFloat();
break;
case TAG_MAXVAL:
// Maximum value for a variable
V5Dassert(length == 8);
vr = f.readInt();
MaxVal[vr] = f.readFloat();
break;
case TAG_COMPRESS:
// Compress mode
V5Dassert(length == 4);
CompressMode = f.readInt();
break;
case TAG_UNITS:
// physical units
V5Dassert(length == 24);
vr = f.readInt();
for (int q=0; q<20; q++) Units[vr][q] = (char) f.readByte();
break;
// Vertical coordinate system
case TAG_VERTICAL_SYSTEM:
V5Dassert(length == 4);
VerticalSystem = f.readInt();
if (VerticalSystem < 0 || VerticalSystem > 3) {
System.err.println("Error: bad vertical coordinate system: " +
VerticalSystem);
}
break;
case TAG_VERT_ARGS:
numargs = f.readInt();
V5Dassert(numargs <= MAXVERTARGS);
for (int q=0; q<numargs; q++) VertArgs[q] = f.readFloat();
V5Dassert(length == numargs * 4 + 4);
break;
case TAG_HEIGHT:
// height of a grid level
V5Dassert(length == 8);
lev = f.readInt();
VertArgs[lev] = f.readFloat();
break;
case TAG_BOTTOMBOUND:
V5Dassert(length == 4);
VertArgs[0] = f.readFloat();
break;
case TAG_LEVINC:
V5Dassert(length == 4);
VertArgs[1] = f.readFloat();
break;
// Map projection information
case TAG_PROJECTION:
V5Dassert(length == 4);
Projection = f.readInt();
// WLH 4-21-95
if (Projection < 0 || Projection > 4) {
System.err.println("Error while reading header, bad projection (" +
Projection + ")");
return false;
}
break;
case TAG_PROJ_ARGS:
numargs = f.readInt();
V5Dassert(numargs <= MAXPROJARGS);
for (int q=0; q<numargs; q++) ProjArgs[q] = f.readFloat();
V5Dassert(length == 4 * numargs + 4);
break;
case TAG_NORTHBOUND:
V5Dassert(length == 4);
if (Projection == 0 || Projection == 1 || Projection == 4) {
ProjArgs[0] = f.readFloat();
}
else {
f.skipBytes(4);
}
break;
case TAG_WESTBOUND:
V5Dassert(length == 4);
if (Projection == 0 || Projection == 1 || Projection == 4) {
ProjArgs[1] = f.readFloat();
}
else {
f.skipBytes(4);
}
break;
case TAG_ROWINC:
V5Dassert(length == 4);
if (Projection == 0 || Projection == 1 || Projection == 4) {
ProjArgs[2] = f.readFloat();
}
else {
f.skipBytes(4);
}
break;
case TAG_COLINC:
V5Dassert(length == 4);
if (Projection == 0 || Projection == 1 || Projection == 4) {
ProjArgs[3] = f.readFloat();
}
else if (Projection == 2) {
ProjArgs[5] = f.readFloat();
}
else if (Projection == 3) {
ProjArgs[4] = f.readFloat();
}
else {
f.skipBytes(4);
}
break;
case TAG_LAT1:
V5Dassert(length == 4);
if (Projection == 2) {
ProjArgs[0] = f.readFloat();
}
else {
f.skipBytes(4);
}
break;
case TAG_LAT2:
V5Dassert(length == 4);
if (Projection == 2) {
ProjArgs[1] = f.readFloat();
}
else {
f.skipBytes(4);
}
break;
case TAG_POLE_ROW:
V5Dassert(length == 4);
if (Projection == 2) {
ProjArgs[2] = f.readFloat();
}
else {
f.skipBytes(4);
}
break;
case TAG_POLE_COL:
V5Dassert(length == 4);
if (Projection == 2) {
ProjArgs[3] = f.readFloat();
}
else {
f.skipBytes(4);
}
break;
case TAG_CENTLON:
V5Dassert(length == 4);
if (Projection == 2) {
ProjArgs[4] = f.readFloat();
}
else if (Projection == 3) {
ProjArgs[1] = f.readFloat();
}
// WLH 4-21-95
else if (Projection == 4) {
ProjArgs[5] = f.readFloat();
}
else {
f.skipBytes(4);
}
break;
case TAG_CENTLAT:
V5Dassert(length == 4);
if (Projection == 3) {
ProjArgs[0] = f.readFloat();
}
// WLH 4-21-95
else if (Projection == 4) {
ProjArgs[4] = f.readFloat();
}
else {
f.skipBytes(4);
}
break;
case TAG_CENTROW:
V5Dassert(length == 4);
if (Projection == 3) {
ProjArgs[2] = f.readFloat();
}
else {
f.skipBytes(4);
}
break;
case TAG_CENTCOL:
V5Dassert(length == 4);
if (Projection == 3) {
ProjArgs[3] = f.readFloat();
}
else {
f.skipBytes(4);
}
break;
case TAG_ROTATION:
V5Dassert(length == 4);
// WLH 4-21-95
if (Projection == 4) {
ProjArgs[6] = f.readFloat();
}
else {
f.skipBytes(4);
}
break;
case TAG_END:
// end of header
end_of_header = true;
f.skipBytes(length);
break;
default:
// unknown tag, skip to next tag
System.err.println("Unknown tag: " + tag + " length=" + length);
f.skipBytes(length);
break;
}
}
v5dVerifyStruct();
// Now we're ready to read the grid data
// Save current file pointer
FirstGridPos = (int) f.getFilePointer();
// compute grid sizes
SumGridSizes = 0;
for (vr=0;vr<NumVars;vr++) {
GridSize[vr] = 8 * Nl[vr] + v5dSizeofGrid(0, vr);