Example #1
0
class test_GeodaticDatum(unittest.TestCase):

    def setUp(self):
        self.datum = GeodeticDatum('WGS84')

    def test_toECEF(self):
        x, y, z = self.datum.toECEF(0, 90)
        nptest.assert_almost_equal(np.array([0, 0, self.datum.geod.b]),
                                   np.array([x, y, z]),
                                   decimal=5)

        x, y, z = self.datum.toECEF(0, 0)
        nptest.assert_almost_equal(np.array([self.datum.geod.a, 0, 0]),
                                   np.array([x, y, z]),
                                   decimal=5)

    def test_ParallelRadi(self):
        r = self.datum.ParallelRadi(0.)
        nptest.assert_almost_equal(r, self.datum.geod.a, decimal=5)

        r = self.datum.ParallelRadi(90.)
        nptest.assert_almost_equal(r, 0., decimal=5)

    def test_GeocentricDistance(self):
        r = self.datum.GeocentricDistance(0., 0.)
        nptest.assert_almost_equal(r, self.datum.geod.a, decimal=5)

        r = self.datum.GeocentricDistance(0., 90.)
        nptest.assert_almost_equal(r, self.datum.geod.b, decimal=5)

    def test_ParallelArcDist(self):
        dist = self.datum.ParallelArcDist(0., 0., 360.)
        nptest.assert_almost_equal(dist, self.datum.geod.a * 2 * np.pi)
Example #2
0
    def test_save_lonlat_nc(self):
        grid_nc.save_lonlat(self.testfile,
                            self.lons, self.lats,
                            GeodeticDatum('WGS84'),
                            self.cells,
                            subsets={'subset_flag': {'points': self.subset,
                                                     'meaning': 'test_flag'}},
                            global_attrs={'test': 'test_attribute'})

        with Dataset(self.testfile) as nc_data:
            nptest.assert_array_equal(self.lats, nc_data.variables['lat'][:])
            nptest.assert_array_equal(self.lons, nc_data.variables['lon'][:])
            nptest.assert_array_equal(self.cells, nc_data.variables['cell'][:])
            nptest.assert_array_equal(
                self.subset, np.where(nc_data.variables['subset_flag'][:] == 1)[0])
            assert nc_data.test == 'test_attribute'
Example #3
0
class test_GeodaticDatum(unittest.TestCase):
    def setUp(self):
        self.datum = GeodeticDatum('WGS84')

    def test_toECEF(self):
        x, y, z = self.datum.toECEF(0, 90)
        nptest.assert_almost_equal(np.array([0, 0, self.datum.geod.b]),
                                   np.array([x, y, z]),
                                   decimal=5)

        x, y, z = self.datum.toECEF(0, 0)
        nptest.assert_almost_equal(np.array([self.datum.geod.a, 0, 0]),
                                   np.array([x, y, z]),
                                   decimal=5)

    def test_ParallelRadi(self):
        r = self.datum.ParallelRadi(0.)
        nptest.assert_almost_equal(r, self.datum.geod.a, decimal=5)

        r = self.datum.ParallelRadi(90.)
        nptest.assert_almost_equal(r, 0., decimal=5)

    def test_GeocentricDistance(self):
        r = self.datum.GeocentricDistance(0., 0.)
        nptest.assert_almost_equal(r, self.datum.geod.a, decimal=5)

        r = self.datum.GeocentricDistance(0., 90.)
        nptest.assert_almost_equal(r, self.datum.geod.b, decimal=5)

    def test_ParallelArcDist(self):
        dist = self.datum.ParallelArcDist(0., 0., 360.)
        nptest.assert_almost_equal(dist, self.datum.geod.a * 2 * np.pi)

        lat, lon1, lon2 = 45., -5., 5.
        __, __, great_circle_dist = self.datum.geod.inv(lon1, lat, lon2, lat)
        parallel_dist = self.datum.ParallelArcDist(lat, lon1, lon2)

        assert great_circle_dist < parallel_dist, \
            (great_circle_dist, parallel_dist)
