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 warnings 

2import sys 

3import numpy as np 

4from lsst.sims.featureScheduler.utils import run_info_table, schema_converter 

5from lsst.sims.featureScheduler.schedulers import simple_filter_sched 

6import time 

7import sqlite3 

8import pandas as pd 

9 

10__all__ = ['sim_runner'] 

11 

12 

13def sim_runner(observatory, scheduler, filter_scheduler=None, mjd_start=None, survey_length=3., 

14 filename=None, delete_past=True, n_visit_limit=None, step_none=15., verbose=True, 

15 extra_info=None, event_table=None): 

16 """ 

17 run a simulation 

18 

19 Parameters 

20 ---------- 

21 survey_length : float (3.) 

22 The length of the survey ot run (days) 

23 step_none : float (15) 

24 The amount of time to advance if the scheduler fails to return a target (minutes). 

25 extra_info : dict (None) 

26 If present, dict gets added onto the information from the observatory model. 

27 event_table : np.array (None) 

28 Any ToO events that were included in the simulation 

29 """ 

30 

31 if extra_info is None: 

32 extra_info = {} 

33 

34 t0 = time.time() 

35 

36 if filter_scheduler is None: 

37 filter_scheduler = simple_filter_sched() 

38 

39 if mjd_start is None: 

40 mjd = observatory.mjd + 0 

41 mjd_start = mjd + 0 

42 else: 

43 mjd = mjd_start + 0 

44 observatory.mjd = mjd 

45 

46 end_mjd = mjd + survey_length 

47 observations = [] 

48 mjd_track = mjd + 0 

49 step = 1./24. 

50 step_none = step_none/60./24. # to days 

51 mjd_run = end_mjd-mjd_start 

52 nskip = 0 

53 new_night = False 

54 

55 while mjd < end_mjd: 

56 if not scheduler._check_queue_mjd_only(observatory.mjd): 

57 scheduler.update_conditions(observatory.return_conditions()) 

58 desired_obs = scheduler.request_observation(mjd=observatory.mjd) 

59 if desired_obs is None: 

60 # No observation. Just step into the future and try again. 

61 warnings.warn('No observation. Step into the future and trying again.') 

62 observatory.mjd = observatory.mjd + step_none 

63 scheduler.update_conditions(observatory.return_conditions()) 

64 nskip += 1 

65 continue 

66 completed_obs, new_night = observatory.observe(desired_obs) 

67 if completed_obs is not None: 

68 scheduler.add_observation(completed_obs[0]) 

69 observations.append(completed_obs) 

70 filter_scheduler.add_observation(completed_obs[0]) 

71 else: 

72 scheduler.flush_queue() 

73 if new_night: 

74 # find out what filters we want mounted 

75 conditions = observatory.return_conditions() 

76 filters_needed = filter_scheduler(conditions) 

77 swap_out = np.setdiff1d(conditions.mounted_filters, filters_needed) 

78 for filtername in swap_out: 

79 # ugh, "swap_filter" means "unmount filter" 

80 observatory.observatory.swap_filter(filtername) 

81 

82 mjd = observatory.mjd + 0 

83 if verbose: 

84 if (mjd-mjd_track) > step: 

85 progress = float(mjd-mjd_start)/mjd_run*100 

86 text = "\rprogress = %.2f%%" % progress 

87 sys.stdout.write(text) 

88 sys.stdout.flush() 

89 mjd_track = mjd+0 

90 if n_visit_limit is not None: 

91 if len(observations) == n_visit_limit: 

92 break 

93 # XXX--handy place to interupt and debug 

94 # if len(observations) > 3: 

95 # import pdb ; pdb.set_trace() 

96 runtime = time.time() - t0 

97 print('Skipped %i observations' % nskip) 

98 print('Flushed %i observations from queue for being stale' % scheduler.flushed) 

99 print('Completed %i observations' % len(observations)) 

100 print('ran in %i min = %.1f hours' % (runtime/60., runtime/3600.)) 

101 print('Writing results to ', filename) 

102 observations = np.array(observations)[:, 0] 

103 if filename is not None: 

104 info = run_info_table(observatory, extra_info=extra_info) 

105 converter = schema_converter() 

106 converter.obs2opsim(observations, filename=filename, info=info, delete_past=delete_past) 

107 if event_table is not None: 

108 df = pd.DataFrame(event_table) 

109 con = sqlite3.connect(filename) 

110 df.to_sql('events', con) 

111 con.close() 

112 return observatory, scheduler, observations