Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added save_csv_combined_output option #43

Merged
merged 8 commits into from
Aug 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe call it save_csv_combined_climate_output_option to be a bit more descriptive (it does not include emissions data, right?) and align better with save_raw_climate_output_option

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It does include emissions, it's the rawoutput file. That's why I went for "combined", but suggestions on a more descriptive name are appreciated

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah yes you're right.

Okay, rather then thinking about it too long, I think what can simply do is add a bit more description in the docs: #44
Can be done after we merge this PR.

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"