From f9fc44b80799061b2c22bc82c49e1aa1b06cd984 Mon Sep 17 00:00:00 2001 From: gabriel-abrahao Date: Mon, 7 Aug 2023 17:32:33 +0200 Subject: [PATCH 1/8] Added save_csv_combined_output option --- src/climate_assessment/cli.py | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/src/climate_assessment/cli.py b/src/climate_assessment/cli.py index 5e87b11..2cbd486 100644 --- a/src/climate_assessment/cli.py +++ b/src/climate_assessment/cli.py @@ -245,7 +245,15 @@ type=bool, show_default=True, ) - +save_csv_combined_output_option = click.option( + "--save-csv-combined-output", + help="Write CSV output with combined climate output and emissions", + is_flag=True, + required=False, + default=False, + type=bool, + show_default=True, +) def _setup_logging(logger): """ @@ -880,6 +888,7 @@ def infill( @gwp_def_false_option @nonco2_warming_option @save_raw_climate_output_option +@save_csv_combined_output_option def clim_cli( harmonizedinfilledemissions, outdir, @@ -898,6 +907,8 @@ def clim_cli( gwp, co2_and_non_co2_warming, save_raw_climate_output, + save_csv_combined_output + ): """ Run the climate emulator step of the IPCC AR6 climate asessment workflow. @@ -967,6 +978,10 @@ def clim_cli( LOGGER.info("write out raw output") results.to_excel(os.path.join(outdir, str(key_string + "_" + "rawoutput.xlsx"))) + + if save_csv_combined_output: + LOGGER.info("write out raw output in csv") + results.to_csv(os.path.join(outdir, str(key_string + "_" + "rawoutput.csv"))) LOGGER.info("COMPLETE") From 3bcd930e7a9981c7019a288e2f032011edef8902 Mon Sep 17 00:00:00 2001 From: gabriel-abrahao Date: Mon, 7 Aug 2023 18:17:54 +0200 Subject: [PATCH 2/8] Updated CHANGELOG.rst --- CHANGELOG.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index f1c12de..71697e2 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -19,6 +19,7 @@ master Added ~~~~~ +- (`#40 https://github.com/iiasa/climate-assessment/pull/43`_) Added CSV option to :func:`climate_assessment.cli.clim_cli` - (`#40 https://github.com/iiasa/climate-assessment/pull/40`_) Update awscli to >= 1.29.4 - (`#36 https://github.com/iiasa/climate-assessment/pull/36`_) Update pyam-iamc to >=1.9.0 - (`#31 https://github.com/iiasa/climate-assessment/pull/31`_) Update pyam-iamc to >=1.7.0 From c4ccfaf7b19417c598c936785805497fab733c07 Mon Sep 17 00:00:00 2001 From: gabriel-abrahao Date: Thu, 10 Aug 2023 11:42:38 +0200 Subject: [PATCH 3/8] Added test_output_written --- .../integration/test_run_clim_integration.py | 43 +++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/tests/integration/test_run_clim_integration.py b/tests/integration/test_run_clim_integration.py index 8b608aa..11936c8 100644 --- a/tests/integration/test_run_clim_integration.py +++ b/tests/integration/test_run_clim_integration.py @@ -201,3 +201,46 @@ def test_historical_eval_period_out_of_order( "`period` must be a string of the form 'YYYY-YYYY' (with the first year being " "less than or equal to the second), we received 2014-1995" ) + +def test_output_written( + tmpdir, test_data_dir, fair_slim_configs_filepath, fair_common_configs_filepath +): + out_dir = str(tmpdir) + inp_file = os.path.join( + test_data_dir, + "workflow-fair", + "ex2_harmonized_infilled.csv", + ) + + fair_version = "1.6.2" + + runner = CliRunner() + result = runner.invoke( + climate_assessment.cli.clim_cli, + [ + inp_file, + out_dir, + "--num-cfgs", + 1, + "--test-run", + "--model", + "fair", + "--model-version", + fair_version, + "--probabilistic-file", + fair_slim_configs_filepath, + "--fair-extra-config", + fair_common_configs_filepath, + "--scenario-batch-size", + 4, + "--historical-warming", + 0.8, + "--save-csv-combined-output" + ], + ) + + out_csv_fname = os.path.join(out_dir, "ex2_harmonized_infilled_rawoutput.csv") + out_xls_fname = os.path.join(out_dir, "ex2_harmonized_infilled_rawoutput.xlsx") + + assert os.path.isfile(out_xls_fname), f"XLS output not written:" + assert os.path.isfile(out_csv_fname), f"--save-csv-combined-output was set but CSV output not written:" \ No newline at end of file From 5b3a33f35435687e39d67e263255d4d0907157d2 Mon Sep 17 00:00:00 2001 From: gabriel-abrahao Date: Thu, 10 Aug 2023 12:04:04 +0200 Subject: [PATCH 4/8] Satisfying linters --- src/climate_assessment/cli.py | 6 +++--- tests/integration/test_run_clim_integration.py | 11 ++++++++--- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/src/climate_assessment/cli.py b/src/climate_assessment/cli.py index 2cbd486..1777951 100644 --- a/src/climate_assessment/cli.py +++ b/src/climate_assessment/cli.py @@ -255,6 +255,7 @@ show_default=True, ) + def _setup_logging(logger): """ Set up logging preferences. This removes unnecessary logger warnings from @@ -907,8 +908,7 @@ def clim_cli( gwp, co2_and_non_co2_warming, save_raw_climate_output, - save_csv_combined_output - + save_csv_combined_output, ): """ Run the climate emulator step of the IPCC AR6 climate asessment workflow. @@ -978,7 +978,7 @@ def clim_cli( LOGGER.info("write out raw output") results.to_excel(os.path.join(outdir, str(key_string + "_" + "rawoutput.xlsx"))) - + if save_csv_combined_output: LOGGER.info("write out raw output in csv") results.to_csv(os.path.join(outdir, str(key_string + "_" + "rawoutput.csv"))) diff --git a/tests/integration/test_run_clim_integration.py b/tests/integration/test_run_clim_integration.py index 11936c8..2690dae 100644 --- a/tests/integration/test_run_clim_integration.py +++ b/tests/integration/test_run_clim_integration.py @@ -202,6 +202,7 @@ def test_historical_eval_period_out_of_order( "less than or equal to the second), we received 2014-1995" ) + def test_output_written( tmpdir, test_data_dir, fair_slim_configs_filepath, fair_common_configs_filepath ): @@ -235,12 +236,16 @@ def test_output_written( 4, "--historical-warming", 0.8, - "--save-csv-combined-output" + "--save-csv-combined-output", ], ) + assert result.exit_code == 0, _format_traceback_and_stdout_from_click_result(result) + out_csv_fname = os.path.join(out_dir, "ex2_harmonized_infilled_rawoutput.csv") out_xls_fname = os.path.join(out_dir, "ex2_harmonized_infilled_rawoutput.xlsx") - assert os.path.isfile(out_xls_fname), f"XLS output not written:" - assert os.path.isfile(out_csv_fname), f"--save-csv-combined-output was set but CSV output not written:" \ No newline at end of file + assert os.path.isfile(out_xls_fname), "XLS output not written:" + assert os.path.isfile( + out_csv_fname + ), "--save-csv-combined-output was set but CSV output not written:" From cd8ce998decbd7067691a11fcbe1c5600320d973 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gabriel=20Medeiros=20Abrah=C3=A3o?= <30908904+gabriel-abrahao@users.noreply.github.com> Date: Fri, 11 Aug 2023 11:26:22 +0200 Subject: [PATCH 5/8] Update CHANGELOG.rst Co-authored-by: Zeb Nicholls --- CHANGELOG.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 71697e2..9082e7b 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -19,7 +19,7 @@ master Added ~~~~~ -- (`#40 https://github.com/iiasa/climate-assessment/pull/43`_) Added CSV option to :func:`climate_assessment.cli.clim_cli` +- (`#40 https://github.com/iiasa/climate-assessment/pull/43`_) Add combined CSV output option to :func:`climate_assessment.cli.clim_cli` - (`#40 https://github.com/iiasa/climate-assessment/pull/40`_) Update awscli to >= 1.29.4 - (`#36 https://github.com/iiasa/climate-assessment/pull/36`_) Update pyam-iamc to >=1.9.0 - (`#31 https://github.com/iiasa/climate-assessment/pull/31`_) Update pyam-iamc to >=1.7.0 From 6220bdb41bc5dfdfa80f9a46d2de699bad2137a0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gabriel=20Medeiros=20Abrah=C3=A3o?= <30908904+gabriel-abrahao@users.noreply.github.com> Date: Fri, 11 Aug 2023 11:27:21 +0200 Subject: [PATCH 6/8] Update tests/integration/test_run_clim_integration.py Co-authored-by: Zeb Nicholls --- tests/integration/test_run_clim_integration.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/integration/test_run_clim_integration.py b/tests/integration/test_run_clim_integration.py index 2690dae..1c1270b 100644 --- a/tests/integration/test_run_clim_integration.py +++ b/tests/integration/test_run_clim_integration.py @@ -203,7 +203,7 @@ def test_historical_eval_period_out_of_order( ) -def test_output_written( +def test_combined_csv_output( tmpdir, test_data_dir, fair_slim_configs_filepath, fair_common_configs_filepath ): out_dir = str(tmpdir) From 6b7ea48117ed5e19f0105f97135558bee7d39f1f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gabriel=20Medeiros=20Abrah=C3=A3o?= <30908904+gabriel-abrahao@users.noreply.github.com> Date: Fri, 11 Aug 2023 11:27:31 +0200 Subject: [PATCH 7/8] Update tests/integration/test_run_clim_integration.py Co-authored-by: Zeb Nicholls --- tests/integration/test_run_clim_integration.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/integration/test_run_clim_integration.py b/tests/integration/test_run_clim_integration.py index 1c1270b..520d099 100644 --- a/tests/integration/test_run_clim_integration.py +++ b/tests/integration/test_run_clim_integration.py @@ -245,7 +245,7 @@ def test_combined_csv_output( out_csv_fname = os.path.join(out_dir, "ex2_harmonized_infilled_rawoutput.csv") out_xls_fname = os.path.join(out_dir, "ex2_harmonized_infilled_rawoutput.xlsx") - assert os.path.isfile(out_xls_fname), "XLS output not written:" + assert os.path.isfile(out_xls_fname), "XLS output not written" assert os.path.isfile( out_csv_fname ), "--save-csv-combined-output was set but CSV output not written:" From 4dc4571d38c29d12e6a348248aa2a4408f48266f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gabriel=20Medeiros=20Abrah=C3=A3o?= <30908904+gabriel-abrahao@users.noreply.github.com> Date: Fri, 11 Aug 2023 11:27:46 +0200 Subject: [PATCH 8/8] Update tests/integration/test_run_clim_integration.py Co-authored-by: Zeb Nicholls --- tests/integration/test_run_clim_integration.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/integration/test_run_clim_integration.py b/tests/integration/test_run_clim_integration.py index 520d099..a7399c9 100644 --- a/tests/integration/test_run_clim_integration.py +++ b/tests/integration/test_run_clim_integration.py @@ -248,4 +248,4 @@ def test_combined_csv_output( assert os.path.isfile(out_xls_fname), "XLS output not written" assert os.path.isfile( out_csv_fname - ), "--save-csv-combined-output was set but CSV output not written:" + ), "--save-csv-combined-output was set but CSV output not written"