This example showcases how xnemogcm can compute certain missing metrics (metrics are called scale factors in the NEMO community, and called metrics in the xgcm community).
It is thus possible to e.g. compute e3u from e3t. We let the reader refer to the NEMO documentation for the explanation of the scale factors.
In [1]:
Copied!
from pathlib import Path
from xnemogcm import open_nemo_and_domain_cfg
from xnemogcm.metrics import compute_missing_metrics
from xnemogcm import __version__ as xnemogcm_version
from pathlib import Path
from xnemogcm import open_nemo_and_domain_cfg
from xnemogcm.metrics import compute_missing_metrics
from xnemogcm import __version__ as xnemogcm_version
In [2]:
Copied!
print(xnemogcm_version)
print(xnemogcm_version)
0.5.0.post2
In [3]:
Copied!
datadir = Path('../../xnemogcm/test/data/4.2.0/open_and_merge/')
datadir = Path('../../xnemogcm/test/data/4.2.0/open_and_merge/')
In [4]:
Copied!
ds = open_nemo_and_domain_cfg(nemo_files=datadir, domcfg_files=datadir)
print(ds)
ds = open_nemo_and_domain_cfg(nemo_files=datadir, domcfg_files=datadir)
print(ds)
<xarray.Dataset> Size: 426kB
Dimensions: (z_c: 4, axis_nbounds: 2, t: 1, y_c: 22, x_c: 32,
x_f: 32, y_f: 22, z_f: 4)
Coordinates: (12/20)
time_centered (t) object 8B dask.array<chunksize=(1,), meta=np.ndarray>
* t (t) object 8B 0001-07-01 00:00:00
* x_c (x_c) int64 256B 0 1 2 3 4 5 6 ... 26 27 28 29 30 31
* y_c (y_c) int64 176B 0 1 2 3 4 5 6 ... 16 17 18 19 20 21
gdept_1d (z_c) float64 32B dask.array<chunksize=(4,), meta=np.ndarray>
* z_c (z_c) int64 32B 0 1 2 3
... ...
gphiv (y_f, x_c) float64 6kB dask.array<chunksize=(22, 32), meta=np.ndarray>
gdepw_1d (z_f) float64 32B dask.array<chunksize=(4,), meta=np.ndarray>
* z_f (z_f) float64 32B -0.5 0.5 1.5 2.5
gdepw_0 (z_f, y_c, x_c) float64 23kB dask.array<chunksize=(4, 22, 32), meta=np.ndarray>
glamf (y_f, x_f) float64 6kB dask.array<chunksize=(22, 32), meta=np.ndarray>
gphif (y_f, x_f) float64 6kB dask.array<chunksize=(22, 32), meta=np.ndarray>
Dimensions without coordinates: axis_nbounds
Data variables: (12/43)
deptht_bounds (z_c, axis_nbounds) float32 32B dask.array<chunksize=(4, 2), meta=np.ndarray>
time_centered_bounds (t, axis_nbounds) object 16B dask.array<chunksize=(1, 2), meta=np.ndarray>
t_bounds (t, axis_nbounds) object 16B dask.array<chunksize=(1, 2), meta=np.ndarray>
toce (t, z_c, y_c, x_c) float32 11kB dask.array<chunksize=(1, 4, 22, 32), meta=np.ndarray>
soce (t, z_c, y_c, x_c) float32 11kB dask.array<chunksize=(1, 4, 22, 32), meta=np.ndarray>
e3t (t, z_c, y_c, x_c) float32 11kB dask.array<chunksize=(1, 4, 22, 32), meta=np.ndarray>
... ...
e3u_0 (z_c, y_c, x_f) float64 23kB dask.array<chunksize=(4, 22, 32), meta=np.ndarray>
e3v_0 (z_c, y_f, x_c) float64 23kB dask.array<chunksize=(4, 22, 32), meta=np.ndarray>
e3f_0 (z_c, y_f, x_f) float64 23kB dask.array<chunksize=(4, 22, 32), meta=np.ndarray>
e3w_0 (z_f, y_c, x_c) float64 23kB dask.array<chunksize=(4, 22, 32), meta=np.ndarray>
e3uw_0 (z_f, y_c, x_f) float64 23kB dask.array<chunksize=(4, 22, 32), meta=np.ndarray>
e3vw_0 (z_f, y_f, x_c) float64 23kB dask.array<chunksize=(4, 22, 32), meta=np.ndarray>
Attributes: (12/18)
Conventions: CF-1.6
timeStamp: 2023-Mar-28 10:42:16 GMT
name: NEMO dataset
description: Ocean grid variables, set on the proper positions
title: Ocean grid variables
DOMAIN_dimensions_ids: [1 2]
... ...
Iperio: 0
Jperio: 0
NFold: 0
NFtype: -
VertCoord: zco
IsfCav: 0
In [5]:
Copied!
help(compute_missing_metrics)
help(compute_missing_metrics)
Help on function compute_missing_metrics in module xnemogcm.metrics:
compute_missing_metrics(ds, all_scale_factors=['e3t', 'e3u', 'e3v', 'e3f', 'e3w', 'e3uw', 'e3vw', 'e3fw'], time_varying=True)
Add all possible scale factors to the dataset.
For the moment, e3t (or e3t_0) at least needs to be present in the dataset
for the time_varying=True (time_varying=False) case.
If e3t_0 is not found (e.g. for nemo 3.6), it will raise a warning and use e3t_1d
(this will lead to wrong results if terrain-following coordinates are used).
May have some boundary issues, and only non-periodic boundaries are implemented.
Will add the metrics to the given dataset. To avoid this, use a ds.copy()
Parameters
----------
ds : xarray.Dataset
dataset containing the scale factors. Must be xgcm compatible (e.g. opened with xnemogcm)
all_scale_factors : list
list of the scale factors to compute (nothing is done for the scale factors
already present in *ds*)
Must be a sublist of: ['e3t', 'e3u', 'e3v', 'e3f', 'e3w', 'e3uw', 'e3vw', 'e3fw']
time_varying : bool
Whether to use the time varying scale factors (True) of the constant ones (False, 'e3x_0')
Returns
-------
the new dataset with the scale factors added
In [6]:
Copied!
# If you just want to get a copy
print(
compute_missing_metrics(ds.copy())
)
# If you just want to get a copy
print(
compute_missing_metrics(ds.copy())
)
--------------------------------------------------------------------------- ModuleNotFoundError Traceback (most recent call last) File ~/checkouts/readthedocs.org/user_builds/xnemogcm/envs/latest/lib/python3.10/site-packages/xnemogcm/metrics.py:54, in compute_missing_metrics(ds, all_scale_factors, time_varying) 53 try: ---> 54 import xgcm 55 except ModuleNotFoundError: ModuleNotFoundError: No module named 'xgcm' During handling of the above exception, another exception occurred: ModuleNotFoundError Traceback (most recent call last) Cell In[6], line 3 1 # If you just want to get a copy 2 print( ----> 3 compute_missing_metrics(ds.copy()) 4 ) File ~/checkouts/readthedocs.org/user_builds/xnemogcm/envs/latest/lib/python3.10/site-packages/xnemogcm/metrics.py:56, in compute_missing_metrics(ds, all_scale_factors, time_varying) 54 import xgcm 55 except ModuleNotFoundError: ---> 56 raise ModuleNotFoundError( 57 "xgcm is not installed, you need xgcm for this function" 58 ) 59 from warnings import warn 61 warn( 62 "This function is in pre-phase. Do not expect a high precision, but a good estimate. Some boundary issues may arise." 63 ) ModuleNotFoundError: xgcm is not installed, you need xgcm for this function
In [7]:
Copied!
# If you just want to add the scale factor inplace
compute_missing_metrics(ds)
print(ds)
# If you just want to add the scale factor inplace
compute_missing_metrics(ds)
print(ds)
--------------------------------------------------------------------------- ModuleNotFoundError Traceback (most recent call last) File ~/checkouts/readthedocs.org/user_builds/xnemogcm/envs/latest/lib/python3.10/site-packages/xnemogcm/metrics.py:54, in compute_missing_metrics(ds, all_scale_factors, time_varying) 53 try: ---> 54 import xgcm 55 except ModuleNotFoundError: ModuleNotFoundError: No module named 'xgcm' During handling of the above exception, another exception occurred: ModuleNotFoundError Traceback (most recent call last) Cell In[7], line 2 1 # If you just want to add the scale factor inplace ----> 2 compute_missing_metrics(ds) 3 print(ds) File ~/checkouts/readthedocs.org/user_builds/xnemogcm/envs/latest/lib/python3.10/site-packages/xnemogcm/metrics.py:56, in compute_missing_metrics(ds, all_scale_factors, time_varying) 54 import xgcm 55 except ModuleNotFoundError: ---> 56 raise ModuleNotFoundError( 57 "xgcm is not installed, you need xgcm for this function" 58 ) 59 from warnings import warn 61 warn( 62 "This function is in pre-phase. Do not expect a high precision, but a good estimate. Some boundary issues may arise." 63 ) ModuleNotFoundError: xgcm is not installed, you need xgcm for this function
It is also possible to compute the time constant metrics, i.e. the e3x_0
In [8]:
Copied!
print(
compute_missing_metrics(ds.copy(), time_varying=False)
)
print(
compute_missing_metrics(ds.copy(), time_varying=False)
)
--------------------------------------------------------------------------- ModuleNotFoundError Traceback (most recent call last) File ~/checkouts/readthedocs.org/user_builds/xnemogcm/envs/latest/lib/python3.10/site-packages/xnemogcm/metrics.py:54, in compute_missing_metrics(ds, all_scale_factors, time_varying) 53 try: ---> 54 import xgcm 55 except ModuleNotFoundError: ModuleNotFoundError: No module named 'xgcm' During handling of the above exception, another exception occurred: ModuleNotFoundError Traceback (most recent call last) Cell In[8], line 2 1 print( ----> 2 compute_missing_metrics(ds.copy(), time_varying=False) 3 ) File ~/checkouts/readthedocs.org/user_builds/xnemogcm/envs/latest/lib/python3.10/site-packages/xnemogcm/metrics.py:56, in compute_missing_metrics(ds, all_scale_factors, time_varying) 54 import xgcm 55 except ModuleNotFoundError: ---> 56 raise ModuleNotFoundError( 57 "xgcm is not installed, you need xgcm for this function" 58 ) 59 from warnings import warn 61 warn( 62 "This function is in pre-phase. Do not expect a high precision, but a good estimate. Some boundary issues may arise." 63 ) ModuleNotFoundError: xgcm is not installed, you need xgcm for this function
And it is possible to only compute a subset of the possible metrics
In [9]:
Copied!
print(
compute_missing_metrics(ds.drop_vars(['e3u', 'e3v', 'e3f', 'e3w']).copy(), all_scale_factors=['e3u'])
)
print(
compute_missing_metrics(ds.drop_vars(['e3u', 'e3v', 'e3f', 'e3w']).copy(), all_scale_factors=['e3u'])
)
--------------------------------------------------------------------------- ValueError Traceback (most recent call last) /tmp/ipykernel_732/4090319970.py in ?() 1 print( ----> 2 compute_missing_metrics(ds.drop_vars(['e3u', 'e3v', 'e3f', 'e3w']).copy(), all_scale_factors=['e3u']) 3 ) ~/checkouts/readthedocs.org/user_builds/xnemogcm/envs/latest/lib/python3.10/site-packages/xarray/core/dataset.py in ?(self, names, errors) 5990 names_set = {names} 5991 else: 5992 names_set = set(names) 5993 if errors == "raise": -> 5994 self._assert_all_in_dataset(names_set) 5995 5996 # GH6505 5997 other_names = set() ~/checkouts/readthedocs.org/user_builds/xnemogcm/envs/latest/lib/python3.10/site-packages/xarray/core/dataset.py in ?(self, names, virtual_okay) 5854 if virtual_okay: 5855 bad_names -= self.virtual_variables 5856 if bad_names: 5857 ordered_bad_names = [name for name in names if name in bad_names] -> 5858 raise ValueError( 5859 f"These variables cannot be found in this dataset: {ordered_bad_names}" 5860 ) ValueError: These variables cannot be found in this dataset: ['e3f']