geolith-dataset-builder 0.5.0

Raster datasets for geolith: district areas, normals, numbers and a map layer per period, and the steps that stage, verify and publish them to any S3-compatible bucket
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
//! The dataset subcommand surface.
//!
//! The engine verbs (`areas`, `normals`, `values`, `build`, `inspect`,
//! `downloads`) and the operator verbs (`stage`, `verify`, `status`, `keys`)
//! of `geolith dataset`, with their flags. Moved out of `src/cli.rs` so the
//! module owns its own command tree; `src/cli.rs` re-exports them.

use std::path::PathBuf;

use clap::{Args, Parser, Subcommand};

use crate::downloads::GeoParquetVersion;

/// Build a raster dataset: district areas, normals and per-period files.
#[derive(Debug, Parser)]
#[command(
    name = "geolith dataset",
    about = "raster dataset → district areas, normals, numbers and map layer",
    version
)]
pub struct DatasetArgs {
    /// An optional JSON deployment settings file (else `GEOLITH_CONFIG`; none
    /// by default): the object store, the publish URLs, the sources and the
    /// alerts. The process environment and the `.env` file (`GEOLITH_ENV_FILE`,
    /// else `./.env`) win over it. Give it before the step: the steps `normals`
    /// and `values` take their own `--config`, the dataset config.
    #[arg(long, value_name = "FILE")]
    pub config: Option<PathBuf>,

    /// The dataset step to run.
    #[command(subcommand)]
    pub action: DatasetAction,
}

/// A step of the raster dataset builder.
#[non_exhaustive]
#[derive(Debug, Subcommand)]
pub enum DatasetAction {
    /// Cut district areas from Overture divisions.
    Areas(AreasArgs),
    /// Compute the per-district normals of a baseline.
    Normals(NormalsArgs),
    /// Build the numbers and map layer of every period.
    Build(BuildArgs),
    /// Compute the area values of one raster into its values cache file.
    Values(ValuesArgs),
    /// Print a raster's layout and whether each read path decodes it.
    Inspect(InspectArgs),
    /// Write or check the Parquet and `GeoParquet` downloads of a stage.
    Downloads(DownloadsArgs),
    /// Stage an engine build as the upload-ready tree of object keys.
    Stage(StageArgs),
    /// Check a stage before it may be uploaded.
    Verify(VerifyArgs),
    /// Print the state of the newest staged build.
    Status(StatusArgs),
    /// Print the object keys an upload of a stage would write, from local files.
    Keys(KeysArgs),
    /// Upload a stage to the bucket, never overwriting a dated key.
    Upload(UploadArgs),
    /// Publish a stage's pointer, the only overwritten key.
    Publish(PublishArgs),
    /// Write the CHIRPS download lists from the host's directory listings.
    Lists(ListsArgs),
    /// Download a dataset's sources: CHIRPS rasters, boundaries, Overture.
    Fetch(FetchArgs),
    /// Stream the NDVI baseline one file at a time into the values cache.
    BaselinePass(BaselinePassArgs),
    /// The Copernicus NDVI dekads: the catalogue lists and the pixel window.
    Cdse(CdseArgs),
    /// The official LGD lists the district crosswalk reads.
    Lgd(LgdArgs),
    /// Build the India district crosswalk (Overture ids ↔ LGD codes).
    Crosswalk(CrosswalkArgs),
    /// Print the inputs record of an engine build, or the newest period.
    Inputs(InputsArgs),
    /// Download, build and verify one dataset, then stop before upload.
    Run(RunArgs),
    /// Delete the source rasters and stages the pipeline no longer reads.
    Prune(PruneArgs),
    /// Check the new period of a stage against its slot history.
    Bounds(BoundsArgs),
    /// Write the measured per-slot table the bounds gate reads.
    Slots(SlotsArgs),
    /// Compare a stage's values with GDAL, the optional external oracle.
    Accuracy(AccuracyArgs),
    /// Install, inspect or remove the daily refresh job.
    Schedule(ScheduleArgs),
    /// Decide, build and publish every published dataset, one pair at a time.
    Refresh(RefreshArgs),
}

