コード例 #1
0
ファイル: decorator.py プロジェクト: gthompson/obspy
 def new_func(*args, **kwargs):
     prefix = '/path/to/'
     # check kwargs
     if arg_kwarg_name in kwargs:
         if isinstance(kwargs[arg_kwarg_name], (str, native_str)):
             if kwargs[arg_kwarg_name].startswith(prefix):
                 try:
                     kwargs[arg_kwarg_name] = \
                         getExampleFile(kwargs[arg_kwarg_name][9:])
                 # file not found by getExampleFile:
                 except IOError:
                     pass
     # check args
     else:
         try:
             ind = inspect.getargspec(func).args.index(arg_kwarg_name)
         except ValueError:
             pass
         else:
             if ind < len(args) and isinstance(args[ind], (str,
                                                           native_str)):
                 # need to check length of args from inspect
                 if args[ind].startswith(prefix):
                     try:
                         args = list(args)
                         args[ind] = getExampleFile(args[ind][9:])
                         args = tuple(args)
                     # file not found by getExampleFile:
                     except IOError:
                         pass
     return func(*args, **kwargs)
コード例 #2
0
ファイル: test_core.py プロジェクト: bmorg/obspy
    def test_write_nlloc_obs(self):
        """
        Test writing nonlinloc observations phase file.
        """
        # load nlloc.qml QuakeML file to generate OBS file from it
        filename = getExampleFile("nlloc.qml")
        cat = readEvents(filename, "QUAKEML")
        # adjust one pick time that got cropped by nonlinloc in NLLOC HYP file
        # due to less precision in hypocenter file (that we used to create the
        # reference QuakeML file)
        for pick in cat[0].picks:
            if pick.waveform_id.station_code == "UH4" and \
               pick.phase_hint == "P":
                pick.time -= 0.005

        # read expected OBS file output
        filename = getExampleFile("nlloc.obs")
        with open(filename, "rb") as fh:
            expected = fh.read().decode()

        # write via plugin
        with NamedTemporaryFile() as tf:
            cat.write(tf, format="NLLOC_OBS")
            tf.seek(0)
            got = tf.read().decode()

        self.assertEqual(expected, got)

        # write manually
        with NamedTemporaryFile() as tf:
            write_nlloc_obs(cat, tf)
            tf.seek(0)
            got = tf.read().decode()

        self.assertEqual(expected, got)
コード例 #3
0
    def test_write_nlloc_obs(self):
        """
        Test writing nonlinloc observations phase file.
        """
        # load nlloc.qml QuakeML file to generate OBS file from it
        filename = getExampleFile("nlloc.qml")
        cat = readEvents(filename, "QUAKEML")
        # adjust one pick time that got cropped by nonlinloc in NLLOC HYP file
        # due to less precision in hypocenter file (that we used to create the
        # reference QuakeML file)
        for pick in cat[0].picks:
            if pick.waveform_id.station_code == "UH4" and \
               pick.phase_hint == "P":
                pick.time -= 0.005

        # read expected OBS file output
        filename = getExampleFile("nlloc.obs")
        with open(filename, "rb") as fh:
            expected = fh.read().decode()

        # write via plugin
        with NamedTemporaryFile() as tf:
            cat.write(tf, format="NLLOC_OBS")
            tf.seek(0)
            got = tf.read().decode()

        self.assertEqual(expected, got)

        # write manually
        with NamedTemporaryFile() as tf:
            write_nlloc_obs(cat, tf)
            tf.seek(0)
            got = tf.read().decode()

        self.assertEqual(expected, got)
