def setUpClass(cls):
     try:
         cls.sm = sbp.SkyModelPre(speedLoad=True)
         mjd = cls.sm.info['mjds'][1] + 4. / 60. / 24.
         tmp = cls.sm.returnMags(mjd)
         cls.data_present = True
     except:
         cls.data_present = False
 def test_various(self):
     """
     Test some various loading things
     """
     # check that the sims_data stuff loads
     sm = sbp.SkyModelPre(speedLoad=True)
     mjd = self.sm.info['mjds'][10]+0.1
     mags = sm.returnMags(mjd)
     # check that it'll load up something later properly
     mags = sm.returnMags(60000)
 def setUpClass(cls):
     try:
         cls.sm = sbp.SkyModelPre(speedLoad=True)
         # cls.sm_fields = sbp.SkyModelPre(speedLoad=True, opsimFields=False, useSimsData=False)
         mjd = cls.sm.info['mjds'][1]+4./60./24.
         tmp = cls.sm.returnMags(mjd)
         cls.data_present = True
     except:
         cls.data_present = False
         warnings.warn('Data files not found, skipping tests. Check data/ for instructions to pull data.')
    def testReturnMags(self):
        """
        Test all the ways ReturnMags can be used
        """
        # Check both the healpix and opsim fields
        if self.data_present:
            sms = [self.sm, sbp.SkyModelPre(speedLoad=True, opsimFields=True)]
            mjds = []
            for mjd in sms[0].info['mjds'][100:102]:
                mjds.append(mjd)
                mjds.append(mjd + .0002)

            # Make sure there's an mjd that is between sunrise/set that gets tested
            diff = sms[0].info['mjds'][1:] - sms[0].info['mjds'][0:-1]
            between = np.where(diff >= sms[0].header['timestep_max'])[0][0]
            mjds.append(sms[0].info['mjds'][between + 1] +
                        sms[0].header['timestep_max'])

            indxes = [None, [100, 101]]
            apply_masks = [True, False]
            apply_planets = [True, False]
            filters = [['u', 'g', 'r', 'i', 'z', 'y'], ['r']]

            for sm in sms:
                for mjd in mjds:
                    for indx in indxes:
                        for am in apply_masks:
                            for planet in apply_planets:
                                for filt in filters:
                                    mags = sm.returnMags(mjd,
                                                         indx=indx,
                                                         airmass_mask=am,
                                                         filters=filt,
                                                         planet_mask=planet)
                                    # Check the filters returned are correct
                                    self.assertEqual(len(filt),
                                                     len(list(mags.keys())))
                                    self.assertEqual(set(filt),
                                                     set(mags.keys()))
                                    airmasses = sm.returnAirmass(mjd,
                                                                 indx=indx)
                                    # Check the magnitudes are correct
                                    if indx is not None:
                                        self.assertEqual(
                                            np.size(
                                                mags[list(mags.keys())[0]]),
                                            np.size(indx))
                                        self.assertEqual(
                                            np.size(airmasses), np.size(indx))
