/****************************************************************************** !Description: 'PutOutputQALine' writes a line of QA data to the output file. !Input Parameters: ds_output 'output' data structure; the following fields are written: qa_buf -- contains the line to be written iband current band to be written (0-based) iline current line to be written (0-based) !Output Parameters: ds_output 'output' data structure; the following fields are modified: (returns) status: 'true' = okay 'false' = error return !Team Unique Header: !Design Notes: *****************************************************************************/ bool PutOutputQALine(Output_t *ds_output, int iband, int iline) { int32 start[MYHDF_MAX_RANK], nval[MYHDF_MAX_RANK]; void *buf = NULL; /* Check the parameters */ if (ds_output == (Output_t *)NULL) RETURN_ERROR("invalid input structure", "PutOutputQALine", false); if (!ds_output->open) RETURN_ERROR("file not open", "PutOutputQALine", false); if (iband < 0 || iband >= ds_output->nqa_band) RETURN_ERROR("invalid band number", "PutOutputQALine", false); if (iline < 0 || iline >= ds_output->size.l) RETURN_ERROR("invalid line number", "PutOutputQALine", false); /* Write the data */ start[0] = iline; start[1] = 0; nval[0] = 1; nval[1] = ds_output->size.s; buf = (void *)ds_output->qa_buf[iband]; if (SDwritedata(ds_output->qa_sds[iband].id, start, NULL, nval, buf) == HDF_ERROR) RETURN_ERROR("writing output", "PutOutputQALine", false); return true; }
/* Create a HDF4 file with one dataset of type dtype. */ int create_hdf_file(int dtype) { int32 sd_id, sds_id; int32 start[2] = {0, 0}, edges[2] = {DIM1, DIM0}; int16 array_data[DIM0][DIM1]; int32 array_data_int32[DIM0][DIM1]; float32 array_data_float32[DIM0][DIM1]; float64 array_data_float64[DIM0][DIM1]; void *data; intn i, j, count; /* Populate data arrays. */ count = 0; for (j = 0; j < DIM0; j++) { for (i = 0; i < DIM1; i++) { array_data[j][i] = count; array_data_int32[j][i] = count; array_data_float32[j][i] = count; array_data_float64[j][i] = count++; } } /* Point to the correct data. */ switch(dtype) { case DFNT_LINT8: case DFNT_LUINT8: case DFNT_LINT16: case DFNT_LUINT16: data = array_data; break; case DFNT_LINT32: case DFNT_LUINT32: data = array_data_int32; break; case DFNT_LFLOAT32: data = array_data_float32; break; case DFNT_LFLOAT64: data = array_data_float64; break; default: return -1; } if ((sd_id = SDstart(FILENAME, DFACC_CREATE)) == -1) ERR; if ((sds_id = SDcreate(sd_id, SDSNAME, dtype, RANK, edges)) == -1) ERR; if (SDwritedata(sds_id, start, NULL, edges, data)) ERR; if (SDend(sd_id)) ERR; return 0; }
/* Test non-special SDSs. This routine creates non-special SDSs, writes * data to one of the SDSs, and checks the sizes returned by SDgetdatasize */ static intn test_nonspecial_SDSs(int32 fid) { int32 sds_id; int32 dimsize[2], start[2], edges[2]; int32 data[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] = Y_LENGTH; dimsize[1] = X_LENGTH; sds_id = SDcreate(fid, "EmptyDataset", DFNT_FLOAT32, 2, dimsize); CHECK(sds_id, FAIL, "test_nonspecial_SDSs: SDcreate 'EmptyDataset'"); /* Close this SDS */ status = SDendaccess(sds_id); CHECK(status, FAIL, "test_nonspecial_SDSs: SDendaccess"); /* Check that this SDS is empty */ check_datasizes(fid, "EmptyDataset", 0, 0, &num_errs); /* Create another 2x2 dataset called "WrittenDataset" */ sds_id = SDcreate(fid, "WrittenDataset", DFNT_INT32, 2, dimsize); CHECK(sds_id, FAIL, "test_nonspecial_SDSs: SDcreate 'WrittenDataset'"); /* Define the location and size of the data to be written to the dataset */ start[0] = 0; start[1] = 0; edges[0] = Y_LENGTH; edges[1] = X_LENGTH; /* Write the stored data to the dataset */ status = SDwritedata(sds_id, start, NULL, edges, (VOIDP)data); CHECK(sds_id, FAIL, "test_nonspecial_SDSs: SDwritedata"); /* Close this SDS */ status = SDendaccess(sds_id); CHECK(status, FAIL, "test_nonspecial_SDSs: SDendaccess"); /* Check the size of the data of this SDS */ check_datasizes(fid, "WrittenDataset", Y_LENGTH*X_LENGTH*SIZE_INT32, Y_LENGTH*X_LENGTH*SIZE_INT32, &num_errs); /* Return the number of errors that's been kept track of so far */ return num_errs; } /* test_nonspecial_SDSs */
/* Write current scan line for all processed bands. Returns 0 if no errors, non-zero otherwise. */ int write_scan(int iscan, unsigned char *process, SDS outsds[Nbands]) { int ib; for (ib = 0; ib < Nbands; ib++) { if (!process[ib]) continue; outsds[ib].start[0] = iscan * outsds[ib].rowsperscan; if (SDwritedata(outsds[ib].id, outsds[ib].start, NULL, outsds[ib].edges, outsds[ib].data) == -1) return 1; } return 0; }
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; }
PetscErrorCode PetscViewerHDF5WriteSDS(PetscViewer viewer, float *xf, int d, int *dims,int bs) { int i; PetscViewer_HDF5 *vhdf5 = (PetscViewer_HDF5*)viewer->data; int32 sds_id,zero32[3],dims32[3]; PetscFunctionBegin; for (i = 0; i < d; i++) { zero32[i] = 0; dims32[i] = dims[i]; } sds_id = SDcreate(vhdf5->sd_id, "Vec", DFNT_FLOAT32, d, dims32); if (sds_id < 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"SDcreate failed"); SDwritedata(sds_id, zero32, 0, dims32, xf); SDendaccess(sds_id); PetscFunctionReturn(0); }
intn write_sds(char *fname) { int32 sd_id, sds_id; int32 dim_sizes[2] = {4,8}; int32 start[2], edges[2]; float32 data[4][8]; int i, j; 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; } sds_id = SDcreate (sd_id, "temperature", DFNT_FLOAT32, 2, dim_sizes); /* * Data set data initialization. */ for (j = 0; j < 4; j++) { for (i = 0; i < 8; i++) data[j][i] = (i + j) + 1; } /* * Define the location and size of the data to be written to the data set. */ start[0] = 0; start[1] = 0; edges[0] = 4; edges[1] = 8; status = SDwritedata (sds_id, start, NULL, edges, (VOIDP)data); status = SDendaccess(sds_id); status = SDend(sd_id); return 0; }
int main(int argc,char **argv) { int32 sdsout_id; int32 dimout_id; int32 sdout_id; int32 index,count,start[MAX_VAR_DIMS]; int32 edge[MAX_VAR_DIMS]; int32 rank,data_type; int32 dim_sizes[MAX_VAR_DIMS]; char name[MAX_NC_NAME]; char names[2][MAX_NC_NAME]; int write_metadata; float32 scalef=1.0; float32 addoff=0.0; char* oz_units= "Dobson"; int16 doy; int idoy,year,nlats,nlons; float minlat, minlon, maxlat, maxlon, latsteps,lonsteps; short int* data; int16 base_date[3]; float lat_array[1024], lon_array[1024]; char* platform= argc>3 ? argv[3] : (char*) "Earthprobe"; if (argc<3) { fprintf(stderr,"usage: %s <input> <output> <platform>\n",argv[0]); exit(-1); } verbose=0; /**** open input ****/ /* determine NLINE by sensor - Feng */ if(strstr(argv[1], "omi")) { NLINE = 15; longline = 25; shortline = 10; } else { NLINE =12; longline =25; shortline = 13; } printf("%d %d %d\n", NLINE, longline, shortline); read_ozone(argv[1], &data, &idoy, &year, &nlats,&nlons, &minlat, &minlon, &maxlat, &maxlon, &latsteps, &lonsteps, lat_array, lon_array); if ( nlats!=180 || fabs(minlat+ 89.500)>0.0001 || fabs(maxlat- 89.500)>0.0001 || nlons!=288 || fabs(minlon+179.375)>0.0001 || fabs(maxlon-179.375)>0.0001 || fabs(latsteps-1.0)>0.0001 || fabs(lonsteps-1.25)>0.0001 )printf( "*** unexpected values ***\n nlats=%d nlons=%d minlat=%f maxlat=%f minlon=%f maxlon=%f\n" ,nlats,nlons,minlat,maxlat,minlon,maxlon); /**** check and open output ****/ write_metadata=0; if ((sdout_id=SDstart(argv[2], DFACC_RDONLY))<0) { if ((sdout_id=SDstart(argv[2], DFACC_CREATE))<0) { fprintf(stderr,"can't create output %s\n",argv[2]); exit(-1); } write_metadata=1; } else { SDend(sdout_id); if ((sdout_id=SDstart(argv[2], DFACC_WRITE))<0) { fprintf(stderr,"can't open output %s\n",argv[2]); exit(-1); } } doy=(int16)idoy; /**** Determine the contents of the file ****/ if (write_metadata) { /**** Copy global attributes to output if creating a new file Write Day Of Year to output ****/ base_date[0]= year; base_date[1]=1; base_date[2]=1; SDsetattr(sdout_id, "base_date", DFNT_INT16,3,base_date); SDsetattr(sdout_id, "Day Of Year", DFNT_INT16,1,&doy); count=strlen(platform)+1; printf("*** platform=(%s) len=%d ***\n",platform,count); SDsetattr(sdout_id,"Platform",DFNT_CHAR8,count,(void*)platform); /**** Copy lat/lon SDS ****/ /************************/ /**** Latitude (lat) ****/ /************************/ dim_sizes[0]= nlats; strcpy(name,"lat"); strcpy(names[0],"lat"); rank=1; if ((sdsout_id=SDcreate(sdout_id,name,DFNT_FLOAT32,rank,&dim_sizes[0]))<0) return -1; start[0]=0; edge[0]=dim_sizes[0]; if (SDwritedata(sdsout_id,start,NULL,edge,lat_array)<0) return -1; dimout_id=SDgetdimid(sdsout_id, 0); SDsetdimname(dimout_id, name); /*************************/ /**** Longitude (lon) ****/ /*************************/ dim_sizes[0]= nlons; strcpy(name,"lon"); strcpy(names[1],"lon"); rank=1; if ((sdsout_id=SDcreate(sdout_id,name,DFNT_FLOAT32,rank,&dim_sizes[0]))<0) return -1; start[0]=0; edge[0]=dim_sizes[0]; if (SDwritedata(sdsout_id,start,NULL,edge,lon_array)<0) return -1; dimout_id=SDgetdimid(sdsout_id, 0); SDsetdimname(dimout_id, name); /************************/ /**** Ozone (ozone) ****/ /************************/ dim_sizes[0]= nlats; dim_sizes[1]= nlons; strcpy(name,"ozone"); rank=2; if ((sdsout_id=SDcreate(sdout_id,name,DFNT_INT16,rank,&dim_sizes[0]))<0) return -1; start[0]=0; start[1]=0; edge[0]=dim_sizes[0]; edge[1]=dim_sizes[1]; if (SDwritedata(sdsout_id,start,NULL,edge,data)<0) return -1; for (index=0;index<rank;index++) { dimout_id=SDgetdimid(sdsout_id, index); SDsetdimname(dimout_id, names[index]); } strcpy(name,"scale_factor"); count=1; data_type= DFNT_FLOAT32; SDsetattr(sdsout_id, name, data_type, count, (void*)&scalef); strcpy(name,"add_offset"); count=1; data_type= DFNT_FLOAT32; SDsetattr(sdsout_id, name, data_type, count, (void*)&addoff); strcpy(name,"units"); count=strlen(oz_units)+1; data_type= DFNT_CHAR8; SDsetattr(sdsout_id, name, data_type, count, (void*)oz_units); } /**** Close input & output ****/ SDend(sdout_id); 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 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 */
int main() { /************************* Variable declaration **************************/ int32 sd_id, sds_id; intn status; int32 comp_type; /* Compression flag */ comp_info c_info; /* Compression structure */ int32 start[2], edges[2], dim_sizes[2]; int32 data[Y_LENGTH][X_LENGTH]; int i, j; /********************* End of variable declaration ***********************/ /* * Buffer array data and define array dimensions. */ for (j = 0; j < Y_LENGTH; j++) { for (i = 0; i < X_LENGTH; i++) data[j][i] = (i + j) + 1; } dim_sizes[0] = Y_LENGTH; dim_sizes[1] = X_LENGTH; /* * Create the file and initialize the SD interface. */ sd_id = SDstart (FILE_NAME, DFACC_CREATE); /* * Create the data set with the name defined in SDS_NAME. */ sds_id = SDcreate (sd_id, SDS_NAME, DFNT_INT32, RANK, dim_sizes); /* * Ininitialize compression structure element and compression * flag for GZIP compression and call SDsetcompress. * * To use the Skipping Huffman compression method, initialize * comp_type = COMP_CODE_SKPHUFF * c_info.skphuff.skp_size = value * * To use the RLE compression method, initialize * comp_type = COMP_CODE_RLE * No structure element needs to be initialized. */ comp_type = COMP_CODE_DEFLATE; c_info.deflate.level = 6; status = SDsetcompress (sds_id, comp_type, &c_info); /* * Define the location and size of the data set * to be written to the file. */ start[0] = 0; start[1] = 0; edges[0] = Y_LENGTH; edges[1] = X_LENGTH; /* * Write the stored data to the data set. The last argument * must be explicitly cast to a generic pointer since SDwritedata * is designed to write generic data. */ status = SDwritedata (sds_id, start, NULL, edges, (VOIDP)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; }
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 */
/* Test compressed SDSs. This routine creates "Compressed-No-Data" and creates * and writes to "CompressedData" and "Non-CompressedData" SDSs. It will then * check the sizes returned from SDgetdatasize calls. */ static intn test_compressed_SDSs(int32 fid) { int32 sds_id, esds_id, usds_id; int32 start[2], edges[2], dim_sizes[2]; comp_coder_t comp_type; /* Compression flag */ comp_info c_info; /* Compression structure */ int32 data[Y_LENGTH][X_LENGTH]; intn status; int i, j; int num_errs = 0; /* number of errors so far */ /* Buffer array data and define array dimensions */ for (j = 0; j < Y_LENGTH; j++) { for (i = 0; i < X_LENGTH; i++) data[j][i] = (i + j) + 1; } dim_sizes[0] = Y_LENGTH; dim_sizes[1] = X_LENGTH; /* Create datasets, one to be written with data, the other not */ sds_id = SDcreate(fid, "CompressedData", DFNT_INT32, RANK, dim_sizes); CHECK(sds_id, FAIL, "test_compressed_SDSs: SDcreate 'CompressedData'"); esds_id = SDcreate(fid, "Compressed-No-Data", DFNT_INT32, RANK, dim_sizes); CHECK(esds_id, FAIL, "test_compressed_SDSs: SDcreate 'Compressed-No-Data'"); usds_id = SDcreate(fid, "Non-CompressedData", DFNT_INT32, RANK, dim_sizes); CHECK(usds_id, FAIL, "test_compressed_SDSs: SDcreate 'Non-CompressedData'"); comp_type = COMP_CODE_DEFLATE; c_info.deflate.level = 6; status = SDsetcompress(sds_id, comp_type, &c_info); CHECK(status, FAIL, "test_compressed_SDSs: SDsetcompress 'CompressedData'"); status = SDsetcompress(esds_id, comp_type, &c_info); CHECK(status, FAIL, "test_compressed_SDSs: SDsetcompress 'Compressed-No-Data'"); /* Define the location and size of the dataset to be written to the file */ start[0] = 0; start[1] = 0; edges[0] = Y_LENGTH; edges[1] = X_LENGTH; /* Write the stored data to the compressed dataset */ status = SDwritedata(sds_id, start, NULL, edges, (VOIDP)data); CHECK(status, FAIL, "test_compressed_SDSs: SDwritedata"); /* Write the stored data to the dataset non-compressed dataset */ status = SDwritedata(usds_id, start, NULL, edges, (VOIDP)data); CHECK(status, FAIL, "test_compressed_SDSs: SDwritedata"); /* Close the SDSs */ status = SDendaccess(sds_id); CHECK(status, FAIL, "test_compressed_SDSs: SDendaccess 'CompressedData'"); status = SDendaccess(esds_id); CHECK(status, FAIL, "test_compressed_SDSs: SDendaccess 'Compressed-No-Data'"); status = SDendaccess(usds_id); CHECK(status, FAIL, "test_compressed_SDSs: SDendaccess 'Non-CompressedData'"); /* Check the size of the data of this SDS - 59 is from examining previously */ check_datasizes(fid, "CompressedData", 59, Y_LENGTH*X_LENGTH*SIZE_INT32, &num_errs); /* Check the size of the data of this SDS */ check_datasizes(fid, "Compressed-No-Data", 0, 0, &num_errs); /* Check the size of the data of this SDS */ check_datasizes(fid, "Non-CompressedData", Y_LENGTH*X_LENGTH*SIZE_INT32, Y_LENGTH*X_LENGTH*SIZE_INT32, &num_errs); /* Return the number of errors that's been kept track of so far */ return num_errs; } /* test_compressed_SDSs */
int main(int argc, char **argv) { printf("\n*** Testing HDF4/NetCDF-4 interoperability...\n"); printf("*** testing that netCDF can read a HDF4 file with some ints..."); { #define PRES_NAME "pres" #define LAT_LEN 3 #define LON_LEN 2 #define DIMS_2 2 int32 sd_id, sds_id; int32 dim_size[DIMS_2] = {LAT_LEN, LON_LEN}; int32 start[DIMS_2] = {0, 0}, edge[DIMS_2] = {LAT_LEN, LON_LEN}; int ncid, nvars_in, ndims_in, natts_in, unlimdim_in; size_t len_in; int data_out[LAT_LEN][LON_LEN], data_in[LAT_LEN][LON_LEN]; size_t nstart[DIMS_2] = {0, 0}, ncount[DIMS_2] = {LAT_LEN, LON_LEN}; size_t nindex[DIMS_2] = {0, 0}; int scalar_data_in = 0; int i, j; /* Create some data. */ for (i = 0; i < LAT_LEN; i++) for (j = 0; j < LON_LEN; j++) data_out[i][j] = j; /* Create a file with one SDS, containing our phony data. */ sd_id = SDstart(FILE_NAME, DFACC_CREATE); sds_id = SDcreate(sd_id, PRES_NAME, DFNT_INT32, DIMS_2, dim_size); if (SDwritedata(sds_id, start, NULL, edge, (void *)data_out)) ERR; if (SDendaccess(sds_id)) ERR; if (SDend(sd_id)) ERR; /* Now open with netCDF and check the contents. */ if (nc_open(FILE_NAME, NC_NOWRITE, &ncid)) ERR; if (nc_inq(ncid, &ndims_in, &nvars_in, &natts_in, &unlimdim_in)) ERR; if (ndims_in != 2 || nvars_in != 1 || natts_in != 0 || unlimdim_in != -1) ERR; if (nc_inq_dim(ncid, 0, NULL, &len_in)) ERR; if (len_in != LAT_LEN) ERR; if (nc_inq_dim(ncid, 1, NULL, &len_in)) ERR; if (len_in != LON_LEN) ERR; /* Read the data through a vara function from the netCDF API. */ if (nc_get_vara(ncid, 0, nstart, ncount, data_in)) ERR; for (i = 0; i < LAT_LEN; i++) for (j = 0; j < LON_LEN; j++) if (data_in[i][j] != data_out[i][j]) ERR; /* Reset for next test. */ for (i = 0; i < LAT_LEN; i++) for (j = 0; j < LON_LEN; j++) data_in[i][j] = -88; /* Read the data through a vara_int function from the netCDF API. */ if (nc_get_vara_int(ncid, 0, nstart, ncount, data_in)) ERR; for (i = 0; i < LAT_LEN; i++) for (j = 0; j < LON_LEN; j++) if (data_in[i][j] != data_out[i][j]) ERR; /* Reset for next test. */ for (i = 0; i < LAT_LEN; i++) for (j = 0; j < LON_LEN; j++) data_in[i][j] = -88; /* Read the data through a var_int function from the netCDF API. */ if (nc_get_var_int(ncid, 0, data_in)) ERR; for (i = 0; i < LAT_LEN; i++) for (j = 0; j < LON_LEN; j++) if (data_in[i][j] != data_out[i][j]) ERR; /* Read the data through a var1 function from the netCDF API. */ for (i = 0; i < LAT_LEN; i++) for (j = 0; j < LON_LEN; j++) { nindex[0] = i; nindex[1] = j; if (nc_get_var1(ncid, 0, nindex, &scalar_data_in)) ERR; if (scalar_data_in != data_out[i][j]) ERR; scalar_data_in = -88; /* reset */ if (nc_get_var1_int(ncid, 0, nindex, &scalar_data_in)) ERR; if (scalar_data_in != data_out[i][j]) ERR; } if (nc_close(ncid)) ERR; } SUMMARIZE_ERR; printf("*** testing with a more complex HDF4 file..."); { #define Z_LEN 3 #define Y_LEN 2 #define X_LEN 5 #define DIMS_3 3 #define NUM_TYPES 8 int32 sd_id, sds_id; int32 dim_size[DIMS_3] = {Z_LEN, Y_LEN, X_LEN}; int dimids_in[DIMS_3]; int ncid, nvars_in, ndims_in, natts_in, unlimdim_in; size_t len_in; nc_type type_in; int hdf4_type[NUM_TYPES] = {DFNT_FLOAT32, DFNT_FLOAT64, DFNT_INT8, DFNT_UINT8, DFNT_INT16, DFNT_UINT16, DFNT_INT32, DFNT_UINT32}; int netcdf_type[NUM_TYPES] = {NC_FLOAT, NC_DOUBLE, NC_BYTE, NC_UBYTE, NC_SHORT, NC_USHORT, NC_INT, NC_UINT}; char tmp_name[NC_MAX_NAME + 1], name_in[NC_MAX_NAME + 1]; char dim_name[NC_MAX_NAME + 1][DIMS_3] = {"z", "y", "x"}; int d, t; /* Create a HDF4 SD file. */ sd_id = SDstart (FILE_NAME, DFACC_CREATE); /* Create some HDF4 datasets. */ for (t = 0; t < NUM_TYPES; t++) { sprintf(tmp_name, "hdf4_dataset_type_%d", t); if ((sds_id = SDcreate(sd_id, tmp_name, hdf4_type[t], DIMS_3, dim_size)) == FAIL) ERR; /* Set up dimensions. By giving them the same names for each * dataset, I am specifying that they are shared * dimensions. */ for (d = 0; d < DIMS_3; d++) { int32 dimid; if ((dimid = SDgetdimid(sds_id, d)) == FAIL) ERR; if (SDsetdimname(dimid, dim_name[d])) ERR; } if (SDendaccess(sds_id)) ERR; } if (SDend(sd_id)) ERR; /* Open the file with netCDF and check it out. */ if (nc_open(FILE_NAME, NC_NOWRITE, &ncid)) ERR; if (nc_inq(ncid, &ndims_in, &nvars_in, &natts_in, &unlimdim_in)) ERR; if (ndims_in != DIMS_3 || nvars_in != NUM_TYPES || natts_in != 0 || unlimdim_in != -1) ERR; if (nc_inq_dim(ncid, 0, NULL, &len_in)) ERR; if (len_in != Z_LEN) ERR; if (nc_inq_dim(ncid, 1, NULL, &len_in)) ERR; if (len_in != Y_LEN) ERR; if (nc_inq_dim(ncid, 2, NULL, &len_in)) ERR; if (len_in != X_LEN) ERR; for (t = 0; t < NUM_TYPES; t++) { if (nc_inq_var(ncid, t, name_in, &type_in, &ndims_in, dimids_in, &natts_in)) ERR; if (type_in != netcdf_type[t] || ndims_in != DIMS_3 || dimids_in[0] != 0 || dimids_in[2] != 2 || dimids_in[2] != 2 || natts_in != 0) ERR; } if (nc_close(ncid)) ERR; } SUMMARIZE_ERR; FINAL_RESULTS; }
int do_lone(char* file_name, int do_diffs) { char sds_name[] = "lone"; int32 rank = 1; int32 dim_sds[1] = {5}; /* dimension of the data set */ int32 data[5] = {1, 2, 3, 4, 5}; int32 start[1]; /* start position to write for each dimension */ int32 edges[1]; /* number of elements to be written along each dimension */ int32 sds_id; int32 dim_id; int32 sd_id; if ( do_diffs ) { data[1] = data[2] = 0; } sd_id = SDstart(file_name, DFACC_CREATE); /* create the SDS */ if ((sds_id = SDcreate (sd_id, sds_name, DFNT_INT32, rank, dim_sds))<0) { printf( "Could not create SDS <%s>\n",sds_name); goto fail; } dim_id = SDgetdimid(sds_id, 0); SDsetdimname(dim_id, sds_name); /* define the location and size of the data to be written to the data set */ start[0] = 0; edges[0] = 5; /* write the stored data to the data set */ if (SDwritedata (sds_id, start, NULL, edges, (VOIDP)data)==FAIL) { printf( "Failed to set write for SDS <%s>\n", sds_name); goto fail; } SDendaccess(sds_id); /* create the SDS */ if ((sds_id = SDcreate (sd_id, "sds", DFNT_INT32, rank, dim_sds))<0) { printf( "Could not create SDS <%s>\n"); goto fail; } if (SDwritedata (sds_id, start, NULL, edges, (VOIDP)data)==FAIL) { printf( "Failed to set write for SDS <%s>\n"); goto fail; } SDendaccess(sds_id); SDend(sd_id); { int32 file1_id; /* HDF file identifier */ int32 an_id; /* AN interface identifier */ int32 file_label_id; /* file label identifier */ /* open file */ if ((file1_id = Hopen (file_name, DFACC_WRITE, (int16)0))<0) { printf("Error: Could not open file <%s>\n",file_name); return FAIL; } /* Initialize the AN interface */ an_id = ANstart (file1_id); /* Create the file label */ file_label_id = ANcreatef (an_id, AN_FILE_LABEL); /* Write the annotations to the file label */ if (ANwriteann (file_label_id,FILE_LABEL_TXT,strlen (FILE_LABEL_TXT))==FAIL) { printf( "Could not write AN\n"); return FAIL; } /* Terminate access to annotation */ if (ANendaccess (file_label_id)==FAIL) { printf( "Could not end AN\n"); return FAIL; } /* Terminate access to the AN interface */ if (ANend (an_id)==FAIL) { printf( "Could not end AN\n"); return FAIL; } /* close the HDF file */ if (Hclose (file1_id)==FAIL) { printf( "Could not close file\n"); return FAIL; } } return SUCCEED; fail: SDend(sd_id); return FAIL; }
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; }
/* 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 */
/*------------------------------------------------------------------------- * 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; }
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 */
int main(void) { int32 sd1_id; /* SD interface identifier */ int32 sd2_id; /* SD interface identifier */ int32 sds1_id; /* SDS identifier */ int32 sds2_id; /* SDS identifier */ int32 dim_sizes[2]; /* sizes of the SDS dimensions */ int32 start[2]; /* start location to write */ int32 edges[2]; /* number of elements to write */ int32 n_values; int32 buf1a[Y_LENGTH][X_LENGTH] = {{1,1},{1,1},{5,6}}; int32 buf1b[Y_LENGTH][X_LENGTH] = {{1,2},{3,4},{5,6}}; /* percent test: compare divide by zero, both zero */ int32 buf2a[Y_LENGTH][X_LENGTH] = {{100,100},{100,0},{0,100}}; int32 buf2b[Y_LENGTH][X_LENGTH] = {{120,80},{0,100},{0,50}}; /* global attributes */ char8 bufga1[] = "Storm_track_data1"; char8 bufga2[] = "Storm_track_data2"; float32 bufa1[2] = {1., 1.}; float32 bufa2[2] = {1., 2.}; /*vdata*/ int32 file1_id; int32 file2_id; int32 vdata1_id; int32 vdata2_id; char8 vdata1_buf1 [N_RECORDS_1] = {'V', 'D', 'A', 'T', 'A'}; char8 vdata1_buf2 [N_RECORDS_1] = {'X', 'D', 'A', 'T', 'A'}; int32 vdata2_buf1 [N_RECORDS_2][ORDER_2] = {{1, 2, 3, 4}, {5, 6, 7, 8}}; int32 vdata2_buf2 [N_RECORDS_2][ORDER_2] = {{1, 1, 1, 1}, {5, 6, 7, 8}}; float32 vdata3_buf1[N_RECORDS_2][N_VALS_PER_REC]={{1,2,3,4,5,6},{7,8,9,10,11,12}}; float32 vdata3_buf2[N_RECORDS_2][N_VALS_PER_REC]={{1,1,1,1,1,1},{7,8,9,10,11,12}}; /* Define the location and size of the data to be written to the data set*/ start[0] = 0; start[1] = 0; edges[0] = Y_LENGTH; edges[1] = X_LENGTH; /* Define the dimensions of the array to be created */ dim_sizes[0] = Y_LENGTH; dim_sizes[1] = X_LENGTH; /*------------------------------------------------------------------------- * SD data *------------------------------------------------------------------------- */ /* Create the files and initialize the SD interface */ if ((sd1_id = SDstart (FILE1_NAME, DFACC_CREATE))==FAIL) goto error; if ((sd2_id = SDstart (FILE2_NAME, DFACC_CREATE))==FAIL) goto error; /* Set a global attribute */ n_values = sizeof(bufga1); if ( SDsetattr (sd1_id, FILE_ATTR_NAME, DFNT_CHAR8, n_values, (VOIDP)bufga1)==FAIL) goto error; if ( SDsetattr (sd2_id, FILE_ATTR_NAME, DFNT_CHAR8, n_values, (VOIDP)bufga2)==FAIL) goto error; /* Create the data sets */ if ((sds1_id = SDcreate (sd1_id, "dset1", DFNT_INT32, RANK, dim_sizes))==FAIL) goto error; if ((sds2_id = SDcreate (sd2_id, "dset1", DFNT_INT32, RANK, dim_sizes))==FAIL) goto error; /* Assign attribute */ n_values = 2; if ( SDsetattr (sds1_id, SDS_ATTR_NAME, DFNT_FLOAT32, n_values, (VOIDP)bufa1)==FAIL) goto error; if ( SDsetattr (sds2_id, SDS_ATTR_NAME, DFNT_FLOAT32, n_values, (VOIDP)bufa2)==FAIL) goto error; /* Write the stored data to the data set */ if ( SDwritedata (sds1_id, start, NULL, edges, (VOIDP)buf1a)==FAIL) goto error; if ( SDwritedata (sds2_id, start, NULL, edges, (VOIDP)buf1b)==FAIL) goto error; /* Terminate access to the data set */ if ( SDendaccess (sds1_id)==FAIL) goto error; if ( SDendaccess (sds2_id)==FAIL) goto error; /* Create another data set */ if (( sds1_id = SDcreate (sd1_id, "dset2", DFNT_INT32, RANK, dim_sizes))==FAIL) goto error; if (( sds2_id = SDcreate (sd2_id, "dset2", DFNT_INT32, RANK, dim_sizes))==FAIL) goto error; if ( SDwritedata (sds1_id, start, NULL, edges, (VOIDP)buf1a)==FAIL) goto error; if ( SDwritedata (sds2_id, start, NULL, edges, (VOIDP)buf1b)==FAIL) goto error; if ( SDendaccess (sds1_id)==FAIL) goto error; if ( SDendaccess (sds2_id)==FAIL) goto error; /* data sets for -p test */ if (( sds1_id = SDcreate (sd1_id, "dset3", DFNT_INT32, RANK, dim_sizes))==FAIL) goto error; if (( sds2_id = SDcreate (sd2_id, "dset3", DFNT_INT32, RANK, dim_sizes))==FAIL) goto error; if ( SDwritedata (sds1_id, start, NULL, edges, (VOIDP)buf2a)==FAIL) goto error; if ( SDwritedata (sds2_id, start, NULL, edges, (VOIDP)buf2b)==FAIL) goto error; if ( SDendaccess (sds1_id)==FAIL) goto error; if ( SDendaccess (sds2_id)==FAIL) goto error; /*------------------------------------------------------------------------- * end SD *------------------------------------------------------------------------- */ /* Terminate access to the SD interface and close the file */ if ( SDend (sd1_id)==FAIL) goto error; if ( SDend (sd2_id)==FAIL) goto error; /*------------------------------------------------------------------------- * VD data *------------------------------------------------------------------------- */ /* Open the HDF file for writing */ if ((file1_id = Hopen (FILE1_NAME, DFACC_WRITE, 0))==FAIL) goto error; if ((file2_id = Hopen (FILE2_NAME, DFACC_WRITE, 0))==FAIL) goto error; /* Initialize the VS interface */ if ( Vstart (file1_id)==FAIL) goto error; if ( Vstart (file2_id)==FAIL) goto error; /*------------------------------------------------------------------------- * VD data one field *------------------------------------------------------------------------- */ /* Create the first vdata and populate it with data from vdata1_buf */ if (VHstoredata (file1_id, FIELD1_NAME, (uint8 *)vdata1_buf1, N_RECORDS_1, DFNT_CHAR8, VDATA1_NAME, CLASS1_NAME)==FAIL) goto error; if (VHstoredata (file2_id, FIELD1_NAME, (uint8 *)vdata1_buf2, N_RECORDS_1, DFNT_CHAR8, VDATA1_NAME, CLASS1_NAME)==FAIL) goto error; /*------------------------------------------------------------------------- * VD data one field, order 4 *------------------------------------------------------------------------- */ /* Create the second vdata and populate it with data from vdata2_buf */ if ( VHstoredatam (file1_id, FIELD2_NAME, (uint8 *)vdata2_buf1, N_RECORDS_2, DFNT_INT32, VDATA2_NAME, CLASS2_NAME, ORDER_2)==FAIL) goto error; if ( VHstoredatam (file2_id, FIELD2_NAME, (uint8 *)vdata2_buf2, N_RECORDS_2, DFNT_INT32, VDATA2_NAME, CLASS2_NAME, ORDER_2)==FAIL) goto error; /*------------------------------------------------------------------------- * VD data several fields *------------------------------------------------------------------------- */ /* Create a new vdata */ if ((vdata1_id = VSattach (file1_id, -1, "w"))==FAIL) goto error; if ((vdata2_id = VSattach (file2_id, -1, "w"))==FAIL) goto error; /* Set name and class name of the vdata */ if ( VSsetname (vdata1_id, VDATA3_NAME)==FAIL) goto error; if ( VSsetclass (vdata1_id, CLASS3_NAME)==FAIL) goto error; if ( VSsetname (vdata2_id, VDATA3_NAME)==FAIL) goto error; if ( VSsetclass (vdata2_id, CLASS3_NAME)==FAIL) goto error; /* Define fields */ if ( VSfdefine (vdata1_id, FIELD3_NAME1, DFNT_FLOAT32, ORDER3_1 )==FAIL) goto error; if ( VSfdefine (vdata1_id, FIELD3_NAME2, DFNT_FLOAT32, ORDER3_2 )==FAIL) goto error; if ( VSfdefine (vdata1_id, FIELD3_NAME3, DFNT_FLOAT32, ORDER3_3 )==FAIL) goto error; if ( VSsetfields (vdata1_id, FIELDNAME3_LIST )==FAIL) goto error; if ( VSfdefine (vdata2_id, FIELD3_NAME1, DFNT_FLOAT32, ORDER3_1 )==FAIL) goto error; if ( VSfdefine (vdata2_id, FIELD3_NAME2, DFNT_FLOAT32, ORDER3_2 )==FAIL) goto error; if ( VSfdefine (vdata2_id, FIELD3_NAME3, DFNT_FLOAT32, ORDER3_3 )==FAIL) goto error; if ( VSsetfields (vdata2_id, FIELDNAME3_LIST)==FAIL) goto error; /* Write the data with full interlacing mode */ if ( VSwrite (vdata1_id, (uint8 *)vdata3_buf1, N_RECORDS_2, FULL_INTERLACE)==FAIL) goto error; if ( VSwrite (vdata2_id, (uint8 *)vdata3_buf2, N_RECORDS_2, FULL_INTERLACE)==FAIL) goto error; if ( VSdetach (vdata1_id)==FAIL) goto error; if ( VSdetach (vdata2_id)==FAIL) goto error; /*------------------------------------------------------------------------- * end VD data *------------------------------------------------------------------------- */ /* Terminate access to the VS interface and close the HDF file */ if ( Vend (file1_id)==FAIL) goto error; if ( Vend (file2_id)==FAIL) goto error; if ( Hclose (file1_id)==FAIL) goto error; if ( Hclose (file2_id)==FAIL) goto error; /*------------------------------------------------------------------------- * write 2 big files for hyperslab reading *------------------------------------------------------------------------- */ if (do_big_file()==FAIL) goto error; /*------------------------------------------------------------------------- * groups *------------------------------------------------------------------------- */ if (do_groups()==FAIL) goto error; return 0; error: printf("Error...Exiting...\n"); return 1; }
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 */