Exemple #1
0
 def __init__(self, station_file, sim_id=0, hypo=None):
     self.station_file = station_file
     self.sim_id = sim_id
     self.install = InstallCfg.getInstance()
     self.coast_file = os.path.join(self.install.A_PLOT_DATA_DIR,
                                    "cali_coastline.mapgen")
     if not os.path.isfile(self.coast_file):
         self.coast_file = ""
     self.value = "GOF"
     self.stats = []
     self.dx = 500.0 #100 mts grid resolution
     self.spacing = [self.dx, self.dx]
     self.hypo = hypo
     self.dim = []
     self.rbounds = []
     self.nw = []
     self.sw = []
     self.se = []
     self.ne = []
     self.PLOT_MAP_LOC = [0.10, 0.15, 0.8, 0.8]
     self.origin = []
     self.offset = []
     self.x_invert = False
     self.y_invert = False
     self.init_dims()
Exemple #2
0
    def run(self):
        """
        Corrects the amplitudes from all stations found in the station
        list according to the correction coefficients provided by the user
        """
        print("Correct PSA".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))

        # Station file
        a_statfile = os.path.join(a_indir, self.r_stations)

        slo = StationList(a_statfile)
        site_list = slo.getStationList()

        # Go through each station
        # print "Working dir: %s" % (self.proc_dir)
        for site in site_list:
            stat = site.scode
            print("==> Correcting amplitudes for station: %s" % (stat))
            self.correct_station(stat, self.extension)

        print("Correct PSA Completed".center(80, '-'))
Exemple #3
0
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)
Exemple #4
0
 def __init__(self, snum=None):
     if snum == None:
         self.sim_id = seqnum.get_seq_num()
     else:
         self.sim_id = int(snum)
     install = InstallCfg.getInstance()
     logdir = install.A_OUT_LOG_DIR
     self.outlogfile = logdir + "/%d/%d.txt" % (self.sim_id, self.sim_id)
Exemple #5
0
 def __init__(self, sim_id=0, options=None):
     """
     Initialization for BBPStatus class
     """
     self.sim_id = sim_id
     self.__options = options
     self.install = InstallCfg.getInstance()
     self.outdir = os.path.join(self.install.A_OUT_DATA_DIR, str(sim_id))
Exemple #6
0
    def run(self):
        """
        Generates a map showing the fault with stations
        """
        print("Plot MAP".center(80, '-'))

        if (self.input_file is None or self.input_file == "" or
            (not self.input_file.endswith(".srf") and
            not self.input_file.endswith(".src"))):
            # We need a SRC or SRF file to get the fault geometry
            return

        install = InstallCfg.getInstance()

        a_indir = os.path.join(install.A_IN_DATA_DIR, str(self.sim_id))
        a_outdir = os.path.join(install.A_OUT_DATA_DIR, str(self.sim_id))

        a_input_file = os.path.join(a_indir, self.input_file)
        a_station_file = os.path.join(a_indir, self.station_file)

        # Define boundaries to plot using the stations in the station file
        (self.north, self.south,
         self.east, self.west) = set_boundaries_from_stations(a_station_file)

        self.log = os.path.join(install.A_OUT_LOG_DIR, str(self.sim_id),
                                "%d.plot_map.log" % (self.sim_id))
        trace_file = "%s.trace" % (a_input_file)
        simple_station_file = "%s.simple" % (a_station_file)
        if self.input_file.endswith(".srf"):
            self.trace = write_fault_trace(a_input_file, trace_file)
        else:
            self.trace = write_simple_trace(a_input_file, trace_file)
        write_simple_stations(a_station_file, simple_station_file)
        map_prefix = os.path.join(a_outdir, "station_map")
        kml_file = os.path.join(a_outdir, "station_map.kml")
        # Get hypo_lon, hypo_lat from src/srf file
        hypo_lon, hypo_lat = fault_utils.calculate_epicenter(a_input_file)

        # Write the kml file
        self.create_kml_output(a_station_file, kml_file,
                               hypo_lat=hypo_lat, hypo_lon=hypo_lon)

        # Matplotlib
        plottitle = 'Fault Trace with Stations'
        plotregion = [self.west, self.east,
                      self.south, self.north]
        topo = os.path.join(install.A_PLOT_DATA_DIR, 'calTopo18.bf')
        coastal = os.path.join(install.A_PLOT_DATA_DIR, 'gshhs_h.txt')
        border = os.path.join(install.A_PLOT_DATA_DIR, 'wdb_borders_h.txt')
        plotter = PlotMap.PlotMap()
        plotter.plot(plottitle, plotregion, topo,
                     coastal, border, trace_file,
                     simple_station_file, map_prefix,
                     hypo_lat=hypo_lat, hypo_lon=hypo_lon)

        print("Plot MAP Completed".center(80, '-'))
Exemple #7
0
    def __init__(self, vmodel_name, a_srcname=None):
        install = InstallCfg.getInstance()
        #
        # Name and Path to executable
        #
        self.R_UC_FFSP_EXE = "ffsp_v2"
        self.A_UC_FFSP_EXE = os.path.join(install.A_UCSB_BIN_DIR,
                                          self.R_UC_FFSP_EXE)
        self.FFSP_OUTPUT_PREFIX = "FFSP_OUTPUT"

        self.FMAX = 50.0 # Nyquist -- use 50 for 100Hz

        vmodel_obj = velocity_models.get_velocity_model_by_name(vmodel_name)
        if vmodel_obj is None:
            raise IndexError("Cannot find velocity model: %s" %
                             (vmodel_name))

        vmodel_params = vmodel_obj.get_codebase_params('ucsb')
        # Configure DT based on information from velocity model
        if 'GF_DT' in vmodel_params:
            self.DT = float(vmodel_params['GF_DT'])
        else:
            raise KeyError("%s parameter missing in velocity model %s" %
                           ("GF_DT", vmodel_name))

        # Other region-specific parameters
        if 'RV_AVG' in vmodel_params:
            self.RV_AVG = float(vmodel_params['RV_AVG'])
        else:
            self.RV_AVG = 2.5

        if 'TP_TR' in vmodel_params:
            self.TP_TR = float(vmodel_params['TP_TR'])
        else:
            self.TP_TR = 0.1

        if 'LF_VELMODEL' in vmodel_params:
            self.A_UC_LF_VELMODEL = os.path.join(vmodel_obj.base_dir,
                                                 vmodel_params['LF_VELMODEL'])
        else:
            raise KeyError("%s parameter missing in velocity model %s" %
                           ("LF_VELMODEL", vmodel_name))

        if a_srcname:
            self.CFGDICT = bband_utils.parse_src_file(a_srcname)

            # RV_AVG is optional!
            # If SRC file has it, it overrides the region and the default values
            if "rv_avg" in self.CFGDICT:
                self.RV_AVG = self.CFGDICT["rv_avg"]
Exemple #8
0
    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, '-'))
Exemple #9
0
    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, '-'))
Exemple #10
0
def plot_dist_gof(resid_file, comp_label, a_outdir_gmpe, sim_id):
    """
    Reads data from resid_file and plots a gof distance plot all
    periods
    """
    # Get directory names
    install = InstallCfg.getInstance()
    a_outdir = os.path.join(install.A_OUT_DATA_DIR, str(sim_id))

    # Collect all the data
    all_data = []
    all_distances = []
    all_gmpe_data = []
    all_gmpe_distances = []
    # Read the residuals data
    for period in DIST_PERIODS:
        summary_output = os.path.join(a_outdir, "%s-%d-resid-%.3f-%s.txt" %
                                      (comp_label, sim_id,
                                       period, COMP_EXT_RD50))
        data, distance = read_resid(resid_file, period, summary_output)
        all_data.append(data)
        all_distances.append(distance)
    # Now do the same for the GMPE data
#    for period in DIST_PERIODS:
#        if os.path.isdir(a_outdir_gmpe):
#            summary_output = os.path.join(a_outdir,
#                                          "%s-%d-resid-gmpe-%.3f-%s.txt" %
#                                          (comp_label, sim_id,
#                                           period, COMP_EXT_RD50))
#            data, distance = read_gmpe_resid(a_outdir_gmpe, sim_id,
#                                             period, summary_output)
#            all_gmpe_data.append(data)
#            all_gmpe_distances.append(distance)
#        else:
#            all_gmpe_data.append([])
#            all_gmpe_distances.append([])

    # Now create the 2 plots, 1 linear and 1 log
    outfile = os.path.join(a_outdir, "gof-dist-linear-%s-%d-rotd50.png" %
                           (comp_label, sim_id))
    create_dist_gof(all_data, all_distances,
                    all_gmpe_data, all_gmpe_distances,
                    comp_label, sim_id, outfile)

    outfile = os.path.join(a_outdir, "gof-dist-log-%s-%d-rotd50.png" %
                           (comp_label, sim_id))
    create_dist_gof(all_data, all_distances,
                    all_gmpe_data, all_gmpe_distances,
                    comp_label, sim_id, outfile, log_scale=True)
 def __init__(self):
     """
     Initialize class structures
     """
     self.install = InstallCfg.getInstance()
     self.output_sim_id = None
     self.a_indir = None
     self.a_outdir = None
     self.a_logdir = None
     self.a_tmpdir = None
     self.station_list = None
     self.input_sims = []
     self.src_files = []
     self.srf_files = []
     self.val_event = None
Exemple #12
0
    def run(self):
        """
        Goes through the station list and copy each low-frequency
        seismogram from the seis_dir to the simulation's tmpdir
        """
        install = InstallCfg.getInstance()
        sim_id = self.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))
        a_stations = os.path.join(a_indir, self.r_stations)

        print(self.seis_dir)

        slo = StationList(a_stations)
        stat_list = slo.getStationList()
        for stat in stat_list:
            # Look for bbp seismogram, copy in
            print("%s/%s-lf.bbp" % (self.seis_dir, stat.scode))
            if os.path.exists("%s/%s-lf.bbp" % (self.seis_dir, stat.scode)):
                print("Copying for site %s" % (stat.scode))
                # Need to eliminate negative times
                fp_in = open("%s/%s-lf.bbp" % (self.seis_dir, stat.scode), 'r')
                fp_out = open("%s/%d.%s-lf.bbp" %
                              (a_tmpdir, sim_id, stat.scode), 'w')
                for line in fp_in:
                    pieces = line.split()
                    try:
                        if pieces[0] == '#' or pieces[0] == '%':
                            fp_out.write(line)
                        elif float(pieces[0]) < -0.0001:
                            continue
                        elif float(pieces[0]) < 0.0001:
                            fp_out.write("0.0\t%s\t%s\t%s\n" % (pieces[1],
                                                                pieces[2],
                                                                pieces[3]))
                        else:
                            fp_out.write(line)
                    except ValueError:
                        fp_out.write(line)
                fp_in.close()
                fp_out.flush()
                fp_out.close()
            else:
                print("Could not find LF seismogram for station %s!" %
                      (stat.scode))
Exemple #13
0
    def __init__(self, i_r_stations, i_r_srcfile,
                 plot_vel, plot_acc, sim_id=0):
        """
        Initialize basic class parameters
        """
        self.r_stations = i_r_stations
        self.plot_vel = plot_vel
        self.plot_acc = plot_acc
        self.sim_id = sim_id

        install = InstallCfg.getInstance()
        a_indir = os.path.join(install.A_IN_DATA_DIR, str(self.sim_id))

        if i_r_srcfile is not None and i_r_srcfile != "":
            i_a_srcfile = os.path.join(a_indir, i_r_srcfile)
            self.src_keys = bband_utils.parse_src_file(i_a_srcfile)
        else:
            self.src_keys = None
Exemple #14
0
    def setUp(self):
        """
        Configures the environment for the tests
        """
        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],
                           print_cmd=False)
Exemple #15
0
    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
Exemple #16
0
    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
Exemple #17
0
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
Exemple #18
0
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
Exemple #19
0
    def __init__(self, resume=True):
        install = InstallCfg.getInstance()
        self.resume = resume
        self.resume_list = []

        # Read checkpoint file
        if self.resume == True:
            resume_file = os.path.join(install.A_OUT_LOG_DIR,
                                       "gen_resume.txt")
            if os.path.exists(resume_file):
                resume_fp = open(resume_file, 'r')
                self.resume_list = resume_fp.read().splitlines()
                resume_fp.close()
            else:
                self.resume = False

        # Setup paths
        self.input_dir = os.path.join(install.A_TEST_REF_DIR,
                                      "accept_inputs")
        self.ref_dir = os.path.join(install.A_TEST_REF_DIR,
                                    "accept_refs")
Exemple #20
0
def uc_create_fault_global(a_faultfile, sim_id, r_srcfile,
                           i_vmodel_name, r_velmodel, r_srffile):
    """
    This fuction creates the faultfile with the parameters specified
    in the src_file and velocity model
    """
    install = InstallCfg.getInstance()
    a_srcfile = os.path.join(install.A_IN_DATA_DIR, str(sim_id), r_srcfile)
    # since KinModel appends .srf automatically, make sure it's not
    # already there
    if r_srffile.endswith(".srf"):
        r_srffile = r_srffile[0:len(r_srffile) - 4]

    # Read source file
    cfg = UCrmgCfg(i_vmodel_name, a_srcfile)

    # Write configuration file
    print("Creating %s" % (a_faultfile))

    fault_file = open(a_faultfile, "w")
    fault_file.write("%.3f %.3f %.1f\n" % (cfg.CFGDICT["lon_top_center"],
                                           cfg.CFGDICT["lat_top_center"],
                                           cfg.CFGDICT["depth_to_top"]))
    fault_file.write("%.2f %.2f\n" % (cfg.CFGDICT["fault_length"],
                                      cfg.CFGDICT["fault_width"]))
    fault_file.write("%.f. %.f. %.f.\n" % (cfg.CFGDICT["strike"],
                                           cfg.CFGDICT["dip"],
                                           cfg.CFGDICT["rake"]))
    fault_file.write("%.1f %.1f\n" % (cfg.CFGDICT["hypo_along_stk"],
                                      cfg.CFGDICT["hypo_down_dip"]))
    fault_file.write("%4.2f\n" % (cfg.CFGDICT['magnitude']))
    fault_file.write("%.3f %.3f\n" % (cfg.CFGDICT["dlen"],
                                      cfg.CFGDICT["dwid"]))
    fault_file.write("%d\n" % (cfg.CFGDICT['seed']))
    fault_file.write("%.2f\n" % (cfg.DT))
    fault_file.write("%.2f\n" % (cfg.CFGDICT['corner_freq']))
    fault_file.write("%s\n" % (r_velmodel))
    fault_file.write("%s\n" % (r_srffile))
    fault_file.close()
def main():
    """
    Parse command line options and create the needed files/directories
    """
    # Detect BBP installation
    bbp_install = InstallCfg.getInstance()

    prog_base = os.path.basename(sys.argv[0])
    usage = "usage: %s [options]" % (prog_base)
    parser = optparse.OptionParser(usage)
    parser.add_option("-c", "--codebase", type="string", action="store",
                      dest="codebase",
                      help="Codebase for the simulation: %s" %
                      (CODEBASES))
    parser.add_option("-v", "--velocity-model", type="string", action="store",
                      dest="vmodel",
                      help="Velocity model (region) for this simulation")
    parser.add_option("--src", "--source", type="string", action="store",
                      dest="source",
                      help="Source description file for the simulation")
    parser.add_option("--stl", "--station-list", type="string", action="store",
                      dest="station_list",
                      help="Station list file for the simulation")
    parser.add_option("-d", "--dir", type="string", action="store",
                      dest="simdir",
                      help="Simulation directory")
    parser.add_option("-n", "--num-stations", type="int", action="store",
                      dest="numsta", help="Number of stations per run")
    parser.add_option("--seed", type="int", action="store",
                      dest="new_seed", help="Overrides seed in SRC file")
    parser.add_option("--email", type="string", action="store",
                      dest="email", help="Email for job notifications")
    parser.add_option("--new-nodes", action="store_true", dest="newnodes",
                      help="Schedule the job in the new HPCC nodes")
    parser.add_option("--no-site-response", action="store_true", dest="nosite",
                      help="Disable the site response module for GP/SDSU/UCSB")
    (options, _) = parser.parse_args()

    # Check if using new HPCC nodes
    if options.newnodes:
        newnodes = True
    else:
        newnodes = False

    # Check if not using site response
    if options.nosite:
        nosite = True
    else:
        nosite = False

    # Validate codebase to use
    codebase = options.codebase
    if codebase is None:
        print "Please specify a codebase!"
        sys.exit(1)
    codebase = codebase.lower()
    if codebase not in CODEBASES:
        print "Codebase needs to be one of: %s" % (CODEBASES)

    # Check for velocity model
    vmodel_names = velocity_models.get_all_names()
    vmodel = options.vmodel
    if vmodel is None:
        print "Please provide a velocity model (region) for this simulation!"
        print "Available options are: %s" % (vmodel_names)
        sys.exit(1)
    vmodels = [v_model.lower() for v_model in vmodel_names]
    if vmodel.lower() not in vmodels:
        print ("Velocity model %s does not appear to be available on BBP" %
               (vmodel))
        print ("Available options are: %s" % (vmodel_names))
        print "Please provide another velocity model or check your BBP installation."
        sys.exit(1)
    # Now get the name with the correct case
    vmodel = vmodel_names[vmodels.index(vmodel.lower())]

    # Get the source file
    source_file = options.source
    if source_file is None:
        print "Please provide a source description (src file)!"
        sys.exit(1)
    # Make it a full path
    source_file = os.path.realpath(source_file)
    # Make sure source file is in the rcf-104 filesystem
    if not "rcf-104" in source_file:
        print "Source file should be in the rcf-104 filesystem!"
        sys.exit(1)
    # Make sure source file exists and is readable
    if not os.path.isfile(source_file) or not os.access(source_file, os.R_OK):
        print "Source file does not seem to be accessible!"
        sys.exit(1)

    # Get the station list
    station_list = options.station_list
    if station_list is None:
        print "Please provide a station list (stl file)!"
        sys.exit(1)
    # Make it a full path
    station_list = os.path.realpath(station_list)
    # Make sure station list is in the rcf-104 filesystem
    if not "rcf-104" in station_list:
        print "Station list should be in the rcf-104 filesystem!"
        sys.exit(1)
    # Make sure station list exists and is readable
    if not os.path.isfile(station_list) or not os.access(station_list, os.R_OK):
        print "Station list foes not seem to be accessible!"
        sys.exit(1)

    # Check for the simulation directory
    simdir = options.simdir
    if simdir is None:
        print "Please provide a simulation directory!"
        sys.exit(1)
    simdir = os.path.abspath(simdir)
    if os.path.exists(simdir):
        print "Simulation directory exists: %s" % (simdir)
        opt = raw_input("Do you want to delete its contents (y/n)? ")
        if opt.lower() != "y":
            print "Please provide another simulation directory!"
            sys.exit(1)
        opt = raw_input("ARE YOU SURE (y/n)? ")
        if opt.lower() != "y":
            print "Please provide another simulation directory!"
            sys.exit(1)
        # Delete existing directory (we already asked the user twice!!!)
        shutil.rmtree(simdir)

    # Pick up number of simulations to run
    numsta = options.numsta
    if numsta < 1:
        print ("Number of stations should be greater than 0")
        sys.exit(1)

    # Check for user-provided seed for this simulation
    new_seed = options.new_seed

    # Check for e-mail address
    email = options.email
    if email is None:
        print "Please provide an e-mail address for job notifications"
        sys.exit(1)

    # Make sure user has configured the setup_bbp_env.sh script
    setup_bbp_env = os.path.join(bbp_install.A_INSTALL_ROOT,
                                 "utils/batch/setup_bbp_env.sh")
    if not os.path.exists(setup_bbp_env):
        print ("Cannot find setup_bbp_env.sh script!")
        print ("Expected at: %s" % (setup_bbp_env))
        sys.exit(1)
    # Create simulation directories
    prefix = "%s-%s" % (os.path.splitext(os.path.basename(source_file))[0],
                        codebase.lower())
    # Make sure we remove spaces from prefix
    prefix = prefix.replace(" ", '')
    os.makedirs(simdir)
    indir = os.path.join(simdir, "Sims", "indata")
    outdir = os.path.join(simdir, "Sims", "outdata")
    tmpdir = os.path.join(simdir, "Sims", "tmpdata")
    logsdir = os.path.join(simdir, "Sims", "logs")
    xmldir = os.path.join(simdir, "Xml")
    srcdir = os.path.join(simdir, "Src")
    stldir = os.path.join(simdir, "Stl")
    for mdir in [indir, outdir, tmpdir, logsdir, xmldir, srcdir, stldir]:
        os.makedirs(mdir)
    # Generate station lists
    numsim, stlbase = generate_stl_files(station_list, numsta, stldir)
    if numsim > MAX_SIMULATIONS:
        print "Too many simulations requested!"
        print "Maximum number allowed is %d!" % (MAX_SIMULATIONS)
        print "Try requesting more stations per simulation..."
        sys.exit(1)
    # Generate source files
    generate_src_files(numsim, source_file, srcdir, prefix, new_seed)
    # Generate xml files
    generate_xml(bbp_install, numsim, srcdir,
                 xmldir, logsdir, vmodel,
                 codebase, prefix, stlbase,
                 nosite)
    # Write pbs file
    write_pbs(bbp_install, numsim, simdir, xmldir, email, prefix, newnodes)
