Beispiel #1
0
def execute_plot(tensor_info,
                 data_type,
                 segments_data,
                 default_dirs,
                 velmodel=None,
                 plot_input=False):
    """We plot modelling results
    
    :param tensor_info: dictionary with moment tensor properties
    :param data_type: set with data types to be used in modelling
    :param plot_input: choose whether to plot initial kinematic model as well
    :param default_dirs: dictionary with default directories to be used
    :param velmodel: dictionary with velocity model
    :type velmodel: dict, optional
    :type default_dirs: dict
    :type tensor_info: dict
    :type data_type: set
    :type plot_input: bool, optional
    """
    print('Plot results')
    segments = segments_data['segments']
    rise_time = segments_data['rise_time']
    solution = get_outputs.read_solution_static_format(segments)
    if not velmodel:
        velmodel = mv.select_velmodel(tensor_info, default_dirs)
    point_sources = pf.point_sources_param(segments, tensor_info, rise_time)
    shear = pf.shear_modulous(point_sources, velmodel=velmodel)
    plot.plot_ffm_sol(tensor_info, segments_data, point_sources, shear,
                      solution, velmodel, default_dirs)
    plot.plot_misfit(data_type)
    # plot.plot_beachballs(tensor_info, segments, data_type)
    traces_info, stations_gps = [None, None]
    if 'strong_motion' in data_type:
        traces_info = json.load(open('strong_motion_waves.json'))
    if 'gps' in data_type:
        names, lats, lons, observed, synthetic, error = get_outputs.retrieve_gps(
        )
        stations_gps = zip(names, lats, lons, observed, synthetic, error)
    if 'strong_motion' in data_type or 'gps' in data_type:
        plot._PlotMap(tensor_info,
                      segments,
                      point_sources,
                      solution,
                      default_dirs,
                      files_str=traces_info,
                      stations_gps=stations_gps)
    if plot_input:
        input_model = load_ffm_model(segments_data, option='Fault.time')
        plot._PlotSlipDist_Compare(segments, point_sources, input_model,
                                   solution)
        plot._PlotComparisonMap(tensor_info, segments, point_sources,
                                input_model, solution)
