summaryrefslogtreecommitdiffstats
path: root/doc/html/Tutor/selectc.html
blob: e01d9dbc6f1f2fcfdcc41e98522af7c13e283ba7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
<HTML><HEAD>
<TITLE>HDF5 Tutorial - Selections using H5Sselect_elements and H5Scopy
</TITLE> 
</HEAD>

<body bgcolor="#ffffff">

<!-- BEGIN MAIN BODY -->

<A HREF="http://www.ncsa.uiuc.edu/"><img border=0 
src="http://www.ncsa.uiuc.edu/Images/NCSAhome/footerlogo.gif"
width=78 height=27 alt="NCSA"><P></A>

 [ <A HREF="title.html"><I>HDF5 Tutorial Top</I></A> ]
<H1>
<BIG><BIG><BIG><FONT COLOR="#c101cd">Selections using H5Sselect_elements and H5SCopy</FONT>
</BIG></BIG></BIG></H1>

<hr noshade size=1>

<BODY>
<H2>Contents:</H2>
<UL>
    <LI> <A HREF="#def">Selecting Independent Points and Copying a Dataspace</A>
    <LI> Programming Example 
    <UL>
      <LI> <A HREF="#desc">Description</A> 
      <LI> <A HREF="#rem">Remarks</A> 
      <LI> <A HREF="#fc">File Contents</A>
    </UL>
</UL>
<HR>
<A NAME="def">
<H2>Selecting Independent Points and Copying a Dataspace</h2>
You can select independent points to read or write to in a 
dataspace by use of the H5Sselect_elements function. 
<P>
The H5Scopy function allows you to make an exact copy of a dataspace,
which can help cut down on the number of function calls needed when
selecting a dataspace.
<P>
<H2> Programming Example</H2>
<A NAME="desc">
<H3><U>Description</U></H3>      
This example shows you how to use H5Sselect_elements to select individual
points in a dataset and how to use H5Scopy to make a copy of a dataspace.

[ <A HREF="examples/h5_copy.c">Download h5_copy.c</A> ]
<PRE>
/***********************************************************************/
/*                                                                     */
/*  PROGRAM:   h5_copy.c                                               */
/*  PURPOSE:   Shows how to use the H5SCOPY function.                  */
/*  DESCRIPTION:                                                       */
/*             This program creates two files, copy1.h5, and copy2.h5. */
/*             In copy1.h5, it creates a 3x4 dataset called 'Copy1',   */
/*             and write 0's to this dataset.                          */
/*             In copy2.h5, it create a 3x4 dataset called 'Copy2',    */
/*             and write 1's to this dataset.                          */
/*             It closes both files, reopens both files, selects two   */
/*             points in copy1.h5 and writes values to them.  Then it  */
/*             does an H5Scopy from the first file to the second, and  */
/*             writes the values to copy2.h5.  It then closes the      */
/*             files, reopens them, and prints the contents of the     */
/*             two datasets.                                           */
/*                                                                     */
/***********************************************************************/
 
#include "hdf5.h"
#define FILE1 "copy1.h5"
#define FILE2 "copy2.h5"

#define RANK  2
#define DIM1  3
#define DIM2  4
#define NUMP  2 

