Beispiel #1
0
    def summarize_rotd50(self, site_list, a_outdir, a_outdir_gmpe):
        """
        Summarizes all rotd50 data and creates the rotd50 GOF plot
        """
        sim_id = self.sim_id
        install = self.install
        config = self.config

        rd50_residfile = os.path.join(
            a_outdir, "%s-%d.rd50-resid.txt" % (self.comp_label, sim_id))
        for comp in config.COMPS_PSA5:
            # Build paths and check lengths
            fileroot = os.path.join(
                a_outdir, "%s-%d_r%d-%d-rd50-%s" %
                (self.comp_label, sim_id, config.MIN_CDST, self.max_cutoff,
                 comp))
            bband_utils.check_path_lengths([rd50_residfile, fileroot],
                                           bband_utils.GP_MAX_FILENAME)

            cmd = ("%s/resid2uncer_varN " % (install.A_GP_BIN_DIR) +
                   "residfile=%s fileroot=%s " % (rd50_residfile, fileroot) +
                   "comp=%s nstat=%d nper=63 " % (comp, len(site_list)) +
                   "min_cdst=%d max_cdst=%d >> %s 2>&1" %
                   (config.MIN_CDST, self.max_cutoff, self.log))
            bband_utils.runprog(cmd, abort_on_error=True, print_cmd=False)

        # Plot GOF for psa5/rotd50 data
        if self.single_component:
            plot_mode = 'rd50-single'
        else:
            plot_mode = 'rd50'
        fileroot = '%s-%d_r0-%d-rd50' % (self.comp_label, sim_id,
                                         self.max_cutoff)
        plottitle = ("GOF Comparison between %s and simulation %d" %
                     (self.comp_label, sim_id))
        plotter = PlotGoF()
        plotter.plot(plottitle,
                     fileroot,
                     a_outdir,
                     a_outdir,
                     cutoff=self.max_cutoff,
                     mode=plot_mode,
                     colorset='single')

        # Finally, plot the distance and map GOFs
        plot_dist_gof(rd50_residfile,
                      self.comp_label,
                      a_outdir_gmpe,
                      sim_id=self.sim_id)
        plot_map_gof(self.r_srcfile,
                     self.r_stations,
                     rd50_residfile,
                     self.comp_label,
                     sim_id=self.sim_id)
Beispiel #2
0
def summarize_rotd50(tmpdir, outdir, combined_resid_file, comp_label,
                     num_stations, num_realization, codebase):
    """
    This function summarizes all rotd50 data and creates the combined
    rotd50 GOF plot
    """
    config = GPGofCfg()

    # Figure out where out binaries are
    if "BBP_DIR" in os.environ:
        install_root = os.path.normpath(os.environ["BBP_DIR"])
    else:
        raise bband_utils.ProcessingError("BBP_DIR is not set!")
    gp_bin_dir = os.path.join(install_root, "src", "gp", "bin")

    logfile = os.path.join(tmpdir, "log.txt")

    for comp in config.COMPS_PSA5:
        # Build paths and check lengths
        fileroot = os.path.join(
            tmpdir, "%s-%s-combined-rd50-%s" % (codebase, comp_label, comp))
        bband_utils.check_path_lengths([combined_resid_file, fileroot],
                                       bband_utils.GP_MAX_FILENAME)

        cmd = ("%s " % (os.path.join(gp_bin_dir, "resid2uncer_varN")) +
               "residfile=%s fileroot=%s " % (combined_resid_file, fileroot) +
               "comp=%s nstat=%d nper=63 " % (comp, num_stations) +
               " >> %s 2>&1" % (logfile))
        bband_utils.runprog(cmd, abort_on_error=True)

    plottitle = ("Combined GOF Plot for %s\n%d Realizations - %s Method" %
                 (comp_label, num_realization, codebase.upper()))
    fileroot = "%s-%s-combined-rd50" % (codebase, comp_label)
    plotter = PlotGoF()
    plotter.plot(plottitle,
                 fileroot,
                 tmpdir,
                 outdir,
                 cutoff=0,
                 mode="rd50-single",
                 colorset="combined")

    print("Stations used: %s" % (num_stations))
Beispiel #3
0
    def generate_plot(self, a_statfile, a_dstdir):
        """
        This function generates the bias plot with ratio of maximum to
        median response across orientations (RotD100/RotD50)
        """
        install = install_cfg.InstallCfg.getInstance()
        sim_id = self.sim_id
        slo = StationList(a_statfile)
        site_list = slo.getStationList()

        rd100_resid_output = os.path.join(
            a_dstdir, "%s-%d-resid-rd100.txt" % (self.comp_label, sim_id))
        for comp in ['rotd50', 'rotd100', 'ratio']:
            # Build paths and check lengths
            fileroot = os.path.join(
                a_dstdir, "%s-%d_r0-%d-rd100-%s" %
                (self.comp_label, sim_id, self.max_cutoff, comp))
            bband_utils.check_path_lengths([rd100_resid_output, fileroot],
                                           bband_utils.GP_MAX_FILENAME)

            cmd = ("%s " %
                   (os.path.join(install.A_GP_BIN_DIR, "resid2uncer_varN")) +
                   "residfile=%s fileroot=%s " %
                   (rd100_resid_output, fileroot) +
                   "comp=%s nstat=%d nper=63 " % (comp, len(site_list)) +
                   "min_cdst=%d max_cdst=%d >> %s 2>&1" %
                   (0, self.max_cutoff, self.log))
            bband_utils.runprog(cmd, abort_on_error=True, print_cmd=False)

        # Generate bias plot
        plot_mode = 'rd100'
        fileroot = ("%s-%d_r0-%d-rd100" %
                    (self.comp_label, sim_id, self.max_cutoff))
        plottitle = ("GOF Comparison between %s and simulation %d" %
                     (self.comp_label, sim_id))
        plotter = PlotGoF()
        plotter.plot(plottitle,
                     fileroot,
                     a_dstdir,
                     a_dstdir,
                     cutoff=self.max_cutoff,
                     mode=plot_mode,
                     colorset='single')
Beispiel #4
0
    def summarize_rotd50(self, site_list, a_outdir, a_outdir_gmpe):
        """
        Summarizes all rotd50 data and creates the rotd50 GOF plot
        """
        sim_id = self.sim_id
        install = self.install
        config = self.config

        rd50_residfile = os.path.join(a_outdir, "%s-%d.rd50-resid.txt" %
                                      (self.comp_label, sim_id))
        for comp in config.COMPS_PSA5:
            # Build paths and check lengths
            fileroot = os.path.join(a_outdir, "%s-%d_r%d-%d-rd50-%s" %
                                    (self.comp_label, sim_id, config.MIN_CDST,
                                     self.max_cutoff, comp))
            bband_utils.check_path_lengths([rd50_residfile, fileroot],
                                           bband_utils.GP_MAX_FILENAME)

            cmd = ("%s/resid2uncer_varN " % (install.A_GP_BIN_DIR) +
                   "residfile=%s fileroot=%s " % (rd50_residfile, fileroot) +
                   "comp=%s nstat=%d nper=63 " % (comp, len(site_list)) +
                   "min_cdst=%d max_cdst=%d >> %s 2>&1" %
                   (config.MIN_CDST, self.max_cutoff, self.log))
            bband_utils.runprog(cmd, abort_on_error=True, print_cmd=False)

        # Plot GOF for psa5/rotd50 data
        if self.single_component:
            plot_mode = 'rd50-single'
        else:
            plot_mode = 'rd50'
        fileroot = '%s-%d_r0-%d-rd50' % (self.comp_label, sim_id, self.max_cutoff)
        plottitle = ("GOF Comparison between %s and simulation %d" %
                     (self.comp_label, sim_id))
        plotter = PlotGoF()
        plotter.plot(plottitle, fileroot, a_outdir, a_outdir,
                     cutoff=self.max_cutoff, mode=plot_mode, colorset='single')

        # Finally, plot the distance and map GOFs
        plot_dist_gof(rd50_residfile, self.comp_label,
                      a_outdir_gmpe, sim_id=self.sim_id)
        plot_map_gof(self.r_srcfile, self.r_stations, rd50_residfile,
                     self.comp_label, sim_id=self.sim_id)
