Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tr/minor tempest regridder changes #140

Merged
merged 3 commits into from
Dec 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 16 additions & 36 deletions ext/TempestRegridderExt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,10 @@ struct TempestRegridder{
SPACE <: ClimaCore.Spaces.AbstractSpace,
STR1 <: AbstractString,
STR2 <: AbstractString,
STR3 <: AbstractString,
} <: Regridders.AbstractRegridder
target_space::SPACE
regrid_dir::STR1
outfile_root::STR2
varname::STR3
varname::STR2
mono::Bool
end

Expand Down Expand Up @@ -75,7 +73,6 @@ function Regridders.TempestRegridder(
)
space = target_space
comms_ctx = ClimaComms.context(space)
outfile_root = varname

if ClimaComms.iamroot(comms_ctx)
@info "Saving TempestRegrid files to $regrid_dir"
Expand All @@ -88,8 +85,7 @@ function Regridders.TempestRegridder(
regrid_dir,
input_file,
varnames,
target_space,
outfile_root;
target_space;
mono,
)
end
Expand All @@ -100,12 +96,10 @@ function Regridders.TempestRegridder(
return TempestRegridder{
typeof(target_space),
typeof(regrid_dir),
typeof(outfile_root),
typeof(varname),
}(
target_space,
regrid_dir,
outfile_root,
varname,
mono,
)
Expand All @@ -119,7 +113,6 @@ Return the field associated to the `regridder` at the given `date`.
function Regridders.regrid(regridder::TempestRegridder, date::Dates.DateTime)
return read_from_hdf5(
regridder.regrid_dir,
regridder.outfile_root,
date,
regridder.varname,
regridder.target_space,
Expand Down Expand Up @@ -190,7 +183,7 @@ function swap_space(field, new_space)
end

"""
read_from_hdf5(REGIRD_DIR, hd_outfile_root, time, varname,
read_from_hdf5(REGIRD_DIR, time, varname,
space)

Read in a variable `varname` from an HDF5 file onto the provided space.
Expand All @@ -201,24 +194,20 @@ Code taken from ClimaCoupler.Regridder.

# Arguments
- `REGRID_DIR`: [String] directory to save output files in.
- `hd_outfile_root`: [String] root of the output file name.
- `time`: [Dates.DateTime] the timestamp of the data being written.
- `varname`: [String] variable name of data.
- `space`: [ClimaCore.Spaces.AbstractSpace] to read the HDF5 file onto.
# Returns
- Field or FieldVector
"""
function read_from_hdf5(REGRID_DIR, hd_outfile_root, time, varname, space)
function read_from_hdf5(REGRID_DIR, time, varname, space)
comms_ctx = ClimaComms.context(space)
# Include time component in HDF5 reader name if it's a valid date
if !(time == Dates.DateTime(0))
hdfreader_path = joinpath(
REGRID_DIR,
hd_outfile_root * "_" * varname * "_" * string(time) * ".hdf5",
)
else
hdfreader_path =
joinpath(REGRID_DIR, hd_outfile_root * "_" * varname * ".hdf5")
joinpath(REGRID_DIR, varname * "_" * string(time) * ".hdf5")
else
hdfreader_path = joinpath(REGRID_DIR, varname * ".hdf5")
end
hdfreader = ClimaCore.InputOutput.HDF5Reader(hdfreader_path, comms_ctx)

Expand All @@ -238,7 +227,7 @@ function read_from_hdf5(REGRID_DIR, hd_outfile_root, time, varname, space)
end

"""
write_to_hdf5(REGRID_DIR, hd_outfile_root, time, field, varname,
write_to_hdf5(REGRID_DIR, time, field, varname,
comms_ctx = ClimaComms.SingletonCommsContext())
Function to save individual HDF5 files after remapping.
If a CommsContext other than SingletonCommsContext is used for `comms_ctx`,
Expand All @@ -249,15 +238,13 @@ Code taken from ClimaCoupler.Regridder.

# Arguments
- `REGRID_DIR`: [String] directory to save output files in.
- `hd_outfile_root`: [String] root of the output file name.
- `time`: [Dates.DateTime] the timestamp of the data being written.
- `field`: [Fields.Field] object to be written.
- `varname`: [String] variable name of data.
- `comms_ctx`: [ClimaComms.AbstractCommsContext] context used for this operation.
"""
function write_to_hdf5(
REGRID_DIR,
hd_outfile_root,
time,
field,
varname,
Expand All @@ -266,10 +253,7 @@ function write_to_hdf5(
# Include time component in HDF5 writer name, and write time to file if it's a valid date
if !(time == Dates.DateTime(0))
hdfwriter = ClimaCore.InputOutput.HDF5Writer(
joinpath(
REGRID_DIR,
hd_outfile_root * "_" * varname * "_" * string(time) * ".hdf5",
),
joinpath(REGRID_DIR, varname * "_" * string(time) * ".hdf5"),
comms_ctx,
)

Expand All @@ -281,7 +265,7 @@ function write_to_hdf5(
) # TODO: a better way to write metadata, CMIP convention
else
hdfwriter = ClimaCore.InputOutput.HDF5Writer(
joinpath(REGRID_DIR, hd_outfile_root * "_" * varname * ".hdf5"),
joinpath(REGRID_DIR, varname * ".hdf5"),
comms_ctx,
)
end
Expand Down Expand Up @@ -335,7 +319,6 @@ end
datafile_rll,
varnames,
space,
outfile_root;
mono = false,
)
Reads and regrids data of all `varnames` variables from an input NetCDF file and
Expand All @@ -357,25 +340,23 @@ Code taken from ClimaCoupler.Regridder.
- `datafile_rll`: [String] filename of RLL dataset to be mapped to CGLL.
- `varnames`: [Vector{String}] the name of the variable to be remapped.
- `space`: [ClimaCore.Spaces.AbstractSpace] the space to which we are mapping.
- `outfile_root`: [String] root of the output file name.
- `mono`: [Bool] flag to specify monotone remapping.
"""
function hdwrite_regridfile_rll_to_cgll(
FT,
REGRID_DIR,
datafile_rll,
varnames::Vector{String},
space,
outfile_root;
space;
mono = false,
)
out_type = "cgll"

datafile_cgll = joinpath(REGRID_DIR, outfile_root * ".g")
meshfile_rll = joinpath(REGRID_DIR, outfile_root * "_mesh_rll.g")
meshfile_cgll = joinpath(REGRID_DIR, outfile_root * "_mesh_cgll.g")
meshfile_overlap = joinpath(REGRID_DIR, outfile_root * "_mesh_overlap.g")
weightfile = joinpath(REGRID_DIR, outfile_root * "_remap_weights.nc")
datafile_cgll = joinpath(REGRID_DIR, "datafile.g")
meshfile_rll = joinpath(REGRID_DIR, "mesh_rll.g")
meshfile_cgll = joinpath(REGRID_DIR, "mesh_cgll.g")
meshfile_overlap = joinpath(REGRID_DIR, "mesh_overlap.g")
weightfile = joinpath(REGRID_DIR, "remap_weights.nc")

# If doesn't make sense to regrid with GPUs/MPI processes
space_singleton = construct_singleton_space(space)
Expand Down Expand Up @@ -490,7 +471,6 @@ function hdwrite_regridfile_rll_to_cgll(
map(
x -> write_to_hdf5(
REGRID_DIR,
outfile_root,
times[x],
offline_fields[x],
varname,
Expand Down
19 changes: 1 addition & 18 deletions ext/nc_common.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,25 +14,8 @@ function read_available_dates(ds::NetCDFDataset)
reinterpret.(Ref(NCDatasets.DateTimeStandard), ds["time"][:])
)
elseif "date" in keys(ds.dim)
return yyyymmdd_to_datetime.(string.(ds["date"][:]))
return Dates.DateTime.(string.(ds["date"][:]), Ref("yyyymmdd"))
else
return Dates.DateTime[]
end
end

"""
strdate_to_datetime(strdate::String)

Convert from String ("YYYYMMDD") to Date format.

# Arguments
- `yyyymmdd`: [String] to be converted to Date type
"""
function yyyymmdd_to_datetime(strdate::String)
length(strdate) == 8 || error("$strdate does not have the YYYYMMDD format")
return Dates.DateTime(
parse(Int, strdate[1:4]),
parse(Int, strdate[5:6]),
parse(Int, strdate[7:8]),
)
end
22 changes: 22 additions & 0 deletions test/file_readers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -107,3 +107,25 @@ end
@test isempty(open_ncfiles)
end
end

@testset "read_available_dates" begin
read_dates_func =
Base.get_extension(
ClimaUtilities,
:ClimaUtilitiesNCDatasetsExt,
).NCFileReaderExt.read_available_dates

data_dir = mktempdir()
NCDataset(joinpath(data_dir, "test_time_1.nc"), "c") do nc
defDim(nc, "time", 2)
times = [DateTime(2022), DateTime(2023)]
defVar(nc, "time", times, ("time",))
@test read_dates_func(nc) == times
end
NCDataset(joinpath(data_dir, "test_date_1.nc"), "c") do nc
defDim(nc, "date", 2)
times = [20220101, 20230101]
defVar(nc, "date", times, ("date",))
@test read_dates_func(nc) == DateTime.(string.(times), "yyyymmdd")
end
end
32 changes: 31 additions & 1 deletion test/regridders.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ using Test

import ClimaUtilities
import ClimaUtilities: Regridders

import NCDatasets
import ClimaCore
import ClimaComms
@static pkgversion(ClimaComms) >= v"0.6" && ClimaComms.@import_required_backends
Expand Down Expand Up @@ -214,3 +214,33 @@ end
@test maximum(err_z) < 1e-5
end
end

@testset "TempestRegridder" begin
for FT in (Float32, Float64)
data_path = joinpath(@__DIR__, "test_data", "era5_1979_1.0x1.0_lai.nc")
ds = NCDatasets.NCDataset(data_path, "r")
original_max = maximum(ds["lai_lv"][:, :, 1])
original_min = minimum(ds["lai_lv"][:, :, 1])
test_time = ds["time"][1]
close(ds)
test_space = make_spherical_space(FT; context).horizontal
regrid_dir = nothing
if ClimaComms.iamroot(context)
regrid_dir = mktempdir()
end
regrid_dir = ClimaComms.bcast(context, regrid_dir)
ClimaComms.barrier(context)
regridder = Regridders.TempestRegridder(
test_space,
"lai_lv",
data_path;
regrid_dir,
)
ClimaComms.barrier(context)
regridded_field = Regridders.regrid(regridder, test_time)
regridded_max = maximum(regridded_field)
regridded_min = minimum(regridded_field)
@test original_max >= regridded_max
@test original_min <= regridded_min
end
end
Loading