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)
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
def forward_modelling(tensor_info, data_type, default_dirs, option='Solucion.txt', max_slip=200): """Routine for forward modelling. :param tensor_info: dictionary with moment tensor properties :param data_type: set with data types to be used in modelling :param option: string with location of input file with kinematic model to use :param max_slip: maximum slip in case of checkerboard test :param default_dirs: dictionary with default directories to be used :type default_dirs: dict :type tensor_info: dict :type data_type: set, optional :type option: string, optional :type max_slip: float, optional """ tensor.write_tensor(tensor_info) if not os.path.isdir('logs'): os.mkdir('logs') if not os.path.isdir('plots'): os.mkdir('plots') len_stk = 5 if not option == 'point_source' else 8 len_dip = 5 if not option == 'point_source' else 1 segments_data = json.load(open('segments_data.json')) # # Get input model # model = load_ffm_model(segments_data, option=option, max_slip=max_slip, len_stk=len_stk, len_dip=len_dip) if not os.path.isfile('velmodel_data.json'): raise FileNotFoundError(errno.ENOENT, os.strerror(errno.ENOENT), 'velmodel_data.json') velmodel = json.load(open('velmodel_data.json')) min_vel, max_vel = __ask_velrange() logger = ml.create_log('forward_model', os.path.join('logs', 'forward_model.log')) logger.info('Write input files') segments, rise_time, point_sources = pl_mng.__read_planes_info() shear = pf.shear_modulous(point_sources, velmodel=velmodel) dx = segments_data[0]['delta_x'] dy = segments_data[0]['delta_y'] slip = model['slip'] zipped = zip(slip, shear) moment_sub = [dx * dy * slip_seg * shear_seg\ for slip_seg, shear_seg in zipped] moment = np.sum( [np.sum(moment_seg.flatten()) for moment_seg in moment_sub]) moment = 10**10 * moment writing_inputs(tensor_info, data_type, segments_data, min_vel, max_vel, moment_mag=moment, forward_model=model) inversion(tensor_info, data_type, default_dirs, logger, forward=True) logger.info('Plot data in folder {}'.format(os.getcwd())) execute_plot(tensor_info, data_type, segments_data, default_dirs, velmodel=velmodel) ml.close_log(logger)
used_data = used_data + ['tele_body'] if args.tele else used_data used_data = used_data + ['surf_tele'] if args.surface else used_data default_dirs = mng.default_dirs() if args.gcmt_tensor: cmt_file = args.gcmt_tensor tensor_info = tensor.get_tensor(cmt_file=cmt_file) else: tensor_info = tensor.get_tensor() segments, rise_time, point_sources = pl_mng.__read_planes_info() if args.ffm_solution: solution = get_outputs.read_solution_static_format(segments) if not os.path.isfile('velmodel_data.json'): vel_model = mv.select_velmodel(tensor_info, default_dirs) else: vel_model = json.load(open('velmodel_data.json')) shear = pf.shear_modulous(point_sources, velmodel=vel_model) plot_ffm_sol(tensor_info, segments, point_sources, shear, solution, vel_model, default_dirs) traces_info, stations_gps = [None, None] if args.gps: names, lats, lons, observed, synthetic, error\ = get_outputs.retrieve_gps() stations_gps = zip(names, lats, lons, observed, synthetic, error) if args.strong: traces_info = json.load(open('strong_motion_waves.json')) if args.strong or args.gps: solution = get_outputs.read_solution_static_format(segments) _PlotMap(tensor_info, segments, point_sources, solution, default_dirs, files_str=traces_info, stations_gps=stations_gps) input_model = load_ffm_model.load_ffm_model(option='Fault.time')