This repository has been archived by the owner on Dec 19, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
slr_zeropoint_shiftmap.py
145 lines (120 loc) · 6.91 KB
/
slr_zeropoint_shiftmap.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import pyfits
import healpy
import numpy
'''
try:
import pylab
pylab.ion()
except:
print 'Could not import pylab, no plotting functions will be available.'
'''
############################################################
class SLRZeropointShiftmap:
'''
Class for applying stellar locus regression (SLR) shifted zeropoints
'''
def __init__(self, zeropoint_shiftmap_file, fill_periphery=True):
'''
Input: name of SLR zeropoint map FITS file
'''
self.zeropoint_shiftmap = pyfits.open(zeropoint_shiftmap_file)[1]
self.nside = healpy.npix2nside(len(self.zeropoint_shiftmap.data))
self.fill_periphery = fill_periphery
#print 'SLR zeropoint shiftmap = %s'%(zeropoint_shiftmap_file)
#print 'Filters = %s'%(', '.join(self.zeropoint_shiftmap.data.names))
#print 'NSIDE = %i (~%.1f arcmin)'%(self.nside, healpy.nside2resol(self.nside, True))
#print 'Area = %.2f deg**2'%(healpy.nside2pixarea(self.nside, degrees=True) * numpy.sum(self.zeropoint_shiftmap.data.field('n_des') > 0))
if self.fill_periphery:
#print 'Filling in periphery pixels...'
for filter in self.zeropoint_shiftmap.data.names:
if 'n_' not in filter:
self._fillPeriphery(filter)
#print 'Peripheral Area = %.2f deg**2'%(self.peripheral_area)
else:
self.peripheral_area = 0.
def _fillPeriphery(self, filter):
'''
Fill in peripheral cells of shiftmap with average of nearest neighbors.
'''
all_neighbor_pix = numpy.unique(healpy.get_all_neighbours(self.nside,
numpy.nonzero(self.zeropoint_shiftmap.data.field(filter) != healpy.UNSEEN)[0]))
filled_pix = numpy.nonzero(self.zeropoint_shiftmap.data.field(filter) != healpy.UNSEEN)[0]
periphery_pix = numpy.setdiff1d(all_neighbor_pix, filled_pix)
shiftmap_filled = numpy.ma.masked_array(self.zeropoint_shiftmap.data.field(filter),
self.zeropoint_shiftmap.data.field(filter) == healpy.UNSEEN)
self.zeropoint_shiftmap.data.field(filter)[periphery_pix] = numpy.array(numpy.mean(shiftmap_filled[healpy.get_all_neighbours(self.nside,
periphery_pix)],
axis=0), dtype=shiftmap_filled.dtype)
self.peripheral_area = healpy.nside2pixarea(self.nside, degrees=True) * len(periphery_pix)
def plot(self, filter, ra=None, dec=None):
'''
Plot zoomed zeropoint shiftmap for chosen filter centered on (ra, dec) given in degrees,
or simply Mollweide all-sky map by default.
'''
if ra is not None and dec is not None:
healpy.gnomview(self.zeropoint_shiftmap.data.field(filter), rot=(ra, dec, 0))
else:
healpy.mollview(self.zeropoint_shiftmap.data.field(filter))
def addZeropoint(self, filter, ra, dec, mag, interpolate=True, plot=False):
'''
Inputs: filter name {g, r, i, z}, arrays for ra (deg), dec (deg), and magnitude
Return: array of corrected magnitudes, array quality flag
Note: uses median fitted zeropoint shift for objects having (rad, dec) outside fitted or periphery pixels
'''
pix = healpy.ang2pix(self.nside, numpy.radians(90. - dec), numpy.radians(ra))
# Interpolate zeropoints
if interpolate:
zeropoint_shift = healpy.get_interp_val(self.zeropoint_shiftmap.data.field(filter),
numpy.radians(90. - dec), numpy.radians(ra))
else:
zeropoint_shift = self.zeropoint_shiftmap.data.field(filter)[pix]
# Quality flag
quality_flag = self.zeropoint_shiftmap.data.field('n_des')[pix]
quality_flag[quality_flag > 0] = 0
quality_flag[quality_flag < 0] = 2
if self.fill_periphery:
quality_flag[numpy.logical_and(quality_flag == 2, numpy.fabs(zeropoint_shift) <= 10.)] = 1
# Use median zeropoint shift for objects outside interpolated region
median_fitted_zeropoint_shift = numpy.median(zeropoint_shift[numpy.fabs(zeropoint_shift) <= 10.])
zeropoint_shift[numpy.fabs(zeropoint_shift) > 10.] = median_fitted_zeropoint_shift
return mag + zeropoint_shift, quality_flag
def GetZeropoint(self, filter, ra, dec, mag, interpolate=True, plot=False):
'''
Inputs: filter name {g, r, i, z}, arrays for ra (deg), dec (deg), and magnitude
Return: array of corrected magnitudes, array quality flag
Note: uses median fitted zeropoint shift for objects having (rad, dec) outside fitted or periphery pixels
'''
pix = healpy.ang2pix(self.nside, numpy.radians(90. - dec), numpy.radians(ra))
# Interpolate zeropoints
if interpolate:
zeropoint_shift = healpy.get_interp_val(self.zeropoint_shiftmap.data.field(filter),
numpy.radians(90. - dec), numpy.radians(ra))
else:
zeropoint_shift = self.zeropoint_shiftmap.data.field(filter)[pix]
# Quality flag
quality_flag = self.zeropoint_shiftmap.data.field('n_des')[pix]
quality_flag[quality_flag > 0] = 0
quality_flag[quality_flag < 0] = 2
if self.fill_periphery:
quality_flag[numpy.logical_and(quality_flag == 2, numpy.fabs(zeropoint_shift) <= 10.)] = 1
# Use median zeropoint shift for objects outside interpolated region
median_fitted_zeropoint_shift = numpy.median(zeropoint_shift[numpy.fabs(zeropoint_shift) <= 10.])
zeropoint_shift[numpy.fabs(zeropoint_shift) > 10.] = median_fitted_zeropoint_shift
return zeropoint_shift, quality_flag
############################################################
'''
# Fill in peripheral cells of shiftmap with average of nearest neighbors
neighbor = numpy.take(self.zeropoint_shiftmap.data.field(filter),
healpy.get_all_neighbours(self.nside, range(0, healpy.nside2npix(self.nside))))
neighbor_mask = numpy.ma.masked_array(neighbor, neighbor == healpy.UNSEEN)
mean_neighbor = neighbor_mask.mean(axis=0)
indices = numpy.nonzero(numpy.logical_and(mean_neighbor > 0,
self.zeropoint_shiftmap.data.field(filter) == healpy.UNSEEN))[0]
neighbor_shiftmap = self.zeropoint_shiftmap.data.field(filter)
neighbor_shiftmap[indices] = numpy.array(mean_neighbor[indices].compressed().tolist(), dtype=neighbor_shiftmap.dtype)
'''
# Plot
#if plot:
# ra_center, dec_center = numpy.median(ra), numpy.median(dec)
# healpy.gnomview(self.zeropoint_shiftmap.data.field(filter), rot=(ra_center, dec_center, 0))
# healpy.gnomview(neighbor_shiftmap, rot=(ra_center, dec_center, 0))