-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathpodi_cosmicrays.py
More file actions
executable file
·422 lines (321 loc) · 16.5 KB
/
podi_cosmicrays.py
File metadata and controls
executable file
·422 lines (321 loc) · 16.5 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
#!/usr/bin/env python3
"""
Wrapper around the LACosmics python implementation by Malte Tewes. Also provides
a stand-alone funtionality, based on Malte's demo program.
"""
import ext_cosmics as cosmics
import astropy.io.fits as pyfits
import numpy
from podi_definitions import *
from podi_commandline import *
import itertools
import podi_sitesetup as sitesetup
import time
import multiprocessing
import podi_cython
import podi_collectcells
import podi_logging
import logging
def remove_cosmics(data, n_iterations=4, method="cy",
gain=1.5, readnoise=9,
sigclip = 5.0, sigfrac = 0.3, objlim = 5.0,
verbose=False,
saturation_limit=65000,
binning=1,
):
corrected = data.copy()
mask = numpy.zeros_like(data)
logger = logging.getLogger("CosmicRayX(%s)" % (method))
logger.debug("Parameters: gain=%.3f readnoise=%.3f sigclip=%.2f, sigfrac=%.2f objlim=%.2f sat-limit=%d" % (
gain, readnoise, sigclip, sigfrac, objlim, saturation_limit))
# Break up each entire OTA into its cells to see if this is faster
for cx, cy in itertools.product(range(8),repeat=2):
if (verbose): stdout_write("\rworking on cell %d,%d" % (cx, cy))
x1, x2,y1, y2 = cell2ota__get_target_region(cx, cy, binning=binning)
cell_data = data[y1:y2, x1:x2]
if (method == "py"):
saturation_limit = saturation_limit if saturation_limit > 0 else 1e10
c = cosmics.cosmicsimage(cell_data,
gain=gain, readnoise=readnoise,
sigclip=sigclip, sigfrac=sigfrac, objlim=objlim,
verbose=False)
c.run(maxiter=n_iterations)
# # Re-insert the data into the full frame
corrected[y1:y2, x1:x2] = c.cleanarray
mask[y1:y2, x1:x2][c.mask] = 1
logger.debug("Found %d cosmics in cell %d,%d" % (numpy.sum(c.mask), cx, cy))
elif (method == "cy"):
# Run cosmic ray removal
crj = podi_cython.lacosmics(cell_data.astype(numpy.float64),
gain=gain, readnoise=readnoise,
niter=int(n_iterations),
sigclip=sigclip, sigfrac=sigfrac, objlim=objlim,
saturation_limit=saturation_limit,
verbose=False)
cell_cleaned, cell_mask, cell_saturated = crj
# Re-insert data into full OTA
corrected[y1:y2, x1:x2] = cell_cleaned
mask[y1:y2, x1:x2] = cell_mask
logger.debug("Found %d cosmics in cell %d,%d" % (numpy.sum(cell_mask>0), cx, cy))
else:
corrected[y1:y2, x1:x2] = cell_data
logger.warning("Unknown Crj-X method: %s" % (method))
return corrected, mask
def removecosmics_mp(input_queue, return_queue):
while (True):
task = input_queue.get()
if (task is None):
input_queue.task_done()
break
data, niter, id, method, verbose, setup = task
gain, readnoise, sigclip, sigfrac, objlim, saturation_limit, binning = setup
print("Starting cosmic removal on one extension")
fixed, mask = remove_cosmics(data, niter, method,
gain=gain, readnoise=readnoise,
sigclip=sigclip, sigfrac=sigfrac, objlim=objlim,
saturation_limit=saturation_limit,
binning=binning,
verbose=verbose)
# Send return data
ret = (fixed, mask, id)
return_queue.put(ret)
input_queue.task_done()
continue
return
def csv_to_list(x):
out = []
items = x.split(",")
for item in items:
out.append(float(item))
return out
fwhm_to_sigma = 2 * math.sqrt(2 * math.log(2))
if __name__ == "__main__":
if (len(sys.argv) <= 1):
print(__doc__)
print(cosmics.__doc__)
elif (cmdline_arg_isset("-simple")):
options = podi_collectcells.read_options_from_commandline()
podi_logging.setup_logging(options)
inputfile = get_clean_cmdline()[1]
hdulist = pyfits.open(inputfile)
bglevel = float(cmdline_arg_set_or_default('-bg', 0.0))
for i in range(len(hdulist)):
if (not is_image_extension(hdulist[i])):
continue
hdulist[i].data += bglevel
crj = podi_cython.lacosmics(hdulist[i].data.astype(numpy.float64),
gain=3.3, readnoise=20,
niter=4,
sigclip=5.0, sigfrac=0.5, objlim=5.0,
saturation_limit=25000,
verbose=False)
cell_cleaned, cell_mask, cell_saturated = crj
hdulist[i].data = cell_cleaned - bglevel
outputfile = get_clean_cmdline()[2]
hdulist.writeto(outputfile, overwrite=True)
podi_logging.shutdown_logging(options)
elif (cmdline_arg_isset("-check")):
bglevels = csv_to_list(cmdline_arg_set_or_default("-bg", '0'))
peaks = csv_to_list(cmdline_arg_set_or_default("-peak", '100'))
cosmicfluxes = csv_to_list(cmdline_arg_set_or_default("-crflux", '1000'))
fwhms = csv_to_list(cmdline_arg_set_or_default("-fwhm", '0.4,0.6'))
nrandom = int(cmdline_arg_set_or_default("-n", 100))
imagesize = int(cmdline_arg_set_or_default("-imgsize", 101))
if (imagesize % 2 == 0):
imagesize += 1
centering = float(cmdline_arg_set_or_default("-center", 0.2))
niter = int(cmdline_arg_set_or_default("-niter", 1))
sigclip = float(cmdline_arg_set_or_default("-sigclip", 5.0))
sigfrac = float(cmdline_arg_set_or_default("-sigfrac", 0.3))
objlim = float(cmdline_arg_set_or_default("-objlim", 5.0))
readnoise = 8
gain = 1.3
primhdu = pyfits.PrimaryHDU()
hdulist_in = [primhdu]
hdulist_out = [primhdu]
# Allocate some memory to hold all the individual postage stamps
n_thumbs = int(math.ceil(math.sqrt(nrandom)))
tile_in = numpy.empty((n_thumbs*imagesize, n_thumbs*imagesize))
tile_out = numpy.empty((n_thumbs*imagesize, n_thumbs*imagesize))
# centering = 0.5
for fwhm in fwhms:
fwhm_pixels = fwhm / 0.11
#imagesize = int(20 * fwhm_pixels)+1
center = 0.5*(imagesize-1)
for peak in peaks:
img = numpy.zeros((imagesize, imagesize))
print(img.shape)
# Now convolve the source with the gaussian PSF
gauss_sigma = fwhm_pixels / fwhm_to_sigma
_x, _y = numpy.indices(img.shape)
_x -= center
_y -= center
_r = numpy.hypot(_x,_y)
psf = numpy.exp(-_r**2/(2*gauss_sigma**2)) * peak
# img[center,center] = peak
# try:
# psf = scipy.ndimage.filters.gaussian_filter(
# input=img,
# sigma = gauss_sigma,
# order = 0,
# mode = 'constant',
# cval = 0,
# truncate = 10
# )
# except:
# psf = scipy.ndimage.filters.gaussian_filter(
# input=img,
# sigma = gauss_sigma,
# order = 0,
# mode = 'constant',
# cval = 0,
# )
for bg in bglevels:
#
# Create a fake image with the source at the center
#
with_bg = psf + bg
for cosmicflux in cosmicfluxes:
# Reset the output buffer
tile_in[:,:] = numpy.nan
tile_out[:,:] = numpy.nan
print(bg, peak, cosmicflux, fwhm)
#
# Add poisson noise to the image
#
noise_level = numpy.sqrt(with_bg * gain + readnoise**2)
print(noise_level.shape)
# Now pick positions for the cosmics
# cosmics will occupy a fraction of the central area of
# each postage stamp
cr_pos = numpy.array(numpy.random.random((nrandom,2))
* centering * imagesize
+ 0.5*(1.-centering)*imagesize,
dtype=int)
for n in range(nrandom):
# Compute some noise and add it to the PSF with background
noise = numpy.random.randn(noise_level.shape[0], noise_level.shape[1]) * noise_level
noisy_image = with_bg + noise
# Add the cosmic
noisy_image[cr_pos[n,0], cr_pos[n,1]] += cosmicflux
# Save image for later
tile_x = n % n_thumbs
tile_y = int(math.floor(n/n_thumbs))
tile_in[tile_x*imagesize:(tile_x+1)*imagesize,
tile_y*imagesize:(tile_y+1)*imagesize] = noisy_image
flux_in = numpy.sum(noisy_image)
#
# Run CRJ detection/rejection
#
if (not cmdline_arg_isset("-nx")):
crj = podi_cython.lacosmics(noisy_image.astype(numpy.float64),
gain=gain,
readnoise=readnoise,
niter=niter, #int(n_iterations),
sigclip=sigclip,
sigfrac=sigfrac,
objlim=objlim,
saturation_limit=65000, #saturation_limit,
verbose=cmdline_arg_isset("-verbose"))
fixed, mask, cell_saturated = crj
else:
for i in range(niter):
crj = podi_cython.lacosmics(noisy_image.astype(numpy.float64),
gain=gain,
readnoise=readnoise,
niter=1, #niter, #int(n_iterations),
sigclip=sigclip,
sigfrac=sigfrac,
objlim=objlim,
saturation_limit=65000, #saturation_limit,
verbose=False)
fixed, mask, cell_saturated = crj
noisy_image[:,:] = fixed[:,:]
# fixed, mask = remove_cosmics(noisy_image, n_iterations=1, method='cy',
# gain=gain,
# readnoise=readnoise,
# # These are the important parameters
# sigclip = 5.0,
# sigfrac = 0.3,
# objlim = 5.0,
# # ...
# saturation_limit=65000.,
# binning=1,
# verbose=True)
tile_out[tile_x*imagesize:(tile_x+1)*imagesize,
tile_y*imagesize:(tile_y+1)*imagesize] = fixed
# Compute how much flux we removed vs.
# how much flux is in the cosmic
flux_out = numpy.sum(fixed)
print("%7.1f %7.1f --> %7.1f == %6.3f" % (
flux_in, flux_out, flux_in-flux_out, (flux_in-flux_out)/cosmicflux
))
#all_images = numpy.append(all_images, noisy_image, axis=0)
imghdu = pyfits.ImageHDU(data=tile_in.copy())
imghdu.header['OBJECT'] = "Input: bg %d, flux %.1f, fwhm=%.2f" % (bg, peak, fwhm)
hdulist_in.append(imghdu)
imghdu_fixed = pyfits.ImageHDU(data=tile_out.copy())
imghdu.header['OBJECT'] = "CRJ fixed: bg %d, flux %.1f, fwhm=%.2f" % (bg, peak, fwhm)
hdulist_out.append(imghdu_fixed)
#
# Check if the cosmic was found
#
# for cosmicflux
# for bg
# for peak
# next fwhm
hdulist_in = pyfits.HDUList(hdulist_in)
hdulist_in.writeto("crj_debug_in.fits", overwrite=True)
hdulist_out = pyfits.HDUList(hdulist_out)
hdulist_out.writeto("crj_debug_out.fits", overwrite=True)
else:
options = podi_collectcells.read_options_from_commandline()
podi_logging.setup_logging(options)
inputfile = get_clean_cmdline()[1]
hdulist = pyfits.open(inputfile)
#import yappi
#yappi.start()
method = cmdline_arg_set_or_default("-method", "cy")
niter = int(cmdline_arg_set_or_default("-niter", 4))
verbose = cmdline_arg_isset("-verbose")
sigclip = float(cmdline_arg_set_or_default("-sigclip", 5.0))
sigfrac = float(cmdline_arg_set_or_default("-sigfrac", 0.3))
objlim = float(cmdline_arg_set_or_default("-objlim", 5.0))
saturation_limit = float(cmdline_arg_set_or_default("-saturate", 60000))
job_queue = multiprocessing.JoinableQueue()
return_queue = multiprocessing.JoinableQueue()
n_jobs = 0
for i in range(len(hdulist)):
if (not is_image_extension(hdulist[i])):
continue
gain = hdulist[i].header['GAIN'] if 'GAIN' in hdulist[i].header else 1.5
readnoise = hdulist[i].header['RDNOISEE'] if 'RDNOISEE' in hdulist[i].header else 8.5
binning = hdulist[0].header['BINNING'] if 'BINNING' in hdulist[0].header else 1
setup = gain, readnoise, sigclip, sigfrac, objlim, saturation_limit, binning
task = (hdulist[i].data, niter, i, method, verbose, setup)
job_queue.put(task)
n_jobs += 1
# Now spawn of the workers
processes = []
worker_args = (job_queue, return_queue)
for i in range(sitesetup.number_cpus):
p = multiprocessing.Process(target=removecosmics_mp, args=worker_args)
p.start()
processes.append(p)
job_queue.put(None)
time.sleep(0.01)
# ignore the mask for now
# Put results back into file
for i in range(n_jobs):
ret = return_queue.get()
fixed, mask, id = ret
hdulist[id].data = fixed
return_queue.task_done()
# Join processes
for p in processes:
p.join()
outputfile = get_clean_cmdline()[2]
hdulist.writeto(outputfile, overwrite=True)
#yappi.get_thread_stats().print_all()
#yappi.get_func_stats().debug_print() #print_all()
podi_logging.shutdown_logging(options)