Пример #1
0
    def test_issue_2222(self):
        """
        Test that hour values of 24 don't break parser.
        """

        # modify the example file to contain an hour 24 and second 60
        with open(get_example_file('nlloc.hyp')) as f:
            nll_str = f.read().splitlines()
        # first add a line with hour 24
        str_list = list(nll_str[-3])
        str_list[37:41] = '2400'
        nll_str[-3] = ''.join(str_list)
        # then add a line with second 60
        str_list = list(nll_str[-4])
        str_list[46:48] = '60'
        nll_str[-4] = ''.join(str_list)
        # write to string io and read into catalog object
        str_io = io.StringIO()
        str_io.write('\n'.join(nll_str))
        str_io.seek(0)
        cat = read_nlloc_hyp(str_io)
        # check catalog is populated and pick times are right
        self.assertEqual(len(cat), 1)
        pick1, pick2 = cat[0].picks[-1], cat[0].picks[-2]
        self.assertEqual(pick1.time.hour, 0)
        self.assertEqual(pick2.time.second, 0)
Пример #2
0
    def test_issue_2222(self):
        """
        Test that hour values of 24 don't break parser.
        """

        # modify the example file to contain an hour 24 and second 60
        nll_str = open(get_example_file('nlloc.hyp')).read().splitlines()
        # first add a line with hour 24
        str_list = list(nll_str[-3])
        str_list[37:41] = '2400'
        nll_str[-3] = ''.join(str_list)
        # then add a line with second 60
        str_list = list(nll_str[-4])
        str_list[46:48] = '60'
        nll_str[-4] = ''.join(str_list)
        # write to string io and read into catalog object
        str_io = io.StringIO()
        str_io.write('\n'.join(nll_str))
        str_io.seek(0)
        cat = read_nlloc_hyp(str_io)
        # check catalog is populated and pick times are right
        self.assertEqual(len(cat), 1)
        pick1, pick2 = cat[0].picks[-1], cat[0].picks[-2]
        self.assertEqual(pick1.time.hour, 0)
        self.assertEqual(pick2.time.second, 0)
Пример #3
0
def read_nlloc_sum(file_in):
    """
    Function made to read a nlloc hypocenter-file and store it into a simple LOTOS_class Catalog
    The ID is read from the event.comments part
    """
    from obspy.io.nlloc.core import read_nlloc_hyp
    from lotos.LOTOS_class import Catalog, Event, Phase
    from general import util as gutil

    #file_in='/media/baillard/Shared/Dropbox/_Moi/Projects/Axial/PROG/NLLOC_AXIAL/loc3/AXIAL.20170130.005908.grid0.loc.hyp'
    #file_in='/media/baillard/Shared/Dropbox/_Moi/Projects/Axial/PROG/NLLOC_AXIAL/loc3/sum.nlloc'

    Ray = Catalog()
    cat = read_nlloc_hyp(file_in)

    stations_dic = Ray.stations_realname

    for event in cat:

        id_event = event.comments[0].text
        origin = event.preferred_origin()
        OT = origin.time

        #### Initialize Event

        Event_p = Event()

        Event_p.x = origin.longitude
        Event_p.y = origin.latitude
        Event_p.z = origin.depth / 1000
        Event_p.id = id_event
        Event_p.ot = OT
        Event_p.num_phase = origin.quality.used_phase_count
        Picks_p = event.picks

        for arrival in origin.arrivals:
            Phase_p = Phase()

            if arrival.phase in ['P', 'Pn']:
                Phase_p.type = 1
            else:
                Phase_p.type = 2

            Pick_p = gutil.getPickForArrival(Picks_p, arrival)
            Phase_p.station = stations_dic[Pick_p.waveform_id.station_code]
            Phase_p.t_obs = Pick_p.time - OT
            Phase_p.t_tho = Phase_p.t_obs - arrival.time_residual

            Event_p.phases.append(Phase_p)

        Ray.events.append(Event_p)

    return Ray
Пример #4
0
 def test_read_nlloc_6_beta_signature(self):
     """
     SIGNATURE field of nlloc hypocenter output file was somehow changed at
     some point after version 6.0 (it appears in 6.0.3 beta release for
     example).
     Date is now seemingly always prepended with 'run:' without a space
     afterwards.
     """
     filename = os.path.join(self.datapath, 'nlloc_post_version_6.hyp')
     cat = read_nlloc_hyp(filename)
     # check that signature time-of-run part is correctly read
     # (actually before the fix the above reading already fails..)
     self.assertEqual(cat[0].creation_info.creation_time,
                      UTCDateTime(2017, 5, 9, 11, 0, 22))
