Skip to content

Commit

Permalink
Load/save summary (event/station selection) #17
Browse files Browse the repository at this point in the history
  • Loading branch information
adam-iris committed Aug 28, 2017
1 parent d8008b2 commit c5affac
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 31 deletions.
57 changes: 55 additions & 2 deletions pyweed/pyweed_gui.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,15 @@
# Basic packages
import sys
import logging
import os.path
import platform
from PyQt4 import QtCore
from PyQt4 import QtGui

# Configure matplotlib backend
import matplotlib
import platform
from pyweed.pyweed_utils import CancelledException
from obspy.core.event.catalog import read_events
from obspy.core.inventory.inventory import read_inventory
matplotlib.use('AGG')

# import gui.qrc # NOQA: F401
Expand All @@ -34,6 +36,8 @@
from pyweed.gui.ConsoleDialog import ConsoleDialog
from pyweed.gui.PreferencesDialog import PreferencesDialog
from pyweed.pyweed_core import PyWeedCore
from pyweed.pyweed_utils import CancelledException
from pyweed.summary import get_filtered_catalog, get_filtered_inventory

LOGGER = logging.getLogger(__name__)

Expand Down Expand Up @@ -146,6 +150,47 @@ def open_waveforms_dialog(self):
self.waveformsDialog.show()
self.waveformsDialog.loadWaveformChoices()

###############
# Summary
###############

def saveSummary(self):
"""
Save the selected events/stations
"""
# If the user quits or cancels this dialog, '' is returned
savePath = str(QtGui.QFileDialog.getExistingDirectory(
parent=self.mainWindow,
caption="Save Summary to"))
if savePath != '':
try:
catalog = get_filtered_catalog(self.events, self.iter_selected_events())
catalog.write(os.path.join(savePath, 'events.xml'), format="QUAKEML")
except Exception as e:
LOGGER.error("Unable to save event selection! %s", e)
try:
inventory = get_filtered_inventory(self.stations, self.iter_selected_stations())
inventory.write(os.path.join(savePath, 'stations.xml'), format="STATIONXML")
except Exception as e:
LOGGER.error("Unable to save station selection! %s", e)

def loadSummary(self):
# If the user quits or cancels this dialog, '' is returned
loadPath = str(QtGui.QFileDialog.getExistingDirectory(
parent=self.mainWindow,
caption="Load Summary from"))
if loadPath != '':
try:
catalog = read_events(os.path.join(loadPath, 'events.xml'))
self.on_events_loaded(catalog)
except Exception as e:
LOGGER.error("Unable to load events! %s", e)
try:
inventory = read_inventory(os.path.join(loadPath, 'stations.xml'))
self.on_stations_loaded(inventory)
except Exception as e:
LOGGER.error("Unable to load stations! %s", e)

###############
# Other UI elements
###############
Expand All @@ -161,6 +206,14 @@ def configure_menu(self):

fileMenu = mainMenu.addMenu('&File')

saveSummaryAction = QtGui.QAction("Save Summary", self.mainWindow)
saveSummaryAction.triggered.connect(self.saveSummary)
fileMenu.addAction(saveSummaryAction)

loadSummaryAction = QtGui.QAction("Load Summary", self.mainWindow)
loadSummaryAction.triggered.connect(self.loadSummary)
fileMenu.addAction(loadSummaryAction)

quitAction = QtGui.QAction("&Quit", self.mainWindow)
quitAction.setShortcut("Ctrl+Q")
quitAction.triggered.connect(self.closeApplication)
Expand Down
29 changes: 0 additions & 29 deletions pyweed/pyweed_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@
import re
from geographiclib.geodesic import Geodesic
from obspy.taup.tau import TauPyModel
from obspy.core.util.attribdict import AttribDict
from future.moves.urllib.parse import urlencode
import copy

LOGGER = logging.getLogger(__name__)
GEOD = Geodesic.WGS84
Expand Down Expand Up @@ -345,30 +343,3 @@ def __str__(self, *args, **kwargs):
return 'Cancelled'
else:
return s


def get_filtered_inventory(inventory, stations_iter):
"""
Given an inventory and an iterator of selected network/station/channel items, return
an inventory containing only the selected items.
"""
networks = {}
stations = {}
for (network, station, channel) in stations_iter:
# Create a station record if necessary, and add this channel to it
full_station_code = "%s.%s" % (network.code, station.code)
if full_station_code not in stations:
f_station = copy.copy(station)
f_station.channels = []
stations[full_station_code] = f_station
# Create a network record if necessary, and add this station to it
if network.code not in networks:
f_network = copy.copy(network)
f_network.stations = []
networks[network.code] = f_network
networks[network.code].stations.append(f_station)
stations[full_station_code].channels.append(channel)

f_inventory = copy.copy(inventory)
f_inventory.networks = list(networks.values())
return f_inventory
49 changes: 49 additions & 0 deletions pyweed/summary.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# -*- coding: utf-8 -*-
"""
Code for saving and loading user-selected events/stations.
:copyright:
Mazama Science, IRIS
:license:
GNU Lesser General Public License, Version 3
(http://www.gnu.org/copyleft/lesser.html)
"""

from __future__ import (absolute_import, division, print_function)
import copy
from obspy.core.inventory.inventory import Inventory
from obspy.core.event.catalog import Catalog


def get_filtered_inventory(inventory, stations_iter):
"""
Given an inventory and an iterator of selected network/station/channel items, return
an inventory containing only the selected items.
"""
networks = {}
stations = {}
for (network, station, channel) in stations_iter:
# Create a station record if necessary, and add this channel to it
full_station_code = "%s.%s" % (network.code, station.code)
if full_station_code not in stations:
f_station = copy.copy(station)
f_station.channels = []
stations[full_station_code] = f_station
# Create a network record if necessary, and add this station to it
if network.code not in networks:
f_network = copy.copy(network)
f_network.stations = []
networks[network.code] = f_network
networks[network.code].stations.append(f_station)
stations[full_station_code].channels.append(channel)

return Inventory(list(networks.values()), inventory.source, inventory.sender)


def get_filtered_catalog(catalog, events_iter):
"""
Given a catalog and an iterator of selected events, return a catalog containing only the selected events.
"""
return Catalog(list(events_iter))


0 comments on commit c5affac

Please sign in to comment.