def start_night(self, night): """Perform actions at the start of the night. Parameters ---------- night : int The current night. """ self.log.info("Night {}".format(night)) self.seq.start_night(night, self.duration) self.comm_time.night = night self.seq.sky_model.update(self.time_handler.current_timestamp) (set_timestamp, rise_timestamp) = self.seq.sky_model.get_night_boundaries( self.conf.sched_driver.night_boundary) delta = math.fabs(self.time_handler.current_timestamp - set_timestamp) self.time_handler.update_time(delta, "seconds") self.log.debug("Start of night {} at {}".format( night, self.time_handler.current_timestring)) self.end_of_night = rise_timestamp end_of_night_str = self.time_handler.future_timestring( 0, "seconds", timestamp=self.end_of_night) self.log.debug("End of night {} at {}".format(night, end_of_night_str)) self.db.clear_data() down_days = self.dh.get_downtime(night) if down_days: self.log.info("Observatory is down: {} days.".format(down_days)) self.comm_time.is_down = True self.comm_time.down_duration = down_days self.comm_time.timestamp = self.time_handler.current_timestamp self.log.log( LoggingLevel.EXTENSIVE.value, "Downtime Start Night Timestamp sent: {:.6f}".format( self.time_handler.current_timestamp)) self.sal.put(self.comm_time) observatory_state = self.seq.get_observatory_state( self.time_handler.current_timestamp) self.log.log( LoggingLevel.EXTENSIVE.value, "Downtime Observatory State: {}".format( topic_strdict(observatory_state))) self.sal.put(observatory_state) delta = math.fabs(self.time_handler.current_timestamp - self.end_of_night) + SECONDS_IN_MINUTE self.time_handler.update_time(delta, "seconds") else: self.comm_time.is_down = False self.comm_time.down_duration = down_days
def start_night(self, night): """Perform actions at the start of the night. Parameters ---------- night : int The current night. """ self.log.info("Night {}".format(night)) self.seq.start_night(night, self.duration) self.comm_time.night = night self.seq.sky_model.update(self.time_handler.current_timestamp) (set_timestamp, rise_timestamp) = self.seq.sky_model.get_night_boundaries(self.conf.sched_driver.night_boundary) delta = math.fabs(self.time_handler.current_timestamp - set_timestamp) self.time_handler.update_time(delta, "seconds") self.log.debug("Start of night {} at {}".format(night, self.time_handler.current_timestring)) self.end_of_night = rise_timestamp end_of_night_str = self.time_handler.future_timestring(0, "seconds", timestamp=self.end_of_night) self.log.debug("End of night {} at {}".format(night, end_of_night_str)) self.db.clear_data() down_days = self.dh.get_downtime(night) if down_days: self.log.info("Observatory is down: {} days.".format(down_days)) self.comm_time.is_down = True self.comm_time.down_duration = down_days self.comm_time.timestamp = self.time_handler.current_timestamp self.log.log(LoggingLevel.EXTENSIVE.value, "Downtime Start Night Timestamp sent: {:.6f}" .format(self.time_handler.current_timestamp)) self.sal.put(self.comm_time) observatory_state = self.seq.get_observatory_state(self.time_handler.current_timestamp) self.log.log(LoggingLevel.EXTENSIVE.value, "Downtime Observatory State: {}".format(topic_strdict(observatory_state))) self.sal.put(observatory_state) delta = math.fabs(self.time_handler.current_timestamp - self.end_of_night) + SECONDS_IN_MINUTE self.time_handler.update_time(delta, "seconds") else: self.comm_time.is_down = False self.comm_time.down_duration = down_days
def run(self): """Run the simulation. """ self.log.info("Starting simulation") self.conf_comm.run() self.save_configuration() self.save_proposal_information() # Get fields from scheduler if self.wait_for_scheduler: self.log.info("Retrieving fields from Scheduler") field_set = set() fields_from_dds = 0 end_fields = False while True: rcode = self.sal.manager.getNextSample_field(self.field) if self.field.ID == 0: continue self.log.log(LoggingLevel.EXTENSIVE.value, self.field.ID) if rcode == 0 and self.field.ID == -1: if end_fields: break else: end_fields = True continue field_set.add((self.field.ID, self.field.fov, self.field.ra, self.field.dec, self.field.gl, self.field.gb, self.field.el, self.field.eb)) fields_from_dds += 1 time.sleep(0.00001) self.log.info("DDS retrieved {} field messages.".format(fields_from_dds)) self.field_list = [write_field(field, self.db.session_id) for field in sorted(field_set)] self.log.info("{} fields retrieved".format(len(self.field_list))) self.log.log(LoggingLevel.EXTENSIVE.value, "{}".format(self.field_list)) self.db.write_table("field", self.field_list) self.log.debug("Duration = {}".format(self.duration)) for night in xrange(1, int(self.duration) + 1): self.start_night(night) while self.time_handler.current_timestamp < self.end_of_night: self.comm_time.timestamp = self.time_handler.current_timestamp self.log.log(LoggingLevel.EXTENSIVE.value, "Timestamp sent: {:.6f}".format(self.time_handler.current_timestamp)) self.sal.put(self.comm_time) observatory_state = self.seq.get_observatory_state(self.time_handler.current_timestamp) self.log.log(LoggingLevel.EXTENSIVE.value, "Observatory State: {}".format(topic_strdict(observatory_state))) self.sal.put(observatory_state) self.cloud_model.set_topic(self.time_handler, self.cloud) self.sal.put(self.cloud) self.seeing_model.set_topic(self.time_handler, self.seeing) self.sal.put(self.seeing) self.get_target_from_scheduler() observation, slew_info, exposure_info = self.seq.observe_target(self.target, self.time_handler) # Add a few more things to the observation observation.night = night elapsed_time = self.time_handler.time_since_given(observation.observation_start_time) observation.cloud = self.cloud_model.get_cloud(elapsed_time) seeing_values = self.seeing_model.calculate_seeing(elapsed_time, observation.filter, observation.airmass) observation.seeing_fwhm_500 = seeing_values[0] observation.seeing_fwhm_geom = seeing_values[1] observation.seeing_fwhm_eff = seeing_values[2] # Pass observation back to scheduler self.sal.put(observation) # Wait for interested proposal information lastconfigtime = time.time() while self.wait_for_scheduler: rcode = self.sal.manager.getNextSample_interestedProposal(self.interested_proposal) if rcode == 0 and self.interested_proposal.num_proposals >= 0: self.log.log(LoggingLevel.EXTENSIVE.value, "Received interested proposal.") break else: tf = time.time() if (tf - lastconfigtime) > 5.0: self.log.log(LoggingLevel.EXTENSIVE.value, "Failed to receive interested proposal due to timeout.") break if self.wait_for_scheduler and observation.targetId != -1: self.db.append_data("target_history", self.target) self.db.append_data("observation_history", observation) self.gather_proposal_history("target", self.target) self.gather_proposal_history("observation", self.interested_proposal) for slew_type, slew_data in slew_info.items(): self.log.log(LoggingLevel.TRACE.value, "{}, {}".format(slew_type, type(slew_data))) if isinstance(slew_data, list): for data in slew_data: self.db.append_data(slew_type, data) else: self.db.append_data(slew_type, slew_data) for exposure_type in exposure_info: self.log.log(LoggingLevel.TRACE.value, "Adding {} to DB".format(exposure_type)) self.log.log(LoggingLevel.TRACE.value, "Number of exposures being added: " "{}".format(len(exposure_info[exposure_type]))) for exposure in exposure_info[exposure_type]: self.db.append_data(exposure_type, exposure) self.end_night() self.start_day()
def run(self): """Run the simulation. """ self.log.info("Starting simulation") self.conf_comm.run() self.save_configuration() self.save_proposal_information() self.save_field_information() self.log.debug("Duration = {}".format(self.duration)) for night in range(1, int(self.duration) + 1): self.start_night(night) while self.time_handler.current_timestamp < self.end_of_night: self.comm_time.timestamp = self.time_handler.current_timestamp self.log.log( LoggingLevel.EXTENSIVE.value, "Timestamp sent: {:.6f}".format( self.time_handler.current_timestamp)) self.sal.put(self.comm_time) observatory_state = self.seq.get_observatory_state( self.time_handler.current_timestamp) self.log.log( LoggingLevel.EXTENSIVE.value, "Observatory State: {}".format( topic_strdict(observatory_state))) self.sal.put(observatory_state) self.cloud_model.set_topic(self.time_handler, self.cloud) self.sal.put(self.cloud) self.seeing_model.set_topic(self.time_handler, self.seeing) self.sal.put(self.seeing) self.get_target_from_scheduler() observation, slew_info, exposure_info = self.seq.observe_target( self.target, self.time_handler) # Add a few more things to the observation observation.night = night elapsed_time = self.time_handler.time_since_given( observation.observation_start_time) observation.cloud = self.cloud_model.get_cloud(elapsed_time) seeing_values = self.seeing_model.calculate_seeing( elapsed_time, observation.filter, observation.airmass) observation.seeing_fwhm_500 = seeing_values[0] observation.seeing_fwhm_geom = seeing_values[1] observation.seeing_fwhm_eff = seeing_values[2] visit_exposure_time = sum([ observation.exposure_times[i] for i in range(observation.num_exposures) ]) observation.five_sigma_depth = m5_flat_sed( observation.filter, observation.sky_brightness, observation.seeing_fwhm_eff, visit_exposure_time, observation.airmass) # Pass observation back to scheduler self.log.log(LoggingLevel.EXTENSIVE.value, "tx: observation") self.sal.put(observation) # Wait for interested proposal information lastconfigtime = time.time() while self.wait_for_scheduler: rcode = self.sal.manager.getNextSample_interestedProposal( self.interested_proposal) if rcode == 0 and self.interested_proposal.num_proposals >= 0: self.log.log(LoggingLevel.EXTENSIVE.value, "Received interested proposal.") break else: tf = time.time() if (tf - lastconfigtime) > 5.0: self.log.log( LoggingLevel.EXTENSIVE.value, "Failed to receive interested proposal due to timeout." ) break if self.wait_for_scheduler and observation.targetId != -1: self.db.append_data("target_history", self.target) self.db.append_data("observation_history", observation) self.gather_proposal_history("target", self.target) self.gather_proposal_history("observation", self.interested_proposal) for slew_type, slew_data in slew_info.items(): self.log.log( LoggingLevel.TRACE.value, "{}, {}".format(slew_type, type(slew_data))) if isinstance(slew_data, list): for data in slew_data: self.db.append_data(slew_type, data) else: self.db.append_data(slew_type, slew_data) for exposure_type in exposure_info: self.log.log(LoggingLevel.TRACE.value, "Adding {} to DB".format(exposure_type)) self.log.log( LoggingLevel.TRACE.value, "Number of exposures being added: " "{}".format(len(exposure_info[exposure_type]))) for exposure in exposure_info[exposure_type]: self.db.append_data(exposure_type, exposure) self.end_night() self.start_day()
def test_topic_string_creation(self): output = topic_strdict(target) self.assertEqual(output.keys()[0], "airmass") self.assertEqual(output["targetId"], str(target.targetId)) self.assertEqual(output["angle"], "{:.3f}".format(target.angle))
def test_topic_string_creation_different_float_format(self): new_float_format = "{:.5f}" output = topic_strdict(target, float_format=new_float_format) self.assertEqual(output["angle"], new_float_format.format(target.angle))
def test_topic_string_creation(self): output = topic_strdict(target) self.assertEqual(list(output.keys())[0], "airmass") self.assertEqual(output["targetId"], str(target.targetId)) self.assertEqual(output["angle"], "{:.3f}".format(target.angle))