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

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

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