Fortran runtime error: End of file in gfortran - fortran

When I run this program using gfortran test.f95, it shows an error
At line 10 of file test.f95 (unit = 15, file = 'open.dat')
Fortran runtime error: End of file
Can someone tell me what is wrong here?
implicit none
integer:: a,b,c,ios
open(unit=15,file="open.dat",status='unknown', action='readwrite',iostat=ios)
open(unit=16,file="open.out",status="unknown",action='write')
do a=1,100
write(15,*)a
end do
do c=1,45,1
read(15,*)
read(15,*)b
write(16,*)b
end do
stop
end

Make sure to change location in the file with rewind/fseek
implicit none
integer :: a, b, c, ios, ierr
open( unit=15, file="open.dat", action='readwrite', iostat=ios)
open( unit=16, file="open.out", action='write')
do a=1,100
write(15,*)a
end do
! Here we need to get back to beginning of file
! otherwise, you try to read where you finished writing
! at the end of file
! CALL FSEEK(UNIT, OFFSET, WHENCE[, STATUS])
! https://gcc.gnu.org/onlinedocs/gfortran/FSEEK.html
! call fseek(15, 0, 0, ierr)
! you can also use rewind file
! https://www.fortran.com/F77_std/rjcnf0001-sh-12.html
rewind(15)
do c=1,45,1
read(15,*)
read(15,*)b
write(16,*)b
end do
end

Related

Fortran read text file

I have a file that writes like this
N
1000
NNODES
3
TURB_INT
0.20000
U_MEAN
30
TYPE_GP
dav
L
120
WIND_TURB
NODE_1 NODE_2 NODE_3
0.90139 -1.02858 0.03962
-2.56887 -1.59726 -0.82062
-0.58745 0.72129 -1.90712
-4.46302 -2.49995 -5.45345
-4.10550 -5.50565 -7.77285
-6.18588 -6.34998 -5.95054
I am really struggling to understand how to read it.
Finally what I need the most is reading the NODES records into variable WT(i,:), hence I could use a DO loop.
Here is what I have:
! open inputfile, ID=100
open(unit=100, file=inputfile, recl=fileln, status='old', iostat=iost)
print *, iost
print *, fileln
if (iost .ne. 0) then
print *, 'Error opening file'
print *, erromsg
else
print *, 'File opened correctly'
print *, erromsg
end if
! Trying to read what's written in file.
read(unit=100, *) N
print *, N
read(unit=100, *) Nval
print *, Nval
I was trying to see how reading is done line by line. I can read variable N, but right at second reading (line 2) I have "severe(24): END OF FILE during read".
Please could you advise me?
Thanks
I think you should read up on the proper flags for your open statement.
The keyword recl is used for direct access files. It means that every record has the same length, so if you need the 64th record, the program knows immediately where that record is.
If your file has a record length, I can't see it. As I said in my comment, I have the strong suspicion that fileln is the length of the file in bytes (you didn't say). So in your first read, the first record would be read, which is the entirety of the file, and then parsed into the variable N -- which can only take a single integer, everything else is then discarded.
And then it tries to read the next fileln bytes after the end of the file, which results in an 'end of file' error.
In your case, I would add the action="READ" and form="FORMATTED" keywords in the file open statement. And I always find it useful to not only look for the error code, but also the error message iomsg.
For what it's worth, here's my suggestion on how to read the file:
program readfile
implicit none
integer :: u ! File handle
integer :: ios
character(len=100) :: iom, line
integer :: N, NNodes, U_Mean, L
real :: Turb_Int
character(len=20) :: Type_GP
real, allocatable :: node_values(:)
character(len=10), allocatable :: node_names(:)
character(len=*), parameter :: inputfile = 'data.txt'
! newunit, instead of unit, creates a new unique
! file unit number. You could also set u to 100
! and then use unit=u
open(newunit=u, file=inputfile, action='READ', &
status='OLD', form='FORMATTED', iostat=ios, &
iomsg=iom)
if (ios /= 0) then
print *, "Error opening file:"
print *, iom
stop 1
end if
! Read the header. I'm always reading 'line' when
! I expect there to be just the next keyword
! in the line. You might want to check for that
read(u, *) line ! Hopefully 'N'
read(u, *) N
read(u, *) line ! Hopefully "NNODES"
read(u, *) NNodes
! I assume that NNODES stands for the number
! of columns later in the file.
! So here I'm also allocating the arrays.
allocate(node_values(NNodes))
allocate(node_names(NNodes))
read(u, *) line ! Hopefully TURB_INT
read(u, *) Turb_Int
read(u, *) line ! Hopefully U_MEN
read(u, *) U_Mean
read(u, *) line ! Hopefully TYPE_GP
read(u, *) Type_GP
read(u, *) line ! Hopefully L
read(u, *) L
read(u, *) line ! Hopefully WIND_TURB
read(u, *) node_names
! Just print out what we got from the header so far to see
! everyting's right
write(*, '(A, I0)') "N = ", N
write(*, '(A, I0)') "NNodes = ", NNodes
write(*, '(A, F5.1)') "Turb_Int = ", Turb_Int
write(*, '(A, I0)') "U_Mean = ", U_Mean
write(*, '(A, A)') "Type_GP = ", trim(Type_GP)
write(*, '(A, I0)') "L = ", L
write(*, '(A, *(A))') node_names
! Now read the data itself. In this case I'm just reading it
! line by line, print it out, and forget it again
! until the end of the file.
data_loop : do
read(u, *, iostat=ios, iomsg=iom) node_values
if (ios /= 0) exit data_loop
print *, node_values
end do data_loop
end program readfile