コード例 #4
0
ファイル: decorator.py プロジェクト: shineusn/obspy
 def new_func(*args, **kwargs):
     prefix = '/path/to/'
     # check kwargs
     if arg_kwarg_name in kwargs:
         if isinstance(kwargs[arg_kwarg_name], (str, native_str)):
             if kwargs[arg_kwarg_name].startswith(prefix):
                 try:
                     kwargs[arg_kwarg_name] = \
                         getExampleFile(kwargs[arg_kwarg_name][9:])
                 # file not found by getExampleFile:
                 except IOError:
                     pass
     # check args
     else:
         try:
             ind = inspect.getargspec(func).args.index(arg_kwarg_name)
         except ValueError:
             pass
         else:
             if ind < len(args) and isinstance(args[ind],
                                               (str, native_str)):
                 # need to check length of args from inspect
                 if args[ind].startswith(prefix):
                     try:
                         args = list(args)
                         args[ind] = getExampleFile(args[ind][9:])
                         args = tuple(args)
                     # file not found by getExampleFile:
                     except IOError:
                         pass
     return func(*args, **kwargs)
コード例 #5
0
ファイル: test_core.py プロジェクト: bmorg/obspy
 def test_is_nlloc_hyp(self):
     # test positive
     filename = getExampleFile("nlloc.hyp")
     self.assertEqual(is_nlloc_hyp(filename), True)
     # test some negatives
     for filenames in ["nlloc.qml", "nlloc.obs", "gaps.mseed",
                       "BW_RJOB.xml", "QFILE-TEST-ASC.ASC", "LMOW.BHE.SAC"]:
         filename = getExampleFile("nlloc.qml")
         self.assertEqual(is_nlloc_hyp(filename), False)
コード例 #6
0
ファイル: test_core.py プロジェクト: bmorg/obspy
 def test_read_nlloc_with_picks(self):
     """
     Test correct resource ID linking when reading NLLOC_HYP file with
     providing original picks.
     """
     picks = readEvents(getExampleFile("nlloc.qml"))[0].picks
     arrivals = readEvents(getExampleFile("nlloc.hyp"), format="NLLOC_HYP",
                           picks=picks)[0].origins[0].arrivals
     expected = [p.resource_id for p in picks]
     got = [a.pick_id for a in arrivals]
     self.assertEqual(expected, got)
コード例 #7
0
 def test_is_nlloc_hyp(self):
     # test positive
     filename = getExampleFile("nlloc.hyp")
     self.assertEqual(is_nlloc_hyp(filename), True)
     # test some negatives
     for filenames in [
             "nlloc.qml", "nlloc.obs", "gaps.mseed", "BW_RJOB.xml",
             "QFILE-TEST-ASC.ASC", "LMOW.BHE.SAC"
     ]:
         filename = getExampleFile("nlloc.qml")
         self.assertEqual(is_nlloc_hyp(filename), False)
コード例 #8
0
 def test_read_nlloc_with_picks(self):
     """
     Test correct resource ID linking when reading NLLOC_HYP file with
     providing original picks.
     """
     picks = readEvents(getExampleFile("nlloc.qml"))[0].picks
     arrivals = readEvents(getExampleFile("nlloc.hyp"),
                           format="NLLOC_HYP",
                           picks=picks)[0].origins[0].arrivals
     expected = [p.resource_id for p in picks]
     got = [a.pick_id for a in arrivals]
     self.assertEqual(expected, got)
コード例 #9
0
ファイル: test_core.py プロジェクト: bmorg/obspy
    def test_read_nlloc_hyp(self):
        """
        Test reading nonlinloc hypocenter phase file.
        """
        filename = getExampleFile("nlloc.hyp")
        cat = read_nlloc_hyp(filename,
                             coordinate_converter=_mock_coordinate_converter)
        with open(getExampleFile("nlloc.qml"), 'rb') as tf:
            quakeml_expected = tf.read().decode()
        with NamedTemporaryFile() as tf:
            cat.write(tf, format="QUAKEML")
            tf.seek(0)
            quakeml_got = tf.read().decode()

        # test creation times manually as they get omitted in the overall test
        creation_time = UTCDateTime("2014-10-17T16:30:08.000000Z")
        self.assertEqual(cat[0].creation_info.creation_time, creation_time)
        self.assertEqual(cat[0].origins[0].creation_info.creation_time,
                         creation_time)

        quakeml_expected = remove_unique_IDs(quakeml_expected,
                                             remove_creation_time=True)
        quakeml_got = remove_unique_IDs(quakeml_got, remove_creation_time=True)
        # In python 3 float.__str__ outputs 5 decimals of precision more.
        # We use it in writing QuakeML, so files look different on Py2/3.
        # We use regex to cut off floats in the xml such that we only compare
        # 7 digits.
        pattern = r'(<.*?>[0-9]*?\.[0-9]{7})[0-9]*?(</.*?>)'
        quakeml_expected = re.sub(pattern, r'\1\2', quakeml_expected)
        quakeml_got = re.sub(pattern, r'\1\2', quakeml_got)

        # remove (changing) obspy version number from output
        re_pattern = '<version>ObsPy .*?</version>'
        quakeml_expected = re.sub(re_pattern, '', quakeml_expected, 1)
        quakeml_got = re.sub(re_pattern, '', quakeml_got, 1)

        compare_xml_strings(quakeml_expected, quakeml_got)
