def test_weighting_two(tmpdir, cmtsource): dcon_two = construct_dcon_two() weight_config = DefaultWeightConfig(normalize_by_energy=False, normalize_by_category=False, comp_weight={ "Z": 1.0, "R": 1.0, "T": 1.0 }, love_dist_weight=1.0, pnl_dist_weight=1.0, rayleigh_dist_weight=1.0, azi_exp_idx=0.5) config = Config(6, dlocation=0.5, ddepth=0.5, dmoment=1.0e22, zero_trace=True, weight_data=True, station_correction=True, weight_config=weight_config) srcinv = Cmt3D(cmtsource, dcon_two, config) srcinv.source_inversion() srcinv.plot_new_synt_seismograms(str(tmpdir))
def test_cmt_bootstrap(cmtsource, tmpdir): dcon_two = construct_dcon_two() weight_config = DefaultWeightConfig(normalize_by_energy=False, normalize_by_category=False, comp_weight={ "Z": 1.0, "R": 1.0, "T": 1.0 }, love_dist_weight=1.0, pnl_dist_weight=1.0, rayleigh_dist_weight=1.0, azi_exp_idx=0.5) config = Config(9, dlocation=0.5, ddepth=0.5, dmoment=1.0e22, zero_trace=True, weight_data=True, station_correction=True, weight_config=weight_config, bootstrap=True, bootstrap_repeat=20, bootstrap_subset_ratio=0.4) srcinv = Cmt3D(cmtsource, dcon_two, config) srcinv.source_inversion() srcinv.plot_summary(str(tmpdir))
def setup_inversion(cmt): dcon_two = construct_dcon_two() weight_config = DefaultWeightConfig(normalize_by_energy=False, normalize_by_category=False, comp_weight={ "Z": 1.0, "R": 1.0, "T": 1.0 }, love_dist_weight=1.0, pnl_dist_weight=1.0, rayleigh_dist_weight=1.0, azi_exp_idx=0.5) config = Config(6, dlocation=0.5, ddepth=0.5, dmoment=1.0e22, zero_trace=True, weight_data=True, station_correction=True, weight_config=weight_config) srcinv = Cmt3D(cmt, dcon_two, config) srcinv.source_inversion() return srcinv
def invert(cmt_file_db, param_path): """Runs the actual inversion. :param cmt_file_db: :param param_path: :return: Nothing, inversion results are written to file. """ # Load Database Parameters databaseparam_path = os.path.join(param_path, "Database/DatabaseParameters.yml") DB_params = read_yaml_file(databaseparam_path) # Inversion Params inversionparam_path = os.path.join(param_path, "CMTInversion/InversionParams.yml") INV_params = read_yaml_file(inversionparam_path) # Weight Params weightparam_path = os.path.join(param_path, "CMTInversion/WeightParams.yml") weight_params = read_yaml_file(weightparam_path) # Get processing path from cmt_filename in database cmt_dir = os.path.dirname(os.path.abspath(cmt_file_db)) # Create cmt source: cmtsource = CMTSource.from_CMTSOLUTION_file(cmt_file_db) # Inversion dictionary directory inv_dict_dir = os.path.join(cmt_dir, "inversion", "inversion_dicts") # Inversion dictionaries inv_dict_files = glob.glob(os.path.join(inv_dict_dir, "inversion*")) # Inversion output directory inv_out_dir = os.path.join(cmt_dir, "inversion", "inversion_output", "cmt3d") # WRite start of inversion process logger.info(" ") logger.info("#######################################################") logger.info("# #") logger.info("# Starting CMT3D Inversion ... #") logger.info("# #") logger.info("#######################################################") logger.info(" ") # Creating Data container dcon = DataContainer(parlist=PARLIST[:DB_params["npar"]]) for _i, inv_dict_file in enumerate(inv_dict_files): # Get processing band bandstring1 = str(os.path.basename(inv_dict_file).split(".")[1]) if "surface" in bandstring1 or "body" in bandstring1: bandstring = bandstring1.split("#")[0] else: bandstring = bandstring1 band = [float(x) for x in bandstring.split("_")] logger.info(" ") logger.info(" " + 54 * "*") logger.info(" Getting data for inversion from period band:") logger.info(" Low: %d s || High: %d s" % tuple(band)) logger.info(" " + 54 * "*") logger.info(" ") # Load inversion file dictionary inv_dict = read_yaml_file(inv_dict_file) asdf_dict = inv_dict["asdf_dict"] window_file = inv_dict["window_file"] # Adding measurements # Print Inversion parameters: logger.info(" Adding measurements to data container:") logger.info(" _____________________________________________________") logger.info(" ") # Add measurements from ASDF file and windowfile logger.info(" Window file:") logger.info(" " + window_file) logger.info(" ") logger.info(" ASDF files:") for key, value in asdf_dict.items(): logger.info(" " + key + ": " + value) dcon.add_measurements_from_asdf(window_file, asdf_dict) logger.info(" _____________________________________________________") logger.info(" ... ") logger.info(" ") logger.info(" ... ") logger.info(" Setting up inversion classes .... ") logger.info(" " + 54 * "*") logger.info(" ... ") # Setting up weight config inv_weight_config = weight_params["weight_config"] weight_config = WeightConfig( normalize_by_energy=inv_weight_config["normalize_by_energy"], normalize_by_category=inv_weight_config["normalize_by_category"], azi_bins=inv_weight_config["azi_bins"], azi_exp_idx=inv_weight_config["azi_exp_idx"]) # Setting up general inversion config inv_params = INV_params["config"] cmt3d_config = Config(DB_params["npar"], dlocation=float(inv_params["dlocation"]), ddepth=float(inv_params["ddepth"]), dmoment=float(inv_params["dmoment"]), weight_data=bool(inv_params["weight_data"]), station_correction=bool( inv_params["station_correction"]), zero_trace=bool(inv_params["zero_trace"]), double_couple=bool(inv_params["double_couple"]), bootstrap=bool(inv_params["bootstrap"]), bootstrap_repeat=int(inv_params["bootstrap_repeat"]), weight_config=weight_config, taper_type=inv_params["taper_type"], damping=float(inv_params["damping"])) logger.info(" PyCMT3D is finding an improved CMTSOLUTION .... ") logger.info(" " + 54 * "*") logger.info(" ") logger.info(" ") # Invert for parameters cmt3d = Cmt3D(cmtsource, dcon, cmt3d_config) cmt3d.source_inversion() cmt3d.compute_new_syn() # Create inversion class # if bool(INV_params["gridsearch"]): # inv = Inversion(cmtsource, dcon, cmt3d_config, grad3d_config) # else: # inv = Inversion(cmtsource, dcon, cmt3d_config, mt_config=None) # Run inversion if bool(INV_params["statistics_plot"]): cmt3d.plot_stats_histogram(outputdir=inv_out_dir) # Plot results if bool(INV_params["summary_plot"]): cmt3d.plot_summary(inv_out_dir, figure_format="pdf") # # if bool(INV_params["statistics_plot"]): # # Plot Statistics for inversion # inv.G.plot_stats_histogram(outputdir=inv_out_dir) # if bool(INV_params["summary_json"]): cmt3d.write_summary_json(outputdir=inv_out_dir, mode="global") if bool(INV_params["write_new_cmt"]): cmt3d.write_new_cmtfile(outputdir=inv_out_dir) if bool(INV_params["write_new_synt"]): cmt3d.write_new_syn(outputdir=os.path.join(inv_out_dir, "new_synt"), file_format="asdf") if bool(INV_params["plot_new_synthetics"]): cmt3d.plot_new_synt_seismograms(outputdir=os.path.join( inv_out_dir, "waveform_plots"), figure_format="pdf")
def main(cmt_filename): # Define parameter directory param_path = os.path.join( os.path.dirname(os.path.dirname(os.path.abspath(__file__))), "params") # Load Database Parameters databaseparam_path = os.path.join(param_path, "Database/DatabaseParameters.yml") DB_params = smart_read_yaml(databaseparam_path, mpi_mode=is_mpi_env()) # Inversion Params inversionparam_path = os.path.join(param_path, "CMTInversion/InversionParams.yml") INV_params = smart_read_yaml(inversionparam_path, mpi_mode=is_mpi_env()) # Get processing path from cmt_filename in database cmt_dir = os.path.dirname(os.path.abspath(cmt_filename)) # Create cmt source: cmtsource = CMTSource.from_CMTSOLUTION_file(cmt_filename) # Window directory window_dir = os.path.join(cmt_dir, "window_data") # Inversion dictionary directory inv_dict_dir = os.path.join(cmt_dir, "inversion", "inversion_dicts") # Inversion dictionaries inv_dicts = glob.glob(os.path.join(inv_dict_dir, "*")) # Inversion output directory inv_out_dir = os.path.join(cmt_dir, "inversion", "inversion_output") if DB_params["verbose"]: print("\n#######################################################") print("# #") print("# Starting inversion ... #") print("# #") print("#######################################################\n") # Creating Data container dcon = DataContainer(parlist=PARLIST[:DB_params["npar"]]) for _i, inv_dict in enumerate(inv_dicts): # Get processing band bandstring = str(os.path.basename(inv_dict)).split(".")[1] band = [float(x) for x in bandstring.split("_")] if DB_params["verbose"]: print("\n") print(" " + 54 * "*") print(" Getting data for inversion from period band:") print(" Low: %d s || High: %d s" % tuple(band)) print(" " + 54 * "*" + "\n") # Load inversion file dictionary asdf_dict = smart_read_yaml(inv_dict, mpi_mode=is_mpi_env()) window_files = glob.glob( os.path.join(window_dir, "windows." + bandstring + "*[!stats].json")) # Adding measurements for _j, window_file in enumerate(window_files): # Print Inversion parameters: if DB_params["verbose"]: print(" Adding measurements to data container:") print( " _____________________________________________________\n" ) # Add measurements from ASDF file and windowfile # if _j == 0: if DB_params["verbose"]: print(" Window file:\n", " ", window_file) print("\n ASDF files:") for key, value in asdf_dict.items(): print(" ", key + ":", value) dcon.add_measurements_from_asdf(window_file, asdf_dict) # else: # if DB_params["verbose"]: # print(" Window file:\n", " ", window_file) # dcon.add_measurements_from_sac(window_file) if DB_params["verbose"]: print( " _____________________________________________________\n" ) print(" ... \n\n") if DB_params["verbose"]: print(" Inverting for a new moment tensor .... ") print(" " + 54 * "*" + "\n\n") # Setting up weight config weight_config = DefaultWeightConfig(normalize_by_energy=False, normalize_by_category=False, comp_weight={ "Z": 1.0, "R": 1.0, "T": 1.0 }, love_dist_weight=1.0, pnl_dist_weight=1.0, rayleigh_dist_weight=1.0, azi_exp_idx=0.5) # Setting up general inversion config config = Config(DB_params["npar"], dlocation=float(INV_params["config"]["dlocation"]), ddepth=float(INV_params["config"]["ddepth"]), dmoment=float(INV_params["config"]["dmoment"]), weight_data=True, station_correction=True, zero_trace=True, double_couple=False, bootstrap=True, bootstrap_repeat=100, weight_config=weight_config) srcinv = Cmt3D(cmtsource, dcon, config) srcinv.source_inversion() # plot result srcinv.plot_summary(inv_out_dir, figure_format="pdf") if DB_params["verbose"]: print(" DONE inversion for period band: %d - %d s.\n" % tuple(band)) if DB_params["verbose"]: print("\n#######################################################") print("# #") print("# Inversion DONE. #") print("# #") print("#######################################################\n")