IMMA: A module for observations in International Meteorological Maritime Archive format¶
This module provides a Python API to IMMA files.
IMMA files are not necessarily consistent in their contents - each line can have a different set of extensions. So we can’t treat the file as a table of data - instead we treat it as a list of records, and read those records one at a time.
So an IMMA file is best thought of as an iterator. Set up a file as an iterator with the get() function:
import IMMA
iobs=IMMA.get('some_file.imma')
Then every time you call iobs.next() you will get the next record from some_file.imma.
Each record is returned as a dict where vthe keys are the IMMA variable names. So after rec=iobs.next(), the sea-surface temperature is in rec['SST'].
If you want all the records in the file loaded simultaniously, either use obs=list(iobs) or call the read() function:
obs=IMMA.read('some_file.imma')
In both cases obs will be a list of dictionaries. So obs[3]['SST'] will be the SST from the fourth record in the file. If you want a list of all the SSTs in the file, you need something like:
SSTs=[ob['SST'] for ob in obs]
SST is in the core extension, so rec['SST'] will either have a valid number, or be None (blank in the IMMA record). If you look for a variable in an extension other than core it may be that the extension is not present in the selected record. In this case looking for it will raise a KeyError. I.e. rec['DCK'] might be a valid deck ID, it might be None (if the ICOADS extension is present but the DCK entry is blank) or it might raise a KeyError (if the ICOADS extension is missing from that record).
To output records to a file, first open the file, and then write() records to it one at a time:
opfile=open('some_output_file.imma','w')
for ob in obs:
IMMA.write(ob,opfile)
The list of extensions to write to the file is controlled by ob['extensions'] - an integer array containing the extension_id for all the extensions in the record. You need to update this explicitly - if you add or delete extensions from a record, update this array.
-
class
IMMA.get(filename)[source]¶ Turn an imma file into an iterator providing its records.
Parameters: filename ( str) – Name of file containing IMMA records.Returns: iterator - call next()on this to get the next record.Return type: func