Beispiel #5
0
def obs2sqlite(observations_in, location='LSST', outfile='observations.sqlite', slewtime_limit=5., 
               full_sky=False, radians=True):
    """
    Utility to take an array of observations and dump it to a sqlite file, filling in useful columns along the way.

    observations_in: numpy array with at least columns of
        ra : RA in degrees
        dec : dec in degrees
        mjd : MJD in day
        filter : string with the filter name
        exptime : the exposure time in seconds
    slewtime_limit : float
        Consider all slewtimes larger than this to be closed-dome time not part of a slew.
    """

    # Set the location to be LSST
    if location == 'LSST':
        telescope = Site('LSST')

    # Check that we have the columns we need
    needed_cols = ['ra', 'dec', 'mjd', 'filter']
    in_cols = observations_in.dtype.names
    for col in needed_cols:
        if needed_cols not in in_cols:
            ValueError('%s column not found in observtion array' % col)

    n_obs = observations_in.size
    sm = None
    # make sure they are in order by MJD
    observations_in.sort(order='mjd')

    # Take all the columns that are in the input and add any missing
    names = ['filter', 'ra', 'dec', 'mjd', 'exptime', 'alt', 'az', 'skybrightness',
             'seeing', 'night', 'slewtime', 'fivesigmadepth', 'airmass', 'sunAlt', 'moonAlt']
    types = ['|S1']
    types.extend([float]*(len(names)-1))

    observations = np.zeros(n_obs, dtype=list(zip(names, types)))

    # copy over the ones we have
    for col in in_cols:
        observations[col] = observations_in[col]

    # convert output to be in degrees like expected
    if radians:
        observations['ra'] = np.degrees(observations['ra'])
        observations['dec'] = np.degrees(observations['dec'])

    if 'exptime' not in in_cols:
        observations['exptime'] = 30.

    # Fill in the slewtime. Note that filterchange time gets included in slewtimes
    if 'slewtime' not in in_cols:
        # Assume MJD is midpoint of exposures
        mjd_sec = observations_in['mjd']*24.*3600.
        observations['slewtime'][1:] = mjd_sec[1:]-mjd_sec[0:-1] - observations['exptime'][0:-1]*0.5 - observations['exptime'][1:]*0.5
        closed = np.where(observations['slewtime'] > slewtime_limit*60.)
        observations['slewtime'][closed] = 0.

    # Let's just use the stupid-fast to get alt-az
    if 'alt' not in in_cols:
        alt, az = stupidFast_RaDec2AltAz(np.radians(observations['ra']), np.radians(observations['dec']),
                                         telescope.latitude_rad, telescope.longitude_rad, observations['mjd'])
        observations['alt'] = np.degrees(alt)
        observations['az'] = np.degrees(az)

    # Fill in the airmass
    if 'airmass' not in in_cols:
        observations['airmass'] = 1./np.cos(np.pi/2. - np.radians(observations['alt']))

    # Fill in the seeing
    if 'seeing' not in in_cols:
        # XXX just fill in a dummy val
        observations['seeing'] = 0.8

    if 'night' not in in_cols:
        m2n = mjd2night()
        observations['night'] = m2n(observations['mjd'])

    # Sky Brightness
    if 'skybrightness' not in in_cols:
        if full_sky:
            sm = SkyModel(mags=True)
            for i, obs in enumerate(observations):
                sm.setRaDecMjd(obs['ra'], obs['dec'], obs['mjd'], degrees=True)
                observations['skybrightness'][i] = sm.returnMags()[obs['filter']]
        else:
            # Let's try using the pre-computed sky brighntesses
            sm = sb.SkyModelPre(preload=False)
            full = sm.returnMags(observations['mjd'][0])
            nside = hp.npix2nside(full['r'].size)
            imax = float(np.size(observations))
            for i, obs in enumerate(observations):
                indx = raDec2Hpid(nside, obs['ra'], obs['dec'])
                observations['skybrightness'][i] = sm.returnMags(obs['mjd'], indx=[indx])[obs['filter']]
                sunMoon = sm.returnSunMoon(obs['mjd'])
                observations['sunAlt'][i] = sunMoon['sunAlt']
                observations['moonAlt'][i] = sunMoon['moonAlt']
                progress = i/imax*100
                text = "\rprogress = %.2f%%"%progress
                sys.stdout.write(text)
                sys.stdout.flush()
            observations['sunAlt'] = np.degrees(observations['sunAlt'])
            observations['moonAlt'] = np.degrees(observations['moonAlt'])


    # 5-sigma depth
    for fn in np.unique(observations['filter']):
        good = np.where(observations['filter'] == fn)
        observations['fivesigmadepth'][good] = m5_flat_sed(fn, observations['skybrightness'][good],
                                                           observations['seeing'][good],
                                                           observations['exptime'][good],
                                                           observations['airmass'][good])

    conn = sqlite3.connect(outfile)
    df = pd.DataFrame(observations)
    df.to_sql('observations', conn)