Beispiel #5
0
    def generate_plot(self, a_statfile, a_dstdir):
        """
        This function generates the bias plot with ratio of maximum to
        median response across orientations (RotD100/RotD50)
        """
        install = install_cfg.InstallCfg.getInstance()
        sim_id = self.sim_id
        slo = StationList(a_statfile)
        site_list = slo.getStationList()

        rd100_resid_output = os.path.join(a_dstdir, "%s-%d-resid-rd100.txt" %
                                          (self.comp_label, sim_id))
        for comp in ['rotd50', 'rotd100', 'ratio']:
            # Build paths and check lengths
            fileroot = os.path.join(a_dstdir, "%s-%d_r0-%d-rd100-%s" %
                                    (self.comp_label, sim_id,
                                     self.max_cutoff, comp))
            bband_utils.check_path_lengths([rd100_resid_output, fileroot],
                                           bband_utils.GP_MAX_FILENAME)

            cmd = ("%s " % (os.path.join(install.A_GP_BIN_DIR,
                                         "resid2uncer_varN")) +
                   "residfile=%s fileroot=%s " %
                   (rd100_resid_output, fileroot) +
                   "comp=%s nstat=%d nper=63 " %
                   (comp, len(site_list)) +
                   "min_cdst=%d max_cdst=%d >> %s 2>&1" %
                   (0, self.max_cutoff, self.log))
            bband_utils.runprog(cmd, abort_on_error=True, print_cmd=False)

        # Generate bias plot
        plot_mode = 'rd100'
        fileroot = ("%s-%d_r0-%d-rd100" %
                    (self.comp_label, sim_id,
                     self.max_cutoff))
        plottitle = ("GOF Comparison between %s and simulation %d" %
                     (self.comp_label, sim_id))
        plotter = PlotGoF()
        plotter.plot(plottitle, fileroot, a_dstdir, a_dstdir,
                     cutoff=self.max_cutoff, mode=plot_mode, colorset='single')
Beispiel #6
0
def summarize_rotd50(tmpdir, outdir, combined_resid_file,
                     comp_label, num_stations, num_realization,
                     codebase):
    """
    This function summarizes all rotd50 data and creates the combined
    rotd50 GOF plot
    """
    config = GPGofCfg()

    # Figure out where out binaries are
    if "BBP_DIR" in os.environ:
        install_root = os.path.normpath(os.environ["BBP_DIR"])
    else:
        raise bband_utils.ProcessingError("BBP_DIR is not set!")
    gp_bin_dir = os.path.join(install_root, "src", "gp", "bin")

    logfile = os.path.join(tmpdir, "log.txt")

    for comp in config.COMPS_PSA5:
        # Build paths and check lengths
        fileroot = os.path.join(tmpdir, "%s-%s-combined-rd50-%s" %
                                (codebase, comp_label, comp))
        bband_utils.check_path_lengths([combined_resid_file, fileroot],
                                       bband_utils.GP_MAX_FILENAME)

        cmd = ("%s/resid2uncer_varN " % (gp_bin_dir) + 
               "residfile=%s fileroot=%s " % (combined_resid_file, fileroot) +
               "comp=%s nstat=%d nper=63 " % (comp, num_stations) +
               " >> %s 2>&1" % (logfile))
        bband_utils.runprog(cmd, abort_on_error=True)

    plottitle = ("Combined GOF Plot for %s\n%d Realizations\n%s Method" %
                 (comp_label, num_realization, codebase.upper()))
    fileroot = "%s-%s-combined-rd50" % (codebase, comp_label)
    plotter = PlotGoF()
    plotter.plot(plottitle, fileroot, tmpdir, outdir,
                 cutoff=0, mode="rd50-single", colorset="combined")

    print "Stations used: %s" % (num_stations)
Beispiel #7
0
    def create_vel_bbp(self, stat):
        """
        This function derives a velocity bbp file from an acceleration
        file
        """
        install = self.install
        sim_id = self.sim_id
        a_tmpdir = os.path.join(install.A_TMP_DATA_DIR, str(sim_id))
        in_acc_file = os.path.join(a_tmpdir,
                                   "%d.%s-hf.acc.bbp" % (sim_id, stat))
        out_vel_file = os.path.join(a_tmpdir, "%d.%s-hf.bbp" % (sim_id, stat))

        # Since we have acceleration files, we need to integrate to
        # get to velocity

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

        cmd = ("%s" % (os.path.join(install.A_GP_BIN_DIR, "wcc2bbp")) +
               " wcc2bbp=0 nsfile=%s ewfile=%s udfile=%s < %s >> %s 2>&1" %
               (nsfile, ewfile, udfile, in_acc_file, self.log))
        bband_utils.runprog(cmd, abort_on_error=True)
        # Now we need to integrate to get to velocity
        for comp in ['000', '090', 'ver']:
            file_in = os.path.join(a_tmpdir,
                                   "%d.%s-hf.acc.%s" % (sim_id, stat, comp))
            file_out = os.path.join(a_tmpdir,
                                    "%s.%s-hf.vel.%s" % (sim_id, stat, comp))
            bband_utils.check_path_lengths([file_in, file_out],
                                           bband_utils.GP_MAX_FILENAME)
            cmd = ("%s" % (os.path.join(install.A_GP_BIN_DIR, "integ_diff")) +
                   " integ=1 filein=%s fileout=%s" % (file_in, file_out))
            bband_utils.runprog(cmd, abort_on_error=True)
        # Now we put together the file again as a velocity bbp file
        nsfile = os.path.join(a_tmpdir, "%d.%s-hf.vel.000" % (sim_id, stat))
        ewfile = os.path.join(a_tmpdir, "%d.%s-hf.vel.090" % (sim_id, stat))
        udfile = os.path.join(a_tmpdir, "%d.%s-hf.vel.ver" % (sim_id, stat))
        bband_utils.check_path_lengths([nsfile, ewfile, udfile],
                                       bband_utils.GP_MAX_FILENAME)

        cmd = ("%s" % (os.path.join(install.A_GP_BIN_DIR, "wcc2bbp")) +
               " wcc2bbp=1 nsfile=%s ewfile=%s udfile=%s > %s 2>> %s" %
               (nsfile, ewfile, udfile, out_vel_file, self.log))
        bband_utils.runprog(cmd, abort_on_error=True)
Beispiel #8
0
    def run(self):
        print("RotD50".center(80, '-'))
        #
        # convert input bbp acc files to peer format acc files
        #

        install = install_cfg.InstallCfg.getInstance()
        sim_id = self.sim_id
        sta_base = os.path.basename(os.path.splitext(self.r_stations)[0])
        self.log = os.path.join(install.A_OUT_LOG_DIR, str(sim_id),
                                "%d.rotd50_%s.log" % (sim_id, sta_base))
        a_statfile = os.path.join(install.A_IN_DATA_DIR, str(sim_id),
                                  self.r_stations)
        a_tmpdir = os.path.join(install.A_TMP_DATA_DIR, str(sim_id))
        a_outdir = os.path.join(install.A_OUT_DATA_DIR, str(sim_id))

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

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

        for site in site_list:
            stat = site.scode
            print("==> Processing station: %s" % (stat))

            # Since we have velocity files, we need to differentiate
            # to get to acceleration

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

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

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

            for c in ["090", "000", "ver"]:
                # Differentiate to get from velocity to accl needed by rotd50
                # Create path names and check if their sizes are within bounds
                filein = os.path.join(a_tmpdir, "%d.%s.%s" % (sim_id, stat, c))
                fileout = os.path.join(a_tmpdir,
                                       "%d.%s.acc.%s" % (sim_id, stat, c))

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

                cmd = ("%s/integ_diff diff=1 " % (install.A_GP_BIN_DIR) +
                       "filein=%s fileout=%s >> %s 2>&1" %
                       (filein, fileout, self.log))
                bband_utils.runprog(cmd, abort_on_error=True, print_cmd=False)

                # Check file length
                bband_utils.check_path_lengths(
                    ["%s/%d.%s.acc.%s" % (a_tmpdir, sim_id, stat, c)],
                    bband_utils.GP_MAX_FILENAME)

            # Now we need to convert them back to bbp
            # Create path names and check if their sizes are
            # within bounds
            nsfile = os.path.join(a_tmpdir, "%d.%s.acc.000" % (sim_id, stat))
            ewfile = os.path.join(a_tmpdir, "%d.%s.acc.090" % (sim_id, stat))
            udfile = os.path.join(a_tmpdir, "%d.%s.acc.ver" % (sim_id, stat))
            bbpfile = os.path.join(a_tmpdir, "%d.%s.acc.bbp" % (sim_id, stat))

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

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

            # Now we need to convert to peer format
            out_n_acc = os.path.join(a_tmpdir,
                                     "%d.%s.peer_n.acc" % (sim_id, stat))
            out_e_acc = os.path.join(a_tmpdir,
                                     "%d.%s.peer_e.acc" % (sim_id, stat))
            out_z_acc = os.path.join(a_tmpdir,
                                     "%d.%s.peer_z.acc" % (sim_id, stat))
            bbp_formatter.bbp2peer(bbpfile, out_n_acc, out_e_acc, out_z_acc)

            # Let's have rotD50 create these output files
            out_rotd50_base = "%d.%s.rd50" % (sim_id, stat)
            tmp_rotd50 = os.path.join(a_tmpdir, out_rotd50_base)
            out_rotd50 = os.path.join(a_outdir, out_rotd50_base)

            # Run the rotD50 program
            self.do_rotd50(a_tmpdir, out_e_acc, out_n_acc, out_z_acc,
                           out_rotd50, self.log)

            cmd = "cp %s %s" % (tmp_rotd50, out_rotd50)
            bband_utils.runprog(cmd, abort_on_error=True, print_cmd=False)

        # All done!
        print("RotD50 Completed".center(80, '-'))
