Coverage for python/lsst/ts/dateloc/date_profile.py : 40%

Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1from __future__ import division
2from datetime import datetime, timedelta
3import math
5import palpy
7__all__ = ["DateProfile"]
9class DateProfile(object):
10 """
11 This class handles calculating the Modified Julian Date and the Local Sidereal Time for
12 the internal timestamp and location coordinates.
13 """
15 SECONDS_IN_HOUR = 60.0 * 60.0
17 def __init__(self, timestamp, location):
18 """Initialize the class.
20 Parameters
21 ----------
22 timestamp : float
23 The UTC timestamp for a given date/time.
24 location : lsst.ts.dateloc.ObservatoryLocation
25 The location site information instance.
26 """
27 self.location = location
28 self.update(timestamp)
30 def __call__(self, timestamp):
31 """Modified Julian Date and Local Sidereal Time from instance.
33 Parameters
34 ----------
35 timestamp : float
36 The UTC timestamp to get the MJD and LST for.
38 Returns
39 -------
40 (float, float)
41 A tuple of the Modified Julian Date and Local Sidereal Time (radians).
42 """
43 self.update(timestamp)
44 return (self.mjd, self.lst_rad)
46 def __get_timestamp(self, dt):
47 """float: Return a timestamp from the datetime instance.
48 """
49 return (dt - datetime(1970, 1, 1)).total_seconds()
51 @property
52 def lst_rad(self):
53 """float: Return the Local Sidereal Time (radians) for the internal timestamp.
54 """
55 value = palpy.gmst(self.mjd) + self.location.longitude_rad
56 if value < 0.:
57 value += 2.0 * math.pi
58 return value
60 @property
61 def mjd(self):
62 """float: Return the Modified Julian Date for the internal timestamp.
63 """
64 mjd = palpy.caldj(self.current_dt.year, self.current_dt.month, self.current_dt.day)
65 mjd += (self.current_dt.hour / 24.0) + (self.current_dt.minute / 1440.) + \
66 (self.current_dt.second / 86400.)
67 return mjd
69 def midnight_timestamp(self):
70 """float: Return the UTC timestamp of midnight for the current date.
71 """
72 midnight_dt = datetime(self.current_dt.year, self.current_dt.month, self.current_dt.day)
73 return self.__get_timestamp(midnight_dt)
75 def next_midnight_timestamp(self):
76 """float: Return the UTC timestamp of midnight for the next day after current date.
77 """
78 midnight_dt = datetime(self.current_dt.year, self.current_dt.month, self.current_dt.day)
79 midnight_dt += timedelta(**{"days": 1})
80 return self.__get_timestamp(midnight_dt)
82 def previous_midnight_timestamp(self):
83 """float: Return the UTC timestamp of midnight for the next day before current date.
84 """
85 midnight_dt = datetime(self.current_dt.year, self.current_dt.month, self.current_dt.day)
86 midnight_dt -= timedelta(**{"days": 1})
87 return self.__get_timestamp(midnight_dt)
89 def update(self, timestamp):
90 """Change the internal timestamp to requested one.
92 Parameters
93 ----------
94 timestamp : float
95 The UTC timestamp to update the internal timestamp to.
96 """
97 self.timestamp = timestamp
98 self.current_dt = datetime.utcfromtimestamp(self.timestamp)