Example #4
0
    def __init__(self, lon, lat, gpis=None, geodatum='WGS84', subset=None,
                 setup_kdTree=True, shape=None):
        """
        init method, prepares lon and lat arrays for _transform_lonlats if
        necessary

        """
        lon = np.asanyarray(lon)
        lat = np.asanyarray(lat)
        if gpis is not None:
            gpis = np.asanyarray(gpis)
        if subset is not None:
            subset = np.asanyarray(subset)

        if lat.shape != lon.shape:
            raise GridDefinitionError(
                "lat and lon np.arrays have to have equal shapes")

        self.n_gpi = len(lon)

        self.arrlon = lon
        self.arrlat = lat

        self.shape = None

        if shape is not None and len(shape) == 2:
            if len(self.arrlat) % shape[0] != 0:
                msg = ("Given shape does not have the correct first dimension."
                       " Length of lat array is not divisible by shape[0] "
                       " without rest")
                raise GridDefinitionError(msg)

            if len(self.arrlon) % shape[1] != 0:
                msg = ("Given shape does not have the correct second "
                       "dimension. Length of lon array is not divisible by "
                       "shape[1] without rest")
                raise GridDefinitionError(msg)

            self.shape = shape
            self.lat2d = np.reshape(self.arrlat, self.shape)
            self.lon2d = np.reshape(self.arrlon, self.shape)

        else:
            self.shape = tuple([len(self.arrlon)])

        self.geodatum = GeodeticDatum(geodatum)

        if gpis is None:
            self.gpis = np.arange(self.n_gpi, dtype=int)
            self.gpidirect = True
        else:
            if lat.shape != gpis.shape:
                raise GridDefinitionError("lat, lon gpi np.arrays have to "
                                          "have equal shapes")
            self.gpis = gpis
            self.gpidirect = False

        self.subset = subset

        if subset is not None:
            self.activearrlon = self.arrlon[subset]
            self.activearrlat = self.arrlat[subset]
            self.activegpis = self.gpis[subset]
            self.allpoints = False
        else:
            self.activearrlon = self.arrlon
            self.activearrlat = self.arrlat
            self.activegpis = self.gpis
            self.allpoints = True

        self.issplit = False

        self.kdTree = None
        if setup_kdTree:
            self._setup_kdtree()
Example #5
0
 def setUp(self):
     self.datum = GeodeticDatum('WGS84')
Example #6
0
    def __init__(self, lon, lat, gpis=None, geodatum='WGS84', subset=None,
                 setup_kdTree=True, shape=None, transform_lon=None):
        """
        init method, prepares lon and lat arrays for _transform_lonlats if
        necessary

        """
        lon = np.asanyarray(lon)
        lat = np.asanyarray(lat)
        if gpis is not None:
            gpis = np.asanyarray(gpis)
        if subset is not None:
            subset = np.asanyarray(subset)

        if lat.shape != lon.shape:
            raise GridDefinitionError(
                "lat and lon np.arrays have to have equal shapes")

        self.n_gpi = len(lon)

        # transfrom longitudes to be between -180 and 180 if they are between 0
        # and 360
        if transform_lon or transform_lon is None:
            if np.any(lon > 180):
                lon[lon > 180] -= 360
                if transform_lon is None:
                    warnings.warn(
                        "Longitude values have been transformed to be in"
                        " (-180, 180]. If this was not intended or to suppress"
                        " this warning set the transform_lon keyword argument"
                    )

        self.arrlon = lon
        self.arrlat = lat

        self.shape = None

        if shape is not None and len(shape) == 2:
            if len(self.arrlat) % shape[0] != 0:
                msg = ("Given shape does not have the correct first dimension."
                       " Length of lat array is not divisible by shape[0] "
                       " without rest")
                raise GridDefinitionError(msg)

            if len(self.arrlon) % shape[1] != 0:
                msg = ("Given shape does not have the correct second "
                       "dimension. Length of lon array is not divisible by "
                       "shape[1] without rest")
                raise GridDefinitionError(msg)

            self.shape = shape
            self.lat2d = np.reshape(self.arrlat, self.shape)
            self.lon2d = np.reshape(self.arrlon, self.shape)

        else:
            self.shape = tuple([len(self.arrlon)])

        self.geodatum = GeodeticDatum(geodatum)

        if gpis is None:
            self.gpis = np.arange(self.n_gpi, dtype=int)
            self.gpidirect = True
        else:
            if lat.shape != gpis.shape:
                raise GridDefinitionError("lat, lon gpi np.arrays have to "
                                          "have equal shapes")
            self.gpis = gpis
            self.gpidirect = False

        self.subset = subset

        if subset is not None:
            self.activearrlon = self.arrlon[subset]
            self.activearrlat = self.arrlat[subset]
            self.activegpis = self.gpis[subset]
            self.allpoints = False
        else:
            self.activearrlon = self.arrlon
            self.activearrlat = self.arrlat
            self.activegpis = self.gpis
            self.allpoints = True

        self.issplit = False

        self.kdTree = None
        if setup_kdTree:
            self._setup_kdtree()