Beispiel #1
0
 def test_decode_cf_with_drop_variables(self):
     original = Dataset({
         't': ('t', [0, 1, 2], {
             'units': 'days since 2000-01-01'
         }),
         'x': ("x", [9, 8, 7], {
             'units': 'km'
         }),
         'foo': (('t', 'x'), [[0, 0, 0], [1, 1, 1], [2, 2, 2]], {
             'units': 'bar'
         }),
         'y': ('t', [5, 10, -999], {
             '_FillValue': -999
         })
     })
     expected = Dataset({
         't':
         pd.date_range('2000-01-01', periods=3),
         'x': ("x", [0, 1, 2]),
         'foo': (('t', 'x'), [[0, 0, 0], [1, 1, 1], [2, 2, 2]], {
             'units': 'bar'
         }),
         'y': ('t', [5, 10, np.nan])
     })
     actual = conventions.decode_cf(original, drop_variables=("x", ))
     actual2 = conventions.decode_cf(original, drop_variables="x")
     self.assertDatasetIdentical(expected, actual)
     self.assertDatasetIdentical(expected, actual2)
Beispiel #2
0
    def test_dataset_repr_with_netcdf4_datetimes(self):
        # regression test for #347
        attrs = {'units': 'days since 0001-01-01', 'calendar': 'noleap'}
        with warnings.catch_warnings():
            warnings.filterwarnings('ignore', 'unable to decode time')
            ds = decode_cf(Dataset({'time': ('time', [0, 1], attrs)}))
            self.assertIn('(time) object', repr(ds))

        attrs = {'units': 'days since 1900-01-01'}
        ds = decode_cf(Dataset({'time': ('time', [0, 1], attrs)}))
        self.assertIn('(time) datetime64[ns]', repr(ds))
Beispiel #3
0
    def test_dataset_repr_with_netcdf4_datetimes(self):
        # regression test for #347
        attrs = {'units': 'days since 0001-01-01', 'calendar': 'noleap'}
        with warnings.catch_warnings():
            warnings.filterwarnings('ignore', 'unable to decode time')
            ds = decode_cf(Dataset({'time': ('time', [0, 1], attrs)}))
            self.assertIn('(time) object', repr(ds))

        attrs = {'units': 'days since 1900-01-01'}
        ds = decode_cf(Dataset({'time': ('time', [0, 1], attrs)}))
        self.assertIn('(time) datetime64[ns]', repr(ds))
Beispiel #4
0
 def roundtrip(self, data, decode_cf=True):
     store = CFEncodedInMemoryStore()
     data.dump_to_store(store)
     if decode_cf:
         yield conventions.decode_cf(store)
     else:
         yield Dataset.load_store(store)
Beispiel #5
0
 def roundtrip(self, data, decode_cf=True):
     store = CFEncodedInMemoryStore()
     data.dump_to_store(store)
     if decode_cf:
         yield conventions.decode_cf(store)
     else:
         yield Dataset.load_store(store)
Beispiel #6
0
 def test_decode_cf_with_drop_variables(self):
     original = Dataset({
         't': ('t', [0, 1, 2], {'units': 'days since 2000-01-01'}),
         'x' : ("x", [9, 8, 7], {'units' : 'km'}),
         'foo': (('t', 'x'), [[0, 0, 0], [1, 1, 1], [2, 2, 2]], {'units': 'bar'}),
         'y': ('t', [5, 10, -999], {'_FillValue': -999})
     })
     expected = Dataset({
         't': pd.date_range('2000-01-01', periods=3),
         'x' : ("x", [0, 1, 2]),
         'foo': (('t', 'x'), [[0, 0, 0], [1, 1, 1], [2, 2, 2]], {'units': 'bar'}),
         'y': ('t', [5, 10, np.nan])
     })
     actual = conventions.decode_cf(original, drop_variables=("x",))
     actual2 = conventions.decode_cf(original, drop_variables="x")
     self.assertDatasetIdentical(expected, actual)
     self.assertDatasetIdentical(expected, actual2)
Beispiel #7
0
 def test_decode_coordinates(self):
     # regression test for GH610
     original = Dataset({
         'foo': ('t', [1, 2], {
             'coordinates': 'x'
         }),
         'x': ('t', [4, 5])
     })
     actual = conventions.decode_cf(original)
     self.assertEqual(actual.foo.encoding['coordinates'], 'x')
Beispiel #8
0
 def test_dataset(self):
     original = Dataset({
         't': ('t', [0, 1, 2], {'units': 'days since 2000-01-01'}),
         'foo': ('t', [0, 0, 0], {'coordinates': 'y', 'units': 'bar'}),
         'y': ('t', [5, 10, -999], {'_FillValue': -999})
     })
     expected = Dataset({'foo': ('t', [0, 0, 0], {'units': 'bar'})},
                        {'t': pd.date_range('2000-01-01', periods=3),
                         'y': ('t', [5.0, 10.0, np.nan])})
     actual = conventions.decode_cf(original)
     self.assertDatasetIdentical(expected, actual)
Beispiel #9
0
 def test_dataset(self):
     original = Dataset({
         't': ('t', [0, 1, 2], {'units': 'days since 2000-01-01'}),
         'foo': ('t', [0, 0, 0], {'coordinates': 'y', 'units': 'bar'}),
         'y': ('t', [5, 10, -999], {'_FillValue': -999})
     })
     expected = Dataset({'foo': ('t', [0, 0, 0], {'units': 'bar'})},
                        {'t': pd.date_range('2000-01-01', periods=3),
                         'y': ('t', [5.0, 10.0, np.nan])})
     actual = conventions.decode_cf(original)
     self.assertDatasetIdentical(expected, actual)
Beispiel #10
0
 def test_invalid_coordinates(self):
     # regression test for GH308
     original = Dataset({'foo': ('t', [1, 2], {'coordinates': 'invalid'})})
     actual = conventions.decode_cf(original)
     self.assertDatasetIdentical(original, actual)
Beispiel #11
0
 def test_invalid_units_raises_eagerly(self):
     ds = Dataset({'time': ('time', [0, 1], {'units': 'foobar since 123'})})
     with self.assertRaisesRegexp(ValueError, 'unable to decode time'):
         decode_cf(ds)
Beispiel #12
0
 def test_invalid_coordinates(self):
     # regression test for GH308
     original = Dataset({'foo': ('t', [1, 2], {'coordinates': 'invalid'})})
     actual = conventions.decode_cf(original)
     self.assertDatasetIdentical(original, actual)
Beispiel #13
0
 def test_invalid_units_raises_eagerly(self):
     ds = Dataset({'time': ('time', [0, 1], {'units': 'foobar since 123'})})
     with self.assertRaisesRegexp(ValueError, 'unable to decode time'):
         decode_cf(ds)
Beispiel #14
0
 def test_decode_coordinates(self):
     # regression test for GH610
     original = Dataset({'foo': ('t', [1, 2], {'coordinates': 'x'}),
                         'x': ('t', [4, 5])})
     actual = conventions.decode_cf(original)
     self.assertEqual(actual.foo.encoding['coordinates'], 'x')