Exemple #22
0
    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.4.1" % (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.4.1 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=%f rtime1_scor=0.5 " %
                      (self.slip_sigma) +
                      "tsfac_bzero=-0.1 tsfac_slope=-0.5 " +
                      "> %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, '-'))
Exemple #23
0
                          'title="LP 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)


        if self.r_srcfile == "":
            #calculate magnitude and write to file
            mag = fault_utils.get_magnitude(os.path.join(a_indir,
                                                         self.r_velmodel),
                                            os.path.join(a_indir,
                                                         self.r_srffile),
                                            sta_base)
            mag_file = open("%s" % os.path.join(a_indir,
                                                "magnitude_%s" %
                                                (sta_base)), 'w')
            mag_file.write("%.2f" % mag)
            mag_file.flush()
            mag_file.close()

        print("GP Jbsim Completed".center(80, '-'))

if __name__ == "__main__":
    print("Testing Module: %s" % os.path.basename((sys.argv[0])))
    INSTALL = InstallCfg()
    ME = Jbsim(sys.argv[1], sys.argv[2], sys.argv[3], sys.argv[4],
               sys.argv[5], sim_id=int(sys.argv[6]))
    ME.run()
    sys.exit(0)
Exemple #24
0
    def run(self):
        """
        Runs the GP low frequency component
        """
        print("GP Jbsim".center(80, '-'))

        install = InstallCfg.getInstance()
        config = JbsimCfg(self.vmodel_name)

        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.jbsim_%s.log" % (sim_id, sta_base))

        a_statfile = os.path.join(install.A_IN_DATA_DIR,
                                  str(sim_id),
                                  self.r_stations)
        a_srffile = os.path.join(install.A_IN_DATA_DIR,
                                 str(sim_id),
                                 self.r_srffile)

        # Set directories, and make sure they exist
        a_indir = os.path.join(install.A_IN_DATA_DIR, str(sim_id))
        a_veldir = os.path.join(install.A_TMP_DATA_DIR, str(sim_id))
        bband_utils.mkdirs([a_veldir, a_indir], print_cmd=False)

        #
        # Read and parse the station list with this call
        #
        slo = StationList(a_statfile)
        site_list = slo.getStationList()

        for sits in site_list:
            slon = float(sits.lon)
            slat = float(sits.lat)
            site = sits.scode

            print("==> Generating LF seismogram for station: %s" % (site))

            #
            # We have a verbose of silent invocation. This is a very
            # verbose program so our default writes to dev/null
            #
            progstring = ("%s latloncoords=1 slon=%f slat=%f " %
                          (os.path.join(install.A_GP_BIN_DIR,
                                        "jbsim"), slon, slat) +
                          "rupmodtype=SRF rupmodfile=%s " % a_srffile +
                          "moment=-1 outdir=%s stat=%s " % (a_veldir, site) +
                          "min_taper_range=0.0 max_taper_range=0.0 " +
                          "gftype=fk gflocs=%s gftimes=%s gf_swap_bytes=%d " %
                          (config.GF_LOCS, config.GF_TIMES,
                           config.GF_SWAP_BYTES) +
                          "gfpath=%s gfname=%s maxnt=%d mindt=%f " %
                          (config.A_GP_GF_DIR, config.GF_NAME,
                           config.MAX_GFNT, config.MIN_GFDT) +
                          "dtout=%f ntout=%d >> %s 2>&1" %
                          (config.DTOUT, config.NTOUT, self.log))
            bband_utils.runprog(progstring, print_cmd=False)

            #This would differentiate to accel, but we want velocity
            #for entries in config.COMPS:
            #  compo = entries
            #  progstring = ("%s/integ_diff diff=1 " % install.A_GP_BIN_DIR + \
            #       "filein=%s/%s.%s fileout=%s/%d.%s-lf.%s " %
            #        (a_veldir, site, compo, a_veldir, sim_id, site, compo))
            #  bband_utils.runprog(progstring)


            # Create path names and check if their sizes are within bounds
            nsfile = os.path.join(a_veldir,
                                  "%s.000" % (site))
            ewfile = os.path.join(a_veldir,
                                  "%s.090" % (site))
            udfile = os.path.join(a_veldir,
                                  "%s.ver" % (site))
            bbpfile = os.path.join(a_veldir,
                                   "%d.%s-lf.bbp" % (sim_id, site))
            bband_utils.check_path_lengths([nsfile, ewfile, udfile],
                                           bband_utils.GP_MAX_FILENAME)

            # Run wcc2bpp
            progstring = ("%s wcc2bbp=1 " %
                          (os.path.join(install.A_GP_BIN_DIR, "wcc2bbp")) +
                          'title="LP 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)


        if self.r_srcfile == "":
            #calculate magnitude and write to file
            mag = fault_utils.get_magnitude(os.path.join(a_indir,
                                                         self.r_velmodel),
                                            os.path.join(a_indir,
                                                         self.r_srffile),
                                            sta_base)
            mag_file = open("%s" % os.path.join(a_indir,
                                                "magnitude_%s" %
                                                (sta_base)), 'w')
            mag_file.write("%.2f" % mag)
            mag_file.flush()
            mag_file.close()

        print("GP Jbsim Completed".center(80, '-'))
Exemple #25
0
def do_reset():
    """
    This function resets an user account by deleting all simulation
    data from previous runs. For each of outdata, tmpdata, indata, and
    logs, it confirms if the user wants to delete the data.
    """

    install = InstallCfg()

    # Print warning message
    print "WARNING".center(80, '=')
    print ("  You are about to permanently delete Broadband simulation data")
    print ("  from all previous runs!")
    print ("  Data in indata, outdata, tmpdata, xml, and logs directories ")
    print ("  will be permanently deleted!")
    print '='*80
    print
    print (" The BBP data directory is set to: %s" % (install.A_DATA_ROOT))
    print

    while True:
        proceed = raw_input("Do you want to proceed? (yes/no) ")
        if str.lower(proceed) == "yes":
            print "Deleting simulation data directories..."
            break
        elif str.lower(proceed) == "no":
            sys.exit(0)

    #proceed = raw_input("Delete 'outdata'? (yes/no) ")
    if str.lower(proceed) == "yes":
        prog_string = "rm -r %s" % (install.A_OUT_DATA_DIR)
        bband_utils.runprog(prog_string)
        print "Deleted 'outdata'!"
    else:
        print "Skipped 'outdata'"

    #proceed = raw_input("Delete 'tmpdata'? (yes/no) ")
    if str.lower(proceed) == "yes":
        prog_string = "rm -r %s" % (install.A_TMP_DATA_DIR)
        bband_utils.runprog(prog_string)
        print "Deleted 'tmpdata'!"
    else:
        print "Skipped 'tmpdata'"

    #proceed = raw_input("Delete 'indata'? (yes/no) ")
    if str.lower(proceed) == "yes":
        prog_string = "rm -r %s" % (install.A_IN_DATA_DIR)
        bband_utils.runprog(prog_string)
        print "Deleted 'indata'!"
    else:
        print "Skipped 'indata'"

    #proceed = raw_input("Delete 'logs'? (yes/no) ")
    if str.lower(proceed) == "yes":
        prog_string = "rm -r %s" % (install.A_OUT_LOG_DIR)
        bband_utils.runprog(prog_string)
        print "Deleted 'logs'!"
    else:
        print "Skipped 'logs'"

    #proceed = raw_input("Delete 'xml'? (yes/no) ")
    if str.lower(proceed) == "yes":
        prog_string = "rm -r %s" % (install.A_XML_DIR)
        bband_utils.runprog(prog_string)
        print "Deleted 'xml'!"
    else:
        print "Skipped 'xml'"

    #
    # delete *.pyc files in comps and in test
    if str.lower(proceed) == "yes":
        prog_string = "rm %s/comps/*.pyc" % (install.A_INSTALL_ROOT)
        bband_utils.runprog(prog_string)
        print "Deleted *.pyc files in comps"
        prog_string = "rm %s/tests/*.pyc" % (install.A_INSTALL_ROOT)
        bband_utils.runprog(prog_string)
        print "Deleted *.pyc files in test"

    #
    # Make clean to remove executables
    #
    if str.lower(proceed) == "yes":
        prog_string = "cd %s/src;make clean" % (install.A_INSTALL_ROOT)
        bband_utils.runprog(prog_string)
        print "src/make clean removed executables in src directory"
Exemple #26
0
    if line.startswith("#") or line.startswith("%"):
        continue
    pieces = line.split()
    pieces = [float(piece) for piece in pieces]
    nsfile.write(" %1.9E\n" % (pieces[1]))
    ewfile.write(" %1.9E\n" % (pieces[2]))
    udfile.write(" %1.9E\n" % (pieces[3]))

# All done, close everything
nsfile.close()
ewfile.close()
udfile.close()
ifile.close()

# Get pointers
install = InstallCfg.getInstance()

# Now convert them into sac format
for comp in ["000", "090", "ver"]:
    file_in = "%s.%s" % (base_file, comp)
    file_out = "%s.sac" % (file_in)
    cmd = "echo '%s' > tmp" % (file_in)
    bband_utils.runprog(cmd)
    cmd = ("%s < tmp >> /dev/null 2>&1" %
           (os.path.join(install.A_UCSB_BIN_DIR, "BBPtoSAC")))
    bband_utils.runprog(cmd)
    os.rename("output.sac", file_out)
    os.unlink(file_in)

# Remove tmp file
os.unlink("tmp")
Exemple #27
0
    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, '-'))
Exemple #28
0
    def run(self):
        """
        This function prepares the parameters for HFSim and then calls it
        """
        print("GP HfSims".center(80, '-'))

        install = InstallCfg.getInstance()
        sim_id = self.sim_id

        # Find validation object if this is a validation run
        if self.val_name is not None:
            self.val_obj = validation_cfg.VE_EVENTS.get_event_by_name(self.val_name)

        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.hfsims_%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_srffile = os.path.join(a_indir, self.r_srffile)
        # Make sure we work when starting from an SRF file
        if self.r_srcfile:
            a_srcfile = os.path.join(a_indir, self.r_srcfile)
        else:
            a_srcfile = ""
        # Set up basic parameters, read SRC file if provided
        config = HfsimsCfg(a_srcfile=a_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))

        # Check for velocity model-specific parameters
        vmodel_params = vel_obj.get_codebase_params('gp')
        # Look for KAPPA
        if 'KAPPA' in vmodel_params:
            self.kappa = float(vmodel_params['KAPPA'])
        else:
            self.kappa = config.KAPPA
        # Look for QFEXP
        if 'QFEXP' in vmodel_params:
            self.qfexp = float(vmodel_params['QFEXP'])
        else:
            self.qfexp = config.DEFAULT_QFEXP
        # Look for SDROP
        if 'SDROP' in vmodel_params:
            self.sdrop = int(vmodel_params['SDROP'])
        else:
            self.sdrop = config.DEFAULT_SDROP
        # Look for C0 and C1
        if 'C0' in vmodel_params:
            self.c0 = int(vmodel_params['C0'])
        else:
            self.c0 = config.DEFAULT_C0
        if 'C1' in vmodel_params:
            self.c1 = int(vmodel_params['C1'])
        else:
            self.c1 = config.DEFAULT_C1
        # Look for DEFAULT_FCFAC
        if 'DEFAULT_FCFAC' in vmodel_params:
            self.default_fcfac = float(vmodel_params['DEFAULT_FCFAC'])
        else:
            self.default_fcfac = config.DEFAULT_FCFAC
        # Look for rayset
        if 'RAYSET' in vmodel_params:
            self.rayset = ast.literal_eval(vmodel_params['RAYSET'])
        else:
            self.rayset = config.RAYSET
        # Look for a high frequency DT
        if 'HF_DT' in vmodel_params:
            self.dt = float(vmodel_params['HF_DT'])
        else:
            self.dt = config.DT
        # Look for MEAN_RVFAC
        if 'MEAN_RVFAC' in vmodel_params:
            self.mean_rvfac = float(vmodel_params['MEAN_RVFAC'])
        else:
            self.mean_rvfac = config.MEAN_RVFAC
        # Look for RANGE_RVFAC
        if 'RANGE_RVFAC' in vmodel_params:
            self.range_rvfac = float(vmodel_params['RANGE_RVFAC'])
        else:
            self.range_rvfac = config.RANGE_RVFAC
        # Look for SHAL_RVFAC
        if 'SHAL_RVFAC' in vmodel_params:
            self.shal_rvfac = float(vmodel_params['SHAL_RVFAC'])
        else:
            self.shal_rvfac = config.SHAL_RVFAC
        # Look for VSMOHO
        if 'VSMOHO' in vmodel_params:
            self.vsmoho = float(vmodel_params['VSMOHO'])
        else:
            self.vsmoho = config.DEFAULT_VSMOHO
        # Look for DEEP_RVFAC
        if 'DEEP_RVFAC' in vmodel_params:
            self.deep_rvfac = float(vmodel_params['DEEP_RVFAC'])
        else:
            self.deep_rvfac = config.DEEP_RVFAC
        # Look for PATH_DUR_MODEL
        if 'PATH_DUR_MODEL' in vmodel_params:
            self.path_dur_model = int(vmodel_params['PATH_DUR_MODEL'])
        else:
            self.path_dur_model = config.PATH_DUR_MODEL
        # Look for RVSIG
        if 'RVSIG' in vmodel_params:
            self.rvsig = float(vmodel_params['RVSIG'])
        else:
            self.rvsig = config.RVSIG
        # Look for DX
        if 'DEFAULT_DX' in vmodel_params:
            self.default_dx = float(vmodel_params['DEFAULT_DX'])
        else:
            self.default_dx = config.DEFAULT_DX
        # Look for DY
        if 'DEFAULT_DY' in vmodel_params:
            self.default_dy = float(vmodel_params['DEFAULT_DY'])
        else:
            self.default_dy = config.DEFAULT_DY

        # Calculate rvfac
        if "common_seed" in config.CFGDICT:
            rvfac = calculate_rvfac(self.mean_rvfac, self.range_rvfac,
                                    config.CFGDICT["common_seed"])
        else:
            rvfac = calculate_rvfac(self.mean_rvfac, self.range_rvfac,
                                    config.CFGDICT["seed"])

        # Look for tlen
        if "TLEN" in vmodel_params:
            self.tlen = float(vmodel_params['TLEN'])
        else:
            self.tlen = config.TLEN

        # Start with some default values
        moment = -1
        extra_fcfac = config.DEFAULT_EXTRA_FCFAC

        if self.val_obj is not None:
            extra_fcfac = float(self.val_obj.get_input("GP", "EXTRA_FCFAC"))
            try:
                tlen = float(self.val_obj.get_input("GP", "TLEN"))
                self.tlen = tlen
            except (ValueError, KeyError, TypeError):
                # No problem, just use the default TLEN for this simulation
                pass

        fcfac = round((1 + self.default_fcfac) * (1 + extra_fcfac) - 1, 4)

        a_slipfile = os.path.join(a_tmpdir, "%s.%s.%fx%f" % (self.r_srffile,
                                                             sta_base,
                                                             self.default_dx,
                                                             self.default_dy))

        progstring = ("%s " %
                      (os.path.join(install.A_GP_BIN_DIR, "srf2stoch")) +
                      "infile=%s outfile=%s " %
                      (a_srffile, a_slipfile) +
                      "target_dx=%f target_dy=%f " %
                      (self.default_dx, self.default_dy) +
                      ">> %s 2>&1" % (self.log))
        bband_utils.runprog(progstring)

        a_outp = os.path.join(a_tmpdir, "tmp_hfsim_out")
        a_velmod = os.path.join(install.A_IN_DATA_DIR, str(sim_id),
                                self.r_velmodel)
        a_statfile = os.path.join(install.A_IN_DATA_DIR, str(sim_id),
                                  self.r_stations)

        # Create local velocity model
        vel_in_fp = open(a_velmod, 'r')
        a_velmod = "%s_%s.local" % (a_velmod, sta_base)
        vel_out_fp = open(a_velmod, 'w')
        vel_in_data = vel_in_fp.readlines()
        vel_in_fp.close()
        i = 0
        for line in vel_in_data:
            i += 1
            if line.startswith('#') or line.startswith('%'):
                continue
            pieces = line.split()
            if len(pieces) >= 4:
                th = float(pieces[0])
                vp = float(pieces[1])
                vs = float(pieces[2])
                dn = float(pieces[3])
                qs = self.c0 + self.c1 * vs
                if i == len(vel_in_data):
                    th = 0.0
                vel_out_fp.write("%8.4f %8.4f %8.4f %8.4f %8.2f %8.2f\n" %
                                 (th, vp, vs, dn, qs, qs))
            else:
                vel_out_fp.write(line)
        vel_out_fp.flush()
        vel_out_fp.close()
        #
        # Scan the station list with this object construction
        # This scanner removes all the comment lines and the
        # list that is returned has one station per line in it.
        #
        slo = StationList(a_statfile)
        site_list = slo.getStationList()
        nstat = len(site_list)

        # Create rayset param list
        rayset_param = ""
        for item in self.rayset:
            rayset_param = rayset_param + "%d " % (item)
        rayset_param = rayset_param.strip()
        #
        # Run initial hfsim conf
        #
        progstring = ("%s >> %s 2>&1 << END\n" %
                      (os.path.join(install.A_GP_BIN_DIR, config.HFSIM),
                       self.log) +
                      "%d\n" % self.sdrop +
                      "%s\n" % a_statfile +
                      "%s\n" % a_outp +
                      "%s\n" % rayset_param +
                      "%d\n" % config.SITEAMP +
                      "4 0 0.02 19.9\n" +
                      "%d\n" % config.CFGDICT["seed"] +
                      "%d\n" % nstat +
                      "%f %f %f %f %f\n" %
                      (self.tlen, self.dt, config.FMAX,
                       self.kappa, self.qfexp) +
                      "%f %f %f %f %f\n" %
                      (rvfac, self.shal_rvfac, self.deep_rvfac,
                       fcfac, config.C_ALPHA) +
                      "%s %f\n" % (moment, config.RUPV) +
                      "%s\n" % a_slipfile +
                      "%s\n" % a_velmod +
                      "%f\n" % self.vsmoho +
                      "-99 0.0 0.0 0.0 0.0 1\n" +
                      "-1\n" +
                      "%f 0.0 %f\n" % (config.FA_SIG1, self.rvsig) +
                      "%d\n" % (self.path_dur_model) +
                      "END")
        bband_utils.runprog(progstring)

        #
        # Start the per station processing
        #
        for site in site_list:
            # Need to integrate each component, since hfsims outputs cm/s/s
            for comp in ['000', '090', 'ver']:
                cmd = ("%s integ=1 " %
                       (os.path.join(install.A_GP_BIN_DIR, "integ_diff")) +
                       "filein=%s_%s.%s fileout=%s/%d.%s-hf.%s >> %s 2>&1" %
                       (a_outp, site.scode, comp, a_tmpdir, sim_id,
                        site.scode, comp, self.log))
                bband_utils.runprog(cmd, print_cmd=False)

            progstring1 = ("%s wcc2bbp=1 " %
                           (os.path.join(install.A_GP_BIN_DIR, "wcc2bbp")) +
                           'title="HF Sim NGAH, stat=%s" ' % (site.scode) +
                           "nsfile=%s/%d.%s-hf.000 " %
                           (a_tmpdir, sim_id, site.scode) +
                           "ewfile=%s/%d.%s-hf.090 " %
                           (a_tmpdir, sim_id, site.scode) +
                           "udfile=%s/%d.%s-hf.ver " %
                           (a_tmpdir, sim_id, site.scode) +
                           'units="%s" > %s/%d.%s-hf.bbp 2>> %s\n' %
                           (str(config.UNITS), a_tmpdir, sim_id,
                            site.scode, self.log))
            bband_utils.runprog(progstring1, print_cmd=False)

            progstring1 = ("%s wcc2bbp=1 " %
                           (os.path.join(install.A_GP_BIN_DIR, "wcc2bbp")) +
                           'title="HF Sim NGAH, stat=%s" ' % site.scode +
                           "nsfile=%s_%s.000 " %
                           (a_outp, site.scode) +
                           "ewfile=%s_%s.090 " %
                           (a_outp, site.scode) +
                           "udfile=%s_%s.ver " %
                           (a_outp, site.scode) +
                           '> %s_%s.bbp 2>> %s\n' %
                           (a_outp, site.scode, self.log))
            bband_utils.runprog(progstring1, print_cmd=False)

        print("GP HfSims Completed".center(80, '-'))
