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

Gridded Forcings Engine Data Provider #828

Draft
wants to merge 6 commits into
base: master
Choose a base branch
from
Draft
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
Binary file not shown.
61 changes: 61 additions & 0 deletions data/forcing/forcings-engine/config_aorc_gridded.yml.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
time_step_seconds: 3600
initial_time: 0
NWM_VERSION: 4.0
NWM_CONFIG: "AORC"
InputForcings: [12]
InputForcingDirectories: ['']
InputForcingTypes: ["NETCDF4"]
InputMandatory: [1]
OutputFrequency: 60
SubOutputHour: 0
SubOutFreq: 0
ScratchDir: "@FORCINGS_ENGINE_SCRATCH_DIR@"
Output: 1
compressOutput: 0
floatOutput: 0
AnAFlag: 0
LookBack : -9999
RefcstBDateProc: "202301170000"
RefcstEDateProc: "202301170100"
ForecastFrequency: 60
ForecastShift: 0
ForecastInputHorizons: [14400]
ForecastInputOffsets: [0]
GeogridIn: "data/forcing/forcings-engine/Gauge_01073000_Grid.nc"
SpatialMetaIn: ""
GRID_TYPE: "gridded"
LONVAR: 'Longitude'
LATVAR: 'Latitude'
HGTVAR: 'Elevation'
SLOPE: 'Slope'
SLOPE_AZIMUTH: 'Aspect'
IgnoredBorderWidths: [0]
RegridOpt: [1]
ForcingTemporalInterpolation: [0]
TemperatureBiasCorrection: [0]
PressureBiasCorrection: [0]
HumidityBiasCorrection: [0]
WindBiasCorrection: [0]
SwBiasCorrection: [0]
LwBiasCorrection: [0]
PrecipBiasCorrection: [0]
TemperatureDownscaling: [0]
ShortwaveDownscaling: [0]
PressureDownscaling: [0]
PrecipDownscaling: [0]
HumidityDownscaling: [0]
DownscalingParamDirs: ["@FORCINGS_ENGINE_SCRATCH_DIR@"]
SuppPcp: []
SuppPcpForcingTypes: ''
SuppPcpDirectories: ''
SuppPcpParamDir: ''
RegridOptSuppPcp: []
SuppPcpTemporalInterpolation: []
SuppPcpInputOffsets: []
SuppPcpMandatory: []
RqiMethod: 0
RqiThreshold: 0.9
cfsEnsNumber: ''
custom_input_fcst_freq: []
includeLQFrac: 0

4 changes: 4 additions & 0 deletions include/bmi/Bmi_Py_Adapter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -623,6 +623,10 @@ namespace models {
bmi_model->attr("set_value_at_indices")(name, index_array, src_array);
}

auto model() {
return bmi_model;
}

protected:
std::string model_name = "BMI Python model";

Expand Down
107 changes: 107 additions & 0 deletions include/forcing/ForcingsEngineGriddedDataProvider.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
#pragma once

#include <NGenConfig.h>

#if NGEN_WITH_PYTHON

#include <forcing/ForcingsEngineDataProvider.hpp>
#include <forcing/GriddedDataSelector.hpp>
#include <geojson/JSONGeometry.hpp>

namespace data_access {

struct GridSpecification
{
//! Total number of rows (aka y)
std::uint64_t rows;

//! Total number of columns (aka x)
std::uint64_t columns;

//! Extent of the grid region (aka min-max corner points)
geojson::box_t extent;
};

struct GridMask {
//! Geographic extent of the mask region (aka min-max corner points)
geojson::box_t extent;

//! Gridded extent of the mask region (aka min-max corner indices)
std::uint64_t rmin;
std::uint64_t cmin;
std::uint64_t rmax;
std::uint64_t cmax;

constexpr std::uint64_t rows() const noexcept
{
return rmax - rmin + 1;
}

constexpr std::uint64_t columns() const noexcept
{
return cmax - cmin + 1;
}

constexpr std::uint64_t size() const noexcept
{
return rows() * columns();
}

static constexpr auto bad_position = static_cast<std::uint64_t>(-1);

//! Derive a discrete position along a real axis from a coordinate component
//! @param component Coordinate component value
//! @param lower_bound Lower bound of component's axis
//! @param upper_bound Upper bound of component's axis
//! @param cardinality Total number of elements along the discrete axis
//! @returns A position along the [0, cardinality) discrete axis.
static std::uint64_t position(double component, double lower_bound, double upper_bound, std::uint64_t cardinality)
{
if (component < lower_bound || component > upper_bound) {
return bad_position;
}

// We can use a static_cast instead of std::floor because the position will never be negative,
// so truncating and flooring are equivalent here.
return static_cast<std::uint64_t>(
(component - lower_bound) * (cardinality / (upper_bound - lower_bound))
);
}
};


struct ForcingsEngineGriddedDataProvider final :
public ForcingsEngineDataProvider<double, GriddedDataSelector>
{
using base_type = ForcingsEngineDataProvider<data_type, selection_type>;

~ForcingsEngineGriddedDataProvider() override = default;

ForcingsEngineGriddedDataProvider(
const std::string& init,
std::size_t time_begin_seconds,
std::size_t time_end_seconds,
geojson::box_t mask
);

data_type get_value(
const selection_type& selector,
data_access::ReSampleMethod m
) override;

std::vector<data_type> get_values(
const selection_type& selector,
data_access::ReSampleMethod m
) override;

const GridMask& mask() const noexcept;

private:
int var_grid_id_;
GridSpecification var_grid_;
GridMask var_grid_mask_;
};

} // namespace data_access

#endif // NGEN_WITH_PYTHON
Loading
Loading