atmtools/typhon

NetCDF4 file handlers returns incorrect times, ignores scale factor

gerritholl opened this issue · 1 comments

Bug report

Bug summary

The times returned by the NetCDF4 FileHandler are wrong.

Code for reproduction

import os
import tempfile
import xarray as xr
import numpy as np
from typhon.files import NetCDF4
fh = NetCDF4()

with tempfile.TemporaryDirectory() as tdir:
    tfile = os.path.join(tdir, "testfile.nc")
    before = xr.Dataset(
            {"a":
                xr.DataArray(
                    np.array(
                        ["2019-02-14T09:00:00", "2019-02-14T09:00:01"],
                        dtype="M8[ns]"))})
    before["a"].encoding = {
            "units": "seconds since 2019-02-14 09:00:00",
            "scale_factor": 0.1}
    fh.write(before, tfile)
    after = fh.read(tfile)
    assert np.array_equal(before["a"], after["a"])

Actual outcome

The assertion fails.

Expected outcome

The assertion should succeed.

Rather than ignoring the scale_factor, it appears to be applied twice… for when I consider the testfile:

netcdf testfile {
dimensions:
        dim_0 = 2 ;
variables:
        double a(dim_0) ;
                a:_FillValue = NaN ;
                a:units = "seconds since 2019-02-14T09:00:00" ;
                a:calendar = "proleptic_gregorian" ;
                a:scale_factor = 0.1 ;
data:

 a = 0, 10 ;
}

and set a breakpoint at typhon/files/handlers/common.py immediately before it calls xr.decode_cf, I find:

<xarray.Dataset>
Dimensions:  (dim_0: 2)
Dimensions without coordinates: dim_0
Data variables:
    a        (dim_0) float64 0.0 1.0

which means then become 0.0 and 0.1…