Examples
Modern development should use WaterData.* functions, which query the newer USGS Waterdata OGC and Samples APIs. These services are more performant and return cleaner, modern data formats.
Index
Waterdata API Examples
These examples use data retrieved from the USGS Waterdata OGC API and the Samples API.
Examining Site 01491000
In this example we fetch information for site "01491000" located on the Choptank River near Greensboro, MD. Site identifiers in the modern API typically include the agency prefix (e.g., USGS-).
First we will obtain metadata about the monitoring location.
using DataRetrieval
siteID = "USGS-01491000"
df, response = WaterData.monitoring_locations(monitoring_location_id=siteID);
# print the site information table
df| Row | geometry | well_constructed_depth | country_code | agency_name | hole_constructed_depth | horizontal_positional_accuracy | altitude_method_name | vertical_datum | drainage_area | district_code | monitoring_location_number | national_aquifer_code | agency_code | state_code | county_name | basin_code | country_name | minor_civil_division_code | altitude | horizontal_position_method_code | construction_date | original_horizontal_datum | altitude_method_code | aquifer_code | vertical_datum_name | site_type_code | hydrologic_unit_code | horizontal_positional_accuracy_code | original_horizontal_datum_name | monitoring_location_name | contributing_drainage_area | horizontal_position_method_name | monitoring_location_id | site_type | altitude_accuracy | aquifer_type_code | county_code | state_name | uses_daylight_savings | depth_source_code | time_zone_abbreviation |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Object… | Nothing | String | String | Nothing | String | String | String | Float64 | String | String | Nothing | String | String | String | Nothing | String | Nothing | Float64 | String | Missing | String | String | Nothing | String | String | String | String | String | String | Nothing | String | String | String | Float64 | Nothing | String | String | String | Nothing | String | |
| 1 | Object{String, Any}("type"=>"Point", "coordinates"=>Any[-75.7858, 38.9972]) | US | U.S. Geological Survey | Accurate to + or - 1 sec. | Interpolated from Digital Elevation Model | NAVD88 | 113.0 | 24 | 01491000 | USGS | 24 | Caroline County | United States of America | 2.73 | M | missing | NAD83 | N | North American Vertical Datum of 1988 | ST | 020600050203 | S | North American Datum of 1983 | CHOPTANK RIVER NEAR GREENSBORO, MD | Interpolated from MAP. | USGS-01491000 | Stream | 0.1 | 011 | Maryland | Y | EST |
We can also look at the raw API response status.
# print the response status code
response.status200Now we can get daily discharge values. We will obtain data for the first three days of January 1980.
# parameter_code 00060 is discharge
df, response = WaterData.daily(monitoring_location_id=siteID,
parameter_code="00060",
time="1980-01-01/1980-01-03");
# print the data frame
df| Row | geometry | time | statistic_id | time_series_id | approval_status | qualifier | monitoring_location_id | parameter_code | value | last_modified | unit_of_measure |
|---|---|---|---|---|---|---|---|---|---|---|---|
| Object… | DateTime | String | String | String | Nothing | String | String | Float64 | Missing | String | |
| 1 | Object{String, Any}("type"=>"Point", "coordinates"=>Any[-75.7858, 38.9972]) | 1980-01-03T00:00:00 | 00003 | 847d8581ff6c45f58ff7cb90dec31fab | Approved | USGS-01491000 | 00060 | 92.0 | missing | ft^3/s | |
| 2 | Object{String, Any}("type"=>"Point", "coordinates"=>Any[-75.7858, 38.9972]) | 1980-01-02T00:00:00 | 00003 | 847d8581ff6c45f58ff7cb90dec31fab | Approved | USGS-01491000 | 00060 | 96.0 | missing | ft^3/s | |
| 3 | Object{String, Any}("type"=>"Point", "coordinates"=>Any[-75.7858, 38.9972]) | 1980-01-01T00:00:00 | 00003 | 847d8581ff6c45f58ff7cb90dec31fab | Approved | USGS-01491000 | 00060 | 100.0 | missing | ft^3/s |
Plotting Flow Data for Site 01646500
In this example we fetch and plot instantaneous (continuous) flow data for site "01646500" (Potomac River near Washington D.C.) for December 1, 2022.
using DataRetrieval, DataFrames
siteID = "USGS-01646500"
df, response = WaterData.continuous(monitoring_location_id=siteID,
parameter_code="00060",
time="2022-12-01/2022-12-05");
# Clean up missing values and sort the data
df = dropmissing(df, [:time, :value])
sort!(df, :time)
# display the first row
first(df)| Row | time | statistic_id | time_series_id | approval_status | qualifier | monitoring_location_id | parameter_code | value | continuous_id | last_modified | unit_of_measure |
|---|---|---|---|---|---|---|---|---|---|---|---|
| DateTime | String | String | String | Nothing | String | String | Float64 | String | Missing | String | |
| 1 | 2022-12-01T00:00:00 | 00011 | 66a4e3fb8abe42fcb0942b5c0fe98f68 | Approved | USGS-01646500 | 00060 | 3810.0 | 3075ad2a-c255-4a59-a27f-ca363eccb6a0 | missing | ft^3/s |
We can fetch additional information about the parameter code (units, description) using the reference table service.
pcodedf, response = WaterData.reference_table("parameter-codes", query=Dict("id" => "00060"));
pcodedf| Row | time_basis | particle_size_basis | sample_fraction | weight_basis | temperature_basis | medium | parameter_description | parameter_name | parameter_group_code | statistical_basis | parameter_code | unit_of_measure | epa_equivalence |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| String | Nothing | Nothing | Nothing | Nothing | String | String | String | String | String | String | String | String | |
| 1 | 1 Day | Water | Discharge, cubic feet per second | Discharge | PHY | Mean | 00060 | ft3/s | Not checked |
Now we plot the discharge data. Note that column names in the modern API are standardized (e.g., time and value).
using Plots
plot(df.time, df.value,
title="Discharge at Potomac River near Washington D.C.",
ylabel="Discharge (ft³/s)",
xlabel="Time",
xrotation=60,
label="Discharge",
dpi=200)Fetching and Plotting Groundwater Levels from Site 393617075380403
In this example we fetch and plot daily groundwater levels (parameter code "72019") for the first six months of 2012.
using DataRetrieval, DataFrames
siteID = "USGS-393617075380403"
df, response = WaterData.daily(monitoring_location_id=siteID,
parameter_code="72019",
time="2012-01-01/2012-06-30");
# Clean up missing values and sort the data
dropmissing!(df, :value)
sort!(df, :time)
# display the first row
first(df)| Row | geometry | time | statistic_id | time_series_id | approval_status | qualifier | monitoring_location_id | parameter_code | value | last_modified | unit_of_measure |
|---|---|---|---|---|---|---|---|---|---|---|---|
| Object… | DateTime | String | String | String | Nothing | String | String | Float64 | Missing | String | |
| 1 | Object{String, Any}("type"=>"Point", "coordinates"=>Any[-75.6343, 39.6047]) | 2012-01-21T00:00:00 | 00003 | 159aaabe0d3546fda466d4c75ee7a243 | Approved | USGS-393617075380403 | 72019 | -0.1 | missing | ft |
Get parameter metadata:
pcodedf, response = WaterData.reference_table("parameter-codes", query=Dict("id" => "72019"));
pcodedf| Row | time_basis | particle_size_basis | sample_fraction | weight_basis | temperature_basis | medium | parameter_description | parameter_name | parameter_group_code | statistical_basis | parameter_code | unit_of_measure | epa_equivalence |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Nothing | Nothing | Nothing | Nothing | Nothing | String | String | String | String | Nothing | String | String | String | |
| 1 | Water | Depth to water level, feet below land surface | Water level, depth LSD | PHY | 72019 | ft | Agree |
Plot the groundwater levels:
using Plots
# Use the value column directly
plot(df.time, df.value,
title="Groundwater Levels at Site 393617075380403",
ylabel="Depth (ft below land surface)",
xlabel="Time",
xrotation=60,
label="Groundwater Level",
yflip=true,
dpi=200,
margin=5Plots.mm)NLDI Examples
The Network Linked Data Index (NLDI) API provides functions to navigate flowlines, find basin boundaries, and discover linked features.
Finding a Basin
In this example, we find the basin for a specific NWIS site.
using DataRetrieval, GeoDataFrames
df_basin, response = NLDI.basin("nwissite", "USGS-01491000");
# display the basin data frame
first(df_basin)Navigating Flowlines
We can also navigate upstream or downstream from a feature to find flowlines.
# Navigate upstream main (UM) up to 10 km
df_flow, response = NLDI.flowlines("UM", feature_source="nwissite", feature_id="USGS-01491000", distance=10);
# display the flowlines
first(df_flow)| Row | feature_type | geometry | nhdplus_comid | geometry_type | coordinates |
|---|---|---|---|---|---|
| String | Object… | String | String | Array… | |
| 1 | Feature | Object{String, Any}("type"=>"LineString", "coordinates"=>Any[Any[-75.7862, 39.0001], Any[-75.7862, 38.9998], Any[-75.7853, 38.9987], Any[-75.7853, 38.998], Any[-75.7854, 38.9977], Any[-75.7854, 38.9961], Any[-75.7852, 38.996]]) | 9407484 | LineString | Any[Any[-75.7862, 39.0001], Any[-75.7862, 38.9998], Any[-75.7853, 38.9987], Any[-75.7853, 38.998], Any[-75.7854, 38.9977], Any[-75.7854, 38.9961], Any[-75.7852, 38.996]] |
We can then plot the basin and flowlines geometries directly.
using Plots
p = plot(df_basin.geometry, fillalpha=0.2, c=:blue, label="Basin")
plot!(p, df_flow.geometry, c=:red, linewidth=2, label="Flowlines UM")
title!("Site 01491000 Basin & Flowlines")Retrieving Linked Features
You can also use NLDI to find all linked features for a given source, or search along navigation lines. We will grab the first few nwissite features and view their rich metadata.
using DataRetrieval, GeoDataFrames
# Retrieve all water quality portal locations
df_features, response = NLDI.features_by_data_source("nwissite");
# display the properties available
first(df_features, 3)Water Quality Portal (WQP) / Samples API Examples
Modern water quality queries should prefer the WaterData.results or WaterData.samples functions.
Identifying Sites with Chloride Measurements
In this example we identify sites with chloride measurements in New Jersey (state code "US:34").
using DataRetrieval, DataFrames
# Find locations with Chloride measurements
njcl, response = WaterData.locations(state_fips="US:34",
characteristic="Chloride");
# print the number of sites found
nrow(njcl)5952Retrieving Water Quality Results
We can also retrieve the actual measurements for a specific location.
# Get narrow profile results for a specific monitoring location
results, response = WaterData.results(
monitoring_location_identifier="USGS-01408029",
characteristic="Chloride",
activity_start_date_lower="2020-01-01"
);
# display the first result
first(results, 1)| Row | Org_Identifier | Org_FormalName | Project_Identifier | Project_Name | Project_QAPPApproved | Project_QAPPApprovalAgency | ProjectAttachment_FileName | ProjectAttachment_FileType | Location_Identifier | Location_Name | Location_Type | Location_Description | Location_State | Location_CountryName | Location_CountyName | Location_CountryCode | Location_StatePostalCode | Location_CountyCode | Location_HUCEightDigitCode | Location_HUCTwelveDigitCode | Location_TribalLandIndicator | Location_TribalLand | Location_Latitude | Location_Longitude | Location_HorzCoordReferenceSystemDatum | Location_LatitudeStandardized | Location_LongitudeStandardized | Location_HorzCoordStandardizedDatum | AlternateLocation_IdentifierCount | Activity_ActivityIdentifier | Activity_ActivityIdentifierUserSupplied | Activity_TypeCode | Activity_Media | Activity_MediaSubdivision | Activity_BottomDepthSamplingComponent | ActivityBiological_AssemblageSampled | ActivityBiological_ToxicityTestType | Activity_ConductingOrganization | Activity_Comment | ActivityLocation_Latitude | ActivityLocation_Longitude | ActivityLocation_HorzCoordReferenceSystemDatum | ActivityLocation_SourceMapScale | ActivityLocation_LatitudeStandardized | ActivityLocation_LongitudeStandardized | ActivityLocation_HorzCoordStandardizedDatum | ActivityLocation_HorzAccuracyMeasure | ActivityLocation_HorzAccuracyMeasureUnit | ActivityLocation_HorizontalAccuracyHorzCollectionMethod | ActivityLocation_Description | Activity_StartDate | Activity_StartTime | Activity_StartTimeZone | Activity_EndDate | Activity_EndTime | Activity_EndTimeZone | Activity_DepthHeightMeasure | Activity_DepthHeightMeasureUnit | Activity_BottomDepthAltitudeReferencePoint | Activity_ActivityRelativeDepth | Activity_TopDepthMeasure | Activity_TopDepthMeasureUnit | Activity_BottomDepthMeasure | Activity_BottomDepthMeasureUnit | SampleCollectionMethod_Identifier | SampleCollectionMethod_IdentifierContext | SampleCollectionMethod_Name | SampleCollectionMethod_QualifierTypeName | SampleCollectionMethod_Description | SampleCollectionMethod_EquipmentName | SampleCollectionMethod_EquipmentComment | SamplePrepMethod_Identifier | SamplePrepMethod_IdentifierContext | SamplePrepMethod_Name | SamplePrepMethod_QualifierType | SamplePrepMethod_Description | SamplePrepMethod_ContainerLabel | SamplePrepMethod_ContainerType | SamplePrepMethod_ContainerColor | SamplePrepMethod_ChemicalPreservativeUsed | SamplePrepMethod_ThermalPreservativeUsed | SamplePrepMethod_TransportStorageDescription | Activity_HydrologicCondition | Activity_HydrologicEvent | ActivityAttachment_FileName | ActivityAttachment_FileType | ActivityAttachment_FileDownload | Result_DataLoggerLine | Result_ResultDetectionCondition | Result_Characteristic | Result_CharacteristicUserSupplied | Result_CASNumber | Result_MethodSpeciation | Result_SampleFraction | ResultBiological_Intent | ResultBiological_IndividualIdentifier | ResultBiological_Taxon | ResultBiological_TaxonUserSupplied | ResultBiological_TaxonUserSuppliedReference | ResultBiological_UnidentifiedSpeciesIdentifier | ResultBiological_SampleTissueAnatomy | ResultBiological_GroupSummaryCount | GroupSummaryWeight_Measure | GroupSummaryWeight_MeasureUnit | ResultDepthHeight_Measure | ResultDepthHeight_MeasureUnit | ResultDepthHeight_AltitudeReferencePoint | ResultDepthHeight_SamplingPointName | ResultDepthHeight_SamplingPointType | ResultDepthHeight_SamplingPointPlaceInSeries | ResultDepthHeight_SamplingPointComment | ResultDepthHeight_RecordIdentifierUserSupplied | Result_MeasureIdentifier | Result_Measure | Result_MeasureUnit | Result_MeasureQualifierCode | Result_MeasureStatusIdentifier | Result_StatisticalBase | Result_StatisticalNValue | Result_MeasureType | Result_WeightBasis | Result_TimeBasis | Result_MeasureTemperatureBasis | Result_MeasureParticleSizeBasis | DataQuality_PrecisionValue | DataQuality_BiasValue | DataQuality_ConfidenceIntervalValue | DataQuality_UpperConfidenceLimitValue | DataQuality_LowerConfidenceLimitValue | DataQuality_ResultComment | DetectionLimit_TypeA | DetectionLimit_MeasureA | DetectionLimit_MeasureUnitA | DetectionLimit_CommentA | DetectionLimit_TypeB | DetectionLimit_MeasureB | DetectionLimit_MeasureUnitB | DetectionLimit_CommentB | LabInfo_LabSampleSplitRatio | LabInfo_LabAccreditationIndicator | LabInfo_LabAccreditationAuthority | LabInfo_TaxonAccreditationIndicator | LabInfo_TaxonAccreditationAuthority | ResultAnalyticalMethod_Identifier | ResultAnalyticalMethod_IdentifierContext | ResultAnalyticalMethod_Name | ResultAnalyticalMethod_QualifierType | ResultAnalyticalMethod_Description | Result_ComparableMethodIdentifier | Result_ComparableMethodIdentifierContext | Result_ComparableMethodModification | LabInfo_Name | LabInfo_AnalysisStartDate | LabInfo_AnalysisStartTime | LabInfo_AnalysisStartTimeZone | LabInfo_AnalysisEndDate | LabInfo_AnalysisEndTime | LabInfo_AnalysisEndTimeZone | LabInfo_LaboratoryComment | LabSamplePrepMethod_Identifier | LabSamplePrepMethod_IdentifierContext | LabSamplePrepMethod_Name | LabSamplePrepMethod_QualifierType | LabSamplePrepMethod_Description | LabSamplePrepMethod_StartDate | LabSamplePrepMethod_StartTime | LabSamplePrepMethod_StartTimeZone | LabSamplePrepMethod_EndDate | LabSamplePrepMethod_EndTime | LabSamplePrepMethod_EndTimeZone | LabSamplePrepMethod_DilutionFactor | ResultAttachment_FileName | ResultAttachment_FileType | ResultAttachment_FileDownload | ProviderName | Result_CharacteristicComparable | Result_CharacteristicGroup | Org_Type | LastChangeDate | USGSpcode | USGSSampleAquifer |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| String7 | String31 | String | Missing | Missing | Missing | Missing | Missing | String15 | String | String7 | Missing | String15 | String31 | String15 | String3 | String3 | Missing | Int64 | Int64 | Missing | Missing | Float64 | Float64 | String7 | Float64 | Float64 | String7 | Missing | String | Missing | String | String7 | String15 | Missing | Missing | Missing | String | String | Missing | Missing | Missing | Missing | Missing | Missing | Missing | Missing | Missing | Missing | Missing | Date | Time | String3 | Missing | Missing | Missing | Missing | Missing | Missing | Missing | Missing | Missing | Missing | Missing | Int64? | String15? | String31? | Missing | String? | String31? | Missing | Missing | Missing | Missing | Missing | Missing | Missing | Missing | Missing | Missing | Missing | Missing | String31 | String15 | Missing | Missing | Missing | Missing | Missing | String15 | String31 | String15 | Missing | String31 | Missing | Missing | Missing | Missing | Missing | Missing | Missing | Missing | Missing | Missing | Missing | Missing | Missing | Missing | Missing | Missing | Missing | Missing | String | Float64 | String7 | Missing | String15 | Missing | Missing | String7 | Missing | Missing | Missing | Missing | Missing | Missing | Missing | Missing | Missing | String? | Missing | Missing | Missing | Missing | String31 | Float64 | String7 | String | Missing | Missing | Missing | Missing | Missing | String7 | String15 | String15 | Missing | String | Missing | Missing | Missing | String | Date | Missing | Missing | Missing | Missing | Missing | String? | Missing | Missing | Missing | Missing | Missing | Missing | Missing | Missing | Missing | Missing | Missing | Missing | Missing | Missing | Missing | String7 | Missing | String31 | String31 | Date | Int64 | Missing | |
| 1 | USGS | U.S. Geological Survey | ["USGS","00003LB00"] | missing | missing | missing | missing | missing | USGS-01408029 | Manasquan River near Allenwood NJ | Stream | missing | New Jersey | United States of America | Monmouth County | US | NJ | missing | 2040301 | 20403010105 | missing | missing | 40.1467 | -74.1222 | NAD83 | 40.1467 | -74.1222 | NAD83 | missing | 313052d5-fa3c-4ca9-9549-c90d9805f5bd | missing | Sample - Routine, regular | Water | Surface water | missing | missing | missing | U.S. Geological Survey-Water Resources Discipline | missing | missing | missing | missing | missing | missing | missing | missing | missing | missing | missing | 2024-03-27 | 10:30:00 | EDT | missing | missing | missing | missing | missing | missing | missing | missing | missing | missing | missing | 40 | USDOI/USGS | Multiple verticals | missing | Depth-integrated: Multiple verticals | Weighted bottle | missing | missing | missing | missing | missing | missing | missing | missing | missing | missing | missing | missing | Stable, normal stage | Routine sample | missing | missing | missing | missing | missing | Chloride | Chloride, water, filtered | 16887-00-6 | missing | Filtered field and/or lab | missing | missing | missing | missing | missing | missing | missing | missing | missing | missing | missing | missing | missing | missing | missing | missing | missing | missing | d6990c7a-f266-4274-8f2f-45594e9e6e3c | 36.8 | mg/L | missing | Accepted | missing | missing | Actual | missing | missing | missing | missing | missing | missing | missing | missing | missing | Hold time parameters were exceeded during the collection to analysis phase of sample processing. | missing | missing | missing | missing | Detection limit by DQCALC | 0.05 | mg/L | Quantitation limit value of the specified type | missing | missing | missing | missing | missing | IC022 | USDOI/USGS | Anions, wf, IC | missing | DESCRIPTION: Anions, filtered water, ion chromatography; CITATION(S): Fishman, M.J., ed., 1993, Methods of analysis by the U.S. Geological Survey National Water Quality Laboratory--Determination of inorganic and organic constituents in water and fluvial sediments: U.S. Geological Survey Open-File Report 93-125, p. 19-25. | USGS | I-2057-90 |<--->| American Public Health Association and American Water Works Association, 2012, Standard methods for the examination of water and wastewater (22d ed.): Washington, D.C., American Public Health Association, method 4110 B-2011, p. 4-5 through 4-7. | STDMETH | 4110 B-2011 |<--->| American Public Health Association, 2020, Standard methods for the examination of water and wastewater: Washington, D.C., American Public Health Association, method 4110 B | STDMETH | 4110 B | missing | missing | missing | USGS-National Water Quality Lab, Denver, CO | 2024-04-26 | missing | missing | missing | missing | missing | [@ - holding time exceeded]; [c - see result laboratory comment] | missing | missing | missing | missing | missing | missing | missing | missing | missing | missing | missing | missing | missing | missing | missing | USGS | missing | Inorganics, Major, Non-metals | Federal/US Government | 2026-02-12 | 940 | missing |
Summary of Modern vs Legacy Functions
| Feature | Modern Function | Legacy Function (Deprecated) |
|---|---|---|
| Site Metadata | WaterData.monitoring_locations | NWIS.site |
| Daily Values | WaterData.daily | NWIS.dv |
| Continuous Values | WaterData.continuous | NWIS.iv / NWIS.unit |
| Parameter Codes | WaterData.reference_table | NWIS.pcode |
| QW Results | WaterData.samples / WaterData.results | NWIS.qwdata |
| WQP Sites | WaterData.locations | WQP.sites |