def comm(tmpdir):
    """
    Fixture for the iterations tests. This already requires parts of the
    iterations component to be working but that is needed for the tests in
    any case.
    """
    tmpdir = str(tmpdir)
    comm = Communicator()

    # Mock the project specific source time function.
    comm.project = mock.MagicMock()
    comm.project.get_project_function = lambda a: None

    IterationsComponent(
        iterations_folder=tmpdir,
        communicator=comm,
        component_name="iterations")
    # Create two iterations.
    comm.iterations.create_new_iteration(
        "1", "ses3d_4_1",
        {"EVENT_1": ["AA.BB", "CC.DD"], "EVENT_2": ["EE.FF"]}, 10.0, 20.0,
        quiet=True)
    comm.iterations.create_new_iteration(
        "2", "ses3d_4_1",
        {"EVENT_1": ["AA.BB", "CC.DD"], "EVENT_2": ["EE.FF"]}, 10.0, 20.0,
        quiet=True)
    it = comm.iterations.get("1")
    it.comments = ["Some", "random comments"]
    comm.iterations.save_iteration(it)
    return comm
示例#2
0
def comm():
    """
    Returns a communicator with an initialized events component.
    """
    data_dir = os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(
        inspect.getfile(inspect.currentframe())))), "data", "ExampleProject",
        "EVENTS")
    comm = Communicator()
    # Add project comm with paths to this fake component.
    comm.project = mock.MagicMock()
    comm.project.read_only_caches = False
    comm.project.paths = {"cache": data_dir, "root": data_dir}
    EventsComponent(data_dir, comm, "events")
    return comm
示例#3
0
def filled_comm(tmpdir):
    """
    Used for doctests. Needs a separate initialization as for some reason
    doctests do sometimes not like chained fixtures...
    """
    tmpdir = str(tmpdir)
    db_file = os.path.join(tmpdir, "inventory_db.sqlite")
    comm = Communicator()
    InventoryDBComponent(db_file=db_file,
                         communicator=comm,
                         component_name="inventory_db")
    comm.inventory_db.save_station_coordinates(station_id="AA.BB",
                                               latitude=1.0,
                                               longitude=2.0,
                                               elevation_in_m=3.0,
                                               local_depth_in_m=4.0)
    comm.inventory_db.save_station_coordinates(station_id="CC.DD",
                                               latitude=2.0,
                                               longitude=2.0,
                                               elevation_in_m=2.0,
                                               local_depth_in_m=2.0)
    comm.inventory_db.save_station_coordinates(station_id="EE.FF",
                                               latitude=None,
                                               longitude=None,
                                               elevation_in_m=None,
                                               local_depth_in_m=None)
    return comm
示例#4
0
def comm():
    """
    Returns a communicator with an initialized events component.
    """
    data_dir = os.path.join(
        os.path.dirname(
            os.path.dirname(
                os.path.abspath(inspect.getfile(inspect.currentframe())))),
        "data", "ExampleProject", "EVENTS")
    comm = Communicator()
    # Add project comm with paths to this fake component.
    comm.project = mock.MagicMock()
    comm.project.read_only_caches = False
    comm.project.paths = {"cache": data_dir, "root": data_dir}
    EventsComponent(data_dir, comm, "events")
    return comm
示例#5
0
def comm(tmpdir):
    tmpdir = str(tmpdir)
    # Test data directory
    data_dir = os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(
        inspect.getfile(inspect.currentframe())))), "data")
    comm = Communicator()
    StationsComponent(
        stationxml_folder=os.path.join(data_dir, "station_files",
                                       "stationxml"),
        seed_folder=os.path.join(data_dir, "station_files", "seed"),
        resp_folder=os.path.join(data_dir, "station_files", "resp"),
        cache_folder=tmpdir,
        communicator=comm,
        component_name="stations")
    comm.cache_dir = tmpdir
    return comm
示例#6
0
def comm(tmpdir):
    """
    Fixture for the iterations tests. This already requires parts of the
    iterations component to be working but that is needed for the tests in
    any case.
    """
    tmpdir = str(tmpdir)
    comm = Communicator()

    # Mock the project specific source time function.
    comm.project = mock.MagicMock()
    comm.project.get_project_function = lambda a: None

    IterationsComponent(iterations_folder=tmpdir,
                        communicator=comm,
                        component_name="iterations")

    # Greatly speed up this fixture.
    with mock.patch("lasif.tools.Q_discrete.calculate_Q_model") as p:
        p.return_value = (np.array([1.6341, 1.0513, 1.5257]),
                          np.array([0.59496, 3.7119, 22.2171]))

        # Create two iterations.
        comm.iterations.create_new_iteration("1",
                                             "ses3d_4_1", {
                                                 "EVENT_1": ["AA.BB", "CC.DD"],
                                                 "EVENT_2": ["EE.FF"]
                                             },
                                             10.0,
                                             20.0,
                                             quiet=True,
                                             create_folders=False)
        comm.iterations.create_new_iteration("2",
                                             "ses3d_4_1", {
                                                 "EVENT_1": ["AA.BB", "CC.DD"],
                                                 "EVENT_2": ["EE.FF"]
                                             },
                                             10.0,
                                             20.0,
                                             quiet=True,
                                             create_folders=False)

    it = comm.iterations.get("1")
    it.comments = ["Some", "random comments"]
    comm.iterations.save_iteration(it)
    return comm
示例#7
0
def comm(tmpdir):
    tmpdir = str(tmpdir)
    db_file = os.path.join(tmpdir, "inventory_db.sqlite")
    comm = Communicator()
    InventoryDBComponent(db_file=db_file,
                         communicator=comm,
                         component_name="inventory_db")
    return comm
示例#8
0
def test_faulty_events(tmpdir, recwarn):
    tmpdir = str(tmpdir)
    file_1 = os.path.join(
        os.path.dirname(
            os.path.dirname(
                os.path.abspath(inspect.getfile(inspect.currentframe())))),
        "data", "ExampleProject", "EVENTS",
        "GCMT_event_TURKEY_Mag_5.1_2010-3-24-14-11.xml")
    file_2 = os.path.join(
        os.path.dirname(
            os.path.dirname(
                os.path.abspath(inspect.getfile(inspect.currentframe())))),
        "data", "ExampleProject", "EVENTS",
        "GCMT_event_TURKEY_Mag_5.9_2011-5-19-20-15.xml")
    cat = obspy.read_events(file_1)
    cat += obspy.read_events(file_2)

    # Modify it to trigger all problems.
    temp = io.BytesIO()
    cat.write(temp, format="quakeml")
    temp.seek(0, 0)
    temp = temp.read()
    pattern = re.compile(r"<depth>.*?</depth>", re.DOTALL)
    temp = re.sub(pattern, "<depth></depth>", temp)
    temp = re.sub(r"<type>.*?</type>", "<type></type>", temp)
    with open(os.path.join(tmpdir, "random.xml"), "wb") as fh:
        fh.write(temp)

    comm = Communicator()
    comm.project = mock.MagicMock()
    comm.project.read_only_caches = False
    comm.project.paths = {"cache": tmpdir, "root": tmpdir}
    EventsComponent(tmpdir, comm, "events")

    event = comm.events.get('random')
    assert "QuakeML file must have exactly one event." in str(
        recwarn.pop(LASIFWarning).message)
    assert "contains no depth" in str(recwarn.pop(LASIFWarning).message)
    assert "Magnitude has no specified type" in str(
        recwarn.pop(LASIFWarning).message)

    # Assert the default values it will then take.
    assert event["depth_in_km"] == 0.0
    assert event["magnitude_type"] == "Mw"
示例#9
0
def comm(tmpdir):
    tmpdir = str(tmpdir)
    # Test data directory
    data_dir = os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(
        inspect.getfile(inspect.currentframe())))), "data")
    comm = Communicator()
    proj_mock = mock.MagicMock()
    proj_mock.read_only_caches = False
    proj_mock.paths = {"root": data_dir}
    comm.register("project", proj_mock)
    StationsComponent(
        stationxml_folder=os.path.join(data_dir, "station_files",
                                       "stationxml"),
        seed_folder=os.path.join(data_dir, "station_files", "seed"),
        resp_folder=os.path.join(data_dir, "station_files", "resp"),
        cache_folder=tmpdir,
        communicator=comm,
        component_name="stations")
    comm.cache_dir = tmpdir
    return comm
示例#10
0
def comm(tmpdir):
    tmpdir = str(tmpdir)
    # Test data directory
    data_dir = os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(
        inspect.getfile(inspect.currentframe())))), "data")
    comm = Communicator()
    proj_mock = mock.MagicMock()
    proj_mock.read_only_caches = False
    proj_mock.paths = {"root": data_dir}
    comm.register("project", proj_mock)
    StationsComponent(
        stationxml_folder=os.path.join(data_dir, "station_files",
                                       "stationxml"),
        seed_folder=os.path.join(data_dir, "station_files", "seed"),
        resp_folder=os.path.join(data_dir, "station_files", "resp"),
        cache_folder=tmpdir,
        communicator=comm,
        component_name="stations")
    comm.cache_dir = tmpdir
    return comm
