-
Notifications
You must be signed in to change notification settings - Fork 0
/
ede_plot.py
executable file
·310 lines (252 loc) · 8.73 KB
/
ede_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
#!/usr/bin/env /proj/sot/ska/bin/python
#############################################################################################
# #
# ede_plot.py: plotting evolution of EdE for ACIS S and HRC S grating obs #
# #
# author: t. isobe (tisobe@cfa.harvard.edu) #
# #
# last update: Jun 18, 2014 #
# #
#############################################################################################
import os
import sys
import re
import string
import math
import random
import operator
import matplotlib as mpl
if __name__ == '__main__':
mpl.use('Agg')
#
#--- reading directory list
#
path = '/data/mta/Script/Grating/EdE/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
import robust_linear as robust
#
#--- temp writing file name
#
rtail = int(10000 * random.random())
zspace = '/tmp/zspace' + str(rtail)
#---------------------------------------------------------------------------------------------------
#-- ede_plots: plotting time evolution of EdE ---
#---------------------------------------------------------------------------------------------------
def ede_plots(file):
"""
plotting time evolution of EdE
Input: file --- a file name of data
Output: *_plot.png/*_low_res_plot.png --- two plots; one is in 200dpi and another in 40dpi
"""
#
#--- read data
#
[xdata, ydata, yerror] = read_data(file)
#
#--- set plotting range
#
[xmin, xmax, ymin, ymax] = set_min_max(ydata)
ymax = 2100
xname = 'Time (year)'
yname = 'EdE'
label = create_label(file)
out = file + '.png'
out = out.replace('Data', 'Plots')
out1 = out.replace('_data.png', '_plot.png')
out2 = out.replace('_data.png', '_low_res_plot.png')
#
#--- create two figures. One is 200dpi and another for 40dpi. the low res plot is great for the intro page
#
plot_single_panel(xmin, xmax, ymin, ymax, xdata, ydata, yerror, xname, yname, label, out1, resolution=200)
plot_single_panel(xmin, xmax, ymin, ymax, xdata, ydata, yerror, xname, yname, label, out2, resolution=40)
#---------------------------------------------------------------------------------------------------
#-- set_min_max: set plotting range ---
#---------------------------------------------------------------------------------------------------
def set_min_max(ydata):
"""
set plotting range
Input: ydata ---- ydata
Output: [xmin, xmax, ymin, ymax]
"""
xmin = 1999
tlist = tcnv.currentTime()
xmax = tlist[0] + 1
ymin = min(ydata)
ymax = max(ydata)
ydiff = ymax - ymin
ymin -= 0.1 * ydiff
ymax += 0.2 * ydiff
if ymin < 0:
ymin = 0
return [xmin, xmax, ymin, ymax]
#---------------------------------------------------------------------------------------------------
#-- create_label: create a label for the plot from the data file ---
#---------------------------------------------------------------------------------------------------
def create_label(file):
"""
create a label for the plot from the data file
Input: file --- input file name
Output: out --- text
"""
atemp = re.split('\/', file)
line = atemp[len(atemp)-1]
if line == '':
line = file
atemp = re.split('_', line)
inst = atemp[0].upper()
grat = atemp[1].upper()
energy = atemp[2]
energy = energy[0] + '.' + energy[1] + energy[2] + energy[3]
out = 'Line: ' + str(energy) + 'keV : ' + inst + '/' + grat
return out
#---------------------------------------------------------------------------------------------------
#-- read_data: read data from a given file ---
#---------------------------------------------------------------------------------------------------
def read_data(file):
"""
read data from a given file
Input: file --- input file name
Output: date_list --- a list of date
ede_list --- a list of ede value
error_list --- a list of computed ede error
"""
f = open(file, 'r')
data = [line.strip() for line in f.readlines()]
f.close()
date_list = []
ede_list = []
error_list = []
for ent in data:
atemp = re.split('\s+', ent)
if mcf.chkNumeric(atemp[0])== False:
continue
fwhm = float(atemp[2])
ferr = float(atemp[3])
ede = float(atemp[4])
dom = float(atemp[10])
(year, ydate) = tcnv.DOMtoYdate(dom)
if tcnv.isLeapYear(year) == 1:
base = 366
else:
base = 365
fyear = year + ydate/base
date_list.append(fyear)
ede_list.append(ede)
#
#--- the error of EdE is computed using FWHM and its error value
#
error = math.sqrt(ede*ede* ((ferr*ferr) / (fwhm*fwhm)))
error_list.append(error)
return [date_list, ede_list, error_list]
#---------------------------------------------------------------------------------------------------
#-- plot_single_panel: plot a single data set on a single panel ---
#---------------------------------------------------------------------------------------------------
def plot_single_panel(xmin, xmax, ymin, ymax, xdata, ydata, yerror, xname, yname, label, outname, resolution=100):
"""
plot a single data set on a single panel
Input: xmin --- min x
xmax --- max x
ymin --- min y
ymax --- max y
xdata --- independent variable
ydata --- dependent variable
yerror --- error in y axis
xname --- x axis label
ynane --- y axis label
label --- a text to indecate what is plotted
outname --- the name of output file
resolution-- the resolution of the plot in dpi
Output: png plot named <outname>
"""
#
#--- fit line --- use robust method
#
(sint, slope, serr) = robust.robust_fit(xdata, ydata)
lslope = '%2.3f' % (round(slope, 3))
#
#--- close everything opened before
#
plt.close('all')
#
#--- set font size
#
mpl.rcParams['font.size'] = 12
props = font_manager.FontProperties(size=9)
#
#--- set plotting range
#
ax = plt.subplot(111)
ax.set_autoscale_on(False)
ax.set_xbound(xmin,xmax)
ax.set_xlim(xmin=xmin, xmax=xmax, auto=False)
ax.set_ylim(ymin=ymin, ymax=ymax, auto=False)
#
#--- plot data
#
plt.plot(xdata, ydata, color='blue', marker='o', markersize=4.0, lw =0)
#
#--- plot error bar
#
plt.errorbar(xdata, ydata, yerr=yerror, lw = 0, elinewidth=1)
#
#--- plot fitted line
#
start = sint + slope * xmin
stop = sint + slope * xmax
plt.plot([xmin, xmax], [start, stop], color='red', lw = 2)
#
#--- label axes
#
plt.xlabel(xname)
plt.ylabel(yname)
#
#--- add what is plotted on this plot
#
xdiff = xmax - xmin
xpos = xmin + 0.5 * xdiff
ydiff = ymax - ymin
ypos = ymax - 0.08 * ydiff
label = label + ': Slope: ' + str(lslope)
plt.text(xpos, ypos, label)
#
#--- set the size of the plotting area in inch (width: 10.0in, height 2.08in x number of panels)
#
fig = matplotlib.pyplot.gcf()
fig.set_size_inches(10.0, 5.0)
#
#--- save the plot in png format
#
plt.savefig(outname, format='png', dpi=resolution)
#--------------------------------------------------------------------
#
#--- 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
if __name__ == '__main__':
cmd = 'ls /data/mta_www/mta_grat/EdE_trend/EdE_Data/*_data > ' + zspace
os.system(cmd)
f = open(zspace, 'r')
data = [line.strip() for line in f.readlines()]
f.close()
for file in data:
ede_plots(file)