Hello Maxime,

You mentioned the phypepat.html tutorial in your question. The phypepat.html 
tutorial shows how to write by pattern in Parallel HDF5.
By "chunk" did you mean "by pattern", or are you looking at another tutorial 
example?

I took the example in this tutorial, made it a 'serial' example, and added code 
at the end that re-opens the file and reads
by pattern.  See the attached example. (Note that reading and writing are very 
similar!)

Using hyperslab selection can be confusing. We have a very simple example in 
the HDF5 Tutorial that you can play
around with to get a feel for how changing the offset/count/stride/block 
affects what gets written.  See this topic in the
"Learning the Basics" tutorial:

   http://www.hdfgroup.org/HDF5/Tutor/selectsimple.html

I attached the h5_subset.c example from this tutorial.

There is also an example, h5ex_hyper.c, on this page, that you may want to look 
at:

    http://www.hdfgroup.org/HDF5/examples/api18-c.html

If you still have questions or would like to see more examples, just let me 
know! I'd be happy to help.

-Barbara

======================
Barbara Jones
The HDF Group Helpdesk
[email protected]
======================

-----Original Message-----
From: Hdf-forum [mailto:[email protected]] On Behalf Of 
Maxime Boissonneault
Sent: Tuesday, February 03, 2015 3:28 PM
To: HDF Users Discussion List
Subject: [Hdf-forum] Question for reading data by pattern

Hi,
I am trying to read data from an HDF5 file, by chunk. I wrote it following the 
tutorial here :
http://www.hdfgroup.org/HDF5/Tutor/phypepat.html

However, I have trouble reading the data the same way. Any chance someone has 
the corresponding example for reading by chunk ?

Thanks,


--
---------------------------------
Maxime Boissonneault
Analyste de calcul - Calcul Québec, Université Laval Ph. D. en physique


_______________________________________________
Hdf-forum is for HDF software users discussion.
[email protected]
http://lists.hdfgroup.org/mailman/listinfo/hdf-forum_lists.hdfgroup.org
Twitter: https://twitter.com/hdf5
 
#include "hdf5.h"
#include "stdlib.h"

#define H5FILE_NAME     "SDS_pat.h5"
#define DATASETNAME     "IntArray" 
#define NX     8                      /* dataset dimensions */
#define NY     4 
#define RANK   2
#define RANK1  1

