Commit 3ed17c29 authored by Maximilian Schanner's avatar Maximilian Schanner
Browse files

Functions now take models as inputs as well as splines.

parent a894e973
......@@ -139,9 +139,10 @@ def master_curve(args):
'''
# get an array of times
times = args2times(args)
# create a master curve using the core function
curves = core.master_curve(times, (args.lat, args.lon), args.model.splines,
ser_type=args.type)
# create a master curve using the core function, check is performed
# in args2times
curves = core.master_curve(times, (args.lat, args.lon), args.model,
ser_type=args.type, check=False)
# output formats for dif and nez components
fmts = {'dif': ('%.2f', '%2.6f', '%2.6f', '%1.7e'),
'nez': ('%.2f', '%1.7e', '%1.7e', '%1.7e')}
......@@ -207,8 +208,9 @@ def dipole_series(args):
'''
# get an array of times
times = args2times(args)
# create the dipole-moment time series for the given model
dip_ser = core.dipole_series(times, args.model.splines)
# create the dipole-moment time series for the given model, check is
# performed in args2times
dip_ser = core.dipole_series(times, args.model, check=False)
# if the --longterm flag is set, translate the years accordingly
if args.longterm:
times = yr2lt(times)
......@@ -258,8 +260,8 @@ def coefficient_series(args):
'''
inds = args.model.valid_degrees_orders(args.degree, args.order)
times = args2times(args)
# evaluate the splines at the epoch
_, _, coeffs = core.coefficients(times, args.model.splines)
# evaluate the splines at the epoch, check is performed in args2times
_, _, coeffs = core.coefficients(times, args.model, check=False)
if args.longterm:
times = yr2lt(times)
......@@ -313,10 +315,8 @@ def coefficients_epoch(args):
return a matplotlib.figure.Figure object, containing a plot of the
coefficients.
'''
# check whether the epoch is in the range of the model
epoch = args.model.valid_epoch(args.epoch)
# evaluate the splines at the epoch
ls, ms, coeffs = core.coefficients(epoch, args.model.splines)
ls, ms, coeffs = core.coefficients(args.epoch, args.model)
# if an output is specified, save the result to text
if args.output is not None:
np.savetxt(args.output,
......@@ -385,12 +385,10 @@ def maps(args):
return a matplotlib.figure.Figure object, containing a plot of the
field map.
'''
# check whether the epoch is in the range of the model
epoch = args.model.valid_epoch(args.epoch)
# set up the points as expected by dsh_basis
z_at = utils.get_z_at(args.res, t=epoch)
z_at = utils.get_z_at(args.res, t=args.epoch)
# use the core function to get the field
field = core.field(z_at, args.model.splines)
field = core.field(z_at, args.model)
# convert the field if necessary
if args.type == 'dif':
field = np.array(utils.nez2dif(*field))
......
......@@ -109,8 +109,7 @@ class Model(object):
3)
def valid_epoch(self, epoch):
'''Check whether the epoch given via the command line is in the
range of the model.
'''Check whether the given epoch is in the range of the model.
Parameters
----------
......@@ -130,8 +129,8 @@ class Model(object):
return epoch
def valid_degrees_orders(self, degrees, orders):
'''Check whether the degrees and orders given via the command line are
valid for the model and calculate the corresponding indices.
'''Check whether the given degrees and orders are valid for the model
and calculate the corresponding indices.
Parameters
----------
......@@ -162,7 +161,7 @@ class Model(object):
return inds
def master_curve(times, loc, splines, ser_type='dif'):
def master_curve(times, loc, splines, ser_type='dif', check=True):
'''Create master curves from a splines object.
Parameters
......@@ -171,11 +170,13 @@ def master_curve(times, loc, splines, ser_type='dif'):
The times for which to create the master curve.
loc : tuple
lat, lon tuple of the location at which to create the master curve.
splines : scipy.interpolate.BSpline
The splines specifying the model.
splines : scipy.interpolate.BSpline or Model
An instance of Model or splines specifying the model.
ser_type : {'dif', 'nez'}
The type of the master curves. May be either 'dif' (default) for
declination, inclination and intensity or 'nez' for north, east, down.
check : bool, optional
If a Model is given, check the input validity.
Returns
-------
......@@ -189,6 +190,11 @@ def master_curve(times, loc, splines, ser_type='dif'):
The third component master curve. Either intensity or down,
depending on the ser_type kwarg.
'''
if isinstance(splines, Model):
if check:
for it in np.atleast_1d(times):
splines.valid_epoch(it)
splines = splines.splines
try:
n_plt = len(times)
except TypeError:
......@@ -211,7 +217,7 @@ def master_curve(times, loc, splines, ser_type='dif'):
f"'nez' are supported.")
def coefficients(epoch, splines):
def coefficients(epoch, splines, check=True):
'''Evaluate splines at an epoch and return the coefficients, together with
the appropriate degrees and orders.
......@@ -219,8 +225,10 @@ def coefficients(epoch, splines):
----------
epoch : float
The epoch at which to return the coefficients, in years.
splines : scipy.interpolate.BSpline
The splines specifying the model.
splines : scipy.interpolate.BSpline or Model
An instance of Model or splines specifying the model.
check : bool, optional
If a Model is given, check the input validity.
Returns
-------
......@@ -232,6 +240,11 @@ def coefficients(epoch, splines):
Coefficients of the model for the given epoch, corresponding to the
lists of degrees and orders.
'''
if isinstance(splines, Model):
if check:
for it in np.atleast_1d(epoch):
splines.valid_epoch(epoch)
splines = splines.splines
coeffs = splines(epoch)
# set up degrees and orders for output purposes
ls = [i2lm_l(it) for it in range(splines.c.shape[1])]
......@@ -240,21 +253,28 @@ def coefficients(epoch, splines):
return ls, ms, coeffs
def dipole_series(times, splines):
def dipole_series(times, splines, check=True):
'''Create a dipole-moment time series from a splines object.
Parameters
----------
times : array-like
The times for which to create the master curve.
splines : scipy.interpolate.BSpline
The splines specifying the model.
splines : scipy.interpolate.BSpline or Model
An instance of Model or splines specifying the model.
check : bool, optional
If a Model is given, check the input validity.
Returns
-------
ndarray
An array containing the dipole-moment time series.
'''
if isinstance(splines, Model):
if check:
for it in np.atleast_1d(times):
splines.valid_epoch(it)
splines = splines.splines
# evaluate the splines
coeffs = splines(times).T
# calculate the dipole moment
......@@ -290,7 +310,7 @@ def file2splines(fname):
return c_splines
def field(z_at, splines):
def field(z_at, splines, check=True):
'''Evaluate coefficient splines at locations z_at and return the field in
north, east, down components.
......@@ -307,8 +327,10 @@ def field(z_at, splines):
You can use pymagglobal.utils.get_z_at to generate an array of input
points.
splines : scipy.interpolate.BSpline
The splines specifying the model.
splines : scipy.interpolate.BSpline or Model
An instance of Model or splines specifying the model.
check : bool, optional
If a Model is given, check the input validity.
Returns
-------
......@@ -316,6 +338,11 @@ def field(z_at, splines):
An array containing the north, east and down component at the input
locations.
'''
if isinstance(splines, Model):
if check:
for it in np.atleast_1d(z_at[3]):
splines.valid_epoch(it)
splines = splines.splines
# count the number of inputs
n_pts = z_at.shape[1]
# count the number of coefficients
......
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