Skip to content

Commit

Permalink
move equal area code to the new file
Browse files Browse the repository at this point in the history
  • Loading branch information
Datseris committed Nov 19, 2020
1 parent e7ad672 commit b25a44d
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 39 deletions.
44 changes: 5 additions & 39 deletions src/core/nc_io.jl
Original file line number Diff line number Diff line change
Expand Up @@ -90,50 +90,16 @@ end
# must be sure to load the correct ranges of dimensions as well though!

function ClimArray(ds::NCDatasets.AbstractDataset, var::String, name = var; eqarea = false)
eqarea && return ClimArray_eqarea(ds, var, name)
svar = string(var)
cfvar = ds[svar]
attrib = Dict(cfvar.attrib)
A = cfvar |> Array
if eqarea
# TODO: This code has not yet been generalized to arbitrary dimensions
if haskey(ds, "ncells") # this is the equal area grid, so we make a Coord dimension
lon = ds["lon"] |> Array .|> wrap_lon
lat = ds["lat"] |> Array
time = ds["time"] |> Array
lonlat = [SVector(lon[i], lat[i]) for i in 1:length(lon)]
# here we sort lonlat and A in ascending latitude order,
# because the CDO output has reverse or even totally unsorted order
si = sortperm(lonlat, by = reverse)
data = ClimArray(A[si, :], (Coord(lonlat[si]), Time(time));
attrib = attrib, name = svar)
elseif haskey(ds, "reduced_points")
# TODO: I've noticed that this converts integer dimension (like pressure)
# into Float64, but I'm not sure why...
alldims = [NCDatasets.dimnames(cfvar)...]
@assert "rgrid" alldims
i = findfirst(x -> x == "rgrid", alldims)
remainingdims = deleteat!(copy(alldims), i)
actualdims = Any[create_dims(ds, remainingdims)...]

lonlat = reduced_grid_to_points(ds["lat"], ds["reduced_points"])
si = sortperm(lonlat, by = reverse)
coords = Coord(lonlat; metadata = Dict("grid" => "Gaussian equal area."))

insert!(actualdims, i, coords)

X = ClimArray(A, Tuple(actualdims))
X = X[Coord(si)]
return ClimArray(X; name = Symbol(name), attrib)
else
error("Don't know how to handle this equal area grid!")
end
else # standard variables
dnames = Tuple(NCDatasets.dimnames(cfvar))
data = ClimArray(A, create_dims(ds, dnames); name = Symbol(name), attrib = attrib)
end
if !any(ismissing, data)
data = nomissing(data)
dnames = Tuple(NCDatasets.dimnames(cfvar))
if !any(ismissing, A)
A = nomissing(A)
end
data = ClimArray(A, create_dims(ds, dnames); name = Symbol(name), attrib = attrib)
return data
end

Expand Down
50 changes: 50 additions & 0 deletions src/physical_dimensions/spatial_equalarea.jl
Original file line number Diff line number Diff line change
@@ -1,3 +1,53 @@
#########################################################################
# Loading equal area
#########################################################################

function ClimArray_eqarea(ds::NCDatasets.AbstractDataset, var::String, name = var)
svar = string(var)
cfvar = ds[svar]
attrib = Dict(cfvar.attrib)
A = cfvar |> Array
# TODO: This code has not yet been generalized to arbitrary dimensions
if haskey(ds, "ncells") # this is the equal area grid, so we make a Coord dimension
lon = ds["lon"] |> Array .|> wrap_lon
lat = ds["lat"] |> Array
time = ds["time"] |> Array
lonlat = [SVector(lon[i], lat[i]) for i in 1:length(lon)]
# here we sort lonlat and A in ascending latitude order,
# because the CDO output has reverse or even totally unsorted order
si = sortperm(lonlat, by = reverse)
data = ClimArray(A[si, :], (Coord(lonlat[si]), Time(time));
attrib = attrib, name = svar)
elseif haskey(ds, "reduced_points")
# TODO: I've noticed that this converts integer dimension (like pressure)
# into Float64, but I'm not sure why...
alldims = [NCDatasets.dimnames(cfvar)...]
@assert "rgrid" alldims
i = findfirst(x -> x == "rgrid", alldims)
remainingdims = deleteat!(copy(alldims), i)
actualdims = Any[create_dims(ds, remainingdims)...]

lonlat = reduced_grid_to_points(ds["lat"], ds["reduced_points"])
si = sortperm(lonlat, by = reverse)
coords = Coord(lonlat; metadata = Dict("grid" => "Gaussian equal area."))

insert!(actualdims, i, coords)

X = ClimArray(A, Tuple(actualdims))
X = X[Coord(si)]
return ClimArray(X; name = Symbol(name), attrib)
else
error("Don't know how to handle this equal area grid!")
end
if !any(ismissing, data)
data = nomissing(data)
end
return data
end

#########################################################################
# Spatial functions
#########################################################################
function spatialidxs(::EqArea, A)
return ((Coord(i),) for i in 1:size(A, Coord))
end
Expand Down

0 comments on commit b25a44d

Please sign in to comment.