int
main (int argc, char **argv)
{
    /*
     * HDF5 APIs definitions
     */         
    hid_t       file_id, dset_id;         /* file and dataset identifiers */
    hid_t       filespace, memspace;      /* file and memory dataspace 
identifiers */
    hsize_t     dimsf[2];                 /* dataset dimensions */
    hsize_t     dimsm[1];                 /* dataset dimensions */
    int         *data;                    /* pointer to data buffer to write */
    int         rdata[4][2];
    hsize_t     count[2];                 /* hyperslab selection parameters */
    hsize_t     stride[2];
    hsize_t     offset[2];
    int         i,ipat, j;
    herr_t      status;


    file_id = H5Fcreate(H5FILE_NAME, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT);
   

    /*
     * Create the dataspace for the dataset.
     */
    dimsf[0] = NX;
    dimsf[1] = NY;
    dimsm[0] = NX;   
    filespace = H5Screate_simple(RANK, dimsf, NULL); 
    memspace  = H5Screate_simple(RANK1, dimsm, NULL); 

    /*
     * Create the dataset with default properties and close filespace.
     */
    dset_id = H5Dcreate(file_id, DATASETNAME, H5T_NATIVE_INT, filespace,
                        H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
    H5Sclose(filespace);

    /* 
     * Write a hyperslab by pattern to the file
     */
    count[0] = 4;
    count[1] = 2;
    stride[0] = 2;
    stride[1] = 2;
    for (ipat=0; ipat<4; ipat++)
    {
       if(ipat == 0) {
          offset[0] = 0;
          offset[1] = 0;
       } 
       if(ipat == 1) {
          offset[0] = 1;
          offset[1] = 0;
       }
       if(ipat == 2) {
          offset[0] = 0;
          offset[1] = 1;
       } 
       if(ipat == 3) {
          offset[0] = 1;
          offset[1] = 1;
       } 

       /*
        * Select hyperslab in the file.
        */
       filespace = H5Dget_space(dset_id);
       status = H5Sselect_hyperslab(filespace, H5S_SELECT_SET, offset, stride, 
count, NULL);

       /*
        * Initialize data buffer 
       */
       data = (int *) malloc(sizeof(int)*dimsm[0]);
       for (i=0; i < (int)dimsm[0]; i++) {
           data[i] = ipat + 1;
       }

       status = H5Dwrite(dset_id, H5T_NATIVE_INT, memspace, filespace,
                         H5P_DEFAULT, data);
       free(data);
    }

    /*
     * Close/release resources.
     */
    H5Dclose(dset_id);
    H5Sclose(filespace);
    H5Sclose(memspace);
    H5Fclose(file_id);

    /***************************************************
     * Re-open the file and read by pattern in a loop 
     ***************************************************/

    file_id = H5Fopen (H5FILE_NAME, H5F_ACC_RDONLY, H5P_DEFAULT);
    dset_id = H5Dopen (file_id, DATASETNAME, H5P_DEFAULT);

    count[0] = 4;
    count[1] = 2;
    stride[0] = 2;
    stride[1] = 2;

    for (ipat=0; ipat<4; ipat++)
    {
      if(ipat == 0) {
         offset[0] = 0;
         offset[1] = 0;
      } 
      if(ipat == 1) {
         offset[0] = 1;
         offset[1] = 0;
      }
      if(ipat == 2) {
         offset[0] = 0;
         offset[1] = 1;
      } 
      if(ipat == 3) {
         offset[0] = 1;
         offset[1] = 1;
      } 
      memspace  = H5Screate_simple(RANK1, dimsm, NULL); 
      filespace = H5Dget_space(dset_id);
      status = H5Sselect_hyperslab(filespace, H5S_SELECT_SET, offset, stride, 
count, NULL);
      status = H5Dread(dset_id, H5T_NATIVE_INT, memspace, filespace, 
H5P_DEFAULT, rdata);
      printf ("\nData Pattern %i:\n", ipat);

       for (i=0;i<4; i++)
          for (j=0;j<2; j++)
               printf (" %i", rdata[i][j]);
      printf ("\n");
    }
    status = H5Dclose(dset_id);
    status = H5Sclose(filespace);
    status = H5Sclose(memspace);
    status = H5Fclose(file_id);


    return 0;
}     
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * Copyright by The HDF Group.                                               *
 * Copyright by the Board of Trustees of the University of Illinois.         *
 * All rights reserved.                                                      *
 *                                                                           *
 * This file is part of HDF5.  The full HDF5 copyright notice, including     *
 * terms governing use, modification, and redistribution, is contained in    *
 * the files COPYING and Copyright.html.  COPYING can be found at the root   *
 * of the source code distribution tree; Copyright.html can be found at the  *
 * root level of an installed copy of the electronic HDF5 document set and   *
 * is linked from the top-level documents page.  It can also be found at     *
 * http://hdfgroup.org/HDF5/doc/Copyright.html.  If you do not have          *
 * access to either file, you may request a copy from [email protected].     *
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

/* 
 *  This example illustrates how to read/write a subset of data (a slab) 
 *  from/to a dataset in an HDF5 file.  It is used in the HDF5 Tutorial.
 */
 
#include "hdf5.h"

#define FILE        "subset.h5"
#define DATASETNAME "IntArray" 
#define RANK  2

#define DIM0_SUB  3                         /* subset dimensions */ 
#define DIM1_SUB  4 


#define DIM0     8                          /* size of dataset */       
#define DIM1     10 

int
main (void)
{
    hsize_t     dims[2], dimsm[2];   
    int         data[DIM0][DIM1];           /* data to write */
    int         sdata[DIM0_SUB][DIM1_SUB];  /* subset to write */
    int         rdata[DIM0][DIM1];          /* buffer for read */
 
    hid_t       file_id, dataset_id;        /* handles */
    hid_t       dataspace_id, memspace_id; 

    herr_t      status;                             
   
    hsize_t     count[2];              /* size of subset in the file */
    hsize_t     offset[2];             /* subset offset in the file */
    hsize_t     stride[2];
    hsize_t     block[2];
    int         i, j;

    
    /*****************************************************************
     * Create a new file with default creation and access properties.*
     * Then create a dataset and write data to it and close the file *
     * and dataset.                                                  *
     *****************************************************************/

    file_id = H5Fcreate (FILE, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT);

    dims[0] = DIM0;
    dims[1] = DIM1;
    dataspace_id = H5Screate_simple (RANK, dims, NULL); 

    dataset_id = H5Dcreate2 (file_id, DATASETNAME, H5T_STD_I32BE, dataspace_id,
                            H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);


    for (j = 0; j < DIM0; j++) {
        for (i = 0; i < DIM1; i++)
            if (i< (DIM1/2))
               data[j][i] = 1;
            else
               data[j][i] = 2;
    }     

    status = H5Dwrite (dataset_id, H5T_NATIVE_INT, H5S_ALL, H5S_ALL,
                      H5P_DEFAULT, data);

    printf ("\nData Written to File:\n");
    for (i = 0; i<DIM0; i++){
       for (j = 0; j<DIM1; j++)
           printf (" %i", data[i][j]);
       printf ("\n");
    }
    status = H5Sclose (dataspace_id);
    status = H5Dclose (dataset_id);
    status = H5Fclose (file_id);


    /*****************************************************
     * Reopen the file and dataset and write a subset of *
     * values to the dataset. 
     *****************************************************/

    file_id = H5Fopen (FILE, H5F_ACC_RDWR, H5P_DEFAULT);
    dataset_id = H5Dopen2 (file_id, DATASETNAME, H5P_DEFAULT);

    /* Specify size and shape of subset to write. */

    offset[0] = 1;
    offset[1] = 2;

    count[0]  = DIM0_SUB;  
    count[1]  = DIM1_SUB;

    stride[0] = 1;
    stride[1] = 1;

    block[0] = 1;
    block[1] = 1;

    /* Create memory space with size of subset. Get file dataspace 
       and select subset from file dataspace. */

    dimsm[0] = DIM0_SUB;
    dimsm[1] = DIM1_SUB;
    memspace_id = H5Screate_simple (RANK, dimsm, NULL); 

    dataspace_id = H5Dget_space (dataset_id);
    status = H5Sselect_hyperslab (dataspace_id, H5S_SELECT_SET, offset,
                                  stride, count, block);

    /* Write a subset of data to the dataset, then read the 
       entire dataset back from the file.  */

    printf ("\nWrite subset to file specifying:\n");
    printf ("    offset=1x2 stride=1x1 count=3x4 block=1x1\n");
    for (j = 0; j < DIM0_SUB; j++) {
        for (i = 0; i < DIM1_SUB; i++)
           sdata[j][i] = 5;
    }     

    status = H5Dwrite (dataset_id, H5T_NATIVE_INT, memspace_id,
                       dataspace_id, H5P_DEFAULT, sdata);
    
    status = H5Dread (dataset_id, H5T_NATIVE_INT, H5S_ALL, H5S_ALL,
                       H5P_DEFAULT, rdata);

    printf ("\nData in File after Subset is Written:\n");
    for (i = 0; i<DIM0; i++){
       for (j = 0; j<DIM1; j++)
           printf (" %i", rdata[i][j]);
       printf ("\n");
    }

    status = H5Sclose (memspace_id);
    status = H5Sclose (dataspace_id);
    status = H5Dclose (dataset_id);
    status = H5Fclose (file_id);
 
}
_______________________________________________
Hdf-forum is for HDF software users discussion.
[email protected]
http://lists.hdfgroup.org/mailman/listinfo/hdf-forum_lists.hdfgroup.org
Twitter: https://twitter.com/hdf5

Reply via email to