Skip to content

Commit

Permalink
feat(ForcingsEngineGriddedDataProvider): initial impl
Browse files Browse the repository at this point in the history
  • Loading branch information
program-- committed Aug 13, 2024
1 parent 088fc26 commit 896b9a6
Show file tree
Hide file tree
Showing 5 changed files with 263 additions and 260 deletions.
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();

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

} // namespace data_access

#endif // NGEN_WITH_PYTHON
259 changes: 0 additions & 259 deletions include/forcing/GridDataSelector.hpp

This file was deleted.

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

#include <string>

struct GriddedDataSelector {
std::string variable_name;
time_t init_time;
long duration;
std::string output_units;
};
4 changes: 3 additions & 1 deletion include/geojson/JSONGeometry.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ namespace geojson {

typedef bg::model::multi_polygon<polygon_t> multipolygon_t;

using box_t = boost::geometry::model::box<geojson::coordinate_t>;

/**
* A two dimensional matrix of doubles
*/
Expand Down Expand Up @@ -176,4 +178,4 @@ namespace geojson {
}
}

#endif // GEOJSON_GEOMETRY_H
#endif // GEOJSON_GEOMETRY_H
Loading

0 comments on commit 896b9a6

Please sign in to comment.