>On 4 March 2010 10:00, ping <[email protected]> wrote:
> I'm trying to read TRMM precipitation data from a binary file that I
> donwloaded from NASA's web site. The information I have about the file
> is:
>
> -----
> Data are in 4-byte binary float from a SUN system (big-endian).
>
> My python code is:
>
>>>> import uu
>>>> infile = 
>>>> 'F:\\Hanlie\\UCT\\M.Sc\\Data\\PERSIANN\\2009_10_PERS6hr\\ms6s4_6h0927400.bin'
>>>> outfile = 
>>>> 'F:\\Hanlie\\UCT\\M.Sc\\Data\\PERSIANN\\2009_10_PERS6hr\\ms6s4_6h0927400.txt'
>>>> uu.decode(infile,outfile)
>
> Which produces the error:
>
> Traceback (most recent call last):
>  File "<pyshell#23>", line 1, in <module>
>    uu.decode(infile,outfile)
>  File "F:\Python26\lib\uu.py", line 98, in decode
>    raise Error('No valid begin line found in input file')
> Error: No valid begin line found in input file

Jose beat me to this, but I'll post my answer anyway, because the
binary file might have header information you'll want to examine...

I'm not very familiar with PERSIANN data files and have no idea what
the 'uu' Python module is, but it should be quite easy to get the data
from the file using the NumPy package (http://www.scipy.org/).

Something like the following should get you started:

import numpy as np

infile = 
'F:\\Hanlie\\UCT\\M.Sc\\Data\\PERSIANN\\2009_10_PERS6hr\\ms6s4_6h0927400.bin'
outfile = 
'F:\\Hanlie\\UCT\\M.Sc\\Data\\PERSIANN\\2009_10_PERS6hr\\ms6s4_6h0927400.txt'

# read the binary file
fp = open(infile, 'rb')
data_string = fp.read()
fp.close()

# convert the binary string to a NumPy array
precip = np.fromstring(data_string, np.float32)

# byte-swap if you are working on a little-endian machine
# your computer is almost certainly little-endian
if sys.byteorder == 'little':
    precip = precip.byteswap()

# if this call to reshape fails then the binary file probably
# contains some header information.
# Try calling 'print data_string[:2880]' to see
# if there's any text there. I'm guessing the 2880 but
# you should be able to get the right number by reading
# documentation on the file format
precip.reshape((480, 1440))

np.savetxt(outfile, precip)

Good luck.

Cheers,
Scott

Reply via email to