Esempio n. 1
0
 def test_geoid(self):
     # Test the undulation getter
     c = ECEF(
         GeodeticRepresentation(latitude=45.5 * u.deg,
                                longitude=3.5 * u.deg))
     z = tools.topography.geoid_undulation(c)
     self.assertEqual(z.size, 1)
     self.assertEqual(z.unit, u.m)
Esempio n. 2
0
    def test_topography_elevation(self):
        # Fetch a test tile
        c = ECEF(
            GeodeticRepresentation(latitude=39.5 * u.deg,
                                   longitude=90.5 * u.deg))
        topography.update_data(c)

        # Test the topography getter
        z = topography.elevation(c)
        self.assertEqual(z.size, 1)
        self.assertEqual(z.unit, u.m)
        self.assertFalse(numpy.isnan(z))
Esempio n. 3
0
    def test_topography_cache(self):
        # Check the cache config
        self.assertEqual(topography.model(), "SRTMGL1")
        self.assertRegex(str(topography.cachedir()),
                         "^.*/grand/tools/data/topography")

        # Clear the cache
        topography.update_data(clear=True)
        self.assertFalse([p for p in topography.cachedir().glob("**/*")])

        # Fetch data
        c = ECEF(
            GeodeticRepresentation(latitude=39.5 * u.deg,
                                   longitude=90.5 * u.deg))
        topography.update_data(c)
        self.assertTrue(
            (topography.cachedir() / "N39E090.SRTMGL1.hgt").exists())

        c = ECEF(
            GeodeticRepresentation(latitude=u.Quantity((39.5, 40.5)) * u.deg,
                                   longitude=u.Quantity((90.5, 90.5)) * u.deg))
        topography.update_data(c)
        self.assertTrue(
            (topography.cachedir() / "N40E090.SRTMGL1.hgt").exists())

        c = ECEF(
            GeodeticRepresentation(latitude=40 * u.deg,
                                   longitude=90.5 * u.deg))
        topography.update_data(c, radius=100 * u.km)
        self.assertTrue(
            (topography.cachedir() / "N39E089.SRTMGL1.hgt").exists())
        self.assertTrue(
            (topography.cachedir() / "N39E091.SRTMGL1.hgt").exists())
        self.assertTrue(
            (topography.cachedir() / "N40E089.SRTMGL1.hgt").exists())
        self.assertTrue(
            (topography.cachedir() / "N40E091.SRTMGL1.hgt").exists())
Esempio n. 4
0
    def test_default(self):
        # Test the initialisation
        self.assertEqual(tools.geomagnet.model, tools.geomagnet._default_model)
        # self.assertEqual(tools.geomagnet.obstime,
        #                 tools.geomagnet._default_obstime.datetime.date)
        self.assertEqual(tools.geomagnet.obstime,
                         tools.geomagnet._default_obstime)

        # Test the default field getter
        c = self.get_coordinates()
        field = tools.geomagnet.field(c)
        self.assertEqual(field.x.size, 1)
        # self.assertEqual(field.x.unit, u.T)
        # self.assertEqual(field.y.unit, u.T)
        # self.assertEqual(field.z.unit, u.T)
        # self.assertEqual(c.obstime, field.obstime)
        self.assertEqual(c.obstime, tools.geomagnet.obstime)
        self.assertField(field)

        # Test the vectorized getter
        n = 10
        c = self.get_coordinates(n)
        field = tools.geomagnet.field(c)
        self.assertEqual(field.x.size, n)
        self.assertEqual(c.obstime, tools.geomagnet.obstime)
        for i in range(len(field.x)):
            value = field[:, i]
            value = CartesianRepresentation(x=value[0], y=value[1], z=value[2])
            self.assertField(value)
        # for value in field:
        #    #self.assertEqual(field.x.unit, u.T)
        #    #self.assertEqual(field.y.unit, u.T)
        #    #self.assertEqual(field.z.unit, u.T)
        # RK: Not sure what is done here.
        #    frame = LTP(location=self.location, orientation='ENU',
        #                magnetic=False)
        #    ltp = value.transform_to(frame)
        #    self.assertField(ltp)

        # Test the getter from ITRS
        # itrs = ITRS(self.location.itrs.cartesian, obstime=self.date)
        # field = tools.geomagnet.field(itrs)
        ecef = ECEF(self.location)
        field = tools.geomagnet.field(ecef)
        self.assertEqual(field.x.size, 1)
        # self.assertEqual(c.obstime, field.obstime)
        self.assertEqual(c.obstime, tools.geomagnet.obstime)
        self.assertField(field)
