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

SpeedyWeather ensemble online training #176

Open
milankl opened this issue Mar 13, 2024 · 0 comments
Open

SpeedyWeather ensemble online training #176

milankl opened this issue Mar 13, 2024 · 0 comments

Comments

@milankl
Copy link

milankl commented Mar 13, 2024

Just throwing in some first code snippets for ensemble online training with SpeedyWeather, will convert to a proper PR soon

We need

using SpeedyWeather

struct MyInterpolatedVorticity{Grid, NF_model, Grid_model} <: SpeedyWeather.AbstractCallback
    "Interpolate only when scheduled"
    schedule::Schedule

    "From which layer to interpolate"
    layer::Int

    "Variable on interpolated grid"
    var::Grid

    "Interpolator to interpolate model grid into var"
    interpolator::RingGrids.AnvilInterpolator{NF_model, Grid_model}
end

function MyInterpolatedVorticity(
    SG::SpectralGrid;
    layer::Integer = 1,
    nlat_half::Integer = 32,
    NF::Type{<:AbstractFloat} = Float32,
    Grid::Type{<:RingGrids.AbstractGrid} = FullGaussianGrid{NF},
    schedule::Schedule = Schedule(every=Day(1))
)
    n_points = RingGrids.get_npoints(Grid, nlat_half)
    var = zeros(Grid, nlat_half) 
    interpolator = RingGrids.AnvilInterpolator(SG.NF, SG.Grid, SG.nlat_half, n_points)
    RingGrids.update_locator!(interpolator, RingGrids.get_latdlonds(var)...)
    MyInterpolatedVorticity{Grid, SG.NF, SG.Grid}(schedule, layer, var, interpolator)
end

function SpeedyWeather.initialize!(
    callback::MyInterpolatedVorticity,
    progn::PrognosticVariables,
    diagn::DiagnosticVariables,
    model::ModelSetup,
)
    initialize!(callback.schedule, progn.clock)
end

function SpeedyWeather.callback!(
    callback::MyInterpolatedVorticity,
    progn::PrognosticVariables,
    diagn::DiagnosticVariables,
    model::ModelSetup,
)
    isscheduled(callbacks.schedule, progn.clock) || return nothing
    k = callback.layer
    (;vor_grid) = diagn.layers[k].grid_variables
    (;var, interpolator) = callback
    RingGrids.interpolate!(var, vor_grid, interpolator)
end

SpeedyWeather.finish!(::MyInterpolatedVorticity, args...) = nothing

@sandreza

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant