def test_read_nshmp_grid_xml():
    file = os.path.join(datdir, 'USGS_grid.xml')
    sub = read_nshmp_grid_xml(file)

    # Test Settings
    mfds = sub['Settings']['DefaultMfds']
    srcp = sub['Settings']['SourceProperties']
    assert [m['a'] for m in mfds] == ['0.0', '0.0', '0.0', '0.0']
    assert [m['b'] for m in mfds] == ['1.0', '1.0', '1.0', '1.0']
    assert [m['dMag'] for m in mfds] == ['0.1', '0.1', '0.1', '0.1']
    assert [m['mMax'] for m in mfds] == ['6.45', '6.95', '7.45', '7.95']
    assert [m['mMin'] for m in mfds] == ['4.75', '4.75', '4.75', '4.75']
    assert [m['type'] for m in mfds] == ['GR', 'GR', 'GR', 'GR']
    assert [m['weight'] for m in mfds] == ['0.2', '0.5', '0.2', '0.1']
    assert srcp['focalMechMap'] == '[STRIKE_SLIP:1.0,NORMAL:0.0,REVERSE:0.0]'
    assert srcp['magDepthMap'] == '[10.0::[5.0:1.0]]'
    assert srcp['maxDepth'] == '22.0'
    assert srcp['ruptureScaling'] == 'NSHM_SOMERVILLE'
    assert srcp['strike'] == 'NaN'

    # Test nodes
    nodes = sub['Nodes']
    z = np.array([n['dep'] for n in nodes])
    ztarget = np.array(
        [0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.])
    np.testing.assert_allclose(z, ztarget)

    lat = np.array([n['lat'] for n in nodes])
    lattarget = np.array([
        34.6, 34.6, 34.6, 34.6, 34.6, 34.6, 34.7, 34.7, 34.7, 34.7, 34.7, 34.7,
        34.7, 39.5
    ])
    np.testing.assert_allclose(lat, lattarget)

    lon = np.array([n['lon'] for n in nodes])
    lontarget = np.array([
        -109.8, -109.7, -109.6, -109.5, -109.4, -109.3, -110., -109.9, -109.8,
        -109.7, -109.6, -109.5, -109.4, -110.7
    ])
    np.testing.assert_allclose(lon, lontarget)

    a = np.array([float(n['a']) for n in nodes])
    atarget = np.array([
        0.06516867, 0.06632903, 0.06741378, 0.06840201, 0.0692732, 0.07000767,
        0.06527287, 0.06659666, 0.06788012, 0.06910356, 0.07024578, 0.07128526,
        0.07220085, 0.27957186
    ])
    np.testing.assert_allclose(lon, lontarget)

    t = np.array([n['type'] for n in nodes])
    ttarget = [
        'GR', 'GR', 'GR', 'GR', 'GR', 'GR', 'GR', 'GR', 'GR', 'GR', 'GR', 'GR',
        'GR', 'GR'
    ]
    assert all(t == ttarget)
