Skip to content

Commit 1e9fd2d

Browse files
committed
Use asset ID in output files
1 parent 542baff commit 1e9fd2d

2 files changed

Lines changed: 29 additions & 17 deletions

File tree

src/asset.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@ pub struct Asset {
3434
}
3535

3636
impl Asset {
37+
/// Get the asset's ID as a u32
38+
pub fn get_id(&self) -> Option<u32> {
39+
self.id.map(|AssetID(id)| id)
40+
}
41+
3742
/// Create a new [`Asset`].
3843
///
3944
/// The `id` field is initially set to `None`, but is changed to a unique value when the asset

src/output.rs

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -66,13 +66,11 @@ pub fn create_output_directory(output_dir: &Path) -> Result<()> {
6666
Ok(())
6767
}
6868

69-
/// Used to represent assets in assets output CSV file and other output files.
70-
///
71-
/// NB: It may be better to represent assets in these other files with IDs instead, see
72-
/// [#581](https://github.com/EnergySystemsModellingLab/MUSE_2.0/issues/581).
69+
/// Represents a row in the assets output CSV file.
7370
#[derive(Serialize, Deserialize, Debug, PartialEq)]
7471
struct AssetRow {
7572
milestone_year: u32,
73+
asset_id: u32,
7674
process_id: ProcessID,
7775
region_id: RegionID,
7876
agent_id: AgentID,
@@ -84,6 +82,7 @@ impl AssetRow {
8482
fn new(milestone_year: u32, asset: &Asset) -> Self {
8583
Self {
8684
milestone_year,
85+
asset_id: asset.get_id().unwrap(),
8786
process_id: asset.process.id.clone(),
8887
region_id: asset.region_id.clone(),
8988
agent_id: asset.agent_id.clone(),
@@ -93,10 +92,10 @@ impl AssetRow {
9392
}
9493

9594
/// Represents the flow-related data in a row of the commodity flows CSV file.
96-
///
97-
/// This will be written along with an [`AssetRow`] containing asset-related info.
9895
#[derive(Serialize, Deserialize, Debug, PartialEq)]
9996
struct CommodityFlowRow {
97+
milestone_year: u32,
98+
asset_id: u32,
10099
commodity_id: CommodityID,
101100
time_slice: TimeSliceID,
102101
flow: f64,
@@ -112,11 +111,11 @@ struct CommodityPriceRow {
112111
price: f64,
113112
}
114113

115-
/// Represents the capacity duals data in a row of the capacity duals CSV file.
116-
///
117-
/// This will be written along with an [`AssetRow`] containing asset-related info.
114+
/// Represents the capacity duals data in a row of the capacity duals CSV file
118115
#[derive(Serialize, Deserialize, Debug, PartialEq)]
119116
struct CapacityDualsRow {
117+
milestone_year: u32,
118+
asset_id: u32,
120119
time_slice: TimeSliceID,
121120
value: f64,
122121
}
@@ -182,13 +181,13 @@ impl DebugDataWriter {
182181
I: Iterator<Item = (&'a AssetRef, &'a TimeSliceID, f64)>,
183182
{
184183
for (asset, time_slice, value) in iter {
185-
let asset_row = AssetRow::new(milestone_year, asset);
186-
let dual_row = CapacityDualsRow {
184+
let row = CapacityDualsRow {
185+
milestone_year,
186+
asset_id: asset.get_id().unwrap(),
187187
time_slice: time_slice.clone(),
188188
value,
189189
};
190-
self.capacity_duals_writer
191-
.serialize((asset_row, dual_row))?;
190+
self.capacity_duals_writer.serialize(row)?;
192191
}
193192

194193
Ok(())
@@ -277,13 +276,14 @@ impl DataWriter {
277276
/// Write commodity flows to a CSV file
278277
pub fn write_flows(&mut self, milestone_year: u32, flow_map: &FlowMap) -> Result<()> {
279278
for ((asset, commodity_id, time_slice), flow) in flow_map {
280-
let asset_row = AssetRow::new(milestone_year, asset);
281-
let flow_row = CommodityFlowRow {
279+
let row = CommodityFlowRow {
280+
milestone_year,
281+
asset_id: asset.get_id().unwrap(),
282282
commodity_id: commodity_id.clone(),
283283
time_slice: time_slice.clone(),
284284
flow: *flow,
285285
};
286-
self.flows_writer.serialize((asset_row, flow_row))?;
286+
self.flows_writer.serialize(row)?;
287287
}
288288

289289
Ok(())
@@ -380,6 +380,8 @@ mod tests {
380380

381381
// Read back and compare
382382
let expected = CommodityFlowRow {
383+
milestone_year,
384+
asset_id: asset.get_id().unwrap(),
383385
commodity_id,
384386
time_slice,
385387
flow: 42.0,
@@ -482,7 +484,12 @@ mod tests {
482484
}
483485

484486
// Read back and compare
485-
let expected = CapacityDualsRow { time_slice, value };
487+
let expected = CapacityDualsRow {
488+
milestone_year,
489+
asset_id: asset.get_id().unwrap(),
490+
time_slice,
491+
value,
492+
};
486493
let records: Vec<CapacityDualsRow> =
487494
csv::Reader::from_path(dir.path().join(CAPACITY_DUALS_FILE_NAME))
488495
.unwrap()

0 commit comments

Comments
 (0)