> For the complete documentation index, see [llms.txt](https://coastwatch.gitbook.io/satellite-course/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://coastwatch.gitbook.io/satellite-course/tutorials/python-tutorial/3.-extract-data-within-a-shapefile-using-erddap.md).

# 3. Extract data within a shapefile using ERDDAP

This tutorial will teach you how to extract and display SST values for a particular time period or average SST over the whole time-series available within a shapefile.\
\
The shapefile for the NOAA Marine National Monument and sanctuaries boundaries can be downloaded here: \
<http://sanctuaries.noaa.gov/library/imast_gis.html>.&#x20;

We are going to extract SST data for the Papahanaumokuakea Marine National Monument (PMNM) in Hawaii. However, because the Monument boundaries cross the dateline, the shapefile provided on the website is tricky to work with. We'll work with a cleaned up version, available here:\
<https://oceanwatch.pifsc.noaa.gov/files/PMNM_bounds.csv>

This tutorial is also available as a [Jupyter notebook](https://github.com/melhawaii/python-satellite-course/blob/master/OW_tutorial3.ipynb).

## Load packages <a href="#load-packages" id="load-packages"></a>

`import pandas as pd` \
`import numpy as np` \
`import urllib.request` \
`import xarray as xr` \
`import netCDF4 as nc` \
`from matplotlib import pyplot as plt` \
`from matplotlib.colors import LinearSegmentedColormap` \
`from shapely.geometry import Point, Polygon`\
`import geopandas as gpd`\
\
`np.warnings.filterwarnings('ignore')`

## Load the Monument boundary <a href="#load-the-monument-boundary" id="load-the-monument-boundary"></a>

`df=pd.read_csv('PMNM_bounds.csv')`

Transform the boundary to a Polygon

`geometry = [Point(xy) for xy in zip(df.lon, df.lat)]` \
`poly = Polygon([(p.x, p.y) for p in geometry])`

`poly`

![](/files/-M6g1ivmKuz-9i2bCE63)

## Data extraction <a href="#data-extraction" id="data-extraction"></a>

The example below extracts [monthly 5km CoralTemp SST](https://oceanwatch.pifsc.noaa.gov/erddap/griddap/CRW_sst_v1_0_monthly) data within the monument boundary.&#x20;

* We are going to download data from ERDDAP for the smallest bounding box that contains our polygon

`xcoord1 = (np.min(df.lon), np.max(df.lon))` \
`ycoord1 = (np.min(df.lat), np.max(df.lat))`

* let's select a date range:

`tcoord = ("2019-01-15", "2019-12-15")`

* and let's build our ERDDAP URL:

`url='`[`https://oceanwatch.pifsc.noaa.gov/erddap/griddap/CRW_sst_v1_0_monthly.nc?analysed_sst[('+`](https://oceanwatch.pifsc.noaa.gov/erddap/griddap/CRW_sst_v1_0_monthly.nc?analysed_sst\[\('+) `tcoord[0] +'):1:('+ tcoord[1] +')][('+ str(ycoord1[0]) +'):1:('+ str(ycoord1[1]) +')][(' + str(xcoord1[0]) +'):1:('+ str(xcoord1[1]) +')]'`

`url`

`'https://oceanwatch.pifsc.noaa.gov/erddap/griddap/CRW_sst_v1_0_monthly.nc?analysed_sst[(2019-01-15):1:(2019-12-15)][(19.2345832):1:(31.79786423)][(177.84422):1:(198.9827)]'`

* now we can download the data:

`urllib.request.urlretrieve(url, "sst.nc")`

* and load it as an xarray dataset:

`ds = xr.open_dataset('sst.nc',decode_cf=False)`

`ds.analysed_sst.shape`

`(12, 252, 424)`

We now have data for a box around our polygon, for 12 monthly time steps (= 1 year).

## Masking the data outside the Monument boundary

The `.within()` function from the `shapely`package checks if a point is within a polygon. We are using it to create a mask which will take the value 1 within the polygon boundary, and NaN outside.

(This takes about 1min or less to run).

`mask=np.empty((len(ds.latitude.values),len(ds.longitude.values)))`\
`mask[:]=np.NaN`\
`for i in range(len(ds.latitude.values)):` \
&#x20;  `for j in range(len(ds.longitude.values)):`\
&#x20;     `p=Point(ds.longitude.values[j],ds.latitude.values[i],)`\
&#x20;     `if int(p.within(poly))==1:` \
&#x20;        `mask[i,j]=int(p.within(poly))`

`plt.contourf(ds.longitude,ds.latitude,mask)`

![](/files/-M6g4o_HxK-Ybp53ssgn)

We now multiply the SST data we downloaded by the mask values:

`SST=ds.analysed_sst*mask`

## Plotting the data <a href="#plotting-the-data" id="plotting-the-data"></a>

The extracted data contains several time steps (months) of sst data in the monument boundaries. Let's make a plot of the 4th time step for example.

* setting up the colormap

`np.min(SST),np.max(SST)`

`array(16.863333),  array(28.78)`

`levs = np.arange(16, 29, 0.05)` \
`jet=["blue", "#007FFF", "cyan","#7FFF7F", "yellow", "#FF7F00", "red", "#7F0000"]` \
`cm = LinearSegmentedColormap.from_list('my_jet', jet, N=len(levs))`

* loading data to plot the coastline. The file can be downloaded [here](https://oceanwatch.pifsc.noaa.gov/files/gz_2010_us_outline_20m.json), and was provided by <https://eric.clst.org/tech/usgeojson/>.\
  Download the file and save it to your computer.

`country = gpd.read_file("gz_2010_us_outline_20m.json")`

* plot:

`country.plot(figsize=(12,8),color='black')`\
`plt.xlim(-183,-153)` \
`plt.ylim(18,32)` \
`cs=plt.contourf(ds.longitude-360,ds.latitude,SST[3,:,:],levs,cmap=cm)`\
`cbar=plt.colorbar(fraction=0.022)`\
`cbar.ax.tick_params(labelsize=12)`\
`cs.ax.tick_params(labelsize=12)` \
`plt.title('SST - April 2019', fontsize=20)`

![](/files/-M6g6JSja31VZkMTITJP)


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://coastwatch.gitbook.io/satellite-course/tutorials/python-tutorial/3.-extract-data-within-a-shapefile-using-erddap.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
