Skip to content

Commit

Permalink
experimental support for different photometry flavors added
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Mommert committed Nov 2, 2017
1 parent 7af345e commit cc8e663
Show file tree
Hide file tree
Showing 8 changed files with 140 additions and 94 deletions.
6 changes: 2 additions & 4 deletions _pp_conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,8 +162,6 @@ def setup_diagnostics():
# photometric calibration
minstars = 0.5

# support
# photometry flavor ['APER', 'ISOCOR', 'AUTO']
photmode = 'AUTO'

# path to local variable star database file
# make sure set this variable accordingly if you want to use the catalog
vsx_database_file = os.environ.get('PPVARSTARSDB')
17 changes: 12 additions & 5 deletions diagnostics.py
Original file line number Diff line number Diff line change
Expand Up @@ -835,7 +835,8 @@ def add_results(data, imagestretch='linear'):
stretch={'linear': LinearStretch(),
'log': LogStretch()}[imagestretch])
# extract aperture radius
aprad = float(hdulist[0].header['APRAD'])
if _pp_conf.photmode == 'APER':
aprad = float(hdulist[0].header['APRAD'])

# create plot
#plotsize = 7. # inches
Expand All @@ -852,10 +853,16 @@ def add_results(data, imagestretch='linear'):
color='white')

# place aperture
circle = plt.Circle((old_div(boxsize,2.), old_div(boxsize,2.)),
aprad, ec='red', fc='none', linewidth=1)
plt.gca().add_patch(circle)

if _pp_conf.photmode == 'APER':
targetpos = plt.Circle((boxsize/2, boxsize/2),
aprad, ec='red', fc='none',
linewidth=1)
else:
targetpos = plt.Rectangle((boxsize/2-7, boxsize/2-7),
15, 15, ec='red', fc='none',
linewidth=1)
plt.gca().add_patch(targetpos)

