Skip to content

Instantly share code, notes, and snippets.

@meggart
Last active May 3, 2020 21:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save meggart/a5788d9d0e79c63b35b46bb337bd050c to your computer and use it in GitHub Desktop.
Save meggart/a5788d9d0e79c63b35b46bb337bd050c to your computer and use it in GitHub Desktop.
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