Beispiel #9
0
    def run(self):
        """
        Go through the station list and create acceleration
        seismogram. Copy results to outdata directory
        """
        print("Copy Seismograms".center(80, '-'))

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

        sta_base = os.path.basename(os.path.splitext(self.r_stations)[0])
        self.log = os.path.join(install.A_OUT_LOG_DIR, str(sim_id),
                                "%d.copy_seis_%s.log" % (sim_id, sta_base))
        a_statfile = os.path.join(install.A_IN_DATA_DIR, str(sim_id),
                                  self.r_stations)

        a_tmpdir = os.path.join(install.A_TMP_DATA_DIR, str(sim_id))
        a_outdir = os.path.join(install.A_OUT_DATA_DIR, str(sim_id))

        # Make sure tmpdir, outdir exist
        dirs = [a_tmpdir, a_outdir]
        bband_utils.mkdirs(dirs, print_cmd=False)

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

        for sits in site_list:
            site = sits.scode
            print("==> Processing station: %s" % (site))

            if self.hybrid:
                expected_file = "%d.%s.bbp" % (sim_id, site)
            else:
                expected_file = "%d.%s.vel.bbp" % (sim_id, site)

            #print("Processing velocity for station %s - %s" %
            #      (site, expected_file))
            bbpfile = os.path.join(a_tmpdir, expected_file)

            # Make sure velocity file is there, otherwise, skip this station
            if not os.path.exists(bbpfile):
                print("No velocity seismograms found for station %s" % (site))
                print("Skipping this station...")
                continue

            # Copy velocity bbp file to outdir
            shutil.copy2(
                bbpfile,
                os.path.join(a_outdir, "%d.%s.vel.bbp" % (sim_id, site)))

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

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

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

            for comp in ['000', '090', 'ver']:
                # Create path names and check if their sizes are
                # within bounds
                filein = os.path.join(a_tmpdir,
                                      "%d.%s.%s" % (sim_id, site, comp))
                fileout = os.path.join(a_tmpdir,
                                       "%d.%s.acc.%s" % (sim_id, site, comp))

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

                cmd = ("%s/integ_diff diff=1 filein=%s fileout=%s" %
                       (install.A_GP_BIN_DIR, filein, fileout))
                bband_utils.runprog(cmd, abort_on_error=True, print_cmd=False)

            # Create path names and check if their sizes are within bounds
            nsfile = os.path.join(a_tmpdir, "%d.%s.acc.000" % (sim_id, site))
            ewfile = os.path.join(a_tmpdir, "%d.%s.acc.090" % (sim_id, site))
            udfile = os.path.join(a_tmpdir, "%d.%s.acc.ver" % (sim_id, site))
            bbpfile = os.path.join(a_tmpdir, "%d.%s.acc.bbp" % (sim_id, site))

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

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

            # Copy acceleration bbp file to outdir
            shutil.copy2(
                bbpfile,
                os.path.join(a_outdir, "%d.%s.acc.bbp" % (sim_id, site)))

        print("Copy Seismograms Completed".center(80, '-'))
