Ejemplo n.º 1
0
    def test_bbp2peer(self):
        """
        Test for the bbp2peer converter
        """
        ref_dir = os.path.join(self.install.A_TEST_REF_DIR, "ucb")
        in_bbp_file = os.path.join(ref_dir, "station.acc.bbp")
        peer_n_out = os.path.join(self.outdir, "output_peer_n.acc")
        peer_e_out = os.path.join(self.outdir, "output_peer_e.acc")
        peer_z_out = os.path.join(self.outdir, "output_peer_z.acc")
        for out_file in [peer_n_out, peer_e_out, peer_z_out]:
            try:
                os.remove(out_file)
            except:
                pass

        #
        # Simplest reference filename.
        #
        peer_n_out_ref = os.path.join(ref_dir, "station.peer_n.acc")
        peer_e_out_ref = os.path.join(ref_dir, "station.peer_e.acc")
        peer_z_out_ref = os.path.join(ref_dir, "station.peer_z.acc")

        bbp_formatter.bbp2peer(in_bbp_file, peer_n_out,
                               peer_e_out, peer_z_out)
        print("created PEER-format files: %s %s %s" %
              (peer_n_out, peer_e_out, peer_z_out))

        res_file = open(peer_n_out, 'r')
        lines = res_file.readlines()
        res_file.close()

        ref_file = open(peer_n_out_ref, 'r')
        rlines = ref_file.readlines()
        ref_file.close()
        #
        # Pass test if bbp file has more than 400 lines.
        # Only confirms that the bbp file is non-zero length
        #
        self.assertTrue(len(lines) == len(rlines))
        # convert to bbp format
        # input to respect
        # retrieve amplitudes from respect
        # compare to amplitudes calculated by PEER
        return 0
Ejemplo n.º 2
0
    def test_bbp2peer(self):
        """
        Test for the bbp2peer converter
        """
        ref_dir = os.path.join(self.install.A_TEST_REF_DIR, "ucb")
        in_bbp_file = os.path.join(ref_dir, "station.acc.bbp")
        peer_n_out = os.path.join(self.outdir, "output_peer_n.acc")
        peer_e_out = os.path.join(self.outdir, "output_peer_e.acc")
        peer_z_out = os.path.join(self.outdir, "output_peer_z.acc")
        for out_file in [peer_n_out, peer_e_out, peer_z_out]:
            try:
                os.remove(out_file)
            except:
                pass

        #
        # Simplest reference filename.
        #
        peer_n_out_ref = os.path.join(ref_dir, "station.peer_n.acc")
        peer_e_out_ref = os.path.join(ref_dir, "station.peer_e.acc")
        peer_z_out_ref = os.path.join(ref_dir, "station.peer_z.acc")

        bbp_formatter.bbp2peer(in_bbp_file, peer_n_out, peer_e_out, peer_z_out)
        print("created PEER-format files: %s %s %s" %
              (peer_n_out, peer_e_out, peer_z_out))

        res_file = open(peer_n_out, 'r')
        lines = res_file.readlines()
        res_file.close()

        ref_file = open(peer_n_out_ref, 'r')
        rlines = ref_file.readlines()
        ref_file.close()
        #
        # Pass test if bbp file has more than 400 lines.
        # Only confirms that the bbp file is non-zero length
        #
        self.assertTrue(len(lines) == len(rlines))
        # convert to bbp format
        # input to respect
        # retrieve amplitudes from respect
        # compare to amplitudes calculated by PEER
        return 0