コード例 #10
0
    def test_read_nlloc_hyp(self):
        """
        Test reading nonlinloc hypocenter phase file.
        """
        filename = getExampleFile("nlloc.hyp")
        cat = read_nlloc_hyp(filename,
                             coordinate_converter=_mock_coordinate_converter)
        with open(getExampleFile("nlloc.qml"), 'rb') as tf:
            quakeml_expected = tf.read().decode()
        with NamedTemporaryFile() as tf:
            cat.write(tf, format="QUAKEML")
            tf.seek(0)
            quakeml_got = tf.read().decode()

        # test creation times manually as they get omitted in the overall test
        creation_time = UTCDateTime("2014-10-17T16:30:08.000000Z")
        self.assertEqual(cat[0].creation_info.creation_time, creation_time)
        self.assertEqual(cat[0].origins[0].creation_info.creation_time,
                         creation_time)

        quakeml_expected = remove_unique_IDs(quakeml_expected,
                                             remove_creation_time=True)
        quakeml_got = remove_unique_IDs(quakeml_got, remove_creation_time=True)
        # In python 3 float.__str__ outputs 5 decimals of precision more.
        # We use it in writing QuakeML, so files look different on Py2/3.
        # We use regex to cut off floats in the xml such that we only compare
        # 7 digits.
        pattern = r'(<.*?>[0-9]*?\.[0-9]{7})[0-9]*?(</.*?>)'
        quakeml_expected = re.sub(pattern, r'\1\2', quakeml_expected)
        quakeml_got = re.sub(pattern, r'\1\2', quakeml_got)

        # remove (changing) obspy version number from output
        re_pattern = '<version>ObsPy .*?</version>'
        quakeml_expected = re.sub(re_pattern, '', quakeml_expected, 1)
        quakeml_got = re.sub(re_pattern, '', quakeml_got, 1)

        compare_xml_strings(quakeml_expected, quakeml_got)
コード例 #11
0
    def test_map_example_filename(self):
        """
        Tests the @map_example_filename decorator
        """
        dummy = "abc"
        example_file = "example.npz"
        path = "/path/to/" + example_file
        path_mapped = getExampleFile(example_file)

        def unchanged(a, b="", **kwargs):
            return list(map(str, (a, b, kwargs)))

        @map_example_filename("a")
        def changed1(a, b="", **kwargs):
            return list(map(str, (a, b, kwargs)))

        self.assertEqual(changed1(dummy, dummy), unchanged(dummy, dummy))
        self.assertEqual(changed1(path, dummy), unchanged(path_mapped, dummy))
        self.assertEqual(changed1(dummy, path), unchanged(dummy, path))
        self.assertEqual(changed1(a=path, b=dummy),
                         unchanged(path_mapped, dummy))
        self.assertEqual(changed1(path, b=dummy),
                         unchanged(path_mapped, dummy))
        self.assertEqual(changed1(path, b=path, x=path),
                         unchanged(path_mapped, path, x=path))

        @map_example_filename("b")
        def changed2(a, b="", **kwargs):
            return list(map(str, (a, b, kwargs)))

        self.assertEqual(changed2(dummy, dummy), unchanged(dummy, dummy))
        self.assertEqual(changed2(path, dummy), unchanged(path, dummy))
        self.assertEqual(changed2(dummy, path), unchanged(dummy, path_mapped))
        self.assertEqual(changed2(a=path, b=dummy), unchanged(path, dummy))
        self.assertEqual(changed2(path, b=path), unchanged(path, path_mapped))
        self.assertEqual(changed2(path, b=path, x=path),
                         unchanged(path, path_mapped, x=path))

        @map_example_filename("x")
        def changed3(a, b="", **kwargs):
            return list(map(str, (a, b, kwargs)))

        self.assertEqual(changed3(dummy, dummy), unchanged(dummy, dummy))
        self.assertEqual(changed3(path, dummy), unchanged(path, dummy))
        self.assertEqual(changed3(dummy, path), unchanged(dummy, path))
        self.assertEqual(changed3(a=path, b=dummy), unchanged(path, dummy))
        self.assertEqual(changed3(path, b=dummy), unchanged(path, dummy))
        self.assertEqual(changed3(path, b=path, x=path),
                         unchanged(path, path, x=path_mapped))
