22from pathlib import Path
33from unittest .mock import patch
44
5+ import numpy as np
56import toml
67import xarray as xr
78from pytest import fixture , mark , raises
@@ -391,6 +392,10 @@ def test_read_existing_trade(tmp_path):
391392
392393 assert isinstance (data , xr .DataArray )
393394 assert set (data .dims ) == {"year" , "technology" , "dst_region" , "region" }
395+ assert list (data .coords ["year" ].values ) == [2010 , 2020 , 2030 , 2040 , 2050 ]
396+ assert list (data .coords ["technology" ].values ) == ["gassupply1" ]
397+ assert list (data .coords ["dst_region" ].values ) == ["R1" , "R2" ]
398+ assert list (data .coords ["region" ].values ) == ["R1" , "R2" ]
394399
395400
396401def test_read_trade_technodata (tmp_path ):
@@ -412,6 +417,296 @@ def test_read_trade_technodata(tmp_path):
412417 "max_capacity_growth" ,
413418 "total_capacity_limit" ,
414419 }
420+ assert all (val == np .float64 for val in data .dtypes .values ())
421+ assert list (data .coords ["dst_region" ].values ) == ["R1" , "R2" ]
422+ assert list (data .coords ["technology" ].values ) == ["gassupply1" ]
423+ assert list (data .coords ["region" ].values ) == ["R1" , "R2" , "R3" ]
424+ assert all (var .coords .equals (data .coords ) for var in data .data_vars .values ())
425+
426+
427+ @fixture
428+ def default_model (tmp_path ):
429+ from muse .examples import copy_model
430+
431+ copy_model ("default" , tmp_path )
432+ return tmp_path / "model"
433+
434+
435+ def test_read_technodictionary (default_model ):
436+ from muse .readers .csv import read_technodictionary
437+
438+ path = default_model / "technodata" / "residential" / "Technodata.csv"
439+ data = read_technodictionary (path )
440+ assert isinstance (data , xr .Dataset )
441+ assert set (data .dims ) == {"technology" , "region" }
442+
443+ assert dict (data .dtypes ) == dict (
444+ level = np .dtype ("O" ),
445+ cap_par = np .dtype ("float64" ),
446+ cap_exp = np .dtype ("int64" ),
447+ fix_par = np .dtype ("int64" ),
448+ fix_exp = np .dtype ("int64" ),
449+ var_par = np .dtype ("int64" ),
450+ interest_rate = np .dtype ("float64" ),
451+ type = np .dtype ("O" ),
452+ fuel = np .dtype ("<U11" ),
453+ enduse = np .dtype ("<U4" ),
454+ agent_share_2 = np .dtype ("int64" ),
455+ tech_type = np .dtype ("<U6" ),
456+ efficiency = np .dtype ("int64" ),
457+ max_capacity_addition = np .dtype ("int64" ),
458+ max_capacity_growth = np .dtype ("float64" ),
459+ scaling_size = np .dtype ("float64" ),
460+ technical_life = np .dtype ("int64" ),
461+ total_capacity_limit = np .dtype ("int64" ),
462+ utilization_factor = np .dtype ("int64" ),
463+ var_exp = np .dtype ("int64" ),
464+ )
465+ assert list (data .coords ["technology" ].values ) == ["gasboiler" , "heatpump" ]
466+ assert list (data .coords ["region" ].values ) == ["R1" ]
467+
468+ for var in data .data_vars :
469+ if var in ("fuel" , "enduse" , "tech_type" ):
470+ assert list (data .data_vars [var ].coords ) == ["technology" ]
471+ else :
472+ assert data .data_vars [var ].coords .equals (data .coords )
473+
474+
475+ def test_read_technodata_timeslices (tmp_path ):
476+ from muse .examples import copy_model
477+ from muse .readers .csv import read_technodata_timeslices
478+
479+ copy_model ("default_timeslice" , tmp_path )
480+ path = tmp_path / "model" / "technodata" / "power" / "TechnodataTimeslices.csv"
481+ data = read_technodata_timeslices (path )
482+
483+ assert isinstance (data , xr .Dataset )
484+ assert set (data .dims ) == {"technology" , "region" , "year" , "timeslice" }
485+ assert dict (data .dtypes ) == dict (
486+ utilization_factor = np .int64 ,
487+ minimum_service_factor = np .int64 ,
488+ )
489+ assert list (data .coords ["technology" ].values ) == ["gasCCGT" , "windturbine" ]
490+ assert list (data .coords ["region" ].values ) == ["R1" ]
491+ assert list (data .coords ["year" ].values ) == [2020 ]
492+ month_values = ["all-year" ] * 6
493+ day_values = ["all-week" ] * 6
494+ hour_values = [
495+ "afternoon" ,
496+ "early-peak" ,
497+ "evening" ,
498+ "late-peak" ,
499+ "morning" ,
500+ "night" ,
501+ ]
502+
503+ assert list (data .coords ["timeslice" ].values ) == list (
504+ zip (month_values , day_values , hour_values )
505+ )
506+ assert list (data .coords ["month" ]) == month_values
507+ assert list (data .coords ["day" ]) == day_values
508+ assert list (data .coords ["hour" ]) == hour_values
509+
510+
511+ def test_read_io_technodata (default_model ):
512+ from muse .readers .csv import read_io_technodata
513+
514+ path = default_model / "technodata" / "residential" / "CommOut.csv"
515+ data = read_io_technodata (path )
516+
517+ assert isinstance (data , xr .Dataset )
518+ assert set (data .dims ) == {"technology" , "region" , "year" , "commodity" }
519+ assert dict (data .dtypes ) == dict (
520+ fixed = np .float64 , flexible = np .float64 , commodity_units = np .dtype ("O" )
521+ )
522+ assert list (data .coords ["technology" ].values ) == ["gasboiler" , "heatpump" ]
523+ assert list (data .coords ["region" ].values ) == ["R1" ]
524+ assert list (data .coords ["year" ].values ) == [2020 ]
525+ assert list (data .coords ["commodity" ].values ) == [
526+ "electricity" ,
527+ "gas" ,
528+ "heat" ,
529+ "CO2f" ,
530+ "wind" ,
531+ ]
532+
533+ assert data .data_vars ["fixed" ].coords .equals (data .coords )
534+ assert data .data_vars ["flexible" ].coords .equals (data .coords )
535+ assert list (data .data_vars ["commodity_units" ].coords ) == ["commodity" ]
536+
537+
538+ def test_read_initial_assets (default_model ):
539+ from muse .readers .csv import read_initial_assets
540+
541+ path = default_model / "technodata" / "residential" / "ExistingCapacity.csv"
542+ data = read_initial_assets (path )
543+
544+ assert isinstance (data , xr .DataArray )
545+ assert set (data .dims ) == {"region" , "asset" , "year" }
546+ assert data .dtype == np .int64
547+
548+ assert list (data .coords ["region" ].values ) == ["R1" ]
549+ assert list (data .coords ["technology" ].values ) == ["gasboiler" , "heatpump" ]
550+ assert list (data .coords ["installed" ].values ) == [2020 , 2020 ]
551+ assert list (data .coords ["year" ].values ) == list (range (2020 , 2055 , 5 ))
552+
553+
554+ def test_global_commodities (default_model ):
555+ from muse .readers .csv import read_global_commodities
556+
557+ path = default_model / "input" / "GlobalCommodities.csv"
558+ data = read_global_commodities (path )
559+
560+ assert isinstance (data , xr .Dataset )
561+ assert set (data .dims ) == {"commodity" }
562+ assert dict (data .dtypes ) == dict (
563+ comm_name = np .dtype ("O" ),
564+ comm_type = np .dtype ("O" ),
565+ emmission_factor = np .float64 ,
566+ heat_rate = np .int64 ,
567+ unit = np .dtype ("O" ),
568+ )
569+
570+ assert list (data .coords ["commodity" ].values ) == [
571+ "electricity" ,
572+ "gas" ,
573+ "heat" ,
574+ "wind" ,
575+ "CO2f" ,
576+ ]
577+ assert all (var .coords .equals (data .coords ) for var in data .data_vars .values ())
578+
579+
580+ def test_read_csv_agent_parameters (default_model ):
581+ from muse .readers .csv import read_csv_agent_parameters
582+
583+ path = default_model / "technodata" / "Agents.csv"
584+ data = read_csv_agent_parameters (path )
585+
586+ assert data == [
587+ {
588+ "name" : "A1" ,
589+ "region" : "R1" ,
590+ "objectives" : ["LCOE" ],
591+ "search_rules" : "all" ,
592+ "decision" : {"name" : "singleObj" , "parameters" : [("LCOE" , True , 1 )]},
593+ "agent_type" : "newcapa" ,
594+ "quantity" : 1 ,
595+ "maturity_threshhold" : - 1 ,
596+ "spend_limit" : np .inf ,
597+ "share" : "agent_share_1" ,
598+ },
599+ {
600+ "name" : "A1" ,
601+ "region" : "R1" ,
602+ "objectives" : ["LCOE" ],
603+ "search_rules" : "all" ,
604+ "decision" : {"name" : "singleObj" , "parameters" : [("LCOE" , True , 1 )]},
605+ "agent_type" : "retrofit" ,
606+ "quantity" : 1 ,
607+ "maturity_threshhold" : - 1 ,
608+ "spend_limit" : np .inf ,
609+ "share" : "agent_share_2" ,
610+ },
611+ ]
612+
613+
614+ def test_read_initial_market (default_model ):
615+ from muse .readers .csv import read_initial_market
616+ from muse .readers .toml import read_settings
617+
618+ settings = read_settings (default_model / "settings.toml" )
619+ path = default_model / "input" / "Projections.csv"
620+ data = read_initial_market (path , timeslices = settings .timeslices )
621+
622+ assert isinstance (data , xr .Dataset )
623+ assert set (data .dims ) == {"region" , "year" , "commodity" , "timeslice" }
624+ assert dict (data .dtypes ) == dict (
625+ prices = np .float64 ,
626+ exports = np .float64 ,
627+ imports = np .float64 ,
628+ static_trade = np .float64 ,
629+ )
630+ assert list (data .coords ["region" ].values ) == ["R1" ]
631+ assert list (data .coords ["year" ].values ) == list (range (2010 , 2105 , 5 ))
632+ assert list (data .coords ["commodity" ].values ) == [
633+ "electricity" ,
634+ "gas" ,
635+ "heat" ,
636+ "CO2f" ,
637+ "wind" ,
638+ ]
639+ assert (
640+ list (data .coords ["units_prices" ].values )
641+ == ["MUS$2010/PJ" ] * 3 + ["MUS$2010/kt" ] * 2
642+ )
643+ month_values = ["all-year" ] * 6
644+ day_values = ["all-week" ] * 6
645+ hour_values = [
646+ "night" ,
647+ "morning" ,
648+ "afternoon" ,
649+ "early-peak" ,
650+ "late-peak" ,
651+ "evening" ,
652+ ]
653+
654+ assert list (data .coords ["timeslice" ].values ) == list (
655+ zip (month_values , day_values , hour_values )
656+ )
657+ assert list (data .coords ["month" ]) == month_values
658+ assert list (data .coords ["day" ]) == day_values
659+ assert list (data .coords ["hour" ]) == hour_values
660+
661+ assert all (var .coords .equals (data .coords ) for var in data .data_vars .values ())
662+
663+
664+ def test_read_attribute_table (default_model ):
665+ from muse .readers .csv import read_attribute_table
666+
667+ path = default_model / "input" / "Projections.csv"
668+ data = read_attribute_table (path )
669+
670+ assert isinstance (data , xr .DataArray )
671+ assert data .dtype == np .float64
672+
673+ assert set (data .dims ) == {"region" , "year" , "commodity" }
674+ assert list (data .coords ["region" ].values ) == ["R1" ]
675+ assert list (data .coords ["year" ].values ) == list (range (2010 , 2105 , 5 ))
676+ assert list (data .coords ["commodity" ].values ) == [
677+ "electricity" ,
678+ "gas" ,
679+ "heat" ,
680+ "CO2f" ,
681+ "wind" ,
682+ ]
683+ assert (
684+ list (data .coords ["units_commodity_price" ].values )
685+ == ["MUS$2010/PJ" ] * 3 + ["MUS$2010/kt" ] * 2
686+ )
687+
688+
689+ def test_read_csv_outputs (default_model ):
690+ from muse .readers .csv import read_csv_outputs
691+
692+ path = default_model / "technodata" / "preset" / "*Consumption.csv"
693+ data = read_csv_outputs (str (path ))
694+
695+ assert isinstance (data , xr .DataArray )
696+ assert data .dtype == np .float64
697+
698+ assert set (data .dims ) == {"year" , "commodity" , "region" , "process" , "timeslice" }
699+ assert list (data .coords ["region" ].values ) == ["R1" ]
700+ assert list (data .coords ["process" ].values ) == ["gasboiler" ]
701+ assert list (data .coords ["timeslice" ].values ) == list (range (1 , 7 ))
702+ assert list (data .coords ["year" ].values ) == [2020 , 2050 ]
703+ assert list (data .coords ["commodity" ].values ) == [
704+ "electricity" ,
705+ "gas" ,
706+ "heat" ,
707+ "CO2f" ,
708+ "wind" ,
709+ ]
415710
416711
417712def test_check_utilization_not_all_zero_success ():
0 commit comments