Skip to content

Commit

Permalink
Fix regrid for (-180,180) lon window
Browse files Browse the repository at this point in the history
  • Loading branch information
briochemc committed Feb 12, 2021
1 parent 3bb0cbe commit ef8d51b
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "OceanGrids"
uuid = "cfe838f4-859f-11e9-2ea1-df7d4e7c3537"
authors = ["Benoit Pasquier <[email protected]>"]
version = "0.3.4"
version = "0.3.5"

[deps]
Interpolations = "a98d9a8b-a2ab-59e6-89dd-64a1c18fca59"
Expand Down
15 changes: 10 additions & 5 deletions src/functions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -511,21 +511,21 @@ Vertical sections
# to be used by interpolation functions.
# These assume that the longitude is in (0,360) and that
# the longitude is stored in the 2nd dimension (lat,lon,depth)
function lonextend(x3D::Array{T,3}) where T
function lonextend(x3D::AbstractArray{T,3}) where T
x3Dext = Array{T,3}(undef, (size(x3D) .+ (0,2,0))...)
x3Dext[:,2:end-1,:] .= x3D
x3Dext[:,1,:] .= x3D[:,end,:]
x3Dext[:,end,:] .= x3D[:,1,:]
return x3Dext
end
function cyclicallon(lon::Vector{T}) where T<:Quantity
function cyclicallon(lon::AbstractVector{T}) where T<:Quantity
lonext = Vector{T}(undef, length(lon) + 2)
lonext[2:end-1] .= lon
lonext[1] = lon[end] - 360°
lonext[end] = lon[1] + 360°
return lonext
end
cyclicallon(lon::Vector) = cyclicallon(convertlon.(lon))
cyclicallon(lon::AbstractVector) = cyclicallon(convertlon.(lon))
# TODO make the function below more generic and be capable of taking in other
# representations than just OceanographyCruises.jl `CruiseTrack`
"""
Expand Down Expand Up @@ -639,7 +639,7 @@ end
Functions to grid/interpolate
=======================================================#

function lonextend(x2D::Array{T,2}) where T
function lonextend(x2D::AbstractArray{T,2}) where T
x2Dext = Array{eltype(x2D),2}(undef, (size(x2D) .+ (0,2))...)
x2Dext[:,2:end-1] .= x2D
x2Dext[:,1] .= x2D[:,end]
Expand All @@ -656,8 +656,13 @@ Returns `x2D` (or `x3D`) interpolated onto `grd`.
function regrid(x2D::AbstractArray{T,2} where T, lat, lon, grd)
# must have lat and lon as metadataarrays?
size(x2D) (length(lat), length(lon)) && error("Dimensions of input and lat/lon don't match")
x2D = lonextend(x2D)
# need to rearrange in case data was in (-180, 180)
lon = convertlon.(lon)
ilon = sortperm(lon)
sort!(lon)
x2D = view(x2D, :, ilon)
knots = convertlat.(lat), cyclicallon(lon)
x2D = lonextend(x2D)
itp = LinearInterpolation(knots, x2D, extrapolation_bc = Flat())
return [itp(y, x) for y in grd.lat, x in grd.lon]
end
Expand Down

2 comments on commit ef8d51b

@briochemc
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/29918

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.3.5 -m "<description of version>" ef8d51bd8b9e9dd4b0ed557e0151419765b9499a
git push origin v0.3.5

Please sign in to comment.