示例#11
0
def test_faulty_events(tmpdir, recwarn):
    tmpdir = str(tmpdir)
    file_1 = os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(
        inspect.getfile(inspect.currentframe())))), "data", "ExampleProject",
        "EVENTS", "GCMT_event_TURKEY_Mag_5.1_2010-3-24-14-11.xml")
    file_2 = os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(
        inspect.getfile(inspect.currentframe())))), "data", "ExampleProject",
        "EVENTS", "GCMT_event_TURKEY_Mag_5.9_2011-5-19-20-15.xml")
    cat = obspy.read_events(file_1)
    cat += obspy.read_events(file_2)

    # Modify it to trigger all problems.
    temp = io.BytesIO()
    cat.write(temp, format="quakeml")
    temp.seek(0, 0)
    temp = temp.read()
    pattern = re.compile(r"<depth>.*?</depth>", re.DOTALL)
    temp = re.sub(pattern, "<depth></depth>", temp)
    temp = re.sub(r"<type>.*?</type>", "<type></type>", temp)
    with open(os.path.join(tmpdir, "random.xml"), "wb") as fh:
        fh.write(temp)

    comm = Communicator()
    comm.project = mock.MagicMock()
    comm.project.read_only_caches = False
    comm.project.paths = {"cache": tmpdir, "root": tmpdir}
    EventsComponent(tmpdir, comm, "events")

    event = comm.events.get('random')
    assert "QuakeML file must have exactly one event." in str(
        recwarn.pop(LASIFWarning).message)
    assert "contains no depth" in str(recwarn.pop(LASIFWarning).message)
    assert "Magnitude has no specified type" in str(
        recwarn.pop(LASIFWarning).message)

    # Assert the default values it will then take.
    assert event["depth_in_km"] == 0.0
    assert event["magnitude_type"] == "Mw"
示例#12
0
 def __init__(self, information_dict: dict):
     """
     Initiate everything to make it work correctly. Make sure that
     a running inversion can be restarted although this is done.
     """
     self.info = information_dict
     self.random_event_processing = False
     self.ad_src_type = "tf_phase_misfit"
     self.__comm = Communicator()
     super(ProjectComponent, self).__init__(self.__comm, "project")
     self.simulation_dict = self._read_config_file()
     self.get_inversion_attributes(first=True)
     self.__setup_components()
     self.get_inversion_attributes(first=False)
     self._validate_inversion_project()
     self.remote_gradient_processing = True
     self.simulation_time_step = False
     # Attempt to get the simulation timestep immediately if it exists.
     self.get_simulation_time_step()
示例#13
0
def test_faulty_events(tmpdir, recwarn):
    import pyasdf

    tmpdir = str(tmpdir)
    file_1 = os.path.join(
        os.path.dirname(
            os.path.dirname(
                os.path.abspath(inspect.getfile(inspect.currentframe()))
            )
        ),
        "data",
        "example_project",
        "DATA",
        "EARTHQUAKES",
        "GCMT_event_TURKEY_Mag_5.1_2010-3-24-14-11.h5",
    )
    file_2 = os.path.join(
        os.path.dirname(
            os.path.dirname(
                os.path.abspath(inspect.getfile(inspect.currentframe()))
            )
        ),
        "data",
        "example_project",
        "DATA",
        "EARTHQUAKES",
        "GCMT_event_TURKEY_Mag_5.9_2011-5-19-20-15.h5",
    )
    with pyasdf.ASDFDataSet(file_1) as ds:
        cat = ds.events
    with pyasdf.ASDFDataSet(file_2) as ds:
        cat += ds.events[0]

    # Modify it to trigger all problems.
    temp = io.BytesIO()
    cat.write(temp, format="quakeml")
    temp.seek(0, 0)
    temp = temp.read()
    pattern = re.compile(r"<depth>.*?</depth>", re.DOTALL)
    temp = re.sub(pattern, "<depth></depth>", temp.decode())
    temp = re.sub(r"<type>.*?</type>", "<type></type>", temp)
    with open(os.path.join(tmpdir, "random.xml"), "wb") as fh:
        fh.write(temp.encode())

    with pyasdf.ASDFDataSet(os.path.join(tmpdir, "random.h5")) as ds:
        ds.add_quakeml(os.path.join(tmpdir, "random.xml"))

    comm = Communicator()
    comm.project = mock.MagicMock()
    comm.project.read_only_caches = False
    comm.project.paths = {"cache": tmpdir, "root": tmpdir}
    EventsComponent(tmpdir, comm, "events")

    event = comm.events.get("random")
    assert "contains no depth" in str(recwarn.pop(LASIFWarning).message)
    assert "Magnitude has no specified type" in str(
        recwarn.pop(LASIFWarning).message
    )

    # Assert the default values it will then take.
    assert event["depth_in_km"] == 0.0
    assert event["magnitude_type"] == "Mw"