Skip to content

Commit

Permalink
First commit
Browse files Browse the repository at this point in the history
  • Loading branch information
CoastWatch-WestCoast committed May 21, 2021
0 parents commit 38ce0d0
Show file tree
Hide file tree
Showing 31 changed files with 10,840 additions and 0 deletions.
Binary file added .DS_Store
Binary file not shown.
31 changes: 31 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@

# R Notebooks for the CoastWatch Satellite Training Course

These files are the Notebook versions of the R exercises found in the CoastWatch Satellite Course R Tutorial
https://dale-robinson.gitbook.io/coastwatch-satellite-course-may-2021/tutorials/r-tutorial

## Exercise book contents
The Notebooks contain step-by-step code examples for extracting data from the ERDDAP data servers using the rxtracto R library.
* Extract data within a boundary
* Matchups satellite data to an animal tracks
* Create timeseries from satellite data
* Matchup satellite to buoy data
* Define a marine habitat
* Reprojecting satellite and buoy data
* Mapping projected datasets

## Downloading
Download the enitre r_code repository. Subfolders contain images and CSV files used in the Notebook code.

## NOAA CoastWatch/OceanWatch/PolarWatch Program

NOAA CoastWatch/OceanWatch provides easy access for everyone to global and regional satellite data products for use in understanding, managing and protecting ocean and coastal resources and for assessing impacts of environmental change in ecosystems, weather, and climate.