Esempio n. 5
0
    def test_custom_topography(self):
        # Fetch a test tile
        dirname, basename = Path("tests/topography"), "N39E090.SRTMGL1.hgt"
        path = dirname / basename
        if not path.exists():
            dirname.mkdir(exist_ok=True)
            with path.open("wb") as f:
                f.write(store.get(basename))

        # Test the topography getter
        topo = Topography(dirname)
        c = ECEF(
            GeodeticRepresentation(latitude=39.5 * u.deg,
                                   longitude=90.5 * u.deg))
        z = topo.elevation(c)
        self.assertEqual(z.size, 1)
        self.assertEqual(z.unit, u.m)
        self.assertFalse(numpy.isnan(z))
Esempio n. 6
0
    def test_ecef(self):
        # Check the forward transform
        ecef = ECEF(self.location.itrs.cartesian, obstime=self.obstime)
        itrs = ecef.transform_to(ITRS(obstime=self.obstime))
        self.assertCartesian(ecef, itrs, u.m, 8)

        # Check the backward transform
        ecef0 = itrs.transform_to(ECEF(obstime=self.obstime))
        self.assertCartesian(ecef, ecef0, u.m, 8)

        # Check the obstime handling
        ecef1 = itrs.transform_to(ECEF)
        self.assertEqual(ecef1.obstime, itrs.obstime)
        self.assertCartesian(ecef1, ecef0, u.m, 8)

        # Check the round trip with different obstimes
        itrs = ecef.transform_to(ITRS)
        ecef0 = itrs.transform_to(ECEF(obstime=self.obstime))
        self.assertCartesian(ecef, ecef0, u.m, 2)

        # Check the Earth location conversion
        location = ecef0.earth_location.itrs.cartesian
        self.assertCartesian(ecef, location, u.m, 2)
