shakemap.utils.amps

class shakemap.utils.amps.AmplitudeHandler(install_path, data_path)[source]

Bases: object

Store and associate strong motion peak amplitudes with earthquake events.

Instantiate amplitude handler with ShakeMap profile paths.

commit()[source]

Commit any operations to the database.

insertEvent(event, update=False)[source]

Insert an event into the database.

A directory with name of event[‘id’] should exist in data_path.

Parameters:
  • event (dict) –

    Dictionary containing fields: - id: Event ID (i.e., us2008abcd). - netid: Network code (i.e., us). - network: Network name (i.e., “USGS Network”). - time: Origin time in UTC (datetime). - lat: Origin latitude (dd). - lon: Origin longitude (dd). - depth: Origin depth (km). - mag: Earthquake magnitude. - locstring: Location string (i.e. ‘2 mi SE of Reno’) - repeats: A list of repeat times (optional) - lastrun: Timestamp of the last run of the event.

    (optional)

  • update (bool) – Update an existing event with new info (True) or insert a new event (False)

Returns:

Nothing.

Return type:

nothing

getEvent(eventid)[source]

Return the event parameters for the specified event.

Parameters:

eventid (str) – The id of the event to query

Returns:

A dictionary of the columns of the table and their values for the the event; a None is returned if the event is not in the database.

Return type:

dictionary

deleteEvent(eventid)[source]

Delete the event from the database.

Parameters:

eventid (str) – The id of the event to delete

Returns:

Nothing.

Return type:

nothing

getRepeats()[source]

Return all the rows from the event table where the ‘repeats’ column is not NULL.

Parameters:

none

Returns:

List of tuples of (eventid, origin_time, [repeats]).

Return type:

(list)

associateAll(pretty_print=False)[source]

Associate peak ground motions with appropriate events, write station XML to file system.

Ground motion records associated with events will be deleted from the database.

Parameters:

pretty_print (bool) – Writes more human-readable XML, but is slower and writes larger files. False by default.

Returns:

The event IDs of the events for which associated data were found.

Return type:

list

associateOne(eventid, pretty_print=False)[source]

Associate peak ground motions with the specified event, write station XML to file system.

Ground motion records associated with events will be deleted from the database.

Parameters:
  • eventid (str) – The event ID of the event to associate

  • pretty_print (bool) – Writes more human-readable XML, but is slower and writes larger files. False by default.

Returns:

The number of amps associated with the specified event. -1 is returned if the event is not found in the database.

Return type:

int

associate(eqtime, eqlat, eqlon)[source]

Find peak ground motion records associated with input event info.

Ground motion records associated with input event are deleted from the database. Note that in the case of duplicate stations, the amps from only one will be used, any others will be deleted from the database.

Parameters:
  • eqtime (int) – Unix timestamp of earthquake origin.

  • eqlat (float) – Latitude of earthquake origin.

  • eqlon (float) – Longitude of earthquake origin.

Returns:

A list of amps associated with the event. Each row in the list has the following columns:

  • code: Station code

  • channel: Channel (HHE, HHN, etc.)

  • imt: Intensity measure type (pga, pgv, etc.)

  • value: IMT value.

  • lat: Station latitude.

  • lon: Station longitude.

  • netid: Station contributing network.

  • name: String describing station name.

  • distance: Distance (km) from station to origin.

  • flag: Value will be 0.

  • loccode: The location code of the instrument.

Return type:

list

writeXML(data_list, eventid, pretty_print=False)[source]

Write the list of tuples as an XML file in the event’s current directory.

Parameters:
  • data_list (list) –

    A list of tuples with the following elements:

    • station code

    • channel

    • imt

    • imt value

    • station latitude

    • station longitude

    • station’s network id

    • station’s name string

    • distance from station to origin

    • imt flag

    • channel’s location code

  • eventid (str) – The event ID of the event associated with the data.

  • pretty_print (bool) – Whether or not to write the XML in a more human-readable form. If True, the file will be somewhat larger and writing will be somewhat slower.

Returns:

Nothing.

Return type:

nothing

insertAmps(xmlfile)[source]

Insert data from amps file into database.

Parameters:

xmlfile (str) – XML file containing peak ground motion data.

cleanAmps(threshold=30)[source]

Clean out amplitude data that is older than the threshold number of days.

Parameters:

threshold (int) – Maximum age in days of amplitude data in the database.

Returns:

Number of stations deleted.

Return type:

int

cleanEvents(threshold=365)[source]

Clean out event data that is older than the threshold number of days.

Parameters:

threshold (int) – Maximum age in days of events in the database.

Returns:

Number of events deleted.

Return type:

int

getStats()[source]

Get summary statistics about the database.

Returns:

Fields:

  • events Number of events in database.

  • stations Number of stations in database.

  • channels Number of unique channels in database.

  • pgms Number of unique pgms in database.

  • event_min: Datetime of earliest event in database.

  • event_max: Datetime of most recent event in database.

  • station_min: Datetime of earliest amplitude data in

    database.

  • station_max: Datetime of most recent amplitude data

    in database.

Return type:

dict

shakemap.utils.amps.dt_to_timestamp(dt)[source]
shakemap.utils.amps.timestr_to_timestamp(timestr)[source]
shakemap.utils.amps.prettify(elem)[source]

Return a pretty-printed XML string.