Beispiel #6
0
    def __init__(self,
                 nside=None,
                 mjd_start=59853.5,
                 seed=42,
                 quickTest=True,
                 alt_min=5.,
                 lax_dome=True,
                 cloud_limit=0.3,
                 sim_ToO=None,
                 seeing_db=None,
                 cloud_db=None,
                 cloud_offset_year=0):
        """
        Parameters
        ----------
        nside : int (None)
            The healpix nside resolution
        mjd_start : float (59853.5)
            The MJD to start the observatory up at
        alt_min : float (5.)
            The minimum altitude to compute models at (degrees).
        lax_dome : bool (True)
            Passed to observatory model. If true, allows dome creep.
        cloud_limit : float (0.3)
            The limit to stop taking observations if the cloud model returns something equal or higher
        sim_ToO : sim_targetoO object (None)
            If one would like to inject simulated ToOs into the telemetry stream.
        seeing_db : filename of the seeing data database (None)
            If one would like to use an alternate seeing database
        cloud_offset_year : float, opt
            Offset into the cloud database by 'offset_year' years. Default 0.
        cloud_db : filename of the cloud data database (None)
            If one would like to use an alternate seeing database
        """

        if nside is None:
            nside = set_default_nside()
        self.nside = nside

        self.cloud_limit = cloud_limit

        self.alt_min = np.radians(alt_min)
        self.lax_dome = lax_dome

        self.mjd_start = mjd_start

        # Conditions object to update and return on request
        self.conditions = Conditions(nside=self.nside)

        self.sim_ToO = sim_ToO

        # Create an astropy location
        self.site = Site('LSST')
        self.location = EarthLocation(lat=self.site.latitude,
                                      lon=self.site.longitude,
                                      height=self.site.height)

        # Load up all the models we need

        mjd_start_time = Time(self.mjd_start, format='mjd')
        # Downtime
        self.down_nights = []
        self.sched_downtime_data = ScheduledDowntimeData(mjd_start_time)
        self.unsched_downtime_data = UnscheduledDowntimeData(mjd_start_time)

        sched_downtimes = self.sched_downtime_data()
        unsched_downtimes = self.unsched_downtime_data()

        down_starts = []
        down_ends = []
        for dt in sched_downtimes:
            down_starts.append(dt['start'].mjd)
            down_ends.append(dt['end'].mjd)
        for dt in unsched_downtimes:
            down_starts.append(dt['start'].mjd)
            down_ends.append(dt['end'].mjd)

        self.downtimes = np.array(list(zip(down_starts, down_ends)),
                                  dtype=list(
                                      zip(['start', 'end'], [float, float])))
        self.downtimes.sort(order='start')

        # Make sure there aren't any overlapping downtimes
        diff = self.downtimes['start'][1:] - self.downtimes['end'][0:-1]
        while np.min(diff) < 0:
            # Should be able to do this wihtout a loop, but this works
            for i, dt in enumerate(self.downtimes[0:-1]):
                if self.downtimes['start'][i + 1] < dt['end']:
                    new_end = np.max([dt['end'], self.downtimes['end'][i + 1]])
                    self.downtimes[i]['end'] = new_end
                    self.downtimes[i + 1]['end'] = new_end

            good = np.where(
                self.downtimes['end'] - np.roll(self.downtimes['end'], 1) != 0)
            self.downtimes = self.downtimes[good]
            diff = self.downtimes['start'][1:] - self.downtimes['end'][0:-1]

        self.seeing_data = SeeingData(mjd_start_time, seeing_db=seeing_db)
        self.seeing_model = SeeingModel()
        self.seeing_indx_dict = {}
        for i, filtername in enumerate(self.seeing_model.filter_list):
            self.seeing_indx_dict[filtername] = i

        self.cloud_data = CloudData(mjd_start_time,
                                    cloud_db=cloud_db,
                                    offset_year=cloud_offset_year)
        sched_logger.info(
            f"Using {self.cloud_data.cloud_db} as cloud database with start year {self.cloud_data.start_time.iso}"
        )

        self.sky_model = sb.SkyModelPre(speedLoad=quickTest)

        self.observatory = ExtendedObservatoryModel()
        self.observatory.configure_from_module()
        # Make it so it respects my requested rotator angles
        self.observatory.params.rotator_followsky = True

        self.filterlist = ['u', 'g', 'r', 'i', 'z', 'y']
        self.seeing_FWHMeff = {}
        for key in self.filterlist:
            self.seeing_FWHMeff[key] = np.zeros(hp.nside2npix(self.nside),
                                                dtype=float)

        self.almanac = Almanac(mjd_start=mjd_start)

        # Let's make sure we're at an openable MJD
        good_mjd = False
        to_set_mjd = mjd_start
        while not good_mjd:
            good_mjd, to_set_mjd = self.check_mjd(to_set_mjd)
        self.mjd = to_set_mjd

        self.obsID_counter = 0
