From cb5dd15aa2907424ac4bcf57a7ad3b7abc9de395 Mon Sep 17 00:00:00 2001 From: trevorb1 Date: Thu, 28 Mar 2024 08:39:33 -0700 Subject: [PATCH] remove old scripts --- .../osemosys_global/feo_legacy_power.py | 179 ------------------ .../osemosys_global/plot_input_data.py | 71 ------- 2 files changed, 250 deletions(-) delete mode 100644 workflow/scripts/osemosys_global/feo_legacy_power.py delete mode 100644 workflow/scripts/osemosys_global/plot_input_data.py diff --git a/workflow/scripts/osemosys_global/feo_legacy_power.py b/workflow/scripts/osemosys_global/feo_legacy_power.py deleted file mode 100644 index 834064c8..00000000 --- a/workflow/scripts/osemosys_global/feo_legacy_power.py +++ /dev/null @@ -1,179 +0,0 @@ -import requests, json -import warnings -warnings.simplefilter(action='ignore', - category=FutureWarning) -import pandas as pd -pd.options.mode.chained_assignment = None # default='warn' -from unidecode import unidecode -import numpy as np -import os -import yaml -from OPG_configuration import ConfigFile, ConfigPaths -import logging -logging.basicConfig(format='%(levelname)s:%(message)s', level=logging.INFO) -from dotenv import load_dotenv -load_dotenv(os.path.join(os.getcwd(), - '.env')) - -def main(): - - U = os.environ.get('username') - P = os.environ.get('password') - - AUTH_URL = "https://auth.feo.transitionzero.org" - URL = "https://power-legacy.feo.transitionzero.org" - - r = requests.post(AUTH_URL + '/auth/token', - data={'username':U, - 'password':P}) - - print(r.status_code) - - # Load config file - config_paths = ConfigPaths() - config = ConfigFile('config') - input_data_dir = config_paths.input_data_dir - custom_nodes_dir = config_paths.custom_nodes_dir - - # Convert country and admin_1 codes to OSeMOSYS Global format (i.e. XXXYY) - # where XXX is the country code and YY is the admin_1 code - geographic_scope = config.get('geographic_scope') - - df_geo_scope = pd.read_csv(os.path.join(input_data_dir, - "iso_codes.csv")) - iso3_to_iso2 = dict(zip(list(df_geo_scope['alpha-3']), - list(df_geo_scope['alpha-2']))) - iso2_to_iso3 = dict(zip(list(df_geo_scope['alpha-2']), - list(df_geo_scope['alpha-3']))) - - geographic_scope_iso2 = [iso3_to_iso2.get(c, c) - for c in geographic_scope] - # Load files from power data API - if r.status_code==200: - token = json.loads(r.text)['access_token'] - headers = {"Authorization": f"Bearer {token}"} - - df = pd.DataFrame() - next_page = 0 - while next_page != None: - params = {'admin_0':geographic_scope_iso2, - 'page':next_page, - 'limit':100} - - r = requests.get(URL+'/units/', - params=params, - headers=headers) - units = json.loads(r.text)['units'] - next_page = json.loads(r.text)['next_page'] - - df_temp = pd.DataFrame(units) - df = pd.concat([df, df_temp]) - - # Keep only 'operating' plants - df = df[df['operating_status'] == 'operating'] - - # Use 'state' or 'State/Province' when 'admin_1 is None - df['admin_1'].fillna(df['properties'].str.get('state'), - inplace=True) - df['admin_1'].fillna(df['properties'].str.get('State/Province'), - inplace=True) - df['admin_1'] = df['admin_1'].str.normalize('NFKD').str.encode('ascii', - errors='ignore').str.decode('utf-8') - - df = df.dropna(subset=['admin_1']) - df = df.dropna(subset=['id']) - - df['admin_1'] = (df['admin_1'].str.normalize('NFKD') - .str.encode('ascii', - errors='ignore') - .str.decode('utf-8')) - - # Set start_year for each power plant. If not available, set to '0' - df['START_YEAR'] = df['operation_start'].str.split('-', - expand=True)[0] - df['START_YEAR'].fillna(0, inplace=True) - df['START_YEAR'] = df['START_YEAR'].astype(int) - df.fillna(value=np.nan, inplace=True) - df_technologies = df['technologies'].apply(pd.Series) - df = pd.concat([df, df_technologies['prime_mover']], axis=1) - df.loc[df['prime_mover'].isin(['cc_gt']), - 'primary_fuel'] = 'Gas-CCGT' - df = df[~(df['primary_fuel'].isin(['waste_heat']))] - # Re-order columns - df = df[['admin_1', - 'primary_fuel', - 'START_YEAR', - 'capacity_mw']] - - # Replace Climate Trace technology codes (ISO2) with FEO tech codes (ISO3) - df_tech_code = pd.read_csv(os.path.join(input_data_dir, - "naming_convention_tech.csv")) - tech_code_dict = dict(zip(list(df_tech_code['tech']), - list(df_tech_code['code']))) - - df['FUEL_TYPE'] = df['primary_fuel'].map(tech_code_dict) - - # Convert admin_1 (e.g. ID-SA) to CUSTOM_NODE format (e.g. IDNSA) - df['iso2'] = df['admin_1'].str.split('-', - expand=True)[0] - df['node'] = df['admin_1'].str.split('-', - expand=True)[1] - - df['CUSTOM_NODE'] = df['iso2'].map(iso2_to_iso3) + df['node'] - - - df_op_life = pd.read_csv(os.path.join(input_data_dir, - "operational_life.csv")) - op_life_dict = dict(zip(list(df_op_life['tech']), - list(df_op_life['years']))) - df['op_life'] = df['FUEL_TYPE'].map(op_life_dict) - - st_yr_fuel_avg = {} - for each_fuel in df['FUEL_TYPE'].unique(): - st_yr_fuel_avg[each_fuel] = df.loc[(df['FUEL_TYPE'].isin([each_fuel])) & - (df['START_YEAR'] != 0), - 'START_YEAR'].mean().astype(int) - - df.loc[(df['FUEL_TYPE'].isin([each_fuel])) & - (~df['FUEL_TYPE'].isin(['SPV', 'WON'])) & - (df['START_YEAR'] == 0), - 'START_YEAR'] = st_yr_fuel_avg[each_fuel] - - df.loc[(df['FUEL_TYPE'].isin([each_fuel])) & - (df['FUEL_TYPE'].isin(['SPV', 'WON'])) & - (df['START_YEAR'] == 0), - 'START_YEAR'] = 2020 - - df['END_YEAR'] = df['START_YEAR'].astype(int) + df['op_life'].astype(int) - df.loc[df['START_YEAR'].astype(int) == 0, - 'END_YEAR'] = 2030 - - try: - os.makedirs(custom_nodes_dir) - except FileExistsError: - pass - - df.rename(columns={'capacity_mw':'CAPACITY'}, - inplace=True) - ''' - df = df[['FUEL_TYPE', - 'CUSTOM_NODE', - 'START_YEAR', - 'END_YEAR', - 'CAPACITY']] - ''' - df = df[['admin_1', - 'FUEL_TYPE', - 'CUSTOM_NODE', - 'START_YEAR', - 'END_YEAR', - 'CAPACITY']] - - df.to_csv(os.path.join(input_data_dir, - "custom_nodes", - "residual_capacity.csv"), - index=None) - -if __name__ == "__main__": - main() - logging.info('Residual capacity data table for custom nodes created') \ No newline at end of file diff --git a/workflow/scripts/osemosys_global/plot_input_data.py b/workflow/scripts/osemosys_global/plot_input_data.py deleted file mode 100644 index 8162040e..00000000 --- a/workflow/scripts/osemosys_global/plot_input_data.py +++ /dev/null @@ -1,71 +0,0 @@ -import pandas as pd -import plotly.graph_objects as go -import os -# from osemosys_global.configuration import ConfigFile, ConfigPaths -from configuration import ConfigFile, ConfigPaths - -pd.set_option('mode.chained_assignment', None) - - -def main(): - '''Creates figures of selected input data''' - - config_paths = ConfigPaths() - config = ConfigFile('config') - data_dir = config_paths.output_data_dir - figure_dir = config_paths.output_dir - - # Check for output directory - try: - os.makedirs(os.path.join(figure_dir, 'figs')) - except FileExistsError: - pass - - for each_tech in ['SPV', 'WON', 'HYD']: - plot_capfac(data_dir, each_tech, figure_dir) - - -def plot_capfac(data_dir, tech, figure_dir): - df = pd.read_csv(os.path.join(data_dir, - 'CapacityFactor.csv')) - - #df = df.loc[df.TECHNOLOGY.str[6:9].isin(['IND', - # 'BTN', - # 'BGD', - # 'NPL'])] - df = df.loc[(df.TECHNOLOGY.str.startswith('PWR')) & - (df.TECHNOLOGY.str[3:6].isin([tech]))] - df.VALUE = df.VALUE.astype('float64') - - df.drop(['REGION'], - axis=1, - inplace=True) - - df = df.groupby(['TIMESLICE', - 'TECHNOLOGY'], - as_index=False)['VALUE'].mean() - - fig = go.Figure(data=go.Heatmap( - z=df['VALUE'], - x=df['TIMESLICE'], - y=df['TECHNOLOGY'], - colorscale='inferno' - ), - ) - - fig.update_layout( - xaxis_nticks=len(df['TECHNOLOGY']), - height=1400) - # fig.update_xaxes(title_text='Hours') - - fig.write_image(os.path.join(figure_dir, - 'figs', - 'capfac_' + - #each_node + - ' ' + - tech + - '.jpg')) - - -if __name__ == '__main__': - main()