Skip to main content

A convenient interface to the USGS NWIS Instantaneous Values (IV) REST Service API.

Project description

OWPHydroTools :: NWIS Client

This subpackage implements an interface to the USGS NWIS Instantaneous Values Web Service. The primary use for this tool is to populate pandas.Dataframe objects with USGS streamflow data. See the NWIS Client Documentation for a complete list and description of the currently available methods. To report bugs or request new features, submit an issue through the OWPHydroTools Issue Tracker on GitHub.

Installation

In accordance with the python community, we support and advise the usage of virtual environments in any workflow using python. In the following installation guide, we use python's built-in venv module to create a virtual environment in which the tool will be installed. Note this is just personal preference, any python virtual environment manager should work just fine (conda, pipenv, etc. ).

# Create and activate python environment, requires python >= 3.8
$ python3 -m venv venv
$ source venv/bin/activate
$ python3 -m pip install --upgrade pip

# Install nwis_client
$ python3 -m pip install hydrotools.nwis_client

Usage

The following example demonstrates how one might use hydrotools.nwis_client to retrieve USGS streamflow observations.

Code

# Import the NWIS IV Client
from hydrotools.nwis_client.iv import IVDataService

# Retrieve data from a single site
service = IVDataService(
    value_time_label="value_time"
)
observations_data = service.get(
    sites='01646500',
    startDT='2019-08-01',
    endDT='2020-08-01'
    )

# Look at the data
print(observations_data.head())

Output

           value_date variable_name usgs_site_code measurement_unit   value qualifiers  series
0 2019-08-01 04:00:00    streamflow       01646500            ft3/s  4170.0        [A]       0
1 2019-08-01 04:15:00    streamflow       01646500            ft3/s  4170.0        [A]       0
2 2019-08-01 04:30:00    streamflow       01646500            ft3/s  4170.0        [A]       0
3 2019-08-01 04:45:00    streamflow       01646500            ft3/s  4170.0        [A]       0
4 2019-08-01 05:00:00    streamflow       01646500            ft3/s  4170.0        [A]       0

Command Line Interface (CLI)

The hydrotools.nwis_client package includes a command-line utility.

This example demonstrates calling the help page:

$ nwis-client --help
Usage: nwis-client [OPTIONS] [SITES]...

  Retrieve data from the USGS IV Web Service API and write in CSV format.

  Example:

  nwis-client 01013500 02146470

Options:
  -o, --output FILENAME       Output file path
  -s, --startDT TIMESTAMP     Start datetime
  -e, --endDT TIMESTAMP       End datetime
  -p, --parameterCd TEXT      Parameter code
  --comments / --no-comments  Enable/disable comments in output, enabled by
                              default
  --header / --no-header      Enable/disable header in output, enabled by
                              default
  --help                      Show this message and exit.

This example retrieves the last discharge value from two sites:

$ nwis-client 01013500 02146470
# USGS IV Service Data
# 
# value_date: Datetime of measurement (UTC) (character string)
# variable: USGS variable name (character string)
# usgs_site_code: USGS Gage Site Code (character string)
# measurement_unit: Units of measurement (character string)
# value: Measurement value (float)
# qualifiers: Qualifier string (character string)
# series: Series number in case multiple time series are returned (integer)
# 
# Generated at 2022-03-04 21:56:30.296051+00:00
# nwis_client version: 3.2.0
# Source code: https://github.com/NOAA-OWP/hydrotools
# 
value_time,variable_name,usgs_site_code,measurement_unit,value,qualifiers,series
2022-03-04 21:45:00,streamflow,01013500,ft3/s,-999999.00,"['P', 'Ice']",0
2022-03-04 21:50:00,streamflow,02146470,ft3/s,1.04,['P'],0

This example retrieves stage data from two sites for a specific time period:

$ nwis-client -p 00065 -s 2021-06-01T00:00 -e 2021-06-01T01:00 01013500 02146470
# USGS IV Service Data
# 
# value_date: Datetime of measurement (UTC) (character string)
# variable: USGS variable name (character string)
# usgs_site_code: USGS Gage Site Code (character string)
# measurement_unit: Units of measurement (character string)
# value: Measurement value (float)
# qualifiers: Qualifier string (character string)
# series: Series number in case multiple time series are returned (integer)
# 
# Generated at 2022-03-04 21:59:02.508468+00:00
# nwis_client version: 3.2.0
# Source code: https://github.com/NOAA-OWP/hydrotools
# 
value_time,variable_name,usgs_site_code,measurement_unit,value,qualifiers,series
2021-05-31 23:00:00,gage height,01013500,ft,4.28,['A'],0
2021-05-31 23:15:00,gage height,01013500,ft,4.28,['A'],0
2021-05-31 23:30:00,gage height,01013500,ft,4.28,['A'],0
2021-05-31 23:45:00,gage height,01013500,ft,4.28,['A'],0
2021-06-01 00:00:00,gage height,01013500,ft,4.28,['A'],0
2021-05-31 23:00:00,gage height,02146470,ft,3.14,['A'],0
2021-05-31 23:05:00,gage height,02146470,ft,3.14,['A'],0
2021-05-31 23:10:00,gage height,02146470,ft,3.14,['A'],0
2021-05-31 23:15:00,gage height,02146470,ft,3.14,['A'],0
2021-05-31 23:20:00,gage height,02146470,ft,3.14,['A'],0
2021-05-31 23:25:00,gage height,02146470,ft,3.14,['A'],0
2021-05-31 23:30:00,gage height,02146470,ft,3.14,['A'],0
2021-05-31 23:35:00,gage height,02146470,ft,3.14,['A'],0
2021-05-31 23:40:00,gage height,02146470,ft,3.14,['A'],0
2021-05-31 23:45:00,gage height,02146470,ft,3.14,['A'],0
2021-05-31 23:50:00,gage height,02146470,ft,3.14,['A'],0
2021-05-31 23:55:00,gage height,02146470,ft,3.14,['A'],0
2021-06-01 00:00:00,gage height,02146470,ft,3.14,['A'],0

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

hydrotools_nwis_client-4.1.0.tar.gz (30.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

hydrotools_nwis_client-4.1.0-py3-none-any.whl (24.4 kB view details)

Uploaded Python 3

File details

Details for the file hydrotools_nwis_client-4.1.0.tar.gz.

File metadata

  • Download URL: hydrotools_nwis_client-4.1.0.tar.gz
  • Upload date:
  • Size: 30.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.2

File hashes

Hashes for hydrotools_nwis_client-4.1.0.tar.gz
Algorithm Hash digest
SHA256 0840f46bdef65473ad8dab5d18734a463a6bb1a9240e993acdb3713f201011fc
MD5 8427d8a2014dae4fc76765412f173e76
BLAKE2b-256 ec6920eb2ff2de79f62810a41dc4fe1a75a42a9fe0f1636ee228dce09a5f559f

See more details on using hashes here.

File details

Details for the file hydrotools_nwis_client-4.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for hydrotools_nwis_client-4.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 98f5c13a7dbd9d20a8f098db6f7468fcc5eaa1b92fb14a0ee84b2ff80f072959
MD5 903391b7233bc8bf07633f0749321bd0
BLAKE2b-256 9790df8db72782745732ccbbc5fa756d29dd883bd6dbb6996be7a2c8e0960974

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page