char* ossimHdf4SubDataset::getTileBuf(const ossimIrect& rect, ossim_uint32 band) { // bool status = false; int32 start[H4_MAX_NC_DIMS], edges[H4_MAX_NC_DIMS]; if (m_rank == 3) { start[0] = band; start[1] = rect.ul().y; start[2] = rect.ul().x; edges[0] = 1; edges[1] = rect.height(); edges[2] = rect.width(); } else if (m_rank == 2) { start[0] = rect.ul().y; start[1] = rect.ul().x; edges[0] = rect.height(); edges[1] = rect.width(); } int32 numValues = rect.width() * rect.height(); ossim_int32 size = getDataTypeSize(m_dataType); char* data = new char[size * numValues]; // intn statusSd = SDreaddata(m_sds_id, start, NULL, edges, (VOIDP)data); SDreaddata(m_sds_id, start, NULL, edges, (VOIDP)data); return data; }
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; }
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); }
/* Read current scan line for all bands to be processed. Returns 0 if no errors, non-zero otherwise. */ int read_scan(int iscan, SDS sds[Nitems]) { int ib; for (ib = 0; ib < Nitems; ib++) { if (sds[ib].id == -1) continue; switch (sds[ib].rank) { case 2: sds[ib].start[0] = iscan * sds[ib].rowsperscan; break; case 3: sds[ib].start[1] = iscan * sds[ib].rowsperscan; break; } if (SDreaddata(sds[ib].id, sds[ib].start, NULL, sds[ib].edges, sds[ib].data) == -1) { fprintf(stderr, " Can't read scan %d of SDS \"%s\"\n", iscan, sds[ib].name); return 1; } } return 0; }
/* 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 void *var_readv(SDSVarInfo *var, void **bufp, int *index) { int32 start[H4_MAX_VAR_DIMS], count[H4_MAX_VAR_DIMS]; size_t bufsize = sds_type_size(var->type); for (int i = 0; i < var->ndims; i++) { if (index[i] < 0) { start[i] = 0; count[i] = (int32)var->dims[i]->size; } else { start[i] = (int32)index[i]; count[i] = 1; } bufsize *= (size_t)count[i]; } H4Buffer *buf = prep_read_buffer(var, bufp); h4buffer_ensure(buf, bufsize); int status = SDreaddata(buf->sds_id, start, NULL, count, buf->data); CHECK_HDF_ERROR(var->sds->path, status); return buf->data; }
static intn test_dim1_SDS2(void) { char sds_name[20]; float32 sds1_data[] = {0.1, 2.3, 4.5, 6.7, 8.9}; float32 sds2_data[2][3] = {{0.1, 2.3, 4.5}, {4.5, 6.7, 8.9}}; int32 dimsize[1], dimsize2[2]; int32 sds1_id, sds2_id, file_id, dim_id, index; int32 start=0, stride=1, stat; int32 start2[2]={0,0}, stride2[2]={1,1}; int32 scale1 [5] = {101,102,103,104,105}, scale1_out[5]; int32 num_type, array_rank, attributes; int32 n_datasets, n_file_attrs, n_local_attrs; float32 out_data2[2][3]; intn datanum, ranknum, status =0, i, idx, idx1, idx2; intn num_errs = 0; /* number of errors so far */ file_id = SDstart(FILE2, DFACC_CREATE); CHECK(file_id, FAIL, "SDstart"); dimsize[0] = 5; dimsize2[0] = 2; dimsize2[1] = 3; sds1_id = SDcreate(file_id, VAR1_NAME, DFNT_FLOAT32, 1, dimsize); CHECK(sds1_id, FAIL, "SDcreate"); /* Set the dimension name to be the same as the next dataset (not created yet) */ dim_id = SDgetdimid(sds1_id, 0); CHECK(dim_id, FAIL, "SDgetdimid"); status = SDsetdimname(dim_id, VAR2_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"); /* Create and write data to the second dataset VAR2_NAME */ sds2_id = SDcreate(file_id, VAR2_NAME, DFNT_FLOAT32, 2, dimsize2); CHECK(sds2_id, FAIL, "SDcreate"); stat = SDwritedata(sds2_id, start2, stride2, dimsize2, sds2_data); CHECK(status, FAIL, "SDwritedata"); status = SDendaccess(sds2_id); CHECK(status, FAIL, "SDendaccess"); /* Get file info and verify that there are 2 datasets in the file. */ status = SDfileinfo(file_id, &n_datasets, &n_file_attrs); CHECK(status, FAIL, "SDfileinfo"); VERIFY(n_datasets, 2, "SDfileinfo"); /* Write values to the dimension VAR2_NAME (same name as VAR2_NAME) */ status = SDsetdimscale (dim_id, dimsize[0], DFNT_INT32, scale1); CHECK(status, FAIL, "SDsetdimscale"); /* Get file info and verify that there are 3 datasets in the file: 2 SDS and 1 coordinate variable */ status = SDfileinfo(file_id, &n_datasets, &n_file_attrs); CHECK(status, FAIL, "SDfileinfo"); VERIFY(n_datasets, 3, "SDfileinfo"); /* Close dataset and file */ status = SDendaccess(sds1_id); CHECK(status, FAIL, "SDendaccess"); status = SDend(file_id); CHECK(status, FAIL, "SDend"); /* Open the file again to check its data */ file_id = SDstart(FILE2, DFACC_RDWR); CHECK(file_id, FAIL, "SDstart"); /* Verify dimension scale of the first dimension of SDS VAR1_NAME */ /* Get access to dataset VAR1_NAME */ index = SDnametoindex(file_id, VAR1_NAME); CHECK(index, FAIL, "SDnametoindex"); sds1_id = SDselect(file_id, index); CHECK(sds1_id, FAIL, "SDselect"); /* Get access to its first dimension */ dim_id = SDgetdimid(sds1_id, 0); CHECK(dim_id, FAIL, "SDgetdimid"); /* Get dimension scale and verify the values */ status = SDgetdimscale (dim_id, scale1_out); CHECK(status, FAIL, "SDgetdimscale"); for (idx = 0; idx < dimsize[0]; idx++) if (scale1_out[idx] != scale1[idx]) { fprintf(stderr, "Read value (%d) differs from written (%d) at [%d]\n", scale1_out[idx], scale1[idx], idx); num_errs++; } /* End verifying dimension scale */ /* Verify dimension scale of the first dimension of SDS VAR1_NAME */ /* Get access to dataset VAR2_NAME */ index = SDnametoindex(file_id, VAR2_NAME); CHECK(index, FAIL, "SDnametoindex"); sds2_id = SDselect(file_id, index); CHECK(sds2_id, FAIL, "SDselect"); /* Get dataset's info and verify them */ status = SDgetinfo(sds2_id, sds_name, &array_rank, dimsize2, &num_type, &n_local_attrs); CHECK(status, FAIL, "SDgetinfo"); VERIFY(array_rank, 2, "SDfileinfo"); VERIFY(num_type, DFNT_FLOAT32, "SDfileinfo"); VERIFY(n_local_attrs, 0, "SDfileinfo"); /* Read and verify the dataset's data */ status = SDreaddata (sds2_id, start2, NULL, dimsize2, &out_data2); CHECK(status, FAIL, "SDreaddata"); for (idx1 = 0; idx1 < dimsize2[0]; idx1++) for (idx2 = 0; idx2 < dimsize2[1]; idx2++) { if (out_data2[idx1][idx2] != sds2_data[idx1][idx2]) { fprintf(stderr, "Read value (%f) differs from written (%f) at [%d][%d]\n", out_data2[idx1][idx2], sds2_data[idx1][idx2], idx1, idx2); num_errs++; } } /* Verify again that the number of datasets in the file is 3, 2 SDSs and 1 coordinate variable */ status = SDfileinfo(file_id, &n_datasets, &n_file_attrs); CHECK(status, FAIL, "SDfileinfo"); VERIFY(n_datasets, 3, "SDfileinfo"); status = SDendaccess(sds1_id); CHECK(status, FAIL, "SDendaccess"); status = SDendaccess(sds2_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_SDS2 */
void RemapHDF4::generateHistogram() { float rSize = m_rawMax-m_rawMin; QProgressDialog progress("Generating Histogram", "Cancel", 0, 100, 0); progress.setMinimumDuration(0); int nX, nY, nZ; nX = m_depth; nY = m_width; nZ = m_height; int nbytes = nY*nZ*m_bytesPerVoxel; uchar *tmp = new uchar[nbytes]; m_histogram.clear(); if (m_voxelType == _UChar || m_voxelType == _Char || m_voxelType == _UShort || m_voxelType == _Short) { for(uint i=0; i<rSize+1; i++) m_histogram.append(0); } else { for(uint i=0; i<65536; i++) m_histogram.append(0); } int histogramSize = m_histogram.size()-1; int32 start[2], edges[2]; start[0] = start[1] = 0; edges[0] = m_width; edges[1] = m_height; for(uint i=0; i<nX; i++) { progress.setValue((int)(100.0*(float)i/(float)nX)); qApp->processEvents(); int32 sd_id = SDstart(m_imageList[i].toAscii().data(), DFACC_READ); int32 sds_id = SDselect(sd_id, m_Index); int status = SDreaddata(sds_id, start, NULL, edges, (VOIDP)tmp); status = SDendaccess(sds_id); status = SDend(sd_id); if (m_voxelType == _UChar) { uchar *ptr = tmp; GENHISTOGRAM(); } else if (m_voxelType == _Char) { char *ptr = (char*) tmp; GENHISTOGRAM(); } if (m_voxelType == _UShort) { ushort *ptr = (ushort*) tmp; GENHISTOGRAM(); } else if (m_voxelType == _Short) { short *ptr = (short*) tmp; GENHISTOGRAM(); } else if (m_voxelType == _Int) { int *ptr = (int*) tmp; GENHISTOGRAM(); } else if (m_voxelType == _Float) { float *ptr = (float*) tmp; GENHISTOGRAM(); } } delete [] tmp; while(m_histogram.last() == 0) m_histogram.removeLast(); while(m_histogram.first() == 0) m_histogram.removeFirst(); progress.setValue(100); qApp->processEvents(); }
int main(int ac, char **av) { char input_file_path[256], output_file_path[256]; char hdf4sds_name[256], hdf4sds_attr_name[256]; char hdf4sds_str_attr_buff[256]; int hdf4sd_id, hdf4access_mode, hdf4status, hdf4n_datasets, hdf4n_file_attrs, hdf4sds_id, hdf4sds_index, hdf4sds_rank, hdf4sds_data_type, hdf4sds_n_attrs, hdf4sds_attr_data_type, hdf4sds_attr_n_values, hdf4sds_num_elements; int hdf4sds_dim_sizes[8], hdf4sds_read_start[8], hdf4sds_read_stride[8]; float *hdf4sds_flt_buff; hid_t hdf5file_id, hdf5dataset_id, hdf5space_id, hdf5attr_id, hdf5attr_space_id, hdf5str_type; herr_t hdf5status; hsize_t hdf5dataset_dims[8], hdf5attr_size; /* Get input file name from the command line, and create output file name */ if (ac < 2) { fprintf(stderr, "Usage is: hdf4tohdf5 inputfile outputfile\n"); fprintf(stderr, "If no output file is given, inputfile.h5 will be produced.\n"); return 1; } strcpy(input_file_path, av[1]); if (ac > 2) { strcpy(output_file_path, av[2]); } else { strcpy(output_file_path, input_file_path); strcat(output_file_path, ".h5"); } /* Open hdf4 file */ hdf4access_mode = DFACC_READ; hdf4sd_id = SDstart(input_file_path, hdf4access_mode); hdf4status = SDfileinfo(hdf4sd_id, &hdf4n_datasets, &hdf4n_file_attrs); if (hdf4status == FAIL) { fprintf(stderr, "Reading hdf4 file failed. Returning.\n"); return 1; } #ifdef DEBUG printf("Id: %i, Datasets: %i, File_attrs: %i\n", hdf4sd_id, hdf4n_datasets, hdf4n_file_attrs); #endif /* Open hdf5 file */ hdf5file_id = H5Fcreate(output_file_path, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT); if (hdf5file_id < 0) { fprintf(stderr, "Failed to open HDF5 file %s for writing.\n", output_file_path); return 1; } /* Loop through datasets */ for(hdf4sds_index = 0; hdf4sds_index < hdf4n_datasets; hdf4sds_index++) { hdf4sds_id = SDselect(hdf4sd_id, hdf4sds_index); hdf4status = SDgetinfo(hdf4sds_id, hdf4sds_name, &hdf4sds_rank, hdf4sds_dim_sizes, &hdf4sds_data_type, &hdf4sds_n_attrs); #ifdef DEBUG printf("\nsds_id: %i, sds_name: %s, sds_rank: %i, sds_data_type: %i,\nn_attrs_: %i, dim_sizes: %i %i %i\n", hdf4sds_id, hdf4sds_name, hdf4sds_rank, hdf4sds_data_type, hdf4sds_n_attrs, hdf4sds_dim_sizes[0], hdf4sds_dim_sizes[1], hdf4sds_dim_sizes[2]); #endif /* Get data from SDS into buffer */ hdf4sds_num_elements = hdf4sds_dim_sizes[0]; hdf4sds_read_start[0] = 0; hdf4sds_read_stride[0] = 1; if (hdf4sds_rank > 1){ for(int i=1; i < hdf4sds_rank; i++) { hdf4sds_num_elements = hdf4sds_num_elements*hdf4sds_dim_sizes[i]; hdf4sds_read_start[i] = 0; hdf4sds_read_stride[i] = 1; } } #ifdef DEBUG printf("Total number of elements for this sds: %i\n", hdf4sds_num_elements); #endif /* Allocate the buffer to read in the data */ hdf4sds_flt_buff = malloc((sizeof (float))*hdf4sds_num_elements); hdf4status = SDreaddata(hdf4sds_id, hdf4sds_read_start, hdf4sds_read_stride, hdf4sds_dim_sizes, (VOIDP)hdf4sds_flt_buff); /* Have all of the data from the hdf4 file, now need to load it into hdf5 */ for (int i=0; i < hdf4sds_rank; i++) { hdf5dataset_dims[i] = (hsize_t)hdf4sds_dim_sizes[i]; } hdf5space_id = H5Screate_simple(hdf4sds_rank, hdf5dataset_dims, hdf5dataset_dims); if (hdf4sds_data_type == 5) { //Floats hdf5dataset_id = H5Dcreate(hdf5file_id, hdf4sds_name, H5T_IEEE_F32LE, hdf5space_id, H5P_DEFAULT); hdf5status = H5Dwrite(hdf5dataset_id, H5T_IEEE_F32LE, H5S_ALL, H5S_ALL, H5P_DEFAULT, hdf4sds_flt_buff); /* We've finished reading in the data. Free the hdf4 data buffer. */ free(hdf4sds_flt_buff); /*Assign the HDF5 attributes */ for(int i=0; i < hdf4sds_n_attrs; i++) { hdf4status = SDattrinfo(hdf4sds_id, i, hdf4sds_attr_name, &hdf4sds_attr_data_type, &hdf4sds_attr_n_values); #ifdef DEBUG printf("Attribute %i: Name = %s, Data type = %i, N_values = %i\n", i, hdf4sds_attr_name, hdf4sds_attr_data_type, hdf4sds_attr_n_values); #endif if(hdf4sds_attr_data_type == 4) { /* Read the string attributes */ hdf4status = SDreadattr(hdf4sds_id, i, (VOIDP)hdf4sds_str_attr_buff); hdf4sds_str_attr_buff[hdf4sds_attr_n_values]='\0'; #ifdef DEBUG printf("Value: %s\n", hdf4sds_str_attr_buff); #endif hdf5attr_size = (hsize_t)hdf4sds_attr_n_values; hdf5attr_space_id = H5Screate(H5S_SCALAR); hdf5str_type = H5Tcopy(H5T_C_S1); hdf5status = H5Tset_size(hdf5str_type, (size_t)hdf4sds_attr_n_values); hdf5attr_id = H5Acreate(hdf5dataset_id, hdf4sds_attr_name, hdf5str_type, hdf5attr_space_id, H5P_DEFAULT); hdf5status = H5Awrite(hdf5attr_id, hdf5str_type, hdf4sds_str_attr_buff); hdf5status = H5Aclose(hdf5attr_id); } } hdf5status = H5Dclose(hdf5dataset_id); } hdf5status = H5Sclose(hdf5space_id); hdf4status = SDendaccess(hdf4sds_id); } /* Close access to hdf4 file */ hdf4status = SDend(hdf4sd_id); /* Close access to HDF5 file */ hdf5status = H5Fclose(hdf5file_id); return 0; }
void RemapHDF4::findMinMax() { QProgressDialog progress("Finding Min and Max", "Cancel", 0, 100, 0); progress.setMinimumDuration(0); int nX, nY, nZ; nX = m_depth; nY = m_width; nZ = m_height; int nbytes = nY*nZ*m_bytesPerVoxel; uchar *tmp = new uchar[nbytes]; m_rawMin = 10000000; m_rawMax = -10000000; int32 start[2], edges[2]; start[0] = start[1] = 0; edges[0] = m_width; edges[1] = m_height; for(uint i=0; i<m_depth; i++) { progress.setValue((int)(100.0*(float)i/(float)nX)); qApp->processEvents(); int32 sd_id = SDstart(m_imageList[i].toAscii().data(), DFACC_READ); int32 sds_id = SDselect(sd_id, m_Index); int status = SDreaddata(sds_id, start, NULL, edges, (void*)tmp); status = SDendaccess(sds_id); status = SDend(sd_id); if (m_voxelType == _UChar) { uchar *ptr = tmp; FINDMINMAX(); } else if (m_voxelType == _Char) { char *ptr = (char*) tmp; FINDMINMAX(); } if (m_voxelType == _UShort) { ushort *ptr = (ushort*) tmp; FINDMINMAX(); } else if (m_voxelType == _Short) { short *ptr = (short*) tmp; FINDMINMAX(); } else if (m_voxelType == _Int) { int *ptr = (int*) tmp; FINDMINMAX(); } else if (m_voxelType == _Float) { float *ptr = (float*) tmp; FINDMINMAX(); } } delete [] tmp; progress.setValue(100); qApp->processEvents(); }
/* Test SDSs with unlimited dimensions. This routine creates SDSs with unlimited dimensions, writes data to it, and checks the sizes returned by SDgetdatasize */ static intn test_extend_SDSs(int32 fid) { int32 sds_id, sds_index; int32 dimsize[2], start[2], edges[2]; int32 dimsize1[1], start1[1], edges1[1]; int32 data[Y_LENGTH][X_LENGTH]; float fdata[Y_LENGTH]; int32 output[Y_LENGTH][X_LENGTH]; intn status; int i, j; int num_errs = 0; /* number of errors so far */ /* Initialize data for the dataset */ for (j = 0; j < Y_LENGTH; j++) { for (i = 0; i < X_LENGTH; i++) data[j][i] = (i + j) + 1; } /* Create a 2x2 dataset called "EmptyDataset" */ dimsize[0] = SD_UNLIMITED; dimsize[1] = X_LENGTH; sds_id = SDcreate(fid, "AppendableDataset 1", DFNT_INT32, 2, dimsize); CHECK(sds_id, FAIL, "test_extend_SDSs: SDcreate 'AppendableDataset 1'"); /* Write the stored data to the dataset */ start[0] = start[1] = 0; edges[0] = Y_LENGTH; edges[1] = X_LENGTH; status = SDwritedata(sds_id, start, NULL, edges, (VOIDP)data); CHECK(sds_id, FAIL, "test_extend_SDSs: SDwritedata"); /* Check data. */ HDmemset(&output, 0, sizeof(output)); status = SDreaddata(sds_id, start, NULL, edges, (VOIDP)output); CHECK(sds_id, FAIL, "test_extend_SDSs: SDreaddata"); /* Initialize data for the dataset */ for (j = 0; j < Y_LENGTH; j++) for (i = 0; i < X_LENGTH; i++) if (output[j][i] != data[j][i]) fprintf(stderr, "Read value (%d) differs from written (%d) at [%d,%d]\n", output[j][i], data[j][i], j, i); /* Close this SDS */ status = SDendaccess(sds_id); CHECK(status, FAIL, "test_extend_SDSs: SDendaccess"); /* Check that this SDS is empty */ check_datasizes(fid, "AppendableDataset 1", Y_LENGTH*X_LENGTH*SIZE_INT32, Y_LENGTH*X_LENGTH*SIZE_INT32, &num_errs); /* Create another dataset with 1 unlimited dimension */ sds_id = SDcreate(fid, "AppendableDataset 2", DFNT_FLOAT64, 1, dimsize); CHECK(sds_id, FAIL, "test_extend_SDSs: SDcreate 'AppendableDataset 2'"); /* Define the location and size of the data to be written to the dataset */ start1[0] = 0; edges1[0] = Y_LENGTH; /* Write the stored data to the dataset */ status = SDwritedata(sds_id, start1, NULL, edges1, (VOIDP)fdata); CHECK(sds_id, FAIL, "test_extend_SDSs: SDwritedata"); /* Close this SDS */ status = SDendaccess(sds_id); CHECK(status, FAIL, "test_extend_SDSs: SDendaccess"); /* Check the size of the data of this SDS */ check_datasizes(fid, "AppendableDataset 2", Y_LENGTH*SIZE_FLOAT64, Y_LENGTH*SIZE_FLOAT64, &num_errs); /* Return the number of errors that's been kept track of so far */ return num_errs; } /* test_extend_SDSs */
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 */
QImage RemapHDF4::getHeightSliceImage(int slc) { if (m_image) delete [] m_image; m_image = new uchar[m_depth*m_width]; int nbytes = m_depth*m_width*m_bytesPerVoxel; uchar *tmp = new uchar[nbytes]; uchar *hdftmp = new uchar[m_width*m_bytesPerVoxel]; int32 start[2], edges[2]; start[0] = slc; start[1] = 0; edges[0] = m_width; edges[1] = 1; for(uint i=0; i<m_depth; i++) { int32 sd_id = SDstart(m_imageList[i].toAscii().data(), DFACC_READ); int32 sds_id = SDselect(sd_id, m_Index); int status = SDreaddata(sds_id, start, NULL, edges, (VOIDP)hdftmp); status = SDendaccess(sds_id); status = SDend(sd_id); for(uint j=0; j<m_width; j++) tmp[i*m_width+j] = hdftmp[j]; } int rawSize = m_rawMap.size()-1; for(uint i=0; i<m_depth*m_width; i++) { int idx = m_rawMap.size()-1; float frc = 0; float v; if (m_voxelType == _UChar) v = ((uchar *)tmp)[i]; else if (m_voxelType == _Char) v = ((char *)tmp)[i]; else if (m_voxelType == _UShort) v = ((ushort *)tmp)[i]; else if (m_voxelType == _Short) v = ((short *)tmp)[i]; else if (m_voxelType == _Int) v = ((int *)tmp)[i]; else if (m_voxelType == _Float) v = ((float *)tmp)[i]; if (v < m_rawMap[0]) { idx = 0; frc = 0; } else if (v > m_rawMap[rawSize]) { idx = rawSize-1; frc = 1; } else { for(uint m=0; m<rawSize; m++) { if (v >= m_rawMap[m] && v <= m_rawMap[m+1]) { idx = m; frc = ((float)v-(float)m_rawMap[m])/ ((float)m_rawMap[m+1]-(float)m_rawMap[m]); } } } uchar pv = m_pvlMap[idx] + frc*(m_pvlMap[idx+1]-m_pvlMap[idx]); m_image[i] = pv; } QImage img = QImage(m_image, m_width, m_depth, m_width, QImage::Format_Indexed8); delete [] tmp; return img; }
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 */
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 */
QPair<QVariant, QVariant> RemapHDF4::rawValue(int d, int w, int h) { QPair<QVariant, QVariant> pair; if (d < 0 || d >= m_depth || w < 0 || w >= m_width || h < 0 || h >= m_height) { pair.first = QVariant("OutOfBounds"); pair.second = QVariant("OutOfBounds"); return pair; } uchar *hdftmp = new uchar[m_bytesPerVoxel]; int32 start[2], edges[2]; start[0] = w; start[1] = h; edges[0] = 1; edges[1] = 1; int32 sd_id = SDstart(m_imageList[d].toAscii().data(), DFACC_READ); int32 sds_id = SDselect(sd_id, m_Index); int status = SDreaddata(sds_id, start, NULL, edges, (VOIDP)hdftmp); if (status == -1) QMessageBox::information(0, "error", "Cannot read"); status = SDendaccess(sds_id); status = SDend(sd_id); QVariant v; if (m_voxelType == _UChar) { uchar *aptr = (uchar*) hdftmp; uchar a = *aptr; v = QVariant((uint)a); } else if (m_voxelType == _Char) { char *aptr = (char*) hdftmp; char a = *aptr; v = QVariant((int)a); } else if (m_voxelType == _UShort) { ushort *aptr = (ushort*) hdftmp; ushort a = *aptr; v = QVariant((uint)a); } else if (m_voxelType == _Short) { short *aptr = (short*) hdftmp; short a = *aptr; v = QVariant((int)a); } else if (m_voxelType == _Int) { int *aptr = (int*) hdftmp; int a = *aptr; v = QVariant((int)a); } else if (m_voxelType == _Float) { float *aptr = (float*) hdftmp; double a = *aptr; v = QVariant((double)a); } int rawSize = m_rawMap.size()-1; int idx = rawSize; float frc = 0; float val; if (v.type() == QVariant::UInt) val = v.toUInt(); else if (v.type() == QVariant::Int) val = v.toInt(); else if (v.type() == QVariant::Double) val = v.toDouble(); if (val <= m_rawMap[0]) { idx = 0; frc = 0; } else if (val >= m_rawMap[rawSize]) { idx = rawSize-1; frc = 1; } else { for(uint m=0; m<rawSize; m++) { if (val >= m_rawMap[m] && val <= m_rawMap[m+1]) { idx = m; frc = ((float)val-(float)m_rawMap[m])/ ((float)m_rawMap[m+1]-(float)m_rawMap[m]); } } } uchar pv = m_pvlMap[idx] + frc*(m_pvlMap[idx+1]-m_pvlMap[idx]); pair.first = v; pair.second = QVariant((uint)pv); return pair; }
void ossimHdfGridModel::setGridNodes(ossimDblGrid& grid, int32 sds_id, const ossimIpt& spacing) { int x=0, y=0; ossim_uint32 index = 0; if (m_isHdf4) { int32 dim_sizes[MAX_VAR_DIMS]; int32 rank, data_type, n_attrs; char name[MAX_NC_NAME]; int32 status = SDgetinfo(sds_id, name, &rank, dim_sizes, &data_type, &n_attrs); if (status == -1) return; int32 origin[2] = {0, 0}; int32 num_rows = dim_sizes[0]; int32 num_cols = dim_sizes[1]; ossimDpt grid_origin(0,0); // The grid as used in base class, has UV-space always at 0,0 origin ossimIpt grid_size (num_cols, num_rows); ossimDpt dspacing (spacing); grid.initialize(grid_size, grid_origin, dspacing); float32* values = new float32 [num_rows * num_cols]; intn statusN = SDreaddata(sds_id, origin, NULL, dim_sizes, (VOIDP)values); if (statusN > -1) { for (y = 0; y < num_rows; y++) { for (x = 0; x < num_cols; x++) { grid.setNode(x, y, values[index++]); } } } delete values; } else { hsize_t dims_out[2]; //dataset dimensions hid_t datatype = H5Dget_type(sds_id); hid_t dataspace = H5Dget_space(sds_id); //dataspace handle int rank = H5Sget_simple_extent_ndims(dataspace); if (rank == 2) { herr_t status_n = H5Sget_simple_extent_dims(dataspace, dims_out, NULL); ossim_int32 latGridRows = dims_out[0]; ossim_int32 lonGridCols = dims_out[1]; ossim_int32 cols = spacing.x; ossim_int32 rows = spacing.y; ossim_int32 spacingX = 0; ossim_int32 spacingY = 0; if (rows % latGridRows == 0 && cols % lonGridCols == 0) { spacingY = rows/latGridRows; //line increment step spacingX = cols/lonGridCols; //pixel increment step } ossimIpt gridSpacing(spacingX, spacingY); ossimDpt grid_origin(0,0); // The grid as used in base class, has UV-space always at 0,0 origin ossimIpt grid_size (cols, rows); ossimDpt dspacing (gridSpacing); grid.initialize(grid_size, grid_origin, dspacing); if( H5Tequal(H5T_NATIVE_FLOAT, datatype)) { ossim_float32* data_out = new ossim_float32[dims_out[0] * dims_out[1]]; herr_t status = H5Dread(sds_id, datatype, dataspace, dataspace, H5P_DEFAULT, data_out); index = 0; for (y = 0; y < rows; y++) { for (x = 0; x < cols; x++) { index = x + y * cols; grid.setNode(x, y, data_out[index]); } } delete[] data_out; } else if( H5Tequal(H5T_NATIVE_DOUBLE, datatype)) { ossim_float64* data_out = new ossim_float64[dims_out[0] * dims_out[1]]; herr_t status = H5Dread(sds_id, datatype, dataspace, dataspace, H5P_DEFAULT, data_out); index = 0; for (y = 0; y < rows; y++) { for (x = 0; x < cols; x++) { index = x + y * cols; grid.setNode(x, y, data_out[index]); } } delete[] data_out; } } H5Tclose(datatype); H5Sclose(dataspace); } }
void RemapHDF4::saveTrimmed(QString trimFile, int dmin, int dmax, int wmin, int wmax, int hmin, int hmax) { QProgressDialog progress("Saving trimmed volume", "Cancel", 0, 100, 0); progress.setMinimumDuration(0); int nX, nY, nZ; nX = m_depth; nY = m_width; nZ = m_height; int mX, mY, mZ; mX = dmax-dmin+1; mY = wmax-wmin+1; mZ = hmax-hmin+1; int nbytes = m_height*m_width*m_bytesPerVoxel; uchar *tmp = new uchar[nbytes]; uchar vt; if (m_voxelType == _UChar) vt = 0; // unsigned byte if (m_voxelType == _Char) vt = 1; // signed byte if (m_voxelType == _UShort) vt = 2; // unsigned short if (m_voxelType == _Short) vt = 3; // signed short if (m_voxelType == _Int) vt = 4; // int if (m_voxelType == _Float) vt = 8; // float QFile fout(trimFile); fout.open(QFile::WriteOnly); fout.write((char*)&vt, 1); fout.write((char*)&mX, 4); fout.write((char*)&mY, 4); fout.write((char*)&mZ, 4); int32 start[2], edges[2]; start[0] = 0; start[1] = 0; edges[0] = m_width; edges[1] = m_height; for(uint i=dmin; i<=dmax; i++) { int32 sd_id = SDstart(m_imageList[i].toAscii().data(), DFACC_READ); int32 sds_id = SDselect(sd_id, m_Index); int status = SDreaddata(sds_id, start, NULL, edges, (VOIDP)tmp); status = SDendaccess(sds_id); status = SDend(sd_id); for(uint j=wmin; j<=wmax; j++) { memcpy(tmp+(j-wmin)*mZ*m_bytesPerVoxel, tmp+(j*nZ + hmin)*m_bytesPerVoxel, mZ*m_bytesPerVoxel); } fout.write((char*)tmp, mY*mZ*m_bytesPerVoxel); progress.setValue((int)(100*(float)(i-dmin)/(float)mX)); qApp->processEvents(); } fout.close(); delete [] tmp; m_headerBytes = 13; // to be used for applyMapping function }
/******************************************************************** 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 int read_array(const coda_cursor *cursor, void *dst) { int32 start[MAX_HDF4_VAR_DIMS]; int32 stride[MAX_HDF4_VAR_DIMS]; int32 edge[MAX_HDF4_VAR_DIMS]; long num_elements; long i; if (coda_hdf4_cursor_get_num_elements(cursor, &num_elements) != 0) { return -1; } if (num_elements <= 0) { /* no data to be read */ return 0; } switch (((coda_hdf4_type *)cursor->stack[cursor->n - 1].type)->tag) { case tag_hdf4_basic_type_array: if (read_attribute(cursor, dst, -1) != 0) { return -1; } break; case tag_hdf4_GRImage: { coda_hdf4_GRImage *type; type = (coda_hdf4_GRImage *)cursor->stack[cursor->n - 1].type; start[0] = 0; start[1] = 0; stride[0] = 1; stride[1] = 1; edge[0] = type->dim_sizes[0]; edge[1] = type->dim_sizes[1]; if (GRreadimage(type->ri_id, start, stride, edge, dst) != 0) { coda_set_error(CODA_ERROR_HDF4, NULL); return -1; } } break; case tag_hdf4_SDS: { coda_hdf4_SDS *type; type = (coda_hdf4_SDS *)cursor->stack[cursor->n - 1].type; if (type->rank == 0) { start[0] = 0; edge[0] = 1; } else { for (i = 0; i < type->rank; i++) { start[i] = 0; edge[i] = type->dimsizes[i]; } } if (SDreaddata(type->sds_id, start, NULL, edge, dst) != 0) { coda_set_error(CODA_ERROR_HDF4, NULL); return -1; } } break; case tag_hdf4_Vdata_field: { coda_hdf4_Vdata *type; coda_hdf4_Vdata_field *field_type; assert(cursor->n > 1); type = (coda_hdf4_Vdata *)cursor->stack[cursor->n - 2].type; field_type = (coda_hdf4_Vdata_field *)cursor->stack[cursor->n - 1].type; if (VSseek(type->vdata_id, 0) < 0) { coda_set_error(CODA_ERROR_HDF4, NULL); return -1; } if (VSsetfields(type->vdata_id, field_type->field_name) != 0) { coda_set_error(CODA_ERROR_HDF4, NULL); return -1; } if (VSread(type->vdata_id, (uint8 *)dst, field_type->num_records, FULL_INTERLACE) < 0) { coda_set_error(CODA_ERROR_HDF4, NULL); return -1; } } break; default: assert(0); exit(1); } return 0; }
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 int read_partial_array(const coda_cursor *cursor, long offset, long length, void *dst) { int32 start[MAX_HDF4_VAR_DIMS]; int32 stride[MAX_HDF4_VAR_DIMS]; int32 edge[MAX_HDF4_VAR_DIMS]; long i; switch (((coda_hdf4_type *)cursor->stack[cursor->n - 1].type)->tag) { case tag_hdf4_basic_type_array: coda_set_error(CODA_ERROR_INVALID_ARGUMENT, "partial array reading is not supported for HDF4 attributes"); return -1; case tag_hdf4_GRImage: { coda_hdf4_GRImage *type; type = (coda_hdf4_GRImage *)cursor->stack[cursor->n - 1].type; stride[0] = 1; stride[1] = 1; if (length < type->dim_sizes[1]) { start[0] = offset / type->dim_sizes[1]; start[1] = offset % type->dim_sizes[1]; edge[0] = 1; edge[1] = length; if (start[1] + edge[1] > type->dim_sizes[1]) { coda_set_error(CODA_ERROR_INVALID_ARGUMENT, "partial array reading for HDF4 GRImage requires " "offset (%ld) and length (%ld) to represent a hyperslab (range [%ld,%ld] " "exceeds length of dimension #1 (%ld)))", offset, length, (long)start[1], (long)start[1] + edge[1] - 1, type->dim_sizes[1]); return -1; } } else { if (length % type->dim_sizes[1] != 0) { coda_set_error(CODA_ERROR_INVALID_ARGUMENT, "partial array reading for HDF4 GRImage requires " "length (%ld) to be a multiple of the subdimension size (%ld)", length, type->dim_sizes[1]); return -1; } if (offset % type->dim_sizes[1] != 0) { coda_set_error(CODA_ERROR_INVALID_ARGUMENT, "partial array reading for HDF4 GRImage requires " " offset (%ld) to be a multiple of the subdimension size (%ld)", offset, type->dim_sizes[1]); return -1; } start[0] = offset / type->dim_sizes[1]; start[1] = 0; edge[0] = length / type->dim_sizes[1]; edge[1] = type->dim_sizes[1]; if (start[0] + edge[0] > type->dim_sizes[0]) { coda_set_error(CODA_ERROR_INVALID_ARGUMENT, "partial array reading for HDF4 GRImage requires " "offset (%ld) and length (%ld) to represent a hyperslab (range [%ld,%ld] " "exceeds length of dimension #0 (%ld)))", offset, length, (long)start[0], (long)start[0] + edge[0] - 1, type->dim_sizes[0]); return -1; } } if (GRreadimage(type->ri_id, start, stride, edge, dst) != 0) { coda_set_error(CODA_ERROR_HDF4, NULL); return -1; } } break; case tag_hdf4_SDS: { coda_hdf4_SDS *type; long block_size = 1; type = (coda_hdf4_SDS *)cursor->stack[cursor->n - 1].type; /* determine hyperslab start/edge */ if (type->rank == 0) { coda_set_error(CODA_ERROR_INVALID_ARGUMENT, "partial array reading not allowed for zero " "dimensional HDF4 SDS"); return -1; } for (i = type->rank - 1; i >= 0; i--) { if (length <= block_size * type->dimsizes[i]) { if (length % block_size != 0) { coda_set_error(CODA_ERROR_INVALID_ARGUMENT, "partial array reading for HDF4 SDS requires " "length (%ld) to be a multiple of the subdimension size (%ld)", length, block_size); return -1; } start[i] = (offset / block_size) % type->dimsizes[i]; edge[i] = length / block_size; break; } start[i] = 0; edge[i] = type->dimsizes[i]; block_size *= type->dimsizes[i]; } if (offset % block_size != 0) { coda_set_error(CODA_ERROR_INVALID_ARGUMENT, "partial array reading for HDF4 SDS requires offset " "(%ld) to be a multiple of the subdimension size (%ld)", offset, block_size); return -1; } if (start[i] + edge[i] > type->dimsizes[i]) { coda_set_error(CODA_ERROR_INVALID_ARGUMENT, "partial array reading for HDF4 SDS requires offset " "(%ld) and length (%ld) to represent a hyperslab (range [%ld,%ld] exceeds length " "of dimension #%d (%ld)))", offset, length, (long)start[i], (long)start[i] + edge[i] - 1, i, type->dimsizes[i]); return -1; } while (i > 0) { block_size *= type->dimsizes[i]; i--; start[i] = (offset / block_size) % type->dimsizes[i]; edge[i] = 1; } if (SDreaddata(type->sds_id, start, NULL, edge, dst) != 0) { coda_set_error(CODA_ERROR_HDF4, NULL); return -1; } } break; case tag_hdf4_Vdata_field: coda_set_error(CODA_ERROR_INVALID_ARGUMENT, "partial array reading is not supported for HDF4 Vdata"); return -1; default: assert(0); exit(1); } return 0; }
static int read_basic_type(const coda_cursor *cursor, void *dst) { int32 start[MAX_HDF4_VAR_DIMS]; int32 stride[MAX_HDF4_VAR_DIMS]; int32 edge[MAX_HDF4_VAR_DIMS]; long index; long i; index = cursor->stack[cursor->n - 1].index; assert(cursor->n > 1); switch (((coda_hdf4_type *)cursor->stack[cursor->n - 2].type)->tag) { case tag_hdf4_basic_type_array: { coda_cursor array_cursor; char *buffer; int native_type_size; long num_elements; /* we first read the whole array and then return only the requested element */ array_cursor = *cursor; array_cursor.n--; if (coda_cursor_get_num_elements(&array_cursor, &num_elements) != 0) { return -1; } assert(index < num_elements); native_type_size = get_native_type_size(((coda_hdf4_type *)cursor->stack[cursor->n - 1].type)->definition->read_type); buffer = malloc(num_elements * native_type_size); if (buffer == NULL) { coda_set_error(CODA_ERROR_OUT_OF_MEMORY, "out of memory (could not allocate %lu bytes) (%s:%u)", (long)(num_elements * native_type_size), __FILE__, __LINE__); return -1; } if (read_attribute(&array_cursor, buffer, -1) != 0) { free(buffer); return -1; } memcpy(dst, &buffer[index * native_type_size], native_type_size); free(buffer); } break; case tag_hdf4_attributes: case tag_hdf4_file_attributes: if (read_attribute(cursor, dst, -1) != 0) { return -1; } break; case tag_hdf4_GRImage: { coda_hdf4_GRImage *type; stride[0] = 1; stride[1] = 1; edge[0] = 1; edge[1] = 1; type = (coda_hdf4_GRImage *)cursor->stack[cursor->n - 2].type; if (type->ncomp != 1) { uint8 *buffer; int component_size; int component_index; component_size = get_native_type_size(type->basic_type->definition->read_type); /* HDF4 does not allow reading a single component of a GRImage, so we have to first read all * components and then return only the data item that was requested */ buffer = malloc(component_size * type->ncomp); if (buffer == NULL) { coda_set_error(CODA_ERROR_OUT_OF_MEMORY, "out of memory (could not allocate %lu bytes) (%s:%u)", (long)(component_size * type->ncomp), __FILE__, __LINE__); return -1; } component_index = index % type->ncomp; index /= type->ncomp; /* For GRImage data the first dimension is the fastest running */ start[0] = index % type->dim_sizes[0]; start[1] = index / type->dim_sizes[0]; if (GRreadimage(type->ri_id, start, stride, edge, buffer) != 0) { coda_set_error(CODA_ERROR_HDF4, NULL); free(buffer); return -1; } memcpy(dst, &buffer[component_index * component_size], component_size); free(buffer); } else { /* For GRImage data the first dimension is the fastest running */ start[0] = index % type->dim_sizes[0]; start[1] = index / type->dim_sizes[0]; if (GRreadimage(type->ri_id, start, stride, edge, dst) != 0) { coda_set_error(CODA_ERROR_HDF4, NULL); return -1; } } } break; case tag_hdf4_SDS: { coda_hdf4_SDS *type; type = (coda_hdf4_SDS *)cursor->stack[cursor->n - 2].type; if (type->rank == 0) { start[0] = 0; edge[1] = 1; } else { for (i = type->rank - 1; i >= 0; i--) { start[i] = index % type->dimsizes[i]; index /= type->dimsizes[i]; edge[i] = 1; } } if (SDreaddata(type->sds_id, start, NULL, edge, dst) != 0) { coda_set_error(CODA_ERROR_HDF4, NULL); return -1; } } break; case tag_hdf4_Vdata_field: { coda_hdf4_Vdata *type; coda_hdf4_Vdata_field *field_type; int record_pos; int order_pos; assert(cursor->n > 2); type = (coda_hdf4_Vdata *)cursor->stack[cursor->n - 3].type; field_type = (coda_hdf4_Vdata_field *)cursor->stack[cursor->n - 2].type; order_pos = index % field_type->order; record_pos = index / field_type->order; if (VSseek(type->vdata_id, record_pos) < 0) { coda_set_error(CODA_ERROR_HDF4, NULL); return -1; } if (VSsetfields(type->vdata_id, field_type->field_name) != 0) { coda_set_error(CODA_ERROR_HDF4, NULL); return -1; } if (field_type->order > 1) { /* HDF4 does not allow reading part of a vdata field, so we have to first read the full field and * then return only the data item that was requested */ uint8 *buffer; int element_size; int size; size = VSsizeof(type->vdata_id, field_type->field_name); if (size < 0) { coda_set_error(CODA_ERROR_HDF4, NULL); return -1; } buffer = malloc(size); if (buffer == NULL) { coda_set_error(CODA_ERROR_OUT_OF_MEMORY, "out of memory (could not allocate %lu bytes) (%s:%u)", (long)size, __FILE__, __LINE__); return -1; } if (VSread(type->vdata_id, buffer, 1, FULL_INTERLACE) < 0) { coda_set_error(CODA_ERROR_HDF4, NULL); free(buffer); return -1; } /* the size of a field element is the field size divided by the order of the field */ element_size = size / field_type->order; memcpy(dst, &buffer[order_pos * element_size], element_size); free(buffer); } else { if (VSread(type->vdata_id, (uint8 *)dst, 1, FULL_INTERLACE) < 0) { coda_set_error(CODA_ERROR_HDF4, NULL); return -1; } } } break; default: assert(0); exit(1); } return 0; }
Databox *ReadSDS(char *filename, int ds_num, double default_value) { Databox *v; int32 dim[MAX_VAR_DIMS]; int32 edges[3]; int32 start[3]; int i; int z; int32 type; char name[MAX_NC_NAME]; int32 sd_id; int32 sds_id; int32 rank, nt, nattrs; int nx, ny, nz; int m; double *double_ptr; sd_id = SDstart(filename, DFACC_RDONLY); sds_id = SDselect(sd_id, ds_num); SDgetinfo(sds_id, name, &rank, dim, &type, &nattrs); start[0] = start[1] = start[2] = 0; /* create the new databox structure */ if((v = NewDatabox(dim[2], dim[1], dim[0], 0, 0, 0, 0, 0, 0, default_value)) == NULL) return((Databox *)NULL); double_ptr = DataboxCoeffs(v); edges[0] = 1; edges[1] = DataboxNy(v); edges[2] = DataboxNx(v); switch (type) { case DFNT_FLOAT32 : { float32 *convert_ptr, *data; if( (data = convert_ptr = (float32 *)malloc(dim[1]*dim[2] * sizeof(float32))) == NULL) { exit(1); } for(z=0; z < dim[0]; z++) { start[0] = z; SDreaddata(sds_id, start, NULL, edges, data); convert_ptr = data; for(i=dim[1]*dim[2]; i--;) *double_ptr++ = *convert_ptr++; } free(data); break; }; case DFNT_FLOAT64 : { float64 *convert_ptr, *data; if( (data = convert_ptr = (float64 *)malloc(dim[1]*dim[2] * sizeof(float64))) == NULL) { exit(1); } for(z=0; z < dim[0]; z++) { start[0] = z; SDreaddata(sds_id, start, NULL, edges, data); convert_ptr = data; for(i=dim[1]*dim[2]; i--;) *double_ptr++ = *convert_ptr++; } free(data); break; }; case DFNT_INT8 : { int8 *convert_ptr, *data; if( (data = convert_ptr = (int8 *)malloc(dim[1]*dim[2] * sizeof(int8))) == NULL) { exit(1); } for(z=0; z < dim[0]; z++) { start[0] = z; SDreaddata(sds_id, start, NULL, edges, data); convert_ptr = data; for(i=dim[1]*dim[2]; i--;) *double_ptr++ = *convert_ptr++; } free(data); break; }; case DFNT_UINT8 : { uint8 *convert_ptr, *data; if( (data = convert_ptr = (uint8 *)malloc(dim[1]*dim[2] * sizeof(uint8))) == NULL) { exit(1); } for(z=0; z < dim[0]; z++) { start[0] = z; SDreaddata(sds_id, start, NULL, edges, data); convert_ptr = data; for(i=dim[1]*dim[2]; i--;) *double_ptr++ = *convert_ptr++; } free(data); break; }; case DFNT_INT16 : { int16 *convert_ptr, *data; if( (data = convert_ptr = (int16 *)malloc(dim[1]*dim[2] * sizeof(int16))) == NULL) { exit(1); } for(z=0; z < dim[0]; z++) { start[0] = z; SDreaddata(sds_id, start, NULL, edges, data); convert_ptr = data; for(i=dim[1]*dim[2]; i--;) *double_ptr++ = *convert_ptr++; } free(data); break; }; case DFNT_UINT16 : { uint16 *convert_ptr, *data; if( (data = convert_ptr = (uint16 *)malloc(dim[1]*dim[2] * sizeof(uint16))) == NULL) { exit(1); } for(z=0; z < dim[0]; z++) { start[0] = z; SDreaddata(sds_id, start, NULL, edges, data); convert_ptr = data; for(i=dim[1]*dim[2]; i--;) *double_ptr++ = *convert_ptr++; } free(data); break; }; case DFNT_INT32 : { int32 *convert_ptr, *data; if( (data = convert_ptr = (int32 *)malloc(dim[1]*dim[2] * sizeof(int32))) == NULL) { exit(1); } for(z=0; z < dim[0]; z++) { start[0] = z; SDreaddata(sds_id, start, NULL, edges, data); convert_ptr = data; for(i=dim[1]*dim[2]; i--;) *double_ptr++ = *convert_ptr++; } free(data); break; }; case DFNT_UINT32 : { uint32 *convert_ptr, *data; if( (data = convert_ptr = (uint32 *)malloc(dim[1]*dim[2] * sizeof(uint32))) == NULL) { exit(1); } for(z=0; z < dim[0]; z++) { start[0] = z; SDreaddata(sds_id, start, NULL, edges, data); convert_ptr = data; for(i=dim[1]*dim[2]; i--;) *double_ptr++ = *convert_ptr++; } free(data); break; }; } SDendaccess(sds_id); SDend(sd_id); return v; }
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; }
void RemapHDF4::findMinMaxandGenerateHistogram() { QProgressDialog progress("Generating Histogram", "Cancel", 0, 100, 0); progress.setMinimumDuration(0); float rSize; float rMin; m_histogram.clear(); if (m_voxelType == _UChar || m_voxelType == _Char) { if (m_voxelType == _UChar) rMin = 0; if (m_voxelType == _Char) rMin = -127; rSize = 255; for(uint i=0; i<256; i++) m_histogram.append(0); } else if (m_voxelType == _UShort || m_voxelType == _Short) { if (m_voxelType == _UShort) rMin = 0; if (m_voxelType == _Short) rMin = -32767; rSize = 65536; for(uint i=0; i<65536; i++) m_histogram.append(0); } else { QMessageBox::information(0, "Error", "Why am i here ???"); return; } int nX, nY, nZ; nX = m_depth; nY = m_width; nZ = m_height; int nbytes = nY*nZ*m_bytesPerVoxel; uchar *tmp = new uchar[nbytes]; int32 start[2], edges[2]; start[0] = start[1] = 0; edges[0] = m_width; edges[1] = m_height; for(uint i=0; i<m_depth; i++) { progress.setValue((int)(100.0*(float)i/(float)m_depth)); qApp->processEvents(); int32 sd_id = SDstart(m_imageList[i].toAscii().data(), DFACC_READ); int32 sds_id = SDselect(sd_id, m_Index); int status = SDreaddata(sds_id, start, NULL, edges, (VOIDP)tmp); status = SDendaccess(sds_id); status = SDend(sd_id); if (m_voxelType == _UChar) { uchar *ptr = tmp; MINMAXANDHISTOGRAM(); } else if (m_voxelType == _Char) { char *ptr = (char*) tmp; MINMAXANDHISTOGRAM(); } if (m_voxelType == _UShort) { ushort *ptr = (ushort*) tmp; MINMAXANDHISTOGRAM(); } else if (m_voxelType == _Short) { short *ptr = (short*) tmp; MINMAXANDHISTOGRAM(); } else if (m_voxelType == _Int) { int *ptr = (int*) tmp; MINMAXANDHISTOGRAM(); } else if (m_voxelType == _Float) { float *ptr = (float*) tmp; MINMAXANDHISTOGRAM(); } } delete [] tmp; while(m_histogram.last() == 0) m_histogram.removeLast(); while(m_histogram.first() == 0) m_histogram.removeFirst(); progress.setValue(100); qApp->processEvents(); }