Ejemplo n.º 3
0
    def calculate_observations(self, a_indir, a_statfile, a_tmpdir_seis, a_dstdir):
        """
        This function calculates RotD100/RotD50 for the observation
        seismograms. It corrects the observations using the user-provided
        correction coefficients.
        """
        sim_id = self.sim_id
        slo = StationList(a_statfile)
        site_list = slo.getStationList()

        # Inialize the CorrectPSA module
        if self.obs_corrections:
            corr_psa = CorrectPSA(self.r_stations,
                                  "rd100",
                                  os.path.join(a_indir,
                                               self.obs_corrections),
                                  a_tmpdir_seis, sim_id)
        else:
            corr_psa = None

        # List of observed seismogram files
        filelist = os.listdir(self.a_obsdir)

        # Go through each station
        for site in site_list:
            stat = site.scode
            print("==> Calculating observations RotD100 for station: %s" %
                  (stat))
            # Check if we have the corresponding calculated seismogram
            expected_calculated_file = os.path.join(a_dstdir,
                                                    "%d.%s.rd100" %
                                                    (sim_id, stat))
            if not os.path.exists(expected_calculated_file):
                # Just skip it
                print("Couldn't find file: %s" %
                      (expected_calculated_file) +
                      "This is not necessarily an error, as you may have " +
                      "run with a subset of a stations. Continuing " +
                      "with available stations.")
                continue

            # Ok, we have a simulated seismogram for this station,
            # let's look for the observed file
            r_e_peer_file = None
            r_n_peer_file = None
            r_z_peer_file = None
            r_bbp_file = "%s.bbp" % (stat)
            # Do different things depending on the format of the
            # observed seismograms
            if self.obs_format == "acc_bbp":
                # We need to look for the bbp file
                if r_bbp_file not in filelist:
                    # No bbp file for this station
                    continue
                print(r_bbp_file)
                # Copy bbp file to the tmp seismogram directory
                a_src_bbp_file = os.path.join(self.a_obsdir, r_bbp_file)
                a_dst_bbp_file = os.path.join(a_tmpdir_seis, r_bbp_file)
                shutil.copy2(a_src_bbp_file, a_dst_bbp_file)
                # Now we need to create the peer files to process with rotd50
                r_e_peer_file = os.path.join(a_tmpdir_seis, "%s_E.acc" % (stat))
                r_n_peer_file = os.path.join(a_tmpdir_seis, "%s_N.acc" % (stat))
                r_z_peer_file = os.path.join(a_tmpdir_seis, "%s_Z.acc" % (stat))
                bbp_formatter.bbp2peer(a_dst_bbp_file,
                                       r_n_peer_file,
                                       r_e_peer_file,
                                       r_z_peer_file)
            elif self.obs_format == "acc_peer":
                # Look for the E, N, and Z files
                for my_file in filelist:
                    if my_file.endswith("%s_E.acc" % (stat)):
                        r_e_peer_file = my_file
                        if (r_n_peer_file is not None and
                            r_z_peer_file is not None):
                            break
                    elif my_file.endswith("%s_N.acc" % (stat)):
                        r_n_peer_file = my_file
                        if (r_e_peer_file is not None and
                            r_z_peer_file is not None):
                            break
                    elif my_file.endswith("%s_Z.acc" % (stat)):
                        r_z_peer_file = my_file
                        if (r_e_peer_file is not None and
                            r_n_peer_file is not None):
                            break
                if ((r_e_peer_file is None) or
                    (r_n_peer_file is None) or
                    (r_z_peer_file is None)):
                    # Couldn't find all 3 files
                    continue
                #print(r_e_peer_file, r_n_peer_file, r_z_peer_file)
                # Copy all three files to the tmp seismogram directory
                for eachfile in (r_e_peer_file, r_n_peer_file, r_z_peer_file):
                    a_src_peer_file = os.path.join(self.a_obsdir, eachfile)
                    a_dst_peer_file = os.path.join(a_tmpdir_seis, eachfile)
                    shutil.copy2(a_src_peer_file, a_dst_peer_file)

                # Now we need to convert them into bbp format
                bbp_formatter.peer2bbp(os.path.join(a_tmpdir_seis,
                                                    r_n_peer_file),
                                       os.path.join(a_tmpdir_seis,
                                                    r_e_peer_file),
                                       os.path.join(a_tmpdir_seis,
                                                    r_z_peer_file),
                                       os.path.join(a_tmpdir_seis,
                                                    r_bbp_file))
            else:
                raise bband_utils.ParameterError("Format %s for " %
                                                 (self.obs_format) +
                                                 "observed seismograms "
                                                 "not supported")

            # Run RotD100 on this file
            if corr_psa is not None:
                # First calculate rd100/50 and psa5 files
                self.do_rotd100(a_tmpdir_seis, r_e_peer_file,
                                 r_n_peer_file, r_z_peer_file,
                                 "%s-orig.rd100" % (stat),
                                 self.log)

                # Now we need to correct the RotD100/RotD50 output
                # using the user-supplied correction factors
                corr_psa.correct_station(stat, "rd100")
            else:
                # Use final names for output files
                self.do_rotd100(a_tmpdir_seis, r_e_peer_file,
                                r_n_peer_file, r_z_peer_file,
                                "%s.rd100" % (stat),
                                self.log)
            shutil.copy2(os.path.join(a_tmpdir_seis, "%s.rd100" % (stat)),
                         os.path.join(a_dstdir, "%s.rd100" % (stat)))
