Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #59370 > unrolled thread

writing fortran equivalent binary file using python

Started bySudheer Joseph <sjo.india@gmail.com>
First post2013-11-13 16:53 -0800
Last post2013-11-14 15:46 +0100
Articles 5 — 4 participants

Back to article view | Back to comp.lang.python


Contents

  writing fortran equivalent binary file using python Sudheer Joseph <sjo.india@gmail.com> - 2013-11-13 16:53 -0800
    Re: writing fortran equivalent binary file using python David Froger <david.froger@inria.fr> - 2013-11-14 07:57 +0100
    Re: writing fortran equivalent binary file using python Oscar Benjamin <oscar.j.benjamin@gmail.com> - 2013-11-14 14:18 +0000
    Re: writing fortran equivalent binary file using python Sudheer Joseph <sjo.india@gmail.com> - 2013-11-14 20:05 +0530
    Re: writing fortran equivalent binary file using python Antoon Pardon <antoon.pardon@rece.vub.ac.be> - 2013-11-14 15:46 +0100

#59370 — writing fortran equivalent binary file using python

FromSudheer Joseph <sjo.india@gmail.com>
Date2013-11-13 16:53 -0800
Subjectwriting fortran equivalent binary file using python
Message-ID<ac6f6659-0053-482c-b587-454066a4a515@googlegroups.com>
Hi,
         I need to write a binary file exactly as written by fortran code below to be read by another code which is part of a model which is not advisable to edit.I would like to use python for this purpose as python has mode flexibility and easy coding methods.
  
  character(40) :: TITLE="122322242"
  integer :: IWI,JWI
  real :: XFIN,YFIN,DXIN=0.5,DYIN=0.5,WDAY(6000)
  XFIN=0.0,YFIN=-90.0,NREC=1461,DXIN=0.5;DYIN=0.5;IWI=720;JWI=361 
  real,allocatable,dimension(:,:,:) :: VAR1_VAL
  real,allocatable,dimension(:,:,:) :: VAR2_VAL

  open(11,file=outf,form='UNFORMATTED')
  WRITE(11) TITLE
  WRITE(11) NX,NY,XFIN,YFIN,DXIN,DYIN,NREC,WDAY
  write(*,'(A10,2f10.3)') "START=",VAR1_VAL(1,1,1),VAR2_VAL(1,1,1)
  write(*,'(A10,2f10.3)') "END=",VAR1_VAL(nx,ny,nrec),VAR2_VAL(nx,ny,nrec)
  do i=1,NREC
  WRITE(11) VAR1_VAL(:,:,i),VAR2_VAL(:,:,i)
  WRITE(*,'(2I10,f10.3)') NX,NY,WDAY(i)
  enddo

My trial code with Python (data is read from file here)

from netCDF4 import Dataset as nc
import numpy as np
XFIN=0.0,YFIN=-90.0,NREC=1461,DXIN=0.5;DYIN=0.5
TITLE="NCMRWF 6HOURLY FORCING MKS"
nf=nc('ncmrwf_uv.nc')
ncv=nf.variables.keys()
IWI=len(nf.variables[ncv[0]])
JWI=len(nf.variables[ncv[1]])
WDAY=nf.varlables[ncv[2]][0:NREC]
U=nf.variables[ncv[3]][0:NREC,:,:]
V=nf.variables[ncv[4]][0:NREC,:,:]
bf=open('ncmrwf_uv.bin',"wb")
f.write(TITLE)
f.write(IWI,JWI,XFIN,YFIN,DXIN,DYIN,NREC,WDAY)
for i in np.arange(0,NREC):
    f.write(U[i,:,:],V[i,:,:])
f.close()

But the issue is that f.write do not allow multiple values( it allows one by one so throws an error with above code ) on same write statement like in the fortran code. experts may please advice if there a solution for this?

with best regards,
Sudheer

[toc] | [next] | [standalone]


#59416

FromDavid Froger <david.froger@inria.fr>
Date2013-11-14 07:57 +0100
Message-ID<mailman.2585.1384415276.18130.python-list@python.org>
In reply to#59370
Hi Sudheer,

Fortan binary format is not portable, and it's hard to work with it
with different langages, compilers, architectures...

In you're Python code, you also use NetCDF, which solve all these problems.

I would suggest to use only NetCDF files in both Python and Fortran code. (So
never use the buildit open and write of Python and Fortran, but always the
NetCDF library).

Hope it helps,
David

