How to download well info and groundwater levels from the NWIS server with Python – Tutorial

Hydrate

We have developed an applied example to download information and groundwater levels for one well and multiple wells with Python and the dataretrieval package. The tutorial stores information and levels on a csv file an can be easily applied for streamflow and water quality data stored on the NWIS.

More information about NWIS:

https://nwis.waterdata.usgs.gov/nwis

More information about the USGS dataretrieval package:

https://github.com/USGS-python/dataretrieval

Tutorial

Code

#!pip install -U dataretrieval
import dataretrieval.nwis as nwis
import pandas as pd

For one station

site = '401612074483401'

Get site info

siteInfo = nwis.get_info(sites=site)
siteInfo[0]
agency_cd site_no station_nm site_tp_cd lat_va long_va dec_lat_va dec_long_va coord_meth_cd coord_acy_cd local_time_fg reliability_cd gw_file_cd nat_aqfr_cd aqfr_cd aqfr_type_cd well_depth_va hole_depth_va depth_src_cd project_no
0 USGS 401612074483401 210545– MW-2Br GW 401612.53 744832.14 40.270147 -74.808928 N 1 Y C YYYYNYNN N300ERLMZC 231LCKG NaN 60 60 D NAWC

1 rows × 42 columns

wellInfoDf = siteInfo[0][['site_no','dec_lat_va','dec_long_va','dec_coord_datum_cd','alt_va','alt_datum_cd','well_depth_va']]
wellInfoDf = wellInfoDf.rename(columns={'site_no':'SiteNo',
                                'dec_lat_va':'Latitude',
                                'dec_long_va':'Longitude',
                                'dec_coord_datum_cd':'LatLonDatum',
                                'alt_va':'SurfaceElevation',
                                'alt_datum_cd':'WellHole',
                                'well_depth_va':'ElevationDatum'}) 
wellInfoDf = wellInfoDf.set_index('SiteNo')
wellInfoDf
Latitude Longitude LatLonDatum SurfaceElevation WellHole ElevationDatum
SiteNo
401612074483401 40.270147 -74.808928 NAD83 157.48 NAVD88 60

Get groundwater levels

wellDf = nwis.get_record(sites=site, service='gwlevels', start='2000-01-01')
wellDf.head()
agency_cd site_no site_tp_cd lev_va sl_lev_va sl_datum_cd lev_status_cd lev_agency_cd lev_dt_acy_cd lev_acy_cd lev_src_cd lev_meth_cd lev_age_cd
datetime
2000-05-18 14:39:00+00:00 USGS 401612074483401 GW 10.78 NaN NaN 1 USGS m NaN S T A
2000-10-23 18:10:00+00:00 USGS 401612074483401 GW 11.05 NaN NaN 1 USGS m NaN S T A
2001-03-29 16:20:00+00:00 USGS 401612074483401 GW 6.94 NaN NaN 1 USGS m NaN S T A
2001-10-23 15:02:00+00:00 USGS 401612074483401 GW 14.32 NaN NaN 1 USGS m NaN S S A
2003-01-31 16:28:00+00:00 USGS 401612074483401 GW 11.25 NaN NaN 1 USGS m NaN S S A
gwWellDf = wellDf[['site_no','site_tp_cd','lev_va']]
gwWellDf = gwWellDf.rename(columns={
    'site_no':'SiteNo',
    'site_tp_cd':'Type',
    'lev_va':'gwLevel'})
gwWellDf
SiteNo Type gwLevel
datetime
2000-05-18 14:39:00+00:00 401612074483401 GW 10.78
2000-10-23 18:10:00+00:00 401612074483401 GW 11.05
2001-03-29 16:20:00+00:00 401612074483401 GW 6.94
2001-10-23 15:02:00+00:00 401612074483401 GW 14.32
2003-01-31 16:28:00+00:00 401612074483401 GW 11.25
2003-03-19 19:22:00+00:00 401612074483401 GW 9.37
2003-03-24 15:26:00+00:00 401612074483401 GW 8.53
2004-05-11 15:00:00+00:00 401612074483401 GW 7.63
2005-06-30 15:55:00+00:00 401612074483401 GW 12.91
2006-06-01 15:36:00+00:00 401612074483401 GW 11.62
2017-11-03 17:25:00+00:00 401612074483401 GW 12.91
2019-05-23 15:41:00+00:00 401612074483401 GW 8.17

