writing fortran equivalent binary file using python
david.froger at inria.fr
Thu Nov 14 07:57:39 CET 2013
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
Hope it helps,
Quoting Sudheer Joseph (2013-11-14 01:53:42)
> 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)
> real,allocatable,dimension(:,:,:) :: VAR1_VAL
> real,allocatable,dimension(:,:,:) :: VAR2_VAL
> 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)
> My trial code with Python (data is read from file here)
> from netCDF4 import Dataset as nc
> import numpy as np
> TITLE="NCMRWF 6HOURLY FORCING MKS"
> for i in np.arange(0,NREC):
> 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,
More information about the Python-list