def test_numpy_data_2d(self):
        DIM = 2
        testdata = np.arange(20 * 21).reshape(20, 21)
        error = 1. * np.ones(testdata.shape)
        source = NumpyData(DataSource.GRAVITY, testdata, null_value=NUMPY_NULL)
        X0, NP, DX = source.getDataExtents()
        for i in range(DIM):
            self.assertAlmostEqual(X0[i], 0., msg="Data origin wrong")
            self.assertEqual(NP[i],
                             testdata.shape[DIM - i - 1],
                             msg="Wrong number of data points")
            self.assertAlmostEqual(DX[i],
                                   1000. / testdata.shape[DIM - i - 1],
                                   msg="Wrong cell size")

        domainbuilder = DomainBuilder(dim=3)
        domainbuilder.addSource(source)
        domainbuilder.setVerticalExtents(depth=-VMIN,
                                         air_layer=VMAX,
                                         num_cells=NE_V)
        domainbuilder.setElementPadding(PAD_X, PAD_Y)
        dom = domainbuilder.getDomain()
        g, s = domainbuilder.getGravitySurveys()[0]

        outfn = os.path.join(WORKDIR, '_npdata2d.csv')
        saveDataCSV(outfn, g=g, s=s)

        DV = (VMAX - VMIN) / NE_V

        # check data
        nx = NP[0] + 2 * PAD_X
        ny = NP[1] + 2 * PAD_Y
        nz = NE_V
        z_data = int(np.round((ALT - VMIN) / DV) - 1)

        out = np.genfromtxt(outfn,
                            delimiter=',',
                            skip_header=1,
                            dtype=np.float64)
        # recompute nz since ripley might have adjusted number of elements
        nz = len(out) // (nx * ny)
        g_out = out[:, 0].reshape(nz, ny, nx)
        s_out = out[:, 1].reshape(nz, ny, nx)
        self.assertAlmostEqual(
            np.abs(g_out[z_data, PAD_Y:PAD_Y + NP[1], PAD_X:PAD_X + NP[0]] -
                   testdata).max(),
            0.,
            msg="Difference in gravity data area")

        self.assertAlmostEqual(
            np.abs(s_out[z_data, PAD_Y:PAD_Y + NP[1], PAD_X:PAD_X + NP[0]] -
                   error).max(),
            0.,
            msg="Difference in error data area")

        # overwrite data -> should only be padding value left
        g_out[z_data, PAD_Y:PAD_Y + NP[1], PAD_X:PAD_X + NP[0]] = NUMPY_NULL
        self.assertAlmostEqual(np.abs(g_out - NUMPY_NULL).max(),
                               0.,
                               msg="Wrong values in padding area")
 def test_add_source_after_domain_built(self):
     db = DomainBuilder()
     source1a = NetCdfData(DataSource.GRAVITY, NC_DATA1, scale_factor=1.)
     db.addSource(source1a)
     _ = db.getDomain()
     source1b = NetCdfData(DataSource.GRAVITY, NC_DATA1, scale_factor=2.)
     self.assertRaises(Exception, db.addSource, source1b)
