Skip to content

Commit

Permalink
Clean up imports
Browse files Browse the repository at this point in the history
Previous commit let us call MARBL_generate_diagnostics_file.py from command
line, but caused problems when importing generate_diagnostics_file() into a
python script. This clean-up allows both use-cases.

Also, I renamed diag_mode_opts() -> valid_diag_modes() because that seems like
a more descriptive name.
  • Loading branch information
mnlevy1981 committed May 10, 2024
1 parent 1971bba commit a3d26cf
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 7 deletions.
7 changes: 4 additions & 3 deletions MARBL_tools/MARBL_generate_diagnostics_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,13 @@
-a, --append Append to existing diagnostics file (default: False)
"""

import MARBL_utils
#######################################

def generate_diagnostics_file(MARBL_diagnostics, diagnostics_file_out, diag_mode="full", append=False):
""" Produce a list of MARBL diagnostic frequencies and operators from a JSON parameter file
"""

from MARBL_tools import valid_diag_modes
import logging
logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -85,7 +85,7 @@ def generate_diagnostics_file(MARBL_diagnostics, diagnostics_file_out, diag_mode
# is also a dictionary containing frequency and operator information. Note that
# string values of frequency and operator are converted to lists of len 1 when the
# JSON file that generates this list is processed
diag_mode_opts = MARBL_utils.diag_mode_opts()
diag_mode_opts = valid_diag_modes()
diag_mode_in = diag_mode_opts.index(diag_mode)
for diag_name in sorted(MARBL_diagnostics.diagnostics_dict.keys()):
frequencies = MARBL_diagnostics.diagnostics_dict[diag_name]['frequency']
Expand All @@ -107,6 +107,7 @@ def _parse_args(marbl_root):
"""

import argparse
from MARBL_tools import valid_diag_modes

parser = argparse.ArgumentParser(description="Generate a MARBL settings file from a JSON file",
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
Expand Down Expand Up @@ -144,7 +145,7 @@ def _parse_args(marbl_root):

# Diagnostic mode (level of output to include)
parser.add_argument('-m', '--diag-mode', action='store', dest='diag_mode', default='full',
choices=MARBL_utils.diag_mode_opts(),
choices=valid_diag_modes(),
help='Level of output to include')

# Append to existing diagnostics file?
Expand Down
8 changes: 4 additions & 4 deletions MARBL_tools/MARBL_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
# PUBLIC MODULE METHODS #
################################################################################

def diag_mode_opts():
def valid_diag_modes():
""" Return ordered list of the valid values for diag_mode.
(Ordered list => selecting diag mode includes variables)
"""
Expand Down Expand Up @@ -98,7 +98,7 @@ def diagnostics_dictionary_is_consistent(DiagsDict):
ii. If they are all lists, must be same length
5. Allowable frequencies are never, low, medium, and high
6. Allowable operators are instantaneous, average, minimum, and maximum
7. Allowable diag_modes are defined in diag_mode_opts()
7. Allowable diag_modes are defined in valid_diag_modes()
"""

import logging
Expand Down Expand Up @@ -157,11 +157,11 @@ def diagnostics_dictionary_is_consistent(DiagsDict):

# 5. Allowable frequencies are never, low, medium, and high
# 6. Allowable operators are instantaneous, average, minimum, and maximum
# 7. Allowable diag_modes are defined in diag_mode_opts()
# 7. Allowable diag_modes are defined in valid_diag_modes()
# * "none" should not appear in the dictionary
ok_freqs = ['never', 'low', 'medium', 'high']
ok_ops = ['instantaneous', 'average', 'minimum', 'maximum']
ok_dms = diag_mode_opts()[1:] # do not include 'none' in ok_dms
ok_dms = valid_diag_modes()[1:] # do not include 'none' in ok_dms
invalid_freq_op_dm = False
if not isinstance(diag_dict['frequency'], dict):
if isinstance(diag_dict['frequency'], list):
Expand Down
1 change: 1 addition & 0 deletions MARBL_tools/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from .MARBL_diagnostics_file_class import MARBL_diagnostics_class
from .MARBL_utils import settings_dictionary_is_consistent
from .MARBL_utils import diagnostics_dictionary_is_consistent
from .MARBL_utils import valid_diag_modes
from .MARBL_share import abort
from .MARBL_share import LogFormatter
from .MARBL_share import sort
Expand Down

0 comments on commit a3d26cf

Please sign in to comment.