-
Notifications
You must be signed in to change notification settings - Fork 1
/
hires_Spire.py
186 lines (161 loc) · 6.36 KB
/
hires_Spire.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# Licensed under a 3-clause BSD style license - see LICENSE.txt
"""
SPIRE-specific code for HiRes.
Copyright (c) 2012-2013, California Institute of Technology
Developer: David Shupe
"""
import hires as hires
try:
import astropy.io.fits as pyfits
except ImportError:
import pyfits
import numpy
import glob
hires.set_FLUX_UNITS('Jy/beam')
#====================================================================
# Override hires.read_all_IN_files method
def read_all_Spire_files():
''' Read all SPIRE level products from the input directory
and return a list of Sample objects
'''
file_list = sorted( glob.glob(hires.INFILE_PREFIX + '*.fits') )
if len(file_list)==0: hires.log(hires.LOG_fatal, 'No input files for: ' + \
hires.INFILE_PREFIX) # QUIT
if (hires.DIAG_PRODUCT):
diagHdu = pyfits.open(hires.DIAG_PRODUCT)
diagTable = diagHdu[1]
chanNames = diagTable.data.field('channelName')
scanNumbers = diagTable.data.field('scanNumber')
coffsets = diagTable.data.field('a0')
samples = []
scan = 0
for file in file_list:
hduList = pyfits.open(file)
maskHdu = hduList['mask']
signalHdu = hduList['signal']
raHdu = hduList['ra']
decHdu = hduList['dec']
names = []
for det in raHdu.data.names:
if (det[:3] == hires.BAND and det[3] != 'T' and det[3] != 'R' and det[4] != 'P'):
names.append(det)
names.sort()
crval1 = hires.get_FITS_keyword('CRVAL1')
crval2 = hires.get_FITS_keyword('CRVAL2')
projection = hires.Gnomonic(crval1, crval2)
for det in names:
if (hires.DIAG_PRODUCT):
inz = (chanNames == det) & (scanNumbers == scan)
offsettmp = coffsets[inz]
if (len(offsettmp) != 1):
raise ValueError('Did not find exactly one offset for %s in scan %d'\
%(det,scan))
a0 = coffsets[inz][0]
else:
a0 = 0
ra = raHdu.data.field(det)
dec = decHdu.data.field(det)
sig = signalHdu.data.field(det) - a0 + hires.FLUX_OFFSET
mask = maskHdu.data.field(det)
inx = numpy.all([(mask & 64401) == 0],axis=0)
#hires.log(3, 'Generating samples for detector %s',det)
x, y = projection.np_lonlat2xy(ra[inx], dec[inx])
# negate x because CDELT1 is negative
# angle = 0.0 to ignore it
if (len(sig[inx]) > 0):
ss = hires.SampleSet(-x, y, sig[inx], '1', 0.0)
thismin = numpy.min(sig[inx],axis=-1)
if thismin < hires.GLOBAL_MIN:
hires.GLOBAL_MIN = thismin
samples.append(ss)
scan += 1
return samples
def read_all_Spire_tables():
''' Read SPIRE table products
and return a list of Sample objects
'''
raHduList = pyfits.open('IN/m33_raTable2.fits')
raHdu = raHduList[1]
names = []
for det in raHdu.data.names:
if (det[:3] == 'PLW' and det[3] != 'T' and det[3] != 'R' and det[4] != 'P'):
names.append(det)
names.sort()
decHduList = pyfits.open('IN/m33_decTable2.fits')
signalHduList = pyfits.open('IN/m33_signalTable2.fits')
maskHduList = pyfits.open('IN/m33_maskTable2.fits')
samples = []
crval1 = hires.get_FITS_keyword('CRVAL1')
crval2 = hires.get_FITS_keyword('CRVAL2')
projection = hires.Gnomonic(crval1, crval2)
for det in names:
#ra = raHduList[1].data[det]
ra = raHduList[1].data.field(det)[::1]
dec = decHduList[1].data.field(det)[::1]
sig = signalHduList[1].data.field(det)[::1] + offset
mask = maskHduList[1].data.field(det)[::1]
# ra = raHduList[1].data.field(det)[::17]
# dec = decHduList[1].data.field(det)[::17]
# sig = signalHduList[1].data.field(det)[::17]
# mask = maskHduList[1].data.field(det)[::17]
#inx = numpy.all([mask <= 1024,mask != 256],axis=0)
inx = numpy.all([(mask & 64401) == 0])
hires.log(3, 'Generating samples for detector %s',det)
x, y = projection.np_lonlat2xy(ra[inx], dec[inx])
# negate x because CDELT1 is negative
# angle = 0.0 to ignore it
ss = hires.SampleSet(-x, y, sig[inx], '1', 0.0)
# ss = hires.SampleSet(-x, y, sig[inx], 1, 0.0)
# if (i%3333 == 0):
# hires.log(3, 'Created %dth sample for detector %s, x=%f, y=%f, ra=%f, dec=%f',\
# i,det,x[i], y[i], ra[inx][i],dec[inx][i])
samples.append(ss)
return samples
#====================================================================
# Override hires.read_all_DRF_files method
def read_all_Spire_beams():
''' Read SPIRE beams
and return a dictionary of response functions
'''
# note: DRF_SET_ID now set in plw.params
# global DRF_SET_ID
DRF_SET_ID = 'single'
detHduList = pyfits.open(hires.DRF_PREFIX)
extn = hires.DRF_EXTN
drf_array = detHduList[extn].data
naxis1 = detHduList[extn].header['NAXIS1']
naxis2 = detHduList[extn].header['NAXIS2']
#deg_per_pix = 1./3600.
#deg_per_pix = 0.8/3600.
deg_per_pix = abs(detHduList[extn].header['CDELT1'])
radius_pix = naxis1 / 2
radius_degrees = radius_pix * deg_per_pix
detectors = {}
#print names
#
def xy2response_function(x, y):
''' interpolate in detector response array
x, y in degrees (relative to DRF center)
'''
iFloat = (x + radius_degrees) / deg_per_pix
jFloat = (y + radius_degrees) / deg_per_pix
# cheap "interpolation" -- just takes nearest one
iInt = int(iFloat+0.5)
jInt = int(jFloat+0.5)
if iInt<0 or iInt>=naxis1 or jInt<0 or jInt>=naxis2 :
response = 0.0
else:
response = drf_array[iInt, jInt]
return response
#
# for id in names:
# detector = hires.Detector(id, radius_degrees, xy2response_function)
# detectors[id] = detector
detectors['1'] = hires.Detector(1, radius_degrees, xy2response_function)
return detectors
hires.read_all_IN_files = read_all_Spire_files
hires.read_all_DRF_files = read_all_Spire_beams
#====================================================================
# Call hires
if __name__ == "__main__":
hires.main()