Beispiel #2
0
def plane_for_chen(tensor_info, segments_data, min_vel, max_vel, velmodel):
    """Code to create files Fault.time, Fault.pos and Niu_model

    :param tensor_info: dictionary with moment tensor information
    :param segments: dictionary with information of the fault segments
    :param rise_time: dictionary with rise time function information
    :param min_vel: minimum rupture velocity to be used
    :param max_vel: maximum rupture velocity to be used
    :param velmodel: dictionary with velocity model information
    :type tensor_info: dict
    :type segments: dict
    :type rise_time: dict
    :type velmodel: dict
    :type min_vel: float
    :type max_vel: float
    """
    segments = segments_data['segments']
    rise_time = segments_data['rise_time']
    delta_strike = segments[0]['delta_strike']
    delta_dip = segments[0]['delta_dip']
    rupture_vel = segments[0]['rupture_vel']
    subfaults = {'delta_strike': delta_strike, 'delta_dip': delta_dip}
    subfaults2 = pf._point_sources_def(rise_time, rupture_vel, subfaults)
    strike_ps = subfaults2['strike_ps']
    dip_ps = subfaults2['dip_ps']
    t1 = rise_time['min_rise']
    t2 = rise_time['delta_rise']
    windows = rise_time['windows']

    hyp_stk = segments[0]['hyp_stk']
    hyp_dip = segments[0]['hyp_dip']
    delta_strike = segments[0]['delta_strike']
    delta_dip = segments[0]['delta_dip']

    depth = tensor_info['depth']
    point_sources = pf.point_sources_param(segments, tensor_info, rise_time)
    shear = pf.shear_modulous(point_sources, velmodel=velmodel)

    disp_or_vel = 0
    string = '{} {} {} {} {}\n'

    with open('Fault.time', 'w') as outfile:
        outfile.write('{} {} {} 10\n'.format(hyp_stk, hyp_dip, depth))
        outfile.write('{} {} {} {} {} {} {} {} {}\n'.format(
            len(segments), delta_strike, delta_dip, strike_ps, dip_ps, min_vel,
            max_vel, -100, 100))
        outfile.write('{} {} {} {} {}\n'.format(t1, t2, windows, rupture_vel,
                                                disp_or_vel))
        for i_segment, segment in enumerate(segments):
            dip = segment['dip']
            strike = segment['strike']
            rake = segment['rake']
            n_stk = segment['stk_subfaults']
            n_dip = segment['dip_subfaults']
            outfile.write('{} {} {}\n'.format(i_segment + 1, dip, strike))
            outfile.write('{} {} 0\n'.format(n_stk, n_dip))
            for i in range(n_dip):
                for j in range(n_stk):
                    slip = 300 if j == hyp_stk - 1 and i == hyp_dip - 1 else 0
                    outfile.write(string.format(slip, rake, 0, t1, t1))

    with open('Fault.pos', 'w') as outfile:
        for i_segment, (ps_seg, segment)\
        in enumerate(zip(point_sources, segments)):
            dip = segment['dip']
            strike = segment['strike']
            n_stk = segment['stk_subfaults']
            n_dip = segment['dip_subfaults']
            outfile.write('{} {} {}\n'.format(i_segment + 1, dip, strike))
            for j1 in range(n_dip):
                for i1 in range(n_stk):
                    for j2 in range(dip_ps):
                        for i2 in range(strike_ps):
                            outfile.write(
                                '{} {} {} {} {} {} {}\n'.format(*ps_seg[j1, i1,
                                                                        j2,
                                                                        i2]))

    with open('Niu_model', 'w') as outfile:
        outfile.write('{}\n'.format(len(shear)))
        for i_segment, (shear_seg, segment) in enumerate(zip(shear, segments)):
            n_stk = segment['stk_subfaults']
            n_dip = segment['dip_subfaults']
            outfile.write('{} {} {}\n'.format(i_segment + 1, n_stk, n_dip))
            ratio = len(shear_seg[0, :]) // 5
            format_str = ('{} ' * 5 + '\n') * ratio
            remain = len(shear_seg[0, :]) % 5
            format_str = format_str if remain == 0\
                else format_str + ('{} ' * remain + '\n')
            for i in range(n_dip):
                outfile.write(format_str.format(*shear_seg[i, :]))
    return
Beispiel #3
0
def forward_model(tensor_info, segments_data, model, vel0, vel1):
    """Rewrite input file Fault.time with input model

    :param tensor_info: dictionary with moment tensor information
    :param segments: dictionary with information of the fault segments
    :param rise_time: dictionary with rise time function information
    :param model: dictionary with properties of the input kinematic model
    :param vel0: minimum rupture velocity to be used
    :param vel1: maximum rupture velocity to be used
    :type tensor_info: dict
    :type segments: dict
    :type rise_time: dict
    :type model: dict
    :type vel0: float
    :type vel1: float
    """
    slip_segs = model['slip']
    rake_segs = model['rake']
    trup_segs = model['trup']
    tris_segs = model['trise']
    tfall_segs = model['tfall']

    segments = segments_data['segments']
    rise_time = segments_data['rise_time']
    hyp_stk = segments[0]['hyp_stk']
    hyp_dip = segments[0]['hyp_dip']
    delta_strike = segments[0]['delta_strike']
    delta_dip = segments[0]['delta_dip']
    rupture_vel = segments[0]['rupture_vel']
    subfaults = {'delta_strike': delta_strike, 'delta_dip': delta_dip}

    subfaults2 = pf._point_sources_def(rise_time, rupture_vel, subfaults)
    strike_ps = subfaults2['strike_ps']
    dip_ps = subfaults2['dip_ps']
    t1 = rise_time['min_rise']
    t2 = rise_time['delta_rise']
    windows = rise_time['windows']

    depth = tensor_info['depth']

    disp_or_vel = 0
    string = '{} {} {} {} {}\n'

    point_sources0 = pf.point_sources_param(segments, tensor_info, rise_time)
    ny = int(dip_ps / 2)
    nx = int(strike_ps / 2)
    times = [
        point_sources[:, :, ny, nx, 4] for point_sources in point_sources0
    ]
    trup_segs2 = [rupt_seg - time for time, rupt_seg in zip(times, trup_segs)]

    zipped = zip(segments, slip_segs, rake_segs, trup_segs2, tris_segs,
                 tfall_segs)
    with open('Fault.time', 'w') as outfile:
        outfile.write('{} {} {} 10\n'.format(hyp_stk, hyp_dip, depth))
        outfile.write('{} {} {} {} {} {} {} {} {}\n'.format(
            len(segments), delta_strike, delta_dip, strike_ps, dip_ps, vel0,
            vel1, -100, 100))
        outfile.write('{} {} {} {} {}\n'.format(t1, t2, windows, rupture_vel,
                                                disp_or_vel))
        for i_segment, (segment, slip_seg, rake_seg, trup_seg, tris_seg, tfall_seg)\
        in enumerate(zipped):
            dip = segment['dip']
            strike = segment['strike']
            n_stk = segment['stk_subfaults']
            n_dip = segment['dip_subfaults']
            outfile.write('{} {} {}\n'.format(i_segment + 1, dip, strike))
            outfile.write('{} {} 0\n'.format(n_stk, n_dip))
            for i in range(n_dip):
                for j in range(n_stk):
                    outfile.write(
                        string.format(slip_seg[i, j], rake_seg[i, j],
                                      trup_seg[i, j], tris_seg[i, j],
                                      tfall_seg[i, j]))
    return