/// Flags of `geolith dataset downloads`.
#[derive(Debug, Args)]
pub struct DownloadsArgs {
    /// The download to write, or `check`.
    #[command(subcommand)]
    pub action: DownloadsAction,
}

/// A step of `geolith dataset downloads`.
#[non_exhaustive]
#[derive(Debug, Subcommand)]
pub enum DownloadsAction {
    /// Write `numbers.parquet` from a build's `numbers.csv` and `index.json`.
    Numbers(NumbersDownloadArgs),
    /// Write `areas.parquet` (`GeoParquet` 2.0.0, `--geoparquet 1.1` for
    /// 1.1.0) and `LICENSE.txt` from `areas.geojson`.
    Areas(AreasDownloadArgs),
    /// Print rows, columns, metadata keys and attribution of a Parquet file
    /// as JSON.
    Check(CheckDownloadArgs),
}

/// Flags of `geolith dataset downloads numbers`.
#[derive(Debug, Args)]
pub struct NumbersDownloadArgs {
    /// `numbers.csv` of the build.
    #[arg(long)]
    pub csv: PathBuf,

    /// `index.json` of the build (attribution, licence and comparison).
    #[arg(long)]
    pub index: PathBuf,

    /// The data source URL, the `source` of the build's area JSON; written
    /// as the `source_url` metadata.
    #[arg(long)]
    pub source_url: String,

    /// Output `numbers.parquet` path.
    #[arg(long)]
    pub out: PathBuf,
}

/// Flags of `geolith dataset downloads areas`.
#[derive(Debug, Args)]
pub struct AreasDownloadArgs {
    /// Areas `GeoJSON` written by `geolith dataset areas`.
    #[arg(long)]
    pub areas: PathBuf,

    /// Overture release of the areas.
    #[arg(long)]
    pub release: String,

    /// Country config; its attribution, licence id and licence file replace
    /// the built-in ODbL ones.
    #[arg(long)]
    pub country_config: Option<PathBuf>,

    /// Output directory for `areas.parquet` and `LICENSE.txt`.
    #[arg(long)]
    pub out: PathBuf,

    /// `GeoParquet` version of `areas.parquet`: `2.0` writes the native
    /// Parquet `GEOMETRY` logical type and per-row-group geospatial
    /// statistics, `1.1` a plain WKB `Binary` column.
    #[arg(long, value_enum, default_value = "2.0")]
    pub geoparquet: GeoParquetVersion,
}

/// Flags of `geolith dataset downloads check`.
#[derive(Debug, Args)]
pub struct CheckDownloadArgs {
    /// The Parquet file to read back.
    pub file: PathBuf,
}

/// Flags of `geolith dataset areas`.
#[derive(Debug, Args)]
pub struct AreasArgs {
    /// Overture release directory holding the divisions Parquet.
    #[arg(long)]
    pub overture: Option<PathBuf>,

    /// Country config; a `shapefile` source reads `--boundaries-dir` instead
    /// of Overture.
    #[arg(long)]
    pub country_config: Option<PathBuf>,

    /// Directory holding the unzipped boundary files of a `shapefile`
    /// country.
    #[arg(long)]
    pub boundaries_dir: Option<PathBuf>,

    /// Comma-separated ISO country codes to cover, e.g. `IN`; taken from the
    /// country config when absent.
    #[arg(long)]
    pub countries: Option<String>,

    /// Overture division subtype used as the area unit.
    #[arg(long, default_value = "county")]
    pub subtype: String,

    /// Survey of India outline; required when the countries include `IN`,
    /// `PK`, `CN` or an `X*` code.
    #[arg(long)]
    pub india_outline: Option<PathBuf>,

    /// Overture release name recorded in the outputs.
    #[arg(long)]
    pub release: Option<String>,

    /// Maximum zoom of the areas tiles.
    #[arg(long, default_value = "8")]
    pub max_zoom: u8,