Quoting Sudheer Joseph (2013-11-14 01:53:42)
> Hi,
>          I need to write a binary file exactly as written by fortran code below to be read by another code which is part of a model which is not advisable to edit.I would like to use python for this purpose as python has mode flexibility and easy coding methods.
>   
>   character(40) :: TITLE="122322242"
>   integer :: IWI,JWI
>   real :: XFIN,YFIN,DXIN=0.5,DYIN=0.5,WDAY(6000)
>   XFIN=0.0,YFIN=-90.0,NREC=1461,DXIN=0.5;DYIN=0.5;IWI=720;JWI=361 
>   real,allocatable,dimension(:,:,:) :: VAR1_VAL
>   real,allocatable,dimension(:,:,:) :: VAR2_VAL
> 
>   open(11,file=outf,form='UNFORMATTED')
>   WRITE(11) TITLE
>   WRITE(11) NX,NY,XFIN,YFIN,DXIN,DYIN,NREC,WDAY
>   write(*,'(A10,2f10.3)') "START=",VAR1_VAL(1,1,1),VAR2_VAL(1,1,1)
>   write(*,'(A10,2f10.3)') "END=",VAR1_VAL(nx,ny,nrec),VAR2_VAL(nx,ny,nrec)
>   do i=1,NREC
>   WRITE(11) VAR1_VAL(:,:,i),VAR2_VAL(:,:,i)
>   WRITE(*,'(2I10,f10.3)') NX,NY,WDAY(i)
>   enddo
> 
> My trial code with Python (data is read from file here)
> 
> from netCDF4 import Dataset as nc
> import numpy as np
> XFIN=0.0,YFIN=-90.0,NREC=1461,DXIN=0.5;DYIN=0.5
> TITLE="NCMRWF 6HOURLY FORCING MKS"
> nf=nc('ncmrwf_uv.nc')
> ncv=nf.variables.keys()
> IWI=len(nf.variables[ncv[0]])
> JWI=len(nf.variables[ncv[1]])
> WDAY=nf.varlables[ncv[2]][0:NREC]
> U=nf.variables[ncv[3]][0:NREC,:,:]
> V=nf.variables[ncv[4]][0:NREC,:,:]
> bf=open('ncmrwf_uv.bin',"wb")
> f.write(TITLE)
> f.write(IWI,JWI,XFIN,YFIN,DXIN,DYIN,NREC,WDAY)
> for i in np.arange(0,NREC):
>     f.write(U[i,:,:],V[i,:,:])
> f.close()
> 
> But the issue is that f.write do not allow multiple values( it allows one by one so throws an error with above code ) on same write statement like in the fortran code. experts may please advice if there a solution for this?
> 
> with best regards,
> Sudheer
> -- 
> https://mail.python.org/mailman/listinfo/python-list

[toc] | [prev] | [next] | [standalone]


#59436

FromOscar Benjamin <oscar.j.benjamin@gmail.com>
Date2013-11-14 14:18 +0000
Message-ID<mailman.2595.1384438765.18130.python-list@python.org>
In reply to#59370
On 14 November 2013 00:53, Sudheer Joseph <sjo.india@gmail.com> wrote:
> My trial code with Python (data is read from file here)
>
> from netCDF4 import Dataset as nc
> import numpy as np
> XFIN=0.0,YFIN=-90.0,NREC=1461,DXIN=0.5;DYIN=0.5
> TITLE="NCMRWF 6HOURLY FORCING MKS"
> nf=nc('ncmrwf_uv.nc')
> ncv=nf.variables.keys()
> IWI=len(nf.variables[ncv[0]])
> JWI=len(nf.variables[ncv[1]])
> WDAY=nf.varlables[ncv[2]][0:NREC]
> U=nf.variables[ncv[3]][0:NREC,:,:]
> V=nf.variables[ncv[4]][0:NREC,:,:]
> bf=open('ncmrwf_uv.bin',"wb")
> f.write(TITLE)
> f.write(IWI,JWI,XFIN,YFIN,DXIN,DYIN,NREC,WDAY)
> for i in np.arange(0,NREC):
>     f.write(U[i,:,:],V[i,:,:])
> f.close()
>
> But the issue is that f.write do not allow multiple values( it allows one by one so throws an error with above code ) on same write statement like in the fortran code. experts may please advice if there a solution for this?

Can you just call write twice? e.g.:

f.write(U[i,:,:])
f.write(V[i,:,:])


Oscar

[toc] | [prev] | [next] | [standalone]


#59442

FromSudheer Joseph <sjo.india@gmail.com>
Date2013-11-14 20:05 +0530
Message-ID<mailman.2601.1384439747.18130.python-list@python.org>
In reply to#59370

[Multipart message — attachments visible in raw view] — view raw

Thank you,
                      But it wont allow to write it in unformatted way so
that the fortran code can read

with

open(11,file="input.bin")
read(11) IWI,JWI,XFIN,YFIN,DXIN,DYIN,NREC,WDAY

with best regards,
sudheer


On Thu, Nov 14, 2013 at 7:48 PM, Oscar Benjamin
<oscar.j.benjamin@gmail.com>wrote:

