|
1 | 1 | import importlib.resources as imp_res |
| 2 | +import os |
2 | 3 | from typing import Dict, Union |
3 | 4 |
|
4 | 5 | import xarray as xr |
@@ -44,6 +45,10 @@ def configure(self, config): |
44 | 45 | """ |
45 | 46 | section = config['ocean'] |
46 | 47 | 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) |
47 | 52 | configs = {'mpas-ocean': 'mpas_ocean.cfg', 'omega': 'omega.cfg'} |
48 | 53 | if model not in configs: |
49 | 54 | raise ValueError(f'Unknown ocean model {model} in config options') |
@@ -228,6 +233,71 @@ def _read_var_map(self): |
228 | 233 | self.mpaso_to_omega_dim_map = nested_dict['dimensions'] |
229 | 234 | self.mpaso_to_omega_var_map = nested_dict['variables'] |
230 | 235 |
|
| 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 | + |
231 | 301 |
|
232 | 302 | # create a single module-level instance available to other components |
233 | 303 | ocean = Ocean() |
0 commit comments