Source code for irfpy.mnpi.background
''' MEX/NPI background proxy module
It provides a getter of background proxy.
The best practice is to use :class:`DataCenterMnpiBackground` class as folows.
>>> from irfpy.mnpi import background
>>> dc = background.DataCenterMnpiBackground()
>>> import datetime
>>> tobs, (back, total) = dc.nearest(datetime.datetime(2010, 1, 9, 20, 10))
>>> print(tobs)
2010-01-09 20:10:00.513000
>>> print(back)
4.0
>>> print(total)
143.0
'''
from irfpy.util.irfpyrc import Rc
import irfpy.npicommon.background as cbg
[docs]class DataCenterMnpiBackground(cbg.DataCenterBackground):
""" MEX NPD background data center
"""
def __init__(self):
"""
"""
rc = Rc()
path = rc.get('mnpi', 'backgroundbase')
cbg.DataCenterBackground.__init__(self, path, name="MEX NPI background")
_db = None
def _createdb(base=None):
''' Create database for MEX/NPI
:keyword base: Base path of database.
'''
global _db
if base is None:
rc = Rc()
base = rc.get('mnpi', 'backgroundbase')
_db = cbg.Database(base)
[docs]def isdb():
global _db
if not _db is None:
return True
try:
_createdb()
except (OSError, IOError):
_db = None
if not _db is None:
_db = None # Back to the original
return True
else:
return False
[docs]def get_background_level(t0, t1, refresh=False, base=None):
''' Get MEX 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]))
1715
'''
if _db is None or refresh:
_createdb(base=base)
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')