Beispiel #10
0
    def process_separate_seismograms(self, site, sta_base, vs30, a_tmpdir):
        """
        Runs the site response module for separate low and high
        frequency seismograms
        """
        sim_id = self.sim_id
        config = self.config
        install = self.install

        # Run HF and LF components
        for freq in ['hf', 'lf']:
            print("**** Processing %s component..." % (freq))
            # Create path names and check if their sizes are
            # within bounds
            nsfile = os.path.join(a_tmpdir,
                                  "temp_%s.%s.000" % (sta_base, freq))
            ewfile = os.path.join(a_tmpdir,
                                  "temp_%s.%s.090" % (sta_base, freq))
            udfile = os.path.join(a_tmpdir,
                                  "temp_%s.%s.ver" % (sta_base, freq))
            bbpfile = os.path.join(a_tmpdir,
                                   "%d.%s-%s.bbp" % (sim_id, site, freq))
            bband_utils.check_path_lengths([nsfile, ewfile, udfile],
                                           bband_utils.GP_MAX_FILENAME)

            # Convert bbp files to 3 wcc files
            progstring = ("%s nsfile=%s ewfile=%s udfile=%s " % (os.path.join(
                install.A_GP_BIN_DIR, "wcc2bbp"), nsfile, ewfile, udfile) +
                          "wcc2bbp=0 < %s >> %s 2>&1" % (bbpfile, self.log))
            bband_utils.runprog(progstring,
                                abort_on_error=True,
                                print_cmd=False)

            for entries in config.COMPS:
                compo = entries

                # Create path names and check if their sizes
                # are within bounds
                filein = os.path.join(
                    a_tmpdir, "temp_%s.%s.%s" % (sta_base, freq, compo))
                fileout = os.path.join(
                    a_tmpdir, "temp_%s.acc.%s.%s" % (sta_base, freq, compo))
                bband_utils.check_path_lengths([filein, fileout],
                                               bband_utils.GP_MAX_FILENAME)

                # Convert to acceleration
                cmd = ("%s diff=1 " %
                       (os.path.join(install.A_GP_BIN_DIR, "integ_diff")) +
                       "filein=%s fileout=%s >> %s 2>&1" %
                       (filein, fileout, self.log))
                bband_utils.runprog(cmd, abort_on_error=True, print_cmd=False)

                # Get PGA for component from high frequency ONLY
                pga_filename = os.path.join(a_tmpdir,
                                            "%s.%s.pga.txt" % (site, compo))

                # No need to check path lengths here
                progstring = (
                    "%s < " %
                    (os.path.join(install.A_GP_BIN_DIR, "wcc_getpeak")) +
                    "%s/temp_%s.acc.hf.%s > %s 2>> %s" %
                    (a_tmpdir, sta_base, compo, pga_filename, self.log))
                # wcc_getpeak returns 33 even when it succeeds
                bband_utils.runprog(progstring, print_cmd=False)
                pga_file = open(pga_filename, "r")
                data = pga_file.readlines()
                pga_file.close()
                pga = float(data[0].split()[1]) / 981.0
                if freq == 'lf':
                    vref = config.LF_VREF
                else:
                    vref = config.HF_VREF

                # Create path names and check if their sizes
                # are within bounds
                filein = os.path.join(
                    a_tmpdir, "temp_%s.acc.%s.%s" % (sta_base, freq, compo))
                fileout = os.path.join(
                    a_tmpdir,
                    "temp_%s.acc.amp.%s.%s" % (sta_base, freq, compo))
                bband_utils.check_path_lengths([filein, fileout],
                                               bband_utils.GP_MAX_FILENAME)

                # Pick the right model to use
                site_amp_model = config.SITEAMP_MODEL

                # Now, run site amplification
                progstring = ("%s pga=%f vref=%d " % (os.path.join(
                    install.A_GP_BIN_DIR, "wcc_siteamp14"), pga, vref) +
                              'vsite=%d model="%s" vpga=%d ' %
                              (vs30, site_amp_model, config.HF_VREF) +
                              'flowcap=%f infile=%s outfile=%s ' %
                              (config.FLOWCAP, filein, fileout) +
                              'fmax=%f fhightop=%f ' %
                              (config.FMAX, config.FHIGHTOP) +
                              "fmidbot=%s fmin=%s >> %s 2>&1" %
                              (config.FMIDBOT, config.FMIN, self.log))
                bband_utils.runprog(progstring, abort_on_error=True)

                # Output becomes input
                filein = fileout
                fileout = os.path.join(
                    a_tmpdir,
                    "temp_%s.vel.amp.%s.%s" % (sta_base, freq, compo))
                bband_utils.check_path_lengths([fileout],
                                               bband_utils.GP_MAX_FILENAME)
                # Now integrate to get velocity
                cmd = ("%s integ=1 " %
                       (os.path.join(install.A_GP_BIN_DIR, "integ_diff")) +
                       "filein=%s fileout=%s >> %s 2>&1" %
                       (filein, fileout, self.log))
                bband_utils.runprog(cmd, abort_on_error=True, print_cmd=False)

            # Do one time for acceleration

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

            progstring = ("%s wcc2bbp=1 units=cm/s/s " %
                          (os.path.join(install.A_GP_BIN_DIR, "wcc2bbp")) +
                          'title="%s Sim NGAH, stat=%s" ' % (freq, 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)

            # Do it again for velocity

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

            progstring = ("%s wcc2bbp=1 units=cm/s " %
                          (os.path.join(install.A_GP_BIN_DIR, "wcc2bbp")) +
                          'title="%s Sim NGAH, stat=%s" ' % (freq, 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)
Beispiel #11
0
    def run(self):
        print("RotD50".center(80, '-'))
        #
        # convert input bbp acc files to peer format acc files
        #

        install = install_cfg.InstallCfg.getInstance()
        sim_id = self.sim_id
        sta_base = os.path.basename(os.path.splitext(self.r_stations)[0])
        self.log = os.path.join(install.A_OUT_LOG_DIR, str(sim_id),
                                "%d.rotd50_%s.log" % (sim_id, sta_base))
        a_statfile = os.path.join(install.A_IN_DATA_DIR,
                                  str(sim_id),
                                  self.r_stations)
        a_tmpdir = os.path.join(install.A_TMP_DATA_DIR, str(sim_id))
        a_outdir = os.path.join(install.A_OUT_DATA_DIR, str(sim_id))

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

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

        for site in site_list:
            stat = site.scode
            print("==> Processing station: %s" % (stat))

            # Since we have velocity files, we need to differentiate
            # to get to acceleration

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

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

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

            for c in ["090", "000", "ver"]:
                # Differentiate to get from velocity to accl needed by rotd50
                # Create path names and check if their sizes are within bounds
                filein = os.path.join(a_tmpdir,
                                      "%d.%s.%s" %
                                      (sim_id, stat, c))
                fileout = os.path.join(a_tmpdir,
                                       "%d.%s.acc.%s" %
                                       (sim_id, stat, c))

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

                cmd = ("%s/integ_diff diff=1 " % (install.A_GP_BIN_DIR) +
                       "filein=%s fileout=%s >> %s 2>&1" %
                       (filein, fileout, self.log))
                bband_utils.runprog(cmd, abort_on_error=True, print_cmd=False)

                # Check file length
                bband_utils.check_path_lengths(["%s/%d.%s.acc.%s" %
                                                (a_tmpdir, sim_id, stat, c)],
                                               bband_utils.GP_MAX_FILENAME)

            # Now we need to convert them back to bbp
            # Create path names and check if their sizes are
            # within bounds
            nsfile = os.path.join(a_tmpdir,
                                  "%d.%s.acc.000" % (sim_id, stat))
            ewfile = os.path.join(a_tmpdir,
                                  "%d.%s.acc.090" % (sim_id, stat))
            udfile = os.path.join(a_tmpdir,
                                  "%d.%s.acc.ver" % (sim_id, stat))
            bbpfile = os.path.join(a_tmpdir,
                                   "%d.%s.acc.bbp" % (sim_id, stat))

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

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

            # Now we need to convert to peer format
            out_n_acc = os.path.join(a_tmpdir,
                                     "%d.%s.peer_n.acc" % (sim_id, stat))
            out_e_acc = os.path.join(a_tmpdir,
                                     "%d.%s.peer_e.acc" % (sim_id, stat))
            out_z_acc = os.path.join(a_tmpdir,
                                     "%d.%s.peer_z.acc" % (sim_id, stat))
            bbp_formatter.bbp2peer(bbpfile, out_n_acc, out_e_acc, out_z_acc)

            # Let's have rotD50 create these output files
            out_rotd50_base = "%d.%s.rd50" % (sim_id, stat)
            tmp_rotd50 = os.path.join(a_tmpdir, out_rotd50_base)
            out_rotd50 = os.path.join(a_outdir, out_rotd50_base)

            # Run the rotD50 program
            self.do_rotd50(a_tmpdir, out_e_acc, out_n_acc, out_z_acc,
                           out_rotd50, self.log)

            cmd = "cp %s %s" % (tmp_rotd50, out_rotd50)
            bband_utils.runprog(cmd, abort_on_error=True, print_cmd=False)

        # All done!
        print("RotD50 Completed".center(80, '-'))
Beispiel #12
0
            src_keys['fault_width'], src_keys['dwid'],
            src_keys['depth_to_top'])
    mech = (src_keys['strike'], src_keys['dip'],
            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)

    bband_utils.check_path_lengths([a_rd50file1, a_rd50file2, resid_file],
                                   bband_utils.GP_MAX_FILENAME)
    cmd = ("%s/gen_resid_tbl_3comp bbp_format=1 " %
           (install.A_GP_BIN_DIR) +
           "datafile1=%s simfile1=%s " % (a_rd50file1, a_rd50file2) +
           "comp1=psa5n comp2=psa5e comp3=rotd50 " +
           "eqname=%s mag=%s stat=%s lon=%.4f lat=%.4f " %
           (event_label, src_keys['magnitude'], 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, resid_file, log_file))
    bband_utils.runprog(cmd, abort_on_error=True)

    # Only need to print header the first time
    if print_header_rd50 == 1:
Beispiel #13
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, '-'))
Beispiel #14
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, '-'))
Beispiel #15
0
    def calculate_simulated(self, a_statfile, a_tmpdir, a_outdir, a_dstdir):
        """
        This function calculates the RotD100/RotD50 values for the
        computed seismograms
        """
        install = install_cfg.InstallCfg.getInstance()
        sim_id = self.sim_id
        slo = StationList(a_statfile)
        site_list = slo.getStationList()

        for site in site_list:
            stat = site.scode
            print("==> Calculating simulation RotD100 for station: %s" %
                  (stat))
            # Since we have velocity files, we need to differentiate
            # to get to acceleration

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

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

            cmd = ("%s " % (os.path.join(install.A_GP_BIN_DIR, "wcc2bbp")) +
                   "wcc2bbp=0 nsfile=%s ewfile=%s udfile=%s < %s >> %s 2>&1" %
                   (nsfile, ewfile, udfile, bbpfile, self.log))
            bband_utils.runprog(cmd, abort_on_error=True, print_cmd=False)

            for component in ["090", "000", "ver"]:
                # Differentiate to get from velocity to accl needed by rotd100
                # Create path names and check if their sizes are within bounds
                filein = os.path.join(a_tmpdir,
                                      "%d.%s.%s" % (sim_id, stat, component))
                fileout = os.path.join(
                    a_tmpdir, "%d.%s.acc.%s" % (sim_id, stat, component))

                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 >> %s 2>&1" %
                       (filein, fileout, self.log))
                bband_utils.runprog(cmd, abort_on_error=True, print_cmd=False)

                # Check file length
                bband_utils.check_path_lengths(
                    ["%s/%d.%s.acc.%s" % (a_tmpdir, sim_id, stat, component)],
                    bband_utils.GP_MAX_FILENAME)

            # Now we need to convert them back to bbp
            # Create path names and check if their sizes are
            # within bounds
            nsfile = os.path.join(a_tmpdir, "%d.%s.acc.000" % (sim_id, stat))
            ewfile = os.path.join(a_tmpdir, "%d.%s.acc.090" % (sim_id, stat))
            udfile = os.path.join(a_tmpdir, "%d.%s.acc.ver" % (sim_id, stat))
            bbpfile = os.path.join(a_tmpdir, "%d.%s.acc.bbp" % (sim_id, stat))

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

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

            # Now we need to convert to peer format
            out_n_acc = os.path.join(a_tmpdir,
                                     "%d.%s.peer_n.acc" % (sim_id, stat))
            out_e_acc = os.path.join(a_tmpdir,
                                     "%d.%s.peer_e.acc" % (sim_id, stat))
            out_z_acc = os.path.join(a_tmpdir,
                                     "%d.%s.peer_z.acc" % (sim_id, stat))
            bbp_formatter.bbp2peer(bbpfile, out_n_acc, out_e_acc, out_z_acc)

            # Let's have rotD100 create these output files
            out_rotd50_base = "%d.%s.rd50" % (sim_id, stat)
            out_rotd100_base = "%d.%s.rd100" % (sim_id, stat)
            out_rotd50v_base = "%d.%s.rd50.vertical" % (sim_id, stat)
            out_rotd100v_base = "%d.%s.rd100.vertical" % (sim_id, stat)

            # Run the rotD100 program twice (horizontals and vertical)
            do_rotd100(a_tmpdir, out_e_acc, out_n_acc, out_rotd100_base,
                       self.log)
            # Run the rotD100 program twice (horizontals and vertical)
            do_rotd100(a_tmpdir, out_z_acc, out_z_acc, out_rotd100v_base,
                       self.log)
            # Create rotd50 files as well
            do_split_rotd50(a_tmpdir, out_rotd100_base, out_rotd50_base,
                            self.log)
            do_split_rotd50(a_tmpdir, out_rotd100v_base, out_rotd50v_base,
                            self.log)

            # Copy horizontals
            cmd = "cp %s %s" % (os.path.join(a_tmpdir, out_rotd100_base),
                                os.path.join(a_dstdir, out_rotd100_base))
            bband_utils.runprog(cmd, abort_on_error=True, print_cmd=False)
            cmd = "cp %s %s" % (os.path.join(a_tmpdir, out_rotd50_base),
                                os.path.join(a_dstdir, out_rotd50_base))
            bband_utils.runprog(cmd, abort_on_error=True, print_cmd=False)
            # Now copy verticals
            cmd = "cp %s %s" % (os.path.join(a_tmpdir, out_rotd100v_base),
                                os.path.join(a_dstdir, out_rotd100v_base))
            bband_utils.runprog(cmd, abort_on_error=True, print_cmd=False)
            cmd = "cp %s %s" % (os.path.join(a_tmpdir, out_rotd50v_base),
                                os.path.join(a_dstdir, out_rotd50v_base))
            bband_utils.runprog(cmd, abort_on_error=True, print_cmd=False)