# place expected position (if within thumbnail)
if (abs(exp_x-obj_x) <= old_div(boxsize,2.) and
abs(exp_y-obj_y) <= old_div(boxsize,2.)):
Expand Down
23 changes: 13 additions & 10 deletions pp_calibrate.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,10 +177,12 @@ def derive_zeropoints(ref_cat, catalogs, filtername, minstars_external,
### currently it seems like pp_photometry (maybe callhorizons)
# has not finished properly

cat.reject_sources_other_than(cat.data['MAG_APER'] != 99)
cat.reject_sources_other_than(cat.data['MAGERR_APER'] != 99)
cat.reject_sources_with(numpy.isnan(cat.data['MAG_APER']))
cat.reject_sources_with(numpy.isnan(cat.data['MAGERR_APER']))
cat.reject_sources_other_than(cat.data['MAG_'+_pp_conf.photmode] != 99)
cat.reject_sources_other_than(cat.data['MAGERR_'
+_pp_conf.photmode] != 99)
cat.reject_sources_with(numpy.isnan(cat.data['MAG_'+_pp_conf.photmode]))
cat.reject_sources_with(numpy.isnan(cat.data['MAGERR_'+
_pp_conf.photmode]))

match = ref_cat.match_with(cat,
match_keys_this_catalog=['ra.deg', 'dec.deg'],
Expand All @@ -190,8 +192,9 @@ def derive_zeropoints(ref_cat, catalogs, filtername, minstars_external,
'ident',
'ra.deg',
'dec.deg'],
extract_other_catalog=['MAG_APER',
'MAGERR_APER'],
extract_other_catalog=['MAG_'+_pp_conf.photmode,
'MAGERR_'+
_pp_conf.photmode],
tolerance=old_div(_pp_conf.pos_epsilon,3600.))

# artificially blow up incredibly small ref_cat uncertainties
Expand Down Expand Up @@ -305,8 +308,8 @@ def derive_zeropoints(ref_cat, catalogs, filtername, minstars_external,


cat.add_fields([filterkey, efilterkey],
[cat['MAG_APER'] + clipping_steps[idx][0],
numpy.sqrt(cat['MAGERR_APER']**2 + \
[cat['MAG_'+_pp_conf.photmode] + clipping_steps[idx][0],
numpy.sqrt(cat['MAGERR_'+_pp_conf.photmode]**2 + \
clipping_steps[idx][1]**2)],
['F', 'F'])

Expand Down Expand Up @@ -436,8 +439,8 @@ def calibrate(filenames, minstars, manfilter, manualcatalog,
efilterkey = 'e_' + filtername + 'mag'
for cat in catalogs:
cat.add_fields([filterkey, efilterkey],
[cat['MAG_APER'] + magzp[0],
numpy.sqrt(cat['MAGERR_APER']**2 + \
[cat['MAG_'+_pp_conf.photmode] + magzp[0],
numpy.sqrt(cat['MAGERR_'+_pp_conf.photmode]**2 + \
magzp[1]**2)],
['F', 'F'])
cat.origin = (cat.origin.strip()+
Expand Down
28 changes: 16 additions & 12 deletions pp_distill.py
Original file line number Diff line number Diff line change
Expand Up @@ -574,7 +574,8 @@ def distill(catalogs, man_targetname, offset, fixed_targets_file, posfile,

# build field lists for observed catalogs
match_keys_other_catalog, extract_other_catalog = [], []
for key in ['ra.deg', 'dec.deg', 'XWIN_IMAGE', 'YWIN_IMAGE', 'FLAGS']:
for key in ['ra.deg', 'dec.deg', 'XWIN_IMAGE', 'YWIN_IMAGE',
'FLAGS', 'FWHM_WORLD']:
if key in cat.fields:
match_keys_other_catalog.append(key)
extract_other_catalog.append(key)
Expand Down Expand Up @@ -606,11 +607,11 @@ def distill(catalogs, man_targetname, offset, fixed_targets_file, posfile,
cal_mag, cal_magerr,
cat.obstime, cat.catalogname,
match[1][2][i], match[1][3][i],
cat.origin, match[1][4][i]])
cat.origin, match[1][4][i], match[1][5][i]])
# format: ident, RA_exp, Dec_exp, RA_img, Dec_img,
# mag_inst, sigmag_instr, mag_cal, sigmag_cal
# obstime, filename, img_x, img_y, origin, flags

# fwhm
targetnames[match[0][2][i]] = 1


Expand All @@ -630,13 +631,13 @@ def distill(catalogs, man_targetname, offset, fixed_targets_file, posfile,

outf = open('photometry_%s.dat' %
target.translate(_pp_conf.target2filename), 'w')
outf.write('# filename julian_date '
'ast_mag ast_sig ast_ra ast_dec '
'[1] [2] [3] [4] [5] ZP ZP_sig '
'inst_mag in_sig [6] [7] [8] [9]\n')
outf.write('# filename julian_date ' +
'mag sig source_ra source_dec [1] [2] ' +
'[3] [4] [5] ZP ZP_sig inst_mag ' +
'in_sig [6] [7] [8] [9] [10] ' +
'FWHM\n')

for dat in data:

# sort measured magnitudes by target
if dat[0] == target:
try:
Expand Down Expand Up @@ -676,17 +677,20 @@ def distill(catalogs, man_targetname, offset, fixed_targets_file, posfile,
('%s ' % catalogname) +
('%s ' % filtername) +
('%3d ' % dat[14]) +
('%s\n' % dat[13].split(';')[0]))
('%s' % dat[13].split(';')[0]) +
('%10s ' % _pp_conf.photmode) +
('%4.2f\n' % (dat[15]*3600)))

outf.writelines('#\n# [1]: predicted_RA - image_RA [arcsec]\n'+
'# [2]: predicted_Dec - image_Dec [arcsec]\n'+
outf.writelines('#\n# [1]: predicted_RA - source_RA [arcsec]\n'+
'# [2]: predicted_Dec - source_Dec [arcsec]\n'+
'# [3,4]: manual target offsets in RA and DEC ' +
'[arcsec]\n'+
'# [5]: exposure time (s)\n'+
'# [6]: photometric catalog\n' +
'# [7]: photometric band\n' +
'# [8]: Source Extractor flag\n' +
'# [9]: telescope/instrument\n')
'# [9]: telescope/instrument\n' +
'# [10]: photometry method\n')
outf.close()


Expand Down
34 changes: 20 additions & 14 deletions pp_extract.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,11 @@ def extract_singleframe(data):

# prepare running SEXTRACTOR
os.remove(ldacname) if os.path.exists(ldacname) else None
optionstring = ' -PHOT_APERTURES %s ' % \
param['aperture_diam']

optionstring = ''
if _pp_conf.photmode == 'APER':
optionstring += ' -PHOT_APERTURES %s ' % \
param['aperture_diam']
if ('global_background' in param and
param['global_background']):
optionstring += ' -BACKPHOTO_TYPE GLOBAL '
Expand Down Expand Up @@ -239,18 +242,21 @@ def extract_multiframe(filenames, parameters):
return {}

# set aperture photometry DIAMETER as string
if ((type(parameters['aprad']) == float and parameters['aprad'] == 0)
or (type(parameters['aprad']) == list
and len(parameters['aprad']) == 0)):
parameters['aperture_diam'] = str(parameters['obsparam']
['aprad_default']*2)
else:
if not isinstance(parameters['aprad'], list) and \
not isinstance(parameters['aprad'], numpy.ndarray):
parameters['aprad'] = [str(parameters['aprad'])]
parameters['aperture_diam'] = ','.join([str(float(rad)*2.) for
rad in parameters['aprad']])

if _pp_conf.photmode == 'APER':
if ((type(parameters['aprad']) == float and
parameters['aprad'] == 0)
or (type(parameters['aprad']) == list
and len(parameters['aprad']) == 0)):
parameters['aperture_diam'] = str(parameters['obsparam']
['aprad_default']*2)
else:
if not isinstance(parameters['aprad'], list) and \
not isinstance(parameters['aprad'], numpy.ndarray):
parameters['aprad'] = [str(parameters['aprad'])]
parameters['aperture_diam'] = ','.join([str(float(rad)*2.)
for rad in
parameters['aprad']])

#check what the binning is and if there is a mask available
binning = get_binning(hdu[0].header, parameters['obsparam'])
bin_string = '%d,%d' % (binning[0], binning[1])
Expand Down
115 changes: 71 additions & 44 deletions pp_photometry.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,13 +173,15 @@ def curve_of_growth_analysis(filenames, parameters,
(filename, targetname,
residuals[numpy.argmin(residuals)]*3600.))
else:
target_flux.append(old_div(data[target_idx]['FLUX_APER'],
max(data[target_idx]['FLUX_APER'])))
target_flux.append(old_div(data[target_idx]['FLUX_'+
_pp_conf.photmode],
max(data[target_idx]['FLUX_'+
_pp_conf.photmode])))
target_snr.append(
data[target_idx]['FLUX_APER']/\
data[target_idx]['FLUXERR_APER']/ \
max(old_div(data[target_idx]['FLUX_APER'], \
data[target_idx]['FLUXERR_APER'])))
data[target_idx]['FLUX_'+_pp_conf.photmode]/\
data[target_idx]['FLUXERR_'+_pp_conf.photmode]/ \
max(old_div(data[target_idx]['FLUX_'+_pp_conf.photmode], \
data[target_idx]['FLUXERR_'+_pp_conf.photmode])))
n_target_identified += 1

### extract background source fluxes and snrs
Expand All @@ -188,18 +190,19 @@ def curve_of_growth_analysis(filenames, parameters,
#n_src = data.shape[0] # use all sources
n_src = 50 # use only 50 sources
for idx, src in enumerate(data.data[:n_src]):
if (numpy.any(numpy.isnan(src['FLUX_APER'])) or
numpy.any(numpy.isnan(src['FLUXERR_APER'])) or
if (numpy.any(numpy.isnan(src['FLUX_'+_pp_conf.photmode])) or
numpy.any(numpy.isnan(src['FLUXERR_'+_pp_conf.photmode])) or
src['FLAGS'] > 3):
continue

# create growth curve
background_flux.append(old_div(src['FLUX_APER'],\
max(src['FLUX_APER'])))
background_snr.append(src['FLUX_APER']/\
src['FLUXERR_APER']/\
max(old_div(src['FLUX_APER'],\
src['FLUXERR_APER'])))
background_flux.append(old_div(src['FLUX_'+_pp_conf.photmode],\
max(src['FLUX_'+_pp_conf.photmode])))
background_snr.append(src['FLUX_'+_pp_conf.photmode]/\
src['FLUXERR_'+_pp_conf.photmode]/\
max(old_div(src['FLUX_'+
_pp_conf.photmode],\
src['FLUXERR_'+_pp_conf.photmode])))


###### investigate curve-of-growth
Expand Down Expand Up @@ -334,9 +337,9 @@ def curve_of_growth_analysis(filenames, parameters,

##### display results
if display:
print('\n########################## APERTURE CORRECTION SUMMARY:\n###')
print('\n#################################### PHOTOMETRY SUMMARY:\n###')
print('### best-fit aperture radius %5.2f (px)' % (optimum_aprad))
print('###\n######################################################\n')
print('###\n#####################################################\n')

logging.info('==> best-fit aperture radius: %3.1f (px)' % (optimum_aprad))

Expand All @@ -362,37 +365,61 @@ def photometry(filenames, sex_snr, source_minarea, aprad,
'quiet' : not display}

### do curve-of-growth analysis if aprad not provided
if aprad is None:

# aperture radius list
aprads = numpy.linspace(obsparam['aprad_range'][0],
obsparam['aprad_range'][1], 20)
for filename in filenames:
hdu = fits.open(filename, mode='update',
ignore_missing_end=True)
hdu[0].header['PHOTMODE'] = (_pp_conf.photmode,
'PP photometry mode')
hdu.flush()
hdu.close()

photpar['aprad'] = aprads
cog = curve_of_growth_analysis(filenames, photpar,
display=display,
diagnostics=diagnostics)
aprad = cog['optimum_aprad']
if _pp_conf.photmode == 'APER':
if aprad is None:
# aperture radius list
aprads = numpy.linspace(obsparam['aprad_range'][0],
obsparam['aprad_range'][1], 20)

photpar['aprad'] = aprads
cog = curve_of_growth_analysis(filenames, photpar,
display=display,
diagnostics=diagnostics)
aprad = cog['optimum_aprad']
else:
## add manually selected aprad to image headers
for filename in filenames:
hdu = fits.open(filename, mode='update',
ignore_missing_end=True)
hdu[0].header['APRAD'] = (aprad,
'manual aperture phot radius (px)')
hdu.flush()
hdu.close()

# run extract using (optimum) aprad
photpar['aprad'] = round(aprad, 2)
photpar['paramfile'] = (_pp_conf.rootpath+
'/setup/singleaperture.sexparam')
logging.info('extract sources using optimum aperture from %d images' % \
len(filenames))

if display:
print(('* extract sources from %d images using aperture ' \
+ 'radius %4.2fpx') % \
(len(filenames), aprad))
else:
## add manually selected aprad to image headers
for filename in filenames:
hdu = fits.open(filename, mode='update', ignore_missing_end=True)
hdu[0].header['APRAD'] = (aprad,
'manual aperture phot radius (px)')
hdu.flush()
hdu.close()

# run extract using (optimum) aprad
photpar['aprad'] = round(aprad, 2)
photpar['paramfile'] = _pp_conf.rootpath+'/setup/singleaperture.sexparam'
logging.info('extract sources using optimum aperture from %d images' % \
len(filenames))

if display:
print(('* extract sources from %d images using aperture ' \
+ 'radius %4.2fpx') % \
(len(filenames), aprad))
photpar['aprad'] = None
photpar['paramfile'] = (_pp_conf.rootpath+
'/setup/singleaperture.sexparam')

logging.info('extract sources using ' + _pp_conf.photmode +
' photometry')

if display:
print(('* extract sources from %d images using ' \
+ _pp_conf.photmode + ' photometry') % \
len(filenames))

photpar['photmode'] = _pp_conf.photmode

pp_extract.extract_multiframe(filenames, photpar)


Expand Down
3 changes: 2 additions & 1 deletion pp_run.py
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,8 @@ def run_the_pipeline(filenames, man_targetname, man_filtername,
"only </FONT>; "
# a fixed aperture radius has been used
else:
summary_message += "using a fixed aperture radius of %.1f px;" % aprad
if _pp_conf.photmode == 'APER':
summary_message += "using a fixed aperture radius of %.1f px;" % aprad


# add information to summary website, if requested
Expand Down
8 changes: 4 additions & 4 deletions setup/singleaperture.sexparam
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ FLUX_APER # Flux vector within fixed circular aperture(s)
FLUXERR_APER # RMS error vector for aperture flux(es) [count]
MAG_APER # Fixed aperture magnitude vector [mag]
MAGERR_APER # RMS error vector for fixed aperture mag. [mag]
#FLUX_AUTO Flux within a Kron-like elliptical aperture [count]
#FLUXERR_AUTO RMS error for AUTO flux [count]
#MAG_AUTO Kron-like elliptical aperture magnitude [mag]
#MAGERR_AUTO RMS error for AUTO magnitude [mag]
FLUX_AUTO Flux within a Kron-like elliptical aperture [count]
FLUXERR_AUTO RMS error for AUTO flux [count]
MAG_AUTO Kron-like elliptical aperture magnitude [mag]
MAGERR_AUTO RMS error for AUTO magnitude [mag]
#FLUX_PETRO Flux within a Petrosian-like elliptical aperture [count]
#FLUXERR_PETRO RMS error for PETROsian flux [count]
#MAG_PETRO Petrosian-like elliptical aperture magnitude [mag]
Expand Down

0 comments on commit cc8e663

Please sign in to comment.