-
Notifications
You must be signed in to change notification settings - Fork 0
/
rename-xffts.py
66 lines (52 loc) · 1.4 KB
/
rename-xffts.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
# standard library
import sys
from pathlib import Path
# dependencies
import numpy as np
# constants
DOT = "."
DTYPES = [
("time", "a28"),
("reserved1", "a4"),
("obsnum", "i8"),
("scantype", "a8"),
("scanmode", "a8"),
("chopperpos", "a8"),
("scancount", "i8"),
("speccount", "i8"),
("integtime", "i4"),
("reserved2", "a172"),
("array", ("f4", 32768)),
]
OBSNUM = "obsnum"
# helper functions
def get_obsnum(xffts: Path, /) -> int:
"""Read the observation number from an XFFTS log."""
with open(xffts, "rb") as f:
try:
log = np.frombuffer(f.read(), dtype=DTYPES)
except ValueError:
return 0
try:
obsnum = int(log[OBSNUM][0])
except IndexError:
return 0
if 0 < obsnum < 1000000:
return obsnum
else:
return 0
def rename(xffts: Path, /) -> Path:
"""Rename an XFFTS log to include its observation number."""
if OBSNUM in xffts.name:
return xffts
parts = xffts.name.split(DOT)
parts.insert(1, f"{OBSNUM}{get_obsnum(xffts):0>6}")
return xffts.rename(xffts.with_name(DOT.join(parts)))
# main part
if __name__ == "__main__":
try:
path = Path(sys.argv[1]).resolve()
except IndexError:
path = Path().resolve()
for xffts in list(path.glob("**/xffts*.*")):
print(f"{xffts} -> {rename(xffts)}")