def main():
    """
    Parse command line options and create the needed files/directories
    """
    prog_base = os.path.basename(sys.argv[0])
    usage = "usage: %s [options]" % (prog_base)
    parser = argparse.ArgumentParser(description="Merges a number of "
                                     "validation segments into a single "
                                     "multi-segment simulation")
    parser.add_argument("-d", "--dir", dest="merged_simdir",
                        help="Merged simulation directory")
    parser.add_argument("-e", "--event", dest="event",
                        help="validation event")
    parser.add_argument('input_segments', nargs='*',
                        help="top-level directories for all segments")

    args = parser.parse_args()

    # Input segments
    if len(args.input_segments) < 2:
        print("Please provide at least two simulations to merge!")
        sys.exit(-1)
    input_segments = args.input_segments

    # Event name
    if args.event is None:
        print("[ERROR]: Please provide an event name!")
        sys.exit(-1)
    event = args.event

    # Check for the simulation directory
    merged_simdir = args.merged_simdir
    if merged_simdir is None:
        print("Please provide a merged simulation directory!")
        sys.exit(1)
    merged_simdir = os.path.abspath(merged_simdir)
    if os.path.exists(merged_simdir):
        print("Merged simulation directory exists: %s" %
              (merged_simdir))
        opt = raw_input("Do you want to delete its contents (y/n)? ")
        if opt.lower() != "y":
            print("Please provide another simulation directory!")
            sys.exit(1)
        opt = raw_input("ARE YOU SURE (y/n)? ")
        if opt.lower() != "y":
            print("Please provide another simulation directory!")
            sys.exit(1)
        # Delete existing directory (we already asked the user twice!!!)
        shutil.rmtree(merged_simdir)

    # Create merged simulation directories
    os.makedirs(merged_simdir)
    merged_indir = os.path.join(merged_simdir, "Sims", "indata")
    merged_outdir = os.path.join(merged_simdir, "Sims", "outdata")
    merged_tmpdir = os.path.join(merged_simdir, "Sims", "tmpdata")
    merged_logdir = os.path.join(merged_simdir, "Sims", "logs")
    xmldir = os.path.join(merged_simdir, "Xml")
    srcdir = os.path.join(merged_simdir, "Src")
    for mdir in [merged_indir, merged_outdir, merged_tmpdir,
                 merged_logdir, xmldir, srcdir]:
        os.makedirs(mdir)

    # Setup BBP installation
    os.environ["BBP_DATA_DIR"] = os.path.join(merged_simdir, "Sims")
    bbp_install = InstallCfg.getInstance()

    # Make sure event exists
    event_names = validation_cfg.VE_EVENTS.get_all_names()
    event_names_lc = [e.lower() for e in event_names]
    if not event.lower() in event_names_lc:
        print("[ERROR]: Unknown event: %s" % (event))
        print("[ERROR]: Available events: ", event_names)
        shutil.rmtree(merged_simdir)
        sys.exit(1)
    val_event = event
    val_obj = validation_cfg.VE_EVENTS.get_event_by_print_name(event)

    # Make sure directories exist
    input_sims = []
    for simdir in input_segments:
        simdir = os.path.abspath(simdir)
        if not os.path.exists(simdir):
            print("Simulation directory %s does not exist!" % (simdir))
            sys.exit(1)
        input_sims.append(simdir)

    # Figure out the realizations
    input_sim = os.path.join(input_sims[0], "Sims", "outdata")
    realizations = glob.glob("%s%s*" % (input_sim, os.path.sep))
    num_realizations = len(realizations)
    realizations = [os.path.basename(item) for item in realizations]

    # Merge each realization
    print(" BBP Multi-segment merging tool ".center(80, "-"))
    print("=> Merging %d realizations..." % (num_realizations))
    for realization in realizations:
        print("==> Realization %s" % (realization))

        # Create directories
        merged_realization_indir = os.path.join(merged_indir,
                                                realization)
        merged_realization_tmpdir = os.path.join(merged_tmpdir,
                                                 realization)
        merged_realization_outdir = os.path.join(merged_outdir,
                                                 realization)
        merged_realization_logdir = os.path.join(merged_logdir,
                                                 realization)
        for mdir in [merged_realization_indir,
                     merged_realization_outdir,
                     merged_realization_tmpdir,
                     merged_realization_logdir]:
            os.makedirs(mdir)

        # Copy indata files
        (src_files,
         srf_files,
         station_list) = copy_indata_files(input_sims,
                                           merged_realization_indir,
                                           merged_realization_tmpdir,
                                           realization)

        # Merge seismograms
        merge_seismograms(input_sims, station_list,
                          merged_realization_outdir,
                          realization)
        # Plot combined SRF plot
        plot_srf_file(merged_realization_indir,
                      merged_realization_tmpdir,
                      merged_realization_outdir,
                      bbp_install,
                      srf_files,
                      val_event)
        # Post-processing steps
        post_process(station_list, src_files,
                     merged_realization_outdir,
                     realization,
                     val_obj)
        print("==> Realization %s Completed" % (realization))
        print("%s" % ("*" * 80))
Exemple #30
0
def plot_combined_map_gof(indir, tmpdir, outdir, codebase):
    """
    This function reads data from the residuals files from multiple
    realizations and plots a map gof plot with a number of periods.
    """
    # Capture number of realizations and event label
    num_realizations = len(os.listdir(tmpdir))
    basedir = os.path.join(tmpdir, os.listdir(tmpdir)[0])
    resid_file = glob.glob("%s%s*-resid-map*rotd50.txt" % (basedir, os.sep))[0]
    event_label = os.path.basename(resid_file).split("-")[0]

    # Get one trace file
    basedir = os.path.join(indir, os.listdir(indir)[0])
    trace_file = glob.glob("%s%s*.trace" % (basedir, os.sep))[0]

    # Now get the SRC (or SRF file) in order to get the hypocenter
    # location. Note that this function will look for the hypocenter
    # location from the first realization. If the simulation was
    # created using randomized hypocenter locations, the plot will
    # only display the location of the hypocenter from the first
    # realization.
    src_file = glob.glob("%s%s*.src" % (basedir, os.sep))
    if not len(src_file):
        srf_file = glob.glob("%s%s*.srf" % (basedir, os.sep))
        if not len(srf_file):
            raise bband_utils.ProcessingError("Cannot find SRC/SRF file!")
        source_file = srf_file[0]
    else:
        source_file = src_file[0]

    # Get hypo_lon, hypo_lat from src/srf file
    hypo_lon, hypo_lat = fault_utils.calculate_epicenter(source_file)

    # Collect all the data from the residuals file
    all_sta_x_data = []
    all_sta_y_data = []
    all_sta_resid_data = []
    for period in DIST_PERIODS:
        (sta_x_data, sta_y_data,
         sta_resid_data) = combine_realization_data(tmpdir, period)
        all_sta_x_data.append(sta_x_data)
        all_sta_y_data.append(sta_y_data)
        all_sta_resid_data.append(sta_resid_data)

    # Get plot boundaries
    (north, south, east,
     west) = set_boundaries_from_lon_lat(all_sta_x_data[0], all_sta_y_data[0])

    # Get directory names
    install = InstallCfg.getInstance()
    # Prepare to plot map GOF
    plotregion = [west, east, south, north]
    topo = os.path.join(install.A_PLOT_DATA_DIR, 'calTopo18.bf')
    coastal = os.path.join(install.A_PLOT_DATA_DIR, 'gshhs_h.txt')
    border = os.path.join(install.A_PLOT_DATA_DIR, 'wdb_borders_h.txt')

    # Now create the map GOF
    outfile = os.path.join(
        outdir, "gof-map-combined-%s-%s-rotd50.png" % (codebase, event_label))

    create_combined_map_gof(all_sta_x_data,
                            all_sta_y_data,
                            all_sta_resid_data,
                            plotregion,
                            topo,
                            coastal,
                            border,
                            trace_file,
                            event_label,
                            num_realizations,
                            codebase,
                            outfile,
                            hypo_lat=hypo_lat,
                            hypo_lon=hypo_lon)
Exemple #31
0
    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 main():
    """
    Parse command line options and create the needed files/directories
    """
    # Detect BBP installation
    bbp_install = InstallCfg.getInstance()

    prog_base = os.path.basename(sys.argv[0])
    usage = "usage: %s [options]" % (prog_base)
    parser = optparse.OptionParser(usage)
    parser.add_option("-c", "--codebase", type="string", action="store",
                      dest="codebase",
                      help="Codebase for the simulation: %s" %
                      (CODEBASES))
    parser.add_option("-e", "--event", type="string", action="store",
                      dest="event",
                      help="Validation event (should be configured in BBP)")
    parser.add_option("-d", "--dir", type="string", action="store",
                      dest="simdir",
                      help="Simulation directory")
    parser.add_option("--skip-rupgen", action="store_true", dest="skiprupgen",
                      help="Skip the rupture generator, run only 1 simulation")
    parser.add_option("--hypo-rand", action="store_true", dest="hyporand",
                      help="Enables hypocenter randomization")
    parser.add_option("--no-hypo-rand", action="store_false", dest="hyporand",
                      help="Disables hypocenter randomization")
    parser.add_option("-n", "--num-simulations", type="int", action="store",
                      dest="numsim", help="Number of simulations to run")
    parser.add_option("--email", type="string", action="store",
                      dest="email", help="Email for job notifications")
    (options, args) = parser.parse_args()

    # Validate codebase to use
    codebase = options.codebase
    if codebase is None:
        print "Please specify a codebase!"
        sys.exit(1)
    codebase = codebase.lower()
    if codebase not in CODEBASES:
        print "Codebase needs to be one of: %s" % (CODEBASES)

    # Check for event
    event = options.event
    if event is None:
        print "Please provide a validation event!"
        sys.exit(1)
    event_names = validation_cfg.VE_EVENTS.get_all_names()
    events = [v_event.lower() for v_event in event_names]
    if event.lower() not in events:
        print ("Event %s does not appear to be properly configured on BBP" %
               (event))
        print ("Available options are: %s" % (event_names))
        print "Please provide another event or check your BBP installation."
        sys.exit(1)
    val_obj = validation_cfg.VE_EVENTS.get_event_by_print_name(event)

    # Check if we want to run the rupture generator
    skip_rupgen = options.skiprupgen

    # Check for hypocenter randomization
    if options.hyporand is None:
        print "Please specify --hypo-rand or --no-hypo-rand!"
        sys.exit(1)

    if options.hyporand:
        hypo_rand = True
    else:
        hypo_rand = False

    if not skip_rupgen:
        # Get source file
        try:
            source_file = val_obj.get_input(codebase, "source").strip()
        except KeyError:
            print ("Unable to get source file for event %s, codebase %s!" %
                   (event, codebase))
            sys.exit(1)
        if not source_file:
            print ("Source file for event %s, codebase %s not specified!" %
                   (event, codebase))
            sys.exit(1)
    else:
        # No need to get the source file, we start from the srf
        source_file = None
        try:
            srf_file = val_obj.get_input(codebase, "srf").strip()
        except KeyError:
            print ("Event %s does not have a srf file for codebase %s!" %
                   (event, codebase))
            sys.exit(1)
        if not srf_file:
            print ("Event %s does not have a srf file for codebase %s!" %
                   (event, codebase))
            sys.exit(1)
        # Force number of simulations to 1
        options.numsim = 1

    # Check for the simulation directory
    simdir = options.simdir
    if simdir is None:
        print "Please provide a simulation directory!"
        sys.exit(1)
    simdir = os.path.abspath(simdir)
    if os.path.exists(simdir):
        print "Simulation directory exists: %s" % (simdir)
        opt = raw_input("Do you want to delete its contents (y/n)? ")
        if opt.lower() != "y":
            print "Please provide another simulation directory!"
            sys.exit(1)
        opt = raw_input("ARE YOU SURE (y/n)? ")
        if opt.lower() != "y":
            print "Please provide another simulation directory!"
            sys.exit(1)
        # Delete existing directory (we already asked the user twice!!!)
        shutil.rmtree(simdir)

    # Pick up number of simulations to run
    numsim = options.numsim
    if numsim < 1 or numsim > MAX_SIMULATIONS:
        print ("Number of simulations should be between 1 and %d" %
               (MAX_SIMULATIONS))
        sys.exit(1)

    # Check for e-mail address
    email = options.email
    if email is None:
        print "Please provide an e-mail address for job notifications"
        sys.exit(1)

    # Make sure user has configured the setup_bbp_epicenter_env.sh script
    setup_bbp_env = os.path.join(bbp_install.A_INSTALL_ROOT,
                                 "utils/batch/setup_bbp_epicenter_env.sh")
    if not os.path.exists(setup_bbp_env):
        print ("Cannot find setup_bbp_epicenter_env.sh script!")
        print ("Expected at: %s" % (setup_bbp_env))
        sys.exit(1)
    # Create simulation directories
    prefix = "%s-%s" % (event.lower(), codebase.lower())
    # Make sure we remove spaces from prefix (e.g. for the "Loma Prieta" event)
    prefix = prefix.replace(" ", '')
    os.makedirs(simdir)
    indir = os.path.join(simdir, "Sims", "indata")
    outdir = os.path.join(simdir, "Sims", "outdata")
    tmpdir = os.path.join(simdir, "Sims", "tmpdata")
    logsdir = os.path.join(simdir, "Sims", "logs")
    xmldir = os.path.join(simdir, "Xml")
    srcdir = os.path.join(simdir, "Src")
    for mdir in [indir, outdir, tmpdir, logsdir, xmldir, srcdir]:
        os.makedirs(mdir)
    # Generate source files if needed
    if source_file is not None:
        generate_src_files(numsim, source_file, srcdir, prefix, hypo_rand)
    # Generate xml files
    generate_xml(bbp_install, numsim, srcdir, xmldir,
                 logsdir, event, codebase, prefix,
                 skip_rupgen)
    # Write pbs file
    write_pbs(bbp_install, numsim, simdir, xmldir, email, prefix)