    /// Comma-separated coded Indian regions allowed to hold no district (the
    /// explicit allowlist of the India check; none by default).
    #[arg(long)]
    pub allow_districtless: Option<String>,

    /// Output directory.
    #[arg(long)]
    pub out: PathBuf,
}

/// Flags of `geolith dataset normals`.
#[derive(Debug, Args)]
pub struct NormalsArgs {
    /// Dataset config JSON.
    #[arg(long)]
    pub config: PathBuf,

    /// Directory of source rasters.
    #[arg(long)]
    pub rasters: PathBuf,

    /// Areas `GeoJSON` written by `geolith dataset areas`.
    #[arg(long)]
    pub areas: PathBuf,

    /// Baseline years, `FIRST-LAST`.
    #[arg(long, default_value = "1991-2020")]
    pub baseline: String,

    /// Comma-separated period slots, each `MM-P`.
    #[arg(long)]
    pub slots: String,

    /// Output normals CSV path.
    #[arg(long)]
    pub out: PathBuf,

    /// Values cache directory: each raster's area values are read from
    /// `<dir>/<raster file stem>.values.csv` when present, else computed and
    /// written there.
    #[arg(long)]
    pub cache: Option<PathBuf>,

    /// Treat an area the baseline rasters never reach as excluded instead of
    /// fatal: a country whose raster stops short of its far north (the United
    /// States: CHIRPS ends at 50°N) declares its coverage floor in the country
    /// config, and this flag carries that decision to the normals.
    #[arg(long)]
    pub allow_empty: bool,
}

/// Flags of `geolith dataset values`.
#[derive(Debug, Args)]
pub struct ValuesArgs {
    /// Dataset config JSON.
    #[arg(long)]
    pub config: PathBuf,

    /// The one source raster.
    #[arg(long)]
    pub raster: PathBuf,

    /// Areas `GeoJSON` written by `geolith dataset areas`.
    #[arg(long)]
    pub areas: PathBuf,

    /// Values cache directory; the file written is
    /// `<dir>/<raster file stem>.values.csv`.
    #[arg(long)]
    pub cache: PathBuf,
}

/// Flags of `geolith dataset inspect`.
#[derive(Debug, Args)]
pub struct InspectArgs {
    /// The raster (`GeoTIFF` or COG) to inspect.
    pub file: PathBuf,

    /// EPSG code of the grid, for files without geokeys.
    #[arg(long)]
    pub crs_epsg: Option<u32>,
}

/// Flags of `geolith dataset build`.
#[derive(Debug, Args)]
pub struct BuildArgs {
    /// Dataset config JSON.
    #[arg(long)]
    pub config: PathBuf,

    /// Directory of source rasters.
    #[arg(long)]
    pub rasters: PathBuf,

    /// Areas `GeoJSON` written by `geolith dataset areas`.
    #[arg(long)]
    pub areas: PathBuf,

    /// Normals CSV written by `geolith dataset normals`.
    #[arg(long)]
    pub normals: Option<PathBuf>,

    /// Survey of India outline that clips the map layer over India.
    #[arg(long)]
    pub india_outline: Option<PathBuf>,

    /// CSV of the published districts (`region_code,district,capital`);
    /// fills `index.json` `published`.
    #[arg(long)]
    pub published: Option<PathBuf>,

    /// District crosswalk CSV (Overture id ↔ LGD code); fills `lgd_code`
    /// and `approximate`.
    #[arg(long)]
    pub crosswalk: Option<PathBuf>,

    /// Periods with a map layer, the latest included (1 to 12); the
    /// config's `layer_periods` (else 1) when absent.
    #[arg(long)]
    pub layer_periods: Option<u8>,

    /// Country config; its dataset limits and boundary attribution fill what
    /// the flags leave out.
    #[arg(long)]
    pub country_config: Option<PathBuf>,

    /// Map bounds `min_lon,min_lat,max_lon,max_lat`; wins over the country
    /// config.
    #[arg(long)]
    pub bounds: Option<String>,