Beispiel #7
0
    def __init__(self, mjd_start=59853.5,
                 readtime=2., filtername=None, f_change_time=140., shutter_time=1.,
                 nside=default_nside, sun_limit=-13., quickTest=True, alt_limit=20.,
                 seed=-1, cloud_limit=0.699, cloud_step=15.,
                 scheduled_downtime=None, unscheduled_downtime=None,
                 seeing_model=None, cloud_model=None,
                 environment=None, filters=None):
        """
        Parameters
        ----------
        mjd_start : float (59853.5)
            The Modified Julian Date to set the observatory to.
        readtime : float (2.)
            The time it takes to read out the camera (seconds).
        settle : float (2.)
            The time it takes the telescope to settle after slewing (seconds)
        filtername : str (None)
            The filter to start the observatory loaded with
        f_change_time : float (120.)
            The time it takes to change filters (seconds)
        shutter_time : float (1.)
            The time it takes to open or close the shutter.
        nside : int (32)
            The healpixel nside to make sky calculations on.
        sun_limit : float (-12.)
            The altitude limit for the sun (degrees)
        quickTest : bool (True)
            Load only a small pre-computed sky array rather than a full year.
        seed : float
            Random seed to potentially pass to unscheduled downtime
        cloud_limit : float (0.699)
            Close dome for cloud values over this (traditionally measured in 8ths of the sky?)
        cloud_step : float (15.)
            Minutes to close if clouds exceed cloud_limit
        scheduled_downtime : ScheduledDowntime (None)
            The scheduled downtime interface. If None (default) use sims_ocs module.
        unscheduled_downtime : UnscheduledDowntime (None)
            The unscheduled downtime interface. If None (default) use sims_ocs module.
        seeing_model : SeeingModel (None)
            The seeing model interface. If None (default) use sims_ocs module.
        cloud_model : CloudModel (None)
            The cloud model interface. If None (default) use sims_ocs module.
        environment : Environment (None)
            The environment interface. If None (default) use sims_ocs module.
        filters : Filters (None)
            The Filters interface. If None (default) use sims_ocs module.
        """
        # Make it easy to see what version the object is
        self.version = version

        self.mjd_start = mjd_start + 0
        self.mjd = mjd_start
        self.f_change_time = f_change_time
        self.readtime = readtime
        self.shutter_time = shutter_time
        self.sun_limit = np.radians(sun_limit)
        self.alt_limit = np.radians(alt_limit)
        # Load up the sky brightness model
        self.sky = sb.SkyModelPre(speedLoad=quickTest)
        # Should realy set this by inspecting the map.
        self.sky_nside = 32

        # Start out parked
        self.ra = None
        self.dec = None
        self.filtername = None

        # Set up all sky coordinates
        hpids = np.arange(hp.nside2npix(self.sky_nside))
        self.ra_all_sky, self.dec_all_sky = _hpid2RaDec(self.sky_nside, hpids)
        self.status = None

        self.site = Site(name='LSST')
        self.obs = ephem.Observer()
        self.obs.lat = self.site.latitude_rad
        self.obs.lon = self.site.longitude_rad
        self.obs.elevation = self.site.height

        self.obs.horizon = 0.

        self.sun = ephem.Sun()
        self.moon = ephem.Moon()

        # Generate sunset times so we can label nights by integers
        self.generate_sunsets()
        self.night = self.mjd2night(self.mjd)

        # Make my dummy time handler
        dth = dummy_time_handler(mjd_start)

        # Make a slewtime interpolator
        self.slew_interp = Slewtime_pre()

        # Compute downtimes
        self.down_nights = []
        if scheduled_downtime is not None:
            sdt = scheduled_downtime
        else:
            sdt = ScheduledDowntime()
        sdt.initialize()

        if unscheduled_downtime is not None:
            usdt = unscheduled_downtime
        else:
            usdt = UnscheduledDowntime()
        usdt.initialize(random_seed=seed)

        for downtime in sdt.downtimes:
            self.down_nights.extend(range(downtime[0], downtime[0]+downtime[1], 1))
        for downtime in usdt.downtimes:
            self.down_nights.extend(range(downtime[0], downtime[0]+downtime[1], 1))
        self.down_nights.sort()

        if seeing_model is not None:
            self.seeing_model = seeing_model
        else:
            self.seeing_model = SeeingSim(dth)

        if cloud_model is not None:
            self.cloud_model = cloud_model
        else:
            self.cloud_model = CloudModel(dth)
            self.cloud_model.read_data()

        self.cloud_limit = cloud_limit
        self.cloud_step = cloud_step/60./24.
Beispiel #8
0
import numpy as np
import lsst.sims.skybrightness_pre as sb

if __name__ == '__main__':
    sm = sb.SkyModelPre(preload=False, verbose=True)

    mjd0 = 59031.
    stepsize = 10. # 60
    mjds = np.arange(mjd0, mjd0+365.25*10+stepsize, stepsize)
    for mjd in mjds:
        mags = sm.returnMags(mjd)

    

