Source code for irfpy.vima.irfima

''' IRF-produced calibrated IMA files.

The IRF-IMA files are produced by Hans & Gabriella.
They are Matlab file.
'''
import os
import logging

import datetime

import scipy.io
import matplotlib.dates

from irfpy.util.irfpyrc import Rc
import irfpy.util.timeseriesdb
from irfpy.util.utc import num2date as _num2date

from irfpy.asperacommon.excepts import DbPathNotFoundError, DatafileNotFoundError


[docs]def get_uribase_path(): ''' Return the database path. Database of IMA flux can be specified by [vima]/irfimauribase in irfpyrc. ''' rc = Rc() path = rc.get('vima', 'irfimauribase') if path is None: raise DbPathNotFoundError('No entry of [vima]/irfimauribase in irfpyrc') return path
[docs]class IrfImaDatabase(irfpy.util.timeseriesdb.DB): '''The IMA flux database. The IMA flux database will have a path, specified by *dbpath* keyword in the :meth:`__init__` method. By default, the class referrs to the path in :meth:`get_uribase_path` method. :keyword dbpath: Database path. If *None*, default directory in :meth:`get_uribase_path` is used. :keyword fitted_data: If *True*, fitted data (name start with ``fion``) is used. If *False* (default), integrated data (name starts with ``ion``) is used. First, instance the IrfImaDatabase class. >>> irfima = IrfImaDatabase() >>> print(len(irfima)) # Database size # doctest: +SKIP 4302 You can get the file (most probably) corresponding to 2006-12-05T13:00:00 >>> t0 = datetime.datetime(2006, 12, 5, 13, 0, 0) >>> fnamn = irfima.get(t0) >>> print(os.path.basename(fnamn)) ion20061205092444.mat IrfImaDatabase class also support fitted data. You can set *fitted_data* keyword. >>> irffima = IrfImaDatabase(fitted_data=True) >>> print(len(irffima)) # doctest: +SKIP 4391 >>> t0 = datetime.datetime(2008, 1, 22, 0, 35, 0) >>> fnamn = irffima.get(t0) >>> print(os.path.basename(fnamn)) fion20080121185400.mat ''' def __init__(self, dbpath=None, fitted_data=False): irfpy.util.timeseriesdb.DB.__init__(self) if dbpath is None: dbpath = get_uribase_path() self.logger = logging.getLogger(self.__class__.__name__) # self.logger.setLevel(logging.DEBUG) if not os.path.isdir(dbpath): raise DbPathNotFoundError('DB path %s does not exist. ' + 'Check the [vima]/irfimauripath in irfpyrc' % dbpath) if fitted_data: prefix = "fion" else: prefix = "ion" for p, d, f in os.walk(dbpath): for file in f: if file.startswith(prefix) and file.endswith('.mat'): self.logger.debug('Processing %s' % file) tstr = file[len(prefix):-4] self.logger.debug(' -> T = %s' % tstr) try: yr = int(tstr[0:4]) mo = int(tstr[4:6]) dy = int(tstr[6:8]) hr = int(tstr[8:10]) mn = int(tstr[10:12]) sc = int(tstr[12:14]) start_time = datetime.datetime(yr, mo, dy, hr, mn, sc) self.logger.debug(' -> T = %s' % start_time) except ValueError as e: self.logger.warn('Not a good file name (%s). Skipped.' % os.path.join(p, file)) self.append(os.path.join(p, file), start_time)
[docs]class IrfImaFile: ''' Class representing the matlab file of IRF IMA flux. The IRF IMA flux file. Instance it. >>> samplefile = IrfImaFile.get_sample_filename() >>> imafile = IrfImaFile(samplefile) >>> print(imafile.gettimerange()[1].strftime('%FT%T')) 2006-12-05T09:43:44 '''
[docs] @classmethod def get_sample_filename(cls, fitted_data=False): p = get_uribase_path() if fitted_data: fn = os.path.join(p, 'fion20080121185400.mat') else: fn = os.path.join(p, 'ion20061205092444.mat') return fn
def __init__(self, fnamn): self.fnamn = fnamn self.mat = scipy.io.loadmat(fnamn)
[docs] def getobstime(self): tlist = self.mat.get('t').flatten() # 1 x N data or N data. flatten needed. Up to version? # tdatetime = _num2date(tlist - 366) # tdatetime = [i.replace(tzinfo=None) for i in tdatetime] tdatetime = [_num2date(_ - 366).replace(tzinfo=None) for _ in tlist] return tdatetime
[docs] def gettimerange(self): t = self.getobstime() return [min(t), max(t)]
import unittest import doctest
[docs]def doctests(): return unittest.TestSuite(( doctest.DocTestSuite(), ))
if __name__ == '__main__': unittest.main(defaultTest='doctests')