FORTRAN parsing file with varying line formate

I have only limited experience with FORTRAN and I need to parse files with a structure similar to this:
H s 13.010000 0.019685
1.962000 0.137977
0.444600 0.478148
s 0.122000 1.000000
p 0.727000 1.000000
***
He s 38.360000 0.023809
5.770000 0.154891
1.240000 0.469987
s 0.297600 1.000000
p 1.275000 1.000000
***
I need to search for the label (e.g. He) and then read the corresponding blocks into an array.
I know I can parse file by specifying the format each line is supposed to have, but here there are different formats possible.
In Python I would just split each line by the white spaces and deal with it depending on the number of columns. But how to approach this in FORTRAN?
You can read each line as a character string and then process it. If, as it seems, the format is fixed (element symbol in first two characters, orbital letter in sixth character, etc.), the following program could serve you as inspiration:
program elms
implicit none
integer, parameter :: MAX_LEN = 40
character(len=MAX_LEN) :: line_el, line
integer :: u
integer :: is
integer :: nlin
character(len=2) :: element = 'He'
integer, parameter :: MAX_LINES = 20
real, dimension(MAX_LINES) :: e, f
open(newunit=u, file='elms.dat', status='old', action='read')
main_loop: do
! Read line
read(u, '(a)', iostat=is) line_el
if (eof_iostat(is)) exit main_loop
! Check first two characters of the line vs. chemical element.
if (line_el(1:2) .eq. element) then
! This is the beginning of an element block
nlin = 0
line = line_el
do
if (line .ne. '') then
! Line is not empty or only spaces.
nlin = nlin + 1
if (line(6:6) .ne. ' ') then
! Line contains an orbital letter - process it.
end if
! Read the real values in the rest of the line
read(line(7:),*) e(nlin), f(nlin)
end if
! Read next line
read(u, '(a)', iostat=is) line
if (eof_iostat(is)) exit main_loop
if (line(1:2) .ne. ' ') then
! Finished processing element block.
exit main_loop
end if
end do
end if
end do main_loop
! Close file
close(u)
contains
logical function eof_iostat(istat)
! Returns true if the end of file has been reached
use, intrinsic :: iso_fortran_env, only: IOSTAT_END
implicit none
integer, intent(in) :: istat
select case (istat)
case (0) ! No error
eof_iostat = .false.
case (IOSTAT_END) ! End of file reached
eof_iostat = .true.
case default ! Error
STOP
end select
end function eof_iostat
end program
You will probably need to make the program a subroutine, make element an intent(in) dummy argument, process the orbital symbols, etc.
Note that, if possible, it would be easier to just read all the data from the file in one go, and then search for the relevant data in the memory (e.g., having an array with the chemical symbols).

I am trying to compile following code wrote in fortran but has many errors

