/*************************************************************************/ /* */ /* maps_co5x5_read.c */ /* */ /* Sample read program for the following MAPS data sets: */ /* MAPS_SRL1_COSEC_HDF */ /* MAPS_SRL2_COSEC_HDF */ /* MAPS_OSTA3_COSEC_HDF */ /* */ /* Version 1.0 October 04, 1996 */ /* Version 2.0 February 03, 1999 */ /* HDF Library: Version 3.3r4 */ /* HDF Library: Version 4.1r2 */ /* */ /* NOTE: Make sure that all paths match to your system. Please review */ /* the makefile, Makefile.maps, before compiling the code. */ /* */ /* This is an interactive program. It prompts the user for all */ /* of the information necessary to read the MAPS data sets. */ /* */ /* CONTACT: If you have any questions, please contact us! */ /* EOSDIS Langley DAAC User and Data Services */ /* Mail Stop 157D */ /* NASA Langley Research Center */ /* Hampton, Virginia 23681-2199 */ /* */ /* Telephone: (757)864-8656 FAX: (757)864-8807 */ /* E-mail: support-asdc@earthdata.nasa.gov */ /* */ /* 02-03-1999: Modified the code to comply with the Langley DAAC */ /* requirements. */ /* */ /*************************************************************************/ #ifndef MAPS #include /* C library */ #include /* C library */ #include /* C library */ #include /* HDF file needs to be in local directory */ #define MAXCHAR 132 /* Max # of characters in string */ #define MAXLEN 4000 #define DFACC_read 1 #define FIRST 1 #define LISTSIZE 23 /* One greater than the number of parameters in file */ #define MAXLNG 23 /* Max number of labels */ #define MAXRANK 2 /* Max number of dimensions */ #define MAXLABLEN 256 /* Max string label length */ #define MAXDATA 43200 /* Max # of columns (3600) times Max # of data */ /* values per parameter (12) times number of */ /* parameters (22)*/ #ifdef PROTOTYPE void Greetings(void); void Assign_rpt_name (char *, char *); void Read_data_descript (char *, FILE *, char *); void Get_data_types (FILE *, char *, int [LISTSIZE]); int Print_data_info (FILE *, char *, int [LISTSIZE]); void Read_data(FILE *, char *, int [LISTSIZE], int); int main(void); #else void Greetings(); /* Declare function */ void Assign_rpt_name(); /* Declare function */ void Read_data_descript(); /* Declare function */ void Get_data_types(); /* Declare function */ int Print_data_info(); /* Declare function */ void Read_data(); /* Declare function */ int main(); #endif #endif #ifdef PROTOTYPE int main() #else int main() #endif { /* Begin main function */ int num_events = 0; /* Stores the number of events */ int dataitems[LISTSIZE]; /* Store reference number of labels */ char yesno[2], /* Stores yes/no number for printing header information */ hdfname[MAXCHAR], /* Stores input data file name */ info[MAXCHAR], /* Used as a temporary string buffer */ outfile[MAXCHAR]; /* Stores output report file name */ FILE *ofp; /* Pointer for output report file */ /**********************************/ /* Main function begins */ /**********************************/ (void) Greetings(); printf("\n\n"); printf("Enter the input data file name. This code assumes file is in your current\n"); printf("working directory.\n"); scanf("%s",info); strcpy(hdfname,info); printf("\n\n"); printf("In your output report file, do you want to include the \n"); printf("file description? Enter 1 for yes and 2 for no .... "); scanf("%s",info); strcpy(yesno,info); printf("\n\n"); /* Create an output report file name. */ (void) Assign_rpt_name (outfile, hdfname); ofp = fopen (outfile, "wt"); if (ofp == NULL) { printf("*********************************************\n"); printf("* ERROR - Not able to open the output file, *\n"); printf("* %s. *\n",outfile); printf("* Program has terminated. *\n"); printf("*********************************************\n"); exit ( -1 ); } /* Reads the data file description from HDF file and writes */ /* it out to the output report file. */ (void) Read_data_descript (hdfname, ofp, yesno); /* Allows the user to select the parameters to be written */ /* out to the output report file. */ (void) Get_data_types (ofp, hdfname, dataitems); /* Print selected data information and output to report file */ num_events = Print_data_info (ofp, hdfname, dataitems); printf("\n\n"); printf("There are %d records/number of events in the HDF file.\n",num_events); printf("NOTE: This will take some time to write out the report file.\n"); /* Read selected data and output to report file */ Read_data (ofp, hdfname, dataitems, num_events); fclose (ofp); /* Print out End of File message onto the screen. */ printf("\n\n"); printf("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\n"); printf("@ @\n"); printf("@ This program has completed and has created an output report @\n"); printf("@ file called %s in your current @\n",outfile); printf("@ working directory. @\n"); printf("@ @\n"); printf("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\n"); printf("\n\n"); } /* End main function */ /**************************************************************************** * * * FUNCTION: Greetings * * * * PURPOSE: To print a banner at the beginning of the execution to inform * * the users what version of this read program he or she is * * working with. * * * ****************************************************************************/ #ifdef PROTOTYPE void Greetings(void) #else void Greetings() #endif { /* Begin Greetings function */ printf("\n\n\n"); printf(" *****************************************************\n"); printf(" * *\n"); printf(" * MAPS Read Program Version 2.0 *\n"); printf(" * *\n"); printf(" * *\n"); printf(" * This sample read program currently reads the *\n"); printf(" * following data sets: *\n"); printf(" * *\n"); printf(" * MAPS_SRL1_COSEC_HDF *\n"); printf(" * MAPS_SRL2_COSEC_HDF *\n"); printf(" * MAPS_OSTA3_COSEC_HDF *\n"); printf(" * *\n"); printf(" * *\n"); printf(" * 02-03-1999 *\n"); printf(" *****************************************************\n"); printf("\n\n\n"); } /* End of Greetings function */ /**************************************************************************** * * * FUNCTION: Assign_rpt_name * * * * PURPOSE: Creates a report filename for the appropriate experiment. * * * ****************************************************************************/ #ifdef PROTOTYPE void Assign_rpt_name (char *outfile, char *hdfname) #else void Assign_rpt_name (outfile, hdfname) char *outfile, *hdfname; #endif { strcpy(outfile,"./"); strcat(outfile,hdfname); strcat(outfile,".rpt"); } /* End of function, Assign_rpt_name. */ /**************************************************************************** * * * FUNCTION: Read_data_descript * * * * PURPOSE: Opens input HDF file, reads the file description, and prints * * the file description to the report file if the user desires to * * have it printed. * * * ****************************************************************************/ #ifdef PROTOTYPE void Read_data_descript (char *hdfname, FILE * ofp, char *yesno) #else void Read_data_descript (hdfname, ofp, yesno) char *hdfname; FILE *ofp; char *yesno; #endif { int c, dfile, i, iret, linecount; char id[MAXLEN], filedesc[MAXLEN]; /* Open HDF input file using HDF user commands. */ dfile = Hopen(hdfname, DFACC_read, 0); if (dfile == NULL) { printf("************************************************\n"); printf("* ERROR - Not able to open the HDF input file. *\n"); printf("* Program has terminated. *\n"); printf("************************************************\n"); exit ( -1 ); } /* Get file id from HDF input file. */ iret = DFANgetfid (dfile, id, MAXLEN, FIRST); if (iret == -1) { printf("************************************************\n"); printf("* ERROR - Not able to get file id from the HDF *\n"); printf("* input data file. *\n"); printf("* Program has terminated. *\n"); printf("************************************************\n"); exit ( -1 ); } /* Get file description from HDF input file. */ iret = DFANgetfds(dfile, filedesc, MAXLEN, FIRST); if (iret == -1) { printf("**************************************************\n"); printf("* ERROR - Error in getting file description from *\n"); printf("* the HDF input data file. *\n"); printf("* Program has terminated. *\n"); printf("**************************************************\n"); exit ( -1 ); } else { if (atoi(yesno) == 1) { fprintf(ofp,"%s\n%s\n\n","File ID: ",id); fprintf(ofp,"%s\n\n","File description is as follows:"); linecount = 0; c = 70; /* the letter F - just any letter */ while (c != 0) { i = 0; while (i<=80) { fprintf(ofp,"%c",filedesc[i+linecount]); if (filedesc[i+linecount] == 0) i = 85; else i++; } if (i == 85) c = 0; else linecount = linecount+81; fprintf(ofp,"\n"); } fprintf(ofp,"\n\n"); } } } /* End of function, Read_data_descript. */ /**************************************************************************** * * * FUNCTION: Get_data_types * * * * PURPOSE: This function prints out all of the parameters within the data * * file (HDF) and allows the user to select which parameters he * * or she wishes to see. * * * ****************************************************************************/ #ifdef PROTOTYPE void Get_data_types(FILE * ofp, char *hdfname, int dataitems[LISTSIZE]) #else void Get_data_types(ofp, hdfname, dataitems) FILE *ofp; char *hdfname; int dataitems[LISTSIZE]; #endif { int nlabels, itemnumber, startpos; int cnt0, cnt1, cnt2, cnt3; uint16 reflist[LISTSIZE]; int index[LISTSIZE]; int numchoices; char labellist[LISTSIZE * MAXLNG + 1]; char datalabellist[MAXLNG][MAXLABLEN]; int datalabelval[MAXLNG]; int datacount; char yesno[2]; int iret, numdims, numtype; int32 dimsize[MAXRANK]; char datalabel[MAXLABLEN], dataunit[MAXLABLEN], datafmt[MAXLABLEN], datacoord[3]; char dimlab[2][40], dimunt[30], dimfmt[5]; char all_flag[2]; TRYAGAIN: cnt0 = 0; cnt2 = 1, cnt3 = 0; startpos = 1; numchoices = 0; /* initialize dataitems array */ for (cnt1 = 0; cnt1 < LISTSIZE; cnt1++) { dataitems[cnt1] = 65535; reflist[cnt1] = 0; index[cnt1] = 0; }/* end of for */ /* Pertains to the selection of parameters by users */ for (cnt1 = 0; cnt1 "); scanf("%s",all_flag); if ((strcmp(all_flag,"Y")==0) || (strcmp(all_flag,"y")==0) ) { for (cnt1=1; cnt1 <= numchoices; cnt1++) { itemnumber = index[cnt1 - 1]; dataitems[cnt0] = reflist[itemnumber]; datalabelval[cnt0] = itemnumber; cnt0++; } } else { /* print labels in groups of 15 to ask for users input */ for (cnt1 = 0; cnt1 < numchoices; ++cnt1) { if (cnt2 == 1) { itemnumber = 1; printf(" Item Number Label\n\n"); }/* end of if */ /* This line prints the short name of the label on the screen. */ printf(" %3d %s\n", cnt1 + 1, labellist + (index[cnt1] * MAXLNG)); /* This line prints the long name of the label on the screen. */ /* This line is currently commented out. */ /* printf(" %3d %s\n", cnt1 + 1, datalabellist[cnt1]); */ cnt2++; if (cnt1 == numchoices - 1) { printf("\nPlease choose the numbers of the data items you wish to output to the screen.\n"); printf("Enter -1 to continue to next list or 0 to begin processing.\n\n"); while (itemnumber != 0 || itemnumber != -1) { /* read the user's selection */ scanf("%d", &itemnumber); /* If user's selection equals -1, then */ /* display 15 more parameters. */ if (itemnumber == -1) break; /* If the user's selection equals 0, then */ /* the user has finished his/her selection */ /* and we can return to the calling routine. */ if (itemnumber == 0) { /* At this time, the code will print out the list of parameters */ /* the user has selected. At this time, the user has the option */ /* to continue with the program or re-enter in his selection. */ printf("\n\n\n"); printf("The following is a list of the parameters you have selected:\n\n"); for (datacount = 0; datacount < cnt0; datacount++) { printf(" %d %s\n",datalabelval[datacount]+1,datalabellist[datalabelval[datacount]]); } printf("\n\n"); printf("Would you like to make a new selection of these parameters or\n"); printf("would you like this program to exit the selection process and\n"); printf("begin retrieving the data? \n"); printf("Enter Y for yes to retrieve data or N for no make a new selection. --> "); scanf("%s",yesno); if (strcmp(yesno,"N")==0) goto TRYAGAIN; else if (strcmp(yesno,"n")==0) goto TRYAGAIN; else printf("\n\n"); dataitems[cnt0] = 0; return; } else { /* If the user's selection is not -1 or 0, */ /* then they have selected a parameter and */ /* we will add the reference number for */ /* that parameter to the dataitems array. */ itemnumber = index[itemnumber - 1]; dataitems[cnt0] = reflist[itemnumber]; datalabelval[cnt0] = itemnumber; cnt0++; }/* end of else */ }/* end of while */ cnt2 = 1; }/* end of if (cnt1 == numchoices - 1) */ }/* end of for */ }/* end of outside else */ dataitems[cnt0] = 0; return; }/* end of Getdatatypes */ /**************************************************************************** * * * FUNCTION: Print_data_info * * * * PURPOSE: This function prints out all of the label information for those* * parameters that the user has just selected. * * * ****************************************************************************/ #ifdef PROTOTYPE int Print_data_info(FILE * ofp, char *hdfname, int dataitems[LISTSIZE]) #else int Print_data_info(ofp, hdfname, dataitems) FILE *ofp; char *hdfname; int dataitems[LISTSIZE]; #endif { int winst[MAXRANK], windims[MAXRANK], winend[MAXRANK]; int dims[MAXRANK], ref_dims[MAXRANK]; int i, j, k, l, iret, c, linecount; int numdims, rnumdims; int32 dimsize[MAXRANK], numtype; int index, event; char datalabel[MAXLABLEN], dataunit[MAXLABLEN]; char datafmt[MAXLABLEN], datacoord[3]; char dimlab[2][40], dimunt[30], dimfmt[5]; float data[MAXDATA]; float ref_data[MAXDATA]; double max, min; int num_events; /* according to reference numbers go to next data set */ printf("\n\nReading labels....\n"); k = 0; while (dataitems[k] != 0) { iret = DFSDreadref(hdfname, dataitems[k]); if (iret == -1) { printf("***********************************************\n"); printf("* ERROR - Not able to get specified reference *\n"); printf("* number %d. *\n",dataitems[k]); printf("***********************************************\n"); exit ( -1 ); } /* get dimensions of SDS */ iret = DFSDgetdims(hdfname, &numdims, dimsize, MAXRANK); if (iret != 0) { printf("***************************************\n"); printf("* ERROR - Not able to get dimensions. *\n"); printf("* Program has terminated. *\n"); printf("***************************************\n"); exit ( -1 ); } else { fprintf(ofp, "\n\nDimensions are %d by %d\n\n", dimsize[0], dimsize[1]); num_events = dimsize[1]; }/* end of else */ /* Get number type */ iret = DFSDgetNT(&numtype); if (iret != 0) { printf("**********************************************\n"); printf("* ERROR - Not able to get DFSDgetNT numtype. *\n"); printf("* Program has terminated. *\n"); printf("**********************************************\n"); exit ( -1 ); } /* Print message indicating there are no dimension scales */ fprintf(ofp, "*** There are no dimension scales for this data set. ***\n\n"); /* set up slice dimensions */ /* no subsetting required */ winst[0] = 1; winst[1] = 1; windims[0] = dimsize[0]; windims[1] = dimsize[1]; dims[0] = windims[0]; dims[1] = windims[1]; /* Print dimension attributes */ for (l = 1; l <= numdims; l++) { iret = DFSDgetdimstrs(l, dimlab[l - 1], dimunt, dimfmt); if (iret != 0) { printf("*************************************************\n"); printf("* ERROR - Not able to get dimension attributes. *\n"); printf("* Program has terminated. *\n"); printf("*************************************************\n"); exit ( -1 ); } else { fprintf(ofp, "%s%s\n%s%s\n%s%s\n\n", "Description : ", dimlab[l - 1], "Units : ", dimunt, "Format : ", dimfmt); } } /* get data attributes */ iret = DFSDgetdatastrs(datalabel, dataunit, datafmt, datacoord); if (iret != 0) { printf("********************************************\n"); printf("* ERROR - Not able to get data attributes. *\n"); printf("* Program has terminated. *\n"); printf("********************************************\n"); exit ( -1 ); } else { /* Print the data attributes */ fprintf(ofp, "Data attributes are as follows:\n\n"); /* fprintf(ofp, "%s%s\n%s%s\n%s%s\n\n", "Label : ", datalabel, "Units : ", dataunit, "Format: ", datafmt); */ fprintf(ofp, "Label : "); linecount = 0; c = 70; /* just picked any letter - letter F */ while (c != 0) { i = 0; while (i <= 70) { fprintf(ofp,"%c",datalabel[i+linecount]); if (datalabel[i+linecount] == 0) i = 75; else i++; } if (i == 75) c = 0; else linecount = linecount + 71; fprintf(ofp,"\n"); } fprintf(ofp, "\n%s%s\n%s%s\n\n", "Units : ", dataunit, "Format: ", datafmt); } /* Get data max and min values */ iret = DFSDgetrange((void *)&max, (void *)&min); if (iret == -1) { printf("*******************************************\n"); printf("* ERROR - Not able to get 32-bit data max *\n"); printf("* and min values. *\n"); printf("* Program has terminated. *\n"); printf("*******************************************\n"); exit ( -1 ); } else { /* print the data max and mins */ fprintf(ofp, "%s%lf\n", "Data Max: ", max); fprintf(ofp, "%s%.9lf\n\n", "Data Min: ", min); } k++; fprintf(ofp, "\n\n"); }/* end of while */ return (num_events); } /* End of function, Print_data_info. */ /**************************************************************************** * * * FUNCTION: Read_data * * * * PURPOSE: This function prints out all of the data for those parameters * * the user has just selected. * * * ****************************************************************************/ #ifdef PROTOTYPE void Read_data(FILE * ofp, char *hdfname, int dataitems[LISTSIZE], int num_events) #else void Read_data(ofp, hdfname, dataitems, num_events) FILE *ofp; char *hdfname; int dataitems[LISTSIZE]; int num_events; #endif { int32 winst[MAXRANK], windims[MAXRANK], winend[MAXRANK]; int32 dims[MAXRANK], ref_dims[MAXRANK]; int ii, i,j, k, l, iret; int numdims, rnumdims; int32 dimsize[MAXRANK], numtype; int index, event,firstevent; char datalabel[MAXLABLEN], dataunit[MAXLABLEN]; char datafmt[MAXLABLEN], datacoord[3]; char dimlab[2][40], dimunt[30], dimfmt[5]; double data[MAXDATA]; float ref_data[MAXDATA]; float max, min; char stringdate[7], stringtime[7]; char temp[50]; /* According to reference numbers go to next data set */ printf("\n\nReading data, Please wait ...\n"); /* print out message for data arrangement */ fprintf(ofp, "Data are printed from left to right in event order.\n\n\n\n"); printf("Data are printed from left to right in event order.\n\n\n\n"); for (ii=0; ii