#include #include #include #include #include int read_ery(char *eryfile, float fillval, char *header, float eryex[180][288]) { /* This function opens the erythemal exposure file specified by the char. variable eryfile, and reads the header records and the data records. The data records contain value codes which are decoded in this routine, so that the float values that are passed back to the calling routine are the represented erythemal exposures. The routine reads the data in character form into the beginning of the location eryex. It then works from the end to the beginning, decoding the value codes, and placing the floating point values into the appropriate locations in eryex. Author: Edward A. Celarier, Software Corp. of America, Beltsville, MD. */ float *dp; unsigned char *cp; FILE *fd_in; int ilat, i, k, item, iline; char hdr1[82], hdr2[82], hdr3[82]; int e, m1, m2; size_t objsize=1, nbytes= 162540; unsigned char cmask= 0x0f; float omag[10]= {1.e0, 1.e1, 1.e2, 1.e3, 1.e4, 1.e5, 1.e6, 1.e7, 1.e8, 1.e9}; fd_in= fopen(eryfile, "r"); if (fd_in != NULL) { fgets(hdr1, sizeof(hdr1), fd_in); fgets(hdr2, sizeof(hdr2), fd_in); fgets(hdr3, sizeof(hdr3), fd_in); header[0]= NULL; strncat(header, hdr1, 100); strncat(header, hdr2, 100); strncat(header, hdr3, 100); fread(eryex, objsize, nbytes,fd_in); /* Initialize pointers. dp is the data destination, cp is the character position in the input character data stream */ dp= &eryex[179][287]; cp= (unsigned char*)&eryex[0][0] + nbytes; /* Here begins the looping. We take 1 latitude at a time, and read all the lines belonging to each latitude. Everything is read from the end of the data to the beginning of the data. The characters from the input file only take up 162540 bytes out of the whole table, which contains 207360 bytes. By reading through the data backwards, we eventually overwrite characters from the input, but only after we have already interpreted them. At the end of this process, (ilat=0, iline=0, item=0), we are reading the first three characters in the table, forming the decoded value, and stuffing the value into the first four bytes of the table for the final overwrite. Clever, no? */ for (ilat= 179 ; ilat >= 0 ; ilat--) { cp -= 16; for (item= 12 ; item >=0 ; item--) /*last line only has 13 items*/ { cp -= 3; e= (int)(cmask & *cp) ; m1= (int)(cmask & *(cp+1)) ; m2= (int)(cmask & *(cp+2)); if( e != 9 || m1 !=9 || m2 !=9) *dp-- = (m1 + 0.1*m2)*omag[e]; else *dp-- = fillval; } cp -= 2; /*skip over 0a20*/ for (iline= 10 ; iline >=0 ; iline--) /*all other lines have 25 items*/ { for (item= 24 ; item >=0 ; item--) { cp -= 3; e= (int)(cmask & *cp) ; m1= (int)(cmask & *(cp+1)) ; m2= (int)(cmask & *(cp+2)); if( e != 9 || m1 !=9 || m2 !=9) *dp-- = (m1 + 0.1*m2)*omag[e]; else *dp-- = fillval; } cp -=2; /*skip over 0a20*/ } cp++; } return(1); } else return(0); } main() { float fillval = -999.; char infile[20]= "ga999999.n7e", outfile[20]= "ery.tst"; float ery[180][288]; char header[300]; int retval, iln, ilt; FILE *fd_out; retval= read_ery(infile, fillval, header, ery); printf("%s", header); fd_out= fopen(outfile,"w"); if(fd_out != NULL) { for (ilt = 0 ; ilt < 180 ; ilt++) for (iln = 0 ; iln < 288 ; iln++) fprintf(fd_out, "%10.2e\n", ery[ilt][iln]); } }