Skip to content

Commit c1150e3

Browse files
authored
Merge pull request #391 from xylar/update-to-0.9.0-alpha.4
Switch to `Tranche` config parser
2 parents 0f14667 + ee1939f commit c1150e3

File tree

9 files changed

+55
-47
lines changed

9 files changed

+55
-47
lines changed

deploy/conda-dev-spec.template

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ cmocean
66
esmf={{ esmf }}={{ mpi_prefix }}_*
77
ffmpeg
88
geometric_features={{ geometric_features }}
9+
gsw
910
importlib_resources
1011
ipython
1112
jupyter
@@ -33,6 +34,7 @@ requests
3334
ruamel.yaml
3435
scipy>=1.8.0
3536
shapely>=2.0,<3.0
37+
tranche>=0.3.0
3638
xarray
3739

3840
# Static typing

docs/conf.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,8 @@
7676
'python': ('https://docs.python.org', None),
7777
'scipy': ('https://docs.scipy.org/doc/scipy/reference', None),
7878
"sphinx": ("https://www.sphinx-doc.org/en/master", None),
79-
'xarray': ('https://xarray.pydata.org/en/stable', None)
79+
'xarray': ('https://xarray.pydata.org/en/stable', None),
80+
'tranche': ('https://xylar.github.io/tranche/', None),
8081
}
8182

8283
# -- MyST settings ---------------------------------------------------

docs/developers_guide/framework/config.md

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22

33
# Config files
44