Beispiel #16
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, '-'))
Beispiel #17
0
    def convert_exsim_to_bbp(self, in_exsim_acc_file, stat, tmpdir, outdir):
        """
        This function reads the in_exsim_acc_file, writes a bbp acc
        file and also generates the corresponding bbp vel file (both
        of those in the outdata directory)
        """
        out_bbp_acc_file = os.path.join(
            outdir, "%s.%s.acc.bbp" % (str(self.sim_id), stat))
        out_bbp_vel_file = os.path.join(
            outdir, "%s.%s.vel.bbp" % (str(self.sim_id), stat))
        # First write the BBP ACC file
        in_seis = open(in_exsim_acc_file, 'r')
        out_acc = open(out_bbp_acc_file, 'w')
        out_acc.write("# Sim stat=%s\n" % (stat))
        out_acc.write("#    time(sec)      N-S(cm/s/s)      "
                      "E-W(cm/s/s)      U-D(cm/s/s)\n")
        ready_to_copy = False
        for line in in_seis:
            if not ready_to_copy:
                line = line.strip()
                if line.find("time(s) acc(cm/s**2)") < 0:
                    # Not the line we are looking for...
                    continue
                ready_to_copy = True
                continue
            # Now copy the data to the BBP acc file
            pieces = line.split()
            if len(pieces) != 2:
                # We expect 2 values per line (time and acc)
                continue
            pieces = [float(piece) for piece in pieces]
            out_acc.write("%15.6e%15.6e%15.6e%15.6e\n" %
                          (pieces[0], pieces[1], pieces[1], pieces[1]))
        in_seis.close()
        out_acc.close()

        # Now create the vel bbp file!
        # Create path names and check if their sizes are within bounds
        nsfile = os.path.join(tmpdir, "%d.%s.acc.000" % (self.sim_id, stat))
        ewfile = os.path.join(tmpdir, "%d.%s.acc.090" % (self.sim_id, stat))
        udfile = os.path.join(tmpdir, "%d.%s.acc.ver" % (self.sim_id, stat))
        bband_utils.check_path_lengths([nsfile, ewfile, udfile],
                                       bband_utils.GP_MAX_FILENAME)

        cmd = ("%s/wcc2bbp " % (self.install.A_GP_BIN_DIR) +
               "wcc2bbp=0 nsfile=%s ewfile=%s udfile=%s < %s >> %s 2>&1" %
               (nsfile, ewfile, udfile, out_bbp_acc_file, self.log))
        bband_utils.runprog(cmd, abort_on_error=True, print_cmd=False)
        # Now we need to integrate to get to velocity
        for comp in ['000', '090', 'ver']:
            file_in = os.path.join(tmpdir,
                                   "%d.%s.acc.%s" % (self.sim_id, stat, comp))
            file_out = os.path.join(tmpdir,
                                    "%s.%s.vel.%s" % (self.sim_id, stat, comp))
            bband_utils.check_path_lengths([file_in, file_out],
                                           bband_utils.GP_MAX_FILENAME)
            cmd = ("%s/integ_diff integ=1 filein=%s fileout=%s" %
                   (self.install.A_GP_BIN_DIR, file_in, file_out))
            bband_utils.runprog(cmd, abort_on_error=True, print_cmd=False)
        # Now we put together the file again as a velocity bbp file
        nsfile = os.path.join(tmpdir, "%d.%s.vel.000" % (self.sim_id, stat))
        ewfile = os.path.join(tmpdir, "%d.%s.vel.090" % (self.sim_id, stat))
        udfile = os.path.join(tmpdir, "%d.%s.vel.ver" % (self.sim_id, stat))
        bband_utils.check_path_lengths([nsfile, ewfile, udfile],
                                       bband_utils.GP_MAX_FILENAME)

        cmd = ("%s/wcc2bbp " % (self.install.A_GP_BIN_DIR) +
               "wcc2bbp=1 nsfile=%s ewfile=%s udfile=%s > %s 2>> %s" %
               (nsfile, ewfile, udfile, out_bbp_vel_file, self.log))
        bband_utils.runprog(cmd, abort_on_error=True, print_cmd=False)
Beispiel #18
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, '-'))
Beispiel #19
0
    def run(self):
        """
        Go through the station list and create acceleration
        seismogram. Copy results to outdata directory
        """
        print("Copy Seismograms".center(80, '-'))

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

        sta_base = os.path.basename(os.path.splitext(self.r_stations)[0])
        self.log = os.path.join(install.A_OUT_LOG_DIR,
                                str(sim_id),
                                "%d.copy_seis_%s.log" % (sim_id, sta_base))
        a_statfile = os.path.join(install.A_IN_DATA_DIR,
                                  str(sim_id),
                                  self.r_stations)

        a_tmpdir = os.path.join(install.A_TMP_DATA_DIR, str(sim_id))
        a_outdir = os.path.join(install.A_OUT_DATA_DIR, str(sim_id))

        # Make sure tmpdir, outdir exist
        dirs = [a_tmpdir, a_outdir]
        bband_utils.mkdirs(dirs, print_cmd=False)

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

        for sits in site_list:
            site = sits.scode
            print("==> Processing station: %s" % (site))

            if self.hybrid:
                expected_file = "%d.%s.bbp" % (sim_id, site)
            else:
                expected_file = "%d.%s.vel.bbp" % (sim_id, site)

            #print("Processing velocity for station %s - %s" %
            #      (site, expected_file))
            bbpfile = os.path.join(a_tmpdir, expected_file)

            # Make sure velocity file is there, otherwise, skip this station
            if not os.path.exists(bbpfile):
                print("No velocity seismograms found for station %s" %
                      (site))
                print("Skipping this station...")
                continue

            # Copy velocity bbp file to outdir
            shutil.copy2(bbpfile,
                         os.path.join(a_outdir, "%d.%s.vel.bbp" %
                                      (sim_id, site)))

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

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

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

            for comp in ['000', '090', 'ver']:
                # Create path names and check if their sizes are
                # within bounds
                filein = os.path.join(a_tmpdir,
                                      "%d.%s.%s" %
                                      (sim_id, site, comp))
                fileout = os.path.join(a_tmpdir,
                                       "%d.%s.acc.%s" %
                                       (sim_id, site, comp))

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

                cmd = ("%s/integ_diff diff=1 filein=%s fileout=%s" %
                       (install.A_GP_BIN_DIR, filein, fileout))
                bband_utils.runprog(cmd, abort_on_error=True, print_cmd=False)

            # Create path names and check if their sizes are within bounds
            nsfile = os.path.join(a_tmpdir,
                                  "%d.%s.acc.000" % (sim_id, site))
            ewfile = os.path.join(a_tmpdir,
                                  "%d.%s.acc.090" % (sim_id, site))
            udfile = os.path.join(a_tmpdir,
                                  "%d.%s.acc.ver" % (sim_id, site))
            bbpfile = os.path.join(a_tmpdir,
                                   "%d.%s.acc.bbp" % (sim_id, site))

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

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

            # Copy acceleration bbp file to outdir
            shutil.copy2(bbpfile,
                         os.path.join(a_outdir, "%d.%s.acc.bbp" %
                                      (sim_id, site)))

        print("Copy Seismograms Completed".center(80, '-'))
