-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathastro-countsubs.py
489 lines (402 loc) · 16.8 KB
/
astro-countsubs.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
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
#!/usr/bin/env python
# Copyright 2023-2025 Martin Junius
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ChangeLog
# Version 0.1 / 2023-07-07
# Added to repository asto
# Version 0.2 / 2023-10-10
# Allow VdS-style filename, new exposure time option
# Version 0.3 / 2023-12-18
# Refactored, output for Astrobin CSV
# Version 0.4 / 2024-02-08
# Get settings from JSON config, -D --extra-data option removed,
# output calibration data also for text output
# Version 0.5 / 2024-06-18
# New option --calibration-set to select calibration data set from JSON config
# New option -d --debug
# Support OBJECT_YYYY-MM-DD subdirs
# Version 1.0 / 2024-07-06
# Version bumped to 1.0, fully working now
# Allow -C option without -o, write CSV to stdout
# Version 1.1 / 2024-07-13
# Added -m --match option
# Version 1.2 / 2024-07-15
# Added -T --total-only / -N --no-calibration options
# Use new module csvoutput
# Version 1.3 / 2024-11-26
# Added -M --markdown option, output as markdown table
# Version 1.4 / 2024-12-07
# Added --target get, include target name in date stats,
# works with TARGET_YYYY-MM-DD subdirectories only
# Version 1.5 / 2025-02-02
# Fixed behavior for normal output without --calibration-set,
# still required for CSV Astrobin output
# Option -N is now -n
VERSION = "1.5 / 2025-02-02"
AUTHOR = "Martin Junius"
NAME = "astro-countsubs"
import os
import argparse
import re
# The following libs must be installed with pip
from icecream import ic
# Disable debugging
ic.disable()
# Local modules
from verbose import verbose, error
from jsonconfig import JSONConfig, config
from csvoutput import csv_output as CSVOutput
global FILTER, EXPOSURE
FILTER = ["L", "R", "G", "B", "Ha", "OIII", "SII"]
EXPOSURE = None
# ASTROBIN_FIELDS = [ "date", "filter", "number", "duration", "binning", "gain", "sensorCooling", "fNumber",
# "darks", "flats", "flatDarks", "bias", "bortle", "meanSqm", "meanFwhm", "temperature" ]
ASTROBIN_FIELDS = [ "date", "filter", "number", "duration", "binning", "gain", "sensorCooling", "fNumber",
"darks", "flats", "flatDarks", "bias", "bortle" ]
KEY_FILTER_SETS = "filter sets"
KEY_CALIBRATION_SETS = "calibration sets"
KEY_SETTINGS = "settings"
# Read config with filter sets and calibration frames
class AstroConfig(JSONConfig):
def __init__(self, file=None):
super().__init__(file)
def get_filter_id(self, filter_set, filter):
obj = self.get_json()
if KEY_FILTER_SETS in obj:
sets = obj[KEY_FILTER_SETS]
if filter_set in sets:
return sets[filter_set][filter]
else:
error(f"unknown filter set \"{filter_set}\"")
else:
error(f"no key \"{KEY_FILTER_SETS}\" in config")
def get_calibration(self, calibration_set, mastertype, param="*"):
obj = self.get_json()
if KEY_CALIBRATION_SETS in obj:
sets = obj[KEY_CALIBRATION_SETS]
if calibration_set in sets:
cal = sets[calibration_set]
if mastertype in cal:
return cal[mastertype][param]
else:
##FIXME: warning()
verbose(f"unknown mastertype \"{mastertype}\"")
return ""
else:
error(f"unknown calibration set \"{calibration_set}\"")
else:
error(f"no key \"{KEY_CALIBRATION_SETS}\" in config")
def get_calibration1(self, calibration_set, mastertype):
""" get first item from mastertype dict """
obj = self.get_json()
if KEY_CALIBRATION_SETS in obj:
sets = obj[KEY_CALIBRATION_SETS]
if calibration_set in sets:
cal = sets[calibration_set]
if mastertype in cal:
kw = cal[mastertype]
for param, n in kw.items():
return (param, n)
else:
return (False, False)
else:
error(f"unknown calibration set \"{calibration_set}\"")
else:
error(f"no key \"{KEY_CALIBRATION_SETS}\" in config")
def get_settings(self, calibration_set):
obj = self.get_json()
if not KEY_CALIBRATION_SETS in obj:
error(f"no key \"{KEY_CALIBRATION_SETS}\" in config")
sets = obj[KEY_CALIBRATION_SETS]
if calibration_set in sets:
cal = sets[calibration_set]
if not KEY_SETTINGS in cal:
error(f"no key \"{KEY_SETTINGS}\" in config")
return cal[KEY_SETTINGS]
def get_setting(self, calibration_set, key):
settings = self.get_settings(calibration_set)
if key in settings:
return settings[key]
else:
return ""
# Get config with filter sets and calibration frames
config = AstroConfig("astro-countsubs-config.json")
# Options
class Options:
match = None # -m --match
total_only = False # -T --total-only
no_calibration = False # -N --no-calibration
csv = False # -C --csv
output = None # -o --output
filter_set = None # -F --filter-set
calibration_set = None # --calibration-set
markdown = False # -M --markdown
target = False # --target
def walk_the_dir(dir):
exposures = {}
if not os.path.isdir(dir):
error(f"no such directory {dir}")
for dirName, subdirList, fileList in os.walk(dir):
verbose(f"found directory {dirName}")
# Test for Astro dir ...
m = re.search(r'[\\/](\d\d\d\d-\d\d-\d\d)[\\/]LIGHT$', dirName)
if not m:
m = re.search(r'[\\/](\d\d\d\d-\d\d-\d\d)$', dirName)
if not m:
basename = os.path.basename(dirName)
if Options.target:
m = re.search(r'(.+_\d\d\d\d-\d\d-\d\d)$', basename)
else:
m = re.search(r'.+_(\d\d\d\d-\d\d-\d\d)$', basename)
if m:
date = m.group(1)
verbose(f"date {date}")
if not date in exposures:
exposures[date] = {}
for f in FILTER:
if not f in exposures[date]:
exposures[date][f] = {}
for fname in fileList:
# Match extra pattern
if Options.match and not Options.match in fname:
continue
# Test for proper sub name ...
match = re.search(r'_(' + f + r')_(\d+)\.00s_', fname)
if not match:
match = re.search(r'_(' + f + r')_.+_(\d+)\.00s_', fname)
if not match:
# VdS "Piehler" style
match = re.search(r'_(' + f + r')_()\d{4}-\d{2}-\d{2}', fname)
if not match:
# OSC old naming without filter
# eg LIGHT_NGC 6744_2024-06-04_01-30-36__-10.00__G120_O30_300.00s_0000.fits
if f == FILTER[0]:
match = re.search(r'_()_.+_(\d+)\.00s_', fname)
if match:
verbose("\t" + fname)
time = match.group(2) if match.group(2) else EXPOSURE
verbose("\t" + date, f, time)
if not time:
error(f"time is none or zero for file {fname}")
if time in exposures[date][f]:
exposures[date][f][time] += 1
else:
exposures[date][f][time] = 1
if Options.csv:
csv_list(exposures)
elif Options.markdown:
md_table(exposures)
else:
print_filter_list(exposures)
def md_table(exp):
"""Output overview as markdown table"""
total = { f: {} for f in FILTER }
# Header
print(f"|Date|{"|".join(FILTER)}|")
print(f"|----|{"|".join(["-" * len(f) for f in FILTER])}|")
# Print dates and filter/exposures
for date in exp.keys():
print(f"|{date}|", end="")
for f in FILTER:
tdict = exp[date].get(f)
if tdict:
tlist = tdict.keys()
tstr = ", ".join([ "{}x {}s".format(tdict[t], t) for t in tlist ])
for t in tlist:
n = tdict[t]
if t in total[f]:
total[f][t] += n
else:
total[f][t] = n
else:
tstr = "-"
print(f"{tstr}|", end="")
print()
# Print totals
print(f"|Total|", end="")
for f in FILTER:
tdict = total.get(f)
if tdict:
tlist = tdict.keys()
tstr = ", ".join([ "{}x {}s".format(tdict[t], t) for t in tlist ])
else:
tstr = "-"
print(f"{tstr}|", end="")
print()
def print_filter_list(exp):
calibration_set = Options.calibration_set
total = {}
single_filter = len(FILTER) == 1
if calibration_set:
darks = {}
flats = {}
bias = config.get_calibration(calibration_set, "masterbias")
(secs, flatdarks) = config.get_calibration1(calibration_set, "masterflatdark")
for f in FILTER:
total[f] = {}
for date in exp.keys():
if not Options.total_only:
if single_filter:
print(date, end="")
else:
print(date)
for f in exp[date].keys():
if exp[date][f]:
if not Options.total_only:
print(f" {f}:", end="")
for time in exp[date][f].keys():
n = exp[date][f][time]
time = int(time)
if not Options.total_only:
print(f" {n}x {time}s", end="")
if calibration_set:
darks[str(time)+"s"] = config.get_calibration(calibration_set, "masterdark", str(time)+"s")
flats[f] = config.get_calibration(calibration_set, "masterflat", f)
if time in total[f]:
total[f][time] += n
else:
total[f][time] = n
if not Options.total_only:
print("")
print("Total")
for f in total.keys():
if total[f]:
print(f" {f}:", end="")
for time in total[f].keys():
n = total[f][time]
time = int(time)
print(f" {n}x {time}s", end="")
print()
total1 = 0
for f in total.keys():
if total[f]:
if not single_filter:
print(f" {f}:", end="")
for time in total[f].keys():
n = total[f][time]
time = int(time)
total1 += n * time
if not single_filter:
print(f" {n*time}s", end="")
if not single_filter:
print()
hours = int(total1 / 3600)
mins = int((total1 - hours*3600) / 60)
print(f" {total1}s / {hours:d}h{mins:02d}")
if calibration_set and not Options.no_calibration and not Options.total_only:
print("Darks")
for t, n in darks.items():
print(f" {n}x {t}", end="")
print()
print("Flats")
for f, n in flats.items():
print(f" {f}: {n}x", end="")
print()
if bias:
print(f"Bias\n {bias}x")
if flatdarks:
print(f"Flatdarks\n {flatdarks}x {secs}")
print("Settings")
for key in ("mode", "gain", "offset", "cooling"):
print(f" {key}: {extra(key)}", end="")
print()
def extra(key):
return config.get_setting(Options.calibration_set, key)
def csv_list(exp):
filter_set = Options.filter_set
calibration_set = Options.calibration_set
# CSV format as required by the Astrobin upload
# For documentation see https://welcome.astrobin.com/importing-acquisitions-from-csv/
#
# Fields:
# "date", "filter", "number", "duration", "binning", "gain", "sensorCooling", "fNumber",
# "darks", "flats", "flatDarks", "bias", "bortle", "meanSqm", "meanFwhm", "temperature"
#
# Not all fields must be present, BUT FIELDS MUST NOT BE EMPTY
for date in exp.keys():
for f in exp[date].keys():
for time in exp[date][f].keys():
n = exp[date][f][time]
time = int(time)
filter = config.get_filter_id(filter_set, f)
darks = config.get_calibration(calibration_set, "masterdark", str(time)+"s")
flats = config.get_calibration(calibration_set, "masterflat", f)
(secs, flatdarks) = config.get_calibration1(calibration_set, "masterflatdark")
if not flatdarks:
flatdarks = 0
bias = config.get_calibration(calibration_set, "masterbias")
# fields = [ date, filter, n, time,
# extra("binning"), extra("gain"), extra("cooling"), extra("fnumber"),
# darks, flats, 0, bias,
# extra("bortle"), extra("sqm"), extra("fwhm"), extra("temperature") ]
fields = [ date, filter, n, time,
extra("binning"), extra("gain"), extra("cooling"), extra("fnumber"),
darks, flats, flatdarks, bias,
extra("bortle") ]
verbose(",".join(map(str, fields)))
CSVOutput.add_row(fields)
if Options.csv:
CSVOutput.add_fields(ASTROBIN_FIELDS)
CSVOutput.write(Options.output, set_locale=False)
def main():
arg = argparse.ArgumentParser(
prog = NAME,
description = "Traverse directory and count N.I.N.A subs",
epilog = "Version: " + VERSION + " / " + AUTHOR)
arg.add_argument("-v", "--verbose", action="store_true", help="debug messages")
arg.add_argument("-d", "--debug", action="store_true", help="more debug messages")
arg.add_argument("-x", "--exclude", help="exclude filter, e.g. Ha,SII")
arg.add_argument("-f", "--filter", help="filter list, e.g. L,R,G,B")
arg.add_argument("-t", "--exposure-time", help="exposure time (sec) if not present in filename")
arg.add_argument("-C", "--csv", action="store_true", help="output CSV list for Astrobin")
arg.add_argument("-o", "--output", help="write CSV to file OUTPUT (default: stdout)")
arg.add_argument("-F", "--filter-set", help="name of filter set for Astrobin CSV (see config)")
arg.add_argument("--calibration-set", help="name of calibration set (see config)")
arg.add_argument("-m", "--match", help="filename must contain MATCH")
arg.add_argument("--target", action="store_true", help="include target name in date stats")
arg.add_argument("-T", "--total-only", action="store_true", help="list total only")
arg.add_argument("-n", "--no-calibration", action="store_true", help="don't list calibration data")
arg.add_argument("-M", "--markdown", action="store_true", help="output markdown table")
arg.add_argument("dirname", help="directory name")
args = arg.parse_args()
verbose.set_prog(NAME)
if args.verbose:
verbose.enable()
if args.debug:
ic.enable()
global FILTER, EXPOSURE
EXPOSURE = args.exposure_time
if args.exclude:
exclude = args.exclude.split(",")
filter1 = [x for x in FILTER if x not in exclude]
FILTER = filter1
if args.filter:
FILTER = args.filter.split(",")
verbose("filter =", FILTER)
Options.csv = args.csv
Options.output = args.output
Options.filter_set = args.filter_set
Options.calibration_set = args.calibration_set
Options.match = args.match
Options.total_only = args.total_only
Options.no_calibration = args.no_calibration
Options.markdown = args.markdown
Options.target = args.target
if Options.csv and Options.target:
error("can't use both --csv and --target")
# quick hack: Windows PowerShell adds a stray " to the end of dirname if it ends with a backslash \ AND contains a space!!!
# see here https://bugs.python.org/issue39845
walk_the_dir(args.dirname.rstrip("\""))
if __name__ == "__main__":
main()