Esempio n. 3
0
    def test_cdf_with_padding(self):
        source = NetCdfData(DataSource.GRAVITY, NC_DATA, ALT, scale_factor=1e-6)
        domainbuilder=DomainBuilder()
        domainbuilder.addSource(source)
        domainbuilder.setVerticalExtents(depth=-VMIN, air_layer=VMAX, num_cells=NE_V)
        domainbuilder.setElementPadding(PAD_X,PAD_Y)
        dom=domainbuilder.getDomain()
        g,s=domainbuilder.getGravitySurveys()[0]

        outfn=os.path.join(WORKDIR, '_ncdata.csv')
        saveDataCSV(outfn, g=g, s=s)

        X0,NP,DX=source.getDataExtents()
        DV=(VMAX-VMIN)/NE_V

        # check metadata
        self.assertEqual(NP, NC_SIZE, msg="Wrong number of data points")
        # this only works if gdal is available

        try:
            import osgeo.osr
            for i in range(len(NC_ORIGIN)):
                self.assertAlmostEqual(X0[i], NC_ORIGIN[i], msg="Data origin wrong")
        except ImportError:
            print("Skipping test of data origin since gdal is not installed.")

        # check data
        nx=NP[0]+2*PAD_X
        ny=NP[1]+2*PAD_Y
        nz=NE_V
        z_data=int(np.round((ALT-VMIN)/DV)-1)
    
        ref=np.genfromtxt(NC_REF, delimiter=',', dtype=np.float64)
        g_ref=ref[:,0].reshape((NP[1],NP[0]))
        s_ref=ref[:,1].reshape((NP[1],NP[0]))

        out=np.genfromtxt(outfn, delimiter=',', skip_header=1, dtype=np.float64)
        # recompute nz since ripley might have adjusted number of elements

        nz=len(out)//(nx*ny)
        g_out=out[:,0].reshape(nz,ny,nx)
        s_out=out[:,1].reshape(nz,ny,nx)

        self.assertAlmostEqual(np.abs(
            g_out[z_data, PAD_Y:PAD_Y+NP[1], PAD_X:PAD_X+NP[0]]-g_ref).max(),
            0., msg="Difference in gravity data area")

        self.assertAlmostEqual(np.abs(
            s_out[z_data, PAD_Y:PAD_Y+NP[1], PAD_X:PAD_X+NP[0]]-s_ref).max(),
            0., msg="Difference in error data area")

        # overwrite data -> should only be padding value left
        g_out[z_data, PAD_Y:PAD_Y+NP[1], PAD_X:PAD_X+NP[0]]=NC_NULL
        self.assertAlmostEqual(np.abs(g_out-NC_NULL).max(), 0.,
                msg="Wrong values in padding area")
