Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.mixmin.net!hq-usenetpeers.eweka.nl!hq-usenetpeers.eweka.nl!xlned.com!feeder5.xlned.com!newsfeed.xs4all.nl!newsfeed4a.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.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; '480': 0.05; 'subject:Python': 0.06; 'binary': 0.07; 'subject:code': 0.07; 'correct,': 0.09; 'output,': 0.09; 'received:67.192': 0.09; 'received:67.192.241': 0.09; 'received:dfw.emailsrvr.com': 0.09; 'subject:into': 0.09; 'python': 0.11; "'rb')": 0.16; '*must*': 0.16; 'code?': 0.16; 'does,': 0.16; 'here.\xa0': 0.16; 'paste.': 0.16; 'skip:n 90': 0.16; 'subject:simple': 0.16; 'url:gz': 0.16; 'url:nasa': 0.16; 'do,': 0.16; 'wrote:': 0.18; 'code.': 0.18; 'meant': 0.20; 'code,': 0.22; 'saying': 0.22; 'header:User- Agent:1': 0.23; 'error': 0.23; 'received:emailsrvr.com': 0.24; 'url:gov': 0.24; 'received:(smtp server)': 0.26; 'code:': 0.26; 'skip:" 20': 0.27; 'header:In-Reply-To:1': 0.27; 'am,': 0.29; "doesn't": 0.30; 'code': 0.31; 'gary': 0.31; 'second,': 0.31; 'file': 0.32; 'probably': 0.32; 'supposed': 0.32; 'actual': 0.34; 'but': 0.35; 'there': 0.35; 'wrong': 0.37; 'two': 0.37; 'same.': 0.38; 'to:addr:python-list': 0.38; 'expect': 0.39; 'does': 0.39; 'to:addr:python.org': 0.39; 'tell': 0.60; 'first': 0.61; 'here:': 0.62; 'charset:windows-1252': 0.65; 'here': 0.66; 'results': 0.69; 'legal': 0.71; 'cut': 0.74; 'contents,': 0.84; 'absolutely': 0.87; 'examine': 0.93; 'obtained': 0.96 X-Virus-Scanned: OK X-Sender-Id: gary.herron@islandtraining.com Date: Sun, 24 Aug 2014 10:40:27 -0700 From: Gary Herron User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Thunderbird/31.0 MIME-Version: 1.0 To: python-list@python.org Subject: Re: Challenge to convert a simple IDL code into Python References: In-Reply-To: Content-Type: multipart/alternative; boundary="------------010800030801090401050106" 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: 487 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1408902408 news.xs4all.nl 2830 [2001:888:2000:d::a6]:60437 X-Complaints-To: abuse@xs4all.nl X-Received-Bytes: 30534 X-Received-Body-CRC: 4024614238 Xref: csiph.com comp.lang.python:76934 This is a multi-part message in MIME format. --------------010800030801090401050106 Content-Type: text/plain; charset=windows-1252; format=flowed Content-Transfer-Encoding: 7bit On 08/24/2014 08:38 AM, Cleo Drakos wrote: > > Here is IDL code: > > |pro read_binary_file > > file= "3B42RT.2014010318.7.bin" > > num_lon= 1440 > num_lat= 480 > > data= {header: bytarr(num_lon*2), precip: intarr(num_lon,num_lat), precip_error: intarr(num_lon,num_lat), $ > source_of_estimate: bytarr(num_lon,num_lat), precip_uncal: intarr(num_lon,num_lat)} > > close, 1 > openr, 1, file > readu, 1, data > close, 1 > precip= swap_endian(data.precip) > > print, precip > end| > > My attempt in Python is here: > > |fname= '3B42RT.2014010318.7.bin' > > num_lon= 1440 > num_lat= 480 > > with open(fname, 'rb') as fi: > contents= fi.read() > precip= struct.unpack_from('>'+str(num_lon*num_lat)+'I', contents, offset= num_lon*2) > > precip= np.array(data,dtype=int) > precip data| > > But, the results obtained from two codes above are not same. The IDL > code is correct, but what is wrong with my python code? > You are not giving us much to go on here. First that's not legal Python . The |last line was probably meant to be a print|||, but if you expect us to examine your code, you absolutely *must* give us the actual code. Please cut and paste. Second, saying it doesn't work us useless. Please tell us what the code is supposed to do, and what it actually does, what you expected... If there is output, cut and paste it. If there is an error traceback, cut and paste it. While you are at it, tell us what the IDL code does and cut and paste it's output. Gary Herron > Here is binary file I was working > with:ftp://trmmopen.gsfc.nasa.gov/pub/merged/3B42RT/3B42RT.2014010318.7.bin.gz > > > cleo > > --------------010800030801090401050106 Content-Type: text/html; charset=windows-1252 Content-Transfer-Encoding: quoted-printable
On 08/24/2014 08:38 AM, Cleo Drakos wrote:

Here is IDL code:

pro read_binary_file

file =3D "3B42RT.2014010318.7.bin"=3D 1440
num_lat =3D 480

data =3D {header: bytarr(num_=
lon*2), precip: intarr(nu=
m_lon,num_lat), precip_error: intarr(num_l=
on,num_lat), $
      source_of_estimate: bytarr(num_lon,n=
um_lat), precip_uncal: intarr(num_lon,nu=
m_lat)}

close, 1
openr, 1, file
readu, 1, data
close, 1   =20
precip =3D swap_endiandata.precip)

print=
, precip   =20
end

My attempt in Python is here:

fname =3D '3B42RT.2014010318.7.bin'<=
span class=3D"" style=3D"margin:0px;padding:0px;border:0px;vertical-align=
:baseline;background:transparent">

num_lon =3D 1440
num_lat =3D 480

with<=
span class=3D"" style=3D"margin:0px;padding:0px;border:0px;vertical-align=
:baseline;background:transparent"> open(fname, 'rb') as fi:=

    contents =3D fi.=
read()
    precip =3D struct.unpack_from('>'+str(num_lon*num_lat)+'I', contents
, offset =3D num_lon=
*2)=


    precip =3D np.array(data,=
dtype=3Dint)
    precip data

But, the results obtained from two codes above are not same. The IDL code is correct, but what is wrong with my python code?

=

You are not giving us much to go on here.=A0

First that's not legal Python .=A0 The last line was probably meant to be a print, but if you expect us to examine your code, you absolutely *must* give us the actual code.=A0 Please cut and paste.=A0

Second, saying it doesn't work us useless.=A0 Please tell us what the= code is supposed to do, and what it actually does, what you expected...=A0 If there is output, cut and paste it.=A0 If there is a= n error traceback, cut and paste it.

While you are at it, tell us what the IDL code does and cut and paste it's output.

Gary Herron





--------------010800030801090401050106--