Lab 6

Teaching, , 2021

Eart 116 Hydrology: Lab 6, Hydrographs and Hydrologic Computing

Insert your name in the cell below.

Scientific coding is an important skill to hone.

We will be utilizing a programming langauge called R! No it wasn’t developed by pirates (sorry had to)! R is orginally a statistical computing langauge first developed in the early 90’s. Today R is one of the most widely used languages in STEM computing. A big key to the R langauge’s success is that it is an “open-source” code, that means that developers, scienctists, or your grandma could write a package of code and contribute to the development of R. Additionally, R is free, everyone likes free!

Today, we will be using open source R packages from the USGS to learn about stream flow. I will include links where I can to the packages if you are interested!

Lets get started!

Please answer the following questions, print out a copy of your lab, and upload a copy of your Jupyter Notebook to Canvas. The last two steps were outlined in the mandatory prelab.

First, we need to load the packages that we will be using today.

As with any coding language, you need to setup your environment. Loading packages is like getting out the applicances neccesacy to make cookies. We will need the mixer and the oven so we will load those packages. Loading more packages than you need will clutter the kitchen (code) and make your code run very slow.

#####Load packages here####
library(dataRetrieval) #This is the open source package from the USGS "dataRetreival"
source("functions/functions.R") # These are pre-canned functions that I have made. Look in the functions folder if you are interested in these.
also installing the dependencies ‘prettyunits’, ‘rprojroot’, ‘pkgbuild’, ‘diffobj’, ‘brio’, ‘callr’, ‘cli’, ‘desc’, ‘pkgload’, ‘praise’, ‘processx’, ‘ps’, ‘rlang’, ‘waldo’, ‘withr’, ‘testthat’, ‘isoband’

Warning message in install.packages("ggplot2"):
“installation of package ‘ps’ had non-zero exit status”Warning message in install.packages("ggplot2"):
“installation of package ‘processx’ had non-zero exit status”Warning message in install.packages("ggplot2"):
“installation of package ‘callr’ had non-zero exit status”Warning message in install.packages("ggplot2"):
“installation of package ‘pkgbuild’ had non-zero exit status”Warning message in install.packages("ggplot2"):
“installation of package ‘pkgload’ had non-zero exit status”Warning message in install.packages("ggplot2"):
“installation of package ‘testthat’ had non-zero exit status”Warning message in install.packages("ggplot2"):
“installation of package ‘isoband’ had non-zero exit status”Warning message in install.packages("ggplot2"):
“installation of package ‘ggplot2’ had non-zero exit status”Updating HTML index of packages in '.Library'
Making 'packages.html' ... done

Error in library(ggplot2): there is no package called ‘ggplot2’

1. library(ggplot2)

2. stop(txt, domain = NA)

The first thing we will be doing is downloading all the data that we will be using today. As you remember from previous labs, downloading hydrologic data manually can be time consuming and confusing. These commands make it easy and formats your data into easy to use tables.

An example of the function is below

site_name = readNWISdata(sites=’sitenumber’,service=’typeofdata’,startDate=”YYYY-MM-DD”,endDate=”YYYY-MM-DD”)


  • site_name = is the name of your site. This must be unique and make sense to anyone who might use (or grade) your code in the future.
  • sitenumber = the site number deliniated by the usgs. MUST BE IN QUOTES
  • service = this is the type of data you want to use. In our case we will be using daily values which can be specified by service=’dv’ .
  • startDate = the day you want to begin pulling your streamflow data from. MUST BE IN QUOTES and the format is “YYYY-MM-DD” . So September 30, 1980 would be “1980-09-30”
  • endDate = the day that you want to stop querying the USGS system for data. This is in the same format as startDate (“YYYY-MM-DD”)

### In the cell below, using the link below, detail what the difference between the functions ‘readNWISdata’ and ‘readNWISdv’.

Question 1

In the code box below I have given you an example line of code pulling in data from the Piedra River Gauge ‘09349800”. All USGS NWIS gages are specified by an (8) digit code.

Use the next 3 lines of code to read in the streaflow data from the 3 unknown sites for water year 2020 (WY2020):

  • 09402000
  • 07010000
  • 09380000
piedra = ap_readNWISdata(sites='09349800',service='dv',startDate="2019-10-01",endDate="2020-09-30")

### site2
site2name = ap_readNWISdata(sites='sitenumber',service='dv',startDate="YYYY-MM-DD",endDate="YYYY-MM-DD")

### site3
site3name = ap_readNWISdata(sites='sitenumber',service='dv',startDate="YYYY-MM-DD",endDate="YYYY-MM-DD")

### site4
site4name = ap_readNWISdata(sites='sitenumber',service='dv',startDate="YYYY-MM-DD",endDate="YYYY-MM-DD")

