Last active
May 3, 2020 21:36
-
-
Save meggart/a5788d9d0e79c63b35b46bb337bd050c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Interpolations | |
import Dierckx | |
function interpolate_data(tsteps_old, tsteps_new, y, options) | |
# Apply interpolation according to the options | |
y_interp = if options.interpolation_method == "Dierckx" | |
if options.boundary_condition == "flat" | |
s = Dierckx.Spline1D(tsteps_old,y,bc="nearest") | |
s(tsteps_new) | |
elseif options.boundary_condition == "extrapolate" | |
s = Dierckx.Spline1D(tsteps_old,y,bc="extrapolate") | |
s(tsteps_new) | |
end | |
elseif options.interpolation_method == "Interpolations" | |
if options.boundary_condition == "flat" | |
#construct the interpolation object | |
itp = Interpolations.interpolate(y, Interpolations.BSpline(Cubic())) | |
itp = extrapolate(itp, Flat()) | |
#And evaluate it at daily time steps | |
itp(tsteps_new) | |
elseif options.boundary_condition == "extrapolate" | |
#construct the interpolations object | |
itp = Interpolations.interpolate(y, Interpolations.BSpline(Cubic())) | |
itp = extrapolate(itp, Line()) | |
#and evaluate it at daily time steps | |
itp(tsteps_new) | |
end | |
elseif options.interpolation_method == "Nearest" | |
#Loop over target time steps | |
map(tsteps_new) do itime | |
#Find the nearest source time step | |
d, ix = findmin(abs.(itime.-tsteps_old)) | |
tsteps_old[ix] # return value at this index | |
end | |
end | |
return y_interp | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment