tadd bilinear interpolation scheme for Arakawa A grids - Granular.jl - Julia pa… | |
git clone git://src.adamsgaard.dk/Granular.jl | |
Log | |
Files | |
Refs | |
README | |
LICENSE | |
--- | |
commit 809cebdad423f7a7109fa9139f871c595df1ea16 | |
parent 23b5df397b0b3d2d7619da5e4672675bc3474e90 | |
Author: Anders Damsgaard <[email protected]> | |
Date: Fri, 21 Apr 2017 17:20:25 -0400 | |
add bilinear interpolation scheme for Arakawa A grids | |
Diffstat: | |
M src/grid.jl | 31 +++++++++++++++++++++++++++++… | |
1 file changed, 31 insertions(+), 0 deletions(-) | |
--- | |
diff --git a/src/grid.jl b/src/grid.jl | |
t@@ -27,3 +27,34 @@ function fitWorldSize(margin::vector = zeros(3)) | |
g_world_size[:] = maxpos[:] + margin[:] | |
end | |
+""" | |
+Use bilinear interpolation to interpolate a staggered grid to an arbitrary | |
+position in a cell. Assumes north-east convention, i.e. (i,j) is located at t… | |
+north-east corner. | |
+ | |
+# Arguments | |
+* `field::Array{Float64, 4}`: a scalar field to interpolate from | |
+* `xi::float`: relative x position in cell [-], must be in `[0., 1.]` | |
+* `yj::float`: relative y position in cell [-], must be in `[0., 1.]` | |
+* `i::Int`: i-index of cell containing point | |
+* `j::Int`: j-index of cell containing point | |
+* `grid_type::String="Arakawa A"`: grid system for `field` | |
+""" | |
+function bilinearInterpolation(field::Array{Float64, 4}, | |
+ xi::float, | |
+ yj::float, | |
+ i::Int, | |
+ j::Int; | |
+ grid_type::String="Arakawa A") | |
+ | |
+ if xi < 0. || xi > 1. || yj < 0. || yj > 1. | |
+ error("relative coordinates outside bounds ($(xi), $(yj))") | |
+ end | |
+ | |
+ if grid_type == "Arakawa A" | |
+ return (field[i,j]*xi + field[i-1,j]*(1. - xi))*yi + | |
+ (field[i,j-1]*xi + field[i-1,j-1]*(1. - xi))*(1. - yi) | |
+ else | |
+ error("grid type not understood.") | |
+ end | |
+end |