|
| 1 | +""" |
| 2 | +Helper functions for working with netCDF files |
| 3 | +""" |
| 4 | + |
| 5 | +import numpy as np |
| 6 | +import xarray as xr |
| 7 | +from netCDF4 import Dataset # pylint: disable=no-name-in-module |
| 8 | + |
| 9 | + |
| 10 | +def _is_dtype_nan_capable(ndarray: np.ndarray): |
| 11 | + """ |
| 12 | + Given a numpy array, return True if it's capable of taking a NaN |
| 13 | + """ |
| 14 | + try: |
| 15 | + np.isnan(ndarray) |
| 16 | + return True |
| 17 | + except TypeError: |
| 18 | + return False |
| 19 | + |
| 20 | + |
| 21 | +def _are_dicts_identical_nansequal(dict0: dict, dict1: dict, keys_to_ignore=None): |
| 22 | + """ |
| 23 | + Compare two dictionaries, considering NaNs to be equal. Don't be strict here about types; if |
| 24 | + they can be coerced to comparable types and then they match, return True. |
| 25 | + """ |
| 26 | + # pylint: disable=too-many-return-statements |
| 27 | + |
| 28 | + if keys_to_ignore is None: |
| 29 | + keys_to_ignore = [] |
| 30 | + keys_to_ignore = np.array(keys_to_ignore) |
| 31 | + |
| 32 | + if len(dict0) != len(dict1): |
| 33 | + return False |
| 34 | + for key, value0 in dict0.items(): |
| 35 | + if key in keys_to_ignore: |
| 36 | + continue |
| 37 | + if key not in dict1: |
| 38 | + return False |
| 39 | + value1 = dict1[key] |
| 40 | + |
| 41 | + # Coerce to numpy arrays to simplify comparison code |
| 42 | + value0 = np.array(value0) |
| 43 | + value1 = np.array(value1) |
| 44 | + |
| 45 | + # Compare, only asking to check equal NaNs if both are capable of taking NaN values |
| 46 | + both_are_nan_capable = _is_dtype_nan_capable(value0) and _is_dtype_nan_capable(value1) |
| 47 | + if not np.array_equal(value0, value1, equal_nan=both_are_nan_capable): |
| 48 | + return False |
| 49 | + |
| 50 | + return True |
| 51 | + |
| 52 | + |
| 53 | +def get_netcdf_format(file_path): |
| 54 | + """ |
| 55 | + Get format of netCDF file |
| 56 | + """ |
| 57 | + with Dataset(file_path, "r") as netcdf_file: |
| 58 | + netcdf_format = netcdf_file.data_model |
| 59 | + return netcdf_format |
| 60 | + |
| 61 | + |
| 62 | +def _is_dataarray_metadata_identical(da0: xr.DataArray, da1: xr.DataArray, keys_to_ignore=None): |
| 63 | + """ |
| 64 | + Check whether two DataArrays have identical-enough metadata |
| 65 | + """ |
| 66 | + |
| 67 | + # Check data type |
| 68 | + if da0.dtype != da1.dtype: |
| 69 | + return False |
| 70 | + |
| 71 | + # Check encoding |
| 72 | + if not _are_dicts_identical_nansequal( |
| 73 | + da0.encoding, da1.encoding, keys_to_ignore=keys_to_ignore |
| 74 | + ): |
| 75 | + return False |
| 76 | + |
| 77 | + # Check attributes |
| 78 | + if not _are_dicts_identical_nansequal(da0.attrs, da1.attrs): |
| 79 | + return False |
| 80 | + |
| 81 | + # Check name |
| 82 | + if da0.name != da1.name: |
| 83 | + return False |
| 84 | + |
| 85 | + # Check dims |
| 86 | + if da0.dims != da1.dims: |
| 87 | + return False |
| 88 | + |
| 89 | + return True |
| 90 | + |
| 91 | + |
| 92 | +def _is_dataarray_data_identical(da0: xr.DataArray, da1: xr.DataArray): |
| 93 | + """ |
| 94 | + Check whether two DataArrays have identical data |
| 95 | + """ |
| 96 | + # pylint: disable=too-many-return-statements |
| 97 | + |
| 98 | + # Check sizes |
| 99 | + if da0.sizes != da1.sizes: |
| 100 | + return False |
| 101 | + |
| 102 | + # Check coordinates |
| 103 | + if bool(da0.coords) or bool(da1.coords): |
| 104 | + if not bool(da0.coords) or not bool(da1.coords): |
| 105 | + return False |
| 106 | + if not da0.coords.equals(da1.coords): |
| 107 | + return False |
| 108 | + |
| 109 | + # Check values ("The array's data converted to numpy.ndarray") |
| 110 | + if not np.array_equal(da0.values, da1.values): |
| 111 | + # Try-except to avoid TypeError from putting NaN-incapable dtypes through |
| 112 | + # np.array_equal(..., equal_nan=True) |
| 113 | + try: |
| 114 | + if not np.array_equal(da0.values, da1.values, equal_nan=True): |
| 115 | + return False |
| 116 | + except TypeError: |
| 117 | + return False |
| 118 | + |
| 119 | + # Check data ("The DataArray's data as an array. The underlying array type (e.g. dask, sparse, |
| 120 | + # pint) is preserved.") |
| 121 | + da0_data_type = type(da0.data) |
| 122 | + if not isinstance(da1.data, da0_data_type): |
| 123 | + return False |
| 124 | + if not isinstance(da0.data, np.ndarray): |
| 125 | + raise NotImplementedError(f"Add support for comparing two objects of type {da0_data_type}") |
| 126 | + |
| 127 | + return True |
| 128 | + |
| 129 | + |
| 130 | +def are_xr_dataarrays_identical(da0: xr.DataArray, da1: xr.DataArray, keys_to_ignore=None): |
| 131 | + """ |
| 132 | + Comprehensively check whether two DataArrays are identical |
| 133 | + """ |
| 134 | + if not _is_dataarray_metadata_identical(da0, da1, keys_to_ignore=keys_to_ignore): |
| 135 | + return False |
| 136 | + |
| 137 | + if not _is_dataarray_data_identical(da0, da1): |
| 138 | + return False |
| 139 | + |
| 140 | + # Fallback to however xarray defines equality, in case we missed something above |
| 141 | + return da0.equals(da1) |
0 commit comments