int main (void)
{
     hid_t   file1, file2, dataset1, dataset2;
     hid_t   mid1, mid2, fid1, fid2;
     hsize_t fdim[] = {DIM1, DIM2};
     hsize_t mdim[] = {DIM1, DIM2};
     hsize_t start[2], stride[2], count[2], block[2];
     int buf1[DIM1][DIM2];
     int buf2[DIM1][DIM2];
     int bufnew[DIM1][DIM2];
     int val[] = {53, 59};
     hsize_t marray[] = {2};
     hssize_t coord[NUMP][RANK];
     herr_t ret;
     uint  i, j;

/***********************************************************************/
/*                                                                     */
/* Create two files containing identical datasets. Write 0's to one    */
/* and 1's to the other.                                               */
/*                                                                     */
/***********************************************************************/

     for ( i = 0; i &lt DIM1; i++ ) 
         for ( j = 0; j &lt DIM2; j++ )
             buf1[i][j] = 0;

     for ( i = 0; i &lt DIM1; i++ ) 
         for ( j = 0; j &lt DIM2; j++ )
             buf2[i][j] = 1;

     file1 = H5Fcreate(FILE1, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT);
     file2 = H5Fcreate(FILE2, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT);

     fid1 = H5Screate_simple (RANK, fdim, NULL);
     fid2 = H5Screate_simple (RANK, fdim, NULL);

     dataset1 = H5Dcreate (file1, "Copy1", H5T_NATIVE_INT, fid1, H5P_DEFAULT);
     dataset2 = H5Dcreate (file2, "Copy2", H5T_NATIVE_INT, fid2, H5P_DEFAULT);

     ret = H5Dwrite(dataset1, H5T_NATIVE_INT, H5S_ALL, H5S_ALL, H5P_DEFAULT, buf1);
     ret = H5Dwrite(dataset2, H5T_NATIVE_INT, H5S_ALL, H5S_ALL, H5P_DEFAULT, buf2);

     ret = H5Dclose (dataset1);
     ret = H5Dclose (dataset2);

     ret = H5Sclose (fid1);
     ret = H5Sclose (fid2);

     ret = H5Fclose (file1);
     ret = H5Fclose (file2);

/***********************************************************************/
/*                                                                     */
/* Open the two files.  Select two points in one file, write values to */
/* those point locations, then do H5Scopy and write the values to the  */
/* other file.  Close files.                                           */
/*                                                                     */
/***********************************************************************/
 
     file1 = H5Fopen (FILE1, H5F_ACC_RDWR, H5P_DEFAULT);
     file2 = H5Fopen (FILE2, H5F_ACC_RDWR, H5P_DEFAULT);
     dataset1 = H5Dopen (file1, "Copy1");
     dataset2 = H5Dopen (file2, "Copy2");
     fid1 = H5Dget_space (dataset1);
     mid1 = H5Screate_simple(1, marray, NULL); 
     coord[0][0] = 0; coord[0][1] = 3;
     coord[1][0] = 0; coord[1][1] = 1;

     ret = H5Sselect_elements (fid1, H5S_SELECT_SET, NUMP, (const hssize_t **)coord);

     ret = H5Dwrite (dataset1, H5T_NATIVE_INT, mid1, fid1, H5P_DEFAULT, val); 

     fid2 = H5Scopy (fid1);

     ret = H5Dwrite (dataset2, H5T_NATIVE_INT, mid1, fid2, H5P_DEFAULT, val); 

     ret = H5Dclose (dataset1);
     ret = H5Dclose (dataset2);
     ret = H5Sclose (fid1);
     ret = H5Sclose (fid2);
     ret = H5Fclose (file1);
     ret = H5Fclose (file2);
     ret = H5Sclose (mid1);

/***********************************************************************/
/*                                                                     */
/* Open both files and print the contents of the datasets.             */
/*                                                                     */
/***********************************************************************/

     file1 = H5Fopen (FILE1, H5F_ACC_RDWR, H5P_DEFAULT);
     file2 = H5Fopen (FILE2, H5F_ACC_RDWR, H5P_DEFAULT);
     dataset1 = H5Dopen (file1, "Copy1");
     dataset2 = H5Dopen (file2, "Copy2");

     ret = H5Dread (dataset1, H5T_NATIVE_INT, H5S_ALL, H5S_ALL,
                    H5P_DEFAULT, bufnew);
     
     printf ("\nDataset 'Copy1' in file 'copy1.h5' contains: \n");
     for (i=0;i&lt;DIM1; i++) {
        for (j=0;j&lt;DIM2;j++) printf ("%3d  ", bufnew[i][j]);
        printf("\n");
     }

     printf ("\nDataset 'Copy2' in file 'copy2.h5' contains: \n");

     ret = H5Dread (dataset2, H5T_NATIVE_INT, H5S_ALL, H5S_ALL,
                    H5P_DEFAULT, bufnew);

     for (i=0;i&lt;DIM1; i++) {
        for (j=0;j&lt;DIM2;j++) printf ("%3d  ", bufnew[i][j]);
        printf("\n");
     }
     ret = H5Dclose (dataset1);
     ret = H5Dclose (dataset2);
     ret = H5Fclose (file1);
     ret = H5Fclose (file2);

}
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
</PRE>

