-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun_flux_calibrations.sh
More file actions
executable file
·189 lines (163 loc) · 4.76 KB
/
run_flux_calibrations.sh
File metadata and controls
executable file
·189 lines (163 loc) · 4.76 KB
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
#!/usr/bin/env bash
set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
ROOT_DIR="$(pwd)"
PYTHON_SCRIPT="$SCRIPT_DIR/calibrate_flux.py"
BACKEND="alma"
DB_PATH=""
TABLE_NAME="flux_estimates"
ARRAY_MAP="0:280,1:220,2:150"
SMA_ALPHA="-0.7"
DRY_RUN=0
SUMMARY_FILE="pointing_calibration_summary.csv"
usage() {
cat <<USAGE
Usage: $(basename "$0") [options]
Loop over observation-number directories and run flux calibration in each one.
Expected ECSV path per obsnum:
{obsnum}/raw/ppt_commissioning_pointing_{obsnum}_citlali.ecsv
Options:
-r, --root DIR Root directory containing obsnum folders (default: current directory)
-p, --python-script PATH Path to calibrate_flux.py (default: ./calibrate_flux.py)
-b, --backend alma|sqlite Backend passed to calibrate_flux.py (default: alma)
-d, --db PATH Optional SQLite DB path (required if backend=sqlite)
-t, --table NAME SQLite table name (default: flux_estimates)
-m, --array-map MAP Array map, e.g. '0:280,1:220,2:150'
-s, --summary-file NAME Summary CSV filename written under --root (default: pointing_calibration_summary.csv)
--sma-spectral-index A Spectral index alpha for SMA fallback (default: -0.7)
-n, --dry-run Print commands without executing
-h, --help Show this help
USAGE
}
while [[ $# -gt 0 ]]; do
case "$1" in
-r|--root)
ROOT_DIR="$2"
shift 2
;;
-p|--python-script)
PYTHON_SCRIPT="$2"
shift 2
;;
-b|--backend)
BACKEND="$2"
shift 2
;;
-d|--db)
DB_PATH="$2"
shift 2
;;
-t|--table)
TABLE_NAME="$2"
shift 2
;;
-m|--array-map)
ARRAY_MAP="$2"
shift 2
;;
-s|--summary-file)
SUMMARY_FILE="$2"
shift 2
;;
--sma-spectral-index)
SMA_ALPHA="$2"
shift 2
;;
-n|--dry-run)
DRY_RUN=1
shift
;;
-h|--help)
usage
exit 0
;;
*)
echo "Unknown argument: $1" >&2
usage >&2
exit 2
;;
esac
done
if [[ "$BACKEND" != "alma" && "$BACKEND" != "sqlite" ]]; then
echo "Error: --backend must be 'alma' or 'sqlite'." >&2
exit 2
fi
if [[ "$BACKEND" == "sqlite" && -z "$DB_PATH" ]]; then
echo "Error: --db is required when --backend sqlite is used." >&2
exit 2
fi
if [[ ! -f "$PYTHON_SCRIPT" ]]; then
echo "Error: Python script not found: $PYTHON_SCRIPT" >&2
exit 2
fi
ROOT_DIR="$(cd "$ROOT_DIR" && pwd)"
if [[ -n "$DB_PATH" ]]; then
DB_PATH="$(cd "$(dirname "$DB_PATH")" && pwd)/$(basename "$DB_PATH")"
fi
shopt -s nullglob
processed=0
summary_inputs=()
for obs_dir in "$ROOT_DIR"/[0-9]*; do
[[ -d "$obs_dir" ]] || continue
obsnum="$(basename "$obs_dir")"
[[ "$obsnum" =~ ^[0-9]+$ ]] || continue
ecsv_rel="raw/ppt_commissioning_pointing_${obsnum}_citlali.ecsv"
ecsv_path="$obs_dir/$ecsv_rel"
if [[ ! -f "$ecsv_path" ]]; then
continue
fi
output_file="cal_ratios_${obsnum}.json"
cmd=(python "$PYTHON_SCRIPT" "$ecsv_rel")
if [[ -n "$DB_PATH" ]]; then
cmd+=("$DB_PATH")
fi
cmd+=(
--backend "$BACKEND"
--table "$TABLE_NAME"
--array-map "$ARRAY_MAP"
--sma-spectral-index "$SMA_ALPHA"
--output "$output_file"
)
echo "[obsnum=$obsnum] running calibration in $obs_dir"
if [[ $DRY_RUN -eq 1 ]]; then
(cd "$obs_dir" && printf ' DRY RUN: '; printf '%q ' "${cmd[@]}"; printf '\n')
else
(cd "$obs_dir" && "${cmd[@]}")
summary_inputs+=("$obs_dir/$output_file")
fi
processed=$((processed + 1))
done
if [[ $processed -eq 0 ]]; then
echo "No matching obsnum directories with expected ECSV files were found under: $ROOT_DIR" >&2
exit 1
fi
summary_path="$ROOT_DIR/$SUMMARY_FILE"
if [[ $DRY_RUN -eq 1 ]]; then
echo "DRY RUN: would write summary table to $summary_path"
elif [[ ${#summary_inputs[@]} -gt 0 ]]; then
python - "$summary_path" "${summary_inputs[@]}" <<'PY'
import csv
import json
import sys
from pathlib import Path
summary_path = Path(sys.argv[1])
input_paths = [Path(p) for p in sys.argv[2:]]
rows = []
for path in input_paths:
obsnum = path.parent.name
data = json.loads(path.read_text(encoding="utf-8"))
for item in data:
item = dict(item)
item["obsnum"] = obsnum
rows.append(item)
if not rows:
raise SystemExit("No calibration rows found to summarize.")
fieldnames = ["obsnum"] + [k for k in rows[0].keys() if k != "obsnum"]
with summary_path.open("w", newline="", encoding="utf-8") as handle:
writer = csv.DictWriter(handle, fieldnames=fieldnames)
writer.writeheader()
writer.writerows(rows)
PY
echo "Wrote summary table: $summary_path"
fi
echo "Finished. Processed $processed observation director$( [[ $processed -eq 1 ]] && echo 'y' || echo 'ies' )."