Exemple #1
0
def test_model_2():

    #
    # This is a small grid with station data and dyfi data (should succeed)
    #
    install_path, data_path = get_config_paths()
    event_path = os.path.join(data_path, 'nc72282711', 'current')
    set_files(
        event_path, {
            'event.xml': 'event.xml',
            'stationlist.xml.small': 'stationlist.xml',
            'dyfi_dat.xml.small': 'dyfi_dat.xml',
            'model.conf': 'model.conf',
            'boat_fault.txt': 'boat_fault.txt'
        })
    assemble = AssembleModule('nc72282711', comment='Test comment.')
    assemble.execute()
    model = ModelModule('nc72282711')
    model.execute()
    #
    # Since we've done this, we might as well run plotregr, too
    #
    plotregr = PlotRegr('nc72282711')
    plotregr.execute()
    plotregr.writeContents()
    clear_files(event_path)
Exemple #2
0
def test_masking():
    install_path, data_path = get_config_paths()
    event_path = os.path.join(data_path, 'masking_test', 'current')
    set_files(
        event_path, {
            'event.xml': 'event.xml',
            'model.conf': 'model.conf',
            'au_continental_shelf.geojson': 'au_continental_shelf.geojson',
        })
    assemble = AssembleModule('masking_test', comment='Test comment.')
    assemble.execute()
    model = ModelModule('masking_test')
    model.execute()
    clear_files(event_path)
    hdf_file = os.path.join(event_path, 'products', 'shake_result.hdf')
    oc = ShakeMapOutputContainer.load(hdf_file)
    sa3 = oc.getIMTGrids('SA(3.0)', 'GREATER_OF_TWO_HORIZONTAL')['mean']
    removed = np.isnan(sa3).astype(int)
    assert (removed[240, 240] == 1)
    assert (removed[260, 240] == 0)
    np.testing.assert_equal(
        removed[::100, ::100],
        [[1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 0, 1],
         [0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0]])
    oc.close()
Exemple #3
0
def test_directivity():

    #
    # Turned on directivity in model.conf
    #
    install_path, data_path = get_config_paths()
    event_path = os.path.join(data_path, 'directivity_test', 'current')
    set_files(
        event_path, {
            'event.xml': 'event.xml',
            'model.conf': 'model.conf',
            'dir_fault.txt': 'dir_fault.txt'
        })
    assemble = AssembleModule('directivity_test', comment='Test comment.')
    assemble.execute()
    model = ModelModule('directivity_test')
    model.execute()
    clear_files(event_path)
    hdf_file = os.path.join(event_path, 'products', 'shake_result.hdf')
    oc = ShakeMapOutputContainer.load(hdf_file)
    sa3 = np.exp(
        oc.getIMTGrids('SA(3.0)', 'GREATER_OF_TWO_HORIZONTAL')['mean'])
    # np.testing.assert_allclose(np.max(sa3), 1.15864273)
    np.testing.assert_allclose(np.max(sa3), 1.1567265149442174)
    # np.testing.assert_allclose(np.min(sa3), 0.9278920)
    np.testing.assert_allclose(np.min(sa3), 0.88508818541678)
    oc.close()
Exemple #4
0
def test_select():

    installpath, datapath = get_config_paths()

    # Process a non-existent event (should fail)
    smod = SelectModule('not_an_event')
    with pytest.raises(NotADirectoryError):
        smod.execute()

    # Normal event (should succeed)
    event_path = os.path.join(datapath, 'nc72282711', 'current')
    set_files(event_path, {'event.xml': 'event.xml'})
    conf_file = os.path.join(datapath, 'nc72282711', 'current',
                             'model_select.conf')
    smod = SelectModule('nc72282711')
    smod.execute()
    failed = False
    if not os.path.isfile(conf_file):
        failed = True
    clear_files(event_path)
    if failed:
        assert False

    # Subduction event (not over slab)
    conf_file = os.path.join(datapath, 'usp0004bxs', 'current',
                             'model_select.conf')
    if os.path.isfile(conf_file):
        os.remove(conf_file)
    try:
        smod = SelectModule('usp0004bxs')
        smod.execute()
    finally:
        if not os.path.isfile(conf_file):
            print('select failed!')
            assert False
        else:
            os.remove(conf_file)

    # Northridge, with moment tensor file
    conf_file = os.path.join(datapath, 'northridge2', 'current',
                             'model_select.conf')
    if os.path.isfile(conf_file):
        os.remove(conf_file)
    try:
        smod = SelectModule('northridge2')
        smod.execute()
    finally:
        if not os.path.isfile(conf_file):
            print('select failed!')
            assert False
        else:
            os.remove(conf_file)
Exemple #5
0
def test_model_4():

    #
    # Run with no data and no fault, and use the default extent.
    #
    install_path, data_path = get_config_paths()
    event_path = os.path.join(data_path, 'nc72282711', 'current')
    set_files(event_path, {'event.xml': 'event.xml',
                           'model.conf': 'model.conf'})
    assemble = AssembleModule('nc72282711',
                              comment='Test comment.')
    assemble.execute()
    model = ModelModule('nc72282711')
    model.execute()
    clear_files(event_path)
Exemple #6
0
def test_model_3():

    #
    # This is a small grid with DYFI data only (should succeed)
    #
    install_path, data_path = get_config_paths()
    event_path = os.path.join(data_path, 'nc72282711', 'current')
    set_files(event_path, {'event.xml': 'event.xml',
                           'dyfi_dat.xml.small': 'dyfi_dat.xml',
                           'model.conf': 'model.conf',
                           'boat_fault.txt': 'boat_fault.txt'})
    assemble = AssembleModule('nc72282711', comment='Test comment.')
    assemble.execute()
    model = ModelModule('nc72282711')
    model.execute()
    clear_files(event_path)
Exemple #7
0
def test_assemble_augment():

    installpath, datapath = get_config_paths()

    # Process a non-existent event (should fail)
    amod = AssembleModule('not_an_event', comment='Test comment.')
    with pytest.raises(NotADirectoryError):
        amod.execute()

    # Process a non-existent event (should fail)
    augment = AugmentModule('not_an_event', comment='Test comment.')
    with pytest.raises(NotADirectoryError):
        augment.execute()

    # Would succeed but we remove event.xml (should fail)
    event_file = os.path.join(datapath, 'wenchuan', 'current', 'event.xml')
    os.rename(event_file, event_file + '_safe')
    try:
        amod = AssembleModule('wenchuan', comment='Test comment.')
        with pytest.raises(FileNotFoundError):
            amod.execute()
    finally:
        os.rename(event_file + '_safe', event_file)

    # Normal event (should succeed)
    data_file = os.path.join(datapath, 'wenchuan', 'current', 'shake_data.hdf')
    if os.path.isfile(data_file):
        os.remove(data_file)
    try:
        amod = AssembleModule('wenchuan', comment='Test comment.')
        amod.execute()
        #
        # Run a second time to exercise a different branch of the code
        #
        amod.execute()
    finally:
        pass

    # This would succeed, but we remove shake_data.hdf (should fail)
    hdf_file = os.path.join(datapath, 'wenchuan', 'current', 'shake_data.hdf')
    if os.path.isfile(hdf_file):
        os.rename(hdf_file, hdf_file + '_safe')
    try:
        augment = AugmentModule('wenchuan', comment='Test comment.')
        with pytest.raises(FileNotFoundError):
            augment.execute()
    finally:
        # Put shake_data.hdf back for the next test
        if os.path.isfile(hdf_file + '_safe'):
            os.rename(hdf_file + '_safe', hdf_file)

    # Normal event (should succeed)
    try:
        augment = AugmentModule('wenchuan', comment='Test comment.')
        augment.execute()
    finally:
        if os.path.isfile(data_file):
            os.remove(data_file)

    # Do an event with model.conf (not model_select.conf) and no zoneinfo
    # (should succeed)
    event_path = os.path.join(datapath, 'nc72282711', 'current')
    set_files(
        event_path, {
            'event.xml': 'event.xml',
            'stationlist.xml': 'stationlist.xml',
            'boat_fault.txt': 'boat_fault.txt',
            'model.conf': 'model.conf'
        })
    data_file = os.path.join(datapath, 'nc72282711', 'current',
                             'shake_data.hdf')
    if os.path.isfile(data_file):
        os.remove(data_file)
    try:
        assemble = AssembleModule('nc72282711', comment='Test comment.')
        assemble.execute()
        augment = AugmentModule('nc72282711', comment='Test comment.')
        augment.execute()
    finally:
        if os.path.isfile(data_file):
            os.remove(data_file)
        clear_files(event_path)

    # Try not having an event-specific config (should succeed)
    set_files(
        event_path, {
            'event.xml': 'event.xml',
            'stationlist.xml': 'stationlist.xml',
            'boat_fault.txt': 'boat_fault.txt'
        })
    try:
        amod = AssembleModule('nc72282711', comment='Test comment.')
        amod.execute()
        augment = AugmentModule('nc72282711', comment='Test comment.')
        augment.execute()
    finally:
        if os.path.isfile(data_file):
            os.remove(data_file)
        clear_files(event_path)

    # Do an event with DYFI data (should succeed)
    set_files(
        event_path, {
            'event.xml': 'event.xml',
            'stationlist.xml': 'stationlist.xml',
            'boat_fault.txt': 'boat_fault.txt',
            'dyfi_dat.xml': 'dyfi_dat.xml'
        })
    try:
        amod = AssembleModule('nc72282711', comment='Test comment.')
        amod.execute()
        augment = AugmentModule('nc72282711', comment='Test comment.')
        augment.execute()
    finally:
        if os.path.isfile(data_file):
            os.remove(data_file)
        clear_files(event_path)

    #
    # Try some bad config files
    #
    # Should fail validation
    set_files(event_path, {
        'event.xml': 'event.xml',
        'model_select.conf.bad0': 'model_select.conf'
    })
    try:
        amod = AssembleModule('nc72282711', comment='Test comment.')
        with pytest.raises(RuntimeError):
            amod.execute()
    finally:
        clear_files(event_path)

    # Should fail vs30 filename check
    set_files(event_path, {
        'event.xml': 'event.xml',
        'model_select.conf.bad1': 'model_select.conf'
    })
    try:
        amod = AssembleModule('nc72282711', comment='Test comment.')
        with pytest.raises(RuntimeError):
            amod.execute()
    finally:
        clear_files(event_path)

    # Should fail prediction locations filename check
    set_files(event_path, {
        'event.xml': 'event.xml',
        'model_select.conf.bad2': 'model_select.conf'
    })
    try:
        amod = AssembleModule('nc72282711', comment='Test comment.')
        with pytest.raises(FileNotFoundError):
            amod.execute()
    finally:
        clear_files(event_path)
    #
    # Make sure the location file substitutions work (should succeed)
    #
    data_file = os.path.join(datapath, 'northridge_points', 'current',
                             'shake_data.hdf')
    if os.path.isfile(data_file):
        os.remove(data_file)
    try:
        assemble = AssembleModule('northridge_points', comment='Test comment.')
        assemble.execute()
        augment = AugmentModule('northridge_points', comment='Test comment.')
        augment.execute()
    finally:
        if os.path.isfile(data_file):
            os.remove(data_file)

    #
    # Try some bad config files
    #
    try:
        set_files(event_path, {'event.xml': 'event.xml'})
        assemble = AssembleModule('nc72282711', comment='Test comment.')
        assemble.execute()
        # Should fail validation
        set_files(event_path, {'model_select.conf.bad0': 'model_select.conf'})
        augment = AugmentModule('nc72282711', comment='Test comment.')
        with pytest.raises(RuntimeError):
            augment.execute()
        clear_files(event_path)

        # Should fail vs30 filename check
        set_files(event_path, {'event.xml': 'event.xml'})
        assemble = AssembleModule('nc72282711', comment='Test comment.')
        assemble.execute()
        set_files(
            event_path, {
                'event.xml': 'event.xml',
                'model_select.conf.bad1': 'model_select.conf'
            })
        augment = AugmentModule('nc72282711', comment='Test comment.')
        with pytest.raises(RuntimeError):
            augment.execute()
        clear_files(event_path)

        # Should fail prediction locations filename check
        set_files(event_path, {'event.xml': 'event.xml'})
        assemble = AssembleModule('nc72282711', comment='Test comment.')
        assemble.execute()
        set_files(
            event_path, {
                'event.xml': 'event.xml',
                'model_select.conf.bad2': 'model_select.conf'
            })
        augment = AugmentModule('nc72282711', comment='Test comment.')
        with pytest.raises(FileNotFoundError):
            augment.execute()
    finally:
        clear_files(event_path)

    #
    # Switch originators (should succeed)
    #
    set_files(event_path, {
        'event.xml': 'event.xml',
        'model.conf.cz': 'model.conf'
    })
    try:
        assemble = AssembleModule('nc72282711', comment='Test comment.')
        assemble.execute()
        augment = AugmentModule('nc72282711', comment='Test comment.')
        augment.execute()
    finally:
        if os.path.isfile(data_file):
            os.remove(data_file)
        clear_files(event_path)