def setUp(self): """ Copy needed files to run the test """ self.velmodel = "nr02-vs500.fk1d" self.srffile = "m5.89-0.20x0.20_s2379646.srf" self.stations = "one_stat.txt" self.vmodel_name = "LABasin500" self.sim_id = int(seqnum.get_seq_num()) self.install = InstallCfg() refdir = os.path.join(self.install.A_TEST_REF_DIR, "gp") indir = os.path.join(self.install.A_IN_DATA_DIR, str(self.sim_id)) tmpdir = os.path.join(self.install.A_TMP_DATA_DIR, str(self.sim_id)) outdir = os.path.join(self.install.A_OUT_DATA_DIR, str(self.sim_id)) logdir = os.path.join(self.install.A_OUT_LOG_DIR, str(self.sim_id)) # Create all directories bband_utils.mkdirs([indir, tmpdir, outdir, logdir], print_cmd=False) cmd = "cp %s %s" % (os.path.join(refdir, self.velmodel), indir) bband_utils.runprog(cmd, print_cmd=False) cmd = "cp %s %s" % (os.path.join(refdir, self.stations), indir) bband_utils.runprog(cmd, print_cmd=False) cmd = "cp %s %s" % (os.path.join(refdir, self.srffile), indir) bband_utils.runprog(cmd, print_cmd=False)
def setUp(self): """ Copy needed files to run the test """ self.velmodel = "genslip_nr_generic1d-gp01.vmod" self.srffile = "m5.89-0.20x0.20_s2379646.srf" self.stations = "one_stat.txt" self.vmodel_name = "LABasin" self.sim_id = int(seqnum.get_seq_num()) self.install = InstallCfg() self.jbsim_cfg = JbsimCfg(self.vmodel_name) indir = os.path.join(self.install.A_IN_DATA_DIR, str(self.sim_id)) tmpdir = os.path.join(self.install.A_TMP_DATA_DIR, str(self.sim_id)) outdir = os.path.join(self.install.A_OUT_DATA_DIR, str(self.sim_id)) logdir = os.path.join(self.install.A_OUT_LOG_DIR, str(self.sim_id)) # Create all directories bband_utils.mkdirs([indir, tmpdir, outdir, logdir], print_cmd=False) cmd = "cp %s/gp/%s %s/%d/." % (self.install.A_TEST_REF_DIR, self.velmodel, self.install.A_IN_DATA_DIR, self.sim_id) bband_utils.runprog(cmd, print_cmd=False) cmd = "cp %s/gp/%s %s/%d/." % (self.install.A_TEST_REF_DIR, self.stations, self.install.A_IN_DATA_DIR, self.sim_id) bband_utils.runprog(cmd, print_cmd=False) cmd = "cp %s/gp/%s %s/%d/." % (self.install.A_TEST_REF_DIR, self.srffile, self.install.A_IN_DATA_DIR, self.sim_id) bband_utils.runprog(cmd, print_cmd=False)
def setUp(self): self.install = InstallCfg() self.cfg = GenslipCfg() os.chdir(self.install.A_COMP_DIR) self.sim_id = int(seqnum.get_seq_num()) self.velmodel = "genslip_nr_generic1d-gp01.vmod" self.srcfile = "test_wh.src" self.outsrf = "%d_test_eq.srf" % self.sim_id indir = os.path.join(self.install.A_IN_DATA_DIR, str(self.sim_id)) tmpdir = os.path.join(self.install.A_TMP_DATA_DIR, str(self.sim_id)) outdir = os.path.join(self.install.A_OUT_DATA_DIR, str(self.sim_id)) logdir = os.path.join(self.install.A_OUT_LOG_DIR, str(self.sim_id)) # Create all directories bband_utils.mkdirs([indir, tmpdir, outdir, logdir], print_cmd=False) cmd = "cp %s/gp/%s %s/%d/." % (self.install.A_TEST_REF_DIR, self.velmodel, self.install.A_IN_DATA_DIR, self.sim_id) bband_utils.runprog(cmd, print_cmd=False) cmd = "cp %s/gp/%s %s/%d/." % (self.install.A_TEST_REF_DIR, self.srcfile, self.install.A_IN_DATA_DIR, self.sim_id) bband_utils.runprog(cmd, print_cmd=False)
def setUp(self): self.install = InstallCfg() self.match_cfg = MatchCfg() os.chdir(self.install.A_INSTALL_ROOT) self.stations = "test_stat.txt" self.vmodel_name = "LABasin500" self.sim_id = int(seqnum.get_seq_num()) self.freqs = ['lf', 'hf'] refdir = os.path.join(self.install.A_TEST_REF_DIR, "gp") indir = os.path.join(self.install.A_IN_DATA_DIR, str(self.sim_id)) tmpdir = os.path.join(self.install.A_TMP_DATA_DIR, str(self.sim_id)) outdir = os.path.join(self.install.A_OUT_DATA_DIR, str(self.sim_id)) logdir = os.path.join(self.install.A_OUT_LOG_DIR, str(self.sim_id)) # Create all directories bband_utils.mkdirs([indir, tmpdir, outdir, logdir], print_cmd=False) # Copy station list cmd = "cp %s %s" % (os.path.join(refdir, self.stations), indir) bband_utils.runprog(cmd) for i in range(1, 6): for freq in self.freqs: cmd = "cp %s %s" % (os.path.join( refdir, "s%02d-%s-site.bbp" % (i, freq)), os.path.join( tmpdir, "%d.s%02d-%s.acc.bbp" % (self.sim_id, i, freq))) bband_utils.runprog(cmd)
def setUp(self): """ Set up unit test """ self.install = InstallCfg() self.r_velocity = "labasin.vel" self.r_stations = "one_stat.txt" self.r_src = "test_wh_ucsb.src" self.r_srf = "test_ucsb.srf" self.sim_id = int(seqnum.get_seq_num()) a_indir = os.path.join(self.install.A_IN_DATA_DIR, str(self.sim_id)) a_tmpdir = os.path.join(self.install.A_TMP_DATA_DIR, str(self.sim_id)) a_outdir = os.path.join(self.install.A_OUT_DATA_DIR, str(self.sim_id)) a_logdir = os.path.join(self.install.A_OUT_LOG_DIR, str(self.sim_id)) # # Make sure output directories exist # bband_utils.mkdirs([a_indir, a_tmpdir, a_outdir, a_logdir], print_cmd=False) # Copy files a_refdir = os.path.join(self.install.A_TEST_REF_DIR, "ucsb") # Copy seismogram shutil.copy2(os.path.join(a_refdir, "s01.3comp"), a_tmpdir) # Copy other input files shutil.copy2(os.path.join(a_refdir, self.r_stations), a_indir) shutil.copy2(os.path.join(a_refdir, self.r_velocity), a_indir) shutil.copy2(os.path.join(a_refdir, self.r_src), a_indir) shutil.copy2(os.path.join(a_refdir, self.r_srf), a_indir) # Change directory to tmpdir os.chdir(a_tmpdir)
def setUp(self): self.install = InstallCfg() self.cfg = GenslipCfg() os.chdir(self.install.A_COMP_DIR) self.sim_id = int(seqnum.get_seq_num()) self.velmodel = "nr02-vs500.fk1d" self.srcfile = "test_wh.src" self.outsrf = "%d_test_eq.srf" % self.sim_id indir = os.path.join(self.install.A_IN_DATA_DIR, str(self.sim_id)) tmpdir = os.path.join(self.install.A_TMP_DATA_DIR, str(self.sim_id)) outdir = os.path.join(self.install.A_OUT_DATA_DIR, str(self.sim_id)) logdir = os.path.join(self.install.A_OUT_LOG_DIR, str(self.sim_id)) # Create all directories bband_utils.mkdirs([indir, tmpdir, outdir, logdir], print_cmd=False) cmd = "cp %s/gp/%s %s/%d/." % (self.install.A_TEST_REF_DIR, self.velmodel, self.install.A_IN_DATA_DIR, self.sim_id) bband_utils.runprog(cmd, print_cmd=False) cmd = "cp %s/gp/%s %s/%d/." % (self.install.A_TEST_REF_DIR, self.srcfile, self.install.A_IN_DATA_DIR, self.sim_id) bband_utils.runprog(cmd, print_cmd=False)
def setUp(self): self.install = InstallCfg() self.cfg = BBToolboxCfg() self.sim_id = int(seqnum.get_seq_num()) self.velmodel = "sdsu-aug2018-labasin-vmod.txt" self.srffile = "m589-s2379646.srf" self.stations = "test_stat.txt" self.srcfile = "wh_test.src" self.vmodel_name = "LABasin500" # Set up paths a_indir = os.path.join(self.install.A_IN_DATA_DIR, str(self.sim_id)) a_tmpdir = os.path.join(self.install.A_TMP_DATA_DIR, str(self.sim_id)) a_outdir = os.path.join(self.install.A_OUT_DATA_DIR, str(self.sim_id)) a_logdir = os.path.join(self.install.A_OUT_LOG_DIR, str(self.sim_id)) a_refdir = os.path.join(self.install.A_TEST_REF_DIR, "sdsu") # Create directories bband_utils.mkdirs([a_indir, a_tmpdir, a_outdir, a_logdir], print_cmd=False) cmd = "cp %s %s" % (os.path.join(a_refdir, self.velmodel), a_indir) bband_utils.runprog(cmd) cmd = "cp %s %s" % (os.path.join(a_refdir, self.stations), a_indir) bband_utils.runprog(cmd) cmd = "cp %s %s" % (os.path.join(a_refdir, self.srffile), a_indir) bband_utils.runprog(cmd) cmd = "cp %s %s" % (os.path.join(a_refdir, self.srcfile), a_indir) bband_utils.runprog(cmd) for i in range(1, 6): cmd = "cp %s %s" % ( os.path.join(self.install.A_TEST_REF_DIR, "gp", "s0%d-lf.bbp" % (i)), os.path.join(a_tmpdir, "%d.s0%d-lf.bbp" % (self.sim_id, i))) bband_utils.runprog(cmd)
def setUp(self): """ Set up and stage in all input files """ self.install = InstallCfg() self.hfsim_cfg = HfsimsCfg() self.velmodel = "genslip_nr_generic1d-gp01.vmod" self.srcfile = "test_wh.src" self.srffile = "m5.89-0.20x0.20_s2379646.srf" self.stations = "test_stat.txt" self.metadata = "metadata.txt" self.sim_id = int(seqnum.get_seq_num()) # Set up paths a_indir = os.path.join(self.install.A_IN_DATA_DIR, str(self.sim_id)) a_tmpdir = os.path.join(self.install.A_TMP_DATA_DIR, str(self.sim_id)) a_outdir = os.path.join(self.install.A_OUT_DATA_DIR, str(self.sim_id)) a_logdir = os.path.join(self.install.A_OUT_LOG_DIR, str(self.sim_id)) # Create directories bband_utils.mkdirs([a_indir, a_tmpdir, a_outdir, a_logdir], print_cmd=False) cmd = "cp %s %s" % (os.path.join(self.install.A_TEST_REF_DIR, "gp", self.velmodel), a_indir) bband_utils.runprog(cmd, print_cmd=False) cmd = "cp %s %s" % (os.path.join(self.install.A_TEST_REF_DIR, "gp", self.stations), a_indir) bband_utils.runprog(cmd, print_cmd=False) cmd = "cp %s %s" % (os.path.join(self.install.A_TEST_REF_DIR, "gp", self.srffile), a_indir) bband_utils.runprog(cmd, print_cmd=False) cmd = "cp %s %s" % (os.path.join(self.install.A_TEST_REF_DIR, "gp", self.srcfile), a_indir) bband_utils.runprog(cmd, print_cmd=False)
def run_simulation(self): """ Generate RotD50/100 values for the calculated timeseries """ print("RotDXX".center(80, '-')) # # convert input bbp acc files to peer format acc files # install = install_cfg.InstallCfg.getInstance() sim_id = self.sim_id sta_base = os.path.basename(os.path.splitext(self.r_stations)[0]) self.log = os.path.join(install.A_OUT_LOG_DIR, str(sim_id), "%d.rotd100_%s.log" % (sim_id, sta_base)) a_statfile = os.path.join(install.A_IN_DATA_DIR, str(sim_id), self.r_stations) a_tmpdir = os.path.join(install.A_TMP_DATA_DIR, str(sim_id)) a_outdir = os.path.join(install.A_OUT_DATA_DIR, str(sim_id)) # # Make sure the tmp and out directories exist # bband_utils.mkdirs([a_tmpdir, a_outdir], print_cmd=False) self.calculate_simulated(a_statfile, a_tmpdir, a_outdir, a_outdir) # All done! print("RotDXX Completed".center(80, '-'))
def create_flat_file_from_cluster(): """ Create a flat file from a cluster simulation """ # Get all we need from the command-line args = parse_arguments() # Figure out top-level directories args.top_level_indir = os.path.join(args.input_dir, "Sims", "indata") args.top_level_outdir = os.path.join(args.input_dir, "Sims", "outdata") args.realizations = sorted(os.listdir(args.top_level_indir)) args.data = {} # Create top-level output directory bband_utils.mkdirs([args.output_dir], print_cmd=False) # Collect simulation-wide parameters collect_simulation_params(args) # Collect parameters for each realization for realization in args.realizations: print("==> Processing realization: %s..." % (realization)) # Create output directory for realization if requested to copy seismograms if args.copy_timeseries: bband_utils.mkdirs([os.path.join(args.output_dir, realization)], print_cmd=False) collect_realization_params(args, realization) # Write flat file write_output_data(args)
def setUp(self): self.install = InstallCfg() self.stations = "nr_v13_3_1.stl" self.source = "nr_v14_02_1.src" self.eventname = "NR" self.sim_id = int(seqnum.get_seq_num()) sta_base = os.path.basename(os.path.splitext(self.stations)[0]) sim_id = self.sim_id # Set up paths a_indir = os.path.join(self.install.A_IN_DATA_DIR, str(sim_id)) a_tmpdir = os.path.join(self.install.A_TMP_DATA_DIR, str(sim_id)) a_tmpdir_seis = os.path.join(self.install.A_TMP_DATA_DIR, str(sim_id), "obs_seis_%s" % (sta_base)) a_outdir = os.path.join(self.install.A_OUT_DATA_DIR, str(sim_id)) a_logdir = os.path.join(self.install.A_OUT_LOG_DIR, str(sim_id)) a_validation_outdir = os.path.join(a_outdir, "validations", "stewart_duration_gmpe") # Create directories bband_utils.mkdirs([ a_indir, a_tmpdir, a_tmpdir_seis, a_outdir, a_logdir, a_validation_outdir ]) # Copy station list cmd = "cp %s %s" % (os.path.join(self.install.A_TEST_REF_DIR, "as16", self.stations), a_indir) bband_utils.runprog(cmd) cmd = "cp %s %s" % (os.path.join(self.install.A_TEST_REF_DIR, "as16", self.source), a_indir) bband_utils.runprog(cmd)
def setUp(self): """ Set up and stage in all input files """ self.install = InstallCfg() self.velmodel = "nr02-vs500.fk1d" self.srcfile = "whittier_v12_11_0_fs.src" self.srffile = "whittier_v12_11_0_fs.srf" self.stations = "whittier_v19_02_1_short.stl" self.sim_id = int(seqnum.get_seq_num()) # Set up paths refdir = os.path.join(self.install.A_TEST_REF_DIR, "irikura") a_indir = os.path.join(self.install.A_IN_DATA_DIR, str(self.sim_id)) a_tmpdir = os.path.join(self.install.A_TMP_DATA_DIR, str(self.sim_id)) a_outdir = os.path.join(self.install.A_OUT_DATA_DIR, str(self.sim_id)) a_logdir = os.path.join(self.install.A_OUT_LOG_DIR, str(self.sim_id)) # Create directories bband_utils.mkdirs([a_indir, a_tmpdir, a_outdir, a_logdir], print_cmd=False) cmd = "cp %s %s" % (os.path.join(refdir, self.velmodel), a_indir) bband_utils.runprog(cmd, print_cmd=False) cmd = "cp %s %s" % (os.path.join(refdir, self.stations), a_indir) bband_utils.runprog(cmd, print_cmd=False) cmd = "cp %s %s" % (os.path.join(refdir, self.srffile), a_indir) bband_utils.runprog(cmd, print_cmd=False) cmd = "cp %s %s" % (os.path.join(refdir, self.srcfile), a_indir) bband_utils.runprog(cmd, print_cmd=False)
def create_flat_file_from_bbp_sim(): """ Create a flat file from a BBP simulation """ # Get all we need from the command-line args = parse_arguments() # Figure out top-level directories args.top_level_indir = os.path.join(INSTALL.A_IN_DATA_DIR, str(args.sim_id)) args.top_level_outdir = INSTALL.A_OUT_DATA_DIR args.top_level_htmldir = os.path.join(INSTALL.A_OUT_DATA_DIR, str(args.first_seg_id)) args.realizations = [str(args.sim_id)] args.data = {} # Create top-level output directory bband_utils.mkdirs([args.output_dir], print_cmd=False) # Collect simulation-wide parameters collect_simulation_params(args) # Collect parameters for each realization print("==> Processing simulation : %d..." % (args.sim_id)) # Create output directory for realization if requested to copy seismograms if args.copy_timeseries: bband_utils.mkdirs([os.path.join(args.output_dir, str(args.sim_id))], print_cmd=False) collect_realization_params(args, str(args.sim_id)) # Write flat file write_output_data(args)
def setUp(self): self.install = InstallCfg() self.stations = "nr_v13_3_1.stl" self.source = "nr_v14_02_1.src" self.eventname = "NR" self.sim_id = int(seqnum.get_seq_num()) sta_base = os.path.basename(os.path.splitext(self.stations)[0]) sim_id = self.sim_id # Set up paths a_indir = os.path.join(self.install.A_IN_DATA_DIR, str(sim_id)) a_tmpdir = os.path.join(self.install.A_TMP_DATA_DIR, str(sim_id)) a_tmpdir_seis = os.path.join(self.install.A_TMP_DATA_DIR, str(sim_id), "obs_seis_%s" % (sta_base)) a_outdir = os.path.join(self.install.A_OUT_DATA_DIR, str(sim_id)) a_logdir = os.path.join(self.install.A_OUT_LOG_DIR, str(sim_id)) a_validation_outdir = os.path.join(a_outdir, "validations", "stewart_duration_gmpe") # Create directories bband_utils.mkdirs([a_indir, a_tmpdir, a_tmpdir_seis, a_outdir, a_logdir, a_validation_outdir]) # Copy station list cmd = "cp %s %s" % (os.path.join(self.install.A_TEST_REF_DIR, "as16", self.stations), a_indir) bband_utils.runprog(cmd) cmd = "cp %s %s" % (os.path.join(self.install.A_TEST_REF_DIR, "as16", self.source), a_indir) bband_utils.runprog(cmd)
def setUp(self): self.install = InstallCfg() self.sim_id = int(seqnum.get_seq_num()) self.a_outdir = os.path.join(self.install.A_OUT_DATA_DIR, str(self.sim_id)) # Create directories bband_utils.mkdirs([self.a_outdir])
def run(self): """ Do all steps needed for creating the ratio of maximum to median response across orientations (RotD100/RotD50) """ print("RotD100".center(80, '-')) # Initialize install = install_cfg.InstallCfg.getInstance() sim_id = self.sim_id sta_base = os.path.basename(os.path.splitext(self.r_stations)[0]) self.log = os.path.join(install.A_OUT_LOG_DIR, str(sim_id), "%d.rotd100_%s.log" % (sim_id, sta_base)) a_statfile = os.path.join(install.A_IN_DATA_DIR, str(sim_id), self.r_stations) a_indir = os.path.join(install.A_IN_DATA_DIR, str(sim_id)) a_tmpdir = os.path.join(install.A_TMP_DATA_DIR, str(sim_id)) a_tmpdir_seis = os.path.join(install.A_TMP_DATA_DIR, str(sim_id), "obs_seis_%s" % (sta_base)) a_outdir = os.path.join(install.A_OUT_DATA_DIR, str(sim_id)) a_validation_outdir = os.path.join(a_outdir, "validations", "baker_rd100") # # Make sure the tmp and out directories exist # bband_utils.mkdirs([a_indir, a_tmpdir, a_tmpdir_seis, a_outdir, a_validation_outdir], print_cmd=False) # Source file, parse it! a_srcfile = os.path.join(a_indir, self.r_srcfile) self.src_keys = bband_utils.parse_src_file(a_srcfile) # Calculate RotD100/RotD50 for simulated seismograms self.calculate_simulated(a_statfile, a_tmpdir, a_outdir, a_validation_outdir) # Calculate RotD100/RotD50 for observation seismograms self.calculate_observations(a_indir, a_statfile, a_tmpdir_seis, a_validation_outdir) # Calculate ratios for simulated and observation data self.calculate_ratios(a_statfile, a_validation_outdir) # Generate comparison data table self.calculate_residuals(a_statfile, a_validation_outdir) # Generate bias plot showing the comparison between # simulations and observations self.generate_plot(a_statfile, a_validation_outdir) # All done! print("RotD100 Completed".center(80, '-'))
def run_validation(self): """ Do all steps needed for creating the ratio of maximum to median response across orientations (RotD100/RotD50) """ print("RotDXX".center(80, '-')) # Initialize install = install_cfg.InstallCfg.getInstance() sim_id = self.sim_id sta_base = os.path.basename(os.path.splitext(self.r_stations)[0]) self.log = os.path.join(install.A_OUT_LOG_DIR, str(sim_id), "%d.rotd100_%s.log" % (sim_id, sta_base)) a_statfile = os.path.join(install.A_IN_DATA_DIR, str(sim_id), self.r_stations) a_indir = os.path.join(install.A_IN_DATA_DIR, str(sim_id)) a_tmpdir = os.path.join(install.A_TMP_DATA_DIR, str(sim_id)) a_tmpdir_seis = os.path.join(install.A_TMP_DATA_DIR, str(sim_id), "obs_seis_%s" % (sta_base)) a_outdir = os.path.join(install.A_OUT_DATA_DIR, str(sim_id)) a_validation_outdir = os.path.join(a_outdir, "validations", "baker_rd100") # # Make sure the tmp and out directories exist # bband_utils.mkdirs( [a_indir, a_tmpdir, a_tmpdir_seis, a_outdir, a_validation_outdir], print_cmd=False) # Source file, parse it! a_srcfile = os.path.join(a_indir, self.r_srcfile) self.src_keys = bband_utils.parse_src_file(a_srcfile) # Calculate RotD100/RotD50 for simulated seismograms self.calculate_simulated(a_statfile, a_tmpdir, a_outdir, a_validation_outdir) # Calculate RotD100/RotD50 for observation seismograms self.calculate_observations(a_indir, a_statfile, a_tmpdir_seis, a_validation_outdir) # Calculate ratios for simulated and observation data self.calculate_ratios(a_statfile, a_validation_outdir) # Generate comparison data table self.calculate_residuals(a_statfile, a_validation_outdir) # Generate bias plot showing the comparison between # simulations and observations self.generate_plot(a_statfile, a_validation_outdir) # All done! print("RotDXX Completed".center(80, '-'))
def setUp(self): self.install = InstallCfg() self.gp_gof_cfg = GPGofCfg() os.chdir(self.install.A_INSTALL_ROOT) self.srcfile = "test_wh.src" self.stations = "test_stat.txt" self.sim_id = int(seqnum.get_seq_num()) sta_base = os.path.basename(os.path.splitext(self.stations)[0]) # Set up paths a_indir = os.path.join(self.install.A_IN_DATA_DIR, str(self.sim_id)) a_tmpdir = os.path.join(self.install.A_TMP_DATA_DIR, str(self.sim_id)) a_outdir_seis = os.path.join(self.install.A_OUT_DATA_DIR, str(self.sim_id), "obs_seis_%s" % (sta_base)) a_outdir = os.path.join(self.install.A_OUT_DATA_DIR, str(self.sim_id)) a_logdir = os.path.join(self.install.A_OUT_LOG_DIR, str(self.sim_id)) # Create directories bband_utils.mkdirs([a_indir, a_tmpdir, a_outdir_seis, a_outdir, a_logdir], print_cmd=False) # Copy stations cmd = "cp %s/gp/%s %s/." % (self.install.A_TEST_REF_DIR, self.stations, a_indir) bband_utils.runprog(cmd, print_cmd=False) # Copy src file cmd = "cp %s/gp/%s %s/." % (self.install.A_TEST_REF_DIR, self.srcfile, a_indir) bband_utils.runprog(cmd, print_cmd=False) for i in range(1, 6): # Copy sample calculated seismograms and response files cmd = ("cp %s/gp/s%02d.merged.bbp %s/%d/%d.s%02d.vel.bbp" % (self.install.A_TEST_REF_DIR, i, self.install.A_OUT_DATA_DIR, self.sim_id, self.sim_id, i)) bband_utils.runprog(cmd, print_cmd=False) cmd = ("cp %s/gp/s%02d.rd50 %s/%d/%d.s%02d.rd50" % (self.install.A_TEST_REF_DIR, i, self.install.A_OUT_DATA_DIR, self.sim_id, self.sim_id, i)) bband_utils.runprog(cmd, print_cmd=False) # Cope sample observed seismograms and response files cmd = ("cp %s/gp/s%02d.merged.bbp %s/s%02d.bbp" % (self.install.A_TEST_REF_DIR, i, a_outdir_seis, i)) bband_utils.runprog(cmd, print_cmd=False) cmd = ("cp %s/gp/s%02d.rd50 %s/s%02d.rd50" % (self.install.A_TEST_REF_DIR, i, a_outdir_seis, i)) bband_utils.runprog(cmd, print_cmd=False)
def setUp(self): self.install = InstallCfg() self.stations = "nr_v13_3_1.stl" self.eventname = "NR" self.sim_id = int(seqnum.get_seq_num()) sta_base = os.path.basename(os.path.splitext(self.stations)[0]) sim_id = self.sim_id # Set up paths a_indir = os.path.join(self.install.A_IN_DATA_DIR, str(sim_id)) a_tmpdir = os.path.join(self.install.A_TMP_DATA_DIR, str(sim_id)) a_tmpdir_seis = os.path.join(self.install.A_TMP_DATA_DIR, str(sim_id), "obs_seis_%s" % (sta_base)) a_outdir = os.path.join(self.install.A_OUT_DATA_DIR, str(sim_id)) a_logdir = os.path.join(self.install.A_OUT_LOG_DIR, str(sim_id)) a_validation_outdir = os.path.join(a_outdir, "validations", "rzz2015") # Create directories bband_utils.mkdirs([a_indir, a_tmpdir, a_tmpdir_seis, a_outdir, a_logdir, a_validation_outdir]) # Copy station list cmd = "cp %s %s" % (os.path.join(self.install.A_TEST_REF_DIR, "rzz2015", self.stations), a_indir) bband_utils.runprog(cmd) # Read station list slo = StationList(os.path.join(a_indir, self.stations)) site_list = slo.getStationList() # Loop over stations for site in site_list: station = site.scode src_sims_acc = os.path.join(self.install.A_TEST_REF_DIR, "rzz2015", "syn_seis", "%s.acc.bbp" % (station)) dst_sims_acc = os.path.join(a_outdir, "%d.%s.acc.bbp" % (sim_id, station)) src_obs_acc = os.path.join(self.install.A_TEST_REF_DIR, "rzz2015", "obs_seis", "%s.bbp" % (station)) dst_obs_acc = os.path.join(a_tmpdir_seis, "%s.bbp" % (station)) cmd = "cp %s %s" % (src_sims_acc, dst_sims_acc) bband_utils.runprog(cmd) cmd = "cp %s %s" % (src_obs_acc, dst_obs_acc) bband_utils.runprog(cmd)
def run(self): """ Calculate GMPEs, create bias plot comparisons """ print("Calculate GMPE".center(80, '-')) # Initialize basic variables install = InstallCfg.getInstance() sim_id = self.sim_id sta_base = os.path.basename(os.path.splitext(self.r_stations)[0]) # Input, tmp, and output directories a_outdir = os.path.join(install.A_OUT_DATA_DIR, str(sim_id)) a_outdir_gmpe = os.path.join(a_outdir, "gmpe_data_%s" % (sta_base)) a_logdir = os.path.join(install.A_OUT_LOG_DIR, str(sim_id)) self.log = os.path.join(a_logdir, "%d.gmpe_compare.log" % (sim_id)) # # Make sure the output and tmp directories exist # dirs = [a_outdir_gmpe, a_outdir, a_logdir] bband_utils.mkdirs(dirs, print_cmd=False) # Source file, parse it! a_srcfile = os.path.join(install.A_IN_DATA_DIR, str(sim_id), self.r_src_file) self.src_keys = bband_utils.parse_src_file(a_srcfile) # Station file a_statfile = os.path.join(install.A_IN_DATA_DIR, str(sim_id), self.r_stations) slo = StationList(a_statfile) site_list = slo.getStationList() # Go through each station, and print comparison headers for # the first station we process for site in site_list: stat = site.scode print("==> Calculating GMPE for station: %s" % (stat)) output_file = os.path.join(a_outdir_gmpe, "%s-gmpe.ri50" % (stat)) self.calculate_gmpe(site, output_file) # All done print("Calculate GMPE Completed".center(80, '-'))
def setUp(self): """ Copy needed files to run the test """ self.install = InstallCfg() self.sim_id = int(seqnum.get_seq_num()) self.a_tmpdir = os.path.join(self.install.A_TMP_DATA_DIR, str(self.sim_id)) self.a_ucsb_refdir = os.path.join(self.install.A_TEST_REF_DIR, "ucsb") self.a_sdsu_refdir = os.path.join(self.install.A_TEST_REF_DIR, "sdsu") self.a_gp_refdir = os.path.join(self.install.A_TEST_REF_DIR, "gp") # # Make sure output directories exist # bband_utils.mkdirs([self.a_tmpdir])
def setUp(self): """ Cope needed files to run the test """ self.vmodel_name = "LABasin" self.sim_id = int(seqnum.get_seq_num()) self.install = InstallCfg() self.vmodel_obj = vmodels.get_velocity_model_by_name(self.vmodel_name) indir = os.path.join(self.install.A_IN_DATA_DIR, str(self.sim_id)) tmpdir = os.path.join(self.install.A_TMP_DATA_DIR, str(self.sim_id)) outdir = os.path.join(self.install.A_OUT_DATA_DIR, str(self.sim_id)) logdir = os.path.join(self.install.A_OUT_LOG_DIR, str(self.sim_id)) # Create all directories bband_utils.mkdirs([indir, tmpdir, outdir, logdir], print_cmd=False) # Copy needed files # src file r_src_file = "nr_v12_11_0_fs.src" src_file = os.path.join(self.install.A_TEST_REF_DIR, "uwo", r_src_file) self.src_file = os.path.join(indir, r_src_file) cmd = "cp %s %s" % (src_file, self.src_file) bband_utils.runprog(cmd) # exsim param template file vmodel_params = self.vmodel_obj.get_codebase_params('exsim') self.failIf('GENERIC_PARAM' not in vmodel_params) r_param_template = vmodel_params['GENERIC_PARAM'] self.failIf(r_param_template == "" or r_param_template is None) param_template = os.path.join(self.vmodel_obj.base_dir, r_param_template) # r_param_template is relative to the velocity model basedir, # get only basename r_param_template = os.path.basename(r_param_template) self.param_template = os.path.join(indir, r_param_template) cmd = "cp %s %s" % (param_template, self.param_template) bband_utils.runprog(cmd) # station file r_stations = "nr_v12_11_2.stl" stations = os.path.join(self.install.A_TEST_REF_DIR, "uwo", r_stations) self.stations = os.path.join(indir, r_stations) cmd = "cp %s %s" % (stations, self.stations) bband_utils.runprog(cmd)
def setUp(self): """ Sets up the environment for the test """ self.install = install_cfg.InstallCfg.getInstance() self.sim_id = int(seqnum.get_seq_num()) # Make sure all directories exist self.indir = os.path.join(self.install.A_IN_DATA_DIR, str(self.sim_id)) self.tmpdir = os.path.join(self.install.A_TMP_DATA_DIR, str(self.sim_id)) self.outdir = os.path.join(self.install.A_OUT_DATA_DIR, str(self.sim_id)) self.logdir = os.path.join(self.install.A_OUT_LOG_DIR, str(self.sim_id)) bband_utils.mkdirs([self.indir, self.tmpdir, self.outdir, self.logdir], print_cmd=False)
def setUp(self): self.install = InstallCfg() os.chdir(self.install.A_COMP_DIR) self.sim_id = int(seqnum.get_seq_num()) self.srcfile = "test_whittier_song.src" self.outsrf = "%d_test_eq.srf" % self.sim_id indir = os.path.join(self.install.A_IN_DATA_DIR, str(self.sim_id)) tmpdir = os.path.join(self.install.A_TMP_DATA_DIR, str(self.sim_id)) outdir = os.path.join(self.install.A_OUT_DATA_DIR, str(self.sim_id)) logdir = os.path.join(self.install.A_OUT_LOG_DIR, str(self.sim_id)) # Create all directories bband_utils.mkdirs([indir, tmpdir, outdir, logdir], print_cmd=False) cmd = "cp %s %s" % ( os.path.join(self.install.A_TEST_REF_DIR, "song", self.srcfile), os.path.join(self.install.A_IN_DATA_DIR, str(self.sim_id))) bband_utils.runprog(cmd)
def create_merged_dirs(self): """ Creates the directory struction for the merged simulation """ install = self.install self.a_indir = os.path.join(install.A_IN_DATA_DIR, str(self.output_sim_id)) self.a_tmpdir = os.path.join(install.A_TMP_DATA_DIR, str(self.output_sim_id)) self.a_outdir = os.path.join(install.A_OUT_DATA_DIR, str(self.output_sim_id)) self.a_logdir = os.path.join(install.A_OUT_LOG_DIR, str(self.output_sim_id)) # Make sure directories exist bband_utils.mkdirs([self.a_tmpdir, self.a_indir, self.a_outdir, self.a_logdir], print_cmd=False)
def setUp(self): self.install = InstallCfg() accept_test_inputs = "accept_inputs" src_path = "" self.resume = True run_dir = self.install.A_USER_DATA_DIR # Create run directory, in case it doesn't exist bband_utils.mkdirs([run_dir], print_cmd=False) if not os.path.exists(os.path.join(run_dir, "northridge_3_sta.stl")): src_path = os.path.join(self.install.A_TEST_REF_DIR, accept_test_inputs, "northridge_3_sta.stl") shutil.copy2(src_path, run_dir) if not os.path.exists(os.path.join(run_dir, "northridge_eq_gp.src")): src_path = os.path.join(self.install.A_TEST_REF_DIR, accept_test_inputs, "northridge_eq_gp.src") shutil.copy2(src_path, run_dir) if not os.path.exists(os.path.join(run_dir, "northridge_eq_ucsb.src")): src_path = os.path.join(self.install.A_TEST_REF_DIR, accept_test_inputs, "northridge_eq_ucsb.src") shutil.copy2(src_path, run_dir) if not os.path.exists(os.path.join(run_dir, "northridge_eq_song.src")): src_path = os.path.join(self.install.A_TEST_REF_DIR, accept_test_inputs, "northridge_eq_song.src") shutil.copy2(src_path, run_dir) if not os.path.exists( os.path.join(self.install.A_OUT_LOG_DIR, "acceptance_test_logs")): bband_utils.mkdirs([ os.path.join(self.install.A_OUT_LOG_DIR, "acceptance_test_logs") ])
def setUp(self): self.install = InstallCfg() self.cfg = RMGCfg() os.chdir(self.install.A_COMP_DIR) self.sim_id = int(seqnum.get_seq_num()) self.srcfile = "test_whittier_song.src" self.outsrf = "%d_test_eq.srf" % self.sim_id indir = os.path.join(self.install.A_IN_DATA_DIR, str(self.sim_id)) tmpdir = os.path.join(self.install.A_TMP_DATA_DIR, str(self.sim_id)) outdir = os.path.join(self.install.A_OUT_DATA_DIR, str(self.sim_id)) logdir = os.path.join(self.install.A_OUT_LOG_DIR, str(self.sim_id)) # Create all directories bband_utils.mkdirs([indir, tmpdir, outdir, logdir], print_cmd=False) cmd = "cp %s %s" % (os.path.join(self.install.A_TEST_REF_DIR, "song", self.srcfile), os.path.join(self.install.A_IN_DATA_DIR, str(self.sim_id))) bband_utils.runprog(cmd)
def setUp(self): self.install = InstallCfg() accept_test_inputs = "accept_inputs" src_path = "" self.resume = True run_dir = self.install.A_USER_DATA_DIR # Create run directory, in case it doesn't exist bband_utils.mkdirs([run_dir], print_cmd=False) if not os.path.exists(os.path.join(run_dir, "northridge_3_sta.stl")): src_path = os.path.join(self.install.A_TEST_REF_DIR, accept_test_inputs, "northridge_3_sta.stl") shutil.copy2(src_path, run_dir) if not os.path.exists(os.path.join(run_dir, "northridge_eq_gp.src")): src_path = os.path.join(self.install.A_TEST_REF_DIR, accept_test_inputs, "northridge_eq_gp.src") shutil.copy2(src_path, run_dir) if not os.path.exists(os.path.join(run_dir, "northridge_eq_ucsb.src")): src_path = os.path.join(self.install.A_TEST_REF_DIR, accept_test_inputs, "northridge_eq_ucsb.src") shutil.copy2(src_path, run_dir) if not os.path.exists(os.path.join(run_dir, "northridge_eq_song.src")): src_path = os.path.join(self.install.A_TEST_REF_DIR, accept_test_inputs, "northridge_eq_song.src") shutil.copy2(src_path, run_dir) if not os.path.exists(os.path.join(self.install.A_OUT_LOG_DIR, "acceptance_test_logs")): bband_utils.mkdirs([os.path.join(self.install.A_OUT_LOG_DIR, "acceptance_test_logs")])
def generate_md5s(data_dir, md5_dir, top_level=False): """ This function generates md5 checksums for the files stored in data_dir, and stores the results in md5_dir. """ print "Entering directory %s\n" % data_dir # Make sure md5_dir exists bband_utils.mkdirs([md5_dir], print_cmd=False) for entry in os.listdir(data_dir): # Skip top-level checksums directory if top_level and entry == "checksums": continue a_entry = os.path.join(data_dir, entry) if os.path.isdir(a_entry) and entry[0] != '.': #skip hidden dirs generate_md5s(a_entry, os.path.join(md5_dir, entry)) else: md5_filename = os.path.join(md5_dir, "%s.md5" % (entry)) filename = os.path.basename(entry) if sys.platform == "linux2": # We are running on linux md5sum = subprocess.Popen(["md5sum", a_entry], stdout=subprocess.PIPE) computed_sum = md5sum.communicate()[0].split()[0] elif sys.platform == "darwin": # This is a Mac OS X computer md5sum = subprocess.Popen(["md5", a_entry], stdout=subprocess.PIPE) computed_sum = md5sum.communicate()[0].split()[3] else: print "Unknow platform %s!" % (sys.platform) sys.exit(1) # Now, write md5sum to file md5_out = open(md5_filename, 'w') md5_out.write("%s %s\n" % (computed_sum, filename)) md5_out.close()
def test_xy2ll(self): """ ll2xy mlon=-118 mlat=34 xazim=0 < stats.ll > stats_out.xy This will return a file with a different suffix to identify the contents """ self.install = InstallCfg.getInstance() self.sim_id = int(seqnum.get_seq_num()) # Make sure all directories exist self.indir = os.path.join(self.install.A_IN_DATA_DIR, str(self.sim_id)) self.tmpdir = os.path.join(self.install.A_TMP_DATA_DIR, str(self.sim_id)) self.outdir = os.path.join(self.install.A_OUT_DATA_DIR, str(self.sim_id)) self.logdir = os.path.join(self.install.A_OUT_LOG_DIR, str(self.sim_id)) bband_utils.mkdirs([self.indir, self.tmpdir, self.outdir, self.logdir]) ilon = -118.0 ilat = 34.0 iaz = 0.0 infile = os.path.join(self.indir, "100.bob.ll") ofile = os.path.join(self.outdir, "100.bob.xy") reffile = os.path.join(self.install.A_TEST_REF_DIR, "sdsu", "100.bob.xy") # Write input file in_file = open(infile, "w") data = "%f %f\n" % (ilon, ilat) in_file.write(data) in_file.close() # Run the test cc.ll2xy(infile, ofile, ilon, ilat, iaz) # Check output self.failIf( filecmp.cmp(reffile, ofile) == False, "LL to XY did not work")
def test_gp2uc(self): """ Inputs a GP format file and get out a UCSB format file """ install = InstallCfg() sim_id = int(seqnum.get_seq_num()) a_tmpdir = os.path.join(install.A_TMP_DATA_DIR, str(sim_id)) a_refdir = os.path.join(install.A_TEST_REF_DIR, "ucsb") # # Make sure output directories exist # bband_utils.mkdirs([a_tmpdir], print_cmd=False) # File paths gpfile = os.path.join(a_refdir, "stats-h0.125.ll") ucref = os.path.join(a_refdir, "stations.ll") ofile = os.path.join(a_tmpdir, "stations.ll") ofile30 = os.path.join(a_tmpdir, "stations.vs30") sl = StationList(gpfile) _ = stas2files.gp2uc_stalist(sl, ofile, ofile30) errmsg = "Conversion of station list from GP to UC format failed" self.failIf(filecmp.cmp(ucref, ofile) == False, errmsg)
def setUp(self): self.install = InstallCfg() self.srcfile = "whittier_v12_11_0_fs.src" self.outsrf = "whittier_v12_11_0_fs.srf" self.velmodel = "nr02-vs500.fk1d" self.sim_id = int(seqnum.get_seq_num()) indir = os.path.join(self.install.A_IN_DATA_DIR, str(self.sim_id)) tmpdir = os.path.join(self.install.A_TMP_DATA_DIR, str(self.sim_id)) outdir = os.path.join(self.install.A_OUT_DATA_DIR, str(self.sim_id)) logdir = os.path.join(self.install.A_OUT_LOG_DIR, str(self.sim_id)) refdir = os.path.join(self.install.A_TEST_REF_DIR, "irikura") # Create all directories bband_utils.mkdirs([indir, tmpdir, outdir, logdir], print_cmd=False) # Copy input files cmd = "cp %s %s" % (os.path.join(refdir, self.velmodel), indir) bband_utils.runprog(cmd, print_cmd=False) cmd = "cp %s %s" % (os.path.join(refdir, self.srcfile), indir) bband_utils.runprog(cmd, print_cmd=False) os.chdir(tmpdir)
def run(self): """ Runs the GMPEs for the six parameters in Rezaeian (2015) """ print("RZZ2015 GMPE".center(80, '-')) # Load configuration, set sim_id install = InstallCfg.getInstance() sim_id = self.sim_id # Build directory paths a_tmpdir = os.path.join(install.A_TMP_DATA_DIR, str(sim_id)) a_indir = os.path.join(install.A_IN_DATA_DIR, str(sim_id)) a_outdir = os.path.join(install.A_OUT_DATA_DIR, str(sim_id)) a_logdir = os.path.join(install.A_OUT_LOG_DIR, str(sim_id)) a_validation_outdir = os.path.join(a_outdir, "validations", "rzz2015_gmpe") # Make sure the output and tmp directories exist bband_utils.mkdirs([a_tmpdir, a_indir, a_outdir, a_validation_outdir], print_cmd=False) # Source file, parse it! a_srcfile = os.path.join(a_indir, self.srcfile) self.src_keys = bband_utils.parse_src_file(a_srcfile) # Now the file paths self.log = os.path.join(a_logdir, "%d.rzz2015gmpe.log" % (sim_id)) sta_file = os.path.join(a_indir, self.stations) # Get station list slo = StationList(sta_file) site_list = slo.getStationList() # Initialize random seed np.random.seed(int(self.src_keys['seed'])) # Create output file, add header out_file = open( os.path.join(a_validation_outdir, '%d.rzz2015gmpe.txt' % (self.sim_id)), 'w') out_file.write("#station, r_rup, vs_30," " ai_mean, d595_mean, tmid_mean," " wmid_mean, wslp_mean, zeta_mean," " ai_stddev, d595_stddev, tmid_stddev," " wmid_stddev, wslp_stddev, zeta_stddev\n") # Go through each station for site in site_list: stat = site.scode print("==> Processing station: %s" % (stat)) # Calculate Rrup origin = (self.src_keys['lon_top_center'], self.src_keys['lat_top_center']) dims = (self.src_keys['fault_length'], self.src_keys['dlen'], self.src_keys['fault_width'], self.src_keys['dwid'], self.src_keys['depth_to_top']) mech = (self.src_keys['strike'], self.src_keys['dip'], self.src_keys['rake']) site_geom = [float(site.lon), float(site.lat), 0.0] (fault_trace1, up_seis_depth, low_seis_depth, ave_dip, dummy1, dummy2) = putils.FaultTraceGen(origin, dims, mech) _, rrup, _ = putils.DistanceToSimpleFaultSurface( site_geom, fault_trace1, up_seis_depth, low_seis_depth, ave_dip) vs30 = site.vs30 mag = self.src_keys['magnitude'] # Fault type is 1 (Reverse) unless condition below is met # Then it is 0 (Strike-pp) fault_type = 1 rake = self.src_keys['rake'] if ((rake >= -180 and rake < -150) or (rake >= -30 and rake <= 30) or (rake > 150 and rake <= 180)): fault_type = 0 #rrup = 13.94 #fault_type = 1 #vs30 = 659.6 #mag = 7.35 [ai_mean, d595_mean, tmid_mean, wmid_mean, wslp_mean, zeta_mean] = self.calculate_mean_values(rrup, vs30, mag, fault_type) # Randomize parameters using standard deviations and correlations sta_ai = [] sta_d595 = [] sta_tmid = [] sta_wmid = [] sta_wslp = [] sta_zeta = [] # Simulate number_of_samples realizations of the error # term for each parameter for _ in range(0, self.number_of_samples): # Simulate zero-mean normal correlated parameters with # stdv = sqrt(sigmai^2+taui^2) # totalerror = eps+etha=[eps1+etha1 eps2+etha2 eps3+etha3 eps4+etha4 # eps5+etha5 eps6+etha6] # mean error vector # m_totalerror = [0, 0, 0, 0, 0, 0] # Covariance matrix std1 = np.sqrt(self.sigma1**2 + self.tau1**2) std2 = np.sqrt(self.sigma2**2 + self.tau2**2) std3 = np.sqrt(self.sigma3**2 + self.tau3**2) std4 = np.sqrt(self.sigma4**2 + self.tau4**2) std5 = np.sqrt(self.sigma5**2 + self.tau5**2) std6 = np.sqrt(self.sigma6**2 + self.tau6**2) s_total_error = [ [ std1**2, std1 * std2 * self.rho_totalerror[0][1], std1 * std3 * self.rho_totalerror[0][2], std1 * std4 * self.rho_totalerror[0][3], std1 * std5 * self.rho_totalerror[0][4], std1 * std6 * self.rho_totalerror[0][5] ], [ std2 * std1 * self.rho_totalerror[1][0], std2**2, std2 * std3 * self.rho_totalerror[1][2], std2 * std4 * self.rho_totalerror[1][3], std2 * std5 * self.rho_totalerror[1][4], std2 * std6 * self.rho_totalerror[1][5] ], [ std3 * std1 * self.rho_totalerror[2][0], std3 * std2 * self.rho_totalerror[2][1], std3**2, std3 * std4 * self.rho_totalerror[2][3], std3 * std5 * self.rho_totalerror[2][4], std3 * std6 * self.rho_totalerror[2][5] ], [ std4 * std1 * self.rho_totalerror[3][0], std4 * std2 * self.rho_totalerror[3][1], std4 * std3 * self.rho_totalerror[3][2], std4**2, std4 * std5 * self.rho_totalerror[3][4], std4 * std6 * self.rho_totalerror[3][5] ], [ std5 * std1 * self.rho_totalerror[4][0], std5 * std2 * self.rho_totalerror[4][1], std5 * std3 * self.rho_totalerror[4][2], std5 * std4 * self.rho_totalerror[4][3], std5**2, std5 * std6 * self.rho_totalerror[4][5] ], [ std6 * std1 * self.rho_totalerror[5][0], std6 * std2 * self.rho_totalerror[5][1], std6 * std3 * self.rho_totalerror[5][2], std6 * std4 * self.rho_totalerror[5][3], std6 * std5 * self.rho_totalerror[5][4], std6**2 ] ] # Matlab returns upper-triangular while Python returns # lower-triangular by default -- no need to transpose later! r_total_error = np.linalg.cholesky(s_total_error) y_total_error = np.random.normal(0, 1, 6) total_error = np.dot(r_total_error, y_total_error) # Generate randomize parameters in the standardnormal space: ui u1 = (self.beta1[0] + self.beta1[1] * (mag / 7.0) + self.beta1[2] * fault_type + self.beta1[3] * math.log(rrup / 25.0) + self.beta1[4] * math.log(vs30 / 750.0)) + total_error[0] u2 = (self.beta2[0] + self.beta2[1] * mag + self.beta2[2] * fault_type + self.beta2[3] * rrup + self.beta2[4] * vs30) + total_error[1] u3 = (self.beta3[0] + self.beta3[1] * mag + self.beta3[2] * fault_type + self.beta3[3] * rrup + self.beta3[4] * vs30) + total_error[2] u4 = (self.beta4[0] + self.beta4[1] * mag + self.beta4[2] * fault_type + self.beta4[3] * rrup + self.beta4[4] * vs30) + total_error[3] u5 = (self.beta5[0] + self.beta5[1] * mag + self.beta5[2] * fault_type + self.beta5[3] * rrup + self.beta5[4] * vs30) + total_error[4] u6 = (self.beta6[0] + self.beta6[1] * mag + self.beta6[2] * fault_type + self.beta6[3] * rrup + self.beta6[4] * vs30) + total_error[5] # Transform parameters ui from standardnormal to the physical space: # thetai (constraint: tmid < d_5_95, removed) theta1 = norm.ppf(norm.cdf(u1), -4.8255, 1.4318) theta2 = 5.0 + (45 - 5) * beta.ppf(norm.cdf(u2), 1.1314, 2.4474) theta3 = 0.5 + (40 - 0.5) * beta.ppf(norm.cdf(u3), 1.5792, 3.6405) theta4 = gamma.ppf(norm.cdf(u4), 4.0982, scale=1.4330) theta5 = self.slpinv(norm.cdf(u5), 17.095, 6.7729, 4.8512, -2, 0.5) theta6 = 0.02 + (1 - 0.02) * beta.ppf(norm.cdf(u6), 1.4250, 5.7208) sta_ai.append(math.exp(theta1)) sta_d595.append(theta2) sta_tmid.append(theta3) sta_wmid.append(theta4) sta_wslp.append(theta5) sta_zeta.append(theta6) # Write output to gmpe file out_file.write( "%s, %7.4f, %7.2f, " % (stat, rrup, vs30) + "%7.4f, %7.4f, %7.4f, %7.4f, %7.4f, %7.4f, " % (ai_mean, d595_mean, tmid_mean, wmid_mean, wslp_mean, zeta_mean) + "%7.4f, %7.4f, %7.4f, %7.4f, %7.4f, %7.4f\n" % (np.std(sta_ai), np.std(sta_d595), np.std(sta_tmid), np.std(sta_wmid), np.std(sta_wslp), np.std(sta_zeta))) ## Write output to file #sta_out_file = open(os.path.join(a_validation_outdir, # '%d.rzz2015gmpe.%s.txt' % # (self.sim_id, stat)), 'w') #sta_out_file.write("#ai(s.g^2), d595(s), tmid(s), " # "wmid(Hz), wslp(Hz/sec), zeta(ratio)\n") #for ai, d595, tmid, wmid, wslp, zeta in zip(sta_ai, sta_d595, # sta_tmid, sta_wmid, # sta_wslp, sta_zeta): # sta_out_file.write("%7.4f, %7.4f, %7.4f, %7.4f, %7.4f, %7.4f\n" % # (ai, d595, tmid, wmid, wslp, zeta)) #sta_out_file.close() # Generate Plots self.plot(stat, a_validation_outdir, rrup, fault_type, vs30, mag, sta_ai, sta_d595, sta_tmid, sta_wmid, sta_wslp, sta_zeta, ai_mean, d595_mean, tmid_mean, wmid_mean, wslp_mean, zeta_mean) # Close output file out_file.close() print("RZZ2015 GMPE Completed".center(80, '-'))
def run(self): """ Extracts needed seismograms from the bin file """ print("SDSU Seismograms".center(80, '-')) install = InstallCfg.getInstance() sim_id = self.sim_id sta_base = os.path.basename(os.path.splitext(self.r_stations)[0]) a_indir = os.path.join(install.A_IN_DATA_DIR, str(sim_id)) a_outdir = os.path.join(install.A_OUT_DATA_DIR, str(sim_id)) a_tmpdir = os.path.join(install.A_TMP_DATA_DIR, str(sim_id)) a_tmpdir_mod = os.path.join(install.A_TMP_DATA_DIR, str(sim_id), "sdsu_seismograms_%s" % (sta_base)) binfile = os.path.join(a_indir, self.r_binfile) # # Make sure the output and tmp directories exist # bband_utils.mkdirs([a_tmpdir, a_tmpdir_mod, a_outdir], print_cmd=False) a_full_stations = os.path.join(a_indir, self.r_full_stations) a_stations = os.path.join(a_indir, self.r_stations) # Copy station files to the tmpdir_mod directory cmd = "cp %s %s" % (a_full_stations, os.path.join(a_tmpdir_mod, self.r_full_stations)) bband_utils.runprog(cmd) cmd = "cp %s %s" % (a_stations, os.path.join(a_tmpdir_mod, self.r_stations)) bband_utils.runprog(cmd) # # Make sure path names are within the limits accepted by the # Fortran code # if len(binfile) >= bband_utils.SDSU_MAX_FILENAME: raise ValueError("binfile is %d characters long, maximum is %d" % (len(binfile), bband_utils.SDSU_MAX_FILENAME)) old_cwd = os.getcwd() os.chdir(a_tmpdir_mod) # Get number of stations in seismogram file, this is a # variation of the code in station_list.py stat_names = {} num_stations = 0 stat_fp = open(a_full_stations, 'r') for line in stat_fp: if line.startswith('#'): continue sta = line.split() if len(sta) >= 3: scode = sta[2] num_stations = num_stations + 1 stat_names[scode] = num_stations stat_fp.close() # Create list of stations to save slo = StationList(a_stations) site_list = slo.getStationList() save_stat_names = [] for stat in site_list: save_stat_names.append(stat.scode) # Convert to bbp format cmd = "%s/bin2bbp %s %d" % (install.A_SDSU_BIN_DIR, binfile, len(stat_names)) bband_utils.runprog(cmd) # Copy over the names for stat in save_stat_names: if not stat in stat_names: continue sta_id = stat_names[stat] shutil.copy2("%s/%d.bbp" % (a_tmpdir_mod, sta_id), "%s/%d.%s-lf.bbp" % (a_tmpdir, sim_id, stat)) del stat_names[stat] # Delete the ones you don't need for stat in stat_names.keys(): os.remove("%s/%d.bbp" % (a_tmpdir_mod, stat_names[stat])) os.chdir(old_cwd) print("SDSU Seismograms Completed".center(80, '-'))
def run(self): """ Runs the Anderson GoF code """ print("RZZ2015".center(80, '-')) # Load configuration, set sim_id install = InstallCfg.getInstance() sim_id = self.sim_id # Build directory paths a_tmpdir = os.path.join(install.A_TMP_DATA_DIR, str(sim_id)) a_indir = os.path.join(install.A_IN_DATA_DIR, str(sim_id)) a_outdir = os.path.join(install.A_OUT_DATA_DIR, str(sim_id)) a_logdir = os.path.join(install.A_OUT_LOG_DIR, str(sim_id)) a_validation_outdir = os.path.join(a_outdir, "validations", "rzz2015") # Make sure the output and tmp directories exist bband_utils.mkdirs([a_tmpdir, a_indir, a_outdir, a_validation_outdir], print_cmd=False) # Now the file paths self.log = os.path.join(a_logdir, "%d.rzz2015.log" % (sim_id)) sta_file = os.path.join(a_indir, self.stations) sta_base = os.path.basename(os.path.splitext(self.stations)[0]) obs_dir = os.path.join(a_tmpdir, "obs_seis_%s" % (sta_base)) # Get station list slo = StationList(sta_file) site_list = slo.getStationList() # Create output file, add header out_file = open(os.path.join(a_validation_outdir, '%d.rzz2015.%s.txt' % (self.sim_id, self.eventname)), 'w') out_file.write("#station, component, epsilon_a, nu_a," " epsilon_b, nu_b, epsilon_c, nu_c," " r1_record, r1_siml, r2_record, r2_siml," " r3_record, r3_siml, r4_record, r4_siml," " r5_record, r5_siml, r6_record, r6_siml\n") out_file.close() # Go through each station for site in site_list: stat = site.scode r_obs_bbp = "%s.bbp" % (stat) a_obs_bbp = os.path.join(obs_dir, r_obs_bbp) r_sym_bbp = "%d.%s.acc.bbp" % (sim_id, stat) a_sym_bbp = os.path.join(a_outdir, r_sym_bbp) if not (os.path.exists(a_obs_bbp) and os.path.exists(a_sym_bbp)): # Just skip it print("===> Couldn't find files " "%s and %s, skipping station %s" % (a_obs_bbp, a_sym_bbp, stat)) continue obs_data = self.read_bbp(a_obs_bbp) sym_data = self.read_bbp(a_sym_bbp) # Process each component separately for comp in range(1, 3): self.process(stat, comp, a_validation_outdir, obs_data[0], obs_data[comp], sym_data[0], sym_data[comp]) print("RZZ2015 Completed".center(80, '-'))
def run(self): """ This function prepares the parameters for Irikura's gen_srf then calls it """ # Load configuration, set sim_id install = InstallCfg.getInstance() sim_id = self.sim_id # Build directory paths a_tmpdir = os.path.join(install.A_TMP_DATA_DIR, str(sim_id)) a_indir = os.path.join(install.A_IN_DATA_DIR, str(sim_id)) a_outdir = os.path.join(install.A_OUT_DATA_DIR, str(sim_id)) a_logdir = os.path.join(install.A_OUT_LOG_DIR, str(sim_id)) # Make sure the output and tmp directories exist bband_utils.mkdirs([a_tmpdir, a_indir, a_outdir]) # Now, file paths self.log = os.path.join(a_logdir, "%d.gen_srf.log" % (sim_id)) a_srcfile = os.path.join(a_indir, self.r_srcfile) # Read src file cfg = IrikuraCfg(a_srcfile) # Define location of input velocity model and output srf file a_srffile = os.path.join(a_indir, self.r_srffile) a_velmod = os.path.join(install.A_IN_DATA_DIR, str(sim_id), self.r_velmodel) # Run in tmpdir subdir to isolate temp fortran files # Save cwd, change back to it at the end old_cwd = os.getcwd() os.chdir(a_tmpdir) # # Run gen_srf code # progstring = ("%s >> %s 2>&1 << END\n" % (os.path.join(install.A_IRIKURA_BIN_DIR, cfg.GENSRF), self.log) + "%s\n" % a_srffile + "%f %f %f %f %f\n" % (cfg.CFGDICT["fault_length"], cfg.CFGDICT["fault_width"], cfg.CFGDICT['strike'], cfg.CFGDICT['dip'], cfg.CFGDICT['rake']) + "%f %f %f\n" % (cfg.CFGDICT["lon_top_center"], cfg.CFGDICT["lat_top_center"], cfg.CFGDICT["depth_to_top"]) + "%f %f\n" % (cfg.CFGDICT["dlen"], cfg.CFGDICT["dwid"]) + "%f %f %f %f\n" % (cfg.CFGDICT["hypo_along_stk"], cfg.CFGDICT["hypo_down_dip"], cfg.DENS, cfg.VS) + "%f\n" % (cfg.DT) + "%d\n" % (int(cfg.CFGDICT['seed'])) + "%s\n" % a_velmod + "END") bband_utils.runprog(progstring) # Restore working directory os.chdir(old_cwd) # # mv result to outputfile # progstring = "cp %s %s" % (a_srffile, os.path.join(a_tmpdir, self.r_srffile)) bband_utils.runprog(progstring) progstring = "cp %s %s" % (a_srffile, os.path.join(a_outdir, self.r_srffile)) bband_utils.runprog(progstring) # Plot SRF plot_srf.run(self.r_srffile, sim_id=self.sim_id) print("Completed Irikura gen_srf .....")
def run(self): """ Runs the Anderson GoF code """ print("Anderson GoF".center(80, '-')) # Load configuration, set sim_id install = InstallCfg.getInstance() sim_id = self.sim_id # Build directory paths a_tmpdir = os.path.join(install.A_TMP_DATA_DIR, str(sim_id)) a_indir = os.path.join(install.A_IN_DATA_DIR, str(sim_id)) a_outdir = os.path.join(install.A_OUT_DATA_DIR, str(sim_id)) a_logdir = os.path.join(install.A_OUT_LOG_DIR, str(sim_id)) a_validation_outdir = os.path.join(a_outdir, "validations", "anderson_gof") # Make sure the output and tmp directories exist bband_utils.mkdirs([a_tmpdir, a_indir, a_outdir, a_validation_outdir], print_cmd=False) # Now the file paths self.log = os.path.join(a_logdir, "%d.anderson.log" % (sim_id)) sta_file = os.path.join(a_indir, self.stations) sta_base = os.path.basename(os.path.splitext(self.stations)[0]) sims_dir = a_outdir obs_dir = os.path.join(a_tmpdir, "obs_seis_%s" % (sta_base)) # Start with first record irec = 0 # Read station list slo = StationList(sta_file) site_list = slo.getStationList() # Figure out station names station_names = [] for station in site_list: station_names.append(station.scode) # Loop over stations for site in site_list: station = site.scode print("==> Processing station: %s" % (station)) file_sims_acc = os.path.join(sims_dir, "%d.%s.acc.bbp" % (sim_id, station)) file_sims_rd50 = os.path.join(sims_dir, "%d.%s.rd50" % (sim_id, station)) lowcut = site.low_freq_corner highcut = site.high_freq_corner #print(lowcut, highcut) (sims_acc_org_time, sims_acc_org_ns, sims_acc_org_ew, sims_acc_org_ver) = np.genfromtxt(file_sims_acc, skip_header=2, dtype='float64', unpack='TRUE') (sims_perd, sims_rd50_ns, sims_rd50_ew, sims_rd50_ver) = np.genfromtxt(file_sims_rd50, skip_header=2, dtype='float64', unpack='TRUE') file_obs_acc = os.path.join(obs_dir, "%s.bbp" % (station)) file_obs_rd50 = os.path.join(obs_dir, "%s.rd50" % (station)) (obs_acc_org_time, obs_acc_org_ns, obs_acc_org_ew, obs_acc_org_ver) = np.genfromtxt(file_obs_acc, skip_header=2, dtype='float64', unpack='TRUE') (obs_perd, obs_rd50_ns, obs_rd50_ew, obs_rd50_ver) = np.genfromtxt(file_obs_rd50, skip_header=2, dtype='float64', unpack='TRUE') # Intitialize the rd50 arrays RD50PER = len(obs_perd) rd1 = np.zeros(RD50PER) rd2 = np.zeros(RD50PER) rd3 = np.zeros(RD50PER) rd4 = np.zeros(RD50PER) # Resample and align the time series (obs_acc_time, obs_acc_ew, sims_acc_time, sims_acc_ew) = self.align_seismograms(obs_acc_org_time, obs_acc_org_ew, sims_acc_org_time, sims_acc_org_ew) (obs_acc_time, obs_acc_ns, sims_acc_time, sims_acc_ns) = self.align_seismograms(obs_acc_org_time, obs_acc_org_ns, sims_acc_org_time, sims_acc_org_ns) obs_org_dt = obs_acc_org_time[1] - obs_acc_org_time[0] sims_org_dt = sims_acc_org_time[1] - sims_acc_org_time[0] obs_dt = obs_acc_time[1] - obs_acc_time[0] sims_dt = sims_acc_time[1] - sims_acc_time[0] if obs_dt == sims_dt: self.dt = obs_dt fs = 1. / self.dt fnyq = 0.5 * fs # Compute the number of pads for the time series # to have equal number of points for the fft # and for criteria 1 and 2. (sims_acc_ns, sims_acc_ew, obs_acc_ns, obs_acc_ew, ndata) = self.smcpadf(sims_acc_ns, sims_acc_ew, obs_acc_ns, obs_acc_ew, self.dt, lowcut, 8, highcut, 8, 'FALSE') # Start the loop for the different frequency bands for iband in range(len(self.B)): f1 = self.B[iband][0] f2 = self.B[iband][1] # Do the job only if the frequency band is within # the filtered band and if fnyq is higher than f1 if f1 >= lowcut and f2 <= highcut and fnyq >= f2: #print("Working on Period Band :", iband + 1, # "[", 1. / f2, 1. / f1, "]") T1 = 1. / f1 T2 = 1. / f2 t_tmp = sims_perd[(sims_perd <= T1) & (T2 <= sims_perd)] acc_1_flt = self.butter_bandpass(f1, f2, fnyq, sims_acc_ns, 2) acc_2_flt = self.butter_bandpass(f1, f2, fnyq, sims_acc_ew, 2) acc_3_flt = self.butter_bandpass(f1, f2, fnyq, obs_acc_ns, 2) acc_4_flt = self.butter_bandpass(f1, f2, fnyq, obs_acc_ew, 2) # Work on the frequency domain # Do the response spectra # Save the rsp for the specific frequency band rd1 = sims_rd50_ns[(sims_perd <= T1) & (T2 <= sims_perd)] rd2 = sims_rd50_ew[(sims_perd <= T1) & (T2 <= sims_perd)] rd3 = obs_rd50_ns[(obs_perd <= T1) & (T2 <= obs_perd)] rd4 = obs_rd50_ew[(obs_perd <= T1) & (T2 <= obs_perd)] self.C8[irec, iband] = np.nanmean( [self.c8_eval(rd1, rd3, t_tmp), self.c8_eval(rd2, rd4, t_tmp)]) # Now the FFT # Compute the FFT frequencies F = np.fft.fftfreq(ndata, self.dt) # Compute the fft and the amplitudes fft_1 = np.fft.fft(sims_acc_ns) fft_1 = fft_1[(0. <= F) & (f1 <= F) & (F <= f2)] fft_2 = np.fft.fft(sims_acc_ew) fft_2 = fft_2[(0. <= F) & (f1 <= F) & (F <= f2)] fft_3 = np.fft.fft(obs_acc_ns) fft_3 = fft_3[(0. <= F) & (f1 <= F) & (F <= f2)] fft_4 = np.fft.fft(obs_acc_ew) fft_4 = fft_4[(0. <= F) & (f1 <= F) & (F <= f2)] # Slice the FFT frequencies for the working frequency band F = F[(f1 <= F) & (F <= f2)] fs1 = abs(fft_1) / len(fft_1) fs2 = abs(fft_2) / len(fft_2) fs3 = abs(fft_3) / len(fft_3) fs4 = abs(fft_4) / len(fft_4) self.C9[irec, iband] = np.nanmean( [self.c9_eval(fs1, fs3, F), self.c9_eval(fs2, fs4, F)]) # Work on the time domain """ # Compute the site corrected accelerograms acc_3_scor = np.fft.ifft(fft_3) acc_4_scor = np.fft.ifft(fft_4) # These do not need filtering because I'm working # in the sliced frequency domain acc_3_flt = abs(acc_3_scor) acc_4_flt = abs(acc_4_scor) """ vel_1 = self.integ(acc_1_flt, self.dt) vel_2 = self.integ(acc_2_flt, self.dt) vel_3 = self.integ(acc_3_flt, self.dt) vel_4 = self.integ(acc_4_flt, self.dt) dis_1 = self.integ(vel_1, self.dt) dis_2 = self.integ(vel_2, self.dt) dis_3 = self.integ(vel_3, self.dt) dis_4 = self.integ(vel_4, self.dt) c11, c31 = self.c13_eval(acc_1_flt, acc_3_flt) c12, c32 = self.c13_eval(acc_2_flt, acc_4_flt) c21, c41 = self.c24_eval(vel_1, vel_3) c22, c42 = self.c24_eval(vel_2, vel_4) self.C1[irec, iband] = np.nanmean( np.array(c11, c12)) self.C2[irec, iband] = np.nanmean( np.array(c21, c22)) self.C3[irec, iband] = np.nanmean( np.array(c31, c32)) self.C4[irec, iband] = np.nanmean( np.array(c41, c42)) self.C5[irec, iband] = np.nanmean( [self.c5_eval(acc_1_flt, acc_3_flt), self.c5_eval(acc_2_flt, acc_4_flt)]) self.C6[irec, iband] = np.nanmean( [self.c6_eval(vel_1, vel_3), self.c6_eval(vel_2, vel_4)]) self.C7[irec, iband] = np.nanmean( [self.c7_eval(dis_1, dis_3), self.c7_eval(dis_2, dis_4)]) self.C10[irec, iband] = np.nanmean( [self.c10_eval(acc_1_flt, acc_3_flt), self.c10_eval(acc_2_flt, acc_4_flt)]) #print(self.C1[irec, iband], # self.C2[irec, iband], # self.C3[irec, iband], # self.C4[irec, iband], # self.C5[irec, iband], # self.C6[irec, iband], # self.C7[irec, iband], # self.C8[irec, iband], # self.C9[irec, iband], # self.C10[irec, iband]) self.S1[irec] = np.nanmean( np.array([np.nanmean(self.C1[irec, :]), np.nanmean(self.C2[irec, :]), np.nanmean(self.C3[irec, :]), np.nanmean(self.C4[irec, :]), np.nanmean(self.C5[irec, :]), np.nanmean(self.C6[irec, :]), np.nanmean(self.C7[irec, :]), np.nanmean(self.C8[irec, :]), np.nanmean(self.C9[irec, :]), np.nanmean(self.C10[irec, :])])) output_file = os.path.join(a_validation_outdir, "gof-%s-%d-anderson-%s.txt" % (self.eventname, self.sim_id, station)) out_file = open(output_file, 'w') line = ('#%s%5s%4s%4s%4s%4s%4s%4s%4s%4s%4s\n' % ('band', 'C1', 'C2', 'C3', 'C4', 'C5', 'C6', 'C7', 'C8', 'C9', 'C10')) out_file.write(line) for i in range(self.BMAX): line = ('%s %3.1f %3.1f %3.1f %3.1f %3.1f %3.1f %3.1f %3.1f %3.1f %3.1f\n' % (self.BNAMES[i], self.C1[irec, i], self.C2[irec, i], self.C3[irec, i], self.C4[irec, i], self.C5[irec, i], self.C6[irec, i], self.C7[irec, i], self.C8[irec, i], self.C9[irec, i], self.C10[irec, i])) out_file.write(line) out_file.close() output_file = os.path.join(a_validation_outdir, "gof-%s-%d-anderson-%s.png" % (self.eventname, self.sim_id, station)) self.cplots(irec, station, output_file) print('===> Station score :', "{:3.1f}".format(self.S1[irec])) irec = irec + 1 print('==> Total number of stations processed: %d' % (irec)) self.C1 = self.C1[0:irec, :] self.C2 = self.C2[0:irec, :] self.C3 = self.C3[0:irec, :] self.C4 = self.C4[0:irec, :] self.C5 = self.C5[0:irec, :] self.C6 = self.C6[0:irec, :] self.C7 = self.C7[0:irec, :] self.C8 = self.C8[0:irec, :] self.C9 = self.C9[0:irec, :] self.C10 = self.C10[0:irec, :] self.S1 = self.S1[0:irec] c1conf = [self.statts(self.C1[:, i]) for i in range(self.BMAX)] c2conf = [self.statts(self.C2[:, i]) for i in range(self.BMAX)] c3conf = [self.statts(self.C3[:, i]) for i in range(self.BMAX)] c4conf = [self.statts(self.C4[:, i]) for i in range(self.BMAX)] c5conf = [self.statts(self.C5[:, i]) for i in range(self.BMAX)] c6conf = [self.statts(self.C6[:, i]) for i in range(self.BMAX)] c7conf = [self.statts(self.C7[:, i]) for i in range(self.BMAX)] c8conf = [self.statts(self.C8[:, i]) for i in range(self.BMAX)] c9conf = [self.statts(self.C9[:, i]) for i in range(self.BMAX)] c10conf = [self.statts(self.C10[:, i]) for i in range(self.BMAX)] s1_event = np.nanmean(self.S1) print('==> Overall event score:', "{:3.1f}".format(s1_event)) output_file = os.path.join(a_validation_outdir, '%d.gof_anderson.%s.txt' % (self.sim_id, self.eventname)) out_file = open(output_file, 'w') line = ('#%s%5s%4s%4s%4s%4s%4s%4s%4s%4s%4s\n' % ('band', 'C1', 'C2', 'C3', 'C4', 'C5', 'C6', 'C7', 'C8', 'C9', 'C10')) out_file.write(line) for i in range(self.BMAX): line = ('%s %3.1f %3.1f %3.1f %3.1f %3.1f %3.1f %3.1f %3.1f %3.1f %3.1f\n' % (self.BNAMES[i], c1conf[i][0], c2conf[i][0], c3conf[i][0], c4conf[i][0], c5conf[i][0], c6conf[i][0], c7conf[i][0], c8conf[i][0], c9conf[i][0], c10conf[i][0])) out_file.write(line) out_file.close() output_file = os.path.join(a_validation_outdir, "gof-%s-%d-anderson-summary.png" % (self.eventname, self.sim_id)) self.fplots(s1_event, np.asarray(c1conf), np.asarray(c2conf), np.asarray(c3conf), np.asarray(c4conf), np.asarray(c5conf), np.asarray(c6conf), np.asarray(c7conf), np.asarray(c8conf), np.asarray(c9conf), np.asarray(c10conf), output_file) print("Anderson GoF Completed".center(80, '-'))
def run(self): """ Generate an index file in the outdata directory """ print("GenHTML".center(80, '-')) install = InstallCfg.getInstance() sim_id = self.sim_id a_indir = os.path.join(install.A_IN_DATA_DIR, str(sim_id)) a_tmpdir = os.path.join(install.A_TMP_DATA_DIR, str(sim_id)) a_outdir = os.path.join(install.A_OUT_DATA_DIR, str(sim_id)) self.log = os.path.join(install.A_OUT_LOG_DIR, str(sim_id), "%d.genhtml.log" % (sim_id)) a_statfile = os.path.join(a_indir, self.r_stations) a_param_outdir = os.path.join(a_outdir, "param_files") a_param_statfile = os.path.join(a_param_outdir, self.r_stations) if self.r_src_file is not None and self.r_src_file != "": a_src_file = os.path.join(a_indir, self.r_src_file) a_param_srcfile = os.path.join(a_param_outdir, self.r_src_file) src_props = bband_utils.parse_properties(a_src_file) if "seed" in src_props: seed = src_props["seed"] else: seed = "not available" else: a_src_file = None a_param_srcfile = None # Make sure tmpdir, outdir exist dirs = [a_tmpdir, a_outdir, a_param_outdir] bband_utils.mkdirs(dirs, print_cmd=False) # Copy station list, srf_file to outdir's param_files directory shutil.copy2(a_statfile, a_param_statfile) if a_param_srcfile is not None: shutil.copy2(a_src_file, a_param_srcfile) # Get pointer to the velocity model object vel_obj = velocity_models.get_velocity_model_by_name(self.vmodel_name) if vel_obj is None: raise bband_utils.ParameterError("Cannot find velocity model: %s" % (self.vmodel_name)) vel_version = ("%s - %s" % (vel_obj.get_name(), vel_obj.get_version())) # Get pointer to validation object, if any val_version = None if self.val_name: val_obj = validation_cfg.VE_EVENTS.get_event_by_name(self.val_name) if val_obj is not None: val_version = ("%s - %s" % (val_obj.get_print_name(), val_obj.get_version())) # # Read and parse the station list with this call # slo = StationList(a_statfile) site_list = slo.getStationList() index_file = os.path.join(a_outdir, "index-%d.html" % (sim_id)) idxout = open(index_file, 'w') idxout.write("<html>\n") idxout.write("<title>Results for simulation %d</title>\n" % (sim_id)) idxout.write("<body>\n") idxout.write("<h2>Simulation Results</h2>\n") idxout.write("<table>\n") idxout.write("<tr>\n") idxout.write("<td>Broadband Version</td>\n") idxout.write("<td>%s</td>\n" % (install.VERSION)) idxout.write("</tr>\n") idxout.write("<tr>\n") idxout.write("<td>Velocity model version</td>\n") idxout.write("<td>%s</td>\n" % (vel_version)) idxout.write("</tr>\n") if val_version: idxout.write("<tr>\n") idxout.write("<td>Validation package version</td>\n") idxout.write("<td>%s</td>\n" % (val_version)) idxout.write("</tr>\n") if install.start_time is not None: idxout.write("<tr>\n") idxout.write("<td>Simulation Start Time</td>\n") idxout.write("<td>%s</td>\n" % (time.strftime("%a %d %b %Y %X %Z", install.start_time))) idxout.write("</tr>\n") idxout.write("<tr>\n") idxout.write("<td>Simulation End Time</td>\n") idxout.write("<td>%s</td>\n" % (time.strftime("%a %d %b %Y %X %Z", time.localtime()))) idxout.write("</tr>\n") idxout.write("<tr>\n") idxout.write("<td>Simulation ID</td>\n") idxout.write("<td>%d</td>\n" % (sim_id)) idxout.write("</tr>\n") idxout.write("<tr>\n") idxout.write("<td>Simulation Method</td>\n") idxout.write("<td>%s</td>\n" % (self.method)) idxout.write("</tr>\n") # Add xml file if os.path.exists(os.path.join(a_outdir, "%d.xml" % (sim_id))): idxout.write("<tr>\n") idxout.write("<td>Sim Spec</td>\n") idxout.write('<td><a href="%s">%s</a></td>\n' % (os.path.join(".", "%d.xml" % (sim_id)), "%d.xml" % (sim_id))) idxout.write("</tr>\n") # Add station list and src_file if os.path.exists(os.path.join(a_param_outdir, self.r_stations)): idxout.write("<tr>\n") idxout.write("<td>Station List</td>\n") idxout.write('<td><a href="%s">%s</a></td>\n' % (os.path.join(".", "param_files", self.r_stations), self.r_stations)) idxout.write("</tr>\n") if a_param_srcfile is not None: if os.path.exists(os.path.join(a_param_outdir, self.r_src_file)): idxout.write("<tr>\n") idxout.write("<td>Source Description</td>\n") idxout.write('<td><a href="%s">%s</a></td>\n' % (os.path.join(".", "param_files", self.r_src_file), self.r_src_file)) idxout.write("</tr>\n") idxout.write("<tr>\n") idxout.write("<td>Random Seed</td>\n") idxout.write('<td>%s</td>\n' % (seed)) idxout.write("</tr>\n") # Get bias plots dist_lin_plot = glob.glob(os.path.join(a_outdir, "gof-dist-lin*.png")) dist_log_plot = glob.glob(os.path.join(a_outdir, "gof-dist-log*.png")) plots = glob.glob(os.path.join(a_outdir, "gof*.png")) rd50plot = glob.glob(os.path.join(a_outdir, "gof*-rd50.png")) gmpegofplot = glob.glob(os.path.join(a_outdir, "gof*-GMPE-*.png")) mapgofplot = glob.glob(os.path.join(a_outdir, "gof-map-*.png")) if len(gmpegofplot) == 1: gmpegofplot = gmpegofplot[0] else: gmpegofplot = "" if len(mapgofplot) == 1: mapgofplot = mapgofplot[0] else: mapgofplot = "" if len(dist_lin_plot) == 1: dist_lin_plot = dist_lin_plot[0] else: dist_lin_plot = "" if len(dist_log_plot) == 1: dist_log_plot = dist_log_plot[0] else: dist_log_plot = "" if len(rd50plot) == 1: rd50plot = rd50plot[0] else: if gmpegofplot: rd50plot = [plot for plot in rd50plot if plot != gmpegofplot] if mapgofplot: rd50plot = [plot for plot in rd50plot if plot != mapgofplot] if dist_lin_plot: rd50plot = [plot for plot in rd50plot if plot != dist_lin_plot] if dist_log_plot: rd50plot = [plot for plot in rd50plot if plot != dist_log_plot] if len(rd50plot) == 1: rd50plot = rd50plot[0] else: rd50plot = "" if len(plots) > 1: rspplot = [plot for plot in plots if (plot != rd50plot and plot != gmpegofplot and plot != mapgofplot and plot != dist_lin_plot and plot != dist_log_plot)] if len(rspplot) == 1: rspplot = rspplot[0] else: rspplot = "" else: rspplot = "" gmpegofplot = os.path.basename(gmpegofplot) mapgofplot = os.path.basename(mapgofplot) rd50plot = os.path.basename(rd50plot) rspplot = os.path.basename(rspplot) dist_lin_plot = os.path.basename(dist_lin_plot) dist_log_plot = os.path.basename(dist_log_plot) # Add RotD50 bias plot if rd50plot: idxout.write("<tr>\n") idxout.write("<td>RotD50 Bias Plot</td>\n") idxout.write('<td><a href="%s">%s</a></td>\n' % (os.path.join(".", "%s" % (rd50plot)), "PNG")) idxout.write("</tr>\n") if mapgofplot: idxout.write("<tr>\n") idxout.write("<td>RotD50 Map GOF Plot</td>\n") idxout.write('<td><a href="%s">%s</a></td>\n' % (os.path.join(".", "%s" % (mapgofplot)), "PNG")) idxout.write("</tr>\n") # Add RSP bias plot if rspplot: idxout.write("<tr>\n") idxout.write("<td>Respect Bias Plot</td>\n") idxout.write('<td><a href="%s">%s</a></td>\n' % (os.path.join(".", "%s" % (rspplot)), "PNG")) idxout.write("</tr>\n") # Add the GMPE bias plot if gmpegofplot: idxout.write("<tr>\n") idxout.write("<td>GMPE Comparison Bias Plot</td>\n") idxout.write('<td><a href="%s">%s</a></td>\n' % (os.path.join(".", "%s" % (gmpegofplot)), "PNG")) idxout.write("</tr>\n") # Add distance plots if dist_lin_plot: idxout.write("<tr>\n") idxout.write("<td>RotD50 Dist Bias Linear</td>\n") idxout.write('<td><a href="%s">%s</a></td>\n' % (os.path.join(".", "%s" % (dist_lin_plot)), "PNG")) idxout.write("</tr>\n") if dist_log_plot: idxout.write("<tr>\n") idxout.write("<td>RotD50 Dist Bias Log</td>\n") idxout.write('<td><a href="%s">%s</a></td>\n' % (os.path.join(".", "%s" % (dist_log_plot)), "PNG")) idxout.write("</tr>\n") # Add station map and kml file if os.path.exists(os.path.join(a_outdir, "station_map.png")): idxout.write("<tr>\n") idxout.write("<td>Station Map</td>\n") idxout.write('<td><a href="%s">%s</a></td>\n' % (os.path.join(".", "station_map.png"), "PNG")) if os.path.exists(os.path.join(a_outdir, "station_map.kml")): idxout.write('<td><a href="%s">%s</a></td>\n' % (os.path.join(".", "station_map.kml"), "KML")) idxout.write("</tr>\n") # Now get SRF file and plot srfs = glob.glob(os.path.join(a_outdir, "*.srf")) if len(srfs) == 1: srffile = os.path.basename(srfs[0]) srfplot = ("%s.png" % (os.path.basename(os.path.splitext(srffile)[0]))) if not os.path.exists(os.path.join(a_outdir, srfplot)): srfplot = "" else: srffile = "" srfplot = "" if srffile: idxout.write("<tr>\n") idxout.write("<td>Rupture file</td>\n") idxout.write('<td><a href="%s">%s</a></td>\n' % (os.path.join(".", srffile), "data")) if srfplot: idxout.write('<td><a href="%s">%s</a></td>\n' % (os.path.join(".", srfplot), "PNG")) idxout.write("</tr>\n") idxout.write("</table>\n") idxout.write("<p><p>\n") for sits in site_list: site = sits.scode idxout.write("<p>\n") idxout.write("<h2>%s</h2>\n" % (site)) idxout.write("<table>\n") # Find all files velfile = "%d.%s.vel.bbp" % (sim_id, site) velplot = "%d.%s_velocity_seis.png" % (sim_id, site) accfile = "%d.%s.acc.bbp" % (sim_id, site) accplot = "%d.%s_acceleration_seis.png" % (sim_id, site) rd50file = "%d.%s.rd50" % (sim_id, site) rspfile = "%d.%s.rsp" % (sim_id, site) rd50plot = glob.glob(os.path.join(a_outdir, "*_%d_%s_rotd50.png" % (sim_id, site))) if len(rd50plot) == 1: rd50plot = os.path.basename(rd50plot[0]) else: rd50plot = "" rspplot = glob.glob(os.path.join(a_outdir, "*_%d_%s_rsp.png" % (sim_id, site))) if len(rspplot) == 1: rspplot = os.path.basename(rspplot[0]) else: rspplot = "" overlayfile = glob.glob(os.path.join(a_outdir, "*_%d_%s_overlay.png" % (sim_id, site))) if len(overlayfile) == 1: overlayfile = os.path.basename(overlayfile[0]) else: overlayfile = "" gmpeplot = glob.glob(os.path.join(a_outdir, "*_%d_%s_gmpe.png" % (sim_id, site))) if len(gmpeplot) == 1: gmpeplot = os.path.basename(gmpeplot[0]) else: gmpeplot = "" if os.path.exists(os.path.join(a_outdir, velfile)): idxout.write("<tr>\n") idxout.write("<td>Velocity</td>\n") idxout.write('<td><a href="%s">%s</a></td>\n' % (os.path.join(".", velfile), "BBP")) if os.path.exists(os.path.join(a_outdir, velplot)): idxout.write('<td><a href="%s">%s</a></td>\n' % (os.path.join(".", velplot), "PNG")) idxout.write("</tr>\n") if os.path.exists(os.path.join(a_outdir, accfile)): idxout.write("<tr>\n") idxout.write("<td>Acceleration</td>\n") idxout.write('<td><a href="%s">%s</a></td>\n' % (os.path.join(".", accfile), "BBP")) if os.path.exists(os.path.join(a_outdir, accplot)): idxout.write('<td><a href="%s">%s</a></td>\n' % (os.path.join(".", accplot), "PNG")) idxout.write("</tr>\n") if os.path.exists(os.path.join(a_outdir, rd50file)): idxout.write("<tr>\n") idxout.write("<td>RotD50</td>\n") idxout.write('<td><a href="%s">%s</a></td>\n' % (os.path.join(".", rd50file), "data")) if rd50plot: idxout.write('<td><a href="%s">%s</a></td>\n' % (os.path.join(".", rd50plot), "PNG")) idxout.write("</tr>\n") if os.path.exists(os.path.join(a_outdir, rspfile)): idxout.write("<tr>\n") idxout.write("<td>Respect</td>\n") idxout.write('<td><a href="%s">%s</a></td>\n' % (os.path.join(".", rspfile), "data")) if rspplot: idxout.write('<td><a href="%s">%s</a></td>\n' % (os.path.join(".", rspplot), "PNG")) idxout.write("</tr>\n") if overlayfile: idxout.write("<tr>\n") idxout.write("<td>Overlay</td>\n") idxout.write('<td><a href="%s">%s</a></td>\n' % (os.path.join(".", overlayfile), "PNG")) idxout.write("</tr>\n") if gmpeplot: idxout.write("<tr>\n") idxout.write("<td>GMPE Plot</td>\n") idxout.write('<td><a href="%s">%s</a></td>\n' % (os.path.join(".", gmpeplot), "PNG")) idxout.write("</tr>\n") idxout.write("</table>\n") idxout.write("</body>\n") idxout.write("</html>\n") idxout.close() print("==> Wrote file: %s" % (index_file)) print("GenHTML Completed".center(80, '-'))
def run(self): """ This function copies the observed seismograms for the stations specified in r_stations to a temporary directory inside the tmpdata directory and converts them to the format needed by the goodness of fitness module """ print("ObsSeismograms".center(80, '-')) # Initialize basic variables install = InstallCfg.getInstance() sim_id = self.sim_id sta_base = os.path.basename(os.path.splitext(self.r_stations)[0]) self.log = os.path.join(install.A_OUT_LOG_DIR, str(sim_id), "%d.obs_seis.log" % (sim_id)) # Input, tmp, and output directories a_indir = os.path.join(install.A_IN_DATA_DIR, str(sim_id)) a_tmpdir = os.path.join(install.A_TMP_DATA_DIR, str(sim_id)) a_tmpdir_seis = os.path.join(install.A_TMP_DATA_DIR, str(sim_id), "obs_seis_%s" % (sta_base)) a_outdir = os.path.join(install.A_OUT_DATA_DIR, str(sim_id)) a_outdir_seis = os.path.join(a_outdir, "obs_seis_%s" % (sta_base)) # # Make sure the output and tmp directories exist # dirs = [a_tmpdir, a_tmpdir_seis, a_outdir, a_outdir_seis] bband_utils.mkdirs(dirs, print_cmd=False) # Station file a_statfile = os.path.join(a_indir, self.r_stations) # List of observed seismogram files filelist = os.listdir(self.a_obsdir) slo = StationList(a_statfile) site_list = slo.getStationList() # Inialize the CorrectPSA module if self.obs_corrections: corr_psa = CorrectPSA(self.r_stations, "rd50", os.path.join(a_indir, self.obs_corrections), a_tmpdir_seis, self.sim_id) else: corr_psa = None # Go through each station for site in site_list: slon = float(site.lon) slat = float(site.lat) stat = site.scode print("==> Processing data for station: %s" % (stat)) # Since we're using the GP station list, make sure the # .rd50 for the station exists. It might not if we ran the # validation with a different station list (like UCSB for # Landers) expected_rd50_file = os.path.join(a_outdir, "%d.%s.rd50" % (sim_id, stat)) if not os.path.exists(expected_rd50_file): # just skip it print("Couldn't find file %s. " % (expected_rd50_file) + "This is not necessarily an error, as you may have " + "run with a subset of a stations. Continuing " + "with available stations.") continue # Ok, we have a calculated rd50 file for this station, # let's look for the observed file r_e_peer_file = None r_n_peer_file = None r_z_peer_file = None r_bbp_file = "%s.bbp" % (stat) # Do different things depending on the format of the # observed seismograms if self.obs_format == "acc_bbp": # We need to look for the bbp file if r_bbp_file not in filelist: # No bbp file for this station continue print("==> Converting file: %s" % (r_bbp_file)) # Copy bbp file to the tmp seismogram directory a_src_bbp_file = os.path.join(self.a_obsdir, r_bbp_file) a_dst_bbp_file = os.path.join(a_tmpdir_seis, r_bbp_file) shutil.copy2(a_src_bbp_file, a_dst_bbp_file) # Now we need to create the peer files to process with rotd50 r_e_peer_file = os.path.join(a_tmpdir_seis, "%s_E.acc" % (stat)) r_n_peer_file = os.path.join(a_tmpdir_seis, "%s_N.acc" % (stat)) r_z_peer_file = os.path.join(a_tmpdir_seis, "%s_Z.acc" % (stat)) bbp_formatter.bbp2peer(a_dst_bbp_file, r_n_peer_file, r_e_peer_file, r_z_peer_file) elif self.obs_format == "acc_peer": # Look for the E, N, and Z files for my_file in filelist: if my_file.endswith("%s_E.acc" % (stat)): r_e_peer_file = my_file if (r_n_peer_file is not None and r_z_peer_file is not None): break elif my_file.endswith("%s_N.acc" % (stat)): r_n_peer_file = my_file if (r_e_peer_file is not None and r_z_peer_file is not None): break elif my_file.endswith("%s_Z.acc" % (stat)): r_z_peer_file = my_file if (r_e_peer_file is not None and r_n_peer_file is not None): break if ((r_e_peer_file is None) or (r_n_peer_file is None) or (r_z_peer_file is None)): # Couldn't find all 3 files continue # print(r_e_peer_file, r_n_peer_file, r_z_peer_file) # Copy all three files to the tmp seismogram directory for eachfile in (r_e_peer_file, r_n_peer_file, r_z_peer_file): a_src_peer_file = os.path.join(self.a_obsdir, eachfile) a_dst_peer_file = os.path.join(a_tmpdir_seis, eachfile) shutil.copy2(a_src_peer_file, a_dst_peer_file) # Now we need to convert them into bbp format bbp_formatter.peer2bbp(os.path.join(a_tmpdir_seis, r_n_peer_file), os.path.join(a_tmpdir_seis, r_e_peer_file), os.path.join(a_tmpdir_seis, r_z_peer_file), os.path.join(a_tmpdir_seis, r_bbp_file)) elif self.obs_format == "gmpe": # GMPE verification packages don't have actual # seismograms, so there's nothing we need to do here! r_gmpe_file = "%s-gmpe.ri50" % (stat) if r_gmpe_file not in filelist: # No gmpe file for this station continue # Copy gmpe file to the tmp obs directory and rename # it so that it has a rd50 extension a_src_gmpe_file = os.path.join(self.a_obsdir, r_gmpe_file) a_dst_rd50_file = os.path.join(a_tmpdir_seis, "%s.rd50" % (stat)) shutil.copy2(a_src_gmpe_file, a_dst_rd50_file) # Create a copy in outdata that averages all gmpes a_avg_rd50_file = os.path.join(a_outdir_seis, "%s.rd50" % (stat)) gmpe_config.average_gmpe(stat, a_src_gmpe_file, a_avg_rd50_file) # All done! continue else: raise bband_utils.ParameterError("Format %s for " % (self.obs_format) + "observed seismograms " "not supported") # Run RotD50 on this file if corr_psa is not None: # First calculate rd50 and psa5 files print("===> Calculating RotD50 for station: %s" % (stat)) RotD50.do_rotd50(a_tmpdir_seis, r_e_peer_file, r_n_peer_file, r_z_peer_file, "%s-orig.rd50" % (stat), self.log) # Now we need to correct the RotD50 output using the # user-supplied correction factors print("===> Correcting PSA for station: %s" % (stat)) corr_psa.correct_station(stat, "rd50") else: # Use final names for output files print("===> Calculating RotD50 for station: %s" % (stat)) RotD50.do_rotd50(a_tmpdir_seis, r_e_peer_file, r_n_peer_file, r_z_peer_file, "%s.rd50" % (stat), self.log) shutil.copy2(os.path.join(a_tmpdir_seis, "%s.rd50" % (stat)), os.path.join(a_outdir_seis, "%s.rd50" % (stat))) print("ObsSeismograms Completed".center(80, '-'))
def run(self): """ This function creates GMPE plots for all stations """ print("GMPE Plot".center(80, '-')) # Initialize basic variables install = InstallCfg.getInstance() sim_id = self.sim_id sta_base = os.path.basename(os.path.splitext(self.r_stations)[0]) self.log = os.path.join(install.A_OUT_LOG_DIR, str(sim_id), "%d.gmpe_gof.log" % (sim_id)) # Input, tmp, and output directories a_tmpdir = os.path.join(install.A_TMP_DATA_DIR, str(sim_id)) a_outdir = os.path.join(install.A_OUT_DATA_DIR, str(sim_id)) a_outdir_gmpe = os.path.join(install.A_OUT_DATA_DIR, str(sim_id), "gmpe_data_%s" % (sta_base)) # # Make sure the output and tmp directories exist # dirs = [a_tmpdir, a_outdir, a_outdir_gmpe] bband_utils.mkdirs(dirs, print_cmd=False) # Figure out gmpe labels gmpe_group = gmpe_config.GMPES[self.gmpe_group_name] gmpe_labels = gmpe_group["labels"] # Station file a_statfile = os.path.join(install.A_IN_DATA_DIR, str(sim_id), self.r_stations) # List of gmpe files filelist = os.listdir(a_outdir_gmpe) slo = StationList(a_statfile) site_list = slo.getStationList() # Go through each station for site in site_list: stat = site.scode print("==> Generating plot for station: %s" % (stat)) # Since we're using the GP station list, make sure the # .rd50 for the station exists. It might not if we ran the # validation with a shorter station list sim_file = os.path.join(a_outdir, "%d.%s.rd50" % (sim_id, stat)) if not os.path.exists(sim_file): # just skip it print("Couldn't find file %s. " % (sim_file) + "This is not necessarily an error, as you may have " + "run with a subset of a stations. Continuing " + "with available stations.") continue # Ok, we have the calculated rd50 for this station # Look for the gmpe file r_gmpe_file = "%s-gmpe.ri50" % (stat) if r_gmpe_file not in filelist: # No gmpe file for this station continue a_gmpe_file = os.path.join(a_outdir_gmpe, r_gmpe_file) # Plot GMPE rotd50 results outfile = os.path.join(a_outdir, "%s_%d_%s_gmpe.png" % (self.comp_label, sim_id, stat)) plot_gmpe.plot_gmpe(stat, sim_file, a_gmpe_file, gmpe_labels, sim_id, self.comp_label, outfile) print("GMPE Plot Completed".center(80, '-'))
def run(self): """ Run the UCSB Stitch code """ print("UCSB Stitch".center(80, '-')) install = InstallCfg.getInstance() sim_id = self.sim_id sta_base = os.path.basename(os.path.splitext(self.r_stations)[0]) self.log = os.path.join(install.A_OUT_LOG_DIR, str(sim_id), "%d.uc_stitch_%s.log" % (sim_id, sta_base)) a_indir = os.path.join(install.A_IN_DATA_DIR, str(sim_id)) a_tmpdir = os.path.join(install.A_TMP_DATA_DIR, str(sim_id)) a_tmpdir_mod = os.path.join(install.A_TMP_DATA_DIR, str(self.sim_id), "uc_stitch_%s" % (sta_base)) a_outdir = os.path.join(install.A_OUT_DATA_DIR, str(sim_id)) # # Make sure the outpute and tmp directories exist # bband_utils.mkdirs([a_tmpdir, a_tmpdir_mod, a_outdir], print_cmd=False) a_velocity = os.path.join(a_indir, self.r_velocity) print("UC_stich - SRF File: %s" % (self.r_srffile)) if not os.path.isfile(self.r_srffile): a_srffile = os.path.join(a_indir, self.r_srffile) if not os.path.isfile(a_srffile): print("Error (uc_stich): Unable to locate SRF file:", a_srffile, "\nExiting Broadband...") sys.exit(1) else: a_srffile = self.r_srffile # Copy srf and velocity files to tmpdir shutil.copy2(a_srffile, os.path.join(a_tmpdir_mod, self.r_srffile)) shutil.copy2(a_velocity, os.path.join(a_tmpdir_mod, self.r_velocity)) # Store cwd and change over to tmpdir so the executable can # find the files old_cwd = os.getcwd() os.chdir(a_tmpdir_mod) a_stations = os.path.join(a_indir, self.r_stations) print(a_stations) slo = StationList(a_stations) site_list = slo.getStationList() # Need stations in UCSB LL format a_uc_stations = os.path.join(a_tmpdir_mod, "stations.ll") a_uc_vs30 = os.path.join(a_tmpdir_mod, "stations.vs30") stas2files.gp2uc_stalist(slo, a_uc_stations, a_uc_vs30) # Convert stations to UCSB XY format, if it doesn't exist if not os.path.exists("stations.xy"): r_faultfile = "faultGlobal.in" a_faultfile = os.path.join(a_tmpdir_mod, r_faultfile) if not os.path.isfile(a_faultfile): if os.path.isfile(os.path.join(a_indir, r_faultfile)): shutil.copy2(os.path.join(a_indir, r_faultfile), a_faultfile) else: print("Extracting faultGlobal.in from " "SRF and velocity model.") fp = open("faultGlobalTmp", 'w') fp.write("%s\n" % self.r_velocity) fp.write("%s\n" % self.r_srffile) fp.close() cmd = ("%s/getfaultGlobal < faultGlobalTmp >> %s 2>&1" % (install.A_UCSB_BIN_DIR, self.log)) bband_utils.runprog(cmd) cmd = "%s/statLL2XY >> %s 2>&1 " % (install.A_UCSB_BIN_DIR, self.log) bband_utils.runprog(cmd) #write stitch BBP sBBP_in = open("stitchBBP.inp", "w") #stations list sBBP_in.write("stations.xy\n") #velocity file list sBBP_in.write("VMname.list\n") #dir with 1D synthetics (<stat>.3comp) sBBP_in.write("%s/\n" % a_tmpdir) #dir with non-linear 3D synthetics (<stat>.<comp>.nl1D.001) sBBP_in.write("%s/\n" % a_tmpdir) #joint frequency, fmax sBBP_in.write("1.0, 15.0\n") # depth of hypocenter if self.r_srcfile is not None and self.r_srcfile != "": a_srcfile = os.path.join(a_indir, self.r_srcfile) hypo_dep = fault_utils.calculate_hypo_depth(a_srcfile) elif self.r_srffile is not None and self.r_srffile != "": hypo_dep = fault_utils.get_hypocenter(a_srffile, sta_base)[2] else: # No way to determine hypocenter depth, existing print("No way to determine hypocenter depth, exiting!") sys.exit(1) sBBP_in.write("%s\n" % hypo_dep) #source model sBBP_in.write("1\n") #output format 2=velocity sBBP_in.write("2\n") sBBP_in.flush() sBBP_in.close() vMname_in = open("%s/VMname.list" % a_tmpdir_mod, "w") for stat in site_list: vMname_in.write("%s\n" % self.r_velocity) vMname_in.flush() vMname_in.close() #stitch expects <stat>.3comp for LF and <stat>.<comp>.nl1D.001 for HF for stat in site_list: if self.acc: if os.path.exists("%s/%d.%s-lf.acc.bbp" % (a_tmpdir, sim_id, stat.scode)): #integrate to velocity cmd = "%s/wcc2bbp nsfile=%s/%d.%s-lf.acc.000 ewfile=%s/%d.%s-lf.acc.090 udfile=%s/%d.%s-lf.acc.ver wcc2bbp=0 < %s/%d.%s-lf.acc.bbp >> %s 2>&1" % (install.A_GP_BIN_DIR, a_tmpdir, sim_id, stat.scode, a_tmpdir, sim_id, stat.scode, a_tmpdir, sim_id, stat.scode, a_tmpdir, sim_id, stat.scode, self.log) bband_utils.runprog(cmd) for comp in ['000', '090', 'ver']: cmd = "%s/integ_diff integ=1 filein=%s/%d.%s-lf.acc.%s fileout=%s/%d.%s-lf.vel.%s" % (install.A_GP_BIN_DIR, a_tmpdir, sim_id, stat.scode, comp, a_tmpdir, sim_id, stat.scode, comp) bband_utils.runprog(cmd) cmd = "%s/wcc2bbp nsfile=%s/%d.%s-lf.vel.000 ewfile=%s/%d.%s-lf.vel.090 udfile=%s/%d.%s-lf.vel.ver units=cm/s wcc2bbp=1 > %s/%d.%s-lf.bbp" % (install.A_GP_BIN_DIR, a_tmpdir, sim_id, stat.scode, a_tmpdir, sim_id, stat.scode, a_tmpdir, sim_id, stat.scode, a_tmpdir, sim_id, stat.scode) bband_utils.runprog(cmd) else: print("Can't find LF acceleration file for " "site %s, aborting." % (stat.scode)) sys.exit(2) if os.path.exists("%s/%d.%s.acc.bbp" % (a_tmpdir, sim_id, stat.scode)): #split into components and rename #nl1D requires 1 line with nt dt #followed by 5 entries per line #Assumes UCSB HF, or why would we be running this? cmd = "%s/wcc2bbp nsfile=%s/%d.%s.acc.000 ewfile=%s/%d.%s.acc.090 udfile=%s/%d.%s.acc.ver wcc2bbp=0 < %s/%d.%s.acc.bbp >> %s 2>&1" % (install.A_GP_BIN_DIR, a_tmpdir, sim_id, stat.scode, a_tmpdir, sim_id, stat.scode, a_tmpdir, sim_id, stat.scode, a_tmpdir, sim_id, stat.scode, self.log) bband_utils.runprog(cmd) for comp in ['000', '090', 'ver']: cmd = "%s/integ_diff integ=1 filein=%s/%d.%s.acc.%s fileout=%s/%d.%s.vel.%s" % (install.A_GP_BIN_DIR, a_tmpdir, sim_id, stat.scode, comp, a_tmpdir, sim_id, stat.scode, comp) bband_utils.runprog(cmd) cmd = "%s/wcc2bbp nsfile=%s/%d.%s.vel.000 ewfile=%s/%d.%s.vel.090 udfile=%s/%d.%s.vel.ver units=cm/s wcc2bbp=1 > %s/%d.%s.bbp" % (install.A_GP_BIN_DIR, a_tmpdir, sim_id, stat.scode, a_tmpdir, sim_id, stat.scode, a_tmpdir, sim_id, stat.scode, a_tmpdir, sim_id, stat.scode) bband_utils.runprog(cmd) elif os.path.exists("%s/%d.%s-hf.acc.bbp" % (a_tmpdir, sim_id, stat.scode)): print("Using HF only.") cmd = "%s/wcc2bbp nsfile=%s/%d.%s-hf.acc.000 ewfile=%s/%d.%s-hf.acc.090 udfile=%s/%d.%s-hf.acc.ver wcc2bbp=0 < %s/%d.%s-hf.acc.bbp >> %s 2>&1" % (install.A_GP_BIN_DIR, a_tmpdir, sim_id, stat.scode, a_tmpdir, sim_id, stat.scode, a_tmpdir, sim_id, stat.scode, a_tmpdir, sim_id, stat.scode, self.log) bband_utils.runprog(cmd) for comp in ['000', '090', 'ver']: cmd = "%s/integ_diff integ=1 filein=%s/%d.%s-hf.acc.%s fileout=%s/%d.%s-hf.vel.%s" % (install.A_GP_BIN_DIR, a_tmpdir, sim_id, stat.scode, comp, a_tmpdir, sim_id, stat.scode, comp) bband_utils.runprog(cmd) cmd = "%s/wcc2bbp nsfile=%s/%d.%s-hf.vel.000 ewfile=%s/%d.%s-hf.vel.090 udfile=%s/%d.%s-hf.vel.ver units=cm/s wcc2bbp=1 > %s/%d.%s-hf.bbp" % (install.A_GP_BIN_DIR, a_tmpdir, sim_id, stat.scode, a_tmpdir, sim_id, stat.scode, a_tmpdir, sim_id, stat.scode, a_tmpdir, sim_id, stat.scode) bband_utils.runprog(cmd) else: print("Can't find HF acceleration file for " "site %s, aborting." % (stat.scode)) if os.path.exists("%s/%d.%s-lf.bbp" % (a_tmpdir, sim_id, stat.scode)): shutil.copy2("%s/%d.%s-lf.bbp" % (a_tmpdir, sim_id, stat.scode), "%s/%d.%s-lf.prestitch.bbp" % (a_tmpdir, sim_id, stat.scode)) shutil.copy2("%s/%d.%s-lf.bbp" % (a_tmpdir, sim_id, stat.scode), "%s/%s.3comp" % (a_tmpdir, stat.scode)) else: print("Can't find LF velocity file for site %s, aborting." % (stat.scode)) sys.exit(2) if os.path.exists("%s/%d.%s.bbp" % (a_tmpdir, sim_id, stat.scode)): #split into components and rename #nl1D requires 1 line with nt dt #followed by 5 entries per line #Assumes UCSB HF, or why would we be running this? shutil.copy2("%s/%d.%s.bbp" % (a_tmpdir, sim_id, stat.scode), "%s/%d.%s.prestitch.bbp" % (a_tmpdir, sim_id, stat.scode)) self.split_bbp(a_tmpdir, stat.scode, "%s/%d.%s.bbp" % (a_tmpdir, sim_id, stat.scode)) elif os.path.exists("%s/%d.%s-hf.bbp" % (a_tmpdir, sim_id, stat.scode)): shutil.copy2("%s/%d.%s-hf.bbp" % (a_tmpdir, sim_id, stat.scode), "%s/%d.%s-hf.prestitch.bbp" % (a_tmpdir, sim_id, stat.scode)) self.split_bbp(a_tmpdir, stat.scode, "%s/%d.%s-hf.bbp" % (a_tmpdir, sim_id, stat.scode)) else: print("Can't find HF velocity file for site %s, aborting." % (stat.scode)) cmd = "%s/stitchBBP >> %s 2>&1" % (install.A_UCSB_BIN_DIR, self.log) bband_utils.runprog(cmd) #reconstitute BBP file for stat in site_list: print("%s/%s.000.gmBB.001" % (a_tmpdir, stat.scode)) if os.path.exists("%s/%s.000.gmBB.001" % (a_tmpdir_mod, stat.scode)): self.make_bbp(a_tmpdir_mod, stat.scode) shutil.copy2("%s/%d.%s-stitch.bbp" % (a_tmpdir_mod, sim_id, stat.scode), "%s/%d.%s.vel.bbp" % (a_tmpdir, sim_id, stat.scode)) shutil.copy2("%s/%d.%s-stitch.bbp" % (a_tmpdir_mod, sim_id, stat.scode), "%s/%d.%s.vel.bbp" % (a_outdir, sim_id, stat.scode)) # Create acceleration seismogram # Create path names and check if their sizes are # within bounds nsfile = os.path.join(a_tmpdir, "%d.%s.000" % (sim_id, stat.scode)) ewfile = os.path.join(a_tmpdir, "%d.%s.090" % (sim_id, stat.scode)) udfile = os.path.join(a_tmpdir, "%d.%s.ver" % (sim_id, stat.scode)) bbpfile = os.path.join(a_tmpdir, "%d.%s.vel.bbp" % (sim_id, stat.scode)) bband_utils.check_path_lengths([nsfile, ewfile, udfile], bband_utils.GP_MAX_FILENAME) cmd = ("%s/wcc2bbp " % (install.A_GP_BIN_DIR) + "nsfile=%s ewfile=%s udfile=%s " % (nsfile, ewfile, udfile) + "wcc2bbp=0 < %s >> %s 2>&1" % (bbpfile, self.log)) bband_utils.runprog(cmd, abort_on_error=True) for comp in ['000', '090', 'ver']: # Differentiate each component filein = os.path.join(a_tmpdir, "%d.%s.%s" % (sim_id, stat.scode, comp)) fileout = os.path.join(a_tmpdir, "%d.%s.acc.%s" % (sim_id, stat.scode, comp)) bband_utils.check_path_lengths([filein, fileout], bband_utils.GP_MAX_FILENAME) cmd = ("%s/integ_diff diff=1 filein=%s fileout=%s" % (install.A_GP_BIN_DIR, filein, fileout)) bband_utils.runprog(cmd, abort_on_error=True) # Create path names and check if their sizes are # within bounds nsfile = os.path.join(a_tmpdir, "%d.%s.acc.000" % (sim_id, stat.scode)) ewfile = os.path.join(a_tmpdir, "%d.%s.acc.090" % (sim_id, stat.scode)) udfile = os.path.join(a_tmpdir, "%d.%s.acc.ver" % (sim_id, stat.scode)) bbpfile = os.path.join(a_tmpdir, "%d.%s.acc.bbp" % (sim_id, stat.scode)) bband_utils.check_path_lengths([nsfile, ewfile, udfile], bband_utils.GP_MAX_FILENAME) cmd = ("%s/wcc2bbp " % (install.A_GP_BIN_DIR) + "nsfile=%s ewfile=%s udfile=%s " % (nsfile, ewfile, udfile) + "units=cm/s/s wcc2bbp=1 > %s 2>> %s" % (bbpfile, self.log)) bband_utils.runprog(cmd, abort_on_error=True) # Copy acceleration bbp file to outdir shutil.copy2(os.path.join(a_tmpdir, "%d.%s.acc.bbp" % (sim_id, stat.scode)), os.path.join(a_outdir, "%d.%s.acc.bbp" % (sim_id, stat.scode))) os.chdir(old_cwd) print("UCSB Stitch Completed".center(80, '-'))
def run(self): """ Runs the UCSB site response program """ print("UCSB Site".center(80, '-')) # # Global installation parameters # install = InstallCfg.getInstance() # # Required Inputs are sim_id, SRC file, and station list # sim_id = self.sim_id sta_base = os.path.basename(os.path.splitext(self.r_stations)[0]) self.log = os.path.join(install.A_OUT_LOG_DIR, str(sim_id), "%d.uc_site_%s.log" % (sim_id, sta_base)) a_indir = os.path.join(install.A_IN_DATA_DIR, str(sim_id)) a_tmpdir = os.path.join(install.A_TMP_DATA_DIR, str(sim_id)) a_tmpdir_mod = os.path.join(install.A_TMP_DATA_DIR, str(sim_id), "uc_site_%s" % (sta_base)) a_outdir = os.path.join(install.A_OUT_DATA_DIR, str(sim_id)) a_velocity = os.path.join(a_indir, self.r_velocity) # # Make sure the output and tmp directories exist # bband_utils.mkdirs([a_tmpdir, a_tmpdir_mod, a_outdir], print_cmd=False) # Parse SRC file if self.r_srcfile is None or self.r_srcfile == "": raise bband_utils.ParameterError("SRC file not defined!") a_srcfile = os.path.join(a_indir, self.r_srcfile) self.cfg = UCSiteCfg(a_srcfile) cfg = self.cfg # Store cwd and change over to tmpdir so the executable can # find the files old_cwd = os.getcwd() os.chdir(a_tmpdir_mod) # Copy velocity file to tmpdir_mod shutil.copy2(a_velocity, os.path.join(a_tmpdir_mod, self.r_velocity)) # Read station list a_stations = os.path.join(a_indir, self.r_stations) print(a_stations) slo = StationList(a_stations) site_list = slo.getStationList() # This is not a UCSB format station list, convert station # list to UCSB format, generating the station file and the # vs30 file a_uc_stations = os.path.join(a_tmpdir_mod, cfg.R_UC_STATION_FILE) a_uc_vs30 = os.path.join(a_tmpdir_mod, cfg.R_UC_VS30_FILE) stas2files.gp2uc_stalist(slo, a_uc_stations, a_uc_vs30) # # The UCSB codes require fixed input names. So here, we copy # the UCSB file over to the expected name "stations.ll" # cmd = ("cp %s %s" % (a_uc_stations, os.path.join(a_tmpdir_mod, "stations.ll"))) bband_utils.runprog(cmd) # Copy .bbp files over to .3comp # If we have anything but just a hybrid file, combine them first # Use site 0 as the dummy for site in site_list: if os.path.exists("%s/%d.%s.bbp" % (a_tmpdir, sim_id, site.scode)): shutil.copy2("%s/%d.%s.bbp" % (a_tmpdir, sim_id, site.scode), "%s/%s.3comp" % (a_tmpdir_mod, site.scode)) elif os.path.exists("%s/%s.3comp" % (a_tmpdir, site.scode)): shutil.copy2("%s/%s.3comp" % (a_tmpdir, site.scode), "%s/%s.3comp" % (a_tmpdir_mod, site.scode)) # determine dt for input seismogram bbp_fp = open("%s/%s.3comp" % (a_tmpdir_mod, site.scode), 'r') bbp_data = bbp_fp.readlines() bbp_fp.close() i = 0 while bbp_data[i][0] == '%' or bbp_data[i][0] == '#': i += 1 t0 = float(bbp_data[i].split()[0]) t1 = float(bbp_data[i+1].split()[0]) input_dt = t1-t0 print("input_dt: %f\n" % (input_dt)) # # Create deconvBBP.inp, stitchBBP.inp, VMname.list # dBBP_in = open("deconvBBP.inp", "w") dBBP_in.write("%s\n" % self.r_velocity) dBBP_in.write("0.29\n") dBBP_in.write("1\n") dBBP_in.write("%d\n" % len(site_list)) for site in site_list: dBBP_in.write("%s\n" % site.scode) dBBP_in.flush() dBBP_in.close() sBBP_in = open("stitchBBP.inp", "w") sBBP_in.write("stations.xy\n") sBBP_in.write("VMname.list\n") sBBP_in.write("./\n") sBBP_in.write("./\n") sBBP_in.write("1.0, 15.0\n") # depth of hypocenter hypo_dep = fault_utils.calculate_hypo_depth(a_srcfile) sBBP_in.write("%s\n" % hypo_dep) sBBP_in.write("1\n") sBBP_in.write("2\n") sBBP_in.flush() sBBP_in.close() vMname_in = open("VMname.list", "w") for site in site_list: vMname_in.write("%s\n" % self.r_velocity) vMname_in.flush() vMname_in.close() # # Create stations.xy if it doesn't exist yet # if not os.path.exists("stations.xy"): # # Create faultGlobal.in # r_faultfile = "faultGlobal.in" a_faultfile = os.path.join(a_tmpdir_mod, r_faultfile) self.create_fault_global_in(a_faultfile) # # Convert stations to xy # cmd = "%s >> %s 2>&1" % (self.cfg.A_SLL2XY, self.log) bband_utils.runprog(cmd) # # Deconvolve # cmd = "%s >> %s 2>&1" % (self.cfg.A_UC_DECON_EXE, self.log) bband_utils.runprog(cmd) # # Logic of separateStats.csh pulled out into function # stations_to_stitch = self.separate_stats(install, a_uc_vs30, input_dt) # # Stitch # # Update station files to only stitch non class A stations # (Class A stations don't have a non-linear component) # Must use 'stations.xy' because it's in stitchBBP.inp # shutil.copy2("stations.xy", "stations.xy.orig") shutil.copy2("stations.ll", "stations.ll.orig") station_in = open("stations.xy.orig", 'r') station_ll_in = open("stations.ll.orig", "r") station_ll_data = station_ll_in.readlines() station_data = station_in.readlines() station_in.close() station_ll_in.close() station_out = open("stations.xy", "w") station_ll_out = open("stations.ll", "w") pieces = station_data[0].split() station_out.write("%d %f %f %f\n" % (len(stations_to_stitch), float(pieces[1]), float(pieces[2]), float(pieces[3]))) station_ll_out.write("%d\n" % len(stations_to_stitch)) i = 1 while i < len(station_data): inList = False stat_data_name = station_data[i].strip() for site in stations_to_stitch: if stat_data_name == site: inList = True break if inList: station_out.write("%s\n" % stat_data_name) station_out.write("%s" % station_data[i+1]) station_ll_out.write("%s" % station_ll_data[(i+1)//2]) i += 2 station_out.flush() station_ll_out.flush() station_out.close() station_ll_out.close() cmd = "%s >> %s 2>&1" % (self.cfg.A_STITCH, self.log) bband_utils.runprog(cmd) # # Copy original stations file back in # shutil.copy2("stations.xy", "stations.xy.stitch") shutil.copy2("stations.xy.orig", "stations.xy") # Convert to 3-component seismograms # cmd = "%s/conv3CompBB >> %s 2>&1" % (install.A_UCSB_BIN_DIR, self.log) bband_utils.runprog(cmd) shutil.copy2("stations.ll", "stations.ll.stitch") shutil.copy2("stations.ll.orig", "station.ll") # Move the results to the output directory, as bbp format for result_file in os.listdir(a_tmpdir_mod): dot_index = result_file.rfind('.3comp') if dot_index > -1: basename = result_file[0:dot_index] shutil.copy2(result_file, "%s/%d.%s.vel.bbp" % (a_outdir, sim_id, basename)) shutil.copy2(result_file, "%s/%d.%s.vel.bbp" % (a_tmpdir, sim_id, basename)) shutil.copy2(result_file, "%s/%s.3comp" % (a_tmpdir, basename)) # Create acceleration seismogram # Create path names and check if their sizes are # within bounds nsfile = os.path.join(a_tmpdir, "%d.%s.000" % (sim_id, basename)) ewfile = os.path.join(a_tmpdir, "%d.%s.090" % (sim_id, basename)) udfile = os.path.join(a_tmpdir, "%d.%s.ver" % (sim_id, basename)) bbpfile = os.path.join(a_tmpdir, "%d.%s.vel.bbp" % (sim_id, basename)) bband_utils.check_path_lengths([nsfile, ewfile, udfile], bband_utils.GP_MAX_FILENAME) cmd = ("%s/wcc2bbp " % (install.A_GP_BIN_DIR) + "nsfile=%s ewfile=%s udfile=%s " % (nsfile, ewfile, udfile) + "wcc2bbp=0 < %s >> %s 2>&1" % (bbpfile, self.log)) bband_utils.runprog(cmd, abort_on_error=True) for comp in cfg.COMPS: # Differentiate each component filein = os.path.join(a_tmpdir, "%d.%s.%s" % (sim_id, basename, comp)) fileout = os.path.join(a_tmpdir, "%d.%s.acc.%s" % (sim_id, basename, comp)) bband_utils.check_path_lengths([filein, fileout], bband_utils.GP_MAX_FILENAME) cmd = ("%s/integ_diff diff=1 filein=%s fileout=%s" % (install.A_GP_BIN_DIR, filein, fileout)) bband_utils.runprog(cmd, abort_on_error=True) # Create path names and check if their sizes are # within bounds nsfile = os.path.join(a_tmpdir, "%d.%s.acc.000" % (sim_id, basename)) ewfile = os.path.join(a_tmpdir, "%d.%s.acc.090" % (sim_id, basename)) udfile = os.path.join(a_tmpdir, "%d.%s.acc.ver" % (sim_id, basename)) bbpfile = os.path.join(a_tmpdir, "%d.%s.acc.bbp" % (sim_id, basename)) bband_utils.check_path_lengths([nsfile, ewfile, udfile], bband_utils.GP_MAX_FILENAME) cmd = ("%s/wcc2bbp " % (install.A_GP_BIN_DIR) + "nsfile=%s ewfile=%s udfile=%s " % (nsfile, ewfile, udfile) + "units=cm/s/s wcc2bbp=1 > %s 2>> %s" % (bbpfile, self.log)) bband_utils.runprog(cmd, abort_on_error=True) # Copy acceleration bbp file to outdir shutil.copy2(os.path.join(a_tmpdir, "%d.%s.acc.bbp" % (sim_id, basename)), os.path.join(a_outdir, "%d.%s.acc.bbp" % (sim_id, basename))) os.chdir(old_cwd) print("UCSB Site Completed".center(80, '-'))
def run(self): """ Runs the UCSB Syn1D simulator """ print("UCSB Syn1D".center(80, '-')) # # Global installation parameters # install = InstallCfg.getInstance() # # Required inputs are sim_id, the src file, the FFSP output # and station list # sim_id = self.sim_id sta_base = os.path.basename(os.path.splitext(self.r_stations)[0]) self.log = os.path.join(install.A_OUT_LOG_DIR, str(sim_id), "%d.syn1d_%s.log" % (sim_id, sta_base)) self.a_indir = os.path.join(install.A_IN_DATA_DIR, str(sim_id)) self.a_tmpdir = os.path.join(install.A_TMP_DATA_DIR, str(sim_id)) a_tmpdir_lf = os.path.join(install.A_TMP_DATA_DIR, str(sim_id), "syn1D_lf_%s" % (sta_base)) a_tmpdir_hf = os.path.join(install.A_TMP_DATA_DIR, str(sim_id), "syn1D_hf_%s" % (sta_base)) a_tmpdir_stitch = os.path.join(install.A_TMP_DATA_DIR, str(sim_id), "stitch_%s" % (sta_base)) a_outdir = os.path.join(install.A_OUT_DATA_DIR, str(sim_id)) # # Make sure the output and tmp directories exist # bband_utils.mkdirs([self.a_tmpdir, a_tmpdir_lf, a_tmpdir_hf, a_tmpdir_stitch, a_outdir], print_cmd=False) # Parse SRC file a_srcfile = os.path.join(self.a_indir, self.r_srcfile) self.cfg = Syn1DCfg(self.vmodel_name, a_srcfile) # Read station list a_stations = os.path.join(self.a_indir, self.r_stations) print(a_stations) self.slo = StationList(a_stations) site_list = self.slo.getStationList() # Make sure syn1D can handle our station list if len(site_list) > self.cfg.MAX_STATIONS: raise bband_utils.ParameterError("Too many stations in " "the station list: %d. " % (len(site_list)) + "Maximum limit is %d." % (self.cfg.MAX_STATIONS)) # Run Syn1D for LF print("Running Syn1D for LF...") self.run_syn1d(a_tmpdir_lf, self.cfg.A_UC_LF_VELMODEL, self.cfg.A_UC_GREENBANK, self.cfg.A_UC_GREEN_SOIL, self.cfg.A_UC_SYN1D_INP_FILE) # Run Syn1D for HF print("Running Syn1D for HF...") self.run_syn1d(a_tmpdir_hf, self.cfg.A_UC_HF_VELMODEL, self.cfg.A_UC_HF_GREENBANK, self.cfg.A_UC_HF_GREEN_SOIL, self.cfg.A_UC_SYN1D_INP_FILE) # Run Stitch to combine LF and HF print("Running Stitch...") self.run_stitch(a_tmpdir_stitch, a_tmpdir_lf, a_tmpdir_hf, self.cfg.A_UC_LF_VELMODEL) # # Convert the outputs to BB format # # Copy station list ll to the stitch directory r_station_file = "stations.ll" shutil.copy2(os.path.join(a_tmpdir_lf, r_station_file), os.path.join(a_tmpdir_stitch, r_station_file)) # Save old directory old_cwd = os.getcwd() os.chdir(a_tmpdir_stitch) cmd = "%s >> %s 2>&1" % (self.cfg.A_CONV, self.log) bband_utils.runprog(cmd) # Restore old directory os.chdir(old_cwd) # # Move the results to the tmpdir directory. Use the stations # list to determine the names of the output file the system # should have produced. Define an output name for each # station BB file. Read each line in the file as a station. # for stat in site_list: a_tmpfile = os.path.join(a_tmpdir_stitch, "%s.3comp" % (stat.scode)) expected_file = os.path.join(self.a_tmpdir, "%d.%s.bbp" % (sim_id, stat.scode)) shutil.copy2(a_tmpfile, expected_file) if self.r_srcfile == "": # calculate magnitude and write to file mag = fault_utils.get_magnitude(os.path.join(self.a_indir, self.r_velmodel), os.path.join(self.a_indir, self.r_srffile), sta_base) mag_file = open(os.path.join(self.a_indir, "magnitude_%s" % (sta_base)), 'w') mag_file.write("%.2f" % mag) mag_file.flush() mag_file.close() print("UCSB Syn1D Completed".center(80, '-'))
def run(self): """ Runs the match module to merge low and high frequency seismograms """ print("Match".center(80, '-')) install = InstallCfg.getInstance() config = MatchCfg() sim_id = self.sim_id sta_base = os.path.basename(os.path.splitext(self.r_stations)[0]) self.log = os.path.join(install.A_OUT_LOG_DIR, str(sim_id), "%d.match_%s.log" % (sim_id, sta_base)) a_statfile = os.path.join(install.A_IN_DATA_DIR, str(sim_id), self.r_stations) a_tmpdir = os.path.join(install.A_TMP_DATA_DIR, str(sim_id)) a_outdir = os.path.join(install.A_OUT_DATA_DIR, str(sim_id)) # Make sure tmpdir exists dirs = [a_tmpdir] bband_utils.mkdirs(dirs, print_cmd=False) pow2_param = 0 if self.pow2: pow2_param = 1 # Start with defaults self.phase = config.PHASE self.hf_fhi = config.HF_FHI self.lf_flo = config.LF_FLO # Set match method if config.MATCH_METHOD == 1: self.phase = 1 elif config.MATCH_METHOD == 2: val = 1.0 / (2.0 * config.HF_ORD) self.hf_fhi = (self.hf_fhi * math.exp(val * math.log(math.sqrt(2.0) - 1.0))) val = -1.0 / (2.0 * config.LF_ORD) self.lf_flo = (self.lf_flo * math.exp(val * math.log(math.sqrt(2.0) - 1.0))) # # Read and parse the station list with this call # slo = StationList(a_statfile) site_list = slo.getStationList() # Get pointer to the velocity model object vel_obj = velocity_models.get_velocity_model_by_name(self.vmodel_name) if vel_obj is None: raise bband_utils.ParameterError("Cannot find velocity model: %s" % (self.vmodel_name)) # Check for velocity model-specific parameters vmodel_params = vel_obj.get_codebase_params('gp') # Figure out what DT we should use when resampling # Figure out the LF DT value if self.acc: seis_ext = '.acc.bbp' else: seis_ext = '.bbp' lf_seis = None # Find one LF seismogram for sites in site_list: site = sites.scode if os.path.exists(os.path.join(a_tmpdir, "%d.%s-lf%s" % (sim_id, site, seis_ext))): lf_seis = os.path.join(a_tmpdir, "%d.%s-lf%s" % (sim_id, site, seis_ext)) break # Need one file if lf_seis is None: raise bband_utils.ParameterError("Cannot find a LF seismogram") # Pick DT from this file lf_dt = None lf_file = open(lf_seis) for line in lf_file: line = line.strip() if line.startswith("#") or line.startswith("%"): continue # Got to first timestamp. Now, pick two consecutive # timestamps values lf_t1 = float(line.strip().split()[0]) lf_t2 = float(lf_file.next().strip().split()[0]) # Subtract the two times lf_dt = lf_t2 - lf_t1 # All done! break lf_file.close() if lf_dt is None: raise bband_utils.ParameterError("Cannot find LF_DT!") # lf_dt *should* match the gf_dt used by jbsim #if not 'GF_DT' in vmodel_params: # raise bband_utils.ParameterError("Cannot find GF_DT parameter in " # "velocity model: %s" % # (self.vmodel_name)) # In the GP method, we can potentially have two independent DT # values, one used by the rupture generator and the # low-frequency jbsim seismogram simulator, and another value # used by the high-frequency hfsims program. We have to use # the smaller of these two values in order to properly combine # the low-, and high-frequency seismograms. #gf_dt = float(vmodel_params['GF_DT']) if 'HF_DT' in vmodel_params: hf_dt = float(vmodel_params['HF_DT']) else: hf_dt = config.NEW_HFDT new_dt = min(lf_dt, hf_dt) # Go through the stations for sites in site_list: # Pick station name site = sites.scode # # We have a verbose of silent invocation. This is a very # verbose program so our default writes to dev/null # # # There are multiple possibilities; either we have # separate HF and LF files, we have HF and .bbp, LF and # .bbp, or just .bbp. In all cases, we need to separate # them to get components. # hf_exists = False lf_exists = False if not self.acc: print("==> Processing velocity seismograms for station: %s" % (site)) # Need to convert to acc first if os.path.exists(os.path.join(a_tmpdir, "%d.%s-hf.bbp" % (sim_id, site))): hf_exists = True if os.path.exists(os.path.join(a_tmpdir, "%d.%s-lf.bbp" % (sim_id, site))): lf_exists = True # If no files exist for this station, make a note and continue if not hf_exists and not lf_exists: print("===> No velocity seismograms found!") print("===> Skipping station...") continue # First process HF files to convert velocity to acceleration # Create path names and check if their sizes are # within bounds nsfile = os.path.join(a_tmpdir, "%d.%s-hf.000" % (sim_id, site)) ewfile = os.path.join(a_tmpdir, "%d.%s-hf.090" % (sim_id, site)) udfile = os.path.join(a_tmpdir, "%d.%s-hf.ver" % (sim_id, site)) bbpfile = os.path.join(a_tmpdir, "%d.%s-hf.bbp" % (sim_id, site)) bband_utils.check_path_lengths([nsfile, ewfile, udfile], bband_utils.GP_MAX_FILENAME) # Run wcc2bbp cmd = ("%s " % (os.path.join(install.A_GP_BIN_DIR, "wcc2bbp")) + "nsfile=%s ewfile=%s udfile=%s " % (nsfile, ewfile, udfile) + "wcc2bbp=0 < %s >> %s 2>&1" % (bbpfile, self.log)) bband_utils.runprog(cmd, abort_on_error=True, print_cmd=False) for comp in config.COMPS: # Create path names and check if their sizes # are within bounds filein = os.path.join(a_tmpdir, "%d.%s-hf.%s" % (sim_id, site, comp)) fileout = os.path.join(a_tmpdir, "%d.%s-hf.acc.%s" % (sim_id, site, comp)) bband_utils.check_path_lengths([filein, fileout], bband_utils.GP_MAX_FILENAME) cmd = ("%s diff=1 " % (os.path.join(install.A_GP_BIN_DIR, "integ_diff")) + "filein=%s fileout=%s" % (filein, fileout)) bband_utils.runprog(cmd, abort_on_error=True, print_cmd=False) # Create path names and check if their sizes are within bounds nsfile = os.path.join(a_tmpdir, "%d.%s-hf.acc.000" % (sim_id, site)) ewfile = os.path.join(a_tmpdir, "%d.%s-hf.acc.090" % (sim_id, site)) udfile = os.path.join(a_tmpdir, "%d.%s-hf.acc.ver" % (sim_id, site)) bbpfile = os.path.join(a_tmpdir, "%d.%s-hf.acc.bbp" % (sim_id, site)) bband_utils.check_path_lengths([nsfile, ewfile, udfile], bband_utils.GP_MAX_FILENAME) cmd = ("%s " % (os.path.join(install.A_GP_BIN_DIR, "wcc2bbp")) + "nsfile=%s ewfile=%s udfile=%s " % (nsfile, ewfile, udfile) + "units=cm/s/s wcc2bbp=1 > %s 2>> %s" % (bbpfile, self.log)) bband_utils.runprog(cmd, abort_on_error=True, print_cmd=False) # Then process LF files to convert velocity to acceleration # Create path names and check if their sizes are within bounds nsfile = os.path.join(a_tmpdir, "%d.%s-lf.000" % (sim_id, site)) ewfile = os.path.join(a_tmpdir, "%d.%s-lf.090" % (sim_id, site)) udfile = os.path.join(a_tmpdir, "%d.%s-lf.ver" % (sim_id, site)) bbpfile = os.path.join(a_tmpdir, "%d.%s-lf.bbp" % (sim_id, site)) bband_utils.check_path_lengths([nsfile, ewfile, udfile], bband_utils.GP_MAX_FILENAME) cmd = ("%s " % (os.path.join(install.A_GP_BIN_DIR, "wcc2bbp")) + "nsfile=%s ewfile=%s udfile=%s " % (nsfile, ewfile, udfile) + "wcc2bbp=0 < %s >> %s 2>&1" % (bbpfile, self.log)) bband_utils.runprog(cmd, abort_on_error=True, print_cmd=False) for comp in config.COMPS: # Create path names and check if their sizes # are within bounds filein = os.path.join(a_tmpdir, "%d.%s-lf.%s" % (sim_id, site, comp)) fileout = os.path.join(a_tmpdir, "%d.%s-lf.acc.%s" % (sim_id, site, comp)) bband_utils.check_path_lengths([filein, fileout], bband_utils.GP_MAX_FILENAME) cmd = ("%s " % (os.path.join(install.A_GP_BIN_DIR, "integ_diff")) + "diff=1 filein=%s fileout=%s" % (filein, fileout)) bband_utils.runprog(cmd, abort_on_error=True, print_cmd=False) # Create path names and check if their sizes are within bounds nsfile = os.path.join(a_tmpdir, "%d.%s-lf.acc.000" % (sim_id, site)) ewfile = os.path.join(a_tmpdir, "%d.%s-lf.acc.090" % (sim_id, site)) udfile = os.path.join(a_tmpdir, "%d.%s-lf.acc.ver" % (sim_id, site)) bbpfile = os.path.join(a_tmpdir, "%d.%s-lf.acc.bbp" % (sim_id, site)) bband_utils.check_path_lengths([nsfile, ewfile, udfile], bband_utils.GP_MAX_FILENAME) cmd = ("%s " % (os.path.join(install.A_GP_BIN_DIR, "wcc2bbp")) + "nsfile=%s ewfile=%s udfile=%s " % (nsfile, ewfile, udfile) + "units=cm/s/s wcc2bbp=1 > %s 2>> %s" % (bbpfile, self.log)) bband_utils.runprog(cmd, abort_on_error=True, print_cmd=False) # We should have acceleration files at this point hf_exists = False lf_exists = False if os.path.exists(os.path.join(a_tmpdir, "%d.%s-hf.acc.bbp" % (sim_id, site))): hf_exists = True if os.path.exists(os.path.join(a_tmpdir, "%d.%s-lf.acc.bbp" % (sim_id, site))): lf_exists = True print("==> Processing acceleration seismograms for station: %s" % (site)) # If no files exist for this station, make a note and continue if not hf_exists and not lf_exists: print("===> No acceleration seismograms found!") print("===> Skipping station...") continue # # Convert HF file to wcc components # # Create path names and check if their sizes are within bounds nsfile = os.path.join(a_tmpdir, "%d.%s-hf.acc.000" % (sim_id, site)) ewfile = os.path.join(a_tmpdir, "%d.%s-hf.acc.090" % (sim_id, site)) udfile = os.path.join(a_tmpdir, "%d.%s-hf.acc.ver" % (sim_id, site)) bbpfile = os.path.join(a_tmpdir, "%d.%s-hf.acc.bbp" % (sim_id, site)) bband_utils.check_path_lengths([nsfile, ewfile, udfile], bband_utils.GP_MAX_FILENAME) progstring = ("%s " % (os.path.join(install.A_GP_BIN_DIR, "wcc2bbp")) + "nsfile=%s ewfile=%s udfile=%s " % (nsfile, ewfile, udfile) + "wcc2bbp=0 < %s >> %s 2>&1" % (bbpfile, self.log)) bband_utils.runprog(progstring, abort_on_error=True, print_cmd=False) # # Convert LF file to wcc components # # Create path names and check if their sizes are within bounds nsfile = os.path.join(a_tmpdir, "%d.%s-lf.acc.000" % (sim_id, site)) ewfile = os.path.join(a_tmpdir, "%d.%s-lf.acc.090" % (sim_id, site)) udfile = os.path.join(a_tmpdir, "%d.%s-lf.acc.ver" % (sim_id, site)) bbpfile = os.path.join(a_tmpdir, "%d.%s-lf.acc.bbp" % (sim_id, site)) bband_utils.check_path_lengths([nsfile, ewfile, udfile], bband_utils.GP_MAX_FILENAME) progstring = ("%s " % (os.path.join(install.A_GP_BIN_DIR, "wcc2bbp")) + "nsfile=%s ewfile=%s udfile=%s " % (nsfile, ewfile, udfile) + "wcc2bbp=0 < %s >> %s 2>&1" % (bbpfile, self.log)) bband_utils.runprog(progstring, abort_on_error=True, print_cmd=False) # # Process each component # for entries in config.COMPS: compo = entries # # HF First # listfile = os.path.join(a_tmpdir, "%s.%s.hf.%s" % (config.FILTLIST, sta_base, compo)) bband_utils.check_path_lengths([listfile], bband_utils.GP_MAX_FILENAME) # Create wcc_tfilter input file out = open(listfile, 'w') # Contains HF input file infile = os.path.join(a_tmpdir, "%d.%s-hf.acc.%s" % (sim_id, site, compo)) out.write("%s\n" % infile) out.flush() out.close() # Also check infile bband_utils.check_path_lengths([infile], bband_utils.GP_MAX_FILENAME) # # Pre-filter and resample HF file # shutil.copy2(infile, "%s.prefilter" % infile) progstring = ("%s " % (os.path.join(install.A_GP_BIN_DIR, "wcc_tfilter")) + "filelist=%s order=%d fhi=%f flo=%s " % (listfile, config.HF_ORD, self.hf_fhi, config.HF_FLO) + "inbin=0 outbin=0 phase=%d " % (self.phase) + "outpath=%s >> %s 2>&1" % (a_tmpdir, self.log)) bband_utils.runprog(progstring, abort_on_error=True, print_cmd=False) outfile = os.path.join(a_tmpdir, "%d.%s-hf-resamp.%s" % (sim_id, site, compo)) bband_utils.check_path_lengths([outfile], bband_utils.GP_MAX_FILENAME) progstring = ("%s newdt=%f " % (os.path.join(install.A_GP_BIN_DIR, "wcc_resamp_arbdt"), new_dt) + "pow2=%d infile=%s outfile=%s >> %s 2>&1" % (pow2_param, infile, outfile, self.log)) bband_utils.runprog(progstring, abort_on_error=True, print_cmd=False) # # LF Next # listfile = os.path.join(a_tmpdir, "%s.%s.lf.%s" % (config.FILTLIST, sta_base, compo)) bband_utils.check_path_lengths([listfile], bband_utils.GP_MAX_FILENAME) # Create wcc_tfilter input file out = open(listfile, 'w') # Contains LF input file infile = os.path.join(a_tmpdir, "%d.%s-lf.acc.%s" % (sim_id, site, compo)) out.write("%s\n" % infile) out.flush() out.close() # Also check infile bband_utils.check_path_lengths([infile], bband_utils.GP_MAX_FILENAME) # # Pre-filter and resample LF file # shutil.copy2(infile, "%s.prefilter" % infile) if not self.using_3d: progstring = ("%s " % (os.path.join(install.A_GP_BIN_DIR, "wcc_tfilter")) + "filelist=%s order=%d fhi=%f flo=%s " % (listfile, config.LF_ORD, config.LF_FHI, self.lf_flo) + "inbin=0 outbin=0 phase=%d " % (self.phase) + "outpath=%s >> %s 2>&1 " % (a_tmpdir, self.log)) bband_utils.runprog(progstring, print_cmd=False) outfile = os.path.join(a_tmpdir, "%d.%s-lf-resamp.%s" % (sim_id, site, compo)) bband_utils.check_path_lengths([outfile], bband_utils.GP_MAX_FILENAME) progstring = ("%s " % (os.path.join(install.A_GP_BIN_DIR, "wcc_resamp_arbdt")) + "newdt=%f pow2=%d " % (new_dt, pow2_param) + "infile=%s outfile=%s >> %s 2>&1" % (infile, outfile, self.log)) bband_utils.runprog(progstring, abort_on_error=True, print_cmd=False) # # Add LF and HF resampled acc seismograms # # Check all path lengths infile1 = os.path.join(a_tmpdir, "%d.%s-lf-resamp.%s" % (sim_id, site, compo)) infile2 = os.path.join(a_tmpdir, "%d.%s-hf-resamp.%s" % (sim_id, site, compo)) outfile = os.path.join(a_tmpdir, "%d.%s.acc.add.%s" % (sim_id, site, compo)) bband_utils.check_path_lengths([infile1, infile2, outfile], bband_utils.GP_MAX_FILENAME) progstring = ("%s " % (os.path.join(install.A_GP_BIN_DIR, "wcc_add")) + "f1=1.00 t1=%f inbin1=0 infile1=%s " % (config.LF_TSTART, infile1) + "f2=1.00 t2=%f inbin2=0 infile2=%s " % (config.HF_TSTART, infile2) + "outbin=0 outfile=%s >> %s 2>&1" % (outfile, self.log)) bband_utils.runprog(progstring, abort_on_error=True, print_cmd=False) # # Create combined velocity files # # Check path lengths filein = os.path.join(a_tmpdir, "%d.%s.acc.add.%s" % (sim_id, site, compo)) fileout = os.path.join(a_tmpdir, "%d.%s.%s" % (sim_id, site, compo)) bband_utils.check_path_lengths([filein, fileout], bband_utils.GP_MAX_FILENAME) cmd = ("%s integ=1 filein=%s fileout=%s" % (os.path.join(install.A_GP_BIN_DIR, "integ_diff"), filein, fileout)) bband_utils.runprog(cmd, abort_on_error=True, print_cmd=False) # We have all the component files, create velocity seismogram # Create path names and check if their sizes are within bounds nsfile = os.path.join(a_tmpdir, "%d.%s.000" % (sim_id, site)) ewfile = os.path.join(a_tmpdir, "%d.%s.090" % (sim_id, site)) udfile = os.path.join(a_tmpdir, "%d.%s.ver" % (sim_id, site)) bbpfile = os.path.join(a_tmpdir, "%d.%s.bbp" % (sim_id, site)) bband_utils.check_path_lengths([nsfile, ewfile, udfile], bband_utils.GP_MAX_FILENAME) progstring = ("%s wcc2bbp=1 " % (os.path.join(install.A_GP_BIN_DIR, "wcc2bbp")) + 'title="Sim NGAH, stat=%s" ' % site + 'nsfile=%s ewfile=%s udfile=%s > %s 2>> %s' % (nsfile, ewfile, udfile, bbpfile, self.log)) bband_utils.runprog(progstring, abort_on_error=True, print_cmd=False) # Copy velocity bbp file to outdir shutil.copy2(os.path.join(a_tmpdir, "%d.%s.bbp" % (sim_id, site)), os.path.join(a_outdir, "%d.%s.vel.bbp" % (sim_id, site))) # Also create acceleration bbp file in outdir # Create path names and check if their sizes are within bounds nsfile = os.path.join(a_tmpdir, "%d.%s.000" % (sim_id, site)) ewfile = os.path.join(a_tmpdir, "%d.%s.090" % (sim_id, site)) udfile = os.path.join(a_tmpdir, "%d.%s.ver" % (sim_id, site)) bbpfile = os.path.join(a_tmpdir, "%d.%s.bbp" % (sim_id, site)) bband_utils.check_path_lengths([nsfile, ewfile, udfile], bband_utils.GP_MAX_FILENAME) cmd = ("%s " % (os.path.join(install.A_GP_BIN_DIR, "wcc2bbp")) + "nsfile=%s ewfile=%s udfile=%s " % (nsfile, ewfile, udfile) + "wcc2bbp=0 < %s >> %s 2>&1" % (bbpfile, self.log)) bband_utils.runprog(cmd, abort_on_error=True, print_cmd=False) for comp in config.COMPS: # Create path names and check if their sizes are within bounds filein = os.path.join(a_tmpdir, "%d.%s.%s" % (sim_id, site, comp)) fileout = os.path.join(a_tmpdir, "%d.%s.acc.%s" % (sim_id, site, comp)) bband_utils.check_path_lengths([filein, fileout], bband_utils.GP_MAX_FILENAME) cmd = ("%s diff=1 filein=%s fileout=%s" % (os.path.join(install.A_GP_BIN_DIR, "integ_diff"), filein, fileout)) bband_utils.runprog(cmd, abort_on_error=True, print_cmd=False) # Create path names and check if their sizes are within bounds nsfile = os.path.join(a_tmpdir, "%d.%s.acc.000" % (sim_id, site)) ewfile = os.path.join(a_tmpdir, "%d.%s.acc.090" % (sim_id, site)) udfile = os.path.join(a_tmpdir, "%d.%s.acc.ver" % (sim_id, site)) bbpfile = os.path.join(a_tmpdir, "%d.%s.acc.bbp" % (sim_id, site)) bband_utils.check_path_lengths([nsfile, ewfile, udfile], bband_utils.GP_MAX_FILENAME) cmd = ("%s " % (os.path.join(install.A_GP_BIN_DIR, "wcc2bbp")) + "nsfile=%s ewfile=%s udfile=%s " % (nsfile, ewfile, udfile) + "units=cm/s/s wcc2bbp=1 > %s 2>> %s" % (bbpfile, self.log)) bband_utils.runprog(cmd, abort_on_error=True, print_cmd=False) # Copy acceleration bbp file to outdir shutil.copy2(os.path.join(a_tmpdir, "%d.%s.acc.bbp" % (sim_id, site)), os.path.join(a_outdir, "%d.%s.acc.bbp" % (sim_id, site))) print("Match Completed".center(80, '-'))
def run(self): print("RotD50".center(80, '-')) # # convert input bbp acc files to peer format acc files # install = install_cfg.InstallCfg.getInstance() sim_id = self.sim_id sta_base = os.path.basename(os.path.splitext(self.r_stations)[0]) self.log = os.path.join(install.A_OUT_LOG_DIR, str(sim_id), "%d.rotd50_%s.log" % (sim_id, sta_base)) a_statfile = os.path.join(install.A_IN_DATA_DIR, str(sim_id), self.r_stations) a_tmpdir = os.path.join(install.A_TMP_DATA_DIR, str(sim_id)) a_outdir = os.path.join(install.A_OUT_DATA_DIR, str(sim_id)) # # Make sure the tmp and out directories exist # bband_utils.mkdirs([a_tmpdir, a_outdir], print_cmd=False) slo = StationList(a_statfile) site_list = slo.getStationList() for site in site_list: stat = site.scode print("==> Processing station: %s" % (stat)) # Since we have velocity files, we need to differentiate # to get to acceleration # Create path names and check if their sizes are within bounds nsfile = os.path.join(a_tmpdir, "%d.%s.000" % (sim_id, stat)) ewfile = os.path.join(a_tmpdir, "%d.%s.090" % (sim_id, stat)) udfile = os.path.join(a_tmpdir, "%d.%s.ver" % (sim_id, stat)) bbpfile = os.path.join(a_outdir, "%d.%s.vel.bbp" % (sim_id, stat)) bband_utils.check_path_lengths([nsfile, ewfile, udfile], bband_utils.GP_MAX_FILENAME) cmd = ("%s/wcc2bbp " % (install.A_GP_BIN_DIR) + "wcc2bbp=0 nsfile=%s ewfile=%s udfile=%s < %s >> %s 2>&1" % (nsfile, ewfile, udfile, bbpfile, self.log)) bband_utils.runprog(cmd, abort_on_error=True, print_cmd=False) for c in ["090", "000", "ver"]: # Differentiate to get from velocity to accl needed by rotd50 # Create path names and check if their sizes are within bounds filein = os.path.join(a_tmpdir, "%d.%s.%s" % (sim_id, stat, c)) fileout = os.path.join(a_tmpdir, "%d.%s.acc.%s" % (sim_id, stat, c)) bband_utils.check_path_lengths([filein, fileout], bband_utils.GP_MAX_FILENAME) cmd = ("%s/integ_diff diff=1 " % (install.A_GP_BIN_DIR) + "filein=%s fileout=%s >> %s 2>&1" % (filein, fileout, self.log)) bband_utils.runprog(cmd, abort_on_error=True, print_cmd=False) # Check file length bband_utils.check_path_lengths(["%s/%d.%s.acc.%s" % (a_tmpdir, sim_id, stat, c)], bband_utils.GP_MAX_FILENAME) # Now we need to convert them back to bbp # Create path names and check if their sizes are # within bounds nsfile = os.path.join(a_tmpdir, "%d.%s.acc.000" % (sim_id, stat)) ewfile = os.path.join(a_tmpdir, "%d.%s.acc.090" % (sim_id, stat)) udfile = os.path.join(a_tmpdir, "%d.%s.acc.ver" % (sim_id, stat)) bbpfile = os.path.join(a_tmpdir, "%d.%s.acc.bbp" % (sim_id, stat)) bband_utils.check_path_lengths([nsfile, ewfile, udfile], bband_utils.GP_MAX_FILENAME) cmd = ("%s/wcc2bbp " % (install.A_GP_BIN_DIR) + "nsfile=%s ewfile=%s udfile=%s " % (nsfile, ewfile, udfile) + "units=cm/s/s wcc2bbp=1 > %s 2>> %s" % (bbpfile, self.log)) bband_utils.runprog(cmd, abort_on_error=True, print_cmd=False) # Now we need to convert to peer format out_n_acc = os.path.join(a_tmpdir, "%d.%s.peer_n.acc" % (sim_id, stat)) out_e_acc = os.path.join(a_tmpdir, "%d.%s.peer_e.acc" % (sim_id, stat)) out_z_acc = os.path.join(a_tmpdir, "%d.%s.peer_z.acc" % (sim_id, stat)) bbp_formatter.bbp2peer(bbpfile, out_n_acc, out_e_acc, out_z_acc) # Let's have rotD50 create these output files out_rotd50_base = "%d.%s.rd50" % (sim_id, stat) tmp_rotd50 = os.path.join(a_tmpdir, out_rotd50_base) out_rotd50 = os.path.join(a_outdir, out_rotd50_base) # Run the rotD50 program self.do_rotd50(a_tmpdir, out_e_acc, out_n_acc, out_z_acc, out_rotd50, self.log) cmd = "cp %s %s" % (tmp_rotd50, out_rotd50) bband_utils.runprog(cmd, abort_on_error=True, print_cmd=False) # All done! print("RotD50 Completed".center(80, '-'))
def run(self): """ Calculate GMPEs, create bias plot comparisons """ print("GMPE Comparison".center(80, '-')) # Initialize basic variables install = InstallCfg.getInstance() sim_id = self.sim_id sta_base = os.path.basename(os.path.splitext(self.r_stations)[0]) # Input, tmp, and output directories a_tmpdir = os.path.join(install.A_TMP_DATA_DIR, str(sim_id)) a_outdir = os.path.join(install.A_OUT_DATA_DIR, str(sim_id)) a_tmpdir_seis = os.path.join(install.A_TMP_DATA_DIR, str(sim_id), "obs_seis_%s" % (sta_base)) a_outdir_gmpe = os.path.join(install.A_OUT_DATA_DIR, str(sim_id), "gmpe_data_%s" % (sta_base)) a_logdir = os.path.join(install.A_OUT_LOG_DIR, str(sim_id)) self.log = os.path.join(a_logdir, "%d.gmpe_compare.log" % (sim_id)) # # Make sure the output and tmp directories exist # dirs = [a_tmpdir, a_tmpdir_seis, a_outdir_gmpe, a_outdir, a_logdir] bband_utils.mkdirs(dirs, print_cmd=False) # Source file, parse it! a_srcfile = os.path.join(install.A_IN_DATA_DIR, str(sim_id), self.r_src_file) self.src_keys = bband_utils.parse_src_file(a_srcfile) # Station file a_statfile = os.path.join(install.A_IN_DATA_DIR, str(sim_id), self.r_stations) slo = StationList(a_statfile) site_list = slo.getStationList() # Go through each station, and print comparison headers for # the first station we process print_headers = True gmpe_models = [] for site in site_list: stat = site.scode obs_file = os.path.join(a_tmpdir_seis, "%s.rd50" % (stat)) gmpe_file = os.path.join(a_outdir_gmpe, "%s-gmpe.ri50" % (stat)) # Skip station if we don't have observation file if not os.access(obs_file, os.R_OK): continue gmpe_data, gmpe_models[:] = self.read_gmpe(gmpe_file) obs_periods, obs_data = self.read_rotd50(obs_file) # Loop through the NGA methods for gmpe_model in gmpe_models: resid_file = os.path.join( a_outdir_gmpe, "%s-%d.resid.txt" % (gmpe_model.lower(), sim_id)) period_set = self.calculate_residuals(site, gmpe_model, gmpe_data, obs_periods, obs_data, resid_file, print_headers) print_headers = False for gmpe_model in gmpe_models: # Now call the resid2uncer_varN program to summarize the # residuals and create the files needed for the GOF plot resid_file = os.path.join( a_outdir_gmpe, "%s-%d.resid.txt" % (gmpe_model.lower(), sim_id)) fileroot = os.path.join( a_outdir, "%s-GMPE-%d_r%d-all-rd50-%s" % (self.comp_label, sim_id, 0, gmpe_model.lower())) cmd = ("%s/resid2uncer_varN " % (install.A_GP_BIN_DIR) + "residfile=%s fileroot=%s " % (resid_file, fileroot) + "comp=%s nstat=%d nper=%d " % (gmpe_model.lower(), len(site_list), len(period_set)) + "min_cdst=%d >> %s 2>&1" % (0, self.log)) bband_utils.runprog(cmd, abort_on_error=True, print_cmd=False) # Plot GOF plot gmpe_group = gmpe_config.GMPES[self.gmpe_group_name] gmpe_labels = gmpe_group["labels"] plotter = PlotGoF() plottitle = "Comparison between GMPEs and %s" % (self.comp_label) fileroot = "%s-GMPE-%d_r%d-all-rd50-" % (self.comp_label, sim_id, 0) dataroot = [ "%s%s" % (fileroot, model.lower()) for model in gmpe_models ] plotter.multi_plot(plottitle, dataroot, a_outdir, a_outdir, gmpe_labels, len(site_list)) print("GMPE Comparison Completed".center(80, '-'))
def run(self): """ Run the AS16 validation for all stations """ print("AS2016".center(80, '-')) # Load configuration, set sim_id install = InstallCfg.getInstance() sim_id = self.sim_id # Build directory paths a_tmpdir = os.path.join(install.A_TMP_DATA_DIR, str(sim_id)) a_indir = os.path.join(install.A_IN_DATA_DIR, str(sim_id)) a_outdir = os.path.join(install.A_OUT_DATA_DIR, str(sim_id)) a_logdir = os.path.join(install.A_OUT_LOG_DIR, str(sim_id)) a_validation_outdir = os.path.join(a_outdir, "validations", "stewart_duration_gmpe") # Make sure the output and tmp directories exist bband_utils.mkdirs([a_tmpdir, a_indir, a_outdir, a_validation_outdir], print_cmd=False) # Now the file paths self.log = os.path.join(a_logdir, "%d.as16.log" % (sim_id)) sta_file = os.path.join(a_indir, self.stations) a_srcfile = os.path.join(a_indir, self.srcfile) # Read SRC file src_keys = bband_utils.parse_src_file(a_srcfile) # Load information from SRC file origin = (src_keys['lon_top_center'], src_keys['lat_top_center']) dims = (src_keys['fault_length'], src_keys['dlen'], src_keys['fault_width'], src_keys['dwid'], src_keys['depth_to_top']) mech = (src_keys['strike'], src_keys['dip'], src_keys['rake']) # Set region to be unknown -- this has no effect in the AS16 # method as z1 is not provided and that causes dz1 to be set # to zero and override the cj parameter cj = -999 # Figure out what mechanism to use # 0 = unknown # 1 = normal # 2 = reverse # 3 = strike-slip rake = src_keys['rake'] if abs(rake) <= 30 or abs(rake) >= 150: mechanism = 3 elif rake > 30 and rake < 150: mechanism = 2 elif rake < -30 and rake > -150: mechanism = 1 else: print("Warning: unknown mechanism for rake = %f" % (rake)) mechanism = 0 # Get station list slo = StationList(sta_file) site_list = slo.getStationList() # Create output file, add header out_file = open(os.path.join(a_validation_outdir, '%d.as16.%s.txt' % (self.sim_id, self.eventname)), 'w') out_file.write("#station, rrup, vs30, sd575, sd595, sd2080," " tau575, tau595, tau2080, phi575, phi595, phi2080\n") # Go through each station for site in site_list: stat = site.scode vs30 = float(site.vs30) # Calculate Rrup site_geom = [site.lon, site.lat, 0.0] (fault_trace1, up_seis_depth, low_seis_depth, ave_dip, dummy1, dummy2) = putils.FaultTraceGen(origin, dims, mech) _, rrup, _ = putils.DistanceToSimpleFaultSurface(site_geom, fault_trace1, up_seis_depth, low_seis_depth, ave_dip) results = calculate_as16(src_keys['magnitude'], rrup, mechanism, vs30, -999.0, cj) out_file.write("%s, %3.2f, %3.2f" % (stat, rrup, vs30)) for piece in results: out_file.write(", %7.5f" % (piece)) out_file.write("\n") # All done, close output file out_file.close() # All done! print("AS2016 Completed".center(80, '-'))
def run(self): """ Runs Genslip """ print("GP Rupture Generator GenSlip".center(80, '-')) # Load configuration, set sim_id install = InstallCfg.getInstance() sim_id = self.sim_id # Build directory paths a_tmpdir = os.path.join(install.A_TMP_DATA_DIR, str(sim_id)) a_indir = os.path.join(install.A_IN_DATA_DIR, str(sim_id)) a_outdir = os.path.join(install.A_OUT_DATA_DIR, str(sim_id)) a_logdir = os.path.join(install.A_OUT_LOG_DIR, str(sim_id)) # Make sure the output and tmp directories exist bband_utils.mkdirs([a_tmpdir, a_indir, a_outdir], print_cmd=False) # Now, file paths self.log = os.path.join(a_logdir, "%d.genslip.log" % (sim_id)) a_srcfile = os.path.join(a_indir, self.r_srcfile) a_velfile = os.path.join(a_indir, self.r_velmodel) # Read src file cfg = GenslipCfg(a_srcfile) # Define location of input velocity model file a_velmodel = os.path.join(a_tmpdir, self.r_velmodel) # Get pointer to the velocity model object vel_obj = velocity_models.get_velocity_model_by_name(self.vmodel_name) if vel_obj is None: raise bband_utils.ParameterError("Cannot find velocity model: %s" % (self.vmodel_name)) # Check for velocity model-specific parameters vmodel_params = vel_obj.get_codebase_params('gp') # Look for RISETIME_COEF if 'RISETIME_COEF' in vmodel_params: self.risetime_coef = float(vmodel_params['RISETIME_COEF']) else: self.risetime_coef = cfg.RISETIME_COEF # Look for SHAL_VRUP if 'SHAL_VRUP' in vmodel_params: self.shal_vrup = float(vmodel_params['SHAL_VRUP']) else: self.shal_vrup = cfg.SHAL_VRUP # Look for MEAN_RVFAC if 'MEAN_RVFAC' in vmodel_params: self.mean_rvfac = float(vmodel_params['MEAN_RVFAC']) else: self.mean_rvfac = cfg.MEAN_RVFAC # Look for RANGE_RVFAC if 'RANGE_RVFAC' in vmodel_params: self.range_rvfac = float(vmodel_params['RANGE_RVFAC']) else: self.range_rvfac = cfg.RANGE_RVFAC # Look for RISETIME_FAC if 'RISETIME_FAC' in vmodel_params: self.risetime_fac = float(vmodel_params['RISETIME_FAC']) else: self.risetime_fac = cfg.RISETIME_FAC # Look for DEEP_RISETIME_FAC if 'DEEP_RISETIME_FAC' in vmodel_params: self.deep_risetime_fac = float(vmodel_params['DEEP_RISETIME_FAC']) else: self.deep_risetime_fac = cfg.DEEP_RISETIME_FAC # Look for SLIP SIGMA if 'SLIP_SIGMA' in vmodel_params: self.slip_sigma = float(vmodel_params['SLIP_SIGMA']) else: self.slip_sigma = cfg.SLIP_SIGMA # Look for DT if 'GF_DT' in vmodel_params: gf_dt = float(vmodel_params['GF_DT']) else: raise bband_utils.ParameterError("Cannot find GF_DT parameter in" "velocity model %s!" % (self.vmodel_name)) # Calculate nstk,ndip nstk = round(cfg.CFGDICT["fault_length"] / cfg.CFGDICT["dlen"]) ndip = round(cfg.CFGDICT["fault_width"] / cfg.CFGDICT["dwid"]) # Calculate rvfac if "common_seed" in cfg.CFGDICT: rvfac = calculate_rvfac(self.mean_rvfac, self.range_rvfac, cfg.CFGDICT["common_seed"]) else: rvfac = calculate_rvfac(self.mean_rvfac, self.range_rvfac, cfg.CFGDICT["seed"]) # moment = math.pow(10, 1.5 * (cfg.MAG + 10.7)) # For multi-segment SRC files if "rupture_delay" in cfg.CFGDICT: rupture_delay = cfg.CFGDICT["rupture_delay"] else: rupture_delay = 0.0 if "moment_fraction" in cfg.CFGDICT: moment_fraction = cfg.CFGDICT["moment_fraction"] else: moment_fraction = -1.0 if "max_fault_length" in cfg.CFGDICT: flen_max = cfg.CFGDICT["max_fault_length"] else: flen_max = -1.0 r_gsftmp = "m%.2f-%.2fx%.2f.gsf" % (cfg.CFGDICT["magnitude"], cfg.CFGDICT["dlen"], cfg.CFGDICT["dwid"]) a_gsftmp = os.path.join(a_tmpdir, r_gsftmp) r_outroot = "m%.2f-%.2fx%.2f_s%d-v5.2.2" % (cfg.CFGDICT["magnitude"], cfg.CFGDICT["dlen"], cfg.CFGDICT["dwid"], cfg.CFGDICT["seed"]) a_srffile = os.path.join(a_indir, "%s.srf" % (r_outroot)) progstring = ("%s/fault_seg2gsf read_slip_vals=0 << EOF > %s 2>> %s\n" % (install.A_GP_BIN_DIR, a_gsftmp, self.log) + "1\n" + "%f %f %f %f %f %f %f %f %d %d\n" % (cfg.CFGDICT["lon_top_center"], cfg.CFGDICT["lat_top_center"], cfg.CFGDICT["depth_to_top"], cfg.CFGDICT["strike"], cfg.CFGDICT["dip"], cfg.CFGDICT["rake"], cfg.CFGDICT["fault_length"], cfg.CFGDICT["fault_width"], nstk, ndip) + "EOF") bband_utils.runprog(progstring) progstring = ("%s/genslip-v5.2.2 read_erf=0 write_srf=1 " % (install.A_GP_BIN_DIR) + "read_gsf=1 write_gsf=0 infile=%s " % (a_gsftmp) + "mag=%.2f nstk=%d ndip=%d " % (cfg.CFGDICT["magnitude"], nstk, ndip) + "ns=1 nh=1 " + "kmodel=2 seed=%d slip_sigma=%f " % (cfg.CFGDICT["seed"], self.slip_sigma) + "circular_average=0 modified_corners=0 " + "velfile=%s shypo=%f dhypo=%f rvfrac=%f " % (a_velfile, cfg.CFGDICT["hypo_along_stk"], cfg.CFGDICT["hypo_down_dip"], rvfac) + "shal_vrup_dep=%f shal_vrup_deprange=%f shal_vrup=%f " % (cfg.RTDEP, cfg.RTDEP_RANGE, self.shal_vrup) + "side_taper=0.02 bot_taper=0.0 top_taper=0.0 " + "dt=%f risetime_coef=%f plane_header=1 " % (gf_dt, self.risetime_coef) + "risetimefac=%f risetimedep=%f risetimedep_range=%f " % (self.risetime_fac, cfg.RTDEP, cfg.RTDEP_RANGE) + "rt_scalefac=%f slip_water_level=%f " % (cfg.RT_SCALEFAC, cfg.SLIP_WATER_LEVEL) + "deep_risetimedep=%f deep_risetimedep_range=%f " % (cfg.DEEP_RISETIMEDEP, cfg.DEEP_RISETIMEDEP_RANGE) + "deep_risetimefac=%f " % (self.deep_risetime_fac) + "flen_max=%f rupture_delay=%f moment_fraction=%f " % (flen_max, rupture_delay, moment_fraction) + "srf_version=2.0 rake_sigma=15.0 fdrup_time=1 " + "deep_vrup=0.6 use_gaus=1 alpha_rough=0.01 " + "lambda_min=0.08 tsfac_coef=1.1 tsfac1_sigma=1.0 " + "tsfac1_scor=0.8 rtime1_sigma=0.85 rtime1_scor=0.8 " + "> %s 2>> %s" % (a_srffile, self.log)) bband_utils.runprog(progstring) # # mv result to outputfile # progstring = "cp %s %s" % (a_srffile, os.path.join(a_tmpdir, self.r_srffile)) bband_utils.runprog(progstring) progstring = "cp %s %s" % (a_srffile, os.path.join(a_indir, self.r_srffile)) bband_utils.runprog(progstring) progstring = "cp %s %s" % (a_srffile, os.path.join(a_outdir, self.r_srffile)) bband_utils.runprog(progstring) # Plot SRF plot_srf.run(self.r_srffile, sim_id=self.sim_id) print("GP GenSlip Completed".center(80, '-'))
def run(self): """ Go through the station list and create acceleration seismogram. Copy results to outdata directory """ print("Copy Seismograms".center(80, '-')) install = InstallCfg.getInstance() sim_id = self.sim_id sta_base = os.path.basename(os.path.splitext(self.r_stations)[0]) self.log = os.path.join(install.A_OUT_LOG_DIR, str(sim_id), "%d.copy_seis_%s.log" % (sim_id, sta_base)) a_statfile = os.path.join(install.A_IN_DATA_DIR, str(sim_id), self.r_stations) a_tmpdir = os.path.join(install.A_TMP_DATA_DIR, str(sim_id)) a_outdir = os.path.join(install.A_OUT_DATA_DIR, str(sim_id)) # Make sure tmpdir, outdir exist dirs = [a_tmpdir, a_outdir] bband_utils.mkdirs(dirs, print_cmd=False) # # Read and parse the statioin list with this call # slo = StationList(a_statfile) site_list = slo.getStationList() for sits in site_list: site = sits.scode print("==> Processing station: %s" % (site)) if self.hybrid: expected_file = "%d.%s.bbp" % (sim_id, site) else: expected_file = "%d.%s.vel.bbp" % (sim_id, site) #print("Processing velocity for station %s - %s" % # (site, expected_file)) bbpfile = os.path.join(a_tmpdir, expected_file) # Make sure velocity file is there, otherwise, skip this station if not os.path.exists(bbpfile): print("No velocity seismograms found for station %s" % (site)) print("Skipping this station...") continue # Copy velocity bbp file to outdir shutil.copy2(bbpfile, os.path.join(a_outdir, "%d.%s.vel.bbp" % (sim_id, site))) # Create path names and check if their sizes are within bounds nsfile = os.path.join(a_tmpdir, "%d.%s.000" % (sim_id, site)) ewfile = os.path.join(a_tmpdir, "%d.%s.090" % (sim_id, site)) udfile = os.path.join(a_tmpdir, "%d.%s.ver" % (sim_id, site)) bband_utils.check_path_lengths([nsfile, ewfile, udfile], bband_utils.GP_MAX_FILENAME) cmd = ("%s/wcc2bbp " % (install.A_GP_BIN_DIR) + "nsfile=%s ewfile=%s udfile=%s " % (nsfile, ewfile, udfile) + "wcc2bbp=0 < %s >> %s 2>&1" % (bbpfile, self.log)) bband_utils.runprog(cmd, abort_on_error=True, print_cmd=False) for comp in ['000', '090', 'ver']: # Create path names and check if their sizes are # within bounds filein = os.path.join(a_tmpdir, "%d.%s.%s" % (sim_id, site, comp)) fileout = os.path.join(a_tmpdir, "%d.%s.acc.%s" % (sim_id, site, comp)) bband_utils.check_path_lengths([filein, fileout], bband_utils.GP_MAX_FILENAME) cmd = ("%s/integ_diff diff=1 filein=%s fileout=%s" % (install.A_GP_BIN_DIR, filein, fileout)) bband_utils.runprog(cmd, abort_on_error=True, print_cmd=False) # Create path names and check if their sizes are within bounds nsfile = os.path.join(a_tmpdir, "%d.%s.acc.000" % (sim_id, site)) ewfile = os.path.join(a_tmpdir, "%d.%s.acc.090" % (sim_id, site)) udfile = os.path.join(a_tmpdir, "%d.%s.acc.ver" % (sim_id, site)) bbpfile = os.path.join(a_tmpdir, "%d.%s.acc.bbp" % (sim_id, site)) bband_utils.check_path_lengths([nsfile, ewfile, udfile], bband_utils.GP_MAX_FILENAME) cmd = ("%s/wcc2bbp " % (install.A_GP_BIN_DIR) + "nsfile=%s ewfile=%s udfile=%s " % (nsfile, ewfile, udfile) + "units=cm/s/s wcc2bbp=1 > %s 2>> %s" % (bbpfile, self.log)) bband_utils.runprog(cmd, abort_on_error=True, print_cmd=False) # Copy acceleration bbp file to outdir shutil.copy2(bbpfile, os.path.join(a_outdir, "%d.%s.acc.bbp" % (sim_id, site))) print("Copy Seismograms Completed".center(80, '-'))