/* * Copyright(c) 2011, Space Science and Engineering Center, UW-Madison * Refer to "McIDAS Software Acquisition and Distribution Policies" * in the file mcidas/data/license.txt */ /**** $Id: viiradir.c,v 1.26 2021/09/02 15:12:50 russd Tst $ ****/ /* ** Name: ** viiradir : ADDE directory server for HDF5 VIIRS data. ** ** Interface: ** int ** main(int argc, char *argv[]) ** ** Input: ** argc : argument count ** argv : argument vector ** ** Input and Output: ** none ** ** Output: ** none ** ** Return values: ** 0 : success ** < 0 : failure, enumerated by text message returned to client ** ** Remarks: ** ** Categories: ** ADDE server */ /* C include files */ #include #include #include #include /* McIDAS include files */ #include "mcidas.h" #include "mcidasp.h" #include "m0glue.h" #include "m0arg.h" #include "hdf5.h" int GetModsCards (int *, int, float, float, float, float, int *, int); int h5getinfo(char *, char *); void do_dtype(hid_t); void do_dset(hid_t, char *); void do_link(hid_t, char *); void scan_group(hid_t, char *); void do_attr(hid_t); void scan_attrs(hid_t); void do_plist(hid_t); #define MAX_NAME 1024 int main ( int argc, char **argv ) #define DIR_SIZE 64 #define IMAGE_STARTING_LINE 1 #define IMAGE_STARTING_ELEMENT 1 #define IMAGE_FILE_VERSION 4 #define MAX_STR_LEN 80 #define MAX_ATR_LEN 80 #define MAX_RECORDS 20000 #define ACTUAL_CENTER_POINT_COORDS_KEY "Predicted Central Point Co-ordinates(degrees) Latitude,Longitude" #define VHRR_IMAGE_GROUP_NAME "/VHRR/Image Data" #define IR_BAND_NAME "VHRR_TIR" #define VIS_BAND_NAME "VHRR_VIS" #define WV_BAND_NAME "VHRR_WV" #define BAND_RESOLUTION_KEY "X_Resolution(km)" #define NAV_RES 2 #define VHRR_GEO_GROUP_NAME "/VHRR/Geo-Location" /* SATELLITE */ #define VIIRS_NPP_SDR 320 #define VIIRS_JP1_SDR 321 #define VIIRS_JP2_SDR 322 #define VIIRS_JP3_SDR 323 #define VIIRS_JP4_SDR 324 #define VIIRS_NPP_EDR 325 #define VIIRS_JP1_EDR 326 #define VIIRS_JP2_EDR 327 #define VIIRS_JP3_EDR 328 #define VIIRS_JP4_EDR 329 #define MIN_BAND 1 #define MAX_BANDS 22 { const char *dum; /* dummy for arg fetchers */ char *dataset; /* dataset name */ char *file_name; /* name of HDF file */ char *nav_name; /* name of NAV HDF file */ char *char_band; /* band name string */ char *cPos; /* check for position = ALL */ char ds_name[MAX_NAME]; int bufsize; int bDate; /* beginning date */ int bTime; /* beginning time */ int eDate; /* ending date */ int eTime; /* ending time */ int bPos, ePos; int bSS, eSS; int checkSS = 0; int checkTime = 0; int checkDay = 0; int checkBand = 0; int dummy = 9999; char *group, *cAux; char *cBand; int file_ptr; int rc; int j,k; int i, ii; const char server[] = {"VIIRADIR"}; char *request = NULL; servacct requestBlock = {0}; int auxFlag = 0; int allBandFlag = 0; int trace = 0; int transaction = 0; int user = 0; /* int VIIRS_AUX_OFFSET = 1280; */ int VIIRS_AUX_OFFSET = 1648; /* 22 slots with 10 values per slot */ int VIIRS_CAL_OFFSET = 768; int VIIRS_NAV_OFFSET = 256; int VIIRS_AUX_ENTRIES = 2; int VIIRS_AUX_SIZE; char cards[24000]; char *cdate; char *ctime; char *cday; char *chms; char *cname; char *comment; /* ADDE dataset comment */ char *format; /* ADDE dataset format */ char *info; /* ADDE dataset info */ char *mask; /* file mask */ char *type; /* ADDE dataset type */ int SDR = 1; int EDR = 2; int Iband = 1; int Mband = 2; int Dband = 3; char **imglist; char **navlist; int N_imglist; int N_navlist; int N_tokens; int I_token; int I_file; int ALL; int PASS; int directory[64]; int hiBound; int iPos; int jPos; int fPos; int nPos; int lastPos; int length; int loBound; int num_band; int num_cards; int send_byte_total; int listCount; int imageCount; int listCyd[MAX_RECORDS]; int listHms[MAX_RECORDS]; int listSec[MAX_RECORDS]; int listSat[MAX_RECORDS]; int listFor[MAX_RECORDS]; int listTyp[MAX_RECORDS]; int listBnd[MAX_RECORDS]; int listImg[MAX_RECORDS]; int listNav[MAX_RECORDS]; int listPos[MAX_RECORDS]; int listLfac[MAX_RECORDS]; int listEfac[MAX_RECORDS]; float listLnav[MAX_RECORDS]; float listEnav[MAX_RECORDS]; float listRes[MAX_RECORDS]; int listLin[MAX_RECORDS]; int listEle[MAX_RECORDS]; int sentCount; int file_handle; int image_line_resolution; int image_element_resolution; int image_format; int image_dmy; int image_cyd; int image_iyd; int image_hms; int image_sec; int image_sat; int image_typ; int image_pos; int image_geo_line_size; int image_geo_element_size; int image_band; int image_nav; int image_list; int bands[MAX_BANDS]; int rt_flag; int file_name_type; /* file naming convention 1=DACC, 2=Direct broadcast */ int D,M,Y; int status; int bband, eband; int request_n_bands; int request_bands[22]; int NBADLAT; int NBADLON; int VALID_IMAGE; int VALID_NAV; int INVERT; int jj; int NavFound; int NavLine; int SAVELIN; int SAVEELE; float SAVECPLAT; float SAVECPLON; float listCPLat[MAX_RECORDS]; float listCPLon[MAX_RECORDS]; float image_geo_latitude; float image_geo_longitude; float image_geo_line_resolution; float image_geo_element_resolution; float image_km_resolution; float *Lat_array; float *Lon_array; /* HDF5 related declaration */ hid_t file_id; hid_t group_id; hid_t Lat_id; hid_t Lon_id; hid_t space_id; hid_t type_id; herr_t H5_status; hsize_t Lat_dims[5]; char NAV_GROUP_NAME[256]; char LATITUDE_DATASET_NAME[256]; char LONGITUDE_DATASET_NAME[256]; int Lat_ndims; McArgSyntax filesyntax = {"/_.", "=", ";", "{\"", "}", "'", "'", NULL, "X", " "}; static char trace_string[500]; /* tracing text */ /* stuff I added */ /* variable for "ALL" */ (void) memcpy(&ALL, "ALL ", 4 ); M0swbyt4(&ALL, 1); /* initialize local server */ rc = M0InitLocalServer(server, argc, argv, &requestBlock, &request); /* see if logging should be done, determined by hidden TRACE= flag */ trace = M0IsTraceSet(request); (void *)sprintf(trace_string, "%s V0.7A VIIRADIR Trace Level = %d", server, trace); M0sxtrce( trace_string ); /* move the request back into the comm block */ (void) strncpy(requestBlock.text, request, sizeof(requestBlock.text)); /* fill in the data type */ transaction = atoi(argv[5]); (void) memcpy(requestBlock.transaction, &transaction, sizeof(requestBlock.transaction)); /* fill in the user */ user = atoi(argv[3]); (void) memcpy(requestBlock.user, &user, sizeof(requestBlock.user)); /* initialize accounting */ m0vserv_(&requestBlock); /* get dataset info */ rc = M0sxdatasetinfo(request, &group, &dataset, &type, &format, &mask, &info, &comment, &loBound, &hiBound, &rt_flag); sprintf(trace_string, "%s group = %s", server,group); M0sxtrce( trace_string ); sprintf(trace_string, "%s dataset = %s", server,dataset); M0sxtrce( trace_string ); sprintf(trace_string, "%s type = %s", server,type); M0sxtrce( trace_string ); sprintf(trace_string, "%s format = %s", server,format); M0sxtrce( trace_string ); sprintf(trace_string, "%s mask = %s", server,mask); M0sxtrce( trace_string ); sprintf(trace_string, "%s info = %s", server,info); M0sxtrce( trace_string ); sprintf(trace_string, "%s comment = %s", server,comment); M0sxtrce( trace_string ); sprintf(trace_string, "%s rt flag = %d", server,rt_flag); M0sxtrce( trace_string ); /* log the request text string */ sprintf(trace_string, "%s Request = %s", server,request); M0sxtrce( trace_string ); /* DATASET name */ sprintf(trace_string,"%s Dataset = %s", server,group); M0sxtrce( trace_string ); /* BAND sort condition */ num_band = Mcargnum(0, "BAN.D"); if( num_band > 0 ) { int i; checkBand = 1; rc = Mcargstr(0,"BAN.D",1,"ALL",(const char **) &cBand); if (strncmp(cBand, "ALL", 3) == 0) { sprintf(trace_string,"%s Looking for ALL bands", server); M0sxtrce( trace_string ); allBandFlag = 1; request_n_bands = 22; for( i=0; i<22; i++ ) request_bands[i] = i+1; } else { /* read the beginning band */ rc = Mcargint(0,"BAN.D",1,0,MIN_BAND,MAX_BANDS,&bband,&dum); if( rc < 0 ) { (void)strcpy(requestBlock.errormsg, "Invalid band number specified"); requestBlock.returncode = -34; M0sxdone(&requestBlock); return (0); } /* read the ending band */ rc = Mcargint(0,"BAN.D",2,bband,MIN_BAND,MAX_BANDS,&eband,&dum); if( rc < 0 ) { (void)strcpy(requestBlock.errormsg, "Invalid band number specified"); requestBlock.returncode = -34; M0sxdone(&requestBlock); return (0); } sprintf(trace_string,"%s bband=%d eband=%d", server,bband,eband); M0sxtrce( trace_string ); request_n_bands = 0; for( i=bband; i<=eband; i++ ) { request_bands[request_n_bands] = i; request_n_bands = request_n_bands+1; } } } else { request_n_bands = 22; for( i=0; i<22; i++ ) request_bands[i] = i+1; } /* TIME sort condition */ checkTime = Mcargnum(0, "TIM.E"); if( checkTime != 0 ) { rc = Mcargihr(0, "TIM.E", 1, -1, -1, 240000, &bTime, &dum); rc = Mcargihr(0, "TIM.E", 2, bTime, -1, 240000, &eTime, &dum); sprintf(trace_string, "%s bTime: %d, eTime: %d", server, bTime, eTime); M0sxtrce( trace_string ); } /* DAY sort condition */ checkDay = Mcargnum(0, "DAY" ); if( checkDay != 0 ) { rc = Mcargiyd(0, "DAY", 1, -1, 999, -999, &bDate, &dum); rc = Mcargiyd(0, "DAY", 2, bDate, 999, -999, &eDate, &dum); /* put in YYYDDD format (year modulo 1900) */ if ((bDate / 1000) >= 2000) { bDate = ((bDate / 1000) % 1900) * 1000 + (bDate % 1000); } else { bDate = bDate % 100000; } if ((eDate / 1000) >= 2000) { eDate = ((eDate / 1000) % 1900) * 1000 + (eDate % 1000); } else { eDate = eDate % 100000; } sprintf(trace_string, "%s bDate: %d, eDate: %d", server, bDate, eDate); M0sxtrce( trace_string ); } /* SS sort condition */ checkSS = Mcargnum(0, "SS" ); if( checkSS != 0 ) { rc = Mcargint(0, "SS", 1, -1, 999, -999, &bSS, &dum); rc = Mcargint(0, "SS", 2, bSS, 999, -999, &eSS, &dum); sprintf(trace_string, "%s bSS: %d, eSS: %d", server, bSS, eSS); M0sxtrce( trace_string ); } /* AUX sort condition */ rc = Mcargstr(0, "AUX", 1, "NO", (const char **) &cAux); if (strncmp(cAux, "YES", 3) == 0) { auxFlag = 1; } else { auxFlag = 0; } /* Second phase of the process is to get a list of the files that match the image and navigation masks. */ /* produce a list of files matching the IMG (mask) file mask */ sprintf(trace_string, "%s IMG file Mask = %s", server,mask); M0sxtrce( trace_string ); imglist = M0GetMaskFileList(mask); if( imglist == (char **)NULL ) { (void)strcpy(requestBlock.errormsg, "No img files satisfy selection criteria"); requestBlock.returncode = -30; M0sxdone(&requestBlock); return(0); } N_imglist = VecLen(imglist); sprintf(trace_string, "%s IMG files found = %d", server, N_imglist); M0sxtrce( trace_string ); for( i=0; i HDF5 format */ rc = Mcargstr( file_handle,"",N_tokens,"",(const char **)&cname ); if( strstr( cname, "h5" ) == NULL ) goto STOP_HERE; /* Get dataset name in hdf file */ sprintf(trace_string, "call h5getinfo %s ", file_name); M0sxtrce( trace_string ); status = h5getinfo(file_name, ds_name); sprintf(trace_string, "ds_name %s ", ds_name); M0sxtrce( trace_string ); /* scan IMG file name for file type */ for( I_token=0; I_token HDF5 format */ rc = Mcargstr( file_handle,"",N_tokens,"",(const char **)&cname ); if( strstr( cname, "h5" ) == NULL ) goto STOP_NAV_HERE; /* scan IMG file name for file type */ for( I_token=0; I_token= listSec[k] ) ) { */ if( (listSec[j] > listSec[k] ) ) { image_format = listFor[k]; image_typ = listTyp[k]; image_sat = listSat[k]; image_cyd = listCyd[k]; image_hms = listHms[k]; image_sec = listSec[k]; image_band = listBnd[k]; image_line_resolution = listLfac[k]; image_element_resolution = listEfac[k]; image_geo_line_resolution = listLnav[k]; image_geo_element_resolution = listEnav[k]; image_km_resolution = listRes[k]; image_list = listImg[k]; image_nav = listNav[k]; listFor[k] = listFor[j]; listTyp[k] = listTyp[j]; listSat[k] = listSat[j]; listCyd[k] = listCyd[j]; listHms[k] = listHms[j]; listSec[k] = listSec[j]; listBnd[k] = listBnd[j]; listLfac[k] = listLfac[j]; listEfac[k] = listEfac[j]; listLnav[k] = listLnav[j]; listEnav[k] = listEnav[j]; listRes[k] = listRes[j]; listImg[k] = listImg[j]; listNav[k] = listNav[j]; listFor[j] = image_format; listTyp[j] = image_typ; listSat[j] = image_sat; listCyd[j] = image_cyd; listHms[j] = image_hms; listSec[j] = image_sec; listBnd[j] = image_band; listLfac[j] = image_line_resolution; listEfac[j] = image_element_resolution; listLnav[j] = image_geo_line_resolution; listEnav[j] = image_geo_element_resolution; listRes[j] = image_km_resolution; listImg[j] = image_list; listNav[j] = image_nav; } } } /* Order the list by Ascending Band number (lowest to highest for same time) */ for( k=0; k 1 ) { for( k=1; k imageCount ) ePos = imageCount; bPos = 1; fPos = 0; nPos = 0; INVERT = 1; } } else { INVERT = 0; rc = Mcargint(0, " ", 3, bPos, bPos, 99999, &ePos, &dum); if( ePos < bPos ) ePos = bPos; if( ePos > listCount ) ePos = imageCount; /* invert the bPos and ePos values */ iPos = imageCount-ePos+1; ePos = imageCount-bPos+1; bPos = iPos; fPos = 0; nPos = 0; } } sprintf(trace_string, "\n\n%s MAIN LOOP --- bpos=%d epos=%d", server, bPos, ePos ); M0sxtrce( trace_string ); /* initialize list entries */ for( i=0; i imageCount-1 */ if( INVERT == 0 ) { iPos = imageCount-jj; jPos = jj+1; } if( INVERT == 1 ) { iPos = jj+1; jPos = imageCount-jj; } VALID_NAV = 0; for( j=0; j ePos ) VALID_IMAGE++; /* check the DATE */ rc = Mccydtoiyd( listCyd[j], &image_iyd); if (checkDay > 0 ) { if( (image_iyd < bDate) || (image_iyd > eDate) ) VALID_IMAGE++; } /* check the TIME */ image_hms = listHms[j]; if (checkTime > 0 ) { if( (image_hms < bTime) || (image_hms > eTime) ) VALID_IMAGE++; } /* check the SS */ image_sat = listSat[j]; if( checkSS > 0 ) { if( (image_sat < bSS) || (image_sat > eSS) ) VALID_IMAGE++; } /* check the band */ if( request_n_bands > 0 ) { image_band = listBnd[j]; PASS = 0; for (i=0; i90.0 ) NBADLAT++; if( Lon_array[k]<-180.0 || Lon_array[k]>180.0 ) NBADLON++; } } sprintf(trace_string, "%s LALO: NBADLAT=%d NBADLON=%d", server, NBADLAT, NBADLON ); M0sxtrce( trace_string); /* compute the center point of the Lat/Lon arrays */ NavFound = 0; for( NavLine=0; NavLine<50; NavLine++ ) { if( NavFound == 0 ) { i = (((Lat_dims[0]/2)-1-NavLine)*Lat_dims[1])+(Lat_dims[1]/2); if( (Lat_array[i]>= -90.0 && Lat_array[i]<= 90.0) && (Lon_array[i]>=-180.0 && Lon_array[i]<=180.0) ) { NavFound = -NavLine; listCPLat[j] = Lat_array[i]; listCPLon[j] = -1.0 * Lon_array[i]; } } if( NavFound == 0 ) { i = (((Lat_dims[0]/2)-1+NavLine)*Lat_dims[1])+(Lat_dims[1]/2); if( (Lat_array[i]>= -90.0 && Lat_array[i]<= 90.0) && (Lon_array[i]>=-180.0 && Lon_array[i]<=180.0) ) { NavFound = NavLine; listCPLat[j] = Lat_array[i]; listCPLon[j] = -1.0 * Lon_array[i]; } } } if( NavFound == 0 ) { (void)strcpy(requestBlock.errormsg, "Navigation READ error"); requestBlock.returncode = -33; M0sxdone(&requestBlock); return (0); } else { sprintf(trace_string, "%s LALO CP : Lat=%f Lon=%f NavOffset=%d", server, Lat_array[i], Lon_array[i], NavFound ); M0sxtrce( trace_string); } /* save a copy of the CP values */ SAVECPLAT = listCPLat[j]; SAVECPLON = listCPLon[j]; VALID_NAV = 1; /* free the Latitude and Longitude arrays */ free( Lat_array ); free( Lon_array ); } } } /* set array dimensions */ listLin[j] = Lat_dims[0]; listEle[j] = Lat_dims[1]; SAVELIN = listLin[j]; SAVEELE = listEle[j]; /* clean up and close the NAV file */ if( VALID_IMAGE == 0 ) { /* Close the type identifier */ H5_status = H5Tclose( type_id ); /* Close the Latitude dataspace */ H5_status = H5Sclose( space_id ); /* Close the Latitude dataset */ H5_status = H5Dclose(Lat_id); H5_status = H5Dclose(Lon_id); /* Close the group */ H5_status = H5Gclose(group_id); /* Close the file */ H5_status = H5Fclose(file_id); } } } } /* ends open/don't NAV for this image */ } /* this closes the OPEN Nav file */ /* we have a vaild image ... send a dircetory */ if( VALID_IMAGE == 0 ) { /* is this necessary? */ if( fPos > 0 ) { if( nPos == 0 ) { nPos++; lastPos = iPos; } else { if( iPos != lastPos ) { nPos++; if( nPos > fPos ) goto LIST_DONE; lastPos = iPos; } } } /* initialize the directory block */ for( i=0; i=6 && image_band<=21 ) { directory[8] = listLin[j]; directory[9] = listEle[j]; directory[11] = 2; directory[12] = 2; directory[14] = 16; image_km_resolution = 0.80; image_geo_line_size = directory[8]; image_geo_element_size = directory[9]; } else { directory[8] = listLin[j]; directory[9] = listEle[j]; directory[11] = 1; directory[12] = 1; directory[14] = 0; image_km_resolution = 0.60; image_geo_line_size = directory[8]; image_geo_element_size = directory[9]; } directory[10] = 2; directory[13] = 1; directory[16] = directory[3]; directory[17] = directory[4]; directory[18] = pow(2,image_band-1); if( image_format == SDR ) { if( image_typ == Iband ) memcpy( (void *)&directory[24], (void *)"VIIRS SDR I-Band",16); if( image_typ == Mband ) memcpy( (void *)&directory[24], (void *)"VIIRS SDR M-Band",16); if( image_typ == Dband ) memcpy( (void *)&directory[24], (void *)"VIIRS SDR DN-Band",17); } if( image_format == EDR ) { if( image_typ == Iband ) memcpy( (void *)&directory[24], (void *)"VIIRS EDR I-Band",16); if( image_typ == Mband ) memcpy( (void *)&directory[24], (void *)"VIIRS EDR M-Band",16); if( image_typ == Dband ) memcpy( (void *)&directory[24], (void *)"VIIRS EDR DN-Band",17); } /* enforce the MAX_AUX_SIZE of 3,000,000 bytes */ for( k=1; k<=10; k++ ) { VIIRS_AUX_SIZE = (((image_geo_line_size/k)*(image_geo_element_size/k)*4)*2)+52; if( VIIRS_AUX_SIZE <= 3000000 ) break; } sprintf(trace_string, "%s VIIRS_AUX_SIZE=%d", server, VIIRS_AUX_SIZE); M0sxtrce( trace_string ); directory[33] = VIIRS_AUX_OFFSET + VIIRS_AUX_SIZE; directory[34] = VIIRS_NAV_OFFSET; directory[50] = directory[14]; memcpy( (void *)&directory[51], (void *)"VIIR",4); memcpy( (void *)&directory[52], (void *)"RAW ",4); directory[59] = VIIRS_AUX_OFFSET; directory[60] = VIIRS_AUX_ENTRIES; directory[62] = VIIRS_CAL_OFFSET; /* build the comment block */ image_geo_latitude = listCPLat[j]; image_geo_longitude = listCPLon[j]; sprintf(trace_string, "%s AUX_FLAG=%d", server, auxFlag); M0sxtrce( trace_string ); status = GetModsCards( directory, image_band, image_geo_latitude, image_geo_longitude, image_km_resolution, image_km_resolution, cards, auxFlag ); num_cards = directory[63]; /* compute and send byte total, and one dummy word */ send_byte_total = (DIR_SIZE * sizeof(int)) + 4 + (num_cards*80); sprintf(trace_string, "%s Send Byte Total = %d", server, send_byte_total); M0sxtrce( trace_string ); requestBlock.reply_length += send_byte_total; M0swbyt4(&send_byte_total, 1); M0sxsend(4, &send_byte_total); M0sxsend(4, &dummy); /* send the directory to client */ send_byte_total = DIR_SIZE * 4; M0swbyt4(&directory[0], 20); M0swbyt4(&directory[21], 3); M0swbyt4(&directory[32], 19); M0swbyt4(&directory[53], 2); M0swbyt4(&directory[58], 6); if( ischar_(&directory[20]) == 0) M0swbyt4(&directory[20], 1); M0sxsend( send_byte_total, (void *)directory); M0swbyt4(&directory[0], 20); M0swbyt4(&directory[21], 3); M0swbyt4(&directory[32], 19); M0swbyt4(&directory[53], 2); M0swbyt4(&directory[58], 6); if( ischar_(&directory[20]) == 0) M0swbyt4(&directory[20], 1); /* send cards to client */ send_byte_total = num_cards * 80; M0sxsend( send_byte_total, (void *)cards); /* increment the number of directories sent */ sentCount++; sprintf(trace_string, "%s SENT=%d POS=%d", server, sentCount, j ); M0sxtrce( trace_string); /* this CLOSES a image directory SEND */ /* if( fPos > 0 ) goto LIST_DONE; */ } } /* this CLOSES the image loop */ } /* this CLOSES the position loop */ /* done with list escape point */ LIST_DONE: /* if no images were sent, send an "error" message */ if (sentCount == 0) { (void) strncpy(requestBlock.errormsg,"No images satisfy the selection criteria", sizeof(requestBlock.errormsg)); requestBlock.returncode = -30; M0sxdone(&requestBlock); return (0); } /* termination */ M0sxdone(&requestBlock); sprintf(trace_string, "%s exit\n", server); M0sxtrce( trace_string ); return (0); } /* *| Name: *| GetModsCards - Extract info for and create comment cards. *| *| Interface: *| int *| GetModsCards ( *| int *areaDir, *| int band, *| int *cards, *| int auxFlag *| ) *| *| Input: *| none *| *| Input and Output: *| none *| *| Output: *| none *| *| Return values: *| 0: success *| -1: failure *| *| Remarks: *| *| Categories: *| ingest/decode */ /* #define BAND_MAP "BAND MAP = 1-38" */ #define CEN_LAT_CARD "Center latitude = %f" #define CEN_LAT_RES_CARD "Computed Latitude resolution (km) = %f" #define CEN_LON_CARD "Center longitude = %f" #define CEN_LON_RES_CARD "Computed Longitude resolution (km) = %f" #define RES_LON_CARD "Longitude resolution (km) = %f" #define RES_LAT_CARD "Latitude resolution (km) = %f" #define CAL_CARD "Valid calibration unit for band %d = %s \"%s\"" int GetModsCards ( int *areaDir, int band, float lat, float lon, float rlat, float rlon, int *cards, int auxFlag ) { int MAX_CARD_LEN = 80; int NUM_COMMENT = 63; int NUM_BAND = 22; int rc; int numCards = 0; int i; char card[80]; static char trace_string[500]; /* tracing text */ /* build cards for center point */ (void) sprintf(card, CEN_LAT_CARD, lat); rc = AddCard(card, (char *) cards, MAX_CARD_LEN, numCards); numCards++; (void) sprintf(card, CEN_LAT_RES_CARD, rlat); rc = AddCard(card, (char *) cards, MAX_CARD_LEN, numCards); numCards++; (void) sprintf(card, CEN_LON_CARD, lon); rc = AddCard(card, (char *) cards, MAX_CARD_LEN, numCards); numCards++; (void) sprintf(card, CEN_LON_RES_CARD, rlon); rc = AddCard(card, (char *) cards, MAX_CARD_LEN, numCards); numCards++; (void) sprintf(card, RES_LAT_CARD, rlat); rc = AddCard(card, (char *) cards, MAX_CARD_LEN, numCards); numCards++; (void) sprintf(card, RES_LON_CARD, rlon); rc = AddCard(card, (char *) cards, MAX_CARD_LEN, numCards); numCards++; if (auxFlag == 1) { if( band == 4 || band == 5 || band == 17 || band == 18 || band == 19 || band == 20 || band == 21 ) { /* raw card */ (void) sprintf(card, CAL_CARD, band, "RAW ", "RAW "); rc = AddCard(card, (char *) cards, MAX_CARD_LEN, numCards); numCards++; /* radiance card */ (void) sprintf(card, CAL_CARD, band, "RAD ", "RADIANCE"); rc = AddCard(card, (char *) cards, MAX_CARD_LEN, numCards); numCards++; /* temperature card */ (void) sprintf(card, CAL_CARD, band, "TEMP", "TEMPERATURE"); rc = AddCard(card, (char *) cards, MAX_CARD_LEN, numCards); numCards++; } else if ( band == 22 ) { /* lrad card */ (void) sprintf(card, CAL_CARD, band, "RAW ", "LOG RADIANCE"); rc = AddCard(card, (char *) cards, MAX_CARD_LEN, numCards); numCards++; /* radiance card */ (void) sprintf(card, CAL_CARD, band, "RAD ", "RADIANCE"); rc = AddCard(card, (char *) cards, MAX_CARD_LEN, numCards); numCards++; } else if ( band>=1 && band<=16 ) { /* raw card */ (void) sprintf(card, CAL_CARD, band, "RAW ", "RAW "); rc = AddCard(card, (char *) cards, MAX_CARD_LEN, numCards); numCards++; /* reflectance card */ (void) sprintf(card, CAL_CARD, band, "REF ", "REFLECTANCE"); rc = AddCard(card, (char *) cards, MAX_CARD_LEN, numCards); numCards++; /* normalized reflectance card */ (void) sprintf(card, CAL_CARD, band, "NREF", "NORMALIZED REF"); rc = AddCard(card, (char *) cards, MAX_CARD_LEN, numCards); numCards++; } else { /* raw card */ (void) sprintf(card, CAL_CARD, band, "RAW ", "RAW "); rc = AddCard(card, (char *) cards, MAX_CARD_LEN, numCards); numCards++; /* radiance card */ (void) sprintf(card, CAL_CARD, band, "RAD ", "RADIANCE"); rc = AddCard(card, (char *) cards, MAX_CARD_LEN, numCards); numCards++; /* reflectance card */ (void) sprintf(card, CAL_CARD, band, "REF ", "REFLECTANCE"); rc = AddCard(card, (char *) cards, MAX_CARD_LEN, numCards); numCards++; /* normalized reflectance card */ (void) sprintf(card, CAL_CARD, band, "NREF", "NORMALIZED REF"); rc = AddCard(card, (char *) cards, MAX_CARD_LEN, numCards); numCards++; } /* brightness card */ (void) sprintf(card, CAL_CARD, band, "BRIT", "BRIGHTNESS"); rc = AddCard(card, (char *) cards, MAX_CARD_LEN, numCards); numCards++; } /* update area directory */ areaDir[NUM_COMMENT] = areaDir[NUM_COMMENT] + numCards; return 0; } /* *| Name: *| AddCard - Add a single comment card to set for transfer. *| *| Interface: *| int *| AddCard ( *| char *inCard, *| char *cards, *| int cardSize, *| int numCards *| ) *| *| Input: *| inCard - text string comment card *| cards - integer array of comment cards consisting of text *| cardSize - max byte length of a text string comment card *| numCards - number of cards in the integer array *| *| Input and Output: *| none *| *| Output: *| none *| *| Return values: *| always returns 0 *| *| Remarks: *| *| Categories: *| ingest/decode */ int AddCard ( char *inCard, char *cards, int cardSize, int numCards ) { int i; /* loop variable */ int len; /* byte length on inCard */ int offset; /* offset into cards array */ /* static char trace_string[500]; sprintf(trace_string, "ADD CARD: %s", inCard); M0sxtrce( trace_string ); */ /* * copy the comment card to the * proper position in the integer array */ offset = (cardSize) * (numCards); len = strlen(inCard); /* * blank out the card trailer and * zero the integer array */ for (i = len; i < cardSize; i++) { inCard[i] = ' '; } for (i = offset; i < offset + cardSize; i++) { cards[i] = 0; } (void) memcpy(cards + offset, inCard, (size_t) cardSize); return 0; } int h5getinfo(char *filename, char *ds_name) { hid_t file; hid_t grp; herr_t status; char trace_string[500]; /* * Example: open a file, open the root, scan the whole file. */ file = H5Fopen(filename, H5F_ACC_RDONLY, H5P_DEFAULT); grp = H5Gopen(file,"/", H5P_DEFAULT); scan_group(grp, ds_name); status = H5Fclose(file); return 0; } /* * Process a group and all it's members * * This can be used as a model to implement different actions and * searches. */ void scan_group(hid_t gid, char *ds_name) { int i; ssize_t len; hsize_t nobj; herr_t err; int otype; hid_t grpid, typeid, dsid; char group_name[MAX_NAME]; char memb_name[MAX_NAME]; char trace_string[MAX_NAME]; char s2[] = "/All_Data/VIIRS"; char *p; group_name[0] = 0; memb_name[0] = 0; /* * Information about the group: * Name and attributes * * Other info., not shown here: number of links, object id */ len = H5Iget_name (gid, group_name, MAX_NAME); /* * process the attributes of the group, if any. */ scan_attrs(gid); /* * Get all the members of the groups, one at a time. */ err = H5Gget_num_objs(gid, &nobj); for (i = 0; i < nobj; i++) { /* * For each object in the group, get the name and * what type of object it is. */ len = H5Gget_objname_by_idx(gid, (hsize_t)i, memb_name, (size_t)MAX_NAME ); otype = H5Gget_objtype_by_idx(gid, (size_t)i ); /* * process each object according to its type */ switch(otype) { case H5G_LINK: do_link(gid,memb_name); break; case H5G_GROUP: grpid = H5Gopen(gid,memb_name, H5P_DEFAULT); scan_group(grpid, ds_name); p = strstr(ds_name, s2); if(p) { return; } H5Gclose(grpid); break; case H5G_DATASET: dsid = H5Dopen(gid,memb_name, H5P_DEFAULT); do_dset(dsid, ds_name); p = strstr(ds_name, s2); if(p) { return; } H5Dclose(dsid); break; case H5G_TYPE: typeid = H5Topen(gid,memb_name, H5P_DEFAULT); do_dtype(typeid); H5Tclose(typeid); break; default: /* printf(" unknown?\n"); */ break; } } } /* * Retrieve information about a dataset. * * Many other possible actions. * * This example does not read the data of the dataset. */ void do_dset(hid_t did, char *ds_name) { hid_t tid; hid_t pid; hid_t sid; hsize_t size; char s2[] = "/All_Data/VIIRS"; char *p; /* * Information about the group: * Name and attributes * * Other info., not shown here: number of links, object id */ H5Iget_name(did, ds_name, MAX_NAME ); p=strstr(ds_name, s2); if(p) { /* printf("do_dset p %s\n", p); */ return; } /* * process the attributes of the dataset, if any. */ scan_attrs(did); /* * Get dataset information: dataspace, data type */ sid = H5Dget_space(did); /* the dimensions of the dataset (not shown) */ tid = H5Dget_type(did); /* printf(" DATA TYPE:\n"); */ do_dtype(tid); /* * Retrieve and analyse the dataset properties */ pid = H5Dget_create_plist(did); /* get creation property list */ do_plist(pid); size = H5Dget_storage_size(did); /* printf("Total space currently written in file: %d\n",(int)size); */ /* * The datatype and dataspace can be used to read all or * part of the data. (Not shown in this example.) */ /* ... read data with H5Dread, write with H5Dwrite, etc. */ H5Pclose(pid); H5Tclose(tid); H5Sclose(sid); } /* * Analyze a data type description */ void do_dtype(hid_t tid) { H5T_class_t t_class; char trace_string[MAX_NAME]; t_class = H5Tget_class(tid); if(t_class < 0){ M0sxtrce(" Invalid datatype.\n"); } else { /* * Each class has specific properties that can be * retrieved, e.g., size, byte order, exponent, etc. */ if(t_class == H5T_INTEGER) { M0sxtrce(" Datatype is 'H5T_INTEGER'.\n"); /* display size, signed, endianess, etc. */ } else if(t_class == H5T_FLOAT) { M0sxtrce(" Datatype is 'H5T_FLOAT'.\n"); /* display size, endianess, exponennt, etc. */ } else if(t_class == H5T_STRING) { M0sxtrce(" Datatype is 'H5T_STRING'.\n"); /* display size, padding, termination, etc. */ } else if(t_class == H5T_BITFIELD) { M0sxtrce(" Datatype is 'H5T_BITFIELD'.\n"); /* display size, label, etc. */ } else if(t_class == H5T_OPAQUE) { M0sxtrce(" Datatype is 'H5T_OPAQUE'.\n"); /* display size, etc. */ } else if(t_class == H5T_COMPOUND) { M0sxtrce(" Datatype is 'H5T_COMPOUND'.\n"); /* recursively display each member: field name, type */ } else if(t_class == H5T_ARRAY) { M0sxtrce(" Datatype is 'H5T_COMPOUND'.\n"); /* display dimensions, base type */ } else if(t_class == H5T_ENUM) { M0sxtrce(" Datatype is 'H5T_ENUM'.\n"); /* display elements: name, value */ } else { M0sxtrce(" Datatype is 'Other'.\n"); /* eg. Object Reference, ...and so on ... */ } } } /* * Analyze a symbolic link * * The main thing you can do with a link is find out * what it points to. */ void do_link(hid_t gid, char *name) { herr_t status; char target[MAX_NAME]; status = H5Gget_linkval(gid, name, MAX_NAME, target ) ; /* printf("Symlink: %s points to: %s\n", name, target); */ } /* * Run through all the attributes of a dataset or group. * This is similar to iterating through a group. */ void scan_attrs(hid_t oid) { int na; hid_t aid; int i; char trace_string[MAX_NAME]; na = H5Aget_num_attrs(oid); for (i = 0; i < na; i++) { aid = H5Aopen_idx(oid, (unsigned int)i ); do_attr(aid); H5Aclose(aid); } } /* * Process one attribute. * This is similar to the information about a dataset. */ void do_attr(hid_t aid) { ssize_t len; hid_t atype; hid_t aspace; char buf[MAX_NAME]; char trace_string[MAX_NAME]; buf[0] = 0; /* * Get the name of the attribute. */ len = H5Aget_name(aid, MAX_NAME, buf ); /* * Get attribute information: dataspace, data type */ aspace = H5Aget_space(aid); /* the dimensions of the attribute data */ atype = H5Aget_type(aid); do_dtype(atype); /* * The datatype and dataspace can be used to read all or * part of the data. (Not shown in this example.) */ /* ... read data with H5Aread, write with H5Awrite, etc. */ H5Tclose(atype); H5Sclose(aspace); } /* * Example of information that can be read from a Dataset Creation * Property List. * * There are many other possibilities, and there are other property * lists. */ void do_plist(hid_t pid) { hsize_t chunk_dims_out[2]; int rank_chunk; int nfilters; H5Z_filter_t filtn; int i; unsigned int filt_flags, filt_conf; size_t cd_nelmts; unsigned int cd_values[32] ; char f_name[MAX_NAME]; H5D_fill_time_t ft; H5D_alloc_time_t at; H5D_fill_value_t fvstatus; unsigned int szip_options_mask; unsigned int szip_pixels_per_block; /* zillions of things might be on the plist */ /* here are a few... */ /* * get chunking information: rank and dimensions. * * For other layouts, would get the relevant information. */ if(H5D_CHUNKED == H5Pget_layout(pid)){ rank_chunk = H5Pget_chunk(pid, 2, chunk_dims_out); /* printf("chunk rank %d, dimensions %lu x %lu\n", rank_chunk, (unsigned long)(chunk_dims_out[0]), (unsigned long)(chunk_dims_out[1])); */ } /* else if contiguous, etc. */ /* * Get optional filters, if any. * * This include optional checksum and compression methods. */ nfilters = H5Pget_nfilters(pid); for (i = 0; i < nfilters; i++) { /* For each filter, get * filter ID * filter specific parameters */ cd_nelmts = 32; filtn = H5Pget_filter(pid, (unsigned)i, &filt_flags, &cd_nelmts, cd_values, (size_t)MAX_NAME, f_name, &filt_conf); /* * These are the predefined filters */ switch (filtn) { case H5Z_FILTER_DEFLATE: /* AKA GZIP compression */ /* printf("DEFLATE level = %d\n", cd_values[0]); */ break; case H5Z_FILTER_SHUFFLE: /* printf("SHUFFLE\n"); */ break; case H5Z_FILTER_FLETCHER32: /* printf("FLETCHER32\n"); */ break; case H5Z_FILTER_SZIP: szip_options_mask=cd_values[0];; szip_pixels_per_block=cd_values[1]; /* printf("SZIP COMPRESSION: "); printf("PIXELS_PER_BLOCK %d\n", szip_pixels_per_block); */ /* print SZIP options mask, etc. */ break; default: /* printf("UNKNOWN_FILTER\n" ); */ break; } } /* * Get the fill value information: * - when to allocate space on disk * - when to fill on disk * - value to fill, if any */ /* printf("ALLOC_TIME "); */ H5Pget_alloc_time(pid, &at); switch (at) { case H5D_ALLOC_TIME_EARLY: /* printf("EARLY\n"); */ break; case H5D_ALLOC_TIME_INCR: /* printf("INCR\n"); */ break; case H5D_ALLOC_TIME_LATE: /* printf("LATE\n"); */ break; default: /* printf("unknown allocation policy"); */ break; } /* printf("FILL_TIME: "); */ H5Pget_fill_time(pid, &ft); switch ( ft ) { case H5D_FILL_TIME_ALLOC: /* printf("ALLOC\n"); */ break; case H5D_FILL_TIME_NEVER: /* printf("NEVER\n"); */ break; case H5D_FILL_TIME_IFSET: /* printf("IFSET\n"); */ break; default: /* printf("?\n"); */ break; } H5Pfill_value_defined(pid, &fvstatus); if (fvstatus == H5D_FILL_VALUE_UNDEFINED) { /* printf("No fill value defined, will use default\n"); */ } else { /* Read the fill value with H5Pget_fill_value. * Fill value is the same data type as the dataset. * (details not shown) **/ } /* ... and so on for other dataset properties ... */ }