I am trying to compile this following code in gfortran compiler:
do_cmd = .not. is_group
do while (.not. EOF(unit=100) .and. .not. flag_stop)
read (100, '(A)', iostat = ios) buf
if (buf(1:1)=="#") then
! comment line
elseif ((buf(1:2)=="v ") .and. do_cmd) then ! vertex
read(buf(2:), *, iostat=ios) p(1), p(2), p(3)
if (ios.ne.0) then
print "(/,2x,'Error of reading vertex position from obj-file (line #'(I3)');')", line_index
stop
end if
But I get these errors when the code is compiling:
do while (.not. EOF(unit=100) .and. .not. flag_stop)
1
Error: Keyword argument requires explicit interface for procedure ‘eof’ at (1)
and;
do while (.not. EOF(unit=100) .and. .not. flag_stop)
1
Error: Function ‘eof’ at (1) has no IMPLICIT type
I will be so grateful if anyone could help me.
edit:
I'm very new in fortran and couldn't find out how eof function works in gfortran.
What does it mean using unit=100 in argument?
And also Thought it is a logical function by default. How should I implicit its type?
As far as I know, EOF is not part of the Fortran Standard. At least I can't find it anywhere.
In most programming languages, EOF stands for "End of File", and it is fairly obvious to me that the EOF in your example is supposed to be a logical function that returns .TRUE. if and only if the read pointer associated with unit (in your case 100) points to the end of the file.
You can use the ios variable to check whether an EOF was encountered during the read. In that case, it will be -1. You can then use the exit statement to immediately quit the loop.
If you don't like the -1, and you use a Fortran 2003 compatible compiler, you might even use the module ISO_FORTRAN_ENV, which provides (amongst other things) the parameter IOSTAT_END.
Here's a very simple example:
program iso
use ISO_FORTRAN_ENV
implicit none
integer, parameter :: u = 100
character(len=80) :: buf
integer :: ios
open(unit=u, action='READ', status='OLD', file='data.dat')
read_loop : do
read(u, '(A)', iostat=ios) buf
if (ios == IOSTAT_END) exit read_loop ! EOF
if (buf(1:1) == '#') cycle read_loop ! This line is a comment
write(*, *) buf
end do read_loop
close(u)
end program iso
One note though: This code is meant as an example. It only exits the loop if it encounters an EOF -- not if the read fails for any other reason. (There are some other reasons that the the read would fail.) If that other error is persistent, then this will lead to an infinite loop.
It may be useful to try a user-defined EOF function (as shown below) to minimize the amount of code modifications; but because this function is rather inefficient, I guess it is probably better to rewrite the original code so as to utilize iostat in read statements directly (as suggested above)...
module eofmod !<-- please save this in a separate file, compile, and link with other programs
implicit none
contains
logical function EOF( unit )
integer, optional :: unit
integer :: ios
if ( .not. present( unit ) ) stop "no unit given"
read( unit, *, iostat=ios )
if ( ios == -1 ) then
EOF = .true.
else
EOF = .false. ; backspace( unit )
endif
end function
end module
program test
use eofmod !<-- needs to be inserted at the top of routines (or modules) where "EOF" is used
implicit none
character(200) :: buf
open(10, file="test.dat", status="old")
do while ( .not. EOF( unit=10 ) )
read( 10, "(a)" ) buf
print "(a)", trim( buf )
enddo
close(10)
end program

Why does Fortran HDF5's unlimited maximum dimension parameter (H5S_UNLIMITED_F) evaluate to zero instead of -1?

