Coverage for python/lsst/sims/skybrightness_pre/SkyModelPre.py : 70%

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
""" from https://gist.github.com/shaunlebron/8832585 """
""" Interpolate angle values (handle wrap around properly). Does nearest neighbor interpolation if values out of range.
Parameters ---------- x_out : float (or array) The points to interpolate to. xp : array Points to interpolate between (must be sorted) anglep : array The angles ascociated with xp degrees : bool (False) Set if anglep is degrees (True) or radidian (False) """
# Where are the interpolation points
# If we are out of bounds, just use the edges
result = np.radians(anglep[left]) + shortAngleDist(np.radians(anglep[left]), np.radians(anglep[right]))*wterm result = result % (2.*np.pi) result = np.degrees(result) else:
""" Load pre-computed sky brighntess maps for the LSST site and use them to interpolate to arbitrary dates. """
speedLoad=True, verbose=False): """ Parameters ---------- data_path : str (None) path to the numpy save files. Looks in standard plances if set to None. opsimFields : bool (False) Mostly depreciated, if True, loads sky brightnesses computed at field centers. Otherwise uses healpixels. speedLoad : bool (True) If True, use the small 3-day file to load found in the usual spot. """
# Look in default location for .npz files to load data_dir = os.environ['SIMS_SKYBRIGHTNESS_DATA'] else:
data_path = os.path.join(data_dir, 'opsimFields') else:
errmssg = 'Failed to find pre-computed .npz files. ' errmssg += 'Copy data from NCSA with sims_skybrightness_pre/data/data_down.sh \n' errmssg += 'or build by running sims_skybrightness_pre/data/generate_sky.py' warnings.warn(errmssg) # Expect filenames of the form mjd1_mjd2.npz, e.g., 59632.155_59633.2.npz
# Set that nothing is loaded at this point
# Go ahead and load the small one in the repo by default filename=os.path.join(data_dir, 'healpix/59853_59856.npz'), npyfile=os.path.join(data_dir, 'healpix/59853_59856.npy'))
""" Load up the .npz file to interpolate things. After python 3 upgrade, numpy.savez refused to write large .npz files, so data is split between .npz and .npy files.
Parameters ---------- mjd : float The Modified Julian Date that we want to load filename : str (None) The filename to restore. If None, it checks the filenames on disk to find one that should have the requested MJD npyfile : str (None) If sky brightness data not in npz file, checks the .npy file with same root name. """
# Figure out which file to load. file_indx = np.where((mjd >= self.mjd_left) & (mjd <= self.mjd_right))[0] if np.size(file_indx) == 0: raise ValueError('MJD = %f is out of range for the files found (%f-%f)' % (mjd, self.mjd_left.min(), self.mjd_right.max())) filename = self.files[file_indx.max()] self.loaded_range = np.array([self.mjd_left[file_indx], self.mjd_right[file_indx]]) else:
print('Loading file %s' % filename) # Add encoding kwarg to restore Python 2.7 generated files self.sb = data['sky_brightness'][()] data.close() else: # the sky brightness had to go in it's own npy file npyfile = filename[:-3]+'npy' print('also loading %s' % npyfile)
# Step to make sure keys are strings not bytes selfDict[key.decode("utf-8")] = selfDict.pop(key)
# Ugh, different versions of the save files could have dicts or np.array. # Let's hope someone fits some Fourier components to the sky brightnesses and gets rid # of the giant save files for good. self.filter_names = list(self.sb.keys()) else:
print('%s loaded' % os.path.split(filename)[1])
""" Parameters ---------- mjd : float Modified Julian Date(s) to interpolate to
Returns ------- sunMoon : dict Dict with keys for the sun and moon RA and Dec and the mooon-sun separation. All values in radians, except for moonSunSep that is in degrees for some reason (that reason is probably because I'm sloppy). """
#warnings.warn('Method returnSunMoon to be depreciated. Interpolating angles is bad!')
'sunDecs', 'moonSunSep']
self._load_data(mjd)
else: # Return a scalar if only doing 1 date. else:
"""
Parameters ---------- mjd : float Modified Julian Date to interpolate to indx : List of int(s) (None) indices to interpolate the sky values at. Returns full sky if None. If the class was instatiated with opsimFields, indx is the field ID, otherwise it is the healpix ID. maxAM : float (10) The maximum airmass to return, everything above this airmass will be set to badval
Returns ------- airmass : np.array Array of airmass values. If the MJD is between sunrise and sunset, all values are masked. """ self._load_data(mjd)
# If we are out of bounds right -= 1 baseline = 1. left += 1 baseline = 1. else:
else: # Check if we are between sunrise/set (self.info['airmass'][closest_indx, indx].ravel() > maxAM))
else: (self.info['airmass'][left, indx] > maxAM) | (self.info['airmass'][right, indx] < 1.) | (self.info['airmass'][right, indx] > maxAM))
moon_mask=True, zenith_mask=True, badval=hp.UNSEEN, filters=['u', 'g', 'r', 'i', 'z', 'y'], extrapolate=False): """ Return a full sky map or individual pixels for the input mjd
Parameters ---------- mjd : float Modified Julian Date to interpolate to indx : List of int(s) (None) indices to interpolate the sky values at. Returns full sky if None. If the class was instatiated with opsimFields, indx is the field ID, otherwise it is the healpix ID. airmass_mask : bool (True) Set high (>2.5) airmass pixels to badval. planet_mask : bool (True) Set sky maps to badval near (2 degrees) bright planets. moon_mask : bool (True) Set sky maps near (10 degrees) the moon to badval. zenith_mask : bool (True) Set sky maps at high altitude (>86.5) to badval. badval : float (-1.6375e30) Mask value. Defaults to the healpy mask value. filters : list List of strings for the filters that should be returned. extrapolate : bool (False) In indx is set, extrapolate any masked pixels to be the same as the nearest non-masked value from the full sky map.
Returns ------- sbs : dict A dictionary with filter names as keys and np.arrays as values which hold the sky brightness maps in mag/sq arcsec. """ self._load_data(mjd)
'moon': moon_mask, 'zenith': zenith_mask}
# Do full sky by default else:
# If we are out of bounds right -= 1 baseline = 1. left += 1 baseline = 1. else:
# Check if we are between sunrise/set else: self.sb[filter_name][right, indx] * w2 self.info[mask_name+'_masks'][right, indx] | np.isinf(sbs[filter_name]))
# If requested a certain pixel(s), and want to extrapolate. masked_pix = False for filter_name in filters: if (badval in sbs[filter_name]) | (True in np.isnan(sbs[filter_name])): masked_pix = True if masked_pix: # We have pixels that are masked that we want reasonable values for full_sky_sb = self.returnMags(mjd, airmass_mask=False, planet_mask=False, moon_mask=False, zenith_mask=False, filters=filters) good = np.where((full_sky_sb[filters[0]] != badval) & ~np.isnan(full_sky_sb[filters[0]]))[0] ra_full = np.radians(self.header['ra'][good]) dec_full = np.radians(self.header['dec'][good]) for filtername in filters: full_sky_sb[filtername] = full_sky_sb[filtername][good] # Going to assume the masked pixels are the same in all filters masked_indx = np.where((sbs[filters[0]].ravel() == badval) | np.isnan(sbs[filters[0]].ravel()))[0] for i, mi in enumerate(masked_indx): # Note, this is going to be really slow for many pixels, should use a kdtree dist = _angularSeparation(np.radians(self.header['ra'][indx][i]), np.radians(self.header['dec'][indx][i]), ra_full, dec_full) closest = np.where(dist == dist.min())[0] for filtername in filters: sbs[filtername].ravel()[mi] = np.min(full_sky_sb[filtername][closest])
|