Source code for irfpy.swim.swim_cache
import sys
from Sadppac import *
import datetime
import numpy
from irfpy.util.utc import *
from irfpy.util.julday import *
from irfpy.util.fivenumsum import *
from irfpy.cy1orb.Cy1OrbitNr import Cy1OrbitNr
from irfpy.swim.SwimSciCount import *
from irfpy.swim.flux import *
import logging
from irfpy.swim import SwimTime
from irfpy.swim import SwimGfact
from irfpy.swim import SwimEnergy
[docs]class SwimDataCache:
SWIM_TIMERESOLUTION=datetime.timedelta(0, SwimTime.time_resolution())
def __init__(self):
self.__orbnr=Cy1OrbitNr()
self.__orbnr.setFromDefaultUri()
self.__loader=SwimLoader()
self.__cache={} # Cache for Start
self.__masscache={} # Cache for Start
[docs] def clearCache(self):
logging.debug('Cache cleared.')
del self.__loader
self.__cache={}
self.__loader=SwimLoader()
def __str__(self):
return "<SwimDataCache: Cache size=%d %d>" % (len(self.__cache), len(self.__masscache))
[docs] def statusCache(self, fp=sys.stderr):
print(str(self), file=fp)
print('\tOrbit=', end=' ', file=fp)
for onr in list(self.__cache.keys()):
print('%04d ' % onr, end=' ', file=fp)
print(file=fp)
return len(self.__cache)
def __loadorbit(self, orbnr):
if orbnr in self.__cache:
logging.debug('Data exists in cache for orbit %d'%orbnr)
else:
logging.debug('Data not exist in cache for orbit %d'%orbnr)
try:
sMat = self.__loader.getStartMatrix(orbnr)
except IOError as e:
# logging.error('Failed to load data for time %s.'%time_dt)
# logging.error('Exception thrown.')
logging.error('Error: %s'%e)
raise RuntimeError('Failed to load SWIM data. No data for the orbit %d'%orbnr)
### sMat is (time,matrix) so convert it to JdSeries class.
jMat=JdSeries()
# print len(sMat[0])
for i in range(len(sMat[0])):
# print i
jMat.add( dt2jd(sMat[0][i]), sMat[1][i])
self.__cache[orbnr] = jMat
def __loadmassorbit(self, orbnr):
logger = logging.getLogger('loadmassorbit')
# logger.setLevel(logging.DEBUG)
if orbnr in self.__masscache:
logger.debug('Data exists in chache for orbit %d' % orbnr)
else:
logger.debug('Data not exists in chache for orbit %d' % orbnr)
try:
sMat = self.__loader.getMassMatrix(orbnr)
except IOError as e:
logging.error('Error: %s' % e)
raise RuntimeError('Failed to load SWIM data. No data for the orbit %d' % orbnr)
# sMat is (time, matrix) so convert to JdSeries.
jMat = JdSeries()
for i in range(len(sMat[0])):
jMat.add(dt2jd(sMat[0][i]), sMat[1][i])
self.__masscache[orbnr] = jMat
[docs] def getStartMatrix(self, time_dt):
''' Get start matrix in the format of JdObject.
'''
orbnr=self.__orbnr.getOrbitNr(dt2tt(time_dt))
self.__loadorbit(orbnr)
jMat=self.__cache[orbnr]
data = jMat.getNeighbor(dt2jd(time_dt))
data_dt = jd2dt(data.getJd())
deltat=abs(data_dt-time_dt)
if deltat > self.SWIM_TIMERESOLUTION:
logging.error('Failed to load data. %s'%time_dt)
raise RuntimeError('Failed to load SWIM data at %s.\nNearest data is %s.'%(time_dt, data_dt))
retdata=JdObject(data.getJd(), numpy.array(data.getData()).copy())
return retdata
[docs] def getMassMatrix(self, time_dt):
''' Get mass matrix
:param time_dt: Time in the format of ``datetime``.
:returns: Mass matrix
:rtype: ``irfpy.util.julday.JdObject`` with ``np.array`` data.
'''
orbnr = self.__orbnr.getOrbitNr(dt2tt(time_dt))
self.__loadmassorbit(orbnr)
jMat = self.__masscache[orbnr]
data = jMat.getNeighbor(dt2jd(time_dt))
data_dt = jd2dt(data.getJd())
deltat = abs(data_dt - time_dt)
if deltat > self.SWIM_TIMERESOLUTION:
logging.error('Failed to load data. %s'%time_dt)
raise RuntimeError('Failed to load SWIM data at %s.\nNearest data is %s.'%(time_dt, data_dt))
retdata=JdObject(data.getJd(), numpy.array(data.getData()).copy())
return retdata
[docs] def getObstime(self, orbit=None, timerange=None):
if orbit is not None:
self.__loadorbit(orbit)
jdser=self.__cache[orbit]
return jdser.getJuldayList()
elif timerange is not None:
# Prepare the returned container.
jdser=JdSeries()
t0,t1=timerange
t0=convert(t0, Julday)
t1=convert(t1, Julday)
o0=self.__orbnr.getOrbitNr(t0)
o1=self.__orbnr.getOrbitNr(t1)
logging.debug('Orbit number (start)=%d'%o0)
logging.debug('Orbit number (stop)=%d'%o1)
for o in range(o0, o1+1):
try:
jds=self.getObstime(orbit=o)
for jd in jds:
if jd >= t0 and jd <= t1:
jdser.add(jd, 0)
except RuntimeError as e :
# No ddata available. Skip.
logging.warn('Error %s'%e)
logging.warn('Orbit %d does not contain SWIM data. Skipped'%o)
return jdser.getJuldayList()
else:
raise RuntimeError('Use orbit or timerange keyword to call getobstime() function.')