-
Notifications
You must be signed in to change notification settings - Fork 0
/
gen_antenna_data.py
293 lines (256 loc) · 9.65 KB
/
gen_antenna_data.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
import hashlib
import json
import sys
import argparse
import logging
import math
from collections import defaultdict, namedtuple
from pathlib import Path
from time import perf_counter
import concurrent.futures
import multiprocessing
import astropy.units as u
from astropy.coordinates import Angle
from astropy.table import Column, MaskedColumn, Table, vstack as table_vstack
from astropy.io import fits
import pandas as pd
from tqdm import tqdm
logger = logging.getLogger(__name__)
Coordinates = namedtuple("Coordinates", ["x", "y"])
Boundaries = namedtuple("Boundaries", ["left", "bottom", "right", "top"])
# import matplotlib.style as mplstyle
# mplstyle.use('fast')
class TqdmLoggingHandler(logging.Handler):
def __init__(self, level=logging.NOTSET):
super(self.__class__, self).__init__(level)
def emit(self, record):
try:
msg = self.format(record)
tqdm.write(msg, file=sys.stderr)
# self.flush()
except (KeyboardInterrupt, SystemExit):
raise
except Exception:
self.handleError(record)
class Benchmark:
def __init__(self, description=None, logger=None):
self._initial_time = perf_counter()
self._logger = logger if logger else print
self.description = "Did stuff" if description is None else description
def __enter__(self):
pass
def __exit__(self, type, value, traceback):
_end = perf_counter()
defaultdict(float)
self._logger(f"{self.description} in {_end - self._initial_time:.3f} seconds ")
def process_antenna_fits(antenna_path: Path, time_resolution=60) -> Table:
antenna_hdulist = fits.open(antenna_path, memmap=True)
try:
antenna_positions = antenna_hdulist["ANTPOSPF"]
except KeyError as error:
try:
antenna_positions = antenna_hdulist["ANTPOSGR"]
except KeyError:
logger.error(f"Failed to find ANTPOSPF or ANTPOSGR table in {antenna_path}")
return Table()
# Resolution is 0.1 seconds, so we read every 10 * 60 seconds to get the position every minute
if time_resolution != 0.1:
reduced = antenna_positions.data[:: math.floor(10 * time_resolution)]
else:
# In this special case, don't bother indexing
reduced = antenna_positions.data
table = Table(reduced)["DMJD", "RAJ2000", "DECJ2000", "MNT_AZ", "MNT_EL"]
if table:
table["RAJ2000"] = Angle(table["RAJ2000"], unit=u.degree)
table["DECJ2000"] = Angle(table["DECJ2000"], unit=u.degree)
table["Scan"] = antenna_hdulist["PRIMARY"].header["SCAN"]
return table
return Table()
def get_data_for_batch(
antenna_paths: list[Path], output_path: Path, time_resolution=60
):
table = table_vstack(
[
process_antenna_fits(path, time_resolution=time_resolution)
for path in antenna_paths
]
)
logger.debug(f"Writing to {output_path}")
table.to_pandas().to_parquet(output_path, engine="pyarrow")
return table
def gen_path_cache(session_root: Path):
total_sessions = 0
total_antenna_files = 0
session_to_antenna = {}
for session_path in tqdm(
list(session_root.iterdir()), unit="session", dynamic_ncols=True
):
if session_path.is_dir():
antenna_paths = [str(p) for p in (session_path / "Antenna").glob("*.fits")]
if antenna_paths:
session_to_antenna[str(session_path)] = antenna_paths
total_sessions += 1
total_antenna_files += len(antenna_paths)
path_cache = {
"session_to_antenna": session_to_antenna,
"total_sessions": total_sessions,
"total_antenna_files": total_antenna_files,
}
return path_cache
def stack(output_dir: Path, final_output_path: Path):
to_stack = []
output_files = list(output_dir.glob("*.parquet"))
for path in tqdm(output_files, unit="file"):
try:
df = pd.read_parquet(path)
except ValueError as error:
raise ValueError(f"Failed to read {path}") from error
to_stack.append(df)
logger.debug(f"Processed {path}")
stacked = pd.concat(to_stack)
print(
f"Writing {len(stacked)} antenna positions from {len(output_files)} "
f"files to {final_output_path}"
)
stacked.to_parquet(final_output_path, engine="pyarrow")
return stacked
def gen_antenna_data(
session_to_antenna: dict[str, list[str]], output_dir: Path, time_resolution: float
):
with concurrent.futures.ProcessPoolExecutor(
max_workers=multiprocessing.cpu_count() // 2,
) as executor:
future_to_session = {}
total_sessions = 0
total_antenna_files = 0
for session_path, antenna_paths in session_to_antenna.items():
output_path = output_dir / f"{Path(session_path).name}.parquet"
if not output_path.exists():
future = executor.submit(
get_data_for_batch,
antenna_paths=antenna_paths,
output_path=output_path,
time_resolution=time_resolution,
)
future_to_session[future] = (session_path, len(antenna_paths))
total_sessions += 1
total_antenna_files += len(antenna_paths)
else:
tqdm.write(f"Skipping {Path(session_path).name}; {output_path} exists")
logger.info(
f"Processing {total_antenna_files} across {total_sessions} sessions"
)
antenna_progress = tqdm(
total=total_antenna_files,
unit="file",
smoothing=0.3,
dynamic_ncols=True,
)
for future in concurrent.futures.as_completed(future_to_session):
session_path, num_antenna_files = future_to_session[future]
try:
future.result()
except Exception as error:
print(
f"Error in {session_path}: {error}",
file=sys.stderr,
flush=True,
)
finally:
tqdm.write(session_path)
antenna_progress.update(num_antenna_files)
# antenna_progress.set_description(Path(session_path).name)
def init_logging(level=logging.DEBUG):
root_logger = logging.getLogger()
root_logger.setLevel(level)
logger.setLevel(level)
console_handler = TqdmLoggingHandler()
console_handler.setLevel(level)
# See: https://docs.python.org/3/library/logging.html#logrecord-attributes
formatter = logging.Formatter("[%(asctime)s - %(levelname)s] %(message)s")
console_handler.setFormatter(formatter)
root_logger.addHandler(console_handler)
def parse_args():
parser = argparse.ArgumentParser()
parser.add_argument(
"session_roots",
type=Path,
nargs="+",
help="Path to session root",
)
parser.add_argument(
"final_output_path",
type=Path,
help="Path at which to save the final stacked Parquet file",
)
parser.add_argument(
"--output-dir",
type=Path,
help="Directory under which to save Antenna FITS data cache files",
default=Path("./session_antenna_fits"),
)
parser.add_argument(
"-R",
"--time-resolution",
type=float,
help="Interval in seconds at at which to sample antenna positions",
default=1,
)
parser.add_argument("-v", "--verbosity", type=int, choices=range(0, 4), default=1)
return parser.parse_args()
def md5(string):
hash_object = hashlib.md5(string.encode("utf8"))
return hash_object.hexdigest()
def main():
print("START")
args = parse_args()
if args.verbosity > 2:
loglevel = logging.DEBUG
elif args.verbosity == 2:
loglevel = logging.INFO
else:
loglevel = logging.WARNING
init_logging(loglevel)
args.output_dir.mkdir(parents=True, exist_ok=True)
session_dir_cache_path = Path("./session_root_cache")
session_dir_cache_path.mkdir(parents=True, exist_ok=True)
path_cache = {
"total_antenna_files": 0,
"total_sessions": 0,
"session_to_antenna": {},
}
session_root_progress = tqdm(
args.session_roots, unit="session_root", dynamic_ncols=True
)
for session_root in session_root_progress:
session_root_progress.set_description(f"{session_root}")
session_root_cache_path = session_dir_cache_path / Path(
f"{md5(str(session_root))}.json"
)
if session_root_cache_path.exists():
logger.info(f"Found {session_root_cache_path} cache")
with open(session_root_cache_path, encoding="utf-8") as file:
session_root_cache = json.load(file)
else:
session_root_cache = gen_path_cache(session_root)
with open(session_root_cache_path, "w", encoding="utf-8") as file:
json.dump(session_root_cache, file)
path_cache["total_sessions"] += session_root_cache["total_sessions"]
path_cache["total_antenna_files"] += session_root_cache["total_antenna_files"]
path_cache["session_to_antenna"].update(
session_root_cache["session_to_antenna"]
)
print(
f"Found {path_cache['total_antenna_files']} Antenna FITS files across "
f" {len(path_cache['session_to_antenna'])} sessions "
f"(from {len(args.session_roots)} session roots)"
)
gen_antenna_data(
path_cache["session_to_antenna"],
output_dir=args.output_dir,
time_resolution=args.time_resolution,
)
stack(args.output_dir, args.final_output_path)
if __name__ == "__main__":
with Benchmark("TOTAL"):
main()