-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathstackData.py
More file actions
458 lines (398 loc) · 15.2 KB
/
Copy pathstackData.py
File metadata and controls
458 lines (398 loc) · 15.2 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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
""" controls the stackData object """
import numpy as np
import astropy.constants as astcon
import astropy.cosmology as astcos
import astropy.modeling as astmod
import astropy.units as astun
import astropy.io.ascii as astasc
import astropy.convolution as astcov
import useful_functions as uf
import matplotlib.pyplot as plt
from analyseData import anaData
import csv
import sys
import os
import timeit
import logging
logger = logging.getLogger(__name__)
Msun = astun.def_unit('Msun',astun.Jy*astun.Mpc**2*astun.km/(astun.s) )
c = astcon.c.to('km/s')
msun = [
(astun.Jy*astun.km*astun.Mpc*astun.Mpc/astun.s, astun.solMass, lambda x: 1*x, lambda x: 1*x)
]
# msun = [
# (astun.km*astun.Mpc*astun.Mpc/astun.s, astun.solMass, lambda x: 1*x, lambda x: 1*x)
# ]
log10 = [
(astun.dex*astun.Msun, astun.Msun, lambda x: 10**x, lambda x: np.log10(x))
]
class objSpec():
""" class contronlling each of the spectra,
Inputs:
z: redshift of the galaxy
rz: random redshift
spec: the spectrum's flux
freq: the spectrum's frequency axis
femit: the rest frequency of the emission/absorption line
cosmology: an astropy object containting the comosmology information """
def __init__(self):
self.redshift = None
self.randredshift = None
self.origspec = None
self.massspec = None
self.refmassspec = None
self.shiftorigspec = None
self.shiftrefspec = None
self.extendspec = None
self.extendrefspec = None
self.spectralspec = None
self.rms = None
self.noisespec = None
self.noisespecmass = None
self.orignoisespec = None
self.dl = None
self.weight = None
self.status = None
self.file = None
self.objid = None
self.totalflux = None
self.dv = None
self.stellarmass = None
self.dvkms = None
def __smoothData(self, spec):
windowsize = 3
kernel = astcov.Box1DKernel(windowsize)
sspec = astcov.convolve(spec, kernel)
return sspec
def __getSpectrumZOK(self, cat, n, runno):
if runno > 1:
if 'red' in cat.uncerttype:
uz = cat.randuz[n][runno-2]
z = cat.catalogue['Redshift'][n] + uz
else:
z = cat.catalogue['Redshift'][n]
else:
z = cat.catalogue['Redshift'][n]
if cat.min_redshift < cat.catalogue['Redshift'][n] < cat.max_redshift:
self.redshift = z
self.randredshift = cat.randz[runno][n]
return True, cat
else:
self.status = 'incomplete'
logger.warning('Spectrum ID: %s, REJECTED: Out of the redshift range'%str(cat.catalogue['Object ID'][n]))
return False, cat
def __getStellarMass(self, cat, n):
if cat.catalogue['Stellar Mass'][n] == 0. or cat.catalogue['Stellar Mass'][n] =='masked':
logger.warning('REJECTED %s - No Stellar Mass'%str(cat.catalogue['Object ID'][n]))
return False, cat
else:
self.stellarmass = (cat.catalogue['Stellar Mass'][n] * cat.catalogue['Stellar Mass'].unit).to(astun.Msun, log10)
# logger.info('Logged stellar mass for %s'%str(cat.catalogue['Object ID'][n]))
return True, cat
def __getSpectrumDVOK(self, spec, cat, n):
checkdv = np.mean(np.array([abs(spec[0]-spec[1]), abs(spec[-1]-spec[-2])]))*cat.spectralunit
self.dv = checkdv
z = cat.catalogue['Redshift'][n]
if 'Hz' in cat.spectralunit.to_string():
self.dvkms = uf.calc_dv_from_df(self.dv, z, cat.femit)
else:
self.dvkms = self.dv.to('km/s')
if 0.95*cat.channelwidth < checkdv < 1.05*cat.channelwidth:
return True, cat
elif 0.9*cat.channelwidth < checkdv < 1.1*cat.channelwidth:
logger.warning(r'Spectrum ID: %s, WARNING: Channel width is 5-10percent different from expected spectrum channel width'%str(cat.catalogue['Object ID'][n]))
return True, cat
else:
self.status = 'incomplete'
logger.warning(r'Spectrum ID: %s, REJECTED: Channel width is more than 5\%-10\% different from expected spectrum channel width'%str(cat.catalogue['Object ID'][n]))
return False, cat
def __getSpectrumlenOK(self, spec, cat, n):
checklen = len(spec)
if checklen > (int(cat.stackmask)):
return True, cat
else:
self.status = 'incomplete'
logger.warning(r'Spectrum ID: %s, REJECTED: spectrum is too short'%str(cat.catalogue['Object ID'][n]))
return False, cat
def __getSpecNaNOK(self, spec, cat):
if any(np.isnan(s) for s in spec):
self.status = 'incomplete'
logger.warning(r'Spectrum ID: %s, REJECTED: contains NaNs'%str(cat.catalogue['Object ID'][n]))
return False, cat
else:
return True, cat
def __getSpectrum(self, cat, n, runno):
self.objid = cat.catalogue['Object ID'][n]
filexist = os.path.isfile(cat.specloc+cat.catalogue['Filename'][n])
if filexist == True:
try:
# print(cat.constan['Object ID'][n])
data = astasc.read(cat.specloc+cat.catalogue['Filename'][n], data_start=cat.rowstart)
checkNAN, cat = self.__getSpecNaNOK( (data[data.colnames[cat.speccol[0]]]).astype(np.float_), cat)
checkZ, cat = self.__getSpectrumZOK(cat, n, runno)
checkDV, cat = self.__getSpectrumDVOK((data[data.colnames[cat.speccol[0]]]).astype(np.float_), cat, n)
checklen, cat = self.__getSpectrumlenOK((data[data.colnames[cat.speccol[1]]]).astype(np.float_), cat, n)
if cat.stackunit == uf.gasfrac:
checkSM, cat = self.__getStellarMass(cat, n)
else:
checkSM = True
if checkDV and checkZ and checklen and checkSM and checkNAN:
spec = data[data.colnames[cat.speccol[1]]] * cat.fluxunit / cat.convfactor
spec[np.isnan(spec)] = 0.
self.origspec = spec.to(astun.Jy)
if cat.veltype == 'optical':
self.spectralspec = data[data.colnames[cat.speccol[0]]].data*cat.spectralunit
else:
self.spectralspec = data[data.colnames[cat.speccol[0]]].data*(1.+self.redshift)*cat.spectralunit
return cat
else:
self.status = 'incomplete'
return cat
except KeyboardInterrupt:
logger.error('Encountered an exception:', exc_info=True)
uf.earlyexit(self)
raise KeyboardInterrupt
except Exception as e:
logger.error('Encountered an exception:', exc_info=True)
self.status = 'incomplete'
return cat
else:
logger.warning('Spectrum ID: %s has no corresponding spectrum file.'%str(cat.catalogue['Object ID'][n]))
self.status = 'incomplete'
return cat
def __calcRestFrame(self, cat):
""" convert the frequency spectrum from observed frame to rest frame """
vrad = astcon.c.to('km/s') * (1 - self.spectralspec/cat.femit)
restaxis = vrad*(1+self.redshift)
restflux = self.origspec/(1+self.redshift)
return restaxis, restflux
def __calcNoiseRMS(self, spec, cat, spec2=None):
if self.status == 'incomplete':
return None, None
else:
""" create the noise spectrum and calculate the rms value of the spectrum """
l = cat.noiselen
s = len(spec)
if spec2 is None:
specv = spec.value
noisespec = np.zeros(l)*spec.unit
masked = np.append(specv[:int(s/2-cat.stackmask/2)],specv[int(s/2+cat.stackmask/2):])*spec.unit
for i in range(int(l/2)):
noisespec[i] = masked[i % len(masked)]
noisespec[int(-(i+1))] = masked[int(-(i+1) % len(masked))]
rms = np.std(noisespec)
if rms == 0:
rms = 1
else: pass
return rms, noisespec
else:
specv = spec.value
specv2 = spec2.value
noisespec = np.zeros(l)*spec.unit
noisespec2 = np.zeros(l)*spec2.unit
ine = int(s/2-cat.stackmask/2)
ins = int(s/2+cat.stackmask/2)
masked = np.append(specv[:ine],specv[ins:])*spec.unit
masked2 = np.append(specv2[:ine],specv2[ins:])*spec2.unit
for i in range(l//2):
wwi = int(i % len(masked))
ssi = int(-(i+1) % len(masked))
eei = int(-(i+1))
noisespec[i] = masked[wwi]
noisespec2[i] = masked2[wwi]
noisespec[eei] = masked[ssi]
noisespec2[eei] = masked2[ssi]
rms = np.std(noisespec)
if rms == 0:
rms = 1
else: pass
return rms, noisespec, noisespec2
def __calcWeights(self, cat, n):
if self.status == 'incomplete':
logger.error("Error calculation on weight calculation for spectrum ID %s"%str(self.objid))
return
else:
if type(self.rms) == int:
self.rms = self.rms*astun.dimensionless_unscaled
else:pass
if cat.weighting == '1':
self.weight = 1.
elif cat.weighting == '2':
if self.rms.value == 0.:
self.weight = 1.
else:
self.weight = 1./(self.rms.value)
elif cat.weighting == '3':
if self.rms.value == 0.:
self.weight = 1.
else:
self.weight = 1./(self.rms.value**2)
elif cat.weighting == '4':
if self.rms.value == 0.:
self.weight = 1.
else:
self.weight = (self.dl.value)**2/(self.rms.value**2)
elif cat.weighting == '5':
self.weight = cat.catalogue['StackWeights'][n]
else:
self.weight = 1
return
def __calcShift(self, cat):
"""shift the spectrum sunch that the galaxy location is centred"""
if self.status == 'incomplete':
logger.error("Error with shifting spectrum %s"%str(self.objid))
return
else:
if 'Hz' in cat.spectralunit.to_string():
axis, spec = self.__calcRestFrame(cat)
else:
axis, spec = self.spectralspec, self.origspec
l = len(self.origspec)
cen = int(l/2)
self.shiftorigspec = np.zeros(l)*spec.unit
self.shiftrefspec = np.zeros(l)*spec.unit
cz = astcon.c.to('km/s')*self.redshift
cz_ran = astcon.c.to('km/s')*self.randredshift
galaxyloc = np.argmin(abs(axis - cz))
galaxyloc_ran = np.argmin(abs(axis - cz_ran))
for i in range(l):
self.shiftorigspec[i] = spec[(galaxyloc-cen+i) % l]
self.shiftrefspec[i] = spec[(galaxyloc_ran-cen+i) % l]
return
def calcMassConversion(self, catalogue, spec, refspec, redshift, randredshift, dl, dv):
# if self.status == 'incomplete':
# logger.error('Error on conversion to mass spectrum for spectrum %s'%str(self.objid))
# return
# else:
try:
massspec = (2.356E+05 * spec * dl**2 * dv/(1.+redshift)).to(astun.solMass, msun)
refmassspec = (2.356E+05 * refspec * dl**2 * dv/(1.+randredshift)).to(astun.solMass, msun)
except:
self.status = 'incomplete'
logger.error('Encountered an exception:', exc_info=True)
return
if (catalogue.stackunit == uf.msun):
return massspec, refmassspec
elif catalogue.stackunit == uf.gasfrac:
massspec, refmassspec = (massspec/self.stellarmass)*uf.gasfrac, (refmassspec/self.stellarmass)*uf.gasfrac
return massspec, refmassspec
def __calcExtendSpectrum(self, cat, spec, rspec, nspec):
""" extend each spectrum such that they are all the same length """
if self.status == 'incomplete':
logger.error("Error with extending spectrum %s"%str(self.objid))
return
else:
tl = cat.exspeclen
extendspec = np.zeros(tl)*spec.unit
extendrefspec = np.zeros(tl)*rspec.unit
cent = int(tl/2)
ol = len(spec)
ceno = int(ol/2)
if (tl-ol) <= 0:
r = cent
elif (tl-ol) > 0:
r = ceno
for i in range(r):
extendspec[cent+i] = spec[ceno+i]
extendspec[cent-(i+1)] = spec[ceno-(i+1)]
extendrefspec[cent+i] = rspec[ceno+i]
extendrefspec[cent-(i+1)] = rspec[ceno-(i+1)]
bl = int((tl - ol)/2)
nl = len(nspec)
for i in range(bl):
extendspec[i] = nspec[i % nl]
extendspec[-(i+1)] = nspec[-(i+1) % nl]
extendrefspec[i] = nspec[i % nl]
extendrefspec[-(i+1)] = nspec[-(i+1) % nl]
# extendspec[-1] = nspec[i%nl]
# extendrefspec[-1] = nspec[i%nl]
return extendspec, extendrefspec
def __plot_process(self, axes, fig, cat):
if axes==None or fig==None:
return
else:
axes[0].plot(self.spectralspec.value, self.origspec.value)
axes[1].plot(cat.spectralaxis.value, self.extendspec.value)
fig.canvas.draw()
def callModule(self, cat, n, runno, axes=None, fig=None):
try:
cat = self.__getSpectrum(cat, n, runno)
if self.origspec is None:
return cat
else:
self.__calcShift(cat)
if cat.cluster == True:
self.dl = cat.clusterDL
else:
self.dl = cat.cosmology.luminosity_distance(self.redshift)
if uf.msun == cat.stackunit:
self.massspec, self.refmassspec = self.calcMassConversion(cat, self.shiftorigspec, self.shiftrefspec, self.redshift, self.randredshift, self.dl, self.dvkms)
self.rms, self.noisespec, self.noisespecmass = self.__calcNoiseRMS(self.shiftorigspec, cat, self.massspec)
self.extendspec, self.extendrefspec = self.__calcExtendSpectrum(cat, self.massspec, self.refmassspec, self.noisespecmass)
self.__calcWeights(cat, n)
elif astun.Jy == cat.stackunit:
self.rms, self.noisespec = self.__calcNoiseRMS(self.shiftorigspec, cat)
self.extendspec, self.extendrefspec = self.__calcExtendSpectrum(cat, self.shiftorigspec, self.shiftrefspec, self.noisespec)
self.__calcWeights(cat, n)
elif uf.gasfrac == cat.stackunit:
self.massspec, self.refmassspec = self.calcMassConversion(cat, self.shiftorigspec, self.shiftrefspec, self.redshift, self.randredshift, self.dl, self.dvkms)
# self.massspec, self.refmassspec = self.calcMassConversion(cat, self.shiftorigspec, self.shiftrefspec, 0.0231, self.randredshift, 100*astun.Mpc, self.dvkms)
self.rms, self.noisespec, self.noisespecmass = self.__calcNoiseRMS(self.shiftorigspec, cat, self.massspec)
self.extendspec, self.extendrefspec = self.__calcExtendSpectrum(cat, self.massspec, self.refmassspec, self.noisespecmass)
self.__calcWeights(cat, n)
else:
logger.error("Something went wrong with deciding the stack unit and how to stack")
uf.earlyexit(cat)
raise sys.exit()
startind = int(len(self.shiftorigspec)/2-cat.stackmask/2)
endind = int(len(self.shiftorigspec)/2+1+cat.stackmask/2)
intfluxspec = (self.shiftorigspec*self.dvkms).to(astun.Jy * astun.km/astun.s)
intflux = np.sum( intfluxspec.value[startind:endind] )
if runno == 0:
cat.outcatalogue.add_row([ cat.catalogue['Bin Number'][n], self.objid, cat.catalogue['Filename'][n], cat.catalogue['Redshift'][n], cat.catalogue['Redshift Uncertainty'][n], cat.catalogue['Stellar Mass'][n], cat.catalogue['Other Data'][n], intflux, self.weight])
else:
h = 0
except KeyboardInterrupt:
logger.error("Encountered exception.", exec_info=True)
uf.earlyexit(cat)
raise KeyboardInterrupt
except SystemExit:
logger.error("Encountered exception.", exec_info=True)
uf.earlyexit(cat)
raise sys.exit()
return cat
class objStack(objSpec):
def __init__(self, spec=[], ref=[], noise=[], rms=[], stackrms=[], dist=[], weight=0., z=[], nobj=0, spectral=[], sunits=None):
self.spec = spec
self.refspec = ref
self.dl = dist
self.noise = noise
self.rms = rms
self.stackrms = stackrms
self.redshift = z
self.nobj = nobj
self.spectralaxis = spectral
self.weightsum = weight
self.stackunits = sunits
def __add__(self, other):
if other.extendspec is None or other.weight == 0.:
return self
elif any(np.isnan(se) == True for se in other.extendspec):
return self
else:
# print(other.extendspec)
spec = np.add(self.spec, other.extendspec*other.weight)
refspec = np.add(self.refspec, other.extendrefspec*other.weight)
weight = self.weightsum + other.weight
nobj = self.nobj + 1
dist = list(self.dl) + [other.dl]
noise = np.add(self.noise, other.noisespec*other.weight)
rms = (self.rms + [(np.std(other.noisespec.value*other.weight)/other.weight)])
srms = self.stackrms+[(np.std(noise.value)/weight)]
redshift = list(self.redshift) + [other.redshift]
return objStack(spec=spec, ref=refspec, noise=noise, rms=rms,stackrms=srms, dist=dist,weight=weight, z=redshift, nobj=nobj,spectral=self.spectralaxis, sunits=self.stackunits)
def __radd__(self, other):
if other.spec is None:
return self
else:
return self.__add__(other)