"""
I think most of these are going to be loading off my spinny disk:

Loading file /Users/yoachim/gitRepos/sims_skybrightness_pre/data/healpix/59194_59407.npz
also loading /Users/yoachim/gitRepos/sims_skybrightness_pre/data/healpix/59194_59407.npy
59194_59407.npz loaded
Loading file /Users/yoachim/gitRepos/sims_skybrightness_pre/data/healpix/59377_59590.npz
also loading /Users/yoachim/gitRepos/sims_skybrightness_pre/data/healpix/59377_59590.npy
59377_59590.npz loaded
Loading file /Users/yoachim/gitRepos/sims_skybrightness_pre/data/healpix/59560_59772.npz
also loading /Users/yoachim/gitRepos/sims_skybrightness_pre/data/healpix/59560_59772.npy
59560_59772.npz loaded
Loading file /Users/yoachim/gitRepos/sims_skybrightness_pre/data/healpix/59743_59955.npz
also loading /Users/yoachim/gitRepos/sims_skybrightness_pre/data/healpix/59743_59955.npy
59743_59955.npz loaded
Loading file /Users/yoachim/gitRepos/sims_skybrightness_pre/data/healpix/59926_60138.npz
also loading /Users/yoachim/gitRepos/sims_skybrightness_pre/data/healpix/59926_60138.npy
Beispiel #9
0
    def __init__(self,
                 mjd_start=59580.035,
                 readtime=2.,
                 filtername=None,
                 f_change_time=140.,
                 nside=default_nside,
                 sun_limit=-13.,
                 quickTest=True,
                 alt_limit=20.,
                 seed=-1,
                 cloud_limit=7.,
                 cloud_step=15.):
        """
        Parameters
        ----------
        mjd_start : float (59580.035)
            The Modified Julian Date to set the observatory to.
        readtime : float (2.)
            The time it takes to read out the camera (seconds).
        settle : float (2.)
            The time it takes the telescope to settle after slewing (seconds)
        filtername : str (None)
            The filter to start the observatory loaded with
        f_change_time : float (120.)
            The time it takes to change filters (seconds)
        nside : int (32)
            The healpixel nside to make sky calculations on.
        sun_limit : float (-12.)
            The altitude limit for the sun (degrees)
        quickTest : bool (True)
            Load only a small pre-computed sky array rather than a full year.
        seed : float
            Random seed to potentially pass to unscheduled downtime
        cloud_limit : float (7)
            Close dome for cloud values over this (traditionally measured in 8ths of the sky)
        cloud_step : float (15.)
            Minutes to close if clouds exceed cloud_limit
        """
        self.mjd_start = mjd_start + 0
        self.mjd = mjd_start
        self.f_change_time = f_change_time
        self.readtime = readtime
        self.sun_limit = np.radians(sun_limit)
        self.alt_limit = np.radians(alt_limit)
        # Load up the sky brightness model
        self.sky = sb.SkyModelPre(preload=False, speedLoad=quickTest)
        # Should realy set this by inspecting the map.
        self.sky_nside = 32

        # Start out parked
        self.ra = None
        self.dec = None
        self.filtername = None

        # Set up all sky coordinates
        hpids = np.arange(hp.nside2npix(self.sky_nside))
        self.ra_all_sky, self.dec_all_sky = _hpid2RaDec(self.sky_nside, hpids)
        self.status = None

        self.site = Site(name='LSST')
        self.obs = ephem.Observer()
        self.obs.lat = self.site.latitude_rad
        self.obs.lon = self.site.longitude_rad
        self.obs.elevation = self.site.height

        self.obs.horizon = 0.

        self.sun = ephem.Sun()

        # Generate sunset times so we can label nights by integers
        self.generate_sunsets()
        self.night = self.mjd2night(self.mjd)

        # Make a slewtime interpolator
        self.slew_interp = Slewtime_pre()

        # Compute downtimes
        self.down_nights = []
        sdt = ScheduledDowntime()
        sdt.initialize()
        usdt = UnscheduledDowntime()
        usdt.initialize(random_seed=seed)
        for downtime in sdt.downtimes:
            self.down_nights.extend(
                range(downtime[0], downtime[0] + downtime[1], 1))
        for downtime in usdt.downtimes:
            self.down_nights.extend(
                range(downtime[0], downtime[0] + downtime[1], 1))
        self.down_nights.sort()

        # Instatiate a seeing model
        env_config = Environment()
        filter_config = Filters()
        self.seeing_model = SeeingModel_no_time()
        self.seeing_model.initialize(env_config, filter_config)

        self.cloud_model = CloudModel_no_time()
        self.cloud_model.initialize()
        self.cloud_limit = cloud_limit
        self.cloud_step = cloud_step / 60. / 24.