    /// Areas covered below this percentage get no page for this dataset;
    /// wins over the country config.
    #[arg(long)]
    pub min_coverage: Option<f64>,

    /// Acknowledgement of the area codes, printed by every page; wins over
    /// the country config.
    #[arg(long)]
    pub codes_attribution: Option<String>,

    /// Output directory.
    #[arg(long)]
    pub out: PathBuf,
}

/// Flags of `geolith dataset stage`.
#[derive(Debug, Args)]
pub struct StageArgs {
    /// The engine build directory to stage.
    pub engine_build_dir: PathBuf,

    /// Dataset id (`DATASET`, else `rainfall`).
    #[arg(long)]
    pub dataset: Option<String>,

    /// ISO country code (`COUNTRY`, else `in`).
    #[arg(long)]
    pub country: Option<String>,
}

/// Flags of `geolith dataset verify`.
#[derive(Debug, Args)]
pub struct VerifyArgs {
    /// Build id; the newest complete stage when absent.
    pub build_id: Option<String>,

    /// Dataset id (`DATASET`, else `rainfall`).
    #[arg(long)]
    pub dataset: Option<String>,

    /// ISO country code (`COUNTRY`, else `in`).
    #[arg(long)]
    pub country: Option<String>,
}

/// Flags of `geolith dataset status`.
#[derive(Debug, Args)]
pub struct StatusArgs {
    /// Dataset id (`DATASET`, else `rainfall`).
    #[arg(long)]
    pub dataset: Option<String>,

    /// ISO country code (`COUNTRY`, else `in`).
    #[arg(long)]
    pub country: Option<String>,
}

/// Flags of `geolith dataset keys`.
#[derive(Debug, Args)]
pub struct KeysArgs {
    /// Build id of the stage.
    pub build_id: String,

    /// Dataset id (`DATASET`, else `rainfall`).
    #[arg(long)]
    pub dataset: Option<String>,

    /// ISO country code (`COUNTRY`, else `in`).
    #[arg(long)]
    pub country: Option<String>,
}

/// Flags of `geolith dataset upload`.
#[derive(Debug, Args)]
pub struct UploadArgs {
    /// Build id of the stage.
    pub build_id: String,

    /// Print the plan; write nothing.
    #[arg(long)]
    pub dry_run: bool,

    /// The bucket to write to: only `storage.bucket` is accepted, so the flag
    /// confirms it.
    #[arg(long)]
    pub bucket: Option<String>,

    /// Dataset id (`DATASET`, else `rainfall`).
    #[arg(long)]
    pub dataset: Option<String>,

    /// ISO country code (`COUNTRY`, else `in`).
    #[arg(long)]
    pub country: Option<String>,
}

/// Flags of `geolith dataset publish`.
#[derive(Debug, Args)]
pub struct PublishArgs {
    /// Build id of the stage.
    pub build_id: String,

    /// Print the plan; write nothing.
    #[arg(long)]
    pub dry_run: bool,

    /// The bucket to write to: only `storage.bucket` is accepted, so the flag
    /// confirms it.
    #[arg(long)]
    pub bucket: Option<String>,

    /// Return the pointer to a build that was live (the refresh path only).
    #[arg(long)]
    pub rollback: bool,

    /// Dataset id (`DATASET`, else `rainfall`).
    #[arg(long)]
    pub dataset: Option<String>,

    /// ISO country code (`COUNTRY`, else `in`).
    #[arg(long)]
    pub country: Option<String>,
}

/// Flags of `geolith dataset lists`.
#[derive(Debug, Args)]
pub struct ListsArgs {
    /// The publisher's final-pentads listing (HTML or a plain list of names).
    #[arg(long)]
    pub listing_final: Option<std::path::PathBuf>,

    /// The publisher's preliminary-pentads listing.
    #[arg(long)]
    pub listing_prelim: Option<std::path::PathBuf>,

