Skip to content

Commit 51fec2d

Browse files
committed
ENH: slocum logfile parser
1 parent cd07a16 commit 51fec2d

File tree

1 file changed

+63
-1
lines changed

1 file changed

+63
-1
lines changed

pyglider/slocum.py

Lines changed: 63 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import yaml
1414
import pyglider.utils as utils
1515
import xml.etree.ElementTree as ET
16+
from datetime import datetime
1617

1718

1819
_log = logging.getLogger(__name__)
@@ -848,5 +849,66 @@ def parse_gliderState(fname):
848849
return dat
849850

850851

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+
851913
__all__ = ['binary_to_rawnc', 'merge_rawnc', 'raw_to_timeseries',
852-
'parse_glider_state']
914+
'parse_glider_state', 'parse_logfiles']

0 commit comments

Comments
 (0)