Skip to content

Commit

Permalink
test: more importlib.resources
Browse files Browse the repository at this point in the history
  • Loading branch information
scivision committed Jan 10, 2025
1 parent 6279e36 commit 366dae8
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 46 deletions.
22 changes: 11 additions & 11 deletions src/georinex/tests/test_conv.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@
for OBS RINEX reader
"""

import pytest
import xarray
import importlib.resources as ir

from datetime import datetime

import xarray
import pytest
from pytest import approx
from pathlib import Path

import georinex as gr

R = Path(__file__).parent / "data"


@pytest.mark.parametrize(
"time, exp_time",
Expand Down Expand Up @@ -52,7 +52,7 @@ def test_bad_files(tmp_path):
def test_netcdf_read():
pytest.importorskip("netCDF4")

dat = gr.load(R / "r2all.nc")
dat = gr.load(ir.files(f"{__package__}.data") / "r2all.nc")

assert isinstance(dat, dict), f"{type(dat)}"
assert isinstance(dat["obs"], xarray.Dataset)
Expand All @@ -65,7 +65,7 @@ def test_netcdf_write(tmp_path):
pytest.importorskip("netCDF4")

fn = tmp_path / "rw.nc"
obs = gr.load(R / "demo.10o", out=fn)
obs = gr.load(ir.files(f"{__package__}.data") / "demo.10o", out=fn)

wobs = gr.load(fn)

Expand All @@ -79,7 +79,7 @@ def test_netcdf_write_sp3(tmp_path):
pytest.importorskip("netCDF4")

fn = tmp_path / "sp3.nc"
obs = gr.load(R / "example1.sp3a", out=fn)
obs = gr.load(ir.files(f"{__package__}.data") / "example1.sp3a", out=fn)

wobs = xarray.load_dataset(fn)

Expand All @@ -94,7 +94,7 @@ def test_locs():

pat = ["*o", "*O.rnx", "*O.rnx.gz", "*O.crx", "*O.crx.gz"]

flist = gr.globber(R, pat)
flist = gr.globber(ir.files(f"{__package__}.data"), pat)

locs = gg.get_locations(flist)

Expand All @@ -105,7 +105,7 @@ def test_locs():
def test_nc_load(dtype):
pytest.importorskip("netCDF4")

truth = xarray.open_dataset(R / "r2all.nc", group=dtype)
truth = xarray.open_dataset(ir.files(f"{__package__}.data") / "r2all.nc", group=dtype)

obs = gr.load(R / f"demo.10{dtype[0].lower()}")
obs = gr.load(ir.files(f"{__package__}.data") / f"demo.10{dtype[0].lower()}")
assert obs.equals(truth)
11 changes: 5 additions & 6 deletions src/georinex/tests/test_hatanaka.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,18 @@
Hatanaka CRINEX
"""

import importlib.resources as ir

import pytest
from pathlib import Path
from datetime import datetime

import georinex as gr

R = Path(__file__).parent / "data"


def test_obs2():
pytest.importorskip("hatanaka")

fn = R / "york0440.15d"
fn = ir.files(f"{__package__}.data") / "york0440.15d"

info = gr.rinexinfo(fn)
assert int(info["version"]) == 1
Expand All @@ -33,7 +32,7 @@ def test_obs2():
def test_obs3_gz():
pytest.importorskip("hatanaka")

fn = R / "CEBR00ESP_R_20182000000_01D_30S_MO.crx.gz"
fn = ir.files(f"{__package__}.data") / "CEBR00ESP_R_20182000000_01D_30S_MO.crx.gz"

info = gr.rinexinfo(fn)
assert int(info["version"]) == 3
Expand Down Expand Up @@ -96,7 +95,7 @@ def test_obs3_gz():
def test_obs3_crx(suffix):
pytest.importorskip("hatanaka")

fn = R / ("P43300USA_R_20190012056_17M_15S_MO" + suffix)
fn = ir.files(f"{__package__}.data") / ("P43300USA_R_20190012056_17M_15S_MO" + suffix)

info = gr.rinexinfo(fn)

Expand Down
27 changes: 15 additions & 12 deletions src/georinex/tests/test_header.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,24 @@
import importlib.resources as ir

import pytest
from pathlib import Path

import georinex as gr


R = Path(__file__).parent / "data"


@pytest.mark.parametrize(
"fn, rtype, vers",
[
(R / "minimal2.10o", "obs", 2.11),
(R / "minimal3.10o", "obs", 3.01),
(R / "minimal2.10n", "nav", 2.11),
(R / "minimal3.10n", "nav", 3.01),
(R / "york0440.15d", "obs", 1.00),
(R / "r2all.nc", "obs", 2.11),
("minimal2.10o", "obs", 2.11),
("minimal3.10o", "obs", 3.01),
("minimal2.10n", "nav", 2.11),
("minimal3.10n", "nav", 3.01),
("york0440.15d", "obs", 1.00),
("r2all.nc", "obs", 2.11),
],
ids=["obs2", "obs3", "nav2", "nav3", "Cobs1", "NetCDF_obs2"],
)
def test_header(fn, rtype, vers):
fn = ir.files(f"{__package__}.data") / fn
if fn.suffix == ".nc":
pytest.importorskip("netCDF4")

Expand All @@ -33,7 +32,11 @@ def test_header(fn, rtype, vers):
assert isinstance(hdr, dict)


