Source code for irfpy.mima.scidata_tree

''' Science data tree (raw count Mat files) for MEX/IMA.

Workflow:

1. Instance a tree object.

>>> mimatree = MimaTree()

2. Get the filename

>>> print('FILE: ' + mimatree.get(datetime.datetime(2014, 9, 19, 18, 8)))  #  doctest: +ELLIPSIS
FILE: ...DDS_IMA_20140919T180000.mat
'''
import os
import logging
logger = logging.getLogger(__name__)

import datetime

from irfpy.util import timeseriesdb as tsdb
from irfpy.mima import scidata

def _t0(filename):
    ''' A function that returns the actual start time.
    '''
    icf = scidata.ImaCountFile(filename)
    t0 = icf.gettimerange()[0]
    return t0

def _guess(filename):
    ''' Function returning the guessed start time

    >>> print(_guess('20120130110000.mat'))
    2012-01-30 11:00:00
    >>> print(_guess('this_is_not_a_datafile.mat'))  # doctest: +IGNORE_EXCEPTION_DETAIL
    Traceback (most recent call last):
    ValueError: this_is_not_a_datafile.mat is not a file convention ...
    
    '''
    fn = os.path.basename(filename)
    try:
        if fn.startswith('DDS_IMA_'):
            yr = int(fn[8:12])
            mo = int(fn[12:14])
            dy = int(fn[14:16])
            hr = int(fn[17:19])
            mn = int(fn[19:21])
            se = int(fn[21:23])
            return datetime.datetime(yr, mo, dy, hr,mn, se)
        else:
            yr = int(fn[0:4])
            mo = int(fn[4:6])
            dy = int(fn[6:8])
            hr = int(fn[8:10])
            mn = int(fn[10:12])
            se = int(fn[12:14])
            return datetime.datetime(yr, mo, dy, hr, mn, se)
    except ValueError:
        raise ValueError('{} is not a file convention for IMA raw matlab data'.format(filename))


[docs]class MimaTree(tsdb.FazzyDB): ''' MIMA matlab file database tree ''' def __init__(self): self.root = scidata.ImaCountFile.get_base_path() guesseddb = tsdb.DB() logger.debug(self.root) for pp, dd, ff in os.walk(self.root): for f in ff: if not f.endswith('.mat'): logger.debug('{} is not mat file.'.format(f)) continue try: t = _guess(f) if t < datetime.datetime(2003, 5, 1): logger.info('{} is before the mission start. Ignored.'.format(f)) fn = os.path.join(pp, f) # logger.debug('{} <- {}'.format(str(t), fn)) guesseddb.append(fn, t) except ValueError as e: logger.warn(str(e)) pass tsdb.FazzyDB.__init__(self, guesseddb, _t0)
import unittest import doctest
[docs]def doctests(): return unittest.TestSuite(( doctest.DocTestSuite(), ))
if __name__ == '__main__': unittest.main(defaultTest='doctests')