Esempio n. 4
0
    def test_numpy_data_2d(self):
        DIM=2
        testdata = np.arange(20*21).reshape(20,21)
        error = 1.*np.ones(testdata.shape)
        source = NumpyData(DataSource.GRAVITY, testdata, null_value=NUMPY_NULL)
        X0,NP,DX=source.getDataExtents()
        for i in range(DIM):
            self.assertAlmostEqual(X0[i], 0., msg="Data origin wrong")
            self.assertEqual(NP[i], testdata.shape[DIM-i-1], msg="Wrong number of data points")
            self.assertAlmostEqual(DX[i], 1000./testdata.shape[DIM-i-1], msg="Wrong cell size")

        domainbuilder=DomainBuilder(dim=3)
        domainbuilder.addSource(source)
        domainbuilder.setVerticalExtents(depth=-VMIN, air_layer=VMAX, num_cells=NE_V)
        domainbuilder.setElementPadding(PAD_X, PAD_Y)
        dom=domainbuilder.getDomain()
        g,s=domainbuilder.getGravitySurveys()[0]

        outfn=os.path.join(WORKDIR, '_npdata2d.csv')
        saveDataCSV(outfn, g=g, s=s)

        DV=(VMAX-VMIN)/NE_V

        # check data
        nx=NP[0]+2*PAD_X
        ny=NP[1]+2*PAD_Y
        nz=NE_V
        z_data=int(np.round((ALT-VMIN)/DV)-1)

        out=np.genfromtxt(outfn, delimiter=',', skip_header=1, dtype=np.float64)
        # recompute nz since ripley might have adjusted number of elements
        nz=len(out)//(nx*ny)
        g_out=out[:,0].reshape(nz,ny,nx)
        s_out=out[:,1].reshape(nz,ny,nx)
        self.assertAlmostEqual(np.abs(
            g_out[z_data, PAD_Y:PAD_Y+NP[1], PAD_X:PAD_X+NP[0]]-testdata).max(),
            0., msg="Difference in gravity data area")

        self.assertAlmostEqual(np.abs(
            s_out[z_data, PAD_Y:PAD_Y+NP[1], PAD_X:PAD_X+NP[0]]-error).max(),
            0., msg="Difference in error data area")

        # overwrite data -> should only be padding value left
        g_out[z_data, PAD_Y:PAD_Y+NP[1], PAD_X:PAD_X+NP[0]]=NUMPY_NULL
        self.assertAlmostEqual(np.abs(g_out-NUMPY_NULL).max(), 0.,
                msg="Wrong values in padding area")
 def test_geodetic_domain(self):
     COORDINATES = WGS84ReferenceSystem()
     db = DomainBuilder(reference_system=COORDINATES)
     source1a = NetCdfData(DataSource.GRAVITY,
                           NC_DATA1,
                           scale_factor=1.,
                           reference_system=COORDINATES)
     db.addSource(source1a)
     db.setVerticalExtents(depth=20000., air_layer=30000., num_cells=10)
     dom = db.getDomain()
     x = dom.getX()
     self.assertAlmostEqual(inf(x[0]),
                            120.2,
                            delta=0.001,
                            msg="phi range wrong")
     self.assertAlmostEqual(inf(x[1]),
                            -29.2,
                            delta=0.0001,
                            msg="lambda range wrong")
     self.assertAlmostEqual(inf(x[2]),
                            -0.2,
                            msg="h range wrong" + str(x[2]))
     # Cannot check upper bounds of coordinates with more than 1 rank
     # because ripley may adjust internally.
     if getMPISizeWorld() == 1:
         self.assertAlmostEqual(sup(x[0]),
                                120.3,
                                delta=0.001,
                                msg="phi range wrong")
         self.assertAlmostEqual(sup(x[1]),
                                -29.1,
                                delta=0.0001,
                                msg="lambda range wrong")
         self.assertAlmostEqual(sup(x[2]),
                                0.3,
                                msg="h range wrong: " + str(x[2]))
 def test_cartesian_domain(self):
     db = DomainBuilder()
     source1a = NetCdfData(DataSource.GRAVITY, NC_DATA1, scale_factor=1.)
     db.addSource(source1a)
     db.setVerticalExtents(depth=20000., air_layer=30000., num_cells=10)
     dom = db.getDomain()
 def test_mixing_utm_zones(self):
     source1 = NetCdfData(DataSource.GRAVITY, NC_DATA1, scale_factor=1.)
     source2 = NetCdfData(DataSource.GRAVITY, NC_DATA2, scale_factor=1.)
     domainbuilder = DomainBuilder()
     domainbuilder.addSource(source1)
     self.assertRaises(ValueError, domainbuilder.addSource, source2)
 def test_add_garbage(self):
     db = DomainBuilder()
     self.assertRaises(TypeError, db.addSource, 42)
    def test_cdf_with_padding_ellipsoid(self):
        ref = WGS84ReferenceSystem()

        source = NetCdfData(DataSource.GRAVITY,
                            NC_DATA,
                            ALT,
                            reference_system=ref,
                            scale_factor=1e-6)
        domainbuilder = DomainBuilder(reference_system=ref)
        domainbuilder.addSource(source)
        domainbuilder.setVerticalExtents(depth=-VMIN,
                                         air_layer=VMAX,
                                         num_cells=NE_V)
        domainbuilder.setElementPadding(PAD_X, PAD_Y)
        dom = domainbuilder.getDomain()
        g, s = domainbuilder.getGravitySurveys()[0]

        outfn = os.path.join(WORKDIR, '_ncdata.csv')
        saveDataCSV(outfn, g=g, s=s)

        X0, NP, DX = source.getDataExtents()
        DV = (VMAX - VMIN) / NE_V

        # check metadata
        self.assertEqual(NP, NC_SIZE, msg="Wrong number of data points")

        for i in range(len(NC_ORIGIN)):
            self.assertAlmostEqual(X0[i],
                                   NC_ORIGIN_WGS84[i],
                                   msg="Data origin wrong")

        # check data
        nx = NP[0] + 2 * PAD_X
        ny = NP[1] + 2 * PAD_Y
        nz = NE_V
        z_data = int(np.round((ALT - VMIN) / DV) - 1)

        ref = np.genfromtxt(NC_REF, delimiter=',', dtype=np.float64)
        g_ref = ref[:, 0].reshape((NP[1], NP[0]))
        s_ref = ref[:, 1].reshape((NP[1], NP[0]))

        out = np.genfromtxt(outfn,
                            delimiter=',',
                            skip_header=1,
                            dtype=np.float64)

        # recompute nz since ripley might have adjusted number of elements
        nz = len(out) // (nx * ny)
        g_out = out[:, 0].reshape(nz, ny, nx)
        s_out = out[:, 1].reshape(nz, ny, nx)

        self.assertAlmostEqual(
            np.abs(g_out[z_data, PAD_Y:PAD_Y + NP[1], PAD_X:PAD_X + NP[0]] -
                   g_ref).max(),
            0.,
            msg="Difference in gravity data area")

        self.assertAlmostEqual(
            np.abs(s_out[z_data, PAD_Y:PAD_Y + NP[1], PAD_X:PAD_X + NP[0]] -
                   s_ref).max(),
            0.,
            msg="Difference in error data area")

        # overwrite data -> should only be padding value left
        g_out[z_data, PAD_Y:PAD_Y + NP[1], PAD_X:PAD_X + NP[0]] = NC_NULL
        self.assertAlmostEqual(np.abs(g_out - NC_NULL).max(),
                               0.,
                               msg="Wrong values in padding area")
    def _ers_tester(self, filename):
        source = ErMapperData(DataSource.GRAVITY,
                              headerfile=filename,
                              altitude=ALT,
                              scale_factor=1e-6)
        domainbuilder = DomainBuilder()
        domainbuilder.addSource(source)
        domainbuilder.setVerticalExtents(depth=-VMIN,
                                         air_layer=VMAX,
                                         num_cells=NE_V)
        domainbuilder.setElementPadding(PAD_X, PAD_Y)
        dom = domainbuilder.getDomain()
        g, s = domainbuilder.getGravitySurveys()[0]

        outfn = os.path.join(WORKDIR, '_ersdata.csv')
        saveDataCSV(outfn, g=g, s=s)

        X0, NP, DX = source.getDataExtents()
        DV = (VMAX - VMIN) / NE_V

        # check metadata
        self.assertEqual(NP, ERS_SIZE, msg="Wrong number of data points")
        # this test only works if gdal is available
        try:
            import osgeo.osr
            for i in range(len(ERS_ORIGIN)):
                self.assertAlmostEqual(X0[i],
                                       ERS_ORIGIN[i],
                                       msg="Data origin wrong")
        except ImportError:
            print("Skipping test of data origin since gdal is not installed.")

        # check data
        nx = NP[0] + 2 * PAD_X
        ny = NP[1] + 2 * PAD_Y
        nz = NE_V
        z_data = int(np.round((ALT - VMIN) / DV) - 1)

        ref = np.genfromtxt(ERS_REF, delimiter=',', dtype=np.float64)
        g_ref = ref[:, 0].reshape((NP[1], NP[0]))
        s_ref = ref[:, 1].reshape((NP[1], NP[0]))

        out = np.genfromtxt(outfn,
                            delimiter=',',
                            skip_header=1,
                            dtype=np.float64)
        # recompute nz since ripley might have adjusted number of elements
        nz = len(out) // (nx * ny)
        g_out = out[:, 0].reshape(nz, ny, nx)
        s_out = out[:, 1].reshape(nz, ny, nx)
        self.assertAlmostEqual(
            np.abs(g_out[z_data, PAD_Y:PAD_Y + NP[1], PAD_X:PAD_X + NP[0]] -
                   g_ref).max(),
            0.,
            msg="Difference in gravity data area")

        self.assertAlmostEqual(
            np.abs(s_out[z_data, PAD_Y:PAD_Y + NP[1], PAD_X:PAD_X + NP[0]] -
                   s_ref).max(),
            0.,
            msg="Difference in error data area")

        # overwrite data -> should only be padding value left
        g_out[z_data, PAD_Y:PAD_Y + NP[1], PAD_X:PAD_X + NP[0]] = ERS_NULL
        self.assertAlmostEqual(np.abs(g_out - ERS_NULL).max(),
                               0.,
                               msg="Wrong values in padding area")