> On 14 November 2013 00:53, Sudheer Joseph <sjo.india@gmail.com> wrote:
> > My trial code with Python (data is read from file here)
> >
> > from netCDF4 import Dataset as nc
> > import numpy as np
> > XFIN=0.0,YFIN=-90.0,NREC=1461,DXIN=0.5;DYIN=0.5
> > TITLE="NCMRWF 6HOURLY FORCING MKS"
> > nf=nc('ncmrwf_uv.nc')
> > ncv=nf.variables.keys()
> > IWI=len(nf.variables[ncv[0]])
> > JWI=len(nf.variables[ncv[1]])
> > WDAY=nf.varlables[ncv[2]][0:NREC]
> > U=nf.variables[ncv[3]][0:NREC,:,:]
> > V=nf.variables[ncv[4]][0:NREC,:,:]
> > bf=open('ncmrwf_uv.bin',"wb")
> > f.write(TITLE)
> > f.write(IWI,JWI,XFIN,YFIN,DXIN,DYIN,NREC,WDAY)
> > for i in np.arange(0,NREC):
> >     f.write(U[i,:,:],V[i,:,:])
> > f.close()
> >
> > But the issue is that f.write do not allow multiple values( it allows
> one by one so throws an error with above code ) on same write statement
> like in the fortran code. experts may please advice if there a solution for
> this?
>
> Can you just call write twice? e.g.:
>
> f.write(U[i,:,:])
> f.write(V[i,:,:])
>
>
> Oscar
>



-- 
with best regards

Sudheer

**********************************************************************************
Dr. Sudheer Joseph

Scientist

INDIAN NATIONAL CENTRE FOR OCEAN INFORMATION SERVICES (INCOIS)
MINISTRY OF EARTH SCIENCES, GOVERNMENT OF INDIA
"OCEAN VALLEY" PRAGATHI NAGAR (BO)
OPP.JNTU, NIZAMPET SO
Andhra Pradesh, India. PIN- 500 090.
TEl:+91-40-23044600(R),Tel:+91-9440832534(Mobile)
Tel:+91-40-23886047(O),Fax:+91-40-23892910(O)
E-mail: sudheer.joseph@yahoo.com;  sjo@incois.gov.in.
Web- http://oppamthadathil.tripod.com
           --------------* ---------------
"The ultimate measure of a man is
not where he stands in moments of
comfort and convenience, but where
he stands at times of challenge and
controversy."
                        Martin Luther King, Jr.
"What we have done for ourselves alone dies with us.
What we have done for others and the world remains and is immortal."
- Albert Pines

[toc] | [prev] | [next] | [standalone]


#59443

FromAntoon Pardon <antoon.pardon@rece.vub.ac.be>
Date2013-11-14 15:46 +0100
Message-ID<mailman.2602.1384440413.18130.python-list@python.org>
In reply to#59370
Op 14-11-13 01:53, Sudheer Joseph schreef:
> Hi,
>          I need to write a binary file exactly as written by fortran code below to be read by another code which is part of a model which is not advisable to edit.I would like to use python for this purpose as python has mode flexibility and easy coding methods.
>   
>   character(40) :: TITLE="122322242"
>   integer :: IWI,JWI
>   real :: XFIN,YFIN,DXIN=0.5,DYIN=0.5,WDAY(6000)
>   XFIN=0.0,YFIN=-90.0,NREC=1461,DXIN=0.5;DYIN=0.5;IWI=720;JWI=361 
>   real,allocatable,dimension(:,:,:) :: VAR1_VAL
>   real,allocatable,dimension(:,:,:) :: VAR2_VAL
> 
>   open(11,file=outf,form='UNFORMATTED')
>   WRITE(11) TITLE
>   WRITE(11) NX,NY,XFIN,YFIN,DXIN,DYIN,NREC,WDAY
>   write(*,'(A10,2f10.3)') "START=",VAR1_VAL(1,1,1),VAR2_VAL(1,1,1)
>   write(*,'(A10,2f10.3)') "END=",VAR1_VAL(nx,ny,nrec),VAR2_VAL(nx,ny,nrec)
>   do i=1,NREC
>   WRITE(11) VAR1_VAL(:,:,i),VAR2_VAL(:,:,i)
>   WRITE(*,'(2I10,f10.3)') NX,NY,WDAY(i)
>   enddo
> 
> My trial code with Python (data is read from file here)
> 
> from netCDF4 import Dataset as nc
> import numpy as np
> XFIN=0.0,YFIN=-90.0,NREC=1461,DXIN=0.5;DYIN=0.5
> TITLE="NCMRWF 6HOURLY FORCING MKS"
> nf=nc('ncmrwf_uv.nc')
> ncv=nf.variables.keys()
> IWI=len(nf.variables[ncv[0]])
> JWI=len(nf.variables[ncv[1]])
> WDAY=nf.varlables[ncv[2]][0:NREC]
> U=nf.variables[ncv[3]][0:NREC,:,:]
> V=nf.variables[ncv[4]][0:NREC,:,:]
> bf=open('ncmrwf_uv.bin',"wb")
> f.write(TITLE)
> f.write(IWI,JWI,XFIN,YFIN,DXIN,DYIN,NREC,WDAY)
> for i in np.arange(0,NREC):
>     f.write(U[i,:,:],V[i,:,:])
> f.close()
> 
> But the issue is that f.write do not allow multiple values( it allows > one by one so throws an error with above code ) on same write statement
> like in the fortran code. experts may please advice if there a solution for this?

That is not the main issue. The python write only works with bytes or
strings, not with floats or ints. So you will have to convert your
numbers using struct. So the question is if you know the byte layout
fortran uses and if you can replicate it with the struct module.

-- 
Antoon Pardon

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web