Skip to content

Commit b9f529e

Browse files
committed
By default, detect ocean model based on files in path
When calling `polaris setup` or `polaris suite` without the `--model` flag, the default behavior will now be to look for a list of required files in the component's path (providied with the `-p` flag) to see which model is detecte. If no model can be detected in this way, an error is raised.
1 parent 37d8818 commit b9f529e

File tree

2 files changed

+72
-2
lines changed

2 files changed

+72
-2
lines changed

polaris/ocean/ocean.cfg

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33

44
# Options related the ocean component
55
[ocean]
6-
# Which model, MPAS-Ocean or Omega, is used
7-
model = mpas-ocean
6+
# Which model, MPAS-Ocean or Omega, is used, or "detect" to auto-detect
7+
model = detect
88

99
# the number of cells per core to aim for
1010
goal_cells_per_core = 200

polaris/tasks/ocean/__init__.py

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import importlib.resources as imp_res
2+
import os
23
from typing import Dict, Union
34

45
import xarray as xr
@@ -44,6 +45,10 @@ def configure(self, config):
4445
"""
4546
section = config['ocean']
4647
model = section.get('model')
48+
if model == 'detect':
49+
model = self._detect_model(config)
50+
print('Detected ocean model:', model)
51+
config.set('ocean', 'model', model)
4752
configs = {'mpas-ocean': 'mpas_ocean.cfg', 'omega': 'omega.cfg'}
4853
if model not in configs:
4954
raise ValueError(f'Unknown ocean model {model} in config options')
@@ -228,6 +233,71 @@ def _read_var_map(self):
228233
self.mpaso_to_omega_dim_map = nested_dict['dimensions']
229234
self.mpaso_to_omega_var_map = nested_dict['variables']
230235

236+
def _detect_model(self, config) -> str:
237+
"""
238+
Detect which ocean model to use
239+
"""
240+
# build config options for each model, so the default component_path
241+
# can be read if it hasn't been overridden
242+
omega_config = config.copy()
243+
omega_config.add_from_package('polaris.ocean', 'omega.cfg')
244+
omega_path = omega_config.get('paths', 'component_path')
245+
246+
mpas_ocean_config = config.copy()
247+
mpas_ocean_config.add_from_package('polaris.ocean', 'mpas_ocean.cfg')
248+
mpas_ocean_path = mpas_ocean_config.get('paths', 'component_path')
249+
250+
if self._detect_omega_build(omega_path):
251+
return 'omega'
252+
elif self._detect_mpas_ocean_build(mpas_ocean_path):
253+
return 'mpas-ocean'
254+
else:
255+
raise ValueError(
256+
'Could not detect ocean model; neither MPAS-Ocean '
257+
'nor Omega appear to be available.'
258+
)
259+
260+
def _detect_omega_build(self, path) -> bool:
261+
"""
262+
Detect if Omega is available
263+
"""
264+
required_files = [
265+
'configs/Default.yml',
266+
'src/omega.exe',
267+
]
268+
path = os.path.abspath(path)
269+
270+
all_found = True
271+
for required_file in required_files:
272+
if not os.path.exists(os.path.join(path, required_file)):
273+
all_found = False
274+
break
275+
return all_found
276+
277+
def _detect_mpas_ocean_build(self, path) -> bool:
278+
"""
279+
Detect if MPAS-Ocean is available
280+
281+
Returns
282+
-------
283+
is_mpas_ocean : bool
284+
True if MPAS-Ocean appears to be available, False otherwise
285+
"""
286+
required_files = [
287+
'default_inputs/namelist.ocean.forward',
288+
'default_inputs/streams.ocean.forward',
289+
'src/Registry_processed.xml',
290+
'ocean_model',
291+
]
292+
path = os.path.abspath(path)
293+
294+
all_found = True
295+
for required_file in required_files:
296+
if not os.path.exists(os.path.join(path, required_file)):
297+
all_found = False
298+
break
299+
return all_found
300+
231301

232302
# create a single module-level instance available to other components
233303
ocean = Ocean()

0 commit comments

Comments
 (0)