Re: Read/write binary files in Linux and Windows



Richard Maine wrote:

My guess is that the answer lies somewhere that you have not shown us. I
doubt that this has anything to do with inquire. My first guess would be
that the compiler is correct and that the shape isn't what you think it
is. Hard for me to tell more, though. In asking for debugging help, it
is generally useful to show complete code. Odds are high that the reason
you can't find the problem is that you are looking in the wrong place,
so showing us just the parts that you are looking at isn't going to be
enough.

Here is my test code:

! orininal http://www.atmos.washington.edu/~salathe/osx_unix/endian.html
!
program binary
call writeBin
call readBin
call endian
end program binary

subroutine endian
! checks if this is a little endian machine
! returns litend=.true. if it is, litend=.false. if not
integer(1) j(2)
integer(2) i
equivalence (i,j)
logical litend
i = 1
if (j(1) == 1) then
litend = .true.
else
litend = .false.
end if
print *, 'Is this PC a ---little endian machine---?', litend
end subroutine endian

subroutine writeBin
integer i,j, d, len1, len2, len3
integer, parameter :: m=5, n=5, t=2
type fieldDay
! Meteorological inputs
real(4), dimension(:,:), pointer :: pre
end type fieldDay
type(fieldDay), dimension(:), allocatable, target :: x
real(4), dimension(:), pointer :: y
real(4) A(m,n)
integer vec(2)
!
allocate (x(t))
do d=1, t
allocate (x(d)%pre(m,n))
end do
! fill A
A=3.0
!insert some code to fill x
do d=1,t
do j=1,n
do i=1,m
x(d)%pre(i,j)=float(2*d)*float(i)
end do
end do
end do
!open direct access unformatted file with records sized for the whole array
!
inquire (IOLENGTH=len1) A
inquire (IOLENGTH=len2) x(1)%pre(1:5,1:5)
inquire (IOLENGTH=len3) x(1)%pre
!
vec = shape(x(1)%pre)
print *, 'len1= ', len1
print *, 'len2= ', len2
print *, 'len3= ', len3
print *, 'shape ', vec
!
open (unit=1, &
file='out.r4', &
form='unformatted', &
access='direct', &
recl=m*n*4) ! compiler used RECL in bytes
!recl =len2)
!write the whole thing as one record
do d=1,t
write (1,rec=d) x(d)%pre
end do
close(1)
print*, 'end writing'
do d=1,t
print*, 'P written', d
do i=1,m
print '(<n>f5.1)', (x(d)%pre(i,j), j=1,n)
end do
end do
! ckeck
y=> x(2)%pre(:,2)
print*, ' Ckeck: col 2: dot_prod (y,y) :', dot_product(y,y)
end subroutine writeBin

subroutine readBin
integer i,j, d
integer, parameter :: m=5, n=5, t=2
real(4), dimension(:), pointer :: y
type fieldDay
! Meteorological inputs
real(4), dimension(:,:), pointer :: pre
end type fieldDay
type(fieldDay), dimension(:), allocatable, target :: x
!
allocate (x(t))
do d=1, t
allocate (x(d)%pre(m,n))
end do
open (unit=1, &
file='out.r4',&
form='unformatted',&
access='direct',&
recl=m*n*4)
!read in as a single record /day
do d=1,t
read (1,rec=d) x(d)%pre
end do
close(1)
do d=1,t
print*, 'P read', d
do i=1,m
print '(<n>f6.1)', (x(d)%pre(i,j), j=1,n)
end do
end do
print*, 'end reading'
! ckeck
y=> x(2)%pre(:,2)
print*, ' Ckeck: col 2: dot_prod (y,y) :', dot_product(y,y)
end subroutine readBin

CVF 6.6 produces the following answer:

len1= 100
len2= 100
len3= 35068824
shape 5 5
end writing

.... the rest is correct.

len3 is wrong. The shape of x is correct.

in Linux all answers are correct. Where lies the problem?

--
Dr. Luis Samaniego
Department Computational Hydrosystems (CHS)
Helmholtz Centre for Environmental Research - UFZ
Permoserstraße 15 / 04318 Leipzig / Germany

luis.samaniego@xxxxxx / http://www.ufz.de
phone +49 341 235 3971 / fax 49 341 235 3939

+++++++++++++++++++++++++++++++++++++++++++++++++

The UFZ has a new name:
Helmholtz Centre for Environmental Research - UFZ

+++++++++++++++++++++++++++++++++++++++++++++++++

Helmholtz-Zentrum für Umweltforschung GmbH - UFZ
Registered Office: Leipzig
Registration Office: Amtsgericht Leipzig
Trade Register Nr. B 4703
Chairman of the Supervisory Board: MinDirig Hartmut F. Grübel
Scientific Director: Prof. Dr. Georg Teutsch
Administrative Director: Dr. Andreas Schmidt
.



Relevant Pages