Exemple #33
0
    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 .....")
Exemple #34
0
    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, '-'))
Exemple #35
0
                # 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()

#-------------------------------------------------------------------------------
# Main program starts here
#-------------------------------------------------------------------------------
CFG = InstallCfg()

if len(sys.argv) == 2:
    if sys.argv[1] == '-g':
        # Generate md5sums instead
        for CHECK_DIR in [CFG.A_GF_DIR, CFG.A_VAL_DIR]:
            SUB_DIRS = bband_utils.list_subdirs(CHECK_DIR)
            for DIRECTORY in SUB_DIRS:
                BASE_DIR = os.path.join(CHECK_DIR, DIRECTORY)
                MD5_DIR = os.path.join(CHECK_DIR, DIRECTORY, "checksums")
                # First, remove checksums directory if it already exists
                if os.path.exists(MD5_DIR):
                    os.system("rm -rf %s" % (MD5_DIR))                    
                generate_md5s(BASE_DIR, MD5_DIR, True)
        # All done!
        sys.exit(0)
Exemple #36
0
def main():
    """
    Parse command line options and create the needed files/directories
    """
    # Detect BBP installation
    bbp_install = InstallCfg.getInstance()

    prog_base = os.path.basename(sys.argv[0])
    usage = "usage: %s [options]" % (prog_base)
    parser = optparse.OptionParser(usage)
    parser.add_option("-c",
                      "--codebase",
                      type="string",
                      action="store",
                      dest="codebase",
                      help="Codebase for the simulation: %s" % (CODEBASES))
    parser.add_option("-v",
                      "--velocity-model",
                      type="string",
                      action="store",
                      dest="vmodel",
                      help="Velocity model (region) for this simulation")
    parser.add_option("--src",
                      "--source",
                      type="string",
                      action="store",
                      dest="source",
                      help="Source description file for the simulation")
    parser.add_option("--stl",
                      "--station-list",
                      type="string",
                      action="store",
                      dest="station_list",
                      help="Station list file for the simulation")
    parser.add_option("--srf",
                      "--srf-prefix",
                      type="string",
                      action="store",
                      dest="srf_prefix",
                      help="Prefix of SRF files to use, "
                      "only for GP, SDSU and UCSB methods. "
                      "Simulations begin after the rupture generator.")
    parser.add_option("-d",
                      "--dir",
                      type="string",
                      action="store",
                      dest="simdir",
                      help="Simulation directory")
    parser.add_option("--hypo-rand",
                      action="store_true",
                      dest="hyporand",
                      help="Enables hypocenter randomization")
    parser.add_option("--no-hypo-rand",
                      action="store_false",
                      dest="hyporand",
                      help="Disables hypocenter randomization")
    parser.add_option("-n",
                      "--num-simulations",
                      type="int",
                      action="store",
                      dest="numsim",
                      help="Number of simulations to run")
    parser.add_option("--email",
                      type="string",
                      action="store",
                      dest="email",
                      help="Email for job notifications")
    parser.add_option("-w",
                      "--walltime",
                      type="int",
                      action="store",
                      dest="walltime",
                      help="Number of hours for walltime")
    parser.add_option("--new-nodes",
                      action="store_true",
                      dest="newnodes",
                      help="Schedule the job in the new HPCC nodes")
    parser.add_option("--save-tmpdata",
                      action="store_true",
                      dest="savetemp",
                      help="Save the contents of the tmpdata directory")
    parser.add_option("--hdd-min",
                      type="float",
                      action="store",
                      dest="hdd_min",
                      help="Min value for hypo down dip in randomization")
    parser.add_option("--hdd-max",
                      type="float",
                      action="store",
                      dest="hdd_max",
                      help="Max value for hypo down dip in randomization")
    parser.add_option("--has-min",
                      type="float",
                      action="store",
                      dest="has_min",
                      help="Min value for hypo along strike in randomization")
    parser.add_option("--has-max",
                      type="float",
                      action="store",
                      dest="has_max",
                      help="Max value for hypo along strike in randomization")
    parser.add_option("--only-rup",
                      action="store_true",
                      dest="only_rup",
                      help="Only runs the rupture generator")
    parser.add_option("--var",
                      "--variation",
                      type="int",
                      action="store",
                      dest="variation",
                      help="seed variation (default 1)")
    parser.add_option("--multiseg",
                      action="store_true",
                      dest="multiseg",
                      help="Indicates simulation part of multiseg run")
    parser.add_option("--first-seg-dir",
                      type="string",
                      action="store",
                      dest="first_seg_dir",
                      help="required for multi-segment segments 2..n")
    parser.add_option("-s",
                      "--site",
                      action="store_true",
                      dest="site_response",
                      help="Use site response module")

    (options, _) = parser.parse_args()

    # Check if using new HPCC nodes
    if options.newnodes:
        newnodes = True
    else:
        newnodes = False

    # Check if multi-segment simulation
    if options.multiseg:
        multiseg = True
    else:
        multiseg = False

    # Check for first segment directory
    if options.first_seg_dir is not None:
        first_seg_dir = os.path.abspath(options.first_seg_dir)
        if not os.path.exists(first_seg_dir):
            print("First segment directory for exists: %s" % (first_seg_dir))
            sys.exit(1)
    else:
        first_seg_dir = None

    # Check if user specified custom walltime
    if options.walltime:
        if options.walltime < 1:
            print("Walltime must be at least 1 hour!")
            sys.exit(1)
        walltime = options.walltime
    else:
        if newnodes:
            walltime = 24
        else:
            walltime = 300

    # Check for variation sequence
    if options.variation:
        variation = options.variation
    else:
        variation = 1

    # Check if user wants to save the contents of tmpdata
    if options.savetemp:
        savetemp = True
    else:
        savetemp = False

    # Check if user wants to only run the rupture generator
    if options.only_rup:
        only_rup = True
    else:
        only_rup = False

    # Validate codebase to use
    codebase = options.codebase
    if codebase is None:
        print "Please specify a codebase!"
        sys.exit(1)
    codebase = codebase.lower()
    if codebase not in CODEBASES:
        print "Codebase needs to be one of: %s" % (CODEBASES)

    # Check for velocity model
    vmodel_names = velocity_models.get_all_names()
    vmodel = options.vmodel
    if vmodel is None:
        print "Please provide a velocity model (region) for this simulation!"
        print "Available options are: %s" % (vmodel_names)
        sys.exit(1)
    vmodels = [v_model.lower() for v_model in vmodel_names]
    if vmodel.lower() not in vmodels:
        print("Velocity model %s does not appear to be available on BBP" %
              (vmodel))
        print("Available options are: %s" % (vmodel_names))
        print "Please provide another velocity model or check your BBP installation."
        sys.exit(1)
    # Now get the name with the correct case
    vmodel = vmodel_names[vmodels.index(vmodel.lower())]

    # Check if users wants to run site response module
    if options.site_response:
        site_response = True
        if codebase not in CODEBASES_SITE:
            print "Cannot use site response with method: %s" % (codebase)
            sys.exit(1)
    else:
        site_response = False

    # Check for hypocenter randomization
    if options.hyporand is None:
        print "Please specify --hypo-rand or --no-hypo-rand!"
        sys.exit(1)

    if options.hyporand:
        hypo_rand = True
    else:
        hypo_rand = False

    # Define area where hypocenter will be randomized
    hypo_area = {}
    hypo_area["hdd_min"] = options.hdd_min
    hypo_area["hdd_max"] = options.hdd_max
    hypo_area["has_min"] = options.has_min
    hypo_area["has_max"] = options.has_max

    # Get the source file (SRC or SRFs)
    source_file = options.source
    srf_prefix = options.srf_prefix

    if source_file is None and srf_prefix is None:
        print(
            "Please provide either source description (src file) "
            "or a srf prefix!")
        sys.exit(1)
    # If user specified both a source file and a srf prefix, we abort!
    if source_file is not None and srf_prefix is not None:
        print "Cannot specify both srf_prefic and source_file!"
        sys.exit(1)
    # If user specified a source file
    if source_file is not None:
        # Make it a full path
        source_file = os.path.realpath(source_file)
        # Make sure source file is in the rcf-104 / scec-00 filesystem
        if not "rcf-104" in source_file and not "scec-00" in source_file:
            print "Source file should be in the rcf-104 / scec-00 filesystems!"
            sys.exit(1)
            # Make sure source file exists and is readable
            if (not os.path.isfile(source_file)
                    or not os.access(source_file, os.R_OK)):
                print "Source file does not seem to be accessible!"
                sys.exit(1)
        # Create a prefix
        prefix = ("%s-%s" % (os.path.splitext(
            os.path.basename(source_file))[0], codebase.lower()))
    # If user specified a SRF prefix
    if srf_prefix is not None:
        # Make it a full path
        srf_prefix = os.path.realpath(srf_prefix)
        # Make sure source file is in the rcf-104 or scec-00 filesystems
        if not "rcf-104" in srf_prefix and not "scec-00" in srf_prefix:
            print "SRF files should be in the rcf-104 / scec-00 filesystems!"
            sys.exit(1)
        # Create a prefix
        prefix = os.path.splitext(os.path.basename(srf_prefix))[0]
        #prefix = prefix.rsplit("-", 1)[0]
    # Make sure we remove spaces from prefix
    prefix = prefix.replace(" ", '')

    # Get the station list
    station_list = options.station_list
    if station_list is None:
        print "Please provide a station list (stl file)!"
        sys.exit(1)
    # Make it a full path
    station_list = os.path.realpath(station_list)
    # Make sure station list is in the rcf-104 or scec-00 filesystems
    if not "rcf-104" in station_list and not "scec-00" in station_list:
        print "Station list should be in the rcf-104 / scec-00 filesystems!"
        sys.exit(1)
    # Make sure station list exists and is readable
    if (not os.path.isfile(station_list)
            or not os.access(station_list, os.R_OK)):
        print "Station list foes not seem to be accessible!"
        sys.exit(1)

    # Check for the simulation directory
    simdir = options.simdir
    if simdir is None:
        print "Please provide a simulation directory!"
        sys.exit(1)
    simdir = os.path.abspath(simdir)
    if os.path.exists(simdir):
        print "Simulation directory exists: %s" % (simdir)
        opt = raw_input("Do you want to delete its contents (y/n)? ")
        if opt.lower() != "y":
            print "Please provide another simulation directory!"
            sys.exit(1)
        opt = raw_input("ARE YOU SURE (y/n)? ")
        if opt.lower() != "y":
            print "Please provide another simulation directory!"
            sys.exit(1)
        # Delete existing directory (we already asked the user twice!!!)
        shutil.rmtree(simdir)

    # Pick up number of simulations to run
    numsim = options.numsim
    if numsim < 1 or numsim > MAX_SIMULATIONS:
        print("Number of simulations should be between 1 and %d" %
              (MAX_SIMULATIONS))
        sys.exit(1)

    # Check for e-mail address
    email = options.email
    if email is None:
        print "Please provide an e-mail address for job notifications"
        sys.exit(1)

    # Make sure user has configured the setup_bbp_env.sh script
    setup_bbp_env = os.path.join(bbp_install.A_INSTALL_ROOT,
                                 "utils/batch/setup_bbp_env.sh")
    if not os.path.exists(setup_bbp_env):
        print("Cannot find setup_bbp_env.sh script!")
        print("Expected at: %s" % (setup_bbp_env))
        sys.exit(1)
    # Create simulation directories
    os.makedirs(simdir)
    indir = os.path.join(simdir, "Sims", "indata")
    outdir = os.path.join(simdir, "Sims", "outdata")
    tmpdir = os.path.join(simdir, "Sims", "tmpdata")
    logsdir = os.path.join(simdir, "Sims", "logs")
    xmldir = os.path.join(simdir, "Xml")
    srcdir = os.path.join(simdir, "Src")
    for mdir in [indir, outdir, tmpdir, logsdir, xmldir, srcdir]:
        os.makedirs(mdir)
    if srf_prefix is None:
        # Generate source files
        generate_src_files(numsim, source_file, srcdir, prefix, hypo_rand,
                           hypo_area, variation, multiseg, first_seg_dir)
    # Generate xml files
    generate_xml(bbp_install, numsim, srcdir, xmldir, logsdir, vmodel,
                 codebase, prefix, station_list, only_rup, srf_prefix,
                 site_response)
    # Write pbs file
    write_pbs(bbp_install, numsim, simdir, xmldir, email, prefix, newnodes,
              walltime, savetemp)

    # Write .info file
    info_file = open(os.path.join(simdir, "%s.info" % (prefix)), 'w')
    info_file.write("# %s\n" % (" ".join(sys.argv)))
    info_file.close()
Exemple #37
0
    def run(self):
        """
        This function prepares the parameter file for ExSim, invokes
        it, and formats its output to be compatible with the Broadband
        Platform
        """
        print("ExSIM".center(80, '-'))

        self.install = InstallCfg.getInstance()
        self.config = ExSimCfg(
            os.path.join(self.install.A_IN_DATA_DIR, str(self.sim_id),
                         self.r_srcfile))
        install = self.install
        config = self.config
        sim_id = self.sim_id

        self.kappa = config.KAPPA
        self.stress = config.STRESS
        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.exsim_%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),
                                    "exsim_%s" % (sta_base))
        a_outdir = os.path.join(install.A_OUT_DATA_DIR, str(sim_id))
        a_param_outdir = os.path.join(a_outdir, "param_files")
        #
        # Make sure the output and two tmp directories exist
        #
        bband_utils.mkdirs([a_tmpdir, a_tmpdir_mod, a_outdir, a_param_outdir],
                           print_cmd=False)

        a_stations = os.path.join(a_indir, self.r_stations)
        self.stat_list = StationList(a_stations)
        self.num_stations = len(self.stat_list.site_list)

        # Need to create ExSim's parameter file
        self.create_exsim_param_file()

        # Keep a copy of ExSim's parameter file in outdata
        a_param_file = os.path.join(a_tmpdir_mod, self.config.PARAM_FILE)
        shutil.copy2(a_param_file,
                     os.path.join(a_param_outdir, self.config.PARAM_FILE))

        # 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_mod)

        exsim_bin = os.path.join(install.A_UWO_BIN_DIR, "exsim14")
        cmd = ("%s >> %s 2>&1 << END\n" % (exsim_bin, self.log) + "%s\n" %
               (self.config.PARAM_FILE) + "END")
        print("==> Running ExSIM...")
        bband_utils.runprog(cmd, abort_on_error=True, print_cmd=False)

        # Restore working directory
        os.chdir(old_cwd)

        # Need to copy and re-format output files
        output_stem = "exsim-output-%s" % (sta_base)
        site_list = self.stat_list.getStationList()

        for site, idx in zip(site_list, range(1, self.num_stations + 1)):
            stat = site.scode
            #out_rotd50_base = "%d.%s.rd50" % (sim_id, stat)
            #out_rotd50 = os.path.join(a_outdir, out_rotd50_base)
            #in_exsim_base = "%s_psa_fs_s%03d.out" % (output_stem, idx)
            #in_exsim_file = os.path.join(a_tmpdir_mod, "PSA", in_exsim_base)
            #print("==> Writing RotD50 file for station: %s" % (stat))
            #self.convert_exsim_to_rd50(in_exsim_file, out_rotd50)
            in_exsim_acc_base = "%sS%03diter001.acc" % (output_stem, idx)
            in_exsim_acc_file = os.path.join(a_tmpdir_mod, "ACC",
                                             in_exsim_acc_base)
            print("==> Writing BBP file for station: %s" % (stat))
            self.convert_exsim_to_bbp(in_exsim_acc_file, stat, a_tmpdir,
                                      a_outdir)

        print("ExSIM Completed".center(80, '-'))
