From aa113b62b32fd2980b371c81b390259d54986494 Mon Sep 17 00:00:00 2001 From: "Nina.Hakansson" Date: Wed, 27 Feb 2019 14:56:07 +0100 Subject: [PATCH 1/2] Fixing start and end times Signed-off-by: Nina.Hakansson --- satpy/tests/writer_tests/test_cf.py | 58 +++++++++++++++++++++++++++++ satpy/writers/cf_writer.py | 4 +- 2 files changed, 60 insertions(+), 2 deletions(-) diff --git a/satpy/tests/writer_tests/test_cf.py b/satpy/tests/writer_tests/test_cf.py index cea575464a..e4999a44b4 100644 --- a/satpy/tests/writer_tests/test_cf.py +++ b/satpy/tests/writer_tests/test_cf.py @@ -113,6 +113,64 @@ def test_bounds(self): finally: os.remove(filename) + def test_bounds_minimum(self): + from satpy import Scene + import xarray as xr + import tempfile + scn = Scene() + start_timeA = datetime(2018, 5, 30, 10, 0) #expected to be used + end_timeA = datetime(2018, 5, 30, 10, 20) + start_timeB = datetime(2018, 5, 30, 10, 3) + end_timeB = datetime(2018, 5, 30, 10, 15) #expected to be used + test_arrayA = np.array([[1, 2], [3, 4]]).reshape(2, 2, 1) + test_arrayB = np.array([[1, 2], [3, 5]]).reshape(2, 2, 1) + scn['test-arrayA'] = xr.DataArray(test_arrayA, + dims=['x', 'y', 'time'], + coords={'time': [np.datetime64('2018-05-30T10:05:00')]}, + attrs=dict(start_time=start_timeA, + end_time=end_timeA)) + scn['test-arrayB'] = xr.DataArray(test_arrayB, + dims=['x', 'y', 'time'], + coords={'time': [np.datetime64('2018-05-30T10:05:00')]}, + attrs=dict(start_time=start_timeB, + end_time=end_timeB)) + try: + handle, filename = tempfile.mkstemp() + os.close(handle) + scn.save_datasets(filename=filename, writer='cf') + import h5netcdf as nc4 + with nc4.File(filename) as f: + self.assertTrue(all(f['time_bnds'][:] == np.array([-300., 600.]))) + finally: + os.remove(filename) + + def test_bounds_missing_time_info(self): + from satpy import Scene + import xarray as xr + import tempfile + scn = Scene() + start_timeA = datetime(2018, 5, 30, 10, 0) + end_timeA = datetime(2018, 5, 30, 10, 15) + test_arrayA = np.array([[1, 2], [3, 4]]).reshape(2, 2, 1) + test_arrayB = np.array([[1, 2], [3, 5]]).reshape(2, 2, 1) + scn['test-arrayA'] = xr.DataArray(test_arrayA, + dims=['x', 'y', 'time'], + coords={'time': [np.datetime64('2018-05-30T10:05:00')]}, + attrs=dict(start_time=start_timeA, + end_time=end_timeA)) + scn['test-arrayB'] = xr.DataArray(test_arrayB, + dims=['x', 'y', 'time'], + coords={'time': [np.datetime64('2018-05-30T10:05:00')]}) + try: + handle, filename = tempfile.mkstemp() + os.close(handle) + scn.save_datasets(filename=filename, writer='cf') + import h5netcdf as nc4 + with nc4.File(filename) as f: + self.assertTrue(all(f['time_bnds'][:] == np.array([-300., 600.]))) + finally: + os.remove(filename) + def test_encoding_kwarg(self): from satpy import Scene import xarray as xr diff --git a/satpy/writers/cf_writer.py b/satpy/writers/cf_writer.py index dcf2f52f8a..a10d1cd19a 100644 --- a/satpy/writers/cf_writer.py +++ b/satpy/writers/cf_writer.py @@ -212,13 +212,13 @@ def _collect_datasets(self, datasets, kwargs): ds_collection.update(get_extra_ds(ds)) datas = {} + start_times = [] + end_times = [] for ds in ds_collection.values(): try: new_datasets = area2cf(ds) except KeyError: new_datasets = [ds.copy(deep=True)] - start_times = [] - end_times = [] for new_ds in new_datasets: start_times.append(new_ds.attrs.pop("start_time", None)) end_times.append(new_ds.attrs.pop("end_time", None)) From a6cc153ef82fc09e1809202fa393d2e37199d5ef Mon Sep 17 00:00:00 2001 From: "Nina.Hakansson" Date: Wed, 27 Feb 2019 15:17:43 +0100 Subject: [PATCH 2/2] Editorial fixes after flake8 diff test --- satpy/tests/writer_tests/test_cf.py | 30 ++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/satpy/tests/writer_tests/test_cf.py b/satpy/tests/writer_tests/test_cf.py index e4999a44b4..b18fde57c0 100644 --- a/satpy/tests/writer_tests/test_cf.py +++ b/satpy/tests/writer_tests/test_cf.py @@ -118,22 +118,22 @@ def test_bounds_minimum(self): import xarray as xr import tempfile scn = Scene() - start_timeA = datetime(2018, 5, 30, 10, 0) #expected to be used + start_timeA = datetime(2018, 5, 30, 10, 0) # expected to be used end_timeA = datetime(2018, 5, 30, 10, 20) start_timeB = datetime(2018, 5, 30, 10, 3) - end_timeB = datetime(2018, 5, 30, 10, 15) #expected to be used + end_timeB = datetime(2018, 5, 30, 10, 15) # expected to be used test_arrayA = np.array([[1, 2], [3, 4]]).reshape(2, 2, 1) test_arrayB = np.array([[1, 2], [3, 5]]).reshape(2, 2, 1) scn['test-arrayA'] = xr.DataArray(test_arrayA, - dims=['x', 'y', 'time'], - coords={'time': [np.datetime64('2018-05-30T10:05:00')]}, - attrs=dict(start_time=start_timeA, - end_time=end_timeA)) + dims=['x', 'y', 'time'], + coords={'time': [np.datetime64('2018-05-30T10:05:00')]}, + attrs=dict(start_time=start_timeA, + end_time=end_timeA)) scn['test-arrayB'] = xr.DataArray(test_arrayB, - dims=['x', 'y', 'time'], - coords={'time': [np.datetime64('2018-05-30T10:05:00')]}, - attrs=dict(start_time=start_timeB, - end_time=end_timeB)) + dims=['x', 'y', 'time'], + coords={'time': [np.datetime64('2018-05-30T10:05:00')]}, + attrs=dict(start_time=start_timeB, + end_time=end_timeB)) try: handle, filename = tempfile.mkstemp() os.close(handle) @@ -154,10 +154,10 @@ def test_bounds_missing_time_info(self): test_arrayA = np.array([[1, 2], [3, 4]]).reshape(2, 2, 1) test_arrayB = np.array([[1, 2], [3, 5]]).reshape(2, 2, 1) scn['test-arrayA'] = xr.DataArray(test_arrayA, - dims=['x', 'y', 'time'], - coords={'time': [np.datetime64('2018-05-30T10:05:00')]}, - attrs=dict(start_time=start_timeA, - end_time=end_timeA)) + dims=['x', 'y', 'time'], + coords={'time': [np.datetime64('2018-05-30T10:05:00')]}, + attrs=dict(start_time=start_timeA, + end_time=end_timeA)) scn['test-arrayB'] = xr.DataArray(test_arrayB, dims=['x', 'y', 'time'], coords={'time': [np.datetime64('2018-05-30T10:05:00')]}) @@ -167,7 +167,7 @@ def test_bounds_missing_time_info(self): scn.save_datasets(filename=filename, writer='cf') import h5netcdf as nc4 with nc4.File(filename) as f: - self.assertTrue(all(f['time_bnds'][:] == np.array([-300., 600.]))) + self.assertTrue(all(f['time_bnds'][:] == np.array([-300., 600.]))) finally: os.remove(filename)