def test_read_nshmp_grid_xml():
    file = os.path.join(datdir, 'USGS_grid.xml')
    sub = read_nshmp_grid_xml(file)

    # Test Settings
    mfds = sub['Settings']['DefaultMfds']
    srcp = sub['Settings']['SourceProperties']
    assert [m['a'] for m in mfds] == ['0.0', '0.0', '0.0', '0.0']
    assert [m['b'] for m in mfds] == ['1.0', '1.0', '1.0', '1.0']
    assert [m['dMag'] for m in mfds] == ['0.1', '0.1', '0.1', '0.1']
    assert [m['mMax'] for m in mfds] == ['6.45', '6.95', '7.45', '7.95']
    assert [m['mMin'] for m in mfds] == ['4.75', '4.75', '4.75', '4.75']
    assert [m['type'] for m in mfds] == ['GR', 'GR', 'GR', 'GR']
    assert [m['weight'] for m in mfds] == ['0.2', '0.5', '0.2', '0.1']
    assert srcp['focalMechMap'] == '[STRIKE_SLIP:1.0,NORMAL:0.0,REVERSE:0.0]'
    assert srcp['magDepthMap'] == '[10.0::[5.0:1.0]]'
    assert srcp['maxDepth'] == '22.0'
    assert srcp['ruptureScaling'] == 'NSHM_SOMERVILLE'
    assert srcp['strike'] == 'NaN'

    # Test nodes
    nodes = sub['Nodes']
    z = np.array([n['dep'] for n in nodes])
    ztarget = np.array(
      [ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.])
    np.testing.assert_allclose(z, ztarget)

    lat = np.array([n['lat'] for n in nodes])
    lattarget = np.array(
      [ 34.6,  34.6,  34.6,  34.6,  34.6,  34.6,  34.7,  34.7,  34.7,
        34.7,  34.7,  34.7,  34.7,  39.5])
    np.testing.assert_allclose(lat, lattarget)

    lon = np.array([n['lon'] for n in nodes])
    lontarget = np.array(
      [-109.8, -109.7, -109.6, -109.5, -109.4, -109.3, -110. , -109.9,
       -109.8, -109.7, -109.6, -109.5, -109.4, -110.7])
    np.testing.assert_allclose(lon, lontarget)

    a = np.array([float(n['a']) for n in nodes])
    atarget = np.array(
      [ 0.06516867,  0.06632903,  0.06741378,  0.06840201,  0.0692732 ,
        0.07000767,  0.06527287,  0.06659666,  0.06788012,  0.06910356,
        0.07024578,  0.07128526,  0.07220085,  0.27957186])
    np.testing.assert_allclose(lon, lontarget)

    t = np.array([n['type'] for n in nodes])
    ttarget = ['GR', 'GR', 'GR', 'GR', 'GR', 'GR', 'GR', 'GR', 'GR', 'GR', 'GR',
       'GR', 'GR', 'GR']
    assert all(t == ttarget)
