Skip to content

About

Daily weather for any place on land and any date: temperatures, rain, snow, thunder, cloud, humidity, wind and the sun, drawn around 1991-2020 climate normals. C++23, header-only, deterministic.

Topics

Resources

Stars

2 stars

Watchers

0 watching

Forks

Repository files navigation

Weather Generator for C++

Requires C++23 (e.g., -std=c++23 for GCC/Clang, /std:c++latest for MSVC).

Weather Generator for C++

GitHub license CI GitHub Releases C++23 Header-only Documentation

API Reference · Usage Guide · Data and Method · Releases

Daily weather for any place on land and any date: the low and the high, rain or snow and how much, snow on the ground, thunder, drizzle and fog, cloud, humidity, wind and gusts, sunrise, sunset, sunshine and solar radiation, drawn around the place's 1991-2020 climate normals so that thirty generated years look like thirty real ones, and nearby places share their weather as real neighbours do.

2024-01-15: light rain, 4.2 to 9.9 °C, 0.5 mm
2024-01-16: sleet, 1.8 to 9.4 °C, 0.7 cm of snow
2024-01-17: sleet, 2.3 to 5.2 °C, 0.3 cm of snow
2024-01-18: mostly cloudy, -2.1 to 3.2 °C

Features

  • Any Place, Any Date. Give a latitude and longitude (or leave them out for a place drawn by land area) and any date. The climate comes from the CRU TS 1991-2020 normals of the nearest cell: a 2-degree grid in lite, half a degree in full. A point off the coast takes the nearest land's weather up to 100 km out, and reports how far it is. An elevation moves temperatures by the standard lapse rate, and warming shifts them for a warmer climate than the normals'.

  • Weather That Behaves Like Weather. Wet days cluster, dry spells run long, warm spells last, fronts bring warm days before the rain and cold days after it, and some months run wet or cold: a Richardson-type weather generator (a semi-Markov rain chain, Weibull amounts, autocorrelated daily mean and range shifted by the rain around each day, month-to-month anomalies), fitted on 162 GHCN-Daily stations and checked against 119 others it never saw. Monthly means, wet days, wet-day amounts, the wettest days, dry spells, day-to-day spread and year-to-year spread land within a few percent of those stations (see Data and Method).

  • Series That Agree. A day's weather depends only on the seed, the place and the date, so the weather of 10 March is the same whether it is asked for alone or inside a series that starts in January, and overlapping series agree.

  • One World per Seed. On the same seed, nearby places share their weather: a rainy day in Paris is likely rainy in London, a cold month cold across the region, with correlations that fall with distance as between real weather stations. Turn spatial off for each place's own weather.

  • Rain, Snow, Sleet, Thunder, Drizzle and Fog. The form of precipitation follows the day's temperature, thunder and drizzle its heat and rain, fog its humidity, rain, wind and season, and the day's strongest gust its wind, all fitted on station records. Snow lies on the ground, melts in a thaw and settles as it ages. Fourteen conditions from clear and fog to heavy_snow and thunderstorm, and frost on days below 0 °C.

  • The Sun. Sunrise, sunset and day length from NOAA's solar equations, polar day and night included, the hours of sunshine the day's cloud lets through (fitted on CRU sunshine records), and the solar energy reaching the ground.

  • Hours, Imperial Units and Real Years. Ask for a day's 24 hours (a temperature curve from sunrise to the next night, rain in spells that add up to the day, cloud, wind, humidity and sunshine); for °F, inches and mph; or for any month of 1991-2020 as it really was (a cold winter cold, a drought summer dry), from CRU TS monthly records. All three are opt-in: a plain request gives the same days without them.

  • Climate Normals Too. climate_at(lat, lon) returns the monthly normals a place's weather is drawn around: temperature, diurnal range, precipitation, wet days, humidity, cloud and wind.

  • Deterministic Seeding. Per-call get_weather(request, seed) for reproducible results, and weather::seed() for replaying a previous generation with the same request and data.

  • Batch Generation. Generate thousands of results from one seed, on several threads with the same output for any thread count, and stream them as JSON Lines or CSV (see the usage guide).

  • Identical Everywhere. A seed gives the same result on every compiler, standard library and CPU (GCC, Clang, MSVC, Emscripten; x86-64 and ARM64), checked by known-answer tests.

  • Extensible Data. The climate table is a TSV file you can extend with layers (extend()): add a station's own normals and the points nearest it use them.

  • JSON and Replay. to_json() on every result, and a signature() recording the seed and the data it came from.

  • Compiled-in Data. Define DASMIG_WEATHER_EMBED to build the lite tier into the program, for WebAssembly and other builds without files.

  • Multi-Instance Support. Construct independent wtg instances with their own data and random engine.

