#include "mfhdf.h" #define DS1_NAME "dset 1" #define DS2_NAME "dset 2" #define DS3_NAME "dset 3" #define DS4_NAME "dset 4" #define DIM1 10 #define DIM2 70 #define DIM3 30 #define DIM4 40 #define FILENAME "Four1D_Shared.hdf" int32 create_SDS(int32 sd_id, char *name, int32 numrecs2write) { int32 sds_id, status, dim_id; int32 dims[2], start[2], edges[2], rank; int32 *array_data, count; char dimname[40]; intn j; unsigned long file_numrecs; int var_numrecs; /* Allocate space for data buffer. */ array_data = (int32 *) HDmalloc(numrecs2write * sizeof(int32)); count = numrecs2write; /* Fill the stored-data array with values. */ for (j = 0; j < numrecs2write; j++) { array_data[j] = count--; } /* Define the rank and dimensions of the data set to be created. */ rank = 1; dims[0] = SD_UNLIMITED; /* Create the array data set. */ sds_id = SDcreate(sd_id, name, DFNT_INT32, rank, dims); if (sds_id == FAIL) return (FAIL); /* Write to the data set 'numrecs2write' of records. */ start[0] = 0; edges[0] = numrecs2write; status = SDwritedata(sds_id, start, NULL, edges, (VOIDP)array_data); if (status == FAIL) return (FAIL); return(sds_id); } /* Prints out the number of records of the variable and its file */ void print_numrecs(int32 sds_id, char *name) { unsigned long file_numrecs = 0; int var_numrecs = 0; intn status = FAIL; #if 0 /* A temporary API is added to mfsd.c, just for this investigation, to get the file numrecs and the var numrecs. */ status = SDgetnumrecs(sds_id, &file_numrecs, &var_numrecs); if (status == FAIL) printf ("SDgetnumrecs failed for sds id %i\n", sds_id); else printf (" file numrecs = %d, var '%s' numrecs = %d\n", file_numrecs, name, var_numrecs); #endif status = SUCCEED; } /****************************************************************************** This program follows these steps: 1) Create 4 datasets with unlimited dimensions 2) Write 10, 70, 30 and 40 records correspondingly to each dataset 2') Use (and preserve) ncdump to see the result 3) Share unlimited dimension for dset2 (70 records) and dset4 (40 records); examine values of all Vdatas; examine file numrecs (BTW, how it will be stored in the file?) 3') use ncdump to see the result 4) Create unlimited dimension scale that is a shared dimension for dset2 and dset4, write 80 values to it; examine all Vdatas; examine file numrec 4') use ncdump to see the result 5) Share unlimited dimension for dset1 and dset3; examine all Vdatas and file numrec 5') use ncdump to see the result 6) Now create dimension scale for the shared dimension between dset1 and dset3 and write 60 values to it; examine as usual 6') use ncdump (this should choke it or give us 60) ********************************************************************************/ int main( ) { int32 sd_id, sds1, sds2, sds3, sds4, status, sds_idx; int32 dim1_id, dim2_id, dim3_id, dim4_id; int32 dims[2], start[2], edges[2], rank; uint8 dimscales[DIM2+10]; intn ii, jj, k, l, m, n; /* Create and open the file and initiate the SD interface. */ sd_id = SDstart(FILENAME, DFACC_CREATE); /* Create and write 4 data sets with the following names and number of records */ sds1 = create_SDS(sd_id, DS1_NAME, DIM1); /* 10 records */ sds2 = create_SDS(sd_id, DS2_NAME, DIM2); /* 70 records */ sds3 = create_SDS(sd_id, DS3_NAME, DIM3); /* 30 records */ sds4 = create_SDS(sd_id, DS4_NAME, DIM4); /* 40 records */ /************* Current values of numrecs ************************ after sds1 written: file numrecs = 10 sds1 numrecs = 10 after sds2 written: file numrecs = 70 sds2 numrecs = 70 after sds3 written: file numrecs = 70 sds3 numrecs = 30 after sds4 written: file numrecs = 70 sds4 numrecs = 40 *****************************************************************/ /**** From ncdump, dumpsds, & dumpvd after writing 4 data sets **** --- Output from ncdump: Four1D_Shared.ncdump_notsharedyet fakeDim0 = 70 ; fakeDim1 = 70 ; fakeDim2 = 70 ; fakeDim3 = UNLIMITED ; // (70 currently) --- Output from hdp dumpsd: Four1D_Shared.sd_notsharedyet fakeDim0 = UNLIMITED (currently 10) fakeDim1 = UNLIMITED (currently 70) fakeDim2 = UNLIMITED (currently 30) fakeDim3 = UNLIMITED (currently 40) --- Output from hdp dumpvd: Four1D_Shared.vd_notsharedyet vdata "fakeDim0": value = 70 vdata "fakeDim1": value = 70 vdata "fakeDim2": value = 70 vdata "fakeDim3": value = 70 *****************************************************************/ /* Print number of records of file and data sets. */ fprintf(stderr, "Number of records after create and write data sets:\n"); print_numrecs(sds1, DS1_NAME); print_numrecs(sds2, DS2_NAME); print_numrecs(sds3, DS3_NAME); print_numrecs(sds4, DS4_NAME); /* Name the unlimited dimension of data set "dset 2" */ dim2_id = SDgetdimid(sds2, 0); status = SDsetdimname(dim2_id, "shared unlim dim for 2 & 4"); /**** From ncdump, dumpsds, & dumpvd after naming the Udim of "dset 2" **** --- Output from ncdump: Four1D_Shared.ncdump_named2 fakeDim0 = 70 ; shared unlim dim for 2 & 4 = 70 ; fakeDim2 = 70 ; fakeDim3 = UNLIMITED ; // (70 currently) (Note: ncdump has a bug that a variable with less number of records than the max numrecs in the file will contain left-over values from the previous variable) --- Output from hdp dumpsd: Four1D_Shared.sd_named2 fakeDim0 = UNLIMITED (currently 10) shared unlim dim for 2 & 4 = UNLIMITED (currently 70) fakeDim2 = UNLIMITED (currently 30) fakeDim3 = UNLIMITED (currently 40) --- Output from hdp dumpvd: Four1D_Shared.vd_named2 vdata "fakeDim0": value = 70 vdata "shared unlim dim for 2 & 4": value = 70 vdata "fakeDim2": value = 70 vdata "fakeDim3": value = 70 *****************************************************************/ /* Share unlimited dimension for "dset 2" and "dset 4" */ dim4_id = SDgetdimid(sds4, 0); status = SDsetdimname(dim4_id, "shared unlim dim for 2 & 4"); /**** From ncdump, dumpsds, & dumpvd after sharing UD for "dset 2" and "dset 4" **** --- Output from ncdump: Four1D_Shared.ncdump_2and4dhared fakeDim0 = 70 ; shared unlim dim for 2 & 4 = 70 ; fakeDim2 = UNLIMITED ; // (70 currently) *** Note: for ncdump, only the last unlimited dimension is shown UNLIMITED like this. --- Output from hdp dumpsd: Four1D_Shared.sd_2and4dhared fakeDim0 = UNLIMITED (currently 10) shared unlim dim for 2 & 4 = UNLIMITED (currently 70) fakeDim2 = UNLIMITED (currently 30) shared unlim dim for 2 & 4 = UNLIMITED (currently 40) --- Output from hdp dumpvd: Four1D_Shared.vd_2and4dhared vdata "fakeDim0": 70 vdata "shared unlim dim for 2 & 4": 70 vdata "fakeDim2": 70 *****************************************************************/ /************* Current values of numrecs **************** file numrecs = 70 sds1: var numrecs = 10 sds2: var numrecs = 70 sds3: var numrecs = 30 sds4: var numrecs = 40 *********************************************************/ for (ii = 0; ii < DIM2+10; ii++) dimscales[ii] = ii; /* Create dimension scale that is a shared dimension for sds2 and sds4 */ status = SDsetdimscale(dim4_id, DIM2+10, DFNT_UINT8, dimscales); /**** From ncdump, dumpsds, & dumpvd after setting dimscale for shared dim 2&4 **** --- Output from ncdump: Four1D_Shared.ncdump_dimscale24 fakeDim0 = 80 ; shared unlim dim for 2 & 4 = 80 ; fakeDim2 = UNLIMITED ; // (80 currently) (Note: all variables are filled up to 80 records) --- Output from hdp dumpsd: Four1D_Shared.sd_dimscale24 fakeDim0 = UNLIMITED (currently 10) // sds 1 shared unlim dim for 2 & 4 = UNLIMITED (currently 70) // sds 2 fakeDim2 = UNLIMITED (currently 30) // sds 3 shared unlim dim for 2 & 4 = UNLIMITED (currently 40) // sds 4 shared unlim dim for 2 & 4 = UNLIMITED (currently 80) // dimension variable Output from hdp dumpvd: Four1D_Shared.vd_dimscale24 vdata "fakeDim0": 80 vdata "shared unlim dim for 2 & 4": 80 vdata "fakeDim2": 80 *****************************************************************/ fprintf(stderr, "Number of records after setdimscale:\n"); print_numrecs(sds1, DS1_NAME); print_numrecs(sds2, DS2_NAME); print_numrecs(sds3, DS3_NAME); print_numrecs(sds4, DS4_NAME); /*************** Number of records after setdimscale:************** file numrecs = 80, var 'dset 1' numrecs = 10 var 'dset 2' numrecs = 70 var 'dset 3' numrecs = 30 var 'dset 4' numrecs = 40 ******************************************************************/ /* Name the unlimited dimension of data set "dset 1" */ dim1_id = SDgetdimid(sds1, 0); status = SDsetdimname(dim1_id, "shared unlim dim for 1 & 3"); /**** From ncdump, dumpsds, & dumpvd after naming the Udim of "dset 1" **** --- Output from ncdump: Four1D_Shared.ncdump_named1 shared unlim dim for 1 & 3 = 80 ; shared unlim dim for 2 & 4 = 80 ; fakeDim2 = UNLIMITED ; // (80 currently) --- Output from hdp dumpsd: Four1D_Shared.sd_named1 shared unlim dim for 1 & 3 = UNLIMITED (currently 10) shared unlim dim for 2 & 4= UNLIMITED (currently 70) fakeDim2 = UNLIMITED (currently 30) shared unlim dim for 2 & 4 = UNLIMITED (currently 40) --- Output from hdp dumpvd: Four1D_Shared.vd_named1 vdata "shared unlim dim for 1 & 3": value = 80 vdata "shared unlim dim for 2 & 4": value = 80 vdata "fakeDim2": value = 80 *****************************************************************/ /* Share unlimited dimension for "dset 1" and "dset 3" */ dim3_id = SDgetdimid(sds3, 0); status = SDsetdimname(dim3_id, "shared unlim dim for 1 & 3"); /**** From ncdump, dumpsds, & dumpvd after sharing UD for "dset 1" and "dset 3" **** --- Output from ncdump: Four1D_Shared.ncdump_1and3dhared shared unlim dim for 1 & 3 = 80 ; shared unlim dim for 2 & 4 = UNLIMITED ; // (80 currently) --- Output from hdp dumpsd: Four1D_Shared.sd_1and3dhared shared unlim dim for 1 & 3 = UNLIMITED (currently 10) shared unlim dim for 2 & 4= UNLIMITED (currently 70) shared unlim dim for 1 & 3 = UNLIMITED (currently 30) shared unlim dim for 2 & 4 = UNLIMITED (currently 40) --- Output from hdp dumpvd: Four1D_Shared.vd_1and3dhared vdata "shared unlim dim for 1 & 3": value = 80 vdata "shared unlim dim for 2 & 4": value = 80 *****************************************************************/ /************* Current values of numrecs **************** file numrecs = 80, var 'dset 1' numrecs = 10 var 'dset 2' numrecs = 70 var 'dset 3' numrecs = 30 var 'dset 4' numrecs = 40 *********************************************************/ /* Use 60 now until I know Elena's intention */ for (ii = 0; ii < 60; ii++) dimscales[ii] = ii; /* Create dimension scale that is a shared dimension for sds1 and sds3 */ status = SDsetdimscale(dim3_id, 60, DFNT_UINT8, dimscales); /**** From ncdump, dumpsds, & dumpvd after setting dimscale for shared dim 1&3 **** --- Output from ncdump: Four1D_Shared.ncdump_dimscale13 shared unlim dim for 1 & 3 = 80 ; shared unlim dim for 2 & 4 = UNLIMITED ; // (80 currently) (Note: as with SDS variables, dim variables also contain left-over values from the previous variable) --- Output from hdp dumpsd: Four1D_Shared.sd_dimscale13 shared unlim dim for 1 & 3 = UNLIMITED (currently 10) // sds 1 shared unlim dim for 2 & 4 = UNLIMITED (currently 70) // sds 2 shared unlim dim for 1 & 3 = UNLIMITED (currently 30) // sds 3 shared unlim dim for 2 & 4 = UNLIMITED (currently 40) // sds 4 shared unlim dim for 2 & 4 = UNLIMITED (currently 80) // dimension variable shared unlim dim for 1 & 3 = UNLIMITED (currently 60) // dimension variable Output from hdp dumpvd: Four1D_Shared.vd_dimscale13 vdata "shared unlim dim for 1 & 3": 80 vdata "shared unlim dim for 2 & 4": 80 *****************************************************************/ fprintf(stderr, "Number of records after setdimscale:\n"); print_numrecs(sds1, DS1_NAME); print_numrecs(sds2, DS2_NAME); print_numrecs(sds3, DS3_NAME); print_numrecs(sds4, DS4_NAME); /*************** Number of records after setdimscale:************** file numrecs = 80, var 'dset 1' numrecs = 10 var 'dset 2' numrecs = 70 var 'dset 3' numrecs = 30 var 'dset 4' numrecs = 40 ******************************************************************/ /* Close the data sets. */ status = SDendaccess(sds1); if (status == FAIL) printf ("SDendaccess failed for sds id %i\n", sds1); status = SDendaccess(sds2); if (status == FAIL) printf ("SDendaccess failed for sds id %i\n", sds2); status = SDendaccess(sds3); if (status == FAIL) printf ("SDendaccess failed for sds id %i\n", sds3); status = SDendaccess(sds4); if (status == FAIL) printf ("SDendaccess failed for sds id %i\n", sds4); /* Close the file. */ status = SDend(sd_id); if (status == FAIL) printf ("SDend failed\n"); return 0; } /* Private notes: Reminder for unrelated issues: 1. Try to access dim2_id after sharing dim4_id to see if it's still accessible */