Exemple #38
0
def find_tests(test, rerun):
    """
    # This function searches for .xml files in the accept_inputs directory
    """

    install = InstallCfg()
    resume = True

    accept_test_inputs = "accept_inputs"
    accept_test_refs = "accept_refs"

    input_dir = os.path.join(install.A_TEST_REF_DIR, accept_test_inputs)
    if not os.path.exists(input_dir):
        # These are expected to be in the dist
        print("Acceptance test inputs dir %s does not exist, aborting" %
              (input_dir))
        sys.exit()

    # Create list of test XML files
    files = os.listdir(input_dir)
    wfext = ".xml"

    # First we find all the tests
    test_files = []
    for testfile in files:
        if testfile.endswith(wfext):
            # Don't add SDSU tests on Mac OS X
            if sys.platform == 'darwin' and testfile.find("SDSU") >= 0:
                if test is None or (test is not None and testfile.find(test) >= 0):
                    print("*** Mac OS X detected: skipping test %s." %
                          (testfile))
                continue
            if test is None:
                test_files.append(testfile)
            else:
                if testfile.find(test) >= 0:
                    test_files.append(testfile)

    resume_file = os.path.join(install.A_OUT_LOG_DIR, "resume.txt")
    resume_list = ""
    if rerun:
        os.remove(resume_file)
    # Check for already completed tests if not rerunning
    if resume == True and rerun == False:
        if os.path.exists(resume_file):
            resume_fp = open(resume_file, 'r')
            resume_list = resume_fp.read().splitlines()
            completed_test_count = len(resume_list)
            print("==> Completed Tests : %d" % (completed_test_count))
            resume_fp.close()
            if ((test is None) and
                (completed_test_count >= len(test_files))):
                print("All the acceptance tests have passed previously!")
                proceed = raw_input("Would you like to re-run "
                                    "all the acceptance tests? (y/n)")
                if str.lower(proceed) == 'y':
                    os.remove(resume_file)
                    resume_list = ""
                else:
                    sys.exit(0)

    # Create unittest test case for each file
    for xml_file in test_files:

        # Skip test if we ran it already
        if xml_file in resume_list:
            print("==> Skipping %s" % (xml_file))
            continue

        file_base = xml_file[0:xml_file.find(wfext)]
        # pieces = file_base.split('-')

        # Adjust tolerance depending on test mode
        tolerance = 0.03

        #This defines a method that we're going to add to the
        #BBPAcceptanceTests class. The keyword binding has to
        #be done b/c Python is storing pointers to 'file' and 'file_base'
        #so w/o the keywords, 'file' and 'file_base' in the function will
        #point to the final values
        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()

        # We create a method object which is an instance method for
        # BBPAcceptanceTests which executes the code in
        # testPermutation
        method = new.instancemethod(permutation_test,
                                    None, BBPAcceptanceTests)
        # We give the method a new name in BBPAcceptanceTests
        # which contains the xml file being run
        setattr(BBPAcceptanceTests, "test_%s" % file_base, method)
    def validation_test(self, event, code_base, test_name, run_rupture_gen):
        """
        This function runs the full validation test for the event and
        code_base specified. It creates a Broadband option file with
        the required answers in the input directory and then invokes
        the platform. After execution finishes, it checks if the .bbp
        files were correctly generated, and looks for the gof output
        file.
        """
        # event - Northridge = 1, Loma Prieta = 2, Landers = 3, LOMAP = 4
        #         NR = 5, Whittier = 6, Saguenay = 7
        # code_base GP = 1, UCSB = 2, SDSU = 3
        events = [
            "Northridge", "Loma Prieta", "Landers", "LOMAP", "NR", "Whittier",
            "Saguenay"
        ]

        num_stations = 0
        bbp_files_found = 0
        gof_png_file_found = False
        gof_png_file_valid = False
        a_station_list_filename = None
        install = InstallCfg()

        if SIM_ID is None:
            self.sim_id = int(seqnum.get_seq_num())
        else:
            self.sim_id = SIM_ID

        #print
        #print "Using sim_ID: %d..." % self.sim_id
        # Create input and log directories
        cmd = "mkdir -p %s" % os.path.join(install.A_IN_DATA_DIR,
                                           str(self.sim_id))
        self.failIf(
            bband_utils.runprog(cmd, False) != 0,
            "Error creating input directory")
        cmd = "mkdir -p %s" % os.path.join(install.A_OUT_LOG_DIR,
                                           str(self.sim_id))
        self.failIf(
            bband_utils.runprog(cmd, False) != 0,
            "Error creating log directory")
        # Create path for option file
        a_opt_filename = os.path.join(install.A_IN_DATA_DIR, str(self.sim_id),
                                      "%d.optfile" % (self.sim_id))
        try:
            opt_file = open(a_opt_filename, 'w')
            # Select validation run
            opt_file.write("y\n")
            # Select event
            opt_file.write("%s\n" % (events[event - 1]))
            # Select the rupture generator
            if run_rupture_gen:
                # Yes to rupture generator
                opt_file.write("y\n")
                if code_base == 2:
                    # Run UCSB rupture generator
                    opt_file.write("2\n")
                else:
                    # Otherwise run GP rupture generator
                    opt_file.write("1\n")
                # No need to provide custom src file, use existing
                opt_file.write("n\n")
            else:
                # Not using the rupture generator
                opt_file.write("n\n")
            # Validate all stations
            opt_file.write("1\n")
            # Select codebase
            if code_base == 1:
                # GP across the board
                opt_file.write("1\n1\n4\n")
            elif code_base == 2:
                # UCSB low-frequency requires UCSB across the board
                opt_file.write("3\n2\n4\n")
            elif code_base == 3:
                # SDSU doesn't have a low-frequency component
                opt_file.write("1\n3\n4\n")
            else:
                self.failIf(True, "Code base not supported!")
            # We don't want velocity seismograms
            opt_file.write("y\n")
            # But we want acceleration seismograms
            opt_file.write("y\n")
            # Select GOF module, for validation we want to use GP
            opt_file.write("1\n")
            opt_file.close()
        except IOError:
            self.failIf(True, "Cannot create option file!")

        # Log output to a_log_filename
        a_log_filename = os.path.join(
            install.A_OUT_LOG_DIR, str(self.sim_id),
            "%s.%s.log" % (str(self.sim_id), test_name))

        # Run BBP command
        cmd = "%s/run_bbp.py -s %d -o %s >> %s" % (
            install.A_COMP_DIR, self.sim_id, a_opt_filename, a_log_filename)
        # print cmd
        # Run the validation scenario
        self.failIf(
            bband_utils.runprog(cmd, False) != 0,
            "Fatal error while running the Broadband Platform")
        # First look for the station list
        try:
            files = os.listdir(
                os.path.join(install.A_IN_DATA_DIR, str(self.sim_id)))
        except OSError:
            self.failIf(True, "Cannot access input directory!")

        for my_file in files:
            if my_file.endswith(".stl"):
                a_station_list_filename = os.path.join(install.A_IN_DATA_DIR,
                                                       str(self.sim_id),
                                                       my_file)
                break

        # Fail if we don't have a station list
        self.failIf(a_station_list_filename is None,
                    "Cannot find file with station list!")

        # Now, figure out how many stations we have
        try:
            stl_file = open(a_station_list_filename, 'r')
            for _ in stl_file:
                num_stations = num_stations + 1
        except IOError:
            self.failIf(True, "Cannot read station list file!")

    # Now look for the output files
        try:
            files = os.listdir(
                os.path.join(install.A_OUT_DATA_DIR, str(self.sim_id)))
        except OSError:
            self.failIf(True, "Cannot access output directory!")
        for my_file in files:
            if my_file.endswith(".acc.bbp"):
                a_bbp_file_path = os.path.join(install.A_OUT_DATA_DIR,
                                               str(self.sim_id), my_file)
                try:
                    my_size = os.stat(a_bbp_file_path).st_size
                except OSError:
                    self.failIf(True, "Cannot stat file %s!" % a_bbp_file_path)
                # Ok, let's count this file
                if my_size > 0:
                    bbp_files_found = bbp_files_found + 1
            if my_file.startswith("gof-") and my_file.endswith(".png"):
                # Found the gof.png file
                gof_png_file_found = True
                a_gofpng_file_path = os.path.join(install.A_OUT_DATA_DIR,
                                                  str(self.sim_id), my_file)
                try:
                    my_size = os.stat(a_gofpng_file_path).st_size
                except OSError:
                    self.failIf(True,
                                "Cannot stat file %s!" % a_gofpng_file_path)
                # Check if size > 0
                if my_size > 0:
                    gof_png_file_valid = True

        # Now check if we got the right results
        self.failIf(not gof_png_file_found, "GOF file not found!")
        self.failIf(not gof_png_file_valid, "GOF file size is 0!")
        self.failIf(
            bbp_files_found != num_stations,
            "Found %d valid BBP files, expecting %d!" %
            (bbp_files_found, num_stations))
Exemple #40
0
    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, '-'))
Exemple #41
0
    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, '-'))
