[refs #137] implementation of GUI driven NonLinLoc location started

This commit is contained in:
Sebastian Wehling-Benatelli 2016-08-25 13:31:51 +02:00
parent 135ac0ef80
commit e53dd99d75
5 changed files with 110 additions and 45 deletions

View File

@ -44,7 +44,7 @@ from pylot.core.io.inputs import FilterOptions, AutoPickParameter
from pylot.core.pick.autopick import autopickevent
from pylot.core.pick.compare import Comparison
from pylot.core.io.phases import picksdict_from_picks
from pylot.core.loc.nll import locate as locateNll
import pylot.core.loc.nll as nll
from pylot.core.util.defaults import FILTERDEFAULTS, COMPNAME_MAP, \
AUTOMATIC_DEFAULTS
from pylot.core.util.errors import FormatError, DatastructureError, \
@ -61,7 +61,7 @@ from pylot.core.util.thread import AutoPickThread
from pylot.core.util.version import get_git_version as _getVersionString
import icons_rc
locateTool = dict(nll=locateNll)
locateTool = dict(nll=nll)
class MainWindow(QMainWindow):
@ -890,10 +890,14 @@ class MainWindow(QMainWindow):
def locateEvent(self):
settings = QSettings()
# get location tool hook
loctool = settings.value("loc/tool", "nll")
extlocpath = settings.value("%s/binPath".format(loctool), None)
locroot = settings.value("%s/rootPath".format(loctool), None)
if extlocpath is None or locroot is None:
lt = locateTool[loctool]
# get working directory
locroot = settings.value("{0}/rootPath".format(loctool), None)
infile = settings.value("{0}/inputFile".format(loctool), None)
lt.locate(infile)
if locroot is None:
self.PyLoTprefs()
def check4Loc(self):

View File

@ -6,13 +6,15 @@ from __future__ import print_function
import argparse
import glob
import string
import os
import numpy as np
from pylot.core.analysis.magnitude import M0Mw
from pylot.core.io.data import Data
from pylot.core.io.inputs import AutoPickParameter
from pylot.core.loc.nll import *
import pylot.core.loc.nll as nll
import pylot.core.loc.hsat as hsat
from pylot.core.pick.autopick import autopickevent, iteratepicker
from pylot.core.util.structure import DATASTRUCTURE
from pylot.core.util.version import get_git_version as _getVersionString
@ -112,16 +114,17 @@ def autoPyLoT(inputfile):
# locating
if locflag == 1:
# write phases to NLLoc-phase file
picksExport(picks, 'NLLoc', phasefile)
nll.export(picks, phasefile)
# For locating the event the NLLoc-control file has to be modified!
evID = event[string.rfind(event, "/") + 1: len(events) - 1]
nllocout = '%s_%s' % (evID, nllocoutpatter)
# create comment line for NLLoc-control file
modifyInputFile(ctrf, nllocroot, nllocout, phasef, ttpat)
nll.modifyInputFile(ctrf, nllocroot, nllocout, phasef,
ttpat)
# locate the event
locate(nlloccall, ctrfile)
nll.locate(ctrfile)
# !iterative picking if traces remained unpicked or occupied with bad picks!
# get theoretical onset times for picks with weights >= 4
@ -162,11 +165,11 @@ def autoPyLoT(inputfile):
print("autoPyLoT: Starting with iteration No. %d ..." % nlloccounter)
picks = iteratepicker(wfdat, nllocfile, picks, badpicks, parameter)
# write phases to NLLoc-phase file
picksExport(picks, 'NLLoc', phasefile)
nll.export(picks, phasefile)
# remove actual NLLoc-location file to keep only the last
os.remove(nllocfile)
# locate the event
locate(nlloccall, ctrfile)
nll.locate(ctrfile)
print("autoPyLoT: Iteration No. %d finished." % nlloccounter)
# get updated NLLoc-location file
nllocfile = max(glob.glob(locsearch), key=os.path.getctime)
@ -201,13 +204,13 @@ def autoPyLoT(inputfile):
hypo71file = '%s/autoPyLoT_HYPO71.pha' % event
if hasattr(finalpicks, 'getpicdic'):
if finalpicks.getpicdic() is not None:
writephases(finalpicks.getpicdic(), 'HYPO71', hypo71file)
hsat.export(finalpicks.getpicdic(), hypo71file)
data.applyEVTData(finalpicks.getpicdic())
else:
writephases(picks, 'HYPO71', hypo71file)
hsat.export(picks, hypo71file)
data.applyEVTData(picks)
else:
writephases(picks, 'HYPO71', hypo71file)
hsat.export(picks, hypo71file)
data.applyEVTData(picks)
fnqml = '%s/autoPyLoT' % event
data.exportEvent(fnqml)
@ -235,15 +238,15 @@ def autoPyLoT(inputfile):
# locating
if locflag == 1:
# write phases to NLLoc-phase file
picksExport(picks, 'NLLoc', phasefile)
nll.export(picks, phasefile)
# For locating the event the NLLoc-control file has to be modified!
nllocout = '%s_%s' % (parameter.get('eventID'), nllocoutpatter)
# create comment line for NLLoc-control file
modifyInputFile(ctrf, nllocroot, nllocout, phasef, ttpat)
nll.modifyInputFile(ctrf, nllocroot, nllocout, phasef, ttpat)
# locate the event
locate(nlloccall, ctrfile)
nll.locate(ctrfile)
# !iterative picking if traces remained unpicked or occupied with bad picks!
# get theoretical onset times for picks with weights >= 4
# in order to reprocess them using smaller time windows around theoretical onset
@ -283,11 +286,11 @@ def autoPyLoT(inputfile):
print("autoPyLoT: Starting with iteration No. %d ..." % nlloccounter)
picks = iteratepicker(wfdat, nllocfile, picks, badpicks, parameter)
# write phases to NLLoc-phase file
picksExport(picks, 'NLLoc', phasefile)
nll.export(picks, phasefile)
# remove actual NLLoc-location file to keep only the last
os.remove(nllocfile)
# locate the event
locate(nlloccall, ctrfile)
nll.locate(ctrfile)
print("autoPyLoT: Iteration No. %d finished." % nlloccounter)
# get updated NLLoc-location file
nllocfile = max(glob.glob(locsearch), key=os.path.getctime)
@ -322,13 +325,13 @@ def autoPyLoT(inputfile):
hypo71file = '%s/%s/autoPyLoT_HYPO71.pha' % (datapath, parameter.get('eventID'))
if hasattr(finalpicks, 'getpicdic'):
if finalpicks.getpicdic() is not None:
writephases(finalpicks.getpicdic(), 'HYPO71', hypo71file)
hsat.export(finalpicks.getpicdic(), hypo71file)
data.applyEVTData(finalpicks.getpicdic())
else:
writephases(picks, 'HYPO71', hypo71file)
hsat.export(picks, hypo71file)
data.applyEVTData(picks)
else:
writephases(picks, 'HYPO71', hypo71file)
hsat.export(picks, hypo71file)
data.applyEVTData(picks)
fnqml = '%s/%s/autoPyLoT' % (datapath, parameter.get('eventID'))
data.exportEvent(fnqml)

View File

@ -1,2 +1,21 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from pylot.core.io.phases import writephases
from pylot.core.util.version import get_git_version as _getVersionString
__version__ = _getVersionString()
def export(picks, fnout):
'''
Take <picks> dictionary and exports picking data to a NLLOC-obs
<phasefile> without creating an ObsPy event object.
:param picks: picking data dictionary
:type picks: dict
:param fnout: complete path to the exporting obs file
:type fnout: str
'''
# write phases to NLLoc-phase file
writephases(picks, 'HYPO71', fnout)

View File

@ -3,14 +3,17 @@
import subprocess
import os
from obspy import read_events
from pylot.core.io.phases import writephases
from pylot.core.util.utils import getPatternLine, runProgram
from pylot.core.util.utils import getPatternLine, runProgram, which
from pylot.core.util.version import get_git_version as _getVersionString
__version__ = _getVersionString()
class NLLocError(EnvironmentError):
pass
def picksExport(picks, locrt, phasefile):
def export(picks, fnout):
'''
Take <picks> dictionary and exports picking data to a NLLOC-obs
<phasefile> without creating an ObsPy event object.
@ -18,14 +21,11 @@ def picksExport(picks, locrt, phasefile):
:param picks: picking data dictionary
:type picks: dict
:param locrt: choose location routine
:type locrt: str
:param phasefile: complete path to the exporting obs file
:type phasefile: str
:param fnout: complete path to the exporting obs file
:type fnout: str
'''
# write phases to NLLoc-phase file
writephases(picks, locrt, phasefile)
writephases(picks, 'NLLoc', fnout)
def modifyInputFile(ctrfn, root, nllocoutn, phasefn, tttn):
@ -66,24 +66,27 @@ def modifyInputFile(ctrfn, root, nllocoutn, phasefn, tttn):
nllfile.close()
def locate(call, fnin):
'''
Takes paths to NLLoc executable <call> and input parameter file <fnin>
and starts the location calculation.
def locate(fnin):
"""
takes an external program name
:param fnin:
:return:
"""
:param call: full path to NLLoc executable
:type call: str
exe_path = which('NLLoc')
if exe_path is None:
raise NLLocError('NonLinLoc executable not found; check your '
'environment variables')
:param fnin: full path to input parameter file
:type fnin: str
'''
# locate the event
runProgram(call, fnin)
# locate the event utilizing external NonLinLoc installation
try:
runProgram(exe_path, fnin)
except subprocess.CalledProcessError as e:
print(e.output)
def readLocation(fn):
pass
return read_events(fn)
if __name__ == '__main__':

View File

@ -277,6 +277,15 @@ def getPatternLine(fn, pattern):
return None
def is_executable(fn):
"""
takes a filename and returns True if the file is executable on the system
and False otherwise
:param fn: path to the file to be tested
:return: True or False
"""
return os.path.isfile(fn) and os.access(fn, os.X_OK)
def isSorted(iterable):
'''
@ -393,9 +402,36 @@ def runProgram(cmd, parameter=None):
cmd.strip()
cmd += ' %s 2>&1' % parameter
output = subprocess.check_output('{} | tee /dev/stderr'.format(cmd),
shell=True)
subprocess.check_output('{} | tee /dev/stderr'.format(cmd), shell=True)
def which(program):
"""
takes a program name and returns the full path to the executable or None
found on: http://stackoverflow.com/questions/377017/test-if-executable-exists-in-python
:param program: name of the desired external program
:return: full path of the executable file
"""
def is_exe(fpath):
return os.path.exists(fpath) and os.access(fpath, os.X_OK)
def ext_candidates(fpath):
yield fpath
for ext in os.environ.get("PATHEXT", "").split(os.pathsep):
yield fpath + ext
fpath, fname = os.path.split(program)
if fpath:
if is_exe(program):
return program
else:
for path in os.environ["PATH"].split(os.pathsep):
exe_file = os.path.join(path, program)
for candidate in ext_candidates(exe_file):
if is_exe(candidate):
return candidate
return None
if __name__ == "__main__":
import doctest