-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathviero_quick_stack.py
More file actions
220 lines (200 loc) · 5.93 KB
/
Copy pathviero_quick_stack.py
File metadata and controls
220 lines (200 loc) · 5.93 KB
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
import pdb
import numpy as np
import os
from Utils import readcol
from astropy.io import fits
from simstack import stack_in_redshift_slices as simstack
from sedstack import stack_in_redshift_slices as sedstack
from invert_sed import single_simple_flux_from_greybody
def viero_quick_stack(
map_names,
catalog_names,
noise_map_names,
efwhm=None,
psf_names=None,
color_correction=None,
n_sources_max = None,
sedfitwavelengths = None,
zed = 0.001
):
if n_sources_max == None: n_sources_max=50000l
nmap = len(map_names)
#PUT DATA INTO CUBE
nlists = len(catalog_names)
nsources = 0 # initialize a counter
cube = np.zeros([n_sources_max, nlists, 2]) # nsources by nlis/nts by 2 for RA/DEC
for i in range(nlists):
list_name = catalog_names[i]
if os.path.getsize(list_name) > 0:
ra, dec = readcol.readcol(list_name,fsep=',',twod=False)
nsources_list=len(ra)
if nsources_list > n_sources_max:
print 'too many sources in catalog: use N_SOURCES_MAX flag'
break
if nsources_list > 0:
cube[0:nsources_list,i,0]=ra
cube[0:nsources_list,i,1]=dec
if nsources_list > nsources:
nsources=nsources_list
cube=cube[0:nsources,:,:] # Crop it down to the length of longest list
#cube=cube[0:nsources-1,:,:] # Crop it down to the length of longest list
stacked_sed=np.zeros([nmap, nlists])
stacked_sed_err=np.zeros([nmap,nlists])
#USE SEDSTACK
if sedfitwavelengths != None:
#FIT SEDS TO FIND FLUXES
cmaps = []
cnoise = []
for wv in range(nmap):
#READ MAPS
tmaps, thd = fits.getdata(map_names[wv], 0, header = True)
if color_correction != None:
tmaps *= color_correction[wv]
cmaps.append(tmaps)
if noise_map_names != None:
tnoise, nhd = fits.getdata(noise_map_names[wv], 0, header = True)
if color_correction != None:
tnoise *= color_correction[wv]
cnoise.append(tnoise)
if efwhm != None:
ifwhm = efwhm
ipsf_names = None
else:
ifwhm = None
ipsf_names = psf_names
stacked_object=sedstack(
np.asarray(cmaps),
thd,
cube,
sedfitwavelengths,
fwhm=ifwhm,
psf_names=ipsf_names,
cnoise=np.asarray(cnoise),
# err_ss=err_ss,
zed=zed,
quiet=None)
print 'yeaaahhh!!'
v = stacked_object.params.valuesdict()
beta = np.asarray(v['b'])
for ised in range(nlists):
Temp = np.asarray(v['T'+str(ised)])
Lir = np.asarray(v['L'+str(ised)])
#pdb.set_trace()
stacked_sed[:,ised]=single_simple_flux_from_greybody(np.asarray(sedfitwavelengths), Trf = Temp, Lrf = Lir, b=beta, zin=zed)
#pdb.set_trace()
return [stacked_sed, v]
else:
#USE SIMSTACK TO STACK AT ONE WAVELENGTH AT A TIME
for wv in range(nmap):
#READ MAPS
cmap, chd = fits.getdata(map_names[wv], 0, header = True)
if noise_map_names != None:
cnoise, nhd = fits.getdata(noise_map_names[wv], 0, header = True)
if efwhm != None:
ifwhm = efwhm[wv]
ipsf_names = None
else:
ifwhm = None
ipsf_names = psf_names[wv]
stacked_object=simstack(
cmap,
chd,
cube,
fwhm=ifwhm,
psf_names=ipsf_names,
cnoise=cnoise,
# err_ss=err_ss,
quiet=None)
stacked_flux = np.array(stacked_object.params.values())
stacked_sed[wv,:] = stacked_flux
#stacked_sed_err[wv,:]=err_ss
#pdb.set_trace()
return stacked_sed
def sedstack_wrapper_map_classes_and_cats_to_cubes(map_library,subcatalog_names):
'''
The map library should have all the details, i.e.;
maps, noisemaps, psf, masks
'''
#if n_sources_max == None: n_sources_max=50000l
n_sources_max=50000l
nmap = len(map_library.keys())
#PUT DATA INTO CUBE
nlists = len(subcatalog_names)
nsources = 0 # initialize a counter
cube = np.zeros([n_sources_max, nlists, 2]) # nsources by nlis/nts by 2 for RA/DEC
for i in range(nlists):
list_name = subcatalog_names[i]
if os.path.getsize(list_name) > 0:
ra, dec = readcol.readcol(list_name,fsep=',',twod=False)
nsources_list=len(ra)
if nsources_list > n_sources_max:
print 'too many sources in catalog: use N_SOURCES_MAX flag'
break
if nsources_list > 0:
cube[0:nsources_list,i,0]=ra
cube[0:nsources_list,i,1]=dec
if nsources_list > nsources:
nsources=nsources_list
cube=cube[0:nsources,:,:] # Crop it down to the length of longest list
#cube=cube[0:nsources-1,:,:] # Crop it down to the length of longest list
stacked_sed=np.zeros([nmap, nlists])
#Decide whether to use SIMSTACK or SEDSTACK
try:
getattr(map_library[map_library.keys()[0]], 'wavelength')
except AttributeError:
print "Using SIMSTACK"
#USE SIMSTACK TO STACK AT ONE WAVELENGTH AT A TIME
for wv in range(nmap):
#READ MAPS
map_object = map_library[map_library.keys()[wv]]
stacked_object=simstack_objects(
map_object,
cube,
quiet=None)
stacked_flux = np.array(stacked_object.params.values())
stacked_sed[wv,:] = stacked_flux
#pdb.set_trace()
return stacked_sed
else:
print 'Using SEDSTACK'
#FIT SEDS TO FIND FLUXES
cmaps = []
cnoise = []
for wv in range(nmap):
#READ MAPS
tmaps, thd = fits.getdata(map_names[wv], 0, header = True)
if color_correction != None:
tmaps *= color_correction[wv]
cmaps.append(tmaps)
if noise_map_names != None:
tnoise, nhd = fits.getdata(noise_map_names[wv], 0, header = True)
if color_correction != None:
tnoise *= color_correction[wv]
cnoise.append(tnoise)
if efwhm != None:
ifwhm = efwhm
ipsf_names = None
else:
ifwhm = None
ipsf_names = psf_names
stacked_object=sedstack(
np.asarray(cmaps),
thd,
cube,
sedfitwavelengths,
fwhm=ifwhm,
psf_names=ipsf_names,
cnoise=np.asarray(cnoise),
# err_ss=err_ss,
zed=zed,
quiet=None)
print 'yeaaahhh!!'
v = stacked_object.params.valuesdict()
beta = np.asarray(v['b'])
for ised in range(nlists):
Temp = np.asarray(v['T'+str(ised)])
Lir = np.asarray(v['L'+str(ised)])
#pdb.set_trace()
stacked_sed[:,ised]=single_simple_flux_from_greybody(np.asarray(sedfitwavelengths), Trf = Temp, Lrf = Lir, b=beta, zin=zed)
#pdb.set_trace()
return [stacked_sed, v]