Skip to content

Commit

Permalink
NCD updates
Browse files Browse the repository at this point in the history
  • Loading branch information
juliasloan25 committed Dec 5, 2023
1 parent a266892 commit ebb1b43
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 15 deletions.
10 changes: 5 additions & 5 deletions experiments/AMIP/modular/user_io/ncep_visualizer.jl
Original file line number Diff line number Diff line change
Expand Up @@ -87,16 +87,16 @@ function download_read_nc(data_source::NCEPMonthlyDataSource, https::String, nce
local_file = joinpath(data_source.tmp_dir, ncep_vname * ".nc")
Downloads.download(https, local_file)
NCDataset(local_file) do ds
t_i = findall(x -> Dates.yearmonth(x) == Dates.yearmonth(data_source.month_date[1]), ds["time"][:]) # time index of month in file
d_i = length(size(ds[ncep_vname][:])) # index of time in the dimension list
lev = "level" in keys(ds) ? ds["level"][:] : [Float64(-999)]
data = dropdims(selectdim(ds[ncep_vname][:], d_i, t_i), dims = d_i)
t_i = findall(x -> Dates.yearmonth(x) == Dates.yearmonth(data_source.month_date[1]), Array(ds["time"])) # time index of month in file
d_i = length(size(Array(ds[ncep_vname]))) # index of time in the dimension list
lev = "level" in keys(ds) ? Array(ds["level"]) : [Float64(-999)]
data = dropdims(selectdim(Array(ds[ncep_vname]), d_i, t_i), dims = d_i)
if length(size(data)) == 3
data .= data[:, end:-1:1, end:-1:1]
else
data .= data[:, end:-1:1]
end
coords = (; lon = ds["lon"][:], lat = ds["lat"][:][end:-1:1], lev = lev[end:-1:1])
coords = (; lon = Array(ds["lon"]), lat = Array(ds["lat"])[end:-1:1], lev = lev[end:-1:1])
(Array(data), coords)
end
end
Expand Down
14 changes: 7 additions & 7 deletions src/Regridder.jl
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ determined by the space type.
"""
function get_coords(ds, ::Spaces.ExtrudedFiniteDifferenceSpace)
data_dates = get_time(ds)
z = ds["z"][:]
z = Array(ds["z"])
return (data_dates, z)
end
function get_coords(ds, ::Spaces.SpectralElementSpace2D)
Expand All @@ -278,9 +278,9 @@ Extracts the time information from a NetCDF file `ds`.
"""
function get_time(ds)
if "time" in ds
data_dates = Dates.DateTime.(ds["time"][:])
data_dates = Dates.DateTime.(Array(ds["time"]))
elseif "date" in ds
data_dates = TimeManager.strdate_to_datetime.(string.(Int.(ds["date"][:])))
data_dates = TimeManager.strdate_to_datetime.(string.(Int.(Array(ds["date"]))))
else
@warn "No dates available in input data file"
data_dates = [Dates.DateTime(0)]
Expand Down Expand Up @@ -585,10 +585,10 @@ Extract data and coordinates from `datafile_latlon`.
"""
function read_remapped_field(name::Symbol, datafile_latlon::String, lev_name = "z")
out = NCDataset(datafile_latlon, "r") do nc
lon = nc["lon"][:]
lat = nc["lat"][:]
lev = lev_name in keys(nc) ? nc[lev_name][:] : Float64(-999)
var = nc[name][:]
lon = Array(nc["lon"])
lat = Array(nc["lat"])
lev = lev_name in keys(nc) ? Array(nc[lev_name]) : Float64(-999)
var = Array(nc[name])
coords = (; lon = lon, lat = lat, lev = lev)

(var, coords)
Expand Down
2 changes: 1 addition & 1 deletion test/TestHelper.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ using NCDatasets
export create_space, gen_ncdata

"""
create_space(FT; comms_ctx = ClimaComms.SingletonCommsContext(),
create_space(FT; comms_ctx = ClimaComms.SingletonCommsContext(),
R = FT(6371e3), ne = 4, polynomial_degree = 3, nz = 1)
Initialize a space on a sphere with the given parameters.
Expand Down
4 changes: 2 additions & 2 deletions test/regridder_tests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ for FT in (Float32, Float64)
T_rll, _ = Regridder.cgll2latlonz(T_cgll)

# check consistency across z-levels
@test T_rll[:, :, 1] == T_rll[:, :, 2]
@test Array(T_rll)[:, :, 1] == Array(T_rll)[:, :, 2]

# check consistency of CGLL remapped data with original data
@test all(isapprox.(extrema(data), extrema(parent(T_cgll)), atol = 1e-2))
Expand All @@ -304,7 +304,7 @@ for FT in (Float32, Float64)

# visual inspection
# Plots.plot(T_cgll) # using ClimaCorePlots
# Plots.contourf(T_rll[:,:,1])
# Plots.contourf(Array(T_rll)[:,1])

# Delete testing directory and files
rm(REGRID_DIR; recursive = true, force = true)
Expand Down

0 comments on commit ebb1b43

Please sign in to comment.