Skip to content
Snippets Groups Projects
Commit 80388922 authored by Powers, Peter M.'s avatar Powers, Peter M.
Browse files

removed stale python scripts

parent 8c577e1e
No related branches found
No related tags found
2 merge requests!593Production Release | nshm-haz,!591Code review 479
# Using nshmp-haz with Python
There are a few different packages that will allow you to call Java code from Python.
This example uses jpype. It also uses NumPy to simplify working with the arrays
returned by the ground motion model calculators.
## Requirements
1. A [build](https://github.com/usgs/nshmp-haz/wiki/building-&-running) of nshmp-haz.
2. [jpype](http://jpype.readthedocs.io/en/latest/install.html)
3. [NumPy](http://www.numpy.org/)
dip,
0,
2.5,
5,
7.5,
10,
12.5,
15,
17.5,
20,
22.5,
25,
27.5,
30,
32.5,
35,
37.5,
40,
42.5,
45,
47.5,
50,
52.5,
55,
57.5,
60,
62.5,
65,
67.5,
70,
72.5,
75,
77.5,
80,
82.5,
85,
87.5,
90,
\ No newline at end of file
## nshmp-haz Ground Motion Model (GMM) batch processing example script
import requests
## Read CSV file of GMM inputs
#
# Each column of the CSV file is a GMM input parameter with the
# first row dictating that GMM input field.
#
# Example CSV to change only dip:
# dip,
# 0.0,
# 45.0,
# 90.0,
#
# For a full list of GMM input paramters see:
# http://usgs.github.io/nshmp-haz/javadoc/gov/usgs/earthquake/nshmp/gmm/GmmInput.html
#
# If 'null' is supplied as a value or a GMM input field and values are
# not given, the default values are used:
# http://usgs.github.io/nshmp-haz/javadoc/gov/usgs/earthquake/nshmp/gmm/GmmInput.Builder.html#withDefaults--
file = open('gmm-inputs.csv', 'r')
inputs = file.read()
file.close()
## URL to POST the CSV file of GMM inputs
#
# Must update the URL host if not on localhost.
#
# The GMMs must be specified in the URL query string.
#
# All GMM services are available to call for batch processing.
host = 'http://localhost:8080'
service = '/nshmp-haz/gmm/spectra'
url = host + service
query = { 'gmm': [ 'AB_06_PRIME', 'CAMPBELL_03', 'FRANKEL_96' ] }
## Conduct HTTP POST Request
#
# Conduct a HTTP POST request, sending the CSV file of GMM inputs.
#
# The POST response is loaded into a object
# following the returned JSON structure.
svcResponse = requests.post(url, data = inputs, params = query).json()
## Check Response
#
# Check to see if the response returned an error and check
# to see if the field 'response' exists in the object.
#
# If the URL does not contain a query string of GMMs the response
# returned will be the service usage.
if svcResponse['status'] == 'error' and ~hasattr(svcResponse, 'response'):
exit()
## Retreive the data
#
# Loop through each response spectrum response and obtain the means
# and sigmas.
for response in svcResponse['response']:
# Request structure contains the GMMs and GMM input parameters used
request = response['request']
# The GMMs used for the calculation
gmms = request['gmms']
# The GMM input parameters used for the calculation
gmmInput = request['input']
# Get the means
for means in response['means']['data']:
data = means['data']
xMeans = data['xs']
yMeans = data['ys']
# Get the sigmas
for sigmas in response['sigmas']['data']:
data = sigmas['data']
xSigmas = data['xs']
ySigmas = data['ys']
#!/usr/bin/env python
## nshmp-haz Ground Motion Model (GMM) calculator example script
# =========================================================================
# This script provides instruction on how to access ground motion models
# (GMMs) implemented in the nshmp-haz library.
# =========================================================================
from jpype import *
import numpy as np
# Specify path to nshmp-haz library:
classpath = '/path/to/repository/nshmp-haz-master/build/libs/nshmp-haz.jar'
# Start Java Virtual Machine and add nshmp-haz to classpath:
startJVM(getDefaultJVMPath(), "-ea",
"-Djava.class.path={0}".format(classpath))
# Import packages:
nshmp = JPackage("gov").usgs.earthquake.nshmp.etc
# =========================================================================
# Single ground motion calcuation:
# Initialize calculator:
hazMat = nshmp.HazMat.init(classpath)
# Note that hazMat is stateless and reusable and should therefore be
# initialized only once in a script if doing many calculations.
# Set up a GMM input parameter object. These data are a source and site
# parameterization that will satisfy all currently implemented Gmms. Note
# that not all models will necessarily use all parameters.
gmmparams = nshmp.GmmParams()
gmmparams.Mw = 6.5
gmmparams.rJB = 5.0
gmmparams.rRup = 5.1
gmmparams.rX = 5.1
gmmparams.dip = 90.0
gmmparams.width = 10.0
gmmparams.zTop = 1.0
gmmparams.zHyp = 6.0
gmmparams.rake = 0.0
gmmparams.vs30 = 760.
gmmparams.vsInf = True
gmmparams.z2p5 = np.nan
gmmparams.z1p0 = np.nan
# Specify a ground motion model. GMM identifiers:
# http://usgs.github.io/nshmp-haz/javadoc/gov/usgs/earthquake/nshmp/gmm/Gmm.html
gmm = 'ASK_14';
# Specify an intensity measure type (IMT). IMT identifiers:
# http://usgs.github.io/nshmp-haz/javadoc/gov/usgs/earthquake/nshmp/gmm/Imt.html
imt = 'PGA';
# Do a calculation. The MatUtil.calc(gmm, imt, gmmInput) method returns an
# array of [ln(median ground motion), sigma]
ln_med_gm, sigma = hazMat.gmmMean(gmm, imt, gmmparams)
print('ln(median ground motion), sigma:')
print(ln_med_gm, sigma)
# =========================================================================
# Determinisitic response spectrum calculation:
# The object returned by the MatUtil.spectrum(gmm, gmmInput) method may
# be converted to NumPy arrays.
# The returned HazMat Spectrum object is not iterable, so do this array
# by array.
spectrumResult = hazMat.gmmSpectrum(gmm, gmmparams)
pds = np.array(spectrumResult.periods)
means = np.array(spectrumResult.means)
sigmas = np.array(spectrumResult.sigmas)
print('period, mean, sigma:')
for i in range(len(pds)):
print(pds[i], means[i], sigmas[i])
# =========================================================================
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment