Example #1
0
def test_no_windows_will_remove_existing_file(tmpdir):
    """
    A window collection that has no windows will attempt to remove its own
    file if it has no windows without emitting a warnings.

    This grants a natural way to get rid of empty windows.
    """
    tmpdir = str(tmpdir)
    filename = os.path.join(tmpdir, "random.xml")

    wc = WindowCollection(filename,
                          event_name="SomeEvent",
                          channel_id="AA.BB.CC.DD",
                          synthetics_tag="Iteration_A")
    wc.add_window(starttime=UTCDateTime(2012, 1, 1),
                  endtime=UTCDateTime(2012, 1, 1, 0, 1),
                  weight=0.5,
                  taper="cosine",
                  taper_percentage=0.08)
    wc.write()

    assert os.listdir(tmpdir) == [os.path.basename(filename)]
    wc.windows = []
    wc.write()
    assert os.listdir(tmpdir) == []
Example #2
0
def test_no_windows_will_remove_existing_file(tmpdir):
    """
    A window collection that has no windows will attempt to remove its own
    file if it has no windows without emitting a warnings.

    This grants a natural way to get rid of empty windows.
    """
    tmpdir = str(tmpdir)
    filename = os.path.join(tmpdir, "random.xml")

    wc = WindowCollection(filename, event_name="SomeEvent",
                          channel_id="AA.BB.CC.DD",
                          synthetics_tag="Iteration_A")
    wc.add_window(starttime=UTCDateTime(2012, 1, 1),
                  endtime=UTCDateTime(2012, 1, 1, 0, 1),
                  weight=0.5, taper="cosine", taper_percentage=0.08)
    wc.write()

    assert os.listdir(tmpdir) == [os.path.basename(filename)]
    wc.windows = []
    wc.write()
    assert os.listdir(tmpdir) == []
Example #3
0
def test_delete_windows(tmpdir):
    """
    Tests the deletion of windows.
    """
    tmpdir = str(tmpdir)
    filename = os.path.join(tmpdir, "random.xml")

    wc = WindowCollection(filename,
                          event_name="SomeEvent",
                          channel_id="AA.BB.CC.DD",
                          synthetics_tag="Iteration_A")
    wc.add_window(starttime=UTCDateTime(2012, 1, 1),
                  endtime=UTCDateTime(2012, 1, 1, 0, 1),
                  weight=0.5,
                  taper="cosine",
                  taper_percentage=0.08)
    # The last three windows differ by a second each.
    wc.add_window(starttime=UTCDateTime(2013, 1, 1),
                  endtime=UTCDateTime(2013, 1, 1, 0, 1),
                  weight=0.5,
                  taper="cosine",
                  taper_percentage=0.08)
    wc.add_window(starttime=UTCDateTime(2013, 1, 1),
                  endtime=UTCDateTime(2013, 1, 1, 0, 1, 1),
                  weight=0.5,
                  taper="cosine",
                  taper_percentage=0.08)
    wc.add_window(starttime=UTCDateTime(2013, 1, 1),
                  endtime=UTCDateTime(2013, 1, 1, 0, 1, 2),
                  weight=0.5,
                  taper="cosine",
                  taper_percentage=0.08)

    assert len(wc) == 4

    wc.delete_window(starttime=UTCDateTime(2012, 1, 1),
                     endtime=UTCDateTime(2012, 1, 1, 0, 1),
                     tolerance=0.0)
    assert len(wc) == 3

    # 2 percent of one minute is 1.2 seconds.
    wc.delete_window(starttime=UTCDateTime(2013, 1, 1),
                     endtime=UTCDateTime(2013, 1, 1, 0, 1),
                     tolerance=0.02)
    assert len(wc) == 1
