index
Python Tutorial - How to work with OceanWatch data in Python
This tutorial will show the steps to grab data in ERDDAP from Python, how to work with NetCDF files in Python and how to make some maps and time-series od chlorophyll-a concentration around the main Hawaiian islands
1. Downlading data from Python
Because ERDDAP includes RESTful services, you can download data listed on any ERDDAP platform from R using the URL structure. For example, the following page allows you to subset monthly Chlorophyll a data from the Aqua-MODIS sensor https://oceanwatch.pifsc.noaa.gov/erddap/griddap/OceanWatch_aqua_chla_monthly.html. Select your region and date range of interest, then select the '.nc' (NetCDF) file type and click on "Just Generate the URL".
In this specific example, the URL we generated is : https://oceanwatch.pifsc.noaa.gov/erddap/griddap/CRW_sst_v1_0_monthly.nc?analysed_sst[(2018-01-01T12:00:00Z):1:(2018-12-01T12:00:00Z)][(17):1:(30)][(195):1:(210)]
In Python, run the following to download the data using the generated URL :
import urllib.request
url="https://oceanwatch.pifsc.noaa.gov/erddap/griddap/CRW_sst_v1_0_monthly.nc?analysed_sst[(2018-01-01T12:00:00Z):1:(2018-12-01T12:00:00Z)][(17):1:(30)][(195):1:(210)]"
urllib.request.urlretrieve(url, "sst.nc")('sst.nc', <http.client.HTTPMessage at 0x2a195863be0>)2. Importing NetCDF4 data in Python
Now that we've downloaded the data locally, we can import it and extract our variables of interest.
The xarray package makes it very convenient to work with NetCDF files. Documentation is available here: http://xarray.pydata.org/en/stable/why-xarray.html
import xarray as xr
import netCDF4 as ncOpen the file and load it as an xarray dataset:
examine the data structure:
examine which coordinates and variables are included in the dataset:
examine the structure of analysed_sst:
Our dataset is a 3-D array with 261 rows corresponding to latitudes and 301 columns corresponding to longitudes, for each of the 12 time steps.
get the dates for each time step:
Working with the extracted data
Creating a map for one time step
Let's create a map of SST for January 2018 (our first time step).
set some color breaks
define a color palette
set color scale using the jet palette
plot the SST map
Plotting a time series
Let's pick the following box : 18-23N, 200-206E. We are going to generate a time series of mean SST within that box.
first, let subset our data:
let's plot the subset:
let's compute the monthly mean over the bounding region:
let's plot the time-series:
Creating a map of average SST over a year
let's compute the yearly mean for the region:
let's plot the map of the 2018 average SST in the region:
Last updated
Was this helpful?