Run the next line of code to plot the discharge from WY2020 for all 4 gages. If you did not change the names of the sites above you do not need to do anything but hit “shift + enter” on the cell below


Question 1 Discussion

What differences do you observe in the magnitude of the discharges?

What temporal (time) differences you observe in the 4 plots above? (I.E. Do gages all have high or low discharge values at the same time periods? Are they different? How?)

Input answer here by double clicking this box and typing below. Once you are satisfied with your answer click the next button at the top of the jupyter notebook or “shift+enter” to render this cell! You can always reenter this box by double clicking on it again.

Using the answers to the questions above, make a guess to what landscapes these streamgages may represent.(I.E. High mountain stream, large lowland river, coastal creek, glacial river, etc)

Question 2

Getting Data For Multiple WY: Looking at Seasonality

A very powerful tool of the dataRetreival package is the abilty to import multiple water years worth of data for comparison.

In the cell below, using the ‘ap_multiyear’ function we are bringing in 5 water years worth of data from the Piedra River. The arguments you must use are the same as the previous question plus one additional argument:

  • numYear = which is the number of water years of data that you want to import.

Run the cell below and the function will automatically import the 5 water years of data from the Piedra River for you.

piedra_multiyear = ap_multiyear(sites='09349800',service='dv',startDate='2017-10-01',endDate='2018-09-30',numYear = 5)

In the cell below

Use the next 3 lines of code to read in the streaflow data from the 3 unknown sites starting WY2020.

Note: This program displays non-fatal errors. Ignore these :)

### site2
site2name_multiyear = ap_multiyear()

### site3
site3name_multiyear = ap_multiyear()

### site4
site4name_multiyear = ap_multiyear()

Using the summary() function from the prelab, find the median, mean, maximum, and minimum for all 4 datasets for the 5 WY period


Using the ap_multiyear_plot function below, plot all the gauges. The arguments should be:



  • gage1 = Piedra River gage
  • gage2 = whatever your gage 2 is
  • …. and so on

Question 2 Discussion

Using the summary statistics above, describe the differences between gages. (I.E. Which gage has the highest median and mean discharges, widest distribution (spread of quantiles realitive to the mean), note maximum and minimum discharges, etc)

Question 3

The final task is to look at the historical record of maximum discharge for the gages. Looking at when maximum flows occur can tell us important information about how and when these streams receieve the most streamflow generating inflow.

Below is a the function ‘ap_readNWISpeak” that reads in the maximum velcocity and corresponding date for that velocity. The only argument that is need is the USGS NWIS gage number.

The next function named “ap_peakPlot” plots 2 plots on top of each other showing the number of maximum discharge readings occuring at a given month throughout the year and a plot of year vs peak discharge values at those years. The only argument that is needed is the name of the peak discharge dataset. For example:


The cell below reads in data for the Peidra River and plots that data.

Run the cell below.

piedra_peak = ap_readNWISpeak('09349800')

Alter the next three cells of code to read in the peak values and dates for the 3 unknown gages and print them to the screen.

gage2_peak = ap_readNWISpeak()
gage4_peak = ap_readNWISpeak()
gage4_peak = ap_readNWISpeak()

Question 3 Discussion:

Why do you think that the Piedra River gage peaks at roughly the same time every year? (Hint: ) And why are the peaks variable from year to year?

If you look closely at gage 09380000 there is a date where the peak discharge changes from May - June and becomes variable. Why do you think this is? (Hint: Look at the box shape of the peaks of from the hydrograph of this site. What might caused this “boxy” behavior? Also, try using the function “ap_multiyear_plot” to plot 80-100 years of data to observe this trend)

When do most of the peak flows occur at gage 09402000? What meterological event occurs this time of year? (Hint: Look at this article or/and watch this video !)

Given even more information make another guess to what landscapes these streamgages may represent.

Final Question

This final line of code will lead us back to the question that we posed at the beginning of the lab, “Where in the World are these gages”? Execute the cell below

piedra =readNWISsite ('09349800')
unknown1 =readNWISsite ('09402000')
unknown2 =readNWISsite ('07010000')
unknown3 =readNWISsite ('09380000')


Using the search engine of your choice, the data above, and the answers to your questions above give a brief summary of:

  • What drives the shape of the hydrograph? (Snowmelt? Extreme precipitation? Dams? Glacial outbursts?)
  • The timing of maximum/minimum flows.(Are they daily, hourly, seasonal, annual, all of the above?)
  • What the primary soruce of stream flow might be for each location.

Bonus Question (1-3 pts)

Please provide 1-3 points of feedback about the lab. I would like honest feedback about things that worked, didn’t work, your thoughts on the usefullness of this demonstration,etc.