Skip to content
Snippets Groups Projects

Misc

Merged Maximilian Schanner requested to merge misc into master
2 files
+ 52
24
Compare changes
  • Side-by-side
  • Inline
Files
2
+ 24
24
@@ -54,6 +54,26 @@ class ListModelsAction(argparse.Action):
parser.exit()
class ModelAction(argparse.Action):
'''Custom action for parsing the positional argument `model`'''
def __call__(self, parser, namespace, values, option_string=None):
'''If the string passed is not a built-in model it is assumed to a
custom model file name'''
try:
namespace.input = models[values]
except KeyError:
namespace.input = values
namespace.model = 'custom model'
class LongtermAction(argparse._StoreTrueAction):
def __call__(self, parser, namespace, values, option_string=None):
namespace.t_unit = 'ka'
namespace.t_label = r'age [ka]'
def argument_parser():
'''the argument parser'''
# the main parsing
@@ -75,12 +95,14 @@ def argument_parser():
base_parser.add_argument('-o', '--output', metavar='<path/to/output>',
type=str, help='where to store the outputs. if '
f'not given, no output is stored')
base_parser.add_argument('--longterm', action='store_true',
base_parser.add_argument('--longterm', action=LongtermAction,
help=f'this flag is intended for longterm models.'
f' if given, all times will be interpreted as '
f'ages before the year 1950 in ka (kiloyears), '
f'i.e. the epoch 10 refers to the year -8050 '
f'without the flag')
# The default time unit is years AD
base_parser.set_defaults(t_unit='yrs.', t_label='age [yrs.]')
# parser for arguments that are shared between time series commands
series_base_parser = argparse.ArgumentParser(add_help=False)
@@ -210,7 +232,7 @@ def argument_parser():
parser.add_argument('model', type=str, help=f'one of the included models.'
f'use pymagglobal --list-models to get a list of '
f'the included models or pass a <path/to/your_model> '
f'to parse your own model')
f'to parse your own model', action=ModelAction)
return parser
@@ -221,28 +243,6 @@ def main():
parser = argument_parser()
# parse the command line arguments
args = parser.parse_args()
# check whether model and input are consistent
if args.model in models:
args.input = models[args.model]
else:
args.input = args.model
args.model = 'custom model'
"""
if args.input is None and args.model not in models:
raise parser.error(f'Model {args.model} is not include in pymagglobal.'
f' To process your own model, specify an input '
f'file using \n'
f' $ pymagglobal --input <path/to/{args.model}> '
f'... {args.model}')
"""
# get the right time units
if args.longterm:
args.t_unit = 'ka'
args.t_label = r'age [ka]'
else:
args.t_unit = 'yrs.'
args.t_label = r't [yrs.]'
# call the respective function, specified using .set_defaults(func=...)
args.func(args)
if not args.no_show:
Loading