Example #4
0
def test_delete_windows(tmpdir):
    """
    Tests the deletion of windows.
    """
    tmpdir = str(tmpdir)
    filename = os.path.join(tmpdir, "random.xml")

    wc = WindowCollection(filename, event_name="SomeEvent",
                          channel_id="AA.BB.CC.DD",
                          synthetics_tag="Iteration_A")
    wc.add_window(starttime=UTCDateTime(2012, 1, 1),
                  endtime=UTCDateTime(2012, 1, 1, 0, 1),
                  weight=0.5, taper="cosine", taper_percentage=0.08,
                  misfit_type="some misfit", misfit_value=1E-5)
    # The last three windows differ by a second each.
    wc.add_window(starttime=UTCDateTime(2013, 1, 1),
                  endtime=UTCDateTime(2013, 1, 1, 0, 1),
                  weight=0.5, taper="cosine", taper_percentage=0.08,
                  misfit_type="some misfit", misfit_value=1E-5)
    wc.add_window(starttime=UTCDateTime(2013, 1, 1),
                  endtime=UTCDateTime(2013, 1, 1, 0, 1, 1),
                  weight=0.5, taper="cosine", taper_percentage=0.08,
                  misfit_type="some misfit", misfit_value=1E-5)
    wc.add_window(starttime=UTCDateTime(2013, 1, 1),
                  endtime=UTCDateTime(2013, 1, 1, 0, 1, 2),
                  weight=0.5, taper="cosine", taper_percentage=0.08,
                  misfit_type="some misfit", misfit_value=1E-5)

    assert len(wc) == 4

    wc.delete_window(starttime=UTCDateTime(2012, 1, 1),
                     endtime=UTCDateTime(2012, 1, 1, 0, 1),
                     tolerance=0.0)
    assert len(wc) == 3

    # 2 percent of one minute is 1.2 seconds.
    wc.delete_window(starttime=UTCDateTime(2013, 1, 1),
                     endtime=UTCDateTime(2013, 1, 1, 0, 1),
                     tolerance=0.02)
    assert len(wc) == 1
Example #5
0
def test_window_collection_i_o(tmpdir):
    """
    Reading and writing windows.
    """
    tmpdir = str(tmpdir)
    filename = os.path.join(tmpdir, "random.xml")

    wc = WindowCollection(filename,
                          event_name="SomeEvent",
                          channel_id="AA.BB.CC.DD",
                          synthetics_tag="Iteration_A")
    wc.add_window(starttime=UTCDateTime(2012, 1, 1),
                  endtime=UTCDateTime(2012, 1, 1, 0, 1),
                  weight=0.5,
                  taper="cosine",
                  taper_percentage=0.08,
                  misfit_type="some misfit")
    wc.write()

    # Hardcode an example to be sure the format is correct.
    reference = ("<?xml version='1.0' encoding='utf-8'?>\n"
                 "<MisfitWindow>\n"
                 "  <Event>SomeEvent</Event>\n"
                 "  <ChannelID>AA.BB.CC.DD</ChannelID>\n"
                 "  <SyntheticsTag>Iteration_A</SyntheticsTag>\n"
                 "  <Window>\n"
                 "    <Starttime>2012-01-01T00:00:00.000000Z</Starttime>\n"
                 "    <Endtime>2012-01-01T00:01:00.000000Z</Endtime>\n"
                 "    <Weight>0.5</Weight>\n"
                 "    <Taper>cosine</Taper>\n"
                 "    <TaperPercentage>0.08</TaperPercentage>\n"
                 "    <Misfit>some misfit</Misfit>\n"
                 "  </Window>\n"
                 "</MisfitWindow>")
    with open(filename, "rb") as fh:
        actual = fh.read().strip()
    assert reference == actual

    # Read it once again and make sure it is identical.
    wc2 = WindowCollection(filename)
    assert wc == wc2

    # Add a new window and test the same again.
    wc.add_window(starttime=UTCDateTime(2013, 1, 1),
                  endtime=UTCDateTime(2013, 1, 1, 0, 1),
                  weight=0.8,
                  taper="hanning",
                  taper_percentage=0.1,
                  misfit_type="other misfit")
    wc.write()
    reference = ("<?xml version='1.0' encoding='utf-8'?>\n"
                 "<MisfitWindow>\n"
                 "  <Event>SomeEvent</Event>\n"
                 "  <ChannelID>AA.BB.CC.DD</ChannelID>\n"
                 "  <SyntheticsTag>Iteration_A</SyntheticsTag>\n"
                 "  <Window>\n"
                 "    <Starttime>2012-01-01T00:00:00.000000Z</Starttime>\n"
                 "    <Endtime>2012-01-01T00:01:00.000000Z</Endtime>\n"
                 "    <Weight>0.5</Weight>\n"
                 "    <Taper>cosine</Taper>\n"
                 "    <TaperPercentage>0.08</TaperPercentage>\n"
                 "    <Misfit>some misfit</Misfit>\n"
                 "  </Window>\n"
                 "  <Window>\n"
                 "    <Starttime>2013-01-01T00:00:00.000000Z</Starttime>\n"
                 "    <Endtime>2013-01-01T00:01:00.000000Z</Endtime>\n"
                 "    <Weight>0.8</Weight>\n"
                 "    <Taper>hanning</Taper>\n"
                 "    <TaperPercentage>0.1</TaperPercentage>\n"
                 "    <Misfit>other misfit</Misfit>\n"
                 "  </Window>\n"
                 "</MisfitWindow>")
    with open(filename, "rb") as fh:
        actual = fh.read().strip()
    assert reference == actual

    wc3 = WindowCollection(filename)
    assert wc == wc3
