From a3d26cf6532166153c5dbad2c50f8837e0e77d79 Mon Sep 17 00:00:00 2001 From: Michael Levy Date: Fri, 10 May 2024 09:56:05 -0600 Subject: [PATCH] Clean up imports 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. --- MARBL_tools/MARBL_generate_diagnostics_file.py | 7 ++++--- MARBL_tools/MARBL_utils.py | 8 ++++---- MARBL_tools/__init__.py | 1 + 3 files changed, 9 insertions(+), 7 deletions(-) diff --git a/MARBL_tools/MARBL_generate_diagnostics_file.py b/MARBL_tools/MARBL_generate_diagnostics_file.py index 6795b64d..de129de7 100755 --- a/MARBL_tools/MARBL_generate_diagnostics_file.py +++ b/MARBL_tools/MARBL_generate_diagnostics_file.py @@ -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__) @@ -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'] @@ -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) @@ -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? diff --git a/MARBL_tools/MARBL_utils.py b/MARBL_tools/MARBL_utils.py index 39b45fb3..43bfd233 100644 --- a/MARBL_tools/MARBL_utils.py +++ b/MARBL_tools/MARBL_utils.py @@ -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) """ @@ -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 @@ -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): diff --git a/MARBL_tools/__init__.py b/MARBL_tools/__init__.py index a52c42c4..c940c6cd 100644 --- a/MARBL_tools/__init__.py +++ b/MARBL_tools/__init__.py @@ -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