From 87cc301f5a86409b7e714660bf926c53bac4ac51 Mon Sep 17 00:00:00 2001 From: JMGilbert Date: Wed, 24 Aug 2022 18:48:43 -0700 Subject: [PATCH 1/8] Create working options menu to run all EPA SCC configurations --- damage_fun_runs/Default_EPA_prototype.ipynb | 2021 ++++++++++++++++--- damage_fun_runs/Directory_setup.py | 51 + damage_fun_runs/command_line_scc.py | 253 +++ damage_fun_runs/master-CAMEL_m1_c0.20.yaml | 45 - 4 files changed, 2058 insertions(+), 312 deletions(-) create mode 100644 damage_fun_runs/Directory_setup.py create mode 100644 damage_fun_runs/command_line_scc.py diff --git a/damage_fun_runs/Default_EPA_prototype.ipynb b/damage_fun_runs/Default_EPA_prototype.ipynb index 86f4597..edd47c5 100644 --- a/damage_fun_runs/Default_EPA_prototype.ipynb +++ b/damage_fun_runs/Default_EPA_prototype.ipynb @@ -2,7 +2,7 @@ "cells": [ { "cell_type": "code", - "execution_count": 1, + "execution_count": 5, "id": "f37cf921-e5fb-4846-91ca-3c8de746ffeb", "metadata": {}, "outputs": [], @@ -10,12 +10,13 @@ "import xarray as xr\n", "import dscim\n", "import yaml\n", - "from dscim.menu.simple_storage import Climate, EconVars\n", + "from dscim.menu.simple_storage import Climate, EconVars, StackedDamages\n", "import pandas as pd\n", "import numpy as np\n", "from xarray.testing import assert_allclose\n", "from xarray.testing import assert_equal\n", "from itertools import product\n", + "from pathlib import Path\n", "\n", "import dask\n", "import logging\n", @@ -23,9 +24,7 @@ "from subprocess import CalledProcessError\n", "from abc import ABC, abstractmethod\n", "from dscim.descriptors import cachedproperty\n", - "from itertools import product\n", "from dscim.menu.decorators import save\n", - "from dscim.menu.simple_storage import StackedDamages, EconVars\n", "from dscim.utils.utils import (\n", " model_outputs,\n", " compute_damages,\n", @@ -38,7 +37,7 @@ }, { "cell_type": "code", - "execution_count": 12, + "execution_count": 59, "id": "ff05959f-b9e7-469d-addd-92635d9e65cb", "metadata": { "tags": [] @@ -50,54 +49,57 @@ "\n", "# function params should be all of the simple parameters that catually want to be changed by EPA\n", "\n", - "def epa_scc(eta = 2.0,\n", + "def epa_scc(sector = \"CAMEL_m1_c0.20\",\n", + " domestic = False,\n", + " eta = 2.0,\n", " rho = 0.0,\n", " pulse_year = 2020,\n", " discount_type = \"euler_ramsey\",\n", " menu_option = \"risk_aversion\",\n", " gases = ['CO2_Fossil', 'CH4', 'N2O'],\n", " weitzman_parameters = [0.1, 0.5],\n", - " fair_aggregation = [\"ce\", \"mean\", \"gwr_mean\", \"median\"],\n", - " ssps = ['SSP2', 'SSP3', 'SSP4']):\n", + " fair_aggregation = [\"mean\"]):\n", " \n", " USER = \"liruixue\"\n", " master = f\"/home/{USER}/repos/dscim-epa/damage_fun_runs/master-CAMEL_m1_c0.20.yaml\"\n", - "\n", " \n", " with open(master, \"r\") as stream:\n", " conf = yaml.safe_load(stream)\n", + " \n", + " if domestic:\n", + " econ = EconVars(\n", + " path_econ=f\"{conf['rffdata']['socioec_output']}/rff_USA_socioeconomics.nc4\"\n", + " )\n", + " else:\n", + " econ = EconVars(\n", + " path_econ=f\"{conf['rffdata']['socioec_output']}/rff_global_socioeconomics.nc4\"\n", + " )\n", " \n", " conf[\"global_parameters\"] = {'fair_aggregation': fair_aggregation,\n", " 'subset_dict': {'ssp': ssps},\n", " 'weitzman_parameter': weitzman_parameters,\n", " 'save_files': []}\n", "\n", - " conf['global_parameters']['fair_aggregation'] \n", - " save_path = \"/home/liruixue/replication_newcode/\"\n", - "\n", - "\n", - " econ = EconVars(\n", - " path_econ=\"/shares/gcp/integration_replication/inputs/econ/rff_global_socioeconomics.nc4\"\n", - " )\n", - "\n", " MENU_OPTIONS = {\n", " \"adding_up\": dscim.menu.baseline.Baseline,\n", " \"risk_aversion\": dscim.menu.risk_aversion.RiskAversionRecipe,\n", " \"equity\": dscim.menu.equity.EquityRecipe,\n", " }\n", + " print(sector)\n", " add_kwargs = {\n", " \"econ_vars\": econ,\n", " \"climate_vars\": Climate(**conf[\"rff_climate\"], pulse_year=pulse_year),\n", - " \"formula\": 'damages ~ -1 + anomaly + np.power(anomaly, 2) + gmsl + np.power(gmsl, 2)',\n", + " \"formula\": conf[\"sectors\"][sector if not domestic else sector[:-4]][\"formula\"],\n", " \"discounting_type\": discount_type,\n", - " \"sector\": \"CAMEL_m1_c0.20\",\n", + " \"sector\": sector,\n", " \"ce_path\": None,\n", - " \"save_path\": save_path,\n", + " \"save_path\": None,\n", " \"eta\": eta,\n", " \"rho\": rho,\n", - " \"damage_function_path\": \"/shares/gcp/integration_replication/results/rff/CAMEL_m1_c0.20/2020\",\n", + " \"damage_function_path\": Path(conf['paths']['rff_damage_function_library']) / sector / str(2020),\n", " \"ecs_mask_path\": None,\n", " \"ecs_mask_name\": None,\n", + " \"fair_dims\":[],\n", " }\n", "\n", " kwargs = conf[\"global_parameters\"].copy()\n", @@ -114,69 +116,79 @@ " return(menu_item)\n", " \n", "# This represents the full gamut of scc runs when run default\n", - "def epa_sccs(etas_rhos = [[2.0, 0.0],\n", - " [1.016010255, 9.149608e-05],\n", + "def epa_sccs(sectors =[\"CAMEL_m1_c0.20\"],\n", + " etas_rhos = [[1.016010255, 9.149608e-05],\n", " [1.244459066, 0.00197263997],\n", - " [1.421158116, 0.00461878399],\n", - " [1.567899395, 0.00770271076]],\n", - " risk_combos = [['risk_aversion', 'euler_ramsey'],\n", - " ['adding_up','euler_ramsey']],\n", - " pulse_year = 2020,\n", + " [1.421158116, 0.00461878399]],\n", + " risk_combos = [['risk_aversion', 'euler_ramsey']],\n", + " pulse_years = [2020,2030,2040,2050,2060,2070,2080],\n", " gases = ['CO2_Fossil', 'CH4', 'N2O'],\n", - " weitzman_parameters = [0.1, 0.5],\n", - " fair_aggregation = [None],\n", - " ssps = ['SSP2', 'SSP3', 'SSP4']):\n", + " weitzman_parameters = [0.5],\n", + " fair_aggregation = [\"mean\"]):\n", "\n", - " coords = {\"eta_rhos\": [str(i) + \"_\" + str(j) for i,j in etas_rhos],\n", - " \"discount_type\": [j for i,j in risk_combos],\n", - " \"runid\": range(1,10001),\n", - " \"gas\": ['CO2_Fossil', 'CH4', 'N2O'],\n", - " \"simulation\": [1],\n", - " \"weitzman_parameter\":['0.1','0.5'],\n", - " \"fair_aggregation\": ['uncollapsed'],\n", - " \"menu_option\": [i for i,j in risk_combos]\n", - " }\n", - " all_arrays_uscc = []\n", - " all_arrays_gcnp = []\n", - " for i, j in product(etas_rhos, risk_combos):\n", - " eta = i[0]\n", - " rho = i[1]\n", + "\n", + " for j, sector in product(risk_combos, sectors):\n", + " all_arrays_uscc = []\n", + " all_arrays_gcnp = []\n", " discount_type= j[1]\n", " menu_option = j[0]\n", - " df_single = epa_scc(discount_type = discount_type,\n", - " menu_option = menu_option,\n", - " eta = eta,\n", - " rho = rho,\n", - " pulse_year = 2020,\n", - " gases = gases,\n", - " weitzman_parameters = weitzman_parameters,\n", - " fair_aggregation = fair_aggregation,\n", - " ssps = ssps)\n", - " \n", - " df_scc = df_single.uncollapsed_sccs.assign_coords(eta_rhos = str(eta) + \"_\" + str(rho), menu_option = menu_option)\n", - " df_scc_expanded = df_scc.expand_dims(['eta_rhos','menu_option'])\n", - " df_scc_expanded.name = \"uncollapsed_sccs\"\n", - " df_scc_expanded = df_scc_expanded.to_dataset()\n", - " all_arrays_uscc = all_arrays_uscc + [df_scc_expanded]\n", - " \n", - " df_gcnp = df_single.global_consumption_no_pulse.assign_coords(eta_rhos = str(eta) + \"_\" + str(rho), menu_option = menu_option)\n", - " df_gcnp_expanded = df_gcnp.expand_dims(['eta_rhos','menu_option'])\n", - " df_gcnp_expanded.name = \"global_consumption_no_pulse\"\n", - " df_gcnp_expanded = df_gcnp_expanded.to_dataset()\n", - " all_arrays_gcnp = all_arrays_gcnp + [df_gcnp_expanded]\n", - " \n", - " df_full_scc = xr.combine_by_coords(all_arrays_uscc)\n", - " df_full_scc.to_netcdf(\"/home/liruixue/replication_newcode/full_order_uncollapsed_sccs.nc4\") \n", - " \n", - " df_full_gcnp = xr.combine_by_coords(all_arrays_gcnp)\n", - " df_full_gcnp.to_netcdf(\"/home/liruixue/replication_newcode/full_order_global_consumption_no_pulse.nc4\") \n", + " for i, pulse_year in product(etas_rhos, pulse_years):\n", + " eta = i[0]\n", + " rho = i[1]\n", + " df_single = epa_scc(sector = sector,\n", + " discount_type = discount_type,\n", + " menu_option = menu_option,\n", + " eta = eta,\n", + " rho = rho,\n", + " pulse_year = pulse_year,\n", + " gases = gases,\n", + " weitzman_parameters = weitzman_parameters,\n", + " fair_aggregation = fair_aggregation)\n", + "\n", + " df_scc = df_single.uncollapsed_sccs.assign_coords(eta_rhos = str(eta) + \"_\" + str(rho), menu_option = menu_option, pulse_year = pulse_year)\n", + " df_scc_expanded = df_scc.expand_dims(['eta_rhos','menu_option','pulse_year'])\n", + " df_scc_expanded.name = \"uncollapsed_sccs\"\n", + " df_scc_expanded = df_scc_expanded.to_dataset()\n", + " all_arrays_uscc = all_arrays_uscc + [df_scc_expanded]\n", + "\n", + " df_gcnp = df_single.global_consumption_no_pulse.assign_coords(eta_rhos = str(eta) + \"_\" + str(rho), menu_option = menu_option, pulse_year = pulse_year)\n", + " df_gcnp_expanded = df_gcnp.expand_dims(['eta_rhos','menu_option','pulse_year'])\n", + " df_gcnp_expanded.name = \"global_consumption_no_pulse\"\n", + " df_gcnp_expanded = df_gcnp_expanded.to_dataset()\n", + " all_arrays_gcnp = all_arrays_gcnp + [df_gcnp_expanded]\n", + "\n", + " df_full_scc = xr.combine_by_coords(all_arrays_uscc)\n", + " df_full_scc.to_netcdf(Path(\"/home/liruixue/replication_newcode/\") / sector / (\"full_order_uncollapsed_sccs_\" + menu_option + \".nc4\")) \n", + "\n", + " df_full_gcnp = xr.combine_by_coords(all_arrays_gcnp)\n", + " df_full_gcnp.to_netcdf(Path(\"/home/liruixue/replication_newcode/\") / sector / (\"full_order_global_consumption_no_pulse_\" + menu_option + \".nc4\")) \n", "\n", "\n" ] }, { "cell_type": "code", - "execution_count": 13, + "execution_count": 67, + "id": "160b76c0-80c6-4660-a119-4b3afe217ed1", + "metadata": {}, + "outputs": [ + { + "ename": "FileNotFoundError", + "evalue": "[Errno 2] No such file or directory: 'generated_conf.yaml'", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mFileNotFoundError\u001b[0m Traceback (most recent call last)", + "\u001b[0;32m/tmp/ipykernel_181402/1085875431.py\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[1;32m 2\u001b[0m \u001b[0mconf1\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0myaml\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0msafe_load\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mstream\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 3\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 4\u001b[0;31m \u001b[0;32mwith\u001b[0m \u001b[0mopen\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"generated_conf.yaml\"\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m\"r\"\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;32mas\u001b[0m \u001b[0mstream\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 5\u001b[0m \u001b[0mconf2\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0myaml\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0msafe_load\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mstream\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 6\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n", + "\u001b[0;31mFileNotFoundError\u001b[0m: [Errno 2] No such file or directory: 'generated_conf.yaml'" + ] + } + ], + "source": [] + }, + { + "cell_type": "code", + "execution_count": 60, "id": "35b7bd68-c731-442c-9a44-aa4db2015fdd", "metadata": {}, "outputs": [ @@ -184,57 +196,82 @@ "name": "stdout", "output_type": "stream", "text": [ + "CAMEL_m1_c0.20\n", "\n", " Executing \n", " Running risk_aversion\n", " sector: CAMEL_m1_c0.20\n", " discounting: euler_ramsey\n", - " eta: 2.0\n", - " rho: 0.0\n", + " eta: 1.016010255\n", + " rho: 9.149608e-05\n", " \n", "Processing damage functions ...\n", - "Skipped saving damage_function_coefficients.\n", "Subsetting on {} keys.\n", "Global consumption found up to 2300.\n", - "Skipped saving global_consumption.\n", - "Skipped saving damage_function_coefficients.\n", - "Skipped saving global_consumption_no_pulse.\n", "Processing SCC calculation ...\n", "Subsetting on {} keys.\n", - "Skipped saving discount_factors.\n", - "Skipped saving damage_function_coefficients.\n", - "Skipped saving global_consumption_pulse.\n", - "Skipped saving marginal_damages.\n", - "Skipped saving scc.\n", - "Skipped saving uncollapsed_sccs.\n", - "Skipped saving uncollapsed_marginal_damages.\n", - "Skipped saving uncollapsed_discount_factors.\n", - "Results available: /home/liruixue/replication_newcode/\n", + "Results available: None\n", + "CAMEL_m1_c0.20\n", "\n", " Executing \n", " Running risk_aversion\n", " sector: CAMEL_m1_c0.20\n", - " discounting: constant\n", - " eta: 2.0\n", - " rho: 0.0\n", + " discounting: euler_ramsey\n", + " eta: 1.016010255\n", + " rho: 9.149608e-05\n", + " \n", + "Processing damage functions ...\n", + "Subsetting on {} keys.\n", + "Global consumption found up to 2300.\n", + "Processing SCC calculation ...\n", + "Subsetting on {} keys.\n", + "Results available: None\n", + "CAMEL_m1_c0.20\n", + "\n", + " Executing \n", + " Running risk_aversion\n", + " sector: CAMEL_m1_c0.20\n", + " discounting: euler_ramsey\n", + " eta: 1.016010255\n", + " rho: 9.149608e-05\n", + " \n", + "Processing damage functions ...\n", + "Subsetting on {} keys.\n", + "Global consumption found up to 2300.\n", + "Processing SCC calculation ...\n", + "Subsetting on {} keys.\n", + "Results available: None\n", + "CAMEL_m1_c0.20\n", + "\n", + " Executing \n", + " Running risk_aversion\n", + " sector: CAMEL_m1_c0.20\n", + " discounting: euler_ramsey\n", + " eta: 1.016010255\n", + " rho: 9.149608e-05\n", + " \n", + "Processing damage functions ...\n", + "Subsetting on {} keys.\n", + "Global consumption found up to 2300.\n", + "Processing SCC calculation ...\n", + "Subsetting on {} keys.\n", + "Results available: None\n", + "CAMEL_m1_c0.20\n", + "\n", + " Executing \n", + " Running risk_aversion\n", + " sector: CAMEL_m1_c0.20\n", + " discounting: euler_ramsey\n", + " eta: 1.016010255\n", + " rho: 9.149608e-05\n", " \n", "Processing damage functions ...\n", - "Skipped saving damage_function_coefficients.\n", "Subsetting on {} keys.\n", "Global consumption found up to 2300.\n", - "Skipped saving global_consumption.\n", - "Skipped saving damage_function_coefficients.\n", - "Skipped saving global_consumption_no_pulse.\n", "Processing SCC calculation ...\n", - "Skipped saving damage_function_coefficients.\n", - "Skipped saving global_consumption_pulse.\n", - "Skipped saving marginal_damages.\n", - "Skipped saving scc.\n", - "Skipped saving uncollapsed_sccs.\n", - "Skipped saving uncollapsed_marginal_damages.\n", "Subsetting on {} keys.\n", - "Skipped saving uncollapsed_discount_factors.\n", - "Results available: /home/liruixue/replication_newcode/\n", + "Results available: None\n", + "CAMEL_m1_c0.20\n", "\n", " Executing \n", " Running risk_aversion\n", @@ -244,48 +281,102 @@ " rho: 9.149608e-05\n", " \n", "Processing damage functions ...\n", - "Skipped saving damage_function_coefficients.\n", "Subsetting on {} keys.\n", "Global consumption found up to 2300.\n", - "Skipped saving global_consumption.\n", - "Skipped saving damage_function_coefficients.\n", - "Skipped saving global_consumption_no_pulse.\n", "Processing SCC calculation ...\n", "Subsetting on {} keys.\n", - "Skipped saving discount_factors.\n", - "Skipped saving damage_function_coefficients.\n", - "Skipped saving global_consumption_pulse.\n", - "Skipped saving marginal_damages.\n", - "Skipped saving scc.\n", - "Skipped saving uncollapsed_sccs.\n", - "Skipped saving uncollapsed_marginal_damages.\n", - "Skipped saving uncollapsed_discount_factors.\n", - "Results available: /home/liruixue/replication_newcode/\n", + "Results available: None\n", + "CAMEL_m1_c0.20\n", "\n", " Executing \n", " Running risk_aversion\n", " sector: CAMEL_m1_c0.20\n", - " discounting: constant\n", + " discounting: euler_ramsey\n", " eta: 1.016010255\n", " rho: 9.149608e-05\n", " \n", "Processing damage functions ...\n", - "Skipped saving damage_function_coefficients.\n", "Subsetting on {} keys.\n", "Global consumption found up to 2300.\n", - "Skipped saving global_consumption.\n", - "Skipped saving damage_function_coefficients.\n", - "Skipped saving global_consumption_no_pulse.\n", "Processing SCC calculation ...\n", - "Skipped saving damage_function_coefficients.\n", - "Skipped saving global_consumption_pulse.\n", - "Skipped saving marginal_damages.\n", - "Skipped saving scc.\n", - "Skipped saving uncollapsed_sccs.\n", - "Skipped saving uncollapsed_marginal_damages.\n", "Subsetting on {} keys.\n", - "Skipped saving uncollapsed_discount_factors.\n", - "Results available: /home/liruixue/replication_newcode/\n", + "Results available: None\n", + "CAMEL_m1_c0.20\n", + "\n", + " Executing \n", + " Running risk_aversion\n", + " sector: CAMEL_m1_c0.20\n", + " discounting: euler_ramsey\n", + " eta: 1.244459066\n", + " rho: 0.00197263997\n", + " \n", + "Processing damage functions ...\n", + "Subsetting on {} keys.\n", + "Global consumption found up to 2300.\n", + "Processing SCC calculation ...\n", + "Subsetting on {} keys.\n", + "Results available: None\n", + "CAMEL_m1_c0.20\n", + "\n", + " Executing \n", + " Running risk_aversion\n", + " sector: CAMEL_m1_c0.20\n", + " discounting: euler_ramsey\n", + " eta: 1.244459066\n", + " rho: 0.00197263997\n", + " \n", + "Processing damage functions ...\n", + "Subsetting on {} keys.\n", + "Global consumption found up to 2300.\n", + "Processing SCC calculation ...\n", + "Subsetting on {} keys.\n", + "Results available: None\n", + "CAMEL_m1_c0.20\n", + "\n", + " Executing \n", + " Running risk_aversion\n", + " sector: CAMEL_m1_c0.20\n", + " discounting: euler_ramsey\n", + " eta: 1.244459066\n", + " rho: 0.00197263997\n", + " \n", + "Processing damage functions ...\n", + "Subsetting on {} keys.\n", + "Global consumption found up to 2300.\n", + "Processing SCC calculation ...\n", + "Subsetting on {} keys.\n", + "Results available: None\n", + "CAMEL_m1_c0.20\n", + "\n", + " Executing \n", + " Running risk_aversion\n", + " sector: CAMEL_m1_c0.20\n", + " discounting: euler_ramsey\n", + " eta: 1.244459066\n", + " rho: 0.00197263997\n", + " \n", + "Processing damage functions ...\n", + "Subsetting on {} keys.\n", + "Global consumption found up to 2300.\n", + "Processing SCC calculation ...\n", + "Subsetting on {} keys.\n", + "Results available: None\n", + "CAMEL_m1_c0.20\n", + "\n", + " Executing \n", + " Running risk_aversion\n", + " sector: CAMEL_m1_c0.20\n", + " discounting: euler_ramsey\n", + " eta: 1.244459066\n", + " rho: 0.00197263997\n", + " \n", + "Processing damage functions ...\n", + "Subsetting on {} keys.\n", + "Global consumption found up to 2300.\n", + "Processing SCC calculation ...\n", + "Subsetting on {} keys.\n", + "Results available: None\n", + "CAMEL_m1_c0.20\n", "\n", " Executing \n", " Running risk_aversion\n", @@ -295,48 +386,87 @@ " rho: 0.00197263997\n", " \n", "Processing damage functions ...\n", - "Skipped saving damage_function_coefficients.\n", "Subsetting on {} keys.\n", "Global consumption found up to 2300.\n", - "Skipped saving global_consumption.\n", - "Skipped saving damage_function_coefficients.\n", - "Skipped saving global_consumption_no_pulse.\n", "Processing SCC calculation ...\n", "Subsetting on {} keys.\n", - "Skipped saving discount_factors.\n", - "Skipped saving damage_function_coefficients.\n", - "Skipped saving global_consumption_pulse.\n", - "Skipped saving marginal_damages.\n", - "Skipped saving scc.\n", - "Skipped saving uncollapsed_sccs.\n", - "Skipped saving uncollapsed_marginal_damages.\n", - "Skipped saving uncollapsed_discount_factors.\n", - "Results available: /home/liruixue/replication_newcode/\n", + "Results available: None\n", + "CAMEL_m1_c0.20\n", "\n", " Executing \n", " Running risk_aversion\n", " sector: CAMEL_m1_c0.20\n", - " discounting: constant\n", + " discounting: euler_ramsey\n", " eta: 1.244459066\n", " rho: 0.00197263997\n", " \n", "Processing damage functions ...\n", - "Skipped saving damage_function_coefficients.\n", "Subsetting on {} keys.\n", "Global consumption found up to 2300.\n", - "Skipped saving global_consumption.\n", - "Skipped saving damage_function_coefficients.\n", - "Skipped saving global_consumption_no_pulse.\n", "Processing SCC calculation ...\n", - "Skipped saving damage_function_coefficients.\n", - "Skipped saving global_consumption_pulse.\n", - "Skipped saving marginal_damages.\n", - "Skipped saving scc.\n", - "Skipped saving uncollapsed_sccs.\n", - "Skipped saving uncollapsed_marginal_damages.\n", "Subsetting on {} keys.\n", - "Skipped saving uncollapsed_discount_factors.\n", - "Results available: /home/liruixue/replication_newcode/\n", + "Results available: None\n", + "CAMEL_m1_c0.20\n", + "\n", + " Executing \n", + " Running risk_aversion\n", + " sector: CAMEL_m1_c0.20\n", + " discounting: euler_ramsey\n", + " eta: 1.421158116\n", + " rho: 0.00461878399\n", + " \n", + "Processing damage functions ...\n", + "Subsetting on {} keys.\n", + "Global consumption found up to 2300.\n", + "Processing SCC calculation ...\n", + "Subsetting on {} keys.\n", + "Results available: None\n", + "CAMEL_m1_c0.20\n", + "\n", + " Executing \n", + " Running risk_aversion\n", + " sector: CAMEL_m1_c0.20\n", + " discounting: euler_ramsey\n", + " eta: 1.421158116\n", + " rho: 0.00461878399\n", + " \n", + "Processing damage functions ...\n", + "Subsetting on {} keys.\n", + "Global consumption found up to 2300.\n", + "Processing SCC calculation ...\n", + "Subsetting on {} keys.\n", + "Results available: None\n", + "CAMEL_m1_c0.20\n", + "\n", + " Executing \n", + " Running risk_aversion\n", + " sector: CAMEL_m1_c0.20\n", + " discounting: euler_ramsey\n", + " eta: 1.421158116\n", + " rho: 0.00461878399\n", + " \n", + "Processing damage functions ...\n", + "Subsetting on {} keys.\n", + "Global consumption found up to 2300.\n", + "Processing SCC calculation ...\n", + "Subsetting on {} keys.\n", + "Results available: None\n", + "CAMEL_m1_c0.20\n", + "\n", + " Executing \n", + " Running risk_aversion\n", + " sector: CAMEL_m1_c0.20\n", + " discounting: euler_ramsey\n", + " eta: 1.421158116\n", + " rho: 0.00461878399\n", + " \n", + "Processing damage functions ...\n", + "Subsetting on {} keys.\n", + "Global consumption found up to 2300.\n", + "Processing SCC calculation ...\n", + "Subsetting on {} keys.\n", + "Results available: None\n", + "CAMEL_m1_c0.20\n", "\n", " Executing \n", " Running risk_aversion\n", @@ -346,126 +476,1405 @@ " rho: 0.00461878399\n", " \n", "Processing damage functions ...\n", - "Skipped saving damage_function_coefficients.\n", "Subsetting on {} keys.\n", "Global consumption found up to 2300.\n", - "Skipped saving global_consumption.\n", - "Skipped saving damage_function_coefficients.\n", - "Skipped saving global_consumption_no_pulse.\n", "Processing SCC calculation ...\n", "Subsetting on {} keys.\n", - "Skipped saving discount_factors.\n", - "Skipped saving damage_function_coefficients.\n", - "Skipped saving global_consumption_pulse.\n", - "Skipped saving marginal_damages.\n", - "Skipped saving scc.\n", - "Skipped saving uncollapsed_sccs.\n", - "Skipped saving uncollapsed_marginal_damages.\n", - "Skipped saving uncollapsed_discount_factors.\n", - "Results available: /home/liruixue/replication_newcode/\n", + "Results available: None\n", + "CAMEL_m1_c0.20\n", "\n", " Executing \n", " Running risk_aversion\n", " sector: CAMEL_m1_c0.20\n", - " discounting: constant\n", + " discounting: euler_ramsey\n", " eta: 1.421158116\n", " rho: 0.00461878399\n", " \n", "Processing damage functions ...\n", - "Skipped saving damage_function_coefficients.\n", "Subsetting on {} keys.\n", "Global consumption found up to 2300.\n", - "Skipped saving global_consumption.\n", - "Skipped saving damage_function_coefficients.\n", - "Skipped saving global_consumption_no_pulse.\n", "Processing SCC calculation ...\n", - "Skipped saving damage_function_coefficients.\n", - "Skipped saving global_consumption_pulse.\n", - "Skipped saving marginal_damages.\n", - "Skipped saving scc.\n", - "Skipped saving uncollapsed_sccs.\n", - "Skipped saving uncollapsed_marginal_damages.\n", "Subsetting on {} keys.\n", - "Skipped saving uncollapsed_discount_factors.\n", - "Results available: /home/liruixue/replication_newcode/\n", + "Results available: None\n", + "CAMEL_m1_c0.20\n", "\n", " Executing \n", " Running risk_aversion\n", " sector: CAMEL_m1_c0.20\n", " discounting: euler_ramsey\n", - " eta: 1.567899395\n", - " rho: 0.00770271076\n", + " eta: 1.421158116\n", + " rho: 0.00461878399\n", " \n", "Processing damage functions ...\n", - "Skipped saving damage_function_coefficients.\n", "Subsetting on {} keys.\n", "Global consumption found up to 2300.\n", - "Skipped saving global_consumption.\n", - "Skipped saving damage_function_coefficients.\n", - "Skipped saving global_consumption_no_pulse.\n", "Processing SCC calculation ...\n", "Subsetting on {} keys.\n", - "Skipped saving discount_factors.\n", - "Skipped saving damage_function_coefficients.\n", - "Skipped saving global_consumption_pulse.\n", - "Skipped saving marginal_damages.\n", - "Skipped saving scc.\n", - "Skipped saving uncollapsed_sccs.\n", - "Skipped saving uncollapsed_marginal_damages.\n", - "Skipped saving uncollapsed_discount_factors.\n", - "Results available: /home/liruixue/replication_newcode/\n", + "Results available: None\n" + ] + } + ], + "source": [ + "sector = ['CAMEL_m1_c0.20'] # Default\n", + "etas_rhos = [[1.016010255, 9.149608e-05],\n", + " [1.244459066, 0.00197263997],\n", + " [1.421158116, 0.00461878399]] # Default\n", + "risk_combos = [['risk_aversion', 'euler_ramsey']] # Default\n", + "gases = ['CO2_Fossil', 'CH4', 'N2O'] # Default\n", + "weitzman_parameters = [0.5] # Default\n", + "test = epa_sccs(sector,\n", + " etas_rhos,\n", + " risk_combos,\n", + " pulse_years=[2020,2030,2040,2050,2060,2070,2080])" + ] + }, + { + "cell_type": "code", + "execution_count": 58, + "id": "fd71cade-fd68-4621-a725-bbb6499c0c32", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "CAMEL_m1_c0.20\n", "\n", " Executing \n", " Running risk_aversion\n", " sector: CAMEL_m1_c0.20\n", - " discounting: constant\n", - " eta: 1.567899395\n", - " rho: 0.00770271076\n", + " discounting: euler_ramsey\n", + " eta: 2.0\n", + " rho: 0.0\n", " \n", "Processing damage functions ...\n", - "Skipped saving damage_function_coefficients.\n", "Subsetting on {} keys.\n", "Global consumption found up to 2300.\n", - "Skipped saving global_consumption.\n", - "Skipped saving damage_function_coefficients.\n", - "Skipped saving global_consumption_no_pulse.\n", "Processing SCC calculation ...\n", - "Skipped saving damage_function_coefficients.\n", - "Skipped saving global_consumption_pulse.\n", - "Skipped saving marginal_damages.\n", - "Skipped saving scc.\n", - "Skipped saving uncollapsed_sccs.\n", - "Skipped saving uncollapsed_marginal_damages.\n", "Subsetting on {} keys.\n", - "Skipped saving uncollapsed_discount_factors.\n", - "Results available: /home/liruixue/replication_newcode/\n" + "Results available: None\n" ] } ], - "source": [ - "etas_rhos = [[2.0, 0.0],\n", - " [1.016010255, 9.149608e-05],\n", - " [1.244459066, 0.00197263997],\n", - " [1.421158116, 0.00461878399],\n", - " [1.567899395, 0.00770271076]]\n", - "#etas_rhos = [[2.0,0.0]]\n", - "risk_combos = [['risk_aversion', 'euler_ramsey'],\n", - " ['risk_aversion','constant']]\n", - "pulse_year = 2020\n", - "gases = ['CO2_Fossil', 'CH4', 'N2O']\n", - "weitzman_parameters = [0.1, 0.5]\n", - "fair_aggregation = [\"mean\"]\n", - "ssps = ['SSP2', 'SSP3', 'SSP4']\n", - "test = epa_sccs(etas_rhos,\n", - " risk_combos,\n", - " pulse_year=2020,\n", - " fair_aggregation = fair_aggregation) \n" - ] + "source": [ + "sector = \"CAMEL_m1_c0.20\"\n", + "domestic = False\n", + "eta = 2.0\n", + "rho = 0.0\n", + "pulse_year = 2020\n", + "discount_type = \"euler_ramsey\"\n", + "menu_option = \"risk_aversion\"\n", + "gases = ['CO2_Fossil', 'CH4', 'N2O']\n", + "weitzman_parameters = [0.1, 0.5]\n", + "fair_aggregation = [\"mean\"]\n", + "ssps = []\n", + "\n", + "USER = \"liruixue\"\n", + "master = f\"/home/{USER}/repos/dscim-epa/damage_fun_runs/master-CAMEL_m1_c0.20.yaml\"\n", + "\n", + "with open(master, \"r\") as stream:\n", + " conf = yaml.safe_load(stream)\n", + "\n", + "if domestic:\n", + " econ = EconVars(\n", + " path_econ=f\"{conf['rffdata']['socioec_output']}/rff_USA_socioeconomics.nc4\"\n", + " )\n", + "else:\n", + " econ = EconVars(\n", + " path_econ=f\"{conf['rffdata']['socioec_output']}/rff_global_socioeconomics.nc4\"\n", + " )\n", + "\n", + "conf[\"global_parameters\"] = {'fair_aggregation': fair_aggregation,\n", + " 'subset_dict': {'ssp': ssps},\n", + " 'weitzman_parameter': weitzman_parameters,\n", + " 'save_files': []}\n", + "\n", + "conf['global_parameters']['fair_aggregation'] \n", + "\n", + "fair_dims = []\n", + "MENU_OPTIONS = {\n", + " \"adding_up\": dscim.menu.baseline.Baseline,\n", + " \"risk_aversion\": dscim.menu.risk_aversion.RiskAversionRecipe,\n", + " \"equity\": dscim.menu.equity.EquityRecipe,\n", + "}\n", + "print(sector)\n", + "add_kwargs = {\n", + " \"econ_vars\": econ,\n", + " \"climate_vars\": Climate(**conf[\"rff_climate\"], pulse_year=pulse_year),\n", + " \"formula\": conf[\"sectors\"][sector if not domestic else sector[:-4]][\"formula\"],\n", + " \"discounting_type\": discount_type,\n", + " \"sector\": sector,\n", + " \"ce_path\": None,\n", + " \"save_path\": None,\n", + " \"eta\": eta,\n", + " \"rho\": rho,\n", + " \"damage_function_path\": Path(conf['paths']['rff_damage_function_library']) / sector / str(2020),\n", + " \"ecs_mask_path\": None,\n", + " \"ecs_mask_name\": None,\n", + " \"fair_dims\":[]\n", + "}\n", + "\n", + "kwargs = conf[\"global_parameters\"].copy()\n", + "for k, v in add_kwargs.items():\n", + " assert (\n", + " k not in kwargs.keys()\n", + " ), f\"{k} already set in config. Please check `global_parameters`.\"\n", + " kwargs.update({k: v})\n", + "\n", + "menu_item = MENU_OPTIONS[menu_option](**kwargs)\n", + "menu_item.order_plate(\"scc\")\n", + "uncollapsed_scc = menu_item.uncollapsed_sccs" + ] + }, + { + "cell_type": "code", + "execution_count": 61, + "id": "aea255ee-684a-4341-be82-ecd9ab63dd22", + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "
<xarray.Dataset>\n",
+       "Dimensions:                              (batch: 15, gcm: 33, ssp: 5, rcp: 2, model: 2, year: 90, region: 24378)\n",
+       "Coordinates:\n",
+       "  * batch                                (batch) <U7 'batch0' ... 'batch9'\n",
+       "  * gcm                                  (gcm) <U23 'surrogate_GFDL-ESM2G_06'...\n",
+       "  * model                                (model) object 'IIASA GDP' 'OECD Env...\n",
+       "  * rcp                                  (rcp) object 'rcp45' 'rcp85'\n",
+       "  * region                               (region) object 'CAN.1.2.28' ... 'BW...\n",
+       "  * ssp                                  (ssp) object 'SSP1' 'SSP2' ... 'SSP5'\n",
+       "  * year                                 (year) int32 2010 2011 ... 2098 2099\n",
+       "Data variables:\n",
+       "    delta_hybrid_damages_vsl_epa_scaled  (gcm, batch, ssp, rcp, model, year, region) float64 dask.array<chunksize=(1, 15, 1, 1, 1, 10, 24378), meta=np.ndarray>\n",
+       "    histclim_deaths_vsl_epa_scaled       (gcm, batch, ssp, rcp, model, year, region) float64 dask.array<chunksize=(1, 15, 1, 1, 1, 10, 24378), meta=np.ndarray>
" + ], + "text/plain": [ + "\n", + "Dimensions: (batch: 15, gcm: 33, ssp: 5, rcp: 2, model: 2, year: 90, region: 24378)\n", + "Coordinates:\n", + " * batch (batch) \n", + " histclim_deaths_vsl_epa_scaled (gcm, batch, ssp, rcp, model, year, region) float64 dask.array" + ] + }, + "execution_count": 61, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "xr.open_zarr('/shares/gcp/integration/float32/sectoral_ir_damages/mortality_data/impacts-darwin-montecarlo-damages-v1.zarr')" + ] + }, + { + "cell_type": "code", + "execution_count": 107, + "id": "a2f4c884-9ada-463d-90ef-b0f570c61332", + "metadata": {}, + "outputs": [ + { + "ename": "AssertionError", + "evalue": "\nNot equal to tolerance rtol=1e-08, atol=0\n\nMismatched elements: 15 / 150000 (0.01%)\nMax absolute difference: 1.84618402e-05\nMax relative difference: 7.52650979e-08\n x: array([[[[[ 1.797611e+02],\n [ 8.083851e+02],\n [ 4.570898e+04]],...\n y: array([[[[[ 1.797611e+02],\n [ 8.083851e+02],\n [ 4.570898e+04]],...", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mAssertionError\u001b[0m Traceback (most recent call last)", + "\u001b[0;32m/tmp/ipykernel_156585/1621371075.py\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[1;32m 14\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 15\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 16\u001b[0;31m \u001b[0mnp\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mtesting\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0massert_allclose\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0ma\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0mb\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0mrtol\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;36m1e-08\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", + " \u001b[0;31m[... skipping hidden 1 frame]\u001b[0m\n", + "\u001b[0;32m~/miniconda3/envs/dscim/lib/python3.8/site-packages/numpy/testing/_private/utils.py\u001b[0m in \u001b[0;36massert_array_compare\u001b[0;34m(comparison, x, y, err_msg, verbose, header, precision, equal_nan, equal_inf)\u001b[0m\n\u001b[1;32m 842\u001b[0m \u001b[0mverbose\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mverbose\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mheader\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mheader\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 843\u001b[0m names=('x', 'y'), precision=precision)\n\u001b[0;32m--> 844\u001b[0;31m \u001b[0;32mraise\u001b[0m \u001b[0mAssertionError\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mmsg\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 845\u001b[0m \u001b[0;32mexcept\u001b[0m \u001b[0mValueError\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 846\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0mtraceback\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", + "\u001b[0;31mAssertionError\u001b[0m: \nNot equal to tolerance rtol=1e-08, atol=0\n\nMismatched elements: 15 / 150000 (0.01%)\nMax absolute difference: 1.84618402e-05\nMax relative difference: 7.52650979e-08\n x: array([[[[[ 1.797611e+02],\n [ 8.083851e+02],\n [ 4.570898e+04]],...\n y: array([[[[[ 1.797611e+02],\n [ 8.083851e+02],\n [ 4.570898e+04]],..." + ] + } + ], + "source": [ + "test1 = (xr.open_dataset(\"/home/liruixue/replication_newcode/full_order_uncollapsed_sccseuler_ramsey.nc4\")\n", + " .sel(menu_option = 'risk_aversion', weitzman_parameter = '0.5',discount_type = 'euler_ramsey', drop = True)\n", + " .to_array()\n", + " .sel(variable = \"uncollapsed_sccs\"))\n", + "test1_compare = (xr.open_dataset(\"/home/liruixue/replication_newcode/compare/RFF-SP_allgas_risk_averse_ramsey_sccs_domesticFalse_unmasked_None_Apr272022.nc\")\n", + " .sel(discount_type = \"Ramsey\", version = \"RFF-SP-v2\", sector = 'CAMEL',pulse_year = 2020,weitzman = '0.5', drop = True)\n", + " .to_array()\n", + " .sel(variable = \"scc\")\n", + " .drop_vars('menu_option'))\n", + "\n", + "a, b = xr.align(test1, test1_compare, join=\"inner\")\n", + "c = (a - b) / b\n", + "d = np.abs(c.variable.values.flatten())\n", + "\n", + "\n", + "np.testing.assert_allclose(a,b,rtol = 1e-08)" + ] + }, + { + "cell_type": "code", + "execution_count": 93, + "id": "8ea44e54-e231-4949-948c-6a83093a2819", + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "
<xarray.Dataset>\n",
+       "Dimensions:             (discount_type: 1, runid: 10000, gas: 3, simulation: 1, weitzman_parameter: 2, fair_aggregation: 1, eta_rhos: 5, menu_option: 1)\n",
+       "Coordinates:\n",
+       "  * discount_type       (discount_type) object 'euler_ramsey'\n",
+       "  * runid               (runid) int64 1 2 3 4 5 6 ... 9996 9997 9998 9999 10000\n",
+       "  * gas                 (gas) object 'CO2_Fossil' 'CH4' 'N2O'\n",
+       "  * simulation          (simulation) int64 1\n",
+       "  * weitzman_parameter  (weitzman_parameter) object '0.1' '0.5'\n",
+       "  * fair_aggregation    (fair_aggregation) object 'uncollapsed'\n",
+       "  * eta_rhos            (eta_rhos) object '1.016010255_9.149608e-05' ... '2.0...\n",
+       "  * menu_option         (menu_option) object 'risk_aversion'\n",
+       "Data variables:\n",
+       "    uncollapsed_sccs    (eta_rhos, menu_option, fair_aggregation, weitzman_parameter, discount_type, runid, gas, simulation) float64 ...
" + ], + "text/plain": [ + "\n", + "Dimensions: (discount_type: 1, runid: 10000, gas: 3, simulation: 1, weitzman_parameter: 2, fair_aggregation: 1, eta_rhos: 5, menu_option: 1)\n", + "Coordinates:\n", + " * discount_type (discount_type) object 'euler_ramsey'\n", + " * runid (runid) int64 1 2 3 4 5 6 ... 9996 9997 9998 9999 10000\n", + " * gas (gas) object 'CO2_Fossil' 'CH4' 'N2O'\n", + " * simulation (simulation) int64 1\n", + " * weitzman_parameter (weitzman_parameter) object '0.1' '0.5'\n", + " * fair_aggregation (fair_aggregation) object 'uncollapsed'\n", + " * eta_rhos (eta_rhos) object '1.016010255_9.149608e-05' ... '2.0...\n", + " * menu_option (menu_option) object 'risk_aversion'\n", + "Data variables:\n", + " uncollapsed_sccs (eta_rhos, menu_option, fair_aggregation, weitzman_parameter, discount_type, runid, gas, simulation) float64 ..." + ] + }, + "execution_count": 93, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [] }, { "cell_type": "code", - "execution_count": 14, - "id": "a2f4c884-9ada-463d-90ef-b0f570c61332", + "execution_count": 100, + "id": "9c57f127-95a8-43c1-a7bd-736425686e14", "metadata": {}, "outputs": [ { @@ -823,10 +2232,10 @@ " fill: currentColor;\n", "}\n", "
<xarray.Dataset>\n",
-       "Dimensions:             (fair_aggregation: 1, discount_type: 2, runid: 10000, gas: 3, simulation: 1, weitzman_parameter: 2, discrate: 6, eta_rhos: 5, menu_option: 1)\n",
+       "Dimensions:             (fair_aggregation: 1, discount_type: 1, runid: 10000, gas: 3, simulation: 1, weitzman_parameter: 2, discrate: 6, eta_rhos: 5, menu_option: 1)\n",
        "Coordinates:\n",
        "  * fair_aggregation    (fair_aggregation) object 'uncollapsed'\n",
-       "  * discount_type       (discount_type) object 'constant' 'euler_ramsey'\n",
+       "  * discount_type       (discount_type) object 'constant'\n",
        "  * runid               (runid) int64 1 2 3 4 5 6 ... 9996 9997 9998 9999 10000\n",
        "  * gas                 (gas) object 'CO2_Fossil' 'CH4' 'N2O'\n",
        "  * simulation          (simulation) int64 1\n",
@@ -835,16 +2244,16 @@
        "  * eta_rhos            (eta_rhos) object '1.016010255_9.149608e-05' ... '2.0...\n",
        "  * menu_option         (menu_option) object 'risk_aversion'\n",
        "Data variables:\n",
-       "    uncollapsed_sccs    (eta_rhos, menu_option, discrate, fair_aggregation, weitzman_parameter, discount_type, runid, gas, simulation) float64 ...
    • uncollapsed_sccs
      (eta_rhos, menu_option, discrate, fair_aggregation, weitzman_parameter, discount_type, runid, gas, simulation)
      float64
      ...
      [1800000 values with dtype=float64]
  • " ], "text/plain": [ "\n", - "Dimensions: (fair_aggregation: 1, discount_type: 2, runid: 10000, gas: 3, simulation: 1, weitzman_parameter: 2, discrate: 6, eta_rhos: 5, menu_option: 1)\n", + "Dimensions: (fair_aggregation: 1, discount_type: 1, runid: 10000, gas: 3, simulation: 1, weitzman_parameter: 2, discrate: 6, eta_rhos: 5, menu_option: 1)\n", "Coordinates:\n", " * fair_aggregation (fair_aggregation) object 'uncollapsed'\n", - " * discount_type (discount_type) object 'constant' 'euler_ramsey'\n", + " * discount_type (discount_type) object 'constant'\n", " * runid (runid) int64 1 2 3 4 5 6 ... 9996 9997 9998 9999 10000\n", " * gas (gas) object 'CO2_Fossil' 'CH4' 'N2O'\n", " * simulation (simulation) int64 1\n", @@ -856,21 +2265,73 @@ " uncollapsed_sccs (eta_rhos, menu_option, discrate, fair_aggregation, weitzman_parameter, discount_type, runid, gas, simulation) float64 ..." ] }, - "execution_count": 14, + "execution_count": 100, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "test1 = xr.open_dataset(\"/home/liruixue/replication_newcode/full_order_uncollapsed_sccs.nc4\")\n", - "test1" + "xr.open_dataset(\"/home/liruixue/replication_newcode/full_order_uncollapsed_sccsconstant.nc4\")" ] }, { "cell_type": "code", - "execution_count": 15, + "execution_count": 104, "id": "24499fb7-d8b3-47c2-bb16-34e4e1179367", "metadata": {}, + "outputs": [ + { + "ename": "AssertionError", + "evalue": "\nNot equal to tolerance rtol=1e-08, atol=0\n\nMismatched elements: 22 / 150000 (0.0147%)\nMax absolute difference: 5.72018325e-06\nMax relative difference: 1.75003765e-07\n x: array([[[[[ 1.370883e+02],\n [ 5.964344e+02],\n [ 3.553115e+04]],...\n y: array([[[[[ 1.370883e+02],\n [ 5.964344e+02],\n [ 3.553115e+04]],...", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mAssertionError\u001b[0m Traceback (most recent call last)", + "\u001b[0;32m/tmp/ipykernel_156585/2792563010.py\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[1;32m 14\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 15\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 16\u001b[0;31m \u001b[0mnp\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mtesting\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0massert_allclose\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0ma\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0mb\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0mrtol\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;36m1e-08\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", + " \u001b[0;31m[... skipping hidden 1 frame]\u001b[0m\n", + "\u001b[0;32m~/miniconda3/envs/dscim/lib/python3.8/site-packages/numpy/testing/_private/utils.py\u001b[0m in \u001b[0;36massert_array_compare\u001b[0;34m(comparison, x, y, err_msg, verbose, header, precision, equal_nan, equal_inf)\u001b[0m\n\u001b[1;32m 842\u001b[0m \u001b[0mverbose\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mverbose\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mheader\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mheader\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 843\u001b[0m names=('x', 'y'), precision=precision)\n\u001b[0;32m--> 844\u001b[0;31m \u001b[0;32mraise\u001b[0m \u001b[0mAssertionError\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mmsg\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 845\u001b[0m \u001b[0;32mexcept\u001b[0m \u001b[0mValueError\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 846\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0mtraceback\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", + "\u001b[0;31mAssertionError\u001b[0m: \nNot equal to tolerance rtol=1e-08, atol=0\n\nMismatched elements: 22 / 150000 (0.0147%)\nMax absolute difference: 5.72018325e-06\nMax relative difference: 1.75003765e-07\n x: array([[[[[ 1.370883e+02],\n [ 5.964344e+02],\n [ 3.553115e+04]],...\n y: array([[[[[ 1.370883e+02],\n [ 5.964344e+02],\n [ 3.553115e+04]],..." + ] + } + ], + "source": [ + "test1 = (xr.open_dataset(\"/home/liruixue/replication_newcode/full_order_uncollapsed_sccs_euler_ramsey.nc4\")\n", + " .sel(menu_option = 'adding_up', weitzman_parameter = '0.5',discount_type = 'euler_ramsey', drop = True)\n", + " .to_array()\n", + " .sel(variable = \"uncollapsed_sccs\"))\n", + "test1_compare = (xr.open_dataset(\"/home/liruixue/replication_newcode/compare/RFF-SP_allgas_risk_neutral_ramsey_sccs_domesticFalse_unmasked_None_Apr272022.nc\")\n", + " .sel(discount_type = \"Ramsey\", version = \"RFF-SP-v2\", sector = 'CAMEL',pulse_year = 2020,weitzman = '0.5', drop = True)\n", + " .to_array()\n", + " .sel(variable = \"scc\")\n", + " .drop_vars('menu_option'))\n", + "\n", + "a, b = xr.align(test1, test1_compare, join=\"inner\")\n", + "c = (a - b) / b\n", + "d = np.abs(c.variable.values.flatten())\n", + "\n", + "\n", + "np.testing.assert_allclose(a,b,rtol = 1e-08)" + ] + }, + { + "cell_type": "code", + "execution_count": 57, + "id": "55ae2b22-86b3-43ae-bec8-7eda0d929fd2", + "metadata": {}, + "outputs": [], + "source": [ + "#xr.open_dataset(\"/shares/gcp/integration_replication/results/rff/agriculture/2020/risk_aversion_euler_ramsey_eta2.0_rho0.0_damage_function_coefficients.nc4\")\n", + "t = xr.open_dataset(\"/home/liruixue/replication_newcode/agriculture/full_order_uncollapsed_sccs_euler_ramsey.nc4\").sel(eta_rhos = '2.0_0.0', pulse_year = 2020, menu_option = 'adding_up', drop = True)\n", + "t1 = xr.open_dataset(\"/shares/gcp/integration_replication/results/rff/agriculture/2020/adding_up_euler_ramsey_eta2.0_rho0.0_uncollapsed_sccs.nc4\")\n", + "a,b = xr.align(t,t1,join ='inner')\n", + "np.testing.assert_allclose(a.to_array(),b.to_array(), rtol = 1e-100)\n" + ] + }, + { + "cell_type": "code", + "execution_count": 51, + "id": "39670173-4e77-4cce-96e9-4ad4c871e421", + "metadata": {}, "outputs": [ { "data": { @@ -1227,45 +2688,71 @@ " fill: currentColor;\n", "}\n", "
    <xarray.Dataset>\n",
    -       "Dimensions:                      (discount_type: 2, year: 281, runid: 10000, gas: 3, simulation: 1, weitzman_parameter: 2, eta_rhos: 5, menu_option: 1)\n",
    +       "Dimensions:             (discount_type: 1, runid: 10000, gas: 3, weitzman_parameter: 2, fair_aggregation: 1)\n",
            "Coordinates:\n",
    -       "  * discount_type                (discount_type) object 'constant' 'euler_ram...\n",
    -       "  * year                         (year) int64 2020 2021 2022 ... 2298 2299 2300\n",
    -       "  * runid                        (runid) int64 1 2 3 4 ... 9997 9998 9999 10000\n",
    -       "  * gas                          (gas) object 'CO2_Fossil' 'CH4' 'N2O'\n",
    -       "  * simulation                   (simulation) int64 1\n",
    -       "  * weitzman_parameter           (weitzman_parameter) object '0.1' '0.5'\n",
    -       "  * eta_rhos                     (eta_rhos) object '1.016010255_9.149608e-05'...\n",
    -       "  * menu_option                  (menu_option) object 'risk_aversion'\n",
    +       "  * discount_type       (discount_type) object 'euler_ramsey'\n",
    +       "  * runid               (runid) int64 1 2 3 4 5 6 ... 9996 9997 9998 9999 10000\n",
    +       "  * gas                 (gas) object 'CO2_Fossil' 'CH4' 'N2O'\n",
    +       "  * weitzman_parameter  (weitzman_parameter) object '0.1' '0.5'\n",
    +       "  * fair_aggregation    (fair_aggregation) object 'uncollapsed'\n",
            "Data variables:\n",
    -       "    global_consumption_no_pulse  (eta_rhos, menu_option, weitzman_parameter, discount_type, runid, year, gas, simulation) float64 ...
    " + " uncollapsed_sccs (fair_aggregation, weitzman_parameter, discount_type, runid, gas) float64 ...\n", + "Attributes: (12/49)\n", + " sector_path: /shares/gcp/integration/float32/input_dat...\n", + " save_path: /shares/gcp/integration_replication/resul...\n", + " gdppc_bottom_code: 39.39265060424805\n", + " subset_dict: {'ssp': ['SSP2', 'SSP3', 'SSP4']}\n", + " econ_vars: <dscim.menu.simple_storage.EconVars objec...\n", + " climate: <dscim.menu.simple_storage.Climate object...\n", + " ... ...\n", + " gases: ['CO2_Fossil', 'CH4', 'N2O']\n", + " base_period: [2001, 2010]\n", + " anomaly_vars: ['temperature', 'gmsl']\n", + " path: /shares/gcp/integration_replication/input...\n", + " machine: battuta\n", + " commit: b'0bdcf93'" ], "text/plain": [ "\n", - "Dimensions: (discount_type: 2, year: 281, runid: 10000, gas: 3, simulation: 1, weitzman_parameter: 2, eta_rhos: 5, menu_option: 1)\n", + "Dimensions: (discount_type: 1, runid: 10000, gas: 3, weitzman_parameter: 2, fair_aggregation: 1)\n", "Coordinates:\n", - " * discount_type (discount_type) object 'constant' 'euler_ram...\n", - " * year (year) int64 2020 2021 2022 ... 2298 2299 2300\n", - " * runid (runid) int64 1 2 3 4 ... 9997 9998 9999 10000\n", - " * gas (gas) object 'CO2_Fossil' 'CH4' 'N2O'\n", - " * simulation (simulation) int64 1\n", - " * weitzman_parameter (weitzman_parameter) object '0.1' '0.5'\n", - " * eta_rhos (eta_rhos) object '1.016010255_9.149608e-05'...\n", - " * menu_option (menu_option) object 'risk_aversion'\n", + " * discount_type (discount_type) object 'euler_ramsey'\n", + " * runid (runid) int64 1 2 3 4 5 6 ... 9996 9997 9998 9999 10000\n", + " * gas (gas) object 'CO2_Fossil' 'CH4' 'N2O'\n", + " * weitzman_parameter (weitzman_parameter) object '0.1' '0.5'\n", + " * fair_aggregation (fair_aggregation) object 'uncollapsed'\n", "Data variables:\n", - " global_consumption_no_pulse (eta_rhos, menu_option, weitzman_parameter, discount_type, runid, year, gas, simulation) float64 ..." + " uncollapsed_sccs (fair_aggregation, weitzman_parameter, discount_type, runid, gas) float64 ...\n", + "Attributes: (12/49)\n", + " sector_path: /shares/gcp/integration/float32/input_dat...\n", + " save_path: /shares/gcp/integration_replication/resul...\n", + " gdppc_bottom_code: 39.39265060424805\n", + " subset_dict: {'ssp': ['SSP2', 'SSP3', 'SSP4']}\n", + " econ_vars: Date: Mon, 29 Aug 2022 14:46:53 -0500 Subject: [PATCH 2/8] Update command_line_scc.py --- damage_fun_runs/command_line_scc.py | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/damage_fun_runs/command_line_scc.py b/damage_fun_runs/command_line_scc.py index 783dad7..732696e 100644 --- a/damage_fun_runs/command_line_scc.py +++ b/damage_fun_runs/command_line_scc.py @@ -26,6 +26,8 @@ ) import inquirer from pyfiglet import Figlet +from pathlib import Path +import os # Config vs function params: @@ -44,8 +46,7 @@ def epa_scc(sector = "CAMEL_m1_c0.20", weitzman_parameters = [0.5], fair_aggregation = ["mean"]): - USER = "liruixue" - master = f"/home/{USER}/repos/dscim-epa/damage_fun_runs/master-CAMEL_m1_c0.20.yaml" + master = Path(os.getcwd()) / "generated_conf.yml" with open(master, "r") as stream: conf = yaml.safe_load(stream) @@ -79,7 +80,7 @@ def epa_scc(sector = "CAMEL_m1_c0.20", "save_path": None, "eta": eta, "rho": rho, - "damage_function_path": Path(conf['paths']['rff_damage_function_library']) / sector / str(2020), + "damage_function_path": Path(conf['paths']['rff_damage_function_library']) / sector, "ecs_mask_path": None, "ecs_mask_name": None, "fair_dims":[], @@ -109,7 +110,9 @@ def epa_sccs(sectors =["CAMEL_m1_c0.20"], gases = ['CO2_Fossil', 'CH4', 'N2O'], weitzman_parameters = [0.5], fair_aggregation = ["mean"]): - + master = Path(os.getcwd()) / "generated_conf.yml" + with open(master, "r") as stream: + conf = yaml.safe_load(stream) for j, sector in product(risk_combos, sectors): all_arrays_uscc = [] @@ -143,10 +146,10 @@ def epa_sccs(sectors =["CAMEL_m1_c0.20"], all_arrays_gcnp = all_arrays_gcnp + [df_gcnp_expanded] df_full_scc = xr.combine_by_coords(all_arrays_uscc) - df_full_scc.to_netcdf(Path("/home/liruixue/replication_newcode/") / sector / ("full_order_uncollapsed_sccs_" + menu_option + ".nc4")) + df_full_scc.to_netcdf(Path(conf['save_path']) / sector / ("full_order_uncollapsed_sccs_" + menu_option + ".nc4")) df_full_gcnp = xr.combine_by_coords(all_arrays_gcnp) - df_full_gcnp.to_netcdf(Path("/home/liruixue/replication_newcode/") / sector / ("full_order_global_consumption_no_pulse_" + menu_option + ".nc4")) + df_full_gcnp.to_netcdf(Path(conf['save_path']) / sector / ("full_order_global_consumption_no_pulse_" + menu_option + ".nc4")) f = Figlet(font='slant') print(f.renderText('DSCIM')) @@ -188,7 +191,8 @@ def epa_sccs(sectors =["CAMEL_m1_c0.20"], ], default = [[1.016010255, 9.149608e-05], [1.244459066, 0.00197263997], - [1.421158116, 0.00461878399]]), + [1.421158116, 0.00461878399], + [1.567899395, 0.00770271076]]), inquirer.Checkbox("pulse_year", message= 'Select pulse years', choices= [ @@ -236,6 +240,8 @@ def epa_sccs(sectors =["CAMEL_m1_c0.20"], sector = [answers['sector']] pulse_years = answers['pulse_year'] domestic = answers['domestic'] +if domestic: + sector = [i + "_USA" for i in sector] print(etas_rhos) print(sector) print(pulse_years) @@ -243,11 +249,12 @@ def epa_sccs(sectors =["CAMEL_m1_c0.20"], if len(etas_rhos) == 0: raise ValueError('You must select at least one eta, rho combination') -risk_combos = [['risk_aversion', 'euler_ramsey']] # Default +risk_combos = [['risk_aversion', 'euler_ramsey'], + ['adding_up','constant']] # Default gases = ['CO2_Fossil', 'CH4', 'N2O'] # Default weitzman_parameters = [0.5] # Default epa_sccs(sector, domestic, etas_rhos, risk_combos, - pulse_years=pulse_years) \ No newline at end of file + pulse_years=pulse_years) From 41910b7dadbf51f2d22368c10c308a2002d23213 Mon Sep 17 00:00:00 2001 From: Jonah Gilbert Date: Mon, 29 Aug 2022 14:47:17 -0500 Subject: [PATCH 3/8] Update Directory_setup.py --- damage_fun_runs/Directory_setup.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/damage_fun_runs/Directory_setup.py b/damage_fun_runs/Directory_setup.py index 465d6c2..dc2b134 100644 --- a/damage_fun_runs/Directory_setup.py +++ b/damage_fun_runs/Directory_setup.py @@ -39,7 +39,8 @@ def makedir(path): 'energy': {'formula': 'damages ~ -1 + anomaly + np.power(anomaly, 2)'}, 'labor': {'formula': 'damages ~ -1 + anomaly + np.power(anomaly, 2)'}, 'AMEL_m1': {'formula': 'damages ~ -1 + anomaly + np.power(anomaly, 2)'}, - 'CAMEL_m1_c0.20': {'formula': 'damages ~ -1 + anomaly + np.power(anomaly, 2) + gmsl + np.power(gmsl, 2)'}}} + 'CAMEL_m1_c0.20': {'formula': 'damages ~ -1 + anomaly + np.power(anomaly, 2) + gmsl + np.power(gmsl, 2)'}}, + 'save_path': str(outputs)} sectors = list(conf_base['sectors'].keys()) for i in sectors: From 4a48de706003a1a7e4e9107f06d39089cd2b3f1e Mon Sep 17 00:00:00 2001 From: Jonah Gilbert Date: Tue, 30 Aug 2022 20:43:27 -0500 Subject: [PATCH 4/8] Update command_line_scc.py --- damage_fun_runs/command_line_scc.py | 25 ++++++++++++++++++------- 1 file changed, 18 insertions(+), 7 deletions(-) diff --git a/damage_fun_runs/command_line_scc.py b/damage_fun_runs/command_line_scc.py index 732696e..c869a88 100644 --- a/damage_fun_runs/command_line_scc.py +++ b/damage_fun_runs/command_line_scc.py @@ -29,6 +29,12 @@ from pathlib import Path import os +master = Path(os.getcwd()) / "generated_conf.yml" +try: + with open(master, "r") as stream: + conf = yaml.safe_load(stream) +except FileNotFoundError: + raise FileNotFoundError("Please run Directory_setup.py or place the config in your current working directory") # Config vs function params: # Config should have all paths and parameters that are messy @@ -146,10 +152,14 @@ def epa_sccs(sectors =["CAMEL_m1_c0.20"], all_arrays_gcnp = all_arrays_gcnp + [df_gcnp_expanded] df_full_scc = xr.combine_by_coords(all_arrays_uscc) - df_full_scc.to_netcdf(Path(conf['save_path']) / sector / ("full_order_uncollapsed_sccs_" + menu_option + ".nc4")) - + scc_path = Path(conf['save_path']) / sector / ("full_order_uncollapsed_sccs_" + menu_option + ".nc4") + df_full_scc.to_netcdf(scc_path) + print(f"SCCs are available in {str(scc_path)}") + df_full_gcnp = xr.combine_by_coords(all_arrays_gcnp) - df_full_gcnp.to_netcdf(Path(conf['save_path']) / sector / ("full_order_global_consumption_no_pulse_" + menu_option + ".nc4")) + gcnp_path = Path(conf['save_path']) / sector / ("full_order_global_consumption_no_pulse_" + menu_option + ".nc4") + df_full_gcnp.to_netcdf(gcnp_path) + print(f"GCNP is available in {gcnp_path}") f = Figlet(font='slant') print(f.renderText('DSCIM')) @@ -191,8 +201,7 @@ def epa_sccs(sectors =["CAMEL_m1_c0.20"], ], default = [[1.016010255, 9.149608e-05], [1.244459066, 0.00197263997], - [1.421158116, 0.00461878399], - [1.567899395, 0.00770271076]]), + [1.421158116, 0.00461878399]]), inquirer.Checkbox("pulse_year", message= 'Select pulse years', choices= [ @@ -249,8 +258,7 @@ def epa_sccs(sectors =["CAMEL_m1_c0.20"], if len(etas_rhos) == 0: raise ValueError('You must select at least one eta, rho combination') -risk_combos = [['risk_aversion', 'euler_ramsey'], - ['adding_up','constant']] # Default +risk_combos = [['risk_aversion', 'euler_ramsey']] # Default gases = ['CO2_Fossil', 'CH4', 'N2O'] # Default weitzman_parameters = [0.5] # Default epa_sccs(sector, @@ -258,3 +266,6 @@ def epa_sccs(sectors =["CAMEL_m1_c0.20"], etas_rhos, risk_combos, pulse_years=pulse_years) + + +print(f"Full combined results are available in {str(Path(conf['save_path']) / sector[0])}") From 3ec6a840cadfd46d82c988f144bbd9f18b967af5 Mon Sep 17 00:00:00 2001 From: Jonah Gilbert Date: Tue, 30 Aug 2022 20:43:44 -0500 Subject: [PATCH 5/8] Update Directory_setup.py --- damage_fun_runs/Directory_setup.py | 1 + 1 file changed, 1 insertion(+) diff --git a/damage_fun_runs/Directory_setup.py b/damage_fun_runs/Directory_setup.py index dc2b134..8d02796 100644 --- a/damage_fun_runs/Directory_setup.py +++ b/damage_fun_runs/Directory_setup.py @@ -45,6 +45,7 @@ def makedir(path): sectors = list(conf_base['sectors'].keys()) for i in sectors: makedir(outputs / i) + makedir(outputs / (i + "_USA")) # Download inputs from internet From 1494efd0601acc0ba4b2a314de8dff7cfc24807e Mon Sep 17 00:00:00 2001 From: Jonah Gilbert Date: Wed, 7 Sep 2022 00:52:11 -0500 Subject: [PATCH 6/8] Update command_line_scc.py with hybrid domestic --- damage_fun_runs/command_line_scc.py | 127 +++++++++++++++++----------- 1 file changed, 76 insertions(+), 51 deletions(-) diff --git a/damage_fun_runs/command_line_scc.py b/damage_fun_runs/command_line_scc.py index c869a88..8c8d4be 100644 --- a/damage_fun_runs/command_line_scc.py +++ b/damage_fun_runs/command_line_scc.py @@ -1,33 +1,17 @@ import xarray as xr import dscim import yaml -from dscim.menu.simple_storage import Climate, EconVars, StackedDamages +from dscim.menu.simple_storage import Climate, EconVars import pandas as pd import numpy as np -from xarray.testing import assert_allclose -from xarray.testing import assert_equal from itertools import product from pathlib import Path -import dask -import logging -import subprocess -from subprocess import CalledProcessError -from abc import ABC, abstractmethod -from dscim.descriptors import cachedproperty -from dscim.menu.decorators import save -from dscim.utils.utils import ( - model_outputs, - compute_damages, - c_equivalence, - power, - quantile_weight_quantilereg, - extrapolate, -) import inquirer from pyfiglet import Figlet from pathlib import Path import os +import re master = Path(os.getcwd()) / "generated_conf.yml" try: @@ -53,19 +37,18 @@ def epa_scc(sector = "CAMEL_m1_c0.20", fair_aggregation = ["mean"]): master = Path(os.getcwd()) / "generated_conf.yml" - + with open(master, "r") as stream: conf = yaml.safe_load(stream) - - if domestic: - econ = EconVars( - path_econ=f"{conf['rffdata']['socioec_output']}/rff_USA_socioeconomics.nc4" - ) - else: - econ = EconVars( - path_econ=f"{conf['rffdata']['socioec_output']}/rff_global_socioeconomics.nc4" - ) - + + econ_dom = EconVars( + path_econ=f"{conf['rffdata']['socioec_output']}/rff_USA_socioeconomics.nc4" + ) + econ_glob = EconVars( + path_econ=f"{conf['rffdata']['socioec_output']}/rff_global_socioeconomics.nc4" + ) + + conf["global_parameters"] = {'fair_aggregation': fair_aggregation, 'subset_dict': {'ssp': []}, 'weitzman_parameter': weitzman_parameters, @@ -77,7 +60,7 @@ def epa_scc(sector = "CAMEL_m1_c0.20", "equity": dscim.menu.equity.EquityRecipe, } add_kwargs = { - "econ_vars": econ, + "econ_vars": econ_dom, "climate_vars": Climate(**conf["rff_climate"], pulse_year=pulse_year), "formula": conf["sectors"][sector if not domestic else sector[:-4]]["formula"], "discounting_type": discount_type, @@ -86,25 +69,66 @@ def epa_scc(sector = "CAMEL_m1_c0.20", "save_path": None, "eta": eta, "rho": rho, - "damage_function_path": Path(conf['paths']['rff_damage_function_library']) / sector, + "damage_function_path": Path(conf['paths']['rff_damage_function_library']) / sector, "ecs_mask_path": None, "ecs_mask_name": None, "fair_dims":[], } - kwargs = conf["global_parameters"].copy() + kwargs_domestic = conf["global_parameters"].copy() for k, v in add_kwargs.items(): assert ( - k not in kwargs.keys() + k not in kwargs_domestic.keys() ), f"{k} already set in config. Please check `global_parameters`." - kwargs.update({k: v}) + kwargs_domestic.update({k: v}) + + conf["global_parameters"] = {'fair_aggregation': fair_aggregation, + 'subset_dict': {'ssp': []}, + 'weitzman_parameter': weitzman_parameters, + 'save_files': []} + + add_kwargs = { + "econ_vars": econ_glob, + "climate_vars": Climate(**conf["rff_climate"], pulse_year=pulse_year), + "formula": conf["sectors"][sector if not domestic else sector[:-4]]["formula"], + "discounting_type": discount_type, + "sector": sector, + "ce_path": None, + "save_path": None, + "eta": eta, + "rho": rho, + "damage_function_path": Path(conf['paths']['rff_damage_function_library']) / [sector if not domestic else sector[:-4]][0], + "ecs_mask_path": None, + "ecs_mask_name": None, + "fair_dims":[], + } + + kwargs_global = conf["global_parameters"].copy() + for k, v in add_kwargs.items(): + assert ( + k not in kwargs_global.keys() + ), f"{k} already set in config. Please check `global_parameters`." + kwargs_global.update({k: v}) + + menu_item_global = MENU_OPTIONS[menu_option](**kwargs_global) + df = menu_item_global.uncollapsed_discount_factors + + if domestic: + menu_item_domestic = MENU_OPTIONS[menu_option](**kwargs_domestic) + md = menu_item_domestic.uncollapsed_marginal_damages + else: + md = menu_item_global.uncollapsed_marginal_damages + + if menu_option == "risk_aversion": + sccs = ( + (md.rename(marginal_damages = 'scc') * df.rename(discount_factor = 'scc')) + .sum("year") + ) + else: + sccs = menu_item_global.discounted_damages(md,"constant").sum(dim="year").rename(marginal_damages = "scc") + gcnp = menu_item_global.global_consumption_no_pulse + return([sccs,gcnp]) - menu_item = MENU_OPTIONS[menu_option](**kwargs) - menu_item.order_plate("scc") - uncollapsed_scc = menu_item.uncollapsed_sccs - - return(menu_item) - # This represents the full gamut of scc runs when run default def epa_sccs(sectors =["CAMEL_m1_c0.20"], domestic = False, @@ -120,12 +144,13 @@ def epa_sccs(sectors =["CAMEL_m1_c0.20"], with open(master, "r") as stream: conf = yaml.safe_load(stream) - for j, sector in product(risk_combos, sectors): + for j in risk_combos: all_arrays_uscc = [] all_arrays_gcnp = [] discount_type= j[1] menu_option = j[0] - for i, pulse_year in product(etas_rhos, pulse_years): + for i, pulse_year, sector in product(etas_rhos, pulse_years, sectors): + print(i, pulse_year, sector) eta = i[0] rho = i[1] df_single = epa_scc(sector = sector, @@ -139,16 +164,16 @@ def epa_sccs(sectors =["CAMEL_m1_c0.20"], weitzman_parameters = weitzman_parameters, fair_aggregation = fair_aggregation) - df_scc = df_single.uncollapsed_sccs.assign_coords(eta_rhos = str(eta) + "_" + str(rho), menu_option = menu_option, pulse_year = pulse_year) - df_scc_expanded = df_scc.expand_dims(['eta_rhos','menu_option','pulse_year']) - df_scc_expanded.name = "uncollapsed_sccs" - df_scc_expanded = df_scc_expanded.to_dataset() + df_scc = df_single[0].assign_coords(eta_rho = str(eta) + "_" + str(rho), menu_option = menu_option, pulse_year = pulse_year, sector = re.split("_",sector)[0]) + df_scc_expanded = df_scc.expand_dims(['eta_rho','menu_option','pulse_year', 'sector']) + if 'simulation' in df_scc_expanded.dims: + df_scc_expanded = df_scc_expanded.drop_vars('simulation') all_arrays_uscc = all_arrays_uscc + [df_scc_expanded] - - df_gcnp = df_single.global_consumption_no_pulse.assign_coords(eta_rhos = str(eta) + "_" + str(rho), menu_option = menu_option, pulse_year = pulse_year) - df_gcnp_expanded = df_gcnp.expand_dims(['eta_rhos','menu_option','pulse_year']) - df_gcnp_expanded.name = "global_consumption_no_pulse" - df_gcnp_expanded = df_gcnp_expanded.to_dataset() + + df_gcnp = df_single[1].assign_coords(eta_rho = str(eta) + "_" + str(rho), menu_option = menu_option, pulse_year = pulse_year, sector = re.split("_",sector)[0]) + df_gcnp_expanded = df_gcnp.expand_dims(['eta_rho','menu_option','pulse_year', 'sector']) + if 'simulation' in df_gcnp_expanded.dims: + df_gcnp_expanded = df_gcnp_expanded.drop_vars('simulation') all_arrays_gcnp = all_arrays_gcnp + [df_gcnp_expanded] df_full_scc = xr.combine_by_coords(all_arrays_uscc) From a3da94f82bd26a3e6d31f83ca62faed09a9f2781 Mon Sep 17 00:00:00 2001 From: davidrzhdu <111376495+davidrzhdu@users.noreply.github.com> Date: Wed, 7 Sep 2022 11:09:56 -0500 Subject: [PATCH 7/8] Add downloading from internet --- damage_fun_runs/Directory_setup.py | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/damage_fun_runs/Directory_setup.py b/damage_fun_runs/Directory_setup.py index 8d02796..f46b236 100644 --- a/damage_fun_runs/Directory_setup.py +++ b/damage_fun_runs/Directory_setup.py @@ -1,7 +1,11 @@ import yaml import os +import shutil from pathlib import Path -base = os.getcwd() +from io import BytesIO +from urllib.request import urlopen +from zipfile import ZipFile +、 inputs = Path(base) / "inputs" outputs = Path(base) / "outputs" @@ -47,7 +51,23 @@ def makedir(path): makedir(outputs / i) makedir(outputs / (i + "_USA")) -# Download inputs from internet +# Download inputs from internet +zipurl = 'https://storage.googleapis.com/climateimpactlab-scc-tool/dscim_input_data/dscim_input_data_v1.0.0.zip' +with urlopen(zipurl) as zipresp: + with ZipFile(BytesIO(zipresp.read())) as zfile: + zfile.extractall(base) +downloaded_file_name = next(os.walk(base))[1][0] +file_names = os.listdir(os.path.join(base, downloaded_file_name)) +for file_name in file_names: + if os.path.exists(os.path.join(base, 'inputs',file_name)): + shutil.rmtree(os.path.join(base, 'inputs',file_name)) + shutil.move(os.path.join(base,downloaded_file_name,file_name),os.path.join(base, 'inputs')) + else: + shutil.move(os.path.join(base,downloaded_file_name,file_name),os.path.join(base, 'inputs')) +shutil.rmtree(os.path.join(base,downloaded_file_name)) + + with open('generated_conf.yml', 'w') as outfile: yaml.dump(conf_base, outfile, default_flow_style=False) + From 5dc158f6345444e944b092023317a18fd6064b25 Mon Sep 17 00:00:00 2001 From: Jonah Gilbert Date: Thu, 8 Sep 2022 17:46:17 -0500 Subject: [PATCH 8/8] Restore deleted line --- damage_fun_runs/Directory_setup.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/damage_fun_runs/Directory_setup.py b/damage_fun_runs/Directory_setup.py index f46b236..d1abade 100644 --- a/damage_fun_runs/Directory_setup.py +++ b/damage_fun_runs/Directory_setup.py @@ -5,7 +5,8 @@ from io import BytesIO from urllib.request import urlopen from zipfile import ZipFile -、 + +base = os.getcwd() inputs = Path(base) / "inputs" outputs = Path(base) / "outputs"