Skip to content

Commit

Permalink
Squashed commit of the following:
Browse files Browse the repository at this point in the history
commit 91c579a7f4accb79be020c6cc841973def934ace
Author: Michael Hirsch, Ph.D <[email protected]>
Date:   Thu Jul 26 01:30:05 2018 -0400

    functionalize, header function"

commit df2f3297e2b355aa567dcd781fab9bd97127c3ff
Author: Michael Hirsch, Ph.D <[email protected]>
Date:   Wed Jul 25 23:54:12 2018 -0400

    improve test coverage
  • Loading branch information
scivision committed Jul 26, 2018
1 parent 0057bc3 commit d58669e
Show file tree
Hide file tree
Showing 10 changed files with 325 additions and 151 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,12 @@ only certain fields are valid for particular satellite systems.
Not every receiver receives every type of GNSS system.
Most Android devices in the Americas receive at least GPS and GLONASS.

#### get OBS header
To get a `dict()` of the RINEX file header:
```python
hdr = gr.rinexheader('myfile.rnx')
```

#### Index OBS data

assume the OBS data from a file is loaded in variable `obs`.
Expand Down
95 changes: 64 additions & 31 deletions georinex/__init__.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
from pathlib import Path
import logging
import xarray
from typing import Union, Tuple
from typing import Union, Tuple, Dict, Any
from datetime import datetime
#
from .io import opener
from .rinex2 import rinexnav2, _scan2
from .rinex3 import rinexnav3, _scan3
from .io import rinexinfo
from .rinex2 import rinexnav2, _scan2, obsheader2, navheader2
from .rinex3 import rinexnav3, _scan3, obsheader3, navheader3

# for NetCDF compression. too high slows down with little space savings.
COMPLVL = 1
Expand All @@ -23,34 +23,19 @@ def readrinex(rinexfn: Path, outfn: Path=None,
obs = None
rinexfn = Path(rinexfn).expanduser()
# %% detect type of Rinex file
if rinexfn.suffix in ('.gz', '.zip'):
fnl = rinexfn.stem.lower()
else:
fnl = rinexfn.name.lower()
rtype = rinextype(rinexfn)

if fnl.endswith('n') or fnl.endswith('n.rnx'):
if rtype == 'nav':
nav = rinexnav(rinexfn, outfn)
elif fnl.endswith('o') or fnl.endswith('o.rnx'):
elif rtype == 'obs':
obs = rinexobs(rinexfn, outfn, use=use, verbose=verbose)
elif fnl.endswith('.crx'):
raise NotImplementedError('Hatanaka compressed RINEX is not yet supported')
elif rinexfn.suffix.endswith('.nc'):
elif rtype == 'nc':
nav = rinexnav(rinexfn)
obs = rinexobs(rinexfn)
else:
raise ValueError(f"I dont know what type of file you're trying to read: {rinexfn}")

return obs, nav


def getRinexVersion(fn: Path) -> float:
"""verify RINEX version"""
fn = Path(fn).expanduser()

with opener(fn) as f:
ver = float(f.readline()[:9]) # yes :9

return ver
# %% Navigation file


Expand All @@ -64,13 +49,13 @@ def rinexnav(fn: Path, ofn: Path=None, group: str='NAV') -> xarray.Dataset:
logging.error(f'Group {group} not found in {fn}')
return

ver = getRinexVersion(fn)
if int(ver) == 2:
info = rinexinfo(fn)
if int(info['version']) == 2:
nav = rinexnav2(fn)
elif int(ver) == 3:
elif int(info['version']) == 3:
nav = rinexnav3(fn)
else:
raise ValueError(f'unknown RINEX verion {ver} {fn}')
raise ValueError(f'unknown RINEX {info} {fn}')

if ofn:
ofn = Path(ofn).expanduser()
Expand Down Expand Up @@ -101,14 +86,14 @@ def rinexobs(fn: Path, ofn: Path=None,
logging.error(f'Group {group} not found in {fn}')
return
# %% version selection
ver = getRinexVersion(fn)
info = rinexinfo(fn)

if int(ver) == 2:
if int(info['version']) == 2:
obs = _scan2(fn, use, tlim=tlim, verbose=verbose)
elif int(ver) == 3:
elif int(info['version']) == 3:
obs = _scan3(fn, use, tlim=tlim, verbose=verbose)
else:
raise ValueError(f'unknown RINEX verion {ver} {fn}')
raise ValueError(f'unknown RINEX {info} {fn}')
# %% optional output write
if ofn:
ofn = Path(ofn).expanduser()
Expand All @@ -120,3 +105,51 @@ def rinexobs(fn: Path, ofn: Path=None,
obs.to_netcdf(ofn, group=group, mode=wmode, encoding=enc)

return obs


def rinextype(fn: Path) -> str:
if fn.suffix in ('.gz', '.zip'):
fnl = fn.stem.lower()
else:
fnl = fn.name.lower()

if fnl.endswith('o') or fnl.endswith('o.rnx'):
return 'obs'
elif fnl.endswith('n') or fnl.endswith('n.rnx'):
return 'nav'
elif fnl.endswith('.crx'):
raise NotImplementedError('Hatanaka compressed RINEX is not yet supported')
elif fn.suffix.endswith('.nc'):
return 'nc'
else:
raise ValueError(f"I dont know what type of file you're trying to read: {fn}")


def rinexheader(fn: Path) -> Dict[str, Any]:
"""
retrieve RINEX 2/3 header as unparsed dict()
"""
fn = Path(fn).expanduser()

info = rinexinfo(fn)

rtype = rinextype(fn)

if int(info['version']) == 2:
if rtype == 'obs':
hdr = obsheader2(fn)
elif rtype == 'nav':
hdr = navheader2(fn)
else:
raise ValueError(f'Unknown rinex type in {fn}')
elif int(info['version']) == 3:
if rtype == 'obs':
hdr = obsheader3(fn)
elif rtype == 'nav':
hdr = navheader3(fn)
else:
raise ValueError(f'Unknown rinex type in {fn}')
else:
raise ValueError(f'unknown RINEX {info} {fn}')

return hdr
24 changes: 18 additions & 6 deletions georinex/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
from pathlib import Path
from contextlib import contextmanager
import io
from typing.io import TextIO
from typing import Union, Dict


@contextmanager
Expand All @@ -21,9 +23,19 @@ def opener(fn: Path):
else:
with fn.open('r') as f:
yield f
#
# with zipfile.ZipFile(fn, 'r') as z:
# flist = z.namelist()
# assert len(flist) == 1, f'which filename {flist} do you want from {fn}'
# with z.open(flist[0], 'r') as f:
# yield f


def rinexinfo(f: Union[Path, TextIO]) -> Dict[str, Union[str, float]]:
"""verify RINEX version"""
if isinstance(f, (str, Path)):
fn = Path(f).expanduser()

with opener(fn) as f:
return rinexinfo(f)

line = f.readline()

info = {'version': float(line[:9]), # yes :9
'filetype': line[20]}

return info
Loading

0 comments on commit d58669e

Please sign in to comment.