#include #include // default HDF5 library #include #define DATASETNAME "HDFEOS/SWATHS/MOP01/Data Fields/MOPITTRadiances" #define MOPITT_RANK 5 /* NOTE: argv[1] = INPUT_FILE argv[2] = OUTPUT_FILE */ /********************* *FUNCTION PROTOTYPES* *********************/ float getElement5D( float *array, hsize_t dimSize[5], int *position ); // note, this function is used for testing. It's temporary int main( int argc, char* argv[] ) { if ( argc != 3 ) { fprintf( stderr, "\nError! Program expects format: %s [input HDF5 file] [output HDF5 file]\n\n", argv[0] ); return EXIT_FAILURE; } hid_t file; // file ID hid_t dataset; // dataset ID hid_t filespace; // filespace ID hid_t memspace; // memoryspace ID hid_t dcpl_id; hsize_t MOPITTdims[MOPITT_RANK]; // size of each MOPITT dimension hsize_t MOPITTmaxsize[MOPITT_RANK]; // maximum allowed size of each dimension in MOPITTdims hsize_t MOPITTChunkSize[MOPITT_RANK]; herr_t status_n, status; /* * Open the file and do error checking */ file = H5Fopen( argv[1], H5F_ACC_RDONLY, H5P_DEFAULT ); if ( file < 0 ) { fprintf( stderr, "\nError! Could not open HDF5 file. Exiting program.\n\n" ); return EXIT_FAILURE; } /* * open radiance dataset and do error checking */ dataset = H5Dopen( file, DATASETNAME, H5F_ACC_RDONLY ); if ( dataset < 0 ) { fprintf( stderr, "\nError! Could not open dataset. Exiting program.\n\n" ); return EXIT_FAILURE; } /* * Get dataset dimension */ filespace = H5Dget_space(dataset); status_n = H5Sget_simple_extent_dims(filespace, MOPITTdims, MOPITTmaxsize ); /* * Create a data_out buffer using the dimensions of the dataset specified by MOPITTdims */ //float *****data_out = allocateMOPITTBuffer( MOPITTdims ); float *data_out = malloc( sizeof(float ) * MOPITTdims[0] * MOPITTdims[1] * MOPITTdims[2] * MOPITTdims[3] * MOPITTdims[4]); /* * get memory space */ memspace = H5Screate_simple( MOPITT_RANK, MOPITTdims, NULL ); /* * Now read dataset into data_out buffer * H5T_NATIVE_FLOAT is specifying that we are reading floating points * H5S_ALL specifies that we are reading the entire dataset instead of just a portion */ status = H5Dread( dataset, H5T_NATIVE_FLOAT, memspace, filespace, H5P_DEFAULT, data_out ); /* Free all memory associated with the input data */ H5Sclose(memspace); H5Sclose(filespace); H5Dclose(dataset); H5Fclose(file); /******************************************************************************************* *Reading the dataset is now complete. We have freed all related memory for the input data * *(except of course the data_out array) and can now work on writing this data to a new file* *******************************************************************************************/ /* Create a file. H5F_ACC_TRUNC says to overwrite if file already exists, H5P_DEFAULT * says to create with default properties. */ file = H5Fcreate( argv[2], H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT ); /* Because dataset is large, we need to use chunking. Create dataset creation property list, * set to use chunking. */ MOPITTChunkSize[0] = 100; MOPITTChunkSize[1] = MOPITTdims[1]; MOPITTChunkSize[2] = MOPITTdims[2]; MOPITTChunkSize[3] = MOPITTdims[3]; MOPITTChunkSize[4] = MOPITTdims[4]; /* set dataspace creation property list to chunk */ dcpl_id = H5Pcreate(H5P_DATASET_CREATE); H5Pset_chunk(dcpl_id, MOPITT_RANK, MOPITTChunkSize); memspace = H5Screate_simple( MOPITT_RANK, MOPITTdims, NULL ); dataset = H5Dcreate( file, "/MOPITT/Data Fields/MOPITT Radiances", H5T_NATIVE_FLOAT, memspace, H5P_DEFAULT, dcpl_id, H5P_DEFAULT ); /* Free all remaining memory */ H5Dclose(dataset); H5Sclose(memspace); H5Pclose(dcpl_id); H5Fclose(file); free(data_out); return 0; } /* DESCRIPTION: This function returns the value specified by the dimensions given to it in the array ARGUMENTS: 1. float array 2. Size of each dimension (given by a 5 element 1D array. Elements 0-4 specify size of dimensions 0-4). 3. Requested position in the array (given by a 5 element 1D array. Elements 0-4 specify which position in each dimension) EFFECTS: None. Does not write anything. RETURN: Value stored at specified dimension array[dim0][dim1][dim2][dim3][dim4] */ float getElement5D( float *array, hsize_t dimSize[5], int position[5] ) { if ( position[0] < 0 || position[0] >= dimSize[0] || position[1] < 0 || position[1] >= dimSize[1] || position[2] < 0 || position[2] >= dimSize[2] || position[3] < 0 || position[3] >= dimSize[3] || position[4] < 0 || position[4] >= dimSize[3] ) { fprintf( stderr, "\ngetElement5D: Error! Invalid position. Check that dimSize and position arguments are correct.\n"); exit (-1); } return array[ (position[0] * dimSize[1] * dimSize[2] * dimSize[3] * dimSize[4]) + (position[1] * dimSize[2] * dimSize[3] * dimSize[4]) + (position[2] * dimSize[3] * dimSize[4]) + (position[3] * dimSize[4]) + position[4] ]; }