Skip to content

Commit

Permalink
Merge pull request #771 from CliMA/gb/leaderboard
Browse files Browse the repository at this point in the history
Change units to mm/day for leaderboard
  • Loading branch information
Sbozzolo authored May 17, 2024
2 parents fe912e8 + 8011b77 commit a06c248
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 19 deletions.
17 changes: 8 additions & 9 deletions experiments/ClimaEarth/user_io/leaderboard/compare_with_obs.jl
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
const OBS_DS = Dict()
const SIM_DS_KWARGS = Dict()

function preprocess_pr_fn(data)
# 1 mm/day -> - 1 kg/m/s2
# The minus sign comes from the different conventions used
return data .* Float32(-1 / 86400)
# -1 kg/m/s2 -> 1 mm/day
return data .* Float32(-86400)
end

OBS_DS["pr"] = ObsDataSource(;
path = joinpath(pr_obs_data_path(), "gpcp.precip.mon.mean.197901-202305.nc"),
var_name = "precip",
preprocess_data_fn = preprocess_pr_fn,
)
OBS_DS["pr"] =
ObsDataSource(; path = joinpath(pr_obs_data_path(), "gpcp.precip.mon.mean.197901-202305.nc"), var_name = "precip")

SIM_DS_KWARGS["pr"] = (; preprocess_data_fn = preprocess_pr_fn, new_units = "mm / day")

# OBS_DS["rsut"] = ObsDataSource(;
# path = "OBS/CERES_EBAF-TOA_Ed4.2_Subset_200003-202303.g025.nc",
Expand All @@ -24,7 +23,7 @@ OBS_DS["pr"] = ObsDataSource(;

function bias(output_dir::AbstractString, short_name::AbstractString, target_dates::AbstractArray{<:Dates.DateTime})
obs = OBS_DS[short_name]
sim = SimDataSource(; path = output_dir, short_name)
sim = SimDataSource(; path = output_dir, short_name, SIM_DS_KWARGS["pr"]...)
return bias(obs, sim, target_dates)
end

Expand Down
24 changes: 19 additions & 5 deletions experiments/ClimaEarth/user_io/leaderboard/data_sources.jl
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ struct ObsDataSource
"""Name of the latitude dimension in the NetCDF file"""
lat_name::AbstractString

"""Function that has to be applied to the data to convert it to the same conventions
as CliMA"""
"""Function that has to be applied to the data to convert it to different units"""
preprocess_data_fn::Function

"""The NCDataset associated to the file"""
Expand Down Expand Up @@ -75,17 +74,32 @@ struct SimDataSource

"""Simulation longitudes and latitudes"""
lonlat::Tuple{AbstractArray, AbstractArray}

"""Function that has to be applied to the data to convert it to different units"""
preprocess_data_fn::Function

# TODO: This should be handled by ClimaAnalysis
"""preprocess_data_fn is typically used to change units, so we have to tell ClimaAnalysis what the new
units are."""
new_units::Union{Nothing, AbstractString}
end

function SimDataSource(; path, short_name, reduction = "average", period = "10d")
function SimDataSource(;
path,
short_name,
reduction = "average",
period = "10d",
preprocess_data_fn = identity,
new_units = nothing,
)

sim = ClimaAnalysis.SimDir(path)
# TODO: Add period, for the time-being, we just pick up what's there
var = get(sim; short_name, reduction)

lonlat = (var.dims["lon"], var.dims["lat"])

return SimDataSource(path, short_name, reduction, period, var, lonlat)
return SimDataSource(path, short_name, reduction, period, var, lonlat, preprocess_data_fn, new_units)
end

"""
Expand All @@ -96,5 +110,5 @@ Return the simulation data at the given date.
function data_at_date(sim_ds::SimDataSource, date::Dates.DateTime)
start_date = Dates.DateTime(sim_ds.var.attributes["start_date"])
time_diff_seconds = (date - start_date) / Dates.Second(1)
return ClimaAnalysis.slice(sim_ds.var, time = time_diff_seconds).data
return sim_ds.preprocess_data_fn(ClimaAnalysis.slice(sim_ds.var, time = time_diff_seconds).data)
end
2 changes: 1 addition & 1 deletion experiments/ClimaEarth/user_io/leaderboard/utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ function bias(obs_ds::ObsDataSource, sim_ds::SimDataSource, target_dates::Abstra
rmse = round(sqrt(integrate_on_sphere(mse_arr, lonlat)); sigdigits = 3)
global_bias = round(integrate_on_sphere(bias_arr, lonlat); sigdigits = 3)

units = sim_ds.var.attributes["units"]
units = isnothing(sim_ds.new_units) ? sim_ds.var.attributes["units"] : sim_ds.new_units

bias_attribs = Dict{String, Any}(
"short_name" => "sim-obs_$short_name",
Expand Down
10 changes: 6 additions & 4 deletions test/experiment_tests/leaderboard.jl
Original file line number Diff line number Diff line change
Expand Up @@ -53,16 +53,18 @@ end
@testset "Leaderboard" begin
simdir = ClimaAnalysis.SimDir(@__DIR__)

sim_datasource = Leaderboard.SimDataSource(path = @__DIR__, short_name = "pr")
preprocess_fn = (data) -> data .* Float32(-1 / 86400)

# The conversion is technically not correct for this data source, but what
# we care about here is that preprocess_data_fn works
sim_datasource = Leaderboard.SimDataSource(path = @__DIR__, short_name = "pr", preprocess_data_fn = preprocess_fn)

pr = get(simdir, "pr")

@test sim_datasource.lonlat[1] == pr.dims["lon"]
@test sim_datasource.lonlat[2] == pr.dims["lat"]

@test Leaderboard.data_at_date(sim_datasource, Dates.DateTime(1979, 1, 2)) == pr.data[1, :, :]

preprocess_fn = (data) -> data .* Float32(-1 / 86400)
@test Leaderboard.data_at_date(sim_datasource, Dates.DateTime(1979, 1, 2)) == preprocess_fn(pr.data[1, :, :])

obs_datasource = Leaderboard.ObsDataSource(;
path = joinpath(pr_obs_data_path(), "gpcp.precip.mon.mean.197901-202305.nc"),
Expand Down

0 comments on commit a06c248

Please sign in to comment.