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 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))
# Create temp dir TMPDIR = tempfile.mkdtemp(prefix="bbp-") resid_file = os.path.join(TMPDIR, "bbp-rd50-resid.txt") log_file = os.path.join(TMPDIR, "bbp-rd50-resid.log") # Get input parameters station_list = sys.argv[1] src_file = sys.argv[2] sim_id_1 = int(sys.argv[3]) sim_id_2 = int(sys.argv[4]) output_dir = sys.argv[5] # Create directory paths install = InstallCfg.getInstance() config = GPGofCfg() a_indir = os.path.join(install.A_IN_DATA_DIR, str(sim_id_1)) a_outdir1 = os.path.join(install.A_OUT_DATA_DIR, str(sim_id_1)) a_outdir2 = os.path.join(install.A_OUT_DATA_DIR, str(sim_id_2)) # Src file a_srcfile = os.path.join(a_indir, src_file) src_keys = bband_utils.parse_src_file(a_srcfile) # Station file a_statfile = os.path.join(a_indir, station_list) slo = StationList(a_statfile) site_list = slo.getStationList() # Capture event_label bias_file = glob.glob("%s%s*.bias" % (a_outdir1, os.sep))
def run(self): """ This function in the main entry point for this module. It runs the gp_gof component. """ print("GP GoF".center(80, '-')) # Initialize basic variables self.install = InstallCfg.getInstance() self.config = GPGofCfg() install = self.install 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.gp_gof.log" % (sim_id)) # Input, tmp, and output directories a_outdir = os.path.join(install.A_OUT_DATA_DIR, str(sim_id)) a_outdir_seis = os.path.join(install.A_OUT_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)) # Source file, parse it! a_srcfile = os.path.join(install.A_IN_DATA_DIR, str(sim_id), self.r_srcfile) 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) # List of observed seismogram files filelist = os.listdir(a_outdir_seis) slo = StationList(a_statfile) site_list = slo.getStationList() # check cutoff value if self.max_cutoff is None: self.max_cutoff = config.MAX_CDST print_header_rd50 = 1 # Remove rd50 resid file rd50_resid_output = os.path.join(a_outdir, "%s-%d.rd50-resid.txt" % (self.comp_label, sim_id)) if os.path.exists(rd50_resid_output): os.remove(rd50_resid_output) for site in site_list: slon = float(site.lon) slat = float(site.lat) stat = site.scode # Now process rd50 files 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("Skipping rotd50/psa5 for station %s..." % (stat)) continue # See if the rd50 file exist for comparison. If it doesn't # exist, skip this station rd50_file = None if ("%s.rd50" % (stat)) in filelist: rd50_file = "%s.rd50" % (stat) else: # Skip this station continue # 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) # Create path names and check if their sizes are within bounds datafile1 = os.path.join(a_outdir_seis, rd50_file) simfile1 = os.path.join(a_outdir, "%d.%s.rd50" % (sim_id, stat)) outfile = os.path.join(a_outdir, "%s-%d.rd50-resid.txt" % (self.comp_label, self.sim_id)) bband_utils.check_path_lengths([datafile1, simfile1, outfile], bband_utils.GP_MAX_FILENAME) cmd = ("%s/gen_resid_tbl_3comp bbp_format=1 " % (install.A_GP_BIN_DIR) + "datafile1=%s simfile1=%s " % (datafile1, simfile1) + "comp1=psa5n comp2=psa5e comp3=rotd50 " + "eqname=%s mag=%s stat=%s lon=%.4f lat=%.4f " % (self.comp_label, self.mag, stat, slon, slat) + "vs30=%d cd=%.2f " % (site.vs30, rrup) + "flo=%f fhi=%f " % (site.low_freq_corner, site.high_freq_corner) + "print_header=%d >> %s 2>> %s" % (print_header_rd50, outfile, self.log)) bband_utils.runprog(cmd, abort_on_error=True, print_cmd=False) # Only need to print header the first time if print_header_rd50 == 1: print_header_rd50 = 0 # Finished per station processing, now summarize and plot the data if os.path.exists(rd50_resid_output): self.summarize_rotd50(site_list, a_outdir, a_outdir_gmpe) print("GP GoF Completed".center(80, '-'))