nasa / ncompare

Compare the structure of two netCDF files at the command line
https://ncompare.readthedocs.io
Apache License 2.0
27 stars 9 forks source link

xarray Dataset.dims FutureWarning #246

Closed nannau closed 4 months ago

nannau commented 4 months ago

Hi there! New ncompare user here. I'm finding it quite useful so far. Many thanks to the developers for providing a great project. "Bug" isn't the best label here, but I wouldn't consider this an enhancement or feature request.

This issue is quite easy to fix, but its output is a bit annoying, especially when comparing lots of files like I am currently doing. Plus, it looks like this change will need to be made anyway. As far as I can tell, there are no functional issues related to this warning (yet).

Expected Behavior

I expect no warnings to be issued.

Actual Behavior

A FutureWarning is displayed related to the attribute dims vs sizes of an xarray dataset. It is raised every time ncompare is used. This does not appear to be related to usage of ncompare, rather, a hardcoded assumption related to an xarray object attribute in ncompare. E.g.:

FutureWarning: The return type of `Dataset.dims` will be changed to return a set of dimension names in future, in order to be more consistent with `DataArray.dims`.
To access a mapping from dimension names to lengths, please use `Dataset.sizes`.

Steps to Reproduce the Problem

  1. Install latest ncompare with pip following: https://github.com/nannau/ncompare/tree/main?tab=readme-ov-file#option-b-using-pip
  2. I am running ncompare from a Python subprocess.call with:
        subprocess.call(
            [
                "ncompare",
                "--column-widths",
                "33",
                "26",
                "26",
                control_path,
                experiment_path,
                "--only-diffs",
                "--file-text",
                f"{output_path}/{control_filename}_diffs.txt",
            ],
        )
  3. The error obviously persists if run manually from terminal, e.g.:
    ncompare --column-widths 33 26 26 /path/to/control.nc /path/to/experiment.nc --only-diffs --file-text diffs.txt

    Proposed solution

In this line of core.py we simply adopt the suggested change to be:

return list(dataset.sizes.items())

I have a fork with this change implemented that I am planning on submitting a PR with.

This change looks like it was introduced in xarray version v2023.12.0 (2023 Dec 08) so I would recommend changing related requirements to avoid backward compatibility issues.

Specifications