Exemple #42
0
    def run(self):
        print("Generating Plots".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,
                                "%d/%d.gen_plots.log" % (sim_id, 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))
        #        if self.a_datadir is None:
        #            # When datadir is None, we look for the observation data in
        #            # the tmpdir where the ObsSeismograms module generates them
        a_tmpdir_seis = os.path.join(install.A_TMP_DATA_DIR, str(sim_id),
                                     "obs_seis_%s" % (sta_base))

        # 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_tmpdir_seis)

        slo = StationList(a_statfile)
        site_list = slo.getStationList()

        for site in site_list:
            stat = site.scode

            # Since we're using the GP station list, make sure the
            # .bbp for the station exists.  It might not if we ran the
            # validation with a different station list (like UCSB for
            # Landers)
            bbpfile = os.path.join(a_tmpdir_seis, "%s.bbp" % stat)
            expected_file = os.path.join(a_outdir,
                                         "%d.%s.vel.bbp" % (sim_id, stat))
            if (not os.path.exists(expected_file)
                    or not os.path.exists(bbpfile)):
                # just skip this station
                continue

            print("==> Plotting seismogram comparison for station: %s" %
                  (stat))
            if self.format == 'vel':
                # We have velocity, nothing we need to do
                filename1 = bbpfile
            elif self.format == 'acc':
                # We have acceleration, must integrate first
                # Create path names and check if their sizes are within bounds
                nsfile = os.path.join(a_tmpdir, "temp.acc.000")
                ewfile = os.path.join(a_tmpdir, "temp.acc.090")
                udfile = os.path.join(a_tmpdir, "temp.acc.ver")
                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, "temp.acc.%s" % (comp))
                    fileout = os.path.join(a_tmpdir, "temp.vel.%s" % (comp))

                    bband_utils.check_path_lengths([filein, fileout],
                                                   bband_utils.GP_MAX_FILENAME)

                    cmd = ("%s/integ_diff integ=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)

                # Create path names and check if their sizes are within bounds
                nsfile = os.path.join(a_tmpdir, "temp.vel.000")
                ewfile = os.path.join(a_tmpdir, "temp.vel.090")
                udfile = os.path.join(a_tmpdir, "temp.vel.ver")
                vel_bbp_file = os.path.join(a_tmpdir, "temp.%s.vel" % stat)

                bband_utils.check_path_lengths([nsfile, ewfile, udfile],
                                               bband_utils.GP_MAX_FILENAME)

                cmd = ("%s/wcc2bbp wcc2bbp=1 " % install.A_GP_BIN_DIR +
                       "nsfile=%s ewfile=%s udfile=%s > %s 2>> %s" %
                       (nsfile, ewfile, udfile, vel_bbp_file, self.log))
                bband_utils.runprog(cmd, abort_on_error=True, print_cmd=False)
                filename1 = vel_bbp_file

            # Generate arias duration files for calculated data
            calc_acc = os.path.join(a_outdir, "%d.%s.acc.bbp" % (sim_id, stat))
            calc_peer_n = os.path.join(a_tmpdir,
                                       "%d.%s_N.acc" % (sim_id, stat))
            calc_peer_e = os.path.join(a_tmpdir,
                                       "%d.%s_E.acc" % (sim_id, stat))
            calc_peer_z = os.path.join(a_tmpdir,
                                       "%d.%s_Z.acc" % (sim_id, stat))
            # Convert calculated acc seismogram into peer format
            bbp_formatter.bbp2peer(calc_acc, calc_peer_n, calc_peer_e,
                                   calc_peer_z)

            # Now calculate arias duration for each component
            for comp in ["N", "E", "Z"]:
                file_in = os.path.join(a_tmpdir,
                                       "%d.%s_%s.acc" % (sim_id, stat, comp))
                file_out = os.path.join(
                    a_tmpdir, "%d.%s_%s.arias" % (sim_id, stat, comp))
                arias_duration.ad_from_acc(file_in, file_out)

            # Generate arias duration files for observed data
            obs_acc = os.path.join(a_tmpdir_seis, "%s.bbp" % stat)
            obs_peer_n = os.path.join(a_tmpdir, "obs.%s_N.acc" % (stat))
            obs_peer_e = os.path.join(a_tmpdir, "obs.%s_E.acc" % (stat))
            obs_peer_z = os.path.join(a_tmpdir, "obs.%s_Z.acc" % (stat))
            # Convert observed acc seismogram into peer format
            bbp_formatter.bbp2peer(obs_acc, obs_peer_n, obs_peer_e, obs_peer_z)

            # Now calculate arias duration for each component
            for comp in ["N", "E", "Z"]:
                file_in = os.path.join(a_tmpdir,
                                       "obs.%s_%s.acc" % (stat, comp))
                file_out = os.path.join(a_tmpdir,
                                        "obs.%s_%s.arias" % (stat, comp))
                arias_duration.ad_from_acc(file_in, file_out)

            # Plot seismograms with arias duration
            filename2 = os.path.join(a_outdir,
                                     "%d.%s.vel.bbp" % (sim_id, stat))
            outfile = os.path.join(
                a_outdir,
                "%s_%d_%s_overlay.png" % (self.comp_label, sim_id, stat))
            obs_arias_n = os.path.join(a_tmpdir, "obs.%s_N.arias" % (stat))
            obs_arias_e = os.path.join(a_tmpdir, "obs.%s_E.arias" % (stat))
            obs_arias_z = os.path.join(a_tmpdir, "obs.%s_Z.arias" % (stat))
            calc_arias_n = os.path.join(a_tmpdir,
                                        "%d.%s_N.arias" % (sim_id, stat))
            calc_arias_e = os.path.join(a_tmpdir,
                                        "%d.%s_E.arias" % (sim_id, stat))
            calc_arias_z = os.path.join(a_tmpdir,
                                        "%d.%s_Z.arias" % (sim_id, stat))

            plot_seismograms.plot_overlay_with_arias(
                stat, filename1, filename2, obs_arias_n, obs_arias_e,
                obs_arias_z, calc_arias_n, calc_arias_e, calc_arias_z,
                self.comp_label, "run %d" % sim_id, outfile)

        # Now create rd50 comparison plots
        for site in site_list:
            stat = site.scode
            print("==> Plotting RotD50 comparison for station: %s" % (stat))

            # 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 .rd50 file exists for comparison. If it don't
            # exist, skip it
            rd50_file = None
            if ("%s.rd50" % (stat)) in filelist:
                rd50_file = "%s.rd50" % (stat)
            else:
                # Skip this station
                continue

            # Plot rotd50 results
            rd50_filename1 = os.path.join(a_tmpdir_seis, rd50_file)
            rd50_filename2 = os.path.join(a_outdir,
                                          "%d.%s.rd50" % (sim_id, stat))
            outfile = os.path.join(
                a_outdir,
                "%s_%d_%s_rotd50.png" % (self.comp_label, sim_id, stat))

            plot_rotd50.plot_rd50(stat,
                                  rd50_filename1,
                                  rd50_filename2,
                                  self.comp_label,
                                  sim_id,
                                  outfile,
                                  site.low_freq_corner,
                                  site.high_freq_corner,
                                  quiet=True)

        print("Generating Plots Completed".center(80, '-'))
Exemple #43
0
    def __init__(self):

        install = InstallCfg.getInstance()
        # Name and Path to executable

        self.COMPS = ["000", "090", "ver"]
        self.INPUT_FORMAT_GOF = "GOF"

        # Path to GOF binaries
        self.GOF_BIN = os.path.join(install.A_SDSU_BIN_DIR, "GOF_MO")
        self.GOF_SpFit_BIN = os.path.join(install.A_SDSU_BIN_DIR, "GOF_SpFit")
        self.GOF_PGX_BIN = os.path.join(install.A_SDSU_BIN_DIR, "GOF_PGX")
        self.GOF_CCFit_BIN = os.path.join(install.A_SDSU_BIN_DIR, "GOF_CCFit")
        self.GOF_DCumEn_BIN = os.path.join(install.A_SDSU_BIN_DIR, "GOF_DCumEn")
        self.GOF_FSComp_BIN = os.path.join(install.A_SDSU_BIN_DIR, "GOF_FSComp")
        self.GOF_InElFit_BIN = os.path.join(install.A_SDSU_BIN_DIR,
                                            "GOF_InElFit")
        self.GOF_SAFit16_BIN = os.path.join(install.A_SDSU_BIN_DIR,
                                            "GOF_SAFit16")
        self.GOF_SpecDurFit_BIN = os.path.join(install.A_SDSU_BIN_DIR,
                                               "GOF_SpecDurFit")
        self.GOF_NGA_BIN = os.path.join(install.A_SDSU_BIN_DIR, "GOF_MO_NGA")

        # Working file names
        self.PARAM_DAT_FILE = "PARAM.dat"
        self.INPUT_SEISMO_1 = "CONCAT_1"
        self.INPUT_SEISMO_2 = "CONCAT_2"

        # Summary file
        self.SUMMARY_FILE = "gof_summary.txt"

        self.cfggof = {}
        self.cfggof["input_format"] = "GOF"
        self.cfggof["input_set_1"] = "./sample1_1.input"
        self.cfggof["input_set_2"] = "./sample1_2.input"
        self.cfggof["output_dir"] = "./output"
        self.cfggof["work_dir"] = "./work"
        self.cfggof["use_nga"] = False

        self.cfggof["num_headers"] = 0 #number of headers preceeding each seismogram
        self.cfggof["num_station"] = 1 #number of stations
        self.cfggof["timesteps_set_1"] = 0 #nt number of timesteps in set 1
        self.cfggof["timesteps_set_2"] = 0 #nt number of timesteps in set 2
        self.cfggof["input_param"] = "A" #Input format(A,V,D), (meter, sec)
        self.cfggof["seismo_length"] = 0 #length of seismograms (seconds)
        self.cfggof["low_cut"] = 0.1 #Low cut (period)
        self.cfggof["high_cut"] = 10.0 #High cut (period)
        self.cfggof["weights"] = dict()
        self.cfggof["weights"]["pga"] = 1.0                          #  Weighting on PGA
        self.cfggof["weights"]["pgv"] = 1.0                          #  Weighting on PGV
        self.cfggof["weights"]["pgd"] = 1.0                          #  Weighting on PGD
        self.cfggof["weights"]["psa"] = 1.0                          #  Weighting on PSA
        self.cfggof["weights"]["spectral_Fit"] = 1.0                 #  Weighting on Spectral Fit
        self.cfggof["weights"]["cumulative_energy_fit"] = 1.0        #  Weighting on Cumulative Energy Fit
        self.cfggof["weights"]["inelastic_elastic_fit"] = 1.0        #  Weighting on Inelastic/Elastic Fit (16)
        self.cfggof["weights"]["sepctral_acc"] = 1.0                 #  Weighting on Spec Acc (16)
        self.cfggof["weights"]["spec_duration"] = 1.0                #  Weighting on Spec Dur (16)
        self.cfggof["weights"]["data_energy_release_duration"] = 1.0 #  Weighting on Data Energy Release Duration (5%-75%)
        self.cfggof["weights"]["cross_correlation"] = 1.0            #  Weighting on Cross-Correlation
        self.cfggof["weights"]["fourier_spectrum"] = 1.0             #  Weighting on Fourier Spectrum
        self.cfggof["file"] = dict()
        self.cfggof["file"]["pga"] = "GOF_PGA.list"
        self.cfggof["file"]["pgv"] = "GOF_PGV.list"
        self.cfggof["file"]["pgd"] = "GOF_PGD.list"
        self.cfggof["file"]["psa"] = "GOF_PSA.list"
        self.cfggof["file"]["spectral_Fit"] = "GOF_SPECFIT.list"
        self.cfggof["file"]["cumulative_energy_fit"] = "GOF_ENERGYFIT.list"
        self.cfggof["file"]["inelastic_elastic_fit"] = "GOF_InElEl.list"
        self.cfggof["file"]["sepctral_acc"] = "GOF_SAFIT.list"
        self.cfggof["file"]["spec_duration"] = "GOF_SPECDUR.list"
        self.cfggof["file"]["data_energy_release_duration"] = "GOF_DUR.list"
        self.cfggof["file"]["cross_correlation"] = "GOF_CROSSCOR.list"
        self.cfggof["file"]["fourier_spectrum"] = "GOF_FS.list"

        self.cfgnga = {}
        self.cfgnga["source_mag"] = 0.0
        self.cfgnga["dip"] = 0.0
        self.cfgnga["rake"] = 0.0
        self.cfgnga["depth_coseismic"] = 0.0
        self.cfgnga["site_file"] = ""
Exemple #44
0
def calculate_epicenter(input_file):
    """
    This function returns the epicenter of an event using either a SRC
    file or a SRF file to look for the hypocenter location. It uses
    Rob Graves' xy2ll utility to convert the coordinates to lat/lon.
    """
    # If we have a SRF file, we already have a function that does this
    if input_file.endswith(".srf"):
        # Get information from srf file
        hypo_lon, hypo_lat, _ = get_hypocenter(input_file)
        return hypo_lon, hypo_lat

    # If we don't have a SRC file, we should print an error here
    if not input_file.endswith(".src"):
        bband_utils.ParameterError("input file should be a SRC or SRF file!")

    # Ok, we have a SRC file
    # Get information from SRC file
    cfgdict = bband_utils.parse_properties(input_file)

    try:
        strike = cfgdict["strike"]
    except KeyError:
        bband_utils.ParameterError("SRC file missing STRIKE parameter!")
    strike = float(strike)

    try:
        dip = cfgdict["dip"]
    except KeyError:
        bband_utils.ParameterError("SRC file missing DIP parameter!")
    dip = float(dip)

    try:
        hypo_down_dip = cfgdict["hypo_down_dip"]
    except KeyError:
        bband_utils.ParameterError("SRC file missing "
                                   "HYPO_DOWN_DIP parameter!")
    hypo_down_dip = float(hypo_down_dip)

    try:
        hypo_along_strike = cfgdict["hypo_along_stk"]
    except KeyError:
        bband_utils.ParameterError("SRC file missing "
                                   "HYPO_ALONG_STK parameter!")
    hypo_along_strike = float(hypo_along_strike)

    try:
        lat_top_center = cfgdict["lat_top_center"]
    except KeyError:
        bband_utils.ParameterError("SRC file missing "
                                   "LAT_TOP_CENTER parameter!")
    lat_top_center = float(lat_top_center)

    try:
        lon_top_center = cfgdict["lon_top_center"]
    except KeyError:
        bband_utils.ParameterError("SRC file missing "
                                   "LON_TOP_CENTER parameter!")
    lon_top_center = float(lon_top_center)

    # Ok, we have all the parameters that we need!
    hypo_perpendicular_strike = hypo_down_dip * math.cos(math.radians(dip))

    # Now call xy2ll program to convert it to lat/long
    # Create temp directory to avoid any race conditions
    tmpdir = tempfile.mkdtemp(prefix="bbp-")
    hypfile = os.path.join(tmpdir, "src_hypo.tmp")
    install = InstallCfg.getInstance()
    cmd = ('echo "%f %f" | %s mlat=%f mlon=%f xazim=%f > %s' %
           (hypo_along_strike, hypo_perpendicular_strike,
            os.path.join(install.A_GP_BIN_DIR, "xy2ll"), lat_top_center,
            lon_top_center, strike, hypfile))
    bband_utils.runprog(cmd, print_cmd=False)
    src_hypo_fp = open(hypfile, 'r')
    src_hypo_data = src_hypo_fp.readline()
    src_hypo_fp.close()
    src_hypo = [float(val) for val in src_hypo_data.split()]
    # Delete temp directory
    shutil.rmtree(tmpdir)

    # Return calculated lon/lat
    return src_hypo[0], src_hypo[1]
Exemple #45
0
def findXML():
    parser = optparse.OptionParser()
    parser.add_option("-i",
                      "--in-dir",
                      dest="inDir",
                      help="Input folder with XML files",
                      metavar="IN_DIR")
    parser.add_option("-r",
                      "--resume",
                      action="store_true",
                      dest="resume",
                      help="Resume batch processing of XML files",
                      metavar="RESUME")
    parser.add_option(
        "-o",
        "--out-dir",
        dest="outDir",
        help=
        "Output folder for simulation data (indata, outdata, log, tmpdata) with simID",
        metavar="OUT_DIR")
    parser.add_option(
        "-f",
        "--force",
        action="store_true",
        dest="force",
        help=
        "Force overwrite of BBP folders (indata, outdata, log, tmpdata) with same simID",
        metavar="FORCE")

    (options, args) = parser.parse_args()

    if not options.inDir:
        parser.error("Folder with XML files is required!")

    if os.path.exists(options.inDir):
        files = os.listdir(options.inDir)
    else:
        print "Invalid input dir: %s" % options.inDir
        sys.exit(1)

    if not files:
        print "No XML files were found in input dir: %s" % options.inDir
        sys.exit(1)

    if options.outDir:
        if not os.path.exists(options.outDir):
            try:
                os.mkdir(options.outDir)
            except:
                print("Failed to create output dir: %s! Aborting..." %
                      (options.outDir))
                sys.exit(1)
        if not os.path.exists("%s/indata" % options.outDir):
            try:
                os.mkdir("%s/indata" % options.outDir)
            except:
                print("Failed to create dir: %s/indata! Aborting..." %
                      (options.outDir))
                sys.exit(1)
        if not os.path.exists("%s/outdata" % options.outDir):
            try:
                os.mkdir("%s/outdata" % options.outDir)
            except:
                print("Failed to create dir: %s/outdata! Aborting..." %
                      (options.outDir))
                sys.exit(1)
        if not os.path.exists("%s/tmpdata" % options.outDir):
            try:
                os.mkdir("%s/tmpdata" % options.outDir)
            except:
                print("Failed to create dir: %s/tmpdata! Aborting..." %
                      (options.outDir))
                sys.exit(1)
        if not os.path.exists("%s/logs" % options.outDir):
            try:
                os.mkdir("%s/logs" % options.outDir)
            except:
                print("Failed to create dir: %s/logs! Aborting..." %
                      (options.outDir))
                sys.exit(1)

    if options.force:
        print "WARNING: Force overwrite is ON!"
        print "Some existing BBP data folders will be overwritten!"

    install = InstallCfg()
    num_sims = 0
    total_sims = 0
    if options.inDir[-1:] == os.path.sep:
        options.inDir = options.inDir[0:-1]
    files = sorted([
        f for f in os.listdir(options.inDir)
        if os.path.isfile(options.inDir + os.path.sep + f)
    ])
    resume_list = ""
    if options.resume == True:
        if os.path.exists("%s/batch_resume.txt" % install.A_OUT_LOG_DIR):
            resume_fp = open("%s/batch_resume.txt" % install.A_OUT_LOG_DIR,
                             'r')
            resume_list = resume_fp.readlines()
            resume_fp.close()
    else:
        if os.path.exists("%s/batch_resume.txt" % install.A_OUT_LOG_DIR):
            os.remove("%s/batch_resume.txt" % install.A_OUT_LOG_DIR)

    run_list = []

    for file in files:
        if file.endswith(".xml"):
            total_sims += 1
            if options.resume == True:
                match = False
                for i in resume_list:
                    if file == i.strip():
                        match = True
                        print "Skipping %s" % file
                        break
                if match == True:
                    continue

            run_list.append(os.path.abspath(os.path.join(options.inDir, file)))
            num_sims += 1

    if not num_sims == total_sims:
        sims = "%d/%d" % (num_sims, total_sims)
    else:
        sims = str(num_sims)

    # Setup the simlist and movelist for logging
    simlist = []
    mvlist = []

    print "Preparing to run %s simulations from %s" % (sims, options.inDir)
    run_count = 1
    for file in run_list:
        filename = os.path.basename(file)
        file_base = filename[0:filename.find(".xml")]
        pieces = file_base.split("_")

        simID = -1
        if len(pieces) > 1:
            try:
                simID = int(pieces[0])
            except ValueError:
                print "Failed to fetch simID from XML file name: %s" % file

        if simID == -1:
            simID = int(seqnum.getSeqNum())
            print "Running with generated simID: %d" % simID

        t0 = time.time()
        start_time = time.strftime("%Y/%m/%d-%H:%M:%S", time.localtime())

        indatadir = "%s/%d" % (install.A_IN_DATA_DIR, simID)
        outdatadir = "%s/%d" % (install.A_OUT_DATA_DIR, simID)
        tmpdatadir = "%s/%d" % (install.A_TMP_DATA_DIR, simID)
        logdir = "%s/%d" % (install.A_OUT_LOG_DIR, simID)
        logfiledir = "%s/logs/%d" % (options.outDir, simID)
        log_file = "%s/%s.log" % (logfiledir, file_base)
        # Make sure we have an absolute path for log_file
        log_file = os.path.abspath(log_file)
        if not os.path.exists(logfiledir):
            try:
                os.mkdir(logfiledir)
            except:
                print("Failed to create dir: %s! Aborting..." % (logfiledir))
                sys.exit(1)

        dir_exists = False
        if os.path.exists(indatadir):
            if options.force:
                shutil.rmtree(indatadir)
            else:
                dir_exists = True
        if os.path.exists(tmpdatadir):
            if options.force:
                shutil.rmtree(tmpdatadir)
            else:
                dir_exists = True
        if os.path.exists(outdatadir):
            if options.force:
                shutil.rmtree(outdatadir)
            else:
                dir_exists = True
        if os.path.exists(logdir):
            if options.force:
                shutil.rmtree(logdir)
            else:
                dir_exists = True

        if dir_exists:
            print "BBP folders with simID %d exists!"
            print "Force overwrite is OFF, skipping %s" % (simID, file)
            continue

        print "Processing file (%d/%d): %s" % (run_count, num_sims, file)
        cmd = "%s/run_bbp.py -x %s -s %d -l %s" % (install.A_COMP_DIR, file,
                                                   simID, log_file)
        if (LOG_ONLY == True):
            simlist.append("%s\n" % (cmd))
            if options.outDir:
                # Notes:
                # 1) force option not currently supported while
                # logging sims
                # 2) assumption is that dir outdir/simid
                # does not already exist
                od_indatadir = "%s/indata" % (options.outDir)
                od_outdatadir = "%s/outdata" % (options.outDir)
                od_tmpdatadir = "%s/tmpdata" % (options.outDir)
                od_logdir = "%s/logs" % (options.outDir)
                mvlist.append("mv %s %s\n" % (indatadir, od_indatadir))
                mvlist.append("mv %s %s\n" % (tmpdatadir, od_tmpdatadir))
                mvlist.append("mv %s %s\n" % (outdatadir, od_outdatadir))
                mvlist.append("mv %s %s\n" % (logdir, od_logdir))
            run_count += 1
            continue
        bband_utils.runprog(cmd, False)
        if options.outDir:
            od_indatadir = "%s/indata/%d" % (options.outDir, simID)
            od_outdatadir = "%s/outdata/%d" % (options.outDir, simID)
            od_tmpdatadir = "%s/tmpdata/%d" % (options.outDir, simID)
            od_logdir = "%s/logs/%d" % (options.outDir, simID)

            od_dir_exists = False
            if os.path.exists(od_indatadir):
                if options.force:
                    shutil.rmtree(od_indatadir)
                else:
                    od_dir_exists = True
            if os.path.exists(od_tmpdatadir):
                if options.force:
                    shutil.rmtree(od_tmpdatadir)
                else:
                    od_dir_exists = True
            if os.path.exists(od_outdatadir):
                if options.force:
                    shutil.rmtree(od_outdatadir)
                else:
                    od_dir_exists = True
            if os.path.exists(od_logdir):
                if options.force:
                    shutil.rmtree(od_logdir)
                else:
                    od_dir_exists = True

            if dir_exists:
                print "Warning: Folder(s) with simID %d exists in output folder %s! Force overwrite is OFF, output will be left in BBP folders!" % (
                    simID, options.outDir)
            else:
                if os.path.exists(indatadir):
                    shutil.move(indatadir, od_indatadir)
                if os.path.exists(tmpdatadir):
                    shutil.move(tmpdatadir, od_tmpdatadir)
                if os.path.exists(outdatadir):
                    shutil.move(outdatadir, od_outdatadir)
                if os.path.exists(logdir):
                    shutil.move(logdir, od_logdir)

        run_time = time.time() - t0
        run_time_str = str(datetime.timedelta(seconds=run_time))
        end_time = time.strftime("%Y/%m/%d-%H:%M:%S", time.localtime())

        if options.outDir:
            if not od_dir_exists:
                out_data_dir = od_outdatadir
        else:
            out_data_dir = "%s/%d" % (install.A_OUT_DATA_DIR, simID)

        files = os.listdir(out_data_dir)
        if files:
            try:
                run_log_fp = open("%s/batch_run.log" % options.inDir, 'a')
            except IOError:
                run_log_fp = open("%s/batch_run.log" % install.A_OUT_LOG_DIR,
                                  'a')

            run_log_fp.write(
                "%d\t%s\t%s\t%s\t%s\t%s\n" %
                (simID, os.path.abspath(file), os.path.abspath(out_data_dir),
                 start_time, end_time, run_time_str))
            run_log_fp.flush()
            run_log_fp.close()
            resume_fp = open("%s/batch_resume.txt" % install.A_OUT_LOG_DIR,
                             'a')
            resume_fp.write("%s\n" % filename)
            resume_fp.flush()
            resume_fp.close()
        run_count += 1

    # Write the sims to the execlog
    print "Opening %s/batch_run_bbp_sims.log" % (options.inDir)
    execlog = open("%s/batch_run_bbp_sims.log" % (options.inDir), 'w')
    for sim in simlist:
        execlog.write(sim)
    execlog.close()

    # Write the moves to the execlog
    print "Opening %s/batch_run_bbp_moves.log" % (options.inDir)
    execlog = open("%s/batch_run_bbp_moves.log" % (options.inDir), 'w')
    for mv in mvlist:
        execlog.write(mv)
    execlog.close()
Exemple #46
0
    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, '-'))
