From: Hongyi on
Hi all,

Hi all,

Sorry for bother again, this time, I attach the fortran binary data
file I want to read use the fortran code.

I've a unformatted binary file named Si.cst_esp which include the
data as follows if these data have been read out into a regular format
file:

---------------
1 1 1 4.185302
2 1 1 2.377237
3 1 1 -0.053069
4 1 1 -1.013479
5 1 1 -0.667829
6 1 1 -0.400710
7 1 1 -0.315664
8 1 1 -0.288375
9 1 1 -0.285014
10 1 1 -0.316897
11 1 1 -0.397220
12 1 1 -0.669933
13 1 1 -1.011012
14 1 1 -0.067510
15 1 1 2.375270
1 2 1 2.377237
2 2 1 0.447560
3 2 1 -0.900999
4 2 1 -0.886866
5 2 1 -0.497244
6 2 1 -0.447079
7 2 1 -0.391529
8 2 1 -0.439345
9 2 1 -0.398312
10 2 1 -0.445836
11 2 1 -0.488855
12 2 1 -0.895775
13 2 1 -0.894621
14 2 1 0.433900
15 2 1 2.332441
... ... ... ...
15 15 15 -0.716868
---------------

I use the following fortran code to read the binary out:

----------------------
program average_esp

implicit none

integer, parameter::dp=kind(1.0d0)

complex(kind=dp), dimension(:,:,:), allocatable::esp
complex(kind=dp), dimension(:), allocatable::esp_col
integer::nspins,ngx,ngy,ngz
integer::i,j,k,nx,ny
character(len=50)::fname

write(*,*)'ESP file name?'
read(*,*)fname
open(unit=10,file=trim(fname),status='old',form='unformatted')
read(10)nspins
read(10)ngx,ngy,ngz

write(*,*)'ESP grid size=',ngx,ngy,ngz

allocate(esp(ngx,ngy,ngz))
allocate(esp_col(ngz))

do i=1,ngx*ngy
read(10)nx,ny,esp_col
esp(nx,ny,:)=esp_col
end do

close(10)

