Skip to main content

geolith_dataset_builder/
lib.rs

1#![warn(missing_docs)]
2
3//! Raster datasets: district numbers and a map layer per period.
4//!
5//! `geolith dataset` turns a folder of gridded rasters (CHIRPS rainfall
6//! first) into per-district values and a PNG map layer. Its engine steps are
7//! `areas` (district polygons from Overture, India cut with the Survey of
8//! India outline), `normals` (per-district baseline averages), `values` (the
9//! area values of one raster, cached for `normals`), `build` (numbers, map
10//! layer and files for every period on disk), `inspect` (a raster's layout
11//! and whether each read path decodes it) and `downloads` (the Parquet and
12//! `GeoParquet` files of a stage). The operator steps are `stage` (an
13//! upload-ready tree of the object keys), `verify` (the checks a stage must
14//! pass), `status` (the newest stage and its staleness) and `keys` (the keys
15//! an upload would write, from local files).
16
17pub mod accuracy;
18pub mod areas;
19pub mod areas_tiles;
20pub mod baseline;
21pub mod bounds;
22pub mod cdse;
23pub mod cli;
24pub mod config;
25pub mod country;
26pub mod coverage;
27pub mod crosswalk;
28pub mod crosswalk_gen;
29pub mod downloads;
30pub mod error;
31mod exact_json;
32pub mod exit;
33pub mod fetch;
34pub mod files;
35pub mod inputs;
36pub mod inspect;
37pub mod keys;
38pub mod layer;
39pub mod layout;
40pub mod lgd;
41pub mod lists;
42pub mod normals;
43pub mod numbers;
44pub mod official_areas;
45pub mod period;
46pub mod prune;
47pub mod rasters;
48pub mod refresh;
49pub mod remote;
50pub mod run;
51pub mod s3;
52pub mod schedule;
53pub mod series;
54pub mod settings;
55pub mod slots;
56pub mod source;
57pub mod stage;
58pub mod status;
59pub mod topology;
60pub mod verify;
61
62use crate::cli::{DatasetAction, DatasetArgs};
63use crate::error::Result;
64
65/// Run the dataset step selected by `args`: the deployment settings are
66/// read once (the process environment, the `.env` file, then the JSON file of
67/// `--config` or `GEOLITH_CONFIG`), then the step runs.
68///
69/// # Errors
70///
71/// Fails when the deployment settings cannot be read, and propagates the
72/// error of the selected step.
73pub fn run(args: &DatasetArgs) -> Result<()> {
74    settings::init(settings::Deployment::load(args.config.as_deref())?);
75    dispatch(args)
76}
77
78/// The step selected by `args`, over the deployment settings already fixed.
79fn dispatch(args: &DatasetArgs) -> Result<()> {
80    match &args.action {
81        DatasetAction::Areas(a) => areas::run(a),
82        DatasetAction::Normals(a) => normals::run(a),
83        DatasetAction::Build(a) => files::run(a),
84        DatasetAction::Values(a) => normals::run_values(a),
85        DatasetAction::Inspect(a) => inspect::run(a),
86        DatasetAction::Downloads(a) => downloads::run(a),
87        DatasetAction::Stage(a) => stage::run(a).map(|dir| {
88            println!("{}", dir.display());
89        }),
90        DatasetAction::Verify(a) => verify::run(a),
91        DatasetAction::Status(a) => status::run(a),
92        DatasetAction::Keys(a) => keys::run(a),
93        DatasetAction::Upload(a) => remote::upload(a),
94        DatasetAction::Publish(a) => remote::publish(a),
95        DatasetAction::Lists(a) => lists::run(a),
96        DatasetAction::Fetch(a) => fetch::run(a),
97        DatasetAction::BaselinePass(a) => baseline::run(a),
98        DatasetAction::Cdse(a) => cdse::run(a),
99        DatasetAction::Lgd(a) => lgd::run(a),
100        DatasetAction::Crosswalk(a) => crosswalk_gen::run(a),
101        DatasetAction::Inputs(a) => inputs::run(a),
102        DatasetAction::Run(a) => run::run(a),
103        DatasetAction::Prune(a) => prune::run(a),
104        DatasetAction::Bounds(a) => bounds::run(a),
105        DatasetAction::Slots(a) => slots::run(a),
106        DatasetAction::Accuracy(a) => accuracy::run(a),
107        DatasetAction::Schedule(a) => schedule::run(a),
108        DatasetAction::Refresh(a) => refresh::run(a),
109    }
110}
111
112#[cfg(test)]
113mod tests {
114    use super::*;
115
116    #[test]
117    fn the_build_step_is_dispatched_to_files() {
118        let args = <DatasetArgs as clap::Parser>::try_parse_from([
119            "geolith dataset",
120            "build",
121            "--config",
122            "/nonexistent/build-config.json",
123            "--rasters",
124            "/r",
125            "--areas",
126            "/a",
127            "--out",
128            "/d",
129        ])
130        .expect("dataset argv parses");
131        let err = dispatch(&args).expect_err("the config does not exist");
132        assert!(
133            geolith::error::report(&err).contains("/nonexistent/build-config.json"),
134            "{err:#}"
135        );
136    }
137
138    #[test]
139    fn the_settings_file_flag_goes_before_the_step_and_leaves_the_step_config_alone() {
140        let args = <DatasetArgs as clap::Parser>::try_parse_from([
141            "geolith dataset",
142            "--config",
143            "/etc/geolith.json",
144            "refresh",
145            "--dry-run",
146        ])
147        .expect("the settings flag parses before the step");
148        assert_eq!(
149            args.config,
150            Some(std::path::PathBuf::from("/etc/geolith.json"))
151        );
152        assert!(matches!(args.action, DatasetAction::Refresh(_)));
153        // `normals` keeps its own `--config`, the dataset config.
154        let args = <DatasetArgs as clap::Parser>::try_parse_from([
155            "geolith dataset",
156            "normals",
157            "--config",
158            "/etc/rainfall.json",
159            "--rasters",
160            "/r",
161            "--areas",
162            "/a",
163            "--slots",
164            "01-1",
165            "--out",
166            "/o",
167        ])
168        .expect("normals parses");
169        assert_eq!(args.config, None);
170        let DatasetAction::Normals(normals) = args.action else {
171            panic!("expected normals");
172        };
173        assert_eq!(
174            normals.config,
175            std::path::PathBuf::from("/etc/rainfall.json")
176        );
177    }
178
179    #[test]
180    fn the_values_step_is_dispatched_to_normals() {
181        let args = <DatasetArgs as clap::Parser>::try_parse_from([
182            "geolith dataset",
183            "values",
184            "--config",
185            "/nonexistent/values-config.json",
186            "--raster",
187            "/r/x.tif",
188            "--areas",
189            "/a",
190            "--cache",
191            "/c",
192        ])
193        .expect("dataset values argv parses");
194        let err = dispatch(&args).expect_err("the config does not exist");
195        assert!(
196            geolith::error::report(&err).contains("/nonexistent/values-config.json"),
197            "{err:#}"
198        );
199    }
200}