11import os
2+ import warnings
3+
24import pandas
35from switch_model .utilities import query_yes_no
46from argparse import ArgumentParser , RawTextHelpFormatter
7173 ('timepoints.csv' , 'timepoint_id' ),
7274 [
7375 ('loads.csv' , 'TIMEPOINT' ),
74- ('variable_capacity_factors.csv' , 'timepoint' )
76+ ('variable_capacity_factors.csv' , 'timepoint' ),
77+ ('hydro_timepoints.csv' , 'timepoint_id' )
7578 ]
7679 ),
7780 "projects" : (
@@ -100,6 +103,8 @@ def main(args=None):
100103
101104 parser .add_argument ('--run' , default = False , action = 'store_true' , help = 'Drop the data.' )
102105 parser .add_argument ('--inputs-dir' , default = 'inputs' , help = 'Directory of the input files. Defaults to "inputs".' )
106+ parser .add_argument ('--silent' , default = False , action = 'store_true' , help = 'Suppress output' )
107+ parser .add_argument ('--no-confirm' , default = False , action = 'store_true' , help = "Skip confirmation prompts" )
103108 args = parser .parse_args (args )
104109
105110 if not args .run :
@@ -109,7 +114,7 @@ def main(args=None):
109114 if not os .path .isdir (args .inputs_dir ):
110115 raise NotADirectoryError ("{} is not a directory" .format (args .inputs_dir ))
111116
112- should_continue = query_yes_no ("WARNING: This will permanently delete data from directory '{}' "
117+ should_continue = args . no_confirm or query_yes_no ("WARNING: This will permanently delete data from directory '{}' "
113118 "WITHOUT backing it up. Are you sure you want to continue?" .format (args .inputs_dir ))
114119
115120 if not should_continue :
@@ -123,30 +128,34 @@ def main(args=None):
123128 warn_about_periods = False
124129 pass_count = 0
125130 while pass_count == 0 or rows_removed_in_pass != 0 :
126- print ("Pass {}..." .format (pass_count ), flush = True )
131+ if not args .silent :
132+ print ("Pass {}..." .format (pass_count ), flush = True )
127133 rows_removed_in_pass = 0
128134 for name , data_type in data_types .items ():
129- print ("Checking '{}'..." .format (name ), flush = True )
135+ if not args .silent :
136+ print ("Checking '{}'..." .format (name ), flush = True )
130137 rows_removed = drop_data (data_type , args )
131138 rows_removed_in_pass += rows_removed
132139
133140 if name == "periods" and rows_removed != 0 :
134141 warn_about_periods = True
135- print ("Removed {} rows during pass." .format (rows_removed_in_pass ))
142+ if not args .silent :
143+ print ("Removed {} rows during pass." .format (rows_removed_in_pass ))
136144
137145 total_rows_removed += rows_removed_in_pass
138146 pass_count += 1
139147
140- print ("\n \n Remove {} rows in total from the input files." .format (total_rows_removed ))
141- print ("\n \n Note: If SWITCH fails to load the model when solving it is possible that some input files were missed."
142- " If this is the case, please add the missing input files to 'data_types' in 'switch_model/tools/drop.py'." )
148+ if not args .silent :
149+ print ("\n \n Remove {} rows in total from the input files." .format (total_rows_removed ))
150+ print ("\n \n Note: If SWITCH fails to load the model when solving it is possible that some input files were missed."
151+ " If this is the case, please add the missing input files to 'data_types' in 'switch_model/tools/drop.py'." )
143152
144153 # It is impossible to know if a row in gen_build_costs.csv is for predetermined generation or for
145154 # a period that was removed. So instead we don't touch it and let the user manually edit
146155 # the input file.
147156 if warn_about_periods :
148- print ("\n \n WARNING: Could not update gen_build_costs.csv. Please manually edit gen_build_costs.csv to remove "
149- "references to the removed periods." )
157+ warnings . warn ("\n \n Could not update gen_build_costs.csv. Please manually edit gen_build_costs.csv to remove "
158+ "references to the removed periods." )
150159
151160
152161def drop_data (id_type , args ):
@@ -170,7 +179,7 @@ def get_valid_ids(primary_file, args):
170179 print ("\n Warning: {} was not found." .format (filename ))
171180 return None
172181
173- valid_ids = pandas .read_csv (path )[primary_key ]
182+ valid_ids = pandas .read_csv (path , dtype = str )[primary_key ]
174183 return valid_ids
175184
176185
@@ -180,17 +189,21 @@ def drop_from_file(filename, foreign_key, valid_ids, args):
180189 if not os .path .exists (path ):
181190 return 0
182191
183- df = pandas .read_csv (path )
192+ df = pandas .read_csv (path , dtype = str )
184193 count = len (df )
194+ if foreign_key not in df .columns :
195+ raise Exception (f"Column { foreign_key } not in file { filename } " )
185196 df = df [df [foreign_key ].isin (valid_ids )]
186197 rows_removed = count - len (df )
187198
188199 if rows_removed != 0 :
189200 df .to_csv (path , index = False )
190201
191- print ("Removed {} rows {}." .format (rows_removed , filename ))
202+ if not args .silent :
203+ print ("Removed {} rows {}." .format (rows_removed , filename ))
192204 if rows_removed == count :
193- print ("WARNING: {} is now empty." .format (filename ))
205+ if not args .silent :
206+ print ("WARNING: {} is now empty." .format (filename ))
194207
195208 return rows_removed
196209
0 commit comments