5-
The primary documentation for the config parser is in
6-
[MPAS-Tools config parser](http://mpas-dev.github.io/MPAS-Tools/stable/config.html).
5+
The primary documentation for the config parser is in the
6+
[tranche documentation](https://xylar.github.io/tranche/).
77
Here, we include some specific details relevant to using the
8-
{py:class}`mpas_tools.config.MpasConfigParser` in polaris.
8+
{py:class}`tranche.Tranche` in polaris.
99

1010
Here, we provide the {py:class}`polaris.config.PolarisConfigParser` that has
1111
almost the same functionality but also ensures that certain relative paths are
@@ -18,7 +18,7 @@ options) as part of setting up polaris tasks and steps. These features are
1818
included to accommodate sharing config options across shared steps and/or
1919
multiple tasks.
2020

21-
The {py:meth}`mpas_tools.config.MpasConfigParser.add_from_package()` method can
21+
The {py:meth}`tranche.Tranche.add_from_package()` method can
2222
be used to add the contents of a config file within a package to the config
2323
options. Examples of this can be found in many tasks as well as in the
2424
`polaris.setup` module. Here is a typical example from
@@ -71,11 +71,12 @@ for use by the framework rather than individual tasks.
7171
Other methods for the `MpasConfigParser` are similar to those for
7272
{py:class}`configparser.ConfigParser`. In addition to `get()`,
7373
`getinteger()`, `getfloat()` and `getboolean()` methods, this class
74-
implements {py:meth}`mpas_tools.config.MpasConfigParser.getlist()`, which
74+
implements {py:meth}`tranche.Tranche.getlist()`, which
7575
can be used to parse a config value separated by spaces and/or commas into
76-
a list of strings, floats, integers, booleans, etc. Another useful method
77-
is {py:meth}`mpas_tools.config.MpasConfigParser.getexpression()`, which can
78-
be used to get python dictionaries, lists and tuples as well as a small set
76+
a list of strings, floats, integers, booleans, etc. Other useful methods
77+
are {py:meth}`tranche.Tranche.getexpression()`, which can
78+
be used to get python dictionaries, lists, and tuples, and
79+
{py:meth}`tranche.Tranche.getnumpy()`, which also suppports a small set
7980
of functions (`range()`, {py:meth}`numpy.linspace()`,
8081
{py:meth}`numpy.arange()`, and {py:meth}`numpy.array()`)
8182

@@ -182,9 +183,10 @@ class Rpe(Task):
182183

183184
## Comments in config files
184185

185-
One of the main advantages of {py:class}`mpas_tools.config.MpasConfigParser`
186+
One of the main advantages of {py:class}`tranche.Tranche`
186187
over {py:class}`configparser.ConfigParser` is that it keeps track of comments
187188
that are associated with config sections and options.
188189

189-
See [comments in config files](http://mpas-dev.github.io/MPAS-Tools/stable/config.html#config_comments)
190-
in MPAS-Tools for more details.
190+
Comments must begin with the `#` character. They must be placed *before* the
191+
config section or option in question (preferably without blank lines between).
192+
The comments can be any number of lines long.

polaris/config.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import os
22
from typing import Union
33

4-
from mpas_tools.config import MpasConfigParser
4+
from tranche import Tranche
55

66

7-
class PolarisConfigParser(MpasConfigParser):
7+
class PolarisConfigParser(Tranche):
88
"""
99
A "meta" config parser that keeps a dictionary of config parsers and their
1010
sources to combine when needed. The custom config parser allows provenance

polaris/job/__init__.py

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,9 @@ def _get_job_options(
244244
wall_time : str
245245
filesystems : str
246246
"""
247-
partition_or_queue = config.get('job', partition_or_queue_option)
247+
par_section = config['parallel']
248+
job_section = config['job']
249+
partition_or_queue = job_section.get(partition_or_queue_option)
248250
if partition_or_queue == '<<<default>>>':
249251
if machine == 'anvil' and partition_or_queue == 'partition':
250252
# choose the partition based on the number of nodes
@@ -254,37 +256,35 @@ def _get_job_options(
254256
partition_or_queue = 'acme-medium'
255257
else:
256258
partition_or_queue = 'acme-large'
257-
elif config.has_option('parallel', partitions_or_queues):
259+
elif par_section.has_option(partitions_or_queues):
258260
# get the first, which is the default
259-
partition_or_queue = config.getlist(
260-
'parallel', partitions_or_queues
261-
)[0]
261+
partition_or_queue = par_section.getlist(partitions_or_queues)[0]
262262
else:
263263
partition_or_queue = ''
264264

265-
qos = config.get('job', 'qos')
265+
qos = job_section.get('qos')
266266
if qos == '<<<default>>>':
267-
if config.has_option('parallel', 'qos'):
268-
qos = config.getlist('parallel', 'qos')[0]
267+
if par_section.has_option('qos'):
268+
qos = par_section.getlist('qos')[0]
269269
else:
270270
qos = ''
271271

272-
constraint = config.get('job', 'constraint')
272+
constraint = job_section.get('constraint')
273273
if constraint == '<<<default>>>':
274-
if config.has_option('parallel', 'constraints'):
275-
constraint = config.getlist('parallel', 'constraints')[0]
274+
if par_section.has_option('constraints'):
275+
constraint = par_section.getlist('constraints')[0]
276276
else:
277277
constraint = ''
278278

279-
if config.has_option('parallel', 'gpus_per_node'):
280-
gpus_per_node = config.get('parallel', 'gpus_per_node')
279+
if par_section.has_option('gpus_per_node'):
280+
gpus_per_node = par_section.get('gpus_per_node')
281281
else:
282282
gpus_per_node = ''
283283

284-
wall_time = config.get('job', 'wall_time')
284+
wall_time = job_section.get('wall_time')
285285

286-
if config.has_option('job', 'filesystems'):
287-
filesystems = config.get('job', 'filesystems')
286+
if job_section.has_option('filesystems'):
287+
filesystems = job_section.get('filesystems')
288288
else:
289289
filesystems = ''
290290

polaris/ocean/model/ocean_model_step.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -303,13 +303,14 @@ def update_namelist_eos(self):
303303
Modify the namelist to make it consistent with eos config options
304304
"""
305305
config = self.config
306-
307-
eos_type = config.get('ocean', 'eos_type')
308-
eos_linear_alpha = config.getfloat('ocean', 'eos_linear_alpha')
309-
eos_linear_beta = config.getfloat('ocean', 'eos_linear_beta')
310-
eos_linear_rhoref = config.getfloat('ocean', 'eos_linear_rhoref')
311-
eos_linear_Tref = config.getfloat('ocean', 'eos_linear_Tref')
312-
eos_linear_Sref = config.getfloat('ocean', 'eos_linear_Sref')
306+
section = config['ocean']
307+
308+
eos_type = section.get('eos_type')
309+
eos_linear_alpha = section.getfloat('eos_linear_alpha')
310+
eos_linear_beta = section.getfloat('eos_linear_beta')
311+
eos_linear_rhoref = section.getfloat('eos_linear_rhoref')
312+
eos_linear_Tref = section.getfloat('eos_linear_Tref')
313+
eos_linear_Sref = section.getfloat('eos_linear_Sref')
313314

314315
replacements = {
315316
'config_eos_type': eos_type,

polaris/version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = '0.9.0-alpha.3'
1+
__version__ = '0.9.0-alpha.4'

polaris/viz/spherical.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -326,9 +326,11 @@ def _setup_colormap(config, colormap_section):
326326

327327
colormap = plt.get_cmap(config.get(colormap_section, 'colormap_name'))
328328

329-
norm_type = config.get(colormap_section, 'norm_type')
329+
section = config[colormap_section]
330330

331-
kwargs = config.getexpression(colormap_section, 'norm_args')
331+
norm_type = section.get('norm_type')
332+
333+
kwargs = section.getnumpy('norm_args')
332334

333335
if norm_type == 'symlog':
334336
norm = cols.SymLogNorm(**kwargs)
@@ -342,17 +344,15 @@ def _setup_colormap(config, colormap_section):
342344
)
343345

344346
try:
345-
ticks = config.getexpression(
346-
colormap_section, 'colorbar_ticks', use_numpyfunc=True
347-
)
347+
ticks = section.getnumpy('colorbar_ticks')
348348
except configparser.NoOptionError:
349349
ticks = None
350350

351-
if config.has_option(colormap_section, 'under_color'):
352-
under_color = config.get(colormap_section, 'under_color')
351+
if section.has_option('under_color'):
352+
under_color = section.get('under_color')
353353
colormap.set_under(under_color)
354-
if config.has_option(colormap_section, 'over_color'):
355-
over_color = config.get(colormap_section, 'over_color')
354+
if section.has_option('over_color'):
355+
over_color = section.get('over_color')
356356
colormap.set_over(over_color)
357357

358358
return colormap, norm, ticks

pyproject.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ classifiers = [
3030
dependencies = [
3131
"cartopy",
3232
"cmocean",
33+
"gsw",
3334
"importlib_resources",
3435
"ipython",
3536
"jigsawpy",
@@ -45,6 +46,7 @@ dependencies = [
4546
"requests",
4647
"scipy>=1.8.0",
4748
"shapely>=2.0,<3.0",
49+
"tranche>=0.3.0",
4850
"xarray",
4951
]
5052

0 commit comments

Comments
 (0)