Ejemplo n.º 4
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)
Ejemplo n.º 5
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, '-'))
Ejemplo n.º 6
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, '-'))
Ejemplo n.º 7
0
    def run(self):
        """
        This function copies the observed seismograms for the stations
        specified in r_stations to a temporary directory inside the
        tmpdata directory and converts them to the format needed by
        the goodness of fitness module
        """
        print("ObsSeismograms".center(80, '-'))

        # Initialize basic variables
        install = InstallCfg.getInstance()
        sim_id = self.sim_id
        sta_base = os.path.basename(os.path.splitext(self.r_stations)[0])

        self.log = os.path.join(install.A_OUT_LOG_DIR, str(sim_id),
                                "%d.obs_seis.log" % (sim_id))

        # Input, tmp, and output directories
        a_indir = os.path.join(install.A_IN_DATA_DIR, str(sim_id))
        a_tmpdir = os.path.join(install.A_TMP_DATA_DIR, str(sim_id))
        a_tmpdir_seis = os.path.join(install.A_TMP_DATA_DIR, str(sim_id),
                                     "obs_seis_%s" % (sta_base))
        a_outdir = os.path.join(install.A_OUT_DATA_DIR, str(sim_id))
        a_outdir_seis = os.path.join(a_outdir, "obs_seis_%s" % (sta_base))
        a_outdir_gmpe = os.path.join(a_outdir, "gmpe_data_%s" % (sta_base))

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

        # Station file
        a_statfile = os.path.join(a_indir, self.r_stations)
        # List of observed seismogram files
        filelist = os.listdir(self.a_obsdir)

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

        # Inialize the CorrectPSA module
        if self.obs_corrections:
            corr_psa = CorrectPSA(self.r_stations, "rd100",
                                  os.path.join(a_indir, self.obs_corrections),
                                  a_tmpdir_seis, sim_id)
        else:
            corr_psa = None

        # Go through each station
        for site in site_list:
            slon = float(site.lon)
            slat = float(site.lat)
            stat = site.scode
            print("==> Processing data for station: %s" % (stat))

            # Look for the files we need
            expected_rd50_file = os.path.join(a_outdir,
                                              "%d.%s.rd50" % (sim_id, stat))
            if not os.path.exists(expected_rd50_file):
                # just skip it
                print("Couldn't find file %s. " % (expected_rd50_file) +
                      "This is not necessarily an error, as you may have " +
                      "run with a subset of a stations. Continuing " +
                      "with available stations.")
                continue

            # Ok, we have a calculated rd50/rd100 files for this station,
            # let's look for the observed file
            r_e_peer_file = None
            r_n_peer_file = None
            r_z_peer_file = None
            r_bbp_file = "%s.bbp" % (stat)
            # Do different things depending on the format of the
            # observed seismograms
            if self.obs_format == "acc_bbp":
                # We need to look for the bbp file
                if r_bbp_file not in filelist:
                    # No bbp file for this station
                    continue
                print("==> Converting file: %s" % (r_bbp_file))
                # Copy bbp file to the tmp seismogram directory
                a_src_bbp_file = os.path.join(self.a_obsdir, r_bbp_file)
                a_dst_bbp_file = os.path.join(a_tmpdir_seis, r_bbp_file)
                shutil.copy2(a_src_bbp_file, a_dst_bbp_file)
                # Now we need to create the peer files to process with rotd50
                r_e_peer_file = os.path.join(a_tmpdir_seis,
                                             "%s_E.acc" % (stat))
                r_n_peer_file = os.path.join(a_tmpdir_seis,
                                             "%s_N.acc" % (stat))
                r_z_peer_file = os.path.join(a_tmpdir_seis,
                                             "%s_Z.acc" % (stat))
                bbp_formatter.bbp2peer(a_dst_bbp_file, r_n_peer_file,
                                       r_e_peer_file, r_z_peer_file)
            elif self.obs_format == "acc_peer":
                # Look for the E, N, and Z files
                for my_file in filelist:
                    if my_file.endswith("%s_E.acc" % (stat)):
                        r_e_peer_file = my_file
                        if (r_n_peer_file is not None
                                and r_z_peer_file is not None):
                            break
                    elif my_file.endswith("%s_N.acc" % (stat)):
                        r_n_peer_file = my_file
                        if (r_e_peer_file is not None
                                and r_z_peer_file is not None):
                            break
                    elif my_file.endswith("%s_Z.acc" % (stat)):
                        r_z_peer_file = my_file
                        if (r_e_peer_file is not None
                                and r_n_peer_file is not None):
                            break
                if ((r_e_peer_file is None) or (r_n_peer_file is None)
                        or (r_z_peer_file is None)):
                    # Couldn't find all 3 files
                    continue
                # print(r_e_peer_file, r_n_peer_file, r_z_peer_file)
                # Copy all three files to the tmp seismogram directory
                for eachfile in (r_e_peer_file, r_n_peer_file, r_z_peer_file):
                    a_src_peer_file = os.path.join(self.a_obsdir, eachfile)
                    a_dst_peer_file = os.path.join(a_tmpdir_seis, eachfile)
                    shutil.copy2(a_src_peer_file, a_dst_peer_file)

                # Now we need to convert them into bbp format
                bbp_formatter.peer2bbp(
                    os.path.join(a_tmpdir_seis, r_n_peer_file),
                    os.path.join(a_tmpdir_seis, r_e_peer_file),
                    os.path.join(a_tmpdir_seis, r_z_peer_file),
                    os.path.join(a_tmpdir_seis, r_bbp_file))
            elif self.obs_format == "gmpe":
                # GMPE verification packages don't have actual
                # seismograms, so there's nothing we need to do here!
                a_src_gmpe_file = os.path.join(a_outdir_gmpe,
                                               "%s-gmpe.ri50" % (stat))

                # Create a copy in outdata averaging all gmpes
                a_avg_rd50_file = os.path.join(a_outdir_seis,
                                               "%s.rd50" % (stat))
                gmpe_config.average_gmpe(stat, a_src_gmpe_file,
                                         a_avg_rd50_file)
                # All done!
                continue
            else:
                raise bband_utils.ParameterError("Format %s for " %
                                                 (self.obs_format) +
                                                 "observed seismograms "
                                                 "not supported")

            out_rotd100_base = "%s.rd100" % (stat)
            out_rotd100v_base = "%s.rd100.vertical" % (stat)
            out_rotd50_base = "%s.rd50" % (stat)
            out_rotd50v_base = "%s.rd50.vertical" % (stat)

            # Run RotDXX on this file
            if corr_psa is not None:
                # First calculate rdXX
                print("===> Calculating RotDXX for station: %s" % (stat))
                rotd100.do_rotd100(a_tmpdir_seis, r_e_peer_file, r_n_peer_file,
                                   "%s-orig.rd100" % (stat), self.log)
                #rotd100.do_rotd100(a_tmpdir_seis, r_z_peer_file,
                #                   r_z_peer_file,
                #                   "%s-orig.rd100.vertical" % (stat), self.log)

                # Now we need to correct the RotD100 outputs using the
                # user-supplied correction factors
                print("===> Correcting PSA for station: %s" % (stat))
                corr_psa.correct_station(stat, "rd100")
                #corr_psa.correct_station(stat, "rd100.vertical")
            else:
                # Use final names for output files
                print("===> Calculating RotDXX for station: %s" % (stat))
                rotd100.do_rotd100(a_tmpdir_seis, r_e_peer_file, r_n_peer_file,
                                   out_rotd100_base, self.log)
                #rotd100.do_rotd100(a_tmpdir_seis, r_z_peer_file,
                #                   r_z_peer_file,
                #                   out_rotd100v_base % (stat), self.log)
            # Create rotd50 files as well
            rotd100.do_split_rotd50(a_tmpdir_seis, out_rotd100_base,
                                    out_rotd50_base, self.log)
            #rotd100.do_split_rotd50(a_tmpdir_seis, out_rotd100v_base,
            #                        out_rotd50v_base, self.log)
            shutil.copy2(os.path.join(a_tmpdir_seis, out_rotd100_base),
                         os.path.join(a_outdir_seis, out_rotd100_base))
            #shutil.copy2(os.path.join(a_tmpdir_seis, out_rotd100v_base),
            #             os.path.join(a_outdir_seis, out_rotd100v_base))
            shutil.copy2(os.path.join(a_tmpdir_seis, out_rotd50_base),
                         os.path.join(a_outdir_seis, out_rotd50_base))
            #shutil.copy2(os.path.join(a_tmpdir_seis, out_rotd50v_base),
            #             os.path.join(a_outdir_seis, out_rotd50v_base))

        print("ObsSeismograms Completed".center(80, '-'))