Beispiel #20
0
    def process_hybrid_seismogram(self, site, sta_base, vs30,
                                  a_tmpdir, a_outdir):
        """
        Runs the site response module for a hybrid seismogram
        """
        sim_id = self.sim_id
        config = self.config
        install = self.install
        print("**** Reading broadband seismogram...")

        # Separate into HF and LF pieces:
        # 1) wcc2bbp
        # 2) convert to acceleration
        # 3) filter HF
        # 4) filter LF
        # 5) recombine HF and LF

        # Figure out where the input seismogram is located
        if self.method == "SDSU" or self.method == "UCSB":
            bbpfile = os.path.join(a_tmpdir,
                                   "%d.%s.bbp" % (sim_id, site))
        elif self.method == "EXSIM":
            bbpfile = os.path.join(a_outdir,
                                   "%d.%s.vel.bbp" %
                                   (sim_id, site))
        else:
            print("Unknown simulation method!")
            sys.exit(-1)

        # Create path names and check if their sizes are within bounds
        nsfile = os.path.join(a_tmpdir,
                              "temp_%s.000" % (sta_base))
        ewfile = os.path.join(a_tmpdir,
                              "temp_%s.090" % (sta_base))
        udfile = os.path.join(a_tmpdir,
                              "temp_%s.ver" % (sta_base))
        bband_utils.check_path_lengths([nsfile, ewfile, udfile],
                                       bband_utils.GP_MAX_FILENAME)

        # Run wcc2bpp
        progstring = ("%s " %
                      (os.path.join(install.A_GP_BIN_DIR, "wcc2bbp")) +
                      "nsfile=%s " % (nsfile) +
                      "ewfile=%s " % (ewfile) +
                      "udfile=%s " % (udfile) +
                      "wcc2bbp=0 " +
                      "< %s >> %s 2>&1 " %
                      (bbpfile, self.log))

        bband_utils.runprog(progstring, abort_on_error=True, print_cmd=False)

        for entries in config.COMPS:
            compo = entries
            # Create path names and check if their sizes are
            # within bounds
            filein = os.path.join(a_tmpdir,
                                  "temp_%s.%s" % (sta_base, compo))
            fileout = os.path.join(a_tmpdir,
                                   "temp_%s.acc.%s" % (sta_base, compo))
            bband_utils.check_path_lengths([filein, fileout],
                                           bband_utils.GP_MAX_FILENAME)

            # Convert from velocity to accl
            cmd = ("%s diff=1 " %
                   (os.path.join(install.A_GP_BIN_DIR, "integ_diff")) +
                   "filein=%s fileout=%s >> %s 2>&1" %
                   (filein, fileout, self.log))
            bband_utils.runprog(cmd, abort_on_error=True, print_cmd=False)

            pga_filename = os.path.join(a_tmpdir,
                                        "%s.%s.pga.txt" % (site, compo))
            # No need to check path lengths here, it inputs
            # fileout from integ_diff
            filein = fileout
            progstring = ("%s < %s > %s 2>> %s" %
                          (os.path.join(install.A_GP_BIN_DIR,
                                        "wcc_getpeak"),
                           filein, pga_filename, self.log))
            # wcc_getpeak returns 33 even when it succeeds
            bband_utils.runprog(progstring, print_cmd=False)
            pga_file = open(pga_filename, "r")
            data = pga_file.readlines()
            pga_file.close()
            pga = float(data[0].split()[1]) / 981.0
            # Create path name for output file
            fileout = os.path.join(a_tmpdir,
                                   "temp_%s.amp.acc.%s" %
                                   (sta_base, compo))
            bband_utils.check_path_lengths([fileout],
                                           bband_utils.GP_MAX_FILENAME)

            # Pick the right model to use
            site_amp_model = config.SITEAMP_MODEL

            # Run site amp
            progstring = ("%s pga=%f vref=%d vsite=%d " %
                          (os.path.join(install.A_GP_BIN_DIR,
                                        "wcc_siteamp14"),
                           pga, config.GEN_ROCK_VS, vs30) +
                          'model="%s" vpga=%d flowcap=%f ' %
                          (site_amp_model,
                           config.GEN_ROCK_VS, config.FLOWCAP) +
                          'infile=%s outfile=%s ' %
                          (filein, fileout) +
                          "fmidbot=%s fmin=%s >> %s 2>&1" %
                          (config.FMIDBOT, config.FMIN, self.log))
            bband_utils.runprog(progstring, abort_on_error=True)

            filein = fileout
            # Create path name for output file
            fileout = os.path.join(a_tmpdir,
                                   "temp_%s.vel.amp.%s" %
                                   (sta_base, compo))
            bband_utils.check_path_lengths([fileout],
                                           bband_utils.GP_MAX_FILENAME)
            # Convert to velocity
            cmd = ("%s integ=1 " %
                   (os.path.join(install.A_GP_BIN_DIR, "integ_diff")) +
                   "filein=%s fileout=%s >> %s 2>&1" %
                   (filein, fileout, self.log))
            bband_utils.runprog(cmd, abort_on_error=True, print_cmd=False)

        # Create the velocity BBP file by combining the 3 vel components

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

        progstring = ("%s wcc2bbp=1 units=cm/s " %
                      (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 output velocity file to output dir
        shutil.copy2(bbpfile, a_outdir)

        # Now, create the acceleration file by combining the 3 acc components

        # Create path names and check if their sizes are within bounds
        nsfile = os.path.join(a_tmpdir,
                              "temp_%s.amp.acc.000" % (sta_base))
        ewfile = os.path.join(a_tmpdir,
                              "temp_%s.amp.acc.090" % (sta_base))
        udfile = os.path.join(a_tmpdir,
                              "temp_%s.amp.acc.ver" % (sta_base))
        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)

        progstring = ("%s wcc2bbp=1 units=cm/s/s " %
                      (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 output acceleration file to output dir
        shutil.copy2(bbpfile, a_outdir)
Beispiel #21
0
    def process_separate_seismograms(self, site, sta_base, vs30,
                                     a_indir, a_tmpdir):
        """
        Runs the site response module for separate low and high
        frequency seismograms
        """
        sim_id = self.sim_id
        config = self.config
        install = self.install

        flowcap = config.FLOWCAP

        if self.stat_3d is not None:
            flowcap = 1.0

        # Run HF and LF components
        for freq in ['hf', 'lf']:
            print("**** Processing %s component..." % (freq))
            # Create path names and check if their sizes are
            # within bounds
            nsfile = os.path.join(a_tmpdir,
                                  "temp_%s.%s.000" % (sta_base, freq))
            ewfile = os.path.join(a_tmpdir,
                                  "temp_%s.%s.090" % (sta_base, freq))
            udfile = os.path.join(a_tmpdir,
                                  "temp_%s.%s.ver" % (sta_base, freq))
            bbpfile = os.path.join(a_tmpdir,
                                   "%d.%s-%s.bbp" % (sim_id, site,
                                                     freq))
            bband_utils.check_path_lengths([nsfile, ewfile, udfile],
                                           bband_utils.GP_MAX_FILENAME)

            # Convert bbp files to 3 wcc files
            progstring = ("%s nsfile=%s ewfile=%s udfile=%s " %
                          (os.path.join(install.A_GP_BIN_DIR, "wcc2bbp"),
                           nsfile, ewfile, udfile) +
                          "wcc2bbp=0 < %s >> %s 2>&1" %
                          (bbpfile, self.log))
            bband_utils.runprog(progstring, abort_on_error=True,
                                print_cmd=False)

            for entries in config.COMPS:
                compo = entries

                # Create path names and check if their sizes
                # are within bounds
                filein = os.path.join(a_tmpdir,
                                      "temp_%s.%s.%s" %
                                      (sta_base, freq, compo))
                fileout = os.path.join(a_tmpdir,
                                       "temp_%s.acc.%s.%s" %
                                       (sta_base, freq, compo))
                bband_utils.check_path_lengths([filein, fileout],
                                               bband_utils.GP_MAX_FILENAME)

                # Convert to acceleration
                cmd = ("%s diff=1 " %
                       (os.path.join(install.A_GP_BIN_DIR, "integ_diff")) +
                       "filein=%s fileout=%s >> %s 2>&1" %
                       (filein, fileout, self.log))
                bband_utils.runprog(cmd, abort_on_error=True,
                                    print_cmd=False)

                # Get PGA for component from high frequency ONLY
                pga_filename = os.path.join(a_tmpdir,
                                            "%s.%s.pga.txt" % (site,
                                                               compo))

                # No need to check path lengths here
                progstring = ("%s < " %
                              (os.path.join(install.A_GP_BIN_DIR,
                                            "wcc_getpeak")) +
                              "%s/temp_%s.acc.hf.%s > %s 2>> %s" %
                              (a_tmpdir, sta_base, compo,
                               pga_filename, self.log))
                # wcc_getpeak returns 33 even when it succeeds
                bband_utils.runprog(progstring, print_cmd=False)
                pga_file = open(pga_filename, "r")
                data = pga_file.readlines()
                pga_file.close()
                pga = float(data[0].split()[1]) / 981.0
                vref = config.GEN_ROCK_VS

                if freq == 'lf' and self.stat_3d is not None:
                    vref = config.VREF_MAX
                    input_file = open(os.path.join(a_indir,
                                                   self.stat_3d), 'r')
                    stat_3d_data = input_file.readlines()
                    input_file.close()
                    for line in stat_3d_data:
                        if line.split()[2] == site:
                            vref = min([vref, float(line.split()[3])])
                            break

                # Create path names and check if their sizes
                # are within bounds
                filein = os.path.join(a_tmpdir,
                                      "temp_%s.acc.%s.%s" %
                                      (sta_base, freq, compo))
                fileout = os.path.join(a_tmpdir,
                                       "temp_%s.acc.amp.%s.%s" %
                                       (sta_base, freq, compo))
                bband_utils.check_path_lengths([filein, fileout],
                                               bband_utils.GP_MAX_FILENAME)

                # Pick the right model to use
                if freq == 'lf' and self.stat_3d is not None:
                    # Only use this for LF 3D seismograms
                    site_amp_model = config.SITEAMP_MODEL3D
                else:
                    site_amp_model = config.SITEAMP_MODEL

                # Now, run site amplification
                progstring = ("%s pga=%f vref=%d " %
                              (os.path.join(install.A_GP_BIN_DIR,
                                            "wcc_siteamp14"), pga, vref) +
                              'vsite=%d model="%s" vpga=%d ' %
                              (vs30, site_amp_model,
                               config.GEN_ROCK_VS) +
                              'flowcap=%f infile=%s outfile=%s ' %
                              (flowcap, filein, fileout) +
                              "fmidbot=%s fmin=%s >> %s 2>&1" %
                              (config.FMIDBOT, config.FMIN, self.log))
                bband_utils.runprog(progstring, abort_on_error=True)

                # Output becomes input
                filein = fileout
                fileout = os.path.join(a_tmpdir,
                                       "temp_%s.vel.amp.%s.%s" %
                                       (sta_base, freq, compo))
                bband_utils.check_path_lengths([fileout],
                                               bband_utils.GP_MAX_FILENAME)
                # Now integrate to get velocity
                cmd = ("%s integ=1 " %
                       (os.path.join(install.A_GP_BIN_DIR, "integ_diff")) +
                       "filein=%s fileout=%s >> %s 2>&1" %
                       (filein, fileout, self.log))
                bband_utils.runprog(cmd, abort_on_error=True, print_cmd=False)

            # Do one time for acceleration

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

            progstring = ("%s wcc2bbp=1 units=cm/s/s " %
                          (os.path.join(install.A_GP_BIN_DIR, "wcc2bbp")) +
                          'title="%s Sim NGAH, stat=%s" ' %
                          (freq, 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)

            # Do it again for velocity

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

            progstring = ("%s wcc2bbp=1 units=cm/s " %
                          (os.path.join(install.A_GP_BIN_DIR, "wcc2bbp")) +
                          'title="%s Sim NGAH, stat=%s" ' %
                          (freq, 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)
Beispiel #22
0
    def calculate_residuals(self, a_statfile, a_dstdir):
        """
        This function calculates the residuals comparing observations and
        calculated data.
        """
        install = install_cfg.InstallCfg.getInstance()
        sim_id = self.sim_id
        slo = StationList(a_statfile)
        site_list = slo.getStationList()

        print_header = 1
        rd100_resid_output = os.path.join(
            a_dstdir, "%s-%d-resid-rd100.txt" % (self.comp_label, sim_id))
        # If output file exists, delete it
        if os.path.exists(rd100_resid_output):
            os.remove(rd100_resid_output)

        # Filenames for tmp files, check if filename size within bounds
        obsfile = os.path.join(a_dstdir, "rd100_obs.txt")
        simfile = os.path.join(a_dstdir, "rd100_sim.txt")
        bband_utils.check_path_lengths([obsfile, simfile, rd100_resid_output],
                                       bband_utils.GP_MAX_FILENAME)

        # Loop through all stations
        for site in site_list:
            slon = float(site.lon)
            slat = float(site.lat)
            stat = site.scode

            # Trim files
            self.trim_rd100_file(
                os.path.join(a_dstdir, "%d.%s.rd100" % (sim_id, stat)),
                simfile)
            self.trim_rd100_file(os.path.join(a_dstdir, "%s.rd100" % (stat)),
                                 obsfile)

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

            cmd = (
                "%s bbp_format=1 " %
                (os.path.join(install.A_GP_BIN_DIR, "gen_resid_tbl_3comp")) +
                "datafile1=%s simfile1=%s " % (obsfile, simfile) +
                "comp1=rotd50 comp2=rotd100 comp3=ratio " +
                "eqname=%s mag=%s stat=%s lon=%.4f lat=%.4f " %
                (self.comp_label, self.src_keys['magnitude'], 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, rd100_resid_output, self.log))
            bband_utils.runprog(cmd, abort_on_error=True, print_cmd=False)

            # Only need to print header the first time
            if print_header == 1:
                print_header = 0

        # Remove temp files
        try:
            os.remove(obsfile)
            os.remove(simfile)
        except:
            pass
Beispiel #23
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, '-'))
Beispiel #24
0
    def calculate_residuals(self, a_statfile, a_dstdir):
        """
        This function calculates the residuals comparing observations and
        calculated data.
        """
        install = install_cfg.InstallCfg.getInstance()
        sim_id = self.sim_id
        slo = StationList(a_statfile)
        site_list = slo.getStationList()

        print_header = 1
        rd100_resid_output = os.path.join(a_dstdir, "%s-%d-resid-rd100.txt" %
                                          (self.comp_label, sim_id))
        # If output file exists, delete it
        if os.path.exists(rd100_resid_output):
            os.remove(rd100_resid_output)

        # Filenames for tmp files, check if filename size within bounds
        obsfile = os.path.join(a_dstdir, "rd100_obs.txt")
        simfile = os.path.join(a_dstdir, "rd100_sim.txt")
        bband_utils.check_path_lengths([obsfile, simfile,
                                        rd100_resid_output],
                                       bband_utils.GP_MAX_FILENAME)

        # Loop through all stations
        for site in site_list:
            slon = float(site.lon)
            slat = float(site.lat)
            stat = site.scode

            # Trim files
            self.trim_rd100_file(os.path.join(a_dstdir,
                                              "%d.%s.rd100" % (sim_id, stat)),
                                 simfile)
            self.trim_rd100_file(os.path.join(a_dstdir,
                                              "%s.rd100" % (stat)),
                                 obsfile)

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

            cmd = ("%s bbp_format=1 " %
                   (os.path.join(install.A_GP_BIN_DIR,
                                 "gen_resid_tbl_3comp")) +
                   "datafile1=%s simfile1=%s " % (obsfile, simfile) +
                   "comp1=rotd50 comp2=rotd100 comp3=ratio " +
                   "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, rd100_resid_output, self.log))
            bband_utils.runprog(cmd, abort_on_error=True, print_cmd=False)

            # Only need to print header the first time
            if print_header == 1:
                print_header = 0

        # Remove temp files
        try:
            os.remove(obsfile)
            os.remove(simfile)
        except:
            pass
Beispiel #25
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, str(sim_id),
                                "%d.gen_plots.log" % (sim_id))

        # Input, tmp, and output directories
        a_tmpdir = os.path.join(install.A_TMP_DATA_DIR, str(sim_id))
        a_outdir = os.path.join(install.A_OUT_DATA_DIR, str(sim_id))
        a_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

            # Look for the files we need
            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, '-'))
Beispiel #26
0
    # Calculate Rrup
    origin = (src_keys['lon_top_center'], src_keys['lat_top_center'])
    dims = (src_keys['fault_length'], src_keys['dlen'],
            src_keys['fault_width'], src_keys['dwid'],
            src_keys['depth_to_top'])
    mech = (src_keys['strike'], src_keys['dip'], src_keys['rake'])

    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)

    bband_utils.check_path_lengths([a_rd50file1, a_rd50file2, resid_file],
                                   bband_utils.GP_MAX_FILENAME)
    cmd = ("%s/gen_resid_tbl_3comp bbp_format=1 " % (install.A_GP_BIN_DIR) +
           "datafile1=%s simfile1=%s " % (a_rd50file1, a_rd50file2) +
           "comp1=psa5n comp2=psa5e comp3=rotd50 " +
           "eqname=%s mag=%s stat=%s lon=%.4f lat=%.4f " %
           (event_label, src_keys['magnitude'], 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, resid_file, log_file))
    bband_utils.runprog(cmd, abort_on_error=True)

    # Only need to print header the first time
    if print_header_rd50 == 1:
        print_header_rd50 = 0
