Skip to content

Commit

Permalink
Add multiplier setting for vdatum to control transformation direction (
Browse files Browse the repository at this point in the history
  • Loading branch information
mpiannucci authored Jul 9, 2024
1 parent a752e1f commit f6c22cd
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 18 deletions.
48 changes: 32 additions & 16 deletions datasets/datasets.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
"path": "s3://nextgen-dmac-cloud-ingest/nos_vdatums/cbofs_vdatums.nc.zarr",
"water_level_var": "zeta",
"vdatum_var": "mllwtomsl",
"vdatum_name": "mllw"
"vdatum_name": "mllw",
"multiplier": -1.0
},
"roms": {}
}
Expand All @@ -24,7 +25,8 @@
"path": "s3://nextgen-dmac-cloud-ingest/nos_vdatums/ciofs_vdatums.nc.zarr",
"water_level_var": "zeta",
"vdatum_var": "mllwtomsl",
"vdatum_name": "mllw"
"vdatum_name": "mllw",
"multiplier": -1.0
},
"roms": {}
}
Expand All @@ -39,7 +41,8 @@
"path": "s3://nextgen-dmac-cloud-ingest/nos_vdatums/creofs_vdatums.nc.zarr",
"water_level_var": "zeta",
"vdatum_var": "mllwtomsl",
"vdatum_name": "mllw"
"vdatum_name": "mllw",
"multiplier": -1.0
}
}
},
Expand All @@ -53,7 +56,8 @@
"path": "s3://nextgen-dmac-cloud-ingest/nos_vdatums/dbofs_vdatums.nc.zarr",
"water_level_var": "zeta",
"vdatum_var": "mllwtomsl",
"vdatum_name": "mllw"
"vdatum_name": "mllw",
"multiplier": -1.0
},
"roms": {}
}
Expand All @@ -68,7 +72,8 @@
"path": "s3://nextgen-dmac-cloud-ingest/nos_vdatums/gomofs_vdatums.nc.zarr",
"water_level_var": "zeta",
"vdatum_var": "mllwtomsl",
"vdatum_name": "mllw"
"vdatum_name": "mllw",
"multiplier": -1.0
},
"roms": {}
}
Expand All @@ -83,7 +88,8 @@
"path": "s3://nextgen-dmac-cloud-ingest/nos_vdatums/gomofs_vdatums.nc.zarr",
"water_level_var": "zeta",
"vdatum_var": "mllwtomsl",
"vdatum_name": "mllw"
"vdatum_name": "mllw",
"multiplier": -1.0
},
"roms": {}
}
Expand All @@ -98,7 +104,8 @@
"path": "s3://nextgen-dmac-cloud-ingest/nos_vdatums/leofs_vdatums.nc.zarr",
"water_level_var": "zeta",
"vdatum_var": "igld85tolwd",
"vdatum_name": "igld85"
"vdatum_name": "igld85",
"multiplier": -1.0
}
}
},
Expand All @@ -112,7 +119,8 @@
"path": "s3://nextgen-dmac-cloud-ingest/nos_vdatums/lmhofs_vdatums.nc.zarr",
"water_level_var": "zeta",
"vdatum_var": "igld85tolwd",
"vdatum_name": "igld85"
"vdatum_name": "igld85",
"multiplier": 1.0
}
}
},
Expand All @@ -126,7 +134,8 @@
"path": "s3://nextgen-dmac-cloud-ingest/nos_vdatums/loofs_vdatums.nc.zarr",
"water_level_var": "zeta",
"vdatum_var": "igld85tolwd",
"vdatum_name": "igld85"
"vdatum_name": "igld85",
"multiplier": 1.0
}
}
},
Expand All @@ -140,7 +149,8 @@
"path": "s3://nextgen-dmac-cloud-ingest/nos_vdatums/lsofs_vdatums.nc.zarr",
"water_level_var": "zeta",
"vdatum_var": "igld85tolwd",
"vdatum_name": "igld85"
"vdatum_name": "igld85",
"multiplier": 1.0
}
}
},
Expand All @@ -161,7 +171,8 @@
"path": "s3://nextgen-dmac-cloud-ingest/nos_vdatums/ngofs2_vdatums.nc.zarr",
"water_level_var": "zeta",
"vdatum_var": "mllwtomsl",
"vdatum_name": "mllw"
"vdatum_name": "mllw",
"multiplier": -1.0
}
}
},
Expand All @@ -175,7 +186,8 @@
"path": "s3://nextgen-dmac-cloud-ingest/nos_vdatums/ngofs2_vdatums.nc.zarr",
"water_level_var": "zeta",
"vdatum_var": "mllwtomsl",
"vdatum_name": "mllw"
"vdatum_name": "mllw",
"multiplier": -1.0
}
}
},
Expand All @@ -189,7 +201,8 @@
"path": "s3://nextgen-dmac-cloud-ingest/nos_vdatums/sfbofs_vdatums.nc.zarr",
"water_level_var": "zeta",
"vdatum_var": "mllwtomsl",
"vdatum_name": "mllw"
"vdatum_name": "mllw",
"multiplier": -1.0
}
}
},
Expand All @@ -209,7 +222,8 @@
"path": "s3://nextgen-dmac-cloud-ingest/nos_vdatums/tbofs_vdatums.nc.zarr",
"water_level_var": "zeta",
"vdatum_var": "mllwtomsl",
"vdatum_name": "mllw"
"vdatum_name": "mllw",
"multiplier": -1.0
},
"roms": {}
}
Expand All @@ -224,7 +238,8 @@
"path": "s3://nextgen-dmac-cloud-ingest/nos_vdatums/wcofs_vdatums.nc.zarr",
"water_level_var": "zeta",
"vdatum_var": "mllwtomsl",
"vdatum_name": "mllw"
"vdatum_name": "mllw",
"multiplier": -1.0
},
"roms": {}
}
Expand All @@ -239,7 +254,8 @@
"path": "s3://nextgen-dmac-cloud-ingest/nos_vdatums/wcofs_vdatums.nc.zarr",
"water_level_var": "zeta",
"vdatum_var": "mllwtomsl",
"vdatum_name": "mllw"
"vdatum_name": "mllw",
"multiplier": -1.0
},
"roms": {}
}
Expand Down
20 changes: 18 additions & 2 deletions xreds/extensions/vdatum.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from typing import Literal
import xarray as xr

