Skip to content

Commit

Permalink
Merge pull request #291 from GEOS-ESM/feature/mathomp4/remap-argparse
Browse files Browse the repository at this point in the history
Convert remap_restart to use argparse
  • Loading branch information
sdrabenh authored Dec 8, 2022
2 parents 13703d4 + efc8073 commit 026666b
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 57 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Removed

## [1.6.3] - 2022-12-07
## [1.6.3] - 2022-12-08

### Changed

- Moved to GitHub Actions for label enforcement
- Updated CircleCI to Baselibs 7.7.0
- Set default data ocean to be `CS` at C90+ resolution in `remap_restarts.py`
- Updated `remap_restarts.py` to use argparse

### Fixed

Expand Down
1 change: 1 addition & 0 deletions GEOS_Util/post/remap_restart/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/__pycache__/
122 changes: 66 additions & 56 deletions GEOS_Util/post/remap_restart/remap_restarts.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,91 +3,101 @@
# source install/bin/g5_modules
#
# Newer GEOS code should load a module with GEOSpyD Python3 if not run:
# module load python/GEOSpyD/Min4.10.3_py3.9
# module load python/GEOSpyD/Min4.11.0_py3.9
#

import sys, getopt
import sys
import argparse
import textwrap
import ruamel.yaml
import questionary
from remap_utils import *
from remap_questions import get_config_from_questionary
from remap_questions import get_config_from_questionary
from remap_params import *
from remap_upper import *
from remap_lake_landice_saltwater import *
from remap_analysis import *
from remap_catchANDcn import *

def main(argv):
# Define the argument parser
def parse_args():

program_description = textwrap.dedent(f'''
USAGE:
There are three ways to use this script to remap restarts.
1. Use an existing config file to remap:
./remap_restarts.py -c my_config.yaml
2. Use questionary to convert template remap_params.tpl to
remap_params.yaml and then remap:
./remap_restarts.py
3. Use command line to input a flattened yaml file:
./remap_restarts.py -o input:air:drymass=1 input:air:hydrostatic=0 ...
NOTE: Each individual script can be executed independently
1. remap_questions.py generates raw_answer.yaml
2. remap_params.py uses raw_answer.yaml and remap_params.tpl as inputs and generates remap_params.yaml
3. remap_upper.py uses remap_params.yaml as input for remapping
4. remap_lake_landice_saltwater.py uses remap_params.yaml as input for remapping
5. remap_catchANDcn.py uses remap_params.yaml as input for remapping
6. remap_analysis.py uses remap_params.yaml as input for remapping
''')

parser = argparse.ArgumentParser(description='Remap restarts',epilog=program_description,formatter_class=argparse.RawDescriptionHelpFormatter)
# define a mutually exclusive group of arguments
group = parser.add_mutually_exclusive_group()
group.add_argument('-c', '--config_file', help='YAML config file')
group.add_argument('-o', '--flattened_yaml', help='Flattened YAML config', metavar='input:air:drymass=1 input:air:hydrostatic=0 ...')

# Parse using parse_known_args so we can pass the rest to the remap scripts
# If config_file is used, then extra_args will be empty
# If flattened_yaml is used, then extra_args will be populated
args, extra_args = parser.parse_known_args()
return args, extra_args

def main():

question_flag = False
cmd2yaml_flag = False
yaml_flag = False
config = ''

try:
opts, args = getopt.getopt(argv,"hc:o", ['config_file='])
except getopt.GetoptError:
print('Usage: remap_restarts.py -c remap_params.yaml or ./remap_restarts.py or ./remap_restarts.py -o key=value ....')
sys.exit('command line error')
for opt, arg in opts:
if opt == '-h':
print('''\nThere are two ways to use this script to remap restarts. \n
1) Use an exsiting config file to remap: \n
./remap_restarts.py -c my_config.yaml \n \n
2) Use questionary to convert template remap_params.tpl to \n
remap_params.yaml and then remap. \n
./remap_restarts.py \n
3) Use command line to input a flattened yaml file: \n
./remap_restarts.py -o input:air:drymass=1 input:air:hydrostatic=0 ...
\nHelp message: \n
1) Each individual script can be executed independently
2) remap_questions.py generates raw_answer.yaml
3) remap_params.py uses raw_answer.yaml and remap_params.tpl as inputs and generates remap_params.yaml
4) remap_upper.py uses remap_params.yaml as input for remapping
5) remap_lake_landice_saltwater.py uses remap_params.yaml as input for remapping
6) remap_catchANDcn.py uses remap_params.yaml as input for remapping
7) remap_analysis.py uses remap_params.yaml as input for remapping ''')
sys.exit(0)
if opt in("-c", "--config_file"):
config_yaml = arg
yaml_flag = True

if opt == '-o':
print("\n Use command line to input a flattened yaml config file \n")
cmd2yaml_flag = True
config_yaml = 'remap_params.yaml'

if yaml_flag :
config = yaml_to_config(config_yaml)
# Parse the command line arguments from parse_args() capturing the arguments and the rest
command_line_args, extra_args = parse_args()
config_yaml = command_line_args.config_file
flattened_yaml = command_line_args.flattened_yaml

if cmd2yaml_flag :
config = args_to_config(args)
if config_yaml:
config = yaml_to_config(config_yaml)
elif flattened_yaml:
config_yaml = 'remap_params.yaml'
config = args_to_config(extra_args)
else:
raw_config = get_config_from_questionary()
params = remap_params(raw_config)
config = params.config
question_flag = True
config_yaml = 'remap_params.yaml'

if not ( yaml_flag or cmd2yaml_flag) :
raw_config = get_config_from_questionary()
params = remap_params(raw_config)
config = params.config
question_flag = True
config_yaml = 'remap_params.yaml'

print('\n')
print_config(config)

questions = [
{
"type": "confirm",
"name": "Continue",
"message": "Above is the YAML config file, would you like to continue?",
"default": True
},]
},]
answer = questionary.prompt(questions)

if not answer['Continue'] :
print("\nYou answered not to continue, exiting.\n")
sys.exit(0)
if yaml_flag or question_flag : write_cmd(config)
if cmd2yaml_flag or question_flag : config_to_yaml(config, config_yaml)

if config_yaml or question_flag: write_cmd(config)
if flattened_yaml or question_flag: config_to_yaml(config, config_yaml)

# upper air
upper = upperair(params_file=config_yaml)
Expand All @@ -106,5 +116,5 @@ def main(argv):
ana.remap()

if __name__ == '__main__' :
main(sys.argv[1:])
main()

0 comments on commit 026666b

Please sign in to comment.