Skip to main content

bmi_nwis

Documentation Status MIT license Binder

bmi_nwis package is an implementation of the Basic Model Interface (BMI) for the USGS NWIS dataset. This package uses the dataretrieval package to download the NWIS dataset and wraps the dataset with BMI for data control and query.

This package is not implemented for people to use but is the key element to convert the NWIS dataset into a data component (pymt_nwis) for the PyMT modeling framework developed by Community Surface Dynamics Modeling System (CSDMS).

Please note that the current bmi_nwis implementation only supports to download time series data for instantaneous values and daily mean values ('iv' or 'dv' service option in the dataretrieval package). If you have any suggestion to improve the current function, please create a github issue here.

Get Started

Install package

Stable Release

The bmi_nwis package and its dependencies can be installed with pip.

$ pip install bmi_nwis
From Source

After downloading the source code, run the following command from top-level folder (the one that contains setup.py) to install bmi_nwis.

$ pip install -e .

Download NWIS Data

You can learn more details from the tutorial notebook and launch binder to run the notebook.

Example 1: use the dataretrieval package to download data
import dataretrieval.nwis as nwis                                 

# get data from NWIS
dataset = nwis.get_record(sites='03339000', service='iv', start='2022-01-01', end='2022-01-03')

# plot data
ax = dataset.plot(y=['00060','00065'], subplots=True, figsize=(10,8), 
                  xlabel='Time', title = 'Time Series Data at USGS Gage 03339000')
ax[0].set_ylabel('Stream flow (ft3/s)')
ax[1].set_ylabel('Gage height (ft)')

ts_plot

Example 2: use BmiNwis class to download data (Demonstration of how to use BMI)
import numpy as np
import cftime
import pandas as pd

from bmi_nwis import BmiNwis


# initiate a data component
data_comp = BmiNwis()
data_comp.initialize('config_file.yaml')

# get variable info
for var_name in  data_comp.get_output_var_names():
    var_unit = data_comp.get_var_units(var_name)
    var_location = data_comp.get_var_location(var_name)
    var_type = data_comp.get_var_type(var_name)
    var_grid = data_comp.get_var_grid(var_name)
    var_itemsize = data_comp.get_var_itemsize(var_name)
    var_nbytes = data_comp.get_var_nbytes(var_name)
    print('variable_name: {} \nvar_unit: {} \nvar_location: {} \nvar_type: {} \nvar_grid: {} \nvar_itemsize: {}' 
            '\nvar_nbytes: {} \n'. format(var_name, var_unit, var_location, var_type, var_grid, var_itemsize, var_nbytes))

# get time info
start_time = data_comp.get_start_time()
end_time = data_comp.get_end_time()
time_step = data_comp.get_time_step()
time_unit = data_comp.get_time_units()
time_steps = int((end_time - start_time)/time_step) + 1
print('start_time:{} \nend_time:{} \ntime_step:{} \ntime_unit:{} \ntime_steps:{} \n'.format(start_time, end_time, time_step, time_unit, time_steps))

# get variable grid info
grid_type = data_comp.get_grid_type(var_grid)
grid_rank = data_comp.get_grid_rank(var_grid) 
grid_node_count = data_comp.get_grid_node_count(var_grid)

site_lon = np.empty(grid_node_count)
data_comp.get_grid_x(var_grid, site_lon)

site_lat = np.empty(grid_node_count)
data_comp.get_grid_y(var_grid, site_lat)

print('grid_type: {} \ngrid_rank: {} \ngrid_node_count: {} \nsite_lon: {} \nsite_lat: {} \n'.format(
    grid_type, grid_rank, grid_node_count, site_lon[0], site_lat[0]))

# initiate dataframe to store data
dataset = pd.DataFrame(columns = ['00060','00065','time'])

for i in range(0, time_steps):
    # get stream flow data
    stream_flow = np.empty(1)
    data_comp.get_value('Stream flow', stream_flow)

    # get gage height data
    gage_height = np.empty(1)
    data_comp.get_value('Height', gage_height)

    # get time data
    cftime_value= data_comp.get_current_time()   
    time = cftime.num2pydate(cftime_value, time_unit)

    # add new row to dataframe
    dataset.loc[len(dataset)]=[stream_flow[0], gage_height[0], time]

    # update to next time step
    data_comp.update()

# convert time to local time
dataset = dataset.set_index('time').tz_localize(tz='UTC').tz_convert(tz='US/Central')

# plot data
ax = dataset.plot(y=['00060','00065'], subplots=True, figsize=(10,8), 
                  xlabel='Time', title = 'Time Series Data at USGS Gage 03339000')
ax[0].set_ylabel('Stream flow (ft3/s)')
ax[1].set_ylabel('Gage height (ft)')

Metadata

Release files for bmi-nwis 0.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Built distribution (wheel)

Table of built distributions (wheels) for bmi-nwis 0.1
File Interpreter ABI Platform Actions
bmi_nwis-0.1-py3-none-any.whl Python 3 none any Details for bmi_nwis-0.1-py3-none-any.whl

Release files / bmi_nwis-0.1-py3-none-any.whl

Download URL bmi_nwis-0.1-py3-none-any.whl
Size 11.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
d2166a745c6badd0e54edab36ba1f7a51bc6139c34ecb5d2116fb95594192995
BLAKE2b-256 checksum
How to use checksums
f74886a096f7719f231903e6478c75fbc301d96bc3590e7288dbd51a9776cc42
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/46.0.0.post20200309 requests-toolbelt/0.9.1 tqdm/4.64.0 CPython/3.7.6

Security for bmi-nwis 0.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.1 This release

1 release file No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page