Source code for irfpy.vima.scidata_tree
''' Science data tree (raw count Mat files)
Workflow:
1. Instance a tree object.
>>> vimatree = VimaTree()
2. Get the filename
>>> print('FILE: ' + vimatree.get(datetime.datetime(2014, 4, 7, 10, 18))) # doctest: +ELLIPSIS
FILE: ...DDS_VIA_20140407T100000.mat
'''
import os
import logging
logger = logging.getLogger('scidata_tree')
import datetime
from irfpy.util import exception as _ex
from irfpy.util import timeseriesdb as tsdb
from irfpy.util import irfpyrc
from irfpy.vima 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_VIA_'):
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 VimaTree(tsdb.FazzyDB):
''' VIMA scidata factory.
'''
def __init__(self):
self.root = scidata.ImaCountFile.get_base_path()
if not os.path.exists(self.root):
raise _ex.IrfpyException('Path {} not found.'.format(self.root))
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.warn('{} is not mat file.'.format(f))
continue
try:
t = _guess(f)
fn = os.path.join(pp, f)
# logger.debug('{} <- {}'.format(str(t), fn))
guesseddb.append(fn, t)
except ValueError as e:
logger.warn('Some error happened to skip the file {}: err={}'.format(fn, 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')