Mapping Cyanobacteria with PACE data¶
Install packages¶
Uncomment the following cell to install the HyperCoast package.
In [1]:
Copied!
# %pip install -U "hypercoast[extra]"
# %pip install -U "hypercoast[extra]"
Import libraries¶
In [2]:
Copied!
import earthaccess
import hypercoast
from hypercoast.pace import (
cyano_band_ratios,
apply_kmeans,
apply_pca,
apply_sam,
apply_sam_spectral,
)
import earthaccess
import hypercoast
from hypercoast.pace import (
cyano_band_ratios,
apply_kmeans,
apply_pca,
apply_sam,
apply_sam_spectral,
)
Download PACE data¶
To download and access the PACE AOP data, you will need to create an Earthdata login. You can register for an account at urs.earthdata.nasa.gov. Once you have an account, run the following cell and enter your NASA Earthdata login credentials.
In [3]:
Copied!
earthaccess.login(persist=True)
earthaccess.login(persist=True)
Out[3]:
<earthaccess.auth.Auth at 0x7f431c7e7260>
Search for PACE AOP data:
In [4]:
Copied!
results = hypercoast.search_pace(
bounding_box=(-83, 25, -81, 28),
temporal=("2024-07-30", "2024-08-15"),
short_name="PACE_OCI_L2_AOP_NRT",
count=1,
)
results = hypercoast.search_pace(
bounding_box=(-83, 25, -81, 28),
temporal=("2024-07-30", "2024-08-15"),
short_name="PACE_OCI_L2_AOP_NRT",
count=1,
)
Download PACE AOP data:
In [5]:
Copied!
hypercoast.download_pace(results[:1], out_dir="data")
hypercoast.download_pace(results[:1], out_dir="data")
Read PACE data¶
Read PACE AOP data as an xarray.Dataset
:
In [6]:
Copied!
filepath = "data/PACE_OCI.20240730T181157.L2.OC_AOP.V2_0.NRT.nc"
dataset = hypercoast.read_pace(filepath)
# dataset
filepath = "data/PACE_OCI.20240730T181157.L2.OC_AOP.V2_0.NRT.nc"
dataset = hypercoast.read_pace(filepath)
# dataset
Compute band ratios¶
In [7]:
Copied!
da = cyano_band_ratios(dataset, plot=True)
da = cyano_band_ratios(dataset, plot=True)
The spectra of cyanobacteria bloom:¶
Cyanobacteria and Spectral Angle Mapper¶
Spectral Angle Mapper: Spectral similarity Input: library of Cyanobacteria bloom Rrs spectra with Chla at different levels
Spectral Mixture Analysis: unmix different cyanobacteria species based on spectral difference.
K-means applied to the whole image¶
In [8]:
Copied!
cluster_labels, latitudes, longitudes = apply_kmeans(dataset, n_clusters=6)
cluster_labels, latitudes, longitudes = apply_kmeans(dataset, n_clusters=6)
K-means applied to selected pixels¶
In [9]:
Copied!
da = dataset["Rrs"]
filter_condition = (
(da.sel(wavelength=650) > da.sel(wavelength=620))
& (da.sel(wavelength=701) > da.sel(wavelength=681))
& (da.sel(wavelength=701) > da.sel(wavelength=450))
)
extent = [-95, -85, 27, 33]
colors = ["#e41a1c", "#377eb8", "#4daf4a", "#f781bf", "#a65628", "#984ea3"]
cluster_labels, latitudes, longitudes = apply_kmeans(
da, n_clusters=6, filter_condition=filter_condition, extent=extent, colors=colors
)
da = dataset["Rrs"]
filter_condition = (
(da.sel(wavelength=650) > da.sel(wavelength=620))
& (da.sel(wavelength=701) > da.sel(wavelength=681))
& (da.sel(wavelength=701) > da.sel(wavelength=450))
)
extent = [-95, -85, 27, 33]
colors = ["#e41a1c", "#377eb8", "#4daf4a", "#f781bf", "#a65628", "#984ea3"]
cluster_labels, latitudes, longitudes = apply_kmeans(
da, n_clusters=6, filter_condition=filter_condition, extent=extent, colors=colors
)
Principal Component Analysis (PCA)¶
In [10]:
Copied!
pca_data = apply_pca(dataset, n_components=3, x_component=0, y_component=1)
pca_data = apply_pca(dataset, n_components=3, x_component=0, y_component=1)
In [11]:
Copied!
pca_data = apply_pca(dataset, n_components=3, x_component=1, y_component=2)
pca_data = apply_pca(dataset, n_components=3, x_component=1, y_component=2)
In [12]:
Copied!
data, latitudes, longitudes = apply_sam(
dataset,
n_components=3,
n_clusters=6,
)
data, latitudes, longitudes = apply_sam(
dataset,
n_components=3,
n_clusters=6,
)
Apply SAM to selected pixels¶
In [13]:
Copied!
extent = [-95, -85, 27, 33]
colors = ["#377eb8", "#ff7f00", "#4daf4a", "#f781bf", "#a65628", "#984ea3"]
data, latitudes, longitudes = apply_sam(
dataset,
n_components=3,
n_clusters=6,
extent=extent,
colors=colors,
)
extent = [-95, -85, 27, 33]
colors = ["#377eb8", "#ff7f00", "#4daf4a", "#f781bf", "#a65628", "#984ea3"]
data, latitudes, longitudes = apply_sam(
dataset,
n_components=3,
n_clusters=6,
extent=extent,
colors=colors,
)
Apply SAM with a filtering condition¶
In [14]:
Copied!
da = dataset["Rrs"]
filter_condition = (
(da.sel(wavelength=650) > da.sel(wavelength=620))
& (da.sel(wavelength=701) > da.sel(wavelength=681))
& (da.sel(wavelength=701) > da.sel(wavelength=450))
)
extent = [-95, -85, 27, 33]
colors = ["#e41a1c", "#377eb8", "#4daf4a", "#f781bf", "#a65628", "#984ea3"]
data, latitudes, longitudes = apply_sam(
dataset,
n_components=3,
n_clusters=6,
filter_condition=filter_condition,
extent=extent,
colors=colors,
)
da = dataset["Rrs"]
filter_condition = (
(da.sel(wavelength=650) > da.sel(wavelength=620))
& (da.sel(wavelength=701) > da.sel(wavelength=681))
& (da.sel(wavelength=701) > da.sel(wavelength=450))
)
extent = [-95, -85, 27, 33]
colors = ["#e41a1c", "#377eb8", "#4daf4a", "#f781bf", "#a65628", "#984ea3"]
data, latitudes, longitudes = apply_sam(
dataset,
n_components=3,
n_clusters=6,
filter_condition=filter_condition,
extent=extent,
colors=colors,
)
Use spectral library¶
In [15]:
Copied!
filepath = "data/PACE_OCI.20240730T181157.L2.OC_AOP.V2_0.NRT.nc"
dataset = hypercoast.read_pace(filepath)
url = "https://github.com/opengeos/datasets/releases/download/hypercoast/SAM_spectral_library.zip"
hypercoast.download_file(url)
spectral_library = "./SAM_spectral_library/*.csv"
filepath = "data/PACE_OCI.20240730T181157.L2.OC_AOP.V2_0.NRT.nc"
dataset = hypercoast.read_pace(filepath)
url = "https://github.com/opengeos/datasets/releases/download/hypercoast/SAM_spectral_library.zip"
hypercoast.download_file(url)
spectral_library = "./SAM_spectral_library/*.csv"
In [16]:
Copied!
extent = [-95, -85, 27, 33]
data, latitudes, longitudes = apply_sam_spectral(
dataset,
spectral_library=spectral_library,
extent=extent,
)
extent = [-95, -85, 27, 33]
data, latitudes, longitudes = apply_sam_spectral(
dataset,
spectral_library=spectral_library,
extent=extent,
)
In [17]:
Copied!
da = dataset["Rrs"]
extent = [-95, -85, 27, 33]
filter_condition = (
(da.sel(wavelength=650) > da.sel(wavelength=620))
& (da.sel(wavelength=701) > da.sel(wavelength=681))
& (da.sel(wavelength=701) > da.sel(wavelength=450))
)
data, latitudes, longitudes = apply_sam_spectral(
da,
spectral_library=spectral_library,
filter_condition=filter_condition,
extent=extent,
)
da = dataset["Rrs"]
extent = [-95, -85, 27, 33]
filter_condition = (
(da.sel(wavelength=650) > da.sel(wavelength=620))
& (da.sel(wavelength=701) > da.sel(wavelength=681))
& (da.sel(wavelength=701) > da.sel(wavelength=450))
)
data, latitudes, longitudes = apply_sam_spectral(
da,
spectral_library=spectral_library,
filter_condition=filter_condition,
extent=extent,
)