Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.mixmin.net!newsreader4.netcologne.de!news.netcologne.de!xlned.com!feeder5.xlned.com!newsfeed.xs4all.nl!newsfeed4a.news.xs4all.nl!xs4all!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.004 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; '480': 0.05; 'subject:Python': 0.06; 'binary': 0.07; 'subject:code': 0.07; 'correct,': 0.09; 'fname': 0.09; 'subject:into': 0.09; 'python': 0.11; "'rb')": 0.16; 'code?': 0.16; 'skip:n 90': 0.16; 'subject:simple': 0.16; 'url:gz': 0.16; 'url:nasa': 0.16; 'url:gov': 0.24; 'code:': 0.26; 'skip:" 20': 0.27; 'message- id:@mail.gmail.com': 0.30; 'code': 0.31; 'file': 0.32; 'skip:& 30': 0.33; 'but': 0.35; 'received:google.com': 0.35; 'wrong': 0.37; 'two': 0.37; 'same.': 0.38; 'to:addr:python-list': 0.38; 'to:addr:python.org': 0.39; 'here:': 0.62; 'here': 0.66; 'results': 0.69; 'pro': 0.69; 'contents,': 0.84; 'obtained': 0.96 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:from:date:message-id:subject:to:content-type; bh=stS/9T9aiUU48Dx4OhYjE9Dr3OCkvPKnoPBWCe7HcIs=; b=qi3npaEMOe+LTzynnAz2rQ0NmuizEAtptnCEkJ+Wz5QHFKJQQWBgT6uyn2EaFamN6K +I+9KoU6KPhQruXws7whlTIWnLb9E3IwjhgfXETNeqa7DejeGhSAlCo1qWZ4suEhce2q HpRB/CJY0LKq0EVv5sJsAmVulmtIpn9U6ukCueXH5LnEaefrFipptFZMQzHzMJETr0GJ dKLxptEEj+pw/UeUyaSGMQPjGm+sjAxGkao5O2nJOrk58a9+W0EXO+WS3XszzCsEXjXc xVfNtqcFXUVrRa+IEq05aPIwMv0FSxUEVuiTiDPJciXUYxZ7pwyt5J0LALTkLRFglhno 5NdQ== X-Received: by 10.182.44.135 with SMTP id e7mr16590825obm.18.1408894738892; Sun, 24 Aug 2014 08:38:58 -0700 (PDT) MIME-Version: 1.0 From: Cleo Drakos Date: Mon, 25 Aug 2014 00:38:18 +0900 Subject: Challenge to convert a simple IDL code into Python To: python-list@python.org Content-Type: multipart/alternative; boundary=001a11c3075c700ce9050161de93 X-Mailman-Approved-At: Sun, 24 Aug 2014 19:05:59 +0200 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: 383 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1408899960 news.xs4all.nl 2892 [2001:888:2000:d::a6]:54580 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:76933 --001a11c3075c700ce9050161de93 Content-Type: text/plain; charset=UTF-8 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? Here is binary file I was working with: ftp://trmmopen.gsfc.nasa.gov/pub/merged/3B42RT/3B42RT.2014010318.7.bin.gz cleo --001a11c3075c700ce9050161de93 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable

Here is IDL code:


pro read_bin=
ary_file

file =3D &q=
uot;3B42RT.2014010318.7.bin"

num_lon =3D 1440
num_lat =3D 480

data =3D {header: bytarr(num_lon*2), precip: intarr(num_lon,num_lat), precip_e=
rror: intarr(num_lon,num_lat), $
      source_of_estimate: bytarrnum_lon,num_lat), prec=
ip_uncal: intarr(num_lon,=
num_lat)}

close, 1
openr, 1, file
readu, 1, data
close, 1   =20
precip =3D swap_endian(=
dataprecip)

print, prec=
ip   =20
end

My attempt in Python is here:


fname =3D'3B42RT.2=
014010318.7.bin'

num_lon =3D 1440
num_lat =3D 480

with open(<=
/span>fname, 'rb') as fi:
    contents =3D fi.read=
()
    precip =3D struct.<=
span class=3D"" style=3D"margin:0px;padding:0px;border:0px;vertical-align:b=
aseline;background:transparent">unpack_from('&g=
t;'+str(num_lon*num_lat)+'I&=
#39;, contents, offset =
=3D num_lon*2)

    precip =3D np.array(<=
/span>data,dtype=3Dint)
    precip data

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

Here is binary file I was working with:ftp://trmmopen.gsfc.nasa= .gov/pub/merged/3B42RT/3B42RT.2014010318.7.bin.gz


cleo
--001a11c3075c700ce9050161de93--