Пример #1
0
def GKtoUTM(ea, no=None, zone=32, gk=None, gkzone=None):
    """Transform any Gauss-Krueger to UTM autodetect GK zone from offset."""
    if gk is None and gkzone is None:
        if no is None:
            rr = ea[0][0]
        else:
            if isinstance(ea, list) or isinstance(ea, tuple):
                rr = ea[0]
            else:
                rr = ea

        gkzone = int(floor(rr * 1e-6))
        print(gkzone)

        if gkzone <= 0 or gkzone >= 5:
            print("cannot detect valid GK zone")

    pyproj = opt_import('pyproj', 'coordinate transformations')
    if pyproj is None:
        return None

    gk = pyproj.Proj(init="epsg:" + str(31464 + gkzone))
    wgs84 = pyproj.Proj(init="epsg:4326")  # pure ellipsoid to doubel transform
    utm = pyproj.Proj(proj='utm', zone=zone, ellps='WGS84')  # UTM
    if no is None:  # two-column matrix
        lon, lat = pyproj.transform(gk, wgs84, ea[0], ea[1])
    else:
        lon, lat = pyproj.transform(gk, wgs84, ea, no)

    return utm(lon, lat)
Пример #2
0
def computeInverseRootMatrix(CM, thrsh=0.001, verbose=False):
    """Compute inverse square root (C^{-0.5} of matrix."""
    spl = opt_import('scipy.linalg', 'scipy linear algebra')
    if spl is None:
        return None

    t = time.time()
    e_vals, e_vecs = spl.eigh(CM)
    if verbose:
        print('(C) Calculation time for eigenvalue decomposition:\n%s sec' %
              (time.time() - t))

    t = time.time()
    A = spl.inv(np.diag(np.sqrt(np.real(e_vals))))
    if verbose:
        print('(C) Calculation time for inv(diag(sqrt)):\n%s sec' %
              (time.time() - t))

    t = time.time()
    gemm = spl.get_blas_funcs("gemm", [e_vecs, A])
    B = gemm(1, e_vecs, A)
    if verbose:
        print('(C) Calculation time for dot 1:\n%s sec' % (time.time() - t))

    t = time.time()
    gemm2 = spl.get_blas_funcs("gemm", [B, np.transpose(e_vecs)])
    if verbose:
        print('gemm test:\n%s sec' % (time.time() - t))
    CM05 = gemm2(1, B, np.transpose(e_vecs))
    if verbose:
        print('(C) Calculation time for dot 2:\n%s sec' % (time.time() - t))

    if thrsh:
        nModel = len(CM)
        RCM05 = pg.RSparseMapMatrix(nModel, nModel)
        for i in range(nModel):
            for j in range(nModel):
                if np.abs(CM05[i][j]) > thrsh:
                    RCM05.setVal(i, j, CM05[i][j])
        return RCM05
    else:
        return CM05  # not making sense as constraints matrix
Пример #3
0
def computeR(J, C, alpha=0.5):
    r"""Return diagional of model resolution matrix.

    Calculates the formal model resolution matrix deterministically following:

    .. math::

        \mathbf{R_m} = (\mathbf{J}^T\mathbf{D}^T\mathbf{D}\mathbf{J} + \alpha
        \mathbf{C}^T\mathbf{C})^{-1}
        \mathbf{J}^T\mathbf{D}^T\mathbf{D}\mathbf{J}

    .. note::

        The current implementation assumes that :math:`\mathbf{D}` is the
        identitiy matrix, i.e. equal data weights.

    Parameters
    ----------
    J : array
        Jacobian matrix.
    C : array
        Constraint matrix.
    alpha : float
        Regularization strength :math:`\alpha`.
    """
    lin = opt_import("scipy.linalg", "calculate model resolution matrices")
    if lin:
        import scipy.sparse as sp
        from scipy.sparse import coo_matrix, csc_matrix
        from scipy.sparse.linalg import aslinearoperator, LinearOperator, lsqr

    JTJ = J.T.dot(J)
    CM_inv = C.T.dot(C)
    #    Jsharp = lin.solve(JTJ + alpha * CM_inv, J.T)
    #    R = np.diag(Jsharp.dot(J))
    RM = lin.solve(JTJ + alpha * CM_inv, JTJ)
    R = np.diag(RM)
    return R
Пример #4
0
 def load(self, polyfile):
     """Read polygon info from XML file (see example.xml)."""
     ET = opt_import("xml.etree.cElementTree", "read in XML files")
     self.doc = ET.parse(polyfile)
     self.parse()
Пример #5
0
# -*- coding: utf-8 -*-
"""TODO documentme!.

SORT ME.
"""
from pygimli.io import opt_import

json = opt_import("json", "read and write inversion settings")


