/******************************************************************************* ** PROGRAM: hdf_subsetting.c **---------------------------------------------------------------------- ** PROGRAMMER: Jack Penick ** NASA Ames Research Center ** Moffett Field, CA 94035-1000 **---------------------------------------------------------------------- ** MODIFICATIONS **28-Feb-03 Made program accept MASTER HDF files ram ** Modifications to CheckforStdFileName() & output file build **25-Mar-03 Added subsetting by absolute line number option. jmg **05Aug2003 Operator dialog changes and general fixes ram ** **---------------------------------------------------------------------- ** DOCUMENTATION: See /atac/notes/mas50_1b/2002-??-hdf_subsetting **---------------------------------------------------------------------- ** ** D E S C R I P T I O N: ** The purpose of this program is to allow a user to subset one ** or more hdf files by choosing one of the following: ** ** (i) a begin and end GMT time, ** (ii) a start and end scan line number ** (iii) a start and end absolute line number 25Mar2003 ** ** The program now recognizes MASTER hdf files as a standard input ** hdf file name. 28Feb2003 ** ** The user may subset an hdf file as many times as he wishes ** choosing any of the three options. ** ** The user can subset hdf files that are already subsetted.05Aug2003 ** ** A subsetted hdf file can itself be subsetted ad infinitem UNTIL ** subsetting can only produce a file with the same output file name. ** ** No output file names can be the same. Care must be taken to use ** times, records that are sufficiently different so the new ** output file name is different. If the above care is not taken ** the original hdf file can be replaced by the new subsetted file. ** ** The user should begin by placing one or more hdf files to be ** subsetted in a directory. The hdf files in the directory can be ** a mix of flight linese from different flights or previously ** subsetted flight lines. The program should then be ** executed from this directory. However, it is usually convenient ** to place flight lines from the same flight in the same directory. ** ** The user will no longer have the option of naming the output file ** if the hdf file has a standard MAS or MASTER name. 05Aug2003 ** ** An output file with automatic name generation will be assigned a name ** identical to the input file name (which should follow the Nasa Ames ASF ** naming convention) except the time will be replaced by the time ** corresponding to the subset. ** ** In case of a non-standard hdf file name the user specifies the output ** file name. The time bracket corresponding to the data subset ** will be appended to the file name along with the extension .hdf. ** ** The output files will contain the global attributes from the input ** file except global attributes "CreationDate", "begin_date", "end_date", ** "producer_granule_id", and "granule_size" will be changed automatically ** as follows: ** ** CreationDate: the current date and time (note that "CompletionDate ** contains the date/time the input file was created). ** begin_date: flight date and subsetted time period as yyyymmdd hhmmss ** end_date: flight date and subsetted time period as yyyymmdd hhmmss ** producer_granule_id: the subset file's file name ** granule_size: the subset file's size in bytes ** ** After a subset file has been created, the user will be prompted to ** see if he/she wishes to change any of the other global attributes. ** ** The scientific data sets (SDS) in a subset file will be changed as ** follows: ** ** (i) an SDS with none of its dimensions corresponding to the number ** of scan lines will be unchanged from the original file ** (ii) an SDS with one of its dimensions corresponding to the number ** of scan lines will be changed from the original file to reflect ** the data for the scan lines in the subset file ** ** I N P U T: ** ** Initially, the first hdf file in the directory is displayed. If the ** hdf file has a standard MAS or MASTER hdf file name then the program ** continues. If the hdf file name is non-standard then the user is ** prompted for an output file name. ** ** - a series of tests determines if the file name ** conforms to the standard MAS/MASTER file name ** format. ** ** If the file name is not in the standard format, ** the operator is prompted for an output file name ** ** Next the operator is prompted for: ** ** - the subsetting method (where q|Q quits the program ** entirely, 0 means done subsetting the current hdf file, ** 1 (default) means subsetting by time, ** 2 means subsetting by scan line numbers, ** 3 means subsetting by absolute record numbers) ** - based upon the method chosen, the operator is prompted as ** follows: ** - 1: enter starting time in GMT as hhmmss ** enter ending time in GMT as hhmmss ** - 2: enter starting scan line number ** enter ending scan line number ** - 3: enter starting absolute line number * enter ending absolute line number ** ** - 0: the next (if any) hdf file name is displayed ** and the program resumes from the top by testing ** for a valid standard MAS/MASTER hdf file name ** ** After a subsetted hdf file is made the user/operator is prompted ** whether he/she wishes to change any of the global attributes. The ** default is yes. ** ** If the operator/user does elect to change global ** attributes then the user is prompted for any number ** of changes with these pair of prompts: ** ** Enter the name of the global attribute-- ** Enter the new value of the global attribute-- ** ** The user ends global attribute changes with 'quit' ** ** O U T P U T: ** Each time an hdf file is subsetted, the program creates an output ** file in the current directory corresponding to the subset time period. ** Initially, the file is assigned a temporary name. Upon completion, the ** temporary file is renamed and given the final output name. ** ** The output file name is described in the INPUT paragraph above. ** ** Language: ANSI C; ** ** Date: November 4, 2002 ** Revision Date: ** Revision: ** */ #define _POSIX_SOURCE 1 #include #include #include #include #include #define END_OF_FILE 1 #define FALSE 0 #define TRUE 1 #define MASTYPE 1 #define MASTERTYPE 2 #ifdef ASF # include "/atac/source/HDF/HDF41r5/include/mfhdf.h" #elif UNIX # include "/usr/local/hdf/include/mfhdf.h" #endif #ifdef WIN32 # include "c:/hdflib/HDF41r5/include/mfhdf.h" #endif #define MASL1B "MASL1B" /* Header for standard input file name */ #define MASTER "MASTER" /* Header for standard input file name */ #define MAXSubMethods 3 /* Maximum subsetting number */ #define MAXSDS 100 /* Maximum number of SDS's */ #define MASPOSof4THUNDERSCORE 25 /* Character position of 4th underscore in input file name */ #define MASTERPOSof4THUNDERSCORE 30 /* Character position of 4th underscore in input file name */ #define REQ_UNDERSCORES 6 /* Required number of underscores in input file name */ #define MASSTANDARDINPLEN1 42 /* Standard input file length MAS Regular */ #define MASTERSTANDARDINPLEN1 47 /* Standard input file length MASTER Regular */ #define MASSTANDARDINPLEN2 46 /* Standard input file length MAS Subset */ #define MASTERSTANDARDINPLEN2 51 /* Standard input file length MASTER Subset */ FILE *Listfp; /* pointer to file containing list of hdf files */ FILE *Outfp; /* pointer to output file */ FILE *TempOutfp; /* pointer to temporary output file */ int StandardInpFile; char begin_date[16]; /* the flight line begin date/time stamp */ char BegofOutName[30]; /* First 13 or 18 characters of input file name used to build output file name */ char DateofOutName[11]; /* Date from input file name used to build output file name */ char end_date[16]; /* the flight line end date/time stamp */ char EndofOutName[9]; /* End of input file name (precedes .hdf) used to build output file name */ char Fl_numofOutName[3]; /* Flight line number from input file name used to build output file name */ char hdfFile[100]; /* name of the hdf file now being processed */ char OutFileName[100]; /* name of the output file with extension */ char producer_granule_id[100]; /* global attribute containing file name */ char TempOutName[100] = "TempOutputFile.hdf"; /* name of the temporary hdf output file */ char UserOutName[100]; /* output file name (no extension) entered by the user */ char hdfListName[100]; /* contains hdf file list temporary file name */ int Dependent_List[MAXSDS]; /* holds a list of SDS indexes with at least one dimension dependent upon the number of scan lines (NumberOfScanlines) */ int Dependent_List_dim_index[MAXSDS]; /* holds the dimension index of the dimension dependent upon the number of scan lines (NumberOfScanlines) */ int fileType; /* 1 => MAS file, 2 => MASTER file */ int n_Dep_List; /* number of SDS's in list Dependent_List */ int n_Not_Dep_List; /* number of SDS's in list Not_Dependent_List */ int Not_Dependent_List[MAXSDS]; /* holds a list of SDS indexes with no dimensions dependent upon the number of scan lines (NumberOfScanlines) */ int SubsetMethod; /* value entered by the user as follows: 0 done subsetting, 1 subset by time bracket, 2 subset by scan line numbers, and 3 subset by absolute line numbers */ /* ********************Buffers for HDF data********************************* */ char8 *char8buf; /* buffer for char8 data */ int8 *int8buf; /* buffer for int8 data (unused) */ int16 *int16buf; /* buffer for int16 data */ int32 *int32buf; /* buffer for int32 data */ float32 *float32buf; /* buffer for float32 data */ float64 *float64buf; /* buffer for float64 data */ /* ************************************************************************* */ int32 dim_sizes[3]; /* used for dimension sizes of an SDS */ int32 edges[3]; /* the array containing the number of data elements in each dimension (e.g., data(716,785) where edges(1) = 716 and edges(2) = 785) */ int32 EndTime; /* the ending time for the subsetting interval */ int32 FromSLRec; /* the record number corresponding to the first scan line number to be processed */ int32 n_datasets; /* number of SDS's in the input file */ int32 n_file_attrs; /* number of global attributes in the input file */ int32 sdin_id; /* SD interface identifier for hdf input file */ int32 sdout_id; /* SD interface identifier for hdf output file */ int32 start[3]; /* start is the array containing the position at which the read will start for each dimension */ int32 StartTime; /* the starting time for the subsetting interval */ int32 status; /* zero indicates success when calling an HDF function; -1 indicates failure */ int32 stride[3]; /* the array specifying the interval between the values that will be read along each dimension */ int32 ToSLRec; /* the record number corresponding to the last scan line number to be processed */ long int granule_size; /* output file byte size assigned to global attribute granule_size */ /*============================================================================*/ /* these routines are LOCAL, listed after MAIN (in alphabetical order) */ void AskForOutputFileName (); /* get OUTPUT file name (no extension) */ void AskForSubsetMethod (); /* get subset method from user */ void BuildOutputName (); /* build the final output name */ void ChangeGlobAttrs (); /* get user changes to global attributes */ int CheckforStdFileName (); /* check input file for standard format */ void CreateSDSLists (); /* create two SDS lists, one dependent on NumberOfScanlines and one that is not */ void Delete_TempOutFile (); /* deletes TempOutputFile.hdf */ void DisplaySubsetMenu (); /* display the subset menu for the user */ int FindLastChar (); /* find the last occurrence of a character in a string */ void Generate_hdflist (); /* generate list of hdf files found in the current directory */ void GetGlobAttrVal (); /* get value for specified global attribute */ void GetRecRange (); /* get record indexes for subsetted range */ void Get_sds_info (); /* get the data type and number of values for the specified sds */ void Initialize_SDS_Arrays (); /* initialize SDS dimension arrays */ int32 typeToSize (); /* given an HDF data type,returns byte size */ void MakeOutputFile (); /* open and write global attributes to the temporary output file */ void Modify_GlobAttr_Date (); /* change the time frame for global attributes begin_date and end_date */ int Open_OutputFile (); /* establish linkage to output file */ void ParseInputFileName (); /* break the input file name into parts to build the output name */ void RenameOutputFile (); /* rename temporary output file using input file name or user-assigned name */ void ResetGlobalAttrs (); /* reset begin_date, end_date, producer_granule_id, and granule_size */ void Reset_start (); /* set the start array, given the rank, to the beginning of the array */ void SetCharGlobAttrVal (); /* set value for specified global attribute */ void SetCreationDate (); /* set global attribute CreationDate */ void Setgranule_size (); /* set value for global attrib granule_size */ void SetIntGlobAttrVal (); /* set value for specified global attribute */ void Setproducer_granule_id (); /* set value for global attribute producer_granule_id */ void StatusError (); /* reports FAIL result from call to HDF routine */ void Write_Changed_SDSets (); /* write SDSs dependent on number of scan lines (NumberOfScanlines) */ void Write_Unchanged_SDSets (); /* write SDSs not dependent on number of scan lines (NumberOfScanlines) */ main() { char data_set_name[100], global_attr_name[100]; char Response[100]; int32 sds_id; int32 attr_index, data_type, n_values; int32 NumberOfRecs; //BOOLEAN FirstFile, Found6; int Found6; long int new_granule_size, old_granule_size; /* put names of all files with the extension .hdf in a list */ printf("\nHDF_SUBSETTING Version 1.4 HDF4.1 06Aug2003\n\n"); Delete_TempOutFile(); /* deletes TempOutputFile.hdf */ /* call this before Generate_hdflist() */ Generate_hdflist(); if ((Listfp = fopen(hdfListName, "r")) == NULL) { printf("ERROR OPENING file %s !! \n",hdfListName); exit (EXIT_FAILURE); } rewind(Listfp); //FirstFile = TRUE; /* loop over all files in file list */ while (fgets(hdfFile, sizeof(hdfFile), Listfp) != NULL) { hdfFile[strlen(hdfFile) - 1] = '\0'; /* Open file as read-only */ sdin_id = SDstart ( hdfFile , DFACC_READ ); if (sdin_id == FAIL) { printf("\n\nHDF File %s Not Found, Unreadable, or Corrupted\n\n"); printf("Will try to open next file in list\n\n"); continue; } printf("\nOpening input file %s\n",hdfFile); /* get the input file's number of global attributes and SDS data sets */ status = SDfileinfo(sdin_id, &n_datasets, &n_file_attrs); if (status == FAIL) StatusError("SDfileinfo", "main", 1); /* break SDS's into two lists - one with dimensions dependent on NumberOfScanlines and one that is not */ CreateSDSLists(); StandardInpFile = CheckforStdFileName(hdfFile); if (StandardInpFile) { /* Have standard file name - break name apart to build output file name */ ParseInputFileName(); } else {/* Not a standard input file name; must enter an output file name for the first file in the list */ printf("The file %s is not a standard input file name.\n", hdfFile); AskForOutputFileName (hdfFile, UserOutName); printf("Output file name is %s\n", UserOutName); } while (TRUE) {/* loop until SubsetMethod equals zero */ DisplaySubsetMenu (); AskForSubsetMethod(); if (SubsetMethod == 0 || SubsetMethod == -1) break; /* done subsetting this file */ BuildOutputName(OutFileName, StartTime, EndTime); strcpy(TempOutName,OutFileName); printf("Subset file name is %s\n",OutFileName); MakeOutputFile (); printf("\nStart processing Scientific Data Sets not scan line dependent.\n"); Write_Unchanged_SDSets(); printf("End processing Scientific Data Sets not scan line dependent.\n\n"); printf("Start processing scan line dependent Scientific Data Sets.\n"); Write_Changed_SDSets(FromSLRec, ToSLRec); printf("End processing scan line dependent Scientific Data Sets.\n\n"); /* build an output file name after finishing with building new hdf file */ SetCreationDate(); /* reset global attributes begin_date and end_date for this hdf file */ /* note that you have to first get the attribute values, modify them based upon the start and end times and then reset the values in the new file */ strcpy(global_attr_name, "begin_date"); GetGlobAttrVal(global_attr_name, begin_date); Modify_GlobAttr_Date (StartTime, begin_date); SetCharGlobAttrVal(global_attr_name, begin_date); strcpy(global_attr_name, "end_date"); GetGlobAttrVal(global_attr_name, end_date); Modify_GlobAttr_Date (EndTime, end_date); SetCharGlobAttrVal(global_attr_name, end_date); /* BuildOutputName(OutFileName, StartTime, EndTime); */ /**********************************************************/ /* reset global attributes producer_granule_id and granule_size for this hdf file */ /* note that the producer_granule_id is the new file name. The granule_size is the file size in bytes */ Setproducer_granule_id (); /**********************************************************/ printf("Change any global attribute values [y/n;CR=y]? "); gets (Response); if (strcmp (Response, "") == 0 || strcmp (Response, "y") == 0 || strcmp (Response, "Y") == 0) ChangeGlobAttrs (); /* use this code to get the subsetted file's file size */ /* file size should be last function performed after closing the temporary output file */ Setgranule_size(); status = SDend(sdout_id); if (status == FAIL) StatusError("SDfileinfo", "main", 2); /* RenameOutputFile(TempOutName, OutFileName);*/ }/* END of subsetting loop for this file */ if( SubsetMethod == -1 ) break; /// FirstFile = FALSE; status = SDend(sdin_id); if (status == FAIL) StatusError("SDfileinfo", "main", 3); }/* loop back for another file */ /* Delete the temporary file holding the input file names */ /* strcpy(hdfListName,"\\rm hdf_filelist"); DEBUG*/ if (Outfp != NULL) { printf("main: close Listfp."); fclose(Listfp); } #ifdef UNIX strcpy(hdfListName,"\\rm hdf_filelist"); #endif #ifdef WIN32 strcpy(hdfListName,"del hdf_filelist"); #endif system(hdfListName); printf("\nDone Processing.\n"); exit (EXIT_SUCCESS); }/* END of main routine */ /*----------------------------------------------------------------------------*/ /*============================================================================*/ void AskForOutputFileName (char *InpFileName, char *UserOutName) {/* START AskForOutputFileName */ int FileNameNotOk=TRUE; char *chrptr; printf("You must enter an output file name.\n\n"); while (FileNameNotOk) {/* make sure user didn't enter a .hdf extension */ printf ("Enter output file name (no .hdf extension): "); gets (UserOutName); chrptr = strstr(UserOutName, ".hdf"); if (chrptr == NULL) FileNameNotOk = FALSE; else printf("Found .hdf in name - try again!\n"); } }/* END AskForOutputFileName */ /*----------------------------------------------------------------------------*/ /*============================================================================*/ void AskForSubsetMethod () {/* START AskForSubsetMethod */ int InputOk; int i; int32 data_type, FirstGMTtm_inFile, FirstScan_inFile, LastGMTtm_inFile, LastScan_inFile, Recs_inFile, sds_id; int32 EndRecNo, EndScanLine, StartRecNo, StartScanLine; int32 FirstAbs_inFile, LastAbs_inFile, StartAbsLine, EndAbsLine; char data_set_name[100], Response[100]; /* Ask user for the subsetting method */ Initialize_SDS_Arrays (); while(TRUE) {/* start of while statement */ InputOk = TRUE; printf ("Enter the subsetting method for this hdf file [CR=1]: "); gets (Response); if (strcmp (Response, "") == 0) { SubsetMethod = 1; } else { switch(Response[0]) { case 'q': case 'Q': SubsetMethod = -1; break; case '0': SubsetMethod = 0; break; case '1': SubsetMethod = 1; break; case '2': SubsetMethod = 2; break; case '3': SubsetMethod = 3; break; default: SubsetMethod = -999; InputOk = FALSE; break; } } if( InputOk ) break; } /* end while(TRUE) */ switch(SubsetMethod) {/* start of switch statement */ case -1: case 0: break; case 1: /* subset by GMT time */ /* Get the data type and number of values for GreenWichMeanTime */ strcpy(data_set_name, "GreenwichMeanTime"); Get_sds_info(sdin_id, data_set_name, &sds_id, &data_type, dim_sizes); start[0] = 0; status = SDreaddata(sds_id, start, stride, edges, (VOIDP) &FirstGMTtm_inFile); if (status == FAIL) StatusError("SDreaddata", "AskForSubsetMethod", 1); start[0] = dim_sizes[0] - 1; status = SDreaddata(sds_id, start, stride, edges, (VOIDP) &LastGMTtm_inFile); if (status == FAIL) StatusError("SDreaddata", "AskForSubsetMethod", 2); while (TRUE) {/* get start and end times */ printf ("Enter the starting time in GMT as hhmmss[min = %d]: ", FirstGMTtm_inFile/100); gets (Response); sscanf(Response, "%d", &StartTime); if( StartTime < FirstGMTtm_inFile/100 ) printf("Start Time = %d must be >= %d\n", StartTime, FirstGMTtm_inFile/100 ); else if( StartTime > LastGMTtm_inFile/100 ) printf("Start Time = %d must be <= %d\n", StartTime, LastGMTtm_inFile/100 ); else break; } while (TRUE) { printf ("Enter the ending time in GMT as hhmmss[max = %d]: ", LastGMTtm_inFile/100); gets (Response); sscanf(Response, "%d", &EndTime); if (EndTime > LastGMTtm_inFile/100) { printf("End Time = %d must be <= %d\n", EndTime, LastGMTtm_inFile/100); continue; } if (EndTime >= StartTime) break; printf("End Time = %d must be >= Start Time = %d\n", EndTime, StartTime); } /* adjust times to be HHMMSSFF */ StartTime *= 100; EndTime *= 100; GetRecRange (SubsetMethod, StartTime, EndTime); break; /* end of GMT case */ case 2: /* subset by scan line numbers */ strcpy(data_set_name, "ScanLineCounter"); Get_sds_info(sdin_id, data_set_name, &sds_id, &data_type, dim_sizes); start[0] = 0; status = SDreaddata(sds_id, start, stride, edges, (VOIDP) &FirstScan_inFile); if (status == FAIL) StatusError("SDreaddata", "AskForSubsetMethod", 3); start[0] = dim_sizes[0] - 1; status = SDreaddata(sds_id, start, stride, edges, (VOIDP) &LastScan_inFile); if (status == FAIL) StatusError("SDreaddata", "AskForSubsetMethod", 4); while (TRUE) {/* get beginning scan line number */ printf ("Enter the starting scan line number[min = %d]: ", FirstScan_inFile); gets (Response); sscanf(Response, "%d", &StartScanLine); if( StartScanLine < FirstScan_inFile ) printf("Start Scan Line = %d must be >= %d\n", StartScanLine, FirstScan_inFile); else if( StartScanLine > LastScan_inFile ) printf("Start Scan Line = %d must be <= %d\n", StartScanLine, LastScan_inFile ); else break; } while (TRUE) {/* get ending scan line number */ printf ("Enter the ending scan line number[max = %d]: ", LastScan_inFile); gets (Response); sscanf(Response, "%d", &EndScanLine); if (EndScanLine > LastScan_inFile) { printf("End Scan Line = %d must be <= %d\n", EndScanLine, LastScan_inFile); continue; } if (EndScanLine >= StartScanLine) break; printf("End Scan Line = %d must be >= Start Scan Line = %d\n", EndScanLine, StartScanLine); } GetRecRange (SubsetMethod, StartScanLine, EndScanLine); break; /* end of scan line case */ case 3: /* subset by absolute line numbers */ /*Note: absolute line number and scanline number are about the same thing.*/ strcpy(data_set_name, "ScanLineCounter"); Get_sds_info(sdin_id, data_set_name, &sds_id, &data_type, dim_sizes); start[0] = 0; status = SDreaddata(sds_id, start, stride, edges, (VOIDP) &FirstScan_inFile); if (status == FAIL) StatusError("SDreaddata", "AskForSubsetMethod", 5); start[0] = dim_sizes[0] - 1; status = SDreaddata(sds_id, start, stride, edges, (VOIDP) &LastScan_inFile); if (status == FAIL) StatusError("SDreaddata", "AskForSubsetMethod", 6); /* Convert scanline to absolute line # : 1st scanline maps to 1 */ FirstAbs_inFile = 1; LastAbs_inFile = dim_sizes[0]; while (TRUE) {/* get beginning absolute line number */ printf ("Enter the starting absolute line number[min = %d]: ",FirstAbs_inFile); gets (Response); sscanf(Response, "%d", &StartAbsLine); if( StartAbsLine < FirstAbs_inFile ) printf("Start Absolute Line = %d must be >= %d\n", StartAbsLine, FirstAbs_inFile); else if( StartAbsLine > LastAbs_inFile ) printf("Start Absolute Line = %d must be <= %d\n", StartAbsLine, LastAbs_inFile); else break; } while (TRUE) {/* get ending absolute line number */ printf ("Enter the ending Absolute line number[max = %d]: ", LastAbs_inFile); gets (Response); sscanf(Response, "%d", &EndAbsLine); if (EndAbsLine > LastAbs_inFile) { printf("End Absolute Line = %d must be <= %d\n", EndAbsLine, LastAbs_inFile); /*continue;*/ } if (EndAbsLine >= StartAbsLine) break; printf("End Absolute Line = %d must be >= Start Absolute Line = %d\n", EndAbsLine, StartAbsLine); } GetRecRange( SubsetMethod, FirstScan_inFile+StartAbsLine-1, FirstScan_inFile+EndAbsLine-1 ); break; /* end of absolute line case */ }/* end of switch statement */ }/* END AskForSubsetMethod */ /*----------------------------------------------------------------------------*/ /*============================================================================*/ void BuildOutputName (OutFileName, StartTime, EndTime) char *OutFileName; int32 StartTime, EndTime; {/* START BuildOutputName */ int Start_HHMMSS, End_HHMMSS; char TimeBracket[14]; Start_HHMMSS = StartTime/100; End_HHMMSS = EndTime/100; sprintf(TimeBracket, "%06d_%06d", Start_HHMMSS, End_HHMMSS); strcpy (OutFileName, ""); if (StandardInpFile) {/* build the name from the beginning string, time bracket and ending string */ strcat(OutFileName, BegofOutName); strcat(OutFileName, Fl_numofOutName); strcat(OutFileName, DateofOutName); strcat(OutFileName, TimeBracket); strcat(OutFileName, EndofOutName); } else {/* build the name from the user name and the time bracket and append .hdf */ strcat(OutFileName, UserOutName); strcat(OutFileName, "_"); strcat(OutFileName, TimeBracket); strcat(OutFileName, ".hdf"); } }/* END BuildOutputName */ /*----------------------------------------------------------------------------*/ /*============================================================================*/ void ChangeGlobAttrs () {/* START ChangeGlobAttrs */ char global_attr_name[100], global_attr_val[100]; int32 attr_index, data_type, n_values; int16 int16val; int32 int32val; float32 float32val; float64 float64val; while (TRUE) {/* loop until user types quit or QUIT */ printf("Enter the global attribute name (type quit when finished): "); gets (global_attr_name); if (strcmp(global_attr_name, "quit") == 0 || strcmp(global_attr_name, "QUIT") == 0) break; attr_index = SDfindattr(sdout_id, global_attr_name); if (attr_index == FAIL) { printf("Couldn't find %s -- the entry must be exact -- try again!\n", global_attr_name); continue; } /* get info for this global attribute */ status = SDattrinfo(sdout_id, attr_index, global_attr_name, &data_type, &n_values); if (status == FAIL) StatusError("SDattrinfo", "ChangeGlobAttrs", 1); if (data_type == 3 || data_type == 4) /* character data */ { char8buf = (char8 *)malloc (n_values*typeToSize(data_type)); status = SDreadattr(sdout_id, attr_index, char8buf); if (status == FAIL) StatusError("SDreadattr", "ChangeGlobAttrs", 2); char8buf[n_values] = '\0'; printf("Global attribute value = %s\n", char8buf); } else if (data_type == 24 || data_type == 25 ) {/* integer data */ status = SDreadattr(sdout_id, attr_index, &int32val); if (status == FAIL) StatusError("SDreadattr", "ChangeGlobAttrs", 3); printf("Global attribute value = %d\n", int32val); } else if (data_type == 22 || data_type == 23 ) {/* short integer data */ status = SDreadattr(sdout_id, attr_index, &int16val); if (status == FAIL) StatusError("SDreadattr", "ChangeGlobAttrs", 4); printf("Global attribute value = %d\n", int16val); } else if (data_type == 5) {/* float32 floating point data */ status = SDreadattr(sdout_id, attr_index, &float32val); if (status == FAIL) StatusError("SDreadattr", "ChangeGlobAttrs", 5); printf("Global attribute value = %f\n", float32val); } else if (data_type == 6) {/* float64 floating point data */ status = SDreadattr(sdout_id, attr_index, &float64val); if (status == FAIL) StatusError("SDreadattr", "ChangeGlobAttrs", 6); printf("Global attribute value = %f\n", float64val); } else {/* unrecognizable type */ printf("Unrecognizable type = %d\n", data_type); exit(EXIT_FAILURE); } printf("Enter new value for this global attribute: "); gets (global_attr_val); if (data_type == 3 || data_type == 4) /* character data */ { n_values = strlen(global_attr_val); status = SDsetattr(sdout_id, global_attr_name, data_type, n_values, (VOIDP) global_attr_val); if (status == FAIL) StatusError("SDsetattr", "ChangeGlobAttrs", 6); } else if (data_type == 24 || data_type == 25 ) /* integer data */ { sscanf(global_attr_val, "%d", &int32val); status = SDsetattr(sdout_id, global_attr_name, data_type, n_values, (VOIDP) &int32val); if (status == FAIL) StatusError("SDsetattr", "ChangeGlobAttrs", 7); } else if (data_type == 22 || data_type == 23 ) /* short integer data */ { sscanf(global_attr_val, "%hd", &int16val); status = SDsetattr(sdout_id, global_attr_name, data_type, n_values, (VOIDP) &int16val); if (status == FAIL) StatusError("SDsetattr", "ChangeGlobAttrs", 8); } else if (data_type == 5 ) /* float32 floating point data */ { sscanf(global_attr_val, "%f", &float32val); status = SDsetattr(sdout_id, global_attr_name, data_type, n_values, (VOIDP) &float32val); if (status == FAIL) StatusError("SDsetattr", "ChangeGlobAttrs", 9); } else if (data_type == 6 ) /* float64 data */ { sscanf(global_attr_val, "%f", &float64val); status = SDsetattr(sdout_id, global_attr_name, data_type, n_values, (VOIDP) &float64val); if (status == FAIL) StatusError("SDsetattr", "ChangeGlobAttrs", 10); } } } /* END ChangeGlobAttrs */ /*----------------------------------------------------------------------------*/ /*============================================================================*/ int CheckforStdFileName(char *InpFileName) {/* START CheckforStdFileName */ int Found6; int i, len, und_count; len = strlen(InpFileName); while (TRUE) {/* loop as long as standard input file name tests are TRUE (loop not needed here)*/ if ((strncmp(InpFileName, MASL1B, 6) == 0) && (len == MASSTANDARDINPLEN1)) { fileType = MASTYPE; // Regular MAS HDF file } else if ((strncmp(InpFileName, MASL1B, 6) == 0) && (len == MASSTANDARDINPLEN2)) { fileType = MASTYPE; // Subset MAS HDF file } else if ((strncmp(InpFileName, MASTER, 6) == 0) && (len == MASTERSTANDARDINPLEN1)) { fileType = MASTERTYPE; // Regular MASTER HDF file } else if ((strncmp(InpFileName, MASTER, 6) == 0) && (len == MASTERSTANDARDINPLEN2)) { fileType = MASTERTYPE; // Subset MASTER HDF file } else { printf("Filename: %s is not a standard HDF filename.\n", InpFileName); return (FALSE); } /* look for exactly 6 underscores in the file name excluding the termination characters Vxx.hdf */ und_count = 0; Found6 = TRUE; for (i=0; i TempOutputFile.hdf"); system(text); #ifdef UNIX strcpy(text,"\\rm TempOutputFile.hdf"); #endif #ifdef WIN32 strcpy(text,"del TempOutputFile.hdf"); #endif system(text); }/* END Delete_TempOutFile */ /*----------------------------------------------------------------------------*/ /*============================================================================*/ void Generate_hdflist() {/* BEGIN Generate_hdflist */ /* this function builds a unix command of the form ls *.hdf redirected to a newly created file named hdf_filelist */ char text[100]; /* The file "hdf_filelist" is used as a temporary file to hold the set of *.hdf input files */ strcpy(hdfListName,"hdf_filelist"); #ifdef UNIX strcpy(text,"ls *.hdf"); #endif #ifdef WIN32 strcpy(text,"dir /b *.hdf"); #endif strcat(text," > hdf_filelist"); system(text); }/* END Generate_hdflist */ /*----------------------------------------------------------------------------*/ /*============================================================================*/ /*retrieve value for one of the global attributes to be automatically changed */ void GetGlobAttrVal(char *AttrName, char *AttrVal) {/* START GetGlobAttrVal */ int32 attr_index; attr_index = SDfindattr(sdin_id, AttrName); status = SDreadattr(sdin_id, attr_index, AttrVal); if (status == FAIL) StatusError("SDreadattr", "GetGlobAttrVal", 1); }/* END GetGlobAttrVal */ /*----------------------------------------------------------------------------*/ /*============================================================================*/ void GetRecRange (int32 SubsetMethod, int32 Start, int32 End) {/* START GetRecRange */ int i; int32 data_type, sds_id; char data_set_name[100]; if (SubsetMethod == 1) /* subset by time range */ strcpy(data_set_name, "GreenwichMeanTime"); else /* subset by scan line */ strcpy(data_set_name, "ScanLineCounter"); Initialize_SDS_Arrays(); Get_sds_info(sdin_id, data_set_name, &sds_id, &data_type, dim_sizes); edges[0] = dim_sizes[0]; int32buf = (int32 *)malloc (dim_sizes[0]*typeToSize(data_type)); status = SDreaddata (sds_id, start, NULL, edges, (VOIDP) int32buf); if (status == FAIL) StatusError("SDreaddata", "GetRecRange", 1); for (i = 0; i < dim_sizes[0]; i ++) if (Start <= int32buf[i]) { FromSLRec = i; break; } for (i = 0; i < dim_sizes[0]; i ++) if (End <= int32buf[i]) { ToSLRec = i; break; } if (SubsetMethod == 2 || SubsetMethod == 3) /* absolute rec nmbs are */ /* already scan line numbers */ {/* get start and end times for building file name if subsetting by scan line line numbers or absolute record numbers */ strcpy(data_set_name, "GreenwichMeanTime"); Get_sds_info(sdin_id, data_set_name, &sds_id, &data_type, dim_sizes); status = SDreaddata (sds_id, start, NULL, edges, (VOIDP) int32buf); if (status == FAIL) StatusError("SDreaddata", "GetRecRange", 2); StartTime = int32buf[FromSLRec]; EndTime = int32buf[ToSLRec]; } free(int32buf); return; }/* END GetRecRange */ /*----------------------------------------------------------------------------*/ /*============================================================================*/ void Get_sds_info (int32 sd_id, char *sds_name, int32 *sds_id, int32 *data_type, int32 *dim_sizes) {/* START Get_sds_info */ /* Get the data type and number of values for the specified sds */ int32 n_attrs, rank, sds_index, status; /* get the index of the data set with the name specified in SDS_name */ sds_index = SDnametoindex(sd_id, sds_name); /* select the data set corresponding to the returned index */ *sds_id = SDselect(sd_id, sds_index); /* --- sds_id = internal bookkeeping id */ /* --- sds_name = name of data set */ /* --- rank = number of dimensions */ /* --- dim_sizes = size of dimensions */ /* --- data_type = type of data */ /* --- n_attrs = Number of Attributes */ status = SDgetinfo ( *sds_id, sds_name, &rank, dim_sizes, data_type, &n_attrs ); if (status == FAIL) StatusError("SDgetinfo", "Get_sds_info", 1); }/* END Get_sds_info */ /*----------------------------------------------------------------------------*/ /*============================================================================*/ void Initialize_SDS_Arrays () {/* START Initialize_SDS_Arrays Initialize start,stride edges arrays for hyperslab data read This will handle SDS"S of single, double, or triple dimensions start is the array containing the position at which the read will start for each dimension stride is the array specifying the interval between the values that will be read along each dimension edges is the array containing the number of data elements in each dimension (e.g., data(716,785) where edges(1) = 716 and edges(2) = 785) */ int i; /* initialize arrays */ for (i = 0; i < 3; i++) { start[i] = 0; edges[i] = 1; stride[i] = 1; dim_sizes[i] = 0; } }/* END Initialize_SDS_Arrays */ /*----------------------------------------------------------------------------*/ /*============================================================================*/ void MakeOutputFile () {/* BEGIN MakeOutputFile */ char global_attr_name[100]; int32 attr_index, data_type, n_values; int16 int16val; int32 int32val; float32 float32val; float64 float64val; /* Open output file with read and write access */ sdout_id = SDstart ( TempOutName , DFACC_CREATE ); if (sdout_id == FAIL) {/* can't open the temporary output file - abort the program */ printf ("Problem opening the temporary output file - program aborted!\n"); exit (EXIT_FAILURE); } /* write all of the global attributes to the output file */ for (attr_index = 0; attr_index < n_file_attrs; attr_index++) { /* get info for this global attribute */ status = SDattrinfo(sdin_id, attr_index, global_attr_name, &data_type, &n_values); if (status == FAIL) StatusError("SDattrinfo", "MakeOutputFile", 1); if (data_type == 3 || data_type == 4) /* character data */ { char8buf = (char8 *)malloc (n_values*typeToSize(data_type)); status = SDreadattr(sdin_id, attr_index, char8buf); if (status == FAIL) StatusError("SDreadattr", "MakeOutputFile", 2); /* char8buf[n_values] = '\0'; */ status = SDsetattr(sdout_id, global_attr_name, data_type, n_values, (VOIDP) char8buf); if (status == FAIL) StatusError("SDsetattr", "MakeOutputFile", 3); } else if (data_type == 24 || data_type == 25 ) {/* integer data */ status = SDreadattr(sdin_id, attr_index, &int32val); if (status == FAIL) StatusError("SDreadattr", "MakeOutputFile", 4); status = SDsetattr(sdout_id, global_attr_name, data_type, n_values, (VOIDP) &int32val); if (status == FAIL) StatusError("SDsetattr", "MakeOutputFile", 5); } else if (data_type == 22 || data_type == 23 ) {/* short integer data */ status = SDreadattr(sdin_id, attr_index, &int16val); if (status == FAIL) StatusError("SDreadattr", "MakeOutputFile", 6); status = SDsetattr(sdout_id, global_attr_name, data_type, n_values, (VOIDP) &int16val); if (status == FAIL) StatusError("SDsetattr", "MakeOutputFile", 7); } else if (data_type == 5) {/* floating point data */ status = SDreadattr(sdin_id, attr_index, &float32val); if (status == FAIL) StatusError("SDreadattr", "MakeOutputFile", 8); status = SDsetattr(sdout_id, global_attr_name, data_type, n_values, (VOIDP) &float32val); if (status == FAIL) StatusError("SDsetattr", "MakeOutputFile", 9); } else if (data_type == 6) {/* float64 data */ status = SDreadattr(sdin_id, attr_index, &float64val); if (status == FAIL) StatusError("SDreadattr", "MakeOutputFile", 10); status = SDsetattr(sdout_id, global_attr_name, data_type, n_values, (VOIDP) &float64val); if (status == FAIL) StatusError("SDsetattr", "MakeOutputFile", 11); } else {/* invalid data type */ printf("Unrecognizable data type = %d\n", data_type); exit (EXIT_FAILURE); } }/* end of global attribute for loop */ }/* END MakeOutputFile */ /*----------------------------------------------------------------------------*/ /*============================================================================*/ /* Leave the date intact for the global attributes begin_date and end_date but change the times to match the times for the hdf subset */ void Modify_GlobAttr_Date (int Bracket_Time, char *GlobAttr_Date) {/* BEGIN Modify_GlobAttr_Date */ char text[20]={0}; int int_HHMMSS; char char_HHMMSS[8]; int_HHMMSS = Bracket_Time/100; sprintf(char_HHMMSS, "%06d", int_HHMMSS); strncpy(text, GlobAttr_Date, 9); strcat(text, char_HHMMSS); strcpy( GlobAttr_Date, text); }/* END Modify_GlobAttr_Date */ /*----------------------------------------------------------------------------*/ /*============================================================================*/ int Open_OutputFile (Outfp, OutName) FILE *(*Outfp); char *OutName; {/* START Open_OutputFile */ if ((*Outfp = fopen (OutName, "w")) == NULL) /* can't OPEN the file for OUTPUT */ return (FALSE); return (TRUE); }/* END Open_OutputFile */ /*----------------------------------------------------------------------------*/ /*============================================================================*/ void ParseInputFileName () {/* START ParseInputFileName */ int nlast_underscore; /* get last part of output file name from the first file name */ nlast_underscore = FindLastChar(hdfFile,'_'); strcpy(EndofOutName, hdfFile+nlast_underscore); /* get the first 13 or characters as the beginning of the output file name - note that the flight line number changes for each file */ /* get the next two characters as the flight line number */ /* the next 10 characters consist of an underscore, the flight date and a trailing underscore */ memset( BegofOutName, '\0', sizeof(BegofOutName) ); memset( Fl_numofOutName, '\0', sizeof(Fl_numofOutName) ); memset( DateofOutName, '\0', sizeof(DateofOutName) ); if( fileType == MASTYPE ) { strncat(BegofOutName, hdfFile, 13); strncat(Fl_numofOutName, hdfFile+13, 2); strncat(DateofOutName, hdfFile+15, 10); } else { strncat(BegofOutName, hdfFile, 18); strncat(Fl_numofOutName, hdfFile+18, 2); strncat(DateofOutName, hdfFile+20, 10); } }/* END ParseInputFileName */ /*----------------------------------------------------------------------------*/ /*============================================================================*/ void RenameOutputFile(char *FromName, char *ToName) /* rename the file using input file name or a user-assigned name */ {/* START RenameOutputFile */ char text[100]; /* rename the temporary output file name to its final name */ #ifdef UNIX strcpy (text, "\\mv "); #endif #ifdef WIN32 strcpy (text, "ren "); #endif strcat (text, FromName); strcat (text, " "); strcat (text, ToName); system (text); }/* END RenameOutputFile */ /*----------------------------------------------------------------------------*/ /*============================================================================*/ void Reset_start(int32 rank) {/* START Reset_start */ int i; for (i = 0; i < rank; i++) { start[i] = 0; } }/* END Reset_start */ /*----------------------------------------------------------------------------*/ /*============================================================================*/ /* set new value for one of the global attributes to be automatically changed */ void SetCharGlobAttrVal (char *AttrName, char *AttrVal) {/* START SetCharGlobAttrVal */ int32 attr_index, data_type, n_values; /* get info for this file attribute */ attr_index = SDfindattr(sdin_id, AttrName); status = SDattrinfo(sdin_id, attr_index, AttrName, &data_type, &n_values); if (status == FAIL) StatusError("SDattrinfo", "SetCharGlobAttrVal", 1); /* printf("SetCharGlobAttrVal: AttrVal = %s\n",AttrVal); printf("SetCharGlobAttrVal: AttrVal = %s\n",(VOIDP)AttrVal); */ status = SDsetattr(sdout_id, AttrName, data_type, n_values, (VOIDP) AttrVal); if (status == FAIL) StatusError("SDsetattr", "SetCharGlobAttrVal", 2); }/* END SetCharGlobAttrVal */ /*----------------------------------------------------------------------------*/ /*============================================================================*/ void SetCreationDate() {/* START SetCreationDate */ time_t currentTime; char creation[30], item[13]; int ix, jx, sx; /* get time as long integer */ time ( ¤tTime ); ix = sizeof (creation); jx = strftime(creation, ix, "%d-%b-%Y %H:%M:%S", localtime( ¤tTime)); /* note that jx should = 20 */ sx = strlen( creation ); strcpy (item, "CreationDate"); status = SDsetattr(sdout_id, item, DFNT_CHAR8,sx,creation); if (status == FAIL) StatusError("SDsetattr", "SetCreationDate", 1); }/* END SetCreationDate */ /*----------------------------------------------------------------------------*/ /*============================================================================*/ /* set value for global attribute granule_size (i.e., file size in bytes) */ void Setgranule_size() {/* START Setgranule_size */ FILE *Dumfp; /* dummy pointer to get file size */ char global_attr_name[100]; /* use this code to get the subsetted file's file size */ /* file size should be last function performed after closing the temporary output file */ if ((Dumfp = fopen(TempOutName, "r")) == NULL) { printf("ERROR OPENING file %s !! \n",TempOutName); exit (EXIT_FAILURE); } fseek(Dumfp, 0L, SEEK_END); granule_size = ftell(Dumfp); strcpy(global_attr_name, "granule_size"); SetIntGlobAttrVal(global_attr_name, &granule_size); }/* END Setgranule_size */ /*----------------------------------------------------------------------------*/ /*============================================================================*/ /* set new value for one of the global attributes to be automatically changed */ void SetIntGlobAttrVal (char *AttrName, long int *AttrVal) {/* START SetIntGlobAttrVal */ int32 attr_index, data_type, n_values; /* get info for this file attribute */ attr_index = SDfindattr(sdin_id, AttrName); status = SDattrinfo(sdin_id, attr_index, AttrName, &data_type, &n_values); if (status == FAIL) StatusError("SDattrinfo", "SetIntGlobAttrVal", 1); status = SDsetattr(sdout_id, AttrName, data_type, n_values, (VOIDP) AttrVal); if (status == FAIL) StatusError("SDsetattr", "SetIntGlobAttrVal", 2); status = SDreadattr(sdout_id, attr_index, AttrVal); if (status == FAIL) StatusError("SDreadattr", "SetIntGlobAttrVal", 3); }/* END SetIntGlobAttrVal */ /*----------------------------------------------------------------------------*/ /*============================================================================*/ /* set value for global attribute producer_granule_id (i.e.,output file name) */ void Setproducer_granule_id() {/* START Setproducer_granule_id */ char global_attr_name[100]; int32 attr_index, data_type, n_values; attr_index = SDfindattr(sdin_id, "producer_granule_id"); /* get info about the file attribute producer_granule_id */ strcpy(global_attr_name, "producer_granule_id"); status = SDattrinfo(sdin_id, attr_index, global_attr_name, &data_type, &n_values); if (status == FAIL) StatusError("SDattrinfo", "Setproducer_granule_id", 1); /* number of characters in new file name */ n_values = strlen(OutFileName); strcpy(producer_granule_id, OutFileName); status = SDsetattr(sdout_id, global_attr_name, data_type, n_values, (VOIDP) producer_granule_id); if (status == FAIL) StatusError("SDsetattr", "Setproducer_granule_id", 2); }/* END Setproducer_granule_id */ /*----------------------------------------------------------------------------*/ /*============================================================================*/ /* Error reporting routine - an aid for a programmer */ void StatusError (char *HDF_FuncName, char *ProgramName, int SeqNo) {/* START StatusError */ printf("HDF function %s returned failure; called from %s(%d)\n", HDF_FuncName, ProgramName, SeqNo); printf("Program terminated.\n"); exit (EXIT_FAILURE); }/* END StatusError */ /*----------------------------------------------------------------------------*/ /*============================================================================*/ /* This routine returns the HDF data type, given the data type flag( as defined in Table 2-F of the UG41r3 manual. */ int32 typeToSize(int32 type) {/* START typeToSize */ int32 size; switch(type) { case 3: size = sizeof(uchar8); break; case 4: size = sizeof(char8); break; case 5: size = sizeof(float32); break; case 6: size = sizeof(float64); break; case 20: size = sizeof(int8); break; case 21: size = sizeof(uint8); break; case 22: size = sizeof(int16); break; case 23: size = sizeof(uint16); break; case 24: size = sizeof(int32); break; case 25: size = sizeof(uint32); break; default: size = sizeof(char); break; } return (size); }/* END typeToSize */ /*----------------------------------------------------------------------------*/ /*============================================================================*/ /* write SDS's dependent on the number of scan lines */ void Write_Changed_SDSets(int32 FromSLRec, int32 ToSLRec) {/* START Write_Changed_SDSets */ int i, j, jj; int32 arraysize, attr_index, data_type, n_attrs, n_values, rank, sdsin_id, sdsout_id, sds_index, status, NumberOfRecs, dim_id; int32 data_type2, n_attrs2, n_values2; char attr_name[100], sds_name[100], dim_name[100]; Initialize_SDS_Arrays(); for (i = 0; i < n_Dep_List; i++) {/* get the index of the SDS and write subsetted values to the output file */ sds_index = Dependent_List[i]; /* select the SDS */ sdsin_id = SDselect(sdin_id, sds_index); status = SDgetinfo (sdsin_id, sds_name, &rank, dim_sizes, &data_type, &n_attrs ); if (status == FAIL) StatusError("SDgetinfo", "Write_Changed_SDSets", 1); arraysize = 1; /* set start and edges to read the desired subset from the array */ NumberOfRecs = ToSLRec - FromSLRec +1; for (j = 0; j < rank; j++) { if (j == Dependent_List_dim_index[i]) { dim_sizes[j] = NumberOfRecs; start[j] = FromSLRec; edges[j] = NumberOfRecs; arraysize = arraysize*edges[j]; } else { edges[j] = dim_sizes[j]; arraysize = arraysize*dim_sizes[j]; } } /* create the SDS for the output file */ sdsout_id = SDcreate(sdout_id, sds_name, data_type, rank, dim_sizes); /* set the SD dimension names from the input hdf file SD */ for (jj = 0; jj < rank; jj++) { dim_id = SDgetdimid ( sdsin_id, jj ); status = SDdiminfo(dim_id, dim_name, &n_values2, &data_type2, &n_attrs2); if (status == FAIL) StatusError("SDdiminfo", "Write_Changed_SDSets", 21); dim_id = SDgetdimid( sdsout_id, jj ); status = SDsetdimname( dim_id, dim_name ); if (status == FAIL) StatusError("SDsetdimname", "Write_Changed_SDSets", 22); } if (data_type == 3 || data_type == 4) {/* character data */ char8buf = (char8 *)malloc (arraysize*typeToSize(data_type)); status = SDreaddata (sdsin_id, start, NULL, edges, (VOIDP) char8buf); if (status == FAIL) StatusError("SDreaddata", "Write_Changed_SDSets", 2); Reset_start(rank); status = SDwritedata(sdsout_id, start, NULL, edges, (VOIDP) char8buf); if (status == FAIL) StatusError("SDwritedata", "Write_Changed_SDSets", 3); free(char8buf); } else if (data_type == 24 || data_type == 25 ) {/* integer data */ int32buf = (int32 *)malloc (arraysize*typeToSize(data_type)); status = SDreaddata (sdsin_id, start, NULL, edges, (VOIDP) int32buf); if (status == FAIL) StatusError("SDreaddata", "Write_Changed_SDSets", 4); Reset_start(rank); status = SDwritedata(sdsout_id, start, NULL, edges, (VOIDP) int32buf); if (status == FAIL) StatusError("SDwritedata", "Write_Changed_SDSets", 5); free(int32buf); } else if (data_type == 22 || data_type == 23 ) {/* short integer data */ int16buf = (int16 *)malloc (arraysize*typeToSize(data_type)); status = SDreaddata (sdsin_id, start, NULL, edges, (VOIDP) int16buf); if (status == FAIL) StatusError("SDreaddata", "Write_Changed_SDSets", 6); if (strcmp(sds_name, "CalibratedData") == 0) { printf("Finished reading CalibratedData dataset.\n"); } Reset_start(rank); status = SDwritedata(sdsout_id, start, NULL, edges, (VOIDP) int16buf); if (status == FAIL) StatusError("SDwritedata", "Write_Changed_SDSets", 7); if (strcmp(sds_name, "CalibratedData") == 0) { printf("Finished writing CalibratedData dataset.\n"); } free(int16buf); } else if (data_type == 5) {/* float32 floating point data */ float32buf = (float32 *)malloc (arraysize*typeToSize(data_type)); status = SDreaddata (sdsin_id, start, NULL, edges, (VOIDP) float32buf); if (status == FAIL) StatusError("SDreaddata", "Write_Changed_SDSets", 8); Reset_start(rank); status = SDwritedata(sdsout_id, start, NULL, edges, (VOIDP) float32buf); if (status == FAIL) StatusError("SDwritedata", "Write_Changed_SDSets", 9); free(float32buf); } else if (data_type == 6) {/* float64 floating point data */ float64buf = (float64 *)malloc (arraysize*typeToSize(data_type)); status = SDreaddata (sdsin_id, start, NULL, edges, (VOIDP) float64buf); if (status == FAIL) StatusError("SDreaddata", "Write_Changed_SDSets", 10); Reset_start(rank); status = SDwritedata(sdsout_id, start, NULL, edges, (VOIDP) float64buf); if (status == FAIL) StatusError("SDwritedata", "Write_Changed_SDSets", 11); free(float64buf); } else {/* unrecognizable type */ printf("Unrecognizable type = %d\n", data_type); exit(EXIT_FAILURE); } /* write any attribute info to output file */ if (n_attrs > 0) {/* get the SDS's attributes name and value and write to output file */ for (attr_index = 0; attr_index < n_attrs; attr_index++) { status = SDattrinfo(sdsin_id, attr_index, attr_name, &data_type, &n_values); if (status == FAIL) StatusError("SDattrinfo", "Write_Changed_SDSets", 10); if (data_type == 3 || data_type == 4) {/* character data */ char8buf = (char8 *)malloc (n_values*typeToSize(data_type)); status = SDreadattr(sdsin_id, attr_index, char8buf); if (status == FAIL) StatusError("SDreadattr", "Write_Changed_SDSets", 11); status = SDsetattr(sdsout_id, attr_name, data_type, n_values, (VOIDP)char8buf); if (status == FAIL) StatusError("SDsetattr", "Write_Changed_SDSets", 12); free(char8buf); } else if (data_type == 24 || data_type == 25 ) {/* integer data */ int32buf = (int32 *)malloc (n_values*typeToSize(data_type)); status = SDreadattr(sdsin_id, attr_index, int32buf); if (status == FAIL) StatusError("SDreadattr", "Write_Changed_SDSets", 13); status = SDsetattr(sdsout_id, attr_name, data_type, n_values, (VOIDP)int32buf); if (status == FAIL) StatusError("SDsetattr", "Write_Changed_SDSets", 14); free(int32buf); } else if (data_type == 22 || data_type == 23 ) {/* short integer data */ int16buf = (int16 *)malloc (n_values*typeToSize(data_type)); status = SDreadattr(sdsin_id, attr_index, int16buf); if (status == FAIL) StatusError("SDreadattr", "Write_Changed_SDSets", 15); status = SDsetattr(sdsout_id, attr_name, data_type, n_values, (VOIDP)int16buf); if (status == FAIL) StatusError("SDsetattr", "Write_Changed_SDSets", 16); free(int16buf); } else if (data_type == 5) {/* float32 floating point data */ float32buf = (float32 *)malloc (n_values*typeToSize(data_type)); status = SDreadattr(sdsin_id, attr_index, float32buf); if (status == FAIL) StatusError("SDreadattr", "Write_Changed_SDSets", 17); status = SDsetattr(sdsout_id, attr_name, data_type, n_values, (VOIDP)float32buf); if (status == FAIL) StatusError("SDsetattr", "Write_Changed_SDSets", 18); free(float32buf); } else if (data_type == 6) {/* float64 floating point data */ float64buf = (float64 *)malloc (n_values*typeToSize(data_type)); status = SDreadattr(sdsin_id, attr_index, float64buf); if (status == FAIL) StatusError("SDreadattr", "Write_Changed_SDSets", 19); status = SDsetattr(sdsout_id, attr_name, data_type, n_values, (VOIDP)float64buf); if (status == FAIL) StatusError("SDsetattr", "Write_Changed_SDSets", 20); free(float64buf); } }/* end of for loop */ }/* end of if n_attrs > 0 */ status = SDendaccess(sdsin_id); if (status == FAIL) StatusError("SDendaccess", "Write_Changed_SDSets", 21); status = SDendaccess(sdsout_id); if (status == FAIL) StatusError("SDendaccess", "Write_Changed_SDSets", 22); }/* end of for loop */ }/* END Write_Changed_SDSets */ /*----------------------------------------------------------------------------*/ /*============================================================================*/ /* write SDS's not dependent on the number of scan lines */ void Write_Unchanged_SDSets() {/* START Write_Unchanged_SDSets */ int i, j, jj; int32 arraysize, attr_index, data_type, n_attrs, n_values, rank, sdsin_id, sdsout_id, sds_index, dim_id, status; int32 data_type2, n_attrs2, n_values2; char attr_name[100], sds_name[100], dim_name[100]; Initialize_SDS_Arrays(); for (i = 0; i < n_Not_Dep_List; i++) {/* get the index of the SDS and write its values to the output file */ sds_index = Not_Dependent_List[i]; sdsin_id = SDselect(sdin_id, sds_index); status = SDgetinfo (sdsin_id, sds_name, &rank, dim_sizes, &data_type, &n_attrs ); if (status == FAIL) StatusError("SDgetinfo", "Write_Unchanged_SDSets", 1); arraysize = 1; for (j = 0; j < rank; j++) { edges[j] = dim_sizes[j]; arraysize = arraysize*dim_sizes[j]; } sdsout_id = SDcreate(sdout_id, sds_name, data_type, rank, dim_sizes); /* set the SD dimension names from the input hdf file SD */ for (jj = 0; jj < rank; jj++) { dim_id = SDgetdimid ( sdsin_id, jj ); status = SDdiminfo(dim_id, dim_name, &n_values2, &data_type2, &n_attrs2); if (status == FAIL) StatusError("SDdiminfo", "Write_Unchanged_SDSets", 21); dim_id = SDgetdimid( sdsout_id, jj ); status = SDsetdimname( dim_id, dim_name ); if (status == FAIL) StatusError("SDsetdimname", "Write_Unchanged_SDSets", 22); } if (data_type == 3 || data_type == 4) {/* character data */ char8buf = (char8 *)malloc (arraysize*typeToSize(data_type)); status = SDreaddata (sdsin_id, start, NULL, edges, (VOIDP) char8buf); if (status == FAIL) StatusError("SDreaddata", "Write_Unchanged_SDSets", 2); status = SDwritedata(sdsout_id, start, NULL, edges, (VOIDP) char8buf); if (status == FAIL) StatusError("SDwritedata", "Write_Unchanged_SDSets", 3); free(char8buf); } else if (data_type == 24 || data_type == 25 ) {/* integer data */ int32buf = (int32 *)malloc (arraysize*typeToSize(data_type)); status = SDreaddata (sdsin_id, start, NULL, edges, (VOIDP) int32buf); if (status == FAIL) StatusError("SDreaddata", "Write_Unchanged_SDSets", 4); status = SDwritedata(sdsout_id, start, NULL, edges, (VOIDP) int32buf); if (status == FAIL) StatusError("SDwritedata", "Write_Unchanged_SDSets", 5); free(int32buf); } else if (data_type == 22 || data_type == 23 ) {/* short integer data */ int16buf = (int16 *)malloc (arraysize*typeToSize(data_type)); status = SDreaddata (sdsin_id, start, NULL, edges, (VOIDP) int16buf); if (status == FAIL) StatusError("SDreaddata", "Write_Unchanged_SDSets", 6); status = SDwritedata(sdsout_id, start, NULL, edges, (VOIDP) int16buf); if (status == FAIL) StatusError("SDwritedata", "Write_Unchanged_SDSets", 7); free(int16buf); } else if (data_type == 5) {/* float32 floating point data */ float32buf = (float32 *)malloc (arraysize*typeToSize(data_type)); status = SDreaddata (sdsin_id, start, NULL, edges, (VOIDP) float32buf); if (status == FAIL) StatusError("SDreaddata", "Write_Unchanged_SDSets", 8); status = SDwritedata(sdsout_id, start, NULL, edges, (VOIDP) float32buf); if (status == FAIL) StatusError("SDwritedata", "Write_Unchanged_SDSets", 9); free(float32buf); } else if (data_type == 6) {/* float64 floating point data */ float64buf = (float64 *)malloc (arraysize*typeToSize(data_type)); status = SDreaddata (sdsin_id, start, NULL, edges, (VOIDP) float64buf); if (status == FAIL) StatusError("SDreaddata", "Write_Unchanged_SDSets", 10); status = SDwritedata(sdsout_id, start, NULL, edges, (VOIDP) float64buf); if (status == FAIL) StatusError("SDwritedata", "Write_Unchanged_SDSets", 11); free(float64buf); } else {/* unrecognizable type */ printf("Unrecognizable type = %d\n", data_type); exit(EXIT_FAILURE); } /* write any attribute info to output file */ if (n_attrs > 0) {/* get the SDS's attributes name and value and write to output file */ for (attr_index = 0; attr_index < n_attrs; attr_index++) { status = SDattrinfo(sdsin_id, attr_index, attr_name, &data_type, &n_values); if (status == FAIL) StatusError("SDattrinfo", "Write_Unchanged_SDSets", 10); if (data_type == 3 || data_type == 4) {/* character data */ char8buf = (char8 *)malloc (n_values*typeToSize(data_type)); status = SDreadattr(sdsin_id, attr_index, char8buf); if (status == FAIL) StatusError("SDreadattr", "Write_Unchanged_SDSets", 11); status = SDsetattr(sdsout_id, attr_name, data_type, n_values, (VOIDP)char8buf); if (status == FAIL) StatusError("SDsetattr", "Write_Unchanged_SDSets", 12); free(char8buf); } else if (data_type == 24 || data_type == 25 ) {/* integer data */ int32buf = (int32 *)malloc (arraysize*typeToSize(data_type)); status = SDreadattr(sdsin_id, attr_index, int32buf); if (status == FAIL) StatusError("SDreadattr", "Write_Unchanged_SDSets", 13); status = SDsetattr(sdsout_id, attr_name, data_type, n_values, (VOIDP)int32buf); if (status == FAIL) StatusError("SDsetattr", "Write_Unchanged_SDSets", 14); free(int32buf); } else if (data_type == 22 || data_type == 23 ) {/* short integer data */ int16buf = (int16 *)malloc (arraysize*typeToSize(data_type)); status = SDreadattr(sdsin_id, attr_index, int16buf); if (status == FAIL) StatusError("SDreadattr", "Write_Unchanged_SDSets", 15); status = SDsetattr(sdsout_id, attr_name, data_type, n_values, (VOIDP)int16buf); if (status == FAIL) StatusError("SDsetattr", "Write_Unchanged_SDSets", 16); free(int16buf); } else if (data_type == 5) {/* float32 floating point data */ float32buf = (float32 *)malloc (arraysize*typeToSize(data_type)); status = SDreadattr(sdsin_id, attr_index, float32buf); if (status == FAIL) StatusError("SDreadattr", "Write_Unchanged_SDSets", 17); status = SDsetattr(sdsout_id, attr_name, data_type, n_values, (VOIDP)float32buf); if (status == FAIL) StatusError("SDsetattr", "Write_Unchanged_SDSets", 18); free(float32buf); } else if (data_type == 6) {/* float64 floating point data */ float64buf = (float64 *)malloc (arraysize*typeToSize(data_type)); status = SDreadattr(sdsin_id, attr_index, float64buf); if (status == FAIL) StatusError("SDreadattr", "Write_Unchanged_SDSets", 19); status = SDsetattr(sdsout_id, attr_name, data_type, n_values, (VOIDP)float64buf); if (status == FAIL) StatusError("SDsetattr", "Write_Unchanged_SDSets", 20); free(float64buf); } }/* end of for loop */ }/* end of if n_attrs > 0 */ status = SDendaccess(sdsin_id); if (status == FAIL) StatusError("SDendaccess", "Write_Unchanged_SDSets", 21); status = SDgetinfo (sdsout_id, sds_name, &rank, dim_sizes, &data_type, &n_attrs ); if (status == FAIL) StatusError("SDgetinfo", "Write_Unchanged_SDSets", 22); /**************************************************************************** if (data_type == 5) { float32buf = (float32 *)malloc (arraysize*typeToSize(data_type)); status = SDreaddata (sdsout_id, start, NULL, edges, (VOIDP) float32buf); if (status == FAIL) StatusError("SDreaddata", "Write_Unchanged_SDSets", 23); free(float32buf); } else if (data_type == 6) { float64buf = (float64 *)malloc (arraysize*typeToSize(data_type)); status = SDreaddata (sdsout_id, start, NULL, edges, (VOIDP) float64buf); if (status == FAIL) StatusError("SDreaddata", "Write_Unchanged_SDSets", 24); free(float64buf); } ****************************************************************************/ status = SDendaccess(sdsout_id); if (status == FAIL) StatusError("SDendaccess", "Write_Unchanged_SDSets", 22); }/* end of for loop */ }/* END Write_Unchanged_SDSets */ /*----------------------------------------------------------------------------*/