Coverage for python/lsst/sims/featureScheduler/sim_runner.py : 9%

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
10__all__ = ['sim_runner']
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
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 """
31 if extra_info is None:
32 extra_info = {}
34 t0 = time.time()
36 if filter_scheduler is None:
37 filter_scheduler = simple_filter_sched()
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
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
55 mjd_last_flush = -1
57 while mjd < end_mjd:
58 if not scheduler._check_queue_mjd_only(observatory.mjd):
59 scheduler.update_conditions(observatory.return_conditions())
60 desired_obs = scheduler.request_observation(mjd=observatory.mjd)
61 if desired_obs is None:
62 # No observation. Just step into the future and try again.
63 warnings.warn('No observation. Step into the future and trying again.')
64 observatory.mjd = observatory.mjd + step_none
65 scheduler.update_conditions(observatory.return_conditions())
66 nskip += 1
67 continue
68 completed_obs, new_night = observatory.observe(desired_obs)
69 if completed_obs is not None:
70 scheduler.add_observation(completed_obs[0])
71 observations.append(completed_obs)
72 filter_scheduler.add_observation(completed_obs[0])
73 else:
74 # An observation failed to execute, usually it was outside the altitude limits.
75 if observatory.mjd == mjd_last_flush:
76 raise RuntimeError("Scheduler has failed to provide a valid observation multiple times.")
77 # if this is a first offence, might just be that targets set. Flush queue and get some new targets.
78 scheduler.flush_queue()
79 mjd_last_flush = observatory.mjd + 0
80 if new_night:
81 # find out what filters we want mounted
82 conditions = observatory.return_conditions()
83 filters_needed = filter_scheduler(conditions)
84 observatory.observatory.mount_filters(filters_needed)
86 mjd = observatory.mjd + 0
87 if verbose:
88 if (mjd-mjd_track) > step:
89 progress = float(mjd-mjd_start)/mjd_run*100
90 text = "\rprogress = %.2f%%" % progress
91 sys.stdout.write(text)
92 sys.stdout.flush()
93 mjd_track = mjd+0
94 if n_visit_limit is not None:
95 if len(observations) == n_visit_limit:
96 break
97 # XXX--handy place to interupt and debug
98 #if len(observations) > 25:
99 # import pdb ; pdb.set_trace()
100 runtime = time.time() - t0
101 print('Skipped %i observations' % nskip)
102 print('Flushed %i observations from queue for being stale' % scheduler.flushed)
103 print('Completed %i observations' % len(observations))
104 print('ran in %i min = %.1f hours' % (runtime/60., runtime/3600.))
105 print('Writing results to ', filename)
106 observations = np.array(observations)[:, 0]
107 if filename is not None:
108 info = run_info_table(observatory, extra_info=extra_info)
109 converter = schema_converter()
110 converter.obs2opsim(observations, filename=filename, info=info, delete_past=delete_past)
111 if event_table is not None:
112 df = pd.DataFrame(event_table)
113 con = sqlite3.connect(filename)
114 df.to_sql('events', con)
115 con.close()
116 return observatory, scheduler, observations