-
Notifications
You must be signed in to change notification settings - Fork 2
/
sunpy__load.py
executable file
·393 lines (306 loc) · 13.9 KB
/
sunpy__load.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
#!/usr/bin/env python
""" Useful loading routines for opening and doing basic processing of the SUNRISE fits files
Most/all loading tools here open the SUNRISE fits files to grab one or two basic fields, and
return it to the use for further processing. These routines are made use of in the sunpy__plot
routines. The routines provided here are all very simple. Many additions/extensions can
be included for further specific functionality.
For usage examples, see the plotting routines in sunpy__plot.
"""
import numpy as np
import os
import sys
try:
import astropy.io.fits as fits
print("Downloading reality...")
except:
try:
import pyfits as fits
print("loaded pyfits")
except:
print("Error: Unable to access PyFITS or AstroPy modules.\n\n" + "With root access, add PyFITS to your site-packages with:\n\n" + "% pip install pyfits\n" + "or\n" +
"% easy_install pyfits\n\n" + "or download at: www.stsci.edu/institute/software_hardware/pyfits/Download\n" + "where additional installation options and instructions can be found.")
#import astropy.io.fits as fits
import cosmocalc # http://cxc.harvard.edu/contrib/cosmocalc/
import scipy as sp
import scipy.ndimage
import sunpy__synthetic_image
__author__ = "Paul Torrey, Greg Snyder, Tyler Metivier"
__copyright__ = "Copyright 2018, The Authors"
__credits__ = ["Paul Torrey", "Greg Snyder","Tyler Metivier"]
__license__ = "MIT"
__version__ = "1.1"
__maintainer__ = "Tyler Metivier"
__email__ = "[email protected]"
__status__ = "Production"
if __name__ == '__main__':
pass
speedoflight_m = 2.99e8
def my_fits_open(filename):
if (not os.path.exists(filename)):
print("file not found:", filename)
sys.exit()
return fits.open(filename)
def load_broadband_image(filename, band=0, **kwargs):
""" Loads an idealized sunrise broadband image for a specified fits file, band, and camera.
The band can be specified as a number or a string (must match the "band_names") """
band_images = load_all_broadband_images(filename, **kwargs)
# This code change is needed because currently load_all_broadband_images seems to have been
# changed to return a tuple, but the code later in this routine assumes that it just returns an
# array. So check for this and get the array if a tuple was returned.
if isinstance(band_images, tuple):
if isinstance(band_images[0], np.ndarray):
band_images = band_images[0]
band_names = load_broadband_names(filename)
if type(band) is int:
return_image = band_images[band, :, :]
else:
# The original version of this code fails for some NumPy versions. New version will work for
# more systems (compatible with original version but also uses syntax that will work for other
# NumPy versions if an error is encountered.
try:
band = (((band_names == band).nonzero())[0])[0]
except AttributeError:
band = (
((np.array(band_names) == band).astype(int).nonzero())[0])[0]
return_image = band_images[band, :, :]
return return_image
def load_broadband_magnitude(filename, band=0, **kwargs):
band_mags = load_all_broadband_photometry(filename, **kwargs)
band_names = load_broadband_names(filename)
if type(band) is not int:
band = int(
np.where([this_band == band for this_band in band_names])[0][0])
return_val = band_mags[band]
return return_val
def load_resolved_broadband_magnitudes(filename, band=0, camera=0, **kwargs):
""" this is a little trickier b/c in W/m/m^2/str. First convert to abs mag, then dist correction """
band_names = load_broadband_names(filename)
if type(band) is not int:
band = int(
np.where([this_band == band for this_band in band_names])[0][0])
# if type(band) is not int:
# band = int( (((band_names == band).nonzero())[0])[0] )
# in W/m/m^2/str shape = [n_band, n_pix, n_pix]
image = load_broadband_image(filename, band=band, camera=camera)
mag = load_broadband_magnitude(filename, band=band, camera=camera)
n_pixels = image.shape[1]
hdulist = fits.open(filename)
lambda_eff = hdulist['FILTERS'].data['lambda_eff']
this_lambda = lambda_eff[band]
to_nu = ((this_lambda**2) / (speedoflight_m)) # * pixel_area_in_str
# 1 muJy/str (1Jy = 1e-26 W/m^2/Hz)
to_microjanskies = (1.0e6) * to_nu * (1.0e26)
image *= to_microjanskies # to microjanskies / str
pixel_in_kpc = load_fov(filename) / n_pixels
pixel_in_sr = (1e3 * pixel_in_kpc / 10.0)**2
image *= pixel_in_sr # in muJy
image /= 1e6 # in Jy
# total image flux in Jy
tot_img_in_Jy = np.sum(image)
abmag = -2.5 * np.log10(tot_img_in_Jy / 3631)
print(abmag)
return abmag
def load_fov(filename):
hdulist = my_fits_open(filename)
data = hdulist['CAMERA0-PARAMETERS'].header['linear_fov']
hdulist.close()
return data
def load_camera_angles(filename, camera=0):
hdulist = my_fits_open(filename)
theta = hdulist['CAMERA' + str(camera) + '-PARAMETERS'].header['theta']
phi = hdulist['CAMERA' + str(camera) + '-PARAMETERS'].header['phi']
hdulist.close()
return theta, phi
def load_broadband_names(filename):
hdulist = my_fits_open(filename)
name_array = hdulist['FILTERS'].data.field(0)
hdulist.close()
name_array = [s.replace(" ", "") for s in name_array]
return name_array
def load_broadband_fast_names(filename):
print(" ")
print("WARNING: fast NAMES HAVE BEEN HARD-CODED; CHECK OUTPUT BELOW FOR CONSISTENCY!!!")
sunrise_names = load_broadband_names(filename)
fast_names = sunrise_names # initial guess
fast_names[2] = "SDSS/u.dat"
fast_names[3] = "SDSS/g.dat"
fast_names[4] = "SDSS/r.dat"
fast_names[5] = "SDSS/i.dat"
fast_names[6] = "SDSS/z.dat"
for index in range(len(fast_names)):
print(sunrise_names[index])
print(fast_names[index])
print(" ")
return fast_names
def load_broadband_effective_wavelengths(filename, band=None):
if (not os.path.exists(filename)):
print("file not found:", filename)
sys.exit()
hdulist = fits.open(filename)
name_array = hdulist['FILTERS'].data['lambda_eff']
hdulist.close()
if band != None:
if type(band) is int:
name_array = name_array[band]
else:
band_names = load_broadband_names(filename)
# The original version of this code fails for some NumPy versions. New version will work for
# more systems (compatible with original version but also uses syntax that will work for other
# NumPy versions if an error is encountered.
try:
band_index = (((band_names == band).nonzero())[0])[0]
except AttributeError:
band_index = (
((np.array(band_names) == band).astype(int).nonzero())[0])[0]
name_array = name_array[band_index]
band = None
return name_array
def load_all_broadband_images(filename, camera=0, openlist=None):
if (not os.path.exists(filename)):
print("file not found:", filename)
sys.exit()
camera_string = 'CAMERA' + str(camera) + '-BROADBAND-NONSCATTER'
if openlist is None:
openlist = fits.open(filename, memmap=False)
data = openlist[camera_string].data
# openlist.close()
print("### Sunpy: opening broadband list: ", openlist.filename())
else:
openfn = openlist.filename()
assert openfn == filename
data = openlist[camera_string].data
data[data < 1e-20] = 1e-20
return data, openlist
def load_broadband_image(filename, band=0, camera=0):
band_images = load_all_broadband_images(filename, camera=camera)
# This code change is needed because currently load_all_broadband_images seems to have been
# changed to return a tuple, but the code later in this routine assumes that it just returns an
# array. So check for this and get the array if a tuple was returned.
if isinstance(band_images, tuple):
if isinstance(band_images[0], np.ndarray):
band_images = band_images[0]
band_names = load_broadband_names(filename)
if type(band) is int:
return_image = band_images[band, :, :]
else:
# The original version of this code fails for some NumPy versions. New version will work for
# more systems (compatible with original version but also uses syntax that will work for other
# NumPy versions if an error is encountered.
try:
band_index = (((band_names == band).nonzero())[0])[0]
except AttributeError:
band_index = (
((np.array(band_names) == band).astype(int).nonzero())[0])[0]
return_image = band_images[int(band_index), :, :]
return return_image
def load_all_broadband_photometry(filename, camera=0):
if (not os.path.exists(filename)):
print("file not found:", filename)
return 0
cst = str(camera)
hdulist = fits.open(filename)
data = hdulist['FILTERS'].data['AB_mag_nonscatter' + cst]
return data
def load_integrated_broadband_apparent_magnitudes(filename, camera=0, dist=4e8):
""" this is fairly easy b/c already in abs mag. Only need to do distance correction """
dist_modulus = 5.0 * (np.log10(dist) - 1.0)
apparent_magnitudes = dist_modulus + \
load_all_broadband_photometry(filename, camera=camera)
return apparent_magnitudes
def load_resolved_broadband_apparent_magnitudes(filename, redshift, camera=0, **kwargs):
""" this is a little trickier b/c in W/m/m^2/str. First convert to abs mag, then dist correction """
images = load_all_broadband_images(
filename, camera=0) # in W/m/m^2/str shape = [n_band, n_pix, n_pix]
mags = load_all_broadband_photometry(filename, camera=0)
n_pixels = images.shape[1]
hdulist = fits.open(filename)
lambda_eff = hdulist['FILTERS'].data['lambda_eff']
for index, this_lambda in enumerate(lambda_eff):
to_nu = ((this_lambda**2) / (speedoflight_m)) # * pixel_area_in_str
# 1 muJy/str (1Jy = 1e-26 W/m^2/Hz)
to_microjanskies = (1.0e6) * to_nu * (1.0e26)
images[index, :, :] = images[index, :, :] * \
to_microjanskies # to microjanskies / str
pixel_in_kpc = load_fov(filename) / n_pixels
pixel_in_sr = (1e3 * pixel_in_kpc / 10.0)**2
images *= pixel_in_sr # in muJy
images /= 1e6 # in Jy
for index, this_lambda in enumerate(lambda_eff):
# total image flux in Jy
tot_img_in_Jy = np.sum(images[index, :, :])
abmag = -2.5 * np.log10(tot_img_in_Jy / 3631)
if True:
print("the ab magnitude of this image is :" +
str(abmag) + " " + str(mags[index]))
print(abmag / mags[index], abmag - mags[index])
print(index, np.sum(images[index, :, :]))
images = -2.5 * np.log10(images / 3631) # abmag in each pixel
dist = (cosmocalc.cosmocalc(redshift, H0=70.4,
WM=0.2726, WV=0.7274))['DL_Mpc'] * 1e6
dist_modulus = 5.0 * (np.log10(dist) - 1.0)
apparent_magnitudes = dist_modulus + images
return apparent_magnitudes
# apparent_magnitudes = dist_modulus +
def load_redshift(filename):
if (not os.path.exists(filename)):
print("file not found:", filename)
sys.exit()
hdulist = fits.open(filename)
redshift = hdulist[1].header['REDSHIFT']
hdulist.close()
return redshift
def load_sed_lambda(filename):
hdulist = my_fits_open(filename)
lambda_array = hdulist['INTEGRATED_QUANTITIES'].data['lambda ']
hdulist.close()
return lambda_array
def load_sed_l_lambda(filename):
hdulist = my_fits_open(filename)
l_lambda_array = hdulist['INTEGRATED_QUANTITIES'].data['L_lambda']
hdulist.close()
return l_lambda_array
# these options only with for sunrise with rad. transfer. Not for current Illustris images #
def load_sed_l_lambda_with_rt(filename, camera=0):
hdulist = my_fits_open(filename)
l_lambda_array = hdulist['INTEGRATED_QUANTITIES'].data['L_lambda_out' +
str(camera)]
hdulist.close()
return l_lambda_array
def load_sed_l_lambda_scatter(filename, camera=0):
hdulist = my_fits_open(filename)
l_lambda_array = hdulist['INTEGRATED_QUANTITIES'].data['L_lambda_scatter' +
str(camera)]
hdulist.close()
return l_lambda_array
def load_sed_l_lambda_nonscatter(filename, camera=0):
hdulist = my_fits_open(filename)
l_lambda_array = hdulist['INTEGRATED_QUANTITIES'].data['L_lambda_nonscatter' +
str(camera)]
hdulist.close()
return l_lambda_array
def load_sed_l_lambda_ir(filename, camera=0):
hdulist = my_fits_open(filename)
l_lambda_array = hdulist['INTEGRATED_QUANTITIES'].data['L_lambda_ir' +
str(camera)]
hdulist.close()
return l_lambda_array
#===============================================================================#
#===============================================================================#
def load_stellar_mass_map(filename, camera=0):
hdulist = fits.open(filename)
camera_string = 'CAMERA' + str(camera) + '-AUX'
aux_image = hdulist[camera_string].data
map = aux_image[4, :, :]
return map
def load_mass_weighted_stellar_age_map(filename, camera=0):
hdulist = fits.open(filename)
camera_string = 'CAMERA' + str(camera) + '-AUX'
aux_image = hdulist[camera_string].data
map = aux_image[7, :, :]
return map
def load_stellar_metal_map(filename, camera=0):
hdulist = fits.open(filename)
camera_string = 'CAMERA' + str(camera) + '-AUX'
aux_image = hdulist[camera_string].data
map = aux_image[5, :, :]
return map