Esempio n. 7
0
    def test_ltp(self):
        ecef = ECEF(self.location.itrs.cartesian, obstime=self.obstime)

        # Check the constructor & to ECEF transform
        ltp = LTP(x=0 * u.m, y=0 * u.m, z=0 * u.m, location=self.location)
        r = ltp.transform_to(ECEF)

        self.assertEqual(r.obstime, ltp.obstime)
        self.assertQuantity(r.x, ecef.x, u.m, 6)
        self.assertQuantity(r.y, ecef.y, u.m, 6)
        self.assertQuantity(r.z, ecef.z, u.m, 6)

        # Check the from ECEF transform
        ltp = ecef.transform_to(LTP(location=self.location))

        self.assertEqual(ltp.obstime, self.obstime)
        self.assertQuantity(ltp.x, 0, u.m, 6)
        self.assertQuantity(ltp.y, 0, u.m, 6)
        self.assertQuantity(ltp.z, 0, u.m, 6)

        # Check the Earth location conversion
        location = ltp.earth_location.itrs.cartesian
        self.assertCartesian(ecef, location, u.m, 2)

        # Check the affine transform
        points = ((0, 0, 1), (1, 0, 0), (0, 1, 0), (1, 1, 0), (1, 1, 1), (0, 1,
                                                                          1))
        for point in (points):
            cart = CartesianRepresentation(x=point[1],
                                           y=point[0],
                                           z=point[2],
                                           unit=u.m)
            altaz = SkyCoord(cart,
                             frame="altaz",
                             location=self.location,
                             obstime=self.obstime)
            ecef0 = altaz.transform_to(ECEF(obstime=self.obstime))

            cart = CartesianRepresentation(x=point[0],
                                           y=point[1],
                                           z=point[2],
                                           unit=u.m)
            ltp = LTP(cart, location=self.location, obstime=self.obstime)
            ecef1 = ltp.transform_to(ECEF)

            self.assertEqual(ecef0.obstime, ecef1.obstime)
            self.assertQuantity(ecef0.x, ecef1.x, u.m, 4)
            self.assertQuantity(ecef0.y, ecef1.y, u.m, 4)
            self.assertQuantity(ecef0.z, ecef1.z, u.m, 4)

        # Check the orientation
        point = (1, -1, 2)
        cart = CartesianRepresentation(x=point[0],
                                       y=point[1],
                                       z=point[2],
                                       unit=u.m)
        altaz = SkyCoord(cart,
                         frame="altaz",
                         location=self.location,
                         obstime=self.obstime)
        ecef0 = altaz.transform_to(ECEF(obstime=self.obstime))

        for (orientation, sign) in ((("N", "E", "U"),
                                     (1, 1, 1)), (("N", "E", "D"), (1, 1, -1)),
                                    (("S", "E", "U"), (-1, 1, 1)),
                                    (("N", "W", "U"), (1, -1, 1))):
            cart = CartesianRepresentation(x=sign[0] * point[0],
                                           y=sign[1] * point[1],
                                           z=sign[2] * point[2],
                                           unit=u.m)
            ltp = LTP(cart,
                      location=self.location,
                      obstime=self.obstime,
                      orientation=orientation)
            ecef1 = ltp.transform_to(ECEF(obstime=self.obstime))

            self.assertQuantity(ecef0.x, ecef1.x, u.m, 4)
            self.assertQuantity(ecef0.y, ecef1.y, u.m, 4)
            self.assertQuantity(ecef0.z, ecef1.z, u.m, 4)

        # Check the unit vector case
        uy = HorizontalRepresentation(azimuth=0 * u.deg, elevation=0 * u.deg)
        ltp = LTP(uy, location=self.location, obstime=self.obstime)

        self.assertQuantity(ltp.x, 0, u.one, 9)
        self.assertQuantity(ltp.y, 1, u.one, 9)
        self.assertQuantity(ltp.z, 0, u.one, 9)

        r = ltp.transform_to(ECEF)

        self.assertEqual(r.obstime, ltp.obstime)
        self.assertQuantity(r.cartesian.norm(), 1, u.one, 6)

        ecef = ECEF(uy, obstime=self.obstime)
        ltp = ecef.transform_to(LTP(location=self.location))

        self.assertEqual(ltp.obstime, ecef.obstime)
        self.assertQuantity(ltp.cartesian.norm(), 1, u.one, 6)

        # Check the magnetic north case
        ltp0 = LTP(uy, location=self.location, obstime=self.obstime)
        frame1 = LTP(location=self.location,
                     obstime=self.obstime,
                     magnetic=True)
        ltp1 = ltp0.transform_to(frame1)
        self.assertEqual(ltp0.obstime, ltp1.obstime)

        declination = numpy.arcsin(ltp0.cartesian.cross(ltp1.cartesian).norm())
        self.assertQuantity(declination.to(u.deg), 0.10 * u.deg, u.deg, 2)

        # Test the magnetic case with no obstime
        with self.assertRaises(ValueError) as context:
            LTP(uy, location=self.location, magnetic=True)
        self.assertRegex(context.exception.args[0], "^Magnetic")

        # Test the invalid frame case
        with self.assertRaises(ValueError) as context:
            LTP(uy, location=self.location, orientation=("T", "O", "T", "O"))
        self.assertRegex(context.exception.args[0], "^Invalid frame")

        # Test the ltp round trip with a position
        frame0 = LTP(location=self.location)
        frame1 = LTP(location=self.location,
                     obstime=self.obstime,
                     magnetic=True)
        ltp0 = LTP(x=1 * u.m,
                   y=2 * u.m,
                   z=3 * u.m,
                   location=self.location,
                   obstime=self.obstime)
        ltp1 = ltp0.transform_to(frame1).transform_to(frame0)
        self.assertCartesian(ltp0, ltp1, u.m, 8)

        # Test the same frame case
        ltp1 = ltp0.transform_to(frame0)
        self.assertCartesian(ltp0, ltp1, u.m, 8)
        self.assertEqual(ltp0.obstime, ltp1.obstime)

        # Test an LTP permutation
        ltp0 = LTP(x=1 * u.m, y=2 * u.m, z=3 * u.m, location=self.location)
        frame1 = LTP(location=self.location, orientation=("N", "E", "D"))
        ltp1 = ltp0.transform_to(frame1)
        self.assertQuantity(ltp0.x, ltp1.y, u.m, 6)
        self.assertQuantity(ltp0.y, ltp1.x, u.m, 6)
        self.assertQuantity(ltp0.z, -ltp1.z, u.m, 6)