Esempio n. 1
0
 def test_metadata_attributes(self):
     self.assertEqual({}, metadata_attributes({}))
     self.assertEqual({}, metadata_attributes({'_dimensions': {}, '_fill_value': -999}))
     self.assertEqual({'title': 'Title'},
                      metadata_attributes({'title': 'Title'})
                      )
     self.assertEqual({'title': 'Title'},
                      metadata_attributes({'title': 'Title', '_fill_value': -999})
                      )
     self.assertIsInstance(metadata_attributes(OrderedDict()), OrderedDict)
Esempio n. 2
0
class TemplateTestCase(unittest.TestCase):
    with open(TEMPLATE_JSON) as t:
        template_dict = json.load(t, object_pairs_hook=OrderedDict)
    dimensions = template_dict['_dimensions']
    variables = template_dict['_variables']
    global_attributes = metadata_attributes(template_dict)
    values1 = np.array([1], dtype=np.float32)
    values10 = np.arange(10, dtype=np.float32)

    @property
    def temp_dir(self):
        if not hasattr(self, '_temp_dir'):
            self._temp_dir = tempfile.mkdtemp(prefix=self.__class__.__name__)
        return self._temp_dir

    @property
    def temp_nc_file(self):
        if not hasattr(self, '_temp_nc_file'):
            with tempfile.NamedTemporaryFile(suffix='.nc', prefix=self.__class__.__name__, dir=self.temp_dir) as f:
                pass
            self._temp_nc_file = f.name
        return self._temp_nc_file

    def tearDown(self):
        if hasattr(self, '_temp_dir'):
            shutil.rmtree(self._temp_dir)
Esempio n. 3
0
    def test_create_file(self):
        template = DatasetTemplate.from_json(TEMPLATE_JSON)
        template.variables['TIME']['_data'] = self.values10
        template.variables['DEPTH']['_data'] = self.values1
        template.variables['TEMP']['_data'] = self.values10.reshape((10, 1))
        template.to_netcdf(self.temp_nc_file)

        dataset = Dataset(self.temp_nc_file)

        expected_dimensions = OrderedDict([
            ('TIME', len(self.values10)),
            ('DEPTH', len(self.values1))
        ])
        ds_dimensions = OrderedDict((k, v.size) for k, v in dataset.dimensions.items())
        self.assertEqual(expected_dimensions, ds_dimensions)

        for vname, vdict in self.variables.items():
            ds_var = dataset[vname]
            self.assertEqual(vdict['_dimensions'], list(ds_var.dimensions))
            self.assertEqual(vdict['_datatype'], ds_var.dtype)
            ds_var_attr = OrderedDict((k, ds_var.getncattr(k)) for k in ds_var.ncattrs())
            self.assertEqual(metadata_attributes(vdict), ds_var_attr)

        self.assertTrue(all(dataset['TIME'] == self.values10))
        self.assertTrue(all(dataset['DEPTH'] == self.values1))
        self.assertTrue(all(dataset['TEMP'] == self.values10.reshape(10, 1)))

        ds_global_attributes = OrderedDict((k, dataset.getncattr(k)) for k in dataset.ncattrs())
        self.assertEqual(self.global_attributes, ds_global_attributes)
Esempio n. 4
0
 def test_init_from_partial_template(self):
     template = DatasetTemplate.from_json(TEMPLATE_PARTIAL_JSON)
     with open(TEMPLATE_PARTIAL_JSON) as t:
         tdict = json.load(t, object_pairs_hook=OrderedDict)
     self.assertEqual({}, template.dimensions)
     self.assertEqual(tdict['_variables'], template.variables)
     self.assertEqual(metadata_attributes(tdict), template.global_attributes)