/* * 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: viiraget.c,v 1.33 2021/12/21 20:33:47 russd Tst $ ****/ /* ** Name: ** viiraget : ADDE image server for VIIRS HDF-5 image files. ** ** 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: ** This program reads HDF-5 format of NPP-VIIRS image files ** ** Categories: ** ADDE server */ #include #include #include #include #include "mcidas.h" #include "mcidasp.h" #include "m0glue.h" #include "m0arg.h" #include "AreaDir.h" #include "hdf5.h" /* Prototypes */ 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 TRUE 1 #define FALSE 0 #define DIR_SIZE 64 #define IMAGE_FILE_VERSION 4 #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 MAX_STR_LEN 80 #define MAX_BANDS 22 #define MAX_NAVS 3 #define MAX_RECORDS 20000 { /* VIIRS constants */ int VIIRS_NAV_OFFSET = 256; /* NAV block offset */ int VIIRS_CAL_OFFSET = 768; /* CAL block offset */ int VIIRS_AUX_OFFSET = 1648; /* AUX block offset */ int AUX_MAGIC_NUMBER = 0x04030201; /* AUX block magic number */ int VIIRS_AUX_ENTRIES = 2; /* AUX block entries */ int VIIRS_LAT_AUX = 24; /* AUX block: LAT entry header byte size */ int VIIRS_LON_AUX = 28; /* AUX block: LON entry header byte size */ int NAV_BLOCK_SIZE = 128; /* NAV block word size */ int CALB_BLOCK_SIZE = 220; /* CAL block word size */ int CAL_BLOCK_SIZE = 220; /* CAL block word size */ int INVALID_VALUE = 65527; float VIIRS_RAW_SCALE = 1. ; /* scaling for RAW data */ float VIIRS_RAD_SCALE = 1000. ; /* scaling for RAD data */ float VIIRS_REF_SCALE = 1000. ; /* scaling for REF data */ float VIIRS_NREF_SCALE = 1000. ; /* scaling for NREF data */ float VIIRS_TEMP_SCALE = 100. ; /* scaling for TEMP data */ float VIIRS_BRIT_SCALE = 1. ; /* scaling for BRIT data */ float DTR = .01745329; /* degrees to radians */ /* variables used: */ const char *dum; /* dummy variable for arg routines */ const char *cptr; /* dummy variable for arg routines */ const char server[]={"VIIRAGET"}; /* server name */ static char trace_string[500]; /* traceing text */ char ds_name[MAX_NAME]; /* dynamic command buffers */ char cmd[512]; char string[180]; char level_map[MAX_BANDS]; /* prefix level map */ char *dataset; /* dataset name */ char *def_unit; /* default UNIT */ char *dir_unit; /* directory UNIT */ char *cdate; /* file date chars */ char *chms; /* file time chars */ char *comment; /* ADDE comment field */ char *cname; /* file name */ char *ctime; /* image time */ char *format; /* ADDE dataset format */ char *group; /* dataset group */ char *info; /* ADDE dataset info */ char *mask; /* dataset file mask */ char *csstr; /* UNITS ORIG or EXP stretch */ char *cBand; /* band */ char *place; /* coordinate system */ char *navproj; /* NAV projection */ char *navpole; /* NAV pole */ char *punit; /* parameter UNITS */ char *request; /* request string */ char *type; /* ADDE dataset type */ char *unit; /* UNITS keyword */ char **imglist; char **navlist; int N_imglist; int N_navlist; int found; int N_tokens; int I_token; int I_file; int VALID_IMAGE; int PASS; int SDR = 1; int EDR = 2; int REF = 0; int RAD = 1; int Iband = 1; int Mband = 2; int Dband = 3; 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 listMap[MAX_RECORDS]; int listLin[MAX_RECORDS]; int listEle[MAX_RECORDS]; char *of_dat; char *of_hdr; char *prefix; /* line prefix */ char geo_string[6]; char hdr_string[6]; char of_string[12]; float temp; /* temperature */ float raw; /* raw count */ float rad; /* radiance */ float radn; /* radiance */ float ref; /* reflectance */ float rera; /* reflective radiance */ float cnts; /* corrected counts */ float brit; /* brightness */ float lat_diff; /* latitude difference */ float lon_diff; /* longitude difference */ float minimum; /* minimum difference */ Fint4 aux_head[100]; /* AUX entry header */ Fint4 directory[DIR_SIZE]; /* image directory block */ Fint4 nav_block[128]; /* navigation block */ Fint4 cal_block[CALB_BLOCK_SIZE]; /* calibration block */ int NMISS; int itemp; int ONE=1; int TWO=2; int FOUR=4; int nzero, ndata; int aux_block_size; /* size of the AUX block */ int bands[MAX_BANDS]; /* list of bands */ int band_ptr; /* band loop pointer */ int band_all; /* ALL bands tranfer flag */ int bband; /* beginning request band */ int eband; /* ending request band */ int curDate; /* current CCYYDDD */ int curTime; /* current HHMMSS */ int dat_block_size; /* data block size */ int def_space; /* default SPACE */ int esize; /* image element size */ int file_handle; /* file handle */ int hiBound; /* ending dataset position */ int hist_DNB[256]; /* histogram for DNB */ int hist_min; /* min value in histogram for DNB */ int hist_max; /* max value in histogram for DNB */ int ptr; /* array pointer */ int i; /* loop index */ int ii; /* forma line index */ int ie; /* secondary line index */ int il; int j; /* loop index */ int jj; /* secondary element index */ int k; /* array index */ int lalo_array_size; /* lat/lon array size */ int lalo_array_line_size; /* lat/lon array line size */ int lalo_array_elem_size; /* lat/lon array elem size */ int loBound; /* starting dataset position */ int length; /* string length */ int num_flist; /* number of image files */ int num_fnlist; /* number of nav files */ int ounit_flag; /* output unit flag */ int pscale; /* parameter scale */ int psize; /* prefix size */ int rc; /* function return code */ int rc2; /* function return code */ int rt_flag; /* ADDE realtime flag */ int space; /* output spacing */ int totalSize; /* transfer byte count */ int trace; /* trace flag */ int transaction = 0; /* transaction flag */ int user = 0; /* user id */ int bad_Latitudes; /* counts number of bad Latitude values */ int bad_Longitudes; /* counts number of bad Longitude values */ int nbad; int options_in[5]; int options_out[5]; int USECALDLM; int USERAWVAL; int USERAWBTVAL; int SAMPLE_size; int NOBAND; int Nbb, bb; int NAVFILE; int INVERT; int ij; unsigned int iexp; /* exponent for pow function */ char attr_name[MAX_STR_LEN]; /* char name[MAX_NC_NAME]; */ char *char_array; char *cday; char *file_name; char *nav_name; char *image_stype; char *last_stype; char *char_band; char *of_pathname; int status; int band; int num_bad, num_BT_bad, num_good, num_BT_good; int bPos; int ePos; int fPos; int nPos; int iPos; int jPos; int sPos; int Pos; int scale_index[MAX_BANDS]; int sd_band_index[MAX_BANDS]; int sd_band_num[MAX_BANDS]; int sd_band_rank[MAX_BANDS]; int image_pos; int image_nav; int image_list; int image_stype_flag; int image_format; int image_cyd; int image_iyd; int image_hms; int image_dmy; int image_n_bands; int image_starting_line; int image_starting_element; int image_dataset_position; int image_line_size; int image_channel_type; Fint STG_parms[6]; int image_element_size; int image_line_base_resolution; int image_element_base_resolution; int image_selected_band; float image_line_resolution; float image_element_resolution; float DATAtoGEO_line; float DATAtoGEO_element; int image_geo_line_size; int image_geo_element_size; int image_geo_line_magnification; int image_geo_element_magnification; int image_geo_data_size; int image_geo_latitude_resolution; int image_geo_longitude_resolution; int image_geo_nav_offset; int nav_offset; int file_name_type; /* file naming convention 1-3=data, 4-6=nav */ int data_name_type; /* data naming convention 1=SVI, 2=SVM, 3=SVDN */ float image_geo_minimum_latitude; float image_geo_maximum_latitude; float image_geo_minimum_longitude; float image_geo_maximum_longitude; float image_geo_center_latitude; float image_geo_center_longitude; float image_geo_line_resolution; float image_geo_element_resolution; float sector_min_lat; float sector_min_lon; float sector_max_lat; float sector_max_lon; float navparm[10]; float Band_min, Band_max; float radiance_scales[MAX_BANDS]; float radiance_offsets[MAX_BANDS]; float reflectance_scales[MAX_BANDS]; float reflectance_offsets[MAX_BANDS]; float corrected_counts_scales[MAX_BANDS]; float corrected_counts_offsets[MAX_BANDS]; float Band_scales[MAX_BANDS]; float Band_offsets[MAX_BANDS]; float Band_BT_scales[MAX_BANDS]; float Band_BT_offsets[MAX_BANDS]; float *float_array_lat; float *float_array_lon; float *Latitude_array; float *Longitude_array; float *Zenith_array; int *Band_array; float *Band_Farray; float *Band22_array; int *Band_BT_array; float *Band_BT_Farray; float *Band22_BT_array; float *Factors_array; float *Factors_BT_array; int *Data_array; double request_center_latitude; double request_center_longitude; int request_data_size; int request_n_bands; int request_bands[MAX_BANDS]; int request_band_type[MAX_BANDS]; int request_lalo_flag; int request_image_flag; int request_area_flag; int request_normalize; int request_line_size; int request_element_size; int request_line_resolution; int request_element_resolution; int request_line_magnification; int request_element_magnification; int request_starting_line; int request_starting_element; int lineSampleRate; int elemSampleRate; int read_line_size; int read_element_size; int read_starting_line; int read_starting_element; int read_ending_line; int read_ending_element; int read_geo_line_size; int read_geo_element_size; int read_geo_starting_line; int read_geo_starting_element; int read_geo_ending_line; int read_geo_ending_element; int total_line; int total_elem; int starting_line; int starting_elem; int ending_line; int ending_elem; int test; int add; int extend; int sum; int size; int Latitude_flag; int Longitude_flag; int Zenith_flag; int All_band_flag; int Data_flag[MAX_BANDS]; int VIIRS_SVI_flag; int VIIRS_SVM_flag; int VIIRS_SVDN_flag; int MatchX; int MatchY[MAX_BANDS]; int tag; int RelativeTime; int file_ptr; int image_band; int image_day; int image_sec; int image_sat; int image_typ; int begArg; int numArg; int image_km_resolution; int Y, M, D; int checkDay, checkTime, checkSS, checkBand; int bDate, eDate; int bTime, eTime; int bSS, eSS; int swath2grid_flag; int singleband_flag; int allBandFlag; int minBand; int maxBand; int bump; int ALL; int listCount; int imageCount; int sentCount; int listInsert; int listPtr; int sensor; int SVI_TRIM_LEFT[32] = { 2180, 2180, 1640, 1640, 1040, 1040, 260, 260, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 260, 260, 1040, 1040, 1640, 1640, 2180, 2180 }; int SVI_TRIM_RIGHT[32] = { 4219, 4219, 4759, 4759, 5359, 5359, 6139, 6139, 6399, 6399, 6399, 6399, 6399, 6399, 6399, 6399, 6399, 6399, 6399, 6399, 6399, 6399, 6399, 6399, 6139, 6139, 5359, 5359, 4759, 4759, 4219, 4219 }; int SVM_TRIM_LEFT[16] = { 1090, 820, 520, 130, 0, 0, 0, 0, 0, 0, 0, 0, 130, 520, 820, 1090 }; int SVM_TRIM_RIGHT[16] = { 2109, 2379, 2679, 3069, 3199, 3199, 3199, 3199, 3199, 3199, 3199, 3199, 3069, 2679, 2379, 2109 }; int CHANNEL_TYPE[22] = { 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0 }; unsigned char *uchar_array; unsigned short int *ushort_array; unsigned int *ulong_array; float RAW_value; float scaled_min_raw, scaled_max_raw; float unscaled_min_raw, unscaled_max_raw; float RAW_BT_value; float fill; float navfill; double sin_zenith; int ntotal; int bi; int bj; int bk; int nelem; int iline; int RadFacFlag; int TemFacFlag; /* HDF5 related declaration */ hid_t dat_id; hid_t nav_id; hid_t dat_group_id; hid_t nav_group_id; hid_t Lat_id, Lon_id, Zen_id; hid_t data_id, data_BT_id, Ref_id, Rad_id, Tem_id; hid_t factor_id, factor_BT_id, RefFac_id, RadFac_id, TemFac_id; hid_t Ref_type_id, RefFac_type_id; hid_t Ref_space_id, RefFac_space_id; hid_t Rad_type_id, RadFac_type_id; hid_t Rad_space_id, RadFac_space_id; hid_t Tem_type_id, TemFac_type_id; hid_t Tem_space_id, TemFac_space_id; hid_t Lat_type_id, Lon_type_id, Zen_type_id; hid_t Lat_space_id, Lon_space_id, Zen_space_id; herr_t H5_status; hsize_t Lat_dims[5], Lon_dims[5], Zen_dims[5]; hsize_t Ref_dims[5], RefFac_dims[5]; hsize_t Rad_dims[5], RadFac_dims[5]; hsize_t Tem_dims[5], TemFac_dims[5]; char DAT_MASK_NAME[256]; char NAV_MASK_NAME[256]; char DAT_GROUP_NAME[256]; char NAV_GROUP_NAME[256]; char REF_DATASET_NAME[256]; char REFFAC_DATASET_NAME[256]; char RAD_DATASET_NAME[256]; char RADFAC_DATASET_NAME[256]; char TEM_DATASET_NAME[256]; char TEMFAC_DATASET_NAME[256]; char LATITUDE_DATASET_NAME[256]; char LONGITUDE_DATASET_NAME[256]; char ZENITH_DATASET_NAME[256]; int Ref_ndims, RefFac_ndims; int Rad_ndims, RadFac_ndims; int Tem_ndims, TemFac_ndims; int Lat_ndims, Lon_ndims, Zen_ndims; McArgSyntax filesyntax = {"/_", "=", ";", "{\"", "}", "'", "'", NULL, "X", " "}; servacct requestBlock; /* request block */ unsigned int value; /* EOS data value */ unsigned short int *McIDAS_2byte_array; /* McIDAS 2 byte array */ /* variable for "ALL" */ (void) memcpy(&ALL, "ALL ", 4 ); M0swbyt4(&ALL, 1); /* initialize MRTSwath variable */ swath2grid_flag = 0; /* initialize local server */ rc = M0InitLocalServer(server, argc, argv, &requestBlock, &request); /* see if logging should be done, determined by hidden TRACE= flag */ trace = M0IsTraceSet(request); if (trace > 0) { (void *)sprintf(trace_string,"\n\n%s TEST VIIRAGET V2.00 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 Descriptor=%s", server,dataset); 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 ); /* The first thing to do is validate the request from the client. We need to check the possible keyword=value pairs to make sure that the request meets certain requirements before proceeding */ /* Dataset Position (POS=) */ rc = Mcargint(0, " ", 2, 0, -99999, 99999, &bPos, &dum); if( rc < 0 ) { (void)strcpy(requestBlock.errormsg, "Invalid Beginning Position"); requestBlock.returncode = -30; M0sxdone(&requestBlock); return (0); } /* Current Day and Time */ RelativeTime = 0; rc = Mcgetdaytime(&curDate, &curTime); /* Day of the data (DAY=) */ checkDay = 0; if( Mcargnum(0, "DAY") != 0 ) { rc = Mcargiyd(0, "DAY", 1, curDate, 1972001, curDate, &image_cyd, &dum); if( rc < 0 ) { (void)strcpy(requestBlock.errormsg, "Invalid DAY format"); requestBlock.returncode = -30; M0sxdone(&requestBlock); return(0); } rc = Mccydtoiyd( image_cyd, &bDate ); if( iPos <= 0 && rc != 300) RelativeTime = 1; rc = Mcargiyd(0, "DAY", 2, bDate, bDate, curDate, &image_cyd, &dum); if( rc < 0 ) { (void)strcpy(requestBlock.errormsg, "Invalid DAY format"); requestBlock.returncode = -30; M0sxdone(&requestBlock); return(0); } rc = Mccydtoiyd( image_cyd, &eDate ); checkDay = 1; } else { rc = Mccydtoiyd( 19722001, &bDate ); rc = Mccydtoiyd( curDate, &eDate ); } (void *)sprintf(trace_string,"%s DAY: range= %d to %d check=%d",server,bDate,eDate,checkDay); M0sxtrce( trace_string ); /* Time of the images (TIME=) */ checkTime = 0; if( Mcargnum(0, "TIM.E" ) != 0 ) { rc = Mcargihr(0, "TIM.E", 1, 0, 0, 235959, &bTime, &dum); if( rc < 0 ) { (void)strcpy(requestBlock.errormsg, "Invalid TIME format"); requestBlock.returncode = -30; M0sxdone(&requestBlock); return(0); } if( iPos <= 0 && rc != 400) RelativeTime = 1; rc2 = Mcargihr(0, "TIM.E", 2, bTime, 0, 235959, &eTime, &dum); if( rc2 < 0 ) { (void)strcpy(requestBlock.errormsg, "Invalid TIME format"); requestBlock.returncode = -30; M0sxdone(&requestBlock); return(0); } if( rc2 == 400 ) eTime = 235959; /* make sure client is asking for somthing other than default time range */ if( rc != 400 || rc2 != 400 ) checkTime = 1; } else { bTime = 0; eTime = 235959; } (void *)sprintf(trace_string,"%s TIME: range= %d to %d check=%d",server,bTime,eTime,checkTime); 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 ); } */ /* Navigation Projection (NAV=) */ rc = Mcargstr(0, "NAV", 1, "", (const char **)&navproj); if( rc < 0 ) { (void)strcpy(requestBlock.errormsg, "Invalid NAV parameter format"); requestBlock.returncode = -30; M0sxdone(&requestBlock); return(0); } /* Navigation reference placement (PLACE=) */ rc = Mcargstr(0, "", 3, "AU", (const char **)&place); if( rc < 0 ) { (void)strcpy(requestBlock.errormsg, "Invalid Coordinate format"); requestBlock.returncode = -30; M0sxdone(&requestBlock); return(0); } request_lalo_flag = FALSE; request_image_flag = FALSE; request_area_flag = FALSE; if( strncmp(place,"EC",2) == 0 || strncmp(place,"EU",2) == 0 ) request_lalo_flag = TRUE; if( strncmp(place,"IC",2) == 0 || strncmp(place,"IU",2) == 0 ) request_image_flag = TRUE; if( strncmp(place,"AC",2) == 0 || strncmp(place,"AU",2) == 0 ) request_area_flag = TRUE; /* Earth Coordinates reference point (LATLON=) */ if( request_lalo_flag == TRUE ) { rc = Mcargdll(0, " ", 4, (double)999.0, (double)-90.0, (double)90.0, &request_center_latitude, &dum); if( rc < 0 ) { (void)strcpy(requestBlock.errormsg, "Invalid LATitude format"); requestBlock.returncode = -30; M0sxdone(&requestBlock); return(0); } rc = Mcargdll(0, " ", 5, (double)999.0, (double)-180.0, (double)180.0, &request_center_longitude, &dum); if( rc < 0 ) { (void)strcpy(requestBlock.errormsg, "Invalid LONgitude format"); requestBlock.returncode = -30; M0sxdone(&requestBlock); return(0); } request_center_longitude = -request_center_longitude; (void *)sprintf(trace_string,"%s Center Earth COORD: Lat=%f Lon=%f" ,server,request_center_latitude,request_center_longitude); M0sxtrce( trace_string ); } /* Image coordinate reference point (LINELE=) */ else if( request_image_flag == TRUE ) { rc = Mcargint(0, "", 4, 1, 1, 0, &request_starting_line, &dum); if( rc < 0 ) { (void)strcpy(requestBlock.errormsg, "Invalid LINe format"); requestBlock.returncode = -30; M0sxdone(&requestBlock); return(0); } rc = Mcargint(0, "", 5, 1, 1, 0, &request_starting_element, &dum); if( rc < 0 ) { (void)strcpy(requestBlock.errormsg, "Invalid ELEment format"); requestBlock.returncode = -30; M0sxdone(&requestBlock); return(0); } (void *)sprintf(trace_string,"%s Starting Image COORD: Lin=%d Ele=%d" ,server,request_starting_line,request_starting_element); M0sxtrce( trace_string ); } /* File coordinate reference point (LINELE=) */ else if( request_area_flag == TRUE ) { rc = Mcargint(0, "", 4, 0, 1, 0, &read_starting_line, &dum); if( rc < 0 ) { (void)strcpy(requestBlock.errormsg, "Invalid LINe format"); requestBlock.returncode = -30; M0sxdone(&requestBlock); return(0); } rc = Mcargint(0, "", 5, 0, 1, 0, &read_starting_element, &dum); if( rc < 0 ) { (void)strcpy(requestBlock.errormsg, "Invalid ELEment format"); requestBlock.returncode = -30; M0sxdone(&requestBlock); return(0); } (void *)sprintf(trace_string,"%s Starting Area COORD: Lin=%d Ele=%d" ,server,read_starting_line,read_starting_element); M0sxtrce( trace_string ); } /* Output Line Magnification (LMAG=) */ rc = Mcargint(0, "LMA.G", 1, 1, -50, 1, &request_line_magnification, &dum); if( rc < 0 ) { (void)strcpy(requestBlock.errormsg, "Invalid Line MAG specified"); requestBlock.returncode = -30; M0sxdone(&requestBlock); return(0); } if( request_line_magnification == 0 ) request_line_magnification = 1; if( request_line_magnification < 0 ) request_line_magnification = -(request_line_magnification); /* Output Element Magnification (EMAG=) */ rc = Mcargint(0, "EMA.G", 1, 1, -50, 1, &request_element_magnification, &dum); if( rc < 0 ) { (void)strcpy(requestBlock.errormsg, "Invalid Element MAG specified"); requestBlock.returncode = -30; M0sxdone(&requestBlock); return(0); } if( request_element_magnification == 0 ) request_element_magnification = 1; if( request_element_magnification < 0 ) request_element_magnification = -(request_element_magnification); (void *)sprintf(trace_string,"%s MAG: Lin=%d Ele=%d" ,server,request_line_magnification,request_element_magnification); M0sxtrce( trace_string ); /* Output image line and element size (SIZE=) */ rc = Mcargint(0, "", 7, 480, 1, 99999, &request_line_size, &dum ); if( rc < 0 ) { (void)strcpy(requestBlock.errormsg, "Invalid Line SIZE specified"); requestBlock.returncode = -30; M0sxdone(&requestBlock); return(0); } rc = Mcargint(0, "", 8, 640, 1, 99999, &request_element_size, &dum ); if( rc < 0 ) { (void)strcpy(requestBlock.errormsg, "Invalid Element SIZE specified"); requestBlock.returncode = -30; M0sxdone(&requestBlock); return(0); } (void *)sprintf(trace_string,"%s SIZE: Lin=%d Ele=%d" ,server,request_line_size,request_element_size); M0sxtrce( trace_string ); /* Output band list (BAND=) */ for( i=0; i 1 ) { (void)strcpy(requestBlock.errormsg, "Multi-band transfer is not permitted for this data type" ); requestBlock.returncode = -30; M0sxdone(&requestBlock); return(0); } /* Output sensor source type (STYPE=) */ image_stype_flag = Mcargnum(0, "STY.PE" ); if( image_stype_flag != 0 ) { char *cstype; /* STYPE keyword */ rc = Mcargstr(0, "STY.PE", 1, "VISR", (const char **)&cstype); if( rc < 0 ) { (void)strcpy(requestBlock.errormsg, "Invalid STYPE specified"); requestBlock.returncode = -30; M0sxdone(&requestBlock); return(0); } if( strncmp( cstype, "VISR",4 ) != 0 ) { (void)strcpy(requestBlock.errormsg, "Invalid STYPE specified"); requestBlock.returncode = -30; M0sxdone(&requestBlock); return(0); } if( request_n_bands > 1 ) { (void)strcpy(requestBlock.errormsg, "STYPE with Multi-band not allowed"); requestBlock.returncode = -30; M0sxdone(&requestBlock); return(0); } image_stype = stralloc( "VISR", NULL); def_unit = stralloc( "BRIT", NULL); last_stype = stralloc( "VIIR", NULL); def_space = 1; CAL_BLOCK_SIZE = 0; VIIRS_CAL_OFFSET = 0; VIIRS_AUX_OFFSET = 768; } else { image_stype = stralloc( "VIIR", NULL); def_unit = stralloc( "RAW",NULL); last_stype = stralloc( " ", NULL); def_space = 2; } /* Output bytes per element (SPACE=) */ rc = Mcargint(0, "SPA.CE", 1, def_space, 0, 4, &space, &dum); if( rc < 0 || space == 3 ) { (void)strcpy(requestBlock.errormsg, "Invalid SPACE specified"); requestBlock.returncode = -30; M0sxdone(&requestBlock); return(0); } /* Output element physical units (UNIT=) */ rc = Mcargstr(0, "UNI.T", 1, def_unit, (const char **)&unit); if( rc < 0 ) { (void)strcpy(requestBlock.errormsg, "Invalid UNIT specified"); requestBlock.returncode = -30; M0sxdone(&requestBlock); return(0); } rc = Mcargstr(0, "UNI.T", 2, " ", (const char **)&csstr); if( strncmp( unit, "RAW",3 ) == 0 ) { if( space == 0 ) space = 2; if( space < 2 ) { (void)strcpy(requestBlock.errormsg, "Invalid Spacing for RAW specified"); requestBlock.returncode = -30; M0sxdone(&requestBlock); return(0); } dir_unit = stralloc( "RAW ",NULL); request_data_size = space; pscale = VIIRS_RAW_SCALE; punit = stralloc(" ",NULL); ounit_flag = 0; } else if( strncmp( unit, "REF",3 ) == 0 ) { if( space == 0 ) space = 2; if( space < 2 ) { (void)strcpy(requestBlock.errormsg, "Invalid Spacing for REF specified"); requestBlock.returncode = -30; M0sxdone(&requestBlock); return(0); } if( request_n_bands > 1 ) { (void)strcpy(requestBlock.errormsg, "Multi-band transfer UNITS must be RAW"); requestBlock.returncode = -30; M0sxdone(&requestBlock); return(0); } dir_unit = stralloc( "REF ",NULL); request_data_size = space; pscale = VIIRS_REF_SCALE; punit = stralloc("1/SR",NULL); ounit_flag = 1; } else if( strncmp( unit, "NREF",4 ) == 0 ) { if( space == 0 ) space = 2; if( space < 2 ) { (void)strcpy(requestBlock.errormsg, "Invalid Spacing for NREF specified"); requestBlock.returncode = -30; M0sxdone(&requestBlock); return(0); } if( request_n_bands > 1 ) { (void)strcpy(requestBlock.errormsg, "Multi-band transfer UNITS must be RAW"); requestBlock.returncode = -30; M0sxdone(&requestBlock); return(0); } dir_unit = stralloc( "NREF",NULL); request_data_size = space; pscale = VIIRS_REF_SCALE; punit = stralloc("1/SR",NULL); ounit_flag = 5; } else if( strncmp( unit, "RAD",3 ) == 0 ) { if( space == 0 ) space = 4; (void *)sprintf(trace_string,"%s BAND[0]=%d",server,request_bands[0]); M0sxtrce( trace_string ); if( space < 4 ) { (void)strcpy(requestBlock.errormsg, "Invalid Spacing for RAD specified"); requestBlock.returncode = -30; M0sxdone(&requestBlock); return(0); } if( request_n_bands > 1 ) { (void)strcpy(requestBlock.errormsg, "Multi-band transfer UNITS must be RAW"); requestBlock.returncode = -30; M0sxdone(&requestBlock); return(0); } dir_unit = stralloc( "RAD ",NULL); request_data_size = space; pscale = VIIRS_RAD_SCALE; punit = stralloc("MW**",NULL); ounit_flag = 2; } else if( strncmp( unit, "TEMP" ,4) == 0 ) { if( space == 0 ) space = 2; if( space < 2 ) { (void)strcpy(requestBlock.errormsg, "Invalid Spacing for TEMP specified"); requestBlock.returncode = -30; M0sxdone(&requestBlock); return(0); } if( request_n_bands > 1 ) { (void)strcpy(requestBlock.errormsg, "Multi-band transfer UNITS must be RAW"); requestBlock.returncode = -30; M0sxdone(&requestBlock); return(0); } dir_unit = stralloc( "TEMP",NULL); request_data_size = space; pscale = VIIRS_TEMP_SCALE; punit = stralloc("K ",NULL); ounit_flag = 3; } else if( strncmp( unit, "BRIT" ,4) == 0 || strncmp( unit, "NORB" ,4) == 0 ) { if( space == 0 ) space = 1; if( request_n_bands > 1 ) { (void)strcpy(requestBlock.errormsg, "Multi-band transfer UNITS must be RAW"); requestBlock.returncode = -30; M0sxdone(&requestBlock); return(0); } request_normalize = FALSE; if( strncmp( unit, "NORB" ,4) == 0 ) request_normalize = TRUE; dir_unit = stralloc( "BRIT",NULL); request_data_size = space; pscale = VIIRS_BRIT_SCALE; punit = stralloc(" ",NULL); ounit_flag = 4; } else { (void)strcpy(requestBlock.errormsg, "Invalid UNIT specified"); requestBlock.returncode = -30; M0sxdone(&requestBlock); return(0); } (void *)sprintf(trace_string,"%s SPACE=%d",server,space); M0sxtrce( trace_string ); (void *)sprintf(trace_string,"%s UNIT=%s",server,unit); M0sxtrce( trace_string ); /* 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 ) { (void)strcpy(requestBlock.errormsg, "FAILED: Invalid Image Position "); requestBlock.returncode = -30; M0sxdone(&requestBlock); return(0); } else { sPos = 0; bPos = ePos; } } } 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 */ sPos = 0; iPos = imageCount-ePos+1; ePos = imageCount-bPos+1; bPos = iPos; } } sprintf(trace_string, "\n%s *** MAIN ... POSITION: bPos=%d ePos=%d", server, bPos,ePos); M0sxtrce( trace_string ); /* cycle through the image file list */ sentCount = 0; MatchX = -1; /* loop through the image positions */ for( ij=0; ij imageCount-1 */ if( INVERT == 0 ) { iPos = imageCount-ij; jPos = ij+1; } if( INVERT == 1 ) { iPos = ij+1; jPos = imageCount-ij; } for( j=0; j ePos ) VALID_IMAGE = VALID_IMAGE+10; /* check the DATE */ rc = Mccydtoiyd( listCyd[j], &image_iyd); if (checkDay > 0 ) { if( (image_iyd < bDate) || (image_iyd > eDate) ) VALID_IMAGE = VALID_IMAGE+100;; } /* check the TIME */ image_hms = listHms[j]; if (checkTime > 0 ) { if( (image_hms < bTime) || (image_hms > eTime) ) VALID_IMAGE = VALID_IMAGE+1000; } /* do we need to check if this is a single banded image */ if( NOBAND == 1 ) { Nbb = 0; for( bb=0; bb 0 ) { MatchX = j; image_dataset_position = MatchX+1; } } if( INVERT == 0 ) { if( MatchX == -1 ) { MatchX = j; image_dataset_position = MatchX+1; } } } } /* end the image loop */ } /* end the position loop */ /* if none of the images satisfies the tests we need to abort and return */ if( MatchX == -1 ) { (void)strcpy(requestBlock.errormsg, "No images match request conditions"); requestBlock.returncode = -30; M0sxdone(&requestBlock); return(0); } else { sprintf(trace_string, "MATCHX: %d: %d %d %d %d %d %d %d %d %d %f %f %f %d %d %d", MatchX, listFor[MatchX], listTyp[MatchX], listSat[MatchX], listCyd[MatchX], listHms[MatchX], listSec[MatchX], listBnd[MatchX], listLfac[MatchX], listEfac[MatchX], listLnav[MatchX], listEnav[MatchX], listRes[MatchX], listImg[MatchX], listNav[MatchX], listPos[MatchX] ); M0sxtrce( trace_string ); } /* At this point we now know which files we need to read to get the data that is specified in the request. This is where we need to put the HDF5 harness to read the data files into the internal arrays. */ /* image file name */ file_name = imglist[ listImg[MatchX] ]; sprintf(trace_string, "%s OPEN DATA FILE = %s", server, file_name); M0sxtrce( trace_string ); /* image file handle */ dat_id = H5Fopen(file_name, H5F_ACC_RDONLY, H5P_DEFAULT); if (dat_id <= 0) { (void)strcpy(requestBlock.errormsg, "FAILED: OPEN Dat File"); requestBlock.returncode = -30; M0sxdone(&requestBlock); return(0); } status = h5getinfo(file_name, ds_name); sprintf(trace_string, "ds_name %s ", ds_name); M0sxtrce( trace_string ); /* what kind of channel is this image (REF or RAD) */ band = listBnd[MatchX]; image_channel_type = CHANNEL_TYPE[band-1]; if( image_channel_type == REF ) { sprintf(trace_string, "%s Image Channel Type = REF", server); } else { sprintf(trace_string, "%s Image Channel Type = RAD", server); } M0sxtrce( trace_string ); /* Nav file name */ if (listNav[MatchX] < 0) { (void)strcpy(requestBlock.errormsg, "FAILED: Nav file not associated with image"); requestBlock.returncode = -30; M0sxdone(&requestBlock); return(0); } nav_name = navlist[ listNav[MatchX] ]; sprintf(trace_string, "%s OPEN NAV FILE = %s", server, nav_name); M0sxtrce( trace_string ); /* nav file handle */ nav_id = H5Fopen(nav_name, H5F_ACC_RDONLY, H5P_DEFAULT); if (nav_id <= 0) { (void)strcpy(requestBlock.errormsg, "FAILED: OPEN Nav File"); requestBlock.returncode = -30; M0sxdone(&requestBlock); return(0); } /* Based on the image format create the DAT_GROUP NAME */ image_format = listFor[MatchX]; image_typ = listTyp[MatchX]; sprintf(trace_string, "%s Image: format=%d type=%d ", server, image_format, image_typ); M0sxtrce( trace_string ); /* Assign DAT Group Name */ /* ds_name example /All_Data/VIIRS-M15-MOD-EDR_All/BrightnessFactors */ /* Truncate at 'All/' for the Data Group Name */ memset(DAT_GROUP_NAME, 0, sizeof(DAT_GROUP_NAME)); cptr = strstr(ds_name, "All/"); length = cptr-ds_name+3; sprintf(trace_string, "length %d", length); strncpy(DAT_GROUP_NAME, ds_name, length); sprintf(trace_string, "%s Data Group Name = %s", server, DAT_GROUP_NAME); M0sxtrce( trace_string ); sprintf(trace_string, "%s Nav Group Name = %s", server, NAV_GROUP_NAME); M0sxtrce( trace_string ); /* Open the groups */ dat_group_id = H5Gopen(dat_id, DAT_GROUP_NAME, H5P_DEFAULT); if (dat_group_id <= 0) { (void)strcpy(requestBlock.errormsg, "FAILED: OPEN Dat Group"); requestBlock.returncode = -30; M0sxdone(&requestBlock); return(0); } nav_group_id = H5Gopen(nav_id, NAV_GROUP_NAME, H5P_DEFAULT); if (dat_group_id <= 0) { (void)strcpy(requestBlock.errormsg, "FAILED: OPEN Nav Group"); requestBlock.returncode = -30; M0sxdone(&requestBlock); return(0); } /* Open the Reflectance and Radiance Datasets */ if( image_channel_type == REF ) { /* REF_DATASET_NAME and REFFAC_DATASET_NAME are based on image format */ if( image_format == SDR ) sprintf(REF_DATASET_NAME, "%s/%s", DAT_GROUP_NAME, "Reflectance"); if( image_format == EDR ) sprintf(REF_DATASET_NAME, "%s/%s", DAT_GROUP_NAME, "Reflectance"); /* if( image_format == EDR ) sprintf(REF_DATASET_NAME, "%s/%s", DAT_GROUP_NAME, "BrightnessTemperatureOrReflectance"); */ sprintf(trace_string, "%s !!!!!!!!!!! REF_DATASET_NAME = %s", server,REF_DATASET_NAME); M0sxtrce( trace_string ); Ref_id = H5Dopen( dat_id, REF_DATASET_NAME, H5P_DEFAULT ); if (Ref_id <= 0) { (void)strcpy(requestBlock.errormsg, "FAILED: OPEN Reflectance dataset"); requestBlock.returncode = -30; M0sxdone(&requestBlock); return(0); } else { if( image_format == SDR ) sprintf(REFFAC_DATASET_NAME, "%s/%s", DAT_GROUP_NAME, "ReflectanceFactors"); if( image_format == EDR ) sprintf(REFFAC_DATASET_NAME, "%s/%s", DAT_GROUP_NAME, "ReflectanceFactors"); /* if( image_format == EDR ) sprintf(REFFAC_DATASET_NAME, "%s/%s", DAT_GROUP_NAME, "BrightnessTemperatureOrReflectanceFactors"); */ sprintf(trace_string, "%s !!!!!!!!!!! REFFAC_DATASET_NAME = %s", server,REFFAC_DATASET_NAME); M0sxtrce( trace_string ); RefFac_id = H5Dopen( dat_id, REFFAC_DATASET_NAME, H5P_DEFAULT ); if (RefFac_id <= 0) { (void)strcpy(requestBlock.errormsg, "FAILED: OPEN Reflectance Factors dataset"); requestBlock.returncode = -30; M0sxdone(&requestBlock); return(0); } } /* RAD_DATASET_NAME and RADFAC_DATASET_NAME are universal */ sprintf(RAD_DATASET_NAME, "%s/%s", DAT_GROUP_NAME, "Radiance"); sprintf(trace_string, "%s !!!!!!!!!!! RAD_DATASET_NAME = %s", server,RAD_DATASET_NAME); M0sxtrce( trace_string ); Rad_id = H5Dopen( dat_id, RAD_DATASET_NAME, H5P_DEFAULT ); if (Rad_id <= 0) { (void)strcpy(requestBlock.errormsg, "FAILED: OPEN Radiance dataset"); requestBlock.returncode = -30; M0sxdone(&requestBlock); return(0); } else { RadFacFlag = 1; sprintf(RADFAC_DATASET_NAME, "%s/%s", DAT_GROUP_NAME, "RadianceFactors"); RadFac_id = H5Dopen( dat_id, RADFAC_DATASET_NAME, H5P_DEFAULT ); if (RadFac_id <= 0) { RadFacFlag = 0; sprintf(trace_string, "%s WARNING -- No Radiance Factors in dataset", server); M0sxtrce( trace_string ); } } /* Get the Reflectance and Radiance space identifier */ Ref_space_id = H5Dget_space( Ref_id ); if (Ref_space_id <= 0) { (void)strcpy(requestBlock.errormsg, "FAILED: SPACE Reflectance dataset"); requestBlock.returncode = -30; M0sxdone(&requestBlock); return(0); } else { RefFac_space_id = H5Dget_space( RefFac_id ); if (RefFac_space_id <= 0) { (void)strcpy(requestBlock.errormsg, "FAILED: SPACE Reflectance Factors dataset"); requestBlock.returncode = -30; M0sxdone(&requestBlock); return(0); } } Rad_space_id = H5Dget_space( Rad_id ); if (Rad_space_id <= 0) { (void)strcpy(requestBlock.errormsg, "FAILED: SPACE Radiance dataset"); requestBlock.returncode = -30; M0sxdone(&requestBlock); return(0); } else { if( RadFacFlag == 1 ) { RadFac_space_id = H5Dget_space( RadFac_id ); if (RadFac_space_id <= 0) { (void)strcpy(requestBlock.errormsg, "FAILED: SPACE Radiance Factors dataset"); requestBlock.returncode = -30; M0sxdone(&requestBlock); return(0); } } } /* get the Reflectance and Radiance array dimensions */ Ref_ndims = H5Sget_simple_extent_dims( Ref_space_id, Ref_dims, NULL ); RefFac_ndims = H5Sget_simple_extent_dims( RefFac_space_id, RefFac_dims, NULL ); Rad_ndims = H5Sget_simple_extent_dims( Rad_space_id, Rad_dims, NULL ); if(RadFacFlag == 1 ) RadFac_ndims = H5Sget_simple_extent_dims( RadFac_space_id, RadFac_dims, NULL ); /* record the size of the image */ listLin[MatchX] = Ref_dims[0]; listEle[MatchX] = Ref_dims[1]; sprintf(trace_string, "%s Dataset: Lines=%d Elems=%d ", server, listLin[MatchX], listEle[MatchX]); M0sxtrce( trace_string ); /* setup Reflectance and Radaince dataset read */ Ref_type_id = H5Dget_type( Ref_id ); if (Ref_type_id <= 0) { (void)strcpy(requestBlock.errormsg, "FAILED: TYPE Reflectance dataset"); requestBlock.returncode = -30; M0sxdone(&requestBlock); return(0); } else { RefFac_type_id = H5Dget_type( Ref_id ); if (RefFac_type_id <= 0) { (void)strcpy(requestBlock.errormsg, "FAILED: TYPE Reflectance Factors dataset"); requestBlock.returncode = -30; M0sxdone(&requestBlock); return(0); } } Rad_type_id = H5Dget_type( Rad_id ); if (Rad_type_id <= 0) { (void)strcpy(requestBlock.errormsg, "FAILED: TYPE Radiance dataset"); requestBlock.returncode = -30; M0sxdone(&requestBlock); return(0); } else { if( RadFacFlag == 1 ) { RadFac_type_id = H5Dget_type( Rad_id ); if (RadFac_type_id <= 0) { (void)strcpy(requestBlock.errormsg, "FAILED: TYPE Radaince Factors dataset"); requestBlock.returncode = -30; M0sxdone(&requestBlock); return(0); } } Rad_ndims = H5Sget_simple_extent_dims( Rad_space_id, Rad_dims, NULL ); sprintf(trace_string, "%s Radiance Array Dims: lines=%d elems=%d", server, Rad_dims[0], Rad_dims[1]); M0sxtrce( trace_string ); } /* set image dimensions */ image_line_size = Rad_dims[0]; image_element_size = Rad_dims[1]; /* Open the Radiance and BrightnessTemperature Datasets */ } else if( image_channel_type == RAD) { /* RAD_DATASET_NAME and RADFAC_DATASET_NAME are based on image format */ if( image_format == SDR ) sprintf(RAD_DATASET_NAME, "%s/%s", DAT_GROUP_NAME, "Radiance"); if( image_format == EDR ) sprintf(RAD_DATASET_NAME, "%s/%s", DAT_GROUP_NAME, "Radiance"); sprintf(trace_string, "%s !!!!!!!!!!! RAD_DATASET_NAME = %s", server,RAD_DATASET_NAME); M0sxtrce( trace_string ); Rad_id = H5Dopen( dat_id, RAD_DATASET_NAME, H5P_DEFAULT ); if (Rad_id <= 0) { (void)strcpy(requestBlock.errormsg, "FAILED: OPEN Radiance dataset"); requestBlock.returncode = -30; M0sxdone(&requestBlock); return(0); } else { RadFacFlag = 1; if( image_format == SDR ) sprintf(RADFAC_DATASET_NAME, "%s/%s", DAT_GROUP_NAME, "RadianceFactors"); if( image_format == EDR ) sprintf(RADFAC_DATASET_NAME, "%s/%s", DAT_GROUP_NAME, "RadianceFactors"); RadFac_id = H5Dopen( dat_id, RADFAC_DATASET_NAME, H5P_DEFAULT ); if (RadFac_id <= 0) { RadFacFlag == 0; sprintf(trace_string, "%s WARNING -- RadianceFactors in dataset", server); M0sxtrce( trace_string ); } } /* for DNB/NCC only */ if( image_typ == Dband ) { TemFacFlag = 0; RadFacFlag = 0; /* Get the Radiance and BrightnessTemperature space identifiers */ Rad_space_id = H5Dget_space( Rad_id ); if (Rad_space_id <= 0) { (void)strcpy(requestBlock.errormsg, "FAILED: SPACE Radiance dataset"); requestBlock.returncode = -30; M0sxdone(&requestBlock); return(0); } else { Rad_ndims = H5Sget_simple_extent_dims( Rad_space_id, Rad_dims, NULL ); sprintf(trace_string, "%s DNB Radiance Array Dims: lines=%d elems=%d ", server, Rad_dims[0], Rad_dims[1]); M0sxtrce( trace_string ); } image_line_size = Rad_dims[0]; image_element_size = Rad_dims[1]; listLin[MatchX] = Rad_dims[0]; listEle[MatchX] = Rad_dims[1]; sprintf(trace_string, "%s WARNING -- No BT Factors in dataset", server); M0sxtrce( trace_string ); } else { if( image_format == SDR ) sprintf(TEM_DATASET_NAME, "%s/%s", DAT_GROUP_NAME, "BrightnessTemperature"); if( image_format == EDR ) sprintf(TEM_DATASET_NAME, "%s/%s", DAT_GROUP_NAME, "BrightnessTemperature"); sprintf(trace_string, "%s *** TEM_DATASET_NAME=%s", server, TEM_DATASET_NAME); M0sxtrce( trace_string ); Tem_id = H5Dopen( dat_id, TEM_DATASET_NAME, H5P_DEFAULT ); if (Tem_id <= 0) { (void)strcpy(requestBlock.errormsg, "FAILED: OPEN BrightnessTemperature dataset"); requestBlock.returncode = -30; M0sxdone(&requestBlock); return(0); } else { TemFacFlag = 1; if( image_format == SDR ) sprintf(TEMFAC_DATASET_NAME, "%s/%s", DAT_GROUP_NAME, "BrightnessTemperatureFactors"); if( image_format == EDR ) sprintf(TEMFAC_DATASET_NAME, "%s/%s", DAT_GROUP_NAME, "BrightnessFactors"); sprintf(trace_string, "%s *** TEMFAC_DATASET_NAME=%s", server, TEMFAC_DATASET_NAME); M0sxtrce( trace_string ); TemFac_id = H5Dopen( dat_id, TEMFAC_DATASET_NAME, H5P_DEFAULT ); if (TemFac_id <= 0) { TemFacFlag = 0 ; sprintf(trace_string, "%s WARNING -- No BT Factors in dataset", server); M0sxtrce( trace_string ); } } /* Get the Radiance and BrightnessTemperature space identifiers */ Rad_space_id = H5Dget_space( Rad_id ); if (Rad_space_id <= 0) { (void)strcpy(requestBlock.errormsg, "FAILED: SPACE Radiance dataset"); requestBlock.returncode = -30; M0sxdone(&requestBlock); return(0); } else { if( RadFacFlag == 1 ) { RadFac_space_id = H5Dget_space( RadFac_id ); if (RadFac_space_id <= 0) { RadFacFlag = 0; } } } Tem_space_id = H5Dget_space( Tem_id ); if (Tem_space_id <= 0) { (void)strcpy(requestBlock.errormsg, "FAILED: SPACE BrightnessTemperature dataset"); requestBlock.returncode = -30; M0sxdone(&requestBlock); return(0); } else { if( TemFacFlag == 1 ) { TemFac_space_id = H5Dget_space( TemFac_id ); if (TemFac_space_id <= 0) { TemFacFlag = 0; } } } /* get the Radiance and BrightnessTemperature array dimensions */ Rad_ndims = H5Sget_simple_extent_dims( Rad_space_id, Rad_dims, NULL ); if( RadFacFlag == 1 ) RadFac_ndims = H5Sget_simple_extent_dims( RadFac_space_id, RadFac_dims, NULL ); Tem_ndims = H5Sget_simple_extent_dims( Tem_space_id, Tem_dims, NULL ); if( TemFacFlag == 1 ) TemFac_ndims = H5Sget_simple_extent_dims( TemFac_space_id, TemFac_dims, NULL ); /* record the size of the image */ listLin[MatchX] = Rad_dims[0]; listEle[MatchX] = Rad_dims[1]; /* setup Radiance dataset read */ Rad_type_id = H5Dget_type( Rad_id ); if (Rad_type_id <= 0) { (void)strcpy(requestBlock.errormsg, "FAILED: TYPE Radiance dataset"); requestBlock.returncode = -30; M0sxdone(&requestBlock); return(0); } else { if( RadFacFlag == 1 ) { RadFac_type_id = H5Dget_type( Rad_id ); if (RadFac_type_id <= 0) { (void)strcpy(requestBlock.errormsg, "FAILED: TYPE Radiance Factors dataset"); requestBlock.returncode = -30; M0sxdone(&requestBlock); return(0); } } } /* setup BrightnessTemperature dataset read */ Tem_type_id = H5Dget_type( Tem_id ); if (Tem_type_id <= 0) { (void)strcpy(requestBlock.errormsg, "FAILED: TYPE BrightnessTemperature dataset"); requestBlock.returncode = -30; M0sxdone(&requestBlock); return(0); } else { if( TemFacFlag == 1 ) { TemFac_type_id = H5Dget_type( Tem_id ); if (TemFac_type_id <= 0) { (void)strcpy(requestBlock.errormsg, "FAILED: TYPE BrightnessTemperatureFactors dataset"); requestBlock.returncode = -30; M0sxdone(&requestBlock); return(0); } } } /* set image dimensions */ image_line_size = Rad_dims[0]; image_element_size = Rad_dims[1]; } } sprintf(trace_string, "%s READ DATASET COMPLETE", server); M0sxtrce( trace_string ); /* set image parameters */ image_geo_line_size = image_line_size; image_geo_element_size = image_element_size; DATAtoGEO_line = 1; DATAtoGEO_element = 1; nav_offset = 0; image_geo_nav_offset = 0; image_geo_line_magnification = request_line_magnification; image_geo_element_magnification = request_element_magnification; image_geo_data_size = 4; request_line_resolution = image_line_resolution * request_line_magnification; request_element_resolution = image_element_resolution * request_element_magnification; /* allocate memory for Scale and Offset read(s) */ Factors_array = malloc( 10 * sizeof(float) ); if( Factors_array == NULL ) { (void)strcpy(requestBlock.errormsg, "FAILED: MALLOC Factors array"); requestBlock.returncode = -30; M0sxdone(&requestBlock); return(0); } Factors_BT_array = malloc( 10 * sizeof(float) ); if( Factors_BT_array == NULL ) { (void)strcpy(requestBlock.errormsg, "FAILED: MALLOC Factors TB array"); requestBlock.returncode = -30; M0sxdone(&requestBlock); return(0); } sprintf(trace_string, "%s OPEN LALO", server); M0sxtrce( trace_string ); /* Open the LALO Datasets */ sprintf(LATITUDE_DATASET_NAME, "%s/%s", NAV_GROUP_NAME, "Latitude"); M0sxtrce( LATITUDE_DATASET_NAME ); Lat_id = H5Dopen( nav_id, LATITUDE_DATASET_NAME, H5P_DEFAULT ); if (Lat_id <= 0) { (void)strcpy(requestBlock.errormsg, "FAILED: TYPE Latitude dataset"); requestBlock.returncode = -30; M0sxdone(&requestBlock); return(0); } sprintf(LONGITUDE_DATASET_NAME, "%s/%s", NAV_GROUP_NAME, "Longitude"); Lon_id = H5Dopen( nav_id, LONGITUDE_DATASET_NAME, H5P_DEFAULT ); if (Lon_id <= 0) { (void)strcpy(requestBlock.errormsg, "FAILED: TYPE Longitude dataset"); requestBlock.returncode = -30; M0sxdone(&requestBlock); return(0); } sprintf(ZENITH_DATASET_NAME, "%s/%s", NAV_GROUP_NAME, "SolarZenithAngle"); Zen_id = H5Dopen( nav_id, ZENITH_DATASET_NAME, H5P_DEFAULT ); if (Zen_id <= 0) { (void)strcpy(requestBlock.errormsg, "FAILED: TYPE SolarZenith dataset"); requestBlock.returncode = -30; M0sxdone(&requestBlock); return(0); } /* Get the LALO space identifier */ Lat_space_id = H5Dget_space( Lat_id ); if (Lat_space_id <= 0) { (void)strcpy(requestBlock.errormsg, "FAILED: SPACE Latitude dataset"); requestBlock.returncode = -30; M0sxdone(&requestBlock); return(0); } Lon_space_id = H5Dget_space( Lon_id ); if (Lon_space_id <= 0) { (void)strcpy(requestBlock.errormsg, "FAILED: SPACE Longitude dataset"); requestBlock.returncode = -30; M0sxdone(&requestBlock); return(0); } Zen_space_id = H5Dget_space( Zen_id ); if (Zen_space_id <= 0) { (void)strcpy(requestBlock.errormsg, "FAILED: SPACE SolarZenith dataset"); requestBlock.returncode = -30; M0sxdone(&requestBlock); return(0); } /* get the LALO array dimensions */ Lat_ndims = H5Sget_simple_extent_dims( Lat_space_id, Lat_dims, NULL ); Lon_ndims = H5Sget_simple_extent_dims( Lon_space_id, Lon_dims, NULL ); Zen_ndims = H5Sget_simple_extent_dims( Zen_space_id, Zen_dims, NULL ); sprintf(trace_string, "%s LALO DIMS", server); M0sxtrce( trace_string ); /* setup LALO dataset read */ Lat_type_id = H5Dget_type( Lat_id ); if (Lat_type_id <= 0) { (void)strcpy(requestBlock.errormsg, "FAILED: TYPE Latitude dataset"); requestBlock.returncode = -30; M0sxdone(&requestBlock); return(0); } Lon_type_id = H5Dget_type( Lon_id ); if (Lon_type_id <= 0) { (void)strcpy(requestBlock.errormsg, "FAILED: TYPE Longitude Factors dataset"); requestBlock.returncode = -30; M0sxdone(&requestBlock); return(0); } Zen_type_id = H5Dget_type( Zen_id ); if (Zen_type_id <= 0) { (void)strcpy(requestBlock.errormsg, "FAILED: TYPE SolarZenith Factors dataset"); requestBlock.returncode = -30; M0sxdone(&requestBlock); return(0); } /* compute the memory block allocation for a complete granule */ SAMPLE_size = image_line_size * image_element_size; sprintf(trace_string, "%s LINES=%d ELEMS=%d Sample Size=%d ", server,image_line_size, image_element_size, SAMPLE_size); M0sxtrce( trace_string ); /* ONLY 1 BAND TRANSFER */ request_n_bands = 1; /* aquire memory to hold the image band(s) */ for( i=0; i255 ) value = 255; hist_DNB[value]++; } for( i=0; i<256; i++ ) { sprintf(trace_string, "%s DNB Hist[%d] = %d", server,i,hist_DNB[i]); M0sxtrce( trace_string ); } hist_min = 0; hist_max = 0; for( i=1; i<256; i++ ) if(hist_min == 0 && hist_DNB[i] > 1000) hist_min = i; for( i=254; i>hist_min; i-- ) if(hist_max == 0 && hist_DNB[i] > 1000) hist_max = i; sprintf(trace_string, "%s hist_min = %d", server,hist_min); M0sxtrce( trace_string ); sprintf(trace_string, "%s hist_max = %d", server,hist_max); M0sxtrce( trace_string ); Band_scales[21] = 1.0; Band_offsets[21] = 0.0; Band_BT_scales[21] = 1.0; Band_BT_offsets[21] = 0.0; /* pack the values from 4 to 2 byte */ (void)mpixel_(&SAMPLE_size, &FOUR, &TWO, Band_BT_array); /* Store histogram range in last two words of cal block for band 22 */ corrected_counts_scales[21] = (float) hist_min; corrected_counts_offsets[21] = (float) hist_max; if( strncmp( dir_unit, "RAW " ,4) == 0 ) { pscale = 1000; punit = stralloc("LOGR",NULL); }else if( strncmp( dir_unit, "RAD " ,4) == 0 ) { pscale = 22033; punit = stralloc("MW**",NULL); } } } sprintf(trace_string, "%s LALO & ZEN MALLOC", server); M0sxtrce( trace_string ); /* aquire memory to hold the image latitude */ Latitude_array = malloc( image_line_size * image_element_size * sizeof(float) ); if( Latitude_array == NULL ) { (void)strcpy(requestBlock.errormsg, "Failed malloc of Latitude array"); requestBlock.returncode = -30; M0sxdone(&requestBlock); return(0); } sprintf(trace_string, "%s MALLOCED LAT ARRAY", server); M0sxtrce( trace_string ); H5_status = H5Dread(Lat_id, H5T_NATIVE_FLOAT, H5S_ALL, H5S_ALL, H5P_DEFAULT, Latitude_array); if (H5_status < 0) { (void)strcpy(requestBlock.errormsg, "FAILED: READ Latitude array"); requestBlock.returncode = -30; M0sxdone(&requestBlock); return(0); } sprintf(trace_string, "%s READ LAT ARRAY", server); M0sxtrce( trace_string ); /* aquire memory to hold the image longitude */ Longitude_array = malloc( image_line_size * image_element_size * sizeof(float) ); if( Longitude_array == NULL ) { (void)strcpy(requestBlock.errormsg, "Failed malloc of Longitude array"); requestBlock.returncode = -30; M0sxdone(&requestBlock); return(0); } sprintf(trace_string, "%s MALLOCED LON ARRAY", server); M0sxtrce( trace_string ); H5_status = H5Dread(Lon_id, H5T_NATIVE_FLOAT, H5S_ALL, H5S_ALL, H5P_DEFAULT, Longitude_array); if (H5_status < 0) { (void)strcpy(requestBlock.errormsg, "FAILED: READ Longitude array"); requestBlock.returncode = -30; M0sxdone(&requestBlock); return(0); } sprintf(trace_string, "%s READ LON ARRAY", server); M0sxtrce( trace_string ); /* aquire memory to hold the image solar zenith angle */ Zenith_array = malloc( image_line_size * image_element_size * sizeof(float) ); if( Zenith_array == NULL ) { (void)strcpy(requestBlock.errormsg, "Failed malloc of SolarZenith array"); requestBlock.returncode = -30; M0sxdone(&requestBlock); return(0); } sprintf(trace_string, "%s MALLOCED ZEN ARRAY", server); M0sxtrce( trace_string ); /* Read the solar zenith angle array */ Zenith_flag = 0; H5_status = H5Dread(Zen_id, H5T_NATIVE_FLOAT, H5S_ALL, H5S_ALL, H5P_DEFAULT, Zenith_array); if (H5_status < 0) { (void)strcpy(requestBlock.errormsg, "FAILED: READ SolarZenith array"); requestBlock.returncode = -30; M0sxdone(&requestBlock); return(0); } Zenith_flag = 1; sprintf(trace_string, "%s READ ZEN ARRAY", server); M0sxtrce( trace_string ); /* Close the data file */ if( image_channel_type == 0 ) { H5_status = H5Sclose( Ref_space_id ); H5_status = H5Sclose( Rad_space_id ); H5_status = H5Sclose( RefFac_space_id ); if( RadFacFlag == 1 ) { H5_status = H5Sclose( RadFac_space_id ); } H5_status = H5Dclose(Ref_id); H5_status = H5Dclose(Rad_id); H5_status = H5Dclose(RefFac_id); if( RadFacFlag == 1 ) { H5_status = H5Dclose(RadFac_id); } } else { H5_status = H5Sclose( Rad_space_id ); if( RadFacFlag == 1 ) { H5_status = H5Sclose( RadFac_space_id ); } H5_status = H5Dclose(Rad_id); if( RadFacFlag == 1 ) { H5_status = H5Dclose(RadFac_id); } H5_status = H5Sclose( Tem_space_id ); if( TemFacFlag == 1 ) { H5_status = H5Sclose( TemFac_space_id ); } H5_status = H5Dclose(Tem_id); if( TemFacFlag == 1 ) { H5_status = H5Dclose(TemFac_id); } } H5_status = H5Gclose(dat_group_id); H5_status = H5Fclose(dat_id); /* Close the nav file */ H5_status = H5Sclose( Lat_space_id ); H5_status = H5Sclose( Lon_space_id ); H5_status = H5Sclose( Zen_space_id ); /* if( Lat_id>0 ) H5_status = H5Dclose(Lat_id); if( Lon_id>0 ) H5_status = H5Dclose(Lon_id); if( Zen_id>0 ) H5_status = H5Dclose(Zen_id); if( nav_group_id >0 ) H5_status = H5Gclose(nav_group_id); if( nav_id > 0 ) H5_status = H5Fclose(nav_id); */ /* ******************** DONE WITH HDF5 ************************** */ /* determine the LALO extents */ image_geo_minimum_latitude = Latitude_array[0]; image_geo_maximum_latitude = Latitude_array[0]; image_geo_minimum_longitude = Longitude_array[0]; image_geo_maximum_longitude = Longitude_array[0]; for (i=0; i image_geo_maximum_latitude ) image_geo_maximum_latitude = Latitude_array[k]; if( Longitude_array[k] < image_geo_minimum_longitude ) image_geo_minimum_longitude = Longitude_array[k]; if( Longitude_array[k] > image_geo_maximum_longitude ) image_geo_maximum_longitude = Longitude_array[k]; } } /* ************* Simple HDF5 open and close ****************** */ COORDINATE_TRANSFORMS: (void *)sprintf(trace_string,"%s COORDINATE TRANSFORMS",server); M0sxtrce( trace_string ); /* Resolutions */ image_starting_line = 1; image_starting_element = 1; image_line_resolution = listLfac[MatchX]; image_element_resolution = listEfac[MatchX]; image_geo_line_resolution = listLnav[MatchX]; image_geo_line_resolution = listLnav[MatchX]; image_geo_latitude_resolution = listLnav[MatchX]; image_geo_longitude_resolution = listLnav[MatchX]; image_km_resolution = listRes[MatchX]; /* set request lines if 99999 was passed */ if( request_line_size == 99999 ) request_line_size = image_line_size/request_line_magnification; if( request_element_size == 99999 ) request_element_size = image_element_size/request_element_magnification; /* make sure that request is a multiple of 4 */ extend = request_element_size; test = request_element_size / 4 * 4; if (test != request_element_size ) { request_element_size = ((request_element_size/4)+1) * 4; } sprintf(trace_string, "%s LAT: min=%f max=%f LON: min=%f max=%f", server, image_geo_minimum_latitude, image_geo_maximum_latitude, image_geo_minimum_longitude, image_geo_maximum_longitude ); M0sxtrce( trace_string ); /* check for Earth Centered/Upper load */ if( strncmp( place, "EC",2 ) == 0 || strncmp( place, "EU",2 ) == 0 ) { /* make sure the requested Earth coordinate is in the granual */ if( request_center_latitude < image_geo_minimum_latitude || request_center_latitude > image_geo_maximum_latitude || request_center_longitude < image_geo_minimum_longitude || request_center_longitude > image_geo_maximum_longitude ) { (void)strcpy(requestBlock.errormsg, "Requested Earth location is not contained in the image"); requestBlock.returncode = -30; M0sxdone(&requestBlock); return(0); } /* scan for the closest lat/lon point in geo-fields */ minimum = 999.0; for( i=0; i image_line_size || read_ending_line < 0 || read_starting_element > image_element_size || read_ending_element < 0 ) { (void)strcpy(requestBlock.errormsg, "The portion of the image requested does not exist"); requestBlock.returncode = -30; M0sxdone(&requestBlock); return(0); } GEO_REQEST: (void *)sprintf(trace_string,"%s GEO REQUEST",server); M0sxtrce( trace_string ); /* determine the size of the geo request */ read_geo_line_size = read_line_size * DATAtoGEO_line; if( read_geo_line_size <= 1 ) read_geo_line_size = 2; read_geo_element_size = read_element_size * DATAtoGEO_element; if( read_geo_element_size <= 1 ) read_geo_element_size = 2; /* determine the coordinates of the geo request */ read_geo_starting_line = (read_starting_line-nav_offset) * DATAtoGEO_line; if( read_geo_starting_line < 0 ) read_geo_starting_line = 0; test = (read_geo_starting_line/image_geo_line_magnification)*image_geo_line_magnification; if( test != read_geo_starting_line ) { read_geo_starting_line = test; } read_geo_starting_element = (read_starting_element-nav_offset) * DATAtoGEO_element; if( read_geo_starting_element < 0 ) read_geo_starting_element = 0; test = (read_geo_starting_element/image_geo_element_magnification)*image_geo_element_magnification; if( test != read_geo_starting_element ) { read_geo_starting_element = test; } read_geo_ending_line = read_geo_starting_line + read_geo_line_size - 1; test = (read_geo_ending_line/image_geo_line_magnification)*image_geo_line_magnification; if( test != read_geo_ending_line ) { read_geo_ending_line = test + image_geo_line_magnification; } read_geo_ending_element = read_geo_starting_element + read_geo_element_size - 1; test = (read_geo_ending_element/image_geo_element_magnification)*image_geo_element_magnification; if( test != read_geo_ending_element ) { read_geo_ending_element = test + image_geo_element_magnification; } /* reduce the request to only the valid lat/lon points */ if( read_geo_starting_line < 0 ) read_geo_starting_line = 0; if( read_geo_starting_element < 0 ) read_geo_starting_element = 0; if( read_geo_ending_line >= image_geo_line_size ) read_geo_ending_line = image_geo_line_size-1; if( read_geo_ending_element >= image_geo_element_size ) read_geo_ending_element = image_geo_element_size-1; read_geo_line_size = read_geo_ending_line - read_geo_starting_line + 1; read_geo_element_size = read_geo_ending_element - read_geo_starting_element + 1; (void *)sprintf(trace_string,"%s READ GEO: line size=%d",server,read_geo_line_size); M0sxtrce( trace_string ); (void *)sprintf(trace_string,"%s READ GEO: element size=%d",server,read_geo_element_size); M0sxtrce( trace_string ); (void *)sprintf(trace_string,"%s READ GEO: starting line=%d",server,read_geo_starting_line); M0sxtrce( trace_string ); (void *)sprintf(trace_string,"%s READ GEO: starting element=%d",server,read_geo_starting_element); M0sxtrce( trace_string ); (void *)sprintf(trace_string,"%s READ GEO: ending line=%d",server,read_geo_ending_line); M0sxtrce( trace_string ); (void *)sprintf(trace_string,"%s READ GEO: ending element=%d",server,read_geo_ending_element); M0sxtrce( trace_string ); /* set the line and element sampling rates prior to SwathToGrid check */ lineSampleRate = request_line_magnification; elemSampleRate = request_element_magnification; /* *************************** Transfer starts here ***************************** */ (void *)sprintf(trace_string,"%s TRANSFER",server); M0sxtrce( trace_string ); /* size of a lat/lon transfer array This is a bit complicated because of the size of the Latitude and Longitude tables. The resolution of the Lat/lon is identical to the resolution of the image data (ie., there is 1 Latitude and Longitude for every image element). ADDE has a fixed limit to the size of the LALO naviagtion block (5000000 bytes). That is the combined size of the Latitude and Longitude tables (@ 4 bytes per). So, we actually need to compute the resolution of the LALO block based on the requested data size and resolution. */ for( i=0; i<10; i++ ) { image_geo_line_magnification = image_geo_line_magnification+i; image_geo_element_magnification = image_geo_element_magnification+i; lalo_array_line_size = read_geo_line_size / image_geo_line_magnification; if ( lalo_array_line_size <= 1 ) lalo_array_line_size = 2; lalo_array_elem_size = read_geo_element_size / image_geo_element_magnification; if ( lalo_array_elem_size <= 1 ) lalo_array_elem_size = 2; lalo_array_size = lalo_array_line_size * lalo_array_elem_size * image_geo_data_size; aux_block_size = (VIIRS_LAT_AUX + lalo_array_size ) + (VIIRS_LON_AUX + lalo_array_size ); if( aux_block_size <= MAX_AUXBLOCK_SIZE*4 ) break; } (void *)sprintf(trace_string,"%s lalo_array_size=%d",server,lalo_array_size); M0sxtrce( trace_string ); (void *)sprintf(trace_string,"%s aux_block_size=%d",server,aux_block_size); M0sxtrce( trace_string ); /* size of the line prefix */ if( request_n_bands == 1 ) { psize = 0; } else { psize = request_n_bands / 4; if( (request_n_bands % 4) != 0 ) psize = psize+1; psize = psize * 4; } /* size of the DAT block */ dat_block_size = (read_line_size / request_line_magnification) * ( (read_element_size / request_element_magnification) * request_n_bands * request_data_size + psize ); (void *)sprintf(trace_string,"%s dat_block_size=%d",server,dat_block_size); M0sxtrce( trace_string ); /* send the size of the total transmission */ totalSize = (DIR_SIZE*4) + (NAV_BLOCK_SIZE*4) + (CAL_BLOCK_SIZE*4) + (aux_block_size) + (dat_block_size); (void *)sprintf(trace_string,"%s total_block_size=%d",server,totalSize); M0sxtrce( trace_string ); requestBlock.reply_length += totalSize; M0swbyt4(&totalSize, 1); rc = M0sxsend(4, &totalSize ); /* ***************** DIRECTORY BLOCK ****************** */ /* initialize the directory block */ for( i=0; i 1 ) { directory[14] = psize; directory[50] = psize; for( i=0; i=0 && i=0 && j sector_max_lat ) sector_max_lat = Latitude_array[k]; } if( Longitude_array[k] != -999.0 ) { if( Longitude_array[k] < sector_min_lon ) sector_min_lon = Longitude_array[k]; if( Longitude_array[k] > sector_max_lon ) sector_max_lon = Longitude_array[k]; } } } } /* initialize the navigation block */ for( i=0; i70 ) { sprintf(trace_string, " NAV BLOCK: nav_block[%d]=%d", i, nav_block[i]); M0sxtrce( trace_string ); } } /* ***************** NAVIGATION BLOCK ***************** */ /* **************** CALIBRATION BLOCK ***************** */ /* initialize the calibration block */ for( i=0; i=0 && i=0 && j 90.0 ) NMISS++; } else { float_array_lat[jj] = -999.0; NMISS++; } jj++; } } sprintf(trace_string, " LATITUDE ARRAY[0] = %f", float_array_lat[0]); M0sxtrce( trace_string ); sprintf(trace_string, " LATITUDE ARRAY[jj] = %f", float_array_lat[jj-1]); M0sxtrce( trace_string ); sprintf(trace_string, " LATITUDE ARRAY: NMISS=%d", NMISS); M0sxtrce( trace_string ); /* sample down the Longitude array */ jj = 0; for ( i=starting_line; i<=ending_line; i=i+image_geo_line_magnification ){ for ( j=starting_elem; j<=ending_elem; j=j+image_geo_element_magnification ){ float_array_lon[jj] = -999.0; if( (i>=0 && i=0 && j 1 ) { memcpy( (void *)&uchar_array[0], (void *)&level_map[0], psize ); } } else if( space == 2 ) { ushort_array=malloc(((psize/2)+(request_n_bands*request_element_size))*sizeof(unsigned short int)); if( ushort_array == NULL ) { (void)strcpy(requestBlock.errormsg, "Failed malloc of line array"); requestBlock.returncode = -30; M0sxdone(&requestBlock); return(0); } if( request_n_bands > 1 ) { memcpy( (void *)&ushort_array[0], (void *)&level_map[0], psize ); } } else if( space == 4 ) { ulong_array=malloc(((psize/4)+(request_n_bands*request_element_size))*sizeof(unsigned int)); if( ulong_array == NULL ) { (void)strcpy(requestBlock.errormsg, "Failed malloc of line array"); requestBlock.returncode = -30; M0sxdone(&requestBlock); return(0); } if( request_n_bands > 1 ) { memcpy( (void *)&ulong_array[0], (void *)&level_map[0], psize ); } } /* ONLY 1 Band is allowed */ band = request_bands[0]; sprintf(trace_string, "%s SERVE BAND=%d", server, band); M0sxtrce( trace_string ); /* setup the calibration */ status = kbprep_(&ONE,"VIIR"); if( status == -1 ) { (void)strcpy(requestBlock.errormsg, "Failed to Prep the Calibration"); requestBlock.returncode = -30; M0sxdone(&requestBlock); return(0); } sprintf(trace_string, "%s KBPREP SUCCESS", server); M0sxtrce( trace_string ); /* STEP 1 - initalize the CALibration module */ options_in[0] = 2; options_in[1] = 4; options_in[2] = directory[2]; options_in[3] = request_bands[0]; status = kb1ini_("RAW ", &directory[52], options_in, 4, 4); if( status == -1 ) { (void)strcpy(requestBlock.errormsg, "Failed Calibration: INIT"); requestBlock.returncode = -30; M0sxdone(&requestBlock); return(0); } sprintf(trace_string, "%s CAL INIT SUCCESS", server); M0sxtrce( trace_string ); /* STEP 2 - tell CAL module that Solar Zenith array is available for the Normalized REFlectivity (NREF) */ status = kb1opt_("SZEN", options_in, options_out, 4); if (status == -1) { (void)strcpy(requestBlock.errormsg, "Failed Calibration: SZEN option"); requestBlock.returncode = -30; M0sxdone(&requestBlock); return(0); } sprintf(trace_string, "%s CAL SZEN SUCCESS", server); M0sxtrce( trace_string ); /* STEP 3 - send the calibration coefficents to the DLM */ status = kb1opt_("CALB", cal_block, options_out, 4); if (status == -1) { (void)strcpy(requestBlock.errormsg, "Failed Calibration: CALB option"); requestBlock.returncode = -30; M0sxdone(&requestBlock); return(0); } sprintf(trace_string, "%s CAL CALB SUCCESS", server); M0sxtrce( trace_string ); sprintf(trace_string, "%s unit %s %s", server, unit, csstr); M0sxtrce( trace_string ); if( strncmp( unit, "BRIT",4 ) == 0 && strncmp( csstr, "EXP" ,3) == 0) { options_in[0] = band; options_in[1] = listSat[MatchX]; options_in[2] = 1; sprintf(trace_string, "options_in %d %d", band, listSat[MatchX]); M0sxtrce( trace_string ); status = kb1opt_("STR ", options_in, options_out, 4); sprintf(trace_string, "kb1opt status %d", status); M0sxtrce( trace_string ); if(status == 0) { directory[EXP_BRIT_FLAG] = 1; memcpy(&directory[EXP_BRIT_NAME1], options_out, 8); } } /* STEP 4A - Bands 8,9,10 and 12 Floating point Radiance into Band_BT_array */ if( band==8 || band==9 || band==10 || band==12 ) { if( ounit_flag == 2 ){ for( k=0; k 0 ) { /* send the calibration */ M0swbyt4( cal_block, CAL_BLOCK_SIZE ); rc = M0sxsend( CAL_BLOCK_SIZE*4, cal_block ); sprintf(trace_string, "%s sent calibration: bytes=%d", server,CAL_BLOCK_SIZE*4); M0sxtrce( trace_string ); /* switch the byte order back */ M0swbyt4( cal_block, CAL_BLOCK_SIZE ); } /* Send AUX block */ /* LATITUDE entry */ aux_head[0] = AUX_MAGIC_NUMBER; aux_head[1] = VIIRS_LAT_AUX + lalo_array_size; aux_head[2] = 8; aux_head[3] = lalo_array_size; memcpy( (void *)&aux_head[4], (void *)"LATITUDE", 8); M0swbyt4( aux_head, 6); rc = M0sxsend( 24, aux_head ); M0swbyt4( float_array_lat, lalo_array_size/4); rc = M0sxsend(lalo_array_size, float_array_lat); sprintf(trace_string, "%s sent latitude: bytes=%d", server,lalo_array_size); M0sxtrce( trace_string ); /* LONGITUDE entry */ aux_head[0] = AUX_MAGIC_NUMBER; aux_head[1] = VIIRS_LON_AUX + lalo_array_size; aux_head[2] = 12; aux_head[3] = lalo_array_size; memcpy( (void *)&aux_head[4], (void *)"LONGITUDE ", 12); M0swbyt4( aux_head, 7 ); rc = M0sxsend( 28, aux_head ); M0swbyt4( float_array_lon, lalo_array_size/4); rc = M0sxsend(lalo_array_size, float_array_lon); sprintf(trace_string, "%s sent longitude: bytes=%d", server,lalo_array_size); M0sxtrce( trace_string ); free( Latitude_array ); free( Longitude_array ); free( float_array_lat ); free( float_array_lon ); /* End of sending header blocks */ sprintf(trace_string, "%s READ LINE: START=%d END=%d BY=%d", server, read_starting_line, read_ending_line, lineSampleRate); M0sxtrce( trace_string ); sprintf(trace_string, "%s READ ELEM: START=%d END=%di BY=%d ", server, read_starting_element, read_ending_element, elemSampleRate); M0sxtrce( trace_string ); /* LINE loop */ for( il=read_starting_line; il<=read_ending_line; il=il+lineSampleRate ) { /* ELEMENT loop */ jj = psize; nzero = 0; ndata = 0; for( ie=read_starting_element; ie<=read_ending_element; ie=ie+elemSampleRate ) { /* start with a NUll value */ value = 0; /* validate the image array index */ if( ( il>=0 && il=0 && ie