-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpintaexec.py
277 lines (225 loc) · 12.7 KB
/
pintaexec.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
import subprocess
import os
import time
import pintautils as utils
import sys
def exec_cmd(session, item, branch, program, xnbin=False):
# xnbin option is valid for dspsr, pdmp and ps2pdf.
outfile = log_file_name(session, item, branch, program, 'out', xnbin)
errfile = log_file_name(session, item, branch, program, 'err', xnbin)
print("[LOG] {}/{} stdout will be written to {}".format(branch, program, outfile))
print("[LOG] {}/{} stderr will be written to {}".format(branch, program, errfile))
if branch == 'gptool' and program == 'gptool':
cmd = "gptool -f ./{} -nodedisp -o ./".format(os.path.basename(item.rawdatafile))
#cmd = "gptool -f {} -nodedisp -o {}".format(item.rawdatafile, session.working_dir)
#cmd_split = filter(lambda x: len(x)>0, cmd.split(' '))
elif program == 'dspsr':
fil_file = output_file_name(session, item, branch, 'fil')
if not xnbin:
#fits_file_prefix = "{}/{}.{}".format(session.working_dir, item.output_root, branch)
fits_file_prefix = "./{}.{}".format(item.output_root, branch)
cmd = "dspsr -N {} -d {} -b {} -E {} -L {} -m {} -A {} -O {} -e fits".format(item.jname, item.npol, item.nbin, item.parfile, item.tsubint, item.timestamp, fil_file, fits_file_prefix)
#cmd_split = filter(lambda x: len(x)>0, cmd.split(' '))
else:
fits_file_prefix = "./{}.{}.{}xNBin".format(item.output_root, branch, session.xnbinfac)
cmd = "dspsr -N {} -d {} -b {} -E {} -L {} -m {} -A {} -O {} -e fits".format(item.jname, item.npol, int(item.nbin*session.xnbinfac), item.parfile, item.tsubint, item.timestamp, fil_file, fits_file_prefix)
elif program == 'psredit':
if not xnbin:
fits_file = output_file_name(session, item, branch, 'fits')
cmd = "psredit -c name={},be:name=GWB,coord={},rcvr:name={},be:config='{}',be:delay={} -m {}".format(item.jname, item.coordstr, item.rcvr_name, item.gwb_config, -item.gwb_delay, fits_file)
else:
fits_file = output_file_name(session, item, branch, '{}xNBin.fits'.format(session.xnbinfac))
cmd = "psredit -c name={},be:name=GWB,coord={},rcvr:name={},be:config='{}',be:delay={} -m {}".format(item.jname, item.coordstr, item.rcvr_name, item.gwb_config, -item.gwb_delay, fits_file)
elif program == 'pam':
if not xnbin:
fits_file = output_file_name(session, item, branch, 'fits')
cmd = "pam -a PSRFITS -m {}".format(fits_file)
else:
fits_file = output_file_name(session, item, branch, '{}xNBin.fits'.format(session.xnbinfac))
cmd = "pam -a PSRFITS -m {}".format(fits_file)
elif program == 'pdmp':
if not xnbin:
fits_file = output_file_name(session, item, branch, 'fits')
summary_file = output_file_name(session, item, branch, 'summary.ps')
cmd = "pdmp -mc 64 -ms 16 -g {}/cps {}".format(summary_file, fits_file)
#cmd_split = filter(lambda x: len(x)>0, cmd.split(' '))
else:
fits_file = output_file_name(session, item, branch, '{}xNBin.fits'.format(session.xnbinfac))
summary_file = output_file_name(session, item, branch, '{}xNBin.summary.ps'.format(session.xnbinfac))
cmd = "pdmp -mc 64 -g {}/cps {}".format(summary_file, fits_file)
elif branch == 'rfiClean' and program == 'rfiClean':
fil_file = output_file_name(session, item, branch, 'fil')
#rfic_hdrfilename = "{}/{}-{}-ttemp-gm.info".format(session.working_dir, item.jname, item.idx)
Nprocess = 16
##cmd = 'crp_rficlean_gm.sh {} {} {} {} {} \"-psrf {} -psrfbins 32 -gmtstamp {}\"'.format(fil_file, session.rfic_conf_file, Nprocess, os.path.basename(item.rawdatafile), item.rfic_hdrfilename, item.f0psr, os.path.basename(item.timestampfile))
## now use absolute delta-freq. instead of Fourier-bins (whose width can change with tsamp,block-size)
cmd = 'crp_rficlean_gm.sh {} {} {} {} {} \"-psrf {} -psrfdf 8.0 -gmtstamp {}\"'.format(fil_file, session.rfic_conf_file, Nprocess, os.path.basename(item.rawdatafile), item.rfic_hdrfilename, item.f0psr, os.path.basename(item.timestampfile))
#cmd_split = ["crp_rficlean_gm.sh", fil_file, session.rfic_conf_file, str(Nprocess), item.rawdatafile, rfic_hdrfilename, rficlean_flags]
elif program == 'ps2pdf':
if not xnbin:
summary_file = output_file_name(session, item, branch, 'summary.ps')
cmd = "ps2pdf {}".format(summary_file)
else:
summary_file = output_file_name(session, item, branch, '{}xNBin.summary.ps'.format(session.xnbinfac))
cmd = "ps2pdf {}".format(summary_file)
print("[CMD]", cmd)
try:
if not session.test_mode:
start_time = time.time()
of = open(outfile, 'w')
ef = open(errfile, 'w')
p = subprocess.Popen(cmd, stdout=of, stderr=ef, shell=True)
p.wait()
of.close()
ef.close()
stop_time = time.time()
exec_time = stop_time-start_time
print_exec_time(branch, program, exec_time)
except:
raise OSError("Error while executing command.\ncmd :: "+cmd)
def output_file_name(session, item, branch, ext):
#return "{}/{}.{}.{}".format(session.working_dir, item.output_root, branch, ext)
return "{}/{}.{}.{}".format('.', item.output_root, branch, ext)
def log_file_name(session, item, branch, program, dev, xnbin=False):
if not xnbin:
return "{}/{}.{}.{}".format(item.logdir, program, branch, dev)
else:
return "{}/{}.{}.{}xNBin.{}".format(item.logdir, program, branch, session.xnbinfac, dev)
def print_exec_time(branch, program, exectime):
print("[TIME] Execution time for {}/{} = {} s".format(branch, program, exectime))
def remove_tmp_file(session, item, branch, ext):
if (not session.test_mode) and session.delete_tmp_files:
filename = output_file_name(session, item, branch, ext)
print("[INFO] Removing file", filename)
os.remove(filename)
def run_gptool(session, item, branch):
program = 'gptool'
exec_cmd(session, item, branch, program)
if not session.test_mode:
gptfilename = "./{}.gpt".format(os.path.basename(item.rawdatafile))
gptfilename_new = output_file_name(session, item, branch, 'gpt.dat')
os.rename(gptfilename, gptfilename_new)
print("[CMD] mv {} {}".format(gptfilename, gptfilename_new))
def run_filterbank(session, item, branch):
program = 'filterbank'
if branch == 'gptool':
filterbank_in_file = output_file_name(session, item, branch, 'gpt.dat')
else:
filterbank_in_file = './' + os.path.basename(item.rawdatafile)
fil_file = output_file_name(session, item, branch, 'fil')
#cmd = "filterbank {} -mjd {} -rf {} -nch {} -bw {} -ts {} -df {} > {}".format(filterbank_in_file, item.timestamp, item.freq, item.nchan, item.chanwidth, item.tsmpl, item.sideband_code, fil_file)
sideband_opt = "-u" if item.sideband=='USB' else ''
cmd = "ugmrt2fil -i {} -o {} -j {} -d {} -f {} -c {} -w {} -t {} {}".format(filterbank_in_file, fil_file, item.jname, item.timestamp, item.freq, item.nchan, item.chanwidth, item.tsmpl, sideband_opt)
print("[CMD]", cmd)
if not session.test_mode:
start_time = time.time()
os.system(cmd)
stop_time = time.time()
print_exec_time(branch, program, stop_time-start_time)
def run_dspsr(session, item, branch, xnbin=False):
program = 'dspsr'
exec_cmd(session, item, branch, program, xnbin=xnbin)
# Checking if output file is created
if not xnbin:
fits_file = "./" + output_file_name(session, item, branch, 'fits')
else:
fits_file = "./" + output_file_name(session, item, branch, '{}xNBin.fits'.format(session.xnbinfac))
if not os.access(fits_file, os.F_OK) and not session.test_mode:
print("[ERROR] dspsr failed to create file {} ... Quitting...".format(fits_file))
raise OSError
def run_psredit(session, item, branch, xnbin=False):
program = 'psredit'
exec_cmd(session, item, branch, program, xnbin=xnbin)
def run_pam(session, item, branch, xnbin=False):
program = 'pam'
exec_cmd(session, item, branch, program, xnbin=xnbin)
def run_pdmp(session, item, branch, xnbin=False):
program = 'pdmp'
exec_cmd(session, item, branch, program, xnbin=xnbin)
def run_ps2pdf(session, item, branch, xnbin=False):
program = 'ps2pdf'
exec_cmd(session, item, branch, program, xnbin=xnbin)
def run_rficlean(session, item, branch):
#print("[INFO] Trying to make the rficlean-gmhdr file ...")
#rfic_hdrfilename = "{}/{}-{}-ttemp-gm.info".format(session.working_dir, item.jname, item.idx)
#if not utils.make_rficlean_hdrfile(rfic_hdrfilename, item.jname, item.freq, item.nchan, item.chanwidth, item.tsmpl, item.sideband):
# print ("[ERROR] Could not make the rficlean-gmhdr file!")
# sys.exit(0)
program = 'rfiClean'
exec_cmd(session, item, branch, program)
def norfix_branch(session, item):
branch = 'norfix'
run_filterbank(session, item, branch)
run_dspsr(session, item, branch)
run_pam(session, item, branch)
run_psredit(session, item, branch)
run_pdmp(session, item, branch)
run_ps2pdf(session, item, branch)
remove_tmp_file(session, item, branch, 'summary.ps')
if session.fold_extra_nbin:
run_dspsr(session, item, branch, xnbin=True)
run_pam(session, item, branch, xnbin=True)
run_psredit(session, item, branch, xnbin=True)
run_pdmp(session, item, branch, xnbin=True)
run_ps2pdf(session, item, branch, xnbin=True)
remove_tmp_file(session, item, branch, '{}xNBin.summary.ps'.format(session.xnbinfac))
remove_tmp_file(session, item, branch, 'fil')
def gptool_branch(session, item):
branch = 'gptool'
run_gptool(session, item, branch)
run_filterbank(session, item, branch)
remove_tmp_file(session, item, branch, 'gpt.dat')
run_dspsr(session, item, branch)
run_pam(session, item, branch)
run_psredit(session, item, branch)
run_pdmp(session, item, branch)
run_ps2pdf(session, item, branch)
remove_tmp_file(session, item, branch, 'summary.ps')
if session.fold_extra_nbin:
run_dspsr(session, item, branch, xnbin=True)
run_pam(session, item, branch, xnbin=True)
run_psredit(session, item, branch, xnbin=True)
run_pdmp(session, item, branch, xnbin=True)
run_ps2pdf(session, item, branch, xnbin=True)
remove_tmp_file(session, item, branch, '{}xNBin.summary.ps'.format(session.xnbinfac))
remove_tmp_file(session, item, branch, 'fil')
def rficlean_branch(session, item):
branch = 'rfiClean'
run_rficlean(session, item, branch)
run_dspsr(session, item, branch)
run_pam(session, item, branch)
run_psredit(session, item, branch)
run_pdmp(session, item, branch)
run_ps2pdf(session, item, branch)
remove_tmp_file(session, item, branch, 'summary.ps')
if session.fold_extra_nbin:
run_dspsr(session, item, branch, xnbin=True)
run_pam(session, item, branch, xnbin=True)
run_psredit(session, item, branch, xnbin=True)
run_pdmp(session, item, branch, xnbin=True)
run_ps2pdf(session, item, branch, xnbin=True)
remove_tmp_file(session, item, branch, '{}xNBin.summary.ps'.format(session.xnbinfac))
remove_tmp_file(session, item, branch, 'fil')
def setup_input_ln(session, item):
if not session.samedir:
for infile in [item.rawdatafile, item.timestampfile]:
ln_src = infile
ln_dst = "./" + os.path.basename(infile)
if os.path.exists(ln_dst) and os.path.islink(ln_dst):
print("[INFO] Removing existing symlink {}".format(ln_dst))
print("[CMD] rm {}".format(ln_dst))
os.remove(ln_dst)
elif os.path.exists(ln_dst) and not os.path.islink(ln_dst):
print("[ERROR] Can't replace {}. Please check working directory.".format(ln_dst))
raise OSError()
print("[INFO] Creating symlink to {}".format(ln_src))
print("[CMD] ln -s {} {}".format(ln_src, ln_dst))
os.symlink(ln_src, ln_dst)
def remove_input_ln(session, item):
if not session.samedir:
for infile in [item.rawdatafile, item.timestampfile]:
ln_dst = "./" + os.path.basename(infile)
if os.path.exists(ln_dst) and os.path.islink(ln_dst):
print("[INFO] Removing symlink {}".format(ln_dst))
print("[CMD] rm {}".format(ln_dst))
os.remove(ln_dst)