I'm try to compile a Fortran application to write HDF5 files. My compiler is gfortran 4.7.2. Specifically, I'm trying to create a dataspace with a certain set of current dimensions and maximum dimensions. I want the last dimension to have an unlimited maximum dimension. There isn't much documentation for Fortran HDF5, but I was able to figure out that this can be specified by setting that dimension in question to H5S_UNLIMITED_F. This value is supposed to evaluate to -1, but in my application it instead evaluates to 0, which causes a runtime error because 0 is less than the corresponding current dimension (in my case, 20). This is the error:
HDF5-DIAG: Error detected in HDF5 (1.8.11) thread 0:
#000: H5S.c line 1388 in H5Screate_simple(): maxdims is smaller than dims
major: Invalid arguments to routine
minor: Bad value
I compiled one of the Fortran examples that came with HDF5 that uses the same H5S_UNLIMITED_F parameter (h5_extend.f90), but for that application, the parameter evaluates to -1 and there is no problem.
What might I be doing wrong?
Below is a test program I wrote to replicate the problem seen in my project:
program simple_test
use hdf5
implicit none
integer :: irank, hdferr
integer(hsize_t) :: ny, nx, nz
real, dimension(:,:,:), allocatable :: dset
character (len = 256) :: hdf_file, dlab
integer(hid_t) :: file_handle, mem_space, file_space, dset_handle
integer(hsize_t), dimension(:), allocatable :: dim_array, max_array
irank = 3
ny = 10
nx = 15
nz = 20
allocate (dset(ny, nx, nz))
hdf_file = 'simple_test.hdf5'
dlab = 'simple_data'
allocate (dim_array(irank))
allocate (max_array(irank))
dim_array = (/ ny, nx, nz /)
max_array = (/ ny, nx, H5S_UNLIMITED_F /)
print *, 'h5s_unlimited_f: ', h5s_unlimited_f
print *, 'dim_array: ', dim_array
print *, 'max_array: ', max_array
call h5open_f(hdferr)
if (hdferr .eq. -1) then
print *, 'Error opening HDF5 Fortran interface.'
end if
! Create a new file.
call h5fcreate_f(hdf_file, H5F_ACC_TRUNC_F, file_handle, hdferr)
if (hdferr .eq. -1) then
print *, 'Error creating HDF5 file.'
end if
! Create memory dataspace.
call h5screate_simple_f(irank, dim_array, mem_space, hdferr, max_array)
if (hdferr .eq. -1) then
print *, 'Error creating HDF5 memory dataspace.'
end if
! Create the dataset.
call h5dcreate_f(file_handle, trim(dlab), H5T_IEEE_F32LE, mem_space, &
dset_handle, hdferr)
if (hdferr .eq. -1) then
print *, 'Error creating HDF5 dataset.'
end if
! Create file dataspace.
call h5screate_simple_f(irank, dim_array, file_space, hdferr, max_array)
if (hdferr .eq. -1) then
print *, 'Error creating HDF5 file dataspace.'
end if
call h5dwrite_f(dset_handle, H5T_IEEE_F32LE, dset, dim_array, hdferr, &
mem_space, file_space)
if (hdferr .eq. -1) then
print *, 'Error writing HDF5 dataset.'
end if
call h5close_f(hdferr)
if (hdferr .eq. -1) then
print *, 'Error closing HDF5 Fortran interface.'
end if
deallocate (dset)
deallocate (dim_array)
deallocate (max_array)
end program simple_test
The first call to h5s_create_simple_f is what fails. If I change the memory dataspace to not use the max_array parameter (since it is optional and in my case perhaps unnecessary), then I still get the same error on the second call to h5s_create_simple_f.
I'm compiling as follows:
gfortran -c simple_test.f90 -o simple_test.o -I<hdf5_include_path>
gfortran -o simple_test simple_test.o -L<hdf5_lib_path> -lhdf5_fortran -lhdf5hl_fortran
I've also tried setting max_array(irank) to -1, but that yields an entirely different error.
(The original issue was that H5S_UNLIMITED_F is a variable that is initialised by a call to H5open_f, referencing it before that initialization is not permitted.)
Are you sure that the call to H5S_create_simple_f is what fails? Your traceback indicates an error from the dataset portion of the library.
I would expect a failure from dataset creation, because for variable sized datasets you need to specify the chunk size. Create a property list using H5Pcreate_f, then set the chunk size using H5Pset_chunk_f, then provide that property list after the error argument in the call to H5Dcreate_f. Comment if that doesn't make sense and I'll dig out an example.

HDF5 for data files written with fortran