from xreds.config import settings
Expand All @@ -13,13 +14,19 @@ def transform_datum(
target_zeta_var: str,
target_datum_var: str,
target_datum_name: str,
multiplier: float,
out_datum_var: str,
) -> xr.Dataset:
"""Transform the dataset to target datum
Args:
ds (xr.Dataset): The dataset to transform
ds_vdatum (xr.Dataset): The vdatum dataset
target_zeta_var (str): The variable name of the water level
target_datum_var (str): The variable name of the target datum
target_datum_name (str): The name of the target datum
multiplier (float): The multiplier to apply to the target datum. Usually 1.0 or -1.0
out_datum_var (str): The name of the output variable
Returns:
xr.Dataset: The transformed dataset
Expand All @@ -36,7 +43,7 @@ def transform_datum(
for o, k in new_dims.items():
assert ds_vdatum[target_datum_var][o].shape == ds.zeta[k].shape

zeta_to_datum = zeta - datum.rename(new_dims)
zeta_to_datum = zeta + (multiplier * datum.rename(new_dims))
zeta_to_datum = zeta_to_datum.assign_attrs({"datum": target_datum_name})

ds_transformed = ds.assign({out_datum_var: zeta_to_datum})
Expand Down Expand Up @@ -75,6 +82,7 @@ def transform_dataset(self, ds: xr.Dataset, config: dict) -> xr.Dataset:
target_zeta_var = config.get("water_level_var", "zeta")
target_datum_var = config.get("vdatum_var", None)
target_datum_name = config.get("vdatum_name", None)
multiplier = config.get("multiplier", 1.0)

if target_datum_var is None or target_datum_name is None:
logger.warning(
Expand All @@ -84,5 +92,13 @@ def transform_dataset(self, ds: xr.Dataset, config: dict) -> xr.Dataset:

out_datum_var = f"{target_zeta_var}_{target_datum_name}"

ds_transformed = transform_datum(ds, ds_vdatum, target_zeta_var, target_datum_var, target_datum_name, out_datum_var)
ds_transformed = transform_datum(
ds,
ds_vdatum,
target_zeta_var,
target_datum_var,
target_datum_name,
multiplier,
out_datum_var
)
return ds_transformed

0 comments on commit f6c22cd

Please sign in to comment.