    /// First day of the season, `MM-DD` on a pentad start.
    #[arg(long)]
    pub season_start: Option<String>,

    /// Baseline years, `FIRST-LAST`; `1991-2020` when absent.
    #[arg(long)]
    pub baseline: Option<String>,

    /// Output directory of `series.txt`, `season.txt` and `baseline.txt`.
    #[arg(long)]
    pub out: Option<std::path::PathBuf>,
}

/// Flags of `geolith dataset fetch`.
#[derive(Debug, Args)]
pub struct FetchArgs {
    /// Also download the baseline years (slow: 2160 files).
    #[arg(long)]
    pub baseline: bool,

    /// Download the country's boundary archives and unzip them instead.
    #[arg(long)]
    pub boundaries: bool,

    /// Raster mode of the NDVI dekads: `clip` (the country window, the
    /// default) or `full` (the whole file).
    #[arg(long)]
    pub mode: Option<String>,

    /// ISO country code (`COUNTRY`, else `in`).
    #[arg(long, default_value = "in")]
    pub country: String,

    /// Dataset id (`DATASET`, else `rainfall`).
    #[arg(long)]
    pub dataset: Option<String>,
}

/// Flags of `geolith dataset baseline-pass`.
#[derive(Debug, Args)]
pub struct BaselinePassArgs {
    /// Raster mode of the baseline files: `clip` (the country window, the
    /// default, through GDAL) or `full` (the whole file, in Rust).
    #[arg(long)]
    pub mode: Option<String>,

    /// ISO country code (`COUNTRY`, else `in`).
    #[arg(long, default_value = "in")]
    pub country: String,

    /// Dataset id (`DATASET`, else `rainfall`).
    #[arg(long)]
    pub dataset: Option<String>,

    /// Free-space floor in GB, kept free before each file (`DATASETS_MIN_FREE_GB`,
    /// else 100).
    #[arg(long)]
    pub min_free_gb: Option<u64>,
}

/// Flags of `geolith dataset cdse`.
#[derive(Debug, Args)]
pub struct CdseArgs {
    /// The CDSE step to run.
    #[command(subcommand)]
    pub action: CdseAction,
}

/// A step of `geolith dataset cdse`.
#[non_exhaustive]
#[derive(Debug, Subcommand)]
pub enum CdseAction {
    /// List the NDVI dekads the STAC catalogue holds, oldest first.
    List(CdseListArgs),
    /// Print the S3 prefixes of the dekads the catalogue lacks.
    Missing(CdseMissingArgs),
    /// Add the dekads of a direct S3 listing to a catalogue list.
    Merge(CdseMergeArgs),
    /// Print `<key> <bytes>` of every object under each prefix, from S3.
    Ls(CdseLsArgs),
    /// Print the range of the current dekad and the 12 before it.
    SeriesRange(CdseSeriesRangeArgs),
    /// Print the pixel window of a country's areas on the NDVI grid.
    Window(CdseWindowArgs),
}

/// Flags of `geolith dataset cdse list`.
#[derive(Debug, Args)]
pub struct CdseListArgs {
    /// First day of the range.
    #[arg(long)]
    pub from: String,

    /// Last day of the range.
    #[arg(long)]
    pub to: String,

    /// STAC root (`CDSE_STAC`, else the public catalogue).
    #[arg(long)]
    pub stac: Option<String>,
}

/// Flags of `geolith dataset cdse missing`.
#[derive(Debug, Args)]
pub struct CdseMissingArgs {
    /// The list `cdse list` wrote.
    #[arg(long)]
    pub list: std::path::PathBuf,

    /// First day of the range.
    #[arg(long)]
    pub from: String,

    /// Last day of the range.
    #[arg(long)]
    pub to: String,

    /// Today (`CDSE_TODAY`, else UTC today).
    #[arg(long)]
    pub today: Option<String>,
}

/// Flags of `geolith dataset cdse merge`.
#[derive(Debug, Args)]
pub struct CdseMergeArgs {
    /// The list `cdse list` wrote.
    #[arg(long)]
    pub list: std::path::PathBuf,

