Coverage for python/lsst/sims/skybrightness_pre/m5percentiles.py : 20%

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
1from builtins import object
2import numpy as np
3import healpy as hp
4import os
5from lsst.utils import getPackageDir
8def ss_split(arr, side='left'):
9 """
10 make it possible to run searchsorted easily on 2 dims
11 """
12 result = np.searchsorted(arr[1:], arr[0], side=side)
13 return result
16class M5percentiles(object):
17 """
18 Take a map of five-sigma limiting depths and convert it to a map of percentiles
19 """
21 def __init__(self):
23 # Load up the saved maps
24 path = getPackageDir('sims_skybrightness_pre')
25 filename = 'data/percentile_m5_maps.npz'
26 temp = np.load(os.path.join(path, filename))
27 self.m5_histograms = temp['histograms'].copy().T
28 self.histogram_npts = temp['histogram_npts'].copy()
29 temp.close()
30 self.npix = self.m5_histograms['u'][:, 0].size
31 self.nside = hp.npix2nside(self.npix)
32 self.nbins = float(self.m5_histograms['u'][0, :].size)
33 # The center of each histogram bin
34 self.percentiles = np.arange(self.nbins)/self.nbins +1./2/self.nbins
36 def dark_map(self, filtername='r', nside_out=64):
37 """Return the darkest every healpixel gets
38 """
39 result = self.m5_histograms[filtername][:, -1]
40 if self.nside != nside_out:
41 result = hp.ud_grade(result, nside_out=nside_out)
42 return result
44 def percentile2m5map(self, percentile, filtername='r', nside=None):
45 """
46 Given a percentile, return the 5-sigma map for that level
48 Parameters
49 ----------
50 percentile : float
51 Value between 0-1.
52 """
53 if nside is None:
54 nside = self.nside
56 diff = np.abs(percentile - self.percentiles)
57 closest = np.where(diff == diff.min())[0].min()
58 result = self.m5_histograms[filtername][:, closest]
59 result = hp.ud_grade(result, nside)
60 return result
62 def m5map2percentile(self, m5map, filtername='r'):
63 """
64 Convert a healpix map to a percentile map
65 """
66 orig_mask = np.where(m5map == hp.UNSEEN)
68 inNside = hp.npix2nside(m5map.size)
69 if inNside != self.nside:
70 m5map = hp.ud_grade(m5map, nside_out=self.nside, pess=False)
72 goodPix = np.where(m5map != hp.UNSEEN)[0]
74 result = np.empty(self.npix, dtype=float)
75 result.fill(hp.UNSEEN)
76 temp_array = np.column_stack((m5map[goodPix], self.m5_histograms[filtername][goodPix, :]))
77 result[goodPix] = np.apply_along_axis(ss_split, 1, temp_array)/self.nbins
79 # convert the percentiles back to the right nside if needed
80 # XXX--I should make a better linear interpolation upgrade function.
81 if inNside != self.nside:
82 result = hp.ud_grade(result, nside_out=inNside)
83 result[orig_mask] = hp.UNSEEN
84 return result