Skip to content

Commit

Permalink
handle OBS2 mangled header and data times
Browse files Browse the repository at this point in the history
init
  • Loading branch information
scivision committed Sep 20, 2018
1 parent ac3fe19 commit 053ad4e
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 14 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ before_install:
- conda create -n test python=$TRAVIS_PYTHON_VERSION
- source activate test

install:
install:
- pip install -e .[tests,io]
- if [[ $TRAVIS_PYTHON_VERSION == 3.6* && $TRAVIS_OS_NAME == linux ]]; then pip install -e .[cov]; fi

Expand Down
47 changes: 36 additions & 11 deletions georinex/obs2.py
Original file line number Diff line number Diff line change
Expand Up @@ -348,18 +348,10 @@ def obsheader2(f: TextIO,
if '# OF SATELLITES' in hdr:
hdr['# OF SATELLITES'] = int(hdr['# OF SATELLITES'][:6])
# %% time
t0s = hdr['TIME OF FIRST OBS']
# NOTE: must do second=int(float()) due to non-conforming files
hdr['t0'] = datetime(year=int(t0s[:6]), month=int(t0s[6:12]), day=int(t0s[12:18]),
hour=int(t0s[18:24]), minute=int(t0s[24:30]), second=int(float(t0s[30:36])),
microsecond=int(float(t0s[30:43]) % 1 * 1000000))
hdr['t0'] = _timehdr(hdr['TIME OF FIRST OBS'])

try:
t0s = hdr['TIME OF LAST OBS']
# NOTE: must do second=int(float()) due to non-conforming files
hdr['t1'] = datetime(year=int(t0s[:6]), month=int(t0s[6:12]), day=int(t0s[12:18]),
hour=int(t0s[18:24]), minute=int(t0s[24:30]), second=int(float(t0s[30:36])),
microsecond=int(float(t0s[30:43]) % 1 * 1000000))
hdr['t1'] = _timehdr(hdr['TIME OF LAST OBS'])
except KeyError:
pass

Expand Down Expand Up @@ -439,6 +431,34 @@ def _skip(f: TextIO, ln: str,
f.readline()


def _timehdr(ln: str) -> datetime:
"""
handles malformed header dates
NOTE: must do second=int(float()) due to non-conforming files that don't line up decimal point.
"""

try:
second = int(float(ln[30:36]))
except ValueError:
second = 0

if not 0 <= second <= 59:
second = 0

try:
usec = int(float(ln[30:43]) % 1 * 1000000)
except ValueError:
usec = 0

if not 0 <= usec <= 999999:
usec = 0

return datetime(year=int(ln[:6]), month=int(ln[6:12]), day=int(ln[12:18]),
hour=int(ln[18:24]), minute=int(ln[24:30]),
second=second,
microsecond=usec)


def _timeobs(ln: str) -> Optional[datetime]:
"""
Python >= 3.7 supports nanoseconds. https://www.python.org/dev/peps/pep-0564/
Expand All @@ -451,13 +471,18 @@ def _timeobs(ln: str) -> Optional[datetime]:
else:
year += 1900

try:
usec = int(float(ln[16:26]) % 1 * 1000000)
except ValueError:
usec = 0

return datetime(year=year,
month=int(ln[4:6]),
day=int(ln[7:9]),
hour=int(ln[10:12]),
minute=int(ln[13:15]),
second=int(ln[16:18]),
microsecond=int(float(ln[16:26]) % 1 * 1000000)
microsecond=usec
)
except ValueError: # garbage between header and RINEX data
logging.info(f'garbage detected in RINEX file')
Expand Down
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[metadata]
name = georinex
version = 1.6.7.1
version = 1.6.7.2
author = Michael Hirsch, Ph.D.
author_email = [email protected]
description = Python RINEX 2/3 NAV/OBS reader with speed and simplicity.
Expand Down
24 changes: 24 additions & 0 deletions tests/data/badtime.10o
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
2.11 OBSERVATION DATA M (MIXED) RINEX VERSION / TYPE
7 L1 L2 P1 P2 C1 S1 S2 # / TYPES OF OBSERV
2010 3 5 0 0 0 .0000000 GPS TIME OF FIRST OBS
END OF HEADER
10 3 5 0 0 0 .0000000 0 8G13R19G32G 7R23G31G20R11
130321269.80108 101549030.34908 24799319.672 9 24799319.752 9 24799318.768 7
62.000 80.000
129262004.57708 24597748.629 7
47.000
133135049.38708 103741584.18208 25334766.349 9 25334768.879 9 25334766.309 7
75.000 83.000
133174968.81808 103772690.97708 25342359.815 9 25342359.952 9 25342359.370 7
65.000 45.000
119323293.47908 22706470.024 7
79.000
114311363.56508 92979182.85108 21752728.352 9 21752728.204 9 21752729.338 7
72.000 63.000
135891004.29908 105889081.83208 25859215.981 9 25859207.736 9 25859205.875 7
44.000 46.000
131986783.86108 25116253.066 7
38.000
10 3 5 0 0 60.0000000 0 1G13
130321269.80108 101549030.34908 24799319.672 9 24799319.752 9 24799318.768 7
62.000 80.000
12 changes: 11 additions & 1 deletion tests/test_obs2.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ def test_meas_miss():
assert obs.fast_processing


def test_mangled():
def test_mangled_data():
fn = R/'14601736.18o'

obs = gr.load(fn)
Expand All @@ -161,6 +161,16 @@ def test_mangled():
assert not obs.fast_processing


def test_mangled_times():
fn = R/'badtime.10o'

obs = gr.load(fn)

times = obs.time.values.astype('datetime64[us]').astype(datetime)

assert times


def test_Z_lzw():
pytest.importorskip('unlzw')

Expand Down

0 comments on commit 053ad4e

Please sign in to comment.