Skip to content

Commit

Permalink
Merge a419bb8 into 89c6c6f
Browse files Browse the repository at this point in the history
  • Loading branch information
trichter committed Jun 21, 2016
2 parents 89c6c6f + a419bb8 commit 539549c
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 34 deletions.
4 changes: 3 additions & 1 deletion obspy/core/stream.py
Expand Up @@ -30,7 +30,8 @@
from obspy.core.util import NamedTemporaryFile
from obspy.core.util.base import (ENTRY_POINTS, _get_function_from_entry_point,
_read_from_plugin, download_to_file)
from obspy.core.util.decorator import (deprecated, map_example_filename,
from obspy.core.util.decorator import (_add_processing_info, deprecated,
map_example_filename,
raise_if_masked, uncompress_file)
from obspy.core.util.misc import get_window_times

Expand Down Expand Up @@ -2564,6 +2565,7 @@ def normalize(self, global_max=False):
tr.normalize(norm=norm)
return self

@_add_processing_info
def rotate(self, method, back_azimuth=None, inclination=None):
"""
Rotate stream objects.
Expand Down
4 changes: 4 additions & 0 deletions obspy/core/tests/test_stream.py
Expand Up @@ -2409,6 +2409,10 @@ def test_slide_nearest_sample(self):
for arg in patch.call_args_list:
self.assertFalse(arg[1]["nearest_sample"])

def test_add_processing_info(self):
stream = read().rotate('ZNE->LQT', 10, 10)
self.assertIn('rotate', stream[0].stats.processing[0])


def suite():
return unittest.makeSuite(StreamTestCase, 'test')
Expand Down
34 changes: 1 addition & 33 deletions obspy/core/trace.py
Expand Up @@ -13,20 +13,18 @@
from future.builtins import * # NOQA
from future.utils import native_str

import inspect
import math
import warnings
from copy import copy, deepcopy

import numpy as np
from decorator import decorator

from obspy.core import compatibility
from obspy.core.utcdatetime import UTCDateTime
from obspy.core.util import AttribDict, create_empty_data_chunk
from obspy.core.util.base import _get_function_from_entry_point
from obspy.core.util.decorator import (
deprecated, raise_if_masked, skip_if_no_data)
_add_processing_info, deprecated, raise_if_masked, skip_if_no_data)
from obspy.core.util.misc import (flat_not_masked_contiguous, get_window_times,
limit_numpy_fft_cache)

Expand Down Expand Up @@ -206,36 +204,6 @@ def _repr_pretty_(self, p, cycle):
p.text(str(self))


@decorator
def _add_processing_info(func, *args, **kwargs):
"""
This is a decorator that attaches information about a processing call as a
string to the Trace.stats.processing list.
"""
callargs = inspect.getcallargs(func, *args, **kwargs)
callargs.pop("self")
kwargs_ = callargs.pop("kwargs", {})
from obspy import __version__
info = "ObsPy {version}: {function}(%s)".format(
version=__version__,
function=func.__name__)
arguments = []
arguments += \
["%s=%s" % (k, repr(v)) if not isinstance(v, native_str) else
"%s='%s'" % (k, v) for k, v in callargs.items()]
arguments += \
["%s=%s" % (k, repr(v)) if not isinstance(v, native_str) else
"%s='%s'" % (k, v) for k, v in kwargs_.items()]
arguments.sort()
info = info % "::".join(arguments)
self = args[0]
result = func(*args, **kwargs)
# Attach after executing the function to avoid having it attached
# while the operation failed.
self._internal_add_processing_info(info)
return result


class Trace(object):
"""
An object containing data of a continuous series, such as a seismic trace.
Expand Down
41 changes: 41 additions & 0 deletions obspy/core/util/decorator.py
Expand Up @@ -14,6 +14,7 @@
from future.utils import PY2, native_str

import functools
import importlib
import inspect
import os
import re
Expand Down Expand Up @@ -239,6 +240,46 @@ def skip_if_no_data(func, *args, **kwargs):
return func(*args, **kwargs)


@decorator
def _add_processing_info(func, *args, **kwargs):
"""
This is a decorator that attaches information about a processing call as a
string to the Trace.stats.processing list.
"""
callargs = inspect.getcallargs(func, *args, **kwargs)
callargs.pop("self")
kwargs_ = callargs.pop("kwargs", {})
module = inspect.getmodule(func).__name__
if '.' in module:
module = module.split('.')[0]
version = getattr(importlib.import_module(module), '__version__')
if module == 'obspy':
module = 'ObsPy'
info = "{module} {version}: {function}(%s)".format(
module=module,
version=version,
function=func.__name__)
arguments = []
arguments += \
["%s=%s" % (k, repr(v)) if not isinstance(v, native_str) else
"%s='%s'" % (k, v) for k, v in callargs.items()]
arguments += \
["%s=%s" % (k, repr(v)) if not isinstance(v, native_str) else
"%s='%s'" % (k, v) for k, v in kwargs_.items()]
arguments.sort()
info = info % "::".join(arguments)
self = args[0]
result = func(*args, **kwargs)
# Attach after executing the function to avoid having it attached
# while the operation failed.
try:
self._internal_add_processing_info(info)
except AttributeError:
for tr in self:
tr._internal_add_processing_info(info)
return result


def map_example_filename(arg_kwarg_name):
"""
Decorator that replaces "/path/to/filename" patterns in the arg or kwarg
Expand Down

0 comments on commit 539549c

Please sign in to comment.