changed structure to class with PyLoT mainwindow as input
This commit is contained in:
parent
488068105b
commit
6c214bbc77
@ -7,37 +7,86 @@ from scipy.interpolate import griddata
|
||||
from matplotlib.backends.backend_qt4agg import NavigationToolbar2QT as NavigationToolbar
|
||||
from PySide import QtCore, QtGui
|
||||
|
||||
#import QtPyLoT
|
||||
from pylot.core.util.dataprocessing import read_metadata
|
||||
from pylot.core.util.widgets import PickDlg
|
||||
|
||||
plt.interactive(False)
|
||||
|
||||
#pf=QtPyLoT.main()
|
||||
class map_projection(QtGui.QWidget):
|
||||
def __init__(self, mainwindow):
|
||||
QtGui.QWidget.__init__(self)
|
||||
self.pyl_mainwindow = mainwindow
|
||||
self.parser = self.get_metadata('/data/Geothermie/Insheim/STAT_INFO/MAGS2_net.dless')
|
||||
self.init_graphics()
|
||||
self.init_stations()
|
||||
self.init_lat_lon_dimensions()
|
||||
self.init_lat_lon_grid()
|
||||
self.init_basemap(projection='mill', resolution='l')
|
||||
self.init_x_y_dimensions()
|
||||
self.connectSignals()
|
||||
self.draw_everything()
|
||||
self.show()
|
||||
|
||||
def onpick(event):
|
||||
def onpick(self, event):
|
||||
ind = event.ind
|
||||
if ind == []:
|
||||
return
|
||||
data = pf.get_data().getWFData()
|
||||
data = self.pyl_mainwindow.get_data().getWFData()
|
||||
for index in ind:
|
||||
station=str(station_names[index])
|
||||
station=str(self.station_names[index])
|
||||
try:
|
||||
pickDlg = PickDlg(pf, infile=pf.getinfile(),
|
||||
pickDlg = PickDlg(self, infile=self.pyl_mainwindow.getinfile(),
|
||||
data=data.select(station=station),
|
||||
station=station,
|
||||
picks=pf.getPicksOnStation(station, 'manual'),
|
||||
autopicks=pf.getPicksOnStation(station, 'auto'))
|
||||
pickDlg.exec_()
|
||||
picks=self.pyl_mainwindow.getPicksOnStation(station, 'manual'),
|
||||
autopicks=self.pyl_mainwindow.getPicksOnStation(station, 'auto'))
|
||||
pyl_mw = self.pyl_mainwindow
|
||||
if pickDlg.exec_():
|
||||
pyl_mw.setDirty(True)
|
||||
pyl_mw.update_status('picks accepted ({0})'.format(station))
|
||||
replot = pyl_mw.addPicks(station, pickDlg.getPicks())
|
||||
if replot:
|
||||
pyl_mw.plotWaveformData()
|
||||
pyl_mw.drawPicks()
|
||||
pyl_mw.draw()
|
||||
else:
|
||||
pyl_mw.drawPicks(station)
|
||||
pyl_mw.draw()
|
||||
else:
|
||||
pyl_mw.update_status('picks discarded ({0})'.format(station))
|
||||
except Exception as e:
|
||||
print('Could not generate Plot for station {st}.\n{er}'.format(st=station, er=e))
|
||||
|
||||
|
||||
|
||||
def get_metadata(path):
|
||||
def get_metadata(self, path):
|
||||
metadata=read_metadata(path)
|
||||
parser=metadata[1]
|
||||
return parser
|
||||
|
||||
def connectSignals(self):
|
||||
self.combobox.currentIndexChanged.connect(self.refresh_drawings)
|
||||
|
||||
def init_graphics(self):
|
||||
self.main_box = QtGui.QVBoxLayout()
|
||||
self.setLayout(self.main_box)
|
||||
|
||||
self.top_row = QtGui.QHBoxLayout()
|
||||
self.main_box.addLayout(self.top_row)
|
||||
|
||||
self.combobox = QtGui.QComboBox()
|
||||
self.combobox.insertItem(0, 'P')
|
||||
self.combobox.insertItem(1, 'S')
|
||||
self.top_row.addWidget(QtGui.QLabel('Select a phase: '))
|
||||
self.top_row.addWidget(self.combobox)
|
||||
|
||||
fig = plt.figure()
|
||||
self.main_ax = fig.add_subplot(111)
|
||||
self.canvas = fig.canvas
|
||||
self.main_box.addWidget(self.canvas)
|
||||
|
||||
self.toolbar = NavigationToolbar(self.canvas, self)
|
||||
self.main_box.addWidget(self.toolbar)
|
||||
|
||||
def init_stations(self):
|
||||
def get_station_names_lat_lon(parser):
|
||||
station_names=[]
|
||||
lat=[]
|
||||
@ -50,11 +99,18 @@ def get_station_names_lat_lon(parser):
|
||||
lon.append(station[0].longitude)
|
||||
return station_names, lat, lon
|
||||
|
||||
def get_picks(pf, station_names):
|
||||
station_names, lat, lon = get_station_names_lat_lon(self.parser)
|
||||
self.station_names = station_names
|
||||
self.lat = lat
|
||||
self.lon = lon
|
||||
|
||||
def init_picks(self):
|
||||
phase = self.combobox.currentText()
|
||||
def get_picks(station_names):
|
||||
picks=[]
|
||||
for station in station_names:
|
||||
try:
|
||||
picks.append(pf.autopicks[station]['P']['mpp'])
|
||||
picks.append(self.pyl_mainwindow.autopicks[station][phase]['mpp'])
|
||||
except:
|
||||
picks.append(np.nan)
|
||||
return picks
|
||||
@ -68,6 +124,10 @@ def get_picks_rel(picks):
|
||||
picks_rel.append(pick)
|
||||
return picks_rel
|
||||
|
||||
self.picks = get_picks(self.station_names)
|
||||
self.picks_rel = get_picks_rel(self.picks)
|
||||
|
||||
def init_picks_active(self):
|
||||
def remove_nan_picks(picks):
|
||||
picks_no_nan=[]
|
||||
for pick in picks:
|
||||
@ -75,6 +135,9 @@ def remove_nan_picks(picks):
|
||||
picks_no_nan.append(pick)
|
||||
return picks_no_nan
|
||||
|
||||
self.picks_no_nan = remove_nan_picks(self.picks_rel)
|
||||
|
||||
def init_stations_active(self):
|
||||
def remove_nan_lat_lon(picks, lat, lon):
|
||||
lat_no_nan=[]
|
||||
lon_no_nan=[]
|
||||
@ -84,25 +147,36 @@ def remove_nan_lat_lon(picks, lat, lon):
|
||||
lon_no_nan.append(lon[index])
|
||||
return lat_no_nan, lon_no_nan
|
||||
|
||||
self.lat_no_nan, self.lon_no_nan = remove_nan_lat_lon(self.picks_rel, self.lat, self.lon)
|
||||
|
||||
def init_lat_lon_dimensions(self):
|
||||
def get_lon_lat_dim(lon, lat):
|
||||
londim = max(lon) - min(lon)
|
||||
latdim = max(lat) - min(lat)
|
||||
return londim, latdim
|
||||
|
||||
self.londim, self.latdim = get_lon_lat_dim(self.lon, self.lat)
|
||||
|
||||
def init_x_y_dimensions(self):
|
||||
def get_x_y_dim(x, y):
|
||||
xdim = max(x) - min(x)
|
||||
ydim = max(y) - min(y)
|
||||
return xdim, ydim
|
||||
|
||||
def init_map(projection, ax, resolution='l'):
|
||||
m = Basemap(projection=projection, resolution = resolution, ax=ax)
|
||||
m.drawmapboundary(fill_color='darkblue')
|
||||
m.drawcountries()
|
||||
m.drawstates()
|
||||
m.fillcontinents(color='grey', lake_color='aqua')
|
||||
m.drawcoastlines()
|
||||
return m
|
||||
self.x, self.y = self.basemap(self.lon, self.lat)
|
||||
self.xdim, self.ydim = get_x_y_dim(self.x, self.y)
|
||||
|
||||
def init_basemap(self, projection, resolution='l'):
|
||||
basemap = Basemap(projection=projection, resolution = resolution, ax=self.main_ax)
|
||||
basemap.drawmapboundary(fill_color='darkblue')
|
||||
basemap.drawcountries()
|
||||
basemap.drawstates()
|
||||
basemap.fillcontinents(color='grey', lake_color='aqua')
|
||||
basemap.drawcoastlines()
|
||||
self.basemap = basemap
|
||||
|
||||
|
||||
def init_lat_lon_grid(self):
|
||||
def get_lat_lon_axis(lat, lon):
|
||||
steplat = (max(lat)-min(lat))/250
|
||||
steplon = (max(lon)-min(lon))/250
|
||||
@ -115,78 +189,71 @@ def get_lat_lon_grid(lataxis, lonaxis):
|
||||
longrid, latgrid = np.meshgrid(lonaxis, lataxis)
|
||||
return latgrid, longrid
|
||||
|
||||
def draw_contour_filled(picks, longrid, latgrid, picksgrid, levels='50'):
|
||||
levels = np.linspace(min(picks), max(picks), 50)
|
||||
contourf = m.contourf(longrid, latgrid, picksgrid, levels, latlon=True, zorder=9)
|
||||
return contourf
|
||||
self.lataxis, self.lonaxis = get_lat_lon_axis(self.lat, self.lon)
|
||||
self.latgrid, self.longrid = get_lat_lon_grid(self.lataxis, self.lonaxis)
|
||||
|
||||
def annotate_ax(ax, x, y, station_names):
|
||||
for index, name in enumerate(station_names):
|
||||
ax.annotate(' %s' % name, xy=(x[index], y[index]), fontsize='x-small', zorder=12)
|
||||
def init_picksgrid(self):
|
||||
self.picksgrid_no_nan = griddata((self.lat_no_nan, self.lon_no_nan),
|
||||
self.picks_no_nan, (self.latgrid, self.longrid), method='linear') ##################
|
||||
|
||||
def connect_pick(ax, onpick):
|
||||
ax.figure.canvas.mpl_connect('pick_event', onpick)
|
||||
def draw_contour_filled(self, nlevel='50'):
|
||||
levels = np.linspace(min(self.picks_rel), max(self.picks_rel), nlevel)
|
||||
self.contourf = self.basemap.contourf(self.longrid, self.latgrid, self.picksgrid_no_nan,
|
||||
levels, latlon=True, zorder=9)
|
||||
|
||||
def add_cbar(ax, scatter, label):
|
||||
cbar = ax.figure.colorbar(scatter)
|
||||
def scatter_all_stations(self):
|
||||
self.sc = self.basemap.scatter(self.lon, self.lat, s=50, facecolor='none', latlon=True,
|
||||
zorder=10, picker=True, edgecolor='m', label='Not Picked')
|
||||
self.cid = self.canvas.mpl_connect('pick_event', self.onpick)
|
||||
|
||||
def scatter_picked_stations(self):
|
||||
self.sc_picked = self.basemap.scatter(self.lon_no_nan, self.lat_no_nan, s=50,
|
||||
c=self.picks_no_nan, latlon=True, zorder=11, label='Picked')
|
||||
|
||||
def annotate_ax(self):
|
||||
self.annotations=[]
|
||||
for index, name in enumerate(self.station_names):
|
||||
self.annotations.append(self.main_ax.annotate(' %s' % name, xy=(self.x[index], self.y[index]),
|
||||
fontsize='x-small', zorder=12))
|
||||
self.legend=self.main_ax.legend()
|
||||
|
||||
def add_cbar(self, label):
|
||||
cbar = self.main_ax.figure.colorbar(self.sc_picked)
|
||||
cbar.set_label(label)
|
||||
return cbar
|
||||
|
||||
qwidget = QtGui.QWidget()
|
||||
QtGui.QVBoxLayout()
|
||||
main_box = QtGui.QVBoxLayout()
|
||||
qwidget.setLayout(main_box)
|
||||
def refresh_drawings(self):
|
||||
self.remove_drawings()
|
||||
self.draw_everything()
|
||||
|
||||
fig = plt.figure()
|
||||
main_ax = fig.add_subplot(111)
|
||||
canvas = main_ax.figure.canvas
|
||||
main_box.addWidget(canvas)
|
||||
def draw_everything(self):
|
||||
self.init_picks()
|
||||
self.init_picks_active()
|
||||
self.init_stations_active()
|
||||
self.init_picksgrid()
|
||||
self.draw_contour_filled()
|
||||
self.scatter_all_stations()
|
||||
self.scatter_picked_stations()
|
||||
self.annotate_ax()
|
||||
self.cbar = self.add_cbar(label='Time relative to first onset [s]')
|
||||
self.canvas.draw()
|
||||
|
||||
toolbar = NavigationToolbar(canvas, qwidget)
|
||||
main_box.addWidget(toolbar)
|
||||
def remove_drawings(self):
|
||||
self.sc_picked.remove()
|
||||
self.sc.remove()
|
||||
self.cbar.remove()
|
||||
self.remove_annotations()
|
||||
self.legend.remove()
|
||||
self.remove_contourf()
|
||||
self.canvas.mpl_disconnect(self.cid)
|
||||
self.canvas.draw()
|
||||
|
||||
def remove_contourf(self):
|
||||
for item in self.contourf.collections:
|
||||
item.remove()
|
||||
|
||||
def remove_annotations(self):
|
||||
for annotation in self.annotations:
|
||||
annotation.remove()
|
||||
|
||||
|
||||
parser = get_metadata('/data/Geothermie/Insheim/STAT_INFO/MAGS2_net.dless')
|
||||
|
||||
station_names, lat, lon = get_station_names_lat_lon(parser)
|
||||
|
||||
picks = get_picks(pf, station_names)
|
||||
picks_rel = get_picks_rel(picks)
|
||||
|
||||
picks_no_nan = remove_nan_picks(picks_rel)
|
||||
lat_no_nan, lon_no_nan = remove_nan_lat_lon(picks_rel, lat, lon)
|
||||
|
||||
londim, latdim = get_lon_lat_dim(lon, lat)
|
||||
x, y = m(lon, lat)
|
||||
xdim, ydim = get_x_y_dim(x, y)
|
||||
|
||||
m = init_map(projection='mill', ax=main_ax, resolution='l')
|
||||
|
||||
lataxis, lonaxis = get_lat_lon_axis(lat, lon)
|
||||
latgrid, longrid = get_lat_lon_grid(lataxis, lonaxis)
|
||||
|
||||
picksgrid_no_nan = griddata((lat_no_nan, lon_no_nan), picks_no_nan, (latgrid, longrid), method='linear')
|
||||
|
||||
contourf = draw_contour_filled(picks_no_nan, longrid, latgrid, picksgrid_no_nan)
|
||||
|
||||
sc = m.scatter(lon, lat, s=50, facecolor='none', latlon=True, zorder=10, picker=True, edgecolor='m', label='Not Picked')
|
||||
sc_picked = m.scatter(lon_no_nan, lat_no_nan, s=50, c=picks_no_nan, latlon=True, zorder=11, label='Picked')
|
||||
|
||||
annotate_ax(main_ax, x, y, station_names)
|
||||
|
||||
main_ax.legend()
|
||||
|
||||
connect_pick(main_ax, onpick)
|
||||
|
||||
cbar = add_cbar(main_ax, sc_picked, label='Time relative to first onset [s]')
|
||||
|
||||
canvas.draw()
|
||||
|
||||
qwidget.show()
|
||||
|
||||
# ax.set_xlim(min(x)-0.5*xdim, max(x)+0.5*xdim)
|
||||
# ax.set_ylim(min(y)-0.5*ydim, max(y)+0.5*ydim)
|
||||
|
||||
|
||||
|
||||
#plt.show()
|
||||
|
@ -1 +1 @@
|
||||
d037-dirty
|
||||
4880-dirty
|
||||
|
Loading…
Reference in New Issue
Block a user