Source code for irfpy.vima.background

''' VEX/IMA background proxy module

It provides a getter of background proxy.

The best-practice is to use :class:`DataCenterVimaBackground` class as follows.

>>> from irfpy.vima import background
>>> dc = background.DataCenterVimaBackground()
>>> import datetime
>>> tobs, (back, total) = dc.nearest(datetime.datetime(2009, 12, 28, 6, 10))
>>> print(tobs)   # Observed time nearest to the given time (2009-12-28T06:10)
2009-12-28 06:11:34.837000
>>> print(back)   # Background count
5.18
>>> print(total)   # Total count
15778.0

'''
import datetime

from irfpy.util.irfpyrc import Rc
import irfpy.imacommon.background as cbg

[docs]class DataCenterVimaBackground(cbg.DataCenterBackground): """ VEX IMA background data center """ def __init__(self): """ """ rc = Rc() path = rc.get('vima', 'backgroundbase') cbg.DataCenterBackground.__init__(self, path, name="VEX IMA background")
_db = None def _createdb(base=None): ''' Create database for VEX/IMA :keyword base: Base path of database. ''' global _db if base is None: rc = Rc() base = rc.get('vima', 'backgroundbase') _db = cbg.Database(base)
[docs]def isdb(): global _db if _db is not None: return True try: _createdb() except (OSError, IOError): _db = None if _db is not None: _db = None # Back to the original return True else: return False
[docs]def get_background_level(t0, t1, refresh=False, base=None): ''' Get VEX background level. :param t0: Time start. :param t1: Time end. :keyword refresh: If ``True``, a new database is created. :keyword base: Give base path of the dataset. If ``None``, the dataset path is given from ``.irfpyrc``. :returns: (``time array``, ``bg array``). >>> import datetime >>> t0 = datetime.datetime(2006, 12, 4) >>> t1 = datetime.datetime(2006, 12, 7) >>> bg = get_background_level(t0, t1) >>> print(len(bg[1])) 225 ''' if _db is None or refresh: _createdb(base=base) if t0 < datetime.datetime(2006, 2, 1): t0 = datetime.datetime(2006, 2, 1) if t1 < t0: t1 = t0 return _db.get_background_level(t0, t1)
import unittest import doctest
[docs]def doctests(): return unittest.TestSuite(( doctest.DocTestSuite(), ))
if __name__ == '__main__': unittest.main(defaultTest='doctests')