Weather Generator 1.1.0
Daily weather for any place and date from climate normals for C++23
Loading...
Searching...
No Matches
dasmig::wtg Class Reference

Weather Generator: daily weather drawn around monthly climate normals. More...

#include <weathergen.hpp>

Public Member Functions

 wtg ()=default
 Default constructor — creates an empty generator with no data.
 
 wtg (const wtg &)=delete
 Not copyable.
 
wtg & operator= (const wtg &)=delete
 Not copyable.
 
 wtg (wtg &&) noexcept=default
 Movable.
 
wtg & operator= (wtg &&) noexcept=default
 Movable.
 
 ~wtg ()=default
 Destructor.
 
weather get_weather (const weather_request &request)
 Generate one day's weather. Use weather_request{} for the defaults.
 
weather get_weather (const weather_request &request, std::uint64_t call_seed) const
 Generate one day's weather, deterministically.
 
std::vector< weather > get_weather_series (const weather_request &request, int days)
 Generate the weather of consecutive days.
 
std::vector< weather > get_weather_series (const weather_request &request, int days, std::uint64_t call_seed) const
 Generate the weather of consecutive days, deterministically.
 
climate_normals climate_at (double latitude, double longitude) const
 The monthly normals of the climate cell nearest a point.
 
std::vector< weather > get_weather_batch (const weather_request &request, const core::batch_options &options)
 Generate a batch of weather results from a seed drawn like get_weather() draws its own.
 
std::vector< weather > get_weather_batch (const weather_request &request, const core::batch_options &options, std::uint64_t batch_seed) const
 Generate a batch of weather results, deterministically.
 
template<typename Sink >
void for_each_weather (const weather_request &request, const core::batch_options &options, std::uint64_t batch_seed, Sink &&sink) const
 Stream a batch of weather results to a sink without holding the whole batch.
 
template<typename Sink >
void for_each_weather (const weather_request &request, const core::batch_options &options, Sink &&sink)
 Stream a batch of weather results with a batch seed drawn like get_weather() draws its own.
 
bool has_data () const
 Check whether any data has been loaded.
 
std::size_t count () const
 Return the number of loaded climate cells.
 
void load (const std::filesystem::path &dir)
 Load data from a resource directory, replacing all data and layers loaded before.
 
bool load (dataset tier)
 Load a dataset tier, replacing all data and layers loaded before.
 
const core::layer_info & extend (const std::filesystem::path &file, core::layer_mode mode=core::layer_mode::override)
 Add a data layer from a TSV file with the climate.tsv columns.
 
const core::layer_info & extend_from_string (std::string_view text, std::string source, core::layer_mode mode=core::layer_mode::override)
 Add a data layer from TSV text (for example from a WebAssembly host).
 
const core::layer_info & extend (core::tsv_document doc, core::layer_mode mode=core::layer_mode::override)
 Add a data layer.
 
bool load_history ()
 Load the months of 1991-2020 for historical requests, from the compiled-in copy or resources/history/history.tsv.
 
void load_history (const std::filesystem::path &file)
 Load the months of 1991-2020 from a file.
 
bool has_history () const
 Check whether historical requests can be served.
 
const std::vector< core::layer_info > & layers () const
 The data layers loaded, in order.
 
std::uint64_t data_hash () const
 Hash of the loaded data, recorded in each result's signature().
 

Static Public Member Functions

static wtg & instance ()
 Access the global singleton instance.
 

Static Public Attributes

static constexpr int max_series_days = 36600
 The longest series one call returns, in days (about 100 years).
 

Detailed Description

Weather Generator: daily weather drawn around monthly climate normals.

A Richardson (1981) type weather generator: rain falls by a two-state Markov chain, wet-day amounts are gamma distributed, the daily mean temperature and the daily range follow the season with residuals from a lag-1 autoregressive process, months carry their own anomalies, and cloud, humidity and wind follow the wet and dry days. Its parameters come from the place's monthly normals (CRU TS 4.10, 1991-2020) and from fits on GHCN-Daily stations (doc/data.md).

A day's weather depends only on the seed, the place and the date: the draws of each day come from that day's own stream, and the chains are read back to the day where their state no longer matters. So the weather of 10 March is the same whether it is asked for alone or inside a series that starts in January. Output is identical on every compiler and platform (dasmig-core).

Can be used as a singleton via instance() or constructed independently.

Thread safety
Each instance is independent. Concurrent calls to get_weather(request) on the same instance require external synchronisation.

Definition at line 504 of file weathergen.hpp.

Member Function Documentation

◆ climate_at()

climate_normals dasmig::wtg::climate_at ( double  latitude,
double  longitude 
) const
inline

The monthly normals of the climate cell nearest a point.

Parameters
latitudeDegrees north, -90 to 90.
longitudeDegrees east, -180 to 180.
Returns
The cell's normals.
Exceptions
std::runtime_errorIf no data has been loaded.
std::invalid_argumentIf the point is out of range or more than 100 km from land.

Definition at line 598 of file weathergen.hpp.

◆ count()

std::size_t dasmig::wtg::count ( ) const
inline

Return the number of loaded climate cells.

