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