Esempio n. 1
0
    def test_keyword_dataset_esmf(self):
        """Test with operations on an ESMF Field."""

        efield = self.get_esmf_field()
        output_format = OutputFormat.iter_possible()
        for kk in output_format:
            ops = OcgOperations(dataset=efield, output_format=kk, prefix=kk)
            ops.execute()
        # self.inspect(ret)
        raise
Esempio n. 2
0
    def test_keyword_dataset_esmf(self):
        """Test with operations on an ESMF Field."""

        efield = self.get_esmf_field(with_bounds=True)
        output_format = OutputFormat.iter_possible()
        for kk in output_format:
            # Geojson may only be written with a WGS84 coordinate system.
            if kk == constants.OUTPUT_FORMAT_GEOJSON:
                output_crs = CFWGS84()
            else:
                output_crs = None
            try:
                ops = OcgOperations(dataset=efield, output_format=kk, prefix=kk, output_crs=output_crs)
            except DefinitionValidationError:
                self.assertEqual(kk, constants.OUTPUT_FORMAT_METADATA_JSON)
                continue
            ret = ops.execute()
            self.assertIsNotNone(ret)
        efield.destroy()
Esempio n. 3
0
    def test_dataset_as_field(self):
        """Test with dataset as field not loaded from file - hence, no metadata."""

        import ESMF

        kwds = dict(output_format=list(OutputFormat.iter_possible()),
                    crs=[None, WGS84()])

        for ii, k in enumerate(self.iter_product_keywords(kwds)):
            field = self.get_field(crs=k.crs)

            ops = OcgOperations(dataset=field)
            ret = ops.execute()
            self.assertNumpyAll(ret.gvu(1, 'foo'), field.variables['foo'].value)

            try:
                ops = OcgOperations(dataset=field, output_format=k.output_format, prefix=str(ii))
            except DefinitionValidationError:
                self.assertEqual(k.output_format, constants.OUTPUT_FORMAT_METADATA_JSON)
                continue
            try:
                ret = ops.execute()
            except ValueError:
                if k.output_format == constants.OUTPUT_FORMAT_NETCDF_UGRID_2D_FLEXIBLE_MESH:
                    self.assertIsNone(field.spatial.geom.polygon)
                    continue
                self.assertIsNone(k.crs)
                self.assertIn(k.output_format, [constants.OUTPUT_FORMAT_CSV, constants.OUTPUT_FORMAT_CSV_SHAPEFILE,
                                                constants.OUTPUT_FORMAT_GEOJSON, constants.OUTPUT_FORMAT_SHAPEFILE])
                continue

            if k.output_format == constants.OUTPUT_FORMAT_NUMPY:
                self.assertIsInstance(ret[1]['foo'], Field)
                continue
            if k.output_format == constants.OUTPUT_FORMAT_METADATA_OCGIS:
                self.assertIsInstance(ret, basestring)
                self.assertTrue(len(ret) > 50)
                continue
            if k.output_format == constants.OUTPUT_FORMAT_ESMPY_GRID:
                self.assertIsInstance(ret, ESMF.Field)
                continue

            folder = os.path.split(ret)[0]

            path_did = os.path.join(folder, '{0}_did.csv'.format(ops.prefix))
            with open(path_did, 'r') as f:
                rows = list(csv.DictReader(f))
            self.assertEqual(rows, [
                {'ALIAS': 'foo', 'DID': '1', 'URI': '', 'UNITS': '', 'STANDARD_NAME': '', 'VARIABLE': 'foo',
                 'LONG_NAME': ''}])

            path_source_metadata = os.path.join(folder, '{0}_source_metadata.txt'.format(ops.prefix))
            with open(path_source_metadata, 'r') as f:
                rows = f.readlines()
            self.assertEqual(rows, [])

            if k.output_format == 'nc':
                with self.nc_scope(ret) as ds:
                    variables_expected = [u'time', u'row', u'col', u'foo']
                    try:
                        self.assertAsSetEqual(ds.variables.keys(), variables_expected)
                    except AssertionError:
                        self.assertIsNotNone(k.crs)
                        variables_expected.append('latitude_longitude')
                        self.assertAsSetEqual(ds.variables.keys(), variables_expected)
                    self.assertNumpyAll(ds.variables['time'][:], field.temporal.value_numtime)
                    self.assertNumpyAll(ds.variables['row'][:], field.spatial.grid.row.value)
                    self.assertNumpyAll(ds.variables['col'][:], field.spatial.grid.col.value)
                    self.assertNumpyAll(ds.variables['foo'][:], field.variables['foo'].value.data.squeeze())

            contents = os.listdir(folder)

            expected_contents = [xx.format(ops.prefix) for xx in
                                 '{0}_source_metadata.txt', '{0}_did.csv', '{0}.log', '{0}_metadata.txt']
            if k.output_format == 'nc':
                expected_contents.append('{0}.nc'.format(ops.prefix))
                self.assertAsSetEqual(contents, expected_contents)
            elif k.output_format == constants.OUTPUT_FORMAT_CSV_SHAPEFILE:
                expected_contents.append('{0}.csv'.format(ops.prefix))
                expected_contents.append('shp')
                self.assertAsSetEqual(contents, expected_contents)
            elif k.output_format == constants.OUTPUT_FORMAT_SHAPEFILE:
                expected_contents = ['{0}.shp', '{0}.dbf', '{0}.shx', '{0}.cpg', '{0}.log', '{0}_metadata.txt',
                                     '{0}_source_metadata.txt', '{0}_did.csv', '{0}.prj']
                expected_contents = [xx.format(ops.prefix) for xx in expected_contents]
                self.assertAsSetEqual(contents, expected_contents)