Returns
The count.

Definition at line 689 of file weathergen.hpp.

◆ data_hash()

std::uint64_t dasmig::wtg::data_hash ( ) const
inline

Hash of the loaded data, recorded in each result's signature().

Returns
The hash; 0 when nothing is loaded.

Definition at line 822 of file weathergen.hpp.

◆ extend() [1/2]

const core::layer_info & dasmig::wtg::extend ( const std::filesystem::path &  file,
core::layer_mode  mode = core::layer_mode::override 
)
inline

Add a data layer from a TSV file with the climate.tsv columns.

Rows are keyed by key. In the default override mode a row replaces the loaded row with the same key and new keys are added (a station's own normals, say, which then serve the points nearest it); a row with disabled set to 1 removes its key. elevation, area, humidity, cloud and wind may be left out (0 m, never drawn as a random place, 70%, 50% and 3.5 m/s).

Parameters
fileThe layer file.
modeHow the layer combines with the data loaded before.
Returns
What the layer did (rows added, replaced, removed, warnings).
Exceptions
core::tsv_errorIf the file cannot be read or a row is invalid; the data is then unchanged.

Definition at line 753 of file weathergen.hpp.

◆ extend() [2/2]

const core::layer_info & dasmig::wtg::extend ( core::tsv_document  doc,
core::layer_mode  mode = core::layer_mode::override 
)
inline

Add a data layer.

Parameters
docTSV with the climate.tsv columns.
modeHow the layer combines with the data loaded before.
Returns
What the layer did.
Exceptions
core::tsv_errorIf a row is invalid; the data is unchanged.

Definition at line 777 of file weathergen.hpp.

◆ extend_from_string()

const core::layer_info & dasmig::wtg::extend_from_string ( std::string_view  text,
std::string  source,
core::layer_mode  mode = core::layer_mode::override 
)
inline

Add a data layer from TSV text (for example from a WebAssembly host).

Parameters
textTSV with the climate.tsv columns.
sourceName used in errors.
modeHow the layer combines with the data loaded before.
Returns
What the layer did.
Exceptions
core::tsv_errorIf a row is invalid; the data is unchanged.

Definition at line 766 of file weathergen.hpp.

◆ for_each_weather() [1/2]

template<typename Sink >
void dasmig::wtg::for_each_weather ( const weather_request &  request,
const core::batch_options &  options,
Sink &&  sink 
)
inline

Stream a batch of weather results with a batch seed drawn like get_weather() draws its own.

Template Parameters
SinkCallable taking (std::uint64_t, weather&).
Parameters
requestWhat every item is generated from.
optionsCount, first index, threads and uniqueness.
sinkReceives the items.

Definition at line 676 of file weathergen.hpp.

◆ for_each_weather() [2/2]

template<typename Sink >
void dasmig::wtg::for_each_weather ( const weather_request &  request,
const core::batch_options &  options,
std::uint64_t  batch_seed,
Sink &&  sink 
) const
inline

Stream a batch of weather results to a sink without holding the whole batch.

Generates the same items as get_weather_batch() and calls sink(index, item) in index order on the calling thread; index includes options.first.

Template Parameters
SinkCallable taking (std::uint64_t, weather&); for example core::jsonl_writer or core::csv_writer.
Parameters
requestWhat every item is generated from.
optionsCount, first index, threads and uniqueness.
batch_seedSeed of the whole batch.
sinkReceives the items.

Definition at line 661 of file weathergen.hpp.

◆ get_weather() [1/2]

weather dasmig::wtg::get_weather ( const weather_request &  request)
inline

Generate one day's weather. Use weather_request{} for the defaults.

Parameters
requestWhat to generate.
Returns
The weather.
Exceptions
std::runtime_errorIf no data has been loaded, or the request is historical and no history is loaded.
std::invalid_argumentIf the request is invalid.

Definition at line 540 of file weathergen.hpp.

◆ get_weather() [2/2]

weather dasmig::wtg::get_weather ( const weather_request &  request,
std::uint64_t  call_seed 
) const
inline

Generate one day's weather, deterministically.

Parameters
requestWhat to generate.
call_seedSeed; the same request, seed and data give the same result. Does not touch the instance engine.
Returns
The weather; equal to day 0 of get_weather_series() with the same request and seed.
Exceptions
std::runtime_errorIf no data has been loaded.
std::invalid_argumentIf the request is invalid.

Definition at line 553 of file weathergen.hpp.

◆ get_weather_batch() [1/2]

std::vector< weather > dasmig::wtg::get_weather_batch ( const weather_request &  request,
const core::batch_options &  options 
)
inline

Generate a batch of weather results from a seed drawn like get_weather() draws its own.

Item i is get_weather(request, core::item_seed(batch_seed, options.first + i)) for the drawn batch seed; see the seeded overload.

Parameters
requestWhat every item is generated from.
optionsCount, first index, threads and uniqueness.
Returns
The results, in index order.

Definition at line 617 of file weathergen.hpp.

◆ get_weather_batch() [2/2]

std::vector< weather > dasmig::wtg::get_weather_batch ( const weather_request &  request,
const core::batch_options &  options,
std::uint64_t  batch_seed 
) const
inline

Generate a batch of weather results, deterministically.

Item i is exactly get_weather(request, core::item_seed(batch_seed, options.first + i)), so an item replays on its own, a range can be generated directly, two batches that follow each other equal one, and the output does not depend on options.threads. With no place set, each item draws its own place.

Parameters
requestWhat every item is generated from.
optionsCount, first index, threads and uniqueness (see core::unique_rule; the rule must be made for weather).
batch_seedSeed of the whole batch.
Returns
The results, in index order.
Exceptions
std::invalid_argumentIf the request or options are invalid.
std::runtime_errorIf no data has been loaded, or a unique batch runs out of attempts.

Definition at line 639 of file weathergen.hpp.

◆ get_weather_series() [1/2]

std::vector< weather > dasmig::wtg::get_weather_series ( const weather_request &  request,
int  days 
)
inline

Generate the weather of consecutive days.

Parameters
requestThe place and the first day (date).
daysHow many days, 1 to max_series_days.
Returns
The days, in order.
Exceptions
std::runtime_errorIf no data has been loaded.
std::invalid_argumentIf the request or days is invalid.

Definition at line 564 of file weathergen.hpp.

◆ get_weather_series() [2/2]

std::vector< weather > dasmig::wtg::get_weather_series ( const weather_request &  request,
int  days,
std::uint64_t  call_seed 
) const
inline

Generate the weather of consecutive days, deterministically.

Day i equals get_weather() for the same place and seed with date moved i days on, so series that overlap agree on the days they share.

Parameters
requestThe place and the first day (date).
daysHow many days, 1 to max_series_days.
call_seedSeed of the series; every day carries it.
Returns
The days, in order.
Exceptions
std::runtime_errorIf no data has been loaded.
std::invalid_argumentIf the request or days is invalid.

Definition at line 581 of file weathergen.hpp.

◆ has_data()

bool dasmig::wtg::has_data ( ) const
inline

Check whether any data has been loaded.

Returns
True when at least one climate cell is loaded.

Definition at line 685 of file weathergen.hpp.

◆ has_history()

bool dasmig::wtg::has_history ( ) const
inline

Check whether historical requests can be served.

Returns
True when load_history() has loaded the months.

Definition at line 813 of file weathergen.hpp.

◆ instance()

static wtg & dasmig::wtg::instance ( )
inlinestatic

Access the global singleton instance.

Loads the lite tier (or full if lite is missing) on first access, from compiled-in data or an auto-probed resources/.

Returns
The singleton.

Definition at line 525 of file weathergen.hpp.

◆ layers()

const std::vector< core::layer_info > & dasmig::wtg::layers ( ) const
inline

The data layers loaded, in order.

Returns
The layer records.

Definition at line 817 of file weathergen.hpp.

◆ load() [1/2]

void dasmig::wtg::load ( const std::filesystem::path &  dir)
inline

Load data from a resource directory, replacing all data and layers loaded before.

Expects the directory to contain climate.tsv, and reads the land mask from land.tsv when it is there (without one, a point must be within 100 km of a cell). A path that is not a directory, or a directory without climate.tsv, is ignored.

Parameters
dirDirectory such as resources/lite.
Exceptions
core::tsv_errorIf the file is malformed.

Definition at line 701 of file weathergen.hpp.

◆ load() [2/2]

bool dasmig::wtg::load ( dataset  tier)
inline

Load a dataset tier, replacing all data and layers loaded before.

lite is a 2-degree grid (5,163 cells), full a half-degree grid (67,213 cells). Uses the compiled-in copy when the library is built with DASMIG_WEATHER_EMBED, otherwise searches resources/, ../resources, weather-generator/resources and ../weather-generator/resources.

Parameters
tierThe tier.
Returns
False when the tier was not found.
Exceptions
core::tsv_errorIf the file is malformed.

Definition at line 726 of file weathergen.hpp.

◆ load_history() [1/2]

bool dasmig::wtg::load_history ( )
inline

Load the months of 1991-2020 for historical requests, from the compiled-in copy or resources/history/history.tsv.

Not loaded by instance() or load(): the table is large (23 MB) and only historical requests read it. It holds, for each 2-degree block of land, each month's temperature, precipitation and wet days against the 1991-2020 normals (CRU TS 4.10); a cell takes the block its point lies in.

Returns
False when the file was not found.
Exceptions
core::tsv_errorIf the file is malformed.

Definition at line 796 of file weathergen.hpp.

◆ load_history() [2/2]

void dasmig::wtg::load_history ( const std::filesystem::path &  file)
inline

Load the months of 1991-2020 from a file.

Parameters
fileA history.tsv.
Exceptions
core::tsv_errorIf the file cannot be read or is malformed; the months loaded before are then kept.

Definition at line 809 of file weathergen.hpp.

Member Data Documentation

◆ max_series_days

constexpr int dasmig::wtg::max_series_days = 36600
staticconstexpr

The longest series one call returns, in days (about 100 years).

Definition at line 508 of file weathergen.hpp.


The documentation for this class was generated from the following file: