def test_rotd50(self): """ Test the rotd50 module """ # Load configuration, set sim_id sim_id = self.sim_id # Reference directory ref_dir = os.path.join(self.install.A_TEST_REF_DIR, "ucb") r_station_list = "rotd50.stl" a_src_station_list = os.path.join(ref_dir, r_station_list) a_dst_station_list = os.path.join(self.indir, r_station_list) # Copy station list to indir cmd = "cp %s %s" % (a_src_station_list, a_dst_station_list) bband_utils.runprog(cmd) # Copy velocity bbp files to outdir for i in range(1, 6): src_bbp = os.path.join(ref_dir, "8000%d.vel.bbp" % (i)) dst_bbp = os.path.join(self.outdir, "%d.8000%d.vel.bbp" % (sim_id, i)) cmd = "cp %s %s" % (src_bbp, dst_bbp) bband_utils.runprog(cmd) rotd50_obj = rotd50.RotD50(r_station_list, sim_id) rotd50_obj.run()
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 run(r_srffile, sim_id=0): """ Creates a SRF plot from an SRF file """ install = InstallCfg.getInstance() 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)) # Save current directory old_cwd = os.getcwd() os.chdir(a_tmpdir) # Write slip file srfbase = r_srffile[0:r_srffile.find(".srf")] slipfile = "%s.slip" % (srfbase) cmd = ("%s/srf2xyz calc_xy=0 type=slip nseg=-1 < %s > %s" % (install.A_GP_BIN_DIR, r_srffile, slipfile)) bband_utils.runprog(cmd) # Write tinit file tinitfile = "%s.tinit" % (srfbase) cmd = ("%s/srf2xyz calc_xy=0 type=tinit nseg=-1 < %s > %s" % (install.A_GP_BIN_DIR, r_srffile, tinitfile)) bband_utils.runprog(cmd) plottitle = 'Rupture Model for %s' % (r_srffile) plot(plottitle, r_srffile, a_outdir) os.chdir(old_cwd)
def run(r_srffile, sim_id=0): """ Creates a SRF plot from an SRF file """ install = InstallCfg.getInstance() 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)) srf2xyz_bin = os.path.join(install.A_GP_BIN_DIR, "srf2xyz") # Save current directory old_cwd = os.getcwd() os.chdir(a_tmpdir) # Get number of segments num_segments = get_srf_num_segments(r_srffile) srfbase = r_srffile[0:r_srffile.find(".srf")] # Write slip and tinit files for each segment for seg in range(num_segments): slipfile = "%s_seg%d.slip" % (srfbase, seg) cmd = ("%s calc_xy=0 type=slip nseg=%d < %s > %s" % (srf2xyz_bin, seg, r_srffile, slipfile)) bband_utils.runprog(cmd) tinitfile = "%s_seg%d.tinit" % (srfbase, seg) cmd = ("%s calc_xy=0 type=tinit nseg=%d < %s > %s" % (srf2xyz_bin, seg, r_srffile, tinitfile)) bband_utils.runprog(cmd) plottitle = 'Rupture Model for %s' % (r_srffile) plot(plottitle, r_srffile, a_outdir) os.chdir(old_cwd)
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 run(r_srffile, sim_id=0): """ Creates a SRF plot from an SRF file """ install = InstallCfg.getInstance() 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)) # Save current directory old_cwd = os.getcwd() os.chdir(a_tmpdir) # Write slip file srfbase = r_srffile[0:r_srffile.find(".srf")] slipfile = "%s.slip" % (srfbase) cmd = ("%s/srf2xyz calc_xy=0 type=slip nseg=-1 < %s > %s" % (install.A_GP_BIN_DIR, r_srffile, slipfile)) bband_utils.runprog(cmd) # Write tinit file tinitfile = "%s.tinit" % (srfbase) cmd = ("%s/srf2xyz calc_xy=0 type=tinit nseg=-1 < %s > %s" % (install.A_GP_BIN_DIR, r_srffile, tinitfile)) bband_utils.runprog(cmd) plottitle = 'Rupture Model for %s' % (r_srffile) plot(plottitle, r_srffile, a_outdir) os.chdir(old_cwd)
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): 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 test_rotd50(self): """ Test the rotd50 module """ # Load configuration, set sim_id sim_id = self.sim_id # Reference directory ref_dir = os.path.join(self.install.A_TEST_REF_DIR, "ucb") r_station_list = "rotd50.stl" a_src_station_list = os.path.join(ref_dir, r_station_list) a_dst_station_list = os.path.join(self.indir, r_station_list) # Copy station list to indir cmd = "cp %s %s" % (a_src_station_list, a_dst_station_list) bband_utils.runprog(cmd) # Copy velocity bbp files to outdir for i in range(1, 6): src_bbp = os.path.join(ref_dir, "8000%d.vel.bbp" % (i)) dst_bbp = os.path.join(self.outdir, "%d.8000%d.vel.bbp" % (sim_id, i)) cmd = "cp %s %s" % (src_bbp, dst_bbp) bband_utils.runprog(cmd) rotd50_obj = rotd50.RotD50(r_station_list, sim_id) rotd50_obj.run()
def plot_srf(self): """ Creates the multi-segment SRF plot """ # Save current directory old_cwd = os.getcwd() os.chdir(self.a_tmpdir) for srf_file in self.srf_files: srf_file = os.path.basename(srf_file) # Write slip file srfbase = srf_file[0:srf_file.find(".srf")] slipfile = "%s.slip" % (srfbase) cmd = ("%s calc_xy=0 type=slip nseg=-1 < %s > %s" % (os.path.join(self.install.A_GP_BIN_DIR, "srf2xyz"), os.path.join(self.a_indir, srf_file), slipfile)) bband_utils.runprog(cmd) # Write tinit file tinitfile = "%s.tinit" % (srfbase) cmd = ("%s calc_xy=0 type=tinit nseg=-1 < %s > %s" % (os.path.join(self.install.A_GP_BIN_DIR, "srf2xyz"), os.path.join(self.a_indir, srf_file), tinitfile)) bband_utils.runprog(cmd) plottitle = 'Rupture Model for %s' % (self.val_event) plot_srf.plot_multi_segment(plottitle, self.srf_files, self.a_outdir) # Restore directory os.chdir(old_cwd)
def setUp(self): self.install = InstallCfg() self.r_srcfile = "test_wh_ucsb.src" self.r_faultfile = "ffsp.inp" self.r_velmodel = "nr02-vs500_lf.vel" self.vmodel_name = "LABasin500" self.sim_id = int(seqnum.get_seq_num()) # Create directories a_refdir = os.path.join(self.install.A_TEST_REF_DIR, "ucsb") 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)) cmd = "mkdir -p %s" % (a_indir) bband_utils.runprog(cmd) cmd = "mkdir -p %s" % (a_tmpdir) bband_utils.runprog(cmd) cmd = "mkdir -p %s" % (a_outdir) bband_utils.runprog(cmd) cmd = "mkdir -p %s" % (a_logdir) bband_utils.runprog(cmd) # Copy SRC file cmd = "cp %s %s" % (os.path.join(a_refdir, self.r_srcfile), a_indir) bband_utils.runprog(cmd)
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.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 generate_xml(install, numsim, srcdir, xmldir, logdir, vmodel, codebase, prefix, station_list): """ Generates xml files in the xmldir for numsim simulations whose source files are in the srcdir using the validation event and codebase specified """ tmpdir = tempfile.mkdtemp(prefix="bbp-") bbproot = "%s/run_bbp.py" % (install.A_COMP_DIR) bfn = os.path.join(xmldir, BATCH_SIM_FILE) batchfile = open(bfn, 'w') for sim in range(0, numsim): srcfile = os.path.join(srcdir, "%s-%04d.src" % (prefix, sim)) ofn = os.path.join(tmpdir, "bbp.optfile") optfile = open(ofn, 'w') optfile.write('n\n') # Scenario optfile.write('%s\n' % (vmodel)) # Velocity model optfile.write('%s\n' % (codebase)) # Codebase to use if codebase != "exsim" and codebase != "csm": optfile.write('y\n') # Run rupture generator optfile.write('2\n') # Enter path to source file optfile.write('%s\n' % (srcfile)) # Source file optfile.write('2\n') # Enter path to station list optfile.write('%s\n' % (station_list)) # Station list if codebase == "exsim": # Don't specify custom ExSIM template file optfile.write('n\n') if (codebase != "exsim" and codebase != "csm" and codebase != "irikura"): # Skip site response optfile.write('n\n') optfile.write('y\n') # Plot velocity seismograms optfile.write('y\n') # Plot acceleration seismograms optfile.flush() optfile.close() # Run BBP and generate the xml file bband_utils.runprog("export BBP_DATA_DIR=%s; %s -o %s -s %d -g" % (tmpdir, bbproot, ofn, sim+1)) # Copy the xml file srcxml = os.path.join(tmpdir, "xml", "%d.xml" % (sim+1)) dstxml = os.path.join(xmldir, "%s-%04d.xml" % (prefix, sim)) shutil.copy2(srcxml, dstxml) # Add entry to the batch file bbp_sim = 10000000 + sim logbase = os.path.join(logdir, str(bbp_sim)) logfile = os.path.join(logbase, "%d_%s.log" % (bbp_sim, prefix)) # Make sure logdir exists os.makedirs(logbase) batchfile.write("%s -x %s -s %d -l %s\n" % (bbproot, dstxml, bbp_sim, logfile)) # Close batch file batchfile.flush() batchfile.close() # Clean-up shutil.rmtree(tmpdir)
def generate_xml(install, numsim, srcdir, xmldir, logdir, vmodel, codebase, prefix, station_list): """ Generates xml files in the xmldir for numsim simulations whose source files are in the srcdir using the validation event and codebase specified """ tmpdir = tempfile.mkdtemp(prefix="bbp-") bbproot = "%s/run_bbp.py" % (install.A_COMP_DIR) bfn = os.path.join(xmldir, BATCH_SIM_FILE) batchfile = open(bfn, 'w') for sim in range(0, numsim): srcfile = os.path.join(srcdir, "%s-%04d.src" % (prefix, sim)) ofn = os.path.join(tmpdir, "bbp.optfile") optfile = open(ofn, 'w') optfile.write('n\n') # Scenario optfile.write('%s\n' % (vmodel)) # Velocity model optfile.write('%s\n' % (codebase)) # Codebase to use if codebase != "exsim" and codebase != "csm": optfile.write('y\n') # Run rupture generator optfile.write('2\n') # Enter path to source file optfile.write('%s\n' % (srcfile)) # Source file optfile.write('2\n') # Enter path to station list optfile.write('%s\n' % (station_list)) # Station list if codebase == "exsim": # Don't specify custom ExSIM template file optfile.write('n\n') if (codebase != "exsim" and codebase != "csm" and codebase != "irikura"): # Skip site response optfile.write('n\n') optfile.write('y\n') # Plot velocity seismograms optfile.write('y\n') # Plot acceleration seismograms optfile.flush() optfile.close() # Run BBP and generate the xml file bband_utils.runprog("export BBP_DATA_DIR=%s; %s -o %s -s %d -g" % (tmpdir, bbproot, ofn, sim + 1)) # Copy the xml file srcxml = os.path.join(tmpdir, "xml", "%d.xml" % (sim + 1)) dstxml = os.path.join(xmldir, "%s-%04d.xml" % (prefix, sim)) shutil.copy2(srcxml, dstxml) # Add entry to the batch file bbp_sim = 10000000 + sim logbase = os.path.join(logdir, str(bbp_sim)) logfile = os.path.join(logbase, "%d_%s.log" % (bbp_sim, prefix)) # Make sure logdir exists os.makedirs(logbase) batchfile.write("%s -x %s -s %d -l %s\n" % (bbproot, dstxml, bbp_sim, logfile)) # Close batch file batchfile.flush() batchfile.close() # Clean-up shutil.rmtree(tmpdir)
def summarize_rotd50(self, site_list, a_outdir, a_outdir_gmpe): """ Summarizes all rotd50 data and creates the rotd50 GOF plot """ sim_id = self.sim_id install = self.install config = self.config rd50_residfile = os.path.join( a_outdir, "%s-%d.rd50-resid.txt" % (self.comp_label, sim_id)) for comp in config.COMPS_PSA5: # Build paths and check lengths fileroot = os.path.join( a_outdir, "%s-%d_r%d-%d-rd50-%s" % (self.comp_label, sim_id, config.MIN_CDST, self.max_cutoff, comp)) bband_utils.check_path_lengths([rd50_residfile, fileroot], bband_utils.GP_MAX_FILENAME) cmd = ("%s/resid2uncer_varN " % (install.A_GP_BIN_DIR) + "residfile=%s fileroot=%s " % (rd50_residfile, fileroot) + "comp=%s nstat=%d nper=63 " % (comp, len(site_list)) + "min_cdst=%d max_cdst=%d >> %s 2>&1" % (config.MIN_CDST, self.max_cutoff, self.log)) bband_utils.runprog(cmd, abort_on_error=True, print_cmd=False) # Plot GOF for psa5/rotd50 data if self.single_component: plot_mode = 'rd50-single' else: plot_mode = 'rd50' fileroot = '%s-%d_r0-%d-rd50' % (self.comp_label, sim_id, self.max_cutoff) plottitle = ("GOF Comparison between %s and simulation %d" % (self.comp_label, sim_id)) plotter = PlotGoF() plotter.plot(plottitle, fileroot, a_outdir, a_outdir, cutoff=self.max_cutoff, mode=plot_mode, colorset='single') # Finally, plot the distance and map GOFs plot_dist_gof(rd50_residfile, self.comp_label, a_outdir_gmpe, sim_id=self.sim_id) plot_map_gof(self.r_srcfile, self.r_stations, rd50_residfile, self.comp_label, sim_id=self.sim_id)
def join_files(bbp_out, units="cm/s"): """ Joins the 3 1-component bbp files into bbp_out """ nsfile = PREFIX_PROC + COMPS[0] ewfile = PREFIX_PROC + COMPS[1] udfile = PREFIX_PROC + COMPS[2] cmd = ("%s/wcc2bbp " % (INSTALL.A_GP_BIN_DIR) + "nsfile=%s ewfile=%s udfile=%s " % (nsfile, ewfile, udfile) + "units=%s " % (units) + "wcc2bbp=1 > %s 2>> /dev/null" % (bbp_out)) bband_utils.runprog(cmd, print_cmd=False, abort_on_error=True)
def split_file(bbp_in): """ Splits bbp_in into 3 1-component files """ nsfile = PREFIX + COMPS[0] ewfile = PREFIX + COMPS[1] udfile = PREFIX + COMPS[2] cmd = ("%s/wcc2bbp " % (INSTALL.A_GP_BIN_DIR) + "nsfile=%s ewfile=%s udfile=%s " % (nsfile, ewfile, udfile) + "wcc2bbp=0 < %s >> /dev/null 2>&1" % (bbp_in)) bband_utils.runprog(cmd, print_cmd=False, abort_on_error=True)
def diff(vel_bbp_in, acc_bbp_out): """ Generates bbp_out by derivating bbp_in """ split_file(vel_bbp_in) for component in COMPS: filein = PREFIX + component fileout = PREFIX_PROC + component cmd = ("%s/integ_diff diff=1 filein=%s fileout=%s" % (INSTALL.A_GP_BIN_DIR, filein, fileout)) bband_utils.runprog(cmd, print_cmd=False, abort_on_error=True) join_files(acc_bbp_out, "cm/s/s") delete_temp_files()
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(self): """ Run the GP WccSiteamp 2014 module """ print("GP Site Response".center(80, '-')) self.install = InstallCfg.getInstance() install = self.install self.config = WccSiteampCfg() config = self.config 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.wcc_siteamp_%s.log" % (sim_id, sta_base)) a_statfile = os.path.join(install.A_IN_DATA_DIR, str(sim_id), self.r_stations) 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_indir = os.path.join(install.A_IN_DATA_DIR, str(sim_id)) progstring = "mkdir -p %s" % (a_tmpdir) bband_utils.runprog(progstring, abort_on_error=True, print_cmd=False) # # Read and parse the station list with this call # slo = StationList(a_statfile) site_list = slo.getStationList() for sites in site_list: site = sites.scode vs30 = sites.vs30 if vs30 > config.VREF_MAX: vs30 = config.VREF_MAX print("*** WccSiteamp Processing station %s..." % (site)) if self.method == "GP": self.process_separate_seismograms(site, sta_base, vs30, a_indir, a_tmpdir) elif self.method == "SDSU" or self.method == "EXSIM" or self.method == "UCSB": self.process_hybrid_seismogram(site, sta_base, vs30, a_tmpdir, a_outdir) print("GP Site Response Completed".center(80, '-'))
def split_file(bbp_in): """ Splits bbp_in into 3 1-component files """ nsfile = PREFIX + COMPS[0] ewfile = PREFIX + COMPS[1] udfile = PREFIX + COMPS[2] cmd = ("%s/wcc2bbp " % (INSTALL.A_GP_BIN_DIR) + "nsfile=%s ewfile=%s udfile=%s " % (nsfile, ewfile, udfile) + "wcc2bbp=0 < %s >> /dev/null 2>&1" % (bbp_in)) bband_utils.runprog(cmd, print_cmd=False, abort_on_error=True)
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 test_site_amp(self): a_ref_dir = os.path.join(self.install.A_TEST_REF_DIR, "ucsb") a_res_dir = os.path.join(self.install.A_TMP_DATA_DIR, str(self.sim_id)) site_obj = UCSite(self.r_velocity, self.r_src, self.r_stations, sim_id=self.sim_id) site_obj.run() # Now compare results a_ref_file = os.path.join(a_ref_dir, "s01.site.3comp") a_newfile = os.path.join(a_res_dir, "s01.3comp") # Headers will be different; need to ignore a_ref_file_no_header = os.path.join(a_res_dir, "ref_file_01_no_header") ref_file_in_fp = open(a_ref_file, 'r') ref_file_out_fp = open(a_ref_file_no_header, 'w') ref_file_data = ref_file_in_fp.readlines() ref_file_in_fp.close() for line in ref_file_data: line = line.strip() # Skip comments if line.startswith('%') or line.startswith('#'): continue ref_file_out_fp.write(line) ref_file_out_fp.flush() ref_file_out_fp.close() test_file_no_header = os.path.join(a_res_dir, "test_file_01_no_header") test_file_in_fp = open(a_newfile, 'r') test_file_out_fp = open(test_file_no_header, 'w') test_file_data = test_file_in_fp.readlines() test_file_in_fp.close() for line in test_file_data: line = line.strip() # Skip comments if line.startswith('%') or line.startswith('#'): continue test_file_out_fp.write(line) test_file_out_fp.flush() test_file_out_fp.close() errmsg = ("Output file %s does not match reference file %s" % (a_newfile, a_ref_file)) self.failIf(cmp_bbp.cmp_bbp(a_ref_file_no_header, test_file_no_header, tolerance=0.035) != 0, errmsg) cmd = "rm %s %s" % (a_ref_file_no_header, test_file_no_header) bband_utils.runprog(cmd)
def join_files(bbp_out, units="cm/s"): """ Joins the 3 1-component bbp files into bbp_out """ nsfile = PREFIX_PROC + COMPS[0] ewfile = PREFIX_PROC + COMPS[1] udfile = PREFIX_PROC + COMPS[2] cmd = ("%s/wcc2bbp " % (INSTALL.A_GP_BIN_DIR) + "nsfile=%s ewfile=%s udfile=%s " % (nsfile, ewfile, udfile) + "units=%s " % (units) + "wcc2bbp=1 > %s 2>> /dev/null" % (bbp_out)) bband_utils.runprog(cmd, print_cmd=False, abort_on_error=True)
def run(self): """ Run the GP WccSiteamp 2014 module """ print("GP Site Response".center(80, '-')) self.install = InstallCfg.getInstance() install = self.install self.config = WccSiteampCfg() config = self.config 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.wcc_siteamp_%s.log" % (sim_id, sta_base)) a_statfile = os.path.join(install.A_IN_DATA_DIR, str(sim_id), self.r_stations) 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_indir = os.path.join(install.A_IN_DATA_DIR, str(sim_id)) progstring = "mkdir -p %s" % (a_tmpdir) bband_utils.runprog(progstring, abort_on_error=True, print_cmd=False) # # Read and parse the station list with this call # slo = StationList(a_statfile) site_list = slo.getStationList() for sites in site_list: site = sites.scode vs30 = sites.vs30 if vs30 > config.VREF_MAX: vs30 = config.VREF_MAX print("*** WccSiteamp Processing station %s..." % (site)) if self.method == "GP": self.process_separate_seismograms(site, sta_base, vs30, a_indir, a_tmpdir) elif self.method == "SDSU" or self.method == "EXSIM" or self.method == "UCSB": self.process_hybrid_seismogram(site, sta_base, vs30, a_tmpdir, a_outdir) print("GP Site Response Completed".center(80, '-'))
def do_rotd100(workdir, peer_input_e_file, peer_input_n_file, peer_input_z_file, output_rotd100_file, logfile): """ This function runs the rotd100 command inside workdir, using the inputs and outputs specified """ install = install_cfg.InstallCfg.getInstance() # Make sure we don't have absolute path names peer_input_e_file = os.path.basename(peer_input_e_file) peer_input_n_file = os.path.basename(peer_input_n_file) peer_input_z_file = os.path.basename(peer_input_z_file) output_rotd100_file = os.path.basename(output_rotd100_file) # Save cwd, change back to it at the end old_cwd = os.getcwd() os.chdir(workdir) # Make sure we remove the output files first or Fortran will # complain if they already exist try: os.unlink(output_rotd100_file) except OSError: pass # # write config file for rotd100 program rd100_conf = open("rotd100_inp.cfg", 'w') # This flag indicates inputs acceleration rd100_conf.write("2 interp flag\n") # This flag indicate we are processing two input files (horizontals) rd100_conf.write("1 Npairs\n") # Number of headers in the file rd100_conf.write("6 Nhead\n") rd100_conf.write("%s\n" % peer_input_e_file) rd100_conf.write("%s\n" % peer_input_n_file) rd100_conf.write("%s\n" % output_rotd100_file) # Close file rd100_conf.close() progstring = ("%s >> %s 2>&1" % (os.path.join(install.A_UCB_BIN_DIR, "rotd100"), logfile)) bband_utils.runprog(progstring, abort_on_error=True, print_cmd=False) # Restore working directory os.chdir(old_cwd)
def do_rotd100(workdir, peer_input_e_file, peer_input_n_file, peer_input_z_file, output_rotd100_file, logfile): """ This function runs the rotd100 command inside workdir, using the inputs and outputs specified """ install = install_cfg.InstallCfg.getInstance() # Make sure we don't have absolute path names peer_input_e_file = os.path.basename(peer_input_e_file) peer_input_n_file = os.path.basename(peer_input_n_file) peer_input_z_file = os.path.basename(peer_input_z_file) output_rotd100_file = os.path.basename(output_rotd100_file) # Save cwd, change back to it at the end old_cwd = os.getcwd() os.chdir(workdir) # Make sure we remove the output files first or Fortran will # complain if they already exist try: os.unlink(output_rotd100_file) except OSError: pass # # write config file for rotd100 program rd100_conf = open("rotd100_inp.cfg", 'w') # This flag indicates inputs acceleration rd100_conf.write("2 interp flag\n") # This flag indicate we are processing two input files (horizontals) rd100_conf.write("1 Npairs\n") # Number of headers in the file rd100_conf.write("6 Nhead\n") rd100_conf.write("%s\n" % peer_input_e_file) rd100_conf.write("%s\n" % peer_input_n_file) rd100_conf.write("%s\n" % output_rotd100_file) # Close file rd100_conf.close() progstring = ( "%s >> %s 2>&1" % (os.path.join(install.A_UCB_BIN_DIR, "rotd100"), logfile)) bband_utils.runprog(progstring, abort_on_error=True, print_cmd=False) # Restore working directory os.chdir(old_cwd)
def test_runprog3(self): """ Yet another test for the runprog function """ cmd = "produce >/dev/null 2>&1" res = bband_utils.runprog(cmd, False) if res == 0: self.fail("Sub process should returned non-zero and it didn't.")
def test_runprog2(self): """ Another test for the runprog function """ cmd = "issuing_intentionally_nonexistant_command >/dev/null 2>&1" res = bband_utils.runprog(cmd, False) if res == 0: self.fail("Did not properly detect an error running a command on the cmdline")
def test_runprog3(self): """ Yet another test for the runprog function """ cmd = "produce >/dev/null 2>&1" res = bband_utils.runprog(cmd, False) if res == 0: self.fail("Sub process should returned non-zero and it didn't.")
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 test_runprog(self): """ Test runprog function """ cmd = "hostname >/dev/null 2>&1" res = bband_utils.runprog(cmd, False) # print "returned %d" % (res) if not res == 0: self.fail("hostname command did not return success returncode")
def test_runprog(self): """ Test runprog function """ cmd = "hostname >/dev/null 2>&1" res = bband_utils.runprog(cmd, False) # print "returned %d" % (res) if not res == 0: self.fail("hostname command did not return success returncode")
def test_python_code_tests(self): """ Run Python with -tt flag to detect mix of tabs and spaces in the code """ self.install = InstallCfg() cmd = ("python -tt -m compileall -f -q -l %s" % (self.install.A_TEST_DIR)) self.failIf(bband_utils.runprog(cmd, False) != 0, "Python code in test directory mixes tabs and spaces!")
def test_rotd100(self): """ Test the rotd100 module """ # Load configuration, set sim_id sim_id = self.sim_id # Reference directory ref_dir = os.path.join(self.install.A_TEST_REF_DIR, "ucb") r_station_list = "rotd50.stl" a_src_station_list = os.path.join(ref_dir, r_station_list) a_dst_station_list = os.path.join(self.indir, r_station_list) # Copy station list to indir cmd = "cp %s %s" % (a_src_station_list, a_dst_station_list) bband_utils.runprog(cmd) # Copy velocity bbp files to outdir for i in range(1, 6): src_bbp = os.path.join(ref_dir, "8000%d.vel.bbp" % (i)) dst_bbp = os.path.join(self.outdir, "%d.8000%d.vel.bbp" % (sim_id, i)) cmd = "cp %s %s" % (src_bbp, dst_bbp) bband_utils.runprog(cmd) rotd100_obj = rotd100.RotD100(r_station_list, sim_id=sim_id) rotd100_obj.run() # Check results for i in range(1, 6): ref_rd100 = os.path.join(ref_dir, "8000%d.rd100" % (i)) ref_rd100_v = os.path.join(ref_dir, "8000%d.rd100.vertical" % (i)) new_rd100 = os.path.join(self.outdir, "%d.8000%d.rd100" % (sim_id, i)) new_rd100_v = os.path.join( self.outdir, "%d.8000%d.rd100.vertical" % (sim_id, i)) self.failIf( cmp_bbp.cmp_files_generic(ref_rd100, new_rd100) != 0, "Output file %s does not match reference file: %s" % (new_rd100, ref_rd100)) self.failIf( cmp_bbp.cmp_files_generic(ref_rd100_v, new_rd100_v) != 0, "Output file %s does not match reference file: %s" % (new_rd100_v, ref_rd100_v))
def run_tests(self, tests): install = InstallCfg.getInstance() # Run the tests and save results as reference data for test in tests: if os.path.basename(test[1]) in self.resume_list: # Skip this test print("Skipping %s" % (os.path.basename(test[1]))) continue # Execute each test cmd = ("%s -s %d -x %s" % (os.path.join( install.A_COMP_DIR, "run_bbp.py"), test[0], test[1])) rc = bband_utils.runprog(cmd, False) if rc != 0: print("Failed to run acceptance test %d-%s, aborting." % (test[0], test[1])) return 1 # Save the bbp and rd50 files test_name = os.path.basename(test[1]).split('.')[0] cmd = "mkdir -p %s" % (os.path.join(self.ref_dir, test_name)) bband_utils.runprog(cmd) rd50files = glob.glob("%s/%d/%d.*.rd50" % (install.A_OUT_DATA_DIR, test[0], test[0])) if len(rd50files) < 1: print("Did not find expected RotD50 files") return 1 for rd50_file in rd50files: filecomps = os.path.basename(rd50_file).split('.') shutil.copy2( rd50_file, os.path.join(self.ref_dir, test_name, "%s.rd50" % (filecomps[1]))) # Write progress to checkpoint file resume_fp = open( os.path.join(install.A_OUT_LOG_DIR, "gen_resume.txt"), 'a') resume_fp.write("%s\n" % os.path.basename(test[1])) resume_fp.flush() resume_fp.close() return 0
def generate_plot(self, a_statfile, a_dstdir): """ This function generates the bias plot with ratio of maximum to median response across orientations (RotD100/RotD50) """ install = install_cfg.InstallCfg.getInstance() sim_id = self.sim_id slo = StationList(a_statfile) site_list = slo.getStationList() rd100_resid_output = os.path.join( a_dstdir, "%s-%d-resid-rd100.txt" % (self.comp_label, sim_id)) for comp in ['rotd50', 'rotd100', 'ratio']: # Build paths and check lengths fileroot = os.path.join( a_dstdir, "%s-%d_r0-%d-rd100-%s" % (self.comp_label, sim_id, self.max_cutoff, comp)) bband_utils.check_path_lengths([rd100_resid_output, fileroot], bband_utils.GP_MAX_FILENAME) cmd = ("%s " % (os.path.join(install.A_GP_BIN_DIR, "resid2uncer_varN")) + "residfile=%s fileroot=%s " % (rd100_resid_output, fileroot) + "comp=%s nstat=%d nper=63 " % (comp, len(site_list)) + "min_cdst=%d max_cdst=%d >> %s 2>&1" % (0, self.max_cutoff, self.log)) bband_utils.runprog(cmd, abort_on_error=True, print_cmd=False) # Generate bias plot plot_mode = 'rd100' fileroot = ("%s-%d_r0-%d-rd100" % (self.comp_label, sim_id, self.max_cutoff)) plottitle = ("GOF Comparison between %s and simulation %d" % (self.comp_label, sim_id)) plotter = PlotGoF() plotter.plot(plottitle, fileroot, a_dstdir, a_dstdir, cutoff=self.max_cutoff, mode=plot_mode, colorset='single')
def test_execute_platform_bbp(self): """ Run Broadband Plotform to make sure we can start it """ self.install = InstallCfg() cmd = ("python %s -v >/dev/null" % (os.path.join(self.install.A_COMP_DIR, "run_bbp.py"))) self.failIf(bband_utils.runprog(cmd, False) != 0, "Cannot start Broadband plotform!")
def test_execute_platform_bbp(self): """ Run Broadband Plotform to make sure we can start it """ self.install = InstallCfg() cmd = ("python %s -v >/dev/null" % (os.path.join(self.install.A_COMP_DIR, "run_bbp.py"))) self.failIf( bband_utils.runprog(cmd, False) != 0, "Cannot start Broadband plotform!")
def summarize_rotd50(tmpdir, outdir, combined_resid_file, comp_label, num_stations, num_realization, codebase): """ This function summarizes all rotd50 data and creates the combined rotd50 GOF plot """ config = GPGofCfg() # Figure out where out binaries are if "BBP_DIR" in os.environ: install_root = os.path.normpath(os.environ["BBP_DIR"]) else: raise bband_utils.ProcessingError("BBP_DIR is not set!") gp_bin_dir = os.path.join(install_root, "src", "gp", "bin") logfile = os.path.join(tmpdir, "log.txt") for comp in config.COMPS_PSA5: # Build paths and check lengths fileroot = os.path.join( tmpdir, "%s-%s-combined-rd50-%s" % (codebase, comp_label, comp)) bband_utils.check_path_lengths([combined_resid_file, fileroot], bband_utils.GP_MAX_FILENAME) cmd = ("%s " % (os.path.join(gp_bin_dir, "resid2uncer_varN")) + "residfile=%s fileroot=%s " % (combined_resid_file, fileroot) + "comp=%s nstat=%d nper=63 " % (comp, num_stations) + " >> %s 2>&1" % (logfile)) bband_utils.runprog(cmd, abort_on_error=True) plottitle = ("Combined GOF Plot for %s\n%d Realizations - %s Method" % (comp_label, num_realization, codebase.upper())) fileroot = "%s-%s-combined-rd50" % (codebase, comp_label) plotter = PlotGoF() plotter.plot(plottitle, fileroot, tmpdir, outdir, cutoff=0, mode="rd50-single", colorset="combined") print("Stations used: %s" % (num_stations))
def test_runprog2(self): """ Another test for the runprog function """ cmd = "issuing_intentionally_nonexistant_command >/dev/null 2>&1" res = bband_utils.runprog(cmd, False) if res == 0: self.fail( "Did not properly detect an error running a command on the cmdline" )
def get_magnitude(velfile, srffile, suffix="tmp"): """ Scans the srffile and returns the magnitude of the event """ magfile = "srf2moment_%s.out" % (suffix) install = InstallCfg.getInstance() cmd = ("%s velfile=%s < %s 2> %s" % (os.path.join( install.A_GP_BIN_DIR, "srf2moment"), velfile, srffile, magfile)) bband_utils.runprog(cmd, False) srf2moment_fp = open(magfile, 'r') srf2moment_data = srf2moment_fp.readlines() srf2moment_fp.close() #magnitude on last line mag_line = srf2moment_data[len(srf2moment_data) - 4] pieces = mag_line.split() magnitude = float(pieces[5].split(")")[0]) cmd = "rm %s" % (magfile) bband_utils.runprog(cmd, False) return magnitude
def test_python_code_tests(self): """ Run Python with -tt flag to detect mix of tabs and spaces in the code """ self.install = InstallCfg() cmd = ("python -tt -m compileall -f -q -l %s" % (self.install.A_TEST_DIR)) self.failIf( bband_utils.runprog(cmd, False) != 0, "Python code in test directory mixes tabs and spaces!")
def run_tests(self, tests): install = InstallCfg.getInstance() # Run the tests and save results as reference data for test in tests: if os.path.basename(test[1]) in self.resume_list: # Skip this test print("Skipping %s" % (os.path.basename(test[1]))) continue # Execute each test cmd = ("%s/run_bbp.py -s %d -x %s" % (install.A_COMP_DIR, test[0], test[1])) rc = bband_utils.runprog(cmd, False) if rc != 0: print("Failed to run acceptance test %d-%s, aborting." % (test[0], test[1])) return 1 # Save the bbp and rsp files test_name = os.path.basename(test[1]).split('.')[0] cmd = "mkdir -p %s" % (os.path.join(self.ref_dir, test_name)) bband_utils.runprog(cmd) rd50files = glob.glob("%s/%d/%d.*.rd50" % (install.A_OUT_DATA_DIR, test[0], test[0])) if len(rd50files) < 1: print("Did not find expected RotD50 files") return 1 for rd50_file in rd50files: filecomps = os.path.basename(rd50_file).split('.') shutil.copy2(rd50_file, os.path.join(self.ref_dir, test_name, "%s.rd50" % (filecomps[1]))) # Write progress to checkpoint file resume_fp = open(os.path.join(install.A_OUT_LOG_DIR, "gen_resume.txt"), 'a') resume_fp.write("%s\n" % os.path.basename(test[1])) resume_fp.flush() resume_fp.close() return 0
def summarize_rotd50(self, site_list, a_outdir, a_outdir_gmpe): """ Summarizes all rotd50 data and creates the rotd50 GOF plot """ sim_id = self.sim_id install = self.install config = self.config rd50_residfile = os.path.join(a_outdir, "%s-%d.rd50-resid.txt" % (self.comp_label, sim_id)) for comp in config.COMPS_PSA5: # Build paths and check lengths fileroot = os.path.join(a_outdir, "%s-%d_r%d-%d-rd50-%s" % (self.comp_label, sim_id, config.MIN_CDST, self.max_cutoff, comp)) bband_utils.check_path_lengths([rd50_residfile, fileroot], bband_utils.GP_MAX_FILENAME) cmd = ("%s/resid2uncer_varN " % (install.A_GP_BIN_DIR) + "residfile=%s fileroot=%s " % (rd50_residfile, fileroot) + "comp=%s nstat=%d nper=63 " % (comp, len(site_list)) + "min_cdst=%d max_cdst=%d >> %s 2>&1" % (config.MIN_CDST, self.max_cutoff, self.log)) bband_utils.runprog(cmd, abort_on_error=True, print_cmd=False) # Plot GOF for psa5/rotd50 data if self.single_component: plot_mode = 'rd50-single' else: plot_mode = 'rd50' fileroot = '%s-%d_r0-%d-rd50' % (self.comp_label, sim_id, self.max_cutoff) plottitle = ("GOF Comparison between %s and simulation %d" % (self.comp_label, sim_id)) plotter = PlotGoF() plotter.plot(plottitle, fileroot, a_outdir, a_outdir, cutoff=self.max_cutoff, mode=plot_mode, colorset='single') # Finally, plot the distance and map GOFs plot_dist_gof(rd50_residfile, self.comp_label, a_outdir_gmpe, sim_id=self.sim_id) plot_map_gof(self.r_srcfile, self.r_stations, rd50_residfile, self.comp_label, sim_id=self.sim_id)
def get_magnitude(velfile, srffile, suffix="tmp"): """ Scans the srffile and returns the magnitude of the event """ magfile = "srf2moment_%s.out" % (suffix) install = InstallCfg.getInstance() cmd = ("%s velfile=%s < %s 2> %s" % (os.path.join(install.A_GP_BIN_DIR, "srf2moment"), velfile, srffile, magfile)) bband_utils.runprog(cmd, False) srf2moment_fp = open(magfile, 'r') srf2moment_data = srf2moment_fp.readlines() srf2moment_fp.close() #magnitude on last line mag_line = srf2moment_data[len(srf2moment_data) - 4] pieces = mag_line.split() magnitude = float(pieces[5].split(")")[0]) cmd = "rm %s" % (magfile) bband_utils.runprog(cmd, False) return magnitude
def get_hypocenter(srffile, suffix="tmp"): """ Looks up the hypocenter of an event in a srffile """ hypfile = "srf_hypo_%s" % (suffix) install = InstallCfg.getInstance() cmd = ("%s < %s > %s" % (os.path.join(install.A_GP_BIN_DIR, "srf_gethypo"), srffile, hypfile)) bband_utils.runprog(cmd) srf_hypo_fp = open(hypfile, 'r') srf_hypo_data = srf_hypo_fp.readline() srf_hypo_fp.close() srf_hypo = srf_hypo_data.split() hypo = [] for i in range(0, 3): hypo.append(float(srf_hypo[i])) cmd = "rm %s" % (hypfile) bband_utils.runprog(cmd) return hypo
def setUp(self): self.install = InstallCfg() self.r_srcfile = "test_wh_ucsb.src" self.r_faultfile = "ffsp.inp" self.r_velmodel = "labasin.vel" self.vmodel_name = "LABasin" self.sim_id = int(seqnum.get_seq_num()) cmd = "mkdir -p %s/%d" % (self.install.A_IN_DATA_DIR, self.sim_id) bband_utils.runprog(cmd) cmd = "mkdir -p %s/%d" % (self.install.A_TMP_DATA_DIR, self.sim_id) bband_utils.runprog(cmd) cmd = "mkdir -p %s/%d" % (self.install.A_OUT_DATA_DIR, self.sim_id) bband_utils.runprog(cmd) cmd = "mkdir -p %s/%d" % (self.install.A_OUT_LOG_DIR, self.sim_id) bband_utils.runprog(cmd) cmd = "cp %s/ucsb/%s %s/%d/." % (self.install.A_TEST_REF_DIR, self.r_srcfile, self.install.A_IN_DATA_DIR, self.sim_id) bband_utils.runprog(cmd)
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 generate_plot(self, a_statfile, a_dstdir): """ This function generates the bias plot with ratio of maximum to median response across orientations (RotD100/RotD50) """ install = install_cfg.InstallCfg.getInstance() sim_id = self.sim_id slo = StationList(a_statfile) site_list = slo.getStationList() rd100_resid_output = os.path.join(a_dstdir, "%s-%d-resid-rd100.txt" % (self.comp_label, sim_id)) for comp in ['rotd50', 'rotd100', 'ratio']: # Build paths and check lengths fileroot = os.path.join(a_dstdir, "%s-%d_r0-%d-rd100-%s" % (self.comp_label, sim_id, self.max_cutoff, comp)) bband_utils.check_path_lengths([rd100_resid_output, fileroot], bband_utils.GP_MAX_FILENAME) cmd = ("%s " % (os.path.join(install.A_GP_BIN_DIR, "resid2uncer_varN")) + "residfile=%s fileroot=%s " % (rd100_resid_output, fileroot) + "comp=%s nstat=%d nper=63 " % (comp, len(site_list)) + "min_cdst=%d max_cdst=%d >> %s 2>&1" % (0, self.max_cutoff, self.log)) bband_utils.runprog(cmd, abort_on_error=True, print_cmd=False) # Generate bias plot plot_mode = 'rd100' fileroot = ("%s-%d_r0-%d-rd100" % (self.comp_label, sim_id, self.max_cutoff)) plottitle = ("GOF Comparison between %s and simulation %d" % (self.comp_label, sim_id)) plotter = PlotGoF() plotter.plot(plottitle, fileroot, a_dstdir, a_dstdir, cutoff=self.max_cutoff, mode=plot_mode, colorset='single')
def summarize_rotd50(tmpdir, outdir, combined_resid_file, comp_label, num_stations, num_realization, codebase): """ This function summarizes all rotd50 data and creates the combined rotd50 GOF plot """ config = GPGofCfg() # Figure out where out binaries are if "BBP_DIR" in os.environ: install_root = os.path.normpath(os.environ["BBP_DIR"]) else: raise bband_utils.ProcessingError("BBP_DIR is not set!") gp_bin_dir = os.path.join(install_root, "src", "gp", "bin") logfile = os.path.join(tmpdir, "log.txt") for comp in config.COMPS_PSA5: # Build paths and check lengths fileroot = os.path.join(tmpdir, "%s-%s-combined-rd50-%s" % (codebase, comp_label, comp)) bband_utils.check_path_lengths([combined_resid_file, fileroot], bband_utils.GP_MAX_FILENAME) cmd = ("%s/resid2uncer_varN " % (gp_bin_dir) + "residfile=%s fileroot=%s " % (combined_resid_file, fileroot) + "comp=%s nstat=%d nper=63 " % (comp, num_stations) + " >> %s 2>&1" % (logfile)) bband_utils.runprog(cmd, abort_on_error=True) plottitle = ("Combined GOF Plot for %s\n%d Realizations\n%s Method" % (comp_label, num_realization, codebase.upper())) fileroot = "%s-%s-combined-rd50" % (codebase, comp_label) plotter = PlotGoF() plotter.plot(plottitle, fileroot, tmpdir, outdir, cutoff=0, mode="rd50-single", colorset="combined") print "Stations used: %s" % (num_stations)
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() 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 generate_xml(self, optfiles): install = InstallCfg.getInstance() # Generate xml workflows tests = [] for key in optfiles.keys(): sim_id = int(seqnum.get_seq_num()) test = key.split('.')[0] xmlfile = os.path.join(self.input_dir, "%s.xml" % (test)) if os.path.basename(xmlfile) in self.resume_list: # Skip this test print("Skipping %s" % (key)) continue print("Generating %s" % (key)) optfile = os.path.join(self.input_dir, key) # Save the option file op = open(optfile, 'w') for line in optfiles[key]: op.write("%s\n" % (line)) op.close() # Generate xml print("Generating xml for %s" % (key)) print("\t %s" % (str(optfiles[key]))) cmd = ("%s/run_bbp.py --expert -s %d -g -o %s" % (install.A_COMP_DIR, sim_id, optfile)) print("Running: %s" % (cmd)) rc = bband_utils.runprog(cmd, False) if rc != 0: print("Failed to run bbp, aborting.") return [] oldxmlfile = os.path.join(install.A_XML_DIR, "%d.xml" % (sim_id)) shutil.copy2(oldxmlfile, xmlfile) if not os.path.exists(xmlfile): print("Workflow %s not found, aborting." % (xmlfile)) return [] tests.append([sim_id, xmlfile]) time.sleep(1) return tests
def permutation_test(self, file_base=file_base, xml_file=xml_file): input_dir = os.path.join(self.install.A_TEST_REF_DIR, accept_test_inputs) log_dir = os.path.join(self.install.A_OUT_LOG_DIR, "acceptance_test_logs") sim_id = int(seqnum.get_seq_num()) self.file_base = file_base self.log_file = os.path.join(log_dir, "%s.log" % (self.file_base)) self.input_file = os.path.join(input_dir, xml_file) cmd = ("%s/run_bbp.py -x %s -s %d -l %s" % (self.install.A_COMP_DIR, self.input_file, sim_id, self.log_file)) rc = bband_utils.runprog(cmd, False) self.failIf(rc != 0, "Acceptance test failed to execute") ref_file_dir = os.path.join(self.install.A_TEST_REF_DIR, accept_test_refs, self.file_base) agree = True for ref_file in os.listdir(ref_file_dir): if os.path.isfile(os.path.join(ref_file_dir, ref_file)): test_file = os.path.join(self.install.A_OUT_DATA_DIR, str(sim_id), ("%d.%s" % (sim_id, ref_file))) a_ref_file = os.path.join(ref_file_dir, ref_file) compare_result = cmp_bbp.cmp_bbp(a_ref_file, test_file, tolerance=tolerance) errmsg = ("Output file " "%s does not match reference file: %s" % (test_file, a_ref_file)) self.failIf(compare_result != 0, errmsg) if compare_result != 0: agree = False if agree == True: # Write success to the resume file resume_fp = open(os.path.join(install.A_OUT_LOG_DIR, "resume.txt"), 'a') resume_fp.write("%s\n" % xml_file) resume_fp.flush() resume_fp.close() sys.stdout.flush() sys.stderr.flush()
def generate_xml(install, numsim, srcdir, xmldir, logdir, vmodel, codebase, prefix, station_list, only_rup, srf_prefix, site_response): """ Generates xml files in the xmldir for numsim simulations whose source files are in the srcdir using the validation event and codebase specified """ tmpdir = tempfile.mkdtemp(prefix="bbp-") bbproot = os.path.join(install.A_COMP_DIR, "run_bbp.py") bfn = os.path.join(xmldir, BATCH_SIM_FILE) batchfile = open(bfn, 'w') for sim in range(0, numsim): if srf_prefix is None: srcfile = os.path.join(srcdir, "%s-%04d.src" % (prefix, sim)) else: srffile = "%s-%04d.srf" % (srf_prefix, sim) # Make sure srf file exists and is readable if (not os.path.isfile(srffile) or not os.access(srffile, os.R_OK)): print "SRF file %s does not seem to be accessible!" % (srffile) sys.exit(1) ofn = os.path.join(tmpdir, "bbp.optfile") optfile = open(ofn, 'w') optfile.write('n\n') # Scenario optfile.write('%s\n' % (vmodel)) # Velocity model optfile.write('%s\n' % (codebase)) # Codebase to use if codebase != "exsim" and codebase != "csm": if srf_prefix is None: optfile.write('y\n') # Run rupture generator else: optfile.write('n\n') # Skip rupture generator optfile.write('2\n') # Enter path to src/srf file if srf_prefix is None: optfile.write('%s\n' % (srcfile)) # Source file else: optfile.write('%s\n' % (srffile)) # SRF file optfile.write('2\n') # Enter path to station list optfile.write('%s\n' % (station_list)) # Station list if codebase == "exsim": # Don't specify custom ExSIM template file optfile.write('n\n') if codebase != "csm": if site_response: # Use site response optfile.write('y\n') else: # Skip site response optfile.write('n\n') optfile.write('y\n') # Plot velocity seismograms optfile.write('y\n') # Plot acceleration seismograms optfile.flush() optfile.close() # Run BBP and generate the xml file bband_utils.runprog("export BBP_DATA_DIR=%s; " "%s --expert -o %s -s %d -g" % (tmpdir, bbproot, ofn, sim+1)) # Copy the xml file srcxml = os.path.join(tmpdir, "xml", "%d.xml" % (sim+1)) dstxml = os.path.join(xmldir, "%s-%04d.xml" % (prefix, sim)) shutil.copy2(srcxml, dstxml) # Add entry to the batch file bbp_sim = 10000000 + sim logbase = os.path.join(logdir, str(bbp_sim)) logfile = os.path.join(logbase, "%d_%s.log" % (bbp_sim, prefix)) # Make sure logdir exists os.makedirs(logbase) # Write this run in the batch file if only_rup: if codebase == "gp" or codebase == "sdsu": end_module = " -e Genslip " elif codebase == "ucsb": end_module = " -e UCrmg " else: end_module = "" batchfile.write("%s -x %s -s %d %s-l %s\n" % (bbproot, dstxml, bbp_sim, end_module, logfile)) # Close batch file batchfile.flush() batchfile.close() # Clean-up shutil.rmtree(tmpdir)