Ejemplo n.º 8
0
    def calculate_observations(self, a_indir, a_statfile, a_tmpdir_seis,
                               a_dstdir):
        """
        This function calculates RotD100/RotD50 for the observation
        seismograms. It corrects the observations using the user-provided
        correction coefficients.
        """
        sim_id = self.sim_id
        slo = StationList(a_statfile)
        site_list = slo.getStationList()

        # Inialize the CorrectPSA module
        if self.obs_corrections:
            corr_psa = CorrectPSA(self.r_stations, "rd100",
                                  os.path.join(a_indir, self.obs_corrections),
                                  a_tmpdir_seis, sim_id)
        else:
            corr_psa = None

        # List of observed seismogram files
        filelist = os.listdir(self.a_obsdir)

        # Go through each station
        for site in site_list:
            stat = site.scode
            print("==> Calculating observations RotD100 for station: %s" %
                  (stat))
            # Check if we have the corresponding calculated seismogram
            expected_calculated_file = os.path.join(
                a_dstdir, "%d.%s.rd100" % (sim_id, stat))
            if not os.path.exists(expected_calculated_file):
                # Just skip it
                print("Couldn't find file: %s" % (expected_calculated_file) +
                      "This is not necessarily an error, as you may have " +
                      "run with a subset of a stations. Continuing " +
                      "with available stations.")
                continue

            # Ok, we have a simulated seismogram for this station,
            # let's look for the observed file
            r_e_peer_file = None
            r_n_peer_file = None
            r_z_peer_file = None
            r_bbp_file = "%s.bbp" % (stat)
            # Do different things depending on the format of the
            # observed seismograms
            if self.obs_format == "acc_bbp":
                # We need to look for the bbp file
                if r_bbp_file not in filelist:
                    # No bbp file for this station
                    continue
                print(r_bbp_file)
                # Copy bbp file to the tmp seismogram directory
                a_src_bbp_file = os.path.join(self.a_obsdir, r_bbp_file)
                a_dst_bbp_file = os.path.join(a_tmpdir_seis, r_bbp_file)
                shutil.copy2(a_src_bbp_file, a_dst_bbp_file)
                # Now we need to create the peer files to process with rotd50
                r_e_peer_file = os.path.join(a_tmpdir_seis,
                                             "%s_E.acc" % (stat))
                r_n_peer_file = os.path.join(a_tmpdir_seis,
                                             "%s_N.acc" % (stat))
                r_z_peer_file = os.path.join(a_tmpdir_seis,
                                             "%s_Z.acc" % (stat))
                bbp_formatter.bbp2peer(a_dst_bbp_file, r_n_peer_file,
                                       r_e_peer_file, r_z_peer_file)
            elif self.obs_format == "acc_peer":
                # Look for the E, N, and Z files
                for my_file in filelist:
                    if my_file.endswith("%s_E.acc" % (stat)):
                        r_e_peer_file = my_file
                        if (r_n_peer_file is not None
                                and r_z_peer_file is not None):
                            break
                    elif my_file.endswith("%s_N.acc" % (stat)):
                        r_n_peer_file = my_file
                        if (r_e_peer_file is not None
                                and r_z_peer_file is not None):
                            break
                    elif my_file.endswith("%s_Z.acc" % (stat)):
                        r_z_peer_file = my_file
                        if (r_e_peer_file is not None
                                and r_n_peer_file is not None):
                            break
                if ((r_e_peer_file is None) or (r_n_peer_file is None)
                        or (r_z_peer_file is None)):
                    # Couldn't find all 3 files
                    continue
                #print(r_e_peer_file, r_n_peer_file, r_z_peer_file)
                # Copy all three files to the tmp seismogram directory
                for eachfile in (r_e_peer_file, r_n_peer_file, r_z_peer_file):
                    a_src_peer_file = os.path.join(self.a_obsdir, eachfile)
                    a_dst_peer_file = os.path.join(a_tmpdir_seis, eachfile)
                    shutil.copy2(a_src_peer_file, a_dst_peer_file)

                # Now we need to convert them into bbp format
                bbp_formatter.peer2bbp(
                    os.path.join(a_tmpdir_seis, r_n_peer_file),
                    os.path.join(a_tmpdir_seis, r_e_peer_file),
                    os.path.join(a_tmpdir_seis, r_z_peer_file),
                    os.path.join(a_tmpdir_seis, r_bbp_file))
            else:
                raise bband_utils.ParameterError("Format %s for " %
                                                 (self.obs_format) +
                                                 "observed seismograms "
                                                 "not supported")

            # Run RotD100 on this file
            if corr_psa is not None:
                # First calculate rd100/50 and psa5 files
                do_rotd100(a_tmpdir_seis, r_e_peer_file, r_n_peer_file,
                           "%s-orig.rd100" % (stat), self.log)

                # Now we need to correct the RotD100/RotD50 output
                # using the user-supplied correction factors
                corr_psa.correct_station(stat, "rd100")
            else:
                # Use final names for output files
                do_rotd100(a_tmpdir_seis, r_e_peer_file, r_n_peer_file,
                           "%s.rd100" % (stat), self.log)
            shutil.copy2(os.path.join(a_tmpdir_seis, "%s.rd100" % (stat)),
                         os.path.join(a_dstdir, "%s.rd100" % (stat)))
