Read the fortran unformatted binary data. 
Author Message
 Read the fortran unformatted binary data.

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



Thu, 23 Dec 2010 18:49:54 GMT  
 Read the fortran unformatted binary data.

Quote:
> 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



Thu, 23 Dec 2010 19:14:47 GMT  
 Read the fortran unformatted binary data.
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



Thu, 23 Dec 2010 20:49:11 GMT  
 Read the fortran unformatted binary data.

Quote:

> 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



Wed, 05 Jan 2011 08:42:13 GMT  
 Read the fortran unformatted binary data.


Quote:
> 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.



Thu, 06 Jan 2011 07:52:35 GMT  
 Read the fortran unformatted binary data.

Quote:



>> 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.

So records have to be of equal length with no begin- or end-record markers or else they're
not records?

What if I read a unformatted, direct access files using 2x (or some other factor of the
file size) the record length to write it? I could still read each of "my" records. Who's
record is right?

I don't know if the official term is "record", but unformatted, sequential files seem more
record-oriented that direct files to me - every record (I don't what else to call
them...i/o access?) has markers at the beginning and end (indicating their length). The
records can be of varying length, but how does make them less record-y?

cheers,

paulv

p.s. And, yes, I realise the format (no pun intended) of unformatted sequential files is
not standardised.



Fri, 07 Jan 2011 21:11:49 GMT  
 Read the fortran unformatted binary data.

Quote:



> >> 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.

> So records have to be of equal length with no begin- or end-record markers
> or else they're not records?
...
> I don't know if the official term is "record"...

I hadn't bothered to respond before, even though this was a bit of a
confused jumble of bits that were correct and those that were not. But
since it seems to be causing confusion...

Terence is correct in saying that unformatted I/O is not necessarily
record oriented and that unformatted information is simply data not
translated from its internal bit form. If it had stopped there, I'd have
been happy with it as an accurate, if a bit brief, comment. (Admitedly,
I'm in no position to criticise others for going on for too long. :-)
But then it gets confused when talking about the end-of-file, which has
nothing much to do with the matter. And as you note, there seems to be
some personal notion of a record as being fixed size; that notion has
little in common with the Fortran standard's definition of a record.

Yes, "record" is the "official" term used in the Fortran standard (along
with pretty much any book on the language). In a quick scan, I can't
find it modified with "oriented", but that seems an obious enough
application of normal English. The f2003 standard uses the term "record
file". Both sequential and direct access imply a record file. From f2003
9.2.2.1 and 9.2.2.2

  "Sequential access is a method of accessing the records of an external
file in order."

  "Direct access is a method of accessing the records of an external
record file in arbitrary order."

Neither of these definitions have anything to do with whether the
records are fixed size or not.

The alternative to record I/O is referred to in the Fortran standard as
stream I/O. Nonstandard pre-f2003 variants of this are sometimes
referred to using other terms, such as "transparent" or "binary" (though
I can't resist mentioning my dislike of this usage of "binary" as having
nothing to do with base 2, which is, after all, what "binary" means).

The notion of formatted versus unformatted is orthogonal to that of
record versus stream. All four combinations exist - six combinations if
you add the distinction of sequential versus direct access.

--
Richard Maine                    | Good judgement comes from experience;
email: last name at domain . net | experience comes from bad judgement.
domain: summertriangle           |  -- Mark Twain



Sat, 08 Jan 2011 00:06:44 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. Reading fortran 'unformatted' binary files

2. Reading MS-FORTRAN unformatted binary files *efficiently*

3. Reading unformatted FORTRAN binary using C

4. Reading Fortran generated unformatted binary files in tcl

5. BINARY and UNFORMATTED - comp.lang.fortran #9859 - comp.lang.fortran #9884 (fwd)

6. Fortran/C code to read LV binary data?

7. Using DEC Fortran to read raw binary data?

8. Problems reading Fortran binary data files

9. help reading fortran generated binary data...

10. Reading a binary data set created by SAS into FORTRAN

11. unformatted vs binary data

12. Reading any binary file as direct-access unformatted

 

 
Powered by phpBB® Forum Software