Beispiel #27
0
    def calculate_simulated(self, a_statfile, a_tmpdir, a_outdir, a_dstdir):
        """
        This function calculates the RotD100/RotD50 values for the
        computed seismograms
        """
        install = install_cfg.InstallCfg.getInstance()
        sim_id = self.sim_id
        slo = StationList(a_statfile)
        site_list = slo.getStationList()

        for site in site_list:
            stat = site.scode
            print("==> Calculating simulation RotD100 for station: %s" %
                  (stat))
            # Since we have velocity files, we need to differentiate
            # to get to acceleration

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

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

            cmd = ("%s " % (os.path.join(install.A_GP_BIN_DIR, "wcc2bbp")) +
                   "wcc2bbp=0 nsfile=%s ewfile=%s udfile=%s < %s >> %s 2>&1" %
                   (nsfile, ewfile, udfile, bbpfile, self.log))
            bband_utils.runprog(cmd, abort_on_error=True, print_cmd=False)

            for c in ["090", "000", "ver"]:
                # Differentiate to get from velocity to accl needed by rotd100
                # Create path names and check if their sizes are within bounds
                filein = os.path.join(a_tmpdir,
                                      "%d.%s.%s" %
                                      (sim_id, stat, c))
                fileout = os.path.join(a_tmpdir,
                                       "%d.%s.acc.%s" %
                                       (sim_id, stat, c))

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

                cmd = ("%s diff=1 " %
                       (os.path.join(install.A_GP_BIN_DIR, "integ_diff")) +
                       "filein=%s fileout=%s >> %s 2>&1" %
                       (filein, fileout, self.log))
                bband_utils.runprog(cmd, abort_on_error=True, print_cmd=False)

                # Check file length
                bband_utils.check_path_lengths(["%s/%d.%s.acc.%s" %
                                                (a_tmpdir, sim_id, stat, c)],
                                               bband_utils.GP_MAX_FILENAME)

            # Now we need to convert them back to bbp
            # Create path names and check if their sizes are
            # within bounds
            nsfile = os.path.join(a_tmpdir,
                                  "%d.%s.acc.000" % (sim_id, stat))
            ewfile = os.path.join(a_tmpdir,
                                  "%d.%s.acc.090" % (sim_id, stat))
            udfile = os.path.join(a_tmpdir,
                                  "%d.%s.acc.ver" % (sim_id, stat))
            bbpfile = os.path.join(a_tmpdir,
                                   "%d.%s.acc.bbp" % (sim_id, stat))

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

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

            # Now we need to convert to peer format
            out_n_acc = os.path.join(a_tmpdir,
                                     "%d.%s.peer_n.acc" % (sim_id, stat))
            out_e_acc = os.path.join(a_tmpdir,
                                     "%d.%s.peer_e.acc" % (sim_id, stat))
            out_z_acc = os.path.join(a_tmpdir,
                                     "%d.%s.peer_z.acc" % (sim_id, stat))
            bbp_formatter.bbp2peer(bbpfile, out_n_acc, out_e_acc, out_z_acc)

            # Let's have rotD100 create these output files
            out_rotd100_base = "%d.%s.rd100" % (sim_id, stat)
            tmp_rotd100 = os.path.join(a_tmpdir, out_rotd100_base)
            out_rotd100 = os.path.join(a_dstdir, out_rotd100_base)

            # Run the rotD100 program
            self.do_rotd100(a_tmpdir, out_e_acc, out_n_acc, out_z_acc,
                            out_rotd100, self.log)

            cmd = "cp %s %s" % (tmp_rotd100, out_rotd100)
            bband_utils.runprog(cmd, abort_on_error=True, print_cmd=False)
