Hide keyboard shortcuts

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 builtins import object 

3import os 

4import sqlite3 

5import numpy as np 

6from astropy.time import Time 

7from lsst.utils import getPackageDir 

8 

9__all__ = ["SeeingData"] 

10 

11 

12class SeeingData(object): 

13 """Read the seeing data from disk and return appropriate FWHM_500 value at a given time. 

14 This is for use in simulations only. Otherwise data would come from the EFD. 

15 

16 Parameters 

17 ---------- 

18 start_time : astropy.time.Time 

19 The time of the start of the simulation. 

20 The seeing database will be assumed to start on Jan 01 of the same year. 

21 seeing_db : str or None, opt 

22 The name of the seeing database. 

23 If None (default), this will use the simsee_pachon_58777_13.db file in the 'data' directory 

24 of this package. 

25 Other available seeing databases from sims_seeingModel include: 

26 seeing.db (the original, less-variable, 3 year seeing database) 

27 simsee_pachon_58777_13.db (the current default, 10 year, seeing database) 

28 simsee_pachon_58777_16.db (a similar, but slightly offset, 13 year seeing database) 

29 For more info on simsee_pachon_58777_*, see https://github.com/lsst/sims_seeingModel/issues/2 

30 offset_year : float, opt 

31 Offset into the cloud database by 'offset_year' years. Default 0. 

32 """ 

33 def __init__(self, start_time, seeing_db=None, offset_year=0): 

34 self.seeing_db = seeing_db 

35 if self.seeing_db is None: 

36 self.seeing_db = os.path.join(getPackageDir('sims_seeingModel'), 'data', 

37 'simsee_pachon_58777_13.db') 

38 

39 # Seeing database starts in Jan 01 of the year of the start of the simulation 

40 year_start = start_time.datetime.year + offset_year 

41 self.start_time = Time('%d-01-01' % year_start, format='isot', scale='tai') 

42 

43 self.seeing_dates = None 

44 self.seeing_values = None 

45 self.read_data() 

46 

47 def __call__(self, time): 

48 """Get the FWHM_500 value for the specified time. 

49 

50 Parameters 

51 ---------- 

52 time : astropy.time.Time 

53 Time in the simulation for which to find the 'current' zenith seeing values. 

54 The difference between this time and the start_time, plus the offset, 

55 will be used to query the seeing database. 

56 

57 Returns 

58 ------- 

59 float 

60 The FWHM_500(") closest to the specified time. 

61 """ 

62 delta_time = (time - self.start_time).sec 

63 # Find the date to look for in the time range of the data. 

64 # Note that data dates should not necessarily start at zero. 

65 dbdate = delta_time % self.time_range + self.min_time 

66 idx = np.searchsorted(self.seeing_dates, dbdate) 

67 # searchsorted ensures that left < date < right 

68 # but we need to know if date is closer to left or to right 

69 left = self.seeing_dates[idx - 1] 

70 right = self.seeing_dates[idx] 

71 if dbdate - left < right - dbdate: 

72 idx -= 1 

73 return self.seeing_values[idx] 

74 

75 def read_data(self): 

76 """Read the seeing information from disk. 

77 

78 The default behavior is to use the module stored database. However, an 

79 alternate database file can be provided. The alternate database file needs to have a 

80 table called *Seeing* with the following columns: 

81 

82 seeingId 

83 int : A unique index for each seeing entry. 

84 s_date 

85 int : The time (in seconds) from the start of the simulation, for the seeing observation. 

86 seeing 

87 float : The FWHM of the atmospheric PSF (in arcseconds) at zenith. 

88 """ 

89 with sqlite3.connect(self.seeing_db) as conn: 

90 cur = conn.cursor() 

91 query = "select s_date, seeing from Seeing order by s_date;" 

92 cur.execute(query) 

93 results = np.array(cur.fetchall()) 

94 self.seeing_dates = np.hsplit(results, 2)[0].flatten() 

95 self.seeing_values = np.hsplit(results, 2)[1].flatten() 

96 cur.close() 

97 # Make sure seeing dates are ordered appropriately (monotonically increasing). 

98 ordidx = self.seeing_dates.argsort() 

99 self.seeing_dates = self.seeing_dates[ordidx] 

100 self.seeing_values = self.seeing_values[ordidx] 

101 self.min_time = self.seeing_dates[0] 

102 self.max_time = self.seeing_dates[-1] 

103 self.time_range = self.max_time - self.min_time 

104 

105 def config_info(self): 

106 """Report information about configuration of this data. 

107 

108 Returns 

109 ------- 

110 OrderedDict 

111 """ 

112 config_info = {} 

113 config_info['Start time for db'] = self.start_time 

114 config_info['Seeing database'] = self.seeing_db 

115 return config_info