forked from iiasa/climate-assessment
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcli.py
1085 lines (945 loc) · 32.7 KB
/
cli.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import logging
import os.path
from concurrent.futures import ProcessPoolExecutor, as_completed
import click
import pandas as pd
import pyam
from tqdm import tqdm
from .checks import (
infiller_vetting,
perform_input_checks,
sanity_check_bounds_kyoto_emissions,
sanity_check_comparison_kyoto_gases,
)
from .climate import DEFAULT_MAGICC_VERSION, climate_assessment
from .climate.post_process import check_hist_warming_period
from .harmonization import run_harmonization
from .harmonization_and_infilling import harmonization_and_infilling
from .infilling import postprocess_infilled_for_climate, run_infilling
from .postprocess import do_postprocess
from .utils import (
_add_variables,
add_gwp100_kyoto_wrapper,
columns_to_basic,
extract_ips,
init_logging,
split_df,
split_scenarios_into_batches,
)
LOGGER = logging.getLogger(__name__)
input_emissions_file_arg = click.argument(
"input_emissions_file",
required=True,
type=click.Path(exists=True, dir_okay=False, readable=True, resolve_path=True),
)
outdir_arg = click.argument(
"outdir",
required=True,
type=click.Path(exists=True, file_okay=False, writable=True, resolve_path=True),
)
output_files_arg = click.argument(
"rawoutput_files",
nargs=-1,
type=click.Path(exists=True, file_okay=True, resolve_path=True),
)
harmonizedinfilledemissions_arg = click.argument(
"harmonizedinfilledemissions",
type=click.Path(exists=True, dir_okay=False, readable=True, resolve_path=True),
)
n_workers_option = click.option(
"--n_workers",
help="Number of workers",
default=8,
type=int,
)
output_file_option = click.option(
"--output-file",
help="Filename where the combined metadata is output",
required=True,
)
input_check_option = click.option(
"--inputcheck/--no-inputcheck",
help="Check input before running the workflow",
required=False,
default=True,
show_default=True,
)
infilling_database_option = click.option(
"--infilling-database",
help="File to use as the infilling database",
required=False, # defaults to infiller database used for ar6
default=os.path.abspath(
os.path.join(
os.path.dirname(__file__),
"infilling",
"cmip6-ssps-workflow-emissions.csv",
)
),
type=click.Path(exists=True, file_okay=True, readable=True, resolve_path=True),
show_default=True,
)
harmonization_instance_option = click.option(
"--harmonization-instance",
help="Harmonisation settings to use",
required=False, # defaults to rcmip historical data as used for ar6
default="ar6",
type=click.Choice(["ar6", "sr15"], case_sensitive=True),
show_default=True,
)
model_option = click.option(
"--model",
help="Climate model to run scenarios with",
required=False,
default="magicc",
type=str,
show_default=True,
)
model_version_option = click.option(
"--model-version",
help="Expected model version (just used to check env is working as intended)",
default=DEFAULT_MAGICC_VERSION,
type=str,
show_default=True,
)
probabilistic_file_option = click.option(
"--probabilistic-file",
help="json file containing climate model probabilistic config",
required=True, # currenlty no default here, users need to locate it
type=click.Path(exists=True, dir_okay=False, readable=True, resolve_path=True),
)
magicc_extra_config_option = click.option(
"--magicc-extra-config",
help="File containing additional MAGICC config",
required=False,
type=click.Path(exists=True, dir_okay=False, readable=True, resolve_path=True),
)
fair_extra_config_option = click.option(
"--fair-extra-config",
help="File containing additional FaIR config",
required=False, # required when running FaIR
type=click.Path(exists=True, dir_okay=False, readable=True, resolve_path=True),
)
hist_warming_option = click.option(
"--historical-warming",
help="Historical warming estimate in kelvin",
required=False, # defaults to WGI estimated historical warming
default=0.85,
type=float,
show_default=True,
)
hist_warming_ref_period_option = click.option(
"--historical-warming-reference-period",
help="Reference period used when calculating historical warming",
required=False,
default="1850-1900",
type=str,
show_default=True,
)
hist_warming_eval_period_option = click.option(
"--historical-warming-evaluation-period",
help="Evaluation period used when calculating historical warming",
required=False,
default="1995-2014",
type=str,
show_default=True,
)
num_cfgs_option = click.option(
"--num-cfgs",
help="Number of climate model configs to run",
default=600,
type=int,
show_default=True,
)
test_run_option = click.option(
"--test-run/--no-test-run",
help="Make this run a test run (doesn't check that historical warming"
"from the climate models comes out consistently)",
default=False,
type=bool,
show_default=True,
)
scenario_batch_size_option = click.option(
"--scenario-batch-size",
help="Number of scenarios to run at a time",
required=False,
default=20,
type=int,
show_default=True,
)
save_raw_climate_output_option = click.option(
"--save-raw-climate-output/--dont-save-raw-climate-output",
help="Save raw climate output to disk",
required=False,
default=False,
type=bool,
show_default=True,
)
categorisation_option = click.option(
"--categorisation/--no-categorisation",
help="Add temperature category to meta data",
required=False,
default=True,
show_default=True,
)
report_completeness_option = click.option(
"--reporting-completeness-categorisation/"
"--no-reporting-completeness-categorisation",
help="Add an emissions reporting completeness category to meta data",
required=False,
default=False,
type=bool,
show_default=True,
)
nonco2_warming_option = click.option(
"--co2-and-non-co2-warming/--no-co2-and-non-co2-warming",
help="Include CO2 and non-CO2 warming assessment",
required=False,
default=False,
type=bool,
show_default=True,
)
prefix_all_out_variables_option = click.option(
"--prefix", help="Prefix for all output variables", required=True
)
prefix_option = click.option(
"--prefix",
help="Prefix for all variable names",
required=False,
default="AR6 climate diagnostics",
type=str,
show_default=True,
)
gwp_option = click.option(
"--gwp/--no-gwp",
help="Add Kyoto Gases in GWP100 to output",
required=False,
default=True,
type=bool,
show_default=True,
)
gwp_def_false_option = click.option(
"--gwp/--no-gwp",
help="Add Kyoto Gases in GWP100 to output",
default=False,
type=bool,
show_default=True,
)
kyoto_ghgs_option = click.option(
"--kyoto-ghgs", help="Add Kyoto GHGs to output", default=True
)
postprocess_option = click.option(
"--postprocess/--no-postprocess",
help="Run postprocessing steps",
default=True,
type=bool,
show_default=True,
)
harmonize_option = click.option(
"--harmonize/--dont-harmonize",
help="Do harmonization",
default=True,
type=bool,
show_default=True,
)
def _setup_logging(logger):
"""
Set up logging preferences. This removes unnecessary logger warnings from
packages that the workflow depends on, most notably pyam. It also sets the
general logger level to INFO.
Parameters
----------
logger : :class:`logging.getLogger`
Input Logger instance created by logging.getLogger(name).
"""
init_logging(logger)
logger.info("Silencing pyam loggers")
logging.getLogger("pyam.core").setLevel(logging.CRITICAL)
logging.getLogger("pyam.utils").setLevel(logging.CRITICAL)
def _get_key_string_and_log_outdir(input_emissions_file, outdir, logger):
# TODO: remove need to parse in logger once we've cleaned things up
logger.info("Outputs will be saved in: %s", outdir)
key_string = os.path.splitext(os.path.basename(input_emissions_file))[0]
logger.info("Outputs will be saved with the ID: %s", key_string)
return key_string
def _load_emissions_convert_to_basic(input_emissions_file, logger):
# TODO: remove need to parse in logger once we've cleaned things up
logger.info("Loading %s", input_emissions_file)
input_df = pyam.IamDataFrame(input_emissions_file)
logger.info("Converting to basic columns i.e. removing any extra columns")
input_df = columns_to_basic(input_df)
return input_df
def _input_checks(input_df, inputcheck, key_string, outdir):
"""
Simple wrapper to call climate assessment workflow native emissions input
checks, for a chosen set of checks.
Returns IAM dataframe with only model-scenario that will be without emissions.
Output also includes the input emissions. The output is interpolated
onto an annual timestep.
For more information, see the code description under
:func:`climate_assessment.checks.perform_input_checks`.
"""
if inputcheck:
LOGGER.info("Performing input data checks")
df = perform_input_checks(
input_df,
output_csv_files=True,
output_filename=key_string,
lead_variable_check=True,
outdir=outdir,
)
else:
df = input_df.copy()
return df
def _harmonize_and_infill(
input_df,
inputcheck,
key_string,
outdir,
infilling_database,
harmonize,
prefix,
harmonization_instance,
):
"""
Thin wrapper function for running both harmonization and infilling.
Returns True if there are scenarios that can be run by a climate
emulator. Those scenarios are not returned by this function, but rather
written to the outdir. Returns False if there are no complete scenarios
to be run.
For more information, see the code description under
:func:`climate_assessment.harmonization_and_infilling.harmonization_and_infilling`.
"""
df = _input_checks(input_df, inputcheck, key_string, outdir)
##################################
# run HARMONIZATION and INFILLING (writes out results, returns True or False, includes some post-infilling checks)
##################################
# TODO: split harmonization and infilling steps
# TODO: remove 'instance' and put config somewhere more sane
# TODO: remove prefix when we change to the new aneris interface
assessable = harmonization_and_infilling(
df,
key_string,
infilling_database,
outdir=outdir,
do_harmonization=harmonize,
prefix=prefix,
instance=harmonization_instance,
)
return assessable
@click.command(context_settings={"help_option_names": ["-h", "--help"]})
@input_emissions_file_arg
@outdir_arg
@input_check_option
@magicc_extra_config_option
@fair_extra_config_option
@model_option
@model_version_option
@probabilistic_file_option
@num_cfgs_option
@hist_warming_option
@hist_warming_ref_period_option
@hist_warming_eval_period_option
@test_run_option
@scenario_batch_size_option
@infilling_database_option
@save_raw_climate_output_option
@postprocess_option
@categorisation_option
@report_completeness_option
@harmonize_option
@prefix_option
@gwp_option
@harmonization_instance_option
@nonco2_warming_option
def workflow(
input_emissions_file,
outdir,
inputcheck,
magicc_extra_config,
fair_extra_config,
model,
model_version,
probabilistic_file,
num_cfgs,
historical_warming,
historical_warming_reference_period,
historical_warming_evaluation_period,
test_run,
scenario_batch_size,
infilling_database,
save_raw_climate_output,
postprocess,
categorisation,
reporting_completeness_categorisation,
harmonize,
prefix,
harmonization_instance,
co2_and_non_co2_warming,
gwp,
):
# TODO: remove "model_version" and `num_cfgs` as mandatory
# options for AR6 release, as there should only be one option per emulator.
"""
Run the full IPCC AR6 climate asessment workflow.
Example usage: ``python scripts/run_workflow.py tests/test-data/ex2.csv output --model "FaIR" --model-version "1.6.2" --num-cfgs 2237 --probabilistic-file tests/test-data/fair-1.6.2-wg3-params-slim.json --fair-extra-config tests/test-data/fair-1.6.2-wg3-params-common.json --infilling-database src/climate_assessment/infilling/cmip6-ssps-workflow-emissions.csv``
See further documentation at :ref:`Description of the workflow <workflow>`.
"""
LOGGER = logging.getLogger("workflow")
_setup_logging(LOGGER)
run_workflow(
input_emissions_file,
outdir,
model,
model_version,
probabilistic_file,
num_cfgs,
inputcheck=inputcheck,
magicc_extra_config=magicc_extra_config,
fair_extra_config=fair_extra_config,
historical_warming=historical_warming,
historical_warming_reference_period=historical_warming_reference_period,
historical_warming_evaluation_period=historical_warming_evaluation_period,
test_run=test_run,
scenario_batch_size=scenario_batch_size,
infilling_database=infilling_database,
save_raw_climate_output=save_raw_climate_output,
postprocess=postprocess,
categorisation=categorisation,
reporting_completeness_categorisation=reporting_completeness_categorisation,
harmonize=harmonize,
prefix=prefix,
harmonization_instance=harmonization_instance,
co2_and_non_co2_warming=co2_and_non_co2_warming,
gwp=gwp,
)
# TODO: move to a new home (probably)
def run_workflow(
input_emissions_file,
outdir,
model,
model_version,
probabilistic_file,
num_cfgs,
inputcheck=True,
magicc_extra_config=None,
fair_extra_config=None,
historical_warming=0.85,
historical_warming_reference_period="1850-1900",
historical_warming_evaluation_period="1995-2014",
test_run=False,
scenario_batch_size=10,
infilling_database=os.path.abspath(
os.path.join(
os.path.dirname(__file__),
"infilling",
"cmip6-ssps-workflow-emissions.csv",
)
),
save_raw_climate_output=False,
postprocess=True,
categorisation=True,
reporting_completeness_categorisation=False,
harmonize=True,
prefix="AR6 climate diagnostics",
harmonization_instance="ar6",
co2_and_non_co2_warming=False,
gwp=True,
):
"""
Run the workflow
Parameters
----------
input_emissions_file : str
Path to input emissions file. This must be read off disk because we use
the filename to help name output files. PRs welcome to split off an interface
which runs in memory.
outdir : str
Where to save the output
model : str
Climate model to run
model_version : str
Version of the climate model to run
probabilistic_file : str
File containing the probabilistic configuration of the climate model
num_cfgs : int
Number of climate model configurations to run
inputcheck : bool
Check input before running the workflow?
magicc_extra_config : str
File containing additional MAGICC config (only required if running MAGICC)
fair_extra_config : str
File containing additional FaIR config (only required if running FaIR)
historical_warming : float
Historical warming estimate
historical_warming_reference_period : str
Reference period when calculating historical warming
historical_warming_evaluation_period : str
Period over which historical warming is evaluated
test_run : bool
Are we doing a test run (if yes, we don't check that historical
warming from the climate models comes out consistently)?
scenario_batch_size : int
How many scenarios to run at once (smaller number means less memory
is needed)?
infilling_database : str
Path to file to use for infilling
save_raw_climate_output : bool
Should raw climate output be saved (warning, requires lots of disk space
and time)?
postprocess : bool
Should postprocessing steps be run?
categorisation : bool
Should categorisation be applied to scenarios?
reporting_completeness_categorisation : bool
Should emissions reporting completeness be added to output?
harmonize : bool
Should scenarios be harmonised?
prefix : str
String to use as a prefix for output variables
harmonization_instance : ["ar6", "sr15"]
Which configuration should be used for harmonisation?
co2_and_non_co2_warming : bool
Calculate CO2 and non-CO2 warming too (requires 3 times as many
climate model runs, available for MAGICC only)
gwp : bool
Calculate GWP equivalents too
"""
key_string = _get_key_string_and_log_outdir(input_emissions_file, outdir, LOGGER)
check_hist_warming_period(historical_warming_reference_period)
check_hist_warming_period(historical_warming_evaluation_period)
input_df = _load_emissions_convert_to_basic(input_emissions_file, LOGGER)
assessable = _harmonize_and_infill(
input_df,
inputcheck,
key_string,
outdir,
infilling_database,
harmonize,
prefix,
harmonization_instance,
)
if not assessable:
LOGGER.warning("No assessable scenarios")
return
# read in infilled database
infilled_emissions = os.path.join(
outdir, "{}_harmonized_infilled.csv".format(key_string)
)
LOGGER.info("Reading in infilled scenarios from: %s", infilled_emissions)
df_infilled = pyam.IamDataFrame(infilled_emissions)
LOGGER.info(df_infilled.timeseries())
####################
# run climate models
####################
df_climate = climate_assessment(
df_infilled,
key_string,
outdir,
magicc_extra_config=magicc_extra_config,
fair_extra_config=fair_extra_config,
model=model,
model_version=model_version,
probabilistic_file=probabilistic_file,
num_cfgs=num_cfgs,
historical_warming=historical_warming,
historical_warming_reference_period=historical_warming_reference_period,
historical_warming_evaluation_period=historical_warming_evaluation_period,
test_run=test_run,
scenario_batch_size=scenario_batch_size,
save_raw_output=save_raw_climate_output,
co2_and_non_co2_warming=co2_and_non_co2_warming,
prefix=prefix,
)
LOGGER.info(df_climate.timeseries())
LOGGER.info("Concatenating infilled df, climate df and input df")
results = pyam.concat([df_infilled, df_climate])
output = pyam.concat(
[input_df.filter(variable=results.variable, keep=False), results]
)
LOGGER.info("write out raw output")
output.to_excel(os.path.join(outdir, str(key_string + "_" + "rawoutput.xlsx")))
if postprocess:
output_postprocess = do_postprocess(
output,
outdir=outdir,
key_string=key_string,
categorisation=categorisation,
reporting_completeness_categorisation=reporting_completeness_categorisation,
prefix=prefix,
gwp=gwp,
model_version=model_version,
model=model,
)
# Sanity checks
sanity_check_bounds_kyoto_emissions(
output_postprocess,
out_kyoto_infilled=f"{prefix}|Infilled|Emissions|Kyoto Gases",
)
sanity_check_comparison_kyoto_gases(
output_postprocess,
out_kyoto_harmonized=f"{prefix}|Harmonized|Emissions|Kyoto Gases",
out_kyoto_infilled=f"{prefix}|Infilled|Emissions|Kyoto Gases",
)
@click.command(context_settings={"help_option_names": ["-h", "--help"]})
@input_emissions_file_arg
@outdir_arg
@input_check_option
@infilling_database_option
@prefix_option
@harmonization_instance_option
def harmonize_and_infill(
input_emissions_file,
outdir,
inputcheck,
infilling_database,
prefix,
# gwp,
harmonization_instance,
):
"""
Harmonise and infill data in ``input_emissions_file``, saving output in ``outdir``
``input_emissions_file`` should be a path to a file of emissions
``outdir`` should be a path a directory which already exists
"""
LOGGER = logging.getLogger("harmonize_and_infill")
_setup_logging(LOGGER)
key_string = _get_key_string_and_log_outdir(input_emissions_file, outdir, LOGGER)
input_df = _load_emissions_convert_to_basic(input_emissions_file, LOGGER)
_harmonize_and_infill(
input_df=input_df,
inputcheck=inputcheck,
key_string=key_string,
outdir=outdir,
infilling_database=infilling_database,
prefix=prefix,
# gwp=gwp, # TODO: implement downstream
harmonization_instance=harmonization_instance,
harmonize=True,
)
@click.command(context_settings={"help_option_names": ["-h", "--help"]})
@input_emissions_file_arg
@outdir_arg
@input_check_option
@prefix_option
@gwp_option
@harmonization_instance_option
def harmonize(
input_emissions_file,
outdir,
inputcheck,
prefix,
gwp,
harmonization_instance,
):
"""
Harmonise data in ``input_emissions_file``, saving output in ``outdir``
``input_emissions_file`` should be a path to a file of emissions
``outdir`` should be a path a directory which already exists
"""
LOGGER = logging.getLogger("harmonize")
_setup_logging(LOGGER)
key_string = _get_key_string_and_log_outdir(input_emissions_file, outdir, LOGGER)
input_df = _load_emissions_convert_to_basic(input_emissions_file, LOGGER)
df = _input_checks(input_df, inputcheck, key_string, outdir)
df_harmonized = run_harmonization(
df, instance=harmonization_instance, prefix=prefix
)
if gwp:
df_harmonized = add_gwp100_kyoto_wrapper(
df_harmonized,
gwps=["AR5GWP100", "AR6GWP100"],
prefixes=[f"{prefix}|Harmonized|"],
)
df_harmonized.to_csv(os.path.join(outdir, "{}_harmonized.csv".format(key_string)))
@click.command(context_settings={"help_option_names": ["-h", "--help"]})
@input_emissions_file_arg
@outdir_arg
@input_check_option
@prefix_option
@gwp_def_false_option
@harmonization_instance_option
def create_infiller_database(
input_emissions_file,
outdir,
inputcheck,
prefix,
gwp,
harmonization_instance,
):
"""
Creates infiller database by harmonizing data in ``input_emissions_file``
which is followed by some common-sense vetting and then saved in ``outdir``
``input_emissions_file`` should be a path to a file of emissions
``outdir`` should be a path a directory which already exists
"""
LOGGER = logging.getLogger("create_infiller_database")
_setup_logging(LOGGER)
key_string = _get_key_string_and_log_outdir(input_emissions_file, outdir, LOGGER)
input_df = _load_emissions_convert_to_basic(input_emissions_file, LOGGER)
df = _input_checks(input_df, inputcheck, key_string, outdir)
df_harmonized = run_harmonization(
df, instance=harmonization_instance, prefix=prefix
)
df_infiller_database = infiller_vetting(df_harmonized)
if gwp:
df_infiller_database = add_gwp100_kyoto_wrapper(
df_infiller_database,
gwps=["AR5GWP100", "AR6GWP100"],
prefixes=[f"{prefix}|Harmonized|"],
)
# Calculate total emissions consistent with afolu and energy for the
# infilling database to avoid losing scenarios which have total, energy
# and afolu reported and total dropped in the checks stage. We could also
# do this at end of harmonization too, depending on whether we want
# the infilling to preference using total CO2 or only fossil CO2
LOGGER.info("Calculating CO2 totals where missing")
_, no_co2_totals = split_df(
df_infiller_database.filter(variable="*Harmonized*CO2*"),
variable=f"{prefix}|Harmonized|Emissions|CO2",
)
co2_totals = _add_variables(
no_co2_totals,
f"{prefix}|Harmonized|Emissions|CO2|Energy and Industrial Processes",
f"{prefix}|Harmonized|Emissions|CO2|AFOLU",
f"{prefix}|Harmonized|Emissions|CO2",
raise_if_mismatch=False,
)
df_infiller_database = df_infiller_database.append(co2_totals, inplace=False)
LOGGER.info("Saving output")
df_infiller_database.to_csv(
os.path.join(outdir, "{}_infillerdatabase.csv".format(key_string))
)
@click.command(context_settings={"help_option_names": ["-h", "--help"]})
@input_emissions_file_arg
@outdir_arg
@infilling_database_option
@prefix_option
@gwp_option
def infill(
input_emissions_file,
outdir,
infilling_database,
prefix,
gwp,
):
"""
Infill harmonized data in ``input_emissions_file``, saving output in ``outdir``
``input_emissions_file`` should be a path to a file of harmonized emissions
``outdir`` should be a path a directory which already exists
"""
LOGGER = logging.getLogger("infill")
_setup_logging(LOGGER)
LOGGER.info("Loading harmonized emissions from %s", input_emissions_file)
LOGGER.info("Output will be saved in %s", outdir)
harmonized = pyam.IamDataFrame(input_emissions_file)
harmonized_start_year = min(harmonized.year)
LOGGER.info("Calling infilling")
infilled, _, _ = run_infilling(
harmonized,
prefix=prefix,
database_filepath=infilling_database,
start_year=harmonized_start_year,
)
LOGGER.info("Post-processing for the climate model step")
infilled = postprocess_infilled_for_climate(
infilled, prefix=prefix, start_year=harmonized_start_year
)
if gwp:
infilled = add_gwp100_kyoto_wrapper(
infilled,
gwps=["AR5GWP100", "AR6GWP100"],
prefixes=[f"{prefix}|Infilled|"],
)
outfile = os.path.join(outdir, "infilled.csv")
LOGGER.info("Saving output to %s", outfile)
infilled.to_csv(outfile)
@click.command(context_settings={"help_option_names": ["-h", "--help"]})
@harmonizedinfilledemissions_arg
@outdir_arg
@num_cfgs_option
@hist_warming_option
@hist_warming_ref_period_option
@hist_warming_eval_period_option
@test_run_option
@model_option
@model_version_option
@magicc_extra_config_option
@fair_extra_config_option
@probabilistic_file_option
@scenario_batch_size_option
@prefix_option
@gwp_def_false_option
@nonco2_warming_option
@save_raw_climate_output_option
def clim_cli(
harmonizedinfilledemissions,
outdir,
num_cfgs,
historical_warming,
historical_warming_reference_period,
historical_warming_evaluation_period,
test_run,
model,
model_version,
magicc_extra_config,
fair_extra_config,
probabilistic_file,
scenario_batch_size,
prefix,
gwp,
co2_and_non_co2_warming,
save_raw_climate_output,
):
"""
Run the climate emulator step of the IPCC AR6 climate asessment workflow.
Example usage: ``python scripts/run_clim.py tests/test-data/workflow-fair/ex2_harmonized_infilled.csv output --model "fair" --model-version "1.6.2" --num-cfgs 2237 --probabilistic-file tests/test-data/fair-1.6.2-wg3-params-slim.json --fair-extra-config tests/test-data/fair-1.6.2-wg3-params-common.json --scenario-batch-size 4``
For more information, see the code description under
:func:`climate_assessment.climate.climate_assessment`.
"""
LOGGER = logging.getLogger("clim_cli")
_setup_logging(LOGGER)
# silence pyam loggers
logging.getLogger("pyam.core").setLevel(logging.CRITICAL)
logging.getLogger("pyam.utils").setLevel(logging.CRITICAL)
LOGGER.info("Outputs will be saved in: %s", outdir)
key_string = os.path.splitext(os.path.basename(harmonizedinfilledemissions))[0]
LOGGER.info("Outputs will be saved with the ID: %s", key_string)
check_hist_warming_period(historical_warming_reference_period)
check_hist_warming_period(historical_warming_evaluation_period)
LOGGER.info(
"Loading harmonized, infilled scenarios: %s", harmonizedinfilledemissions
)
df_infilled = pyam.IamDataFrame(harmonizedinfilledemissions)
df_climate = climate_assessment(
df_infilled,
key_string,
outdir,
magicc_extra_config=magicc_extra_config,
fair_extra_config=fair_extra_config,
model=model,
model_version=model_version,
probabilistic_file=probabilistic_file,
num_cfgs=num_cfgs,
historical_warming=historical_warming,
historical_warming_reference_period=historical_warming_reference_period,
historical_warming_evaluation_period=historical_warming_evaluation_period,
test_run=test_run,
scenario_batch_size=scenario_batch_size,
save_raw_output=save_raw_climate_output,
co2_and_non_co2_warming=co2_and_non_co2_warming,
prefix=prefix,
)
if df_climate is None:
LOGGER.error("Climate assessment failed, exiting")
return
LOGGER.info("Concatenating infilled df and climate df")
results = pyam.concat([df_infilled, df_climate])
if gwp:
LOGGER.info("Adding extra Kyoto Gases variables in GWP100 for each scenario")
results = add_gwp100_kyoto_wrapper(
results,
gwps=["AR5GWP100", "AR6GWP100"],
prefixes=[
"",
"AR6 climate diagnostics|Harmonized|",
"AR6 climate diagnostics|Infilled|",
],
)
LOGGER.info("write out raw output")
results.to_excel(os.path.join(outdir, str(key_string + "_" + "rawoutput.xlsx")))
LOGGER.info("COMPLETE")
def _postprocess_worker(fname, outdir, **kwargs):
"""
Helper function which takes a file that ends with "_rawoutput.xlsx" and the output
location, and then calls the function `do_postprocess(output, outdir, key_string, prefix)`.
"""
if not fname.endswith("_rawoutput.xlsx"):
raise AssertionError(fname)
output = pyam.IamDataFrame(fname)
key_string = os.path.basename(fname).replace("_rawoutput.xlsx", "")
return do_postprocess(output, outdir=outdir, key_string=key_string, **kwargs)
@click.command(context_settings={"help_option_names": ["-h", "--help"]})
@output_files_arg
@outdir_arg
@n_workers_option
@output_file_option
@prefix_all_out_variables_option
@kyoto_ghgs_option
@model_option
@model_version_option
@categorisation_option
@report_completeness_option
def postprocess(