-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdinos.py
368 lines (292 loc) · 11.2 KB
/
dinos.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
# Python utilities
import sys
import argparse
import json
import os
import shutil
import pandas as pd
# Astropy utilities
from astropy.time import Time
# DINOS utilities
import dino_tools as tools
import all_sky_map
import local_sky
import airmass
import finder_image
import object_stats
# read command line arguments
parser = argparse.ArgumentParser(description="Just an example",
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument("-i", "--input", help="input path")
parser.add_argument("-o", "--output", help="output path")
parser.add_argument("-v", "--verbose", action="store_true", help="increase verbosity")
args = vars(parser.parse_args())
# define templates
temp_night = """
\\newpage
\DIV[1]{{section}}*{{The Night}}\label{{Ueff}}
\\vspace{{-0.2cm}}\hrule
\\vspace{{1cm}}
\\begin{{minipage}}{{0.45\\textwidth}}
\\textbf{{Telescope Name}}: {0}
\\textbf{{Telescope Location}}: {2:.4f} {1:.4f}
\\textbf{{Telescope Elevation}}: {3}
\\textbf{{Sunset}}: {4}
\\textbf{{Sunrise}}: {5}
\\textbf{{Civil Twilights}}: {6} , {7}
\\textbf{{Nautical Twilights}}: {8} , {9}
\\textbf{{Astronomical Twilights}}: {10} , {11}
\end{{minipage}}
\hspace{{0.1\\textwidth}}
\\begin{{minipage}}{{0.45\\textwidth}}
\\textbf{{Observation Start}}: {12}
\\textbf{{Observation End}}: {13}
\\textbf{{Observation Blocks}}:
\\vspace{{0.2cm}}
\\begin{{tabular}}{{l|c|c}}%
\\bfseries Block Name & \\bfseries Start Time & \\bfseries End Time
\csvreader[head to column names]{{blocks.csv}}{{}}
{{\\\\\hline\\name & \starttime & \endtime }}
\end{{tabular}}
\end{{minipage}}
"""
temp_finder = """
\\newpage
\DIV[1]{{section}}*{{Finder Chart {0}}}\label{{Ueff}}
%\\vspace{{0.2cm}}
\\begin{{minipage}}{{0.6\\textwidth}}
\includegraphics[width=9cm]{{{1}/finder_{7}.jpg}}
\\vspace{{-2cm}}
\end{{minipage}}
\\begin{{minipage}}{{0.3\\textwidth}}
\\textbf{{Object RA}}: {2}
\\textbf{{Object DEC}}: {3}
\\textbf{{Object Type}}: {4}
\\textbf{{Spectral Type}}: {5}
\\textbf{{Apparent V magnitude}}: {6}
\end{{minipage}}
"""
#print(temp_finder)
def _read_input(path):
# load the json
with open(path) as f:
data = json.load(f)
f.close()
night_data = data['Night']
target_data = data['Targets']
config_data = data['Config']
return night_data, target_data, config_data
if __name__ == "__main__":
"""
Dino time
"""
print("""
_____ _____ _ _ ____ _____ __
| __ \_ _| \ | |/ __ \ / ____| /_ |
| | | || | | \| | | | | (___ __ _| |
| | | || | | . ` | | | |\___ \ \ \ / / |
| |__| || |_| |\ | |__| |____) | \ V /| |
|_____/_____|_| \_|\____/|_____/ \_/ |_|
""")
print("It's dino time\n")
# read command line arguments
if args['input'] == None:
args['input'] = "./dinos_config.json"
if args['output'] == None:
args['output'] = "./output"
if not os.path.exists(args['output']):
os.mkdir(args['output'])
if args['verbose']:
print("----------------------------------")
print("----------- Arguments ------------")
print("----------------------------------")
print(args)
print()
# read configuration file
print("reading configuration...")
night_data, target_data, config_data = _read_input(args['input'])
# define observer
print("defining observer...")
dino_loc = tools.setup_location(night_data['telescope_name'],
night_data['observer_lat'],
night_data['observer_long'],
night_data['observer_elevation'],
night_data['observer_timezone'])
print("setting up times...")
try:
bst = night_data['block_start_times']
except:
bst = None
try:
bet = night_data['block_end_times']
except:
bet = None
try:
bc = night_data['block_colors']
except:
bc = None
times = tools.setup_times(dino_loc,
night_data['obs_start'],
night_data['obs_end'],
block_start_times=bst,
block_end_times=bet,
block_colors=bc)
if args['verbose']:
print("----------------------------------")
print("---------- times -----------")
print("----------------------------------")
print(times)
print()
# setup blocks.csv
blocks_df = pd.DataFrame(columns=["name", "starttime", "endtime"])
i = 1
for this_block in times['blocks']:
this_dict = {
"name":"Block " + str(i),
"starttime":this_block['times'][0].iso.split()[1][:8],
"endtime":this_block['times'][1].iso.split()[1][:8]
}
this_df = pd.DataFrame(this_dict, index=[0])
blocks_df = pd.concat([blocks_df, this_df], join="outer")
i += 1
blocks_df.to_csv("{0}/blocks.csv".format(args['output']))
# define targets
if args['verbose']:
print("----------------------------------")
print("---------- target_data -----------")
print("----------------------------------")
print(target_data)
print()
print("setting up targets...")
targets = tools.setup_target_list(target_data, dino_loc)
if args['verbose']:
print("----------------------------------")
print("------------ targets -------------")
print("----------------------------------")
print(targets)
print()
# create targets.csv, rise_and_set.csv
df = pd.DataFrame(columns=["Object", "RA", "DEC", "oType", "spType",
"d", "V", "rise", "set", "lowest_a"])
for target in targets:
# get target properties
try:
data = object_stats.simbad_query(target['name'])
except:
try:
data = {"oType":" ",
"spType":" ",
"RA":str(target['target'].ra),
"DEC":str(target['target'].dec),
"d":" ",
"V":" "}
except:
data = {"oType":"non-fixed",
"spType":" ",
"RA":"variable",
"DEC":"variable",
"d":" ",
"V":" "}
data['Object'] = target['name']
# get rise time
try:
data['rise'] = dino_loc.target_rise_time(night_data['obs_start'],
target['target'],
which="nearest").iso.split()[1][:8]
#print(dino_loc.target_rise_time(night_data['obs_start'],
# target['target'],
# which="nearest").scale)
except:
data['rise'] = "NA"
# get set time
try:
data['set'] = dino_loc.target_set_time(night_data['obs_start'],
target['target'],
which="nearest").iso.split()[1][:8]
except:
data['set'] = "NA"
# get time of lowest airmass
# append it to the dataframe
this_df = pd.DataFrame(data, index=[0])
df = pd.concat([df, this_df], join="outer")
target.update(data)
if args['verbose']:
print("----------------------------------")
print("------------ targets -------------")
print("----------------------------------")
print(targets)
print()
df.to_csv("{}/targets.csv".format(args['output']))
# create all-sky map
print("creating plots...")
print("all sky map")
all_sky_map.plot(targets, times=times, path=args['output'],
observer=dino_loc, **config_data['all_sky_map'])
# create local-sky map
print("local sky map")
local_sky.plot(dino_loc, times, targets, path=args['output'],
**config_data['local_sky'])
# create airmass plot
print("airmass")
airmass.plot(dino_loc, times, targets, path=args['output'],
**config_data['airmass'])
# create finder images
for target in targets:
print("Finder image {0}".format(target['name']))
finder_image.plot(target, path=args['output'],
**config_data['finder_images'])
finder_charts = ""
for target in targets:
finder_charts += temp_finder.format(target['name'],
args['output'],
target['RA'],
target['DEC'],
target['oType'],
target['spType'],
target['V'],
target['name'].replace(" ", "").replace(".", "_"))
# create the night page
print("formatting document...")
night_page = temp_night.format(
night_data['telescope_name'],
night_data['observer_lat'],
night_data['observer_long'],
night_data['observer_elevation'],
times['sunset'].iso,
times['sunrise'].iso,
times['civ_twl'][0].iso.split()[1][:8],
times['civ_twl'][1].iso.split()[1][:8],
times['nau_twl'][0].iso.split()[1][:8],
times['nau_twl'][1].iso.split()[1][:8],
times['ast_twl'][0].iso.split()[1][:8],
times['ast_twl'][1].iso.split()[1][:8],
night_data['obs_start'],
night_data['obs_end']
)
# edit latex template
this_date = night_data['obs_start'].split()[0]
# read the template
with open('report_template.tex', 'r') as file:
file_data = file.read()
# Searching and replacing the text
file_data = file_data.replace("DD-MM-YYYY", this_date)
file_data = file_data.replace("TELESCOPE-NAME",
night_data['telescope_name'])
file_data = file_data.replace("%FINDERCHARTS", finder_charts)
file_data = file_data.replace("%THENIGHT", night_page)
file_data = file_data.replace("OUTPUTDIRECTORY", args['output'])
# generate the name of the new tex file
script_name = '{0}/dinos_{1}_{2}.tex'.format(args['output'],
this_date,
night_data['telescope_name'].replace(" ", "-"))
# write changes to new tex file
with open(script_name, 'w') as file:
# Writing the replaced data in our
# text file
file.write(file_data)
# run report script
if args['verbose']:
os.system("pdflatex -jobname dinos_report -output-directory {0} {1}".format(args['output'], script_name))
else:
os.system("pdflatex -jobname dinos_report -output-directory {0} --interaction=batchmode {1}".format(args['output'], script_name))
print("Done!")