Пример #5
0
 def test_read_nlloc_6_beta_signature(self):
     """
     SIGNATURE field of nlloc hypocenter output file was somehow changed at
     some point after version 6.0 (it appears in 6.0.3 beta release for
     example).
     Date is now seemingly always prepended with 'run:' without a space
     afterwards.
     """
     filename = os.path.join(self.datapath, 'nlloc_post_version_6.hyp')
     cat = read_nlloc_hyp(filename)
     # check that signature time-of-run part is correctly read
     # (actually before the fix the above reading already fails..)
     self.assertEqual(
         cat[0].creation_info.creation_time,
         UTCDateTime(2017, 5, 9, 11, 0, 22))
Пример #6
0
def relocate(cat, root_name, in_file, pick_uncertainty=0.1):
    """
    Run NonLinLoc relocations on a catalog. This is a function hardcoded for my laptop only.
    :type cat: obspy.Catalog
    :param cat: catalog of events with picks to relocate
    :type root_name: str
    :param root_name: String specifying where the nlloc.obs files will be written from the catalog
    :type outfiles: str
    :param outfiles: Output directory for location files
    :type in_file: str
    :param in_file: NLLoc input file
    :return: same catalog with new origins appended to each event
    """
    for ev in cat:
        if len(ev.picks) < 5:
            print('Fewer than 5 picks for %s. Will not locate.' %
                  str(ev.resource_id))
            continue
        for pk in ev.picks:
            pk.time_errors.uncertainty = pick_uncertainty
        id_str = str(ev.resource_id).split('/')[-1]
        filename = root_name + 'obs/' + id_str + '.nll'
        outfile = root_name + 'loc/' + id_str
        # TODO This clause needs faster file existece check. Do 25-7.
        if os.path.isfile(outfile):
            if len(glob(outfile + '.????????.??????.grid0.loc.hyp')) > 0:
                print('LOC file already written, reading output to catalog')
        else:
            ev.write(filename, format="NLLOC_OBS")
            # Specify awk command to edit NLLoc .in file
            cmnd = """awk '$1 == "LOCFILES" {$2 = "%s"; $5 = "%s"}1' %s > tmp.txt && mv tmp.txt %s""" % (
                filename, outfile, in_file, in_file)
            call(cmnd, shell=True)
            # Call to NLLoc
            call('NLLoc %s' % in_file, shell=True)
        # Now reading NLLoc output back into catalog as new origin
        # XXX BE MORE CAREFUL HERE. CANNOT GRAB BOTH SUM AND NON-SUM
        out_w_ext = glob(outfile + '.????????.??????.grid0.loc.hyp')
        try:
            new_o = read_nlloc_hyp(out_w_ext[0],
                                   coordinate_converter=my_conversion,
                                   picks=ev.picks)
        except ValueError as ve:
            print(ve)
            continue
        ev.origins.append(new_o[0].origins[0])
        ev.preferred_origin_id = str(new_o[0].origins[0].resource_id)
    return cat
Пример #7
0
def relocate(cat, root_name, in_file):
    """
    Run NonLinLoc relocations on a catalog. This is a function hardcoded for my laptop only.
    :type cat: obspy.Catalog
    :param cat: catalog of events with picks to relocate
    :type root_name: str
    :param root_name: String specifying where the nlloc.obs files will be written from the catalog
    :type outfiles: str
    :param outfiles: Output directory for location files
    :type in_file: str
    :param in_file: NLLoc input file
    :return:
    """
    # root_name = '/media/chet/hdd/seismic/NZ/NLLoc/mrp/2015_Rawlinson_spicks/obs/'
    for ev in cat:
        if len(ev.picks) < 6:
            print('Fewer than 8 picks for %s. Will not locate.' %
                  str(ev.resource_id))
            continue
        id_str = str(ev.resource_id).split('/')[-1]
        filename = root_name + 'obs/' + id_str + '.nll'
        ev.write(filename, format="NLLOC_OBS")
        # Specify awk command to edit NLLoc .in file
        outfile = root_name + 'loc/' + id_str
        cmnd = """awk '$1 == "LOCFILES" {$2 = "%s"; $5 = "%s"}1' %s > tmp && mv tmp %s""" % (
            filename, outfile, in_file, in_file)
        call(cmnd, shell=True)
        # Call to NLLoc
        call('NLLoc %s' % in_file, shell=True)
        # Now reading NLLoc output back into catalog as new origin
        # XXX BE MORE CAREFUL HERE. CANNOT GRAB BOTH SUM AND NON-SUM
        out_w_ext = glob(outfile + '.????????.??????.grid0.loc.hyp')
        try:
            new_o = read_nlloc_hyp(out_w_ext[0],
                                   coordinate_converter=my_conversion,
                                   picks=ev.picks)
        except ValueError as ve:
            print(ve)
            continue
        ev.origins.append(new_o[0].origins[0])
        ev.preferred_origin_id = str(new_o[0].origins[0].resource_id)
    return cat
