def test_write_scen_files_and_make_full_cfgs(test_scenarios): adapter = MAGICC7() test_scenarios_magiccdf = pymagicc.io.MAGICCData(test_scenarios) res = adapter._write_scen_files_and_make_full_cfgs( test_scenarios_magiccdf, [{ "file_emisscen_3": "overwritten by adapter.magicc_scenario_setup", "other_cfg": 12, }], ) for (model, scenario), _ in test_scenarios_magiccdf.meta.groupby( ["model", "scenario"]): scen_file_name = ("{}_{}.SCEN7".format( scenario, model).upper().replace("/", "-").replace("\\", "-").replace(" ", "-")) scenario_cfg = [v for v in res if v["file_emisscen"] == scen_file_name] assert len(scenario_cfg) == 1 scenario_cfg = scenario_cfg[0] assert scenario_cfg["other_cfg"] == 12 assert scenario_cfg["model"] == model assert scenario_cfg["scenario"] == scenario for i in range(2, 9): scen_flag_val = scenario_cfg["file_emisscen_{}".format(i)] assert scen_flag_val == "NONE"
def magicc7_is_available(): try: magicc_version = MAGICC7.get_version() if magicc_version != "v7.4.2": raise AssertionError( "Wrong MAGICC version for tests ({})".format(magicc_version) ) except KeyError: pytest.skip("MAGICC7 not available")
data_file_regression = os.path.join( os.path.dirname(__file__), DATA_FILE_REGRESSION_NAME ) logger = logging.getLogger(__name__) logging.getLogger().setLevel(logging.DEBUG) logFormatter = logging.Formatter("%(threadName)s - %(levelname)s: %(message)s") stdoutHandler = logging.StreamHandler() stdoutHandler.setFormatter(logFormatter) logging.getLogger().addHandler(stdoutHandler) scenarios = pyam.IamDataFrame(data_file_scenarios) assert MAGICC7.get_version() == EXPECTED_MAGICC_VERSION, MAGICC7.get_version() res = run( climate_models_cfgs={ "MAGICC7": [ { "core_climatesensitivity": 3, "rf_soxi_dir_wm2": -0.2, "out_temperature": 1, }, { "core_climatesensitivity": 2, "rf_soxi_dir_wm2": -0.1, "out_temperature": 1, }, {
return scenarios @pytest.fixture(scope="session") def test_scenario_ssp370_world(test_data_dir): scenario = ScmRun( os.path.join(test_data_dir, "rcmip-emissions-annual-means-v5-1-0-ssp370-world.csv"), lowercase_cols=True, ) return scenario try: MAGICC_VERSION = MAGICC7.get_version() if MAGICC_VERSION != "v7.5.1": CORRECT_MAGICC_IS_AVAILABLE = False else: CORRECT_MAGICC_IS_AVAILABLE = True except KeyError: MAGICC_VERSION = None CORRECT_MAGICC_IS_AVAILABLE = False def pytest_runtest_setup(item): for mark in item.iter_markers(): if mark.name == "magicc" and not CORRECT_MAGICC_IS_AVAILABLE: if MAGICC_VERSION is None: pytest.skip("MAGICC7 not available")
def test_run( self, test_scenarios, test_data_dir, update_expected_values, ): expected_output_file = os.path.join( test_data_dir, "expected-integration-output", "expected_magicc7_test_run_output.json", ) res = run( climate_models_cfgs={ "MAGICC7": [ { "core_climatesensitivity": 3, "rf_soxi_dir_wm2": -0.2, "out_temperature": 1, "out_forcing": 1, "out_dynamic_vars": [ "DAT_AEROSOL_ERF", "DAT_HEATCONTENT_AGGREG_TOTAL", "DAT_CO2_AIR2LAND_FLUX", ], "out_ascii_binary": "BINARY", "out_binary_format": 2, }, { "core_climatesensitivity": 2, "rf_soxi_dir_wm2": -0.1, "out_temperature": 1, "out_forcing": 1, "out_ascii_binary": "BINARY", "out_binary_format": 2, }, { "core_climatesensitivity": 5, "rf_soxi_dir_wm2": -0.35, "out_temperature": 1, "out_forcing": 1, "out_ascii_binary": "BINARY", "out_binary_format": 2, }, ], }, scenarios=test_scenarios.filter( scenario=["ssp126", "ssp245", "ssp370"]), output_variables=( "Surface Air Temperature Change", "Effective Radiative Forcing", "Effective Radiative Forcing|Aerosols", "Effective Radiative Forcing|CO2", "Heat Content|Ocean", "Net Atmosphere to Land Flux|CO2", ), ) assert isinstance(res, ScmRun) assert res["run_id"].min() == 0 assert res["run_id"].max() == 8 assert res.get_unique_meta("climate_model", no_duplicates=True) == "MAGICC{}".format( MAGICC7.get_version()) assert set(res.get_unique_meta("variable")) == set([ "Surface Air Temperature Change", "Effective Radiative Forcing", "Effective Radiative Forcing|Aerosols", "Effective Radiative Forcing|CO2", "Heat Content|Ocean", "Net Atmosphere to Land Flux|CO2", ]) # check we can also calcluate quantiles assert "run_id" in res.meta quantiles = calculate_quantiles(res, [0, 0.05, 0.17, 0.5, 0.83, 0.95, 1]) assert "run_id" not in quantiles.meta self._check_output(res, expected_output_file, update_expected_values)
def test_magicc7_run(test_scenarios, magicc7_is_available): debug_run = False res = run( climate_models_cfgs={ "MAGICC7": [ { "core_climatesensitivity": 3, "rf_soxi_dir_wm2": -0.2, "out_temperature": 1, "out_forcing": 1, "out_dynamic_vars": [ "DAT_AEROSOL_ERF", "DAT_HEATCONTENT_AGGREG_TOTAL", "DAT_CO2_AIR2LAND_FLUX", ], "out_ascii_binary": "BINARY", "out_binary_format": 2, }, { "core_climatesensitivity": 2, "rf_soxi_dir_wm2": -0.1, "out_temperature": 1, "out_forcing": 1, "out_ascii_binary": "BINARY", "out_binary_format": 2, }, { "core_climatesensitivity": 5, "rf_soxi_dir_wm2": -0.35, "out_temperature": 1, "out_forcing": 1, "out_ascii_binary": "BINARY", "out_binary_format": 2, }, ], }, scenarios=test_scenarios.filter( scenario=["ssp126", "ssp245", "ssp370"]), output_variables=( "Surface Temperature", "Effective Radiative Forcing", "Effective Radiative Forcing|Aerosols", "Effective Radiative Forcing|CO2", "Heat Content|Ocean", "Net Atmosphere to Land Flux|CO2", ), ) assert isinstance(res, ScmRun) assert res["run_id"].min() == 0 assert res["run_id"].max() == 8 assert res.get_unique_meta("climate_model", no_duplicates=True) == "MAGICC{}".format( MAGICC7.get_version()) assert set(res.get_unique_meta("variable")) == set([ "Surface Temperature", "Effective Radiative Forcing", "Effective Radiative Forcing|Aerosols", "Effective Radiative Forcing|CO2", "Heat Content|Ocean", "Net Atmosphere to Land Flux|CO2", ]) # check ocean heat content unit conversion comes through correctly _check_res( 1824.05, res.filter( unit="ZJ", variable="Heat Content|Ocean", region="World", year=2100, scenario="ssp126", ).values.max(), not debug_run, rtol=RTOL, ) _check_res( 0.472378, res.filter( unit="GtC / yr", variable="Net Atmosphere to Land Flux|CO2", region="World", year=2100, scenario="ssp126", ).values.max(), not debug_run, rtol=RTOL, ) _check_res( 2.756034, res.filter(variable="Surface Temperature", region="World", year=2100, scenario="ssp126").values.max(), not debug_run, rtol=RTOL, ) _check_res( 1.2195495, res.filter(variable="Surface Temperature", region="World", year=2100, scenario="ssp126").values.min(), not debug_run, rtol=RTOL, ) _check_res( 5.5226571, res.filter(variable="Surface Temperature", region="World", year=2100, scenario="ssp370").values.max(), not debug_run, rtol=RTOL, ) _check_res( 2.733369581, res.filter(variable="Surface Temperature", region="World", year=2100, scenario="ssp370").values.min(), not debug_run, rtol=RTOL, ) # check we can also calcluate quantiles quantiles = calculate_quantiles(res, [0.05, 0.17, 0.5, 0.83, 0.95]) _check_res( 1.27586919, quantiles.filter( variable="Surface Temperature", region="World", year=2100, scenario="ssp126", quantile=0.05, ).values, not debug_run, rtol=RTOL, ) _check_res( 2.6587052, quantiles.filter( variable="Surface Temperature", region="World", year=2100, scenario="ssp126", quantile=0.95, ).values, not debug_run, rtol=RTOL, ) _check_res( 2.83627686, quantiles.filter( variable="Surface Temperature", region="World", year=2100, scenario="ssp370", quantile=0.05, ).values, not debug_run, rtol=RTOL, ) _check_res( 5.34663565, quantiles.filter( variable="Surface Temperature", region="World", year=2100, scenario="ssp370", quantile=0.95, ).values, not debug_run, rtol=RTOL, ) if debug_run: assert False, "Turn off debug"