# Read 1D dataset into a 2D array

**URL:** <https://forum.hdfgroup.org/t/read-1d-dataset-into-a-2d-array/1028>\
**Category:** Hdf-forum archives\
**Created:** [June 18, 2008, 4:20pm UTC](https://forum.hdfgroup.org/t/read-1d-dataset-into-a-2d-array/1028 "2008-06-18T16:20:51Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nikhil\_Laghave1](https://forum.hdfgroup.org/letter_avatar_proxy/v4/letter/n/3be4f8/32.png) [@Nikhil\_Laghave1](https://forum.hdfgroup.org/u/Nikhil_Laghave1)\
**Post date:** [June 18, 2008, 4:20pm UTC](https://forum.hdfgroup.org/t/read-1d-dataset-into-a-2d-array/1028/1 "2008-06-18T16:20:51Z")

</div>

Hi All,

I am trying to convert a simple fortran read to a HDF read.  
The fortran code is: read(17) amp(1:i1reach,k1)

I assume this means read the contents of unit 17 into the array 'amp', through  
rows '1- i1reach' and column no. 'k1'(Please correct me if I am wrong, since I  
am new to Fortran as well). But the thing is the contents in unit 17 are a 1D  
array and it being read into a 2D array 'amp'

Now I have the contents of the file 17 in a HDF dataset as well in a 1D array,  
from which I am trying to read the contents and store in the array 'amp' that is  
2D. This is what I am doing:

C Open file containing dataset  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;CALL h5fopen\_f (filename,H5F\_ACC\_RDWR\_F,file\_id,error)

C Open Dataset containing the contents of file 17  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;CALL h5dopen\_f(file\_id,dsetname,dset\_id, error)

C Select a hyperslab.

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;CALL h5dget\_space\_f(dset\_id, filespace, error)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;CALL h5sget\_simple\_extent\_dims\_f(filespace,  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;& dimsw, maxdimsw, error)

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;offset(1) = 0  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dims(1) = i1reach

C Select the 1D array of length 'i1reach'

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;CALL h5sselect\_hyperslab\_f(filespace,  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;& H5S\_SELECT\_SET\_F,offset,dims,error)

C create memory dataspace(2D) corresponding to array 'amp'.  
C Note: dims of amp are stored in dims\_mem

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;CALL h5screate\_simple\_f(2,dims\_mem,memspace,error)

C select memory hyperslab from memspace, through rows '1-i1reach'  
C and column k1

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;offset\_out(1) = 0  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;offset\_out(2) = k1

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;count\_out(1) = i1reach  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;count\_out(2) = 1  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;CALL h5sselect\_hyperslab\_f(memspace, H5S\_SELECT\_SET\_F,  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;& offset\_out, count\_out, error)

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;CALL h5dread\_f(dset\_id,H5T\_NATIVE\_REAL,amp,  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;& dims\_mem,error,memspace,filespace)

But I am getting an error in Hdfread operation. It says that "selection+offset  
not within extent". Can anyone tell me where I am going wrong, since I can't  
spot where is the discrepancy.

Thanks a lot.

Regards,  
Nikhil

> **···**
>
> ----------------------------------------------------------------------  
> This mailing list is for HDF software users discussion.  
> To subscribe to this list, send a message to [hdf-forum-subscribe@hdfgroup.org](mailto:hdf-forum-subscribe@hdfgroup.org).  
> To unsubscribe, send a message to [hdf-forum-unsubscribe@hdfgroup.org](mailto:hdf-forum-unsubscribe@hdfgroup.org).
