-
Notifications
You must be signed in to change notification settings - Fork 16
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Snuffling Quick Save #25
Open
grajh
wants to merge
7
commits into
pyrocko:master
Choose a base branch
from
grajh:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 6 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
3c593d2
add: snuffling Quick Save
grajh f6a3f43
corr: proposed changes
grajh 84d9706
corr: newline at EOF
grajh ab34783
corr: various
grajh 061c873
add: Velest support.
grajh ebca084
corr: various
grajh d871b67
corr: optimization, corrections, rigorous testing
grajh File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,226 @@ | ||
#-*- coding: utf-8 -*- | ||
|
||
import datetime | ||
import os | ||
|
||
from pyrocko.snuffling import Choice, Snuffling, Switch | ||
from pyrocko.pile_viewer import Marker | ||
|
||
# Defaults to user's home folder. os.path.expanduser('~') | ||
USER_DEF_ROOT_FOLDER = \ | ||
os.path.expanduser('~') | ||
|
||
|
||
def folder_check(save_path): | ||
try: | ||
os.makedirs(save_path) | ||
except OSError: | ||
if not os.path.isdir(save_path): | ||
raise | ||
|
||
|
||
class QuickSave(Snuffling): | ||
|
||
""" | ||
|
||
<html> | ||
<h2>Quick Save</h2> | ||
<body> | ||
<p> | ||
Choose the predefined or user defined path from the <b>Root folder</b> | ||
menu, desired <b>options</b> and press <b>Run</b>. Path to marker file is | ||
printed to the terminal.<br> | ||
User defined path is selected by default and can be modified in the source | ||
file. It is given as a string under <i>USER_DEF_ROOT_FOLDER</i> variable | ||
(line 11). Its default value points to user's home folder. | ||
</p> | ||
<p> | ||
Options:</br> | ||
<ul> | ||
<li>Save only selected markers.</li> | ||
<li>Save only markers associated to the active event.</li> | ||
<ul> | ||
<li>Add active event name to path. Creates new folder if needed.</li> | ||
<li>Use active event name as filename.</li> | ||
<li>Save to Velest format.</li> | ||
</ul> | ||
</ul> | ||
</p> | ||
<p> | ||
Author: G. Rajh. | ||
</p> | ||
</body> | ||
</html> | ||
|
||
""" | ||
|
||
def setup(self): | ||
self.set_name('Quick Save') | ||
|
||
home_folder = os.path.expanduser('~') | ||
|
||
self.add_parameter(Choice('Root folder', 'root_folder', | ||
USER_DEF_ROOT_FOLDER, [USER_DEF_ROOT_FOLDER, | ||
home_folder + '/Desktop', | ||
home_folder + '/Documents' | ||
])) | ||
|
||
self.add_parameter(Switch( | ||
'Save only selected markers', 'save_sel_markers', False)) | ||
self.add_parameter(Switch( | ||
'Save only markers associated\nto the active event', | ||
'save_asc_markers', False)) | ||
self.add_parameter(Switch('Add active event name to path', | ||
'name_to_path', False)) | ||
self.add_parameter(Switch('Use active event name as filename', | ||
'name_as_filename', False)) | ||
self.add_parameter(Switch('Save to Velest format', | ||
'velest_format', False)) | ||
|
||
self.setup_gui(reloaded=True) | ||
self.set_live_update(False) | ||
|
||
def parse_markers(self): | ||
self.cleanup() | ||
|
||
if self.save_asc_markers and not self.save_sel_markers: | ||
active_event_marker, asc_phase_markers = \ | ||
self.get_active_event_and_phase_markers() | ||
self.phase_markers = asc_phase_markers[:] | ||
asc_phase_markers.insert(0, active_event_marker) | ||
self.selected_markers = asc_phase_markers | ||
self.active_event = active_event_marker.get_event() | ||
self.ae_name = self.active_event.name | ||
|
||
elif not self.save_asc_markers and self.save_sel_markers: | ||
self.selected_markers = self.get_selected_markers() | ||
|
||
else: | ||
self.selected_markers = self.get_markers() | ||
|
||
def save_markers(self): | ||
if self.save_asc_markers and not self.save_sel_markers: | ||
|
||
if self.name_to_path and self.name_as_filename: | ||
save_path = self.root_folder + "/" + self.ae_name | ||
folder_check(save_path) | ||
self.save_path = save_path + "/" + self.ae_name | ||
|
||
elif self.name_to_path: | ||
save_path = self.root_folder + "/" + self.ae_name | ||
folder_check(save_path) | ||
self.save_path = save_path + "/picks" | ||
|
||
elif self.name_as_filename: | ||
self.save_path = self.root_folder + "/" + self.ae_name | ||
|
||
else: | ||
self.save_path = self.root_folder + "/picks" | ||
|
||
else: | ||
self.save_path = self.root_folder + "/picks" | ||
|
||
Marker.save_markers(self.selected_markers, self.save_path) | ||
|
||
def save_velest(self): | ||
start_time = datetime.datetime(1970, 1, 1) | ||
event_time = self.active_event.time | ||
event_date = start_time.utcfromtimestamp(event_time) | ||
event_year = int(str(event_date.year)[2:]) | ||
event_month = event_date.month | ||
event_day = event_date.day | ||
event_hour = event_date.hour | ||
event_min = event_date.minute | ||
event_sec = event_date.second + ( | ||
round(event_date.microsecond / 10**6, 2)) | ||
event_lat = round(self.active_event.lat, 4) | ||
event_lon = round(self.active_event.lon, 4) | ||
event_depth = round(self.active_event.depth / 10**3, 2) | ||
event_mag = round(self.active_event.magnitude, 2) | ||
|
||
if event_lat >= 0: | ||
event_NS = "N" | ||
else: | ||
event_NS = "S" | ||
|
||
if event_lon >= 0: | ||
event_EW = "E" | ||
else: | ||
event_EW = "W" | ||
|
||
event_line = "{:02d}{:02d}{:02d} {:02d}{:02d} {:05.2f} {:7.4f}{} \ | ||
{:8.4f}{}{:8.2f}{:7.2f} 99 0.0 0.00 1.0 1.0 \n".format( | ||
event_year, event_month, event_day, event_hour, event_min, | ||
event_sec, event_lat, event_NS, event_lon, event_EW, event_depth, | ||
event_mag | ||
) | ||
|
||
self.save_path_velest = self.save_path + "_velest" | ||
|
||
if self.name_to_path or self.name_as_filename: | ||
velest_file = open(self.save_path_velest, 'w') | ||
else: | ||
velest_file = open(self.save_path_velest, 'a+') | ||
|
||
velest_file.write(event_line) | ||
|
||
phase_i = 0 | ||
phase_num = len(self.phase_markers) | ||
|
||
for phase_marker in self.phase_markers: | ||
phase_i += 1 | ||
phase_station = list(phase_marker.nslc_ids)[0][1] | ||
phase_name = phase_marker._phasename | ||
phase_tmin = phase_marker.tmin | ||
phase_tmax = phase_marker.tmax | ||
phase_unc = phase_marker._uncertainty | ||
|
||
if phase_tmin == phase_tmax: | ||
phase_time = phase_tmin | ||
else: | ||
phase_mid_int = (phase_tmax - phase_tmin) * 0.5 | ||
phase_time = phase_tmin + phase_mid_int | ||
|
||
phase_rtime = round(phase_time - event_time, 2) | ||
|
||
if phase_unc <= 0.1: | ||
phase_unc_class = 0 | ||
elif 0.1 < phase_unc <= 0.2: | ||
phase_unc_class = 1 | ||
elif 0.2 < phase_unc <= 0.5: | ||
phase_unc_class = 2 | ||
else: | ||
phase_unc_class = 3 | ||
|
||
if phase_i % 6.0 == 0 or phase_i == phase_num: | ||
phase_block = "{:4s}{}{}{:06.2f}\n".format(phase_station, | ||
phase_name, phase_unc_class, phase_rtime) | ||
else: | ||
phase_block = "{:4s}{}{}{:06.2f}".format(phase_station, | ||
phase_name, phase_unc_class, phase_rtime) | ||
|
||
|
||
velest_file.write(phase_block) | ||
|
||
velest_file.write('\n') | ||
velest_file.close() | ||
|
||
def call(self): | ||
self.parse_markers() | ||
self.save_markers() | ||
print('Markers saved to: {}.'.format(self.save_path)) | ||
|
||
if self.velest_format: | ||
self.save_asc_markers = True | ||
self.save_sel_markers = False | ||
self.parse_markers() | ||
self.save_velest() | ||
print('Velest format saved to: {}.'.format(self.save_path_velest)) | ||
else: | ||
pass | ||
|
||
def __snufflings__(): | ||
"""Returns a list of snufflings to be exported by this module.""" | ||
|
||
return[ QuickSave() ] | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
missing whitespace, please remove the added white spaces again. this does not follow pep8 ...