Skip to content
Open
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 63 additions & 15 deletions fre/pp/tests/test_split_netcdf.py
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@cwhitlock-NOAA i'll admit that in these testing routines, the one nccmp call via subprocess does an impressive amount for us in very few lines.

But payoff is pretty good- for learning to compare these fields with other packages, we can simplify fre-cli's environment a tad. that means faster CI runs for everyone afterwards too

Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
from pathlib import Path

import click
import numpy as np
import pytest
import xarray as xr
from click.testing import CliRunner

from fre import fre
Expand Down Expand Up @@ -162,14 +164,25 @@ def test_split_file_data(workdir,newdir, origdir):
print(f"orig count: {orig_count} new count: {new_count}")
all_files_equal=True
for sf in split_files:
nccmp_cmd = [ 'nccmp', '-d', '--force',
osp.join(origdir, sf), osp.join(newdir, sf) ]
sp = subprocess.run( nccmp_cmd)
if sp.returncode != 0:
all_files_equal=False
print(" ".join(nccmp_cmd))
print("comparison of " + nccmp_cmd[-1] + " and " + nccmp_cmd[-2] + " did not match")
print(sp.stdout, sp.stderr)
these_files_equal=False
orig_file = osp.join(origdir, sf)
new_file = osp.join(newdir, sf)
ds_orig = None
ds_new = None
try:
ds_orig = xr.open_dataset(orig_file)
ds_new = xr.open_dataset(new_file)
xr.testing.assert_equal(ds_orig, ds_new)
these_files_equal=True
except AssertionError:
these_files_equal=False
print(f"data comparison of {new_file} and {orig_file} did not match")
finally:
all_files_equal = all_files_equal and these_files_equal
if ds_orig is not None:
ds_orig.close()
if ds_new is not None:
ds_new.close()
assert all_files_equal and same_count_files

#test_split_file_metadata is currently commented out because the set of commands:
Expand Down Expand Up @@ -209,14 +222,49 @@ def test_split_file_metadata(workdir,newdir, origdir):
same_count_files = new_count == orig_count
all_files_equal=True
for sf in split_files:
nccmp_cmd = [ 'nccmp', '-mg', '--force',
osp.join(origdir, sf), osp.join(newdir, sf) ]
sp = subprocess.run( nccmp_cmd)
if sp.returncode != 0:
print(" ".join(nccmp_cmd))
orig_file = osp.join(origdir, sf)
new_file = osp.join(newdir, sf)
ds_orig = None
ds_new = None
try:
ds_orig = xr.open_dataset(orig_file)
ds_new = xr.open_dataset(new_file)
# Compare global attributes (-g flag equivalent)
assert set(ds_orig.attrs.keys()) == set(ds_new.attrs.keys()), \
f"global attribute keys differ for {sf}"
for attr_key in ds_orig.attrs:
orig_val = ds_orig.attrs[attr_key]
new_val = ds_new.attrs[attr_key]
if isinstance(orig_val, np.ndarray):
assert np.array_equal(orig_val, new_val), \
f"global attribute {attr_key} differs for {sf}"
else:
assert orig_val == new_val, \
f"global attribute {attr_key} differs for {sf}"
# Compare variable metadata/attributes (-m flag equivalent)
assert set(ds_orig.variables) == set(ds_new.variables), \
f"variable sets differ for {sf}"
for var in ds_orig.variables:
assert set(ds_orig[var].attrs.keys()) == set(ds_new[var].attrs.keys()), \
f"attribute keys for variable {var} differ in {sf}"
for attr_key in ds_orig[var].attrs:
orig_val = ds_orig[var].attrs[attr_key]
new_val = ds_new[var].attrs[attr_key]
if isinstance(orig_val, np.ndarray):
assert np.array_equal(orig_val, new_val), \
f"attribute {attr_key} for variable {var} differs in {sf}"
else:
assert orig_val == new_val, \
f"attribute {attr_key} for variable {var} differs in {sf}"
except AssertionError as exc:
all_files_equal=False
print("comparison of " + nccmp_cmd[-1] + " and " + nccmp_cmd[-2] + " did not match")
print(sp.stdout, sp.stderr)
print(f"metadata comparison of {new_file} and {orig_file} did not match")
print(exc)
finally:
if ds_orig is not None:
ds_orig.close()
if ds_new is not None:
ds_new.close()
assert all_files_equal and same_count_files

#clean up splitting files
Expand Down
Loading