writing fortran equivalent binary file using python

S

Sudheer Joseph

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 modeflexibility 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
 
D

David Froger

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)
 
O

Oscar Benjamin

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
 
S

Sudheer Joseph

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


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: (e-mail address removed); (e-mail address removed).
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,071
Latest member
MetabolicSolutionsKeto

Latest Threads

Top