Exemple #1
0
def test_compare_data_type(dummy_nc, tchange):
    """Check differing data types are reported"""
    actual_nc, expected_nc = dummy_nc
    expected_ds = nc.Dataset(expected_nc, mode="r")
    actual_ds = nc.Dataset(actual_nc, mode="w")

    # copy the whole dataset except for the tchange variable
    # netcdf API does not have the concept of deleting a variable
    for key in expected_ds.ncattrs():
        actual_ds.setncattr(key, expected_ds.getncattr(key))
    # pylint: disable=no-member
    for dim_name in expected_ds.dimensions.keys():
        # pylint: disable=unsubscriptable-object
        actual_ds.createDimension(dim_name,
                                  expected_ds.dimensions[dim_name].size)
    # pylint: disable=no-member
    for var_name in expected_ds.variables.keys():
        if var_name == tchange:
            continue
        new_var = actual_ds.createVariable(var_name,
                                           expected_ds[var_name].datatype,
                                           expected_ds[var_name].dimensions)
        new_var[:] = expected_ds[var_name][:]
        for key in expected_ds[var_name].ncattrs():
            new_var.setncattr(key, expected_ds[var_name].getncattr(key))

    # re-add the type-changed variable, but with a different float type
    expected_var = expected_ds[tchange]
    if expected_var.dtype == np.float32:
        new_type = np.float64
    else:
        new_type = np.float32
    new_dew = actual_ds.createVariable(tchange, new_type,
                                       expected_var.dimensions)
    for key in expected_var.ncattrs():
        new_dew.setncattr(key, expected_var.getncattr(key))
    new_dew[:] = expected_var[:].astype(new_type)

    messages_reported = []

    def message_collector(message):
        messages_reported.append(message)

    compare.compare_vars("root", actual_ds, expected_ds, 0.0, 0.0, None,
                         message_collector)
    assert len(messages_reported) == 1
    assert "type" in messages_reported[0]
Exemple #2
0
def test_compare_vars_renamed(dummy_nc):
    """Check that renaming a variable is identified"""
    actual_nc, expected_nc = dummy_nc
    expected_ds = nc.Dataset(expected_nc, mode="r")
    actual_ds = nc.Dataset(actual_nc, mode="a")
    actual_ds.renameVariable(DEWPOINT, "new_dew")

    messages_reported = []

    def message_collector(message):
        messages_reported.append(message)

    compare.compare_vars("root", actual_ds, expected_ds, 0.0, 0.0, [], None,
                         message_collector)
    assert len(messages_reported) == 1
    assert DEWPOINT in messages_reported[0]
    assert "new_dew" in messages_reported[0]