Skip to content

Commit 28d7c68

Browse files
Add channel measurement endpoint (#218)
* Add channel measurement endpoint
1 parent 4dc9f6a commit 28d7c68

3 files changed

Lines changed: 175 additions & 1 deletion

File tree

dataretrieval/waterdata/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
# Public API exports
1313
from .api import (
14+
get_channel,
1415
get_codes,
1516
get_continuous,
1617
get_daily,
@@ -32,6 +33,7 @@
3233
)
3334

3435
__all__ = [
36+
"get_channel",
3537
"get_codes",
3638
"get_continuous",
3739
"get_daily",

dataretrieval/waterdata/api.py

Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2005,3 +2005,168 @@ def get_stats_date_range(
20052005
expand_percentiles=expand_percentiles
20062006
)
20072007

2008+
2009+
def get_channel(
2010+
monitoring_location_id: Optional[Union[str, List[str]]] = None,
2011+
field_visit_id: Optional[Union[str, List[str]]] = None,
2012+
measurement_number: Optional[Union[str, List[str]]] = None,
2013+
time: Optional[Union[str, List[str]]] = None,
2014+
channel_name: Optional[Union[str, List[str]]] = None,
2015+
channel_flow: Optional[Union[str, List[str]]] = None,
2016+
channel_flow_unit: Optional[Union[str, List[str]]] = None,
2017+
channel_width: Optional[Union[str, List[str]]] = None,
2018+
channel_width_unit: Optional[Union[str, List[str]]] = None,
2019+
channel_area: Optional[Union[str, List[str]]] = None,
2020+
channel_area_unit: Optional[Union[str, List[str]]] = None,
2021+
channel_velocity: Optional[Union[str, List[str]]] = None,
2022+
channel_velocity_unit: Optional[Union[str, List[str]]] = None,
2023+
channel_location_distance: Optional[Union[str, List[str]]] = None,
2024+
channel_location_distance_unit: Optional[Union[str, List[str]]] = None,
2025+
channel_stability: Optional[Union[str, List[str]]] = None,
2026+
channel_material: Optional[Union[str, List[str]]] = None,
2027+
channel_evenness: Optional[Union[str, List[str]]] = None,
2028+
horizontal_velocity_description: Optional[Union[str, List[str]]] = None,
2029+
vertical_velocity_description: Optional[Union[str, List[str]]] = None,
2030+
longitudinal_velocity_description: Optional[Union[str, List[str]]] = None,
2031+
measurement_type: Optional[Union[str, List[str]]] = None,
2032+
last_modified: Optional[Union[str, List[str]]] = None,
2033+
channel_measurement_type: Optional[Union[str, List[str]]] = None,
2034+
properties: Optional[List[str]] = None,
2035+
skip_geometry: Optional[bool] = None,
2036+
bbox: Optional[List[float]] = None,
2037+
limit: Optional[int] = None,
2038+
convert_type: bool = True,
2039+
) -> Tuple[pd.DataFrame, BaseMetadata]:
2040+
"""
2041+
Channel measurements taken as part of streamflow field measurements.
2042+
2043+
Parameters
2044+
----------
2045+
monitoring_location_id : string or list of strings, optional
2046+
A unique identifier representing a single monitoring location. This
2047+
corresponds to the id field in the monitoring-locations endpoint.
2048+
Monitoring location IDs are created by combining the agency code of
2049+
the agency responsible for the monitoring location (e.g. USGS) with
2050+
the ID number of the monitoring location (e.g. 02238500), separated
2051+
by a hyphen (e.g. USGS-02238500).
2052+
field_visit_id : string or list of strings, optional
2053+
A universally unique identifier (UUID) for the field visit. Multiple measurements
2054+
may be made during a single field visit.
2055+
measurement_number : string or list of strings, optional
2056+
Measurement number.
2057+
time : string or list of strings, optional
2058+
The date an observation represents. You can query this field using
2059+
date-times or intervals, adhering to RFC 3339, or using ISO 8601
2060+
duration objects. Intervals may be bounded or half-bounded (double-dots
2061+
at start or end). Only features that have a time that intersects the
2062+
value of datetime are selected. If a feature has multiple temporal
2063+
properties, it is the decision of the server whether only a single
2064+
temporal property is used to determine the extent or all relevant
2065+
temporal properties.
2066+
Examples:
2067+
2068+
* A date-time: "2018-02-12T23:20:50Z"
2069+
* A bounded interval: "2018-02-12T00:00:00Z/2018-03-18T12:31:12Z"
2070+
* Half-bounded intervals: "2018-02-12T00:00:00Z/.." or "../2018-03-18T12:31:12Z"
2071+
* Duration objects: "P1M" for data from the past month or "PT36H" for
2072+
the last 36 hours
2073+
channel_name : string or list of strings, optional
2074+
The channel name.
2075+
channel_flow : string or list of strings, optional
2076+
The units for channel discharge.
2077+
channel_width : string or list of strings, optional
2078+
The channel width.
2079+
channel_width_unit : string or list of strings, optional
2080+
The units for channel width.
2081+
channel_area : string or list of strings, optional
2082+
The channel area.
2083+
channel_area_unit : string or list of strings, optional
2084+
The units for channel area.
2085+
channel_velocity : string or list of strings, optional
2086+
The mean channel velocity.
2087+
channel_velocity_unit : string or list of strings, optional
2088+
The units for channel velocity.
2089+
channel_location_distance : string or list of strings, optional
2090+
The channel location distance.
2091+
channel_location_distance_unit : string or list of strings, optional
2092+
The units for channel location distance.
2093+
channel_stability : string or list of strings, optional
2094+
The stability of the channel material.
2095+
channel_material : string or list of strings, optional
2096+
The channel material.
2097+
channel_evenness : string or list of strings, optional
2098+
The channel evenness from bank to bank.
2099+
horizontal_velocity_description : string or list of strings, optional
2100+
The horizontal velocity description.
2101+
vertical_velocity_description : string or list of strings, optional
2102+
The vertical velocity description.
2103+
longitudinal_velocity_description : string or list of strings, optional
2104+
The longitudinal velocity description.
2105+
measurement_type : string or list of strings, optional
2106+
The measurement type.
2107+
The last time a record was refreshed in our database. This may happen
2108+
due to regular operational processes and does not necessarily indicate
2109+
anything about the measurement has changed. You can query this field
2110+
using date-times or intervals, adhering to RFC 3339, or using ISO 8601
2111+
duration objects. Intervals may be bounded or half-bounded (double-dots
2112+
at start or end).
2113+
Examples:
2114+
2115+
* A date-time: "2018-02-12T23:20:50Z"
2116+
* A bounded interval: "2018-02-12T00:00:00Z/2018-03-18T12:31:12Z"
2117+
* Half-bounded intervals: "2018-02-12T00:00:00Z/.." or "../2018-03-18T12:31:12Z"
2118+
* Duration objects: "P1M" for data from the past month or "PT36H" for the
2119+
last 36 hours
2120+
2121+
Only features that have a last_modified that intersects the value of
2122+
datetime are selected.
2123+
skip_geometry : boolean, optional
2124+
This option can be used to skip response geometries for each feature.
2125+
The returning object will be a data frame with no spatial information.
2126+
Note that the USGS Water Data APIs use camelCase "skipGeometry" in
2127+
CQL2 queries.
2128+
channel_measurement_type : string or list of strings, optional
2129+
The channel measurement type.
2130+
properties : string or list of strings, optional
2131+
A vector of requested columns to be returned from the query. Available
2132+
options are: geometry, channel_measurements_id, monitoring_location_id,
2133+
field_visit_id, measurement_number, time, channel_name, channel_flow,
2134+
channel_flow_unit, channel_width, channel_width_unit, channel_area,
2135+
channel_area_unit, channel_velocity, channel_velocity_unit,
2136+
channel_location_distance, channel_location_distance_unit, channel_stability,
2137+
channel_material, channel_evenness, horizontal_velocity_description,
2138+
vertical_velocity_description, longitudinal_velocity_description,
2139+
measurement_type, last_modified, channel_measurement_type. The default (NA) will
2140+
return all columns of the data.
2141+
convert_type : boolean, optional
2142+
If True, the function will convert the data to dates and qualifier to
2143+
string vector
2144+
2145+
Returns
2146+
-------
2147+
df : ``pandas.DataFrame`` or ``geopandas.GeoDataFrame``
2148+
Formatted data returned from the API query.
2149+
md: :obj:`dataretrieval.utils.Metadata`
2150+
A custom metadata object
2151+
2152+
Examples
2153+
--------
2154+
.. code::
2155+
2156+
>>> # Get channel data from a
2157+
>>> # single site from a single year
2158+
>>> df, md = dataretrieval.waterdata.get_channel(
2159+
... monitoring_location_id="USGS-02238500",
2160+
... )
2161+
"""
2162+
service = "channel-measurements"
2163+
output_id = "channel_measurements_id"
2164+
2165+
# Build argument dictionary, omitting None values
2166+
args = {
2167+
k: v
2168+
for k, v in locals().items()
2169+
if k not in {"service", "output_id"} and v is not None
2170+
}
2171+
2172+
return get_ogc_data(args, output_id, service)

tests/waterdata_test.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@
1818
get_time_series_metadata,
1919
get_reference_table,
2020
get_stats_por,
21-
get_stats_date_range
21+
get_stats_date_range,
22+
get_channel
2223
)
2324

2425
def mock_request(requests_mock, request_url, file_path):
@@ -310,3 +311,9 @@ def test_get_stats_date_range():
310311
assert "percentile" in df.columns
311312
assert df['interval_type'].isin(['month', 'calendar_year', 'water_year']).all()
312313

314+
def test_get_channel():
315+
df, _ = get_channel(monitoring_location_id="USGS-02238500")
316+
317+
assert df.shape[0] > 470
318+
assert df.shape[1] == 27 # if geopandas installed, 21 columns if not
319+
assert "channel_measurements_id" in df.columns

0 commit comments

Comments
 (0)