ankflag 3.0.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- ankflag/__init__.py +0 -0
- ankflag/data/__init__.py +0 -0
- ankflag/data/ank.yml +84 -0
- ankflag/scripts/__init__.py +0 -0
- ankflag/scripts/runank.py +337 -0
- ankflag/utils/__init__.py +0 -0
- ankflag/utils/ankcopy.py +183 -0
- ankflag/utils/ankdata.py +112 -0
- ankflag/utils/convertfits.py +384 -0
- ankflag/utils/inputs.py +109 -0
- ankflag/utils/plottingfns.py +127 -0
- ankflag/utils/print_badant.py +82 -0
- ankflag/utils/showank.py +71 -0
- ankflag/utils/udocker_utils.py +325 -0
- ankflag-3.0.0.dist-info/METADATA +167 -0
- ankflag-3.0.0.dist-info/RECORD +20 -0
- ankflag-3.0.0.dist-info/WHEEL +5 -0
- ankflag-3.0.0.dist-info/entry_points.txt +2 -0
- ankflag-3.0.0.dist-info/licenses/LICENSE +31 -0
- ankflag-3.0.0.dist-info/top_level.txt +1 -0
ankflag/__init__.py
ADDED
|
File without changes
|
ankflag/data/__init__.py
ADDED
|
File without changes
|
ankflag/data/ank.yml
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
# -----------------------------------------------------------------
|
|
2
|
+
# This file provides the control parameters for aNKflag
|
|
3
|
+
# -----------------------------------------------------------------
|
|
4
|
+
|
|
5
|
+
Ukey: "UU" # Keyword for u
|
|
6
|
+
Vkey: "VV" # Keyword for v
|
|
7
|
+
Wkey: "WW" # Keyword for w
|
|
8
|
+
UgridCal: 1 # Gridsize in u for calibrators
|
|
9
|
+
VgridCal: 1 # Gridsize in v for calibrators
|
|
10
|
+
UgridTar: 10 # Gridsize in u for Target
|
|
11
|
+
VgridTar: 10 # Gridsize in v for Target
|
|
12
|
+
Npols: 2 # Number of polarizations
|
|
13
|
+
N_ants: 30 # Maximum number of antennas
|
|
14
|
+
ScanFlMean: ['rms', 1.5, 1.5, 0.01] # [FLAGON, tolerance_mean, tolearnce_rms, min fraction (should be > 0.01)]]
|
|
15
|
+
# ONLY for 'baseline' mode
|
|
16
|
+
ScanBrkSec: 300.0 # Scan break time (minimum)
|
|
17
|
+
BlockPow: 0.8 # Power low for Block non-Gaussianity
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
# Actual flagging parameters
|
|
21
|
+
|
|
22
|
+
FlgCalCh0: # Applicable to single channel calibrators only
|
|
23
|
+
- {what: 'rec_ind', flagon: 'mean', stat: 'median', data: 'am', tol: 5.0, minfrac: 0.1}
|
|
24
|
+
- {what: 'rec_ind', flagon: 'mean', stat: 'median', data: 'am', tol: 2.0, minfrac: 0.1}
|
|
25
|
+
- {what: 'rec_ind', flagon: 'mean', stat: 'median', data: 'am', tol: 1.5, minfrac: 0.1}
|
|
26
|
+
- {what: 'rec_ind', flagon: 'mean', stat: 'median', data: 're', tol: 1.2, minfrac: 0.1}
|
|
27
|
+
- {what: 'rec_ind', flagon: 'mean', stat: 'median', data: 'im', tol: 1.2, minfrac: 0.1}
|
|
28
|
+
- {what: 'rec_ind', flagon: 'mean', stat: 'median', data: 'am', tol: 1.1, minfrac: 0.1}
|
|
29
|
+
- {what: 'rec_ind', flagon: 'mean', stat: 'median', data: 're', tol: 1.1, minfrac: 0.1}
|
|
30
|
+
- {what: 'rec_ind', flagon: 'mean', stat: 'median', data: 'im', tol: 1.1, minfrac: 0.1}
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
FlgCalBp: # Applicable to bandpass calibrators only
|
|
34
|
+
- {what: 'rec_ind', flagon: 'rms', stat: 'median', data: 'am', tol: 5.0, minfrac: 0.1, fitorder: 0}
|
|
35
|
+
- {what: 'chan_ind', flagon: 'rms', stat: 'median', data: 'am', tol: 5.0, minfrac: 0.1, fitorder: 0}
|
|
36
|
+
- {what: 'rec_ind', flagon: 'rms', stat: 'mean', data: 'am', tol: 5.0, minfrac: 0.1, fitorder: 0}
|
|
37
|
+
- {what: 'chan_ind', flagon: 'rms', stat: 'mean', data: 'am', tol: 5.0, minfrac: 0.1, fitorder: 0}
|
|
38
|
+
- {what: 'rec_ind', flagon: 'rms', stat: 'mean', data: 'am', tol: 2.0, minfrac: 0.1, fitorder: 0}
|
|
39
|
+
- {what: 'chan_ind', flagon: 'rms', stat: 'mean', data: 'am', tol: 2.0, minfrac: 0.1, fitorder: 0}
|
|
40
|
+
- {what: 'chan_ind', flagon: 'rms', stat: 'median', data: 'am', tol: 1.5, minfrac: 0.1, fitorder: 2}
|
|
41
|
+
- {what: 'chan_ind', flagon: 'rms', stat: 'median', data: 're', tol: 1.5, minfrac: 0.1, fitorder: 2}
|
|
42
|
+
- {what: 'chan_ind', flagon: 'rms', stat: 'median', data: 'im', tol: 1.5, minfrac: 0.1, fitorder: 2}
|
|
43
|
+
- {what: 'rec_ind', flagon: 'rms', stat: 'median', data: 'am', tol: 1.5, minfrac: 0.1, fitorder: 2}
|
|
44
|
+
- {what: 'rec_ind', flagon: 'rms', stat: 'median', data: 're', tol: 1.5, minfrac: 0.1, fitorder: 2}
|
|
45
|
+
- {what: 'rec_ind', flagon: 'rms', stat: 'median', data: 'im', tol: 1.5, minfrac: 0.1, fitorder: 2}
|
|
46
|
+
- {what: 'chan_ind', flagon: 'rms', stat: 'mean', data: 're', tol: 1.5, minfrac: 0.1, fitorder: 2}
|
|
47
|
+
- {what: 'chan_ind', flagon: 'rms', stat: 'mean', data: 'im', tol: 1.5, minfrac: 0.1, fitorder: 2}
|
|
48
|
+
- {what: 'rec_ind', flagon: 'rms', stat: 'mean', data: 're', tol: 1.5, minfrac: 0.1, fitorder: 2}
|
|
49
|
+
- {what: 'rec_ind', flagon: 'rms', stat: 'mean', data: 'im', tol: 1.5, minfrac: 0.1, fitorder: 2}
|
|
50
|
+
- {what: 'chan_ind', flagon: 'rms', stat: 'median', data: 'am', tol: 1.2, minfrac: 0.1, fitorder: 2}
|
|
51
|
+
- {what: 'chan_ind', flagon: 'rms', stat: 'median', data: 're', tol: 1.2, minfrac: 0.1, fitorder: 2}
|
|
52
|
+
- {what: 'chan_ind', flagon: 'rms', stat: 'median', data: 'im', tol: 1.2, minfrac: 0.1, fitorder: 2}
|
|
53
|
+
- {what: 'rec_ind', flagon: 'rms', stat: 'median', data: 'am', tol: 1.2, minfrac: 0.1, fitorder: 2}
|
|
54
|
+
- {what: 'rec_ind', flagon: 'rms', stat: 'median', data: 're', tol: 1.2, minfrac: 0.1, fitorder: 2}
|
|
55
|
+
- {what: 'rec_ind', flagon: 'rms', stat: 'median', data: 'im', tol: 1.2, minfrac: 0.1, fitorder: 2}
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
FlgNorm: # Applicable to calibrated target
|
|
59
|
+
- {what: 'vis_ind', flagon: 'mean', stat: 'mean', data: 'am', tol: 5.0, minfrac: 0.1, fitorder: 0}
|
|
60
|
+
- {what: 'vis_ind', flagon: 'mean', stat: 'mean', data: 'am', tol: 3.0, minfrac: 0.1, fitorder: 0}
|
|
61
|
+
- {what: 'vis_ind', flagon: 'mean', stat: 'median', data: 'am', tol: 2.0, minfrac: 0.1, fitorder: 1}
|
|
62
|
+
- {what: 'chan_ind', flagon: 'rms', stat: 'mean', data: 'am', tol: 2.0, minfrac: 0.1, fitorder: 0}
|
|
63
|
+
- {what: 'rec_ind', flagon: 'rms', stat: 'mean', data: 'am', tol: 2.0, minfrac: 0.1, fitorder: 0}
|
|
64
|
+
- {what: 'vis_ind', flagon: 'mean', stat: 'median', data: 'am', tol: 1.6, minfrac: 0.1, fitorder: 2}
|
|
65
|
+
- {what: 'chan_ind', flagon: 'rms', stat: 'mean', data: 'am', tol: 1.5, minfrac: 0.1, fitorder: 2}
|
|
66
|
+
- {what: 'rec_ind', flagon: 'rms', stat: 'mean', data: 'am', tol: 1.5, minfrac: 0.1, fitorder: 2}
|
|
67
|
+
- {what: 'chan_ind', flagon: 'rms', stat: 'median', data: 're', tol: 1.2, minfrac: 0.1, fitorder: 2}
|
|
68
|
+
- {what: 'chan_ind', flagon: 'rms', stat: 'median', data: 'im', tol: 1.2, minfrac: 0.1, fitorder: 2}
|
|
69
|
+
- {what: 'rec_ind', flagon: 'rms', stat: 'median', data: 're', tol: 1.2, minfrac: 0.1, fitorder: 2}
|
|
70
|
+
- {what: 'rec_ind', flagon: 'rms', stat: 'median', data: 'im', tol: 1.2, minfrac: 0.1, fitorder: 2}
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
FlgUvsub: # Applicable to UV-subtracted target
|
|
74
|
+
- {what: 'vis_ind', flagon: 'mean', stat: 'mean', data: 'am', tol: 5.0, minfrac: 0.1, fitorder: 0}
|
|
75
|
+
- {what: 'vis_ind', flagon: 'mean', stat: 'mean', data: 'am', tol: 3.0, minfrac: 0.1, fitorder: 0}
|
|
76
|
+
- {what: 'vis_ind', flagon: 'mean', stat: 'median', data: 'am', tol: 2.0, minfrac: 0.1, fitorder: 1}
|
|
77
|
+
- {what: 'chan_ind', flagon: 'rms', stat: 'mean', data: 'am', tol: 2.0, minfrac: 0.1, fitorder: 0}
|
|
78
|
+
- {what: 'rec_ind', flagon: 'rms', stat: 'mean', data: 'am', tol: 2.0, minfrac: 0.1, fitorder: 0}
|
|
79
|
+
- {what: 'chan_ind', flagon: 'rms', stat: 'mean', data: 'am', tol: 1.5, minfrac: 0.1, fitorder: 2}
|
|
80
|
+
- {what: 'chan_ind', flagon: 'rms', stat: 'median', data: 're', tol: 1.2, minfrac: 0.1, fitorder: 2}
|
|
81
|
+
- {what: 'chan_ind', flagon: 'rms', stat: 'median', data: 'im', tol: 1.2, minfrac: 0.1, fitorder: 2}
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
|
|
File without changes
|
|
@@ -0,0 +1,337 @@
|
|
|
1
|
+
import os, sys
|
|
2
|
+
import numpy as np
|
|
3
|
+
import time as tm
|
|
4
|
+
import argparse as ap
|
|
5
|
+
import ankflag.data as ad
|
|
6
|
+
from pyfiglet import Figlet
|
|
7
|
+
from ankflag.utils.convertfits import *
|
|
8
|
+
from ankflag.utils.inputs import *
|
|
9
|
+
from ankflag.utils.plottingfns import *
|
|
10
|
+
from ankflag.utils.ankdata import init_ankflag_data
|
|
11
|
+
from ankflag.utils.udocker_utils import initialize_ankflag_container, run_ankflag
|
|
12
|
+
|
|
13
|
+
# This script is written by Apurba bera
|
|
14
|
+
# Formatted and udocker contanierisation is done by Devojyoti Kansabanik
|
|
15
|
+
|
|
16
|
+
def cli():
|
|
17
|
+
parser = ap.ArgumentParser(
|
|
18
|
+
description="aNKflag command line interface"
|
|
19
|
+
)
|
|
20
|
+
subparsers = parser.add_subparsers(
|
|
21
|
+
dest="command",
|
|
22
|
+
required=True,
|
|
23
|
+
)
|
|
24
|
+
# =====================
|
|
25
|
+
# init command
|
|
26
|
+
# =====================
|
|
27
|
+
init_parser = subparsers.add_parser(
|
|
28
|
+
"init",
|
|
29
|
+
help="Initialize aNKflag configuration"
|
|
30
|
+
)
|
|
31
|
+
init_parser.add_argument(
|
|
32
|
+
"--datadir",
|
|
33
|
+
help="Directory to save aNKflag container",
|
|
34
|
+
type=str,
|
|
35
|
+
default="",
|
|
36
|
+
)
|
|
37
|
+
|
|
38
|
+
# =====================
|
|
39
|
+
# run command
|
|
40
|
+
# =====================
|
|
41
|
+
run_parser = subparsers.add_parser(
|
|
42
|
+
"run",
|
|
43
|
+
help="Run aNKflag"
|
|
44
|
+
)
|
|
45
|
+
|
|
46
|
+
# Essential parameters
|
|
47
|
+
basic_args = run_parser.add_argument_group(
|
|
48
|
+
"###################\nEssential parameters\n###################"
|
|
49
|
+
)
|
|
50
|
+
basic_args.add_argument(
|
|
51
|
+
"infilename",
|
|
52
|
+
help="Input FITS file",
|
|
53
|
+
type=str,
|
|
54
|
+
)
|
|
55
|
+
basic_args.add_argument(
|
|
56
|
+
"outfilename",
|
|
57
|
+
help="Output FITS file",
|
|
58
|
+
type=str,
|
|
59
|
+
)
|
|
60
|
+
basic_args.add_argument(
|
|
61
|
+
"--scratchdir",
|
|
62
|
+
help="Scratch directory",
|
|
63
|
+
type=str,
|
|
64
|
+
default=None,
|
|
65
|
+
)
|
|
66
|
+
basic_args.add_argument(
|
|
67
|
+
"--npol",
|
|
68
|
+
help="Number of polarisation",
|
|
69
|
+
type=int,
|
|
70
|
+
default=2,
|
|
71
|
+
)
|
|
72
|
+
basic_args.add_argument(
|
|
73
|
+
"--nant",
|
|
74
|
+
help="Number of antennas",
|
|
75
|
+
type=int,
|
|
76
|
+
default=30,
|
|
77
|
+
)
|
|
78
|
+
|
|
79
|
+
# Flagging control parameters
|
|
80
|
+
flag_args = run_parser.add_argument_group(
|
|
81
|
+
"###################\nFlagging control parameters\n###################"
|
|
82
|
+
)
|
|
83
|
+
flag_args.add_argument(
|
|
84
|
+
"--flagmode",
|
|
85
|
+
help="Mode of flagging (baseline, uvbin)",
|
|
86
|
+
type=str,
|
|
87
|
+
default=None,
|
|
88
|
+
)
|
|
89
|
+
flag_args.add_argument(
|
|
90
|
+
"--target_type",
|
|
91
|
+
help="Target source type (calch0, calbp, normal, uvsub)",
|
|
92
|
+
type=str,
|
|
93
|
+
default=None,
|
|
94
|
+
)
|
|
95
|
+
flag_args.add_argument(
|
|
96
|
+
"--rfifreqfile",
|
|
97
|
+
help="File with RFI frequencies",
|
|
98
|
+
type=str,
|
|
99
|
+
default=None,
|
|
100
|
+
)
|
|
101
|
+
flag_args.add_argument(
|
|
102
|
+
"--flagautocorr",
|
|
103
|
+
help="Flag autocorrelations",
|
|
104
|
+
action="store_true",
|
|
105
|
+
)
|
|
106
|
+
|
|
107
|
+
|
|
108
|
+
# Advanced parameters
|
|
109
|
+
adv_args = run_parser.add_argument_group(
|
|
110
|
+
"###################\nAdvanced control parameters\n###################"
|
|
111
|
+
)
|
|
112
|
+
adv_args.add_argument(
|
|
113
|
+
"--parfile",
|
|
114
|
+
help="Custom YAML file with aNKflag specific input params",
|
|
115
|
+
type=str,
|
|
116
|
+
default=None,
|
|
117
|
+
)
|
|
118
|
+
adv_args.add_argument(
|
|
119
|
+
"--verbose",
|
|
120
|
+
help="Verbose output",
|
|
121
|
+
action="store_true",
|
|
122
|
+
)
|
|
123
|
+
adv_args.add_argument(
|
|
124
|
+
"--logfile",
|
|
125
|
+
help="Name of logfile",
|
|
126
|
+
type=str,
|
|
127
|
+
default="logfile.txt",
|
|
128
|
+
)
|
|
129
|
+
adv_args.add_argument(
|
|
130
|
+
"--nthreads",
|
|
131
|
+
help="Number of threads",
|
|
132
|
+
type=int,
|
|
133
|
+
default=1,
|
|
134
|
+
)
|
|
135
|
+
adv_args.add_argument(
|
|
136
|
+
"--clearscratch",
|
|
137
|
+
help="Clear scratch directory?",
|
|
138
|
+
action="store_true",
|
|
139
|
+
)
|
|
140
|
+
|
|
141
|
+
if len(sys.argv) == 1:
|
|
142
|
+
parser.print_help(sys.stderr)
|
|
143
|
+
return 1
|
|
144
|
+
|
|
145
|
+
args = parser.parse_args()
|
|
146
|
+
f = Figlet(font="big")
|
|
147
|
+
print(f.renderText("aNKflag"))
|
|
148
|
+
|
|
149
|
+
if args.command == "init":
|
|
150
|
+
print("Running initialization of aNKflag...")
|
|
151
|
+
init_ankflag_data(datadir=args.datadir,update=False)
|
|
152
|
+
initialize_ankflag_container(name="ankflag", update=True)
|
|
153
|
+
return 0
|
|
154
|
+
elif args.command == "run":
|
|
155
|
+
print(f"Running aNKflag on: {args.infilename}")
|
|
156
|
+
# --------------------------------------
|
|
157
|
+
# Read and verify input parameters
|
|
158
|
+
# --------------------------------------
|
|
159
|
+
if args.parfile == None:
|
|
160
|
+
datadir = os.path.dirname(ad.__file__)
|
|
161
|
+
parfile = f"{datadir}/ank.yml"
|
|
162
|
+
print(f"Using default input YAML file: {parfile}")
|
|
163
|
+
else:
|
|
164
|
+
parfile = args.parfile
|
|
165
|
+
print(f"Using custom input YAML file: {parfile}")
|
|
166
|
+
flagparams = getparsyaml(parfile, showparams=False)
|
|
167
|
+
|
|
168
|
+
if args.scratchdir == None:
|
|
169
|
+
scratchdir = f"{os.getcwd()}/ankscratch/"
|
|
170
|
+
else:
|
|
171
|
+
scratchdir = f"{args.scratchdir.rstrip('/')}/"
|
|
172
|
+
os.makedirs(scratchdir, exist_ok=True)
|
|
173
|
+
if args.clearscratch:
|
|
174
|
+
print("Clearing scratch directory....")
|
|
175
|
+
os.system(f"rm -rf {scratchdir}/*")
|
|
176
|
+
|
|
177
|
+
if os.path.exists(args.infilename):
|
|
178
|
+
infilefits = args.infilename
|
|
179
|
+
else:
|
|
180
|
+
print("Provide a real file")
|
|
181
|
+
return 1
|
|
182
|
+
|
|
183
|
+
if args.outfilename == None:
|
|
184
|
+
print("Provide a filename")
|
|
185
|
+
return 1
|
|
186
|
+
else:
|
|
187
|
+
outfilefits = args.outfilename
|
|
188
|
+
|
|
189
|
+
if not (args.flagmode == "uvbin" or args.flagmode == "baseline"):
|
|
190
|
+
print("Provide from these two flagging mode: uvbin or baseline")
|
|
191
|
+
else:
|
|
192
|
+
print(f"Flagging in {args.flagmode} mode.")
|
|
193
|
+
|
|
194
|
+
if args.npol!=2 and args.npol!=4:
|
|
195
|
+
print(f"Number of polarisations should be either 2 or 4, not {args.npol}.")
|
|
196
|
+
return 1
|
|
197
|
+
|
|
198
|
+
if args.target_type in tartypes:
|
|
199
|
+
print("Target type: ", args.target_type)
|
|
200
|
+
partype = partypes[tartypes.index(args.target_type)]
|
|
201
|
+
if partype in flagparams:
|
|
202
|
+
print("Using parameters: ", partype)
|
|
203
|
+
flagparams["Npols"]=args.npol
|
|
204
|
+
flagparams["N_ants"]=args.nant
|
|
205
|
+
if args.verbose:
|
|
206
|
+
print("Flagging commands: ")
|
|
207
|
+
print(*flagparams[partype], sep="\n")
|
|
208
|
+
else:
|
|
209
|
+
print(partype, "is not provided...")
|
|
210
|
+
return 1
|
|
211
|
+
else:
|
|
212
|
+
print("Only supported target types are ", *tartypes, sep=" ")
|
|
213
|
+
return 1
|
|
214
|
+
|
|
215
|
+
if (args.target_type == "calch0") or (args.target_type == "calbp"):
|
|
216
|
+
uugrids = flagparams["UgridCal"]
|
|
217
|
+
vvgrids = flagparams["VgridCal"]
|
|
218
|
+
else:
|
|
219
|
+
uugrids = flagparams["UgridTar"]
|
|
220
|
+
vvgrids = flagparams["VgridTar"]
|
|
221
|
+
|
|
222
|
+
# ------------------------------------------
|
|
223
|
+
# Convert parameters to aNKflag secret codes
|
|
224
|
+
# ------------------------------------------
|
|
225
|
+
covertparams(
|
|
226
|
+
flagparams,
|
|
227
|
+
partype,
|
|
228
|
+
scratchdir + "/flagpars.pars",
|
|
229
|
+
args,
|
|
230
|
+
uugrids=uugrids,
|
|
231
|
+
vvgrids=vvgrids,
|
|
232
|
+
)
|
|
233
|
+
|
|
234
|
+
# ------------------------------------------------
|
|
235
|
+
# Convert FITS to aNKflag compatible binary files
|
|
236
|
+
# ------------------------------------------------
|
|
237
|
+
|
|
238
|
+
start0 = tm.time()
|
|
239
|
+
|
|
240
|
+
if args.flagautocorr:
|
|
241
|
+
# Preflag autocorrelations
|
|
242
|
+
infile = fits.open(infilefits)
|
|
243
|
+
data = infile[0].data
|
|
244
|
+
nauto = flagautocorr(flagparams["N_ants"], data)
|
|
245
|
+
infile.writeto(
|
|
246
|
+
scratchdir + "/scratchfits.fits", output_verify="warn", overwrite=True
|
|
247
|
+
)
|
|
248
|
+
infile.close()
|
|
249
|
+
|
|
250
|
+
if ConvertFITS:
|
|
251
|
+
if args.flagautocorr:
|
|
252
|
+
infile = fits.open(scratchdir + "/scratchfits.fits")
|
|
253
|
+
else:
|
|
254
|
+
infile = fits.open(infilefits)
|
|
255
|
+
data = infile[0].data
|
|
256
|
+
|
|
257
|
+
if (args.rfifreqfile != None) and os.path.exists(args.rfifreqfile):
|
|
258
|
+
# ----------- Flagging hopeless channels ---------------------------
|
|
259
|
+
print("Identifying bad channels from ", args.rfifreqfile)
|
|
260
|
+
else:
|
|
261
|
+
print("No bad channel list found... continuing...")
|
|
262
|
+
|
|
263
|
+
if args.flagmode == "uvbin":
|
|
264
|
+
uvfitstobinary(
|
|
265
|
+
data,
|
|
266
|
+
scratchdir,
|
|
267
|
+
uugrids,
|
|
268
|
+
vvgrids,
|
|
269
|
+
flagparams,
|
|
270
|
+
verbose=args.verbose,
|
|
271
|
+
)
|
|
272
|
+
|
|
273
|
+
elif args.flagmode == "baseline":
|
|
274
|
+
baselinestobinary(
|
|
275
|
+
flagparams["N_ants"],
|
|
276
|
+
data,
|
|
277
|
+
scratchdir,
|
|
278
|
+
flagparams["ScanBrkSec"],
|
|
279
|
+
flagparams,
|
|
280
|
+
verbose=args.verbose,
|
|
281
|
+
)
|
|
282
|
+
else:
|
|
283
|
+
print("Unknown flagging mode !!! ........")
|
|
284
|
+
|
|
285
|
+
infile.close()
|
|
286
|
+
print("Convertion done in %d seconds" % (tm.time() - start0))
|
|
287
|
+
|
|
288
|
+
# ----------------------------
|
|
289
|
+
# Actual flagging is done here
|
|
290
|
+
# ----------------------------
|
|
291
|
+
|
|
292
|
+
start1 = tm.time()
|
|
293
|
+
|
|
294
|
+
if DoFlag:
|
|
295
|
+
status = run_ankflag(
|
|
296
|
+
scratchdir,
|
|
297
|
+
args.logfile,
|
|
298
|
+
args.npol,
|
|
299
|
+
container_name="ankflag",
|
|
300
|
+
check_container=False,
|
|
301
|
+
verbose=args.verbose,
|
|
302
|
+
)
|
|
303
|
+
print("Flagging done in %d seconds" % (tm.time() - start1))
|
|
304
|
+
|
|
305
|
+
# ---------------------------------
|
|
306
|
+
# Convert back binary files to FITS
|
|
307
|
+
# ---------------------------------
|
|
308
|
+
if ReadBack:
|
|
309
|
+
if args.flagautocorr:
|
|
310
|
+
infile2 = fits.open(scratchdir + "/scratchfits.fits")
|
|
311
|
+
else:
|
|
312
|
+
infile2 = fits.open(infilefits)
|
|
313
|
+
data2 = infile2[0].data
|
|
314
|
+
|
|
315
|
+
if args.flagmode == "uvbin":
|
|
316
|
+
bintofits = uvfitsfrombinary(
|
|
317
|
+
data2, scratchdir, uugrids, vvgrids, flagparams
|
|
318
|
+
)
|
|
319
|
+
|
|
320
|
+
elif args.flagmode == "baseline":
|
|
321
|
+
baselinesfrombinary(
|
|
322
|
+
flagparams["N_ants"], data2, scratchdir, flagparams, verbose=args.verbose,
|
|
323
|
+
)
|
|
324
|
+
|
|
325
|
+
# Write the output FITS --------------
|
|
326
|
+
if WriteOut:
|
|
327
|
+
print("Writing output FITS...")
|
|
328
|
+
infile2.writeto(outfilefits, output_verify="warn", overwrite=True)
|
|
329
|
+
|
|
330
|
+
infile2.close()
|
|
331
|
+
print("Everything done in %d seconds" % (tm.time() - start0))
|
|
332
|
+
|
|
333
|
+
if args.clearscratch:
|
|
334
|
+
print("Clearing scratch directory....")
|
|
335
|
+
os.system(f"rm -rf {scratchdir}/*")
|
|
336
|
+
return 0
|
|
337
|
+
|
|
File without changes
|
ankflag/utils/ankcopy.py
ADDED
|
@@ -0,0 +1,183 @@
|
|
|
1
|
+
import numpy as np
|
|
2
|
+
from astropy.io import fits
|
|
3
|
+
import matplotlib.pyplot as plt
|
|
4
|
+
import multiprocessing as mp
|
|
5
|
+
import time as tm
|
|
6
|
+
|
|
7
|
+
ANTS = 30 # Total number of antennas
|
|
8
|
+
parallel = False # Process baselines parallelly # SET TO "FALSE"
|
|
9
|
+
recflag = False # Copy flags for whole records
|
|
10
|
+
chanflag = False # Copy flags for whole channels
|
|
11
|
+
visflag = True # Copy flags for individual visibilities
|
|
12
|
+
writeout = True # Write the output file
|
|
13
|
+
|
|
14
|
+
srcname = '/media/devojyoti/Data1/aurora_test/workdir/target_scan2.fits' # Input FITS file (FG TABLE IS NOT READ!)
|
|
15
|
+
targetname = '/media/devojyoti/Data1/aurora_test/workdir/testankflag_out.fits' # Target FITS file
|
|
16
|
+
outname = '/media/devojyoti/Data1/aurora_test/workdir/target_scan2_out.fits' # Output FITS file
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
# -------------------------------------------------------------------- Functions ----------------------------------------------------
|
|
20
|
+
# Function to copy flags from one dataset to another for a baseline
|
|
21
|
+
def copybase (bl,datasrc,datatar):
|
|
22
|
+
print('Baseline %d-%d'%(bl[0],bl[1]))
|
|
23
|
+
bindxsrc = np.where(datasrc.par('BASELINE')==bl[2])
|
|
24
|
+
bdatasrc = datasrc[bindxsrc[0]]
|
|
25
|
+
bindxtar = np.where(datatar.par('BASELINE')==bl[2])
|
|
26
|
+
bdatatar = datatar[bindxtar[0]]
|
|
27
|
+
|
|
28
|
+
if(len(bdatasrc)==0):
|
|
29
|
+
print('No data in source file .... flagging entire baseline in target file')
|
|
30
|
+
if(len(bdatatar)==0):
|
|
31
|
+
print('No data in target file either.... nothing to do')
|
|
32
|
+
return [2.0,[[1.0,1.0,1.0],[1.0,1.0,1.0]],0]
|
|
33
|
+
for p in range (0,2):
|
|
34
|
+
tfdatatar = bldatatar[:,0,0,0,:,p,:] # Time frequency data target
|
|
35
|
+
tfretar = tfdatatar[:,:,0] # Real part of target
|
|
36
|
+
tfimtar = tfdatatar[:,:,1] # Imaginary Part of target
|
|
37
|
+
tfwtar = tfdatatar[:,:,2] # Weight of target
|
|
38
|
+
tfwtar = 0.0
|
|
39
|
+
tfwre = 0.0
|
|
40
|
+
tfwim = 0.0
|
|
41
|
+
return [1.0,[[1.0,1.0,1.0],[1.0,1.0,1.0]],0]
|
|
42
|
+
bldatasrc = bdatasrc.data
|
|
43
|
+
bldatatar = bdatatar.data
|
|
44
|
+
flgfrac = []
|
|
45
|
+
for p in range (0,2):
|
|
46
|
+
tfdatasrc = bldatasrc[:,0,0,0,:,p,:] # Time frequency data source
|
|
47
|
+
tfdatatar = bldatatar[:,0,0,0,:,p,:] # Time frequency data target
|
|
48
|
+
tfresrc = tfdatasrc[:,:,0] # Real part of source
|
|
49
|
+
tfretar = tfdatatar[:,:,0] # Real part of target
|
|
50
|
+
tfimsrc = tfdatasrc[:,:,1] # Imaginary Part of source
|
|
51
|
+
tfimtar = tfdatatar[:,:,1] # Imaginary Part of target
|
|
52
|
+
tfwsrc = tfdatasrc[:,:,2] # Weight of source
|
|
53
|
+
tfwtar = tfdatatar[:,:,2] # Weight of target
|
|
54
|
+
|
|
55
|
+
if(np.size(tfwsrc)!=np.size(tfwtar)):
|
|
56
|
+
print('Caution ! Sizes do not match src = %d target = %d'%(np.size(tfwsrc),np.size(tfwtar)))
|
|
57
|
+
|
|
58
|
+
srcfrac = 1.0-float(np.count_nonzero(tfwsrc>0.0))/np.size(tfwsrc)
|
|
59
|
+
tarfrac = 1.0-float(np.count_nonzero(tfwtar>0.0))/np.size(tfwtar)
|
|
60
|
+
print('Pol %d flagged fractions src = %.2f target = %.2f'%(p,srcfrac,tarfrac))
|
|
61
|
+
|
|
62
|
+
if(recflag):
|
|
63
|
+
#print('Copyting record flags ...........')
|
|
64
|
+
nrecs = len(tfwsrc)
|
|
65
|
+
nrect = len(tfwtar)
|
|
66
|
+
if(nrecs!=nrect):
|
|
67
|
+
print('Error ! Records do not match !!!!')
|
|
68
|
+
return 3
|
|
69
|
+
for r in range(0,nrecs):
|
|
70
|
+
if(np.count_nonzero(tfwsrc[r])==0):
|
|
71
|
+
tfwtar[r] = 0.0
|
|
72
|
+
tfretar[r] = 0.0
|
|
73
|
+
tfimtar[r] = 0.0
|
|
74
|
+
|
|
75
|
+
if(chanflag):
|
|
76
|
+
#print('Copyting channel flags ...........')
|
|
77
|
+
nchans = len(tfwsrc[0])
|
|
78
|
+
nchant = len(tfwtar[0])
|
|
79
|
+
if(nchans!=nchant):
|
|
80
|
+
print('Error ! Channels do not match !!!!')
|
|
81
|
+
return 3
|
|
82
|
+
for c in range(0,nchans):
|
|
83
|
+
if(np.count_nonzero(tfwsrc[:,c])==0):
|
|
84
|
+
tfwtar[:,c] = 0.0
|
|
85
|
+
tfretar[:,c] = 0.0
|
|
86
|
+
tfimtar[:,c] = 0.0
|
|
87
|
+
|
|
88
|
+
if(visflag):
|
|
89
|
+
#print('Copyting visibilities flags ...........')
|
|
90
|
+
nrecs = len(tfwsrc)
|
|
91
|
+
nrect = len(tfwtar)
|
|
92
|
+
nchans = len(tfwsrc[0])
|
|
93
|
+
nchant = len(tfwtar[0])
|
|
94
|
+
if((nrecs!=nrect) or (nchans!=nchant)):
|
|
95
|
+
print('Error ! Visibilities do not match !!!!')
|
|
96
|
+
return 3
|
|
97
|
+
for r in range(0,nrecs):
|
|
98
|
+
for c in range(0,nchans):
|
|
99
|
+
if(tfwsrc[r,c]<=0.0):
|
|
100
|
+
tfwtar[r,c] = 0.0
|
|
101
|
+
tfretar[r,c] = 0.0
|
|
102
|
+
tfimtar[r,c] = 0.0
|
|
103
|
+
|
|
104
|
+
tarfracf = 1.0-float(np.count_nonzero(tfwtar>0.0))/np.size(tfwtar)
|
|
105
|
+
print(' target = %.2f'%(tarfracf))
|
|
106
|
+
flgfrac.append([srcfrac,tarfrac,tarfracf])
|
|
107
|
+
return [0.0,flgfrac,bldatatar,bindxsrc[0],bindxtar[0],bl]
|
|
108
|
+
# ---------------------------------------------------------------------------------------------
|
|
109
|
+
|
|
110
|
+
|
|
111
|
+
|
|
112
|
+
|
|
113
|
+
# --------------------------------------------------- Main programme -----------------------------------------------------------------
|
|
114
|
+
|
|
115
|
+
b_start = tm.time()
|
|
116
|
+
|
|
117
|
+
srcfile = fits.open(srcname)
|
|
118
|
+
targetfile = fits.open(targetname)
|
|
119
|
+
|
|
120
|
+
datasrc = srcfile[0].data
|
|
121
|
+
nrecsrc = len(datasrc)
|
|
122
|
+
datatar = targetfile[0].data
|
|
123
|
+
nrectar = len(datatar)
|
|
124
|
+
|
|
125
|
+
blid = []
|
|
126
|
+
for a in range (1,ANTS):
|
|
127
|
+
for b in range (a+1,ANTS+1):
|
|
128
|
+
blid.append([a,b,256*a+b])
|
|
129
|
+
blid = np.array(blid)
|
|
130
|
+
nbase = len(blid)
|
|
131
|
+
|
|
132
|
+
print('\nIdeally total baselines = %d'%nbase)
|
|
133
|
+
print('Source records = %d \nTarget records = %d\n'%(nrecsrc,nrectar))
|
|
134
|
+
print('\nCopying flags for each baseline\n')
|
|
135
|
+
|
|
136
|
+
if(parallel):
|
|
137
|
+
# parallel
|
|
138
|
+
print('Sorry ! Parallel execution is not yet supported ...........')
|
|
139
|
+
else:
|
|
140
|
+
# Serial
|
|
141
|
+
blstat = []
|
|
142
|
+
for i in range (0,nbase):
|
|
143
|
+
bls = copybase(blid[i],datasrc,datatar)
|
|
144
|
+
blstat.append(bls)
|
|
145
|
+
|
|
146
|
+
# Copying baseline
|
|
147
|
+
|
|
148
|
+
print('\nCopying baselines\n')
|
|
149
|
+
for i in range (0,nbase):
|
|
150
|
+
if(blstat[i][0]<1.5):
|
|
151
|
+
bldata = blstat[i][2]
|
|
152
|
+
lenbase = len(bldata)
|
|
153
|
+
if(lenbase!=len(blstat[i][4])):
|
|
154
|
+
print('Lengths do not match !!!!')
|
|
155
|
+
for k in range (0,lenbase):
|
|
156
|
+
if(datatar[blstat[i][4][k]].par('BASELINE')!=blstat[i][5][2]):
|
|
157
|
+
print('Baselines do not match !!!! %d %d %d'%(k,datatar[blstat[i][4][k]].par('BASELINE'),blstat[i][5][2]))
|
|
158
|
+
np.copyto(datatar[blstat[i][4][k]].data,bldata[k])
|
|
159
|
+
print('Baseline %d-%d Copied'%(blid[i][0],blid[i][1]))
|
|
160
|
+
else:
|
|
161
|
+
print('Baseline %d-%d No data'%(blid[i][0],blid[i][1]))
|
|
162
|
+
|
|
163
|
+
flstat = []
|
|
164
|
+
for i in range (0,nbase):
|
|
165
|
+
flstat.append(blstat[i][1])
|
|
166
|
+
flstat = np.array(flstat)
|
|
167
|
+
srcf = [np.mean(flstat[:,0,0]),np.mean(flstat[:,1,0])]
|
|
168
|
+
tarfb = [np.mean(flstat[:,0,1]),np.mean(flstat[:,1,1])]
|
|
169
|
+
tarfa = [np.mean(flstat[:,0,2]),np.mean(flstat[:,1,2])]
|
|
170
|
+
|
|
171
|
+
print('\nSource %.2f %.2f'%(srcf[0],srcf[1]))
|
|
172
|
+
print('\nTarget before %.2f %.2f'%(tarfb[0],tarfb[1]))
|
|
173
|
+
print('\nTarget after %.2f %.2f'%(tarfa[0],tarfa[1]))
|
|
174
|
+
|
|
175
|
+
if(writeout):
|
|
176
|
+
targetfile.writeto(outname,output_verify='warn',overwrite=True)
|
|
177
|
+
|
|
178
|
+
srcfile.close()
|
|
179
|
+
targetfile.close()
|
|
180
|
+
|
|
181
|
+
b_end = tm.time()
|
|
182
|
+
print('\nDone in %d seconds\n'%(int(b_end-b_start)))
|
|
183
|
+
|