Source code for seagrid.sources.ocean_colour.carbon

"""Phytoplankton size and carbon products derived from OC-CCI."""

from __future__ import annotations

import pandas as pd
import xarray as xr

from seagrid.sources.base import add_history, snap_to_grid
from seagrid.sources.families import HTTPSource

__all__ = ["BICEPCarbon", "BICEPPOC", "TihoPSD"]


class _MonthlyOCProduct(HTTPSource, register=False):
    """A monthly product on an OC-CCI grid, one file per month."""

    family = "ocean_colour"
    default_timetype = "monthly"

    def _postprocess(
        self,
        ds: xr.Dataset,
        *,
        dtm: pd.Timestamp,
        data_var: str | None,
        timetype: str,
        resolution: str | None,
    ) -> xr.Dataset:
        if "time" in ds.dims and ds.sizes["time"] == 1:
            ds = ds.isel(time=0)
        ds = super()._postprocess(
            ds, dtm=dtm, data_var=data_var, timetype=timetype, resolution=resolution
        )
        return snap_to_grid(ds, self.grid(resolution))


[docs] class TihoPSD(_MonthlyOCProduct): """Particle size distribution and phytoplankton size classes (Kostadinov). Monthly, 4 km, 1998–2020, from PANGAEA (doi:10.1594/PANGAEA.939863). The files store variables as (lon, lat) with latitude descending; they are returned as (lat, lon), latitude ascending. About 900 MB per month. Settings section ``[tiho_psd]`` (2.x: ``[tiho-psd]``). Examples -------- >>> import seagrid >>> seagrid.get_source("tiho_psd").filename("2010-01-15") 'ESACCI-OC-L3S-MERGED-1M_MONTHLY_4km_Mapped_PSD_PSC_201001-fv5.0.nc' """ name = "tiho_psd" aliases = ("tiho-psd", "psd") title = "Phytoplankton size distribution and carbon (Kostadinov, PANGAEA)" def _postprocess( self, ds: xr.Dataset, *, dtm: pd.Timestamp, data_var: str | None, timetype: str, resolution: str | None, ) -> xr.Dataset: names = { "Grid_latitudes_pixel_centers": "lat", "Grid_longitudes_pixel_centers": "lon", } if all(k in ds for k in names): ds = ( ds.set_coords(list(names)) .swap_dims( { "Latitude": "Grid_latitudes_pixel_centers", "Longitude": "Grid_longitudes_pixel_centers", } ) .rename(names) ) ds = ds.transpose(..., "lat", "lon") add_history( ds, "pixel-centre coordinates renamed to lat/lon; (lat, lon) order" ) return super()._postprocess( ds, dtm=dtm, data_var=data_var, timetype=timetype, resolution=resolution )
[docs] class BICEPPOC(_MonthlyOCProduct): """BICEP particulate organic carbon, monthly, 4 km (CEDA). Settings section ``[bicep_poc]`` (2.x module ``seagrid.bicep.bicep_poc``). Examples -------- >>> import seagrid >>> seagrid.get_source("bicep_poc").filename("2010-01-15") 'BICEP_NCEO_POC_ESA-OC-L3S-MERGED-1M_MONTHLY_4km_GEO_PML_201001-fv5.0.nc' """ name = "bicep_poc" title = "BICEP particulate organic carbon, monthly 4 km (CEDA)" default_data_var = "POC"
[docs] class BICEPCarbon(_MonthlyOCProduct): """BICEP phytoplankton carbon, monthly, 9 km (CEDA). Settings section ``[bicep_cp]`` (2.x module ``seagrid.bicep.bicep_cp``). Examples -------- >>> import seagrid >>> seagrid.get_source("bicep_cp").filename("2010-01-15") 'BICEP_NCEO_PC_ESA-OC-L3S-MERGED-1M_MONTHLY_9km_mapped_201001-fv5.nc' """ name = "bicep_cp" title = "BICEP phytoplankton carbon, monthly 9 km (CEDA)"