Thanks to all who have answered. I'll try the numpy route.

2010/3/5, [email protected]
<[email protected]>:
> =============================================================================
> Today's Topic Summary
> =============================================================================
>
> Group: [email protected]
> Url: http://groups.google.com/group/python-gis-sig/topics
>
>   - Reading TRMM data [4 Updates]
>     http://groups.google.com/group/python-gis-sig/t/f231bfb1ccff41e3
>
>
> =============================================================================
> Topic: Reading TRMM data
> Url: http://groups.google.com/group/python-gis-sig/t/f231bfb1ccff41e3
> =============================================================================
>
> ---------- 1 of 4 ----------
> From: ping <[email protected]>
> Date: Mar 04 12:00AM -0800
> Url: http://groups.google.com/group/python-gis-sig/msg/89310162cac9d787
>
> Hi,
>
> 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).
> units are mm/hr, mm/6hr, mm/day, etc.
>
> coverage is:
>    60 to -60 lat
>     0 to 360 long
>
> at:
>       .25 x .25 deg resolution
>
> for:
>       480 rows x 1440 cols
>
> data is stored in C style row centric format.
> The first value is centered at
>       59.875,.125
> the second value at
>       59.875,.375
> last 2 values are centered at:
>       -59.875,359.625 &
>       -59.875,359.875
>
> sample matlab code:
>
> function om = loadbfn(fn,dim,sz)
> % loadbfn -- loads a binary file fn of dim
> %       ([nrows ncols]) of sz 'float','int', etc.
> %
> %       by DKB on 2/5/96
> %
>
> if(nargin < 3)
>         sz = 'float32';
> end
>
> f = fopen(fn,'r','b');
>
> if(nargout > 0)
>         om = fread(f,flipud(dim(:))',sz)';
> end
> fclose(f);
>
> -----
>
> My python code is:
>
>>>> 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
>
>
> Can someone perhaps help me?
>
> Thanks
> Hanlie
>
>
> ---------- 2 of 4 ----------
> From: Jose Gomez-Dans <[email protected]>
> Date: Mar 04 12:25PM
> Url: http://groups.google.com/group/python-gis-sig/msg/83a71b94788bdffe
>
> Hi,
>
>
>> 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:
>
> The easiest way is using using numpy
> trmm_data = (numpy.fromfile ( TRMM_file, dtype=numpy.float32,
> count=480*1440).byteswap()).reshape((480,1440))
>
> (you may not need byteswap() if you're on a SUN architecture).
>
> Jose
>
>
> ---------- 3 of 4 ----------
> From: Scott Sinclair <[email protected]>
> Date: Mar 04 02:30PM +0200
> Url: http://groups.google.com/group/python-gis-sig/msg/1a21970682c6b862
>
>>  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
>
>
> ---------- 4 of 4 ----------
> From: Scott Sinclair <[email protected]>
> Date: Mar 04 02:35PM +0200
> Url: http://groups.google.com/group/python-gis-sig/msg/b119d58272986eff
>
>>> is:
>> Something like the following should get you started:
>
>> import numpy as np
>
> You also need
>
> import sys
>
> Cheers,
> Scott
>
>
>
>
>

Reply via email to