1+ import re
12from abc import ABC , abstractmethod
23from datetime import datetime
34from pathlib import Path
1213from geoh5py .objects import ObjectBase
1314from geoh5py .ui_json .utils import fetch_active_workspace
1415
16+
1517def compute_JtJdiags (data_misfit , m ):
1618 if hasattr (data_misfit , "getJtJdiag" ):
1719 return data_misfit .getJtJdiag (m )
@@ -26,6 +28,7 @@ def compute_JtJdiags(data_misfit, m):
2628
2729 return np .asarray (jtj_diag )
2830
31+
2932class BaseSaveGeoH5 (InversionDirective , ABC ):
3033 """
3134 Base class for saving inversion results to a geoh5 file
@@ -364,13 +367,21 @@ def write(self, iteration: int, **_):
364367 if iteration == 0 :
365368 with open (filepath , "w" , encoding = "utf-8" ) as f :
366369 f .write ("iteration beta phi_d phi_m time\n " )
367-
368- with open (filepath , "a" , encoding = "utf-8" ) as f :
369- date_time = datetime .now ().strftime ("%b-%d-%Y:%H:%M:%S" )
370- f .write (
371- f"{ iteration } { self .invProb .beta :.3e} { self .invProb .phi_d :.3e} "
372- f"{ self .invProb .phi_m :.3e} { date_time } \n "
373- )
370+ log = []
371+ with open (dirpath / "SimPEG.log" , "r" , encoding = "utf-8" ) as file :
372+ iter = 0
373+ for line in file :
374+ val = re .findall (
375+ "[+\-]?(?:0|[1-9]\d*)(?:\.\d*)?(?:[eE][+\-]?\d+)" , line
376+ )
377+ if len (val ) == 5 :
378+ log .append (val [:- 2 ])
379+ iter += 1
380+
381+ if len (log ) > 0 :
382+ with open (filepath , "a" , encoding = "utf-8" ) as file :
383+ date_time = datetime .now ().strftime ("%b-%d-%Y:%H:%M:%S" )
384+ file .write (f"{ iter - 1 } " + " " .join (log [- 1 ]) + f" { date_time } \n " )
374385
375386 self .save_log ()
376387
0 commit comments