forked from typpo/astrokit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
run_image_reductions.py
executable file
·154 lines (122 loc) · 5.49 KB
/
run_image_reductions.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
#!/usr/bin/env python
#
# Usage: ./run_image_reductions.py [analysis_id]
#
import logging
import os
import sys
import django
import numpy as np
from django.core.exceptions import ObjectDoesNotExist
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'settings')
sys.path.insert(0, os.getcwd())
django.setup()
import airmass
import corrections
from imageflow.models import Reduction, ImageAnalysis, ImageFilter
from reduction.util import find_star_by_designation
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
def supporting_calculations(analysis, reduction):
'''Compute a variety of terms for the main photometry reduction equation.
'''
# Only reduce {comparison stars + target}
logger.info('Target: %s' % analysis.get_target())
reduction.reduced_stars = [analysis.get_target()] + analysis.get_comparison_stars()
# Airmass
airmass.annotate_with_airmass(analysis, reduction)
# Compute lighttime correction.
analysis.image_jd = corrections.get_uncorrected_jd(analysis)
analysis.image_jd_corrected = corrections.get_lighttime_correction(analysis)
def run_reductions(analysis):
'''Run reductions on a given ImageAnalysis.
'''
logger.info('Running reductions for analysis %d' % analysis.id)
reduction = analysis.get_or_create_reduction()
lightcurve_reduction = analysis.lightcurve.get_or_create_reduction()
supporting_calculations(analysis, reduction)
# Get the URAT1 keys for each CI band. eg. 'Bmag', 'jmag'
magband_key = analysis.lightcurve.magband.urat1_key
use_color_index = analysis.lightcurve.ciband != 'NONE'
if use_color_index:
ci1_key = analysis.lightcurve.get_ci_band1().urat1_key
ci2_key = analysis.lightcurve.get_ci_band2().urat1_key
# Get the set of comparison stars. These are a subset of reduced stars.
comparison_star_desigs = analysis.lightcurve.get_comparison_desigs()
# Now put it all together.
for i in xrange(len(reduction.reduced_stars)):
# Assume this star is our target (in reality, there is 1 true target
# and N reference stars)
star = reduction.reduced_stars[i]
# Mt = (mt - mc) - k"f Xt (CIt - CIc) + Tf (CIt - CIc) + Mc
estimates = []
for desig in comparison_star_desigs:
if 'designation' in star and star['designation'] == desig:
# Don't compare a star against itself.
continue
# Get comparison star in THIS IMAGE.
comparison_star = find_star_by_designation(analysis.annotated_point_sources, desig)
if not comparison_star:
logger.error('Could not find comparison star %s in reductions for analysis %d' % (desig, analysis.id))
continue
term1 = star['mag_instrumental'] - comparison_star['mag_instrumental']
# Right now we use the color index computed for the TARGET, not
# necessarily this star.
# TODO(ian): Use correct color index for each star.
if use_color_index:
ci_target = lightcurve_reduction.color_index_manual
if not ci_target:
# No manual color index, use computed.
ci_target = lightcurve_reduction.color_index
ci_comparison = comparison_star[ci1_key] - comparison_star[ci2_key]
ci_diff = ci_target - ci_comparison
term2 = lightcurve_reduction.second_order_extinction * star['airmass'] * ci_diff
term3 = lightcurve_reduction.tf * ci_diff
else:
term2 = 0
term3 = 0
mc = comparison_star[magband_key]
combined = term1 - term2 + term3 + mc
if star['id'] == analysis.target_id:
print '%s\t%f\t%f\t%f\t%f' % \
(comparison_star['designation'], combined, star['mag_instrumental'],
comparison_star['mag_instrumental'], mc)
estimates.append(combined)
star['mag_standard'] = np.mean(estimates)
star['mag_std'] = np.std(estimates)
# Set mag_catalog to the URAT1 magnitude in this band.
if magband_key in star:
star['mag_catalog'] = star[magband_key]
star['mag_error'] = star['mag_standard'] - star['mag_catalog']
else:
#logger.info('No filter %s in star %s' % (magband_key, star))
pass
reduction.status = Reduction.COMPLETE
reduction.save()
analysis.status = ImageAnalysis.REDUCTION_COMPLETE
analysis.save()
logger.info('Completed reduction %d for analysis %d' % (reduction.id, analysis.id))
def process_pending_reductions():
pending = Reduction.objects.filter(status=Reduction.PENDING)
for reduction in pending:
if reduction.analysis.target_id and reduction.analysis.target_id > 0:
run_reductions(reduction.analysis)
if __name__ == '__main__':
import os
import django
from django.core.exceptions import ObjectDoesNotExist
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'settings')
sys.path.insert(0, os.getcwd())
django.setup()
from imageflow.models import ImageAnalysis
if len(sys.argv) > 1:
analid = int(sys.argv[1])
try:
analysis = ImageAnalysis.objects.get(pk=analid)
except ObjectDoesNotExist:
logger.info('Could not find analysis %d' % analid)
sys.exit(1)
run_reductions(analysis)
else:
# Run all pending reductions.
process_pending_reductions()