Sometimes data product are stored in binary format. Python has a great tool to read binary files properly, ie., "struct" module,. This module should be imported before we start. This module is designed to interpret the strings as packed binary.
For examples,
from struct import *
# open the file in the binary format
f= open(filename,'rb')
#to find the total size of your file,
file_size = os.path.getsize(filename)
#We can start read the file byte by byte: to read the first 8 bytes which stors the datetime information
buf = f.read(8)
#convert it to the readable data by using the "unpack" method
date = unpack('>7H', date)
#read the next 2 bytes for orbit number
orb = f.read(2)
orb = unpack('>H',orb)
f.close()
We can use f.tell, f.seek for the reading positions.
Yes, we need to know a great deal of the details about data format before the coding !
For finding the format used in the struct module in python, check out http://docs.python.org/library/struct.html.
This is quite helpful. Thanks for the example !
RépondreSupprimerThanks !
RépondreSupprimer