Source code for irfpy.cy1orb.pvat

''' pvat module is a collection of functions (syntax sugar) for the PVAT related calculations.

- Orbit of CY-1 for arbitrary time by getorbit()
- Conversion from SC frame to LSE for arbitrary time frame by getlsevec()

.. note::

    Use :mod:`irfpy.cy1orb.pvat2` for practical use.

    :mod:`irfpy.cy1orb.pvat2` aims to replace of :mod:`irfpy.cy1orb.pvat`,
    because the first implementation of
    :mod:`irfpy.cy1orb.pvat` has a problem in its specification on caching.
    Clearly stating,
    :mod:`irfpy.cy1orb.pvat` uses huge memory after a long use.
    Now :mod:`irfpy.cy1orb.pvat2` uses :mod:`irfpy.util.ringcache` to solve the problem.

    However, performance in the calculation time is slightly worse than pvat.
    The calculation of :mod:`irfpy.cy1orb.pvat2` takes 10% more time than :mod:`irfpy.cy1orb.pvat`.

'''
import warnings
warnings.warn('Use irfpy.cy1orb.pvat2', DeprecationWarning)

from irfpy.util.utc import convert
from irfpy.util import julday
from irfpy.cy1orb.Cy1OrbitNr import Cy1OrbitNr
from irfpy.util import vector3d
from irfpy.cy1orb import Cy1Orbit

import logging
import datetime
import numpy

#__cy1orbitnr=Cy1OrbitNr()
#__cy1orbitnr.setFromDefaultUri()

class __cy1orbitnr:
    instance = None
    @classmethod
    def cache(cls):
        if cls.instance is None:
            cls.instance = Cy1OrbitNr()
            cls.instance.setFromDefaultUri()
        return cls.instance


__cy1orbitcache=Cy1Orbit.Cy1Orbit()   # Behave as cache of the original data
__cy1attitudecache=Cy1Orbit.Cy1Attitude()

# Used for getlsepos function.
__cy1lseorbitcache={}  # Behave as a cache of JdSeries data:  orbit to JdSeries
# Used for getmepos function.
__cy1meorbitcache={}

# Used for getlsevec function
__cy1attitudedatacache={}   # Behave as a cache:  datetime to matrix.


[docs]def clear_cache(): ''' Clear cache related to pvat module. ''' logging.debug('Clearing cache') # __cy1orbitnr=Cy1OrbitNr() # __cy1orbitnr.setFromDefaultUri() __cy1orbitnr.instance = None __cy1orbitcache.clearCache() # Behave as cache of the original data __cy1lseorbitcache={} # Behave as a cache of JdSeries data. __cy1meorbitcache={} __cy1attitudecache.clearCache() __cy1attitudedatacache={} # Does not work???
[docs]def getlsepos(t): ''' Get LSE orbit at the time of t as an instance of Vector3d. The LSE orbit in km unit is returned. The returned position of the spacecraft is at the sampling time (1 sec resolution) closest to the specified time. The data is read from default URI, and once the data is read from file, it is cached. ''' orbnr=__cy1orbitnr.cache().getOrbitNr(t) logging.debug('Orb=%d'%orbnr) t_dt = convert(t, datetime.datetime) logging.debug(t_dt) if orbnr in __cy1lseorbitcache: logging.debug('Found a cache for orbit %d.'%orbnr) else: orbdata = __cy1orbitcache.getPosOfOrb2(orbnr) # JdSeries logging.debug(orbdata.size()) __cy1lseorbitcache[orbnr] = orbdata orbdata=__cy1lseorbitcache[orbnr] v=orbdata.getNeighbor(convert(t, julday.Julday)).getData() # For safety reasons, v is copied explicitly. return vector3d.Vector3d(v.x,v.y,v.z)
[docs]def getmepos(t): ''' Get s/c position in ME frame as an array of [lon,lat,height]. lon and lat is in degrees, height is in km above the surface. >>> me=getmepos(datetime.datetime(2009,4,18,1,30,0)) ''' orbnr=__cy1orbitnr.cache().getOrbitNr(t) t_dt = convert(t, datetime.datetime) if orbnr in __cy1meorbitcache: logging.debug('Found a cache for orbit %d'%orbnr) else: orbdata=__cy1orbitcache.getSelenographicOfOrb2(orbnr) # JdSeries __cy1meorbitcache[orbnr]=orbdata orbdata=__cy1meorbitcache[orbnr] jd=convert(t, julday.Julday) val=orbdata.getNeighbor(jd) v=val.getData() return [v[0], v[1], v[2]]
[docs]def getlsevec(t, vec_in_sc): ''' Get LSE-frame vector correspoinding to the SC-frame vector at the time of t. ''' dt = convert(t, datetime.datetime) if dt not in __cy1attitudedatacache: orbnr=__cy1orbitnr.cache().getOrbitNr(t) logging.debug('Orbit %d at %s'%(orbnr, t)) jd = convert(t, julday.Julday) attdata = __cy1attitudecache.getSc2LseMatrixOfOrb(orbnr) matx = attdata.getNeighbor(jd).getData() logging.debug(matx) __cy1attitudedatacache[dt] = matx else: logging.debug('Find time %s in cache'%dt) matx = __cy1attitudedatacache[dt] logging.debug(matx) vec_sc = [vec_in_sc.x, vec_in_sc.y, vec_in_sc.z] logging.debug(vec_sc) vec_lse = numpy.dot(matx, vec_sc) logging.debug(vec_lse) return vector3d.Vector3d(vec_lse[0], vec_lse[1], vec_lse[2])
if __name__=='__main__': # logging.getLogger().setLevel(logging.DEBUG) ### Get orbit in LSE. pos=getlsepos(datetime.datetime(2009,1,25,14,0,0)) pos2=getlsepos(datetime.datetime(2009,1,25,14,0,0)) print(pos) print(pos2) print('is?', pos is pos2) print('equals?', pos.equals(pos2)) ### Get vector in LSE. X_sc is the nadir direction. vec=getlsevec(datetime.datetime(2009,1,25,14,0,0), vector3d.Vector3d(1,0,0)) print(vec) vec=getlsevec(datetime.datetime(2009,1,25,14,0,0), vector3d.Vector3d(0,1,0)) print(vec) vec=getlsevec(datetime.datetime(2009,1,25,14,0,0), vector3d.Vector3d(0,0,1)) print(vec) clear_cache() vec=getlsevec(datetime.datetime(2009,1,25,14,0,0), vector3d.Vector3d(0,0,1)) print(vec)