コード例 #12
0
ファイル: inventory.py プロジェクト: shineusn/obspy
def read_inventory(path_or_file_object, format=None):
    """
    Function to read inventory files.

    :param path_or_file_object: Filename or file like object.
    """
    # if pathname starts with /path/to/ try to search in examples
    if isinstance(path_or_file_object, (str, native_str)) and \
       path_or_file_object.startswith('/path/to/'):
        try:
            path_or_file_object = getExampleFile(path_or_file_object[9:])
        except:
            # otherwise just try to read the given /path/to folder
            pass
    return _readFromPlugin("inventory", path_or_file_object, format=format)[0]
コード例 #13
0
ファイル: test_util_decorator.py プロジェクト: kaeufl/obspy
    def test_map_example_filename(self):
        """
        Tests the @map_example_filename decorator
        """
        dummy = "abc"
        example_file = "example.npz"
        path = "/path/to/" + example_file
        path_mapped = getExampleFile(example_file)

        def unchanged(a, b="", **kwargs):
            return list(map(str, (a, b, kwargs)))

        @map_example_filename("a")
        def changed1(a, b="", **kwargs):
            return list(map(str, (a, b, kwargs)))

        self.assertEqual(changed1(dummy, dummy), unchanged(dummy, dummy))
        self.assertEqual(changed1(path, dummy), unchanged(path_mapped, dummy))
        self.assertEqual(changed1(dummy, path), unchanged(dummy, path))
        self.assertEqual(changed1(a=path, b=dummy), unchanged(path_mapped, dummy))
        self.assertEqual(changed1(path, b=dummy), unchanged(path_mapped, dummy))
        self.assertEqual(changed1(path, b=path, x=path), unchanged(path_mapped, path, x=path))

        @map_example_filename("b")
        def changed2(a, b="", **kwargs):
            return list(map(str, (a, b, kwargs)))

        self.assertEqual(changed2(dummy, dummy), unchanged(dummy, dummy))
        self.assertEqual(changed2(path, dummy), unchanged(path, dummy))
        self.assertEqual(changed2(dummy, path), unchanged(dummy, path_mapped))
        self.assertEqual(changed2(a=path, b=dummy), unchanged(path, dummy))
        self.assertEqual(changed2(path, b=path), unchanged(path, path_mapped))
        self.assertEqual(changed2(path, b=path, x=path), unchanged(path, path_mapped, x=path))

        @map_example_filename("x")
        def changed3(a, b="", **kwargs):
            return list(map(str, (a, b, kwargs)))

        self.assertEqual(changed3(dummy, dummy), unchanged(dummy, dummy))
        self.assertEqual(changed3(path, dummy), unchanged(path, dummy))
        self.assertEqual(changed3(dummy, path), unchanged(dummy, path))
        self.assertEqual(changed3(a=path, b=dummy), unchanged(path, dummy))
        self.assertEqual(changed3(path, b=dummy), unchanged(path, dummy))
        self.assertEqual(changed3(path, b=path, x=path), unchanged(path, path, x=path_mapped))
