Skip to content

Commit

Permalink
Merge pull request #43 from gabriel-abrahao/save_csv
Browse files Browse the repository at this point in the history
Added save_csv_combined_output option
  • Loading branch information
jkikstra authored Aug 14, 2023
2 parents 9b701da + 4dc4571 commit 485f3d2
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ master

Added
~~~~~
- (`#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
Expand Down
15 changes: 15 additions & 0 deletions src/climate_assessment/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +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):
Expand Down Expand Up @@ -880,6 +889,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,
Expand All @@ -898,6 +908,7 @@ 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.
Expand Down Expand Up @@ -968,6 +979,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")


Expand Down
48 changes: 48 additions & 0 deletions tests/integration/test_run_clim_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,3 +201,51 @@ 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_combined_csv_output(
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",
],
)

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), "XLS output not written"
assert os.path.isfile(
out_csv_fname
), "--save-csv-combined-output was set but CSV output not written"

0 comments on commit 485f3d2

Please sign in to comment.