For many groundwater wells

wellsDf = pd.read_csv('../Txt/NAWC2018reportedWLs.txt',delimiter='t')
wellList = list(wellsDf.NWISnumber)
wellList[:5]
[401612074483401,
 401609074483402,
 401605074485102,
 401619074484801,
 401614074483101]
sitesInfo = nwis.get_info(sites=wellList)

wellsInfoDf = sitesInfo[0][['site_no','dec_lat_va','dec_long_va','dec_coord_datum_cd','alt_va','alt_datum_cd','well_depth_va']]
wellsInfoDf = wellsInfoDf.rename(columns={'site_no':'SiteNo',
                                'dec_lat_va':'Latitude',
                                'dec_long_va':'Longitude',
                                'dec_coord_datum_cd':'LatLonDatum',
                                'alt_va':'SurfaceElevation',
                                'alt_datum_cd':'WellHole',
                                'well_depth_va':'ElevationDatum'}) 
wellsInfoDf = wellsInfoDf.set_index('SiteNo')
wellsInfoDf.to_csv('../Csv/gwWells.csv')
wellsInfoDf.head()
Latitude Longitude LatLonDatum SurfaceElevation WellHole ElevationDatum
SiteNo
401603074485301 40.267533 -74.814331 NAD83 150.02 NAVD88 48.0
401605074483401 40.268033 -74.809100 NAD83 143.39 NAVD88 75.0
401605074485102 40.268289 -74.813811 NAD83 149.03 NAVD88 7.0
401606074485001 40.268364 -74.813592 NAD83 149.29 NAVD88 13.0
401606074485101 40.268328 -74.813725 NAD83 148.36 NAVD88 13.0
wellsDf = nwis.get_record(sites=wellList, service='gwlevels', start='2000-01-01')
wellsDf.head()
agency_cd site_tp_cd lev_va sl_lev_va sl_datum_cd lev_status_cd lev_agency_cd lev_dt_acy_cd lev_acy_cd lev_src_cd lev_meth_cd lev_age_cd
site_no datetime
401603074485301 2000-05-18 17:50:00+00:00 USGS GW 7.92 NaN NaN 1 USGS m NaN S S A
2000-10-23 14:25:00+00:00 USGS GW 7.65 NaN NaN 1 USGS m NaN S S A
2001-03-29 18:04:00+00:00 USGS GW 7.82 NaN NaN 1 USGS m NaN S S A
2001-10-23 17:35:00+00:00 USGS GW 11.81 NaN NaN 1 USGS m NaN S S A
2003-03-19 21:51:00+00:00 USGS GW 6.62 NaN NaN 1 USGS m NaN S S A
gwWellsDf = wellsDf[['site_tp_cd','lev_va']]

gwWellsDf.index.set_names(['SiteNo','Datetime'], inplace=True)

gwWellsDf = gwWellsDf.rename(columns={
    'site_tp_cd':'Type',
    'lev_va':'gwLevel'})

gwWellsDf.to_csv('../Csv/gwLevels.csv')

gwWellsDf
Type gwLevel
SiteNo Datetime
401603074485301 2000-05-18 17:50:00+00:00 GW 7.92
2000-10-23 14:25:00+00:00 GW 7.65
2001-03-29 18:04:00+00:00 GW 7.82
2001-10-23 17:35:00+00:00 GW 11.81
2003-03-19 21:51:00+00:00 GW 6.62
401625074484001 2005-06-30 14:10:00+00:00 GW 18.71
2006-06-01 16:32:00+00:00 GW 18.09
2017-11-03 17:58:00+00:00 GW 14.85
NaN GW 16.27
NaN GW 15.14

651 rows × 2 columns

Input files

You can download the input files from this link.

—————
TRANSCOM ISPFree Sigma HSE Email
Level 6 Domain Names are FREE – Register Now.

Related Posts