1

This is a .dat file.

In Matlab, I can use this code to read.

lonlatfile='NOM_ITG_2288_2288(0E0N)_LE.dat';
f=fopen(lonlatfile,'r');
lat_fy=fread(f,[2288*2288,1],'float32');
lon_fy=fread(f,[2288*2288,1],'float32')+86.5;
lon=reshape(lon_fy,2288,2288);
lat=reshape(lat_fy,2288,2288);

Here are some results of Matlab: matalab

How to do in python to get the same result?

PS: My code is this:

def fromfileskip(fid,shape,counts,skip,dtype):
"""
fid    : file object,    Should be open binary file.
shape  : tuple of ints,  This is the desired shape of each data block.
       For a 2d array with xdim,ydim = 3000,2000 and xdim = fastest 
       dimension, then shape = (2000,3000).
counts : int, Number of times to read a data block.
skip   : int, Number of bytes to skip between reads.
dtype  : np.dtype object, Type of each binary element.
"""
    data = np.zeros((counts,)  + shape)
    for c in range(counts):
        block = np.fromfile(fid,dtype=np.float32,count=np.product(shape))
        data[c] = block.reshape(shape)
        fid.seek( fid.tell() + skip)
    return data

fid = open(r'NOM_ITG_2288_2288(0E0N)_LE.dat','rb')
data = fromfileskip(fid,(2288,2288),1,0,np.float32)
loncenter = 86.5 #Footpoint of FY2E
latcenter = 0
lon2e = data+loncenter
lat2e = data+latcenter
Lon = lon2e.reshape(2288,2288)
Lat = lat2e.reshape(2288,2288)

But, the result is different from that of Matlab.

2
  • 3
    Different...how? Commented Mar 27, 2017 at 15:46
  • @excaza The same position of the array has different value. Maybe, you can download the .dat file and run the code to compare the result with matlab's(that picture i post). Commented Mar 27, 2017 at 21:44

1 Answer 1

3

You should be able to translate the code directly into Python with little change:

lonlatfile = 'NOM_ITG_2288_2288(0E0N)_LE.dat'
with open(lonlatfile, 'rb') as f:
    lat_fy = np.fromfile(f, count=2288*2288, dtype='float32')
    lon_fy = np.fromfile(f, count=2288*2288, dtype='float32')+86.5
lon = lon_ft.reshape([2288, 2288], order='F');
lat = lat_ft.reshape([2288, 2288], order='F');

Normally the numpy reshape would be transposed compared to the MATLAB result, due to different index orders. The order='F' part makes sure the final output has the same layout as the MATLAB version. It is optional, if you remember the different index order you can leave that off.

The with open() as f: opens the file in a safe manner, making sure it is closed again when you are done even if the program has an error or is cancelled for whatever reason. Strictly speaking it is not needed, but you really should always use it when opening a file.

Sign up to request clarification or add additional context in comments.

1 Comment

@ZhangXin: Then please mark the answer as "correct" so other people can find it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.