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

1import numpy as np 

2from lsst.sims.featureScheduler.surveys import Blob_survey, BaseSurvey 

3import healpy as hp 

4import copy 

5 

6 

7__all__ = ['ToO_master', 'ToO_survey'] 

8 

9 

10class ToO_master(BaseSurvey): 

11 """ 

12 A target of opportunity class. Every time a new ToO comes in, it will spawn a new sub-survey. 

13 

14 Parameters 

15 ---------- 

16 example_ToO_survey : lsst.sims.featureScheduler.surveys.ToO_survey object 

17 A survey object that will be coppied and have a new target map set 

18 for each incoming ToO. 

19 """ 

20 

21 def __init__(self, example_ToO_survey): 

22 self.example_ToO_survey = example_ToO_survey 

23 self.surveys = [] 

24 self.highest_reward = -np.inf 

25 

26 def add_observation(self, observation, indx=None): 

27 if len(self.surveys) > 0: 

28 for survey in self.surveys: 

29 survey.add_observation(observation, indx=indx) 

30 

31 

32 def _spawn_new_survey(self, too): 

33 """Create a new survey object for a ToO we haven't seen before. 

34 

35 Parameters 

36 ---------- 

37 too : lsst.sims.featureScheduler.utils.TargetoO object 

38 """ 

39 new_survey = copy.deepcopy(self.example_ToO_survey) 

40 new_survey.set_id(too.id) 

41 new_survey.set_target_map(too.footprint) 

42 

43 return new_survey 

44 

45 def _check_survey_list(self, conditions): 

46 """There is a current ToO in the conditions. 

47 """ 

48 

49 running_ids = [survey.too_id for survey in self.surveys] 

50 current_ids = [too.id for too in conditions.targets_of_opportunity] 

51 

52 # delete any ToO surveys that are no longer relevant 

53 self.surveys = [survey for survey in self.surveys if survey.too_id in current_ids] 

54 

55 # Spawn new surveys that are needed 

56 new_surveys = [] 

57 for too in conditions.targets_of_opportunity: 

58 if too.id not in running_ids: 

59 new_surveys.append(self._spawn_new_survey(too)) 

60 self.surveys.extend(new_surveys) 

61 

62 def calc_reward_function(self, conditions): 

63 # Catch if a new ToO has happened 

64 if conditions.targets_of_opportunity is not None: 

65 self._check_survey_list(conditions) 

66 

67 if len(self.surveys) > 0: 

68 rewards = [np.nanmax(survey.calc_reward_function(conditions)) for survey in self.surveys] 

69 self.reward = np.nanmax(rewards) 

70 self.highest_reward = np.min(np.where(rewards == self.reward)) 

71 else: 

72 self.reward = -np.inf 

73 self.highest_reward = None 

74 return self.reward 

75 

76 def generate_observations(self, conditions): 

77 if self.reward > -np.inf: 

78 result = self.surveys[self.highest_reward].generate_observations(conditions) 

79 return result 

80 

81 

82class ToO_survey(Blob_survey): 

83 """Survey class to catch incoming target of opportunity anouncements and try to observe them. 

84 

85 The idea is that we can dynamically update the target footprint basis function, and add new features as more ToOs come in. 

86 

87 Parameters 

88 ---------- 

89 too_id : int (None) 

90 A unique integer ID for the ToO getting observed 

91 """ 

92 def __init__(self, basis_functions, basis_weights, 

93 filtername1='r', filtername2=None, 

94 slew_approx=7.5, filter_change_approx=140., 

95 read_approx=2., exptime=30., nexp=2, 

96 ideal_pair_time=22., min_pair_time=15., 

97 search_radius=30., alt_max=85., az_range=180., 

98 flush_time=30., 

99 smoothing_kernel=None, nside=None, 

100 dither=True, seed=42, ignore_obs=None, 

101 survey_note='ToO', detailers=None, camera='LSST', 

102 too_id=None): 

103 super(ToO_survey, self).__init__(basis_functions=basis_functions, basis_weights=basis_weights, 

104 filtername1=filtername1, filtername2=filtername2, slew_approx=slew_approx, 

105 filter_change_approx=filter_change_approx, read_approx=read_approx, exptime=exptime, 

106 nexp=nexp, ideal_pair_time=ideal_pair_time, min_pair_time=min_pair_time, search_radius=search_radius, 

107 alt_max=alt_max, az_range=az_range, flush_time=flush_time, smoothing_kernel=smoothing_kernel, nside=nside, 

108 dither=dither, seed=seed, ignore_obs=ignore_obs, survey_note=survey_note, detailers=detailers, camera=camera) 

109 # Include the ToO id in the note 

110 self.survey_note_base = self.survey_note 

111 self.set_id(too_id) 

112 

113 def set_id(self, newid): 

114 """Set the id 

115 """ 

116 self.too_id = newid 

117 self.survey_note = self.survey_note_base + ', ' + str(newid) 

118 

119 def set_target_map(self, newmap): 

120 """ 

121 Expect one of the basis functions to be Footprint_nvis_basis_function 

122 """ 

123 for basis_func in self.basis_functions: 

124 if hasattr(basis_func, 'footprint'): 

125 basis_func.footprint = newmap 

126 

127 def generate_observations_rough(self, conditions): 

128 # Always spin the tesselation before generating a new block. 

129 if self.dither: 

130 self._spin_fields() 

131 result = super(ToO_survey, self).generate_observations_rough(conditions) 

132 return result