Exemple #47
0
    """
    print("Usage: %s <command> <options>" % os.path.basename(sys.argv[0]))
    print()
    print("Available commands:")
    print(" plot <bbp_in> <png_out>       - plots bbp_in, generating png_out")
    print(" comp <bbp1> <bbp2> <png_out>  - plots bbp1 and bbp2 into png_out")
    print(" integrate <acc_bbp> <vel_bbp> - acc_bbp -> integration -> vel_bbp")
    print(" diff <vel_bbp> <acc_bbp>      - vel_bbp -> diff -> acc_bbp")
    print()
    sys.exit(0)

# ------------------------------------------------------------------------------
# Main
# ------------------------------------------------------------------------------

INSTALL = InstallCfg.getInstance()

# Check if at least 1 parameter
if len(sys.argv) < 2:
    usage()
# Get command
CMD = sys.argv[1].lower()
if CMD == "plot":
    if len(sys.argv) < 4:
        usage()
    plot(sys.argv[2], sys.argv[3])
elif CMD == "comp":
    if len(sys.argv) < 5:
        usage()
    comp(sys.argv[2], sys.argv[3], sys.argv[4])
elif CMD == "integrate" or CMD == "int":
Exemple #48
0
def main():
    """
    Parse command line options and create the needed files/directories
    """
    # Detect BBP installation
    bbp_install = InstallCfg.getInstance()

    prog_base = os.path.basename(sys.argv[0])
    usage = "usage: %s [options]" % (prog_base)
    parser = optparse.OptionParser(usage)
    parser.add_option("-c",
                      "--codebase",
                      type="string",
                      action="store",
                      dest="codebase",
                      help="Codebase for the simulation: %s" % (CODEBASES))
    parser.add_option("-v",
                      "--velocity-model",
                      type="string",
                      action="store",
                      dest="vmodel",
                      help="Velocity model (region) for this simulation")
    parser.add_option("--src",
                      "--source",
                      type="string",
                      action="store",
                      dest="source",
                      help="Source description file for the simulation")
    parser.add_option("--stl",
                      "--station-list",
                      type="string",
                      action="store",
                      dest="station_list",
                      help="Station list file for the simulation")
    parser.add_option("-d",
                      "--dir",
                      type="string",
                      action="store",
                      dest="simdir",
                      help="Simulation directory")
    parser.add_option("--hypo-rand",
                      action="store_true",
                      dest="hyporand",
                      help="Enables hypocenter randomization")
    parser.add_option("--no-hypo-rand",
                      action="store_false",
                      dest="hyporand",
                      help="Disables hypocenter randomization")
    parser.add_option("-n",
                      "--num-simulations",
                      type="int",
                      action="store",
                      dest="numsim",
                      help="Number of simulations to run")
    parser.add_option("--email",
                      type="string",
                      action="store",
                      dest="email",
                      help="Email for job notifications")
    (options, _) = parser.parse_args()

    # Validate codebase to use
    codebase = options.codebase
    if codebase is None:
        print "Please specify a codebase!"
        sys.exit(1)
    codebase = codebase.lower()
    if codebase not in CODEBASES:
        print "Codebase needs to be one of: %s" % (CODEBASES)

    # Check for velocity model
    vmodel_names = velocity_models.get_all_names()
    vmodel = options.vmodel
    if vmodel is None:
        print "Please provide a velocity model (region) for this simulation!"
        print "Available options are: %s" % (vmodel_names)
        sys.exit(1)
    vmodels = [v_model.lower() for v_model in vmodel_names]
    if vmodel.lower() not in vmodels:
        print("Velocity model %s does not appear to be available on BBP" %
              (vmodel))
        print("Available options are: %s" % (vmodel_names))
        print "Please provide another velocity model or check your BBP installation."
        sys.exit(1)
    # Now get the name with the correct case
    vmodel = vmodel_names[vmodels.index(vmodel.lower())]

    # Check for hypocenter randomization
    if options.hyporand is None:
        print "Please specify --hypo-rand or --no-hypo-rand!"
        sys.exit(1)

    if options.hyporand:
        hypo_rand = True
    else:
        hypo_rand = False

    # Get the source file
    source_file = options.source
    if source_file is None:
        print "Please provide a source description (src file)!"
        sys.exit(1)
    # Make it a full path
    source_file = os.path.realpath(source_file)
    # Make sure source file exists and is readable
    if not os.path.isfile(source_file) or not os.access(source_file, os.R_OK):
        print "Source file does not seem to be accessible!"
        sys.exit(1)

    # Get the station list
    station_list = options.station_list
    if station_list is None:
        print "Please provide a station list (stl file)!"
        sys.exit(1)
    # Make it a full path
    station_list = os.path.realpath(station_list)
    # Make sure station list exists and is readable
    if not os.path.isfile(station_list) or not os.access(
            station_list, os.R_OK):
        print "Station list foes not seem to be accessible!"
        sys.exit(1)

    # Check for the simulation directory
    simdir = options.simdir
    if simdir is None:
        print "Please provide a simulation directory!"
        sys.exit(1)
    simdir = os.path.abspath(simdir)
    if os.path.exists(simdir):
        print "Simulation directory exists: %s" % (simdir)
        opt = raw_input("Do you want to delete its contents (y/n)? ")
        if opt.lower() != "y":
            print "Please provide another simulation directory!"
            sys.exit(1)
        opt = raw_input("ARE YOU SURE (y/n)? ")
        if opt.lower() != "y":
            print "Please provide another simulation directory!"
            sys.exit(1)
        # Delete existing directory (we already asked the user twice!!!)
        shutil.rmtree(simdir)

    # Pick up number of simulations to run
    numsim = options.numsim
    if numsim < 1 or numsim > MAX_SIMULATIONS:
        print("Number of simulations should be between 1 and %d" %
              (MAX_SIMULATIONS))
        sys.exit(1)

    # Check for e-mail address
    email = options.email
    if email is None:
        print "Please provide an e-mail address for job notifications"
        sys.exit(1)

    # Make sure user has configured the setup_bbp_epicenter_env.sh script
    setup_bbp_env = os.path.join(bbp_install.A_INSTALL_ROOT,
                                 "utils/batch/setup_bbp_epicenter_env.sh")
    if not os.path.exists(setup_bbp_env):
        print("Cannot find setup_bbp_epicenter_env.sh script!")
        print("Expected at: %s" % (setup_bbp_env))
        sys.exit(1)
    # Create simulation directories
    prefix = "%s-%s" % (os.path.splitext(
        os.path.basename(source_file))[0], codebase.lower())
    # Make sure we remove spaces from prefix
    prefix = prefix.replace(" ", '')
    os.makedirs(simdir)
    indir = os.path.join(simdir, "Sims", "indata")
    outdir = os.path.join(simdir, "Sims", "outdata")
    tmpdir = os.path.join(simdir, "Sims", "tmpdata")
    logsdir = os.path.join(simdir, "Sims", "logs")
    xmldir = os.path.join(simdir, "Xml")
    srcdir = os.path.join(simdir, "Src")
    for mdir in [indir, outdir, tmpdir, logsdir, xmldir, srcdir]:
        os.makedirs(mdir)
    # Generate source files
    generate_src_files(numsim, source_file, srcdir, prefix, hypo_rand)
    # Generate xml files
    generate_xml(bbp_install, numsim, srcdir, xmldir, logsdir, vmodel,
                 codebase, prefix, station_list)
    # Write pbs file
    write_pbs(bbp_install, numsim, simdir, xmldir, email, prefix)
Exemple #49
0
def plot_map_gof(r_srcfile, r_stations, resid_file, comp_label, sim_id):
    """
    Reads data from resid_file and plots a map gof plot with a number
    of periods
    """
    # Make sure we have a src or srf file
    if (r_srcfile is None or r_srcfile == "" or
        (not r_srcfile.endswith(".srf") and not r_srcfile.endswith(".src"))):
        # We need a SRC or SRF file to get the fault geometry
        return

    # Get directory names
    install = InstallCfg.getInstance()
    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_input_file = os.path.join(a_indir, r_srcfile)
    a_station_file = os.path.join(a_indir, r_stations)

    # Define boundaries to plot using the stations in the station file
    (north, south, east,
     west) = plot_map.set_boundaries_from_stations(a_station_file)
    trace_file = "%s.trace" % (a_input_file)
    simple_station_file = "%s.simple" % (a_station_file)

    if r_srcfile.endswith(".srf"):
        plot_map.write_fault_trace(a_input_file, trace_file)
    else:
        plot_map.write_simple_trace(a_input_file, trace_file)
    plot_map.write_simple_stations(a_station_file, simple_station_file)

    # Get hypo_lon, hypo_lat from src/srf file
    hypo_lon, hypo_lat = fault_utils.calculate_epicenter(a_input_file)

    plotregion = [west, east, south, north]
    topo = os.path.join(install.A_PLOT_DATA_DIR, 'calTopo18.bf')
    coastal = os.path.join(install.A_PLOT_DATA_DIR, 'gshhs_h.txt')
    border = os.path.join(install.A_PLOT_DATA_DIR, 'wdb_borders_h.txt')

    # Collect all the data from the residuals file
    all_sta_x_data = []
    all_sta_y_data = []
    all_sta_resid_data = []
    for period in DIST_PERIODS:
        summary_output = os.path.join(
            a_outdir, "%s-%d-resid-map-%.3f-%s.txt" %
            (comp_label, sim_id, period, COMP_EXT_RD50))
        sta_x_data, sta_y_data, sta_resid_data = read_resid(
            resid_file, period, summary_output)
        all_sta_x_data.append(sta_x_data)
        all_sta_y_data.append(sta_y_data)
        all_sta_resid_data.append(sta_resid_data)

    # Now create the map GOF
    outfile = os.path.join(a_outdir,
                           "gof-map-%s-%d-rotd50.png" % (comp_label, sim_id))
    create_map_gof(all_sta_x_data,
                   all_sta_y_data,
                   all_sta_resid_data,
                   plotregion,
                   topo,
                   coastal,
                   border,
                   trace_file,
                   comp_label,
                   sim_id,
                   outfile,
                   hypo_lat=hypo_lat,
                   hypo_lon=hypo_lon)
Exemple #50
0
    def run(self):
        """
        This function prepares the parameter file for BBToolbox, and
        then invokes it
        """
        print("SDSU BBToolBox".center(80, '-'))

        self.install = InstallCfg.getInstance()
        install = self.install
        sim_id = self.sim_id

        # Build path names
        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.bbtoolbox_%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),
                                    "bbtoolbox_%s" % (sta_base))
        a_outdir = os.path.join(install.A_OUT_DATA_DIR, str(sim_id))
        a_param_outdir = os.path.join(a_outdir, "param_files")
        #
        # Make sure the output and two tmp directories exist
        #
        bband_utils.mkdirs([a_tmpdir, a_tmpdir_mod, a_outdir, a_param_outdir],
                           print_cmd=False)

        # Make sure BBToolbox works when starting from a srf file
        if self.r_srcfile:
            a_srcfile = os.path.join(a_indir, self.r_srcfile)
        else:
            a_srcfile = ""
        self.config = BBToolboxCfg(a_srcfile=a_srcfile)
        # Set default parameters
        config = self.config
        # Initialize random number with seed and calculate new iseed
        random.seed(config.SEED)
        self.iseed = int(random.random() * 10000)
        self.fmax = config.FMAX
        self.kappa = config.KAPPA
        self.q_coda = config.Q_CODA
        self.fdec = config.FDEC
        self.source_func = config.SOURCE_FUNC
        self.gs_flag = config.GS_FLAG
        self.ngaw_flag = config.NGAW_FLAG
        self.tr_sca = config.TR_SCA
        self.afac = config.AFAC
        self.bfac = config.BFAC
        self.str_fac = config.STR_FAC

        # Write valid par file, which includes correct references to
        # output dir, velocity model, stations list, fault
        # description, and scattering

        a_stations = os.path.join(a_indir, self.r_stations)

        # Need to create SDSU's BBToolbox files first
        self.create_bbtoolbox_files(a_stations)
        a_stations = os.path.join(a_indir, "bbtstations_%s.dat" % (sta_base))

        parfilename = os.path.join(a_tmpdir_mod, "parfilename_%s" % (sta_base))
        filename_fp = open(parfilename, "w")
        filename_fp.write('"%s"\n' % (os.path.join(a_indir, self.r_bbparfile)))
        filename_fp.flush()
        filename_fp.close()

        # Get list of stations
        stat_file_fp = open(a_stations, "r")
        data = stat_file_fp.readlines()
        stat_file_fp.close()
        for i in range(0, len(data)):
            pieces = data[i].split()
            if len(pieces) > 1:
                if pieces[1] == "X":
                    break
        stat_names = []
        for j in range(i + 1, len(data)):
            pieces = data[j].split()
            stat_names.append(pieces[2])

        # Check if we have stations in our list
        if len(stat_names) == 0:
            # No stations! We should output an error
            raise ValueError("No stations in the station list!")

        # Stagein seismograms to working dir
        for i in range(0, len(stat_names)):
            glob_list = glob.glob("%s/%d.%s*" %
                                  (a_tmpdir, sim_id, stat_names[i]))
            for seismogram_file in glob_list:
                basename = os.path.basename(seismogram_file)
                shutil.copy2(seismogram_file,
                             os.path.join(a_tmpdir_mod, basename))

        # Run in tmpdir subdir to isolate temp fortran files
        os.chdir(a_tmpdir_mod)

        cmd = "%s/BBtoolbox.exe < %s >> %s 2>&1" % (install.A_SDSU_BIN_DIR,
                                                    parfilename, self.log)
        bband_utils.runprog(cmd, abort_on_error=True)

        for i in range(0, len(stat_names)):
            shutil.copy2("%s/BB.%s.hyb" % (a_tmpdir_mod, stat_names[i]),
                         "%s/%d.%s.bbp" % (a_tmpdir, sim_id, stat_names[i]))

        # Change to tmpdir so run.log file is put in tmpdir
        os.chdir(a_tmpdir)

        print("SDSU BBToolBox Completed".center(80, '-'))
Exemple #51
0
    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, '-'))
Exemple #52
0
    print("Usage: %s <command> <options>" % os.path.basename(sys.argv[0]))
    print()
    print("Available commands:")
    print(" plot <bbp_in> <png_out>       - plots bbp_in, generating png_out")
    print(" comp <bbp1> <bbp2> <png_out>  - plots bbp1 and bbp2 into png_out")
    print(" integrate <acc_bbp> <vel_bbp> - acc_bbp -> integration -> vel_bbp")
    print(" diff <vel_bbp> <acc_bbp>      - vel_bbp -> diff -> acc_bbp")
    print()
    sys.exit(0)


# ------------------------------------------------------------------------------
# Main
# ------------------------------------------------------------------------------

INSTALL = InstallCfg.getInstance()

# Check if at least 1 parameter
if len(sys.argv) < 2:
    usage()
# Get command
CMD = sys.argv[1].lower()
if CMD == "plot":
    if len(sys.argv) < 4:
        usage()
    plot(sys.argv[2], sys.argv[3])
elif CMD == "comp":
    if len(sys.argv) < 5:
        usage()
    comp(sys.argv[2], sys.argv[3], sys.argv[4])
elif CMD == "integrate" or CMD == "int":
Exemple #53
0
    sys.exit(1)

# 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
Exemple #54
0
    def run(self):
        """
        Runs the UCSB rupture generator
        """
        print("UCSB Rupture Generator".center(80, '-'))

        #
        # installation directories
        #
        install = InstallCfg.getInstance()
        sim_id = self.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))
        a_outdir = os.path.join(install.A_OUT_DATA_DIR, str(sim_id))
        a_param_outdir = os.path.join(a_outdir, "param_files")
        logfile = os.path.join(install.A_OUT_LOG_DIR, str(sim_id),
                               "%d.ucrmg.log" % (sim_id))
        #
        # Update the Configuration File
        #
        a_srcfile = os.path.join(a_indir, self.r_srcfile)

        cfg = UCrmgCfg(self.vmodel_name, a_srcfile)

        # Make sure the tmp and output directories exist
        bband_utils.mkdirs([a_tmpdir, a_outdir, a_param_outdir],
                           print_cmd=False)

        # Save cwd; change back to it at the end
        old_cwd = os.getcwd()
        os.chdir(a_tmpdir)

        # define location of input velocity model file
        # a_velmodel = os.path.join(a_indir, self.r_velmodel)

        # Create ffsp.inp
        r_ffsp_inp = "ffsp.inp"
        a_ffsp_inp = os.path.join(a_tmpdir, r_ffsp_inp)
        uc_fault_utils.uc_create_ffsp_inp(a_ffsp_inp, a_srcfile,
                                          self.vmodel_name)

        # Copy velocity model to work directory
        shutil.copy2(
            cfg.A_UC_LF_VELMODEL,
            os.path.join(a_tmpdir, os.path.basename(cfg.A_UC_LF_VELMODEL)))

        # Save a copy of the ffsp.inp file
        shutil.copy2(a_ffsp_inp, os.path.join(a_param_outdir, r_ffsp_inp))

        # Now, run the UCSB rupture generator
        cmd = ("%s >> %s 2>&1" % (cfg.A_UC_FFSP_EXE, logfile))
        bband_utils.runprog(cmd)

        # Copy output to indata and outdata directories
        ffsp_output_file = "%s.001" % (cfg.FFSP_OUTPUT_PREFIX)
        shutil.copy2(ffsp_output_file, os.path.join(a_indir, ffsp_output_file))
        shutil.copy2(ffsp_output_file, os.path.join(a_outdir,
                                                    ffsp_output_file))

        os.chdir(old_cwd)

        print("UCSB Rupture Generator Completed".center(80, '-'))
Exemple #55
0
    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, '-'))
Exemple #56
0
    def run(self):
        """
        This function prepares the parameter file for the Irikura
        Recipe, invokes it, and formats its output to be compatible
        with the Broadband Platform
        """
        print("IrikuraHF".center(80, '-'))

        self.install = InstallCfg.getInstance()
        install = self.install
        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.irikura_hf_%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))
        irikura_dir = os.path.join(install.A_TMP_DATA_DIR, str(sim_id),
                                   "irikura_hf_%s" % (sta_base))
        irikura_hor_dir = os.path.join(irikura_dir, "HOR")
        irikura_ver_dir = os.path.join(irikura_dir, "VER")
        a_outdir = os.path.join(install.A_OUT_DATA_DIR, str(sim_id))
        #
        # Make sure the output and two tmp directories exist
        #
        bband_utils.mkdirs([
            a_tmpdir, irikura_dir, a_outdir, irikura_hor_dir, irikura_ver_dir
        ])

        a_velmodel = os.path.join(a_indir, self.r_velmodel)
        a_stations = os.path.join(a_indir, self.r_stations)
        a_srffile = os.path.join(a_indir, self.r_srffile)
        self.stat_list = StationList(a_stations)

        # Read input files, calculate CSM parameters
        self.config = IrikuraHFCfg(
            os.path.join(install.A_IN_DATA_DIR, str(sim_id), self.r_srcfile),
            a_velmodel)

        # Initilize random number generator with seed from src file
        random.seed(self.config.SEED)

        # Create filenames for all intermediate files
        vel_file = os.path.join(irikura_dir, "%d_soil.dat" % (sim_id))
        vel_file_p = os.path.join(irikura_dir, "%d_soil_p.dat" % (sim_id))
        station_file = os.path.join(irikura_dir, "%d_station.dat" % (sim_id))
        fault_param_dat_file = os.path.join(irikura_dir,
                                            "%d_fault_param.dat" % (sim_id))
        elem_param_dat_file = os.path.join(irikura_dir,
                                           "%s_elem_param.dat" % (sim_id))
        srf2grns_input_file = os.path.join(irikura_dir, "input.txt")
        phase_file = os.path.join(irikura_dir, "phase.dat")
        phase2_file = os.path.join(irikura_dir, "phase2.dat")

        # Create Irikura velocity model file
        self.create_velocity_file(vel_file, vel_file_p)

        # Create Irikura station list
        self.create_station_list(station_file)

        # Create other input files
        self.create_irikura_files(fault_param_dat_file)

        # Create phase files
        self.create_phase_files(phase_file, phase2_file)

        # Copy velocity model and srf file to Irikura dir
        shutil.copy2(a_velmodel, os.path.join(irikura_dir, self.r_velmodel))
        shutil.copy2(a_srffile, os.path.join(irikura_dir, self.r_srffile))

        # Irikura binaries
        srf2grns_bin = os.path.join(install.A_IRIKURA_BIN_DIR, "srf2grns")
        statgreen_bin = os.path.join(install.A_IRIKURA_BIN_DIR, "statgreen")
        statgreen2_bin = os.path.join(install.A_IRIKURA_BIN_DIR, "statgreen2")
        greenscale_bin = os.path.join(install.A_IRIKURA_BIN_DIR, "greenscale")

        # Run in tmpdir subdir to isolate files
        # Save cwd, change back to it at the end
        old_cwd = os.getcwd()
        os.chdir(irikura_dir)

        # Run the srf2grns code
        config_file = open(srf2grns_input_file, 'w')
        config_file.write("%s\n" % (self.r_srffile))
        config_file.write("%s\n" % (os.path.basename(elem_param_dat_file)))
        config_file.close()
        cmd = ("%s < %s >> %s 2>&1" %
               (srf2grns_bin, os.path.basename(srf2grns_input_file), self.log))

        bband_utils.runprog(cmd, abort_on_error=True)

        # Run the statgreen program
        os.chdir(irikura_hor_dir)
        cmd = ("%s %s %s %s ../phase.dat >> %s 2>&1" %
               (statgreen_bin, station_file, fault_param_dat_file, vel_file,
                self.log))
        bband_utils.runprog(cmd, abort_on_error=True)

        # Create directories for the AS and SAS files, and move data
        bband_utils.mkdirs([
            os.path.join(irikura_hor_dir, "AS"),
            os.path.join(irikura_hor_dir, "SAS")
        ])
        for fname in glob.iglob(os.path.join(irikura_hor_dir, "AS*.dat")):
            shutil.move(fname, os.path.join(irikura_hor_dir, "AS"))
        for fname in glob.iglob(os.path.join(irikura_hor_dir, "SAS*.dat")):
            shutil.move(fname, os.path.join(irikura_hor_dir, "SAS"))

        # Run statgreen2 (for vertical component)
        os.chdir(irikura_ver_dir)
        cmd = ("%s %s %s %s ../phase2.dat >> %s 2>&1" %
               (statgreen2_bin, station_file, fault_param_dat_file, vel_file_p,
                self.log))
        bband_utils.runprog(cmd, abort_on_error=True)

        # Create directories for the AS and SAS files, and move data
        bband_utils.mkdirs([
            os.path.join(irikura_ver_dir, "AS"),
            os.path.join(irikura_ver_dir, "SAS")
        ])
        for fname in glob.iglob(os.path.join(irikura_ver_dir, "AS*.dat")):
            shutil.move(fname, os.path.join(irikura_ver_dir, "AS"))
        for fname in glob.iglob(os.path.join(irikura_ver_dir, "SAS*.dat")):
            shutil.move(fname, os.path.join(irikura_ver_dir, "SAS"))

        # Now, run the greenscale code for the
        # horizontal and vertical components
        for working_dir in [irikura_hor_dir, irikura_ver_dir]:
            os.chdir(working_dir)

            cmd = ("%s SAS %s %s %s LogGS.dat HP 1.0 0 >> %s 2>&1" %
                   (greenscale_bin, station_file, elem_param_dat_file,
                    fault_param_dat_file, self.log))
            bband_utils.runprog(cmd, abort_on_error=True)

            # Create directories for the output files, move data
            bband_utils.mkdirs([
                os.path.join(working_dir, "acc"),
                os.path.join(working_dir, "vel"),
                os.path.join(working_dir, "velf")
            ])
            for fname in glob.iglob(os.path.join(working_dir, "ac0*.dat")):
                shutil.move(fname, os.path.join(working_dir, "acc"))

            for fname in glob.iglob(os.path.join(working_dir, "ve0*HP*.dat")):
                shutil.move(fname, os.path.join(working_dir, "velf"))
            for fname in glob.iglob(os.path.join(working_dir, "ve0*.dat")):
                shutil.move(fname, os.path.join(working_dir, "vel"))

        # Restore working directory
        os.chdir(old_cwd)

        # Need to copy and re-format output seismograms
        self.process_seismograms(irikura_dir)

        print("IrikuraHF Completed".center(80, '-'))
Exemple #57
0
def main():
    """
    Parse command line options and create the needed files/directories
    """
    # Detect BBP installation
    bbp_install = InstallCfg.getInstance()

    prog_base = os.path.basename(sys.argv[0])
    usage = "usage: %s [options]" % (prog_base)
    parser = optparse.OptionParser(usage)
    parser.add_option("-c", "--codebase", type="string", action="store",
                      dest="codebase",
                      help="Codebase for the simulation: %s" %
                      (CODEBASES))
    parser.add_option("-v", "--velocity-model", type="string", action="store",
                      dest="vmodel",
                      help="Velocity model (region) for this simulation")
    parser.add_option("--src", "--source", type="string", action="store",
                      dest="source",
                      help="Source description file for the simulation")
    parser.add_option("--stl", "--station-list", type="string", action="store",
                      dest="station_list",
                      help="Station list file for the simulation")
    parser.add_option("--srf", "--srf-prefix", type="string", action="store",
                      dest="srf_prefix",
                      help="Prefix of SRF files to use, "
                      "only for GP, SDSU and UCSB methods. "
                      "Simulations begin after the rupture generator.")
    parser.add_option("-d", "--dir", type="string", action="store",
                      dest="simdir",
                      help="Simulation directory")
    parser.add_option("--hypo-rand", action="store_true", dest="hyporand",
                      help="Enables hypocenter randomization")
    parser.add_option("--no-hypo-rand", action="store_false", dest="hyporand",
                      help="Disables hypocenter randomization")
    parser.add_option("-n", "--num-simulations", type="int", action="store",
                      dest="numsim", help="Number of simulations to run")
    parser.add_option("--email", type="string", action="store",
                      dest="email", help="Email for job notifications")
    parser.add_option("-w", "--walltime", type="int", action="store",
                      dest="walltime", help="Number of hours for walltime")
    parser.add_option("--new-nodes", action="store_true", dest="newnodes",
                      help="Schedule the job in the new HPCC nodes")
    parser.add_option("--save-tmpdata", action="store_true", dest="savetemp",
                      help="Save the contents of the tmpdata directory")
    parser.add_option("--hdd-min", type="float",
                      action="store", dest="hdd_min",
                      help="Min value for hypo down dip in randomization")
    parser.add_option("--hdd-max", type="float",
                      action="store", dest="hdd_max",
                      help="Max value for hypo down dip in randomization")
    parser.add_option("--has-min", type="float",
                      action="store", dest="has_min",
                      help="Min value for hypo along strike in randomization")
    parser.add_option("--has-max", type="float",
                      action="store", dest="has_max",
                      help="Max value for hypo along strike in randomization")
    parser.add_option("--only-rup", action="store_true", dest="only_rup",
                      help="Only runs the rupture generator")
    parser.add_option("--var", "--variation", type="int", action="store",
                      dest="variation", help="seed variation (default 1)")
    parser.add_option("--multiseg", action="store_true", dest="multiseg",
                      help="Indicates simulation part of multiseg run")
    parser.add_option("--first-seg-dir", type="string", action="store",
                      dest="first_seg_dir",
                      help="required for multi-segment segments 2..n")
    parser.add_option("-s", "--site", action="store_true",
                      dest="site_response", help="Use site response module")

    (options, _) = parser.parse_args()

    # Check if using new HPCC nodes
    if options.newnodes:
        newnodes = True
    else:
        newnodes = False

    # Check if multi-segment simulation
    if options.multiseg:
        multiseg = True
    else:
        multiseg = False

    # Check for first segment directory
    if options.first_seg_dir is not None:
        first_seg_dir = os.path.abspath(options.first_seg_dir)
        if not os.path.exists(first_seg_dir):
            print("First segment directory for exists: %s" %
                  (first_seg_dir))
            sys.exit(1)
    else:
        first_seg_dir = None
        
    # Check if user specified custom walltime
    if options.walltime:
        if options.walltime < 1:
            print("Walltime must be at least 1 hour!")
            sys.exit(1)
        walltime = options.walltime
    else:
        if newnodes:
            walltime = 24
        else:
            walltime = 300

    # Check for variation sequence
    if options.variation:
        variation = options.variation
    else:
        variation = 1
            
    # Check if user wants to save the contents of tmpdata
    if options.savetemp:
        savetemp = True
    else:
        savetemp = False

    # Check if user wants to only run the rupture generator
    if options.only_rup:
        only_rup = True
    else:
        only_rup = False

    # Validate codebase to use
    codebase = options.codebase
    if codebase is None:
        print "Please specify a codebase!"
        sys.exit(1)
    codebase = codebase.lower()
    if codebase not in CODEBASES:
        print "Codebase needs to be one of: %s" % (CODEBASES)

    # Check for velocity model
    vmodel_names = velocity_models.get_all_names()
    vmodel = options.vmodel
    if vmodel is None:
        print "Please provide a velocity model (region) for this simulation!"
        print "Available options are: %s" % (vmodel_names)
        sys.exit(1)
    vmodels = [v_model.lower() for v_model in vmodel_names]
    if vmodel.lower() not in vmodels:
        print ("Velocity model %s does not appear to be available on BBP" %
               (vmodel))
        print ("Available options are: %s" % (vmodel_names))
        print "Please provide another velocity model or check your BBP installation."
        sys.exit(1)
    # Now get the name with the correct case
    vmodel = vmodel_names[vmodels.index(vmodel.lower())]

    # Check if users wants to run site response module
    if options.site_response:
        site_response = True
        if codebase not in CODEBASES_SITE:
            print "Cannot use site response with method: %s" % (codebase)
            sys.exit(1)
    else:
        site_response = False
    
    # Check for hypocenter randomization
    if options.hyporand is None:
        print "Please specify --hypo-rand or --no-hypo-rand!"
        sys.exit(1)

    if options.hyporand:
        hypo_rand = True
    else:
        hypo_rand = False

    # Define area where hypocenter will be randomized
    hypo_area = {}
    hypo_area["hdd_min"] = options.hdd_min
    hypo_area["hdd_max"] = options.hdd_max
    hypo_area["has_min"] = options.has_min
    hypo_area["has_max"] = options.has_max

    # Get the source file (SRC or SRFs)
    source_file = options.source
    srf_prefix = options.srf_prefix

    if source_file is None and srf_prefix is None:
        print ("Please provide either source description (src file) "
               "or a srf prefix!")
        sys.exit(1)
    # If user specified both a source file and a srf prefix, we abort!
    if source_file is not None and srf_prefix is not None:
        print "Cannot specify both srf_prefic and source_file!"
        sys.exit(1)
    # If user specified a source file
    if source_file is not None:
        # Make it a full path
        source_file = os.path.realpath(source_file)
        # Make sure source file is in the rcf-104 / scec-00 filesystem
        if not "rcf-104" in source_file and not "scec-00" in source_file:
            print "Source file should be in the rcf-104 / scec-00 filesystems!"
            sys.exit(1)
        # Make sure source file exists and is readable
            if (not os.path.isfile(source_file) or
                not os.access(source_file, os.R_OK)):
                print "Source file does not seem to be accessible!"
                sys.exit(1)
        # Create a prefix
        prefix = ("%s-%s" %
                  (os.path.splitext(os.path.basename(source_file))[0],
                   codebase.lower()))
    # If user specified a SRF prefix
    if srf_prefix is not None:
        # Make it a full path
        srf_prefix = os.path.realpath(srf_prefix)
        # Make sure source file is in the rcf-104 or scec-00 filesystems
        if not "rcf-104" in srf_prefix and not "scec-00" in srf_prefix:
            print "SRF files should be in the rcf-104 / scec-00 filesystems!"
            sys.exit(1)
        # Create a prefix
        prefix = os.path.splitext(os.path.basename(srf_prefix))[0]
        #prefix = prefix.rsplit("-", 1)[0]
    # Make sure we remove spaces from prefix
    prefix = prefix.replace(" ", '')

    # Get the station list
    station_list = options.station_list
    if station_list is None:
        print "Please provide a station list (stl file)!"
        sys.exit(1)
    # Make it a full path
    station_list = os.path.realpath(station_list)
    # Make sure station list is in the rcf-104 or scec-00 filesystems
    if not "rcf-104" in station_list and not "scec-00" in station_list:
        print "Station list should be in the rcf-104 / scec-00 filesystems!"
        sys.exit(1)
    # Make sure station list exists and is readable
    if (not os.path.isfile(station_list) or
        not os.access(station_list, os.R_OK)):
        print "Station list foes not seem to be accessible!"
        sys.exit(1)

    # Check for the simulation directory
    simdir = options.simdir
    if simdir is None:
        print "Please provide a simulation directory!"
        sys.exit(1)
    simdir = os.path.abspath(simdir)
    if os.path.exists(simdir):
        print "Simulation directory exists: %s" % (simdir)
        opt = raw_input("Do you want to delete its contents (y/n)? ")
        if opt.lower() != "y":
            print "Please provide another simulation directory!"
            sys.exit(1)
        opt = raw_input("ARE YOU SURE (y/n)? ")
        if opt.lower() != "y":
            print "Please provide another simulation directory!"
            sys.exit(1)
        # Delete existing directory (we already asked the user twice!!!)
        shutil.rmtree(simdir)

    # Pick up number of simulations to run
    numsim = options.numsim
    if numsim < 1 or numsim > MAX_SIMULATIONS:
        print ("Number of simulations should be between 1 and %d" %
               (MAX_SIMULATIONS))
        sys.exit(1)

    # Check for e-mail address
    email = options.email
    if email is None:
        print "Please provide an e-mail address for job notifications"
        sys.exit(1)

    # Make sure user has configured the setup_bbp_env.sh script
    setup_bbp_env = os.path.join(bbp_install.A_INSTALL_ROOT,
                                 "utils/batch/setup_bbp_env.sh")
    if not os.path.exists(setup_bbp_env):
        print ("Cannot find setup_bbp_env.sh script!")
        print ("Expected at: %s" % (setup_bbp_env))
        sys.exit(1)
    # Create simulation directories
    os.makedirs(simdir)
    indir = os.path.join(simdir, "Sims", "indata")
    outdir = os.path.join(simdir, "Sims", "outdata")
    tmpdir = os.path.join(simdir, "Sims", "tmpdata")
    logsdir = os.path.join(simdir, "Sims", "logs")
    xmldir = os.path.join(simdir, "Xml")
    srcdir = os.path.join(simdir, "Src")
    for mdir in [indir, outdir, tmpdir, logsdir, xmldir, srcdir]:
        os.makedirs(mdir)
    if srf_prefix is None:
        # Generate source files
        generate_src_files(numsim, source_file, srcdir,
                           prefix, hypo_rand, hypo_area,
                           variation, multiseg, first_seg_dir)
    # Generate xml files
    generate_xml(bbp_install, numsim, srcdir, xmldir,
                 logsdir, vmodel, codebase, prefix,
                 station_list, only_rup, srf_prefix,
                 site_response)
    # Write pbs file
    write_pbs(bbp_install, numsim, simdir, xmldir,
              email, prefix, newnodes, walltime, savetemp)

    # Write .info file
    info_file = open(os.path.join(simdir, "%s.info" % (prefix)), 'w')
    info_file.write("# %s\n" % (" ".join(sys.argv)))
    info_file.close()
Exemple #58
0
    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
        hf_seis = None

        # Find one LF seismogram and one HF 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))
                if os.path.exists(
                        os.path.join(a_tmpdir,
                                     "%d.%s-hf%s" % (sim_id, site, seis_ext))):
                    hf_seis = os.path.join(
                        a_tmpdir, "%d.%s-hf%s" % (sim_id, site, seis_ext))
                    break

        # Need one of each
        if lf_seis is None:
            raise bband_utils.ParameterError("Cannot find a LF seismogram")
        if hf_seis is None:
            raise bband_utils.ParameterError("Cannot find a HF seismogram")

        # Pick DT from these files
        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!")

        hf_dt = None
        hf_file = open(hf_seis)
        for line in hf_file:
            line = line.strip()
            if line.startswith("#") or line.startswith("%"):
                continue
            # Got to first timestamp. Now, pick two consecutive
            # timestamps values
            hf_t1 = float(line.strip().split()[0])
            hf_t2 = float(hf_file.next().strip().split()[0])
            # Subtract the two times
            hf_dt = hf_t2 - hf_t1
            # All done!
            break
        hf_file.close()

        if hf_dt is None:
            raise bband_utils.ParameterError("Cannot find HF_DT!")

        # 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.

        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)
                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, '-'))
Exemple #59
0
    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, '-'))
Exemple #60
0
    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",
                                           "anderson_gof")

        # Create directories
        bband_utils.mkdirs([
            a_indir, a_tmpdir, a_tmpdir_seis, a_outdir, a_logdir,
            a_validation_outdir
        ],
                           print_cmd=False)

        # Copy station and correction files
        cmd = "cp %s %s" % (os.path.join(self.install.A_TEST_REF_DIR,
                                         "anderson_gof",
                                         self.stations), a_indir)
        bband_utils.runprog(cmd, print_cmd=False)

        # 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,
                                        "anderson_gof", "syn_seis",
                                        "%s.acc.bbp" % (station))
            src_sims_rd50 = os.path.join(self.install.A_TEST_REF_DIR,
                                         "anderson_gof", "syn_seis",
                                         "%s.rd50" % (station))
            dst_sims_acc = os.path.join(a_outdir,
                                        "%d.%s.acc.bbp" % (sim_id, station))
            dst_sims_rd50 = os.path.join(a_outdir,
                                         "%d.%s.rd50" % (sim_id, station))
            src_obs_acc = os.path.join(self.install.A_TEST_REF_DIR,
                                       "anderson_gof", "obs_seis",
                                       "%s.bbp" % (station))
            src_obs_rd50 = os.path.join(self.install.A_TEST_REF_DIR,
                                        "anderson_gof", "obs_seis",
                                        "%s.rd50" % (station))
            dst_obs_acc = os.path.join(a_tmpdir_seis, "%s.bbp" % (station))
            dst_obs_rd50 = os.path.join(a_tmpdir_seis, "%s.rd50" % (station))

            cmd = "cp %s %s" % (src_sims_acc, dst_sims_acc)
            bband_utils.runprog(cmd, print_cmd=False)

            cmd = "cp %s %s" % (src_sims_rd50, dst_sims_rd50)
            bband_utils.runprog(cmd, print_cmd=False)

            cmd = "cp %s %s" % (src_obs_acc, dst_obs_acc)
            bband_utils.runprog(cmd, print_cmd=False)

            cmd = "cp %s %s" % (src_obs_rd50, dst_obs_rd50)
            bband_utils.runprog(cmd, print_cmd=False)