### Install xnemogcm Source: https://github.com/rcaneill/xnemogcm/blob/main/docs/index.md Installation commands for pip and conda environments. ```bash pip3 install xnemogcm ``` ```bash conda install -c conda-forge xnemogcm ``` -------------------------------- ### Complete NEMO Workflow Example Source: https://context7.com/rcaneill/xnemogcm/llms.txt A comprehensive example showing how to open NEMO files, create an xgcm grid, perform computations like volume-weighted means, interpolation, and differentiation, and load namelist parameters. Supports lazy loading with Dask. ```python from pathlib import Path from xnemogcm import ( open_nemo_and_domain_cfg, get_metrics, open_namelist ) from xnemogcm.metrics import compute_missing_metrics import xgcm # Define data paths data_dir = Path('/path/to/nemo/experiment/') output_dir = data_dir / 'output' domain_dir = data_dir / 'domain' # Open combined dataset with chunking for large files ds = open_nemo_and_domain_cfg( nemo_files=output_dir, domcfg_files=domain_dir, nemo_kwargs={'chunks': {'time_counter': 1}}, linear_free_surface=False ) # Compute any missing vertical scale factors compute_missing_metrics(ds, time_varying=True) # Create xgcm Grid with full metrics metrics = get_metrics(ds) grid = xgcm.Grid(ds, metrics=metrics, periodic=False) # Example: Compute volume-weighted mean temperature temperature = ds['votemper'] cell_volume = ds['e1t'] * ds['e2t'] * ds['e3t'] weighted_temp = (temperature * cell_volume).sum(['x_c', 'y_c', 'z_c']) total_volume = cell_volume.sum(['x_c', 'y_c', 'z_c']) mean_temp = weighted_temp / total_volume # Example: Interpolate temperature to velocity points temp_on_u = grid.interp(temperature, 'X', boundary='extend') temp_on_v = grid.interp(temperature, 'Y', boundary='extend') # Example: Compute horizontal temperature gradient dT_dx = grid.derivative(temperature, 'X', boundary='extend') dT_dy = grid.derivative(temperature, 'Y', boundary='extend') # Load namelist for run parameters namelist = open_namelist(datadir=data_dir) timestep = float(namelist.get('rn_rdt', 3600)) print(f"Model timestep: {timestep} seconds") # Save processed data mean_temp.to_netcdf('mean_temperature.nc') ``` -------------------------------- ### Install xnemogcm via package managers Source: https://github.com/rcaneill/xnemogcm/blob/main/README.md Use conda or pip to install the package in your environment. ```shell conda install --channel conda-forge xnemogcm ``` ```shell pip install xnemogcm ``` -------------------------------- ### Display xnemogcm Version Source: https://github.com/rcaneill/xnemogcm/blob/main/docs/examples/recombing_mesh_mask_domain_cfg.ipynb Print the installed version of the xnemogcm library. ```python xnemogcm_version ``` -------------------------------- ### Open NEMO output and initialize xgcm grid Source: https://github.com/rcaneill/xnemogcm/blob/main/docs/index.md Load NEMO output files and domain configuration, then create an xgcm grid object with metrics. ```python from pathlib import Path from xnemogcm import open_nemo_and_domain_cfg ds = open_nemo_and_domain_cfg( nemo_files='/path/to/output/files', domcfg_files='/path/to/domain_cfg/mesh_mask/files' ) # Interface with xgcm from xnemogcm import get_metrics import xgcm grid = xgcm.Grid(ds, metrics=get_metrics(ds), periodic=False) ``` -------------------------------- ### Open and access namelist parameters Source: https://github.com/rcaneill/xnemogcm/blob/main/docs/examples/open_process_files.ipynb Load the namelist from the directory and access specific configuration variables. ```python name = open_namelist(datadir) name ``` ```python name.nn_it000 ``` -------------------------------- ### Open NEMO dataset with domain configuration Source: https://github.com/rcaneill/xnemogcm/blob/main/docs/examples/compute_metrics.ipynb Load NEMO model data and domain configuration files into a dataset. Prints the dataset object. ```python ds = open_nemo_and_domain_cfg(nemo_files=datadir, domcfg_files=datadir) print(ds) ``` -------------------------------- ### Open Multi-File Domain Configuration Source: https://github.com/rcaneill/xnemogcm/blob/main/docs/examples/recombing_mesh_mask_domain_cfg.ipynb Open a domain configuration from a directory containing multiple mesh_mask files. The `open_domain_cfg` function automatically handles the recombination. ```python domcfg = open_domain_cfg(datadir=datadir) domcfg ``` -------------------------------- ### Compare Domain Configurations Source: https://github.com/rcaneill/xnemogcm/blob/main/docs/examples/recombing_mesh_mask_domain_cfg.ipynb Compare two domain configuration DataArrays to verify if they are identical. This is useful after opening multi-file and single-file configurations. ```python domcfg_1_file.equals(domcfg) ``` -------------------------------- ### Open NEMO Domain Configuration Files Source: https://context7.com/rcaneill/xnemogcm/llms.txt Use open_domain_cfg to load and merge domain_cfg or mesh_mask files, including those split across multiple processors. ```python from pathlib import Path from xnemogcm import open_domain_cfg # Method 1: Provide directory path (auto-detects domain_cfg*.nc and mesh_mask*.nc files) domcfg = open_domain_cfg(datadir='/path/to/nemo/output/') # Method 2: Provide specific file names within directory domcfg = open_domain_cfg(datadir='/path/to/data/', files=['mesh_mask.nc']) # Method 3: Provide full file paths using glob patterns domcfg = open_domain_cfg(files=Path('/path/to/data/').glob('*mesh_mask*.nc')) # Method 4: Open multi-processor split files (automatically recombined) # Works with files like mesh_mask_0000.nc, mesh_mask_0001.nc, etc. domcfg = open_domain_cfg(datadir='/path/to/multi_proc_output/') # Access grid metrics and coordinates print(domcfg.coords) # x_c, x_f, y_c, y_f, z_c, z_f coordinates print(domcfg['e1t']) # Grid spacing in X direction at T points print(domcfg['glamt']) # Longitude at T points print(domcfg['gphit']) # Latitude at T points ``` -------------------------------- ### Display help for open_namelist Source: https://github.com/rcaneill/xnemogcm/blob/main/docs/examples/open_process_files.ipynb Access the docstring and usage information for the open_namelist function. ```python help(open_namelist) ``` -------------------------------- ### Open Single-File Domain Configuration Source: https://github.com/rcaneill/xnemogcm/blob/main/docs/examples/recombing_mesh_mask_domain_cfg.ipynb Open a domain configuration from a single mesh_mask file. This is used for comparison with multi-file configurations. ```python domcfg_1_file = open_domain_cfg(datadir=Path('../../xnemogcm/test/data/4.2.0/mesh_mask_1_file/'), files=['mesh_mask.nc']) ``` -------------------------------- ### Import xnemogcm Functions Source: https://github.com/rcaneill/xnemogcm/blob/main/docs/examples/open_process_files.ipynb Import necessary functions from the xnemogcm library, including those for opening different file types and the library's version. ```python from pathlib import Path from os import listdir from xnemogcm import open_domain_cfg, open_nemo, process_nemo, open_namelist, open_nemo_and_domain_cfg from xnemogcm import __version__ as xnemogcm_version ``` -------------------------------- ### Create Point Objects for Coordinate Mappings Source: https://context7.com/rcaneill/xnemogcm/llms.txt Demonstrates creating Point objects for different grid cell types (center, face) and printing their coordinate mappings. Useful for understanding grid cell locations. ```python from xnemogcm import Point t_point = Point('T') print(t_point.x, t_point.y, t_point.z) # x_c, y_c, z_c (cell centers) u_point = Point('U') print(u_point.x, u_point.y, u_point.z) # x_f, y_c, z_c (X-face, Y-center, Z-center) v_point = Point('V') print(v_point.x, v_point.y, v_point.z) # x_c, y_f, z_c (X-center, Y-face, Z-center) w_point = Point('W') print(w_point.x, w_point.y, w_point.z) # x_c, y_c, z_f (centers, Z-face) f_point = Point('F') print(f_point.x, f_point.y, f_point.z) # x_f, y_f, z_c (X-face, Y-face, Z-center) # W-variant points (velocity at W levels) uw_point = Point('UW') print(uw_point.x, uw_point.y, uw_point.z) # x_f, y_c, z_f ``` -------------------------------- ### Import xnemogcm and related modules Source: https://github.com/rcaneill/xnemogcm/blob/main/docs/examples/compute_metrics.ipynb Import necessary modules from xnemogcm and pathlib for file operations. ```python 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 ``` -------------------------------- ### Create xgcm Grid with Metrics Source: https://context7.com/rcaneill/xnemogcm/llms.txt Extracts scale factors from a dataset to create an xgcm.Grid object for grid-aware computations like interpolation and differentiation. ```python from xnemogcm import open_nemo_and_domain_cfg, get_metrics import xgcm # Open the combined dataset ds = open_nemo_and_domain_cfg( nemo_files='/path/to/output/', domcfg_files='/path/to/domain/' ) # Get metrics dictionary for xgcm metrics = get_metrics(ds) # Returns: {('X',): ['e1t', 'e1u', 'e1v', 'e1f'], # ('Y',): ['e2t', 'e2u', 'e2v', 'e2f'], # ('Z',): ['e3t', 'e3u', 'e3v', ...]} # Create xgcm Grid with metrics grid = xgcm.Grid(ds, metrics=metrics, periodic=False) # Now perform grid-aware operations # Interpolate temperature from T-point to U-point temp_on_u = grid.interp(ds['votemper'], 'X') # Compute derivative dT_dx = grid.derivative(ds['votemper'], 'X') # Integrate with proper cell volumes integral = grid.integrate(ds['votemper'], ['X', 'Y', 'Z']) ``` -------------------------------- ### Define namelist data directory Source: https://github.com/rcaneill/xnemogcm/blob/main/docs/examples/open_process_files.ipynb Set the path to the folder containing reference and configuration namelist files. ```python datadir = Path('../../xnemogcm/test/data/namelist/') ``` -------------------------------- ### Compute time-constant metrics Source: https://github.com/rcaneill/xnemogcm/blob/main/docs/examples/compute_metrics.ipynb Computes time-constant metrics (e.g., e3x_0) by creating a copy of the dataset. Prints the result. ```python print( compute_missing_metrics(ds.copy(), time_varying=False) ) ``` -------------------------------- ### Import xnemogcm Functions Source: https://github.com/rcaneill/xnemogcm/blob/main/docs/examples/recombing_mesh_mask_domain_cfg.ipynb Import necessary functions from the xnemogcm library and standard Python modules for file path manipulation. ```python from pathlib import Path from os import listdir from xnemogcm import open_domain_cfg, open_nemo_and_domain_cfg from xnemogcm import __version__ as xnemogcm_version ``` -------------------------------- ### Open NEMO Output Files Source: https://context7.com/rcaneill/xnemogcm/llms.txt Use open_nemo to load NEMO output files, requiring a pre-opened domain configuration for coordinate mapping. ```python from xnemogcm import open_domain_cfg, open_nemo # First, open the domain configuration domcfg = open_domain_cfg(datadir='/path/to/data/') # Open NEMO output files (requires domcfg for coordinate information) # Method 1: Auto-detect files with *grid_*.nc pattern nemo_ds = open_nemo(domcfg=domcfg, datadir='/path/to/output/') # Method 2: Provide specific files nemo_ds = open_nemo( domcfg=domcfg, files=['/path/to/GYRE_grid_T.nc', '/path/to/GYRE_grid_U.nc'] ) # Method 3: With chunking for large datasets (uses Dask) nemo_ds = open_nemo( domcfg=domcfg, datadir='/path/to/output/', chunks={'time_counter': 10} # Use original dimension names for chunks ) # Method 4: Parallel processing for faster loading nemo_ds = open_nemo( domcfg=domcfg, datadir='/path/to/output/', parallel=True ) # Variables are now on proper grid coordinates # T-point variables use (x_c, y_c, z_c) # U-point variables use (x_f, y_c, z_c) # V-point variables use (x_c, y_f, z_c) # W-point variables use (x_c, y_c, z_f) ``` -------------------------------- ### Display help for compute_missing_metrics Source: https://github.com/rcaneill/xnemogcm/blob/main/docs/examples/compute_metrics.ipynb Show the documentation and usage information for the compute_missing_metrics function. ```python help(compute_missing_metrics) ``` -------------------------------- ### Open NEMO and Domain Files Simultaneously Source: https://github.com/rcaneill/xnemogcm/blob/main/docs/examples/open_process_files.ipynb Opens both NEMO output and domain configuration files into a single xarray Dataset. This function merges two internally created datasets and supports all argument options available for `open_nemo` and `open_domain_cfg`. ```python help(open_nemo_and_domain_cfg) ``` ```python # the simplest for simple cases, provide the path ds = open_nemo_and_domain_cfg(nemo_files=datadir, domcfg_files=datadir) # or provide the files ds = open_nemo_and_domain_cfg(nemo_files=datadir.glob('*grid*.nc'), domcfg_files=datadir.glob('*mesh*.nc')) # or use the nemo_kwargs and domcfg_kwargs dictionaries ds = open_nemo_and_domain_cfg(nemo_kwargs=dict(datadir=datadir), domcfg_kwargs={'files':datadir.glob('*mesh*.nc')}) ds ``` -------------------------------- ### List Files in Data Directory Source: https://github.com/rcaneill/xnemogcm/blob/main/docs/examples/recombing_mesh_mask_domain_cfg.ipynb Print a list of all files present in the specified data directory. ```python print(listdir(datadir)) ``` -------------------------------- ### Open NEMO and Domain Configuration Source: https://context7.com/rcaneill/xnemogcm/llms.txt Opens and merges NEMO output and domain configuration files into a single xarray Dataset. Supports directory paths, file generators, and custom keyword arguments for underlying functions. ```python from pathlib import Path from xnemogcm import open_nemo_and_domain_cfg # Method 1: Simple case - provide directory paths ds = open_nemo_and_domain_cfg( nemo_files='/path/to/nemo/output/', domcfg_files='/path/to/domain/' ) # Method 2: Provide file lists/generators ds = open_nemo_and_domain_cfg( nemo_files=Path('/path/to/data/').glob('*grid*.nc'), domcfg_files=Path('/path/to/data/').glob('*mesh*.nc') ) # Method 3: Pass additional kwargs to underlying functions ds = open_nemo_and_domain_cfg( nemo_files='/path/to/output/', domcfg_files='/path/to/domain/', nemo_kwargs={'chunks': {'time_counter': 10}}, domcfg_kwargs={'add_coordinates': True} ) # Method 4: Handle linear free surface (adds e3x from e3x_0) ds = open_nemo_and_domain_cfg( nemo_files='/path/to/output/', domcfg_files='/path/to/domain/', linear_free_surface=True # Copy e3x_0 to e3x if not present ) # The merged dataset contains both grid info and output variables print(ds['votemper']) # Temperature variable print(ds['e1t']) # Grid metric from domain print(ds['glamt']) # Longitude coordinate ``` -------------------------------- ### Open NEMO Domain Configuration Source: https://github.com/rcaneill/xnemogcm/blob/main/docs/examples/open_process_files.ipynb Opens NEMO domain configuration files (domain_cfg and mesh_mask). You can provide file paths, a directory with standard filenames, or glob patterns for the files. The `domcfg` dataset is essential for processing NEMO output variables. ```python help(open_domain_cfg) ``` ```python datadir = Path('../../xnemogcm/test/data/4.2.0/open_and_merge/') print(listdir(datadir)) ``` ```python domcfg = open_domain_cfg(datadir=datadir) # or domcfg = open_domain_cfg(datadir=datadir, files=['mesh_mask.nc']) # or domcfg = open_domain_cfg(files=datadir.glob('*mesh_mask*.nc')) domcfg ``` -------------------------------- ### Process Pre-opened Datasets Source: https://context7.com/rcaneill/xnemogcm/llms.txt Use process_nemo to assign existing xarray Datasets to correct grid positions, either explicitly or via metadata attributes. ```python import xarray as xr from xnemogcm import open_domain_cfg, process_nemo domcfg = open_domain_cfg(datadir='/path/to/data/') # Open datasets manually from any source ds_t = xr.open_dataset('/path/to/T.nc') ds_u = xr.open_dataset('/path/to/U.nc') ds_v = xr.open_dataset('/path/to/V.nc') ds_w = xr.open_dataset('/path/to/W.nc') # Method 1: Explicitly specify grid position for each dataset nemo_ds = process_nemo( positions=[ (ds_t, 'T'), (ds_u, 'U'), (ds_v, 'V'), (ds_w, 'W') ], domcfg=domcfg ) # Method 2: Let xnemogcm infer positions from 'description' attribute # (Requires datasets to have attribute like 'ocean T grid variables') nemo_ds = process_nemo( positions=[ (ds_t, None), # Position inferred from attribute (ds_u, None), (ds_v, None), (ds_w, None) ], domcfg=domcfg ) # Method 3: With parallel processing via Dask nemo_ds = process_nemo( positions=[(ds_t, 'T'), (ds_u, 'U')], domcfg=domcfg, parallel=True ) ``` -------------------------------- ### Define data directory path Source: https://github.com/rcaneill/xnemogcm/blob/main/docs/examples/compute_metrics.ipynb Specify the directory containing NEMO model data files. ```python datadir = Path('../../xnemogcm/test/data/4.2.0/open_and_merge/') ``` -------------------------------- ### Open NEMO Output Files Source: https://github.com/rcaneill/xnemogcm/blob/main/docs/examples/open_process_files.ipynb Opens NEMO output files, requiring the `domcfg` dataset to correctly position variables on the grid. Supports various methods for specifying input files and allows passing extra arguments to `xarray.open_mfdataset`. ```python help(open_nemo) ``` ```python nemo = open_nemo(domcfg=domcfg, datadir=datadir) # or nemo = open_nemo(domcfg=domcfg, files=datadir.glob('*grid*.nc')) # or, using attributes from dataset and not name datadir2 = Path('../../xnemogcm/test/data/4.2.0/nemo_no_grid_in_filename/') nemo = open_nemo( domcfg=domcfg, files=[ datadir2 / 'T.nc', datadir2 / 'U.nc', datadir2 / 'V.nc', datadir2 / 'W.nc' ] ) nemo ``` -------------------------------- ### Compute missing metrics (copy) Source: https://github.com/rcaneill/xnemogcm/blob/main/docs/examples/compute_metrics.ipynb Computes missing metrics by creating a copy of the dataset. Prints the modified dataset. ```python # If you just want to get a copy print( compute_missing_metrics(ds.copy()) ) ``` -------------------------------- ### Process NEMO Output Files Manually Source: https://github.com/rcaneill/xnemogcm/blob/main/docs/examples/open_process_files.ipynb Manually processes NEMO output files using `process_nemo`, which is useful when files are already opened or retrieved from sources like zarr. Requires a list of tuples, where each tuple contains an xarray Dataset and its grid position type ('T', 'U', 'V', 'W'), along with the `domcfg` dataset. ```python help(process_nemo) ``` ```python import xarray as xr datadir2 = Path('../../xnemogcm/test/data/4.2.0/nemo_no_grid_in_filename/') nemo = process_nemo( positions=[ (xr.open_dataset(datadir2 / 'T.nc'), 'T'), (xr.open_dataset(datadir2 / 'U.nc'), 'U'), (xr.open_dataset(datadir2 / 'V.nc'), 'V'), (xr.open_dataset(datadir2 / 'W.nc'), 'W') ], domcfg=domcfg ) # or, if the datasets contain the attribute 'description' nemo = process_nemo( positions=[ (xr.open_dataset(datadir2 / 'T.nc'), None), (xr.open_dataset(datadir2 / 'U.nc'), None), (xr.open_dataset(datadir2 / 'V.nc'), None), (xr.open_dataset(datadir2 / 'W.nc'), None) ], domcfg=domcfg ) ``` -------------------------------- ### Compute missing metrics (in-place) Source: https://github.com/rcaneill/xnemogcm/blob/main/docs/examples/compute_metrics.ipynb Computes missing metrics and modifies the dataset in-place. Prints the modified dataset. ```python # If you just want to add the scale factor inplace compute_missing_metrics(ds) print(ds) ``` -------------------------------- ### Specify Data Directory Source: https://github.com/rcaneill/xnemogcm/blob/main/docs/examples/recombing_mesh_mask_domain_cfg.ipynb Define the Path object pointing to the directory containing NEMO mesh_mask files for processing. ```python datadir = Path('../../xnemogcm/test/data/4.2.0/mesh_mask_multi_files/') ``` -------------------------------- ### Open NEMO Namelists Source: https://context7.com/rcaneill/xnemogcm/llms.txt Reads Fortran namelist files into an xarray Dataset. Useful for comparing parameters across different model runs. ```python from xnemogcm import open_namelist # Open reference and configuration namelists from directory # Looks for namelist_ref and namelist_cfg files namelist = open_namelist(datadir='/path/to/run/') # Open specific namelist files namelist = open_namelist( files=['/path/to/namelist_ref', '/path/to/namelist_cfg'] ) # Open only the reference namelist namelist = open_namelist( datadir='/path/to/run/', ref=True, cfg=False ) # Access namelist parameters print(namelist['nn_it000']) # Starting timestep print(namelist['nn_itend']) # Ending timestep print(namelist['rn_rdt']) # Timestep in seconds # Check which namelist block a parameter belongs to print(namelist['nn_it000'].attrs['namelist']) # e.g., 'namrun' # Compare parameters between different runs namelist1 = open_namelist(datadir='/path/to/run1/') namelist2 = open_namelist(datadir='/path/to/run2/') print(f"Run1 dt: {float(namelist1['rn_rdt'])}") print(f"Run2 dt: {float(namelist2['rn_rdt'])}") ``` -------------------------------- ### Compute a subset of metrics Source: https://github.com/rcaneill/xnemogcm/blob/main/docs/examples/compute_metrics.ipynb Computes only a specified subset of metrics (e.g., 'e3u') after dropping other potential metric variables from a copy of the dataset. Prints the result. ```python print( compute_missing_metrics(ds.drop_vars(['e3u', 'e3v', 'e3f', 'e3w']).copy(), all_scale_factors=['e3u']) ) ``` -------------------------------- ### Access Arakawa C-Grid Points Source: https://context7.com/rcaneill/xnemogcm/llms.txt Provides access to Arakawa C-grid position definitions and coordinate names. ```python from xnemogcm.arakawa_points import Point, ALL_POINTS # Available grid points print(ALL_POINTS) # ['UW', 'VW', 'FW', 'T', 'U', 'V', 'F', 'W'] ``` -------------------------------- ### Compute Missing Scale Factors Source: https://context7.com/rcaneill/xnemogcm/llms.txt Calculates missing vertical scale factors by interpolating from e3t. Can modify datasets in-place or return a copy. ```python from xnemogcm import open_nemo_and_domain_cfg from xnemogcm.metrics import compute_missing_metrics ds = open_nemo_and_domain_cfg( nemo_files='/path/to/output/', domcfg_files='/path/to/domain/' ) # Compute missing time-varying scale factors (e3x) from e3t # Modifies dataset in-place compute_missing_metrics(ds, time_varying=True) # Or create a copy with computed metrics ds_with_metrics = compute_missing_metrics(ds.copy(), time_varying=True) # Compute time-constant scale factors (e3x_0) instead ds_const = compute_missing_metrics(ds.copy(), time_varying=False) # Compute only specific scale factors ds_subset = compute_missing_metrics( ds.copy(), all_scale_factors=['e3u', 'e3v'], # Only compute these time_varying=True ) # Available scale factors that can be computed: # ['e3t', 'e3u', 'e3v', 'e3f', 'e3w', 'e3uw', 'e3vw', 'e3fw'] ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.