Beispiel #4
0
def static_to_fsp(tensor_info, segments_data, used_data, vel_model, solution):
    """Write FSP file with the solution of FFM modelling from file Solucion.txt

    :param tensor_info: dictionary with moment tensor information
    :param segments_data: list of dictionaries with properties of fault segments
    :param rise_time: dictionary with rise time information
    :param point_sources: properties of point sources of the fault plane
    :param used_data: list with data types to be used in modelling
    :param solution: dictionary with output kinematic model properties
    :param vel_model: dictionary with velocity model properties
    :type tensor_info: dict
    :type segments_data: list
    :type rise_time: dict
    :type point_sources: array
    :type used_data: list
    :type solution: dict
    :type vel_model: dict
    """
    locator = flinnengdahl.FlinnEngdahl()
    segments = segments_data['segments']
    rise_time = segments_data['rise_time']
    point_sources = pf.point_sources_param(segments, tensor_info, rise_time)
    slips = solution['slip']
    rakes = solution['rake']
    trup = solution['rupture_time']
    trise = solution['trise']
    tfall = solution['tfall']
    latitudes = solution['lat']
    longitudes = solution['lon']
    depths = solution['depth']
    moment = solution['moment']
    total_moment = np.sum(np.array(moment).flatten())

    string = ' ---------------------------------- '
    event_lat = tensor_info['lat']
    event_lon = tensor_info['lon']
    depth = tensor_info['depth']
    moment_mag = 2 * np.log10(total_moment) / 3 - 10.7
    location = locator.get_region(event_lon, event_lat)
    date = tensor_info['datetime']
    tag = date
    now = datetime.datetime.now()

    plane_info = segments[0]
    stk_subfaults, dip_subfaults, delta_strike, delta_dip, hyp_stk, hyp_dip\
        = pl_mng.__unpack_plane_data(plane_info)
    hyp_stk = (hyp_stk + 0.5) * delta_strike
    length = delta_strike * stk_subfaults
    hyp_dip = (hyp_dip + 0.5) * delta_dip
    width = delta_dip * dip_subfaults
    strike = plane_info['strike']
    dip = plane_info['dip']
    rake = plane_info['rake']
    ps_depths = [ps_segment[:, :, :, :, 2] for ps_segment in point_sources]
    min_depth = min([np.min(ps_depth.flatten()) for ps_depth in ps_depths])
    ps_distances = [ps_segment[:, :, 0, 0, 3] for ps_segment in point_sources]
    ps_times = [ps_segment[:, :, 0, 0, 4] for ps_segment in point_sources]
    delta_time = [rupt_seg - ps_time for ps_time, rupt_seg\
        in zip(ps_times, trup)]
    total_subfaults = [segment['stk_subfaults'] * segment['dip_subfaults']\
        for segment in segments]
    total_subfaults = np.sum(np.array(total_subfaults))
    avg_time = [np.sum(dt_seg.flatten()) for dt_seg in delta_time]
    avg_time = np.sum(np.array(avg_time).flatten()) / total_subfaults
    avg_vel = [ps_dist / rupt_seg for ps_dist, rupt_seg\
        in zip(ps_distances, trup)]
    avg_vel = [np.sum(avg_vel_seg.flatten()) for avg_vel_seg in avg_vel]
    avg_vel = np.sum(np.array(avg_vel).flatten()) / total_subfaults
    min_rise = rise_time['min_rise']
    delta_rise = rise_time['delta_rise']
    windows = rise_time['windows']

    quantity_strong = 0
    if 'strong_motion' in used_data:
        strong_data = json.load(open('strong_motion_waves.json'))
        quantity_strong = len(strong_data)
    quantity_gps = 0
    if 'gps' in used_data:
        gps_data = json.load(open('static_data.json'))
        quantity_gps = len(gps_data)
    quantity_tele = 0
    if 'tele_body' in used_data:
        tele_data = json.load(open('tele_waves.json'))
        quantity_tele = len(tele_data)
    quantity_surf = 0
    if 'surf_tele' in used_data:
        surf_data = json.load(open('surf_waves.json'))
        quantity_surf = len(surf_data)

    n_layers = len(vel_model['dens'])
    p_vel = [float(v) for v in vel_model['p_vel']]
    s_vel = [float(v) for v in vel_model['s_vel']]
    dens = [float(v) for v in vel_model['dens']]
    thick = [float(v) for v in vel_model['thick']]
    qp = [float(v) for v in vel_model['qa']]
    qs = [float(v) for v in vel_model['qb']]
    depth2 = np.cumsum(np.array(thick))
    depth2 = depth2 - depth2[0]
    zipped = zip(depth2, p_vel, s_vel, dens, qp, qs)
    zipped2 = zip(segments, point_sources, latitudes, longitudes, depths,
                  slips, rakes, trup, trise, tfall, moment)

    string2 = '{0:9.4f} {1:9.4f} {2:9.4f} {3:9.4f} {4:9.4f} '\
        '{5:8.4f} {6:9.4f} {7:8.4f} {8:9.4f}  {9:8.2e}\n'
    string_fun = lambda a, b, c, d, e, f, g, h, i, j:\
        string2.format(a, b, c, d, e, f, g, h, i, j)

    with open('fsp_sol_file', 'w') as outfile:
        outfile.write('%{}FINITE-SOURCE RUPTURE '\
            'MODEL{}\n%\n'.format(string, string))
        outfile.write('% Event : {} {} CSN\n'.format(location, date))
        outfile.write('% EventTAG: {}\n%\n'.format(tag))
        outfile.write('% Loc  : LAT = {}  LON = {}  DEP = {}\n'.format(
            event_lat, event_lon, depth))
        outfile.write(
            '% Size : LEN = {} km  WID = {} km  Mw = {}  Mo = {} Nm\n'.format(
                length, width, moment_mag, total_moment * 10**-7))
        outfile.write(
            '% Mech : STRK = {} DIP = {} RAKE = {}  Htop = {} '\
            'km\n'.format(strike, dip, rake, min_depth))
        outfile.write(
            '% Rupt : HypX = {} km  Hypz = {} km  avTr = {:5.2f} s  avVr = {:5.2f} '\
            'km/s\n%\n'.format(hyp_stk, hyp_dip, avg_time, avg_vel))
        outfile.write('% {} inversion-related '\
            'parameters{}\n'.format(string, string))
        outfile.write('%\n% Invs : Nx = {}  Nz = {} Fmin = {} Hz  '\
            'Fmax = {} Hz\n'.format(stk_subfaults, dip_subfaults, 0.01, 0.125))
        outfile.write('% Invs : Dx = {} km  Dz = {} '\
            'km\n'.format(delta_strike, delta_dip))
        outfile.write('% Invs : Ntw = {}  Nsg = {}     '\
            '(# of time-windows,# of fault segments)'\
            '\n'.format(windows, len(segments)))
        outfile.write('% Invs : LEN = {} s SHF = {} s    '\
            '(time-window length and time-shift)\n'.format(
                min_rise + delta_rise, delta_rise))
        outfile.write('% SVF  : Asymetriccosine    '\
            '(type of slip-velocity function used)\n')
        outfile.write('%\n% Data : SGM TELE TRIL LEVEL GPS INSAR SURF OTHER\n')
        outfile.write('% Data : {} {} 0 0 {} 0 {} '\
            '0\n'.format(quantity_strong, quantity_tele, quantity_gps,
                         quantity_surf))
        outfile.write('% Data : {} {} 0 0 {} 0 {} '\
            '0\n'.format(quantity_strong, quantity_tele, quantity_gps,
                         quantity_surf))
        outfile.write('% Data : {} {} 0 0 {} 0 {} '\
            '0\n'.format(quantity_strong, quantity_tele, quantity_gps,
                         quantity_surf))
        outfile.write('%\n%{}{}\n'.format(string, string))
        outfile.write('%\n% VELOCITY-DENSITY STRUCTURE\n')
        outfile.write('% No. of layers = {}\n'.format(n_layers))
        outfile.write('%\n% DEPTH P_VEL S_VEL DENS QP QS\n')
        outfile.write('% [km] [km/s] [km/s] [g/cm^3]\n')
        for dep, pv, sv, den, qpp, qss in zipped:
            outfile.write('% {} {} {} {}  {}  '\
            '{}\n'.format(dep, pv, sv, den, qpp, qss))
        outfile.write('%\n%{}{}\n'.format(string, string))
        outfile.write('% {}/{}/{} created by [email protected].'\
        'cl\n'.format(now.day, now.month, now.year))
        outfile.write('%\n% SOURCE MODEL PARAMETERS\n')
        if len(segments) == 1:
            outfile.write('% Nsbfs = {} subfaults\n'.format(
                stk_subfaults, dip_subfaults))
        outfile.write('% X,Y,Z coordinates in km; SLIP in m\n')
        outfile.write('% if applicable: RAKE in deg, RISE in s, TRUP in s, '\
            'slip in each TW in m\n')
        outfile.write('%\n% Coordinates are given for center of each '\
            'subfault or segment: |\'|\n')
        outfile.write('% Origin of local coordinate system at epicenter: '\
            'X (EW) = 0, Y (NS) = 0\n')
        if len(segments) == 1:
            outfile.write('% LAT LON X==EW Y==NS Z SLIP RAKE TRUP RISE ')
            outfile.write('SF_MOMENT\n%{}{}\n'.format(string, string))
            lat_fault = latitudes[0].flatten()
            lon_fault = longitudes[0].flatten()
            depth_fault = depths[0].flatten()
            slip_fault = slips[0].flatten()
            rake_fault = rakes[0].flatten()
            trup_fault = trup[0].flatten()
            trise_fault = trise[0].flatten()
            tfall_fault = tfall[0].flatten()
            moment_fault = moment[0].flatten()
            zipped3 = zip(lat_fault, lon_fault, depth_fault, slip_fault,
                          rake_fault, trup_fault, trise_fault, tfall_fault,
                          moment_fault)
            for line in zipped3:
                lat, lon, dep, slip, rake, t_rup, t_ris, t_fal, moment = line
                north_south = (float(lat) - event_lat) * 111.11
                east_west = (float(lon) - event_lon) * 111.11
                moment = moment * 10**-7
                slip = slip * 10**-2
                outfile.write(
                    string_fun(lat, lon, east_west, north_south, dep, slip,
                               rake, t_rup, t_ris + t_fal, moment))
        else:
            outfile.write('%{}{}\n'.format(string, string))
            outfile.write('%{} MULTISEGMENT MODEL {}\n'.format(string, string))
            outfile.write('%{}{}\n'.format(string, string))
            start_line = 10
            for i_segment, fault_segment_data in enumerate(zipped2):
                segment = fault_segment_data[0].flatten()
                ps_seg = fault_segment_data[1].flatten()
                lat_fault = fault_segment_data[2].flatten()
                lon_fault = fault_segment_data[3].flatten()
                depth_fault = fault_segment_data[4].flatten()
                slip_fault = fault_segment_data[5].flatten()
                rake_fault = fault_segment_data[6].flatten()
                trup_fault = fault_segment_data[7].flatten()
                trise_fault = fault_segment_data[8].flatten()
                tfall_fault = fault_segment_data[9].flatten()
                moment_fault = fault_segment_data[10].flatten()
                strike = segment['strike']
                dip = segment['dip']
                stk_subfaults, dip_subfaults, delta_strike, delta_dip, hyp_stk, hyp_dip\
                    = pl_mng.__unpack_plane_data(plane_info)
                length = stk_subfaults * delta_strike
                width = dip_subfaults * delta_dip
                min_dep = np.min(ps_seg[:, :, :, :, 2].flatten())
                lat0 = ps_seg[-1, -1, -1, -1, 0]
                lon0 = ps_seg[-1, -1, -1, -1, 1]
                hyp_stk = (hyp_stk + 0.5) * delta_strike
                hyp_dip = (hyp_dip + 0.5) * delta_dip
                n_subfaults = stk_subfaults * dip_subfaults
                outfile.write('% SEGMENT # {}: STRIKE = {} deg DIP = {} '\
                    'deg\n'.format(i_segment + 1, strike, dip))
                outfile.write('% LEN = {} km WID = {} km\n'.format(
                    length, width))
                outfile.write(
                    '% depth to top: Z2top = {:6.2f} km\n'.format(min_dep))
                outfile.write('% coordinates of top-center\n')
                outfile.write('% LAT = {}, LON = {}\n'.format(lat0, lon0))
                outfile.write('% hypocenter on SEG # {} : along-strike (X) '\
                    '= {}, down-dip (Z) = {}\n'.format(
                    i_segment + 1, hyp_stk, hyp_dip))
                outfile.write('% Nsbfs = {} subfaults\n'.format(n_subfaults))
                outfile.write('% LAT LON X==EW Y==NS Z SLIP RAKE TRUP RISE ')
                outfile.write('SF_MOMENT\n%{}{}\n'.format(string, string))
                zipped3 = zip(lat_fault, lon_fault, depth_fault, slip_fault,
                              rake_fault, trup_fault, trise_fault, tfall_fault,
                              moment_fault)
                for line in zipped:
                    lat, lon, dep, slip, rake, t_rup, t_ris, t_fal,\
                    moment = line
                    north_south = (float(lat) - event_lat) * 111.11
                    east_west = (float(lon) - event_lon) * 111.11
                    moment = moment * 10**-7
                    slip = slip * 10**-2
                    outfile.write(
                        string_fun(lat, lon, east_west, north_south, dep, slip,
                                   rake, t_rup, t_ris + t_fal, moment))
                    start_line = start_line + 1
                start_line = start_line + 9