Exemple #1
0
def write_coulomb(path, srf, proj, scut=0):
    """
    Write Coulomb input file.

    path: fine name on disk
    srf: (meta, data) SRF dictionaries
    proj: PyProj map projection instance
    scut: slip-rate below which values are not output
    """
    import numpy as np
    from cst import coord

    # slip components
    meta, data = srf
    s1, s2 = data['slip1'], data['slip2']
    s = np.sin(math.pi / 180.0 * data['rake'])
    c = np.cos(math.pi / 180.0 * data['rake'])
    r1 = -c * s1 + s * s2
    r2 = s * s1 + c * s2

    # coordinates
    x, y, z = data['lon'], data['lat'], data['dep']
    rot = coord.rotation(x, y, proj)[1]
    x, y = proj(x, y)
    x *= 0.001
    y *= 0.001
    z *= 0.001
    delta = 0.0005 * meta['plane'][0]['delta'][0]
    dx = delta * np.sin(math.pi / 180.0 * (data['stk'] + rot))
    dy = delta * np.cos(math.pi / 180.0 * (data['stk'] + rot))
    dz = delta * np.sin(math.pi / 180.0 * data['dip'])
    x1, x2 = x - dx, x + dx
    y1, y2 = y - dy, y + dy
    z1, z2 = z - dz, z + dz

    # source file
    i = (s1 ** 2 + s2 ** 2) > (np.sign(scut) * scut**2)
    c = np.array([
        x1[i], y1[i],
        x2[i], y2[i],
        r1[i], r2[i],
        data['dip'][i],
        z1[i], z2[i]
    ]).T
    with open(path + 'source.inp', 'w') as fh:
        fh.write(coulomb_header.format(**meta))
        np.savetxt(fh, c, coulomb_fmt)
        fh.write(coulomb_footer)

    # receiver file
    s1.fill(0.0)
    c = np.array([x1, y1, x2, y2, s1, s1, data['dip'], z1, z2]).T
    with open(path + 'receiver.inp', 'w') as fh:
        fh.write(coulomb_header.format(**meta))
        np.savetxt(fh, c, coulomb_fmt)
        fh.write(coulomb_footer)
    return
Exemple #2
0
def write_awp(
    fh, srf, t, mu, lam=0.0, delta=1.0, proj=None, binary=True, interp='linear'
):
    """
    Write ODC-AWP moment rate input file.

    fh: file handle
    srf: (meta, data) SRF dictionaries
    t: array of time values
    mu, lam: elastic moduli
    delta: grid step size, single value or [dx, dy, dz]
    proj: Function to project lon/lat to logical model coordinates
    binary: If true, write AWP binary format, otherwise text format.
    interp: interpolation method, linear or cubic
    """
    import numpy as np
    from cst import coord

    fh = open_(fh)

    # parameters
    meta, data = srf
    if not isinstance(delta, (tuple, list)):
        delta = delta, delta, delta
    x = data['lon']
    y = data['lat']
    z = data['dep']
    dt = data['dt']
    t0 = data['t0']
    stk = data['stk']
    dip = data['dip']
    rake = data['rake']
    area = data['area']

    # coordinates
    if proj:
        rot = coord.rotation(x, y, proj)[1]
        x, y = proj(x, y)
    else:
        rot = 0.0
    jj = (x / delta[0] + 1.5).astype('i')
    kk = (y / delta[1] + 1.5).astype('i')
    ll = (z / delta[2] + 1.5).astype('i')
    del(x, y, z)

    # moment tensor components
    s1, s2, n = coord.slip_vectors(stk + rot, dip, rake)
    m1 = mu * area * coord.potency_tensor(n, s1) * 2.0
    m2 = mu * area * coord.potency_tensor(n, s2) * 2.0
    m3 = lam * area * coord.potency_tensor(n, n) * 2.0
    del(s1, s2, n)

    # write file
    s = np.zeros_like
    i1 = 0
    i2 = 0
    i3 = 0
    for i in range(dt.size):
        n1 = data['nt1'][i]
        n2 = data['nt2'][i]
        n3 = data['nt3'][i]
        s1 = data['sv1'][i1:i1+n1]
        s2 = data['sv2'][i2:i2+n2]
        s3 = data['sv3'][i3:i3+n3]
        t1 = t0[i], t0[i] + dt[i] * (n1 - 1)
        t2 = t0[i], t0[i] + dt[i] * (n2 - 1)
        t3 = t0[i], t0[i] + dt[i] * (n3 - 1)
        s1 = interp.interp1(t1, s1, t, s(t), interp, bound=True)
        s2 = interp.interp1(t2, s2, t, s(t), interp, bound=True)
        s3 = interp.interp1(t3, s3, t, s(t), interp, bound=True)
        ii = np.array([[jj[i], kk[i], ll[i]]], 'i')
        mm = np.array([
            m1[0, 0, i] * s1 + m2[0, 0, i] * s2 + m3[0, 0, i] * s3,
            m1[0, 1, i] * s1 + m2[0, 1, i] * s2 + m3[0, 1, i] * s3,
            m1[0, 2, i] * s1 + m2[0, 2, i] * s2 + m3[0, 2, i] * s3,
            m1[1, 1, i] * s1 + m2[1, 1, i] * s2 + m3[1, 1, i] * s3,
            m1[1, 0, i] * s1 + m2[1, 0, i] * s2 + m3[1, 0, i] * s3,
            m1[1, 2, i] * s1 + m2[1, 2, i] * s2 + m3[1, 2, i] * s3,
        ])
        if binary:
            mm.astype('f').tofile(fh)
        else:
            np.savetxt(fh, ii, '%d')
            np.savetxt(fh, mm.T, '%14.6e')
        i1 += n1
        i2 += n2
        i3 += n3
    return
