Beispiel #1
0
    def test_read(self):
        """
        Should read a rayfan group file from the disk
        """
        rays = rayfan.readRayfanGroup(get_example_file('test1.rays'))

        self.assertEqual(len(rays.rayfans), 2)
Beispiel #2
0
    def test_read_vm(self):
        """
        Should read the native VM format 
        """
        example_file = "benchmark2d.vm"
        # make sure example can be found
        path = get_example_file(example_file)
        self.assertTrue(os.path.isfile(path))

        # should load the example file
        vm = VM(example_file, head_only=False)
        self.assertTrue(hasattr(vm, "grid"))
        self.assertEqual(vm.grid.nx, 1041)
        self.assertEqual(vm.grid.ny, 1)
        self.assertEqual(vm.grid.nz, 506)
        self.assertEqual(vm.sl.shape[0], 1041)
        self.assertEqual(vm.sl.shape[1], 1)
        self.assertEqual(vm.sl.shape[2], 506)

        self.assertAlmostEqual(1.0 / vm.grid.values[0, 0, 0], 0.333, 3)

        self.assertEqual(vm.rf.shape, (5, vm.grid.nx, vm.grid.ny))
        self.assertEqual(vm.jp.shape, (5, vm.grid.nx, vm.grid.ny))
        self.assertEqual(vm.ir.shape, (5, vm.grid.nx, vm.grid.ny))
        self.assertEqual(vm.ij.shape, (5, vm.grid.nx, vm.grid.ny))
Beispiel #3
0
    def test_read_vm(self):
        """
        Should read the native VM format 
        """
        example_file = 'benchmark2d.vm'
        # make sure example can be found
        path = get_example_file(example_file)
        self.assertTrue(os.path.isfile(path))

        # should load the example file
        vm = VM(example_file, head_only=False)
        self.assertTrue(hasattr(vm, 'grid'))
        self.assertEqual(vm.grid.nx, 1041)
        self.assertEqual(vm.grid.ny, 1)
        self.assertEqual(vm.grid.nz, 506)
        self.assertEqual(vm.sl.shape[0], 1041)
        self.assertEqual(vm.sl.shape[1], 1)
        self.assertEqual(vm.sl.shape[2], 506)

        self.assertAlmostEqual(1. / vm.grid.values[0, 0, 0], 0.333, 3)

        self.assertEqual(vm.rf.shape, (5, vm.grid.nx, vm.grid.ny))
        self.assertEqual(vm.jp.shape, (5, vm.grid.nx, vm.grid.ny))
        self.assertEqual(vm.ir.shape, (5, vm.grid.nx, vm.grid.ny))
        self.assertEqual(vm.ij.shape, (5, vm.grid.nx, vm.grid.ny))
Beispiel #4
0
    def test_write(self):
        """
        Should write a rayfan group file to the disk
        """
        tempfile = 'temp123.rays'
        if os.path.isfile(tempfile):
            os.remove(tempfile)

        # read example rayfan group
        rays = rayfan.readRayfanGroup(get_example_file('test1.rays'))

        # should write a copy to the disk
        rays.write(tempfile)
        self.assertTrue(os.path.isfile(tempfile))

        # should have correct data
        rays1 = rayfan.readRayfanGroup(tempfile)

        self.assertEqual(len(rays.rayfans), len(rays1.rayfans))

        for az0, az1 in zip(rays.azimuths, rays1.azimuths):
            self.assertEqual(az0, az1)

        for r0, r1 in zip(rays.offsets, rays1.offsets):
            self.assertEqual(r0, r1)

        for e0, e1 in zip(rays.residuals, rays1.residuals):
            self.assertEqual(e0, e1)

        for rfn0, rfn1 in zip(rays.rayfans, rays1.rayfans):

            # should have correct metadata
            self.assertEqual(rfn0.start_point_id, rfn1.start_point_id)

            self.assertEqual(rfn0.nrays, rfn1.nrays)

            # should have correct number of raypaths
            self.assertEqual(len(rfn0.paths), len(rfn1.paths))

            for i in range(len(rfn0.paths)):
                # should have correct path metadata
                self.assertEqual(rfn0.end_point_ids[i], rfn1.end_point_ids[i])
                self.assertEqual(rfn0.event_ids[i], rfn1.event_ids[i])
                self.assertEqual(rfn0.event_subids[i], rfn1.event_subids[i])
                self.assertEqual(rfn0.pick_times[i], rfn1.pick_times[i])
                self.assertEqual(rfn0.travel_times[i], rfn1.travel_times[i])
                self.assertEqual(rfn0.pick_errors[i], rfn1.pick_errors[i])

                for j in range(len(rfn0.paths[i])):
                    for k in range(3):
                        # should have correct path coordinates
                        self.assertEqual(rfn0.paths[i][j][k],
                                         rfn1.paths[i][j][k])

        if os.path.isfile(tempfile):
            os.remove(tempfile)
