intn write_attr(char *fname) { int32 sd_id, sds_id; intn status; /* * Open the file and initialize the SD interface. */ sd_id = SDstart (fname, DFACC_WRITE); if (sd_id == FAIL) { fprintf(stderr, "SDstart() failed.\n"); return -1; } /* Get the identifier for the data set "Latitude". */ sds_id = SDselect(sd_id, 0); if (sds_id == FAIL) { fprintf(stderr, "SDselect() failed.\n"); return -1; } /* Set an attribute. */ status = SDsetattr(sds_id, "units", DFNT_CHAR8, 13, (VOIDP)"degrees_north"); if (status == FAIL) { fprintf(stderr, "SDsetattr() failed.\n"); return -1; } /* * Terminate access to the data set. */ status = SDendaccess (sds_id); /* Get the identifier for the data set "Longitude". */ sds_id = SDselect(sd_id, 1); if (sds_id == FAIL) { fprintf(stderr, "SDselect() failed.\n"); return -1; } /* Set an attribute. */ status = SDsetattr(sds_id, "units", DFNT_CHAR8, 12, (VOIDP)"degrees_east"); if (status == FAIL) { fprintf(stderr, "SDsetattr() failed.\n"); return -1; } /* * Terminate access to the data set. */ status = SDendaccess (sds_id); /* * Terminate access to the SD interface and close the file. */ status = SDend (sd_id); return 0; }
/* Given the file ID and variable ID, retrieve information about the * specified attribute. Return success (0) if the attribute information * was obtained sucessfully, otherwise this function returns the failure * status (-1). */ int cuattinq_hdf(CuFile* file, int varid, const char* name, CuType* datatype, int* len){ int err, saveopts; hdf_type dtype; int t_len; int32 sds_id, attr_index; char attr_name[H4_MAX_NC_NAME]; /* Get the identifier for the first data set or file. */ if (varid == CU_GLOBAL) sds_id = file->internid1; else sds_id = SDselect(file->internid1, varid); /* Find the data set attribute name index. */ attr_index = SDfindattr(sds_id, name); /* Get information about the data set attribute. */ if((err = SDattrinfo(sds_id, attr_index, attr_name, &dtype, (len ? len : &t_len))) != -1) if(datatype) { cumapdatatype_hdf(dtype, datatype); if (*datatype==CuInvalidType) return -1; } /* Return success ( 0 ), or failure ( -1 ). */ return (err == -1 ? -1 : CU_SUCCESS); }
scan_sd(string infile) { int id, sds, n, i, j, k, ret, size, type, na, nd; char name[256]; id = SDstart(infile, DFACC_RDONLY); if (id<0) error("%s is probably not an HDF SD (scientific dataset)", infile); ret = SDfileinfo(id, &nd, &na); dprintf(0,"Found %d scientific data set%s in %s with %d attributes\n", nd,(nd > 1 ? "s" : ""),infile, na); for (k=0;;k++) { sds = SDselect(id, nd); ret = SDgetinfo(sds, name, &rank, shape, &type, &na); label[0] = unit[0] = fmt[0] = coordsys[0] = 0; ret = SDgetdatastrs(sds, label, unit, fmt, coordsys, 256); dprintf(0,"%d: %s(",k,label); for (i=0, size=1; i<rank; i++) { if (i==rank-1) dprintf(0,"%d)",shape[i]); else dprintf(0,"%d,",shape[i]); size *= shape[i]; } dprintf(0," %s ",unit); dprintf(0," -> [%d elements of type: %d]\n", size, type); /* ret = SDendaccess(sds); */ } SDend(id); }
int list_sds(int32 infile_id, int32 outfile_id, int32 sd_id, int32 sd_out, list_table_t *list_tbl, dim_table_t *td1, dim_table_t *td2, options_t *options) { int32 sds_id, /* dataset identifier */ n_datasets, /* number of datasets in the file */ n_file_attrs, /* number of file attributes */ index, /* index of a dataset */ sds_ref, /* reference number */ dim_sizes[H4_MAX_VAR_DIMS],/* dimensions of an image */ data_type, /* number type */ rank, /* rank */ n_attrs; /* number of attributes */ char name[H4_MAX_GR_NAME]; /* name of dataset */ /* determine the number of data sets in the file and the number of file attributes */ if (SDfileinfo (sd_id, &n_datasets, &n_file_attrs)==FAIL){ printf("Could not get SDS info\n"); return FAIL; } for (index = 0; index < n_datasets; index++) { sds_id = SDselect (sd_id, index); SDgetinfo(sds_id, name, &rank, dim_sizes, &data_type, &n_attrs); sds_ref = SDidtoref(sds_id); /* check if already inserted in Vgroup; search all SDS tags */ if ( list_table_search(list_tbl,DFTAG_SD,sds_ref)>=0 || list_table_search(list_tbl,DFTAG_SDG,sds_ref)>=0 || list_table_search(list_tbl,DFTAG_NDG,sds_ref)>=0 ) { SDendaccess (sds_id); continue; } /* copy SDS */ if (copy_sds(sd_id,sd_out,TAG_GRP_DSET,sds_ref,0,NULL,options,list_tbl,td1,td2, infile_id,outfile_id)<0) goto out; /* terminate access to the current dataset */ SDendaccess (sds_id); } return SUCCEED; out: SDendaccess (sds_id); return FAIL; }
bool GetSDSInfo(int32 sds_file_id, Myhdf_sds_t *sds) /* !C****************************************************************************** !Description: 'GetSDSInfo' reads information for a specific SDS. !Input Parameters: sds_file_id SDS file id !Output Parameters: sds SDS data structure; the following fields are updated: index, name, id, rank, type, nattr (returns) Status: 'true' = okay 'false' = error reading the SDS information !Team Unique Header: ! Design Notes: 1. An error status is returned if the SDS rank is greater than 'MYHDF_MAX_RANK'. 2. On normal returns the SDS is selected for access. 3. The HDF file is assumed to be open for SD (Science Data) access. 4. Error messages are handled with the 'RETURN_ERROR' macro. !END**************************************************************************** */ { int32 dims[MYHDF_MAX_RANK]; // printf("SDS name %s\n",sds->name); sds->index = SDnametoindex(sds_file_id, sds->name); if (sds->index == HDF_ERROR) RETURN_ERROR("getting sds index", "GetSDSInfo", false); sds->id = SDselect(sds_file_id, sds->index); if (sds->id == HDF_ERROR) RETURN_ERROR("getting sds id", "GetSDSInfo", false); if (SDgetinfo(sds->id, sds->name, &sds->rank, dims, &sds->type, &sds->nattr) == HDF_ERROR) { SDendaccess(sds->id); RETURN_ERROR("getting sds information", "GetSDSInfo", false); } if (sds->rank > MYHDF_MAX_RANK) { SDendaccess(sds->id); RETURN_ERROR("sds rank too large", "GetSDSInfo", false); } return true; }
int create_hdf_file(int dtype) { int32 sd_id, sds_id, istat, sd_index; int32 dims[2], start[2], edges[2], rank; int16 array_data[DIM0][DIM1]; intn i, j, count; start[0] = 0; start[1] = 0; edges[0] = DIM1; edges[1] = DIM0; // populate data array count = 0; for (j = 0; j < DIM0; j++) { for (i = 0; i < DIM1; i++) array_data[j][i] = count++; } printf("\to Creating hdf4 file with little-endian datatype %d....\t",dtype); sd_id = SDstart(FILENAME, DFACC_CREATE); //sds_id = SDcreate(sd_id, SDSNAME, DFNT_LITEND|dtype, RANK, edges); sds_id = SDcreate(sd_id, SDSNAME, dtype, RANK, edges); istat = SDendaccess(sds_id); if(istat) {printf("Failure %d\n", istat); SDend(sd_id); return istat;} istat = SDend(sd_id); if(istat) {printf("Failure %d\n", istat); SDend(sd_id); return istat;} sd_id = SDstart(FILENAME, DFACC_WRITE); sd_index = 0; sds_id = SDselect(sd_id, sd_index); istat = SDwritedata(sds_id, start, NULL, edges, (VOIDP)array_data); if(istat) {printf("Failure %d\n", istat); SDend(sd_id); return istat;} istat = SDendaccess(sds_id); if(istat) {printf("Failure %d\n", istat); SDend(sd_id); return istat;} istat = SDend(sd_id); if(istat) {printf("Failure %d\n", istat); return istat;} printf("Success\n"); return 0; }
/* Given the file ID and variable ID, get information about the * data set attribute. */ int cuattname_hdf(CuFile* file, int varid, int attnum, char* name){ int32 sds_id, num_type, count, status; char attr_name[H4_MAX_NC_NAME]; /* Get the identifier for the data set or file. */ if (varid == CU_GLOBAL) sds_id = file->internid1; else sds_id = SDselect(file->internid1, varid); /* Get information about the data set attribute. */ status = SDattrinfo(sds_id, attnum, name, &num_type, &count); /* Return success ( 0 ), or failure ( -1 ). */ return (status == -1 ? -1 : CU_SUCCESS); }
bool ossimHdf4SubDataset::open() { static const char MODULE[] = "ossimHdf4SubDataset::open"; bool result = false; m_sd_id = SDstart(m_fileName.c_str(), DFACC_READ); if (m_sd_id < 0) { result = false; } else { result = true; } m_sds_id = SDselect(m_sd_id, m_sds_index); if (m_sds_id < 0) { result = false; } else { result = true; } if (result == false) { SDendaccess (m_sds_id); SDend (m_sd_id); } initMeta(); if (traceDebug()) { ossimNotify(ossimNotifyLevel_DEBUG) << MODULE << " exit status = " << (result?"true":"false\n") << std::endl; } return result; }
int main(int argc, char *argv[]){ char filename[100], sds_name[100][MAX_NUM_SDS]; int32 file_ID, sds_ID, num_sds, num_file_attribute; int which_sds=0, i; char name[100], type_name[100]; int32 num_dim, dim_size[MAX_NUM_DIM]; int32 data_type, num_sds_attribute; strcpy( filename, "2B31.070101.52026.6.HDF"); /* * Read Data */ file_ID = SDstart( filename, DFACC_READ ); sds_ID = SDfileinfo( file_ID, &num_sds, &num_file_attribute ); printf( "\n there are %d SDSs in the file.\n", num_sds ); for( which_sds=0 ; which_sds<num_sds ; which_sds++ ){ sds_ID = SDselect( file_ID, which_sds ); SDgetinfo( sds_ID, name, &num_dim, dim_size, &data_type, &num_sds_attribute ); SDendaccess( sds_ID ); switch (data_type){ case 5: strcpy( type_name, "float32" ); break; case 20: strcpy( type_name, "int8" ); break; case 22: strcpy( type_name, "int16" ); break; case 24: strcpy( type_name, "int32" ); break; default: strcpy( type_name, "other" ); break; } strcpy( sds_name[which_sds], name ); printf( "%3d %10s %20s(", which_sds, type_name, sds_name[which_sds] ); for(i=0;i<num_dim;i++){ printf("%5d, ", dim_size[i]); } printf( ")\n" ); } SDend( file_ID ); exit(0); }
void hdf_get_sds( char *filename, char *sds_name, int *start, int *stride, int *edge, void *data, /* area for input data */ int *status) /* see above */ { int32 edge32[MAX_VAR_DIMS]; int32 i; char name[MAX_NC_NAME]; int32 natts; /* # attributes */ int32 number_type; /* HDF type */ int32 rank; int s; /* error status */ int32 sd_id; /* file handle */ int32 sds_id; /* SDS handle */ int32 sds_index; /* SDS index */ int32 shape32[MAX_VAR_DIMS]; int32 start32[MAX_VAR_DIMS]; int32 stride32[MAX_VAR_DIMS]; sd_id = SDstart(filename, DFACC_RDONLY); CHECK(sd_id >= 0, 1); sds_index = SDnametoindex(sd_id, sds_name); CHECK(sds_index >= 0, 2); sds_id = SDselect(sd_id, sds_index); CHECK(sds_id >= 0, 3); s = SDgetinfo(sds_id, name, &rank, shape32, &number_type, &natts); CHECK(s == SUCCEED, 4); for (i = 0; i < rank; i++) { start32[i] = start[i]; stride32[i] = stride[i]; edge32[i] = edge[i]; } s = SDreaddata(sds_id, start32, stride32, edge32, data); CHECK(s == SUCCEED, 5); s = SDend(sd_id); CHECK(s == SUCCEED, 6); *status = 0; }
/* Given the file ID, variable ID, and name of the dimension, return * the dimension ID. */ int cudimid_hdf(CuFile* file, int varid, const char* name){ int32 sds_idx, sds_id; /* Search for the index of the named array data set. */ if ((sds_idx=SDnametoindex(file->internid1, name)) == -1) { CuError(CU_DRIVER,"Obtaining dataset in file %s",file->controlpath); cuerrorreport_hdf(); return -1; } /* Select the data set corresponding to the returned index. */ if ((sds_id=SDselect(file->internid1, sds_idx)) == -1) { CuError(CU_DRIVER,"Obtaining dataset in file %s",file->controlpath); cuerrorreport_hdf(); return -1; } /* Return dimension ID or failure ( -1 ). */ return (SDgetdimid(sds_id, 0) - file->internid2); /* pass back the dimension id */ }
void RemapHDF4::getDepthSlice(int slc, uchar *slice) { int nbytes = m_width*m_height*m_bytesPerVoxel; int32 start[2], edges[2]; start[0] = start[1] = 0; edges[0] = m_width; edges[1] = m_height; int32 sd_id = SDstart(m_imageList[slc].toAscii().data(), DFACC_READ); int32 sds_id = SDselect(sd_id, m_Index); int status = SDreaddata(sds_id, start, NULL, edges, (VOIDP)slice); status = SDendaccess(sds_id); status = SDend(sd_id); }
/* Retrieve the variable values. Return success (0) if the * variable values were obtained sucessfully, otherwise this * function returns the failure status (-1). */ int cuvarget_hdf(CuFile* file, int varid, const long start[], const long count[], void* values){ int32 sds_id, i, ndims; int32 *startvalues, *edges, natts; int32 dim_sizes[H4_MAX_VAR_DIMS]; char name[H4_MAX_NC_NAME+1]; hdf_type dtype; /* Get the identifier for the data set. */ sds_id = SDselect(file->internid1, varid); /* Get the number of dimensions. */ if (SDgetinfo(sds_id, name, &ndims, dim_sizes, &dtype, &natts) == -1) { cuerrorreport_hdf(); return (-1); } /* Define dimension size */ startvalues = (int32 *)malloc((ndims)*sizeof(int32)); edges = (int32 *)malloc((ndims)*sizeof(int32)); for (i = 0; i < ndims; ++i) { startvalues[i] = start[i]; edges[i] = count[i]; } /* Read the data array. */ if (SDreaddata(sds_id, startvalues, NULL, edges, (VOIDP)values) == -1) { cuerrorreport_hdf(); return (-1); } free ((char *) startvalues); free ((char *) edges); /* Return success ( 0 ). */ return (CU_SUCCESS); }
static H4Buffer *prep_read_buffer(SDSVarInfo *var, void **bufp) { H4Buffer *buf = (H4Buffer *)*bufp; if (buf) { assert(buf->free == (void (*)(void *))h4buffer_free); } else { *((H4Buffer **)bufp) = buf = h4buffer_create(var->sds); } if (buf->sds_index != var->id) { if (buf->sds_id != -1) { // close other var int status = SDendaccess(buf->sds_id); CHECK_HDF_ERROR(buf->path, status); } // open this var buf->sds_id = SDselect(var->sds->id, var->id); CHECK_HDF_ERROR(var->sds->path, buf->sds_id); buf->sds_index = var->id; } return buf; }
/* Given the file ID, variable ID, and attribute name, retrieve attribute * values. Return success (0) if the attribute information was obtained * sucessfully, otherwise this function returns the failure status (-1). */ int cuattget_hdf(CuFile* file, int varid, const char* name, void* value){ int32 sds_id, attr_index, num_type, count; int32 status; int8 *buffer; char attr_name[H4_MAX_NC_NAME]; /* Get the identifier for the data set or file. */ if (varid == CU_GLOBAL) sds_id = file->internid1; else sds_id = SDselect(file->internid1, varid); /* Find the data set attribute name index. */ attr_index = SDfindattr(sds_id, name); /* Get information about the data set attribute. */ SDattrinfo(sds_id, attr_index, attr_name, &num_type, &count); /* Read the attribute data and return success ( 0 ), * or failure ( -1 ). */ return (SDreadattr(sds_id, attr_index, value) == -1 ? -1 : CU_SUCCESS); }
/* Utility routine that selects that named SDS, then calls SDgetdatasize * and verifies the returned values. */ static void check_datasizes( int32 fid, /* file id */ char* sds_name, /* name of the inquired SDS */ int32 comp_size_check, /* expected compressed data size */ int32 uncomp_size_check,/* expected non-compressed data size */ int* ret_num_errs /* current number of errors */ ) { int32 sds_id, sds_index; intn status; int num_errs = 0; char mesg[80]; int32 uncomp_size=0, comp_size=0; /* Get index of dataset using its name */ sds_index = SDnametoindex(fid, sds_name); sprintf(mesg, "check_datasizes: SDnametoindex (%s)", sds_name); CHECK(sds_index, FAIL, mesg); /* Select the dataset */ sds_id = SDselect(fid, sds_index); CHECK(sds_id, FAIL, "check_datasizes: SDselect"); /* The retrieved values from SDgetdatasize are verified against 'comp_size_check' and 'uncomp_size_check' */ status = SDgetdatasize(sds_id, &comp_size, &uncomp_size); sprintf(mesg, "SDgetdatasize: SDS named (%s)", sds_name); CHECK(status, FAIL, mesg); VERIFY(comp_size, comp_size_check, mesg); VERIFY(uncomp_size, uncomp_size_check, mesg); /* Close this SDS */ status = SDendaccess(sds_id); CHECK(status, FAIL, "check_datasizes: SDendaccess"); *ret_num_errs = num_errs; } /* check_datasizes */
/* Get information about the variable. Return success (0) if the * variable information was obtained sucessfully, otherwise this * function returns the failure status (-1). */ int cuvarinq_hdf(CuFile* file, int varid, char* name, CuType* datatype, int* ndims, int dimids[], int* natts){ int err, i; hdf_type dtype; char t_name[H4_MAX_NC_NAME+1]; int t_ndims, t_natts, numdims; int t_dimids[H4_MAX_VAR_DIMS]; int32 sds_id, index; int32 dim_sizes[H4_MAX_VAR_DIMS]; /* Select the data set corresponding to the returned index. */ sds_id = SDselect(file->internid1, varid); /* Get the variable information. */ if ((err=SDgetinfo(sds_id, (name ? name : t_name), (ndims ? ndims : &t_ndims), dim_sizes, &dtype, (natts ? natts : &t_natts))) != -1) { if(datatype) { cumapdatatype_hdf(dtype,datatype); if (*datatype==CuInvalidType) return -1; } /* Retrieve dimension IDs. */ if (dimids) { numdims = (ndims ? *ndims : t_ndims); for (i=0; i < numdims; ++i) /* reverse the order */ dimids[i] = SDgetdimid (sds_id, i) - file->internid2; } } /* Return variable ID, or failure ( -1 ). */ return (err==-1 ? -1 : CU_SUCCESS); }
int main() { /************************* Variable declaration **************************/ int32 sd_id, sds_id, sds_index; intn status; int32 dim_sizes[2]; int32 data[Y_LENGTH][X_LENGTH], append_data[X_LENGTH]; int32 start[2], edges[2]; int i, j; /********************* End of variable declaration ***********************/ /* * Data initialization. */ for (j = 0; j < Y_LENGTH; j++) { for (i = 0; i < X_LENGTH; i++) data[j][i] = (i + 1) + (j + 1); } /* * Create the file and initialize the SD interface. */ sd_id = SDstart (FILE_NAME, DFACC_CREATE); /* * Define dimensions of the array. Make the first dimension * appendable by defining its length to be unlimited. */ dim_sizes[0] = SD_UNLIMITED; dim_sizes[1] = X_LENGTH; /* * Create the array data set. */ sds_id = SDcreate (sd_id, SDS_NAME, DFNT_INT32, RANK, dim_sizes); /* * Define the location and the size of the data to be written * to the data set. */ start[0] = start[1] = 0; edges[0] = Y_LENGTH; edges[1] = X_LENGTH; /* * Write the data. */ status = SDwritedata (sds_id, start, NULL, edges, (VOIDP)data); /* * Terminate access to the array data set, terminate access * to the SD interface, and close the file. */ status = SDendaccess (sds_id); status = SDend (sd_id); /* * Store the array values to be appended to the data set. */ for (i = 0; i < X_LENGTH; i++) append_data[i] = 1000 + i; /* * Reopen the file and initialize the SD interface. */ sd_id = SDstart (FILE_NAME, DFACC_WRITE); /* * Select the first data set. */ sds_index = 0; sds_id = SDselect (sd_id, sds_index); /* * Check if selected SDS is unlimited. If it is not, then terminate access * to the SD interface and close the file. */ if ( SDisrecord (sds_id) ) { /* * Define the location of the append to start at the first column * of the 11th row of the data set and to stop at the end of the * eleventh row. */ start[0] = Y_LENGTH; start[1] = 0; edges[0] = 1; edges[1] = X_LENGTH; /* * Append data to the data set. */ status = SDwritedata (sds_id, start, NULL, edges, (VOIDP)append_data); } /* * Terminate access to the data set. */ status = SDendaccess (sds_id); /* * Terminate access to the SD interface and close the file. */ status = SDend (sd_id); return 0; }
int main(int argc, char *argv[]) { char *MOD021KMfile, *MOD02HKMfile, *MOD02QKMfile; char *filename; /* output file */ FILE *fp; int outfile_exists; char *ancpath; SDS sds[Nitems], outsds[Nbands], dem, height; int32 MOD02QKMfile_id, MOD02HKMfile_id, MOD021KMfile_id; int32 sd_id, attr_index, count, num_type; int ib, j, iscan, Nscans, irow, jcol, idx, crsidx; int nbands; char *SDSlocatorQKM[Nitems] = {"EV_250_RefSB", "EV_250_RefSB", "EV_500_RefSB", "EV_500_RefSB", "EV_500_RefSB", "EV_500_RefSB", "EV_500_RefSB","EV_1KM_RefSB", "EV_1KM_RefSB", "EV_1KM_RefSB", "EV_1KM_RefSB", "EV_1KM_RefSB", "EV_1KM_RefSB", "EV_1KM_RefSB", "EV_1KM_RefSB", "EV_1KM_RefSB", "SolarZenith", "SensorZenith", "SolarAzimuth", "SensorAzimuth", "Longitude", "Latitude"}; char *SDSlocatorHKM[Nitems] = {"EV_500_RefSB", "EV_500_RefSB", "EV_500_RefSB", "EV_500_RefSB", "EV_500_RefSB", "EV_500_RefSB", "EV_500_RefSB", "Reflectance_Img_I1","Reflectance_Img_I2","Reflectance_Img_I3", "EV_1KM_RefSB","EV_1KM_RefSB","EV_1KM_RefSB", "EV_1KM_RefSB", "EV_1KM_RefSB", "EV_1KM_RefSB","SolZenAng_Mod", "SenZenAng_Mod", "SolAziAng_Mod", "SenAziAng_Mod", "Longitude", "Latitude" }; char *SDSlocator1KM[Nitems] = {"Reflectance_Mod_M5", "Reflectance_Mod_M7", "Reflectance_Mod_M3", "Reflectance_Mod_M4", "Reflectance_Mod_M8", "Reflectance_Mod_M10", "Reflectance_Mod_M11", "EV_1KM_RefSB", "EV_1KM_RefSB", "EV_1KM_RefSB", "EV_1KM_RefSB", "EV_1KM_RefSB", "EV_1KM_RefSB", "EV_1KM_RefSB", "EV_1KM_RefSB", "EV_1KM_RefSB", "SolZenAng_Mod", "SenZenAng_Mod", "SolAziAng_Mod", "SenAziAng_Mod", "Longitude", "Latitude"}; char indexlocator[Nitems] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 5, 7, 9, 10, 0, 0, 0, 0, 0, 0}; char numtypelocator[Nitems] = {DFNT_UINT16, DFNT_UINT16, DFNT_UINT16, DFNT_UINT16, DFNT_UINT16, DFNT_UINT16, DFNT_UINT16, DFNT_UINT16, DFNT_UINT16, DFNT_UINT16, DFNT_UINT16, DFNT_UINT16, DFNT_UINT16, DFNT_UINT16, DFNT_UINT16, DFNT_UINT16,DFNT_FLOAT32 ,DFNT_FLOAT32 ,DFNT_FLOAT32 ,DFNT_FLOAT32 , DFNT_FLOAT32, DFNT_FLOAT32}; uint16 *l1bdata[Nbands]; float32 *sola, *solz, *sena, *senz, *solzfill; float32 *lon, *lat, *lonfill, *latfill; char *attr_name; float64 scale_factor[Nitems], add_offset[Nitems]; unsigned char process[Nbands]; float refl, *mus, muv, phi; float *rhoray, *sphalb, *TtotraytH2O, *tOG; int aggfactor, crsrow1, crsrow2, crscol1, crscol2; int crsidx11, crsidx12, crsidx21, crsidx22; float mus0, mus11, mus12, mus21, mus22; float fractrow, fractcol, t, u; float rhoray0, rhoray11, rhoray12, rhoray21, rhoray22; float sphalb0, sphalb11, sphalb12, sphalb21, sphalb22; float reflmin=REFLMIN, reflmax=REFLMAX, maxsolz=MAXSOLZ; int bad; int write_mode = DFACC_CREATE; int st; size_t nbytes; int ftype; extern char *optarg; extern int optind, opterr; int option_index = 0; static int verbose, overwrite; static int gzip, append; static int output500m, output1km; static int sealevel, TOA, nearest; char dummy[H4_MAX_NC_NAME]; enum{OPT_BANDS = 1, OPT_RANGE, OPT_OUTFILE, OPT_MAXSOLZ}; static struct option long_options[] = { {"1km", no_argument, &output1km, 1}, {"500m", no_argument, &output500m, 1}, {"append", no_argument, &append, 1}, {"bands", required_argument, (int *) NULL, OPT_BANDS}, {"gzip", no_argument, &gzip, 1}, {"maxsolz", required_argument, (int *) NULL, OPT_MAXSOLZ}, {"nearest", no_argument, &nearest, 1}, {"of", required_argument, (int *) NULL, OPT_OUTFILE}, {"overwrite", no_argument, &overwrite, 1}, {"range", required_argument, (int *) NULL, OPT_RANGE}, {"sealevel", no_argument, &sealevel, 1}, {"toa", no_argument, &TOA, 1}, {"verbose", no_argument, &verbose, 1}, {(char *) NULL, 0, (int *) NULL, 0} }; int c; static char dem_filename_buff[MAXNAMELENGTH]; MOD021KMfile = MOD02HKMfile = MOD02QKMfile = (char *) NULL; filename = (char *) NULL; for (ib = 0; ib < Nbands; ib++) process[ib] = FALSE; /* default settings */ output500m = output1km = 0; append = gzip = nearest = sealevel = TOA = verbose = overwrite = 0; while ((c = getopt_long(argc, argv, "", long_options, &option_index)) >= 0) { switch (c) { case 0: /* do nothing for options which will have a flag set automatically by getopt_long() */ break; case OPT_BANDS: if (parse_bands(optarg, process)) { fputs("Invalid band(s) specified.\n", stderr); exit(1); } break; case OPT_RANGE: if (sscanf(optarg, "%g,%g", &reflmin, &reflmax) != 2) { fputs("Error parsing reflectance range.\n", stderr); exit(1); } if ( range_check(reflmin, 0.0F, 1.0F) || range_check(reflmax, 0.0F, 1.0F) || (reflmin >= reflmax) ) { fputs("Invalid reflectance range.\n", stderr); exit(1); } printf("Output reflectance range [%.3f,%.3f] requested.\n", reflmin, reflmax); break; case OPT_MAXSOLZ: maxsolz = (float) atof(optarg); if (range_check(maxsolz, 0.0F, 90.0F)) { fputs("Invalid max. solar zenith angle.\n", stderr); exit(1); } break; case OPT_OUTFILE: filename = optarg; break; default: usage(); exit(1); } } if (append) write_mode = DFACC_RDWR; /* at least one input file must follow */ if (optind >= argc) { usage(); exit(1); } /* check for conflicting options */ if (overwrite && append) { fputs("Options --overwrite and --append are mutually exclusive.\n", stderr); exit(1); } if (sealevel && TOA) { fputs("Options --sealevel and --toa are mutually exclusive.\n", stderr); exit(1); } #ifdef DEBUG printf("append = %d\n", append); if (filename) printf("output filename = %s\n", filename); printf("output1km = %d\n", (int) output1km); printf("output500m = %d\n", (int) output500m); printf("gzip = %d\n", gzip); printf("nearest = %d\n", nearest); printf("sealevel = %d\n", sealevel); printf("TOA = %d\n", TOA); printf("Max. solar zenith angle: %g degrees\n", maxsolz); if (filename) printf("Output file: %s.", filename); #endif if (verbose) puts("Verbose mode requested."); if (overwrite) puts("Overwriting existing output file."); if (gzip) puts("Gzip compression requested."); if (sealevel) puts("Sea-level atmospheric correction requested. Terrain height ignored."); if (TOA) puts("Top-of-the-atmosphere reflectance requested. No atmospheric correction."); if (output1km) puts("1km-resolution output requested."); if (nearest) puts("Interpolation disabled."); /* parse input file names */ for (j = optind; j < argc; j++) { ftype = input_file_type(argv[j]); switch (ftype) { case INPUT_1KM: MOD021KMfile = argv[j]; break; case INPUT_500M: MOD02HKMfile = argv[j]; break; case INPUT_250M: MOD02QKMfile = argv[j]; break; default: fprintf(stderr, "Unrecognized input file \"%s\".\n", argv[j]); MOD021KMfile = argv[j]; /* exit(1); I commented that*/ break; } } if (verbose && MOD021KMfile) printf("Input geolocation file: %s\n", MOD021KMfile); /* output file name is mandatory */ if (!filename) { fputs("Missing output file name.\n", stderr); exit(1); } #ifdef DEBUG if (MOD021KMfile) printf("MOD/MYD021KMfile = %s\n", MOD021KMfile); if (MOD02HKMfile) printf("MOD/MYD02HKMfile = %s\n", MOD02HKMfile); if (MOD02QKMfile) printf("MOD/MYD02QKMfile = %s\n", MOD02QKMfile); #endif /* 1KM file is mandatory for angles. HKM file is mandatory unless 1-km output is requested. QKM file is mandatory unless 500-m or 1-km output is requested. */ /* if ( (!MOD021KMfile) || (!MOD02HKMfile && !output1km) || (!MOD02QKMfile && !output500m && !output1km) ) { fputs("Invalid combination of input files.\n", stderr); exit(1); } commented that too Eric*/ /* count number of bands to process */ for (ib = nbands = 0; ib < Nbands; ib++) if (process[ib]) nbands++; if (nbands < 1) { process[BAND1] = process[BAND3] = process[BAND4] = TRUE; if (verbose) puts("No band(s) specified. Default is bands 1, 3, and 4."); } /* open input files */ if ( MOD02QKMfile && (!output500m) && !output1km && (MOD02QKMfile_id = SDstart(MOD02QKMfile, DFACC_READ)) == -1 ) { fprintf(stderr, "Cannot open input file %s.\n", MOD02QKMfile); exit(1); } if ( MOD02HKMfile && (!output1km) && (MOD02HKMfile_id = SDstart(MOD02HKMfile, DFACC_READ)) == -1 ) { fprintf(stderr, "Cannot open input file %s.\n", MOD02HKMfile); exit(1); } if ( MOD021KMfile && (MOD021KMfile_id = SDstart(MOD021KMfile, DFACC_READ)) == -1 ) { fprintf(stderr, "Cannot open input file %s.\n", MOD021KMfile); exit(1); } if (!sealevel && !TOA) { dem.filename = dem_filename_buff; if ((ancpath = getenv("ANCPATH")) == NULL) sprintf(dem.filename, "%s/%s", ANCPATH, DEMFILENAME); else sprintf(dem.filename, "%s/%s", ancpath, DEMFILENAME); if ( (dem.file_id = SDstart(dem.filename, DFACC_READ)) == -1 ) { fprintf(stderr, "Cannot open file %s.\n", dem.filename); exit(1); } } if ( (fp = fopen(filename, "r")) ) { (void) fclose(fp); outfile_exists = 1; } else outfile_exists = 0; if ((write_mode == DFACC_CREATE) && !overwrite && outfile_exists) { fprintf(stderr, "File \"%s\" already exits.\n", filename); exit(1); } if (output500m) { sds[BAND10].file_id =sds[BAND8].file_id = sds[BAND9].file_id = MOD02HKMfile_id; sds[BAND10].filename =sds[BAND8].filename = sds[BAND9].filename = MOD02HKMfile; } else { if (output1km) { sds[BAND1].file_id = sds[BAND2].file_id = MOD021KMfile_id; sds[BAND1].filename = sds[BAND2].filename = MOD021KMfile; } else { sds[BAND1].file_id = sds[BAND2].file_id = MOD02QKMfile_id; sds[BAND1].filename = sds[BAND2].filename = MOD02QKMfile; } } if (output1km) { sds[BAND3].file_id = sds[BAND4].file_id = sds[BAND5].file_id = sds[BAND6].file_id = sds[BAND7].file_id = MOD021KMfile_id; sds[BAND3].filename = sds[BAND4].filename = sds[BAND5].filename = sds[BAND6].filename = sds[BAND7].filename = MOD021KMfile; } else { sds[BAND3].file_id = sds[BAND4].file_id = sds[BAND5].file_id = sds[BAND6].file_id = sds[BAND7].file_id = MOD02HKMfile_id; sds[BAND3].filename = sds[BAND4].filename = sds[BAND5].filename = sds[BAND6].filename = sds[BAND7].filename = MOD02HKMfile; } sds[SOLZ].file_id = sds[SOLA].file_id = sds[SENZ].file_id = sds[SENA].file_id = sds[LON].file_id = sds[LAT].file_id = MOD021KMfile_id; sds[SOLZ].filename = sds[SOLA].filename = sds[SENZ].filename = sds[SENA].filename = sds[LON].filename = sds[LAT].filename = MOD021KMfile; sds[BAND11].file_id = sds[BAND12].file_id = sds[BAND13].file_id = sds[BAND14].file_id = sds[BAND15].file_id = sds[BAND16].file_id = MOD021KMfile_id; sds[BAND11].filename = sds[BAND12].filename = sds[BAND13].filename = sds[BAND14].filename = sds[BAND15].filename = sds[BAND16].filename = MOD021KMfile; for (ib=0; ib < Nitems; ib++) { /* initializing these fields will simplify releasing memory later */ sds[ib].data = sds[ib].fillvalue = (void *) NULL; if ( ib < Nbands && ! process[ib] ) { sds[ib].id = -1; continue; } if (output500m) sds[ib].name = SDSlocatorHKM[ib]; else if (output1km) sds[ib].name = SDSlocator1KM[ib]; else sds[ib].name = SDSlocatorQKM[ib]; if ( (sds[ib].index = SDnametoindex(sds[ib].file_id, sds[ib].name)) == -1 ) { fprintf(stderr, "Cannot find SDS %s in file %s.\n", sds[ib].name, sds[ib].filename); continue; } if ( (sds[ib].id = SDselect(sds[ib].file_id, sds[ib].index)) == -1 ) { fprintf(stderr, "Cannot select SDS no. %d\n", sds[ib].index); if (ib < Nbands) process[ib] = FALSE; continue; } /* Original code passed sds[ib].name as destination for SDS name in call to SDgetinfo(). This was causing a core dump, apparently because SDgetinfo() writes some additional characters beyond the terminating null at the end of the SDS name, so I replaced the argument with a dummy character array. */ if (SDgetinfo(sds[ib].id, dummy, &sds[ib].rank, sds[ib].dim_sizes, &sds[ib].num_type, &sds[ib].n_attr) == -1) { fprintf(stderr, "Can't get info from SDS \"%s\" in file %s.\n", sds[ib].name, sds[ib].filename); SDendaccess(sds[ib].id); sds[ib].id = -1; if (ib < Nbands) process[ib] = FALSE; continue; } sds[ib].factor = 1; if (ib < 5 ) sds[ib].factor = 2.441742e-05; attr_name = "scale_factor"; printf("band %d \n",ib); if ( (attr_index = SDfindattr(sds[ib].id, attr_name)) != -1 && SDattrinfo(sds[ib].id, attr_index, dummy, &num_type, &count) != -1 && SDreadattr(sds[ib].id, attr_index, scale_factor) != -1 ) sds[ib].factor = ((float32 *)scale_factor)[indexlocator[ib]]; else { attr_name = "Scale"; if ((attr_index = SDfindattr(sds[ib].id, attr_name)) != -1 && SDattrinfo(sds[ib].id, attr_index, dummy, &num_type, &count) != -1 && SDreadattr(sds[ib].id, attr_index, scale_factor) != -1 ) sds[ib].factor = *scale_factor; } sds[ib].offset = 0; attr_name = "reflectance_offsets"; if ( (attr_index = SDfindattr(sds[ib].id, attr_name)) != -1 && SDattrinfo(sds[ib].id, attr_index, dummy, &num_type, &count) != -1 && SDreadattr(sds[ib].id, attr_index, add_offset) != -1 ) sds[ib].offset = ((float32 *)add_offset)[indexlocator[ib]]; else { attr_name = "add_offset"; if ( (attr_index = SDfindattr(sds[ib].id, attr_name)) != -1 && SDattrinfo(sds[ib].id, attr_index, dummy, &num_type, &count) != -1 && SDreadattr(sds[ib].id, attr_index, add_offset) != -1 ) sds[ib].offset = *add_offset; } sds[ib].fillvalue = (void *) malloc(1 * DFKNTsize(sds[ib].num_type)); if ( SDgetfillvalue(sds[ib].id, sds[ib].fillvalue) != 0 ) { fprintf(stderr, "Cannot read fill value of SDS \"%s\".\n", sds[ib].name); /* exit(1); commmented that*/ } switch (sds[ib].rank) { case 2: sds[ib].Nl = sds[ib].dim_sizes[0]; sds[ib].Np = sds[ib].dim_sizes[1]; sds[ib].rowsperscan = (int)(NUM1KMROWPERSCAN * sds[ib].Np / (float)NUM1KMCOLPERSCAN + 0.5); sds[ib].start[1] = 0; sds[ib].edges[0] = sds[ib].rowsperscan; sds[ib].edges[1] = sds[ib].Np; break; case 3: sds[ib].Nl = sds[ib].dim_sizes[1]; sds[ib].Np = sds[ib].dim_sizes[2]; sds[ib].rowsperscan = (int)(NUM1KMROWPERSCAN * sds[ib].Np / (float)NUM1KMCOLPERSCAN + 0.5); sds[ib].start[0] = indexlocator[ib]; sds[ib].start[2] = 0; sds[ib].edges[0] = 1; sds[ib].edges[1] = sds[ib].rowsperscan; sds[ib].edges[2] = sds[ib].Np; break; default: fprintf(stderr, "SDS rank must be 2 or 3.\n"); continue; } if (verbose) printf("SDS \"%s\": %dx%d scale factor: %g offset: %g\n", sds[ib].name, sds[ib].Np, sds[ib].Nl, sds[ib].factor, sds[ib].offset); if (sds[ib].num_type != numtypelocator[ib]) { fprintf(stderr, "SDS \"%s\" has not the expected data type.\n", sds[ib].name); exit(-1); } sds[ib].data = malloc(sds[ib].Np * sds[ib].rowsperscan * DFKNTsize(sds[ib].num_type)); if (!sds[ib].data) { (void) fputs("Error allocating memory.\n", stderr); exit(1); } } if (sealevel || TOA) { dem.id = -1; dem.Nl = dem.Np = 0; } else { /* dem.name = strdup(DEMSDSNAME); */ dem.name = DEMSDSNAME; if ( (dem.index = SDnametoindex(dem.file_id, dem.name)) == -1 ) { fprintf(stderr, "Cannot find SDS %s in file %s.\n", dem.name, dem.filename); exit(1); } if ( (dem.id = SDselect(dem.file_id, dem.index)) == -1 ) { fprintf(stderr, "Cannot select SDS no. %d\n", dem.index); exit(1); } if (SDgetinfo(dem.id, dummy, &dem.rank, dem.dim_sizes, &dem.num_type, &dem.n_attr) == -1) { fprintf(stderr, "Can't get info from SDS \"%s\" in file %s.\n", dem.name, dem.filename); SDendaccess(dem.id); exit(1); } dem.Nl = dem.dim_sizes[0]; dem.Np = dem.dim_sizes[1]; dem.rowsperscan = (int)(NUM1KMROWPERSCAN * dem.Np / (float)NUM1KMCOLPERSCAN + 0.5); } if ( sds[SOLZ].id == -1 || sds[SOLA].id == -1 || sds[SENZ].id == -1 || sds[SENA].id == -1 || sds[LON].id == -1 || sds[LAT].id == -1 || ((dem.id == -1) && !sealevel && !TOA) ) { fprintf(stderr, "Solar and Sensor angles and DEM are necessary to process granule.\n"); exit(1); } if ( sds[REFSDS].Np != sds[SOLZ].Np || sds[REFSDS].Np != sds[SOLA].Np || sds[REFSDS].Np != sds[SENZ].Np || sds[REFSDS].Np != sds[SENA].Np || sds[REFSDS].Np != sds[LON].Np || sds[REFSDS].Np != sds[LAT].Np ) { fprintf(stderr, "Solar and Sensor angles must have identical dimensions.\n"); exit(1); } ib = 0; while (sds[ib].id == -1) ib++; if (ib >= Nbands) { fprintf(stderr, "No L1B SDS can be read successfully.\n"); exit(1); } Nscans = sds[ib].Nl / sds[ib].rowsperscan; /* finally, open output file */ if ( (sd_id = SDstart(filename, write_mode)) == -1 ) { fprintf(stderr, "Cannot open output file %s.\n", filename); exit(1); } if (!append) { if (write_global_attributes(sd_id, MOD021KMfile, MOD02HKMfile, MOD02QKMfile, maxsolz, sealevel, TOA, nearest)) { fputs("Error writing global attributes.\n", stderr); exit(1); } } /* create output SDSs and set SDS-specific attributes and dimension names */ if (init_output_sds(sd_id, process, outsds, sds, gzip, verbose)) exit(1); mus = (float *) malloc(sds[REFSDS].rowsperscan * sds[REFSDS].Np * sizeof(float)); height.data = (int16 *) malloc(sds[REFSDS].rowsperscan * sds[REFSDS].Np * sizeof(int16)); if (!mus || !height.data) { (void) fputs("Error allocating memory.\n", stderr); exit(1); } if (sealevel || TOA) dem.data = (void *) NULL; else { dem.data = (int16 *) malloc(dem.Nl * dem.Np * sizeof(int16)); if (!dem.data) { (void) fputs("Error allocating memory.\n", stderr); exit(1); } } if (!TOA) { nbytes = Nbands * sds[REFSDS].rowsperscan * sds[REFSDS].Np * sizeof(float); rhoray = (float *) malloc(nbytes); sphalb = (float *) malloc(nbytes); TtotraytH2O = (float *) malloc(nbytes); tOG = (float *) malloc(nbytes); if (!rhoray || !sphalb || !TtotraytH2O || !tOG) { (void) fputs("Error allocating memory.\n", stderr); exit(1); } } solz = sds[SOLZ].data; sola = sds[SOLA].data; senz = sds[SENZ].data; sena = sds[SENA].data; solzfill = sds[SOLZ].fillvalue; lon = sds[LON].data; lat = sds[LAT].data; lonfill = sds[LON].fillvalue; latfill = sds[LAT].fillvalue; for (ib = 0; ib < Nbands; ib++) l1bdata[ib] = sds[ib].data; /* don't need DEM if --sealevel or --toa specified */ if (!sealevel && !TOA) { dem.start[0] = 0; dem.start[1] = 0; dem.edges[0] = dem.Nl; dem.edges[1] = dem.Np; if (SDreaddata(dem.id, dem.start, NULL, dem.edges, dem.data) == -1) { fprintf(stderr, " Can't read DEM SDS \"%s\"\n", dem.name); exit(-1); } (void) SDendaccess(dem.id); (void) SDend(dem.file_id); } /* loop over each MODIS scan */ for (iscan = 0; iscan < Nscans; iscan++) { if ((iscan % NUM1KMROWPERSCAN == 0) && verbose) printf("Processing scan %d...\n", iscan); /* Fill scan buffer for each band to be processed. Exit scan loop if error occurred while reading. */ if (read_scan(iscan, sds)) break; for (idx = 0; idx < sds[REFSDS].rowsperscan*sds[REFSDS].Np; idx++) { if (solz[idx] * sds[SOLZ].factor >= maxsolz) solz[idx] = *solzfill; if (!sealevel && (lon[idx] == *lonfill || lat[idx] == *latfill)) solz[idx] = *solzfill; if (solz[idx] != *solzfill) { mus[idx] = cos(solz[idx] * sds[SOLZ].factor * DEG2RAD); if (sealevel || TOA) ((int16 *)height.data)[idx] = 0; else ((int16 *)height.data)[idx] = (int16) interp_dem(lat[idx], lon[idx], &dem); } } if (!TOA) { for (irow=0; irow<sds[REFSDS].rowsperscan; irow++) { for (jcol=0; jcol<sds[REFSDS].Np; jcol++) { idx = irow * sds[REFSDS].Np + jcol; if (solz[idx] == *solzfill) continue; phi = sola[idx] * sds[SOLA].factor - sena[idx] * sds[SENA].factor; muv = cos(senz[idx] * sds[SENZ].factor * DEG2RAD); if ( getatmvariables(mus[idx], muv, phi, ((int16 *)height.data)[idx], process, &sphalb[idx * Nbands], &rhoray[idx * Nbands], &TtotraytH2O[idx * Nbands], &tOG[idx * Nbands]) == -1 ) solz[idx] = *solzfill; /* printf(" some data %f %f %f %f %f \n",senz[idx],phi,mus[idx],rhoray[idx * Nbands],tOG[idx * Nbands]);*/ } } } for (ib=0; ib<Nbands; ib++) { if (! process[ib]) continue; aggfactor = outsds[ib].rowsperscan / sds[REFSDS].rowsperscan; for (irow=0; irow<outsds[ib].rowsperscan; irow++) { if (!nearest) { fractrow = (float)irow / aggfactor - 0.5; /* We want fractrow integer on coarse pixel center */ crsrow1 = floor(fractrow); crsrow2 = crsrow1 + 1; if (crsrow1 < 0) crsrow1 = crsrow2 + 1; if (crsrow2 > sds[REFSDS].rowsperscan - 1) crsrow2 = crsrow1 - 1; t = (fractrow - crsrow1) / (crsrow2 - crsrow1); } for (jcol=0; jcol<outsds[ib].Np; jcol++) { idx = irow * outsds[ib].Np + jcol; crsidx = (int)(irow / aggfactor) * sds[REFSDS].Np + (int)(jcol / aggfactor); if ( solz[crsidx] == *solzfill || /* Bad geolocation or night pixel */ l1bdata[ib][idx] >= 65528 ) { /* VIIRS SDR is read as uint16, fills start at 65528 */ if (l1bdata[ib][idx] == (65536 + MISSING)) ((int16 *)outsds[ib].data)[idx] = 32768 + MISSING; else ((int16 *)outsds[ib].data)[idx] = *(int16 *)outsds[ib].fillvalue; continue; } if (nearest) { mus0 = mus[crsidx]; if (! TOA) { rhoray0 = rhoray[crsidx * Nbands + ib]; sphalb0 = sphalb[crsidx * Nbands + ib]; if ( sphalb0 <= 0.0F ) { /* Atm variables not computed successfully in this band */ ((int16 *)outsds[ib].data)[idx] = *(int16 *)outsds[ib].fillvalue; continue; } } } else { fractcol = ((float) jcol) / aggfactor - 0.5F; /* We want fractcol integer on coarse pixel center */ crscol1 = (int) floor(fractcol); crscol2 = crscol1 + 1; if (crscol1 < 0) crscol1 = crscol2 + 1; if (crscol2 > sds[REFSDS].Np - 1) crscol2 = crscol1 - 1; u = (fractcol - crscol1) / (crscol2 - crscol1); /* We want u=0 on coarse pixel center */ crsidx11 = crsrow1 * sds[REFSDS].Np + crscol1; crsidx12 = crsrow1 * sds[REFSDS].Np + crscol2; crsidx21 = crsrow2 * sds[REFSDS].Np + crscol1; crsidx22 = crsrow2 * sds[REFSDS].Np + crscol2; /* mus0 = t * u * mus[crsidx22] + (1.0F - t) * u * mus[crsidx12] + t * (1.0F - u) * mus[crsidx21] + (1.0F - t) * (1.0F - u) * mus[crsidx11]; bad = (solz[crsidx11] == *solzfill) || (solz[crsidx12] == *solzfill) || (solz[crsidx21] == *solzfill) || (solz[crsidx22] == *solzfill); commented by eric to handle the viirs fill value hardcoding */ bad = (solz[crsidx11] <-900.) || (solz[crsidx12] <-900.) || (solz[crsidx21] <-900.) || (solz[crsidx22] <-900.); if (bad) { ((int16 *)outsds[ib].data)[idx] = *(int16 *)outsds[ib].fillvalue; continue; } if (! TOA) { rhoray11 = rhoray[crsidx11 * Nbands + ib]; rhoray12 = rhoray[crsidx12 * Nbands + ib]; rhoray21 = rhoray[crsidx21 * Nbands + ib]; rhoray22 = rhoray[crsidx22 * Nbands + ib]; rhoray0 = t * u * rhoray22 + (1.0F - t) * u * rhoray12 + t * (1.0F - u) * rhoray21 + (1.0F - t) * (1.0F - u) * rhoray11; sphalb11 = sphalb[crsidx11 * Nbands + ib]; sphalb12 = sphalb[crsidx12 * Nbands + ib]; sphalb21 = sphalb[crsidx21 * Nbands + ib]; sphalb22 = sphalb[crsidx22 * Nbands + ib]; bad = (sphalb11 <= 0.0F) || (sphalb12 <= 0.0F) || (sphalb21 <= 0.0F) || (sphalb22 <= 0.0F); if (bad) { ((int16 *)outsds[ib].data)[idx] = *(int16 *)outsds[ib].fillvalue; continue; } sphalb0 = t * u * sphalb22 + (1.0F - t) * u * sphalb12 + t * (1.0F - u) * sphalb21 + (1.0F - t) * (1.0F - u) * sphalb11; } } /* TOA reflectance */ /*printf(" mus0 is %f\n",mus0);*/ refl = (l1bdata[ib][idx] - sds[ib].offset) * sds[ib].factor /*/ mus0 commented by Eric who suspected something*/; /* corrected reflectance */ if (!TOA) refl = correctedrefl(refl, TtotraytH2O[crsidx * Nbands + ib], tOG[crsidx * Nbands + ib], rhoray0, sphalb0); /* reflectance bounds checking */ if (refl > reflmax) refl = reflmax; if (refl < reflmin) refl = reflmin; ((int16 *)outsds[ib].data)[idx] = (int16) (refl / outsds[ib].factor + 0.5); } } } /* write current scan line for all processed bands */ if (write_scan(iscan, process, outsds)) { fprintf(stderr, "Cannot write scan %d of SDS %s\n", iscan, outsds[ib].name); exit(1); } } /* end of scan loop */ for (ib = 0; ib < Nitems; ib++) if (sds[ib].id != -1) SDendaccess(sds[ib].id); for (ib = 0; ib < Nbands; ib++) if (process[ib]) SDendaccess(outsds[ib].id); SDend(MOD02QKMfile_id); SDend(MOD02HKMfile_id); SDend(MOD021KMfile_id); SDend(sd_id); /* ----- free memory ----- */ for (ib = 0; ib < Nitems; ib++) { if (sds[ib].fillvalue) free(sds[ib].fillvalue); if (sds[ib].data) free(sds[ib].data); } free(height.data); free(mus); if (!TOA) { free(tOG); free(TtotraytH2O); free(sphalb); free(rhoray); } /* not allocated if --sealevel specified */ if (dem.data) free(dem.data); return 0; }
/* Open and read the HDF file. Return the file ID if successful, or * report error and send back failure status (-1). */ int cuopenread_hdf(const char* controlpath, const char* datapath){ CuFile* file; int32 cdfid; int nvars=0, natts=0; int varid, ndims, dimidx, maxdim, mindim; int32 dimid, sds_id; CuDim *dim; /* Open the HDF file. DFACC_RDONLY is defined in hdf.h. */ if ((cdfid=SDstart(controlpath, DFACC_RDONLY))==-1) { CuError(CU_EOPEN,"Opening HDF file %s",controlpath); cuerrorreport_hdf(); return -1; } /* Make sure there is a scientific dataset */ SDfileinfo(cdfid, &nvars, &natts); if (nvars==0 && natts==0){ CuError(CU_EOPEN,"HDF file %s does not contain any scientific datasets",controlpath); cuerrorreport_hdf(); return -1; } if((file = CuCreateFile(CuHdf))==(CuFile*)0){ return -1; } /* Set the file path and file ID */ strncpy(file->controlpath,controlpath,CU_MAX_PATH); file->internid1 = cdfid; /* Set up a mapping of cdunif dimid to HDF dimid: */ /* Go through all the dimensions, find min and max HDF_dimid. */ /* ASSUME THAT DIMENSION IDS ARE CONSECUTIVE NONNEGATIVE NUMBERS. */ /* Store base dimid in file->internid2. */ /* The mapping is 0 --> mindim, 1 --> mindim+1, etc. */ mindim = maxdim = -1; for (varid=0; varid<nvars; varid++){ if (cuvarinq_hdf(file, varid, NULL, NULL, &ndims, NULL, NULL)) return -1; if ((sds_id=SDselect(file->internid1, varid)) == -1) { cuerrorreport_hdf(); return -1; } for (dimidx=0; dimidx<ndims; dimidx++){ if((dimid=SDgetdimid(sds_id, dimidx))==-1){ cuerrorreport_hdf(); return -1; } if (maxdim==-1){ mindim = maxdim = dimid; } else{ mindim = MIN(mindim, dimid); maxdim = MAX(maxdim, dimid); } } } file->internid2 = mindim; file->ndims = maxdim-mindim+1; /* Return file ID */ return file->id; }
static intn test_dim1_SDS1(void) { char sds_name[20]; float32 sds1_data[] = {0.1, 2.3, 4.5, 6.7, 8.9}; float32 out_data[5]; int32 dimsize[1]; int32 sds_id, file_id, dim_id, index; int32 start=0, stride=1; int32 scale1 [5] = {101,102,103,104,105}, scale1_out[5]; int32 num_type, array_rank, count; int32 n_datasets, n_file_attrs, n_local_attrs, n_vars = 0; intn datanum, ranknum, status =0, i, idx, idx1, idx2; hdf_varlist_t* var_list; intn is_coord = FALSE; char attr_name[H4_MAX_NC_NAME], attr_values[80]; intn num_errs = 0; /* number of errors so far */ file_id = SDstart(FILE1, DFACC_CREATE); CHECK(file_id, FAIL, "SDstart"); /* Create a one-dim dataset named VAR1_NAME, of type DFNT_FLOAT32. */ dimsize[0] = 5; sds_id = SDcreate(file_id, VAR1_NAME, DFNT_FLOAT32, 1, dimsize); CHECK(sds_id, FAIL, "SDcreate"); /* Set the dimension name to be the same as its dataset. */ dim_id = SDgetdimid(sds_id, 0); CHECK(dim_id, FAIL, "SDgetdimid"); status = SDsetdimname(dim_id, VAR1_NAME); /* status = SDsetdimname(dim_id, VAR1_NAME); */ CHECK(status, FAIL, "SDsetdimname"); /* Get file info and verify that there is 1 dataset in the file. */ status = SDfileinfo(file_id, &n_datasets, &n_file_attrs); CHECK(status, FAIL, "SDfileinfo"); VERIFY(n_datasets, 1, "SDfileinfo"); /* Set an attribute to dimension VAR1_NAME. */ status = SDsetattr(dim_id, ATTR1_NAME, DFNT_CHAR8, ATTR1_LEN, ATTR1_VAL); CHECK(status, FAIL, "SDsetattr"); /* Set an attribute to dataset VAR1_NAME. */ status = SDsetattr(sds_id, ATTR2_NAME, DFNT_CHAR8, ATTR2_LEN, ATTR2_VAL); CHECK(status, FAIL, "SDsetattr"); /* Get file info and verify that there are 2 datasets in the file: 1 SDS and 1 coordinate variable (because of SDsetattr dim) */ status = SDfileinfo(file_id, &n_datasets, &n_file_attrs); CHECK(status, FAIL, "SDfileinfo"); VERIFY(n_datasets, 2, "SDfileinfo"); /* Write data to the SDS */ status = SDwritedata(sds_id, &start, &stride, dimsize, (VOIDP)sds1_data); CHECK(status, FAIL, "SDwritedata"); /* Close dataset and file. */ status = SDendaccess(sds_id); CHECK(status, FAIL, "SDendaccess"); status = SDend(file_id); CHECK(status, FAIL, "SDend"); /* Open the file again to check its data */ file_id = SDstart(FILE1, DFACC_RDWR); CHECK(file_id, FAIL, "SDstart"); /* Check variable type and attributes of each element in the file */ /* Get the number of variables of name VAR1_NAME */ status = SDgetnumvars_byname(file_id, VAR1_NAME, &n_vars); if (n_vars == 1) { /* Get index of dataset VAR1_NAME */ index = SDnametoindex(file_id, VAR1_NAME); CHECK(index, FAIL, "SDnametoindex"); } else { /* Get the list of all variables of named VAR1_NAME */ var_list = (hdf_varlist_t *)HDmalloc(n_vars * sizeof(hdf_varlist_t)); status = SDnametoindices(file_id, VAR1_NAME, var_list); /* In this case, the first variable is a dataset */ for (idx = 0; idx < n_vars; idx++) { if (var_list[idx].var_type == IS_SDSVAR) { index = var_list[idx].var_index; VERIFY(index, 0, "SDnametoindices"); } } } sds_id = SDselect(file_id, index); CHECK(sds_id, FAIL, "SDselect"); /* Verify that this variable is a dataset. */ is_coord = SDiscoordvar(sds_id); VERIFY(is_coord, FALSE, "SDiscoordvar"); /* Read and verify the information of the SDS' first attribute. */ status = SDattrinfo(sds_id, 0, attr_name, &num_type, &count); CHECK(status, FAIL, "SDattrinfo"); VERIFY(count, ATTR2_LEN, "SDattrinfo"); VERIFY(HDstrncmp(attr_name, ATTR2_NAME, 14), 0, "SDattrinfo"); /* Read and verify the values of the SDS' first attribute. */ status = SDreadattr(sds_id, 0, attr_values); CHECK(status, FAIL, "SDreadattr"); if (HDstrncmp(attr_values, ATTR2_VAL, ATTR2_LEN) != 0) { fprintf(stderr, "Unmatched attribute values for SDS %s: is <%s>, should be <%s>\n", VAR1_NAME, attr_values, ATTR2_VAL); num_errs++; } /* Get access to the SDS' first dimension. */ dim_id = SDgetdimid(sds_id, 0); CHECK(dim_id, FAIL, "SDgetdimid"); /* Read and verify the information of the dimension's first attribute. */ status = SDattrinfo(dim_id, 0, attr_name, &num_type, &count); CHECK(status, FAIL, "SDattrinfo"); VERIFY(count, 19, "SDattrinfo"); VERIFY(HDstrncmp(attr_name, ATTR1_NAME, 21), 0, "SDattrinfo"); /* Read and verify the values of the dimension's first attribute. */ status = SDreadattr(dim_id, 0, attr_values); CHECK(status, FAIL, "SDreadattr"); if (HDstrncmp(attr_values, ATTR1_VAL, ATTR1_LEN) != 0) { fprintf(stderr, "Unmatched attribute values for dimension %s: is <%s>, should be <%s>\n", VAR1_NAME, attr_values, ATTR1_VAL); num_errs++; } /* Verify again that the number of datasets in the file is 2, 1 SDS and 1 coordinate variable */ status = SDfileinfo(file_id, &n_datasets, &n_file_attrs); CHECK(status, FAIL, "SDfileinfo"); VERIFY(n_datasets, 2, "SDfileinfo"); VERIFY(n_file_attrs, 0, "SDfileinfo"); /* Read and verify the dataset's data */ status = SDreaddata (sds_id, &start, NULL, dimsize, &out_data); CHECK(status, FAIL, "SDreaddata"); for (idx1 = 0; idx1 < dimsize[0]; idx1++) if (out_data[idx1] != sds1_data[idx1]) { fprintf(stderr, "Read value (%f) differs from written (%f) at [%d]\n", out_data[idx1], sds1_data[idx1], idx1); num_errs++; } /* Close dataset and file. */ status = SDendaccess(sds_id); CHECK(status, FAIL, "SDendaccess"); status = SDend(file_id); CHECK(status, FAIL, "SDend"); /* Return the number of errors that's been kept track of so far */ return num_errs; } /* test_dim1_SDS1 */
static intn test_szip_chunk() { /************************* Variable declaration **************************/ int32 sd_id, sds_id, sds_index; intn status; int32 flag, maxcache, new_maxcache; int32 dim_sizes[2], origin[2]; HDF_CHUNK_DEF c_def; /* Chunking definitions */ HDF_CHUNK_DEF c_def_out; /* Chunking definitions */ int32 c_flags, c_flags_out; int32 all_data[LENGTH_CH][WIDTH_CH]; int32 start[2], edges[2]; int32 comp_size=0, uncomp_size=0; int32 chunk_out[CLENGTH][CWIDTH]; int32 row[CWIDTH] = { 5, 5 }; int32 column[CLENGTH] = { 4, 4, 4 }; int32 fill_value = 0; /* Fill value */ comp_coder_t comp_type; /* to retrieve compression type into */ comp_info cinfo; /* compression information structure */ int num_errs = 0; /* number of errors so far */ int i,j; /* * Define all chunks. Note that chunks 4 & 5 are not used to write, * only to verify the read data. The 'row' and 'column' are used * to write in the place of these chunks. */ int32 chunk1[CLENGTH][CWIDTH] = { 1, 1, 1, 1, 1, 1 }; int32 chunk2[CLENGTH][CWIDTH] = { 2, 2, 2, 2, 2, 2 }; int32 chunk3[CLENGTH][CWIDTH] = { 3, 3, 3, 3, 3, 3 }; int32 chunk4[CLENGTH][CWIDTH] = { 0, 4, 0, 4, 0, 4 }; int32 chunk5[CLENGTH][CWIDTH] = { 0, 0, 5, 5, 0, 0 }; int32 chunk6[CLENGTH][CWIDTH] = { 6, 6, 6, 6, 6, 6 }; /* Initialize chunk lengths. */ c_def.comp.chunk_lengths[0] = CLENGTH; c_def.comp.chunk_lengths[1] = CWIDTH; /* Create the file and initialize SD interface. */ sd_id = SDstart (FILE_NAME, DFACC_CREATE); CHECK(sd_id, FAIL, "SDstart"); /* Create LENGTH_CHxWIDTH_CH SDS. */ dim_sizes[0] = LENGTH_CH; dim_sizes[1] = WIDTH_CH; sds_id = SDcreate (sd_id, SDS_NAME_CH,DFNT_INT32, RANK_CH, dim_sizes); CHECK(sds_id, FAIL, "SDcreate:Failed to create a data set for chunking/szip compression testing"); /* Fill the SDS array with the fill value. */ status = SDsetfillvalue (sds_id, (VOIDP)&fill_value); CHECK(status, FAIL, "SDsetfillvalue"); /* Set parameters for Chunking/SZIP */ c_def.comp.comp_type = COMP_CODE_SZIP; c_def.comp.cinfo.szip.pixels_per_block = 2; c_def.comp.cinfo.szip.options_mask = SZ_EC_OPTION_MASK; c_def.comp.cinfo.szip.options_mask |= SZ_MSB_OPTION_MASK; c_def.comp.cinfo.szip.bits_per_pixel = 0; c_def.comp.cinfo.szip.pixels = 0; c_def.comp.cinfo.szip.pixels_per_scanline = 0; c_flags = HDF_CHUNK | HDF_COMP; status = SDsetchunk (sds_id, c_def, c_flags); CHECK(status, FAIL, "SDsetchunk"); /* Set chunk cache to hold maximum of 3 chunks. */ maxcache = 3; flag = 0; new_maxcache = SDsetchunkcache (sds_id, maxcache, flag); CHECK(new_maxcache, FAIL, "SDsetchunkcache"); HDmemset(&c_def_out, 0, sizeof(HDF_CHUNK_DEF)); c_flags_out = 0; status = SDgetchunkinfo(sds_id, &c_def_out, &c_flags_out); CHECK(status, FAIL, "SDgetchunkinfo"); VERIFY(c_flags_out, c_flags, "SDgetchunkinfo"); VERIFY(c_def_out.comp.comp_type, COMP_CODE_SZIP, "SDgetchunkinfo"); /* * Write chunks using SDwritechunk function. Chunks can be written * in any order. */ /* Write the chunk with the coordinates (0,0). */ origin[0] = 0; origin[1] = 0; status = SDwritechunk (sds_id, origin, (VOIDP) chunk1); CHECK(status, FAIL, "SDwritechunk"); /* Write the chunk with the coordinates (1,0). */ origin[0] = 1; origin[1] = 0; status = SDwritechunk (sds_id, origin, (VOIDP) chunk3); CHECK(status, FAIL, "SDwritechunk"); /* Write the chunk with the coordinates (0,1). */ origin[0] = 0; origin[1] = 1; status = SDwritechunk (sds_id, origin, (VOIDP) chunk2); CHECK(status, FAIL, "SDwritechunk"); /* Write chunk with the coordinates (1,2) using SDwritedata function. */ start[0] = 6; start[1] = 2; edges[0] = 3; edges[1] = 2; status = SDwritedata (sds_id, start, NULL, edges, (VOIDP) chunk6); CHECK(status, FAIL, "SDwritedata"); /* Fill second column in the chunk with the coordinates (1,1) using * SDwritedata function. */ start[0] = 3; start[1] = 3; edges[0] = 3; edges[1] = 1; status = SDwritedata (sds_id, start, NULL, edges, (VOIDP) column); CHECK(status, FAIL, "SDwritedata"); /* Fill second row in the chunk with the coordinates (0,2) using * SDwritedata function. */ start[0] = 7; start[1] = 0; edges[0] = 1; edges[1] = 2; status = SDwritedata (sds_id, start, NULL, edges, (VOIDP) row); CHECK(status, FAIL, "SDwritedata"); /* Terminate access to the data set. */ status = SDendaccess (sds_id); CHECK(status, FAIL, "SDendaccess"); /* Terminate access to the SD interface and close the file. */ status = SDend (sd_id); CHECK(status, FAIL, "SDend"); /* * Verify the compressed data */ /* Reopen the file and access the first data set. */ sd_id = SDstart (FILE_NAME, DFACC_READ); sds_index = 0; sds_id = SDselect (sd_id, sds_index); CHECK(sds_id, FAIL, "SDselect:Failed to select a data set for chunking/szip compression testing"); /* Retrieve compression information about the dataset */ comp_type = COMP_CODE_INVALID; /* reset variables before retrieving info */ HDmemset(&cinfo, 0, sizeof(cinfo)) ; status = SDgetcompinfo(sds_id, &comp_type, &cinfo); CHECK(status, FAIL, "SDgetcompinfo"); VERIFY(comp_type, COMP_CODE_SZIP, "SDgetcompinfo"); /* Retrieve compression method alone from the dataset */ comp_type = COMP_CODE_INVALID; /* reset variables before retrieving info */ status = SDgetcomptype(sds_id, &comp_type); CHECK(status, FAIL, "SDgetcomptype"); VERIFY(comp_type, COMP_CODE_SZIP, "SDgetcomptype"); /* Read the entire data set using SDreaddata function. */ start[0] = 0; start[1] = 0; edges[0] = LENGTH_CH; edges[1] = WIDTH_CH; status = SDreaddata (sds_id, start, NULL, edges, (VOIDP)all_data); CHECK(status, FAIL, "SDreaddata"); /* * This is how the entire array should look like: * * 1 1 2 2 * 1 1 2 2 * 1 1 2 2 * 3 3 0 4 * 3 3 0 4 * 3 3 0 4 * 0 0 6 6 * 5 5 6 6 * 0 0 6 6 */ /* Read chunk #4 with the coordinates (1,1) and verify it. */ origin[0] = 1; origin[1] = 1; status = SDreadchunk (sds_id, origin, chunk_out); CHECK(status, FAIL, "SDreadchunk"); for (j=0; j<CLENGTH; j++) { for (i=0; i<CWIDTH; i++) { if (chunk_out[j][i] != chunk4[j][i]) { fprintf(stderr,"Bogus val in loc [%d][%d] in chunk #4, want %ld got %ld\n", j, i, chunk4[j][i], chunk_out[j][i]); num_errs++; } } } /* * Read chunk #5 with the coordinates (2,0) and verify it. */ origin[0] = 2; origin[1] = 0; status = SDreadchunk (sds_id, origin, chunk_out); CHECK(status, FAIL, "SDreadchunk"); for (j=0; j<CLENGTH; j++) { for (i=0; i<CWIDTH; i++) if (chunk_out[j][i] != chunk5[j][i]) { fprintf(stderr,"Bogus val in loc [%d][%d] in chunk #5, want %ld got %ld\n", j, i, chunk5[j][i], chunk_out[j][i]); num_errs++; } } /* Get the data sizes */ status = SDgetdatasize(sds_id, &comp_size, &uncomp_size); CHECK(status, FAIL, "test_chkcmp_SDSs: SDgetdatasize"); /* Terminate access to the data set. */ status = SDendaccess (sds_id); CHECK(status, FAIL, "SDendaccess"); /* Terminate access to the SD interface and close the file. */ status = SDend (sd_id); CHECK(status, FAIL, "SDend"); /* Return the number of errors that's been kept track of so far */ return num_errs; } /* test_szip_chunk */
static intn test_getszipinfo() { /************************* Variable declaration **************************/ int32 sd_id, sds_id, sds_index; intn status; int32 dim_sizes[2], array_rank, num_type, attributes; char name[H4_MAX_NC_NAME]; comp_info c_info; int32 start[2], edges[2]; comp_coder_t comp_type; int32 comp_size, uncomp_size, orig_size; int16 fill_value = 0; /* Fill value */ int i,j; int num_errs = 0; /* number of errors so far */ char testfile[512] = ""; int32 *out_data = NULL; char *basename = "sds_szipped.dat"; /********************* End of variable declaration ***********************/ /* Make the name for the test file */ make_datafilename(basename, testfile, sizeof(testfile)); /* Open the file and select dataset SDS1_NAME */ sd_id = SDstart (testfile, DFACC_RDONLY); CHECK(sd_id, FAIL, "SDstart"); sds_index = SDnametoindex(sd_id, SDS1_NAME); CHECK(sds_index, FAIL, "SDnametoindex"); sds_id = SDselect (sd_id, sds_index); CHECK(sds_id, FAIL, "SDselect:Failed to select a data set for szip compression testing"); /* Retrieve and verify information of the data set */ status = SDgetinfo(sds_id, name, &array_rank, dim_sizes, &num_type, &attributes); CHECK(status, FAIL, "SDgetinfo"); VERIFY(array_rank, RANK, "SDgetinfo"); VERIFY(dim_sizes[0], SZ_LENGTH, "SDgetinfo"); VERIFY(dim_sizes[1], SZ_WIDTH, "SDgetinfo"); VERIFY(num_type, DFNT_INT32, "SDgetinfo"); VERIFY(attributes, 0, "SDgetinfo"); /* Get the compression method and verify it */ comp_type = COMP_CODE_INVALID; /* reset variables before retrieving info */ status = SDgetcomptype(sds_id, &comp_type); CHECK(status, FAIL, "SDgetcomptype"); VERIFY(comp_type, COMP_CODE_SZIP, "SDgetcomptype"); /* Get the compressed data size and non-compressed data size */ status = SDgetdatasize(sds_id, &comp_size, &uncomp_size); CHECK(status, FAIL, "SDgetdatasize"); /* Compute the uncompressed/original size of the data for comparision */ orig_size = SZ_WIDTH * SZ_LENGTH * SIZE_INT32; VERIFY(uncomp_size, orig_size, "SDgetdatasize"); /* In this test, compressed data size should be smaller than non-compressed data size */ if (comp_size >= uncomp_size) { printf("*** Routine test_getszipinfo: FAILED at line %d ***\n", __LINE__); printf(" In this test, compressed data size (%d) should be smaller than non-compressed data size (%d)\n", comp_size, uncomp_size); num_errs++; } /* Terminate access to the data set */ status = SDendaccess (sds_id); CHECK(status, FAIL, "SDendaccess"); /* Terminate access to the SD interface and close the file */ status = SDend (sd_id); CHECK(status, FAIL, "SDend"); /* Return the number of errors that's been kept track of so far */ return num_errs; } /* test_getszipinfo */
/* Get dimension coordinates values. Return success (0) if the * dimension values were obtained sucessfully, otherwise this * function returns the failure status (-1). */ int cudimget_hdf(CuFile* file, int dimidx, void* values){ char dimname[H4_MAX_NC_NAME+1]; float *fp; int cdfid; int get_dimid; int dimvarid; int found; int ndims; int saveopts; long i; long length; long start; char varname[H4_MAX_NC_NAME+1]; hdf_type hdftype; int natts; int32 dimid; int32 sds_id, datatype, nattrs, attr_index, num_type, count; int32 dim_sizes[H4_MAX_VAR_DIMS]; char attr_name[H4_MAX_NC_NAME]; cdfid = file->internid1; if((dimid = cudimid2hdf(file, dimidx))==-1) return -1; /* Get information about the selected dimension. */ if(SDdiminfo(dimid, attr_name, &length, &datatype, &nattrs)==-1){ return -1; } /* Inquire a variable with */ /* - the same name as dimname, */ /* - a single dimension, and */ /* - a (single) dimension id which equals dimid */ if((dimvarid = SDnametoindex(cdfid, attr_name)) != -1){ sds_id = SDselect(cdfid, dimvarid); if (SDgetinfo(sds_id, varname, &ndims, dim_sizes, &hdftype, &natts) == -1){ cuerrorreport_hdf(); return -1; } /* pass back the dimension id */ if ((get_dimid = SDgetdimid(sds_id, 0)) == -1){ cuerrorreport_hdf(); return -1; } found = (ndims == 1 && get_dimid == dimid); } else found = 0; /* If the dimension variable was found, read it */ if(found){ start = 0; if(values && SDgetdimscale(dimid, (VOIDP) values)==-1) return -1; } else{ /* Otherwise assign the default dimension */ if(values){ for(i=0, fp=(float*)values; i<length; i++){ *fp++ = (float)i; } } } /* Return success ( 0 ). */ return CU_SUCCESS; }
/* Get information about the dimension. Return success (0) if the * dimension information was obtained sucessfully, otherwise this * function returns the failure status (-1). */ int cudiminq_hdf(CuFile* file, int dimidx, char* dimname, char* dimunits, CuType* dataType, CuDimType* dimtype, int* varid, long* length){ char dname[H4_MAX_NC_NAME+1]; int cdfid; int dimvarid; /* HDF ID of variable associated with this dimension. */ int found; /* True iff a dimension variable was found. */ int ndims; int get_dimid; int saveopts; long len; hdf_type hdftype, hdfunitstype; int natts; char varname[H4_MAX_NC_NAME+1]; int attlen; int32 sds_id, attr_index, datatype, nattrs; int32 dim_sizes[H4_MAX_VAR_DIMS]; char attr_name[H4_MAX_NC_NAME]; int32 dimid; cdfid = file->internid1; if((dimid = cudimid2hdf(file, dimidx))==-1) return -1; /* Get information about the selected dimension. */ if(SDdiminfo(dimid, dname, &len, &datatype, &nattrs)==-1){ cuerrorreport_hdf(); return -1; } if(dimname) strncpy(dimname,dname,CU_MAX_NAME); if(length) *length = len; /* HDF dimensions are always global */ if(varid) *varid = CU_GLOBAL; if(dimtype) *dimtype = CuGlobalDim; /* Inquire a variable with */ /* - the same name as dimname, */ /* - a single dimension, and */ /* - a dimension name which equals the variable name. */ if((dimvarid = SDnametoindex(cdfid, dname)) != -1){ sds_id = SDselect(cdfid, dimvarid); if (SDgetinfo(sds_id, varname, &ndims, dim_sizes, &hdftype, &natts) == -1){ cuerrorreport_hdf(); return -1; } /* pass back the dimension id */ if ((get_dimid = SDgetdimid(sds_id, 0)) == -1){ cuerrorreport_hdf(); return -1; } found = (ndims == 1 && get_dimid == dimid); } else found = 0; /* If dimension variable was found, */ /* inquire the units attribute (if any) */ if(found){ sds_id = SDselect(cdfid, dimvarid); /* Set the length of an unlimited dimension. */ if (len==0 && length) *length = dim_sizes[0]; /* Find the data set attribute name index. */ attr_index = SDfindattr(sds_id, "units"); /* Get information about the data set attribute. */ if(SDattrinfo(sds_id, attr_index, attr_name, &hdfunitstype, &attlen) != -1 && hdfunitstype == DFNT_CHAR) { if(dimunits && SDreadattr(sds_id, attr_index, dimunits)==-1) return -1; } /* Dimension variable was found, but no character units string */ else{ if(dimunits) strcpy(dimunits,""); } if(dataType) { cumapdatatype_hdf(hdftype, dataType); if (*dataType==CuInvalidType) return -1; } } else{ /* The dimension variable was not found: */ /* return default units and datatype */ if(dimunits) strcpy(dimunits,""); if(dataType) *dataType = CuFloat; } /* Return success ( 0 ). */ return CU_SUCCESS; }
/******************************************************************** Name: test_getszipdata() - verifies that SZIP compressed data can be read when either SZIP library encoder or only decoder is present Description: This test function opens the existing file "sds_szipped.dat" that contains a dataset with SZIP compression and verifies that the SZIP compressed data can be read with or without the encoder as long as the szlib is available. The input file, sds_szipped.dat, is generated by the program mfhdf/libsrc/gen_sds_szipped.c Return value: The number of errors occurred in this routine. BMR - Oct 10, 2008 *********************************************************************/ #ifdef H4_HAVE_LIBSZ /* needed to read data, either decoder or encoder */ static intn test_getszipdata() { /************************* Variable declaration **************************/ int32 sd_id, sds_id; intn status; int32 dim_sizes[2], array_rank, num_type, attributes; char name[H4_MAX_NC_NAME]; comp_info c_info; int32 start[2], edges[2]; int16 fill_value = 0; /* Fill value */ int i,j; int num_errs = 0; /* number of errors so far */ int32 out_data[SZ_LENGTH][SZ_WIDTH]; char testfile[512] = ""; char *basename = "sds_szipped.dat"; /* data to compare against read data from sds_szipped.dat */ int32 in_data[SZ_LENGTH][SZ_WIDTH]={ 100,100,200,200,300, 0, 0, 0, 0, 0, 100,100,200,200,300, 400,300,200,100,0, 300,300, 0,400,300, 300,300, 0,400,300, 300,300, 0,400,300, 0, 0,600,600,300, 500,500,600,600,300, 0, 0,600,600,300, 0, 0,600,600,300, 0, 0,600,600,300, 0, 0,600,600,300, 500,500,600,600,300, 500,500,600,600,300, 500,500,600,600,300 }; /********************* End of variable declaration ***********************/ /* Make the name for the test file */ make_datafilename(basename, testfile, sizeof(testfile)); /* Open the file */ sd_id = SDstart (testfile, DFACC_READ); CHECK(sd_id, FAIL, "SDstart"); /* Get the first SDS */ sds_id = SDselect (sd_id, 0); CHECK(sds_id, FAIL, "SDselect:Failed to select a data set for szip compression testing"); /* Retrieve information of the data set */ status = SDgetinfo(sds_id, name, &array_rank, dim_sizes, &num_type, &attributes); CHECK(status, FAIL, "SDgetinfo"); /* Prepare for reading */ start[0] = 0; start[1] = 0; edges[0] = dim_sizes[0]; edges[1] = dim_sizes[1]; /* Wipe out the output buffer */ HDmemset(&out_data, 0, sizeof(out_data)); /* Read the data set */ status = SDreaddata (sds_id, start, NULL, edges, (VOIDP)out_data); CHECK(status, FAIL, "SDreaddata"); /* Compare read data against input data */ for (j=0; j<SZ_LENGTH; j++) { for (i=0; i<SZ_WIDTH; i++) if (out_data[j][i] != in_data[j][i]) { fprintf(stderr,"This one: Bogus val in loc [%d][%d] in compressed dset, want %ld got %ld\n", j, i, (long)in_data[j][i], (long)out_data[j][i]); num_errs++; } } /* Terminate access to the data set */ status = SDendaccess (sds_id); CHECK(status, FAIL, "SDendaccess"); /* Terminate access to the SD interface and close the file */ status = SDend (sd_id); CHECK(status, FAIL, "SDend"); /* Return the number of errors that's been kept track of so far */ return num_errs; } /* test_getszipdata */
static intn test_szip_chunk_3d() { /************************* Variable declaration **************************/ int32 sd_id, sds_id0, sds_id, sds_index; intn status; int32 dim_sizes[3]; HDF_CHUNK_DEF c_def; /* Chunking definitions */ HDF_CHUNK_DEF c_def_out; /* Chunking definitions */ int32 c_flags, c_flags_out; int32 start[3], edges[3]; int16 fill_value = 0; /* Fill value */ comp_coder_t comp_type; /* to retrieve compression type into */ comp_info cinfo; /* compression information structure */ int num_errs = 0; /* number of errors so far */ int i,j,k; for (i = 0; i < SDS_DIM0; i++) { for (j = 0; j < SDS_DIM1; j++) { for (k = 0; k < SDS_DIM2; k++) { out_data[i][j][k] = i*100+j*10+k; }}} /* Initialize chunk lengths. */ c_def.comp.chunk_lengths[0] = CHK_DIM0; c_def.comp.chunk_lengths[1] = CHK_DIM1; c_def.comp.chunk_lengths[2] = CHK_DIM2; /* Create the file and initialize SD interface. */ sd_id = SDstart (FILE_NAME_3D, DFACC_CREATE); CHECK(sd_id, FAIL, "SDstart"); /* Create SDS_DIM0xSDS_DIM1 SDS. */ dim_sizes[0] = SDS_DIM0; dim_sizes[1] = SDS_DIM1; dim_sizes[2] = SDS_DIM2; sds_id = SDcreate (sd_id, SDS_NAME_CH3D, DFNT_INT16, RANK_CH3, dim_sizes); CHECK(sds_id, FAIL, "SDcreate:Failed to create a data set for chunking/szip compression testing"); /* Create a similar SDS and will make it chunked, but will not write data to it */ sds_id0 = SDcreate (sd_id, SDS_NAME_CH0, DFNT_INT16, RANK_CH3, dim_sizes); CHECK(sds_id0, FAIL, "SDcreate:Failed to create a data set for chunking/szip compression testing"); /* Fill the SDS array with the fill value. */ status = SDsetfillvalue (sds_id, (VOIDP)&fill_value); CHECK(status, FAIL, "SDsetfillvalue"); /* Set parameters for Chunking/SZIP */ c_def.comp.comp_type = COMP_CODE_SZIP; c_def.comp.cinfo.szip.pixels_per_block = 2; c_def.comp.cinfo.szip.options_mask = SZ_EC_OPTION_MASK; c_def.comp.cinfo.szip.options_mask |= SZ_MSB_OPTION_MASK; c_def.comp.cinfo.szip.bits_per_pixel = 2; c_def.comp.cinfo.szip.pixels = 16; c_def.comp.cinfo.szip.pixels_per_scanline = 2; c_flags = HDF_CHUNK | HDF_COMP; status = SDsetchunk (sds_id0, c_def, c_flags); status = SDsetchunk (sds_id, c_def, c_flags); CHECK(status, FAIL, "SDsetchunk"); start[0] = 0; start[1] = 0; start[2] = 0; edges[0] = SDS_DIM0; edges[1] = SDS_DIM1; edges[2] = SDS_DIM2; status = SDwritedata (sds_id, start, NULL, edges, (VOIDP) out_data); CHECK(status, FAIL, "SDwritedata"); HDmemset(&c_def_out, 0, sizeof(HDF_CHUNK_DEF)); c_flags_out = 0; status = SDgetchunkinfo(sds_id0, &c_def_out, &c_flags_out); CHECK(status, FAIL, "SDgetchunkinfo"); VERIFY(c_flags_out, c_flags, "SDgetchunkinfo"); VERIFY(c_def_out.comp.comp_type, COMP_CODE_SZIP, "SDgetchunkinfo"); HDmemset(&c_def_out, 0, sizeof(HDF_CHUNK_DEF)); c_flags_out = 0; status = SDgetchunkinfo(sds_id, &c_def_out, &c_flags_out); CHECK(status, FAIL, "SDgetchunkinfo"); VERIFY(c_flags_out, c_flags, "SDgetchunkinfo"); VERIFY(c_def_out.comp.comp_type, COMP_CODE_SZIP, "SDgetchunkinfo"); /* Terminate access to the data sets. */ status = SDendaccess (sds_id0); CHECK(status, FAIL, "SDendaccess"); status = SDendaccess (sds_id); CHECK(status, FAIL, "SDendaccess"); /* Terminate access to the SD interface and close the file. */ status = SDend (sd_id); CHECK(status, FAIL, "SDend"); /* * Verify the compressed data */ /* Reopen the file and access the first data set. */ sd_id = SDstart (FILE_NAME_3D, DFACC_READ); sds_index = 0; sds_id = SDselect (sd_id, sds_index); CHECK(sds_id, FAIL, "SDselect:Failed to select a data set for chunking/szip compression testing"); /* Retrieve compression information about the dataset */ comp_type = COMP_CODE_INVALID; /* reset variables before retrieving info */ HDmemset(&cinfo, 0, sizeof(cinfo)) ; status = SDgetcompinfo(sds_id, &comp_type, &cinfo); CHECK(status, FAIL, "SDgetcompinfo"); VERIFY(comp_type, COMP_CODE_SZIP, "SDgetcompinfo"); /* Retrieve compression method alone from the dataset */ comp_type = COMP_CODE_INVALID; /* reset variables before retrieving info */ status = SDgetcomptype(sds_id, &comp_type); CHECK(status, FAIL, "SDgetcomptype"); VERIFY(comp_type, COMP_CODE_SZIP, "SDgetcomptype"); start[0] = 0; start[1] = 0; start[2] = 0; edges[0] = SDS_DIM0; edges[1] = SDS_DIM1; edges[2] = SDS_DIM2; status = SDreaddata (sds_id, start, NULL, edges, (VOIDP)all_data); CHECK(status, FAIL, "SDreaddata"); for (i = 0; i < SDS_DIM0; i++) { for (j = 0; j < SDS_DIM1; j++) { for (k = 0; k < SDS_DIM2; k++) { if (out_data[i][j][k] != all_data[i][j][k]) { fprintf(stderr,"Bogus val in loc [%d][%d][%d] want %ld got %ld\n", i, j,k, out_data[i][j][k], all_data[i][j][k]); num_errs++; } } } } /* Terminate access to the data set. */ status = SDendaccess (sds_id); CHECK(status, FAIL, "SDendaccess"); /* Terminate access to the SD interface and close the file. */ status = SDend (sd_id); CHECK(status, FAIL, "SDend"); /* Return the number of errors that's been kept track of so far */ return num_errs; } /* test_szip_chunk_3D */
/*------------------------------------------------------------------------- * write 2 big files for hyperslab reading *------------------------------------------------------------------------- */ static int do_big_file(void) { int32 sd1_id; /* SD interface identifier */ int32 sd2_id; /* SD interface identifier */ int32 sds1_id; /* SDS identifier */ int32 sds2_id; /* SDS identifier */ int32 dims[2]; /* sizes of the SDS dimensions */ int32 start[2]; /* start location to write */ int32 edges[2]; /* number of elements to write */ int32 sds1_idx; int32 sds2_idx; int32 rank; uint8 array_data[DIM0][DIM1]; uint8 append_data1[DIM1]; uint8 append_data2[DIM1]; intn i, j, n; /* Create 2 files and initiate the SD interface. */ if ((sd1_id = SDstart(FILE3_NAME, DFACC_CREATE))==FAIL) goto error; if ((sd2_id = SDstart(FILE4_NAME, DFACC_CREATE))==FAIL) goto error; /* Define the rank and dimensions of the data set to be created. */ rank = 2; dims[0] = SD_UNLIMITED; dims[1] = DIM1; /* Create 2 data sets */ if ((sds1_id = SDcreate(sd1_id, "data1", DFNT_UINT8, rank, dims))==FAIL) goto error; if ((sds2_id = SDcreate(sd2_id, "data1", DFNT_UINT8, rank, dims))==FAIL) goto error; /* initial values */ for (j = 0; j < DIM0; j++) { for (i = 0; i < DIM1; i++) array_data[j][i] = (i + j) + 1; } /* define the location, pattern, and size of the data set */ for (i = 0; i < rank; i++) { start[i] = 0; } edges[0] = DIM0; /* 10 */ edges[1] = DIM1; /* 5 */ if ( SDwritedata(sds1_id, start, NULL, edges, (VOIDP)array_data)==FAIL) goto error; if ( SDwritedata(sds2_id, start, NULL, edges, (VOIDP)array_data)==FAIL) goto error; /* terminate access to the datasets and SD interface */ if ( SDendaccess(sds1_id)==FAIL) goto error; if ( SDendaccess(sds2_id)==FAIL) goto error; if ( SDend(sd1_id)==FAIL) goto error; if ( SDend(sd2_id)==FAIL) goto error; /* append data */ if (( sd1_id = SDstart(FILE3_NAME, DFACC_WRITE))==FAIL) goto error; if (( sd2_id = SDstart(FILE4_NAME, DFACC_WRITE))==FAIL) goto error; if ((sds1_idx = SDnametoindex (sd1_id, "data1"))==FAIL) goto error; if ((sds2_idx = SDnametoindex (sd2_id, "data1"))==FAIL) goto error; if ((sds1_id = SDselect (sd1_id, sds1_idx))==FAIL) goto error; if ((sds2_id = SDselect (sd2_id, sds2_idx))==FAIL) goto error; /* define the location of the append */ for (n = 0; n < ADD_ROWS; n++) { start[0] = DIM0 + n; /* 10 */ start[1] = 0; edges[0] = 1; /* 1 row at a time */ edges[1] = DIM1; /* 5 elements */ /* store array values to be appended */ for (i = 0; i < DIM1; i++) append_data1[i] = i + 1; for (i = 0; i < DIM1; i++) append_data2[i] = i + 1; if (n == 20 || n == ADD_ROWS / 2 || n == ADD_ROWS - 10 ) { /* change a few values at 3 places evenly divided */ for (i = 0; i < DIM1; i++) append_data2[i] = 100; /* append data to file */ if ( SDwritedata (sds1_id, start, NULL, edges, (VOIDP) append_data1)==FAIL) goto error; if ( SDwritedata (sds2_id, start, NULL, edges, (VOIDP) append_data2)==FAIL) goto error; } } /* terminate access */ if ( SDendaccess (sds1_id)==FAIL) goto error; if ( SDendaccess (sds2_id)==FAIL) goto error; if ( SDend (sd1_id)==FAIL) goto error; if ( SDend (sd2_id)==FAIL) goto error; return SUCCEED; error: printf("Error...Exiting...\n"); return FAIL; }
GDALDataset *HDF4Dataset::Open( GDALOpenInfo * poOpenInfo ) { int32 i; if( !Identify( poOpenInfo ) ) return NULL; /* -------------------------------------------------------------------- */ /* Try opening the dataset. */ /* -------------------------------------------------------------------- */ int32 hHDF4; hHDF4 = Hopen(poOpenInfo->pszFilename, DFACC_READ, 0); if( hHDF4 <= 0 ) return( NULL ); Hclose( hHDF4 ); /* -------------------------------------------------------------------- */ /* Create a corresponding GDALDataset. */ /* -------------------------------------------------------------------- */ HDF4Dataset *poDS; poDS = new HDF4Dataset(); poDS->fp = poOpenInfo->fp; poOpenInfo->fp = NULL; /* -------------------------------------------------------------------- */ /* Open HDF SDS Interface. */ /* -------------------------------------------------------------------- */ poDS->hSD = SDstart( poOpenInfo->pszFilename, DFACC_READ ); if ( poDS->hSD == -1 ) { delete poDS; return NULL; } /* -------------------------------------------------------------------- */ /* Now read Global Attributes. */ /* -------------------------------------------------------------------- */ if ( poDS->ReadGlobalAttributes( poDS->hSD ) != CE_None ) { delete poDS; return NULL; } poDS->SetMetadata( poDS->papszGlobalMetadata, "" ); /* -------------------------------------------------------------------- */ /* Determine type of file we read. */ /* -------------------------------------------------------------------- */ const char *pszValue; if ( (pszValue = CSLFetchNameValue(poDS->papszGlobalMetadata, "Signature")) && EQUAL( pszValue, pszGDALSignature ) ) { poDS->iSubdatasetType = H4ST_GDAL; poDS->pszSubdatasetType = "GDAL_HDF4"; } else if ( (pszValue = CSLFetchNameValue(poDS->papszGlobalMetadata, "Title")) && EQUAL( pszValue, "SeaWiFS Level-1A Data" ) ) { poDS->iSubdatasetType = H4ST_SEAWIFS_L1A; poDS->pszSubdatasetType = "SEAWIFS_L1A"; } else if ( (pszValue = CSLFetchNameValue(poDS->papszGlobalMetadata, "Title")) && EQUAL( pszValue, "SeaWiFS Level-2 Data" ) ) { poDS->iSubdatasetType = H4ST_SEAWIFS_L2; poDS->pszSubdatasetType = "SEAWIFS_L2"; } else if ( (pszValue = CSLFetchNameValue(poDS->papszGlobalMetadata, "Title")) && EQUAL( pszValue, "SeaWiFS Level-3 Standard Mapped Image" ) ) { poDS->iSubdatasetType = H4ST_SEAWIFS_L3; poDS->pszSubdatasetType = "SEAWIFS_L3"; } else if ( (pszValue = CSLFetchNameValue(poDS->papszGlobalMetadata, "L1 File Generated By")) && EQUALN( pszValue, "HYP version ", 12 ) ) { poDS->iSubdatasetType = H4ST_HYPERION_L1; poDS->pszSubdatasetType = "HYPERION_L1"; } else { poDS->iSubdatasetType = H4ST_UNKNOWN; poDS->pszSubdatasetType = "UNKNOWN"; } /* -------------------------------------------------------------------- */ /* If we have HDF-EOS dataset, process it here. */ /* -------------------------------------------------------------------- */ char szName[VSNAMELENMAX + 1], szTemp[8192]; char *pszString; const char *pszName; int nCount; int32 aiDimSizes[H4_MAX_VAR_DIMS]; int32 iRank, iNumType, nAttrs; bool bIsHDF = true; // Sometimes "HDFEOSVersion" attribute is not defined and we will // determine HDF-EOS datasets using other records // (see ReadGlobalAttributes() method). if ( poDS->bIsHDFEOS || CSLFetchNameValue(poDS->papszGlobalMetadata, "HDFEOSVersion") ) { bIsHDF = false; int32 nSubDatasets, nStrBufSize; /* -------------------------------------------------------------------- */ /* Process swath layers. */ /* -------------------------------------------------------------------- */ hHDF4 = SWopen( poOpenInfo->pszFilename, DFACC_READ ); if( hHDF4 < 0) { delete poDS; CPLError( CE_Failure, CPLE_OpenFailed, "Failed to open HDF4 `%s'.\n", poOpenInfo->pszFilename ); return NULL; } nSubDatasets = SWinqswath(poOpenInfo->pszFilename, NULL, &nStrBufSize); #if DEBUG CPLDebug( "HDF4", "Number of HDF-EOS swaths: %d", (int)nSubDatasets ); #endif if ( nSubDatasets > 0 && nStrBufSize > 0 ) { char *pszSwathList; char **papszSwaths; pszSwathList = (char *)CPLMalloc( nStrBufSize + 1 ); SWinqswath( poOpenInfo->pszFilename, pszSwathList, &nStrBufSize ); pszSwathList[nStrBufSize] = '\0'; #if DEBUG CPLDebug( "HDF4", "List of HDF-EOS swaths: %s", pszSwathList ); #endif papszSwaths = CSLTokenizeString2( pszSwathList, ",", CSLT_HONOURSTRINGS ); CPLFree( pszSwathList ); if ( nSubDatasets != CSLCount(papszSwaths) ) { CSLDestroy( papszSwaths ); delete poDS; CPLDebug( "HDF4", "Can not parse list of HDF-EOS grids." ); return NULL; } for ( i = 0; i < nSubDatasets; i++) { char *pszFieldList; char **papszFields; int32 *paiRank, *paiNumType; int32 hSW, nFields, j; hSW = SWattach( hHDF4, papszSwaths[i] ); nFields = SWnentries( hSW, HDFE_NENTDFLD, &nStrBufSize ); pszFieldList = (char *)CPLMalloc( nStrBufSize + 1 ); paiRank = (int32 *)CPLMalloc( nFields * sizeof(int32) ); paiNumType = (int32 *)CPLMalloc( nFields * sizeof(int32) ); SWinqdatafields( hSW, pszFieldList, paiRank, paiNumType ); #if DEBUG { char *pszTmp = SPrintArray( GDT_UInt32, paiRank, nFields, "," ); CPLDebug( "HDF4", "Number of data fields in swath %d: %d", (int) i, (int) nFields ); CPLDebug( "HDF4", "List of data fields in swath %d: %s", (int) i, pszFieldList ); CPLDebug( "HDF4", "Data fields ranks: %s", pszTmp ); CPLFree( pszTmp ); } #endif papszFields = CSLTokenizeString2( pszFieldList, ",", CSLT_HONOURSTRINGS ); for ( j = 0; j < nFields; j++ ) { SWfieldinfo( hSW, papszFields[j], &iRank, aiDimSizes, &iNumType, NULL ); if ( iRank < 2 ) continue; // Add field to the list of GDAL subdatasets nCount = CSLCount( poDS->papszSubDatasets ) / 2; sprintf( szTemp, "SUBDATASET_%d_NAME", nCount + 1 ); // We will use the field index as an identificator. poDS->papszSubDatasets = CSLSetNameValue( poDS->papszSubDatasets, szTemp, CPLSPrintf("HDF4_EOS:EOS_SWATH:\"%s\":%s:%s", poOpenInfo->pszFilename, papszSwaths[i], papszFields[j]) ); sprintf( szTemp, "SUBDATASET_%d_DESC", nCount + 1 ); pszString = SPrintArray( GDT_UInt32, aiDimSizes, iRank, "x" ); poDS->papszSubDatasets = CSLSetNameValue( poDS->papszSubDatasets, szTemp, CPLSPrintf( "[%s] %s %s (%s)", pszString, papszFields[j], papszSwaths[i], poDS->GetDataTypeName(iNumType) ) ); CPLFree( pszString ); } CSLDestroy( papszFields ); CPLFree( paiNumType ); CPLFree( paiRank ); CPLFree( pszFieldList ); SWdetach( hSW ); } CSLDestroy( papszSwaths ); } SWclose( hHDF4 ); /* -------------------------------------------------------------------- */ /* Process grid layers. */ /* -------------------------------------------------------------------- */ hHDF4 = GDopen( poOpenInfo->pszFilename, DFACC_READ ); nSubDatasets = GDinqgrid( poOpenInfo->pszFilename, NULL, &nStrBufSize ); #if DEBUG CPLDebug( "HDF4", "Number of HDF-EOS grids: %d", (int)nSubDatasets ); #endif if ( nSubDatasets > 0 && nStrBufSize > 0 ) { char *pszGridList; char **papszGrids; pszGridList = (char *)CPLMalloc( nStrBufSize + 1 ); GDinqgrid( poOpenInfo->pszFilename, pszGridList, &nStrBufSize ); #if DEBUG CPLDebug( "HDF4", "List of HDF-EOS grids: %s", pszGridList ); #endif papszGrids = CSLTokenizeString2( pszGridList, ",", CSLT_HONOURSTRINGS ); CPLFree( pszGridList ); if ( nSubDatasets != CSLCount(papszGrids) ) { CSLDestroy( papszGrids ); delete poDS; CPLDebug( "HDF4", "Can not parse list of HDF-EOS grids." ); return NULL; } for ( i = 0; i < nSubDatasets; i++) { char *pszFieldList; char **papszFields; int32 *paiRank, *paiNumType; int32 hGD, nFields, j; hGD = GDattach( hHDF4, papszGrids[i] ); nFields = GDnentries( hGD, HDFE_NENTDFLD, &nStrBufSize ); pszFieldList = (char *)CPLMalloc( nStrBufSize + 1 ); paiRank = (int32 *)CPLMalloc( nFields * sizeof(int32) ); paiNumType = (int32 *)CPLMalloc( nFields * sizeof(int32) ); GDinqfields( hGD, pszFieldList, paiRank, paiNumType ); #if DEBUG { char* pszTmp = SPrintArray( GDT_UInt32, paiRank, nFields, "," ); CPLDebug( "HDF4", "Number of fields in grid %d: %d", (int) i, (int) nFields ); CPLDebug( "HDF4", "List of fields in grid %d: %s", (int) i, pszFieldList ); CPLDebug( "HDF4", "Fields ranks: %s", pszTmp ); CPLFree( pszTmp ); } #endif papszFields = CSLTokenizeString2( pszFieldList, ",", CSLT_HONOURSTRINGS ); for ( j = 0; j < nFields; j++ ) { GDfieldinfo( hGD, papszFields[j], &iRank, aiDimSizes, &iNumType, NULL ); if ( iRank < 2 ) continue; // Add field to the list of GDAL subdatasets nCount = CSLCount( poDS->papszSubDatasets ) / 2; sprintf( szTemp, "SUBDATASET_%d_NAME", nCount + 1 ); // We will use the field index as an identificator. poDS->papszSubDatasets = CSLSetNameValue(poDS->papszSubDatasets, szTemp, CPLSPrintf( "HDF4_EOS:EOS_GRID:\"%s\":%s:%s", poOpenInfo->pszFilename, papszGrids[i], papszFields[j])); sprintf( szTemp, "SUBDATASET_%d_DESC", nCount + 1 ); pszString = SPrintArray( GDT_UInt32, aiDimSizes, iRank, "x" ); poDS->papszSubDatasets = CSLSetNameValue( poDS->papszSubDatasets, szTemp, CPLSPrintf("[%s] %s %s (%s)", pszString, papszFields[j], papszGrids[i], poDS->GetDataTypeName(iNumType)) ); CPLFree( pszString ); } CSLDestroy( papszFields ); CPLFree( paiNumType ); CPLFree( paiRank ); CPLFree( pszFieldList ); GDdetach( hGD ); } CSLDestroy( papszGrids ); GDclose( hHDF4 ); } GDclose( hHDF4 ); bIsHDF = ( nSubDatasets == 0 ); // Try to read as HDF } if( bIsHDF ) { /* -------------------------------------------------------------------- */ /* Make a list of subdatasets from SDSs contained in input HDF file. */ /* -------------------------------------------------------------------- */ int32 nDatasets; if ( SDfileinfo( poDS->hSD, &nDatasets, &nAttrs ) != 0 ) return NULL; for ( i = 0; i < nDatasets; i++ ) { int32 iSDS; iSDS = SDselect( poDS->hSD, i ); if ( SDgetinfo( iSDS, szName, &iRank, aiDimSizes, &iNumType, &nAttrs) != 0 ) return NULL; if ( iRank == 1 ) // Skip 1D datsets continue; // Do sort of known datasets. We will display only image bands if ( (poDS->iSubdatasetType == H4ST_SEAWIFS_L1A ) && !EQUALN( szName, "l1a_data", 8 ) ) continue; else pszName = szName; // Add datasets with multiple dimensions to the list of GDAL subdatasets nCount = CSLCount( poDS->papszSubDatasets ) / 2; sprintf( szTemp, "SUBDATASET_%d_NAME", nCount + 1 ); // We will use SDS index as an identificator, because SDS names // are not unique. Filename also needed for further file opening poDS->papszSubDatasets = CSLSetNameValue(poDS->papszSubDatasets, szTemp, CPLSPrintf( "HDF4_SDS:%s:\"%s\":%ld", poDS->pszSubdatasetType, poOpenInfo->pszFilename, (long)i) ); sprintf( szTemp, "SUBDATASET_%d_DESC", nCount + 1 ); pszString = SPrintArray( GDT_UInt32, aiDimSizes, iRank, "x" ); poDS->papszSubDatasets = CSLSetNameValue(poDS->papszSubDatasets, szTemp, CPLSPrintf( "[%s] %s (%s)", pszString, pszName, poDS->GetDataTypeName(iNumType)) ); CPLFree( pszString ); SDendaccess( iSDS ); } SDend( poDS->hSD ); poDS->hSD = 0; } /* -------------------------------------------------------------------- */ /* Build a list of raster images. Note, that HDF-EOS dataset may */ /* contain a raster image as well. */ /* -------------------------------------------------------------------- */ hHDF4 = Hopen(poOpenInfo->pszFilename, DFACC_READ, 0); poDS->hGR = GRstart( hHDF4 ); if ( poDS->hGR != -1 ) { if ( GRfileinfo( poDS->hGR, &poDS->nImages, &nAttrs ) == -1 ) return NULL; for ( i = 0; i < poDS->nImages; i++ ) { int32 iInterlaceMode; int32 iGR = GRselect( poDS->hGR, i ); // iRank in GR interface has another meaning. It represents number // of samples per pixel. aiDimSizes has only two dimensions. if ( GRgetiminfo( iGR, szName, &iRank, &iNumType, &iInterlaceMode, aiDimSizes, &nAttrs ) != 0 ) return NULL; nCount = CSLCount( poDS->papszSubDatasets ) / 2; sprintf( szTemp, "SUBDATASET_%d_NAME", nCount + 1 ); poDS->papszSubDatasets = CSLSetNameValue(poDS->papszSubDatasets, szTemp,CPLSPrintf( "HDF4_GR:UNKNOWN:\"%s\":%ld", poOpenInfo->pszFilename, (long)i)); sprintf( szTemp, "SUBDATASET_%d_DESC", nCount + 1 ); pszString = SPrintArray( GDT_UInt32, aiDimSizes, 2, "x" ); poDS->papszSubDatasets = CSLSetNameValue(poDS->papszSubDatasets, szTemp, CPLSPrintf( "[%sx%ld] %s (%s)", pszString, (long)iRank, szName, poDS->GetDataTypeName(iNumType)) ); CPLFree( pszString ); GRendaccess( iGR ); } GRend( poDS->hGR ); poDS->hGR = 0; } Hclose( hHDF4 ); poDS->nRasterXSize = poDS->nRasterYSize = 512; // XXX: bogus values // Make sure we don't try to do any pam stuff with this dataset. poDS->nPamFlags |= GPF_NOSAVE; /* -------------------------------------------------------------------- */ /* If we have single subdataset only, open it immediately */ /* -------------------------------------------------------------------- */ if ( CSLCount( poDS->papszSubDatasets ) / 2 == 1 ) { char *pszSDSName; pszSDSName = CPLStrdup( CSLFetchNameValue( poDS->papszSubDatasets, "SUBDATASET_1_NAME" )); delete poDS; poDS = NULL; GDALDataset* poRetDS = (GDALDataset*) GDALOpen( pszSDSName, poOpenInfo->eAccess ); CPLFree( pszSDSName ); if (poRetDS) { poRetDS->SetDescription(poOpenInfo->pszFilename); } return poRetDS; } else { /* -------------------------------------------------------------------- */ /* Confirm the requested access is supported. */ /* -------------------------------------------------------------------- */ if( poOpenInfo->eAccess == GA_Update ) { delete poDS; CPLError( CE_Failure, CPLE_NotSupported, "The HDF4 driver does not support update access to existing" " datasets.\n" ); return NULL; } } return( poDS ); }
static intn test_szip_SDSfl64bit() { /************************* Variable declaration **************************/ int32 sd_id, sds_id; intn status; int32 dim_sizes[2], array_rank, num_type, attributes; char name[H4_MAX_NC_NAME]; comp_info c_info; int32 start[2], edges[2]; float64 fill_value = 0; /* Fill value */ int i,j; int num_errs = 0; /* number of errors so far */ float64 out_data[LENGTH][WIDTH]; float64 in_data[LENGTH][WIDTH]={ 100.0,100.0,200.0,200.0,300.0,400.0, 100.0,100.0,200.0,200.0,300.0,400.0, 100.0,100.0,200.0,200.0,300.0,400.0, 300.0,300.0, 0.0,400.0,300.0,400.0, 300.0,300.0, 0.0,400.0,300.0,400.0, 300.0,300.0, 0.0,400.0,300.0,400.0, 0.0, 0.0,600.0,600.0,300.0,400.0, 500.0,500.0,600.0,600.0,300.0,400.0, 0.0, 0.0,600.0,600.0,300.0,400.0}; /********************* End of variable declaration ***********************/ /* Create the file and initialize SD interface */ sd_id = SDstart (FILE_NAMEfl64, DFACC_CREATE); CHECK(sd_id, FAIL, "SDstart"); /* Create the SDS */ dim_sizes[0] = LENGTH; dim_sizes[1] = WIDTH; sds_id = SDcreate (sd_id, SDS_NAME, DFNT_FLOAT64, RANK, dim_sizes); CHECK(sds_id, FAIL, "SDcreate:Failed to create a data set for szip compression testing"); /* Define the location, pattern, and size of the data set */ for (i = 0; i < RANK; i++) { start[i] = 0; edges[i] = dim_sizes[i]; } /* Fill the SDS array with the fill value */ status = SDsetfillvalue (sds_id, (VOIDP)&fill_value); CHECK(status, FAIL, "SDsetfillvalue"); /* Initialization for SZIP */ c_info.szip.pixels_per_block = 2; c_info.szip.options_mask = SZ_EC_OPTION_MASK; c_info.szip.options_mask |= SZ_RAW_OPTION_MASK; c_info.szip.bits_per_pixel = 0; c_info.szip.pixels = 0; c_info.szip.pixels_per_scanline = 0; /* Set the compression */ status = SDsetcompress (sds_id, COMP_CODE_SZIP, &c_info); CHECK(status, FAIL, "SDsetcompress"); /* Write data to the SDS */ status = SDwritedata(sds_id, start, NULL, edges, (VOIDP)in_data); CHECK(status, FAIL, "SDwritedata"); /* Terminate access to the data set */ status = SDendaccess (sds_id); CHECK(status, FAIL, "SDendaccess"); /* Terminate access to the SD interface and close the file to flush the compressed info to the file */ status = SDend (sd_id); CHECK(status, FAIL, "SDend"); /* * Verify the compressed data */ /* Reopen the file and select the first SDS */ sd_id = SDstart (FILE_NAMEfl64, DFACC_READ); CHECK(sd_id, FAIL, "SDstart"); sds_id = SDselect (sd_id, 0); CHECK(sds_id, FAIL, "SDselect:Failed to select a data set for szip compression testing"); /* Retrieve information of the data set */ status = SDgetinfo(sds_id, name, &array_rank, dim_sizes, &num_type, &attributes); CHECK(status, FAIL, "SDgetinfo"); /* Wipe out the output buffer */ HDmemset(&out_data, 0, sizeof(out_data)); /* Read the data set */ start[0] = 0; start[1] = 0; edges[0] = LENGTH; edges[1] = WIDTH; status = SDreaddata (sds_id, start, NULL, edges, (VOIDP)out_data); CHECK(status, FAIL, "SDreaddata"); /* Compare read data against input data */ for (j=0; j<LENGTH; j++) { for (i=0; i<WIDTH; i++) if (out_data[j][i] != in_data[j][i]) { fprintf(stderr,"Bogus val in loc [%d][%d] in compressed dset, want %ld got %ld\n", j, i, (long)in_data[j][i], (long)out_data[j][i]); num_errs++; } } /* Terminate access to the data set */ status = SDendaccess (sds_id); CHECK(status, FAIL, "SDendaccess"); /* Terminate access to the SD interface and close the file */ status = SDend (sd_id); CHECK(status, FAIL, "SDend"); /* Return the number of errors that's been kept track of so far */ return num_errs; } /* test_szip_SDSfl64bit */