    /// The direct S3 listing (`<key> <bytes>` lines).
    #[arg(long)]
    pub s3: std::path::PathBuf,
}

/// Flags of `geolith dataset cdse ls`.
#[derive(Debug, Args)]
pub struct CdseLsArgs {
    /// The prefixes to list, one per line, relative to `eodata/`.
    #[arg(long)]
    pub prefixes: std::path::PathBuf,
}

/// Flags of `geolith dataset cdse series-range`.
#[derive(Debug, Args)]
pub struct CdseSeriesRangeArgs {
    /// Today (`CDSE_TODAY`, else UTC today).
    #[arg(long)]
    pub today: Option<String>,
}

/// Flags of `geolith dataset cdse window`.
#[derive(Debug, Args)]
pub struct CdseWindowArgs {
    /// Areas `GeoJSON` written by `geolith dataset areas`.
    #[arg(long)]
    pub areas: std::path::PathBuf,

    /// ISO country code of the areas (`IN`).
    #[arg(long, default_value = "IN")]
    pub country: String,

    /// Survey of India outline; required for `IN`.
    #[arg(long)]
    pub outline: Option<std::path::PathBuf>,

    /// `<x0>,<y0>,<px>,<py>,<width>,<height>`; the NDVI grid when absent.
    #[arg(long)]
    pub grid: Option<String>,

    /// Block size the window is snapped to.
    #[arg(long, default_value = "512")]
    pub block: u32,
}

/// Flags of `geolith dataset lgd`.
#[derive(Debug, Args)]
pub struct LgdArgs {
    /// The LGD step to run.
    #[command(subcommand)]
    pub action: LgdAction,

    /// LGD directory (`LGD_DIR`, else `$DATA_ROOT/datasets/lgd`).
    #[arg(long, global = true)]
    pub dir: Option<std::path::PathBuf>,
}

/// A step of `geolith dataset lgd`.
#[non_exhaustive]
#[derive(Debug, clap::Subcommand)]
pub enum LgdAction {
    /// Parse the LGD state page into `states.csv`.
    States(LgdStatesArgs),
    /// Check the owner-saved district list and record it.
    Districts(LgdDistrictsArgs),
}

/// Flags of `geolith dataset lgd states`.
#[derive(Debug, Args)]
pub struct LgdStatesArgs {
    /// Refetch and rewrite `states.csv` even when it exists.
    #[arg(long)]
    pub force: bool,

    /// Parse a saved copy of the page instead of fetching it.
    #[arg(long)]
    pub html: Option<std::path::PathBuf>,
}

/// Flags of `geolith dataset lgd districts`.
#[derive(Debug, Args)]
pub struct LgdDistrictsArgs {
    /// The time the owner downloaded the list, ISO 8601 with an offset.
    #[arg(long)]
    pub downloaded_at: Option<String>,
}

/// Flags of `geolith dataset crosswalk`.
#[derive(Debug, Args)]
pub struct CrosswalkArgs {
    /// Areas GeoJSON (`$DATA_ROOT/datasets/boundaries/in/<release>/areas.geojson`).
    #[arg(long)]
    pub areas: Option<std::path::PathBuf>,

    /// LGD directory holding `states.csv` and `districts.csv`.
    #[arg(long)]
    pub lgd: Option<std::path::PathBuf>,

    /// Reviewed aliases CSV.
    #[arg(long)]
    pub aliases: Option<std::path::PathBuf>,

    /// Output crosswalk CSV.
    #[arg(long)]
    pub out: Option<std::path::PathBuf>,

    /// Output summary markdown.
    #[arg(long)]
    pub summary: Option<std::path::PathBuf>,
}

/// Flags of `geolith dataset inputs`.
#[derive(Debug, Args)]
pub struct InputsArgs {
    /// Dataset config JSON.
    pub config: std::path::PathBuf,

    /// Directory of source rasters.
    pub src: std::path::PathBuf,

