-
Notifications
You must be signed in to change notification settings - Fork 19
Add map between MPAS-Ocean and Omega variables #237
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
xylar
merged 12 commits into
E3SM-Project:main
from
xylar:map-mpaso-omega-variable-names
Oct 21, 2024
Merged
Changes from 6 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
714fd8c
Add map between MPAS-Ocean and Omega variables
xylar e79d3cf
Update variable mapping methods
xylar 68c036f
Add documentation of the new feature
xylar f19cea7
Add OceanIOStep for reading/writing ocean model files
xylar 702a178
Add OceanIOStep to the docs
xylar e60ced5
Update convergence framework and manufactured solution
xylar 8127134
Rename methods and fix docstring based on review
xylar 76135a5
Pass kwargs to xr.open_dataset()
xylar 97242b4
Fix renaming
xylar 6215400
Add ssh to mpaso --> omega map
xylar 0a17c6d
Update docs
xylar 72fa2e4
Move ocean-specific docs to ocean framework
xylar File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,3 @@ | ||
| from polaris.ocean.model.ocean_io_step import OceanIOStep | ||
| from polaris.ocean.model.ocean_model_step import OceanModelStep | ||
| from polaris.ocean.model.time import get_time_interval_string |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,154 @@ | ||
| import importlib.resources as imp_res | ||
| from typing import Dict, Union | ||
|
|
||
| import xarray as xr | ||
| from mpas_tools.io import write_netcdf | ||
| from ruamel.yaml import YAML | ||
|
|
||
| from polaris import Step | ||
|
|
||
|
|
||
| class OceanIOStep(Step): | ||
| """ | ||
| A step that writes input and/or output files for Omega or MPAS-Ocean | ||
| Attributes | ||
| ---------- | ||
| mpaso_to_omega_var_map : dict | ||
| A map from MPAS-Ocean variable names to their Omega equivalents | ||
| omega_to_mpaso_var_map : dict | ||
| A map from Omega variable names to their MPAS-Ocean equivalents, the | ||
| inverse of ``mpaso_to_omega_var_map`` | ||
| """ | ||
| def __init__(self, component, name, **kwargs): | ||
| """ | ||
| Create a new step | ||
| Parameters | ||
| ---------- | ||
| component : polaris.Component | ||
| The component the step belongs to | ||
| name : str | ||
| the name of the task | ||
| kwargs | ||
| keyword arguments passed to `polaris.Step()` | ||
| """ | ||
| super().__init__( | ||
| component=component, name=name, **kwargs) | ||
|
|
||
| self.mpaso_to_omega_var_map: Union[None, Dict[str, str]] = None | ||
| self.omega_to_mpaso_var_map: Union[None, Dict[str, str]] = None | ||
|
|
||
| def setup(self): | ||
| """ | ||
| Determine if we will make yaml files or namelists and streams files, | ||
| then, determine the number of MPI tasks to use based on the estimated | ||
| mesh size | ||
xylar marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| """ | ||
| config = self.config | ||
| model = config.get('ocean', 'model') | ||
| if model == 'omega': | ||
| self._read_var_map() | ||
| elif model != 'mpas-ocean': | ||
| raise ValueError(f'Unexpected ocean model: {model}') | ||
| super().setup() | ||
|
|
||
| def map_to_model_dataset(self, ds): | ||
| """ | ||
| If the model is Omega, rename variables in a dataset from their | ||
| MPAS-Ocean names to the Omega equivalent (appropriate for input | ||
| datasets like an initial condition) | ||
| Parameters | ||
| ---------- | ||
| ds : xarray.Dataset | ||
| A dataset containing MPAS-Ocean variable names | ||
| Returns | ||
| ------- | ||
| ds : xarray.Dataset | ||
| The same dataset with variables renamed as appropriate for the | ||
| ocean model being run | ||
| """ | ||
| config = self.config | ||
| model = config.get('ocean', 'model') | ||
| if model == 'omega': | ||
| assert self.mpaso_to_omega_var_map is not None | ||
| ds = ds.rename(self.mpaso_to_omega_var_map) | ||
| return ds | ||
|
|
||
| def write_model_dataset(self, ds, filename): | ||
| """ | ||
| Write out the given dataset, mapping variable names from MPAS-Ocean | ||
| to Omega names if appropriate | ||
| Parameters | ||
| ---------- | ||
| ds : xarray.Dataset | ||
| A dataset containing MPAS-Ocean variable names | ||
| filename : str | ||
| The path for the NetCDF file to write | ||
| """ | ||
| ds = self.map_to_model_dataset(ds) | ||
| write_netcdf(ds=ds, fileName=filename) | ||
|
|
||
| def map_from_model_dataset(self, ds): | ||
| """ | ||
| If the model is Omega, rename variables in a dataset from their | ||
| Omega names to the MPAS-Ocean equivalent (appropriate for datasets | ||
| that are output from the model) | ||
| Parameters | ||
| ---------- | ||
| ds : xarray.Dataset | ||
| A dataset containing variable names native to either ocean model | ||
| Returns | ||
| ------- | ||
| ds : xarray.Dataset | ||
| The same dataset with variables named as expected in MPAS-Ocean | ||
| """ | ||
| config = self.config | ||
| model = config.get('ocean', 'model') | ||
| if model == 'omega': | ||
| assert self.omega_to_mpaso_var_map is not None | ||
| ds = ds.rename(self.omega_to_mpaso_var_map) | ||
| return ds | ||
|
|
||
| def open_model_dataset(self, filename): | ||
| """ | ||
| Open the given dataset, mapping variable names from Omega to MPAS-Ocean | ||
| names if appropriate | ||
| Parameters | ||
| ---------- | ||
| filename : str | ||
| The path for the NetCDF file to open | ||
| Returns | ||
| ------- | ||
| ds : xarray.Dataset | ||
| The dataset with variables named as expected in MPAS-Ocean | ||
| """ | ||
| ds = xr.open_dataset(filename) | ||
xylar marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| ds = self.map_from_model_dataset(ds) | ||
| return ds | ||
|
|
||
| def _read_var_map(self): | ||
| """ | ||
| Read the map from MPAS-Ocean to Omega config options | ||
| """ | ||
| package = 'polaris.ocean.model' | ||
| filename = 'mpaso_to_omega.yaml' | ||
| text = imp_res.files(package).joinpath(filename).read_text() | ||
|
|
||
| yaml_data = YAML(typ='rt') | ||
| nested_dict = yaml_data.load(text) | ||
| self.mpaso_to_omega_var_map = nested_dict['variables'] | ||
| assert self.mpaso_to_omega_var_map is not None | ||
| self.omega_to_mpaso_var_map = { | ||
| v: k for k, v in self.mpaso_to_omega_var_map.items()} | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are we aware of others?