Skip to content

Commit

Permalink
Update execute_forward.py
Browse files Browse the repository at this point in the history
Adjusted input function to account for new GCAM-USA data format.
  • Loading branch information
cdburley committed May 24, 2024
1 parent 862d408 commit 7cb72eb
Showing 1 changed file with 76 additions and 16 deletions.
92 changes: 76 additions & 16 deletions tell/execute_forward.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,39 +9,99 @@
from scipy import interpolate
from .states_fips_function import state_metadata_from_state_abbreviation

def extract_gcam_usa_loads(scenario_to_process: str, filename: str) -> DataFrame:
# def extract_gcam_usa_loads(scenario_to_process: str, filename: str) -> DataFrame:
# """Extracts the state-level annual loads from a GCAM-USA output file.
#
# :param scenario_to_process: Scenario to process
# :type scenario_to_process: str
#
# :param filename: Name of the GCAM-USA output file
# :type filename: str
#
# :return: DataFrame of state-level annual total electricity loads
#
# """
#
# # Load in the raw GCAM-USA output file:
# gcam_usa_df = pd.read_csv(filename, index_col=None, header=0)
#
# # Cluge the scenario for historical runs:
# if scenario_to_process == 'historic':
# scenario_to_process_gcam = 'rcp45cooler_ssp3'
# else:
# scenario_to_process_gcam = scenario_to_process
#
# # Subset the data to only the scenario you want to process:
# gcam_usa_df = gcam_usa_df[gcam_usa_df['scenario'].isin([scenario_to_process_gcam])]
#
# # Subset the data to only the total annual consumption of electricity by state:
# gcam_usa_df = gcam_usa_df[gcam_usa_df['param'].isin(['elecFinalBySecTWh'])]
#
# # Make a list of all of the states in the "gcam_usa_df":
# states = gcam_usa_df['subRegion'].unique()
#
# # Loop over the states and interpolate their loads to an annual time step:
# for i in range(len(states)):
#
# # Subset to just the data for the state being processed:
# subset_df = gcam_usa_df[gcam_usa_df['subRegion'].isin([states[i]])].copy()
#
# # Retrieve the state metadata:
# (state_fips, state_name) = state_metadata_from_state_abbreviation(states[i])
#
# # Linearly interpolate the 5-year loads from GCAM-USA to an annual time step:
# annual_time_vector = pd.Series(range(subset_df['x'].min(), subset_df['x'].max()))
# interpolation_function = interpolate.interp1d(subset_df['x'], subset_df['value'], kind='linear')
# annual_loads = interpolation_function(annual_time_vector)
#
# # Create an empty dataframe and store the results:
# state_df = pd.DataFrame()
# state_df['Year'] = annual_time_vector.tolist()
# state_df['GCAM_USA_State_Annual_Load_TWh'] = annual_loads
# state_df['State_FIPS'] = state_fips
# state_df['State_Name'] = state_name
# state_df['State_Abbreviation'] = states[i]
#
# # Aggregate the output into a new dataframe:
# if i == 0:
# gcam_usa_output_df = state_df
# else:
# gcam_usa_output_df = pd.concat([gcam_usa_output_df, state_df])
#
# return gcam_usa_output_df


def extract_gcam_usa_loads(scenario_to_process: str, gcam_usa_input_dir:str) -> DataFrame:
"""Extracts the state-level annual loads from a GCAM-USA output file.
:param scenario_to_process: Scenario to process
:type scenario_to_process: str
:param filename: Name of the GCAM-USA output file
:type filename: str
:param gcam_usa_input_dir: Path to where the GCAM-USA data are stored
:type gcam_usa_input_dir: str
:return: DataFrame of state-level annual total electricity loads
"""

# Load in the raw GCAM-USA output file:
gcam_usa_df = pd.read_csv(filename, index_col=None, header=0)

# Cluge the scenario for historical runs:
if scenario_to_process == 'historic':
scenario_to_process_gcam = 'rcp45cooler_ssp3'
scenario_to_process_gcam = 'rcp45cooler_ssp3'
else:
scenario_to_process_gcam = scenario_to_process
scenario_to_process_gcam = scenario_to_process

# Subset the data to only the scenario you want to process:
gcam_usa_df = gcam_usa_df[gcam_usa_df['scenario'].isin([scenario_to_process_gcam])]
# Create the filename for the needed GCAM run:
filename = (os.path.join(gcam_usa_input_dir, ('electricity_demand_' + scenario_to_process_gcam + '.csv')))

# Subset the data to only the total annual consumption of electricity by state:
gcam_usa_df = gcam_usa_df[gcam_usa_df['param'].isin(['elecFinalBySecTWh'])]
# Load in the raw GCAM-USA output file:
gcam_usa_df = pd.read_csv(filename, index_col=None, header=0)

# Make a list of all of the states in the "gcam_usa_df":
states = gcam_usa_df['subRegion'].unique()

# Loop over the states and interpolate their loads to an annual time step:
for i in range(len(states)):
# for i in range(1):

# Subset to just the data for the state being processed:
subset_df = gcam_usa_df[gcam_usa_df['subRegion'].isin([states[i]])].copy()
Expand Down Expand Up @@ -475,7 +535,7 @@ def output_tell_county_data(joint_mlp_df: DataFrame, year_to_process: str, gcam_
state_name = state_name.replace(",", "_")

csv_output_filename = os.path.join(
data_output_dir + '/County_Level_Data/TELL_' + state_name + '_' + county_name + '_Hourly_Load_Data_' +
data_output_dir + '/County_Level_Data/' + year_to_process + '/TELL_' + state_name + '_' + county_name + '_Hourly_Load_Data_' +
year_to_process + '_Scaled_' + gcam_target_year + '.csv')

# Write out the dataframe to a .csv file:
Expand Down Expand Up @@ -537,12 +597,12 @@ def execute_forward(year_to_process: str, gcam_target_year: str, scenario_to_pro
if os.path.exists(data_output_dir_full) is False:
os.makedirs(data_output_dir_full)
if save_county_data:
if os.path.exists(os.path.join(data_output_dir_full, 'County_Level_Data')) is False:
os.mkdir(os.path.join(data_output_dir_full, 'County_Level_Data'))
if os.path.exists(os.path.join(data_output_dir_full, 'County_Level_Data', year_to_process)) is False:
os.mkdir(os.path.join(data_output_dir_full, 'County_Level_Data', year_to_process))

# Load in the sample GCAM-USA output file and subset the data to only the "year_to_process":
gcam_usa_df = extract_gcam_usa_loads(scenario_to_process = scenario_to_process,
filename = (os.path.join(gcam_usa_input_dir, 'gcamDataTable_aggParam.csv')))
gcam_usa_input_dir = gcam_usa_input_dir)
gcam_usa_df = gcam_usa_df[gcam_usa_df['Year'] == int(gcam_target_year)]

# Load in the most recent (i.e., 2019) BA service territory mapping file:
Expand Down

0 comments on commit 7cb72eb

Please sign in to comment.