class InversionSettings(dict):
    """Extends the built-in dict with methods for saving and loading a file."""
    def __init__(self, *args, **kwargs):
        """Constructor.

        Initialize the settings object by either using a file or sepcifying the
        settings directly. Works exatly like a dict(), only with extended
        capabilities like saving and loading to disk.
        """
        try:
            self.filename = kwargs.pop('filename')
        except KeyError:
            print('Creating new settings object.')
        else:
            print('Loading settings from: "{}".'.format(self.filename))
        finally:
            super(InversionSettings, self).__init__(*args, **kwargs)

        if hasattr(self, 'filename'):
            self.update(InversionSettings.load(self.filename))
Пример #6
0
"""
Methods to calculate the model resolution.
"""

import numpy as np

from pygimli.io import opt_import

lin = opt_import("scipy.linalg", "calculate model resolion matrices")
if lin:
    import scipy.sparse as sp
    from scipy.sparse import coo_matrix, csc_matrix
    from scipy.sparse.linalg import aslinearoperator, LinearOperator, lsqr


def computeR(J, C, alpha=0.5):
    r"""Return diagional of model resolution matrix.

    Calculates the formal model resolution matrix deterministically following:

    .. math::

        \mathbf{R_m} = (\mathbf{J}^T\mathbf{D}^T\mathbf{D}\mathbf{J} + \alpha
        \mathbf{C}^T\mathbf{C})^{-1}
        \mathbf{J}^T\mathbf{D}^T\mathbf{D}\mathbf{J}

    .. note::

        The current implementation assumes that :math:`\mathbf{D}` is the
        identitiy matrix, i.e. equal data weights.
Пример #7
0
def test(target=None,
         show=False,
         onlydoctests=False,
         coverage=False,
         htmlreport=False,
         abort=False,
         verbose=True):
    """Run docstring examples and additional tests.

    Examples
    --------
    >>> import pygimli as pg
    >>> # You can test everything with pg.test() or test an individual function:
    >>> pg.test("pg.utils.boxprint", verbose=False)
    >>> # The target argument can also be the function directly
    >>> from pygimli.utils import boxprint
    >>> pg.test(boxprint, verbose=False)

    Parameters
    ----------
    target : function or string, optional
        Function or method to test. By default everything is tested.
    show : boolean, optional
        Show matplotlib windows during test run. They will be closed
        automatically.
    onlydoctests : boolean, optional
        Run test files in ../tests as well.
    coverage : boolean, optional
        Create a coverage report. Requires the pytest-cov plugin.
    htmlreport : str, optional
        Filename for HTML report such as www.pygimli.org/build_tests.html.
        Requires pytest-html plugin.
    abort : boolean, optional
        Return correct exit code, e.g. abort documentation build when a test
        fails.
    """

    old_backend = plt.get_backend()
    if not show:
        plt.switch_backend("Agg")

    if target:
        if isinstance(target, str):
            # If target is a string, such as "pg.solver.solve"
            # the code below will overwrite target with the corresponding
            # imported function, so that doctest works.
            target = target.replace("pg.", "pygimli.")
            import importlib
            mod_name, func_name = target.rsplit('.', 1)
            mod = importlib.import_module(mod_name)
            target = getattr(mod, func_name)

        import doctest
        doctest.run_docstring_examples(target,
                                       globals(),
                                       verbose=verbose,
                                       optionflags=doctest.ELLIPSIS,
                                       name=target.__name__)
        return

    try:
        import pytest
    except ImportError:
        raise ImportError("pytest is required to run test suite. "
                          "Try 'sudo pip install pytest'.")

    cwd = join(realpath(__path__[0]), '..')

    excluded = [
        "gui", "physics/traveltime/example.py", "physics/em/fdemexample.py"
    ]

    if onlydoctests:
        excluded.append("testing")

    cmd = ([
        "-v", "-rsxX", "--color", "yes", "--doctest-modules", "--durations", 5,
        cwd
    ])
    for directory in excluded:
        cmd.extend(["--ignore", join(cwd, directory)])

    if coverage:
        pc = opt_import("pytest_cov", "create a code coverage report")
        if pc:
            cmd.extend(["--cov", "pygimli"])
            cmd.extend(["--cov-report", "term"])

    if htmlreport:
        ph = opt_import("pytest_html", "create a html report")
        if ph:
            cmd.extend(["--html", htmlreport])

    exitcode = pytest.main(cmd)
    plt.switch_backend(old_backend)
    plt.close('all')
    if abort:
        sys.exit(exitcode)
Пример #8
0
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""Collection of several utility functions."""

import sys
from math import floor, sqrt

import numpy as np

import pygimli as pg
from pygimli.io import opt_import

coo_matrix = opt_import("scipy.sparse.coo_matrix",
                        "convert sparse matrices to numpy/scipy format.")


def sparse2scipy(matrix):
    """Convert pygimli RSparseMatrix to scipy format.

    Parameters
    ----------
    matrix : pg.RSpareMatrix
        Matrix to convert.

    Returns
    -------
    coo_matrix
        `matrix` in scipy.sparse.coo_matrix format.

    """
    pass
