-
Notifications
You must be signed in to change notification settings - Fork 0
/
astrasens_run.py
executable file
·197 lines (167 loc) · 6.67 KB
/
astrasens_run.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
#!/usr/bin/env python
'''
--- astrasens_run.py ---
Purpose
--------
Get sensitivity limits and create summary plots
Inputs and Options
------------------
Example:
--------
python astrasens_run.py image
Version Control:
----------------
2018/12/05 jlillobox First version
'''
# ======================================
# Imports
# ======================================
import numpy as np
import emcee
import scipy.optimize as op
from math import *
import get_xx
from astropy.io import ascii
import emcee
import scipy.optimize as op
import sys
from astropy.table import Table, Column
import math
#import reveal_prepare as prepare
import astrasens_fitter as fitter
import astrasens_plot as plotting
import multiprocessing
import time
import argparse
import os
from termcolor import colored
import glob
def run(args):
'''run
Main function to run the different scripts with different options
Parameter
---------
image image name
Returns
-------
none
'''
ana_fold = args.root+'/22_ANALYSIS/'
night_fold = args.root+'/22_ANALYSIS/'+args.night+'/'
sens_fold = args.root+'/22_ANALYSIS/'+args.night+'/Sensitivity/'
sour_fold = args.root+'/22_ANALYSIS/'+args.night+'/DetectedSources/'
summ_fold = args.root+'/22_ANALYSIS/'+args.night+'/Summary_plots/'
if (os.path.isdir(ana_fold) == False): os.mkdir(ana_fold)
if (os.path.isdir(night_fold) == False): os.mkdir(night_fold)
if (os.path.isdir(sens_fold) == False): os.mkdir(sens_fold)
if (os.path.isdir(sour_fold) == False): os.mkdir(sour_fold)
if (os.path.isdir(summ_fold) == False): os.mkdir(summ_fold)
if args.PLOTS == True:
sources, target, popt, fake, myfwhm, center, sources2 = fitter.sources(args)
plotting.plotting(sources, target, popt, fake, myfwhm, center, sources2, args)
else:
# ===== Find sources in the image
print("\n")
print("+++++++++++++++++++++++++")
print("Source identification...")
print("+++++++++++++++++++++++++")
filename = fitter.get_filename(args)
outfile = args.root+'/22_ANALYSIS/'+args.night+'/DetectedSources/'+filename+'__Sources.npz'
if os.path.isfile(outfile) == True and args.FORCEDET == False:
print("\t --> File *Sources.nzp found, sources recovered. Use --FORCEDET to force re-analysis.")
results = np.load(outfile)
sources = results['sources']
target = results['target']
popt = results['popt']
fake = results['fake']
myfwhm = results['myfwhm']
center = results['center']
sources2 = results['sources2']
else:
sources, target, popt, fake, myfwhm, center, sources2 = fitter.sources(args)
# ===== Sensitivity
print("\n")
print("+++++++++++++++++++++++++")
print("Sensitivity curve... ")
print("+++++++++++++++++++++++++")
# file = os.path.splitext(args.image)[0]
# rate = file.split('_')[1]
# filename = file[14:]+'_'+rate
sensitivity_file = args.root+'/22_ANALYSIS/'+args.night+'/Sensitivity/'+filename+'__Sensitivity.npz'
if os.path.isfile(sensitivity_file) and args.FORCE is not True:
print("\t --> File found, sensitivity curve not re-calculated. Use --FORCE to force overwrite")
else:
fitter.sensitivity(sources, target, popt, fake, myfwhm, args)
# ===== Plots
print("\n")
print("+++++++++++++++++++++++++")
print("Plotting... ")
print("+++++++++++++++++++++++++")
plotting.plotting(sources, target, popt, fake, myfwhm, center, sources2, args)
def cli():
parser = argparse.ArgumentParser()
parser.add_argument("image", help="Image name")
parser.add_argument("root", help="Root folder")
parser.add_argument("night", help="Night folder")
parser.add_argument("-G", "--GDR3", help="Gaia DR3 id of the observed source", default=None)
parser.add_argument("-V", "--VERBOSE", help="VERBOSE", action="store_true")
parser.add_argument("-P", "--PLOTS", help="Plots only", action="store_true")
parser.add_argument("-F", "--FORCE", help="Force re-calculation of sensitivity curve", action="store_true")
parser.add_argument("-FD", "--FORCEDET", help="Force detection of source companions", action="store_true")
parser.add_argument("-I", "--IPAC", help="Format output of detected sources", action="store_true")
parser.add_argument("-W", "--WINDOW", help="Maximum distance to consider", default=None,type=float)
parser.add_argument("-T", "--TIC", help="TIC id of the object if known", default=None,type=int)
args = parser.parse_args()
return args
# ======================================
# MAIN
# ======================================
if __name__ == "__main__":
print( "............................................")
print(colored(" _ ", "light_yellow"))
print(colored(" __ _ ___| |_ _ __ __ _ ___ ___ _ __ ___ ", "light_yellow"))
print(colored(" / _` / __| __| '__/ _` / __|/ _ \ '_ \/ __|", "light_yellow"))
print(colored("| (_| \__ \ |_| | | (_| \__ \ __/ | | \__ \ ", "light_yellow"))
print(colored(" \__,_|___/\__|_| \__,_|___/\___|_| |_|___/", "light_yellow"))
print( " by @jlillobox ")
print( "............................................")
print('\n')
args = cli()
if args.image == 'all':
_images = glob.glob(os.path.join(args.root,'11_REDUCED',args.night,'TDRIZZLE*0100*.fits'))
fok = open(os.path.join(args.root,'files_completed.lis'),'w')
fnotok = open(os.path.join(args.root,'files_error.lis'),'w')
for i in _images:
image = os.path.basename(i)
print(colored("=======================================================", "light_blue"))
print(colored("Running image "+image, "light_blue"))
print(colored("=======================================================", "light_blue"))
args.image = image
try:
run(args)
fok.writelines(image+'\n')
print(colored(image +"...ok \n", 'green'))
except:
fnotok.writelines(image+'\n')
print(colored("ERROR:" +image +" could not be processed \n", 'red'))
fok.close()
fnotok.close()
elif args.image.endswith('.lis'):
images = np.genfromtxt(args.image, dtype=None, encoding='utf-8')
for image in images:
print(colored("=======================================================", "light_blue"))
print(colored("Running image "+image, "light_blue"))
print(colored("=======================================================", "light_blue"))
args.image = image
try:
run(args)
print(colored(image +"...ok \n", 'green'))
except:
print(colored("ERROR:" +image +" could not be processed \n", 'red'))
else:
print(colored("=======================================================", "light_blue"))
print(colored("Running image "+args.image, "light_blue"))
print(colored("=======================================================", "light_blue"))
run(args)
print('\n')
print(colored(args.image +"...ok \n", 'green'))