예제 #1
0
 def test_write_netcdf_unlimited_to_fixedsize(self):
     v = VectorDimension(value=[1, 2, 3], name='foo', unlimited=True)
     path = self.get_temporary_file_path('foobar.nc')
     with self.nc_scope(path, 'w') as ds:
         v.write_netcdf(ds, unlimited_to_fixedsize=True)
     with self.nc_scope(path) as ds:
         d = ds.dimensions['foo']
         self.assertFalse(d.isunlimited())
예제 #2
0
    def test_write_netcdf_bounds_dimension_exists(self):
        """Test writing with bounds when the bounds dimension has already been created."""

        vd = VectorDimension(value=[3., 7.], name='one')
        vd.set_extrapolated_bounds()
        vd2 = VectorDimension(value=[5., 6.], name='two')
        vd2.set_extrapolated_bounds()
        path = os.path.join(self.current_dir_output, 'foo.nc')
        with nc_scope(path, 'w') as ds:
            vd.write_netcdf(ds)
            vd2.write_netcdf(ds)
            self.assertEqual(ds.variables.keys(), ['one', 'one_bounds', 'two', 'two_bounds'])
예제 #3
0
    def test_write_netcdf(self):
        path = os.path.join(self.current_dir_output, 'foo.nc')

        other_bounds_name = 'bnds'
        keywords = dict(with_bounds=[True, False],
                        with_attrs=[True, False],
                        unlimited=[False, True],
                        kwargs=[{}, {'zlib': True}],
                        bounds_dimension_name=[None, other_bounds_name],
                        axis=[None, 'GG'],
                        name=[None, 'temporal'],
                        name_bounds=[None, 'time_bounds'],
                        name_value=[None, 'time'],
                        format=[None, 'NETCDF4_CLASSIC'])

        for k in itr_products_keywords(keywords, as_namedtuple=True):
            if k.with_attrs:
                attrs = {'a': 5, 'b': np.array([5, 6])}
            else:
                attrs = None
            vd = VectorDimension(value=[2., 4.], attrs=attrs, name=k.name, name_bounds=k.name_bounds,
                                 name_value=k.name_value, axis=k.axis, unlimited=k.unlimited)
            if k.with_bounds:
                vd.set_extrapolated_bounds()
            with nc_scope(path, 'w') as ds:
                try:
                    vd.write_netcdf(ds, bounds_dimension_name=k.bounds_dimension_name, **k.kwargs)
                except ValueError:
                    self.assertIsNone(vd.name)
                    continue

            with nc_scope(path, 'r') as ds:
                var = ds.variables[vd.name_value]

                if k.axis is None:
                    axis_actual = ''
                else:
                    axis_actual = vd.axis
                self.assertEqual(var.axis, axis_actual)

                try:
                    self.assertIn(constants.OCGIS_BOUNDS, ds.dimensions)
                except AssertionError:
                    try:
                        self.assertFalse(k.with_bounds)
                    except AssertionError:
                        try:
                            self.assertEqual(k.bounds_dimension_name, other_bounds_name)
                        except AssertionError:
                            self.assertIsNotNone(k.name_bounds_suffix)
                            self.assertIsNone(k.bounds_dimension_name)
                            self.assertIn(k.name_bounds_suffix, ds.variables[vd.name_bounds].dimensions)
                try:
                    self.assertFalse(ds.dimensions[vd.name].isunlimited())
                except AssertionError:
                    self.assertTrue(k.unlimited)

                try:
                    self.assertEqual(var.a, attrs['a'])
                    self.assertNumpyAll(var.b, attrs['b'])
                except AttributeError:
                    self.assertFalse(k.with_attrs)
                try:
                    self.assertEqual(var.bounds, vd.name_bounds)
                    self.assertNumpyAll(vd.bounds, ds.variables[vd.name_bounds][:])
                except (AttributeError, KeyError):
                    self.assertFalse(k.with_bounds)
                self.assertEqual(var._name, vd.name_value)
                self.assertEqual(var.dimensions, (vd.name,))
                self.assertNumpyAll(vd.value, var[:])