from IPython.display import clear_output
import pandas as pd
import numpy as np
import os
import warnings
import cartopy.crs as ccrs
import cartopy.feature as cfeature
from cartopy.mpl.ticker import LongitudeFormatter, LatitudeFormatter
import matplotlib.pyplot as plt
import xarray as xr
'ignore') warnings.filterwarnings(
Matchup satellite data to ship, glider, or animal tracks
Overview
History | Updated Mar 2024
In this exercise you will extract satellite data around a set of points defined by longitude, latitude, and time coordinates, like that produced by an animal telemetry tag, and ship track, or a glider tract.
Please note that there may be more efficient ways, more Pythonic ways, to accomplish the tasks in this tutorial. The tutorial was developed to be easier to follow for less experienced users of Python.
The exercise demonstrates the following techniques:
- Loading data from a tab- or comma-separated file
- Plotting the latitude/longitude points onto a map
- Extracting satellite data along a track
- Saving results as a CSV file
- Plotting the satellite data onto a map
Datasets used:
- Chlorophyll a concentration from the European Space Agency’s Ocean Colour Climate Change Initiative Monthly dataset v6.0
- A loggerhead turtle telemetry track that has been subsample to reduce the data requests needed for this tutorial from over 1200 to 25. The turtle was raised in captivity in Japan, then tagged and released on 05/04/2005 in the Central Pacific. Its tag transmitted for over 3 years and went all the way to the Southern tip of Baja California. The track data can be downloaded from
data
folder in this project folder.
Python packages used:
- pandas (reading and analyzing data)
- numpy (data analysis, manipulation)
- xarray (multi-dimensional data analysis, manipulation)
- matplotlib (mapping)
- cartopy (mapping)
- datetime (date manipulation)
Import the required Python modules
Load the track data into a Pandas data frame
Below, the track data will load using the Pandas “read_csv” method. * Then use the “.head()” method to view the column names and the first few rows of data.
= os.path.join('..',
track_path 'data',
'25317_05_subsampled.dat')
= pd.read_csv(track_path)
df print(df.head(2))
mean_lon mean_lat year month day
0 176.619433 32.678728 2005 5 4
1 175.860895 35.057734 2005 6 23
Plot the track on a map
=(14, 10))
plt.figure(figsize
# Label axes of a Plate Carree projection with a central longitude of 180:
= plt.subplot(211, projection=ccrs.PlateCarree(central_longitude=180))
ax1
# Use the lon and lat ranges to set the extent of the map
# the 120, 260 lon range will show the whole Pacific
# the 15, 55 lat range with capture the range of the data
120, 260, 15, 55], ccrs.PlateCarree())
ax1.set_extent([
# Set the tick marks to be slightly inside the map extents
range(125, 255, 20), crs=ccrs.PlateCarree())
ax1.set_xticks(range(20, 60, 10), crs=ccrs.PlateCarree())
ax1.set_yticks(
# Add feature to the map
='0.6')
ax1.add_feature(cfeature.LAND, facecolor
ax1.coastlines()
# Format the lat and lon axis labels
= LongitudeFormatter(zero_direction_label=True)
lon_formatter = LatitudeFormatter()
lat_formatter
ax1.xaxis.set_major_formatter(lon_formatter)
ax1.yaxis.set_major_formatter(lat_formatter)
# Bring the lon and lat data into a numpy array
= df.mean_lon.to_numpy(), df.mean_lat.to_numpy()
x, y = plt.plot(x, y, transform=ccrs.PlateCarree(), color='k')
ax1 # start point in green star
= plt.plot(x[0], y[0],
ax1 ='*',
marker='g',
color=ccrs.PlateCarree(),
transform=10)
markersize# end point in red X
= plt.plot(x[-1], y[-1],
ax1 ='X',
marker='r',
color=ccrs.PlateCarree(),
transform=10)
markersize'Animal Track for Turtle #25317', fontsize=20)
plt.title(
plt.show()
Prepare track data for use to extract satellite data
Create a column with Pandas date objects
Reload the “25317_05_subsampled.dat”. This time we will use the “parse_dates” option to create a Pandas date object column (year_month_day) from the ‘year’, ‘month’, and ‘day’ columns.
= pd.read_csv(track_path,
df =[['year', 'month', 'day']]
parse_dates
)
print('The new year_month_day column contains the Pandas date objects')
print(df.head(2))
print(df.dtypes)
The new year_month_day column contains the Pandas date objects
year_month_day mean_lon mean_lat
0 2005-05-04 176.619433 32.678728
1 2005-06-23 175.860895 35.057734
year_month_day datetime64[ns]
mean_lon float64
mean_lat float64
dtype: object
Extract data from a satellite dataset corresponding to points on the track
We are going to download data from an ERDDAP server using the following steps: * Select a dataset on an ERDDAP server * Open the dataset using the Xarray module * Loop though the track data and pull out the date, latitude and longitude coordinates from each row * Insert these coordinates into the Xarray open-dataset object to select and download the satellite data that corresponds to the coordinates. * Store the satellite data in a temporary Pandas data frame * Once all the satellite data has been added to the temporary data frame, merge it with the track data frame.
Select a dataset
We’ll use the European Space Agency’s OC-CCI product (https://climate.esa.int/en/projects/ocean-colour/) to obtain chlorophyll data. This is a merged product that blends data from many ocean color sensors to create a long time series (1997-present) with better spatial coverage than any single sensor.
Ideally we would use a daily dataset, selecting the day that corresponds to the track data date. However, chlorophyll measurements can have a lot of missing data, primarily due to cloud cover. To reduce data gaps and improve the likelihood of data for our matchups, we can use a dataset that combines all of the data from each month into the monthly average.
The ERDDAP URL to the monthly version of the OC-CCI product is below:
https://oceanwatch.pifsc.noaa.gov/erddap/griddap/esa-cci-chla-monthly-v6-0
Open the satellite data in Xarray
- Use the ERDDAP URL with no extension (e.g. without .html or .graph…). This is the OPeNDAP URL, which allows viewing the dataset metadata and, when you select the data you want, downloading the data.
- Use the Xarray “open_dataset” function then view the metadata
= '/'.join(['https://oceanwatch.pifsc.noaa.gov',
erddap_url 'erddap',
'griddap',
'esa-cci-chla-monthly-v6-0'])
= xr.open_dataset(erddap_url)
ds ds
<xarray.Dataset> Size: 472GB Dimensions: (time: 316, latitude: 4320, longitude: 8640) Coordinates: * time (time) datetime64[ns] 3kB 1997-09-04 ... 2023-12-01 * latitude (latitude) float64 35kB 89.98 89.94 ... -89.94 -89.98 * longitude (longitude) float64 69kB 0.02083 0.0625 ... 359.9 360.0 Data variables: chlor_a (time, latitude, longitude) float32 47GB ... MERIS_nobs_sum (time, latitude, longitude) float32 47GB ... MODISA_nobs_sum (time, latitude, longitude) float32 47GB ... OLCI_A_nobs_sum (time, latitude, longitude) float32 47GB ... OLCI_B_nobs_sum (time, latitude, longitude) float32 47GB ... SeaWiFS_nobs_sum (time, latitude, longitude) float32 47GB ... VIIRS_nobs_sum (time, latitude, longitude) float32 47GB ... chlor_a_log10_bias (time, latitude, longitude) float32 47GB ... chlor_a_log10_rmsd (time, latitude, longitude) float32 47GB ... total_nobs_sum (time, latitude, longitude) float32 47GB ... Attributes: (12/53) cdm_data_type: Grid comment: See summary attribute Conventions: CF-1.7, COARDS, ACDD-1.3 creation_date: Thu Jan 18 09:04:18 2024 creator_email: help@esa-oceancolour-cci.org creator_name: Plymouth Marine Laboratory ... ... time_coverage_end: 2023-12-01T00:00:00Z time_coverage_resolution: P1M time_coverage_start: 1997-09-04T00:00:00Z title: Chlorophyll a concentration, ESA OC CC... tracking_id: abd52a4c-7009-464f-b1eb-958f7d333a1d Westernmost_Easting: 0.020833333333314386
Opening the dataset in Xarray lets you look at the dataset metadata.
* The metadata are listed above. * No data is downloaded until you request it.
From the metadata you can view: * The coordinates (time, latitude and longitude) that you will use to select the data to download. * A list of ten data variables. For this exercise, we want the “chlor_a” variable. If you want, you can find out about each variable with clicking the page icon to the right of each variable name.
A note on dataset selection
We have preselected the OC-CCI monthly dataset because we know it will work with this exercise. If you were selecting datasets on your own, you would want to check out the dataset to determine if its spatial and temporal coverages are suitable for your application.
You can find that information above by clicking the right arrow next to “Attribute”. Then look through the list to find: * ‘time_coverage_start’ and ‘time_coverage_end’: the time range * ‘geospatial_lat_min’ and ‘geospatial_lat_max’: the latitude range * ‘geospatial_lon_min’ and ‘geospatial_lon_max’: the longitude range
There are a lot of metadata attributes to look through. We can make it easier with a little code to print out the metadata of interest. Then compare these ranges to those found in your track data.
print('Temporal and spatial ranges of the satellite dataset')
print('time range', ds.attrs['time_coverage_start'],
'time_coverage_end'])
ds.attrs[print('latitude range', ds.attrs['geospatial_lat_min'],
'geospatial_lat_max'])
ds.attrs[print('longitude range', ds.attrs['geospatial_lon_min'],
'geospatial_lon_max'])
ds.attrs[print(' ')
print('Temporal and spatial ranges of the track data')
print('time range', df.year_month_day.min(), df.year_month_day.max())
print('latitude range',
round(df.mean_lat.min(), 2), round(df.mean_lat.max(), 2))
print('longitude range',
round(df.mean_lon.min(), 2), round(df.mean_lon.max(), 2))
Temporal and spatial ranges of the satellite dataset
time range 1997-09-04T00:00:00Z 2023-12-01T00:00:00Z
latitude range -89.97916666666666 89.97916666666667
longitude range 0.020833333333314386 359.97916666666663
Temporal and spatial ranges of the track data
time range 2005-05-04 00:00:00 2008-08-16 00:00:00
latitude range 23.72 41.77
longitude range 175.86 248.57
Download the satellite data that corresponds to each track location
# Create a temporary Pandas data frame to hold the downloaded satellite data
= ["erddap_date", "matched_lat", "matched_lon", "matched_chla"]
col_names = pd.DataFrame(columns=col_names)
tot
# Finish each URL and download
for i in range(0, len(df)):
=True)
clear_output(waitprint(i+1, 'of', len(df))
# Crop the dataset to include data that corresponds to track locations
= ds['chlor_a'].sel(time=df.year_month_day[i],
cropped_ds =df.mean_lat[i],
latitude=df.mean_lon[i],
longitude='nearest'
method
)
# Downloaded the data and add it to a new line in the tot data frame
len(tot.index)] = [cropped_ds.time.values,
tot.loc[round(cropped_ds.latitude.values, 5), # round 5 dec
np.round(cropped_ds.longitude.values, 5), # round 5 dec
np.round(cropped_ds.values, 2) # round 2 decimals
np.
]
print(tot.loc[[len(tot)-1]])
2) tot.head(
25 of 25
erddap_date matched_lat matched_lon matched_chla
24 2008-08-01 26.77083 245.77083 0.71
erddap_date | matched_lat | matched_lon | matched_chla | |
---|---|---|---|---|
0 | 2005-05-01 | 32.6875 | 176.60417 | 0.29 |
1 | 2005-07-01 | 35.0625 | 175.85417 | 0.11 |
Consolidate the downloaded satellite data into the track data frame
'matched_lat',
df[['matched_lon',
'matched_chla',
'erddap_date']] = tot[['matched_lat',
'matched_lon',
'matched_chla',
'erddap_date']]
2) df.head(
year_month_day | mean_lon | mean_lat | matched_lat | matched_lon | matched_chla | erddap_date | |
---|---|---|---|---|---|---|---|
0 | 2005-05-04 | 176.619433 | 32.678728 | 32.6875 | 176.60417 | 0.29 | 2005-05-01 |
1 | 2005-06-23 | 175.860895 | 35.057734 | 35.0625 | 175.85417 | 0.11 | 2005-07-01 |
Save your work
'chl_matchup_turtle25327.csv', index=False, encoding='utf-8') df.to_csv(
Plot chlorophyll matchup data onto a map
First plot a histogram of the chlorophyll data
print('Range:', df.matched_chla.min(), df.matched_chla.max())
= df.matched_chla.hist(bins=40) _
Range: 0.06 0.71
The range of chlorophyll values can be large, with lots of very low values and a few very high values. Using a linear color bar, most of the lower values would have the same color. * To better visualize the data, we often plot the log or log10 of chlorophyll.
Plot a histogram of the log of the chlorophyll data
print('Range:', np.log(df.matched_chla.min()), np.log(df.matched_chla.max()))
= np.log(df.matched_chla).hist(bins=40) _
Range: -2.8134108 -0.34249032
- The logarithmic transformation displays the range of values across the color bar range (above).
Map the chlorophyll data
=(14, 10))
plt.figure(figsize
# Label axes of a Plate Carree projection with a central longitude of 180:
# set the projection
= plt.subplot(211, projection=ccrs.PlateCarree(central_longitude=180))
ax1
# Use the lon and lat ranges to set the extent of the map
# the 120, 260 lon range will show the whole Pacific
# the 15, 55 lat range with capture the range of the data
120,255, 15, 55], ccrs.PlateCarree())
ax1.set_extent([
# set the tick marks to be slightly inside the map extents
range(120,255,20), crs=ccrs.PlateCarree())
ax1.set_xticks(range(20,50,10), crs=ccrs.PlateCarree())
ax1.set_yticks(
# Add geographical features
='0.6')
ax1.add_feature(cfeature.LAND, facecolor
ax1.coastlines()
# format the lat and lon axis labels
= LongitudeFormatter(zero_direction_label=True)
lon_formatter = LatitudeFormatter()
lat_formatter
ax1.xaxis.set_major_formatter(lon_formatter)
ax1.yaxis.set_major_formatter(lat_formatter)
# build and plot coordinates onto map
= list(df.mean_lon),list(df.mean_lat)
x,y = plt.scatter(x, y, transform=ccrs.PlateCarree(),
ax1 ='o',
marker=np.log(df.matched_chla),
c=plt.get_cmap('jet')
cmap
)=plt.plot(x[0],y[0],marker='*', transform=ccrs.PlateCarree(), markersize=10)
ax1=plt.plot(x[-1],y[-1],marker='X', transform=ccrs.PlateCarree(), markersize=10)
ax1
# control color bar values spacing
= np.arange(-2.5, 0, 0.5)
levs2 =plt.colorbar(ticks=levs2, shrink=0.75, aspect=10)
cbar"Chl a (mg/m^3)", size=15, labelpad=20)
cbar.set_label(
# set the labels to be exp(levs2) so the label reflect values of chl-a, not log(chl-a)
2), size=10)
cbar.ax.set_yticklabels(np.around(np.exp(levs2),
"Chlorophyll Matchup to Animal Track #25317", size=15)
plt.title( plt.show()
On your own!
Exercise 1:
Repeat the steps above with a different dataset. For example, extract sea surface temperature data using the NOAA Geo-polar Blended Analysis SST, GHRSST dataset: https://coastwatch.pfeg.noaa.gov/erddap/griddap/nesdisGeoPolarSSTN5NRT_Lon0360.html
* This dataset is a different ERDDAP; It has a different base URL and dataset ID and variable name. * Check the metadata to make sure the dataset covers the spatial and temporal range of the track dataset.
Exercise 2:
Go to an ERDDAP of your choice, find a dataset of interest, generate the URL, copy it and edit the script above to run a match up on that dataset. To find other ERDDAP servers, you can use this search engine: http://erddap.com/
* This dataset will likely be on a different base URL and dataset ID and variable name. * Check the metadata to make sure the dataset covers the spatial and temporal range of the track dataset.
Optional
Repeat the steps above with a daily version of the OC-CCI dataset to see how cloud cover can reduce the data you retrieve. https://oceanwatch.pifsc.noaa.gov/erddap/griddap/CRW_sst_v1_0.html