@pytest.mark.parametrize("fn", [R / "demo.10o", R / "demo3.10o"], ids=["obs2", "obs3"])
@pytest.mark.parametrize(
"fn",
["demo.10o", "demo3.10o"],
ids=["obs2", "obs3"],
)
def test_position(fn):
hdr = gr.rinexheader(fn)
hdr = gr.rinexheader(ir.files(f"{__package__}.data") / fn)
assert len(hdr["position"]) == 3
39 changes: 22 additions & 17 deletions src/georinex/tests/test_nav3.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
from pathlib import Path
import importlib.resources as ir
import pytest
from pytest import approx
import xarray
from datetime import datetime
import georinex as gr


R = Path(__file__).parent / "data"


@pytest.mark.parametrize("fname", ["demo_nav3.17n"])
def test_nav3header(fname):
hdr = gr.rinexheader(R / fname)
hdr = gr.rinexheader(ir.files(f"{__package__}.data") / fname)
assert hdr["IONOSPHERIC CORR"]["GPSA"] == approx(
[1.1176e-08, -1.4901e-08, -5.9605e-08, 1.1921e-07]
)
Expand All @@ -22,15 +19,15 @@ def test_nav3header(fname):

@pytest.mark.parametrize("fname", ["VILL00ESP_R_20181700000_01D_MN.rnx.gz"])
def test_time(fname):
times = gr.gettime(R / fname)
times = gr.gettime(ir.files(f"{__package__}.data") / fname)

assert times[0] == datetime(2018, 4, 24, 8)
assert times[-1] == datetime(2018, 6, 20, 22)


@pytest.mark.parametrize("fname", ["CEDA00USA_R_20182100000_01D_MN.rnx.gz"])
def test_tlim_past_eof(fname):
fn = R / fname
fn = ir.files(f"{__package__}.data") / fname
nav = gr.load(fn, tlim=("2018-07-29T23", "2018-07-29T23:30"))

times = gr.to_datetime(nav.time)
Expand All @@ -44,7 +41,7 @@ def test_spare(fname):
NAV3 with filled spare fields (many files omit the spare fields)
"""

fn = R / fname
fn = ir.files(f"{__package__}.data") / fname
nav = gr.load(fn)
G01 = nav.sel(sv="G01").dropna(dim="time", how="all")
assert G01.time.size == 2
Expand All @@ -53,7 +50,7 @@ def test_spare(fname):

@pytest.mark.parametrize("fname", ["ELKO00USA_R_20182100000_01D_MN.rnx.gz"])
def test_mixed(fname):
fn = R / fname
fn = ir.files(f"{__package__}.data") / fname
nav = gr.load(fn, tlim=(datetime(2018, 7, 28, 21), datetime(2018, 7, 28, 23)))

E04 = nav.sel(sv="E04").dropna(dim="time", how="all")
Expand Down Expand Up @@ -185,7 +182,7 @@ def test_mixed(fname):
ids=["SBAS", "GPS", "BDS", "GAL", "GLO"],
)
def test_large(filename, sv, shape):
nav = gr.load(R / filename, use=sv[0])
nav = gr.load(ir.files(f"{__package__}.data") / filename, use=sv[0])

assert nav.svtype[0] == sv[0] and len(nav.svtype) == 1

Expand All @@ -204,7 +201,7 @@ def test_large(filename, sv, shape):
"sv, size", [("C05", 25), ("E05", 45), ("G05", 7), ("R05", 19), ("S36", 542)]
)
def test_large_all(sv, size):
fn = R / "VILL00ESP_R_20181700000_01D_MN.rnx.gz"
fn = ir.files(f"{__package__}.data") / "VILL00ESP_R_20181700000_01D_MN.rnx.gz"
nav = gr.load(fn)
assert set(nav.svtype) == {"C", "E", "G", "R", "S"}

Expand All @@ -231,16 +228,16 @@ def test_ref(rfn, ncfn):
"""
pytest.importorskip("netCDF4")

truth = gr.load(R / ncfn)
nav = gr.load(R / rfn)
truth = gr.load(ir.files(f"{__package__}.data") / ncfn)
nav = gr.load(ir.files(f"{__package__}.data") / rfn)

for v in nav.data_vars:
assert truth[v].equals(nav[v])
assert nav.equals(truth)


def test_ionospheric_correction():
nav = gr.load(R / "demo_nav3.17n")
nav = gr.load(ir.files(f"{__package__}.data") / "demo_nav3.17n")

assert nav.attrs["ionospheric_corr_GPS"] == approx(
[
Expand All @@ -255,14 +252,18 @@ def test_ionospheric_correction():
]
)

nav = gr.load(R / "galileo3.15n")
nav = gr.load(ir.files(f"{__package__}.data") / "galileo3.15n")

assert nav.attrs["ionospheric_corr_GAL"] == approx([0.1248e03, 0.5039, 0.2377e-01])


def test_missing_fields():
"""Tests the conditions when missing fields exist within rinex data."""
nav = gr.load(R / "BRDC00IGS_R_20201360000_01D_MN.rnx", use="E", verbose=True)
nav = gr.load(
ir.files(f"{__package__}.data") / "BRDC00IGS_R_20201360000_01D_MN.rnx",
use="E",
verbose=True,
)
# missing fields should be interpreted as zero and not NaN
# no NaN values should exist when loading the provided rinex file
assert nav.to_dataframe().isna().sum().sum() == 0
Expand All @@ -276,5 +277,9 @@ def test_missing_fields_end_of_line():
value should be interpretted as 0.
"""
nav = gr.load(R / "BRDM00DLR_R_20130010000_01D_MN.rnx", use="J", verbose=True)
nav = gr.load(
ir.files(f"{__package__}.data") / "BRDM00DLR_R_20130010000_01D_MN.rnx",
use="J",
verbose=True,
)
assert nav.to_dataframe()["FitIntvl"].to_list() == [0.0, 0.0]

0 comments on commit 366dae8

Please sign in to comment.