Exemple #3
0
def write_sord(path, srf, delta=(1, 1, 1), proj=None, dbytes=4):
    """
    Write SORD potency tensor input files.

    path: file name root
    srf: (meta, data) SRF dictionaries
    delta: grid step size (dx, dy, dz)
    proj: function to project lon/lat to logical model coordinates
    dbytes: 4 or 8
    """
    from cst import coord

    # setup
    meta, data = srf
    i_ = 'i%s' % dbytes
    f_ = 'f%s' % dbytes

    # time
    i1 = data['nt1'] > 0
    i2 = data['nt2'] > 0
    i3 = data['nt3'] > 0
    f1 = open(path + 'nt.bin', 'wb')
    f2 = open(path + 'dt.bin', 'wb')
    f3 = open(path + 't0.bin', 'wb')
    with f1, f2, f3:
        data['nt1'][i1].astype(i_).tofile(f1)
        data['nt2'][i2].astype(i_).tofile(f1)
        data['nt3'][i3].astype(i_).tofile(f1)
        for i in i1, i2, i3:
            data['dt'][i].astype(f_).tofile(f2)
            data['t0'][i].astype(f_).tofile(f3)

    # coordinates
    x = data['lon']
    y = data['lat']
    z = data['dep']
    if proj:
        rot = coord.rotation(x, y, proj)[1]
        x, y = proj(x, y)
    else:
        rot = 0.0
    x /= delta[0]
    y /= delta[1]
    z /= delta[2]
    f1 = open(path + 'xi1.bin', 'wb')
    f2 = open(path + 'xi2.bin', 'wb')
    f3 = open(path + 'xi3.bin', 'wb')
    with f1, f2, f3:
        for i in i1, i2, i3:
            x[i].astype(f_).tofile(f1)
            y[i].astype(f_).tofile(f2)
            z[i].astype(f_).tofile(f3)
    del(x, y, z)

    # fault local coordinate system
    s1, s2, n = coord.slip_vectors(
        data['stk'] + rot, data['dip'], data['rake']
    )
    p1 = data['area'] * coord.potency_tensor(n, s1)
    p2 = data['area'] * coord.potency_tensor(n, s2)
    p3 = data['area'] * coord.potency_tensor(n, n)
    del(s1, s2, n)

    # tensor components
    f11 = open(path + 'w11.bin', 'wb')
    f22 = open(path + 'w23.bin', 'wb')
    f33 = open(path + 'w33.bin', 'wb')
    with f11, f22, f33:
        for p, i in (p1, i1), (p2, i2), (p3, i3):
            p[0, 0, i].astype(f_).tofile(f11)
            p[0, 1, i].astype(f_).tofile(f22)
            p[0, 2, i].astype(f_).tofile(f33)
    f23 = open(path + 'w23.bin', 'wb')
    f31 = open(path + 'w31.bin', 'wb')
    f12 = open(path + 'w12.bin', 'wb')
    with f23, f31, f12:
        for p, i in (p1, i1), (p2, i2), (p3, i3):
            p[1, 0, i].astype(f_).tofile(f23)
            p[1, 1, i].astype(f_).tofile(f31)
            p[1, 2, i].astype(f_).tofile(f12)
    del(p1, p2, p3)

    # time history
    i1 = 0
    i2 = 0
    i3 = 0
    with open(path + 'history.bin', 'wb') as fh:
        n = meta['nsource']
        for i in range(n):
            n = data['nt1'][i]
            s = data['sv1'][i1:i1+n].cumsum() * data['dt'][i]
            s.astype(f_).tofile(fh)
            i1 += n
        for i in range(n):
            n = data['nt2'][i]
            s = data['sv2'][i2:i2+n].cumsum() * data['dt'][i]
            s.astype(f_).tofile(fh)
            i2 += n
        for i in range(n):
            n = data['nt3'][i]
            s = data['sv3'][i3:i3+n].cumsum() * data['dt'][i]
            s.astype(f_).tofile(fh)
            i3 += n
    return