diff --git a/act/utils/data_utils.py b/act/utils/data_utils.py index 17682c6187..cebb7a5fea 100644 --- a/act/utils/data_utils.py +++ b/act/utils/data_utils.py @@ -455,8 +455,14 @@ def add_in_nan(time, data): mode = stats.mode(diff, keepdims=True).mode[0] except TypeError: mode = stats.mode(diff).mode[0] + index = np.where(diff > (2.0 * mode)) + # If the data is not float time and we try to insert a NaN it will + # not auto upconvert the data. Need to convert before inserting NaN. + if len(index) > 0 and np.issubdtype(data.dtype, np.integer): + data = data.astype('float32') + offset = 0 for i in index[0]: corr_i = i + offset diff --git a/tests/plotting/baseline/test_add_nan_line_integer.png b/tests/plotting/baseline/test_add_nan_line_integer.png new file mode 100644 index 0000000000..be7bda4d31 Binary files /dev/null and b/tests/plotting/baseline/test_add_nan_line_integer.png differ diff --git a/tests/plotting/test_timeseriesdisplay.py b/tests/plotting/test_timeseriesdisplay.py index 9c237b7001..d1461b3779 100644 --- a/tests/plotting/test_timeseriesdisplay.py +++ b/tests/plotting/test_timeseriesdisplay.py @@ -606,6 +606,34 @@ def test_add_nan_line(): matplotlib.pyplot.close(display.fig) +@pytest.mark.mpl_image_compare(tolerance=10) +def test_add_nan_line_integer(): + data = np.arange(100, dtype=np.int32) + time = np.array('2019-11-01T00:00:00', dtype='datetime64[m]') + np.arange(data.size) + time = time.astype('datetime64[ns]') # Only done to stop a warning appearing + + # Remove data to produce a gap + data = np.delete(data, np.arange(50, 60), axis=0) + time = np.delete(time, np.arange(50, 60), axis=0) + data = np.delete(data, np.arange(70, 75), axis=0) + time = np.delete(time, np.arange(70, 75), axis=0) + + ds = xr.Dataset( + data_vars={'data': ('time', data, {'long_name': 'Data values', 'units': 'degC'})}, + coords={'time': ('time', time, {'long_name': 'Time in UTC'})}, + ) + + display = TimeSeriesDisplay({'test_datastream': ds}, figsize=(15, 10), subplot_shape=(1,)) + display.plot('data', subplot_index=(0,), add_nan=True, marker='.', markersize=20, linewidth=5) + + assert np.issubdtype(ds['data'].dtype, np.integer) + + try: + return display.fig + finally: + matplotlib.pyplot.close(display.fig) + + @pytest.mark.mpl_image_compare(tolerance=10) def test_timeseries_invert(): ds = act.io.arm.read_arm_netcdf(sample_files.EXAMPLE_IRT25m20s)