open(unit=10,file=trim(fname)//'.formatted',status='unknown')
do i=1,ngx
do j=1,ngy
do k=1,ngz
write(10,'(3i6,f20.8)')i,j,k,real(esp(i,j,k),dp)
end do
end do
end do
close(10)
write(*,*)'Written potential to ',trim(fname)//'.formatted'

deallocate(esp)
deallocate(esp_col)

stop

end program average_esp
----------------------

I compile the following code with gfortan and then read the Si.cst_esp
with the compiled utility, but I meet the following errors:

------------------
At line 18 of file read_cst_esp.f90 (unit = 10, file = 'Si.cst_esp')
Fortran runtime error: End of file
--------------------

Who can give me some hints?

Sincerely yours,

--------------
hongyi.zhao
2008-07-06
From: Arjen Markus on
On 6 jul, 12:49, Hongyi <hongyi.z...(a)gmail.com> wrote:
> Hi all,
>
> Hi all,
>
> Sorry for bother again, this time, I attach the fortran binary data
> file I want to read use the fortran code.
>
> I've a  unformatted binary file named Si.cst_esp which include the
> data as follows if these data have been read out into a regular format
> file:
>
> ---------------
>      1     1     1            4.185302
>      2     1     1            2.377237
>      3     1     1           -0.053069
>      4     1     1           -1.013479
>      5     1     1           -0.667829
>      6     1     1           -0.400710
>      7     1     1           -0.315664
>      8     1     1           -0.288375
>      9     1     1           -0.285014
>     10     1     1           -0.316897
>     11     1     1           -0.397220
>     12     1     1           -0.669933
>     13     1     1           -1.011012
>     14     1     1           -0.067510
>     15     1     1            2.375270
>      1     2     1            2.377237
>      2     2     1            0.447560
>      3     2     1           -0.900999
>      4     2     1           -0.886866
>      5     2     1           -0.497244
>      6     2     1           -0.447079
>      7     2     1           -0.391529
>      8     2     1           -0.439345
>      9     2     1           -0.398312
>     10     2     1           -0.445836
>     11     2     1           -0.488855
>     12     2     1           -0.895775
>     13     2     1           -0.894621
>     14     2     1            0.433900
>     15     2     1            2.332441
>     ...    ...    ...           ...
>     15    15    15         -0.716868
> ---------------
>
> I use the following fortran code to read the binary out:
>
> ----------------------
> program average_esp
>
>   implicit none
>
>   integer, parameter::dp=kind(1.0d0)
>
>   complex(kind=dp), dimension(:,:,:), allocatable::esp
>   complex(kind=dp), dimension(:), allocatable::esp_col
>   integer::nspins,ngx,ngy,ngz
>   integer::i,j,k,nx,ny
>   character(len=50)::fname
>
>   write(*,*)'ESP file name?'
>   read(*,*)fname
>   open(unit=10,file=trim(fname),status='old',form='unformatted')
>   read(10)nspins
>   read(10)ngx,ngy,ngz
>
>   write(*,*)'ESP grid size=',ngx,ngy,ngz
>
>   allocate(esp(ngx,ngy,ngz))
>   allocate(esp_col(ngz))
>
>   do i=1,ngx*ngy
>     read(10)nx,ny,esp_col
>     esp(nx,ny,:)=esp_col
>   end do
>
>   close(10)
>
>   open(unit=10,file=trim(fname)//'.formatted',status='unknown')
>   do i=1,ngx
>     do j=1,ngy
>       do k=1,ngz
>          write(10,'(3i6,f20.8)')i,j,k,real(esp(i,j,k),dp)
>       end do
>     end do
>   end do
>   close(10)
>   write(*,*)'Written potential to ',trim(fname)//'.formatted'
>
>   deallocate(esp)
>   deallocate(esp_col)
>
>   stop
>
> end program average_esp
> ----------------------
>
> I compile the following code with gfortan and then read the Si.cst_esp
> with the compiled utility,  but I meet the following errors:
>
> ------------------
> At line 18 of file read_cst_esp.f90 (unit = 10, file = 'Si.cst_esp')
> Fortran runtime error: End of file
> --------------------
>
> Who can give me some hints?
>
> Sincerely yours,
>
> --------------
> hongyi.zhao
> 2008-07-06

Hello,

First of all: line 18 seems to be the empty line after read(10) ngx,
ngy, ngz.
I will assume that it tries to read the second record though.

Can you be sure that the unformatted file contains as the
first two records, one that contains a single integer, the second
three integers?

According to the message, the program finds an end of file - so no
second record.

Could it be that everything is contained on a single record?

Regards,

Arjen
From: Arjen Markus on
For the record:

Hongyi contacted me personally, and the unformatted file turns out to
be
big-endian. So, use gfortran's flag -fconvert=big-endian to compile
the program.

Regards,

Arjen
From: glen herrmannsfeldt on
Hongyi wrote:

> Sorry for bother again, this time, I attach the fortran binary data
> file I want to read use the fortran code.

> I've a unformatted binary file named Si.cst_esp which include the
> data as follows if these data have been read out into a regular format

Show at least the WRITE statements of the program that wrote
the unformatted file that you want to read. Without that,
it is not possible to say how to read it.

Remember that UNFORMATTED I/O is record oriented.
(So is FORMATTED, but you didn't ask about that.)

-- glen

From: Terence on
On Jul 19, 10:42 am, glen herrmannsfeldt <g...(a)ugcs.caltech.edu>
wrote:

> Remember that UNFORMATTED I/O is record oriented.
> (So is FORMATTED, but you didn't ask about that.)

NO!
UNFORMMATED I/O is not necesarily record oriented.
It can be so if DIRECT access is used and a record length specified in
bytes (or 4-byte words), or if you read it sequentially in fixed
blocks, (which I would often do for buffering reasons when I process
unknown data by bytes).

UNFORMATTED information is simply data not translated from its
internal bit form, whatever that might be (and which is processor
dependenta anyway) the only "marker" is the end-of-file signal from
the I/O system.