Skip to content

Commit

Permalink
Merge branch 'ecdr-at-25km' into ecdr-at-25km-ss
Browse files Browse the repository at this point in the history
  • Loading branch information
trey-stafford committed Sep 12, 2024
2 parents ef8fcfb + 74bfd45 commit 94eba01
Show file tree
Hide file tree
Showing 35 changed files with 757 additions and 695 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
# v0.2.0

* Produce 25km CDR instead of 12.5km.
* Refactor how platforms are handled to support overriding platform start dates
via yaml configuration files.


# v0.1.0

* Initial version of the ECDR.
4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
[project]
name = "seaice_ecdr"
version = "0.1.0"
version = "0.2.0"

[tool.bumpversion]
current_version = "0.1.0"
current_version = "0.2.0"
commit = false
tag = false

Expand Down
2 changes: 1 addition & 1 deletion seaice_ecdr/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

from seaice_ecdr.constants import LOGS_DIR

__version__ = "v0.1.0"
__version__ = "v0.2.0"

DEFAULT_LOG_LEVEL = "INFO"

Expand Down
11 changes: 0 additions & 11 deletions seaice_ecdr/_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,3 @@

# In kilometers.
ECDR_SUPPORTED_RESOLUTIONS = Literal["12.5", "25"]

# Supported sats
SUPPORTED_SAT = Literal[
"am2", # AMSR2
"ame", # AMSRE
"F17", # SSMIS F17
"F13", # SSMI F13
"F11", # SSMI F11
"F08", # SSMI F08
"n07", # Nimbus-7 SMMR
]
22 changes: 11 additions & 11 deletions seaice_ecdr/ancillary.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
from seaice_ecdr._types import ECDR_SUPPORTED_RESOLUTIONS
from seaice_ecdr.constants import CDR_ANCILLARY_DIR
from seaice_ecdr.grid_id import get_grid_id
from seaice_ecdr.platforms import SUPPORTED_SAT, get_platform_by_date
from seaice_ecdr.platforms import PLATFORM_CONFIG, Platform
from seaice_ecdr.platforms.config import N07_PLATFORM

ANCILLARY_SOURCES = Literal["CDRv4", "CDRv5"]

