Skip to content

Commit 6195e5e

Browse files
committed
allow longitude wrapping to work with Coord dimension
1 parent ca56885 commit 6195e5e

File tree

2 files changed

+18
-5
lines changed

2 files changed

+18
-5
lines changed

src/io/netcdf_read.jl

-1
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,6 @@ function ncread_unstructured(
278278
end
279279

280280
function load_coordinate_points(ds)
281-
@show keys(ds)
282281
if haskey(ds, "reduced_points")
283282
lonlat = reduced_grid_to_points(ds["lat"], ds["reduced_points"])
284283
original_grid_dim = "rgrid" # Specific to CDO Gaussian grid

src/physical_dimensions/spatial.jl

+18-4
Original file line numberDiff line numberDiff line change
@@ -74,21 +74,35 @@ of `X` with shift `l`. If `wrap = true` the longitudes are wrapped to (-180, 180
7474
using the modulo operation.
7575
7676
If `l` is not given, it is as much as necessary so that all longitudes > 180 are
77-
wrapped.
77+
circshifted (and also wrapped if `wrap`).
78+
79+
If `X` has a `CoordinateSpace`, then no circshift is happening (the lon-lat
80+
are one dimension), but the longitude is still shifted.
7881
"""
7982
function longitude_circshift(X::ClimArray, l = nothing; wrap = true)
80-
if isnothing(l); l = count((180), dims(X, Lon).val); end
81-
isnothing(l) && return X
83+
hasdim(X, Coord) && return longitude_circshift_coord(X, wrap)
84+
if isnothing(l); l = count((180), dims(X, Lon)); end
85+
l == 0 && return X
8286
shifts = map(d -> d isa Lon ? l : 0, dims(X))
8387
shifted_data = circshift(X.data, shifts)
84-
shifted_lon = circshift(dims(X, Lon).val, l)
88+
shifted_lon = circshift(gnv(dims(X, Lon)), l)
8589
if wrap; shifted_lon = wrap_lon.(shifted_lon); end
8690
shifted_lon = vector2range(shifted_lon)
8791
shifted_dim = Lon(shifted_lon; metadata = DimensionalData.metadata(dims(X, Lon)))
8892
new_dims = map(d -> d isa Lon ? shifted_dim : d, dims(X))
8993
return ClimArray(shifted_data, new_dims; name = X.name, attrib = X.attrib)
9094
end
9195

96+
# this is just longitude wrapping
97+
function longitude_circshift_coord(X, wrap = true)
98+
!wrap && return X
99+
coords = dims(X, Coord)
100+
lons = getindex.(coords, 1)
101+
wlons = wrap_lon.(lons)
102+
newcoords = [SVector(wlons[i], coords[i][2]) for i in eachindex(coords)]
103+
return DimensionalData.set(X, Coord => newcoords)
104+
end
105+
92106
#########################################################################
93107
# averaging functions over space or time
94108
#########################################################################

0 commit comments

Comments
 (0)