-
Notifications
You must be signed in to change notification settings - Fork 2
/
surface_brightness.py
469 lines (353 loc) · 19.2 KB
/
surface_brightness.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
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
456
457
458
459
460
461
462
463
464
465
466
467
468
469
#General use
import os
import numpy as np
from astropy.io import fits
from astropy.cosmology import WMAP9 as cosmo
#For reading and creating tables
from astropy.table import Table, unique
#For performing photometry
from astropy import units as u
from photutils import aperture_photometry, SkyCircularAperture
from astropy.wcs import WCS
#For generating a plot
import matplotlib.pyplot as plt
from matplotlib.backends.backend_pdf import PdfPages
class surface_brightness():
def __init__(self, cord_dict = {}, red_dict = {}):
#Make sure we have redshift and coordinates for each supernova
if cord_dict.keys() == red_dict.keys():
self.cord_dict = cord_dict #Dictionary of supernova coordinates
self.red_dict = red_dict #Dictionary of supernova redshifts
else:
raise ValueError('''Keys in coordinate and redshift dictionaries
(self.cord_dict, self.red_dict) do not match''')
def conv_error(self, val, err):
'''
Use a black box error method to find the uncertanty in
astropy.cosmology.WMAP9.kpc_comoving_per_arcmin().
Args:
val (float): A redshift value
err (float): Error in the redshift
Returns:
error (float): Error in the conversion factor
'''
diff = (cosmo.kpc_comoving_per_arcmin(val + err)**2
- cosmo.kpc_comoving_per_arcmin(val - err)**2)
error = abs(.5 * diff.value)
return(error)
def lum_dist_error(self, val, err):
'''
Use a black box error method to find the uncertanty in
astropy.cosmology.WMAP9.luminosity_distance().
Args:
val (float): A redshift value
err (float): Error in the redshift
Returns:
error (float): Error in the luminosity distance
'''
diff = (cosmo.luminosity_distance(val + err).cgs
- cosmo.luminosity_distance(val - err).cgs)
error = abs(.5 * diff.value)
return(error)
def flux_error(self, pho_val, pho_err, conv_val):
'''
Find the statistical error in a flux value calculated from average
photon counts per second.
Args:
pho_val (float): Average photon counts per second
pho_err (float): Error in pho_val
conv_val (float): Conversion factor from average photon counts
per second to flux
Returns:
error (float): Error in the associated flux value
'''
error = (conv_val * pho_err)
return(error)
def luminosity_error(self, flux_val, flux_err, dist_val, dist_err):
'''
Find the error in a luminosity value due to uncertanty in flux and
luminosity distance.
Args:
flux_val (float): Flux
flux_err (float): Error in Flux
dist_val (float): Luminosity distance
dist_err (float): Error in luminosity distance
Returns:
error (float): Error in the luminosity
'''
error = np.sqrt((4 * np.pi * (dist_val**2) * flux_err)**2
+ (8 * np.pi * dist_val * flux_val * dist_err)**2)
return(error)
def surf_brightness_error(self, lum_val, lum_err, conv_val, conv_err):
'''
Find the error in the surface brightness of a supernova enviornment
due to uncertanties in luminosity and
astropy.cosmology.WMAP9.kpc_comoving_per_arcmin().
Args:
lum_val (float): Luminosity of a supernova environment
lum_err (float): Error in the luminosity
conv_val (float): Conversion factor from arcmin^2 to Kpc^2
conv_err (float): Error in the conversion factor
Returns:
error (float): Error in the Surface brightness
'''
error = np.sqrt((conv_val * lum_err / 3600)**2
+ (lum_val * conv_err / 3600)**2)
return(error)
def zero_check(self, fits_file, cordinate, r):
'''
Perform photometry on a exp type .fits file and check if there are
data pixels in an aperture. Return a number code that corresponds
to the result: 0 = no exp file found, 1 = data pixels found inside
the aperture, 2 = no data pixels found in the aperture
Args:
fits_file (str) : File path of an int type .fits file
cordinate (SkyCoord): Coordinate of a supernova in degrees
r (Quantity): Radius of a photometry aperture in arcmin
Returns:
result (int): A number code as outlined in the description
'''
if os.path.isfile(fits_file.replace("d-int", "d-exp")):
with fits.open(fits_file.replace("d-int", "d-exp")) as exp_file:
aperture = SkyCircularAperture(cordinate, r)
phot_table = aperture_photometry(exp_file[0], aperture)
if phot_table[0][0] != 0:
result = 1
elif phot_table[0][0] == 0:
result = 2
else:
result = 0
return(result)
def photometry(self, fits_file, radius):
'''
Perform photometry on an int type .fits file.
Args:
fits_file (str) : File path of an int type .fits file
radius (float): Unitless radius of the desired aperture in kpc
Returns:
results (list): [supernova name (str),
exposure time (float)
photometry value (float),
photometry_error (float)]
results (list): ["error" (str),
fits file path (str),
error description (str)]
'''
results = []
if os.path.isfile(fits_file.replace("d-int", "d-skybg")):
with fits.open(fits_file) as (int_file
), fits.open(fits_file.replace("d-int", "d-skybg")) as (skybg_file
):
wcs = WCS(fits_file)
for sn in self.cord_dict:
#Define the SN location in pixels
w = wcs.all_world2pix(self.cord_dict[sn].ra, self.cord_dict[sn].dec, 1)
#Make sure the sn is located in the image
if 0 < w[0] < 3600 and 0 < w[1] < 3600:
#Find arcmin of a 1kpc radius region
r = radius * u.kpc / cosmo.kpc_comoving_per_arcmin(float(self.red_dict[sn]))
#Create an aperture
aperture = SkyCircularAperture(self.cord_dict[sn], r)
#create an array of the error in each pixel
exp_time = int_file[0].header["EXPTIME"]
int_error = np.sqrt(int_file[0].data / exp_time)
skybg_error = np.sqrt(skybg_file[0].data / exp_time)
#Perform photometry
int_phot_table = aperture_photometry(int_file[0], aperture, error = int_error)
if int_phot_table[0][0] == 0:
check = self.zero_check(fits_file, self.cord_dict[sn], r)
if check == 1:
skybg_phot_table = aperture_photometry(skybg_file[0], aperture, error = skybg_error)
photometry_sum = int_phot_table[0][0] - skybg_phot_table[0][0]
photometry_error = np.sqrt(int_phot_table[0][1]**2 + skybg_phot_table[0][1]**2)
results.append([sn, exp_time, photometry_sum, photometry_error])
elif check == 0:
results.append(["error", fits_file, "no check file"])
else:
skybg_phot_table = aperture_photometry(skybg_file[0], aperture, error = skybg_error)
photometry_sum = int_phot_table[0][0] - skybg_phot_table[0][0]
photometry_error = np.sqrt(int_phot_table[0][1]**2 + skybg_phot_table[0][1]**2)
results.append([sn, exp_time, photometry_sum, photometry_error])
if results == []:
results.append(["error", fits_file, "no supernova found"])
return(results)
else:
return(results)
else:
results.append(["error", fits_file, "no skybg file"])
return(results)
def create_tables(self, uv_type, directory, radius, print_progress = False):
'''
Perform photometry on a directory of .fits files and create a list
of two tables. The first table contains the redshift, exposure time,
luminosity, and surface brightness for various supernova, along with
the associated error values. The second table is a log outlining any
files that do not contain a supernova or are missing checkfiles. If
print_progress is set equal to true, the path each fits file will be
printed before performing photometry on along with the number of
remaining files.
Args:
uv_type (str) : Specifies which type of uv to create a table
for. Use either "NUV" or "FUV".
directory (str) : A directory containing .fits files
radius (float): Radius of desired photometry aperture in kpc
print_progress (bool) : Whether or not to print the file path of each
fits file
Returns:
results (list): [Data table (Table), Log table (Table)]
'''
#Make sure we have redshift and coordinates of each supernova
if self.cord_dict.keys() != self.red_dict.keys():
raise ValueError('''Keys in coordinate and redshift dictionaries
(self.cord_dict, self.red_dict) do not match''')
label = uv_type + " " + str(radius) + "kpc "
#Define the tables that will be returned by the function
log = Table(names = ["File Path", "Issue"], dtype = [object, object])
out = Table(names = ["sn",
"Redshift",
"Redshift Error",
label + "Exposure Time",
"Flux",
"Flux Error",
label + "Luminosity",
label + "Luminosity Error",
label + "Surface Brightness",
label + "Surface Brightness Error"],
dtype = ("S70", "float64", "float64", "float64", "float64",
"float64", "float64", "float64", "float64", "float64"))
out["Redshift"].unit = u.dimensionless_unscaled
out["Redshift Error"].unit = u.dimensionless_unscaled
out[label + "Exposure Time"].unit = u.s
out["Flux"].unit = u.erg / u.s / u.Angstrom / u.kpc / u.kpc / u.cm / u.cm / np.pi
out["Flux Error"].unit = u.erg / u.s / u.Angstrom / u.kpc / u.kpc / u.cm / u.cm / np.pi
out[label + "Luminosity"].unit = u.erg / u.s / u.Angstrom / u.kpc / u.kpc
out[label + "Luminosity Error"].unit = u.erg / u.s / u.Angstrom / u.kpc / u.kpc
out[label + "Surface Brightness"].unit = u.erg / u.s / u.Angstrom / u.arcsec / u.arcsec
out[label + "Surface Brightness Error"].unit = u.erg / u.s / u.Angstrom / u.arcsec / u.arcsec
#Set parameters that are specific to NUV or FUV observations
if "N" in uv_type.upper():
file_key = "nd-int" #A string distinguing galex file types
flux_conv = 2.06 * 1e-16 #A conversion factor from counts per second to flux
elif "F" in uv_type.upper():
file_key = "fd-int"
flux_conv = 1.40 * 1e-15
#Create a list of files to perform photometry on
file_list = []
for path, subdirs, files in os.walk(directory):
for name in files:
if file_key in name and len(name.split(".")) < 3:
file_list.append(os.path.join(path, name))
count = len(file_list)
#Perform photometry on each .fits file
for fits_file in file_list:
if print_progress == True:
print(count, ":", fits_file, flush = True)
count -= 1
p = self.photometry(fits_file, radius)
for elt in p:
if elt[0] == "error":
log.add_row([elt[1], elt[2]])
if print_progress == True:
print("error", elt[2], "\n", flush = True)
else:
#We calculate the values to be entered in the table
redshift = float(self.red_dict[elt[0]])
peculiar_redshift = np.sqrt((1 + (300 / 299792.458)) / (1 - (300 / 299792.458))) - 1
redshift_err = np.sqrt((redshift / 1000)**2 + (peculiar_redshift)**2)
arcmin = cosmo.kpc_comoving_per_arcmin(redshift).value**2 #kpc^2 per arcmin^2
arcmin_err = self.conv_error(redshift, redshift_err)
photom = elt[2] #The photometry value
photom_err = elt[3]
flux = flux_conv * photom #convert cps to flux using the conversion factor
flux_err = self.flux_error(photom, photom_err, flux_conv)
ldist = cosmo.luminosity_distance(redshift).cgs.value #Luminosity Distance (cm)
ldist_err = self.lum_dist_error(redshift, redshift_err)
lum = flux * 4 * np.pi * (ldist**2) #luminosity = flux*4*pi*r^2
lum_err = self.luminosity_error(flux, flux_err, ldist, ldist_err)
sbrightness = lum * arcmin / 3600
sbrightness_err = self.surf_brightness_error(lum, lum_err, arcmin, arcmin_err)
out.add_row([elt[0], redshift, redshift_err, elt[1], flux, flux_err,
lum, lum_err, sbrightness, sbrightness_err])
out.sort(label + "Surface Brightness Error")
out_unique = unique(out, keys = "sn")
out_unique.sort("sn")
return([out_unique, log])
def create_plots(self, data_table, uv_type, radius):
'''
Create .pdf plots of UV surface brightness vs redshift in units of
erg s-1 A-1 arcsec-2 and erg s-1 A-1 kpc-2. Plots are created using
results from the create_table() function. Both plots are saved to the
working directory.
Args:
data_table (Table): Data table returned by create_table()
uv_type (str) : Specifies if the data is for "NUV" or "FUV".
radius (float): The radius used to generate data_table
Returns:
None
'''
try:
label = uv_type + " " + str(radius) + "kpc "
if "N" in uv_type.upper(): plot_name = "NUV Surface Brightness of " + str(radius) + "kpc SN Enviornments"
elif "F" in uv_type.upper(): plot_name = "FUV Surface Brightness of " + str(radius) + "kpc SN Enviornments"
plt.figure(1)
plt.xlabel("Redshift")
plt.ylabel(str(data_table[label + "Surface Brightness"].unit))
plt.title(plot_name)
plt.figure(2)
plt.xlabel("Redshift")
plt.ylabel(str(data_table[label + "Luminosity"].unit))
plt.title(plot_name)
for row in data_table:
if row[label + "Surface Brightness"] > 0:
sigma = row["Flux"] / row["Flux Error"]
if sigma <= 3:
plt.figure(1)
plt.semilogy(row["Redshift"],
row[label + "Surface Brightness"],
marker = u"$\u21a7$",
markeredgecolor="lightgrey",
color = "lightgrey")
plt.figure(2)
plt.semilogy(row["Redshift"],
row[label + "Luminosity"],
marker = u"$\u21a7$",
markeredgecolor="lightgrey",
color = "lightgrey")
for row in data_table:
if row[label + "Surface Brightness"] > 0:
sigma = row["Flux"] / row["Flux Error"]
if sigma > 3:
error = (row[label + "Surface Brightness Error"],
row[label + "Luminosity Error"])
plt.figure(1)
plt.semilogy(row["Redshift"],
row[label + "Surface Brightness"],
marker = ".",
color = "black")
plt.errorbar(row["Redshift"],
row[label + "Surface Brightness"],
yerr = error[0],
color = "black",
linestyle = "")
plt.figure(2)
plt.semilogy(row["Redshift"],
row[label + "Luminosity"],
marker = ".",
color = "black")
plt.errorbar(row["Redshift"],
row[label + "Luminosity"],
yerr = error[1],
color = "black",
linestyle = "")
plt.figure(1)
plt.savefig(str(radius) + "kpc " + uv_type + " plot (arcsec).pdf")
plt.figure(2)
plt.savefig(str(radius) + "kpc " + uv_type + " plot (kpc).pdf")
plt.close("all")
except KeyError as e:
if "Redshift" in str(e): raise KeyError("Input table missing column 'Redshift'")
elif "Flux Error" in str(e): raise KeyError("Input table missing column 'Flux Error'")
elif "Flux" in str(e): raise KeyError("Input table missing column 'Flux'")
elif "Surface Brightness" in str(e) or "Luminosity" in str(e):
raise KeyError("Input table missing column " + str(e))
else: raise KeyError(str(e))