Source code for irfpy.mnpi.backend_mnpi

''' Backend of MEX/NPI dataset.

MEX/NPI data set was produced by MatsH.
The file is an ascii format containing all the count rate.
In addition other misc information is included.

.. todo::

    There is also a "simple" format, i.e. without misc information.
    This simple format should also be implemented for future use.

.. todo::

    Re-organize the files would be very important.
    Inteface should be the same.

'''
import os

import dateutil.parser

import irfpy.npicommon.backend
from irfpy.util.julday import Julday, JdObject

[docs]class MexNpiEntry: ''' Instance NPI data. The NPI data consists of (at least) time and the count rate. ''' def __init__(self, linedata): # self.entry = linedata if linedata.startswith('#'): raise ValueError('Comment') lsp = linedata.split() if len(lsp) != 53: raise ValueError('Illegal token (%d != "53")' % len(lsp)) self.entry = lsp self.t = Julday(dateutil.parser.parse(lsp[1]))
[docs] def getTime(self): return self.t
[docs] def getCount(self): ''' Return the count. ''' cnt = [float(c) for c in self.entry[3:35]] return cnt
[docs]class MexNpiFile(irfpy.npicommon.backend.NpiFileCommon): ''' NPI data file for MEX. NPI data file. Typical usage is as follows. First, instance the file. >>> sample = MexNpiFile.get_sample_filename() >>> npi = MexNpiFile(sample) Then you can get dataset via :meth:`getDataset` method. The returned dataset is the instance of ``irfpy.util.julday.JdSeries``. >>> dataset = npi.getDataset() >>> print(dataset.size()) 360 '''
[docs] @classmethod def get_sample_filename(cls): from pkg_resources import resource_filename fn = resource_filename(__name__, os.path.join('sample', 'npi_sample.txt.bz2')) return fn
def __init__(self, *args, **kwds): irfpy.npicommon.backend.NpiFileCommon.__init__(self, *args, **kwds)
[docs] def loadfile(self, fp): ''' Load the file, and store it in the ``npidata`` variable. ''' for line in fp: # Data try: d = MexNpiEntry(str(line)) t = d.getTime() self.npidata.add(t, d) except ValueError as e: continue
import unittest,doctest
[docs]def doctests(): return unittest.TestSuite(( doctest.DocTestSuite(), ))
if __name__=='__main__': unittest.main(defaultTest='doctests')