/***************************************************************** * Filename: read_sws2A.c * * Usage: * % read_sws2A [-av] [ ] * where: * -a entire file * -v verbose * : the name of the SeaWinds Level 2A input file * , : the records to retrieve from the file * * Read a slab of a SeaWinds L2A file, then process (just print) the data. * * This program does not hard code the following values, available at run-time: * - swath length and swath width (1624 and 76 or 810) * - scale factor for each object (e.g. .01 for sigma0) * Difficult not to hard code: * - rank, the # of dimensions, for the objects (1 to 3) * - the order of the dimensions: swath length, swath width, quadrant * * NOTE: * The HDF library, version 4, must be installed for this program to work. * The National Center for Supercomputing Applications (NCSA) at * http://hdf.ncsa.uiuc.edu freely distributes HDF. * * Author: Richard Chen, PO.DAAC, JPL * Date: 17 July 2003 ******************************************************************/ #include #include #include /* getopt(), optind */ #include "hdfPodaac.h" /* anything involving DTYPE */ #include /* int32, DFACC_RDONLY, SD*(); includes */ int verbose = 0; void getInput(int argc, char **argv, int32 *sd_id, int32 *v_id, int *row1, int *rowN, int *verbose); /* These print data via different ways. Your code would be like one of them */ void process1(int32 sd_id, int32 v_id, int row1, int rowN); void process2(int32 sd_id, int32 v_id, int row1, int rowN); int main(int argc, char *argv[]) { int row1, rowN; int32 sd_id, v_id; getInput(argc, argv, &sd_id, &v_id, &row1, &rowN, &verbose); row1--; rowN--; /* use C indices */ process1(sd_id, v_id, row1, rowN); /* try process2() yourself */ SDend(sd_id); Vend(v_id); Hclose(v_id); return(0); } /* main() */ /* The next two functions do the same thing but in different ways. * You would probably model your code like one of them. */ /* This version dynamically allocates memory and accesses data via * getReal(), etc. Probably slower than process2(), BUT more flexible * (i.e. row0 and rowN can be anything). */ void process1(int32 sd_id, int32 v_id, int row0, int rowN) { DTYPE *wrt, *rn, *ns, *nspc, *nwti, *mwti, *swti, *nwto, *mwto, *swto, *cix, *clt, *cln, *ca, *ci, *s0, *sam, *ka, *kb, *kg, *sqf, *smf, *sf, /* the AMSR vars: */ *ap, *aaa, *aaau, *aab, *aabu, *wqr, *wqq; int32 start[3], len[3]; /* len == hdf's "edges"; dim 3 ignored for 2D vars*/ int i, j; start[0] = row0; start[1] = start[2] = 0; len[0] = rowN - row0 + 1; len[1] = len[2] = 0; /* let openSDS() get * from these dimensions */ wrt =openVdata(v_id, "wvc_row_time", start[0],len[0], NULL,NULL); rn =openSDS(sd_id, "row_number", start,len, NULL,NULL); ns =openSDS(sd_id, "num_sigma0", start,len, NULL,NULL); nspc =openSDS(sd_id, "num_sigma0_per_cell", start,len, NULL,NULL); nwti =openSDS(sd_id, "num_wvc_tb_in", start,len, NULL,NULL); mwti =openSDS(sd_id, "mean_wvc_tb_in", start,len, NULL,NULL); swti =openSDS(sd_id, "std_dev_wvc_tb_in", start,len, NULL,NULL); nwto =openSDS(sd_id, "num_wvc_tb_out", start,len, NULL,NULL); mwto =openSDS(sd_id, "mean_wvc_tb_out", start,len, NULL,NULL); swto =openSDS(sd_id, "std_dev_wvc_tb_out", start,len, NULL,NULL); cix =openSDS(sd_id, "cell_index", start,len, NULL,NULL); clt =openSDS(sd_id, "cell_lat", start,len, NULL,NULL); cln =openSDS(sd_id, "cell_lon", start,len, NULL,NULL); ca =openSDS(sd_id, "cell_azimuth", start,len, NULL,NULL); ci =openSDS(sd_id, "cell_incidence", start,len, NULL,NULL); s0 =openSDS(sd_id, "sigma0", start,len, NULL,NULL); sam =openSDS(sd_id, "sigma0_attn_map", start,len, NULL,NULL); ka =openSDS(sd_id, "kp_alpha", start,len, NULL,NULL); kb =openSDS(sd_id, "kp_beta", start,len, NULL,NULL); kg =openSDS(sd_id, "kp_gamma", start,len, NULL,NULL); sqf =openSDS(sd_id, "sigma0_qual_flag", start,len, NULL,NULL); smf =openSDS(sd_id, "sigma0_mode_flag", start,len, NULL,NULL); sf =openSDS(sd_id, "surface_flag", start,len, NULL,NULL); ap =openSDS(sd_id, "amsr_pointer", start,len, NULL,NULL); aaa =openSDS(sd_id, "amsr_atm_attn", start,len, NULL,NULL); aaau =openSDS(sd_id, "amsr_atm_attn_uncert", start,len, NULL,NULL); aab =openSDS(sd_id, "amsr_atm_backscatter", start,len, NULL,NULL); aabu =openSDS(sd_id, "amsr_atm_backscatter_uncert", start,len, NULL,NULL); wqr =openSDS(sd_id, "wvc_quad_rain_indicator", start,len, NULL,NULL); wqq =openSDS(sd_id, "wvc_quad_qual_flag", start,len, NULL,NULL); for (j = 0; j < len[0]; j++) { int oldCellIndex = -1; char *timeString; int numSigma0 = getInteger(ns, 1, j); printf("\nWVC ROW: %-9d", getInteger(rn, 1, j)); timeString = (char *) getCharArray(wrt, NULL, 1, j); printf("TIME: %s ", timeString); free(timeString); printf("TOTAL NUMBER OF SIGMA0s: %d\n", numSigma0); printf("wvc#: #sigma0s=hpolTempCount(Mean StdDev) + vpolTempCount(Mean StdDev). 2 cols\n"); for (i = 0; i < getDTYPE_DIM(nspc,1) / 2; i++) { /* ROW INFO */ int iOpp = getDTYPE_DIM(nspc,1) - 1 - i; /* i=0..37; iOpp=38..75 */ /* NOTE: std_dev*'s %4.1f saves space but rounds erratically */ printf("%2d: %2d=%2d(%6.2f %4.1f)+%2d(%6.2f %4.1f) ", i+1, getInteger(nspc, 2, j,i), getInteger(nwti, 2, j,i), getReal(mwti, 2, j,i), getReal(swti, 2, j,i), getInteger(nwto, 2, j,i), getReal(mwto, 2, j,i), getReal(swto, 2, j,i)); printf("%2d: %2d=%2d(%6.2f %4.1f)+%2d(%6.2f %4.1f)\n", iOpp+1, getInteger(nspc, 2, j,iOpp), getInteger(nwti, 2, j,iOpp), getReal(mwti, 2, j,iOpp), getReal(swti, 2, j,iOpp), getInteger(nwto, 2, j,iOpp), getReal(mwto, 2, j,iOpp), getReal(swto, 2, j,iOpp)); } if (numSigma0 <= 0) continue; for (i = 0; i < numSigma0; i++) { int cellIndex = getInteger(cix, 2, j,i); if (cellIndex != oldCellIndex) { /* many sigma0s in each wvc..*/ int k, cI = cellIndex - 1; /* ..so don't repeat wvc info*/ oldCellIndex = cellIndex; printf("WVC ROW: %d (continued) WIND VECTOR CELL: %d\n", getInteger(rn, 1, j), cellIndex); /* AMSR info. Move to ROW INFO loop? Would uglify output */ printf(" AMSR[1-4]: atten uncert backscatterPower uncert rain quality\n"); for (k = 0; k < getDTYPE_DIM(aaa,2); k++) { printf(" %.2f %.3f %.2f %.3f %.2f %05X", getReal(aaa, 3, j,cI,k), getReal(aaau, 3, j,cI,k), getReal(aab, 3, j,cI,k), getReal(aabu, 3, j,cI,k), getReal(wqr, 3, j,cI,k), getUInteger(wqq, 3, j,cI,k)); if (k % 2 == 1) putchar('\n'); } /* end of AMSR info */ printf(" Lat Lon Q Azi IncAng Sigma0 Atten Kp_alpha _beta _gamma Qual Mode Surf\n"); } /* NOTE kp_gamma should be %10.4e */ printf("%6.2f %6.2f %1d %6.2f %5.2f %6.2f %5.2f %5.3f %8.2e %8.2e x%03X %04X %03X\n", getReal(clt, 2, j,i), getReal(cln, 2, j,i), getUInteger(ap, 2, j,i), getReal(ca, 2, j,i), getReal(ci, 2, j,i), getReal(s0, 2, j,i), getReal(sam, 2, j,i), getReal(ka, 2, j,i), getReal(kb, 2, j,i), getReal(kg, 2, j,i), getUInteger(sqf, 2, j,i), getUInteger(smf, 2, j,i), getUInteger(sf, 2, j,i)); } } closeDTYPE(wrt); closeDTYPE(rn); closeDTYPE(ns); closeDTYPE(nspc); closeDTYPE(nwti); closeDTYPE(mwti); closeDTYPE(swti); closeDTYPE(nwto); closeDTYPE(mwto); closeDTYPE(swto); closeDTYPE(cix); closeDTYPE(clt); closeDTYPE(cln); closeDTYPE(ca); closeDTYPE(ci); closeDTYPE(s0); closeDTYPE(sam); closeDTYPE(ka); closeDTYPE(kb); closeDTYPE(kg); closeDTYPE(sqf); closeDTYPE(smf); closeDTYPE(sf); closeDTYPE(ap); closeDTYPE(aaa); closeDTYPE(aaau); closeDTYPE(aab); closeDTYPE(aabu); closeDTYPE(wqr); closeDTYPE(wqq); } /* process1() */ /* This version statically allocates memory and accesses data directly via 3D * arrays. Probably fastest and good for parallelization, BUT you must know at * compile-time the max number of requested rows, then set YGRID to it. * Note that *Mem's types are based on their storage types. */ #define XGRID1 76 /* can't tell at compile-time # of good wvcs */ #define XGRID2 810 /* can't tell at compile-time # of sigma0s */ #define YGRID 20 /* <=1702; inputted rows must be <= YGRID */ #define ZGRID 4 /* for amsr_* objects */ #define WRTLEN 21 /* the string length is like a dimension */ void process2(int32 sd_id, int32 v_id, int row0, int rowN) { DTYPE *wrt, *rn, *ns, *nspc, *nwti, *mwti, *swti, *nwto, *mwto, *swto, *cix, *clt, *cln, *ca, *ci, *s0, *sam, *ka, *kb, *kg, *sqf, *smf, *sf, /* the AMSR vars: */ *ap, *aaa, *aaau, *aab, *aabu, *wqr, *wqq; double mwtiScale, swtiScale, mwtoScale, swtoScale, cltScale, clnScale, caScale, ciScale, s0Scale, samScale, kaScale, kbScale, kgScale, aaaScale, aaauScale, aabScale, aabuScale, wqrScale; /* declare as static to get off stack; else possible crash if big YGRID */ static uint8 wrtMem[YGRID][WRTLEN]; static int16 rnMem[YGRID]; static int16 nsMem[YGRID]; static uint8 nspcMem[YGRID][XGRID1]; static uint8 nwtiMem[YGRID][XGRID1]; static uint16 mwtiMem[YGRID][XGRID1]; static uint16 swtiMem[YGRID][XGRID1]; static uint8 nwtoMem[YGRID][XGRID1]; static uint16 mwtoMem[YGRID][XGRID1]; static uint16 swtoMem[YGRID][XGRID1]; static uint8 cixMem[YGRID][XGRID2]; static int16 cltMem[YGRID][XGRID2]; static uint16 clnMem[YGRID][XGRID2]; static uint16 caMem[YGRID][XGRID2]; static int16 ciMem[YGRID][XGRID2]; static int16 s0Mem[YGRID][XGRID2]; static int16 samMem[YGRID][XGRID2]; static int16 kaMem[YGRID][XGRID2]; static uint16 kbMem[YGRID][XGRID2]; static float32 kgMem[YGRID][XGRID2]; static uint16 sqfMem[YGRID][XGRID2]; static uint16 smfMem[YGRID][XGRID2]; static uint16 sfMem[YGRID][XGRID2]; static uint8 apMem[YGRID][XGRID2]; static int16 aaaMem[YGRID][XGRID1][ZGRID]; static int16 aaauMem[YGRID][XGRID1][ZGRID]; static int16 aabMem[YGRID][XGRID1][ZGRID]; static int16 aabuMem[YGRID][XGRID1][ZGRID]; static int16 wqrMem[YGRID][XGRID1][ZGRID]; static uint32 wqqMem[YGRID][XGRID1][ZGRID]; int32 start[3], len[3]; /* len == hdf's "edges"; dim 3 ignored for 2D vars*/ int i, j; start[0] = row0; start[1] = 0; start[2] = 0; len[0] = rowN - row0 + 1; len[1] = XGRID1; len[2] = ZGRID; if (len[0] > YGRID) { fprintf(stderr, "ERROR process2(,,%d,%d) YGRID too small: %d\n", row0, rowN, YGRID); exit(0); } wrt=openVdata(v_id, "wvc_row_time", start[0],len[0], wrtMem,sizeof wrtMem); rn =openSDS(sd_id, "row_number", start,len, rnMem,sizeof rnMem); ns =openSDS(sd_id, "num_sigma0", start,len, nsMem,sizeof nsMem); nspc=openSDS(sd_id, "num_sigma0_per_cell",start,len,nspcMem,sizeof nspcMem); nwti=openSDS(sd_id, "num_wvc_tb_in", start,len, nwtiMem,sizeof nwtiMem); mwti=openSDS(sd_id, "mean_wvc_tb_in", start,len, mwtiMem,sizeof mwtiMem); swti=openSDS(sd_id, "std_dev_wvc_tb_in", start,len, swtiMem,sizeof swtiMem); nwto=openSDS(sd_id, "num_wvc_tb_out", start,len, nwtoMem,sizeof nwtoMem); mwto=openSDS(sd_id, "mean_wvc_tb_out", start,len, mwtoMem,sizeof mwtoMem); swto=openSDS(sd_id, "std_dev_wvc_tb_out",start,len, swtoMem,sizeof swtoMem); aaa =openSDS(sd_id, "amsr_atm_attn", start,len, aaaMem,sizeof aaaMem); aaau =openSDS(sd_id, "amsr_atm_attn_uncert", start,len, aaauMem,sizeof aaauMem); aab =openSDS(sd_id, "amsr_atm_backscatter", start,len, aabMem,sizeof aabMem); aabu =openSDS(sd_id, "amsr_atm_backscatter_uncert", start,len, aabuMem,sizeof aabuMem); wqr =openSDS(sd_id, "wvc_quad_rain_indicator", start,len, wqrMem,sizeof wqrMem); wqq =openSDS(sd_id, "wvc_quad_qual_flag", start,len, wqqMem,sizeof wqqMem); len[1] = XGRID2; cix =openSDS(sd_id, "cell_index", start,len, cixMem,sizeof cixMem); clt =openSDS(sd_id, "cell_lat", start,len, cltMem,sizeof cltMem); cln =openSDS(sd_id, "cell_lon", start,len, clnMem,sizeof clnMem); ca =openSDS(sd_id, "cell_azimuth", start,len, caMem,sizeof caMem); ci =openSDS(sd_id, "cell_incidence", start,len, ciMem,sizeof ciMem); s0 =openSDS(sd_id, "sigma0", start,len, s0Mem,sizeof s0Mem); sam =openSDS(sd_id, "sigma0_attn_map", start,len, samMem,sizeof samMem); ka =openSDS(sd_id, "kp_alpha", start,len, kaMem,sizeof kaMem); kb =openSDS(sd_id, "kp_beta", start,len, kbMem,sizeof kbMem); kg =openSDS(sd_id, "kp_gamma", start,len, kgMem,sizeof kgMem); sqf =openSDS(sd_id, "sigma0_qual_flag", start,len, sqfMem,sizeof sqfMem); smf =openSDS(sd_id, "sigma0_mode_flag", start,len, smfMem,sizeof smfMem); sf =openSDS(sd_id, "surface_flag", start,len, sfMem,sizeof sfMem); ap =openSDS(sd_id, "amsr_pointer", start,len, apMem,sizeof apMem); mwtiScale = getAttrDouble(mwti, "scale_factor"); /* == .01 */ swtiScale = getAttrDouble(swti, "scale_factor"); /* == .01 */ mwtoScale = getAttrDouble(mwto, "scale_factor"); /* == .01 */ swtoScale = getAttrDouble(swto, "scale_factor"); /* == .01 */ cltScale = getAttrDouble(clt, "scale_factor"); /* == .01 */ clnScale = getAttrDouble(cln, "scale_factor"); /* == .01 */ caScale = getAttrDouble(ca, "scale_factor"); /* == .01 */ ciScale = getAttrDouble(ci, "scale_factor"); /* == .01 */ s0Scale = getAttrDouble(s0, "scale_factor"); /* == .01 */ samScale = getAttrDouble(sam, "scale_factor"); /* == .01 */ kaScale = getAttrDouble(ka, "scale_factor"); /* == .001 */ kbScale = getAttrDouble(kb, "scale_factor"); /* == 1.0e-7 */ kgScale = getAttrDouble(kg, "scale_factor"); /* == 1.0 */ aaaScale = getAttrDouble(aaa, "scale_factor"); /* == .01 */ aaauScale = getAttrDouble(aaau, "scale_factor"); /* == .01 */ aabScale = getAttrDouble(aab, "scale_factor"); /* == .001 */ aabuScale = getAttrDouble(aabu, "scale_factor"); /* == .001 */ wqrScale = getAttrDouble(wqr, "scale_factor"); /* == .01 */ for (j = 0; j < len[0]; j++) { /* not (j = row0; j <= rowN; j++) */ int oldCellIndex = -1; int numSigma0 = nsMem[j]; printf("\nWVC ROW: %-9d", rnMem[j]); printf("TIME: %.*s ", WRTLEN, (char *)wrtMem[j]); printf("TOTAL NUMBER OF SIGMA0s: %d\n", numSigma0); printf("wvc#: #sigma0s=hpolTempCount(Mean StdDev) + vpolTempCount(Mean StdDev). 2 cols\n"); for (i = 0; i < XGRID1 / 2; i++) { /* ROW INFO */ int iOpp = XGRID1 - 1 - i; /* NOTE: std_dev*'s %4.1f saves space but rounds erratically */ printf("%2d: %2d=%2d(%6.2f %4.1f)+%2d(%6.2f %4.1f) ", i+1, nspcMem[j][i], nwtiMem[j][i], mwtiMem[j][i] * mwtiScale, swtiMem[j][i] * swtiScale, nwtoMem[j][i], mwtoMem[j][i] * mwtoScale, swtoMem[j][i] * swtoScale); printf("%2d: %2d=%2d(%6.2f %4.1f)+%2d(%6.2f %4.1f)\n", iOpp+1, nspcMem[j][iOpp], nwtiMem[j][iOpp], mwtiMem[j][iOpp] * mwtiScale, swtiMem[j][iOpp] * swtiScale, nwtoMem[j][iOpp], mwtoMem[j][iOpp] * mwtoScale, swtoMem[j][iOpp] * swtoScale); } if (numSigma0 <= 0) continue; for (i = 0; i < numSigma0; i++) { int cellIndex = cixMem[j][i]; if (cellIndex != oldCellIndex) { /* many sigma0s in each wvc..*/ int k, cI = cellIndex - 1; /* ..so don't repeat wvc info*/ oldCellIndex = cellIndex; printf("WVC ROW: %d (continued) WIND VECTOR CELL: %d\n", rnMem[j], cellIndex); /* AMSR info. Move to ROW INFO loop? Would uglify output */ printf(" AMSR[1-4]: atten uncert backscatterPower uncert rain quality\n"); for (k = 0; k < ZGRID; k++) { printf(" %.2f %.3f %.2f %.3f %.2f %05X", aaaMem[j][cI][k] * aaaScale, aaauMem[j][cI][k] * aaauScale, aabMem[j][cI][k] * aabScale, aabuMem[j][cI][k] * aabuScale, wqrMem[j][cI][k] * wqrScale, wqqMem[j][cI][k]); if (k % 2 == 1) putchar('\n'); } /* end of AMSR info */ printf(" Lat Lon Q Azi IncAng Sigma0 Atten Kp_alpha _beta _gamma Qual Mode Surf\n"); } /* NOTE kp_gamma should be %10.4e */ printf("%6.2f %6.2f %1d %6.2f %5.2f %6.2f %5.2f %5.3f %8.2e %8.2e x%03X %04X %03X\n", cltMem[j][i] * cltScale, clnMem[j][i] * clnScale, apMem[j][i], caMem[j][i] * caScale, ciMem[j][i] * ciScale, s0Mem[j][i] * s0Scale, samMem[j][i] * samScale, kaMem[j][i] * kaScale, kbMem[j][i] * kbScale, kgMem[j][i] * kgScale, sqfMem[j][i], smfMem[j][i], sfMem[j][i]); } } closeDTYPE(wrt); closeDTYPE(rn); closeDTYPE(ns); closeDTYPE(nspc); closeDTYPE(nwti); closeDTYPE(mwti); closeDTYPE(swti); closeDTYPE(nwto); closeDTYPE(mwto); closeDTYPE(swto); closeDTYPE(cix); closeDTYPE(clt); closeDTYPE(cln); closeDTYPE(ca); closeDTYPE(ci); closeDTYPE(s0); closeDTYPE(sam); closeDTYPE(ka); closeDTYPE(kb); closeDTYPE(kg); closeDTYPE(sqf); closeDTYPE(smf); closeDTYPE(sf); closeDTYPE(ap); closeDTYPE(aaa); closeDTYPE(aaau); closeDTYPE(aab); closeDTYPE(aabu); closeDTYPE(wqr); closeDTYPE(wqq); } /* process2() */ #undef WRTLEN #undef XGRID1 #undef XGRID2 #undef YGRID #undef ZGRID void getInput(int argc, char **argv, int32 *sd_id, int32 *v_id, int *row1, int *rowN, int *verbose) { extern int optind; char buf[256]; int c; int all = 0; char *x; /* if user didn't enter a number, x == NULL */ int maxRows; while ((c = getopt(argc, argv, "av")) != EOF) switch(c) { case 'a': all = 1; break; case 'v': *verbose = 1; break; } if (optind == argc) { /* exit if no input file */ fprintf(stderr, "usage: %s [-av] [ ]\n", argv[0]); fprintf(stderr, " -a: all: process entire file\n"); fprintf(stderr, " -v: verbose\n"); fprintf(stderr, " 1 <= row <= 1702\n"); /* must hardcode */ exit(-1); } *sd_id = SDstart(argv[optind], DFACC_RDONLY); checkHDF(*sd_id, "SDstart", argv[optind]); *v_id = Hopen(argv[optind], DFACC_RDONLY, 0); checkHDF(*v_id, "Hopen", argv[optind]); checkHDF(Vstart(*v_id), "Vstart", NULL); maxRows = getMetaInt(*sd_id, "l2a_actual_wvc_rows"); /* use _expected_? */ if (all) { *row1 = 1; *rowN = maxRows; } else { if (++optind < argc) strncpy(buf, argv[optind], 256); else strcpy(buf, ""); while (( !(*row1 = (int) strtol(buf, &x, 0)) && (x == buf)) || (*row1 < 1) || (*row1 > maxRows)) { printf("row1 (>=1 and <=%d): ", maxRows); fgets(buf, 256, stdin); } if (++optind < argc) strncpy(buf, argv[optind], 256); else strcpy(buf, ""); while (( !(*rowN = (int) strtol(buf, &x, 0)) && (x == buf)) || (*rowN < *row1) || (*rowN > maxRows)) { printf("rowN (>=%d and <=%d): ", *row1, maxRows); fgets(buf, 256, stdin); } } if (*verbose) printf("info: rows: %d to %d\n", *row1, *rowN); } /* getInput() */ /*^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^*/