@@ -4,7 +4,7 @@ use super::super::*;
44use super :: demand_slicing:: { read_demand_slices, DemandSliceMap } ;
55use crate :: commodity:: { Commodity , CommodityID , CommodityType , DemandMap } ;
66use crate :: id:: IDCollection ;
7- use crate :: region:: { parse_region_str , RegionID } ;
7+ use crate :: region:: RegionID ;
88use crate :: time_slice:: TimeSliceInfo ;
99use crate :: year:: parse_year_str;
1010use anyhow:: { ensure, Result } ;
@@ -19,8 +19,8 @@ const DEMAND_FILE_NAME: &str = "demand.csv";
1919struct Demand {
2020 /// The commodity this demand entry refers to
2121 commodity_id : String ,
22- /// The region(s) of the demand entry
23- regions : String ,
22+ /// The region of the demand entry
23+ region_id : String ,
2424 /// The year(s) of the demand entry
2525 years : String ,
2626 /// Annual demand quantity
@@ -118,21 +118,19 @@ where
118118 demand. commodity_id
119119 )
120120 } ) ?;
121+ let region_id = region_ids. get_id_by_str ( & demand. region_id ) ?;
121122
122123 ensure ! (
123124 demand. demand. is_normal( ) && demand. demand > 0.0 ,
124125 "Demand must be a valid number greater than zero"
125126 ) ;
126127
127- let years = parse_year_str ( & demand. years , milestone_years) ?;
128- for region_id in parse_region_str ( & demand. regions , region_ids) ? {
129- for & year in years. iter ( ) {
130- try_insert (
131- & mut map,
132- ( commodity_id. clone ( ) , region_id. clone ( ) , year) ,
133- demand. demand ,
134- ) ?;
135- }
128+ for year in parse_year_str ( & demand. years , milestone_years) ? {
129+ try_insert (
130+ & mut map,
131+ ( commodity_id. clone ( ) , region_id. clone ( ) , year) ,
132+ demand. demand ,
133+ ) ?;
136134 }
137135 }
138136
@@ -217,13 +215,13 @@ mod tests {
217215 let demand = [
218216 Demand {
219217 years : "2020" . into ( ) ,
220- regions : "GBR" . to_string ( ) ,
218+ region_id : "GBR" . to_string ( ) ,
221219 commodity_id : "commodity1" . to_string ( ) ,
222220 demand : 10.0 ,
223221 } ,
224222 Demand {
225223 years : "2020" . into ( ) ,
226- regions : "USA" . to_string ( ) ,
224+ region_id : "USA" . to_string ( ) ,
227225 commodity_id : "commodity1" . to_string ( ) ,
228226 demand : 11.0 ,
229227 } ,
@@ -244,13 +242,13 @@ mod tests {
244242 let demand = [
245243 Demand {
246244 years : "2020" . into ( ) ,
247- regions : "GBR" . to_string ( ) ,
245+ region_id : "GBR" . to_string ( ) ,
248246 commodity_id : "commodity2" . to_string ( ) ,
249247 demand : 10.0 ,
250248 } ,
251249 Demand {
252250 years : "2020" . into ( ) ,
253- regions : "USA" . to_string ( ) ,
251+ region_id : "USA" . to_string ( ) ,
254252 commodity_id : "commodity1" . to_string ( ) ,
255253 demand : 11.0 ,
256254 } ,
@@ -270,13 +268,13 @@ mod tests {
270268 let demand = [
271269 Demand {
272270 years : "2020" . into ( ) ,
273- regions : "FRA" . to_string ( ) ,
271+ region_id : "FRA" . to_string ( ) ,
274272 commodity_id : "commodity1" . to_string ( ) ,
275273 demand : 10.0 ,
276274 } ,
277275 Demand {
278276 years : "2020" . into ( ) ,
279- regions : "USA" . to_string ( ) ,
277+ region_id : "USA" . to_string ( ) ,
280278 commodity_id : "commodity1" . to_string ( ) ,
281279 demand : 11.0 ,
282280 } ,
@@ -296,13 +294,13 @@ mod tests {
296294 let demand = [
297295 Demand {
298296 years : "2010" . into ( ) ,
299- regions : "GBR" . to_string ( ) ,
297+ region_id : "GBR" . to_string ( ) ,
300298 commodity_id : "commodity1" . to_string ( ) ,
301299 demand : 10.0 ,
302300 } ,
303301 Demand {
304302 years : "2020" . into ( ) ,
305- regions : "USA" . to_string ( ) ,
303+ region_id : "USA" . to_string ( ) ,
306304 commodity_id : "commodity1" . to_string ( ) ,
307305 demand : 11.0 ,
308306 } ,
@@ -327,7 +325,7 @@ mod tests {
327325 // Bad demand quantity
328326 let demand = [ Demand {
329327 years : "2020" . into ( ) ,
330- regions : "GBR" . to_string ( ) ,
328+ region_id : "GBR" . to_string ( ) ,
331329 commodity_id : "commodity1" . to_string ( ) ,
332330 demand : quantity,
333331 } ] ;
@@ -346,19 +344,19 @@ mod tests {
346344 let demand = [
347345 Demand {
348346 years : "2020" . into ( ) ,
349- regions : "GBR" . to_string ( ) ,
347+ region_id : "GBR" . to_string ( ) ,
350348 commodity_id : "commodity1" . to_string ( ) ,
351349 demand : 10.0 ,
352350 } ,
353351 Demand {
354352 years : "2020" . into ( ) ,
355- regions : "GBR" . to_string ( ) ,
353+ region_id : "GBR" . to_string ( ) ,
356354 commodity_id : "commodity1" . to_string ( ) ,
357355 demand : 10.0 ,
358356 } ,
359357 Demand {
360358 years : "2020" . into ( ) ,
361- regions : "USA" . to_string ( ) ,
359+ region_id : "USA" . to_string ( ) ,
362360 commodity_id : "commodity1" . to_string ( ) ,
363361 demand : 11.0 ,
364362 } ,
@@ -378,21 +376,21 @@ mod tests {
378376 let demand = [
379377 Demand {
380378 years : "2020" . into ( ) ,
381- regions : "GBR" . to_string ( ) ,
379+ region_id : "GBR" . to_string ( ) ,
382380 commodity_id : "commodity1" . to_string ( ) ,
383381 demand : 10.0 ,
384382 } ,
385383 Demand {
386384 years : "2020" . into ( ) ,
387- regions : "USA" . to_string ( ) ,
385+ region_id : "USA" . to_string ( ) ,
388386 commodity_id : "commodity1" . to_string ( ) ,
389387 demand : 11.0 ,
390388 } ,
391389 Demand {
392390 years : "2030" . into ( ) ,
393- regions : "USA " . to_string ( ) ,
391+ region_id : "GBR " . to_string ( ) ,
394392 commodity_id : "commodity1" . to_string ( ) ,
395- demand : 5 .0,
393+ demand : 10 .0,
396394 } ,
397395 ] ;
398396 assert_error ! (
@@ -402,7 +400,7 @@ mod tests {
402400 & region_ids,
403401 & [ 2020 , 2030 ]
404402 ) ,
405- "Commodity commodity1 is missing demand data for [(RegionID(\" GBR \" ), 2030)]"
403+ "Commodity commodity1 is missing demand data for [(RegionID(\" USA \" ), 2030)]"
406404 ) ;
407405 }
408406
@@ -412,7 +410,7 @@ mod tests {
412410 let mut file = File :: create ( file_path) . unwrap ( ) ;
413411 writeln ! (
414412 file,
415- "commodity_id,regions ,years,demand\n \
413+ "commodity_id,region_id ,years,demand\n \
416414 commodity1,GBR,2020,10\n \
417415 commodity1,USA,all,11\n "
418416 )
0 commit comments