Expand Down Expand Up @@ -96,7 +97,6 @@ def get_surfacetype_da(
hemisphere: Hemisphere,
resolution: ECDR_SUPPORTED_RESOLUTIONS,
ancillary_source: ANCILLARY_SOURCES,
platform: SUPPORTED_SAT,
) -> xr.DataArray:
"""Return a dataarray with surface type information for this date."""
ancillary_ds = get_ancillary_ds(
Expand All @@ -111,7 +111,8 @@ def get_surfacetype_da(
polehole_surface_type = 100
if "polehole_bitmask" in ancillary_ds.data_vars.keys():
polehole_bitmask = ancillary_ds.polehole_bitmask
polehole_bitlabel = f"{platform}_polemask"
platform = PLATFORM_CONFIG.get_platform_by_date(date)
polehole_bitlabel = f"{platform.id}_polemask"
polehole_bitvalue = bitmask_value_for_meaning(
var=polehole_bitmask,
meaning=polehole_bitlabel,
Expand Down Expand Up @@ -174,8 +175,8 @@ def nh_polehole_mask(
*,
date: dt.date,
resolution: ECDR_SUPPORTED_RESOLUTIONS,
sat=None,
ancillary_source: ANCILLARY_SOURCES,
platform: Platform | None = None,
) -> xr.DataArray:
"""Return the northern hemisphere pole hole mask for the given date and resolution."""
ancillary_ds = get_ancillary_ds(
Expand All @@ -186,12 +187,12 @@ def nh_polehole_mask(

polehole_bitmask = ancillary_ds.polehole_bitmask

if sat is None:
sat = get_platform_by_date(
if platform is None:
platform = PLATFORM_CONFIG.get_platform_by_date(
date=date,
)

polehole_bitlabel = f"{sat}_polemask"
polehole_bitlabel = f"{platform.id}_polemask"
polehole_bitvalue = bitmask_value_for_meaning(
var=polehole_bitmask,
meaning=polehole_bitlabel,
Expand Down Expand Up @@ -280,22 +281,21 @@ def get_invalid_ice_mask(
hemisphere: Hemisphere,
date: dt.date,
resolution: ECDR_SUPPORTED_RESOLUTIONS,
platform: SUPPORTED_SAT,
ancillary_source: ANCILLARY_SOURCES,
platform: Platform,
) -> xr.DataArray:
"""Return an invalid ice mask for the given date.
SMMR (n07) uses a day-of-year based climatology. All other platforms use a
month-based mask.
"""
# SMMR / n07 case:
if platform == "n07":
if platform == N07_PLATFORM:
# TODO: Daily (SMMR) mask is used at end for cleanup,
# but not for initial TB field generation
# Skip the smmr invalid ice mask for now...
print("WARNING: Using non-SMMR invalid ice masks")
# return get_smmr_invalid_ice_mask(hemisphere=hemisphere, date=date, resolution=resolution, ancillary_source=ancillary_source)

# return get_smmr_invalid_ice_mask(hemisphere=hemisphere, date=date)
# All other platforms:
ancillary_ds = get_ancillary_ds(
hemisphere=hemisphere,
Expand Down
12 changes: 4 additions & 8 deletions seaice_ecdr/complete_daily_ecdr.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,7 @@
date_in_nh_melt_season,
melting,
)
from seaice_ecdr.platforms import (
get_platform_by_date,
)
from seaice_ecdr.platforms import PLATFORM_CONFIG
from seaice_ecdr.set_daily_ncattrs import finalize_cdecdr_ds
from seaice_ecdr.spillover import LAND_SPILL_ALGS
from seaice_ecdr.temporal_composite_daily import get_tie_filepath, make_tiecdr_netcdf
Expand Down Expand Up @@ -76,19 +74,19 @@ def get_ecdr_filepath(
is_nrt: bool,
) -> Path:
"""Return the complete daily eCDR file path."""
platform = get_platform_by_date(date)
platform = PLATFORM_CONFIG.get_platform_by_date(date)
if is_nrt:
ecdr_filename = nrt_daily_filename(
hemisphere=hemisphere,
date=date,
sat=platform,
platform_id=platform.id,
resolution=resolution,
)
else:
ecdr_filename = standard_daily_filename(
hemisphere=hemisphere,
date=date,
sat=platform,
platform_id=platform.id,
resolution=resolution,
)

Expand Down Expand Up @@ -443,12 +441,10 @@ def _add_surfacetype_da(
# The methodology here should be reviewed to see if there is
# a "better" way to add a geo-referenced dataarray to an existing
# xr Dataset.
platform = get_platform_by_date(date)
surfacetype_da = get_surfacetype_da(
date=date,
hemisphere=hemisphere,
resolution=resolution,
platform=platform,
ancillary_source=ancillary_source,
)
# Force use of the cde_ds coords instead of the x, y, time vars
Expand Down
10 changes: 6 additions & 4 deletions seaice_ecdr/daily_aggregate.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@
from seaice_ecdr.constants import DEFAULT_BASE_OUTPUT_DIR
from seaice_ecdr.nc_attrs import get_global_attrs
from seaice_ecdr.nc_util import concatenate_nc_files
from seaice_ecdr.platforms import get_first_platform_start_date
from seaice_ecdr.platforms import PLATFORM_CONFIG
from seaice_ecdr.util import (
get_complete_output_dir,
sat_from_filename,
platform_id_from_filename,
standard_daily_aggregate_filename,
)

Expand All @@ -37,7 +37,9 @@ def _get_daily_complete_filepaths_for_year(
resolution: ECDR_SUPPORTED_RESOLUTIONS,
) -> list[Path]:
data_list = []
start_date = max(dt.date(year, 1, 1), get_first_platform_start_date())
start_date = max(
dt.date(year, 1, 1), PLATFORM_CONFIG.get_first_platform_start_date()
)
for period in pd.period_range(start=start_date, end=dt.date(year, 12, 31)):
expected_fp = get_ecdr_filepath(
date=period.to_timestamp().date(),
Expand Down Expand Up @@ -115,7 +117,7 @@ def _update_ncrcat_daily_ds(
temporality="daily",
aggregate=True,
source=", ".join([fp.name for fp in daily_filepaths]),
sats=[sat_from_filename(fp.name) for fp in daily_filepaths],
platform_ids=[platform_id_from_filename(fp.name) for fp in daily_filepaths],
)
ds.attrs = daily_aggregate_ds_global_attrs

Expand Down
45 changes: 22 additions & 23 deletions seaice_ecdr/initial_daily_ecdr.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
from pm_tb_data._types import NORTH, Hemisphere
from pm_tb_data.fetch.nsidc_0001 import NSIDC_0001_SATS

from seaice_ecdr._types import ECDR_SUPPORTED_RESOLUTIONS, SUPPORTED_SAT
from seaice_ecdr._types import ECDR_SUPPORTED_RESOLUTIONS
from seaice_ecdr.ancillary import (
ANCILLARY_SOURCES,
get_empty_ds_with_time,
Expand All @@ -42,7 +42,7 @@
from seaice_ecdr.cli.util import datetime_to_date
from seaice_ecdr.constants import CDR_ANCILLARY_DIR, DEFAULT_BASE_OUTPUT_DIR
from seaice_ecdr.grid_id import get_grid_id
from seaice_ecdr.platforms import get_platform_by_date
from seaice_ecdr.platforms import PLATFORM_CONFIG, SUPPORTED_PLATFORM_ID
from seaice_ecdr.regrid_25to12 import reproject_ideds_25to12
from seaice_ecdr.spillover import LAND_SPILL_ALGS, land_spillover
from seaice_ecdr.tb_data import (
Expand All @@ -54,8 +54,8 @@
from seaice_ecdr.util import get_intermediate_output_dir, standard_daily_filename


def platform_is_smmr(platform):
return platform in ("n07", "s36")
def platform_is_smmr(platform_id: SUPPORTED_PLATFORM_ID):
return platform_id in ("n07", "s36")


def cdr_bootstrap_raw(
Expand All @@ -64,7 +64,7 @@ def cdr_bootstrap_raw(
tb_h37: npt.NDArray,
tb_v19: npt.NDArray,
bt_coefs,
platform: SUPPORTED_SAT,
platform: SUPPORTED_PLATFORM_ID,
):
"""Generate the raw bootstrap concentration field.
Note: tb fields should already be transformed before
Expand Down Expand Up @@ -205,7 +205,7 @@ def _setup_ecdr_ds(
ecdr_ide_ds.attrs["data_source"] = tb_data.data_source

# Set the platform
ecdr_ide_ds.attrs["platform"] = tb_data.platform
ecdr_ide_ds.attrs["platform"] = tb_data.platform_id

file_date = dt.date(1970, 1, 1) + dt.timedelta(
days=int(ecdr_ide_ds.variables["time"].data)
Expand Down Expand Up @@ -407,7 +407,7 @@ def compute_initial_daily_ecdr_dataset(
# The CDRv4 calculation causes TB to be zero/missing where
# no sea ice can occur because of invalid region or land
logger.debug(f"Applying invalid ice mask to TB field: {tb_si_varname}")
platform = get_platform_by_date(date)
platform = PLATFORM_CONFIG.get_platform_by_date(date)
invalid_ice_mask = get_invalid_ice_mask(
hemisphere=hemisphere,
date=date,
Expand Down Expand Up @@ -561,26 +561,26 @@ def compute_initial_daily_ecdr_dataset(
)
logger.debug("Initialized spatial_interpolation_flag with TB fill locations")

platform = get_platform_by_date(date)
if platform == "am2":
platform = PLATFORM_CONFIG.get_platform_by_date(date)
if platform.id == "am2":
bt_coefs_init = pmi_bt_params_amsr2.get_ausi_amsr2_bootstrap_params(
date=date,
satellite="amsr2",
gridid=ecdr_ide_ds.grid_id,
)
elif platform == "ame":
elif platform.id == "ame":
bt_coefs_init = pmi_bt_params_amsre.get_ausi_amsre_bootstrap_params(
date=date,
satellite="amsre",
gridid=ecdr_ide_ds.grid_id,
)
elif platform in get_args(NSIDC_0001_SATS):
elif platform.id in get_args(NSIDC_0001_SATS):
bt_coefs_init = pmi_bt_params_0001.get_nsidc0001_bootstrap_params(
date=date,
satellite=platform,
satellite=platform.id,
gridid=ecdr_ide_ds.grid_id,
)
elif platform_is_smmr(platform):
elif platform_is_smmr(platform.id):
bt_coefs_init = get_smmr_params(hemisphere=hemisphere, date=date)
else:
raise RuntimeError(f"platform bootstrap params not implemented: {platform}")
Expand Down Expand Up @@ -618,14 +618,14 @@ def compute_initial_daily_ecdr_dataset(
pole_mask = nh_polehole_mask(
date=date,
resolution=tb_data.resolution,
sat=platform,
ancillary_source=ancillary_source,
platform=platform,
)
ecdr_ide_ds["pole_mask"] = pole_mask

nt_params = get_cdr_nt_params(
hemisphere=hemisphere,
platform=platform,
platform=platform.id,
)

nt_coefs = NtCoefs(
Expand Down Expand Up @@ -683,7 +683,7 @@ def compute_initial_daily_ecdr_dataset(
v19=bt_v19,
v22=bt_v22,
),
platform=platform.lower(),
platform=platform.id.lower(),
)
bt_v37 = transformed["v37"]
bt_h37 = transformed["h37"]
Expand Down Expand Up @@ -713,7 +713,7 @@ def compute_initial_daily_ecdr_dataset(
wintrc=bt_coefs_init["wintrc"],
wslope=bt_coefs_init["wslope"],
wxlimt=bt_coefs_init["wxlimt"],
is_smmr=platform_is_smmr(platform),
is_smmr=platform_is_smmr(platform.id),
)

# Note:
Expand Down Expand Up @@ -853,7 +853,7 @@ def compute_initial_daily_ecdr_dataset(
tb_h37=bt_h37,
tb_v19=bt_v19,
bt_coefs=bt_coefs,
platform=platform,
platform=platform.id,
)

# Set any bootstrap concentrations below 10% to 0.
Expand Down Expand Up @@ -950,7 +950,6 @@ def compute_initial_daily_ecdr_dataset(
tb_data=tb_data,
algorithm=land_spillover_alg,
land_mask=non_ocean_mask.data,
platform=platform,
ancillary_source=ancillary_source,
bt_conc=bt_asCDRv4_conc,
nt_conc=nt_asCDRv4_conc,
Expand Down Expand Up @@ -1252,7 +1251,7 @@ def get_idecdr_dir(*, intermediate_output_dir: Path) -> Path:
def get_idecdr_filepath(
*,
date: dt.date,
platform,
platform_id: SUPPORTED_PLATFORM_ID,
hemisphere: Hemisphere,
resolution: ECDR_SUPPORTED_RESOLUTIONS,
intermediate_output_dir: Path,
Expand All @@ -1262,7 +1261,7 @@ def get_idecdr_filepath(
standard_fn = standard_daily_filename(
hemisphere=hemisphere,
date=date,
sat=platform,
platform_id=platform_id,
resolution=resolution,
)
idecdr_fn = "idecdr_" + standard_fn
Expand All @@ -1285,10 +1284,10 @@ def make_idecdr_netcdf(
ancillary_source: ANCILLARY_SOURCES,
overwrite_ide: bool = False,
) -> None:
platform = get_platform_by_date(date)
platform = PLATFORM_CONFIG.get_platform_by_date(date)
output_path = get_idecdr_filepath(
date=date,
platform=platform,
platform_id=platform.id,
hemisphere=hemisphere,
intermediate_output_dir=intermediate_output_dir,
resolution=resolution,
Expand Down
Loading

0 comments on commit 94eba01

Please sign in to comment.