Path: csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!news.tele.dk!feed118.news.tele.dk!news.tele.dk!small.news.tele.dk!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.010 X-Spam-Evidence: '*H*': 0.98; '*S*': 0.00; '(so': 0.07; 'binary': 0.07; 'skip:u 30': 0.07; 'subject:file': 0.07; 'f.close()': 0.09; 'forcing': 0.09; 'subject:using': 0.09; 'throws': 0.09; 'python': 0.11; 'suggest': 0.14; '(data': 0.16; 'library).': 0.16; 'numpy': 0.16; 'portable,': 0.16; 'subject:writing': 0.16; 'subject:python': 0.16; 'flexibility': 0.16; 'code.': 0.18; 'written': 0.21; 'code,': 0.22; 'import': 0.22; 'coding': 0.22; 'this?': 0.23; 'header:User-Agent:1': 0.23; 'error': 0.23; 'integer': 0.24; 'skip:" 40': 0.26; 'skip:v 30': 0.26; 'header:In- Reply-To:1': 0.27; 'mode': 0.30; 'statement': 0.30; 'url:mailman': 0.30; 'code': 0.31; 'allows': 0.31; 'file': 0.32; 'another': 0.32; 'open': 0.33; 'url:python': 0.33; 'received:fr': 0.33; 'advice': 0.35; 'but': 0.35; 'there': 0.35; 'skip:f 40': 0.36; 'skip:j 20': 0.36; 'url:listinfo': 0.36; 'hi,': 0.36; 'url:org': 0.36; 'to:addr :python-list': 0.38; 'files': 0.38; 'issue': 0.38; 'to:addr:python.org': 0.39; 'url:mail': 0.40; 'read': 0.60; 'easy': 0.60; 'experts': 0.60; 'problems.': 0.60; 'skip:n 30': 0.60; 'solve': 0.60; 'hope': 0.61; 'skip:o 30': 0.61; "you're": 0.61; 'real': 0.63; 'skip:n 10': 0.64; 'different': 0.65; 'skip:w 30': 0.69; 'skip:r 30': 0.69; 'trial': 0.83; 'compilers,': 0.84; 'here)': 0.84 X-IronPort-AV: E=Sophos;i="4.93,697,1378850400"; d="scan'208";a="42667075" Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable From: David Froger User-Agent: alot/0.3.5 X-Sender-Computer: INRIA-ROCQ-UBUNTU-fl-58186.rocq.inria.fr To: python-list@python.org References: In-Reply-To: Subject: Re: writing fortran equivalent binary file using python Date: Thu, 14 Nov 2013 07:57:39 +0100 X-Mailman-Approved-At: Thu, 14 Nov 2013 08:47:54 +0100 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 77 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1384415276 news.xs4all.nl 15934 [2001:888:2000:d::a6]:48751 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:59416 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 adv= isable to edit.I would like to use python for this purpose as python has mo= de flexibility and easy coding methods. > = > character(40) :: TITLE=3D"122322242" > integer :: IWI,JWI > real :: XFIN,YFIN,DXIN=3D0.5,DYIN=3D0.5,WDAY(6000) > XFIN=3D0.0,YFIN=3D-90.0,NREC=3D1461,DXIN=3D0.5;DYIN=3D0.5;IWI=3D720;JWI= =3D361 = > real,allocatable,dimension(:,:,:) :: VAR1_VAL > real,allocatable,dimension(:,:,:) :: VAR2_VAL > = > open(11,file=3Doutf,form=3D'UNFORMATTED') > WRITE(11) TITLE > WRITE(11) NX,NY,XFIN,YFIN,DXIN,DYIN,NREC,WDAY > write(*,'(A10,2f10.3)') "START=3D",VAR1_VAL(1,1,1),VAR2_VAL(1,1,1) > write(*,'(A10,2f10.3)') "END=3D",VAR1_VAL(nx,ny,nrec),VAR2_VAL(nx,ny,nr= ec) > do i=3D1,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=3D0.0,YFIN=3D-90.0,NREC=3D1461,DXIN=3D0.5;DYIN=3D0.5 > TITLE=3D"NCMRWF 6HOURLY FORCING MKS" > nf=3Dnc('ncmrwf_uv.nc') > ncv=3Dnf.variables.keys() > IWI=3Dlen(nf.variables[ncv[0]]) > JWI=3Dlen(nf.variables[ncv[1]]) > WDAY=3Dnf.varlables[ncv[2]][0:NREC] > U=3Dnf.variables[ncv[3]][0:NREC,:,:] > V=3Dnf.variables[ncv[4]][0:NREC,:,:] > bf=3Dopen('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 i= n 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