navigation bar
API documentation

Application Programming Interface

The ground motion database is equipped with a web interface, which is what you are currently viewing, and an application programming interface (API) that exposes the data to HTTP requests. The web interface is useful if you quickly want to look at data in a tabular form, but it is cumbersome if you want to develop an end-to-end workflow. Copying HTML tables is kind of a pain, and it takes time to render those tables. The API exposes the underlying data through HTTP requests, providing the data in JSON format so you can work with it more directly and incoporate it into a workflow.

Users can access the API by authenticating, retrieving an authorization token, and submitting a request that includes that token in the request header. The authorization token is required because we want to make sure that we return the information only to people who have an active account, thereby avoiding attacks by unauthorized users, and that users have access to data consistent with their role (user, modeler, admin).

Authentication / Authorization

The first step in using the API is to authenticate and retrieve an authorization token. To authenticate, submit an API request to the following uniform resource locator (URL)

https://www.gmdatabase.org/users/login

You must use the Basic Auth method to include your username and password with the HTTP request. Programs specifically designed for making HTTP requests include curl, postman, and others. In this documentation, we provide examples using the Python requests package. The example below authenticates the user using HTTP Basic Auth and retrieves an authorization token. We suggest never hard-coding your username and password into a script. You might accidentally share the script, and divulge your authentication credentials. We have used the getpass package here facilitate password entry. The token is valid for 120 minutes, after which you will need to re-authenticate and retrieve a new token. It is important not to share your token with others.


                    import requests
                    from requests.auth import HTTPBasicAuth
                    import getpass
                    import json

                    username = input('username: ')
                    password = getpass.getpass('password: ')
                    url = 'http://www.gmdatabase.org/users/login'
                    headers = {"User-Agent":"XY", "Accept":"application/json"}
                    r = requests.get(url, headers=headers, auth=HTTPBasicAuth(username,password))
                    token = json.loads(r.text)['token']

                

Submitting a Request

After retrieving an authorization token, you can now submit a GET request to the desired URL, and the requested data will be returned in JSON format. Two headers must be included in your request. The "Accept":"application/json" header is used to identify that JSON data is being requested. The "Authorization":"Bearer {}".format(token) header contains the authorization token that was provided after successful authentication.
An example request is provided in the Python script below. In this case, the API requests 50 entries from the events table. The URL structure and available query string parameters for customizing the request are discussed in the next section.

                    
                        import pandas as pd
                        import requests
                        import json

                        headers = {"Accept":"application/json","Authorization": "Bearer {}".format(token)}
                        url = 'http://www.gmdatabase.org/events?limit=50'
                        r= requests.get(url, headers=headers)
                        df = pd.DataFrame.from_dict(json.loads(r.text))

                    
                

URL structure

Requests are submitted to a uniform resource locator (URL) that contains the following components:

url = base_url/endpoint?query_string


base_url

The base_url is the webpage containing the API. In this case, it is

https://www.gmdatabase.org
Submitting a request without also specifying an endpoint will produce an error. The endpoint is necessary so we know which query to run to retrieve the data you have requested. The query string is optional, and can be used to customize your queries.

endpoint

An endpoint is a location where an API receives requests about a resource. The endpoints correspond to table names converted to pluralized lower camel case with underscores removed. All of the tables except for the user table are accessible. The user table is not accessible for privacy purposes. Furthermore, a flatfile endpoint is also available. The flatfile endpoint combines tables together in a manner similar to the NGA flatfiles. Tables included in the flatfile endpoint are indicated with a checkmark in the table below. The flatfile can be generated for any pseudospectral acceleration component, or the effective amplitude spectra (i.e., smoothed Fourier spectra), but not more than one in a single query because the resulting table would be large and unwieldy. The psa_rotd50 table is returned in the flatfile endpoint by default.

table name endpoint table included in flatfile
aftershock_mainshock aftershockMainshocks
basin_model basinsModels
basin_site basinsSites
citation citations
collections collections
collection_motion collectionsMotions
eas eas (✔)
event events
event_eqid eventEqids
event_geometry eventsGeometries
event_type eventTypes
finite_fault finiteFaults
finite_fault_kinematic_parameter finiteFaultKinematicParameters
finite_fault_segment finiteFaultSegments
intensity_measure intensityMeasures
motion motions
network networks
path paths
psa_h1 psaH1 (✔)
psa_h2 psaH2 (✔)
psa_rotd0 psaRotd0 (✔)
psa_rotd50 psaRotd50 (✔)
psa_rotd100 psaRot100 (✔)
psa_v psaV (✔)
site sites
site_geometry geometriesSites
station stations
station_ssn stationSsns
time_series_data timeSeriesData
time_series_metadata timeSeriesMetadata
version versions
version_time_series_metadata timeSeriesMetadataVersions
vs30_citation vs30Citations
vs30_code vs30Codes
z_code zCodes