    /// Print the newest period on disk instead of the record.
    #[arg(long)]
    pub latest: bool,

    /// The engine binary the record hashes.
    pub engine: Option<std::path::PathBuf>,

    /// The published list the record hashes.
    pub published: Option<std::path::PathBuf>,

    /// The normals CSV the record hashes.
    pub normals: Option<std::path::PathBuf>,

    /// The areas GeoJSON the record hashes.
    pub areas: Option<std::path::PathBuf>,

    /// The crosswalk CSV the record hashes, when the build read one.
    pub crosswalk: Option<std::path::PathBuf>,
}

/// Flags of `geolith dataset run`.
#[derive(Debug, Args)]
pub struct RunArgs {
    /// Print what each step would do; fetch, build and write nothing.
    #[arg(long)]
    pub dry_run: bool,

    /// Always run a new engine build, even when the inputs are unchanged.
    #[arg(long)]
    pub rebuild: bool,

    /// Dataset id (`DATASET`, else `rainfall`).
    #[arg(long)]
    pub dataset: Option<String>,

    /// ISO country code (`COUNTRY`, else `in`).
    #[arg(long)]
    pub country: Option<String>,
}

/// Flags of `geolith dataset prune`.
#[derive(Debug, Args)]
pub struct PruneArgs {
    /// Print what would be deleted; delete nothing.
    #[arg(long)]
    pub dry_run: bool,
}

/// Flags of `geolith dataset bounds`.
#[derive(Debug, Args)]
pub struct BoundsArgs {
    /// The staged build to check.
    #[arg(long)]
    pub build: String,

    /// Dataset id (`DATASET`, else `rainfall`).
    #[arg(long)]
    pub dataset: Option<String>,

    /// ISO country code (`COUNTRY`, else `in`).
    #[arg(long)]
    pub country: Option<String>,
}

/// Flags of `geolith dataset slots`.
#[derive(Debug, Args)]
pub struct SlotsArgs {
    /// The values cache `geolith dataset values` filled.
    #[arg(long)]
    pub cache: PathBuf,

    /// The table to write; the committed `<dataset>-slots[-<cc>].json` when
    /// absent.
    #[arg(long)]
    pub out: Option<PathBuf>,

    /// Dataset id (`DATASET`, else `rainfall`).
    #[arg(long)]
    pub dataset: Option<String>,

    /// ISO country code (`COUNTRY`, else `in`).
    #[arg(long)]
    pub country: Option<String>,
}

/// Flags of `geolith dataset accuracy`.
#[derive(Debug, Args)]
pub struct AccuracyArgs {
    /// The staged build to compare.
    #[arg(long)]
    pub build: String,

    /// Dataset id (`DATASET`, else `rainfall`).
    #[arg(long)]
    pub dataset: Option<String>,

    /// ISO country code (`COUNTRY`, else `in`).
    #[arg(long)]
    pub country: Option<String>,
}

/// Flags of `geolith dataset schedule`.
#[derive(Debug, Args)]
pub struct ScheduleArgs {
    /// What to do with the daily refresh job.
    #[command(subcommand)]
    pub action: ScheduleAction,
}

/// A schedule action.
#[non_exhaustive]
#[derive(Debug, Subcommand)]
pub enum ScheduleAction {
    /// Install the binary and the daily job, and load it.
    Install,
    /// Print the job's state, its binary's digest and the last history lines.
    Status,
    /// Unload the job and remove its definition, leaving the binary.
    Uninstall,
}

/// Flags of `geolith dataset refresh`.
#[derive(Debug, Args)]
pub struct RefreshArgs {
    /// List the sources and write the history line; build, upload and publish
    /// nothing.
    #[arg(long)]
    pub dry_run: bool,

    /// Only this dataset (every published dataset by default).
    #[arg(long)]
    pub dataset: Option<String>,

    /// Only this country (`COUNTRY` when set, else every country config).
    #[arg(long)]
    pub country: Option<String>,
}