<A NAME="rem">
<H3><U>Remarks</U></H3>
<UL>
<LI>H5Sselect_elements selects array elements to be included in the
selection for a dataspace:
<PRE>
   herr_t H5Sselect_elements (hid_t space_id, H5S_seloper_t op, 
                              size_t num_elem, const hssize_t **coord ) 
</PRE>
<UL>
<LI>The <I>space_id</I> parameter is the dataspace identifier.<BR>
<LI>The <I>op</I> parameter currently can only be set to H5S_SELECT_SET and
specifies to replace the existing selection with the parameters from this
call.
<LI>The <I>coord</I> array is a two-dimensional array of size 'dataspace rank'
by the number of elements to be selected, <I>num_elem</I>.<BR>
</UL>
<P>
<LI>H5Scopy creates an exact copy of a dataspace:
<PRE>
   hid_t H5Scopy(hid_t space_id ) 
</PRE>
<UL>
<LI>The <I>space_id</I> parameter is the dataspace identifier to copy. 
</UL>
</UL>
</UL>
<P>

<A NAME="fc">
<H3><U>File Contents</U></H3>

Following is the DDL for <I>copy1.h5</I> and <I>copy2.h5</I>, as viewed with 
the commands "h5dump copy1.h5" and "h5dump copy2.h5".
<P>
<B>Fig. S.1</B> &nbsp; <I>'copy1.h5' in DDL</I>
<PRE>
HDF5 "copy1.h5" {
GROUP "/" {
   DATASET "Copy1" {
      DATATYPE { H5T_STD_I32BE }
      DATASPACE { SIMPLE ( 3, 4 ) / ( 3, 4 ) }
      DATA {
         0, 59, 0, 53,
         0, 0, 0, 0,
         0, 0, 0, 0
      }
   }
}
}
</PRE>
<B>Fig. S.2</B> &nbsp; <I>'copy2.h5' in DDL</I>
<PRE>
HDF5 "copy2.h5" {
GROUP "/" {
   DATASET "Copy2" {
      DATATYPE { H5T_STD_I32BE }
      DATASPACE { SIMPLE ( 3, 4 ) / ( 3, 4 ) }
      DATA {
         1, 59, 1, 53,
         1, 1, 1, 1,
         1, 1, 1, 1
      }
   }
}
}

</PRE>



<!-- BEGIN FOOTER INFO -->

<P><hr noshade size=1>
<font face="arial,helvetica" size="-1">
  <a href="http://www.ncsa.uiuc.edu/"><img border=0
     src="http://www.ncsa.uiuc.edu/Images/NCSAhome/footerlogo.gif"
     width=78 height=27 alt="NCSA"><br>
  The National Center for Supercomputing Applications</A><br>
  <a href="http://www.uiuc.edu/">University of Illinois
    at Urbana-Champaign</a><br>
  <br>
<!-- <A HREF="helpdesk.mail.html"> -->
<A HREF="mailto:hdfhelp@ncsa.uiuc.edu">
hdfhelp@ncsa.uiuc.edu</A>
<BR> <H6>Last Modified: August 27, 1999</H6><BR>
<!-- modified by Barbara Jones - bljones@ncsa.uiuc.edu -->
</FONT>
<BR>
<!-- <A HREF="mailto:hdfhelp@ncsa.uiuc.edu"> -->

</BODY>
</HTML>