query_string

A query string is a list of parameters used to customize the request. If a query string is not specified, default values will be returned. A list of valid query string parameters and their default values is below.

query string options for table endpoints

query string parameter default description
limit 20 number of records per page
sort primary_key field to sort by
direction asc sort direction (asc or desc)
page 1 page number
role user Role for users with elevated permissions (i.e., modeler or admin).
where - Used to filter selected data (e.g., ?where=pga_rotd50>0.1+AND+pga_rotd50<0.2
select all fields comma separated list of fields to query

First query string parameter

The first query string parameter comes after a question mark (?) at the end of the url. For example: https://www.gmdatabase.org/intensityMeasures?page=1.

Second and subsequent query string parameters

The 2nd and subsequent query string parameters are separated by an ampersand (&). For example: https://www.gmdatabase.org/?page=1&limit=50.

Filtering data using "where" statements

Data can be filtered using the "where" query string parameter. The where statement is used to create a SQL statement excerpt "WHERE [field] [operator] [value]". For example, to retrieve events with hypocenter latitude larger than 40.0°, the following should query string parameter would be ?where=hypocenter_latitude>40.0 This query string parameter will be translated to the SQL excerpt WHERE hypocenter_latitude > 40.0 when the query is executed. Available operators depend on data type as described in the table below.
type operators
numeric (e.g., float, int) >, >=, =, <=, <, IN, NOT IN
string >, >=, =, <=, <, LIKE, NOT LIKE, IN, NOT IN
boolean true, false
The query string parameters are reasonably intuitive, perhaps with the exception of applying multiple conditions, the equal sign (=) for float fields, IN and NOT IN, and LIKE and NOT LIKE, which are discussed in more detail below.

Applying Multiple Conditions

Multiple operators can be applied using an "AND" or "OR" separator. When constructing the query string, spaces should be replaced by the plus sign "+", as shown below. For example,
"?where=hypocenter_latitude<40+OR+hypocenter_longitude>-118.0"
Furthermore, conditions can be grouped using parenthesis like this:
"?where=(hypocenter_latitude>40.0+AND+hypocenter_latitude<42.0)+OR+(hypocenter_longitude>-118.0+AND+hypocenter_longitude<-117.0)"


Equal sign (=) for float fields

You should avoid using the equal sign (=) operator for float-valued fields because floats are stored in an approximate manner. For example, if a strike appears as 196.2 in the database, you should recognize that its value may not be precisely 196.2 because we format outputs for presentation purposes. Therefore, performing a query "WHERE strike=196.2" is unlikely to return the entry you're looking for. You could use a combination of greater than (>) and less than (<) operators to search for a narrow range instead. It is fine to use the equal sign (=) operator for integer, string, and decimal fields. Decimal fields are used, for example, for latitude and longitude data such that all of the fields contain 5 decimals in the database, and can be compared using the equal operator. The schema page lists the type for each field. Strings are "varchar", integers are "int(N)" where N is the bit depth, floats are "float(N)", and decimal format is denoted "float(M,L)", where M is the total number of characters, and L is the number after the decimal point.


LIKE and NOT LIKE

The LIKE operator is used to search for a specified pattern in a string.
The NOT LIKE operator returns the complementary set of entries excluded by the LIKE operator.
Two wildcards are often used with LIKE and NOT LIKE statements:
  • The asterisk (*) is a placeholder for an arbitrary number of characters
  • The underscore (_) is a placeholder for a single character
Consider the examples below in which we search on a field called "event_name". Note that the wildcard character in the SQL query is the percent sign '%'. We use * instead of % in the query string because the percent sign is used in URL encoding and would result in potential errors.
Query String SQL Statement Excerpt Description
where=event_name+LIKE+v* WHERE event_name LIKE 'v%' Finds any value that starts with "v"
where=event_name+NOT+LIKE+v* WHERE event_name NOT LIKE 'v%' Finds any value that does not start with "v"
where=event_name+LIKE+*v WHERE event_name LIKE '%v' Finds any value that ends with "v"
where=event_name+LIKE+*v* WHERE event_name LIKE '%v%' Finds any value that contains "v" in any position
where=event_name+LIKE+_v WHERE event_name LIKE '_v' Finds any value that contains "v" in the 2nd position
where=event_name+LIKE+v_* WHERE event_name LIKE 'v_%' Finds any value that starts with "v" and is at least 2 characters in length
where=event_name+LIKE+v__* WHERE event_name LIKE 'v__%' Finds any value that starts with "v" and is at least 3 characters in length
where=event_name+LIKE+v*n WHERE event_name LIKE 'v%n' Finds any value that starts with "v" and ends with "n"


IN and NOT IN Operators

The IN operator is used to select values from an array of options, and is shorthand for multiple OR conditions.
The NOT IN operator returns the complementary set of entries excluded by the IN operator.
Since the IN and NOT IN operators imply an equal sign (=), you should not use them for float fields for the reasons explained above.
Consider the examples below in which we search on an integer field called "event_id".
Note that the value array is a comma-separated list contained within parenthesis.
Query String SQL Statement Excerpt Description
where=event_id+IN+(1,2,3) WHERE event_id IN (1, 2, 3) Finds entries with id = 1, 2, or 3
where=event_id+NOT+IN+(1,2,3) WHERE event_id NOT IN (1, 2, 3) Finds entries with id ≠ 1, 2, or 3

flatfile endpoint

The database is organized into many different tables to ensure data integrity, but users often want to query data from multiple tables and join them together in a way that results in repetition of certain fields. For example, users might want to retrieve all event, site, and ground motion data together in the same table, where event and site information is repeated for each ground motion. The flatfile endpoint provides the logic to join together tables that we believe will serve the needs of most users, and that match the published flatfiles.

The tables listed below are queried and presented in a "flattened" form via the flatfile endpoint. If users do not specify a select, sort, or where key that identifies the ground motion component, the Rotd50 component is used by default. If users specify a select field, the selected fields are returned along with the primary keys for all tables involved in the query. Other components can be queried instead by adding a "component" query string parameter to explicitly specify the component (e.g., "?component=PsaRotd100"), or by simply including a separate query string parameter that uses data from the table you wish to query (e.g., "?sort=psa_rotd100_0p010). In the latter case, the table(s) will be inferred from the provided query strings. Note that you can request data from multiple psa tables and the eas table. However, you should recognize that doing so may result in an amount of data that is too large to be provided by the server, or may result in slow response times.

Tables included in flatfile endpoint query

table
motion
time_series_metadata
intensity_measure
psa_h1
psa_h2
psa_rotd0
psa_rotd50
psa_rotd100
psa_v
eas
event
event_type
finite_fault
finite_fault_kinematic_parameter
station
site
station
network
path
event_eqid
station_ssn

Example Queries

Below are a few example queries that show how to specify endpoints and query string parameters. This is not an exhaustive list, but just a few examples.

example 1

Query 20 events sorted in descending order by magnitude:
https://www.gmdatabase.org/events?limit=20&sort=magnitude&direction=desc

example 2

Query 20 events sorted in descending order by magnitude where magnitude is between 5 and 6
https://www.gmdatabase.org/events?limit=20&sort=magnitude&direction=desc&where=(magnitude>5.0+AND+magnitude<6.0)

example 3

Now add a where statement to exclude event_id values of 3, 5, and 10
https://gmdatabase.org/events?where=magnitude>5.0+AND+magnitude<6.0+AND+event_id+NOT+IN+(3,5,10)

example 4

Query 20 results for the rotd50 response_spectra component sorted in descending order by spectral acceleration at 0.1 second period:
https://www.gmdatabase.org/psaRotd50?limit=20&sort=psa_rotd50_0p100&direction=desc

Possible Problems

The database is pretty complicated and there may be bugs in the code. If so, please contact us. Here is a list of a few things to watch out for.

Requesting huge amounts of data

There is a limit to the amount of data that can be returned by the server. You will receive an error message if you request an amount of data that exceeds the limit. In that case, you will need to retrieve the data in batches using the "page" query string parameter.

role

When users register for an account, they are assigned a "user" role. Additional roles are reserved for administrators and model developers. The user role has restricted access to the following tables: [motions, time_series_metadata, intensity_measures, response_spectra, fourier_spectra], and full access to all other tables. This is because the database team needs to perform QA / QC on the motions before making them publicly available, which is handled through the public_motion field in the motions table.

Floats/Doubles and =, IN, NOT IN

Using an equal sign or the IN or NOT IN operators to filter float-valued fields may return unexpected results due to floating point precision issues. For example, 0.1 might not actually be 0.1000000000, but something like 0.10000000015. You might need to restrict float fields within a narrow range instead. However, there are some exceptions. The database permits storing numerical data in decimal format, which have a fixed structure. For example, latitudes and longitudes are stored using the SQL float(8,5) format. For those cases, it is OK to use the equal sign. You can find the data types for each field on the schema page.

Filters that return no data

You might get an empty table back if you request data that doesn't exist. For example, asking for events with magnitude larger than 10 is going to fail. Also, doing something like rotd50<0.1&rotd50>0.2 will return no data because it's impossible for a number to be smaller than 0.1 and larger than 0.2.