Ejemplo n.º 9
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)
Ejemplo n.º 10
0
    def run(self):
        """
        This function copies the observed seismograms for the stations
        specified in r_stations to a temporary directory inside the
        tmpdata directory and converts them to the format needed by
        the goodness of fitness module
        """
        print("ObsSeismograms".center(80, '-'))

        # Initialize basic variables
        install = InstallCfg.getInstance()
        sim_id = self.sim_id
        sta_base = os.path.basename(os.path.splitext(self.r_stations)[0])

        self.log = os.path.join(install.A_OUT_LOG_DIR,
                                str(sim_id),
                                "%d.obs_seis.log" %
                                (sim_id))

        # Input, tmp, and output directories
        a_indir = os.path.join(install.A_IN_DATA_DIR, str(sim_id))
        a_tmpdir = os.path.join(install.A_TMP_DATA_DIR, str(sim_id))
        a_tmpdir_seis = os.path.join(install.A_TMP_DATA_DIR, str(sim_id),
                                     "obs_seis_%s" % (sta_base))
        a_outdir = os.path.join(install.A_OUT_DATA_DIR, str(sim_id))
        a_outdir_seis = os.path.join(a_outdir, "obs_seis_%s" % (sta_base))

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

        # Station file
        a_statfile = os.path.join(a_indir, self.r_stations)
        # List of observed seismogram files
        filelist = os.listdir(self.a_obsdir)

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

        # Inialize the CorrectPSA module
        if self.obs_corrections:
            corr_psa = CorrectPSA(self.r_stations,
                                  "rd50",
                                  os.path.join(a_indir,
                                               self.obs_corrections),
                                  a_tmpdir_seis,
                                  self.sim_id)
        else:
            corr_psa = None

        # Go through each station
        for site in site_list:
            slon = float(site.lon)
            slat = float(site.lat)
            stat = site.scode
            print("==> Processing data for station: %s" % (stat))
            # Since we're using the GP station list, make sure the
            # .rd50 for the station exists.  It might not if we ran the
            # validation with a different station list (like UCSB for
            # Landers)
            expected_rd50_file = os.path.join(a_outdir,
                                              "%d.%s.rd50" %
                                              (sim_id, stat))
            if not os.path.exists(expected_rd50_file):
                # just skip it
                print("Couldn't find file %s. " %
                      (expected_rd50_file) +
                      "This is not necessarily an error, as you may have " +
                      "run with a subset of a stations. Continuing " +
                      "with available stations.")
                continue

            # Ok, we have a calculated rd50 file for this station,
            # let's look for the observed file
            r_e_peer_file = None
            r_n_peer_file = None
            r_z_peer_file = None
            r_bbp_file = "%s.bbp" % (stat)
            # Do different things depending on the format of the
            # observed seismograms
            if self.obs_format == "acc_bbp":
                # We need to look for the bbp file
                if r_bbp_file not in filelist:
                    # No bbp file for this station
                    continue
                print("==> Converting file: %s" % (r_bbp_file))
                # Copy bbp file to the tmp seismogram directory
                a_src_bbp_file = os.path.join(self.a_obsdir, r_bbp_file)
                a_dst_bbp_file = os.path.join(a_tmpdir_seis, r_bbp_file)
                shutil.copy2(a_src_bbp_file, a_dst_bbp_file)
                # Now we need to create the peer files to process with rotd50
                r_e_peer_file = os.path.join(a_tmpdir_seis, "%s_E.acc" % (stat))
                r_n_peer_file = os.path.join(a_tmpdir_seis, "%s_N.acc" % (stat))
                r_z_peer_file = os.path.join(a_tmpdir_seis, "%s_Z.acc" % (stat))
                bbp_formatter.bbp2peer(a_dst_bbp_file,
                                       r_n_peer_file,
                                       r_e_peer_file,
                                       r_z_peer_file)
            elif self.obs_format == "acc_peer":
                # Look for the E, N, and Z files
                for my_file in filelist:
                    if my_file.endswith("%s_E.acc" % (stat)):
                        r_e_peer_file = my_file
                        if (r_n_peer_file is not None and
                            r_z_peer_file is not None):
                            break
                    elif my_file.endswith("%s_N.acc" % (stat)):
                        r_n_peer_file = my_file
                        if (r_e_peer_file is not None and
                            r_z_peer_file is not None):
                            break
                    elif my_file.endswith("%s_Z.acc" % (stat)):
                        r_z_peer_file = my_file
                        if (r_e_peer_file is not None and
                            r_n_peer_file is not None):
                            break
                if ((r_e_peer_file is None) or
                    (r_n_peer_file is None) or
                    (r_z_peer_file is None)):
                    # Couldn't find all 3 files
                    continue
                # print(r_e_peer_file, r_n_peer_file, r_z_peer_file)
                # Copy all three files to the tmp seismogram directory
                for eachfile in (r_e_peer_file, r_n_peer_file, r_z_peer_file):
                    a_src_peer_file = os.path.join(self.a_obsdir, eachfile)
                    a_dst_peer_file = os.path.join(a_tmpdir_seis, eachfile)
                    shutil.copy2(a_src_peer_file, a_dst_peer_file)

                # Now we need to convert them into bbp format
                bbp_formatter.peer2bbp(os.path.join(a_tmpdir_seis,
                                                    r_n_peer_file),
                                       os.path.join(a_tmpdir_seis,
                                                    r_e_peer_file),
                                       os.path.join(a_tmpdir_seis,
                                                    r_z_peer_file),
                                       os.path.join(a_tmpdir_seis,
                                                    r_bbp_file))
            elif self.obs_format == "gmpe":
                # GMPE verification packages don't have actual
                # seismograms, so there's nothing we need to do here!
                r_gmpe_file = "%s-gmpe.ri50" % (stat)
                if r_gmpe_file not in filelist:
                    # No gmpe file for this station
                    continue
                # Copy gmpe file to the tmp obs directory and rename
                # it so that it has a rd50 extension
                a_src_gmpe_file = os.path.join(self.a_obsdir, r_gmpe_file)
                a_dst_rd50_file = os.path.join(a_tmpdir_seis, "%s.rd50" %
                                               (stat))
                shutil.copy2(a_src_gmpe_file, a_dst_rd50_file)
                # Create a copy in outdata that averages all gmpes
                a_avg_rd50_file = os.path.join(a_outdir_seis,
                                               "%s.rd50" % (stat))
                gmpe_config.average_gmpe(stat,
                                         a_src_gmpe_file,
                                         a_avg_rd50_file)
                # All done!
                continue
            else:
                raise bband_utils.ParameterError("Format %s for " %
                                                 (self.obs_format) +
                                                 "observed seismograms "
                                                 "not supported")

            # Run RotD50 on this file
            if corr_psa is not None:
                # First calculate rd50 and psa5 files
                print("===> Calculating RotD50 for station: %s" % (stat))
                RotD50.do_rotd50(a_tmpdir_seis, r_e_peer_file,
                                 r_n_peer_file, r_z_peer_file,
                                 "%s-orig.rd50" % (stat),
                                 self.log)

                # Now we need to correct the RotD50 output using the
                # user-supplied correction factors
                print("===> Correcting PSA for station: %s" % (stat))
                corr_psa.correct_station(stat, "rd50")
            else:
                # Use final names for output files
                print("===> Calculating RotD50 for station: %s" % (stat))
                RotD50.do_rotd50(a_tmpdir_seis, r_e_peer_file,
                                 r_n_peer_file, r_z_peer_file,
                                 "%s.rd50" % (stat),
                                 self.log)
            shutil.copy2(os.path.join(a_tmpdir_seis, "%s.rd50" % (stat)),
                         os.path.join(a_outdir_seis, "%s.rd50" % (stat)))

        print("ObsSeismograms Completed".center(80, '-'))
Ejemplo n.º 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, '-'))
Ejemplo n.º 12
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, '-'))