Example #6
0
def test_window_collection_i_o(tmpdir):
    """
    Reading and writing windows.
    """
    tmpdir = str(tmpdir)
    filename = os.path.join(tmpdir, "random.xml")

    wc = WindowCollection(filename, event_name="SomeEvent",
                          channel_id="AA.BB.CC.DD",
                          synthetics_tag="Iteration_A")
    wc.add_window(starttime=UTCDateTime(2012, 1, 1),
                  endtime=UTCDateTime(2012, 1, 1, 0, 1),
                  weight=0.5, taper="cosine", taper_percentage=0.08,
                  misfit_type="some misfit", misfit_value=1E-5)
    wc.write()

    # Hardcode an example to be sure the format is correct.
    reference = (
        "<?xml version='1.0' encoding='utf-8'?>\n"
        "<MisfitWindow>\n"
        "  <Event>SomeEvent</Event>\n"
        "  <ChannelID>AA.BB.CC.DD</ChannelID>\n"
        "  <SyntheticsTag>Iteration_A</SyntheticsTag>\n"
        "  <Window>\n"
        "    <Starttime>2012-01-01T00:00:00.000000Z</Starttime>\n"
        "    <Endtime>2012-01-01T00:01:00.000000Z</Endtime>\n"
        "    <Weight>0.5</Weight>\n"
        "    <Taper>cosine</Taper>\n"
        "    <TaperPercentage>0.08</TaperPercentage>\n"
        "    <Misfit>some misfit</Misfit>\n"
        "    <MisfitValue>1e-05</MisfitValue>\n"
        "  </Window>\n"
        "</MisfitWindow>")
    with open(filename, "rb") as fh:
        actual = fh.read().strip()
    assert reference == actual

    # Read it once again and make sure it is identical.
    wc2 = WindowCollection(filename)
    assert wc == wc2

    # Add a new window and test the same again.
    wc.add_window(starttime=UTCDateTime(2013, 1, 1),
                  endtime=UTCDateTime(2013, 1, 1, 0, 1),
                  weight=0.8, taper="hanning", taper_percentage=0.1,
                  misfit_type="other misfit", misfit_value=0.0)
    wc.write()
    reference = (
        "<?xml version='1.0' encoding='utf-8'?>\n"
        "<MisfitWindow>\n"
        "  <Event>SomeEvent</Event>\n"
        "  <ChannelID>AA.BB.CC.DD</ChannelID>\n"
        "  <SyntheticsTag>Iteration_A</SyntheticsTag>\n"
        "  <Window>\n"
        "    <Starttime>2012-01-01T00:00:00.000000Z</Starttime>\n"
        "    <Endtime>2012-01-01T00:01:00.000000Z</Endtime>\n"
        "    <Weight>0.5</Weight>\n"
        "    <Taper>cosine</Taper>\n"
        "    <TaperPercentage>0.08</TaperPercentage>\n"
        "    <Misfit>some misfit</Misfit>\n"
        "    <MisfitValue>1e-05</MisfitValue>\n"
        "  </Window>\n"
        "  <Window>\n"
        "    <Starttime>2013-01-01T00:00:00.000000Z</Starttime>\n"
        "    <Endtime>2013-01-01T00:01:00.000000Z</Endtime>\n"
        "    <Weight>0.8</Weight>\n"
        "    <Taper>hanning</Taper>\n"
        "    <TaperPercentage>0.1</TaperPercentage>\n"
        "    <Misfit>other misfit</Misfit>\n"
        "    <MisfitValue>0.0</MisfitValue>\n"
        "  </Window>\n"
        "</MisfitWindow>")
    with open(filename, "rb") as fh:
        actual = fh.read().strip()
    assert reference == actual

    wc3 = WindowCollection(filename)
    assert wc == wc3