def test_read_nshmp_rlme_xml():
    file = os.path.join(datdir, 'Charlevoix Seismic Zone.xml')
    sub = read_nshmp_grid_xml(file)

    # Test Settings
    mfds = sub['Settings']['DefaultMfds']
    srcp = sub['Settings']['SourceProperties']
    assert [m['floats'] for m in mfds] == ['false', 'false', 'false', 'false']
    assert [m['m'] for m in mfds] == ['6.75', '7.0', '7.25', '7.5']
    assert [m['rate'] for m in mfds] == ['0.0', '0.0', '0.0', '0.0']
    assert [m['type']
            for m in mfds] == ['SINGLE', 'SINGLE', 'SINGLE', 'SINGLE']
    assert [m['weight'] for m in mfds] == ['0.2', '0.5', '0.2', '0.1']
    assert srcp['focalMechMap'] == '[STRIKE_SLIP:1.0,NORMAL:0.0,REVERSE:0.0]'
    assert srcp['magDepthMap'] == '[10.0::[5.0:1.0]]'
    assert srcp['maxDepth'] == '22.0'
    assert srcp['ruptureScaling'] == 'NSHM_POINT_WC94_LENGTH'
    assert srcp['strike'] == 'NaN'

    # Test Nodes
    nodes = sub['Nodes']
    z = np.array([n['dep'] for n in nodes])
    ztarget = np.array([
        0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
        0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
        0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
        0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.
    ])
    np.testing.assert_allclose(z, ztarget)

    lat = np.array([n['lat'] for n in nodes])
    lattarget = np.array([
        47., 47.1, 47.1, 47.1, 47.2, 47.2, 47.2, 47.2, 47.2, 47.3, 47.3, 47.3,
        47.3, 47.3, 47.3, 47.3, 47.4, 47.4, 47.4, 47.4, 47.4, 47.4, 47.4, 47.4,
        47.4, 47.5, 47.5, 47.5, 47.5, 47.5, 47.5, 47.5, 47.5, 47.5, 47.5, 47.6,
        47.6, 47.6, 47.6, 47.6, 47.6, 47.6, 47.6, 47.6, 47.6, 47.7, 47.7, 47.7,
        47.7, 47.7, 47.7, 47.7, 47.7, 47.7, 47.7, 47.8, 47.8, 47.8, 47.8, 47.8,
        47.8, 47.8, 47.8, 47.8, 47.9, 47.9, 47.9
    ])
    np.testing.assert_allclose(lat, lattarget)

    lon = np.array([n['lon'] for n in nodes])
    lontarget = np.array([
        -70.4, -70.5, -70.4, -70.3, -70.6, -70.5, -70.4, -70.3, -70.2, -70.7,
        -70.6, -70.5, -70.4, -70.3, -70.2, -70.1, -70.7, -70.6, -70.5, -70.4,
        -70.3, -70.2, -70.1, -70., -69.9, -70.7, -70.6, -70.5, -70.4, -70.3,
        -70.2, -70.1, -70., -69.9, -69.8, -70.6, -70.5, -70.4, -70.3, -70.2,
        -70.1, -70., -69.9, -69.8, -69.7, -70.5, -70.4, -70.3, -70.2, -70.1,
        -70., -69.9, -69.8, -69.7, -69.6, -70.4, -70.3, -70.2, -70.1, -70.,
        -69.9, -69.8, -69.7, -69.6, -70.1, -70., -69.9
    ])
    np.testing.assert_allclose(lon, lontarget)

    rate = np.array([float(n['rate']) for n in nodes])
    ratetarget = np.array([
        2.04776120e-05, 2.04776120e-05, 2.04776120e-05, 2.04776120e-05,
        2.04776120e-05, 2.04776120e-05, 2.04776120e-05, 2.04776120e-05,
        2.04776120e-05, 2.04776120e-05, 2.04776120e-05, 2.04776120e-05,
        2.04776120e-05, 2.04776120e-05, 2.04776120e-05, 2.04776120e-05,
        2.04776120e-05, 2.04776120e-05, 2.04776120e-05, 2.04776120e-05,
        2.04776120e-05, 2.04776120e-05, 2.04776120e-05, 2.04776120e-05,
        2.04776120e-05, 2.04776120e-05, 2.04776120e-05, 2.04776120e-05,
        2.04776120e-05, 2.04776120e-05, 2.04776120e-05, 2.04776120e-05,
        2.04776120e-05, 2.04776120e-05, 2.04776120e-05, 2.04776120e-05,
        2.04776120e-05, 2.04776120e-05, 2.04776120e-05, 2.04776120e-05,
        2.04776120e-05, 2.04776120e-05, 2.04776120e-05, 2.04776120e-05,
        2.04776120e-05, 2.04776120e-05, 2.04776120e-05, 2.04776120e-05,
        2.04776120e-05, 2.04776120e-05, 2.04776120e-05, 2.04776120e-05,
        2.04776120e-05, 2.04776120e-05, 2.04776120e-05, 2.04776120e-05,
        2.04776120e-05, 2.04776120e-05, 2.04776120e-05, 2.04776120e-05,
        2.04776120e-05, 2.04776120e-05, 2.04776120e-05, 2.04776120e-05,
        2.04776120e-05, 2.04776120e-05, 2.04776120e-05
    ])
    np.testing.assert_allclose(rate, ratetarget)
def test_read_nshmp_rlme_xml():
    file = os.path.join(datdir, 'Charlevoix Seismic Zone.xml')
    sub = read_nshmp_grid_xml(file)

    # Test Settings
    mfds = sub['Settings']['DefaultMfds']
    srcp = sub['Settings']['SourceProperties']
    assert [m['floats'] for m in mfds] == ['false', 'false', 'false', 'false']
    assert [m['m'] for m in mfds] == ['6.75', '7.0', '7.25', '7.5']
    assert [m['rate'] for m in mfds] == ['0.0', '0.0', '0.0', '0.0']
    assert [m['type'] for m in mfds] == ['SINGLE', 'SINGLE', 'SINGLE', 'SINGLE']
    assert [m['weight'] for m in mfds] == ['0.2', '0.5', '0.2', '0.1']
    assert srcp['focalMechMap'] == '[STRIKE_SLIP:1.0,NORMAL:0.0,REVERSE:0.0]'
    assert srcp['magDepthMap'] == '[10.0::[5.0:1.0]]'
    assert srcp['maxDepth'] == '22.0'
    assert srcp['ruptureScaling'] == 'NSHM_POINT_WC94_LENGTH'
    assert srcp['strike'] == 'NaN'

    # Test Nodes
    nodes = sub['Nodes']
    z = np.array([n['dep'] for n in nodes])
    ztarget = np.array(
      [ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,
        0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,
        0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,
        0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,
        0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,
        0.,  0.])
    np.testing.assert_allclose(z, ztarget)

    lat = np.array([n['lat'] for n in nodes])
    lattarget = np.array(
      [ 47. ,  47.1,  47.1,  47.1,  47.2,  47.2,  47.2,  47.2,  47.2,
        47.3,  47.3,  47.3,  47.3,  47.3,  47.3,  47.3,  47.4,  47.4,
        47.4,  47.4,  47.4,  47.4,  47.4,  47.4,  47.4,  47.5,  47.5,
        47.5,  47.5,  47.5,  47.5,  47.5,  47.5,  47.5,  47.5,  47.6,
        47.6,  47.6,  47.6,  47.6,  47.6,  47.6,  47.6,  47.6,  47.6,
        47.7,  47.7,  47.7,  47.7,  47.7,  47.7,  47.7,  47.7,  47.7,
        47.7,  47.8,  47.8,  47.8,  47.8,  47.8,  47.8,  47.8,  47.8,
        47.8,  47.9,  47.9,  47.9])
    np.testing.assert_allclose(lat, lattarget)

    lon = np.array([n['lon'] for n in nodes])
    lontarget = np.array(
      [-70.4, -70.5, -70.4, -70.3, -70.6, -70.5, -70.4, -70.3, -70.2,
       -70.7, -70.6, -70.5, -70.4, -70.3, -70.2, -70.1, -70.7, -70.6,
       -70.5, -70.4, -70.3, -70.2, -70.1, -70. , -69.9, -70.7, -70.6,
       -70.5, -70.4, -70.3, -70.2, -70.1, -70. , -69.9, -69.8, -70.6,
       -70.5, -70.4, -70.3, -70.2, -70.1, -70. , -69.9, -69.8, -69.7,
       -70.5, -70.4, -70.3, -70.2, -70.1, -70. , -69.9, -69.8, -69.7,
       -69.6, -70.4, -70.3, -70.2, -70.1, -70. , -69.9, -69.8, -69.7,
       -69.6, -70.1, -70. , -69.9])
    np.testing.assert_allclose(lon, lontarget)

    rate = np.array([float(n['rate']) for n in nodes])
    ratetarget = np.array(
      [  2.04776120e-05,   2.04776120e-05,   2.04776120e-05,
         2.04776120e-05,   2.04776120e-05,   2.04776120e-05,
         2.04776120e-05,   2.04776120e-05,   2.04776120e-05,
         2.04776120e-05,   2.04776120e-05,   2.04776120e-05,
         2.04776120e-05,   2.04776120e-05,   2.04776120e-05,
         2.04776120e-05,   2.04776120e-05,   2.04776120e-05,
         2.04776120e-05,   2.04776120e-05,   2.04776120e-05,
         2.04776120e-05,   2.04776120e-05,   2.04776120e-05,
         2.04776120e-05,   2.04776120e-05,   2.04776120e-05,
         2.04776120e-05,   2.04776120e-05,   2.04776120e-05,
         2.04776120e-05,   2.04776120e-05,   2.04776120e-05,
         2.04776120e-05,   2.04776120e-05,   2.04776120e-05,
         2.04776120e-05,   2.04776120e-05,   2.04776120e-05,
         2.04776120e-05,   2.04776120e-05,   2.04776120e-05,
         2.04776120e-05,   2.04776120e-05,   2.04776120e-05,
         2.04776120e-05,   2.04776120e-05,   2.04776120e-05,
         2.04776120e-05,   2.04776120e-05,   2.04776120e-05,
         2.04776120e-05,   2.04776120e-05,   2.04776120e-05,
         2.04776120e-05,   2.04776120e-05,   2.04776120e-05,
         2.04776120e-05,   2.04776120e-05,   2.04776120e-05,
         2.04776120e-05,   2.04776120e-05,   2.04776120e-05,
         2.04776120e-05,   2.04776120e-05,   2.04776120e-05,
         2.04776120e-05])
    np.testing.assert_allclose(rate, ratetarget)