---
title: "Ingest Primary Productivity (VGPM, annual integral)"
subtitle: "OSU VGPM R2022 (VIIRS chl + sst) monthly HDFs 2014-2023 -> annual NPP that integrates the year (polar night / ice = 0), beside the legacy mean of observed months"
msens:
target_name: ingest_productivity
# `ingest` is accepted without a `dataset:` block (ingest_worms / ingest_taxon do the same): productivity
# is an environmental layer scored by score_cell_metrics.qmd, not a species dataset, so build_registry.qmd
# (reads `msens$dataset$ds_key`) and bootstrap_version.qmd (verifies only ingests that declare a ds_key)
# both skip it. `ingest` rather than `grid` keeps it in the `dependency: [auto]` set the release uses.
workflow_type: ingest
dependency: [build_cell_grid]
output: data/manifests/ingest_productivity.json
format: html
editor_options:
chunk_output_type: console
---
<!--
HOW score_cell_metrics.qmd CONSUMES THIS (the orchestrator wires it; nothing here couples through a file):
- VGPM_INTEGRATED=0 (default, the control; v10 and earlier) -> score_cell_metrics reads the OLD
`vgpm.r2022.v.chl.v.sst.2160x4320_2014-2023.avg.sd.tif` (layer `npp_avg`): mean of the OBSERVED months.
- VGPM_INTEGRATED=1 (v11, Q4) -> it reads the NEW
`vgpm.r2022.v.chl.v.sst.2160x4320_2014-2023.avg.sd_integrated.tif` (layer `npp_avg`): the year integrated
with polar-night / ice months = 0 (`msens::vgpm_annual_integrate()`), divided by days in the year.
The OLD file is never written or modified here (v10's checkpoint reads it). The manifest records which outputs
exist and their xxhash64 digests, so a downstream change is attributable.
-->
## Goal
Rasser's v1 review (comment C76) found the Arctic Planning Areas (Hope, Norton) implausibly productive. Cause:
the legacy annual value is `mean(na.rm = TRUE)` over the 12 monthly VGPM composites, but monthly VGPM has **no
retrieval** under polar night or ice, so north of ~65 N a "year" was the mean of the 4-6 observed bloom months
while a Gulf year was the mean of all twelve. Both were then treated as a full-year daily rate.
This notebook keeps the monthly inputs (the legacy notebook deleted the tarballs and HDFs after each annual mean),
and produces the **integrated** annual rate: per year `msens::vgpm_annual_integrate()` =
sum over months of `rate_m * days_in_month / days_in_year`, a month with no retrieval contributing 0 and a cell
with no retrieval in any month staying `NA` (land is not painted as zero). Units stay mg C / m^2^ / day (an
annual-mean daily rate), so nothing downstream changes except the file `score_cell_metrics.qmd` reads. The
legacy method is **reproduced from the same monthly inputs** (`_meanobs.tif`) so the two can be compared on
identical data, and the reproduction is asserted against the legacy per-year and ten-year files when those are
present (a check that fails if the HDF orientation or no-data handling is wrong).
Source:
- [Ocean Productivity from Oregon State](http://sites.science.oregonstate.edu/ocean.productivity)
- [VIIRS R2022 - 2160 by 4320 Monthly HDF files](https://orca.science.oregonstate.edu/2160.by.4320.monthly.hdf.vgpm.v.chl.v.sst.php)
- Behrenfeld and Falkowski (1997a) [VGPM](http://science.oregonstate.edu/ocean.productivity/references.php#Behrenfeld.1997a): a "chlorophyll-based" model that estimates net primary production from chlorophyll using a temperature-dependent description of chlorophyll-specific photosynthetic efficiency, with available light and a euphotic-zone depth from Morel and Berthon (1989).
- old (CAFE was tried and abandoned; do not switch products here): [CAFE model description](https://orca.science.oregonstate.edu/cafe.model.php); Silsbe et al (2016) [The CAFE model](https://agupubs.onlinelibrary.wiley.com/doi/10.1002/2016GB005521)
Units: mg C / m^2^ / day.
## Design
```{mermaid}
%%| label: fig-design
%%| fig-cap: "OSU monthly VGPM tarballs (kept) -> HDFs (kept) -> per-year integrated + mean-of-observed rasters -> ten-year avg/sd, beside the untouched legacy file"
flowchart LR
osu["OSU orca<br/>vgpm.v.{yyyy}.tar"] --> hdf["monthly/{yyyy}/unzipped<br/>12 HDFs per year (kept)"]
hdf --> int["vgpm_annual_integrate()<br/>vgpm_{yyyy}_integrated.tif"]
hdf --> obs["mean of observed months<br/>vgpm_{yyyy}_meanobs.tif"]
int --> avg["..._avg.sd_integrated.tif<br/>npp_avg, npp_sd, n_valid"]
obs --> avgo["..._avg.sd_meanobs.tif"]
old["legacy ..._avg.sd.tif<br/>(v10 control, untouched)"] -.-> chk{{"compare"}}
avgo --> chk
avg --> mf["manifest: hashes + latitude-band ratios"]
```
## Setup
Paths come from `libs/paths.R` (`dir_raw`); the legacy per-year means and ten-year file stay where
`score_cell_metrics.qmd` reads them, and the kept monthly inputs go in `monthly/` beside them. The flags are
read from `libs/vars.R`: `REDO_VGPM_DOWNLOAD=1` re-downloads and re-untars the tarballs, `REDO_INGEST=1` rebuilds
the derived rasters, and `VGPM_INTEGRATED=1` (or either redo flag) is what makes this target build the
integrated products at all, so the v10 control run never needs the network or the ~2 GB of HDFs.
```{r}
#| label: setup
librarian::shelf(
dplyr, digest, fs, glue, here, jsonlite, R.utils, readr, stringr, terra, tibble, tidyr,
MarineSensitivity/msens, quiet = T)
source(here("libs/paths.R")) # dir_raw, ver
source(here("libs/vars.R")) # vgpm_integrated, redo_vgpm_download, redo_ingest
# the integrator lands in msens 0.61.0 (R/productivity.R)
stopifnot(
"msens >= 0.61.0 (vgpm_annual_integrate) is required; reinstall ../msens" =
packageVersion("msens") >= "0.61.0",
"msens::vgpm_annual_integrate not exported" =
"vgpm_annual_integrate" %in% getNamespaceExports("msens"))
yrs <- 2014:2023 # VIIRS: the most recent complete 10 years
y0 <- min(yrs); y1 <- max(yrs)
dir_osu <- path_expand(glue("{dir_raw}/oregonstate.edu"))
dir_vgpm <- glue("{dir_osu}/vgpm.r2022.v.chl.v.sst.2160x4320") # legacy per-year means lived inside this dir
dir_monthly <- path(dir_osu, "monthly") # KEPT monthly inputs (v11 Q4)
dir_figs <- here("figs/ingest_productivity")
dir_create(c(dir_osu, dir_monthly, dir_figs, here("data/manifests")))
# legacy outputs (READ ONLY here: the v10 control and score_cell_metrics read them)
tif_old <- glue("{dir_vgpm}_{y0}-{y1}.avg.sd.tif") # npp_avg, npp_sd
# new outputs, beside the legacy file
tif_int <- glue("{dir_vgpm}_{y0}-{y1}.avg.sd_integrated.tif") # npp_avg, npp_sd, n_valid
tif_obs <- glue("{dir_vgpm}_{y0}-{y1}.avg.sd_meanobs.tif") # npp_avg, npp_sd (legacy method, same inputs)
manifest <- here("data/manifests/ingest_productivity.json")
# the integrated products are built when asked for; the control run (all flags off) skips straight
# to the manifest and stays offline
do_integrate <- vgpm_integrated || redo_vgpm_download || redo_ingest
url_tar <- \(yr) glue("https://orca.science.oregonstate.edu/data/2x4/monthly/vgpm.r2022.v.chl.v.sst/hdf/vgpm.v.{yr}.tar")
dir_yr <- \(yr) path(dir_monthly, yr) # untarred tarball contents + unzipped/
tar_yr <- \(yr) path(dir_monthly, glue("vgpm.v.{yr}.tar"))
tif_yr <- \(yr, kind) path(dir_monthly, glue("vgpm_{yr}_{kind}.tif")) # kind: integrated | meanobs
csv_yr <- \(yr) path(dir_monthly, glue("vgpm_{yr}_month_share_ge66N.csv"))
# the legacy per-year mean: either beside the legacy ten-year file's dir or inside it
tif_old_yr <- \(yr) {
cand <- c(path(dir_vgpm, glue("vgpm.{yr}.tif")), path(dir_osu, glue("vgpm.{yr}.tif")))
cand[file_exists(cand)][1] # NA when neither exists
}
message(glue("ingest_productivity: do_integrate={do_integrate} (VGPM_INTEGRATED={vgpm_integrated}, ",
"REDO_VGPM_DOWNLOAD={redo_vgpm_download}, REDO_INGEST={redo_ingest}); msens {packageVersion('msens')}"))
```
## Functions
`read_vgpm_hdf()` keeps the legacy notebook's reading rules: the HDF carries no usable georeferencing, so the extent
is set to the globe, and `-9999` is the no-retrieval value. It goes through terra (R's GDAL carries the HDF4 driver;
Homebrew's `gdalinfo` does not). `hdf_files()` lists a year's 12 HDFs in month order from the day-of-year in the
file name (`vgpm.{yyyyDDD}.hdf`, the DOY is the first day of the month).
```{r}
#| label: functions
# 12 HDFs of a year, in month order (asserts exactly one per month)
hdf_files <- function(yr) {
if (!dir_exists(dir_yr(yr))) return(character())
f <- dir_ls(dir_yr(yr), recurse = T, type = "file", regexp = "vgpm\\.[0-9]{7}\\.hdf$")
if (length(f) == 0) return(character())
d <- as.Date(str_match(path_file(f), "vgpm\\.([0-9]{7})\\.hdf$")[, 2], format = "%Y%j")
f <- f[order(d)]; d <- sort(d)
stopifnot(
"an HDF is not in the requested year" = all(as.integer(format(d, "%Y")) == yr),
"HDF months are not unique" = !anyDuplicated(format(d, "%m")))
setNames(as.character(f), format(d, "%m"))
}
read_vgpm_hdf <- function(f) {
r <- terra::rast(f) # HDF4 via GDAL
if (nlyr(r) < 1) r <- terra::sds(f)[[1]] # multi-subdataset file: first dataset
stopifnot("HDF is not 2160 x 4320" = nrow(r) == 2160 && ncol(r) == 4320)
r <- r[[1]]
ext(r) <- c(-180, 180, -90, 90) # no georeferencing in the HDF: global
crs(r) <- "epsg:4326"
NAflag(r) <- -9999
r <- terra::subst(r, -9999, NA) # belt and braces: -9999 = no retrieval = NA
names(r) <- "npp"
r
}
# write atomically (a killed render never leaves a half-written TIF that a resume would trust)
write_tif <- function(r, f) {
tmp <- paste0(f, ".part.tif")
writeRaster(r, tmp, overwrite = T, datatype = "FLT4S", gdal = c("COMPRESS=DEFLATE"))
if (file_exists(f)) file_delete(f)
file_move(tmp, f)
invisible(f)
}
xxh <- \(f) digest::digest(file = as.character(f), algo = "xxhash64")
```
## Download (resumable, gated)
For each year the OSU tarball `vgpm.v.{yyyy}.tar` (~200 MB) is fetched, untarred into `monthly/{yyyy}/` and each
gzipped HDF is expanded into `monthly/{yyyy}/unzipped/`. **Both the tarball and the HDFs are kept** (the legacy
notebook deleted them, which is why a ~2 GB re-download is needed once). A year whose 12 HDFs are already present is
skipped unless `REDO_VGPM_DOWNLOAD=1`; a present tarball is not fetched again, only re-untarred.
```{r}
#| label: download
if (do_integrate) {
options(timeout = max(3600, getOption("timeout")))
for (yr in yrs) { # yr = 2019
have <- hdf_files(yr)
if (length(have) == 12 && !redo_vgpm_download) {
message(glue("{yr}: 12 HDFs present, skipping download"))
next
}
dir_create(dir_yr(yr))
if (!file_exists(tar_yr(yr)) || redo_vgpm_download) {
part <- paste0(tar_yr(yr), ".part")
message(glue("{yr}: downloading {url_tar(yr)}"))
download.file(url_tar(yr), part, method = "curl", extra = "-k -L -f")
file_move(part, tar_yr(yr))
}
stopifnot("tarball has no members" = length(untar(tar_yr(yr), list = T)) >= 12)
untar(tar_yr(yr), exdir = dir_yr(yr))
# expand the gzipped HDFs, keeping the .gz
dir_unz <- path(dir_yr(yr), "unzipped"); dir_create(dir_unz)
for (f_gz in dir_ls(dir_yr(yr), recurse = T, glob = "*.hdf.gz")) {
f_hdf <- path(dir_unz, sub("\\.gz$", "", path_file(f_gz)))
if (!file_exists(f_hdf) || redo_vgpm_download)
gunzip(f_gz, destname = f_hdf, remove = F, overwrite = T)
}
have <- hdf_files(yr)
if (length(have) != 12)
stop(glue("{yr}: expected 12 monthly HDFs, found {length(have)}"))
}
}
# per-year file counts, from what is on disk (also in the control run, where nothing is downloaded)
n_hdf <- setNames(vapply(yrs, \(yr) length(hdf_files(yr)), integer(1)), yrs)
tibble(year = yrs, n_hdf = unname(n_hdf)) |> print()
```
## Integrate each year
Per year the 12 monthly rasters are stacked in month order and passed to `msens::vgpm_annual_integrate()`; the
result (`npp`, `n_valid`) is written as `monthly/vgpm_{yyyy}_integrated.tif`. The legacy method (mean of the
observed months) is computed from the same stack as `monthly/vgpm_{yyyy}_meanobs.tif`. Each reproduction is
asserted against the legacy `vgpm.{yyyy}.tif` when it exists (same no-data pattern, values equal to float
precision), which is the check that the HDFs were read the way the legacy notebook read them. Per month, the
share of cells north of 66 N with a retrieval is kept (denominator: cells with a retrieval in at least one month
of that year) for the table further down. A year whose three outputs exist is skipped unless `REDO_INGEST=1`.
```{r}
#| label: integrate_years
legacy_chk <- list()
if (do_integrate) {
for (yr in yrs) { # yr = 2019
outs <- c(tif_yr(yr, "integrated"), tif_yr(yr, "meanobs"), csv_yr(yr))
if (all(file_exists(outs)) && !redo_ingest) {
message(glue("{yr}: derived rasters present, skipping"))
} else {
f_hdf <- hdf_files(yr)
if (length(f_hdf) != 12)
stop(glue("{yr}: need 12 HDFs, found {length(f_hdf)} (run with REDO_VGPM_DOWNLOAD=1)"))
monthly <- rast(lapply(f_hdf, read_vgpm_hdf)) # 12 layers, month order
names(monthly) <- glue("npp_{names(f_hdf)}")
r_int <- msens::vgpm_annual_integrate(monthly, year = yr) # npp, n_valid
stopifnot(
"integrator must return layers npp + n_valid" = identical(names(r_int), c("npp", "n_valid")),
"integrator must keep the 2160 x 4320 grid" = nrow(r_int) == 2160 && ncol(r_int) == 4320)
r_obs <- mean(monthly, na.rm = T) # the legacy method, verbatim
names(r_obs) <- "npp"
# per-month share of >= 66 N cells with a retrieval
m66 <- crop(monthly, ext(-180, 180, 66, 90))
den <- global(sum(!is.na(m66)) > 0, "sum")[[1]]
tibble(
year = yr,
month = as.integer(names(f_hdf)),
n_ret = global(!is.na(m66), "sum")[[1]],
n_den = den) |>
write_csv(csv_yr(yr))
write_tif(r_int, tif_yr(yr, "integrated"))
write_tif(r_obs, tif_yr(yr, "meanobs"))
}
# reproduction of the legacy per-year mean from the same monthly inputs
f_old <- tif_old_yr(yr)
if (!is.na(f_old)) {
a <- rast(tif_yr(yr, "meanobs"), lyrs = "npp"); b <- rast(f_old)[[1]]
d <- global(abs(a - b), "max", na.rm = T)[[1]]
n_na_mismatch <- global(is.na(a) != is.na(b), "sum", na.rm = T)[[1]]
legacy_chk[[as.character(yr)]] <- tibble(year = yr, max_abs_diff = d, n_na_mismatch = n_na_mismatch)
if (!is.finite(d) || d >= 0.01)
stop(glue("{yr}: meanobs differs from the legacy vgpm.{yr}.tif by {d} (HDF read orientation / no-data?)"))
if (n_na_mismatch != 0)
stop(glue("{yr}: {n_na_mismatch} cells differ in NA pattern from the legacy vgpm.{yr}.tif"))
}
}
legacy_chk <- bind_rows(legacy_chk)
if (nrow(legacy_chk)) print(legacy_chk) else message("no legacy per-year means found to compare against")
}
```
## Ten-year average and standard deviation
Across the ten years: the mean of the integrated annual `npp` layers (`NA` only where all ten are `NA`), their
standard deviation, and the mean `n_valid` (months with a retrieval) as a climatology. The legacy method is
aggregated identically from the per-year mean-of-observed layers. Both are written beside the legacy file, which
is not touched; the reproduction is asserted against it when present.
```{r}
#| label: aggregate_years
if (do_integrate) {
if (all(file_exists(c(tif_int, tif_obs))) && !redo_ingest) {
message("ten-year integrated + meanobs files present, skipping")
} else {
r_i <- rast(lapply(yrs, \(yr) rast(tif_yr(yr, "integrated")))) # 20 layers: npp, n_valid per year
stopifnot(nlyr(r_i) == 2 * length(yrs))
i_npp <- r_i[[seq(1, nlyr(r_i), by = 2)]]
i_nv <- r_i[[seq(2, nlyr(r_i), by = 2)]]
stopifnot(all(names(r_i)[seq(1, nlyr(r_i), by = 2)] == "npp"),
all(names(r_i)[seq(2, nlyr(r_i), by = 2)] == "n_valid"))
r_int <- c(mean(i_npp, na.rm = T), stdev(i_npp, na.rm = T), mean(i_nv, na.rm = T))
names(r_int) <- c("npp_avg", "npp_sd", "n_valid")
write_tif(r_int, tif_int)
o_npp <- rast(lapply(yrs, \(yr) rast(tif_yr(yr, "meanobs"))))
r_obs <- c(mean(o_npp, na.rm = T), stdev(o_npp, na.rm = T))
names(r_obs) <- c("npp_avg", "npp_sd")
write_tif(r_obs, tif_obs)
}
# the legacy ten-year file, reproduced from the monthly inputs
if (file_exists(tif_old)) {
a <- rast(tif_obs); b <- rast(tif_old)
stopifnot("legacy file must have npp_avg, npp_sd" = all(c("npp_avg", "npp_sd") %in% names(b)))
d_avg <- global(abs(a[["npp_avg"]] - b[["npp_avg"]]), "max", na.rm = T)[[1]]
d_sd <- global(abs(a[["npp_sd"]] - b[["npp_sd"]]), "max", na.rm = T)[[1]]
message(glue("meanobs vs legacy ten-year file: max |d npp_avg| = {signif(d_avg, 3)}, max |d npp_sd| = {signif(d_sd, 3)}"))
stopifnot(
"meanobs npp_avg differs from the legacy ten-year file" = is.finite(d_avg) && d_avg < 0.01,
"meanobs npp_sd differs from the legacy ten-year file" = is.finite(d_sd) && d_sd < 0.05)
} else {
message("legacy ten-year file not found: skipping the reproduction check")
}
}
```
## Integrated vs mean of observed, by latitude
The ratio of the integrated climatology to the mean of the observed months, area-weighted (cosine of latitude) over
cells that have a retrieval, in 5 degree bands north of 50 N. The spec's measured expectation (one-year 2019
prototype) is 1.00 below 50 N, then 0.88, 0.74, 0.61, 0.43, 0.32, 0.21; a deviation is reported, and only the
shape is asserted (about 1 at low latitude, never rising with latitude), because the ten-year climatology is not
the 2019 prototype. The band containing the equator (50 S to 50 N) is the "< 50" band; the southern high-latitude
band is listed separately because the Southern Ocean has the same polar-night gap.
```{r}
#| label: lat_bands
lat_expect <- c("50S-50N" = 1.00, "50-55N" = 0.88, "55-60N" = 0.74, "60-65N" = 0.61,
"65-70N" = 0.43, "70-75N" = 0.32, "75-85N" = 0.21)
lat_ratio <- NULL
if (file_exists(tif_int) && file_exists(tif_obs)) {
ia <- as.matrix(rast(tif_int, lyrs = "npp_avg"), wide = T)
mo <- as.matrix(rast(tif_obs, lyrs = "npp_avg"), wide = T)
ok <- !is.na(ia) & !is.na(mo)
stopifnot("integrated has a retrieval where mean-of-observed has none (or the reverse)" =
identical(is.na(ia), is.na(mo)))
ia[!ok] <- 0; mo[!ok] <- 0
y <- yFromRow(rast(tif_int), seq_len(nrow(ia)))
brk <- c(-50, 50, 55, 60, 65, 70, 75, 85)
lab <- c("S of 50S", "50S-50N", "50-55N", "55-60N", "60-65N", "65-70N", "70-75N", "75-85N", "N of 85N")
lat_ratio <- tibble(
band = factor(lab[findInterval(y, brk) + 1], levels = lab),
w = cos(y * pi / 180),
s_i = rowSums(ia), s_o = rowSums(mo), n = rowSums(ok)) |>
group_by(band) |>
summarize(
n_cell = sum(n),
mean_observed = sum(w * s_o) / sum(w * n),
mean_integrated= sum(w * s_i) / sum(w * n),
ratio = sum(w * s_i) / sum(w * s_o),
.groups = "drop") |>
mutate(expected = unname(lat_expect[as.character(band)]), delta = ratio - expected)
print(lat_ratio, n = Inf)
r_main <- lat_ratio |> filter(band %in% names(lat_expect))
stopifnot(
"low-latitude (50S-50N) integrated/observed ratio should be ~1" =
abs(r_main$ratio[r_main$band == "50S-50N"] - 1) < 0.02,
"integrated/observed ratio must not rise with latitude (50N and north)" =
all(diff(r_main$ratio[r_main$band != "50S-50N"]) <= 1e-6))
if (any(abs(r_main$delta) > 0.10, na.rm = T))
warning("a latitude-band ratio is more than 0.10 from the spec's measured value: ",
paste(r_main$band[abs(r_main$delta) > 0.10], collapse = ", "))
}
```
## Cells with a retrieval, by month (>= 66 N)
Share of cells north of 66 N with a retrieval in each month, pooled over the years (the denominator is the cells
with a retrieval in at least one month of the same year). Expectation from the spec: Jan-Apr and Nov-Dec 0 %,
May 14 %, Jun 30 %, Jul 77 %, Aug-Sep about 100 %, Oct 3 %.
```{r}
#| label: month_share
month_share <- NULL
if (all(file_exists(csv_yr(yrs)))) {
month_share <- bind_rows(lapply(yrs, \(yr) read_csv(csv_yr(yr), show_col_types = F))) |>
group_by(month) |>
summarize(pct_with_retrieval = round(100 * sum(n_ret) / sum(n_den), 1), .groups = "drop")
print(month_share, n = 12)
}
```
## Map: mean of observed months vs integrated
```{r}
#| label: fig_compare
png_cmp <- path(dir_figs, "map_vgpm_meanobs_vs_integrated.png")
if (file_exists(tif_int) && file_exists(tif_obs) && (!file_exists(png_cmp) || redo_ingest)) {
e <- ext(-180, 180, 45, 90)
a <- aggregate(crop(rast(tif_obs, lyrs = "npp_avg"), e), fact = 2, na.rm = T)
b <- aggregate(crop(rast(tif_int, lyrs = "npp_avg"), e), fact = 2, na.rm = T)
rng <- c(0, as.numeric(global(a, quantile, probs = 0.99, na.rm = T)[[1]]))
png(png_cmp, width = 1800, height = 560, res = 150)
par(mfrow = c(1, 2), mar = c(2, 2, 3, 3))
plot(a, range = rng, main = "mean of observed months (legacy)")
plot(b, range = rng, main = "integrated year, polar night / ice = 0")
dev.off()
}
if (file_exists(png_cmp)) knitr::include_graphics(as.character(png_cmp))
```
## Manifest
The content hash is the xxhash64 of the integrated ten-year TIF when it exists (otherwise of the legacy file, the
control run). `stats` records which outputs exist and their digests, the per-year HDF counts, and the
latitude-band ratios. Paths are relative to `dir_raw`, so the manifest is host-independent. The legacy file's own
digest is recorded as the proof it is untouched.
```{r}
#| label: manifest
rel <- \(f) as.character(path_rel(f, dir_raw |> path_expand()))
outs <- c(integrated = tif_int, meanobs = tif_obs, legacy = tif_old)
outs <- outs[file_exists(outs)]
stopifnot("neither the integrated nor the legacy VGPM file exists" = length(outs) > 0)
h_out <- vapply(outs, xxh, character(1))
h <- unname(if ("integrated" %in% names(outs)) h_out["integrated"] else h_out["legacy"])
msens::write_manifest(
manifest, target = "ingest_productivity", content_hash = h,
stats = list(
version = ver,
years = as.list(yrs),
n_hdf = as.list(n_hdf),
integrated_built= "integrated" %in% names(outs),
outputs = as.list(setNames(vapply(outs, rel, character(1)), names(outs))),
xxhash64 = as.list(h_out),
lat_band_ratio = if (is.null(lat_ratio)) NULL else
as.list(setNames(round(lat_ratio$ratio, 3), as.character(lat_ratio$band))),
pct_ge66N_with_retrieval = if (is.null(month_share)) NULL else
as.list(setNames(month_share$pct_with_retrieval, sprintf("m%02d", month_share$month)))),
force = msens::force_target("ingest_productivity"))
message(glue("ingest_productivity: {paste(names(outs), collapse = ', ')} -> {basename(manifest)} ({h})"))
```