44
55from pandas import DataFrame , read_csv
66
7- from os import remove
7+ from os import remove , rename
88from re import search , match , findall
99from json import dumps
1010
@@ -27,7 +27,7 @@ def __init__(self, filename: str):
2727 self ._fl_list = self ._read_fls ()
2828 self ._cf_list = self ._read_cfs ()
2929
30- #------------------------ OBJECT MANIPULATION TOOLS -----------------------#
30+ # ------------------------ OBJECT MANIPULATION TOOLS -----------------------#
3131
3232 def _read_object (self , id_regex : str ) -> List [Object ]:
3333 '''
@@ -57,7 +57,7 @@ def _read_fls(self) -> List[FL]:
5757 Looks for FLs in the input file and returns them as a list of FL objects.
5858 '''
5959 return self ._read_object (r'\bFL\d{3}00\b' )
60-
60+
6161 def _read_cfs (self ) -> List [CF ]:
6262 '''
6363 Looks for CFs in the input file and returns them as a list of CF objects.
@@ -198,7 +198,7 @@ def get_cf(self, cf_id: str) -> CF:
198198 Searches for a CF in the input file and returns it as a CF object.
199199 '''
200200 cf_data = {}
201-
201+
202202 arg_c = 0 # arg counter
203203
204204 with open (self ._filename , 'r' ) as file :
@@ -216,20 +216,21 @@ def get_cf(self, cf_id: str) -> CF:
216216 record_data ['CFTYPE' ] = record [2 ]
217217 record_data ['NCFARG' ] = record [3 ]
218218 record_data ['CFSCAL' ] = record [4 ]
219- record_data ['CFADCN' ] = record [5 ] if len (record ) > 5 else 0.0
219+ record_data ['CFADCN' ] = record [5 ] if len (
220+ record ) > 5 else 0.0
220221 elif termination == '01' :
221222 if record [1 ] in ['.TRUE.' , '.FALSE.' ]:
222223 record_data ['LCFVAL' ] = record [1 ]
223224 else :
224- record_data ['CFVALR' ] = record [1 ]
225+ record_data ['CFVALR' ] = record [1 ]
225226 elif termination == '02' :
226227 record_data ['ICFLIM' ] = record [1 ]
227228 if int (record [1 ]) in [1 , 2 , 3 ]:
228229 record_data ['CFLIML' ] = record [2 ]
229230 if int (record [1 ]) in [2 , 3 ]:
230231 record_data ['CFLIMU' ] = record [3 ]
231232 elif match (r'0[3-4]' , termination ):
232- record_data ['FIELDS' ] = record [1 ] # Fixable
233+ record_data ['FIELDS' ] = record [1 ] # Fixable
233234 elif termination == '05' :
234235 record_data ['CLASS' ] = record [1 ]
235236 elif termination == '06' :
@@ -262,46 +263,37 @@ def get_fl_list(self) -> List[FL]:
262263 Return the list of CVs in parsed file.
263264 '''
264265 return self ._fl_list
265-
266+
266267 def get_cf_list (self ) -> List [CF ]:
267268 '''
268269 Return the list of CFs in parsed file.
269270 '''
270271 return self ._cf_list
271272
272- def remove_object (self , obj_id : str , src_file : str = None , new_file : str = None ) -> None :
273+ def remove_object (self , obj_id : str , overwrite : bool = False , new_file : str = None ) -> None :
273274 '''
274275 Deletes an object from the input file.
275276 '''
276277
277- src_file = src_file or self ._filename
278- new_file = new_file or self . _filename + '_NEW'
278+ src_file = self ._filename
279+ new_file = new_file or src_file + '_NEW'
279280
280281 with open (src_file , 'r' ) as f1 , open (new_file , 'w' ) as f2 :
281282 for line in f1 :
282283 if not line .startswith (obj_id ):
283284 f2 .write (line )
284285
285- def remove_objects (self , obj_ids : List [str ], src_file : str = None , new_file : str = None ) -> None :
286- '''
287- Deletes a list of objects from the input file.
288- '''
289-
290- src_file = src_file or self ._filename
291- new_file = new_file or self ._filename + '_NEW'
286+ if overwrite :
287+ remove (src_file )
288+ rename (new_file , src_file )
292289
293- with open (src_file , 'r' ) as f1 , open (new_file , 'w' ) as f2 :
294- for line in f1 :
295- if line [:5 ] not in obj_ids :
296- f2 .write (line )
297-
298- def write_object (self , obj : Object , src_file : str = None , new_file : str = None ) -> None :
290+ def write_object (self , obj : Object , overwrite : bool = False , new_file : str = None ) -> None :
299291 '''
300292 Writes a new object in the input file.
301293 '''
302294
303- src_file = src_file or self ._filename
304- new_file = new_file or self . _filename + '_NEW'
295+ src_file = self ._filename
296+ new_file = new_file or src_file + '_NEW'
305297
306298 with open (src_file , 'r' ) as f1 , open (new_file , 'w' ) as f2 :
307299 written = False
@@ -312,71 +304,37 @@ def write_object(self, obj: Object, src_file: str = None, new_file: str = None)
312304 else :
313305 f2 .write (line )
314306
315- def write_objects (self , obj_list : List [Object ], src_file : str = None , new_file : str = None ) -> None :
316- '''
317- Writes a new object in the input file.
318- '''
319-
320- src_file = src_file or self ._filename
321- new_file = new_file or self ._filename + '_NEW'
322-
323- with open (src_file , 'r' ) as f1 , open (new_file , 'w' ) as f2 :
324- written = False
325- for line in f1 :
326- if line .startswith ('.' ) and not written :
327- for obj in obj_list :
328- f2 .write ('*\n ' + str (obj ) + '*\n ' )
329- f2 .write (line )
330- written = True
331- else :
332- f2 .write (line )
307+ if overwrite :
308+ remove (src_file )
309+ rename (new_file , src_file )
333310
334- def update_object (self , obj : Object , src_file : str = None , new_file : str = None ) -> None :
311+ def update_object (self , obj : Object , overwrite : bool = False , new_file : str = None ) -> None :
335312 '''
336313 Updates object input information.
337314 '''
338315
339- src_file = src_file or self ._filename
340- tmp_file = self ._filename + '_TMP'
341- new_file = new_file or self ._filename + '_NEW'
316+ src_file = self ._filename
317+ new_file = new_file or src_file + '_NEW'
342318
343319 obj_id = obj .get_id ()
344320
345- self .remove_object (obj_id , new_file = tmp_file )
346- self .write_object (obj , src_file = tmp_file , new_file = new_file )
347-
348- remove (self ._filename + '_TMP' )
349-
350- def update_objects (self , obj_list : Object , src_file : str = None , new_file : str = None ) -> None :
351- '''
352- Updates objects input information.
353- '''
354-
355- src_file = src_file or self ._filename
356- tmp_file = self ._filename + '_TMP'
357- new_file = new_file or self ._filename + '_NEW'
358-
359- obj_ids = [obj .get_id () for obj in obj_list ]
360-
361- self .remove_objects (obj_ids , new_file = tmp_file )
362- self .write_objects (obj_list , src_file = tmp_file , new_file = new_file )
363-
364- remove (self ._filename + '_TMP' )
365-
366- def clear_objects (self , src_file : str = None , new_file : str = None ) -> None :
367- '''
368- Removes every CV or FL in the input file.
369- '''
370-
371- src_file = src_file or self ._filename
372- new_file = new_file or self ._filename + '_NEW'
373-
374- with open (src_file , 'r' ) as f1 , open (new_file , 'w' ) as f2 :
375- for line in f1 :
376- if line [:2 ] not in ['CV' , 'FL' , 'CF' , 'TF' ]:
377- f2 .write (line )
378-
379- #-------------------------------- EDF TOOLS -------------------------------#
321+ if overwrite :
322+ self .remove_object (obj_id , overwrite = True )
323+ self .write_object (obj , overwrite = True )
324+ else :
325+ tmp_file = src_file + '_TMP'
326+ self .remove_object (obj_id , new_file = tmp_file )
327+ with open (tmp_file , 'r' ) as f1 , open (new_file , 'w' ) as f2 :
328+ written = False
329+ for line in f1 :
330+ if line .startswith ('.' ) and not written :
331+ f2 .write ('*\n ' + str (obj ) + '*\n ' + line )
332+ written = True
333+ else :
334+ f2 .write (line )
335+ remove (tmp_file )
336+
337+ # -------------------------------- EDF TOOLS -------------------------------#
380338
381339 def get_edf_vars (self ) -> List [str ]:
382340 '''
@@ -424,7 +382,7 @@ def plot_edf(self, datafile: str, y_var: str) -> None:
424382 self .as_dataframe (datafile ).plot (x = 'TIME' , y = y_var )
425383 plt .show ()
426384
427- #----------------------------- CONNECTION TOOLS ---------------------------#
385+ # ----------------------------- CONNECTION TOOLS ---------------------------#
428386
429387 def get_fl_connections (self , cv_id : str ) -> List [FL ]:
430388 '''
@@ -450,7 +408,6 @@ def get_connected_cvs(self, cv_id: str) -> List[CV]:
450408 cv_connected .append (self .id_search (
451409 self ._cv_list , 'CV' + fl .get_field ('KCVFM' )))
452410 return cv_connected
453-
454411
455412 def get_connected_cfs (self , obj_id : str ) -> List [CF ]:
456413 '''
@@ -473,15 +430,14 @@ def get_connected_cfs(self, obj_id: str) -> List[CF]:
473430 elif obj_id .startswith ('CF' ):
474431 cf = self .get_cf (obj_id )
475432 cf_values = findall (r'\bCFVALU\.\d+\b' , dumps (cf .records ))
476- for value in cf_values :
433+ for value in cf_values :
477434 dot_pos = value .find ('.' )
478435 cf_id = 'CF' + value [dot_pos + 1 :]
479436 cf_connected .append (self .get_cf (cf_id ))
480437 cf_connected += self .get_connected_cfs (cf_id )
481438
482439 return cf_connected
483440
484-
485441 def create_submodel (self , cv_id : str , new_file : str = None ) -> Union [List [CV ], List [FL ]]:
486442 '''
487443 Creates a submodel related to a given CV. Those neighbour CVs are made time-independent.
@@ -506,7 +462,7 @@ def create_submodel(self, cv_id: str, new_file: str = None) -> Union[List[CV], L
506462
507463 return sub_cvs , sub_fls
508464
509- #------------------------------- AUX TOOLS --------------------------------#
465+ # ------------------------------- AUX TOOLS --------------------------------#
510466
511467 def get_used_ids (self , obj_list : List [Object ]) -> List [str ]:
512468 '''
@@ -541,7 +497,7 @@ def used_to_csv(self, obj_list: List[Object], title='./used.csv') -> DataFrame:
541497 df .to_csv (title , index = False )
542498
543499 return df
544-
500+
545501 def available_to_csv (self , obj_list : List [Object ], title = './available.csv' ) -> DataFrame :
546502 '''
547503 Returns a single-column available IDs DataFrame from an object list. Also exports it as a CSV file.
0 commit comments