Beispiel #5
0
 def __init__(self, filename=None, **kwargs):
     """
     Class for managing a VM Tomography model
     """
     if filename is not None:
         if os.path.isfile(filename):
             self.read(filename, **kwargs)
         else:
             try:
                 _filename = get_example_file(
                     filename.replace('.vm', '') + '.vm')
                 self.read(_filename, **kwargs)
             except IOError:
                 raise IOError('No such file: {:}'.format(filename))
     else:
         shape = kwargs.pop('shape', (128, 1, 128))
         self._init_model(shape, **kwargs)
Beispiel #6
0
    def read(self, path_or_buf, fmt=None, **kwargs):
        """
        Read a VM model from a file

        Parameters
        ----------
        path_or_buf: string or file handle
            File path or object to read model from.
        fmt: string, optional
            Format of file to read. Default is to try to guess the format
            from the file extension or try to read in the native VM (*.vm)
            format. Supported formats must be accessible by _read_{fmt}
            private methods.
        kwargs:
            Keyword arguments that are passed to the read methods. See
            _read_{fmt} methods for advanced options.
        """
        if not hasattr(path_or_buf, 'read'):
            try:
                buf = open(path_or_buf, 'rb')
                close = True
            except IOError as err:
                try:
                    buf = open(get_example_file(path_or_buf), 'rb')
                    close = True
                except:
                    raise IOError(err)
        else:
            buf = path_or_buf

        if (fmt is None) and (not hasattr(path_or_buf, 'read')):
            try:
                _, fmt = os.path.splitext(path_or_buf)
                fmt = fmt[1:]
                method = self.__getattribute__('_read_{:}'.format(fmt))
            except AttributeError:
                fmt = 'vm'
                method = self.__getattribute__('_read_{:}'.format('vm'))
        else:
            method = self.__getattribute__('_read_{:}'.format(fmt))

        method(buf, **kwargs)

        if close:
            buf.close()
Beispiel #7
0
    def read(self, path_or_buf, fmt=None, **kwargs):
        """
        Read a VM model from a file

        Parameters
        ----------
        path_or_buf: string or file handle
            File path or object to read model from.
        fmt: string, optional
            Format of file to read. Default is to try to guess the format
            from the file extension or try to read in the native VM (*.vm)
            format. Supported formats must be accessible by _read_{fmt}
            private methods.
        kwargs:
            Keyword arguments that are passed to the read methods. See
            _read_{fmt} methods for advanced options.
        """
        if not hasattr(path_or_buf, "read"):
            try:
                buf = open(path_or_buf, "rb")
                close = True
            except IOError as err:
                try:
                    buf = open(get_example_file(path_or_buf), "rb")
                    close = True
                except:
                    raise IOError(err)
        else:
            buf = path_or_buf

        if (fmt is None) and (not hasattr(path_or_buf, "read")):
            try:
                _, fmt = os.path.splitext(path_or_buf)
                fmt = fmt[1:]
                method = self.__getattribute__("_read_{:}".format(fmt))
            except AttributeError:
                fmt = "vm"
                method = self.__getattribute__("_read_{:}".format("vm"))
        else:
            method = self.__getattribute__("_read_{:}".format(fmt))

        method(buf, **kwargs)

        if close:
            buf.close()