-
Notifications
You must be signed in to change notification settings - Fork 0
/
grating_plot.py
executable file
·538 lines (458 loc) · 17.7 KB
/
grating_plot.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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
#!/usr/bin/env /proj/sot/ska/bin/python
#########################################################################################
# #
# grating_plot.py: create grating movement plots for monthly report #
# #
# author: t. isobe (tisobe@cfa.harvard.eud) #
# #
# last update: Oct 24, 2014 #
# #
#########################################################################################
import os
import sys
import re
import string
import math
import unittest
import matplotlib as mpl
if __name__ == '__main__':
mpl.use('Agg')
from pylab import *
import matplotlib.pyplot as plt
import matplotlib.font_manager as font_manager
import matplotlib.lines as lines
#
#--- reading directory list
#
path = '/data/mta/Script/Month/SIM/house_keeping/dir_list_py'
f = open(path, 'r')
data = [line.strip() for line in f.readlines()]
f.close()
for ent in data:
atemp = re.split(':', ent)
var = atemp[1].strip()
line = atemp[0].strip()
exec "%s = %s" %(var, line)
#
#--- 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
datafile = "/data/mta/www/mta_otg/OTG_sorted.rdb"
#-----------------------------------------------------------------------------------------
#-- plot_grat_movement: create grating movement plots ---
#-----------------------------------------------------------------------------------------
def plot_grat_movement():
"""
create grating movement plots
input: none, but read from database
outupu: monthly_grat.png and monthly_grat_ang.png
"""
#
#--- read data
#
[time, h_in_ang, h_out_ang, l_in_ang, l_out_ang, h_in, h_out, l_in, l_out] = get_grat_data()
#
#--- plot insertion/retraction angle plots
#
plot_steps(time, h_in_ang, h_out_ang, l_in_ang, l_out_ang)
#
#--- plot hetg/letg cumulative count rate plots
#
plot_cum_grating(time, h_in, l_in)
#-----------------------------------------------------------------------------------------
#-- get_grat_data: read database and extract needed information, then create data --
#-----------------------------------------------------------------------------------------
def get_grat_data():
"""
read database and extract needed information, then create data
input: none but read from the database: "/data/mta/www/mta_otg/OTG_sorted.rdb"
output: [time, h_in_ang, h_out_ang, l_in_ang, l_out_ang, h_in, h_out, l_in, l_out]
where: time --- time in fractional year
h_in_ang --- hetig insertion angle
h_out_ang --- hetig retraction angle
l_in_ang --- letig insertion angle
l_out_ang --- letig retraction angle
h_in --- hetig insertion cumm count
h_out --- hetig retraction cumm count
l_in --- letig insertion cumm count
l_out --- hetig retraction cumm count
"""
#
#--- read data
#
f = open(datafile, 'r')
data = [line.strip() for line in f.readlines()]
f.close()
#
#--- find the current year. this will be used to remove iregular data
#
[year, mon, day, hours, min, sec, weekday, yday, dst] = tcnv.currentTime()
direct = []
grating = []
start = []
stop = []
hposa = []
hposb = []
fposa = []
fposb = []
for i in range(1, len(data)):
ent = data[i]
atemp = re.split('\s+', ent)
try:
test = float(atemp[2])
test2 = float(atemp[4])
except:
continue
direct.append(atemp[0].strip())
grating.append(atemp[1].strip())
val = convert_time(atemp[2])
start.append(val)
stop.append(convert_time(atemp[4]))
hposa.append(float(atemp[18]))
hposb.append(float(atemp[19]))
fposa.append(float(atemp[20]))
fposb.append(float(atemp[21]))
#
#--- create start and stop lists of data bin. the width is a month
#
[blist, elist] = create_monthly_bins(2000, year, mon)
blen = len(blist)
time = [0 for x in range(0, blen)]
h_in = [0 for x in range(0, blen)] #--- hetg insertion cumm count rate
h_in_ang = [0 for x in range(0, blen)] #--- hetg insertion angle
h_out = [0 for x in range(0, blen)] #--- hetg retraction cumm count rate
h_out_ang = [0 for x in range(0, blen)] #--- hetg retraction angle
l_in = [0 for x in range(0, blen)]
l_in_ang = [0 for x in range(0, blen)]
l_out = [0 for x in range(0, blen)]
l_out_ang = [0 for x in range(0, blen)]
for j in range(1, blen):
time[j] = 0.5 * (blist[j] + elist[j]) #--- take a mid point for the bin's time
#
#-- creating cummulative count; the current bin should have, at least, as the same as the
#-- previous bin
#
h_in[j] = h_in[j-1]
h_out[j] = h_out[j-1]
l_in[j] = l_in[j-1]
l_out[j] = l_out[j-1]
h_in_ang_cnt = 0
h_out_ang_cnt = 0
l_in_ang_cnt = 0
l_out_ang_cnt = 0
#
#--- since the data are not ordered by date, go through begining to the end
#--- every bin cycle
#
for i in range(0, len(start)):
if start[i] >= blist[j] and start[i] < elist[j]:
if direct[i] == 'INSR':
if grating[i] == 'HETG':
h_in_ang[j] += fposa[i]
h_in_ang_cnt += 1
h_out_ang[j] += hposa[i]
h_out_ang_cnt += 1
if grating[i] == 'LETG':
l_in_ang[j] += fposa[i]
l_in_ang_cnt += 1
l_out_ang[j] += hposa[i]
l_out_ang_cnt += 1
#
#--- taking monthly average
#
if h_in_ang_cnt> 0:
h_in_ang[j] /= h_in_ang_cnt
if h_out_ang_cnt> 0:
h_out_ang[j] /= h_out_ang_cnt
if l_in_ang_cnt> 0:
l_in_ang[j] /= l_in_ang_cnt
if l_out_ang_cnt> 0:
l_out_ang[j] /= l_out_ang_cnt
#
#--- adding in/out count for the month to appropriate bins
#
h_in[j] += h_in_ang_cnt
h_out[j] += h_out_ang_cnt
l_in[j] += l_in_ang_cnt
l_out[j] += l_out_ang_cnt
return [time, h_in_ang, h_out_ang, l_in_ang, l_out_ang, h_in, h_out, l_in, l_out]
#-----------------------------------------------------------------------------------------
#-- convert_time: convert time format from <year><ydate>.<hh><mm><ss> to frac year ---
#-----------------------------------------------------------------------------------------
def convert_time(otime):
"""
convert time format from <year><ydate>.<hh><mm><ss> to frac year
input: otime --- time in e.g. 2014059.122333.1
output: fyear --- fractional year, e.g. 2014.1630585
"""
year = float(otime[0:4])
ydate = float(otime[4:7])
hours = float(otime[8:10])
mins = float(otime[10:12])
secs = float(otime[12:14])
if tcnv.isLeapYear(year) == 1:
base = 366.0
else:
base = 365.0
fday = hours / 24.0 + mins / 1440.0 + secs / 86400.0
fyear = year + (ydate + fday) / base
return fyear
#-----------------------------------------------------------------------------------------
#-- create_monthly_bins: create a month wide bin for given periods ---
#-----------------------------------------------------------------------------------------
def create_monthly_bins(ystart, ystop, mstop):
"""
create a month wide bin for given periods
input: ystart --- starting year
ystop --- stopping year
mstop --- stopping month of the stopping month
output: [blist, elist] a list of lists of starting and stoping period in fractional year
"""
interval1 = [0.0, 31.0, 59.0, 90.0, 120.0, 151.0, 181.0, 212.0, 243.0, 273.0, 304.0, 334.0, 365.0]
interval2 = [0.0, 31.0, 60.0, 91.0, 121.0, 152.0, 182.0, 213.0, 244.0, 274.0, 305.0, 335.0, 366.0]
blist = []
elist = []
for year in range(ystart, ystop+1):
#
#--- check leap year
#
if tcnv.isLeapYear(year) == 1:
interval = interval2
base = 366.0
else:
interval = interval1
base = 365.0
#
#--- go around 12 months
#
for i in range(0, 12):
if year == ystop and i >= mstop:
break
begin = year + interval[i] / base
end = year + interval[i+1] / base
if int(end) > year:
end = year + 1
blist.append(begin)
elist.append(end)
return [blist, elist]
#-----------------------------------------------------------------------------------------
#-- : create insertion and retraction angle plots for hetig and letig --
#-----------------------------------------------------------------------------------------
def plot_steps(time, set1, set2, set3, set4):
"""
create insertion and retraction angle plots for hetig and letig
input: time --- time in fractional year
set1 --- mean hetig insertion angle
set2 --- mean hetig retraction angle
set3 --- mean letig insertion angle
set4 --- mean letig retraction angle
where "mean" means month average
output: monthly_grat_ang.png
"""
#
#--- setting plotting range
#
[year, mon, day, hours, min, sec, weekday, yday, dst] = tcnv.currentTime()
xmin = 2000
xmax = year + 1
if mon > 6:
xmax += 1
ymin1 = 5.0
ymax1 = 10.0
ymin2 = 76
ymax2 = 81
#
#--- set a few parameters
#
fsize = 9 #--- font size
lsize = 0 #--- line width
color = 'red'
marker = 'o'
msize = 2
plt.close("all")
mpl.rcParams['font.size'] = fsize
props = font_manager.FontProperties(size=fsize)
plt.subplots_adjust(hspace=0.11, wspace=0.11) #--- spacing of panels
#
#--- 'Mean HETG Inserted Angle
#
a1 = plt.subplot(221)
plot_sub(a1, time, set1, xmin, xmax, ymin1, ymax1, color, lsize, marker, msize, tline='Mean HETG Inserted Angle')
a1.set_ylabel('Insertion Angle (Degree)', size=fsize)
#
#--- 'Mean HETG Retracted Angle
#
a2 = plt.subplot(223)
plot_sub(a2, time, set2, xmin, xmax, ymin2, ymax2, color, lsize, marker, msize, tline='Mean HETG Retracted Angle')
a2.set_xlabel('Time (year)', size=fsize)
a2.set_ylabel('Retraction Angle (Degree)', size=fsize)
#
#--- 'Mean LETG Inserted Angle
#
a3 = plt.subplot(222)
plot_sub(a3, time, set3, xmin, xmax, ymin1, ymax1, color, lsize, marker, msize, tline='Mean LETG Inserted Angle')
#
#--- 'Mean LETG Retracted Angle
#
a4 = plt.subplot(224)
plot_sub(a4, time, set4, xmin, xmax, ymin2, ymax2, color, lsize, marker, msize, tline='Mean LETG Rectracted Angle')
a4.set_xlabel('Time (year)', size=fsize)
#
#--- save the plot
#
fig = matplotlib.pyplot.gcf()
fig.set_size_inches(10.0, 5.0)
outname = 'monthly_grat_ang.png'
plt.savefig(outname, format='png', dpi=100)
#-----------------------------------------------------------------------------------------
#-- plot_cum_grating: plot cummulative count rates of hetig and letig insertion --
#-----------------------------------------------------------------------------------------
def plot_cum_grating(time, h_in, l_in):
"""
plot cummulative count rates of hetig and letig insertion.
input: time --- fractional year
h_in --- hetig insertion cummulative count rate (month step)
l_in --- letig insertion cummulative count rate
output: monthly_grat.prn
"""
#
#--- set x axis plotting range
#
[year, mon, day, hours, min, sec, weekday, yday, dst] = tcnv.currentTime()
xmin = 2000
xmax = year + 1
if mon > 6:
xmax += 1
#
#--- set y axis plotting range
#
ymin = 0.0
ymax = max(h_in)
ymax2 = max(l_in)
if ymax2 > ymax:
ymax = ymax2
ymax = int(1.1 * ymax) + 10
#
#--- set a few parameters
#
fsize = 9
lsize = 0
color = 'red'
marker = 'o'
msize = 3
plt.close("all")
mpl.rcParams['font.size'] = fsize
props = font_manager.FontProperties(size=fsize)
plt.subplots_adjust(hspace=0.08, wspace=0.10)
#
#--- HETG Cumulative Count Plots
#
a1 = plt.subplot(121) #--- two panel plot: left
plot_sub(a1, time, h_in, xmin, xmax, ymin, ymax, color, lsize, marker, msize, tline='HETG')
a1.set_xlabel('Time (year)', size=fsize)
a1.set_ylabel('Cumulative Insertion Counts', size=fsize)
#
#--- LETG Cumulative Count Plots
#
a1 = plt.subplot(122) #--- two panel plot: right
plot_sub(a1, time, l_in, xmin, xmax, ymin, ymax, color, lsize, marker, msize, tline='LETG')
a1.set_xlabel('Time (year)', size=fsize)
#
#--- save the plot
#
fig = matplotlib.pyplot.gcf()
fig.set_size_inches(10.0, 5.0)
outname = 'monthly_grat.png'
plt.savefig(outname, format='png', dpi=100)
#-----------------------------------------------------------------------------------------
#-- plot_sub: plotting each panel --
#-----------------------------------------------------------------------------------------
def plot_sub(ap, x, y, xmin, xmax, ymin, ymax, color, lsize, marker, msize, tline=''):
"""
plotting each panel
input ap --- panel name
x --- x data list
y --- y data list
xmin --- xmin
xmax --- xmax
ymin --- ymin
ymax --- ymax
color --- color of data point
lsize --- line size
marker --- marker shape
msize --- size of the marker
tlime --- extra text line
"""
ap.set_autoscale_on(False)
ap.set_xbound(xmin,xmax)
ap.set_xlim(xmin=xmin, xmax=xmax, auto=False)
ap.set_ylim(ymin=ymin, ymax=ymax, auto=False)
plt.plot(x, y , color=color, lw=lsize, marker=marker, markersize=msize)
if tline != '':
xpos = 0.05 * (xmax - xmin) + xmin
ypos = ymax -0.10 * (ymax - ymin)
text(xpos, ypos, tline, fontsize=11,style='italic', weight='bold')
#-----------------------------------------------------------------------------------------
#-- TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST ---
#-----------------------------------------------------------------------------------------
class TestFunctions(unittest.TestCase):
"""
testing functions
"""
#------------------------------------------------------------
def test_get_grat_data(self):
[time, h_in_ang, h_out_ang, l_in_ang, l_out_ang, h_in, h_out, l_in, l_out] = get_grat_data()
h_in_ang_test = [5.96, 0, 5.96, 6.086666666666667, 5.96]
h_out_ang_test = [79.09, 0, 79.09000000000002, 79.09, 79.09]
l_in_ang_test = [7.09, 7.09, 7.09, 6.71, 6.773333333333333]
l_out_ang_test = [78.15, 78.15, 77.96, 77.96, 77.96]
h_in_test = [368, 368, 374, 377, 379]
h_out_test = [368, 368, 374, 377, 379]
l_in_test = [204, 206, 207, 208, 214]
l_out_test = [204, 206, 207, 208, 214]
self.assertEquals(h_in_ang[100:105], h_in_ang_test)
self.assertEquals(h_out_ang[100:105], h_out_ang_test)
self.assertEquals(l_in_ang[100:105], l_in_ang_test)
self.assertEquals(l_out_ang[100:105], l_out_ang_test)
self.assertEquals(h_in[100:105], h_in_test)
self.assertEquals(h_out[100:105], h_out_test)
self.assertEquals(l_in[100:105], l_in_test)
self.assertEquals(l_out[100:105], l_out_test)
#------------------------------------------------------------
def test_convert_time(self):
time = '2014059.122333.1'
val = convert_time(time)
val = round(val, 7)
self.assertEquals(val, 2014.1630585)
#------------------------------------------------------------
def test_create_monthly_bins(self):
out1 = [2013.0, 2013.0849315068492, 2013.1616438356164, 2013.2465753424658, 2013.3287671232877, 2013.4136986301369, 2013.495890410959, 2013.5808219178082, 2013.6657534246576, 2013.7479452054795, 2013.8328767123287, 2013.9150684931508, 2014.0, 2014.0849315068492]
out2 = [2013.0849315068492, 2013.1616438356164, 2013.2465753424658, 2013.3287671232877, 2013.4136986301369, 2013.495890410959, 2013.5808219178082, 2013.6657534246576, 2013.7479452054795, 2013.8328767123287, 2013.9150684931508, 2014, 2014.0849315068492, 2014.1616438356164]
ystart = 2013
ystop = 2014
mstop = 2
[blist, elist] = create_monthly_bins(ystart, ystop, mstop)
self.assertEquals(blist, out1)
self.assertEquals(elist, out2)
#-----------------------------------------------------------------------------------------
#
#--- pylab plotting routine related modules
#
from pylab import *
import matplotlib.pyplot as plt
import matplotlib.font_manager as font_manager
import matplotlib.lines as lines
chk = 0
if len(sys.argv) == 2:
chk = 1
if __name__ == '__main__':
if chk > 0:
plot_grat_movement()
else:
unittest.main()