Skip to main content

Process and access NHDPlus V2 data through NLDI and WaterData web services.

Project description

https://raw.githubusercontent.com/cheginit/hydrodata/develop/docs/_static/pynhd_logo.png

Package

Description

Hydrodata

Access NWIS, HCDN 2009, NLCD, and SSEBop databases

PyGeoOGC

Query data from any ArcGIS RESTful-, WMS-, and WFS-based services

PyGeoUtils

Convert responses from PyGeoOGC’s supported web services to datasets

PyNHD

Access NLDI and WaterData web services for navigating the NHDPlus database

Py3DEP

Access topographic data through the 3D Elevation Program (3DEP) web service

PyDaymet

Access the Daymet database for daily climate data

PyNHD: Navigate and extract NHDPlus database

PyPi Conda Version CodeCov Github Actions Binder

CodeFactor black pre-commit

🚨 This package is under heavy development and breaking changes are likely to happen. 🚨

Features

PyNHD is a part of Hydrodata software stack and provides access to WaterData and NLDI web services. These two web services can be used to navigate and extract vector data from NHDPlus V2 database such as catchments, HUC8, HUC12, GagesII, flowlines, and water bodies.

Additionally, PyNHD offers some extra utilities for processing the flowlines:

  • prepare_nhdplus: For cleaning up the dataframe by, for example, removing tiny networks, adding a to_comid column, and finding a terminal flowlines if it doesn’t exist.

  • topoogical_sort: For sorting the river network topologically which is useful for routing and flow accumulation.

  • vector_accumulation: For computing flow accumulation in a river network. This function is generic and any routing method can be plugged in.

These utilities are developed based on an R package called nhdplusTools.

You can try using PyNHD without installing it on you system by clicking on the binder badge below the PyNHD banner. A Jupyter notebook instance with the Hydrodata software stack pre-installed will be launched in your web browser and you can start coding!

Moreover, requests for additional functionalities can be submitted via issue tracker.

Installation

You can install PyNHD using pip after installing libgdal on your system (for example, in Ubuntu run sudo apt install libgdal-dev):

$ pip install pynhd

Alternatively, PyNHD can be installed from the conda-forge repository using Conda:

$ conda install -c conda-forge pynhd

Quick start

Let’s explore the capabilities of NLDI. We need to instantiate the class first:

from pynhd import NLDI

nldi = NLDI()
station_id = "USGS-01031500"
UT = "upstreamTributaries"
UM = "upstreamMain"

We can get the basin geometry for the USGS station number 01031500:

basin = nldi.getfeature_byid("nwissite", station_id, basin=True)

NLDI offers navigating a river network from any point in the network in the upstream or downstream direction. We can also limit the navigation distance (in km). The navigation can be done for all the valid NLDI sources which are comid, huc12pp, nwissite, wade, WQP. For example, let’s find all the USGS stations upstream of 01031500, through the tributaries, and then limit the navigation to only the main channel.

args = {
    "fsource": "nwissite",
    "fid": station_id,
    "navigation": UM,
    "source": "nwissite",
    "distance": None,
}

st_main = nldi.navigate_byid(**args)

args["distance"] = 20  # km
st_d150 = nldi.navigate_byid(**args)

args.update({"distance": None, "navigation": UT})
st_trib = nldi.navigate_byid(**args)

We can set the source to huc12pp to get HUC12 pour points.

args["source"] = "huc12pp"
pp = nldi.navigate_byid(**args)

NLDI provides only comid and geometry of the flowlines which can further be used to get the other available columns in the NHDPlus database. Let’s see how we can combine NLDI and WaterData to get the NHDPlus data for our station.

wd = WaterData("nhdflowline_network")

args.update({"source" : None, "navigation": UM})
comids = nldi.navigate_byid(**args).nhdplus_comid.tolist()
flw_main = wd.byid("comid", comids)

args["navigation"] = UT
comids = nldi.navigate_byid(**args).nhdplus_comid.tolist()
flw_trib = wd.byid("comid", comids)
https://raw.githubusercontent.com/cheginit/hydrodata/develop/docs/_static/example_plots_pynhd.png

Other feature sources in the WaterData database are nhdarea, nhdwaterbody, catchmentsp, gagesii, huc08, huc12, huc12agg, and huc12all. For example, we can get the contributing catchments of the flowlines using catchmentsp.

wd = WaterData("catchmentsp")
catchments = wd.byid("featureid", comids)

The WaterData class also has a method called bybox to get data from the feature sources within a bounding box.

wd = WaterData("nhdwaterbody")
wb = wd.bybox((-69.7718, 45.0742, -69.3141, 45.4534))

Next, lets clean up the flowlines and use it to compute flow accumulation. For simplicity, we assume that the flow in each river segment is equal to the length of the segment. Therefore, the accumulated flow at each point should be equal to the sum of the lengths of all its upstream river segments i.e., arbolatesu column in the NHDPlus database. We can use this to validate the flow accumulation result.

import pynhd as nhd

flw = nhd.prepare_nhdplus(flw_trib, 1, 1, 1, True, True)

def routing(qin, q):
    return qin + q

qsim = nhd.vector_accumulation(
    flw[["comid", "tocomid", "lengthkm"]], routing, "lengthkm", ["lengthkm"],
)
flw = flw.merge(qsim, on="comid")
diff = flw.arbolatesu - flw.acc

print(diff.abs().sum() < 1e-5)

Contributing

Contributions are very welcomed. Please read CONTRIBUTING.rst file for instructions.

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

pynhd-0.1.2.tar.gz (27.9 kB view details)

Uploaded Source

Built Distribution

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

pynhd-0.1.2-py2.py3-none-any.whl (13.1 kB view details)

Uploaded Python 2Python 3

File details

Details for the file pynhd-0.1.2.tar.gz.

File metadata

  • Download URL: pynhd-0.1.2.tar.gz
  • Upload date:
  • Size: 27.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/47.1.0 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.8.5

File hashes

Hashes for pynhd-0.1.2.tar.gz
Algorithm Hash digest
SHA256 244841108e6265d90d07561d7132f16fcca64c0ed49f2be1042f04b3dfd60b50
MD5 7eeffbd63505def8d051b0aae2ad700b
BLAKE2b-256 2350dad56df31c1a9e07f2ef3942b890a3f4d2dca53aa418e13d950dfab23a01

See more details on using hashes here.

File details

Details for the file pynhd-0.1.2-py2.py3-none-any.whl.

File metadata

  • Download URL: pynhd-0.1.2-py2.py3-none-any.whl
  • Upload date:
  • Size: 13.1 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/47.1.0 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.8.5

File hashes

Hashes for pynhd-0.1.2-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 8e2ad340b02dc3af5c3b87b9995874d7af40fb2dd1a8955f0dfc591203f1b136
MD5 9f08b1c1464485f61cb615031b262efc
BLAKE2b-256 55a8ba22f6b7d06c3321f14032ceb90f67f74b77c7a4a6d8b0b0b2a257b09fc2

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