Beispiel #28
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, '-'))
Beispiel #29
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, '-'))
Beispiel #30
0
    def process_hybrid_seismogram(self, site, sta_base, vs30, a_tmpdir,
                                  a_outdir):
        """
        Runs the site response module for a hybrid seismogram
        """
        sim_id = self.sim_id
        config = self.config
        install = self.install
        print("**** Reading broadband seismogram...")

        # Separate into HF and LF pieces:
        # 1) wcc2bbp
        # 2) convert to acceleration
        # 3) filter HF
        # 4) filter LF
        # 5) recombine HF and LF

        # Figure out vref to use in hybrid scenarios
        vref = config.LF_VREF
        vpga = config.HF_VREF

        # Figure out where the input seismogram is located
        if self.method == "SDSU" or self.method == "UCSB":
            bbpfile = os.path.join(a_tmpdir, "%d.%s.bbp" % (sim_id, site))
        elif self.method == "EXSIM":
            bbpfile = os.path.join(a_outdir, "%d.%s.vel.bbp" % (sim_id, site))
        else:
            print("Unknown simulation method!")
            sys.exit(-1)

        # Create path names and check if their sizes are within bounds
        nsfile = os.path.join(a_tmpdir, "temp_%s.000" % (sta_base))
        ewfile = os.path.join(a_tmpdir, "temp_%s.090" % (sta_base))
        udfile = os.path.join(a_tmpdir, "temp_%s.ver" % (sta_base))
        bband_utils.check_path_lengths([nsfile, ewfile, udfile],
                                       bband_utils.GP_MAX_FILENAME)

        # Run wcc2bpp
        progstring = ("%s " % (os.path.join(install.A_GP_BIN_DIR, "wcc2bbp")) +
                      "nsfile=%s " % (nsfile) + "ewfile=%s " % (ewfile) +
                      "udfile=%s " % (udfile) + "wcc2bbp=0 " +
                      "< %s >> %s 2>&1 " % (bbpfile, self.log))

        bband_utils.runprog(progstring, abort_on_error=True, print_cmd=False)

        for entries in config.COMPS:
            compo = entries
            # Create path names and check if their sizes are
            # within bounds
            filein = os.path.join(a_tmpdir, "temp_%s.%s" % (sta_base, compo))
            fileout = os.path.join(a_tmpdir,
                                   "temp_%s.acc.%s" % (sta_base, compo))
            bband_utils.check_path_lengths([filein, fileout],
                                           bband_utils.GP_MAX_FILENAME)

            # Convert from velocity to accl
            cmd = ("%s diff=1 " %
                   (os.path.join(install.A_GP_BIN_DIR, "integ_diff")) +
                   "filein=%s fileout=%s >> %s 2>&1" %
                   (filein, fileout, self.log))
            bband_utils.runprog(cmd, abort_on_error=True, print_cmd=False)

            pga_filename = os.path.join(a_tmpdir,
                                        "%s.%s.pga.txt" % (site, compo))
            # No need to check path lengths here, it inputs
            # fileout from integ_diff
            filein = fileout
            progstring = ("%s < %s > %s 2>> %s" %
                          (os.path.join(install.A_GP_BIN_DIR, "wcc_getpeak"),
                           filein, pga_filename, self.log))
            # wcc_getpeak returns 33 even when it succeeds
            bband_utils.runprog(progstring, print_cmd=False)
            pga_file = open(pga_filename, "r")
            data = pga_file.readlines()
            pga_file.close()
            pga = float(data[0].split()[1]) / 981.0
            # Create path name for output file
            fileout = os.path.join(a_tmpdir,
                                   "temp_%s.amp.acc.%s" % (sta_base, compo))
            bband_utils.check_path_lengths([fileout],
                                           bband_utils.GP_MAX_FILENAME)

            # Pick the right model to use
            site_amp_model = config.SITEAMP_MODEL

            # Run site amp
            progstring = ("%s pga=%f vref=%d vsite=%d " % (os.path.join(
                install.A_GP_BIN_DIR, "wcc_siteamp14"), pga, vref, vs30) +
                          'model="%s" vpga=%d flowcap=%f ' %
                          (site_amp_model, vpga, config.FLOWCAP) +
                          'infile=%s outfile=%s ' % (filein, fileout) +
                          'fmax=%f fhightop=%f ' %
                          (config.FMAX, config.FHIGHTOP) +
                          "fmidbot=%s fmin=%s >> %s 2>&1" %
                          (config.FMIDBOT, config.FMIN, self.log))
            bband_utils.runprog(progstring, abort_on_error=True)

            filein = fileout
            # Create path name for output file
            fileout = os.path.join(a_tmpdir,
                                   "temp_%s.vel.amp.%s" % (sta_base, compo))
            bband_utils.check_path_lengths([fileout],
                                           bband_utils.GP_MAX_FILENAME)
            # Convert to velocity
            cmd = ("%s integ=1 " %
                   (os.path.join(install.A_GP_BIN_DIR, "integ_diff")) +
                   "filein=%s fileout=%s >> %s 2>&1" %
                   (filein, fileout, self.log))
            bband_utils.runprog(cmd, abort_on_error=True, print_cmd=False)

        # Create the velocity BBP file by combining the 3 vel components

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

        progstring = ("%s wcc2bbp=1 units=cm/s " %
                      (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 output velocity file to output dir
        shutil.copy2(bbpfile, a_outdir)

        # Now, create the acceleration file by combining the 3 acc components

        # Create path names and check if their sizes are within bounds
        nsfile = os.path.join(a_tmpdir, "temp_%s.amp.acc.000" % (sta_base))
        ewfile = os.path.join(a_tmpdir, "temp_%s.amp.acc.090" % (sta_base))
        udfile = os.path.join(a_tmpdir, "temp_%s.amp.acc.ver" % (sta_base))
        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)

        progstring = ("%s wcc2bbp=1 units=cm/s/s " %
                      (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 output acceleration file to output dir
        shutil.copy2(bbpfile, a_outdir)
Beispiel #31
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, '-'))
Beispiel #32
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, '-'))