Path: csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!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.011 X-Spam-Evidence: '*H*': 0.98; '*S*': 0.00; 'received:134': 0.05; '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; '(data': 0.16; 'numpy': 0.16; 'subject:writing': 0.16; 'subject:python': 0.16; 'flexibility': 0.16; 'code.': 0.18; 'written': 0.21; 'import': 0.22; 'coding': 0.22; 'issue.': 0.22; 'this?': 0.23; 'header:User-Agent:1': 0.23; 'error': 0.23; 'byte': 0.24; 'bytes': 0.24; 'integer': 0.24; 'question': 0.24; 'skip:" 40': 0.26; 'skip:v 30': 0.26; 'header :In-Reply-To:1': 0.27; 'mode': 0.30; 'statement': 0.30; 'code': 0.31; 'struct': 0.31; 'allows': 0.31; 'file': 0.32; 'another': 0.32; 'advice': 0.35; 'convert': 0.35; 'but': 0.35; 'there': 0.35; 'module.': 0.36; 'skip:f 40': 0.36; 'skip:j 20': 0.36; 'hi,': 0.36; 'to:addr:python-list': 0.38; 'issue': 0.38; 'to:addr:python.org': 0.39; 'read': 0.60; 'easy': 0.60; 'experts': 0.60; 'skip:n 30': 0.60; 'skip:o 30': 0.61; 'numbers': 0.61; 'real': 0.63; 'skip:n 10': 0.64; 'skip:w 30': 0.69; 'skip:r 30': 0.69; 'trial': 0.83; 'here)': 0.84; 'pardon': 0.84; 'replicate': 0.84; 'struct.': 0.84 X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: Ap8EAIHhhFKGuA9G/2dsb2JhbABZAcA3gnyBNYMaAQVrDRELIRACBA8JAwIBAgFFEwgCF4dmtzOIXo9mFhEBhAkDmBCGKotigyk Date: Thu, 14 Nov 2013 15:46:45 +0100 From: Antoon Pardon User-Agent: Mozilla/5.0 (X11; Linux i686; rv:10.0.12) Gecko/20130116 Icedove/10.0.12 MIME-Version: 1.0 To: python-list@python.org Subject: Re: writing fortran equivalent binary file using python References: In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit 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: 51 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1384440414 news.xs4all.nl 16006 [2001:888:2000:d::a6]:50345 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:59443 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