-
Notifications
You must be signed in to change notification settings - Fork 0
/
galsim_galaxy.py
246 lines (212 loc) · 8.81 KB
/
galsim_galaxy.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
#!/usr/bin/env python
# encoding: utf-8
"""
galsim_galaxy.py
Wrapper for GalSim galaxy models to use in MCMC.
"""
import numpy as np
import galsim
def lsst_noise(random_seed):
"""
See GalSim/examples/lsst.yaml
gain: e- / ADU
read_noise: Variance in ADU^2
sky_level: ADU / arcsec^2
"""
rng = galsim.BaseDeviate(random_seed)
return galsim.CCDNoise(rng, gain=2.1, read_noise=3.4, sky_level=18000)
def wfirst_noise(random_seed):
"""
From http://wfirst-web.ipac.caltech.edu/wfDepc/visitor/temp1927222740/results.jsp
"""
rng = galsim.BaseDeviate(random_seed)
exposure_time_s = 150.
pixel_scale_arcsec = 0.11
read_noise_e_rms = 5.
sky_background = 3.60382E-01 # e-/pix/s
gain = 2.1 # e- / ADU
return galsim.CCDNoise(rng, gain=2.1,
read_noise=(read_noise_e_rms / gain) ** 2,
sky_level=sky_background / pixel_scale_arcsec ** 2 * exposure_time_s)
class GalSimGalParams(object):
"""Parameters for GalSim galaxies"""
def __init__(self, galaxy_model="Gaussian"):
self.galaxy_model = galaxy_model
if galaxy_model == "Gaussian":
self.gal_flux = 1.e5
self.gal_sigma = 2.
self.e = 0.3
self.beta = np.pi/4.
self.n_params = 4
elif galaxy_model == "Spergel":
raise NotImplementedError()
elif galaxy_model == "Sersic":
self.gal_flux = 1.e5
self.n = 3.4
self.hlr = 1.8
self.e = 0.3
self.beta = np.pi/4.
self.n_params = 5
elif galaxy_model == "BulgeDisk":
self.gal_flux = 1.e5
self.bulge_n = 3.4
self.disk_n = 1.5
self.bulge_re = 2.3
self.disk_r0 = 0.85
self.bulge_frac = 0.0 #0.3
self.e_bulge = 0.01
self.e_disk = 0.25
self.beta_bulge = np.pi/4.
self.beta_disk = 3. * np.pi/4.
self.n_params = 10
else:
raise AttributeError("Unimplemented galaxy model")
def num_params(self):
return self.n_params
def get_params(self):
"""
Return an array of parameter values.
"""
if self.galaxy_model == "Gaussian":
return np.array([self.gal_flux, self.gal_sigma, self.e, self.beta])
elif self.galaxy_model == "Spergel":
raise NotImplementedError()
elif self.galaxy_model == "Sersic":
return np.array([self.gal_flux, self.n, self.hlr, self.e, self.beta])
elif self.galaxy_model == "BulgeDisk":
return np.array([self.gal_flux, self.bulge_n, self.disk_n, self.bulge_re, self.disk_r0,
self.bulge_frac, self.e_bulge, self.e_disk, self.beta_bulge, self.beta_disk])
else:
raise AttributeError("Unimplemented galaxy model")
class GalSimGalaxyModel(object):
"""
Parametric galaxy model from GalSim for MCMC.
Mimics GalSim examples/demo1.py
"""
def __init__(self,
psf_sigma=0.5,
pixel_scale=0.2,
noise=None,
galaxy_model="Gaussian",
wavelength=1.e-6,
primary_diam_meters=2.4,
atmosphere=False):
self.psf_sigma = psf_sigma
self.pixel_scale = pixel_scale
if noise is None:
noise = galsim.GaussianNoise(sigma=30.)
self.noise = noise
self.galaxy_model = galaxy_model
self.wavelength = wavelength
self.primary_diam_meters = primary_diam_meters
self.atmosphere = atmosphere
self.params = GalSimGalParams(galaxy_model=galaxy_model)
def set_params(self, p):
"""
Take a list of parameters and set local variables
For use in emcee.
"""
return NotImplementedError()
def get_params(self):
"""
Return a list of model parameter values.
"""
return self.params.get_params()
def get_psf(self):
lam_over_diam = self.wavelength / self.primary_diam_meters
lam_over_diam *= 206265. # arcsec
optics = galsim.Airy(lam_over_diam, obscuration=0.548, flux=1.)
if self.atmosphere:
atmos = galsim.Kolmogorov(lam_over_r0=9.e-8)
psf = galsim.Convolve([atmos, optics])
else:
psf = optics
return psf
def get_image(self, out_image=None, add_noise=False):
if self.galaxy_model == "Gaussian":
gal = galsim.Gaussian(flux=self.params.gal_flux, sigma=self.params.gal_sigma)
gal_shape = galsim.Shear(g=self.params.e, beta=self.params.beta*galsim.radians)
gal = gal.shear(gal_shape)
elif self.galaxy_model == "Spergel":
raise NotImplementedError()
elif self.galaxy_model == "Sersic":
gal = galsim.Sersic(n=self.params.n, half_light_radius=self.params.hlr,
flux=self.params.gal_flux)
gal_shape = galsim.Shear(g=self.params.e, beta=self.params.beta*galsim.radians)
gal = gal.shear(gal_shape)
elif self.galaxy_model == "BulgeDisk":
bulge = galsim.Sersic(n=self.params.bulge_n, half_light_radius=self.params.bulge_re)
bulge = bulge.shear(g=self.params.e_bulge, beta=self.params.beta_bulge*galsim.radians)
disk = galsim.Sersic(n=self.params.disk_n, half_light_radius=self.params.disk_r0)
disk = disk.shear(g=self.params.e_disk, beta=self.params.beta_disk*galsim.radians)
gal = self.params.bulge_frac * bulge + (1 - self.params.bulge_frac) * disk
gal = gal.withFlux(self.params.gal_flux)
else:
raise AttributeError("Unimplemented galaxy model")
final = galsim.Convolve([gal, self.get_psf()])
# wcs = galsim.PixelScale(self.pixel_scale)
image = final.drawImage(image=out_image, scale=self.pixel_scale)
if add_noise:
image.addNoise(self.noise)
return image
def save_image(self, file_name):
image = self.get_image()
image.write(file_name)
return None
def plot_image(self, file_name, ngrid=None):
import matplotlib.pyplot as plt
if ngrid is not None:
out_image = galsim.Image(ngrid, ngrid)
else:
out_image = None
###
fig = plt.figure(figsize=(8, 8), dpi=100)
ax = fig.add_subplot(1,1,1)
im = ax.matshow(self.get_image(out_image, add_noise=True).array, cmap=plt.get_cmap('coolwarm')) #, vmin=-350, vmax=350)
fig.colorbar(im)
fig.savefig(file_name)
return None
def get_moments(self, add_noise=True):
results = self.get_image(add_noise=add_noise).FindAdaptiveMom()
print 'HSM reports that the image has observed shape and size:'
print ' e1 = %.3f, e2 = %.3f, sigma = %.3f (pixels)' % (results.observed_shape.e1,
results.observed_shape.e2, results.moments_sigma)
def make_test_images():
"""
Use the GalSimGalaxyModel class to make test images of a galaxy for LSST and WFIRST.
"""
import h5py
print "Making test images for LSST and WFIRST"
lsst = GalSimGalaxyModel(pixel_scale=0.2, noise=lsst_noise(82357), galaxy_model="Sersic",
wavelength=500.e-9, primary_diam_meters=8.4, atmosphere=True)
lsst.save_image("test_lsst_image.fits")
lsst.plot_image("test_lsst_image.png", ngrid=64)
wfirst = GalSimGalaxyModel(pixel_scale=0.11, noise=wfirst_noise(82357), galaxy_model="Sersic",
wavelength=1.e-6, primary_diam_meters=2.4, atmosphere=False)
wfirst.save_image("test_wfirst_image.fits")
wfirst.plot_image("test_wfirst_image.png", ngrid=64)
lsst_data = lsst.get_image(galsim.Image(64, 64), add_noise=True).array
wfirst_data = wfirst.get_image(galsim.Image(64, 64), add_noise=True).array
# -------------------------------------------------------------------------
### Save a file with joint image data for input to the Roaster
f = h5py.File('test_image_data.h5', 'w')
### Instrument/epoch 1
cutout1 = f.create_group("cutout1")
dat1 = cutout1.create_dataset('pixel_data', data=lsst_data)
### TODO: Add segmentation mask
noise1 = cutout1.create_dataset('noise_model', data=lsst.noise.getVariance())
### TODO: add WCS information
### TODO: add background model(s)
cutout1.attrs['instrument'] = 'LSST'
### Instrument/epoch 2
cutout2 = f.create_group("cutout2")
dat2 = cutout2.create_dataset('pixel_data', data=wfirst_data)
### TODO: Add segmentation mask
noise2 = cutout2.create_dataset('noise_model', data=wfirst.noise.getVariance())
### TODO: add WCS information
### TODO: add background model(s)
cutout2.attrs['instrument'] = 'WFIRST'
f.close()
# -------------------------------------------------------------------------
if __name__ == "__main__":
make_test_images()