Commit 25b70518 authored by Maximilian Schanner's avatar Maximilian Schanner
Browse files

renamed pymagglobal (#5) and commands; also removed the --input argument

parent 7e1d55d5
......@@ -27,8 +27,8 @@
__all__ = ['master_curve', 'dipole_series', 'file2splines', 'field',
'models', 'coefficients', 'utils']
from .pymagglobal import master_curve, dipole_series, file2splines, \
from pymagglobal.core import master_curve, dipole_series, file2splines, \
field, coefficients, models
from . import utils
from pymagglobal import utils
__version__ = '0.0.2'
......@@ -29,8 +29,8 @@
import sys
import argparse
from . import commands
from .pymagglobal import models
from pymagglobal import _commands
from pymagglobal.core import models
class ListModelsAction(argparse.Action):
......@@ -59,12 +59,12 @@ def argument_parser():
parser.add_argument('-h', '--help', action='help',
default=argparse.SUPPRESS,
help=f'show this message and exit. each of the '
f'commands above has its own --help, so use e.g. '
f'_commands above has its own --help, so use e.g. '
f'master --help to get information on how to create a '
f'master curve')
parser.add_argument('--list-models', help='list available models and exit',
action=ListModelsAction)
# parser for arguments that are shared between all commands
# parser for arguments that are shared between all _commands
base_parser = argparse.ArgumentParser(add_help=False)
base_parser.add_argument('--no-show', action='store_true',
help='do not show the results')
......@@ -77,7 +77,7 @@ def argument_parser():
f'ages before the year 1950 in ka (kiloyears), '
f'i.e. the epoch 10 refers to the year -8050 '
f'without the flag')
# parser for arguments that are shared between time series commands
# parser for arguments that are shared between time series _commands
series_base_parser = argparse.ArgumentParser(add_help=False)
res_default = 401
......@@ -101,7 +101,7 @@ def argument_parser():
f'not given, use the latest year '
f'possible')
# the subparser for commands
subparsers = parser.add_subparsers(dest='command')#, required=True)
subparsers = parser.add_subparsers(dest='command', required=True)
# parser for the master curve handling
# XXX Prototyping with epilogues
mst_epilog = '''Example of use: :command:`pymagglobal master 12 12 gufm1`
......@@ -113,7 +113,7 @@ def argument_parser():
series_base_parser],
epilog=mst_epilog)
mst_parser.set_defaults(func=commands.master_curve)
mst_parser.set_defaults(func=_commands.master_curve)
mst_parser.add_argument('lat', type=float,
help=f'the latitude at which to create the master '
f'curve')
......@@ -132,13 +132,13 @@ def argument_parser():
f'of the dipole moment',
parents=[base_parser,
series_base_parser])
dip_parser.set_defaults(func=commands.dipole_series)
dip_parser.set_defaults(func=_commands.dipole_series)
# parser for coefficients
cff_parser = subparsers.add_parser('coefficients', help=f'output '
f'coefficients for a specific epoch',
parents=[base_parser])
cff_parser.set_defaults(func=commands.coefficients)
cff_parser.set_defaults(func=_commands.coefficients)
cff_parser.add_argument('epoch', type=float, help='the epoch for which '
'the coeffients are returned')
......@@ -150,7 +150,7 @@ def argument_parser():
f' over the globe, so it may appear '
f'unconventional',
parents=[base_parser])
map_parser.set_defaults(func=commands.maps)
map_parser.set_defaults(func=_commands.maps)
map_parser.add_argument('--type', choices=['dif', 'nez'], type=str,
help=f'the type of the resulting field. '
f'may be either dif for declination, inclination '
......@@ -167,28 +167,36 @@ def argument_parser():
default=res_default)
# the model to be used
parser.add_argument('model', type=str, help='a model')
"""
parser.add_argument('-i', '--input', metavar='<path/to/input>', type=str,
help=f'input path to a model file. may be used to '
f'evaluate your own model. if given, this path will '
f'be used instead over the included files')
"""
return parser
def main():
'''the main command of pymagglobal. it handles command line arguments and
calls the respective functions from the commands module.
calls the respective functions from the _commands module.
'''
parser = argument_parser()
# parse the command line arguments
args = parser.parse_args()
# check whether model and input are consistent
if args.input is None and args.model in models:
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' $ pycals --input <path/to/{args.model}> '
f'... {args.model}')
"""
# get the right time units
if args.longterm:
args.t_unit = 'ka'
......
# This module contains the commands that are used by the main module of
# pymagglobal.
# core.
#
# Copyright (C) 2020 Helmholtz Centre Potsdam GFZ,
# German Research Centre for Geosciences, Potsdam, Germany
......@@ -7,7 +7,7 @@
# Cite as:
# TODO
#
# This file is part of pymagglobal.
# This file is part of core.
#
# pymagglobal is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
......@@ -20,7 +20,7 @@
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with pymagglobal. If not, see <https://www.gnu.org/licenses/>.
# along with core. If not, see <https://www.gnu.org/licenses/>.
'''Just a bunch of functions ...
......@@ -35,8 +35,8 @@ from matplotlib import pyplot as plt, colors, cm
from pyfield import REARTH
from . import pymagglobal
from . import utils
from pymagglobal import core
from pymagglobal import utils
class OutOfRangeWarning(Warning):
......@@ -126,10 +126,10 @@ def master_curve(args):
# get an array of times
times = args2times(args)
# get the splines from the input file
splines = pymagglobal.file2splines(args.input)
splines = core.file2splines(args.input)
# create a master curve using the core function
curves = pymagglobal.master_curve(times, (args.lat, args.lon), splines,
ser_type=args.type)
curves = core.master_curve(times, (args.lat, args.lon), splines,
ser_type=args.type)
# output formats for dif and nez components
fmts = {'dif': ('%.2f', '%2.6f', '%2.6f', '%1.7e'),
'nez': ('%.2f', '%1.7e', '%1.7e', '%1.7e')}
......@@ -148,7 +148,7 @@ def master_curve(args):
fmt=fmts[args.type],
delimiter=',',
header=f'Master curves at ({args.lat}°, {args.lon}°) '
f'for the model {args.model}\n'
f'for {args.model}\n'
f'{args.t_label}'
f'{utils.labels[args.type][0]},'
f'{utils.labels[args.type][1]},'
......@@ -158,7 +158,7 @@ def master_curve(args):
if not args.no_show:
fig = plt.figure(figsize=(10, 7))
fig.suptitle(f'Master curves at ({args.lat}°, {args.lon}°) '
f'for the model {args.model}')
f'for {args.model}')
axs = np.empty(3, dtype=object)
axs[0] = fig.add_subplot(221)
axs[1] = fig.add_subplot(222)
......@@ -189,9 +189,9 @@ def dipole_series(args):
# get an array of times
times = args2times(args)
# get the splines from the input file
splines = pymagglobal.file2splines(args.input)
splines = core.file2splines(args.input)
# create the dipole-moment time series for the given model
dip_ser = pymagglobal.dipole_series(times, splines)
dip_ser = core.dipole_series(times, splines)
# if the --longterm flag is set, translate the years accordingly
if args.longterm:
times = yr2lt(times)
......@@ -203,7 +203,7 @@ def dipole_series(args):
dip_ser]).T,
fmt=('%.2f', '%1.7e'),
delimiter=',',
header=f'Dipole moment series for for the model '
header=f'Dipole moment series for '
f'{args.model}\n'
f'{args.t_label},m [m^2 A]')
......@@ -212,7 +212,7 @@ def dipole_series(args):
fig = plt.figure(figsize=(10, 7))
ax = fig.add_subplot(111)
ax.plot(times, dip_ser)
ax.set_title(f'Dipole moment series for for the model {args.model}')
ax.set_title(f'Dipole moment series for {args.model}')
ax.set_ylabel(r'$m$ [m$^2$ A]')
if args.longterm:
ax.invert_xaxis()
......@@ -235,22 +235,22 @@ def coefficients(args):
# check whether the epoch is in the range of the model
epoch = valid_epoch(args)
# get the splines from the input file
splines = pymagglobal.file2splines(args.input)
splines = core.file2splines(args.input)
# evaluate the splines at the epoch
ls, ms, coeffs = pymagglobal.coefficients(epoch, splines)
ls, ms, coeffs = core.coefficients(epoch, splines)
# if an output is specified, save the result to text
if args.output is not None:
np.savetxt(args.output,
np.array([ls, ms, coeffs]).T,
fmt=('%.d', '%d', '%1.7e'),
delimiter=',',
header=f'Coefficients for the model {args.model} at epoch '
header=f'Coefficients for {args.model} at epoch '
f'{args.epoch} {args.t_unit}\n'
f'l,m,g_l^m')
# if no output is given and the --no-show flag is not set, print the
# coefficients to the command line
elif not args.no_show:
print(f'Coefficients for the model {args.model} at epoch '
print(f'Coefficients for {args.model} at epoch '
f'{args.epoch} {args.t_unit}')
right = len(str(ms[-1]))
print(f'l\t' + ' '*(right-1) + 'm\t g_l^m')
......@@ -279,9 +279,9 @@ def maps(args):
z_at[2] = REARTH
z_at[3] = epoch
# get the splines from the input file
splines = pymagglobal.file2splines(args.input)
splines = core.file2splines(args.input)
# use the core function to get the field
field = pymagglobal.field(z_at, splines)
field = core.field(z_at, splines)
# convert the field if necessary
if args.type == 'dif':
field = np.array(utils.nez2dif(*field))
......@@ -299,7 +299,7 @@ def maps(args):
field[2]]).T,
fmt=fmts[args.type],
delimiter=',',
header=f'Field map for the model {args.model} at epoch '
header=f'Field map for {args.model} at epoch '
f'{args.epoch} {args.t_unit}\n'
f'lat,lon,'
f'{utils.labels[args.type][0]},'
......@@ -334,7 +334,7 @@ def maps(args):
fig, axs = plt.subplots(1, 3, figsize=(13, 3),
subplot_kw={'projection': proj})
fig.suptitle(f'Field maps for the model {args.model} at epoch '
fig.suptitle(f'Field maps for {args.model} at epoch '
f'{args.epoch} {args.t_unit}')
for it in range(3):
......
......@@ -35,7 +35,7 @@ from scipy.interpolate import BSpline
from pyfield import CRUSpline as Kernel, REARTH, i2lm_l, i2lm_m, lmax2N
from .utils import nez2dif
from pymagglobal.utils import nez2dif
# generate a dictionary of models from models the models folder
here = os.path.abspath(os.path.dirname(__file__))
......
......@@ -3,7 +3,7 @@ import doctest
from pathlib import PurePath
from pymagglobal import utils
from pymagglobal import commands
from pymagglobal import _commands
def test_suite():
'''To be called in setup.py
......@@ -13,6 +13,6 @@ def test_suite():
suite = unittest.TestLoader().discover(tests_path)
# Add doc-test manually
suite.addTest(doctest.DocTestSuite(utils))
suite.addTest(doctest.DocTestSuite(commands))
suite.addTest(doctest.DocTestSuite(_commands))
return suite
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment