Skip to main content

powerio_matrix/io/
mod.rs

1//! File I/O: Matrix Market (`.mtx`) and JSON metadata, plus the gridfm-datakit
2//! Parquet export (`--features gridfm`).
3
4#[cfg(feature = "gridfm")]
5pub mod gridfm;
6pub mod meta;
7pub mod mtx;
8
9#[cfg(feature = "gridfm")]
10pub use gridfm::{
11    GridfmOptions, GridfmOutputs, GridfmRead, GridfmSnapshot, GridfmTables, gridfm_base_case,
12    gridfm_record_batches, gridfm_record_batches_batch, numbered_snapshots, read_gridfm_dataset,
13    read_gridfm_network, read_gridfm_scenarios, write_gridfm_batch, write_gridfm_dataset,
14};
15pub use meta::{CaseMetadata, MatrixMetadata, write_meta_json};
16pub use mtx::{read_mtx, read_vector_mtx, write_mtx, write_vector_mtx};
17
18/// Read one scenario of a dataset directory in the named `from` format: the
19/// directory sibling of [`powerio::parse_file`], and the one place dataset
20/// format names are dispatched (the C ABI's `pio_read_dir` is a thin wrapper).
21/// `gridfm` is the one dataset format today; `scenario` selects within it.
22/// PyPSA CSV directories are case inputs, not datasets, and parse through
23/// `parse_file`.
24///
25/// # Errors
26/// [`powerio::Error::UnknownFormat`] for a non-dataset format name; otherwise
27/// as [`read_gridfm_dataset`].
28#[cfg(feature = "gridfm")]
29pub fn read_dataset_dir(
30    dir: impl AsRef<std::path::Path>,
31    from: &str,
32    scenario: i64,
33) -> powerio::Result<GridfmRead> {
34    require_dataset_format(from)?;
35    read_gridfm_dataset(dir, scenario)
36}
37
38/// The distinct scenario ids of the dataset directory `dir` in the named
39/// `from` format, ascending, the introspection sibling of
40/// [`read_dataset_dir`] (and the C ABI's `pio_scenario_ids`).
41///
42/// # Errors
43/// As [`read_dataset_dir`].
44#[cfg(feature = "gridfm")]
45pub fn dataset_scenario_ids(
46    dir: impl AsRef<std::path::Path>,
47    from: &str,
48) -> powerio::Result<Vec<i64>> {
49    require_dataset_format(from)?;
50    gridfm::gridfm_scenario_ids(dir)
51}
52
53#[cfg(feature = "gridfm")]
54fn require_dataset_format(from: &str) -> powerio::Result<()> {
55    if from.eq_ignore_ascii_case("gridfm") {
56        return Ok(());
57    }
58    Err(powerio::Error::UnknownFormat(format!(
59        "{from} is not a dataset directory format (dataset formats: gridfm); \
60         PyPSA CSV directories parse through parse_file"
61    )))
62}