13
13
import yaml
14
14
import pyglider .utils as utils
15
15
import xml .etree .ElementTree as ET
16
+ from datetime import datetime
16
17
17
18
18
19
_log = logging .getLogger (__name__ )
@@ -848,5 +849,66 @@ def parse_gliderState(fname):
848
849
return dat
849
850
850
851
852
+ def parse_logfiles (files ):
853
+ """
854
+ Parse time, lat, lon, and amph_total from glider logfiles.
855
+
856
+ Parameters
857
+ ----------
858
+ files : list of strings or Paths
859
+ List of logfiles to parse. Should be sorted.
860
+
861
+ Returns
862
+ -------
863
+ out : xarray
864
+ xarray data set with fields time, lon, lat, ampH indexed by surfacing.
865
+ More could be added.
866
+ """
867
+
868
+ times = ['' ] * 10000
869
+ gps = ['' ] * 10000
870
+ amph = ['' ] * 10000
871
+ ntimes = 0
872
+ for fn in files :
873
+ found_time = False
874
+
875
+ with open (fn , 'r' ) as fin :
876
+ for l in fin :
877
+ if 'Curr Time:' in l :
878
+ times [ntimes ] = l
879
+ ntimes += 1
880
+ found_time = True
881
+ if found_time and 'GPS Location' in l :
882
+ gps [ntimes - 1 ] = l
883
+ if found_time and "sensor:m_coulomb_amphr_total" in l :
884
+ amph [ntimes - 1 ] = l
885
+ amph = amph [:ntimes ]
886
+ gps = gps [:ntimes ]
887
+ times = times [:ntimes ]
888
+
889
+ # now parse them
890
+ out = xr .Dataset (coords = {'time' : ('surfacing' , np .zeros (ntimes , dtype = 'datetime64[s]' ))})
891
+ out ['ampH' ] = ('surfacing' , np .zeros (ntimes ) * np .NaN )
892
+ out ['lon' ] = ('surfacing' , np .zeros (ntimes ) * np .NaN )
893
+ out ['lat' ] = ('surfacing' , np .zeros (ntimes ) * np .NaN )
894
+
895
+ for i in range (ntimes ):
896
+ timestring = times [i ][11 :- 13 ]
897
+ out ['time' ][i ] = np .datetime64 (datetime .strptime (timestring , '%a %b %d %H:%M:%S %Y' ))
898
+ try :
899
+ st = amph [i ].index ('=' )
900
+ en = amph [i ][st :].index (' ' ) + st
901
+ out ['ampH' ][i ] = float (amph [i ][(st + 1 ):en ])
902
+
903
+ # GPS Location: 4912.737 N -12357.253 E measured 110.757 secs ago
904
+ sp = gps [i ].split (' ' )
905
+ out ['lat' ][i ] = utils .nmea2deg (float (sp [3 ]))
906
+ out ['lon' ][i ] = utils .nmea2deg (float (sp [5 ]))
907
+ except :
908
+ pass
909
+
910
+ return out
911
+
912
+
851
913
__all__ = ['binary_to_rawnc' , 'merge_rawnc' , 'raw_to_timeseries' ,
852
- 'parse_glider_state' ]
914
+ 'parse_glider_state' , 'parse_logfiles' ]
0 commit comments