![](images/cw_logo_80.png) <span style="color:blue;font-size:30px;">[CoastWatch](https://coastwatch.noaa.gov/)</span>


## CoastWatch West Coast Regional Node

These course materials were developed and are distributed by the [West Coast Node](https://coastwatch.pfeg.noaa.gov/) of NOAA CoastWatch. Information about the courses and services offered by West Coast Node can be found at the following links:

| [Courses](https://coastwatch.pfeg.noaa.gov/courses/satellite_course.html) | [Data Catalog](https://coastwatch.pfeg.noaa.gov/data.html) | [Data Server](https://coastwatch.pfeg.noaa.gov/erddapinfo/index.html) | [Xtractomatic](https://coastwatch.pfeg.noaa.gov/xtracto/) |
137 changes: 137 additions & 0 deletions define_marine_habitat.Rmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
---
title: Define a marine habitat
author: NOAA CoastWatch
date: May 20, 2021
output:
md_document:
variant: gfm
---
# Define a marine habitat
>notebook filename | 07_turtlewatch_xtracto.Rmd
The TurleWatch project investigated the thermal habitat of loggerhead sea turtles in the Pacific Ocean north of the Hawaiian Islands. Research results indicate that most loggerhead turtles stay in water between 17.5°C and 18.5°C. When the 17.5°C to 18.5°C temperature contour is drawn on a map of sea surface temperature conditions, it delineates the boundary of the loggerhead's preferred habitat.

In this exercise you will plot the thermal boundary of loggerhead sea turtles using satellite sea surface temperature. The exercise demonstrates the following techniques:
* Using xtracto_3D to extract data from a rectangular area
* Masking a data array
* Plotting maps using ggplot

## Install required packages and load libraries

```{r install, message=FALSE, warning=FALSE}
# Function to check if pkgs are installed, install missing pkgs, and load
pkgTest <- function(x)
{
if (!require(x,character.only = TRUE))
{
install.packages(x,dep=TRUE,repos='http://cran.us.r-project.org')
if(!require(x,character.only = TRUE)) stop(x, " :Package not found")
}
}
list.of.packages <- c( "ncdf4", "rerddap", "plotdap", "RCurl",
"raster", "colorRamps", "maps", "mapdata",
"ggplot2", "RColorBrewer", "rerddapXtracto")
# create list of installed packages
pkges = installed.packages()[,"Package"]
for (pk in list.of.packages) {
pkgTest(pk)
}
```

## Select the Satellite Data
* Use the MUR SST dataset (ID jplMURSST41mday)
* Gather information about the dataset (metadata) using **rerddap**
* Displays the information
```{r dataInfo}
# CHOOSE DATASET and get information about it
url = "http://coastwatch.pfeg.noaa.gov/erddap/"
dataInfo <- rerddap::info('jplMURSST41mday',url=url)
parameter <- 'sst'
```

## Get Satellite Data
* Select an area off the coast of California: longitude range of -130 to -115 east and latitude range of 25 to 40 north
* Set the time range to days withing one month: tcoord=c('2018-06-06','2018-06-08')). The values do have to be different.

```{r getdata}
# latitude and longitude of the vertices
ylim<-c(25,40)
xlim<-c(-130,-115)
# Choose an area off the coast of California
# Extract the data
SST <- rxtracto_3D(dataInfo,xcoord=xlim,ycoord=ylim,parameter=parameter,
tcoord=c('2018-06-06','2018-06-08'))
# Drop command needed to reduce SST from a 3D variable to a 2D one
SST$sst <- drop(SST$sst)
```
## Make a quick plot using plotBBox
```{r qplot}
plotBBox(SST, plotColor = 'thermal',maxpixels=100000)
```

## Define the Thermal niche of Loggerhead Turtles

__ Set the thermal range to 17.5-18.5 degrees C, as determined by the TurtleWatch program.__

```{r temp}
## Define turtle temperature range
min.temp <- 17.5
max.temp <- 18.5
```
__Create another variable for habitat temperature__

Set the habitat temperature to equal NA

```{r makeVar}
SST2 <- SST
SST2$sst[SST2$sst >= min.temp & SST2$sst <= max.temp] <- NA
plotBBox(SST2, plotColor = 'thermal',maxpixels=100000)
```

It would be nicer to color in the turtle habitat area (the NA values) with a different color. If you want to customize graphs its better to use `ggplot` than the `plotBBox` that comes with
`rerrdapXtracto` package. Here we will use `ggplot` to plot the data. But first the data is reformatted for use in `ggplot`.

Restructure the data
```{r restructure}
dims <- dim(SST2$sst)
SST2.lf <- expand.grid(x=SST$longitude,y=SST$latitude)
SST2.lf$sst<-array(SST2$sst,dims[1]*dims[2])
```

## Plot the Data using 'ggplot'
```{r plot}
coast <- map_data("worldHires", ylim = ylim, xlim = xlim)
par(mar=c(3,3,.5,.5), las=1, font.axis=10)
myplot<-ggplot(data = SST2.lf, aes(x = x, y = y, fill = sst)) +
geom_tile(na.rm=T) +
geom_polygon(data = coast, aes(x=long, y = lat, group = group), fill = "grey80") +
theme_bw(base_size = 15) + ylab("Latitude") + xlab("Longitude") +
coord_fixed(1.3,xlim = xlim, ylim = ylim) +
ggtitle(unique(as.Date(SST2$time))) +
scale_fill_gradientn(colours = rev(rainbow(12)),limits=c(10,22),na.value = "firebrick4")
myplot
```



137 changes: 137 additions & 0 deletions extract_data_marine sanctuary.Rmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
---
title: Extract data within a marine sanctuary boundary
author: NOAA CoastWatch
date: May 20, 2021
output:
md_document:
variant: gfm
---
# Extract data within a boundary

In this exercise, you will download data from within the boundaries of the Monterey Bay National Marine Sanctuary (MBNMS) and visualize the data on a map.

The exercise demonstrates the following skills:

* Using **rerddap** to retrieve information about a dataset from ERDDAP
* Using the **rxtractogon** function from **rerdapXtracto** to extract satellite data within an polygon over time
* Mapping satellite data

## Install packages and load libraries
```{r install,message=FALSE,warning=FALSE}
pkges = installed.packages()[,"Package"]
# Function to check if pkgs are installed, install missing pkgs, and load
pkgTest <- function(x)
{
if (!require(x,character.only = TRUE))
{
install.packages(x,dep=TRUE,repos='http://cran.us.r-project.org')
if(!require(x,character.only = TRUE)) stop(x, " :Package not found")
}
}
# create list of required packages
list.of.packages <- c("ncdf4", "rerddap","plotdap", "parsedate",
"sp", "ggplot2", "RColorBrewer",
"reshape2", "maps", "mapdata",
"jsonlite", "rerddapXtracto")
# Run install and load function
for (pk in list.of.packages) {
pkgTest(pk)
}
# create list of installed packages
pkges = installed.packages()[,"Package"]
```

## Load sanctuary boundary coordinates
The **rerddapXtracto** package comes with the dataset **mbnms** which conatains the longitude and latitude values for the boundary of the Monterey Bay National Marine Sanctuary. These coordinates draw the the boundary of the sanctuary on a map, like tracing a dot-to-dot drawing. Take a quick look at the contents of this data variable.

```{r mbnms}
str(mbnms)
```

Additional sanctuary boundaries may be obtained at [http://sanctuaries.noaa.gov/library/imast_gis.html](http://sanctuaries.noaa.gov/library/imast_gis.html).

**The script below:**

* Extracts the longitude and latitude data into vector variables

```{r latlon}
xcoord <- mbnms$Longitude
ycoord <- mbnms$Latitude
```
## Select the chloropyll dataset
For this example we will use a 750 m VIIRS monthly chlorophyll dataset (ID erdVHNchlamday)

**The script below:**
* Gathers information about the dataset (metadata) using **rerddap**
* The default source ERDDAP for **rerddap** is "https://upwell.pfeg.noaa.gov/erddap". Since we are pulling the data from a different ERDDAP at "http://coastwatch.pfeg.noaa.gov/erddap/", change the url to url = "http://coastwatch.pfeg.noaa.gov/erddap/"
* Displays the dataset information
```{r dataInfo}
# Use rerddap to get dataset metadata
url = "http://coastwatch.pfeg.noaa.gov/erddap/"
dataInfo <- rerddap::info('erdVHNchlamday',url=url) # N. Pacific 750 m VIIRS chl
# Display the metadata dataset info
dataInfo
```
## Set the options for the polygon data extract
The **rxtractogon** function will need the parameter and coordinates for the extract
* For the parameter: Use the name of the chlorophyll parameter that was displayed above in dataInfo: **parameter <- "chla"**
* For the coordinates: determine your selctions for x, y, z, and time.
* z coordinate: The metadata from dataInfo shows that this variable has a altitude coordinate that equals zero. So set the value of the z coordinate to zero: **zcoord <- 0.**
* time coordinate: The time variable passed to xtracogon must contain two elements, the start and endpoints of the desired time period. This example uses ERDDAP's **last** option to retrieve data from the most recent time step. The **last** option also accepts the minus **-** operator. To request the time step with the second most recent data use "last-1". In the script below the time variable (tcoord) is defined as **tcoord <- c("last-1", "last")**

```{r options}
# set the parameter to extract
parameter <- 'chla'
# set the time range
tcoord <- c("last-1", "last")
# Assign longitude and latitude vectors from the CSV file to variables
xcoord <- mbnms$Longitude
ycoord <- mbnms$Latitude
# set the altitude variable to zero
zcoord <- 0.
```
## Extract data and mask it using rxtractogon
* Run **rxtractogon** to extract data from the "erdVHNchlamday" dataset and mask out any data outside the MBNMS boundary.
* List the data
```{r octogon}
## Request the data
sanctchl <- rxtractogon (dataInfo, parameter=parameter, xcoord=xcoord, ycoord=ycoord,tcoord=tcoord,zcoord=zcoord)
## List the returned data
str(sanctchl)
```

## Choose Time to Plot
The extracted data contains two time periods of chlorophyll data within the sanctuary boundaries. For this example we will show how to select just one time period from the options and map it, here we choose the second time stamp.
```{r selecttime}
sanctchl1 <- sanctchl
sanctchl1$chla <- sanctchl1$chla[, , 2]
sanctchl1$time <- sanctchl1$time[2]
```
### Plot the data
* Use the plotBBox function in rerddapXtracto to quickly plot the data

```{r map}
plotBBox(sanctchl1, plotColor = 'algae',maxpixels=100000)
```

### Apply a function to the data
* Here we apply a log function to the chlorophyll data and plot it again.

```{r addfunc}
myFunc <- function(x) log(x)
plotBBox(sanctchl1, plotColor = 'algae',maxpixels=100000, myFunc=myFunc)
```



Binary file added images/cw_logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/cw_logo_80.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/cw_logo_notext.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/nsidc.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/sea_ice.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions main/HEAD
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ref: refs/heads/master
6 changes: 6 additions & 0 deletions main/config
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[core]
repositoryformatversion = 0
filemode = true
bare = true
ignorecase = true
precomposeunicode = true
1 change: 1 addition & 0 deletions main/description
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Unnamed repository; edit this file 'description' to name the repository.
15 changes: 15 additions & 0 deletions main/hooks/applypatch-msg.sample
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/bin/sh
#
# An example hook script to check the commit log message taken by
# applypatch from an e-mail message.
#
# The hook should exit with non-zero status after issuing an
# appropriate message if it wants to stop the commit. The hook is
# allowed to edit the commit message file.
#
# To enable this hook, rename this file to "applypatch-msg".

. git-sh-setup
commitmsg="$(git rev-parse --git-path hooks/commit-msg)"
test -x "$commitmsg" && exec "$commitmsg" ${1+"$@"}
:
24 changes: 24 additions & 0 deletions main/hooks/commit-msg.sample
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#!/bin/sh
#
# An example hook script to check the commit log message.
# Called by "git commit" with one argument, the name of the file
# that has the commit message. The hook should exit with non-zero
# status after issuing an appropriate message if it wants to stop the
# commit. The hook is allowed to edit the commit message file.
#
# To enable this hook, rename this file to "commit-msg".

# Uncomment the below to add a Signed-off-by line to the message.
# Doing this in a hook is a bad idea in general, but the prepare-commit-msg
# hook is more suited to it.
#
# SOB=$(git var GIT_AUTHOR_IDENT | sed -n 's/^\(.*>\).*$/Signed-off-by: \1/p')
# grep -qs "^$SOB" "$1" || echo "$SOB" >> "$1"

# This example catches duplicate Signed-off-by lines.

test "" = "$(grep '^Signed-off-by: ' "$1" |
sort | uniq -c | sed -e '/^[ ]*1[ ]/d')" || {
echo >&2 Duplicate Signed-off-by lines.
exit 1
}
Loading

0 comments on commit 38ce0d0

Please sign in to comment.