jroplot_data.py
427 lines
| 14.5 KiB
| text/x-python
|
PythonLexer
|
r865 | |||
import os | ||||
r889 | import zmq | |||
|
r865 | import time | ||
import numpy | ||||
import datetime | ||||
import numpy as np | ||||
import matplotlib.pyplot as plt | ||||
from mpl_toolkits.axes_grid1 import make_axes_locatable | ||||
from matplotlib.ticker import FuncFormatter, LinearLocator | ||||
r889 | from multiprocessing import Process | |||
|
r865 | |||
from schainpy.model.proc.jroproc_base import Operation | ||||
r889 | #plt.ion() | |||
|
r865 | func = lambda x, pos: ('%s') %(datetime.datetime.utcfromtimestamp(x).strftime('%H:%M')) | ||
d1970 = datetime.datetime(1970,1,1) | ||||
r889 | class PlotData(Operation, Process): | |||
|
r865 | |||
r889 | CODE = 'Figure' | |||
colormap = 'jet' | ||||
|
r897 | CONFLATE = True | ||
|
r865 | __MAXNUMX = 80 | ||
__MAXNUMY = 80 | ||||
__missing = 1E30 | ||||
r889 | def __init__(self, **kwargs): | |||
|
r865 | |||
|
r897 | Operation.__init__(self, **kwargs) | ||
r889 | Process.__init__(self) | |||
self.mp = False | ||||
|
r865 | self.dataOut = None | ||
self.isConfig = False | ||||
self.figure = None | ||||
r889 | self.axes = [] | |||
|
r865 | self.localtime = kwargs.pop('localtime', True) | ||
r889 | self.show = kwargs.get('show', True) | |||
self.save = kwargs.get('save', False) | ||||
self.colormap = kwargs.get('colormap', self.colormap) | ||||
r907 | self.showprofile = kwargs.get('showprofile', True) | |||
|
r866 | self.title = kwargs.get('wintitle', '') | ||
r889 | self.xaxis = kwargs.get('xaxis', 'time') | |||
|
r865 | self.zmin = kwargs.get('zmin', None) | ||
self.zmax = kwargs.get('zmax', None) | ||||
r889 | self.xmin = kwargs.get('xmin', None) | |||
self.xmax = kwargs.get('xmax', None) | ||||
self.xrange = kwargs.get('xrange', 24) | ||||
|
r866 | self.ymin = kwargs.get('ymin', None) | ||
self.ymax = kwargs.get('ymax', None) | ||||
|
r898 | self.throttle_value = 1 | ||
|
r865 | def fill_gaps(self, x_buffer, y_buffer, z_buffer): | ||
if x_buffer.shape[0] < 2: | ||||
return x_buffer, y_buffer, z_buffer | ||||
deltas = x_buffer[1:] - x_buffer[0:-1] | ||||
x_median = np.median(deltas) | ||||
index = np.where(deltas > 5*x_median) | ||||
if len(index[0]) != 0: | ||||
|
r897 | z_buffer[::, index[0], ::] = self.__missing | ||
|
r865 | z_buffer = np.ma.masked_inside(z_buffer, | ||
0.99*self.__missing, | ||||
1.01*self.__missing) | ||||
return x_buffer, y_buffer, z_buffer | ||||
|
r866 | def decimate(self): | ||
r889 | ||||
|
r898 | # dx = int(len(self.x)/self.__MAXNUMX) + 1 | ||
|
r866 | dy = int(len(self.y)/self.__MAXNUMY) + 1 | ||
r889 | ||||
|
r898 | # x = self.x[::dx] | ||
x = self.x | ||||
r889 | y = self.y[::dy] | |||
|
r898 | z = self.z[::, ::, ::dy] | ||
r889 | ||||
|
r866 | return x, y, z | ||
r889 | def __plot(self): | |||
print 'plotting...{}'.format(self.CODE) | ||||
|
r866 | |||
self.plot() | ||||
r892 | self.figure.suptitle('{} {} - Date:{}'.format(self.title, self.CODE.upper(), | |||
datetime.datetime.utcfromtimestamp(self.max_time).strftime('%y/%m/%d %H:%M:%S'))) | ||||
r889 | ||||
|
r866 | if self.save: | ||
r889 | figname = os.path.join(self.save, '{}_{}.png'.format(self.CODE, | |||
|
r898 | datetime.datetime.utcfromtimestamp(self.times[0]).strftime('%y%m%d_%H%M%S'))) | ||
|
r866 | print 'Saving figure: {}'.format(figname) | ||
self.figure.savefig(figname) | ||||
self.figure.canvas.draw() | ||||
r889 | def plot(self): | |||
print 'plotting...{}'.format(self.CODE.upper()) | ||||
return | ||||
|
r866 | |||
r889 | def run(self): | |||
|
r866 | |||
r889 | print '[Starting] {}'.format(self.name) | |||
context = zmq.Context() | ||||
receiver = context.socket(zmq.SUB) | ||||
receiver.setsockopt(zmq.SUBSCRIBE, '') | ||||
|
r897 | receiver.setsockopt(zmq.CONFLATE, self.CONFLATE) | ||
r889 | receiver.connect("ipc:///tmp/zmq.plots") | |||
|
r866 | |||
r889 | while True: | |||
try: | ||||
#if True: | ||||
self.data = receiver.recv_pyobj(flags=zmq.NOBLOCK) | ||||
self.dataOut = self.data['dataOut'] | ||||
self.times = self.data['times'] | ||||
self.times.sort() | ||||
|
r898 | self.throttle_value = self.data['throttle'] | ||
r889 | self.min_time = self.times[0] | |||
self.max_time = self.times[-1] | ||||
|
r866 | |||
r889 | if self.isConfig is False: | |||
self.setup() | ||||
self.isConfig = True | ||||
self.__plot() | ||||
|
r898 | if self.data['ENDED'] is True: | ||
r893 | # self.__plot() | |||
|
r898 | self.isConfig = False | ||
r889 | ||||
except zmq.Again as e: | ||||
print 'Waiting for data...' | ||||
|
r898 | plt.pause(self.throttle_value) | ||
# time.sleep(3) | ||||
|
r866 | |||
def close(self): | ||||
if self.dataOut: | ||||
self._plot() | ||||
r889 | ||||
|
r866 | |||
class PlotSpectraData(PlotData): | ||||
r889 | CODE = 'spc' | |||
colormap = 'jro' | ||||
|
r897 | CONFLATE = False | ||
r889 | def setup(self): | |||
ncolspan = 1 | ||||
colspan = 1 | ||||
self.ncols = int(numpy.sqrt(self.dataOut.nChannels)+0.9) | ||||
self.nrows = int(self.dataOut.nChannels*1./self.ncols + 0.9) | ||||
self.width = 3.6*self.ncols | ||||
self.height = 3.2*self.nrows | ||||
if self.showprofile: | ||||
ncolspan = 3 | ||||
colspan = 2 | ||||
self.width += 1.2*self.ncols | ||||
self.ylabel = 'Range [Km]' | ||||
self.titles = ['Channel {}'.format(x) for x in self.dataOut.channelList] | ||||
if self.figure is None: | ||||
self.figure = plt.figure(figsize=(self.width, self.height), | ||||
edgecolor='k', | ||||
facecolor='w') | ||||
else: | ||||
self.figure.clf() | ||||
n = 0 | ||||
for y in range(self.nrows): | ||||
for x in range(self.ncols): | ||||
|
r897 | if n >= self.dataOut.nChannels: | ||
r889 | break | |||
ax = plt.subplot2grid((self.nrows, self.ncols*ncolspan), (y, x*ncolspan), 1, colspan) | ||||
if self.showprofile: | ||||
ax.ax_profile = plt.subplot2grid((self.nrows, self.ncols*ncolspan), (y, x*ncolspan+colspan), 1, 1) | ||||
ax.firsttime = True | ||||
self.axes.append(ax) | ||||
n += 1 | ||||
r907 | self.figure.subplots_adjust(left=0.1, right=0.95, bottom=0.15, top=0.85, wspace=0.9, hspace=0.5) | |||
r889 | self.figure.show() | |||
|
r866 | |||
|
r865 | def plot(self): | ||
r889 | ||||
if self.xaxis == "frequency": | ||||
x = self.dataOut.getFreqRange(1)/1000. | ||||
xlabel = "Frequency (kHz)" | ||||
elif self.xaxis == "time": | ||||
x = self.dataOut.getAcfRange(1) | ||||
xlabel = "Time (ms)" | ||||
else: | ||||
x = self.dataOut.getVelRange(1) | ||||
xlabel = "Velocity (m/s)" | ||||
y = self.dataOut.getHeiRange() | ||||
z = self.data[self.CODE] | ||||
for n, ax in enumerate(self.axes): | ||||
if ax.firsttime: | ||||
self.xmax = self.xmax if self.xmax else np.nanmax(x) | ||||
self.xmin = self.xmin if self.xmin else -self.xmax | ||||
self.ymin = self.ymin if self.ymin else np.nanmin(y) | ||||
self.ymax = self.ymax if self.ymax else np.nanmax(y) | ||||
self.zmin = self.zmin if self.zmin else np.nanmin(z) | ||||
self.zmax = self.zmax if self.zmax else np.nanmax(z) | ||||
ax.plot = ax.pcolormesh(x, y, z[n].T, | ||||
vmin=self.zmin, | ||||
vmax=self.zmax, | ||||
cmap=plt.get_cmap(self.colormap) | ||||
) | ||||
divider = make_axes_locatable(ax) | ||||
cax = divider.new_horizontal(size='3%', pad=0.05) | ||||
self.figure.add_axes(cax) | ||||
plt.colorbar(ax.plot, cax) | ||||
ax.set_xlim(self.xmin, self.xmax) | ||||
ax.set_ylim(self.ymin, self.ymax) | ||||
ax.xaxis.set_major_locator(LinearLocator(5)) | ||||
#ax.yaxis.set_major_locator(LinearLocator(4)) | ||||
ax.set_ylabel(self.ylabel) | ||||
ax.set_xlabel(xlabel) | ||||
ax.firsttime = False | ||||
if self.showprofile: | ||||
ax.plot_profile= ax.ax_profile.plot(self.data['rti'][self.max_time][n], y)[0] | ||||
ax.ax_profile.set_xlim(self.zmin, self.zmax) | ||||
ax.ax_profile.set_ylim(self.ymin, self.ymax) | ||||
ax.ax_profile.set_xlabel('dB') | ||||
ax.ax_profile.grid(b=True, axis='x') | ||||
r892 | ax.plot_noise = ax.ax_profile.plot(numpy.repeat(self.data['noise'][self.max_time][n], len(y)), y, | |||
color="k", linestyle="dashed", lw=2)[0] | ||||
r889 | [tick.set_visible(False) for tick in ax.ax_profile.get_yticklabels()] | |||
else: | ||||
ax.plot.set_array(z[n].T.ravel()) | ||||
if self.showprofile: | ||||
ax.plot_profile.set_data(self.data['rti'][self.max_time][n], y) | ||||
r892 | ax.plot_noise.set_data(numpy.repeat(self.data['noise'][self.max_time][n], len(y)), y) | |||
|
r866 | |||
r892 | ax.set_title('{} - Noise: {:.2f} dB'.format(self.titles[n], self.data['noise'][self.max_time][n]), | |||
size=8) | ||||
|
r866 | |||
class PlotRTIData(PlotData): | ||||
r889 | ||||
CODE = 'rti' | ||||
colormap = 'jro' | ||||
def setup(self): | ||||
|
r866 | self.ncols = 1 | ||
self.nrows = self.dataOut.nChannels | ||||
r889 | self.width = 10 | |||
|
r866 | self.height = 2.2*self.nrows | ||
r907 | if self.nrows==1: | |||
self.height += 1 | ||||
|
r866 | self.ylabel = 'Range [Km]' | ||
r889 | self.titles = ['Channel {}'.format(x) for x in self.dataOut.channelList] | |||
if self.figure is None: | ||||
self.figure = plt.figure(figsize=(self.width, self.height), | ||||
edgecolor='k', | ||||
facecolor='w') | ||||
else: | ||||
self.figure.clf() | ||||
|
r898 | self.axes = [] | ||
r889 | ||||
for n in range(self.nrows): | ||||
ax = self.figure.add_subplot(self.nrows, self.ncols, n+1) | ||||
ax.firsttime = True | ||||
self.axes.append(ax) | ||||
self.figure.subplots_adjust(hspace=0.5) | ||||
self.figure.show() | ||||
|
r866 | def plot(self): | ||
|
r865 | |||
r889 | self.x = np.array(self.times) | |||
|
r866 | self.y = self.dataOut.getHeiRange() | ||
self.z = [] | ||||
r889 | ||||
for ch in range(self.nrows): | ||||
self.z.append([self.data[self.CODE][t][ch] for t in self.times]) | ||||
self.z = np.array(self.z) | ||||
|
r865 | for n, ax in enumerate(self.axes): | ||
r889 | ||||
|
r866 | x, y, z = self.fill_gaps(*self.decimate()) | ||
|
r898 | xmin = self.min_time | ||
xmax = xmin+self.xrange*60*60 | ||||
|
r865 | if ax.firsttime: | ||
r889 | self.ymin = self.ymin if self.ymin else np.nanmin(self.y) | |||
self.ymax = self.ymax if self.ymax else np.nanmax(self.y) | ||||
self.zmin = self.zmin if self.zmin else np.nanmin(self.z) | ||||
|
r898 | self.zmax = self.zmax if self.zmax else np.nanmax(self.z) | ||
|
r866 | plot = ax.pcolormesh(x, y, z[n].T, | ||
r889 | vmin=self.zmin, | |||
vmax=self.zmax, | ||||
|
r865 | cmap=plt.get_cmap(self.colormap) | ||
) | ||||
divider = make_axes_locatable(ax) | ||||
r889 | cax = divider.new_horizontal(size='2%', pad=0.05) | |||
|
r865 | self.figure.add_axes(cax) | ||
plt.colorbar(plot, cax) | ||||
|
r866 | ax.set_ylim(self.ymin, self.ymax) | ||
|
r898 | if self.xaxis == 'time': | ||
r889 | ax.xaxis.set_major_formatter(FuncFormatter(func)) | |||
ax.xaxis.set_major_locator(LinearLocator(6)) | ||||
|
r898 | # ax.yaxis.set_major_locator(LinearLocator(4)) | ||
r889 | ||||
ax.set_ylabel(self.ylabel) | ||||
|
r898 | # if self.xmin is None: | ||
# xmin = self.min_time | ||||
# else: | ||||
# xmin = (datetime.datetime.combine(self.dataOut.datatime.date(), | ||||
# datetime.time(self.xmin, 0, 0))-d1970).total_seconds() | ||||
r889 | ||||
ax.set_xlim(xmin, xmax) | ||||
|
r865 | ax.firsttime = False | ||
r889 | else: | |||
ax.collections.remove(ax.collections[0]) | ||||
|
r898 | ax.set_xlim(xmin, xmax) | ||
r889 | plot = ax.pcolormesh(x, y, z[n].T, | |||
vmin=self.zmin, | ||||
vmax=self.zmax, | ||||
cmap=plt.get_cmap(self.colormap) | ||||
) | ||||
ax.set_title('{} {}'.format(self.titles[n], | ||||
datetime.datetime.utcfromtimestamp(self.max_time).strftime('%y/%m/%d %H:%M:%S')), | ||||
size=8) | ||||
class PlotCOHData(PlotRTIData): | ||||
CODE = 'coh' | ||||
def setup(self): | ||||
self.ncols = 1 | ||||
self.nrows = self.dataOut.nPairs | ||||
self.width = 10 | ||||
self.height = 2.2*self.nrows | ||||
r907 | if self.nrows==1: | |||
self.height += 1 | ||||
r889 | self.ylabel = 'Range [Km]' | |||
self.titles = ['Channels {}'.format(x) for x in self.dataOut.pairsList] | ||||
|
r865 | |||
r889 | if self.figure is None: | |||
self.figure = plt.figure(figsize=(self.width, self.height), | ||||
edgecolor='k', | ||||
facecolor='w') | ||||
else: | ||||
self.figure.clf() | ||||
r907 | self.axes = [] | |||
r889 | ||||
for n in range(self.nrows): | ||||
ax = self.figure.add_subplot(self.nrows, self.ncols, n+1) | ||||
ax.firsttime = True | ||||
self.axes.append(ax) | ||||
self.figure.subplots_adjust(hspace=0.5) | ||||
self.figure.show() | ||||
|
r865 | |||
r907 | class PlotNoiseData(PlotData): | |||
CODE = 'noise' | ||||
def setup(self): | ||||
self.ncols = 1 | ||||
self.nrows = 1 | ||||
self.width = 10 | ||||
self.height = 3.2 | ||||
self.ylabel = 'Intensity [dB]' | ||||
self.titles = ['Noise'] | ||||
r889 | ||||
r907 | if self.figure is None: | |||
self.figure = plt.figure(figsize=(self.width, self.height), | ||||
edgecolor='k', | ||||
facecolor='w') | ||||
else: | ||||
self.figure.clf() | ||||
self.axes = [] | ||||
self.ax = self.figure.add_subplot(self.nrows, self.ncols, 1) | ||||
self.ax.firsttime = True | ||||
self.figure.show() | ||||
def plot(self): | ||||
x = self.times | ||||
xmin = self.min_time | ||||
xmax = xmin+self.xrange*60*60 | ||||
if self.ax.firsttime: | ||||
for ch in self.dataOut.channelList: | ||||
y = [self.data[self.CODE][t][ch] for t in self.times] | ||||
self.ax.plot(x, y, lw=1, label='Ch{}'.format(ch)) | ||||
self.ax.firsttime = False | ||||
self.ax.xaxis.set_major_formatter(FuncFormatter(func)) | ||||
self.ax.xaxis.set_major_locator(LinearLocator(6)) | ||||
self.ax.set_ylabel(self.ylabel) | ||||
plt.legend() | ||||
else: | ||||
for ch in self.dataOut.channelList: | ||||
y = [self.data[self.CODE][t][ch] for t in self.times] | ||||
self.ax.lines[ch].set_data(x, y) | ||||
self.ax.set_xlim(xmin, xmax) | ||||
self.ax.set_ylim(min(y)-5, max(y)+5) | ||||
class PlotSNRData(PlotRTIData): | ||||
|
r898 | CODE = 'snr' | ||
r889 | ||||
|
r898 | class PlotDOPData(PlotRTIData): | ||
CODE = 'dop' | ||||
colormap = 'jet' | ||||
r889 | ||||
class PlotPHASEData(PlotCOHData): | ||||
CODE = 'phase' | ||||
colormap = 'seismic' | ||||