@@ -0,0 +1,196 | |||||
|
1 | ||||
|
2 | import os | |||
|
3 | import time | |||
|
4 | import numpy | |||
|
5 | import datetime | |||
|
6 | import numpy as np | |||
|
7 | import matplotlib.pyplot as plt | |||
|
8 | from mpl_toolkits.axes_grid1 import make_axes_locatable | |||
|
9 | from matplotlib.ticker import FuncFormatter, LinearLocator | |||
|
10 | ||||
|
11 | from schainpy.model.proc.jroproc_base import Operation | |||
|
12 | ||||
|
13 | func = lambda x, pos: ('%s') %(datetime.datetime.utcfromtimestamp(x).strftime('%H:%M')) | |||
|
14 | ||||
|
15 | d1970 = datetime.datetime(1970,1,1) | |||
|
16 | ||||
|
17 | ||||
|
18 | class PlotData(Operation): | |||
|
19 | ||||
|
20 | __MAXNUMX = 80 | |||
|
21 | __MAXNUMY = 80 | |||
|
22 | __missing = 1E30 | |||
|
23 | ||||
|
24 | def __init__(self): | |||
|
25 | ||||
|
26 | Operation.__init__(self) | |||
|
27 | self.xmin = None | |||
|
28 | self.xmax = None | |||
|
29 | self.newdataOut = None | |||
|
30 | self.dataOut = None | |||
|
31 | self.isConfig = False | |||
|
32 | self.figure = None | |||
|
33 | ||||
|
34 | def setup(self, dataOut, **kwargs): | |||
|
35 | ||||
|
36 | self.first = True | |||
|
37 | self.plottype = kwargs.pop('plottype', 'rti') | |||
|
38 | self.localtime = kwargs.pop('localtime', True) | |||
|
39 | self.show = kwargs.pop('show', True) | |||
|
40 | self.save = kwargs.pop('save', False) | |||
|
41 | self.pause = kwargs.pop('pause', False) | |||
|
42 | self.time = [] | |||
|
43 | self.nblock = 0 | |||
|
44 | self.z = [] | |||
|
45 | self.data = [{} for __ in dataOut.channelList] | |||
|
46 | self.axes = [] | |||
|
47 | self.colormap = kwargs.get('colormap', 'jet') | |||
|
48 | self.title = kwargs.get('wintitle', self.plottype.upper()) | |||
|
49 | self.xaxis = kwargs.get('xaxis', None) | |||
|
50 | self.zmin = kwargs.get('zmin', None) | |||
|
51 | self.zmax = kwargs.get('zmax', None) | |||
|
52 | ||||
|
53 | xmin = kwargs.get('xmin', 0) | |||
|
54 | xmax = kwargs.get('xmax', xmin+4) | |||
|
55 | ||||
|
56 | dt = dataOut.datatime.date() | |||
|
57 | dtmin = datetime.datetime.combine(dt, datetime.time(xmin, 0, 0)) | |||
|
58 | dtmax = datetime.datetime.combine(dt, datetime.time(xmax, 59, 59)) | |||
|
59 | ||||
|
60 | self.xmin = (dtmin-d1970).total_seconds() | |||
|
61 | self.xmax = (dtmax-d1970).total_seconds() | |||
|
62 | ||||
|
63 | if self.plottype in ('rti',): | |||
|
64 | self.ncols = 1 | |||
|
65 | self.nrows = dataOut.nChannels | |||
|
66 | self.width = 8 | |||
|
67 | self.height = 2.2*self.nrows | |||
|
68 | self.ylabel = 'Range [Km]' | |||
|
69 | self.y = dataOut.getHeiRange() | |||
|
70 | ||||
|
71 | self.ymin = kwargs.get('ymin', min(self.y)) | |||
|
72 | self.ymax = kwargs.get('ymax', max(self.y)) | |||
|
73 | ||||
|
74 | if self.figure is None: | |||
|
75 | self.figure = plt.figure() | |||
|
76 | else: | |||
|
77 | self.figure.clf() | |||
|
78 | ||||
|
79 | for n in range(dataOut.nChannels): | |||
|
80 | ax = self.figure.add_subplot(self.nrows, self.ncols, n+1) | |||
|
81 | ax.firsttime = True | |||
|
82 | self.axes.append(ax) | |||
|
83 | ||||
|
84 | self.figure.set_size_inches (self.width, self.height) | |||
|
85 | ||||
|
86 | def fill_gaps(self, x_buffer, y_buffer, z_buffer): | |||
|
87 | ||||
|
88 | if x_buffer.shape[0] < 2: | |||
|
89 | return x_buffer, y_buffer, z_buffer | |||
|
90 | ||||
|
91 | deltas = x_buffer[1:] - x_buffer[0:-1] | |||
|
92 | x_median = np.median(deltas) | |||
|
93 | ||||
|
94 | index = np.where(deltas > 5*x_median) | |||
|
95 | ||||
|
96 | if len(index[0]) != 0: | |||
|
97 | z_buffer[index[0],::] = self.__missing | |||
|
98 | z_buffer = np.ma.masked_inside(z_buffer, | |||
|
99 | 0.99*self.__missing, | |||
|
100 | 1.01*self.__missing) | |||
|
101 | ||||
|
102 | return x_buffer, y_buffer, z_buffer | |||
|
103 | ||||
|
104 | def plot(self): | |||
|
105 | ||||
|
106 | dt = datetime.datetime.utcfromtimestamp(self.time[-2]) | |||
|
107 | ||||
|
108 | if self.plottype=='rti': | |||
|
109 | self.time.sort() | |||
|
110 | for ch in self.dataOut.channelList: | |||
|
111 | self.z.append([self.data[ch][t] for t in self.time]) | |||
|
112 | self.x = np.array(self.time) | |||
|
113 | self.z = np.array(self.z) | |||
|
114 | self.plot_rti() | |||
|
115 | ||||
|
116 | for n, ax in enumerate(self.axes): | |||
|
117 | ||||
|
118 | if self.xaxis=='time': | |||
|
119 | ax.xaxis.set_major_formatter(FuncFormatter(func)) | |||
|
120 | ax.xaxis.set_major_locator(LinearLocator(6)) | |||
|
121 | ||||
|
122 | ax.yaxis.set_major_locator(LinearLocator(4)) | |||
|
123 | ||||
|
124 | ax.set_ylabel(self.ylabel) | |||
|
125 | ax.set_ylim(self.ymin, self.ymax) | |||
|
126 | ax.set_xlim(self.xmin, self.xmax) | |||
|
127 | ||||
|
128 | ax.set_title('Channel {} {}'.format( | |||
|
129 | self.dataOut.channelList[n], | |||
|
130 | dt.strftime('%y/%m/%d %H:%M:%S'), | |||
|
131 | size=6)) | |||
|
132 | ||||
|
133 | if self.save: | |||
|
134 | figname = os.path.join(self.save, 'rti_{}.png'.format(dt.strftime('%y%m%d_%H%M%S'))) | |||
|
135 | print 'Saving figure: {}'.format(figname) | |||
|
136 | self.figure.savefig(figname) | |||
|
137 | ||||
|
138 | self.figure.canvas.draw() | |||
|
139 | if self.show: | |||
|
140 | self.figure.show() | |||
|
141 | if self.pause: | |||
|
142 | raw_input('Press <ENTER> to continue') | |||
|
143 | ||||
|
144 | def plot_rti(self): | |||
|
145 | ||||
|
146 | for n, ax in enumerate(self.axes): | |||
|
147 | x, y, z = self.fill_gaps(self.x, self.y, self.z[n]) | |||
|
148 | if ax.firsttime: | |||
|
149 | zmin = self.zmin if self.zmin else np.nanmin(self.z) | |||
|
150 | zmax = self.zmax if self.zmax else np.nanmax(self.z) | |||
|
151 | plot = ax.pcolormesh(x, y, z.T, | |||
|
152 | vmin=zmin, | |||
|
153 | vmax=zmax, | |||
|
154 | cmap=plt.get_cmap(self.colormap) | |||
|
155 | ) | |||
|
156 | divider = make_axes_locatable(ax) | |||
|
157 | cax = divider.new_horizontal(size='3%', pad=0.05) | |||
|
158 | self.figure.add_axes(cax) | |||
|
159 | plt.colorbar(plot, cax) | |||
|
160 | ax.firsttime = False | |||
|
161 | else: | |||
|
162 | plot = ax.pcolormesh(x, y, z.T, | |||
|
163 | vmin=zmin, | |||
|
164 | vmax=zmax, | |||
|
165 | cmap=plt.get_cmap(self.colormap) | |||
|
166 | ) | |||
|
167 | ||||
|
168 | self.figure.suptitle(self.title) | |||
|
169 | self.figure.subplots_adjust(wspace=None, hspace=0.5) | |||
|
170 | ||||
|
171 | def update(self): | |||
|
172 | ||||
|
173 | self.nblock += 1 | |||
|
174 | self.time.append(self.dataOut.ltctime) | |||
|
175 | if self.plottype=='rti': | |||
|
176 | for ch in self.dataOut.channelList: | |||
|
177 | self.data[ch][self.dataOut.ltctime] = self.dataOut.getPower()[ch] | |||
|
178 | ||||
|
179 | def run(self, dataOut, **kwargs): | |||
|
180 | ||||
|
181 | self.dataOut = dataOut | |||
|
182 | ||||
|
183 | if not self.isConfig: | |||
|
184 | self.setup(dataOut, **kwargs) | |||
|
185 | self.isConfig = True | |||
|
186 | ||||
|
187 | self.update() | |||
|
188 | ||||
|
189 | if dataOut.ltctime>=self.xmax: | |||
|
190 | self.plot() | |||
|
191 | self.isConfig = False | |||
|
192 | ||||
|
193 | def close(self): | |||
|
194 | ||||
|
195 | self.plot() | |||
|
196 | No newline at end of file |
General Comments 0
You need to be logged in to leave comments.
Login now