Integration

weathergen.hpp is the library released here. Keep core.hpp and the core/ directory (dasmig-core) in the same directory. Add

#include <dasmig/weathergen.hpp>

to the files you want to use it in, and supply the resources folder (or define DASMIG_WEATHER_EMBED).

Usage

#include <dasmig/weathergen.hpp>
#include <iostream>

using namespace std::chrono_literals;

auto& gen = dasmig::wtg::instance(); // loads the lite tier

dasmig::weather_request paris;
paris.latitude = 48.85;
paris.longitude = 2.35;
paris.date = 2024y / 1 / 15;

auto day = gen.get_weather(paris, 7); // seed 7
std::cout << day << '\n';             // 2024-01-15: light rain, 4.2 to 9.9 °C, 0.5 mm
std::cout << day.to_json() << '\n';   // every field, with the seed

for (const auto& d : gen.get_weather_series(paris, 7, 42)) // a week, seed 42
    std::cout << d << '\n';

paris.elevation = 1500.0; // the same place higher up
paris.warming = 1.2;      // in a climate 1.2 °C warmer

auto anywhere = gen.get_weather(dasmig::weather_request{}); // a place drawn by land area

// The same seed gives the same result everywhere.
auto again = gen.get_weather(paris, day.seed());

See the usage guide for everything else.

Building

make            # example
make test       # tests
make bench      # benchmarks, results in build/bench.tsv
make test-embed # tests with the lite tier compiled in
make coverage   # code coverage
make lint       # clang-tidy
make docs       # API docs
make embed      # regenerate dasmig/weathergen_data.hpp from resources/lite

Compiler Support

Tested with:

  • Clang 18+ (-std=c++23)
  • GCC 14+ (-std=c++23)
  • MSVC 19.38+ (/std:c++latest)
  • Emscripten 3.1.74

Dependencies

Dependency Version Bundled Purpose
dasmig-core 1.4 Yes (core.hpp, core/) Platform-independent random engine and distributions, data layers, JSON
Catch2 3.x Yes (amalgamated) Unit testing

Related Libraries

Library Description
address-generator Postal addresses with real streets by place
biodata-generator Procedural human physical characteristics
birth-generator Demographically plausible birthdays
city-generator Weighted city selection by population
company-generator Companies by country: industry, size, legal form, name, registration numbers and domain
country-generator Weighted country selection by population
device-generator Devices, browsers, user agents and IP addresses that fit together
email-generator Email addresses in each country's style
entity-generator ECS-based entity generation
finance-generator Bank accounts and payment cards in each country's form
id-generator National identity numbers, passports and identity cards that fit a person and a date
interest-generator Religion, hobbies and favourite media by country, age and sex
name-generator Culturally appropriate full names
nickname-generator Gamer-style nicknames
occupation-generator Employment status, education and occupation by country, age and sex
persona-generator Complete characters composed from the other generators
phone-generator Phone numbers valid in each country's numbering plan
property-generator Homes that fit a household: dwelling type, tenure, rooms, size, age and cost
relationship-generator Families around a person: parents, siblings, partners and children
trait-generator Personality traits with conflicts, synergies and Big Five axes
user-generator Complete user records: account, username, email, phone and address
vehicle-generator Cars and motorcycles with plates and VINs that fit their country and date

License

This library is released under the MIT License. The climate data comes from CRU TS and CRU CL 2.0, which have their own terms (the Open Database License); the sources are listed in LICENSE_DATA.txt and doc/data.md.

MIT License

Copyright (c) 2026 Diego Dasso Migotto

About

Daily weather for any place on land and any date: temperatures, rain, snow, thunder, cloud, humidity, wind and the sun, drawn around 1991-2020 climate normals. C++23, header-only, deterministic.

Topics

Resources

Stars

2 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages