Source code for irfpy.cena.cena_mass

''' Easy syntax of CENA mass mode data retriever.

Internally, CenaLoader instance is stored.
Therefore, if memory becomes too much, clearCache() should be called explicitly.
'''

import logging
import datetime

from irfpy.util.julday import *
from irfpy.cena.cena_scicnt import *
from irfpy.cy1orb.Cy1OrbitNr import Cy1OrbitNr

__orbitnr=Cy1OrbitNr()
__orbitnr.setFromDefaultUri()

import warnings

warnings.warn("irfpy.cena.cena_mass module is deprecated.  Use irfpy.cena.cena_mass2 module.", DeprecationWarning)

[docs]class CenaMassMatrixCache: def __init__(self, rcfile=None): self.__rc=Rc() if rcfile: self.__rc.append(rcfile) self.__matrixcache={} self.__orbitnr=Cy1OrbitNr() self.__orbitnr.setFromDefaultUri() self.cenaloader=CenaLoader()
[docs] def clearCache(self): self.__matrixcache={} self.cenaloader.clearCache()
[docs] def getMassMatrix(self, t): ''' Get a mass matrix of the specified time. The read data is stored to the cache. The returned data is a JdObject which includes data of (8,7,128) shaped numpy.array instance, which is copy()-ed new instance. >>> jdobj=__cenamassmatrixcache.getMassMatrix(datetime.datetime(2009,4,18,1,30,0)) >>> jd=jdobj.getJd() >>> data=jdobj.getData() >>> data.shape (8, 7, 128) >>> data.sum() 5.0 ''' o = self.__orbitnr.getOrbitNr(t) logging.debug('Orbit nr %d for time %s'%(o,t)) if o in self.__matrixcache: mm=self.__matrixcache[o] else: mm = self.cenaloader.getFullMassMatrix(o) self.__matrixcache[o] = mm data = mm.getNeighbor(convert(t, Julday)) jd = data.getJd() dt_spec = convert(t, datetime.datetime) dt_ = convert(jd, datetime.datetime) dt = abs(dt_spec-dt_) logging.debug('Dt=%s'%dt) if dt > datetime.timedelta(seconds=4): logging.error('Failed to load data. %s'%dt_spec) raise RuntimeError('Failed to load CENA data at %s.\nNearest data is %s.'%(dt_spec, dt_)) data = data.getData() return JdObject(jd, data.copy())
__cenamassmatrixcache=CenaMassMatrixCache() # An cache for CENA mass matrix. __cenaloader=__cenamassmatrixcache.cenaloader # An instance for CENA loader. It also support cache of orbit based CenaArray instance.
[docs]def clearCache(): ''' Clear a cache of CENA. ''' __cenamassmatrixcache.clearCache()
[docs]def getdata(t, filter=[]): ''' Obtain CENA data for specified time t. @param t Time @param filter Filter to the date applied after the data retrieval. not supported yet. @retval JdObject that includes data of (8,7,128) shaped numpy.array >>> jddat=getdata(datetime.datetime(2009,4,18,1,30,0)) >>> dat=jddat.getData() >>> print(dat.sum()) 5.0 >>> jd=jddat.getJd() >>> print(jd.juld()) # doctest: +ELLIPSIS 2454939.56251... ''' jddata = __cenamassmatrixcache.getMassMatrix(t) jd=jddata.getJd() dat=jddata.getData() for filt in filter: dat=filt(dat) return JdObject(jd, dat)
[docs]def getobstime(orbit=None, timerange=None): ''' Get the observation time of CENA in MASS mode for the specified orbit or the specified timerange. Obtain the observation time as an array of Julday instance. If both orbit and timerange is not specified, RuntimeError will be raised. @param orbit Orbit number. @param timerange Time range. @retval An array of Julday instance of observation time used mass accumulation mode for CENA. @except RuntimeError raised if both orbit and timerange is not specified. >>> obt=getobstime(orbit=1945) >>> len(obt) # v3.2, 1735. 1767 is based on the old database? On 100712, 1766 is returned via nfs. On 110224, 1764 is returned based on v3 dataset. 1735 >>> print(obt[0]) 2454939.51960(2009-04-18T00:28:13.095) >>> obt=getobstime(orbit=1951) >>> len(obt) 0 >>> masst=getobstime(timerange=[datetime.datetime(2009,1,28,17,0,0), datetime.datetime(2009,1,28,19,0,0)]) >>> print(len(masst)) # 582 is based on the old data base? On 100712, 597 is returned via nfs. 597 ''' global __cenaloader tlist=[] if orbit is not None: logging.debug('Orbit=%d'%orbit) obt=__cenaloader.getTMmodes(orbit) obt_jds = obt.getJuldayList() for jd in obt_jds: if obt.getNeighbor(jd).getData() == CenaPacket.TMMODE_MASS_ACC: tlist.append(jd) return tlist elif timerange is not None: o0 = convert(__orbitnr.getOrbitNr(timerange[0])) o1 = convert(__orbitnr.getOrbitNr(timerange[1]))+1 jd0 = convert(timerange[0], Julday) jd1 = convert(timerange[1], Julday) logging.debug('Orbit from %d to %d is loaded.'%(o0,o1)) for o in range(o0, o1+1): otlist = getobstime(orbit=o) for ot in otlist: if not ot.isEarlier(jd0) and not ot.isLater(jd1): logging.debug('TIME=%s will be returned.'%ot) tlist.append(ot) else: logging.debug('TIME=%s will not be returned.'%ot) return tlist else: raise RuntimeError('Either orbit or timerange should be specified.')