Skip to content

Commit

Permalink
adapt tests
Browse files Browse the repository at this point in the history
  • Loading branch information
hgloeckner committed Jan 31, 2025
1 parent b057cfa commit a9ef64b
Showing 1 changed file with 20 additions and 25 deletions.
45 changes: 20 additions & 25 deletions tests/test_qc.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"q": {"dims": ("time"), "data": np.array([np.nan, np.nan, np.nan, 0.7])},
"p": {"dims": ("time"), "data": np.array([1.0, 10.0, 100.0, 1000.0])},
"alt": {"dims": ("time"), "data": np.array([30.0, 20.0, 15.0, 10.0])},
"rh": {"dims": ("time"), "data": np.array([30.0, 20.0, np.nan, 10.0])},
"rh": {"dims": ("time"), "data": np.array([30.0, np.nan, np.nan, 10.0])},
},
}

Expand All @@ -42,20 +42,21 @@ def qc():
@pytest.fixture
def qc_vars(qc):
qc.set_qc_variables(["q", "p", "rh"])
qc.set_qc_ds(ds)
qc.alt_dim = "alt"
assert qc.qc_vars == ["q", "p", "rh"]
return qc


def test_profile_sparsity(qc_vars):
qc_vars.profile_sparsity(ds, variable_dict={"q": 2, "p": 2})
qc_vars.profile_sparsity(variable_dict={"q": 2, "p": 2, "rh": 4})
assert qc_vars.qc_flags["p_profile_sparsity"]
assert not qc_vars.qc_flags["q_profile_sparsity"]
assert qc_vars.qc_flags["q_profile_sparsity"]
assert not qc_vars.qc_flags["rh_profile_sparsity"]


def test_near_surface(qc_vars):
qc_vars.near_surface_coverage(
ds, alt_bounds=[0, 18], alt_dim="alt", count_threshold=2
)
qc_vars.near_surface_coverage(alt_bounds=[0, 18], count_threshold=2)

assert qc_vars.qc_flags["p_near_surface"]
assert not qc_vars.qc_flags["q_near_surface"]
Expand All @@ -67,20 +68,18 @@ def test_near_surface(qc_vars):
"qc_flag,output",
[
("p_profile_sparsity", True),
("q_profile_sparsity", False),
("rh_profile_sparsity", False),
("p_profile_sparsity,p_near_surface", True),
("p_profile_sparsity,q_near_surface", False),
(None, True),
("all", False),
("all_except_p_profile_sparsity", False),
("p_profile_sparsity,rh_profile_sparsity", ValueError),
("all_except_rh_profile_sparsity", False),
("p_profile_sparsity,ta_profile_sparsity", ValueError),
],
)
def test_check_qc(qc_vars, qc_flag, output):
qc_vars.profile_sparsity(ds, variable_dict={"q": 2, "p": 2})
qc_vars.near_surface_coverage(
ds, alt_bounds=[0, 18], alt_dim="alt", count_threshold=2
)
qc_vars.profile_sparsity(variable_dict={"q": 2, "p": 2, "rh": 4})
qc_vars.near_surface_coverage(alt_bounds=[0, 18], count_threshold=2)
if (type(output) is type) and issubclass(output, Exception):
with pytest.raises(output):
res = qc_vars.check_qc(used_flags=qc_flag)
Expand All @@ -93,15 +92,13 @@ def test_check_qc(qc_vars, qc_flag, output):
"varname,output",
[
("p", "GOOD"),
("q", "BAD"),
("rh", "UGLY"),
("rh", "BAD"),
("q", "UGLY"),
],
)
def test_add_variable_flag_to_ds(qc_vars, varname, output):
qc_vars.profile_sparsity(ds, variable_dict={"q": 2, "p": 2, "rh": 2})
qc_vars.near_surface_coverage(
ds, alt_bounds=[0, 18], alt_dim="alt", count_threshold=2
)
qc_vars.profile_sparsity(variable_dict={"q": 2, "p": 2, "rh": 4})
qc_vars.near_surface_coverage(alt_bounds=[0, 18], count_threshold=2)
ds_out = qc_vars.add_variable_flags_to_ds(ds, varname)
assert ds_out[f"{varname}_qc"].qc_status == output
assert (
Expand All @@ -115,19 +112,17 @@ def test_add_variable_flag_to_ds(qc_vars, varname, output):
[
(["p"], 0), # GOOD
(["q", "p"], 2), # UGLY
(["q"], 1), # BAD
(["rh"], 2), # UGLY
(["rh"], 1), # BAD
(["q"], 2), # UGLY
(["rh", "q"], 2), # UGLY
(["rh", "p"], 2), # UGLY
],
)
def test_sonde_qc(qc_vars, variables, output):
varname = "sonde_qc"
qc_vars.qc_vars = variables
qc_vars.profile_sparsity(ds, variable_dict={var: 2 for var in variables})
qc_vars.near_surface_coverage(
ds, alt_bounds=[0, 18], alt_dim="alt", count_threshold=2
)
qc_vars.profile_sparsity(variable_dict={"q": 2, "p": 2, "rh": 4})
qc_vars.near_surface_coverage(alt_bounds=[0, 18], count_threshold=2)

ds_out = qc_vars.add_sonde_flag_to_ds(ds, varname)
assert ds_out[varname] == output

0 comments on commit a9ef64b

Please sign in to comment.