/
run_grating.py
executable file
·276 lines (237 loc) · 8.21 KB
/
run_grating.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
#!/usr/bin/env /proj/sot/ska/bin/python
#################################################################################
# #
# run_grating.py: process grating data #
# #
# author: t. isobe (tisobe@cfa.harvard.edu) #
# #
# last update: May 08, 2015 #
# #
#################################################################################
import os
import sys
import re
import string
import math
import numpy
import unittest
#
#--- from ska
#
from Ska.Shell import getenv, bash
ascdsenv = getenv('source /home/ascds/.ascrc -r release', shell='tcsh')
bin_dir = '/data/mta4/Gratings/Script/'
mta_dir = '/data/mta/Script/Python_script2.7/'
dat_dir = '/data/mta/MTA/data/'
#
#--- append a path to a private folder to python directory
#
sys.path.append(bin_dir)
sys.path.append(mta_dir)
#
#--- converTimeFormat contains MTA time conversion routines
#
import convertTimeFormat as tcnv
import mta_common_functions as mcf
#
#--- a couple of things needed
#
dare = mcf.get_val('.dare', dir = dat_dir, lst=1)
hakama = mcf.get_val('.hakama', dir = dat_dir, lst=1)
#--------------------------------------------------------------------------------------
#-- run_grating: controlling function to run the script --
#--------------------------------------------------------------------------------------
def run_grating():
"""
controlling function to run the script
input: none
output: /data/mta/www/mta_grat/<dir> where dir is <Mon><yy>
"""
#
#--- set data collecting range
#
(start, stop, dir) = find_time_interval()
#
#--- extract needed data fits files
#
run_arc4gl(start, stop)
#
#--- process fits files
#
run_idl(dir)
#
#--- update records
#
update_record(dir)
#
#--- move the data to an appropriate location
#
cmd = 'mv /data/mta4/Gratings/' + dir + ' /data/mta/www/mta_grat/.'
os.system(cmd)
#
#--- clean up
#
os.system('rm -rf *.fits.gz')
os.system('rm -rf run_arc mk_idl_command.pl mkcommand.idl')
#--------------------------------------------------------------------------------------
#-- run_arc4gl: extract acis and hrc evt1a.fits files using arc4gl --
#--------------------------------------------------------------------------------------
def run_arc4gl(start, stop):
"""
extract acis and hrc evt1a.fits files using arc4gl
input: start --- start time in the format of 03/01/15
stop --- stop time
output: fits files (e.g., acisf17108_001N002_evt1a.fits.gz)
"""
#
#--- read a template and create the current command file
#
file = bin_dir + 'house_keeping/arc_template'
line = open(file, 'r').read()
line = line.replace('#START#', start)
line = line.replace('#STOP#', stop)
fo = open('./run_arc', 'w')
fo.write(line)
fo.close()
#
#--- run arc4gl
#
cmd1 = "/usr/bin/env PERL5LIB="
cmd2 = ' echo ' + hakama + ' |arc4gl -U' + dare + ' -Sarcocc -i./run_arc '
cmd = cmd1 + cmd2
bash(cmd, env=ascdsenv)
#
#--- remove unwanted fits files
#
os.system('rm *src1a*')
#--------------------------------------------------------------------------------------
#-- run_idl: process fits files with an updated idl scripts --
#--------------------------------------------------------------------------------------
def run_idl(dir):
"""
process fits files with an updated idl scripts
input: dir --- the name of output directory
output: dir/<stemp> --- a directory which contains the processed data
"""
#
#--- read a template and create the current command file
#
file = bin_dir + 'house_keeping/pl_template'
line = open(file, 'r').read()
line = line.replace('#DIR#', dir)
fo = open('./mk_idl_command.pl', 'w')
fo.write(line)
fo.close()
#
#--- make an output directory
#
cmd = 'mkdir /data/mta4/Gratings/' + dir
os.system(cmd)
#
#--- run a perl script to create an idl script
#
cmd1 = "/usr/bin/env PERL5LIB="
cmd2 = ' perl ./mk_idl_command.pl '
cmd = cmd1 + cmd2
bash(cmd, env=ascdsenv)
#
#--- run the idl script to process fits files
#
os.system('idl ./mkcommand.idl')
#--------------------------------------------------------------------------------------
#-- find_time_interval: create start and stop time and output directory name ---
#--------------------------------------------------------------------------------------
def find_time_interval(test='NO', month=4, year=2015):
"""
create start and stop time and output directory name
input: test --- if it is test, yes. default: 'NO'
month --- test month input. default: 4. it is ignored if not test.
year --- test year input. default: 2015. it is ignored if not test.
output: start --- start time in the format of mm/01/yy (e.g. 03/01/15)
stop --- stop time
dir --- output directory name in the format of <Mon><yy> (e.g. Jan15)
"""
#
#--- for the case if you want to run the test, there is option...
#
if test == 'NO':
#
#--- this function gives you the current time. year in the format of 2015, and month in 4
#
[year, month, day, hours, min, sec, weekday, yday, dst] = tcnv.currentTime()
#
#--- we need start and stop times in the formats of 03/01/15, 04/01/15
#
year_start = year
year_end = year
this_month = str(month)
if month < 10:
this_month = '0' + this_month
temp_month = month -1
if temp_month < 1:
temp_month = 12
year_start -= 1
last_month = str(temp_month)
if temp_month < 10:
last_month = '0' + last_month
temp = str(year_start)
year_start = temp[2] + temp[3]
temp = str(year_end)
year_end = temp[2] + temp[3]
start = last_month + '/01/' + year_start
stop = this_month + '/01/' + year_end
#
#--- create the output directory name (format example: Apr15)
#
cmonth = tcnv.changeMonthFormat(temp_month)
dir = cmonth + year_start
return (start, stop, dir)
#--------------------------------------------------------------------------------------
#-- update_record: update the recormed of the process data --
#--------------------------------------------------------------------------------------
def update_record(dir):
"""
update the recormed of the process data
input: none but read from the current output directory
outpu: obslist in 'Angles', 'EdE', 'Focus', 'Zero' directories
"""
#
#--- find out which data are processed from the output directory
#
cmd = 'ls /data/mta4/Gratings/' + dir + '> ./ztemp'
os.system(cmd)
f = open('./ztemp', 'r')
data = [line.strip() for line in f.readlines()]
f.close()
os.system('rm ./ztemp')
line = ''
for each in data:
line = line + '../' + dir + '/' + each + '\n'
#
#--- there are four directories with "obslist"
#
for odir in ('Angles', 'EdE', 'Focus', 'Zero'):
file = '/data/mta/www/mta_grat/' + odir + '/obslist'
fo = open(file, 'a')
fo.write(line)
fo.close()
#--------------------------------------------------------------------------------------
#---TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST----
#--------------------------------------------------------------------------------------
class TestFunctions(unittest.TestCase):
"""
testing functions
"""
#------------------------------------------------------------
def test_find_time_interval(self):
out = find_time_interval(test='YES')
comp = ('03/01/15', '04/01/15', 'Mar15')
self.assertEquals(out, comp)
#--------------------------------------------------------------------------------------
if __name__ == '__main__':
#
#--- if you like to run test, uncomment the next, and commend
#--- on the main script calling
#
#unittest.main()
run_grating()