コード例 #14
0
ファイル: parser.py プロジェクト: kasra-hosseini/obspy
    def read(self, data):
        """
        General parser method for XML-SEED and Dataless SEED files.

        :type data: Filename, URL, Basestring or StringIO object.
        :param data: Filename, URL or XSEED/SEED string as file pointer or
            StringIO.
        """
        if getattr(self, "_format", None):
            warnings.warn("Clearing parser before every subsequent read()")
            self.__init__()
        # try to transform everything into StringIO object
        if isinstance(data, basestring):
            # if it starts with /path/to/ try to search in examples
            if data.startswith("/path/to/"):
                try:
                    data = getExampleFile(data[9:])
                except:
                    # otherwise just try to read the given /path/to folder
                    pass
            if "://" in data:
                # some URL
                data = urllib2.urlopen(data).read()
            elif os.path.isfile(data):
                # looks like a file - read it
                data = open(data, "rb").read()
            # but could also be a big string with data
            data = StringIO(data)
        elif not hasattr(data, "read"):
            raise TypeError
        # check first byte of data StringIO object
        first_byte = data.read(1)
        data.seek(0)
        if first_byte.isdigit():
            # SEED volumes starts with a number
            self._parseSEED(data)
            self._format = "SEED"
        elif first_byte == "<":
            # XML files should always starts with an '<'
            self._parseXSEED(data)
            self._format = "XSEED"
        else:
            raise IOError
コード例 #15
0
ファイル: parser.py プロジェクト: msimon00/obspy
    def read(self, data):
        """
        General parser method for XML-SEED and Dataless SEED files.

        :type data: Filename, URL, Basestring or StringIO object.
        :param data: Filename, URL or XSEED/SEED string as file pointer or
            StringIO.
        """
        if getattr(self, "_format", None):
            warnings.warn("Clearing parser before every subsequent read()")
            self.__init__()
        # try to transform everything into StringIO object
        if isinstance(data, basestring):
            # if it starts with /path/to/ try to search in examples
            if data.startswith('/path/to/'):
                try:
                    data = getExampleFile(data[9:])
                except:
                    # otherwise just try to read the given /path/to folder
                    pass
            if "://" in data:
                # some URL
                data = urllib2.urlopen(data).read()
            elif os.path.isfile(data):
                # looks like a file - read it
                data = open(data, 'rb').read()
            # but could also be a big string with data
            data = StringIO(data)
        elif not hasattr(data, "read"):
            raise TypeError
        # check first byte of data StringIO object
        first_byte = data.read(1)
        data.seek(0)
        if first_byte.isdigit():
            # SEED volumes starts with a number
            self._parseSEED(data)
            self._format = 'SEED'
        elif first_byte == '<':
            # XML files should always starts with an '<'
            self._parseXSEED(data)
            self._format = 'XSEED'
        else:
            raise IOError
コード例 #16
0
from obspy.core.util import getExampleFile
from obspy.segy.segy import readSU
from obspy.segy.benchmark import plotBenchmark

files = [getExampleFile('seismic01_fdmpi_vz.su'),
         getExampleFile('seismic01_gemini_vz.su'),
         getExampleFile('seismic01_sofi2D_transformed_vz.su'),
         getExampleFile('seismic01_specfem_vz.su')]

sufiles = [readSU(file) for file in files]
plotBenchmark(sufiles, title="Homogenous halfspace", xmax=0.14)
コード例 #17
0
ファイル: test_core.py プロジェクト: bmorg/obspy
 def test_read_nlloc_hyp_via_plugin(self):
     filename = getExampleFile("nlloc.hyp")
     cat = readEvents(filename)
     self.assertEqual(len(cat), 1)
     cat = readEvents(filename, format="NLLOC_HYP")
     self.assertEqual(len(cat), 1)
コード例 #18
0
 def test_read_nlloc_hyp_via_plugin(self):
     filename = getExampleFile("nlloc.hyp")
     cat = readEvents(filename)
     self.assertEqual(len(cat), 1)
     cat = readEvents(filename, format="NLLOC_HYP")
     self.assertEqual(len(cat), 1)