Hi
I attempt to creat a .data file whose data are from many different datasets.
I've try to write them once but did not work. The outcome is the data only
derived from the first HDF file and others are lost.
Then I see
this:http://hdf-forum.184993.n3.nabble.com/Can-hyperslab-select-between-datasets-td194609.html.

Hence I was wondering that if I could write data into that .data file after
I close the previous  writing process.

following are my source code:

void createDataFile2()  /*create a .data file for 2 dimensions*/
{
     hsize_t     dims[2];              /* dataset dimensions */
    short       (*data)[NY2];      /* data that get from source file, then
write to target file */
    hid_t       sourceDataset, targetDataset;         /* handles */
    hid_t       sourceDataspace, targetDataspace;   
    hid_t       sourceFile, targetFile;    
    hid_t       memspace; 
    hid_t       plist;    
    herr_t      status;                                
    hsize_t     dimsm[2], count[2], offset[2];              /* size of the
hyperslab in the file */
    hsize_t     dims_out[2], count_out[2], offset_out[2];   /* hyperslab
offset in the file */
    int         rank, RecNum,i;
        char *k[50];

        RecNum = ReadRec("./name.txt", k);// get the HDF filenames and put them
into a array
        printf("RecNum=%d\n", RecNum);   
    /*read data from source file, begin*/                         
    
   // printf("\nRank: %d\nDimensions: %lu  %lu \n", rank,
         //  (unsigned long)(dims_out[0]), (unsigned long)(dims_out[1]));

    /* Define the whole memory dataspace.*/
    dimsm[0] = NX2;
    dimsm[1] = NY2*RecNum;
    memspace = H5Screate_simple (RANK2, dimsm, NULL);  
        
        //for(i = 0; i < RecNum; i++)
        //      {
        sourceFile = H5Fopen (k[0], H5F_ACC_RDONLY, H5P_DEFAULT);
    sourceDataset = H5Dopen (sourceFile, DATASETNAME2);
    sourceDataspace = H5Dget_space (sourceDataset);    /* dataspace handle
*/
    rank    = H5Sget_simple_extent_ndims (sourceDataspace);
    status  = H5Sget_simple_extent_dims (sourceDataspace, dims_out, NULL);
        /* 
     * Define a hyperslab in current dataset. 
     */
        offset[0] = 0;
    offset[1] = 0;
    count[0]  = NX2;
    count[1]  = NY2;
    status = H5Sselect_hyperslab (sourceDataspace, H5S_SELECT_SET, offset,
NULL, 
                                  count, NULL);

    /* Define memory hyperslab. */
    offset_out[0] = 0;
    offset_out[1] = 0;
    count_out[0]  = NX2;
    count_out[1]  = NY2;
    status = H5Sselect_hyperslab (memspace, H5S_SELECT_SET, offset_out,
NULL, 
                                  count_out, NULL);

    data = malloc(NX2*NY2*sizeof(short));
    /* Read data from hyperslab in the file into the hyperslab in memory.*/
    status = H5Dread (sourceDataset, H5T_NATIVE_INT16, memspace,
sourceDataspace,
                      H5P_DEFAULT, data);
    /*read data from source file, end*/

        H5Dclose(sourceDataset);    
        H5Sclose(sourceDataspace);
        H5Fclose(sourceFile); 
        
        //      }

        /* attempt to read data from the 2nd HDF5*/
        sourceFile = H5Fopen (k[1], H5F_ACC_RDONLY, H5P_DEFAULT);
    sourceDataset = H5Dopen (sourceFile, DATASETNAME2);
    sourceDataspace = H5Dget_space (sourceDataset);    /* dataspace handle
*/
    rank    = H5Sget_simple_extent_ndims (sourceDataspace);
    status  = H5Sget_simple_extent_dims (sourceDataspace, dims_out, NULL);
        /* 
     * Define a hyperslab in current dataset. 
     */
        offset[0] = 0;
    offset[1] = 0;
    count[0]  = NX2;
    count[1]  = NY2;
    status = H5Sselect_hyperslab (sourceDataspace, H5S_SELECT_SET, offset,
NULL, 
                                  count, NULL);

    /* Define memory hyperslab. */
    offset_out[0] = 0;
    offset_out[1] = 1*NY2;
    count_out[0]  = NX2;
    count_out[1]  = NY2;
    status = H5Sselect_hyperslab (memspace, H5S_SELECT_SET, offset_out,
NULL, 
                                  count_out, NULL);

    data = malloc(NX2*NY2*sizeof(short));
    /* Read data from hyperslab in the file into the hyperslab in memory.*/
    status = H5Dread (sourceDataset, H5T_NATIVE_INT16, memspace,
sourceDataspace,
                      H5P_DEFAULT, data);
    /*read data from source file, end*/

        H5Dclose(sourceDataset);    
        H5Sclose(sourceDataspace);
        H5Fclose(sourceFile); 

        /* attempt to read data from the 2nd HDF5 end*/
        
    /*write data to target file*/
    targetFile = H5Fcreate (TARGETFILE, H5F_ACC_TRUNC, H5P_DEFAULT,
H5P_DEFAULT);
    
    /* create data space for dataset with 2 dimensions. */
    dims[0] = NX2;
    dims[1] = NY2*2;
    targetDataspace = H5Screate_simple (RANK2, dims, NULL);    

        
    /*create dataset and the related external file*/
    plist = H5Pcreate (H5P_DATASET_CREATE);
    status = H5Pset_external (plist, TARGET_DATA_FILE2, 0,
(hsize_t)(NX2*(NY2*2)*sizeof(short)) );     
    targetDataset = H5Dcreate (targetFile, DATASETNAME2, H5T_NATIVE_INT16,
targetDataspace, plist);
                         
    /* Write the data to the dataset using default transfer properties. */
    status = H5Dwrite (targetDataset, H5T_NATIVE_INT16, H5S_ALL, H5S_ALL,
                      H5P_DEFAULT, data);           

        
    H5Pclose(plist);      
    H5Sclose(targetDataspace);   
    H5Fclose(targetFile);  
        H5Dclose(targetDataset);
    H5Sclose(memspace);           
}



--
View this message in context: 
http://hdf-forum.184993.n3.nabble.com/Can-I-read-data-from-datasets-belong-to-different-HDF-tp4025519.html
Sent from the hdf-forum mailing list archive at Nabble.com.

_______________________________________________
Hdf-forum is for HDF software users discussion.
[email protected]
http://mail.hdfgroup.org/mailman/listinfo/hdf-forum_hdfgroup.org

Reply via email to