forked from tammojan/facet-calibration
-
Notifications
You must be signed in to change notification settings - Fork 0
/
backup_direction.py
76 lines (64 loc) · 2.3 KB
/
backup_direction.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
from __future__ import print_function
import numpy as np
import pyrap.tables as pt
import os
import sys
from multiprocessing import Pool
def backup_previous_direction(ms, source, backup=None, column="CORRECTED_DATA"):
"""
Create a backup of CORRECTED_DATA after running a direction. This data
could be entered into SUBTRACTED_DATA_ALL to get the previous state.
"""
print(ms)
print(source)
# Open the MS
if not os.path.exists(ms):
raise Exception('MS file not found')
ms_table = pt.table(ms)
# Read the column
if column not in ms_table.colnames():
raise Exception('Column name not found in the MS')
data = ms_table.getcol(column)
# Save the data
np.save("{}/direction_{}.npy".format(ms, source), data)
def restore_direction(ms, backup, column="SUBTRACTED_DATA_ALL"):
"""
Load a backup of CORRECTED_DATA after running a direction into
SUBTRACTED_DATA_ALL.
"""
print(ms)
print(backup)
# Read the data
data = np.load(backup)
# Open the MS
if not os.path.exists(ms):
raise Exception('MS file not found')
ms_table = pt.table(ms, readonly=False)
# Write the column
if column not in ms_table.colnames():
raise Exception('Column name not found in the MS')
ms_table.putcol(column, data)
def backup_previous_direction_p(mslist, source, backup=None,
column="CORRECTED_DATA", parallel=0):
if parallel != 0:
if parallel > 0:
pool = Pool(parallel)
else:
pool = Pool() # Use all the procesors available
for ms in mslist:
pool.apply_async(backup_previous_direction,
args=(ms, source,),
kwds={"backup": backup,
"column": column}
)
pool.close()
pool.join()
else:
for ms in mslist:
backup_previous_direction(ms, source, backup=backup,
column=column)
if __name__ == "__main__":
config = {}
execfile(sys.argv[1])
mslist = ["{name:s}_SB{b1:03d}-{b2:03d}.{res:s}.ms".format(name=NAME, res=RES, b1=b, b2=b+9) for b in BANDS]
backup_previous_direction_p(mslist, do_sources[-1], parallel=-1)