Пример #8
0
    def test_read_nlloc_hyp(self):
        """
        Test reading nonlinloc hypocenter phase file.
        """
        filename = get_example_file("nlloc_custom.hyp")
        cat = read_nlloc_hyp(filename,
                             coordinate_converter=_mock_coordinate_converter)
        # reset pick channel codes, these got automatically mapped upon reading
        for pick in cat[0].picks:
            pick.waveform_id.channel_code = None
        with open(get_example_file("nlloc_custom.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)
Пример #9
0
    def test_read_nlloc_hyp(self):
        """
        Test reading nonlinloc hypocenter phase file.
        """
        filename = get_example_file("nlloc_custom.hyp")
        cat = read_nlloc_hyp(filename,
                             coordinate_converter=_mock_coordinate_converter)
        with open(get_example_file("nlloc_custom.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_with_builtin_projection(self):
        """
        Test reading nonlinloc hyp file without a coordinate_converter.
        """
        cat = read_nlloc_hyp(get_example_file("nlloc.hyp"))
        cat_expected = read_events(get_example_file("nlloc.qml"))

        # test event
        ev = cat[0]
        ev_expected = cat_expected[0]
        self.assertAlmostEqual(ev.creation_info.creation_time,
                               ev_expected.creation_info.creation_time)

        # test origin
        orig = ev.origins[0]
        orig_expected = ev_expected.origins[0]
        self.assertAlmostEqual(orig.time, orig_expected.time)
        self.assertAlmostEqual(orig.longitude, orig_expected.longitude)
        self.assertAlmostEqual(orig.longitude_errors.uncertainty,
                               orig_expected.longitude_errors.uncertainty)
        self.assertAlmostEqual(orig.latitude, orig_expected.latitude)
        self.assertAlmostEqual(orig.latitude_errors.uncertainty,
                               orig_expected.latitude_errors.uncertainty)
        self.assertAlmostEqual(orig.depth, orig_expected.depth)
        self.assertAlmostEqual(orig.depth_errors.uncertainty,
                               orig_expected.depth_errors.uncertainty)
        self.assertAlmostEqual(orig.depth_errors.confidence_level,
                               orig_expected.depth_errors.confidence_level)
        self.assertEqual(orig.depth_type, orig_expected.depth_type)
        self.assertEqual(orig.quality.associated_phase_count,
                         orig_expected.quality.associated_phase_count)
        self.assertEqual(orig.quality.used_phase_count,
                         orig_expected.quality.used_phase_count)
        self.assertEqual(orig.quality.associated_station_count,
                         orig_expected.quality.associated_station_count)
        self.assertEqual(orig.quality.used_station_count,
                         orig_expected.quality.used_station_count)
        self.assertAlmostEqual(orig.quality.standard_error,
                               orig_expected.quality.standard_error)
        self.assertAlmostEqual(orig.quality.azimuthal_gap,
                               orig_expected.quality.azimuthal_gap)
        self.assertAlmostEqual(orig.quality.secondary_azimuthal_gap,
                               orig_expected.quality.secondary_azimuthal_gap)
        self.assertEqual(orig.quality.ground_truth_level,
                         orig_expected.quality.ground_truth_level)
        self.assertAlmostEqual(orig.quality.minimum_distance,
                               orig_expected.quality.minimum_distance)
        self.assertAlmostEqual(orig.quality.maximum_distance,
                               orig_expected.quality.maximum_distance)
        self.assertAlmostEqual(orig.quality.median_distance,
                               orig_expected.quality.median_distance)
        self.assertAlmostEqual(
            orig.origin_uncertainty.min_horizontal_uncertainty,
            orig_expected.origin_uncertainty.min_horizontal_uncertainty)
        self.assertAlmostEqual(
            orig.origin_uncertainty.max_horizontal_uncertainty,
            orig_expected.origin_uncertainty.max_horizontal_uncertainty)
        self.assertAlmostEqual(
            orig.origin_uncertainty.azimuth_max_horizontal_uncertainty,
            orig_expected.origin_uncertainty.
            azimuth_max_horizontal_uncertainty)
        self.assertEqual(
            orig.origin_uncertainty.preferred_description,
            orig_expected.origin_uncertainty.preferred_description)
        self.assertAlmostEqual(
            orig.origin_uncertainty.confidence_level,
            orig_expected.origin_uncertainty.confidence_level)
        self.assertEqual(orig.creation_info.creation_time,
                         orig_expected.creation_info.creation_time)
        self.assertEqual(orig.comments[0].text, orig_expected.comments[0].text)

        # test a couple of arrivals
        for n in range(2):
            arriv = orig.arrivals[n]
            arriv_expected = orig_expected.arrivals[n]
            self.assertEqual(arriv.phase, arriv_expected.phase)
            self.assertAlmostEqual(arriv.azimuth, arriv_expected.azimuth)
            self.assertAlmostEqual(arriv.distance, arriv_expected.distance)
            self.assertAlmostEqual(arriv.takeoff_angle,
                                   arriv_expected.takeoff_angle)
            self.assertAlmostEqual(arriv.time_residual,
                                   arriv_expected.time_residual)
            self.assertAlmostEqual(arriv.time_weight,
                                   arriv_expected.time_weight)

        # test a couple of picks
        for n in range(2):
            pick = ev.picks[n]
            pick_expected = ev_expected.picks[n]
            self.assertAlmostEqual(pick.time, pick_expected.time)
            self.assertEqual(pick.waveform_id.station_code,
                             pick_expected.waveform_id.station_code)
            self.assertEqual(pick.onset, pick_expected.onset)
            self.assertEqual(pick.phase_hint, pick_expected.phase_hint)
            self.assertEqual(pick.polarity, pick_expected.polarity)
Пример #11
0
        continue
    id_str = str(ev.resource_id).split('/')[-1]
    filename = root_name + id_str + '.nll'
    ev.write(filename, format="NLLOC_OBS")
    # Specify awk command to edit NLLoc .in file
    in_file = '/Users/home/hoppche/NLLoc/mrp/run/nlloc_mrp.in'
    outfile = '/Users/home/hoppche/NLLoc/mrp/loc/' + id_str
    cmnd = """awk '$1 == "LOCFILES" {$2 = "%s"; $5 = "%s"}1' %s > tmp && mv tmp %s""" % (
        filename, outfile, in_file, in_file)
    call(cmnd, shell=True)
    # Call to NLLoc
    call('NLLoc /Users/home/hoppche/NLLoc/mrp/run/nlloc_mrp.in', shell=True)
    # Now reading NLLoc output back into catalog as new origin
    out_w_ext = glob(outfile + '*.grid0.loc.hyp')
    new_o = read_nlloc_hyp(out_w_ext[0],
                           coordinate_converter=my_conversion,
                           picks=ev.picks)
    ev.origins.append(new_o[0].origins[0])
    ev.preferred_origin_id = str(new_o[0].origins[0].resource_id)

# Cut templates for each new event based on new picks
for event in refined_cat:
    ev_name = str(event.resource_id).split('/')[2]
    st = template_dict[event.resource_id]
    st1 = pre_processing.shortproc(st,
                                   lowcut=1.0,
                                   highcut=20.0,
                                   filt_order=3,
                                   samp_rate=50,
                                   debug=0)
    print('Feeding stream to _template_gen...')
Пример #12
0
    def test_read_nlloc_hyp_with_builtin_projection(self):
        """
        Test reading nonlinloc hyp file without a coordinate_converter.
        """
        cat = read_nlloc_hyp(get_example_file("nlloc.hyp"))
        cat_expected = read_events(get_example_file("nlloc.qml"))

        # test event
        ev = cat[0]
        ev_expected = cat_expected[0]
        self.assertAlmostEqual(ev.creation_info.creation_time,
                               ev_expected.creation_info.creation_time)

        # test origin
        orig = ev.origins[0]
        orig_expected = ev_expected.origins[0]
        self.assertAlmostEqual(orig.time, orig_expected.time)
        self.assertAlmostEqual(orig.longitude, orig_expected.longitude)
        self.assertAlmostEqual(orig.longitude_errors.uncertainty,
                               orig_expected.longitude_errors.uncertainty)
        self.assertAlmostEqual(orig.latitude, orig_expected.latitude)
        self.assertAlmostEqual(orig.latitude_errors.uncertainty,
                               orig_expected.latitude_errors.uncertainty)
        self.assertAlmostEqual(orig.depth, orig_expected.depth)
        self.assertAlmostEqual(orig.depth_errors.uncertainty,
                               orig_expected.depth_errors.uncertainty)
        self.assertAlmostEqual(orig.depth_errors.confidence_level,
                               orig_expected.depth_errors.confidence_level)
        self.assertEqual(orig.depth_type, orig_expected.depth_type)
        self.assertEqual(orig.quality.associated_phase_count,
                         orig_expected.quality.associated_phase_count)
        self.assertEqual(orig.quality.used_phase_count,
                         orig_expected.quality.used_phase_count)
        self.assertEqual(orig.quality.associated_station_count,
                         orig_expected.quality.associated_station_count)
        self.assertEqual(orig.quality.used_station_count,
                         orig_expected.quality.used_station_count)
        self.assertAlmostEqual(orig.quality.standard_error,
                               orig_expected.quality.standard_error)
        self.assertAlmostEqual(orig.quality.azimuthal_gap,
                               orig_expected.quality.azimuthal_gap)
        self.assertAlmostEqual(orig.quality.secondary_azimuthal_gap,
                               orig_expected.quality.secondary_azimuthal_gap)
        self.assertEqual(orig.quality.ground_truth_level,
                         orig_expected.quality.ground_truth_level)
        self.assertAlmostEqual(orig.quality.minimum_distance,
                               orig_expected.quality.minimum_distance)
        self.assertAlmostEqual(orig.quality.maximum_distance,
                               orig_expected.quality.maximum_distance)
        self.assertAlmostEqual(orig.quality.median_distance,
                               orig_expected.quality.median_distance)
        self.assertAlmostEqual(
            orig.origin_uncertainty.min_horizontal_uncertainty,
            orig_expected.origin_uncertainty.min_horizontal_uncertainty)
        self.assertAlmostEqual(
            orig.origin_uncertainty.max_horizontal_uncertainty,
            orig_expected.origin_uncertainty.max_horizontal_uncertainty)
        self.assertAlmostEqual(
            orig.origin_uncertainty.azimuth_max_horizontal_uncertainty,
            orig_expected.origin_uncertainty.azimuth_max_horizontal_uncertainty
        )
        self.assertEqual(
            orig.origin_uncertainty.preferred_description,
            orig_expected.origin_uncertainty.preferred_description)
        self.assertAlmostEqual(
            orig.origin_uncertainty.confidence_level,
            orig_expected.origin_uncertainty.confidence_level)
        self.assertEqual(orig.creation_info.creation_time,
                         orig_expected.creation_info.creation_time)
        self.assertEqual(orig.comments[0].text, orig_expected.comments[0].text)

        # test a couple of arrivals
        for n in range(2):
            arriv = orig.arrivals[n]
            arriv_expected = orig_expected.arrivals[n]
            self.assertEqual(arriv.phase, arriv_expected.phase)
            self.assertAlmostEqual(arriv.azimuth, arriv_expected.azimuth)
            self.assertAlmostEqual(arriv.distance, arriv_expected.distance)
            self.assertAlmostEqual(arriv.takeoff_angle,
                                   arriv_expected.takeoff_angle)
            self.assertAlmostEqual(arriv.time_residual,
                                   arriv_expected.time_residual)
            self.assertAlmostEqual(arriv.time_weight,
                                   arriv_expected.time_weight)

        # test a couple of picks
        for n in range(2):
            pick = ev.picks[n]
            pick_expected = ev_expected.picks[n]
            self.assertAlmostEqual(pick.time, pick_expected.time)
            self.assertEqual(pick.waveform_id.station_code,
                             pick_expected.waveform_id.station_code)
            self.assertEqual(pick.onset, pick_expected.onset)
            self.assertEqual(pick.phase_hint, pick_expected.phase_hint)
            self.assertEqual(pick.polarity, pick_expected.polarity)