The HDF5 data storage uses the C convention, i.e. if I am storing a matrix A(N,M,K) in a binary file, the fastest changing dimension of the stored data will have size N. Apparently when I use the Fortran wrapper of HDF5, HDF5 automatically transposes the matrix, to be consistent with C.
I have a data of size (256 by 128 by 256) stored in an unformatted binary file written by fortran. I am trying to convert it into h5 format by using a program given below. But the final output is giving me the dimensions of the stored matrix as (128,256,256). I have no idea what to do to make sure that the final hd5 file can be rightly visualized in the visualizing software (Paraview).
PROGRAM H5_RDWT
USE HDF5 ! This module contains all necessary modules
IMPLICIT NONE
CHARACTER(LEN=6), parameter :: out_file = "out.h5" ! File name
CHARACTER(LEN=6), parameter :: in_file = "in.dat" ! File name
CHARACTER(LEN=4), parameter :: dsetname = "vort"! Dataset name
CHARACTER(LEN=50) :: len
INTEGER(HID_T) :: in_file_id ! File identifier
INTEGER(HID_T) :: out_file_id ! File identifier
INTEGER(HID_T) :: dset_id ! Dataset identifier
INTEGER(HID_T) :: dspace_id ! Dataspace identifier
INTEGER :: in_file_id = 23
INTEGER :: nx = 256, ny=128, nz=256
INTEGER(HSIZE_T), DIMENSION(3) :: dims ! Dataset dimensions
INTEGER :: rank = 3 ! Dataset rank
INTEGER :: error ! Error flag
INTEGER :: i, j, k, ii, jj, kk ! Indices
REAL, allocatable :: buff_r(:,:,:) ! buffer for reading from input file
dims(1) = nx
dims(2) = ny
dims(3) = nz
allocate(buff_r(nx,ny,nz))
! Read the input data.
open (in_file_id,FILE=in_file,form='unformatted',access='direct',recl=4*nx*ny*nz)
read (in_file_id,rec=1) buff_r
! Initialize FORTRAN interface of HDF5.
CALL h5open_f(error)
! Create a new file.
CALL h5fcreate_f (out_file, H5F_ACC_TRUNC_F, out_file_id, error)
! Create the dataspace.
CALL h5screate_simple_f(rank, dims, dspace_id, error)
! Create the dataset with default properties.
CALL h5dcreate_f(out_file_id, dsetname, H5T_NATIVE_REAL, dspace_id, &
dset_id, error)
! Write the dataset.
CALL h5dwrite_f(dset_id, H5T_NATIVE_REAL, buff_r, dims, error)
! End access to the dataset and release resources used by it.
CALL h5dclose_f(dset_id, error)
! Terminate access to the data space.
CALL h5sclose_f(dspace_id, error)
! Close the file.
CALL h5fclose_f(out_file_id, error)
! Close FORTRAN interface.
CALL h5close_f(error)
deallocate(buff_r)
END PROGRAM H5_RDWT
To illustrate what is happening, I am generating a sample data file using the following script:
program main
!-------- initialize variables -------------
character(8) :: fname
integer, parameter:: n = 32
real*8, dimension(n,n,2*n) :: re
integer i,j,k, recl
Inquire( iolength = recl ) re
!------ fill in the array with sample data ----
do k = 1, 2*n
do j = 1, n
do i = 1, n
re(i,j,k) = 1.0
end do
end do
end do
!------ write in data in a file -----------
write(fname, "(A)") "data.dat"
open (10, file=fname, form='unformatted', access='direct', recl=recl)
write(10,rec=1) re
close(10)
stop
end program main
I copy pasted the program by Ian Bush and changed the values of nx, ny and nz to 32, 32 and 64 respectively. I would expect the generated h5 file to have dimensions (32,32,64). But it is coming out to be (64,32,32). Here is what is happening in my machine:
[pradeep#laptop]$gfortran generate_data.f90
[pradeep#laptop]$./a.out
[pradeep#laptop]$ls -l data.dat
-rw-r--r-- 1 pradeep staff 524288 Mar 12 14:04 data.dat
[pradeep#laptop]$h5fc convert_to_h5.f90
[pradeep#laptop]$./a.out
[pradeep#laptop]$ls -l out.h5
-rw-r--r-- 1 pradeep staff 526432 Mar 12 14:05 out.h5
[pradeep#laptop]$h5dump -H out.h5
HDF5 "out.h5" {
GROUP "/" {
DATASET "data" {
DATATYPE H5T_IEEE_F64LE
DATASPACE SIMPLE { ( 64, 32, 32 ) / ( 64, 32, 32 ) }
}
}
}
Please confirm with me if you are seeing the same thing.
I have also run into trouble with viewing HDF5 files that I've written with a Fortran application. The basic issue is that Fortran and C store multidimensional arrays differently (Fortran is column-major, C is row-major), and since the Fortran HDF5 libraries are interfaces into the C HDF5 libraries, the Fortran wrapper transposes the dimensions before passing the data into the C code. Likewise, when a Fortran application reads an HDF5 file, the Fortran wrapper transposes the dimensions again.
So if you do all your writing and reading with Fortran applications, you shouldn't notice any discrepancies. If you write the file with a Fortran app and then read it with a C app (such as h5dump), the dimensions will appear transposed. That's not a bug, it's just how it works.
If you want to display the data correctly, either read the data with a Fortran application or use a C app and transpose the data first. (Or you could transpose the data before writing it in the first place.)
As already mentioned, this is all explained fairly well in section 7.3.2.5 of the documentation: http://www.hdfgroup.org/HDF5/doc/UG/UG_frame12Dataspaces.html
Long comment really rather than an answer ...
Can you clarify why you don't think it is working? Once I correct a couple of things in your code
1) in_file_id is declared twice with 2 different kinds
2) Recl for direct access files are not necessarily in terms of bytes - Inquire by output list is much more portable
I get the following which, having generated a dummy file with random data, seems to work:
ian#ian-pc:~/test/stack$ cat hdf5.f90
PROGRAM H5_RDWT
USE HDF5 ! This module contains all necessary modules
IMPLICIT NONE
CHARACTER(LEN=6), parameter :: out_file = "out.h5" ! File name
CHARACTER(LEN=6), parameter :: in_file = "in.dat" ! File name
CHARACTER(LEN=4), parameter :: dsetname = "vort"! Dataset name
CHARACTER(LEN=50) :: len
!!$ INTEGER(HID_T) :: in_file_id ! File identifier
INTEGER(HID_T) :: out_file_id ! File identifier
INTEGER(HID_T) :: dset_id ! Dataset identifier
INTEGER(HID_T) :: dspace_id ! Dataspace identifier
INTEGER(HID_T) :: in_file_id = 23
INTEGER :: nx = 256, ny=128, nz=256
INTEGER(HSIZE_T), DIMENSION(3) :: dims ! Dataset dimensions
INTEGER :: rank = 3 ! Dataset rank
Integer :: recl
INTEGER :: error ! Error flag
INTEGER :: i, j, k, ii, jj, kk ! Indices
REAL, allocatable :: buff_r(:,:,:) ! buffer for reading from input file
dims(1) = nx
dims(2) = ny
dims(3) = nz
allocate(buff_r(nx,ny,nz))
Inquire( iolength = recl ) buff_r
! Read the input data.
open (in_file_id,FILE=in_file,form='unformatted',access='direct',recl=recl)
read (in_file_id,rec=1) buff_r
! Initialize FORTRAN interface of HDF5.
CALL h5open_f(error)
! Create a new file.
CALL h5fcreate_f (out_file, H5F_ACC_TRUNC_F, out_file_id, error)
! Create the dataspace.
CALL h5screate_simple_f(rank, dims, dspace_id, error)
! Create the dataset with default properties.
CALL h5dcreate_f(out_file_id, dsetname, H5T_NATIVE_REAL, dspace_id, &
dset_id, error)
! Write the dataset.
CALL h5dwrite_f(dset_id, H5T_NATIVE_REAL, buff_r, dims, error)
! End access to the dataset and release resources used by it.
CALL h5dclose_f(dset_id, error)
! Terminate access to the data space.
CALL h5sclose_f(dspace_id, error)
! Close the file.
CALL h5fclose_f(out_file_id, error)
! Close FORTRAN interface.
CALL h5close_f(error)
deallocate(buff_r)
END PROGRAM H5_RDWT
ian#ian-pc:~/test/stack$ h5fc hdf5.f90
ian#ian-pc:~/test/stack$ ./a.out
ian#ian-pc:~/test/stack$ ls -l out.h5
-rw-rw-r-- 1 ian ian 33556576 Mar 11 10:29 out.h5
ian#ian-pc:~/test/stack$ ncdump out.h5 | head
netcdf out {
dimensions:
phony_dim_0 = 256 ;
phony_dim_1 = 128 ;
variables:
float vort(phony_dim_0, phony_dim_1, phony_dim_0) ;
data:
vort =
0.9975595, 0.5668247, 0.9659153, 0.7479277, 0.3673909, 0.4806369,
ian#ian-pc:~/test/stack$
So why do you think there is a problem?
For safe reasons I would recommend you to disassemble matrices into vector form and store them as 1D datasets in HDF5 file. Then, while reading assemble them in the same manner. Use H5SSELECT_HYPERSLAB_F for writing/reading slices of your matrix.