Пример #9
0
def test(target=None,
         show=False,
         onlydoctests=False,
         coverage=False,
         htmlreport=False,
         abort=False):
    """Run docstring examples and additional tests.

    Examples
    --------
    >>> import pygimli as pg
    >>> from pygimli.utils import boxprint
    >>> pg.test(boxprint)

    Parameters
    ----------
    target : function, optional
        Function or method to test. By default everything is tested.
    show : boolean, optional
        Show matplotlib windows during test run. They will be closed
        automatically.
    onlydoctests : boolean, optional
        Run test files in ../tests as well.
    coverage : boolean, optional
        Create a coverage report. Requires the pytest-cov plugin.
    htmlreport : str, optional
        Filename for HTML report such as www.pygimli.org/build_tests.html.
        Requires pytest-html plugin.
    abort : boolean, optional
        Return correct exit code, e.g. abort documentation build when a test
        fails.
    """

    old_backend = plt.get_backend()
    if not show:
        plt.switch_backend("Agg")

    if target:
        import doctest
        doctest.run_docstring_examples(target, globals())
        return

    try:
        import pytest
    except ImportError:
        raise ImportError("pytest is required to run test suite. "
                          "Try 'sudo pip install pytest'.")

    cwd = join(realpath(__path__[0]), '..')

    excluded = [
        "gui",
        "physics/traveltime/example.py",
        # "physics/em/fdemexample.py"
    ]

    if onlydoctests:
        excluded.append("testing")

    cmd = ([
        "-v", "-rsxX", "--color", "yes", "--doctest-modules", "--durations", 5,
        cwd
    ])
    for directory in excluded:
        cmd.extend(["--ignore", join(cwd, directory)])

    if coverage:
        pc = opt_import("pytest_cov", "create a code coverage report")
        if pc:
            cmd.extend(["--cov", "pygimli"])
            cmd.extend(["--cov-report", "term"])

    if htmlreport:
        ph = opt_import("pytest_html", "create a html report")
        if ph:
            cmd.extend(["--html", htmlreport])

    exitcode = pytest.main(cmd)
    plt.switch_backend(old_backend)
    plt.close('all')
    if abort:
        sys.exit(exitcode)
Пример #10
0
    def readHEMData(self, filename, takeevery=1, choosevcp=True):
        """Read RESOLVE type airborne EM data from .XYZ file."""
        self.header = {}
        keyword = ''
        i = 0
        with open(filename) as f:
            for i, line in enumerate(f):
                if line[0] == '/':
                    line = line[1:].strip('\n').replace(',',
                                                        '').replace('AND', '')
                    try:
                        result = [float(co) for co in line.split()]
                    except:
                        result = line.split()
                    if len(result) == 1:
                        result = result[0]
                    if keyword:
                        if isinstance(keyword, list):
                            for kw, res in zip(keyword, result):
                                self.header[kw] = res
                        else:
                            self.header[keyword] = result
                        keyword = ''
                    else:
                        keyword = result
                else:
                    break
            line = f.readline()
            print(line)
#            tmp = np.genfromtxt(fname=f, autostrip=True, comments='/',
#                skip_header=0, dtype=float, names=1, case_sensitive='lower',
#                missing_values='*', filling_values=-9999, skip_footer=1)
        tmp = np.genfromtxt(fname=filename,
                            autostrip=True,
                            comments='/',
                            skip_header=i + 1,
                            dtype=float,
                            names=True,
                            case_sensitive='lower',
                            missing_values='*',
                            filling_values=-9999,
                            skip_footer=1)
        # read properties from header
        if choosevcp:
            ivcp = np.nonzero(np.array(self.header['COILGEOMETRY']) == 1)[0]
        else:
            ivcp = range(len(self.header['FREQUENCY']))
        self.frequencies = np.array(self.header['FREQUENCY'])[ivcp]
        self.coilSpacing = np.array(self.header['COILSEPERATION'])[ivcp]
        # read properties from data block
        names = tmp.dtype.names
        pyproj = opt_import('pyproj',
                            'coordinate transformations EM lon,lat values')
        if 'lon' in names and 'lat' in names and pyproj is not None:
            utm = pyproj.Proj(proj='utm', zone=32, ellps='WGS84')  # projection
            x, y = utm(tmp['lon'], tmp['lat'])
        else:
            x, y = tmp['x'], tmp['y']
        self.pos = np.column_stack((x, y))[::takeevery]
        dx = np.sqrt(np.diff(self.pos[:, 0])**2 + np.diff(self.pos[:, 1])**2)
        self.x = np.hstack((0., np.cumsum(dx)))
        self.z = tmp['h_laser'][::takeevery]
        self.topo = tmp['topo'][::takeevery]
        IP = np.column_stack([tmp['real_' + str(i + 1)] for i in ivcp])
        OP = np.column_stack([tmp['quad_' + str(i + 1)] for i in ivcp])
        # better do a decimation or running average here
        self.IP = IP[::takeevery, :]
        self.OP = OP[::takeevery, :]
        self.isActiveFreq = self.frequencies > 0.0
        self.activeFreq = np.nonzero(self.isActiveFreq)[0]