11import os
2+ import warnings
3+
24import pandas
35from switch_model .utilities import query_yes_no
46from argparse import ArgumentParser , RawTextHelpFormatter
6466 ),
6567 "timepoints" : (
6668 ("timepoints.csv" , "timepoint_id" ),
67- [("loads.csv" , "TIMEPOINT" ), ("variable_capacity_factors.csv" , "timepoint" )],
69+ [
70+ ("loads.csv" , "TIMEPOINT" ),
71+ ("variable_capacity_factors.csv" , "timepoint" ),
72+ ("hydro_timepoints.csv" , "timepoint_id" ),
73+ ],
6874 ),
6975 "projects" : (
7076 ("generation_projects_info.csv" , "GENERATION_PROJECT" ),
@@ -99,6 +105,15 @@ def main(args=None):
99105 default = "inputs" ,
100106 help = 'Directory of the input files. Defaults to "inputs".' ,
101107 )
108+ parser .add_argument (
109+ "--silent" , default = False , action = "store_true" , help = "Suppress output"
110+ )
111+ parser .add_argument (
112+ "--no-confirm" ,
113+ default = False ,
114+ action = "store_true" ,
115+ help = "Skip confirmation prompts" ,
116+ )
102117 args = parser .parse_args (args )
103118
104119 if not args .run :
@@ -108,7 +123,7 @@ def main(args=None):
108123 if not os .path .isdir (args .inputs_dir ):
109124 raise NotADirectoryError ("{} is not a directory" .format (args .inputs_dir ))
110125
111- should_continue = query_yes_no (
126+ should_continue = args . no_confirm or query_yes_no (
112127 "WARNING: This will permanently delete data from directory '{}' "
113128 "WITHOUT backing it up. Are you sure you want to continue?" .format (
114129 args .inputs_dir
@@ -126,34 +141,40 @@ def main(args=None):
126141 warn_about_periods = False
127142 pass_count = 0
128143 while pass_count == 0 or rows_removed_in_pass != 0 :
129- print ("Pass {}..." .format (pass_count ), flush = True )
144+ if not args .silent :
145+ print ("Pass {}..." .format (pass_count ), flush = True )
130146 rows_removed_in_pass = 0
131147 for name , data_type in data_types .items ():
132- print ("Checking '{}'..." .format (name ), flush = True )
148+ if not args .silent :
149+ print ("Checking '{}'..." .format (name ), flush = True )
133150 rows_removed = drop_data (data_type , args )
134151 rows_removed_in_pass += rows_removed
135152
136153 if name == "periods" and rows_removed != 0 :
137154 warn_about_periods = True
138- print ("Removed {} rows during pass." .format (rows_removed_in_pass ))
155+ if not args .silent :
156+ print ("Removed {} rows during pass." .format (rows_removed_in_pass ))
139157
140158 total_rows_removed += rows_removed_in_pass
141159 pass_count += 1
142160
143- print (
144- "\n \n Remove {} rows in total from the input files." .format (total_rows_removed )
145- )
146- print (
147- "\n \n Note: If SWITCH fails to load the model when solving it is possible that some input files were missed."
148- " If this is the case, please add the missing input files to 'data_types' in 'switch_model/tools/drop.py'."
149- )
161+ if not args .silent :
162+ print (
163+ "\n \n Remove {} rows in total from the input files." .format (
164+ total_rows_removed
165+ )
166+ )
167+ print (
168+ "\n \n Note: If SWITCH fails to load the model when solving it is possible that some input files were missed."
169+ " If this is the case, please add the missing input files to 'data_types' in 'switch_model/tools/drop.py'."
170+ )
150171
151172 # It is impossible to know if a row in gen_build_costs.csv is for predetermined generation or for
152173 # a period that was removed. So instead we don't touch it and let the user manually edit
153174 # the input file.
154175 if warn_about_periods :
155- print (
156- "\n \n WARNING: Could not update gen_build_costs.csv. Please manually edit gen_build_costs.csv to remove "
176+ warnings . warn (
177+ "\n \n Could not update gen_build_costs.csv. Please manually edit gen_build_costs.csv to remove "
157178 "references to the removed periods."
158179 )
159180
@@ -179,7 +200,7 @@ def get_valid_ids(primary_file, args):
179200 print ("\n Warning: {} was not found." .format (filename ))
180201 return None
181202
182- valid_ids = pandas .read_csv (path )[primary_key ]
203+ valid_ids = pandas .read_csv (path , dtype = str )[primary_key ]
183204 return valid_ids
184205
185206
@@ -189,17 +210,21 @@ def drop_from_file(filename, foreign_key, valid_ids, args):
189210 if not os .path .exists (path ):
190211 return 0
191212
192- df = pandas .read_csv (path )
213+ df = pandas .read_csv (path , dtype = str )
193214 count = len (df )
215+ if foreign_key not in df .columns :
216+ raise Exception (f"Column { foreign_key } not in file { filename } " )
194217 df = df [df [foreign_key ].isin (valid_ids )]
195218 rows_removed = count - len (df )
196219
197220 if rows_removed != 0 :
198221 df .to_csv (path , index = False )
199222
200- print ("Removed {} rows {}." .format (rows_removed , filename ))
223+ if not args .silent :
224+ print ("Removed {} rows {}." .format (rows_removed , filename ))
201225 if rows_removed == count :
202- print ("WARNING: {} is now empty." .format (filename ))
226+ if not args .silent :
227+ print ("WARNING: {} is now empty." .format (filename ))
203228
204229 return rows_removed
205230
0 commit comments