forked from mommermi/photometrypipeline
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path#pp_stackedphotometry.py#
executable file
·464 lines (373 loc) · 17.2 KB
/
#pp_stackedphotometry.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
#!/usr/bin/env python
""" PP_STACKEDPHOTOMETRY - wrapper to perform photometry on stacked images
v1.0: 2017-10-19, [email protected]
"""
from __future__ import print_function
# Photometry Pipeline
# Copyright (C) 2016 Michael Mommert, [email protected]
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program. If not, see
# <http://www.gnu.org/licenses/>.
import re
import os
import gc
import sys
try:
import numpy as np
except ImportError:
print('Module numpy not found. Please install with: pip install numpy')
sys.exit()
import shutil
import logging
import subprocess
import argparse, shlex
import time
try:
from astropy.io import fits
except ImportError:
print('Module astropy not found. Please install with: pip install astropy')
sys.exit()
# only import if Python3 is used
if sys.version_info > (3,0):
from builtins import str
from builtins import range
### pipeline-specific modules
import _pp_conf
from catalog import *
import pp_prepare
import pp_extract
import pp_register
import pp_photometry
import pp_calibrate
import pp_distill
import pp_combine
import diagnostics as diag
# setup logging
logging.basicConfig(filename = _pp_conf.log_filename,
level = _pp_conf.log_level,
format = _pp_conf.log_formatline,
datefmt = _pp_conf.log_datefmt)
# def run_the_pipeline(filenames, man_targetname, man_filtername,
# fixed_aprad, source_tolerance):
# """
# wrapper to run the photometry pipeline
# """
# # increment pp process idx
# _pp_conf.pp_process_idx += 1
# # reset diagnostics for this data set
# _pp_conf.dataroot, _pp_conf.diagroot, \
# _pp_conf.index_filename, _pp_conf.reg_filename, _pp_conf.cal_filename, \
# _pp_conf.res_filename = _pp_conf.setup_diagnostics()
# # setup logging again (might be a different directory)
# logging.basicConfig(filename = _pp_conf.log_filename,
# level = _pp_conf.log_level,
# format = _pp_conf.log_formatline,
# datefmt = _pp_conf.log_datefmt)
# ### read telescope information from fits headers
# # check that they are the same for all images
# logging.info('##### new pipeline process in %s #####' % _pp_conf.dataroot)
# logging.info(('check for same telescope/instrument for %d ' + \
# 'frames') % len(filenames))
# instruments = []
# for idx, filename in enumerate(filenames):
# try:
# hdulist = fits.open(filename, ignore_missing_end=True)
# except IOError:
# logging.error('cannot open file %s' % filename)
# print('ERROR: cannot open file %s' % filename)
# filenames.pop(idx)
# continue
# header = hdulist[0].header
# for key in _pp_conf.instrument_keys:
# if key in header:
# instruments.append(header[key])
# break
# if len(filenames) == 0:
# raise IOError('cannot find any data...')
# if len(instruments) == 0:
# raise KeyError('cannot identify telescope/instrument; please update' + \
# '_pp_conf.instrument_keys accordingly')
# # check if there is only one unique instrument
# if len(set(instruments)) > 1:
# print('ERROR: multiple instruments used in dataset: %s' % \
# str(set(instruemnts)))
# logging.error('multiple instruments used in dataset: %s' %
# str(set(instruments)))
# for i in range(len(filenames)):
# logging.error('%s %s' % (filenames[i], instruments[i]))
# sys.exit()
# telescope = _pp_conf.instrument_identifiers[instruments[0]]
# obsparam = _pp_conf.telescope_parameters[telescope]
# logging.info('%d %s frames identified' % (len(filenames), telescope))
# ### read filter information from fits headers
# # check that they are the same for all images
# logging.info(('check for same filter for %d ' + \
# 'frames') % len(filenames))
# filters = []
# for idx, filename in enumerate(filenames):
# try:
# hdulist = fits.open(filename, ignore_missing_end=True)
# except IOError:
# logging.error('cannot open file %s' % filename)
# print('ERROR: cannot open file %s' % filename)
# filenames.pop(idx)
# continue
# header = hdulist[0].header
# filters.append(header[obsparam['filter']])
# if len(filters) == 0:
# raise KeyError('cannot identify filter; please update' + \
# 'setup/telescopes.py accordingly')
# if len(set(filters)) > 1:
# print('ERROR: multiple filters used in dataset: %s' % str(set(filters)))
# logging.error('multiple filters used in dataset: %s' %
# str(set(filters)))
# for i in range(len(filenames)):
# logging.error('%s %s' % (filenames[i], filters[i]))
# sys.exit()
# if man_filtername is None:
# try:
# filtername = obsparam['filter_translations'][filters[0]]
# except KeyError:
# print(('Cannot translate filter name (%s); please adjust ' + \
# 'keyword "filter_translations" for %s in ' + \
# 'setup/telescopes.py') % (filters[0], telescope))
# logging.error(('Cannot translate filter name (%s); please adjust '+\
# 'keyword "filter_translations" for %s in ' + \
# 'setup/telescopes.py') % (filters[0], telescope))
# return None
# else:
# filtername = man_filtername
# logging.info('%d %s frames identified' % (len(filenames), filtername))
# print('run photometry pipeline on %d %s %s frames' % \
# (len(filenames), telescope, filtername))
# change_header = {}
# if man_targetname is not None:
# change_header['OBJECT'] = man_targetname
# ### prepare fits files for photometry pipeline
# preparation = pp_prepare.prepare(filenames, obsparam,
# change_header,
# diagnostics=True, display=True)
# ### run wcs registration
# # default sextractor/scamp parameters
# snr, source_minarea = obsparam['source_snr'], obsparam['source_minarea']
# aprad = obsparam['aprad_default']
# print('\n----- run image registration\n')
# registration = pp_register.register(filenames, telescope, snr,
# source_minarea, aprad,
# None, obsparam,
# obsparam['source_tolerance'],
# display=True,
# diagnostics=True)
# if len(registration['badfits']) == len(filenames):
# summary_message = "<FONT COLOR=\"red\">registration failed</FONT>"
# elif len(registration['goodfits']) == len(filenames):
# summary_message = "<FONT COLOR=\"green\">all images registered" + \
# "</FONT>; "
# else:
# summary_message = "<FONT COLOR=\"orange\">registration failed for " + \
# ("%d/%d images</FONT>; " %
# (len(registration['badfits']),
# len(filenames)))
# # add information to summary website, if requested
# if _pp_conf.use_diagnostics_summary:
# diag.insert_into_summary(summary_message)
# # in case not all image were registered successfully
# filenames = registration['goodfits']
# # # stop here if filtername == None
# # if filtername == None:
# # logging.info('Nothing else to do for this filter (%s)' %
# # filtername)
# # print('Nothing else to do for this filter (%s)' % filtername)
# # return None
# # stop here if registration failed for all images
# if len(filenames) == 0:
# logging.info('Nothing else to do for this image set')
# print('Nothing else to do for this image set')
# diag.abort('pp_registration')
# return None
# ### run photometry (curve-of-growth analysis)
# snr, source_minarea = 1.5, obsparam['source_minarea']
# background_only = False
# target_only = False
# if fixed_aprad == 0:
# aprad = None # force curve-of-growth analysis
# else:
# aprad = fixed_aprad # skip curve_of_growth analysis
# print('\n----- derive optimium photometry aperture\n')
# phot = pp_photometry.photometry(filenames, snr, source_minarea, aprad,
# man_targetname, background_only,
# target_only,
# telescope, obsparam, display=True,
# diagnostics=True)
# # data went through curve-of-growth analysis
# if phot is not None:
# summary_message = ("<FONT COLOR=\"green\">aprad = %5.1f px, " + \
# "</FONT>") % phot['optimum_aprad']
# if phot['n_target'] > 0:
# summary_message += "<FONT COLOR=\"green\">based on target and " + \
# "background</FONT>; "
# else:
# summary_message += "<FONT COLOR=\"orange\">based on background " + \
# "only </FONT>; "
# # a fixed aperture radius has been used
# else:
# summary_message += "using a fixed aperture radius of %.1f px;" % aprad
# # add information to summary website, if requested
# if _pp_conf.use_diagnostics_summary:
# diag.insert_into_summary(summary_message)
# ### run photometric calibration
# minstars = _pp_conf.minstars
# manualcatalog = None
# print('\n----- run photometric calibration\n')
# calibration = pp_calibrate.calibrate(filenames, minstars, filtername,
# manualcatalog, obsparam, display=True,
# diagnostics=True)
# # if calibration == None:
# # print('Nothing to do!')
# # logging.error('Nothing to do! Error in pp_calibrate')
# # diag.abort('pp_calibrate')
# # sys.exit(1)
# try:
# zps = [frame['zp'] for frame in calibration['zeropoints']]
# zp_errs = [frame['zp_sig'] for frame in calibration['zeropoints']]
# if calibration['ref_cat'] is not None:
# refcatname = calibration['ref_cat'].catalogname
# else:
# refcatname = 'instrumental magnitudes'
# summary_message = "<FONT COLOR=\"green\">average zeropoint = " + \
# ("%5.2f+-%5.2f using %s</FONT>; " %
# (numpy.average(zps),
# numpy.average(zp_errs),
# refcatname))
# except TypeError:
# summary_message = "<FONT COLOR=\"red\">no phot. calibration</FONT>; "
# # add information to summary website, if requested
# if _pp_conf.use_diagnostics_summary:
# diag.insert_into_summary(summary_message)
# ### distill photometry results
# print('\n----- distill photometry results\n')
# distillate = pp_distill.distill(calibration['catalogs'],
# man_targetname, [0,0],
# None, None,
# display=True, diagnostics=True)
# targets = numpy.array(list(distillate['targetnames'].keys()))
# try:
# target = targets[targets != 'control_star'][0]
# mags = [frame[7] for frame in distillate[target]]
# summary_message = ("average target brightness and std: " +
# "%5.2f+-%5.2f\n" % (numpy.average(mags),
# numpy.std(mags)))
# except IndexError:
# summary_message = "no primary target extracted"
# # add information to summary website, if requested
# if _pp_conf.use_diagnostics_summary:
# diag.insert_into_summary(summary_message)
# print('\nDone!\n')
# logging.info('----- successfully done with this process ----')
# gc.collect() # collect garbage; just in case, you never know...
if __name__ == '__main__':
# command line arguments
parser = argparse.ArgumentParser(description='stacked photometry')
parser.add_argument('-comoving', help='stack in moving target frame',
action='store_true')
parser.add_argument('-filter', help='filter name override',
default=None)
parser.add_argument('-method',
help='combination method',
choices=['average', 'median', 'clipped'],
default='clipped')
parser.add_argument('-fixed_aprad', help='fixed aperture radius (px)',
default=0)
parser.add_argument('-snr',
help='SNR limit for detected sources',
default=3)
parser.add_argument('images', help='images to process',
nargs='+')
args = parser.parse_args()
comoving = args.comoving
man_filtername = args.filter
combinemethod = args.method
fixed_aprad = float(args.fixed_aprad)
snr = args.snr
filenames = args.images
### read telescope and filter information from fits headers
# check that they are the same for all images
instruments = []
for filename in filenames:
hdulist = fits.open(filename, ignore_missing_end=True,
verify='silentfix')
header = hdulist[0].header
for key in _pp_conf.instrument_keys:
if key in header:
instruments.append(header[key])
if len(instruments) == 0:
raise KeyError('cannot identify telescope/instrument; please update'
'_pp_conf.instrument_keys accordingly')
# assign telescope parameters (telescopes.py)
telescope = _pp_conf.instrument_identifiers[instruments[0]]
obsparam = _pp_conf.telescope_parameters[telescope]
# create skycoadd in current directory
combination = pp_combine.combine(filenames, obsparam, False, None, None,
combinemethod, False,
display=True, diagnostics=True)
# create separate directory to analyze skycoadd data
rootdir = os.getcwd()
if os.path.exists('skycoadd/'):
shutil.rmtree('skycoadd/')
os.mkdir('skycoadd/')
os.rename('skycoadd.fits', 'skycoadd/skycoadd.fits')
os.chdir('skycoadd/')
# prepare image
# reset diagnostics for this data set
_pp_conf.dataroot, _pp_conf.diagroot, \
_pp_conf.index_filename, _pp_conf.reg_filename, _pp_conf.cal_filename, \
_pp_conf.res_filename = _pp_conf.setup_diagnostics()
# setup logging again (might be a different directory)
logging.basicConfig(filename = _pp_conf.log_filename,
level = _pp_conf.log_level,
format = _pp_conf.log_formatline,
datefmt = _pp_conf.log_datefmt)
preparation = pp_prepare.prepare(['skycoadd.fits'], obsparam,
{}, keep_wcs=True,
diagnostics=True, display=True)
### run photometry (curve-of-growth analysis)
source_minarea = obsparam['source_minarea']
background_only = False
target_only = False
if fixed_aprad == 0:
aprad = None # force curve-of-growth analysis
else:
aprad = fixed_aprad # skip curve_of_growth analysis
print('\n----- derive optimium photometry aperture\n')
phot = pp_photometry.photometry(['skycoadd.fits'], snr, source_minarea,
aprad,
None, background_only,
target_only,
telescope, obsparam, display=True,
diagnostics=True)
# data went through curve-of-growth analysis
if phot is not None:
aprad = phot['optimum_aprad']
# a fixed aperture radius has been used
else:
aprad = fixed_aprad
### run photometric calibration
minstars = _pp_conf.minstars
manualcatalog = None
print('\n----- run photometric calibration\n')
calibration = pp_calibrate.calibrate(['skycoadd.fits', minstars,
man_filtername,
manualcatalog, obsparam, display=True,
diagnostics=True)
zp = calibration['zeropoints'][0]['zp']
zp_err = calibration['zeropoints'][0]['zp_sig']
os.chdir(rootdir)
print(aprad, zp, zp_err)