Extract Required Bytes From A File In Python
I have a binary file here: ftp://n5eil01u.ecs.nsidc.org/SAN/GLAS/GLA06.034/2003.02.21/GLA06_634_1102_001_0079_3_01_0001.DAT I have to extract the following data from that file: Byt
Solution 1:
Using a hard coded offset is a rather fragile solution. But assuming you know what you are doing:
Byte Offset: 176
Data type: 4-byte (long) integer
Total bytes: 160
AKAICT, that leads to 160/4 = 40 values to read (could you confirm that?)
In addition, the type should be one of the numpy defined type. Here np.int32
might be the right one:
data= np.fromfile(fi,dtype=np.int32,count=40)
On my computer, this produces the following result:
[1919251297 997485633 1634494218 1936678771 1634885475 825124212
808333629 808464432 942813232 1818692155 1868526433 1918854003
1600484449 1702125924 842871086 758329392 841822768 1728723760
1601397100 1600353135 1702125938 1835627615 1026633317 809119792
808466992 1668483643 1668509535 1952543327 1026633317 960048688
960051513 909654073 926037812 1668483643 1668509535 1952543327
1633967973 825124212 808464957 842018099]
If this is not what expected, maybe you have a problem of endianness.
Numpy as support for custom defined types to solve that problem:
For example:
np.dtype('<i4')
is 4 bytes (signed) integer little endiannp.dtype('>i4')
is 4 bytes (signed) integer big endian
In you case, to force reading data as little endian, you might write:
dt = np.dtype('<i4')
with open(fname,'rb') as fi:
fi.seek (176,0)
data= np.fromfile(fi,dtype=dt,count=40)
print data
Post a Comment for "Extract Required Bytes From A File In Python"