Ejemplo n.º 1
0
def calculate_distances(src_files, site):
    """
    Calculate Rrup, Rjb, Rx using multiple SRC files
    """
    rrup = 10000000
    rjb = 10000000
    rx = 10000000

    for src_file in src_files:
        src_keys = bband_utils.parse_src_file(src_file)
        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)
        my_rjb, my_rrup, my_rx = putils.DistanceToSimpleFaultSurface(
            site_geom, fault_trace1, up_seis_depth, low_seis_depth, ave_dip)
        rjb = min(my_rjb, rjb)
        rrup = min(my_rrup, rrup)
        rx = min(my_rx, rx)

    return rrup, rjb, rx
Ejemplo n.º 2
0
def load_all_data(comp_label, input_indir, input_obsdir, combined_file,
                  temp_dir, component):
    """
    This function loads all data from each station file
    and creates the structures needed for plotting.
    """
    data = {}

    # Get realizations
    realizations = sorted(os.listdir(input_indir))
    one_realization = realizations[0]
    basedir = os.path.join(input_indir, one_realization)

    # Get the GMPE data for the RZZ2015 metrics
    base_outdir = os.path.join(input_obsdir, one_realization, "validations",
                               "rzz2015_gmpe")
    a_rzz2015_gmpe = glob.glob("%s%s%s.rzz2015gmpe.txt" %
                               (base_outdir, os.sep, one_realization))
    a_rzz2015_gmpe = a_rzz2015_gmpe[0]
    # Get the station list
    a_statfile = glob.glob("%s%s*.stl" % (basedir, os.sep))
    if len(a_statfile) != 1:
        raise bband_utils.ProcessingError("Cannot get station list!")
    a_statfile = a_statfile[0]
    slo = StationList(a_statfile)
    site_list = slo.getStationList()

    # Get source file
    a_srcfile = glob.glob("%s%s*.src" % (basedir, os.sep))
    if len(a_srcfile) != 1:
        raise bband_utils.ProcessingError("Cannot get src file!")
    a_srcfile = a_srcfile[0]

    # Parse it!
    src_keys = bband_utils.parse_src_file(a_srcfile)

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

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

        # Read data for this station
        data_file = os.path.join(temp_dir, "%s.rzz2015" % (stat))

        data[stat] = {}
        data[stat]["dist"] = rrup
        data[stat]["r1"] = []
        data[stat]["r2"] = []
        data[stat]["r3"] = []
        data[stat]["r4"] = []
        data[stat]["r5"] = []
        data[stat]["r1_obs"] = None
        data[stat]["r2_obs"] = None
        data[stat]["r3_obs"] = None
        data[stat]["r4_obs"] = None
        data[stat]["r5_obs"] = None
        data[stat]["r1_gmpe"] = None
        data[stat]["r2_gmpe"] = None
        data[stat]["r3_gmpe"] = None
        data[stat]["r4_gmpe"] = None
        data[stat]["r5_gmpe"] = None

        in_file = open(data_file, 'r')
        for line in in_file:
            line = line.strip()
            if line.startswith("#"):
                # Skip comments
                continue
            pieces = line.split(",")
            comp = pieces[1].strip()
            # Check if we want this component
            if component != "both":
                if comp != component:
                    # Skip
                    continue
            # We want this data point
            pieces = pieces[2:]
            pieces = [float(piece) for piece in pieces]
            # Get observation values
            if data[stat]["r1_obs"] is None:
                data[stat]["r1_obs"] = pieces[6]
            if data[stat]["r2_obs"] is None:
                data[stat]["r2_obs"] = pieces[8]
            if data[stat]["r3_obs"] is None:
                data[stat]["r3_obs"] = pieces[10]
            if data[stat]["r4_obs"] is None:
                data[stat]["r4_obs"] = pieces[12]
            if data[stat]["r5_obs"] is None:
                data[stat]["r5_obs"] = pieces[14]
            # Get simulated data values
            data[stat]["r1"].append(pieces[7])
            data[stat]["r2"].append(pieces[9])
            data[stat]["r3"].append(pieces[11])
            data[stat]["r4"].append(pieces[13])
            data[stat]["r5"].append(pieces[15])
        in_file.close()

    gmpe_file = open(a_rzz2015_gmpe, 'r')
    for line in gmpe_file:
        line = line.strip()
        # Skip comments
        if line.startswith("#"):
            continue
        pieces = line.split(",")
        stat = pieces[0].strip()
        pieces = pieces[1:]
        pieces = [float(piece.strip()) for piece in pieces]
        data[stat]["r1_gmpe"] = pieces[2]
        data[stat]["r2_gmpe"] = pieces[3]
        data[stat]["r3_gmpe"] = pieces[2] / pieces[3]
        data[stat]["r4_gmpe"] = pieces[5]
        data[stat]["r5_gmpe"] = pieces[6]
    gmpe_file.close()

    # Return all data
    return data
Ejemplo n.º 3
0
    a_rd50file2 = os.path.join(a_outdir2, "%d.%s.rd50" % (sim_id_2, stat))
    if not os.path.exists(a_rd50file1) or not os.path.exists(a_rd50file2):
        # Just skip it
        print "Skipping station %s..." % (stat)
        continue

    # 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))
Ejemplo n.º 4
0
    def run(self):
        """
        Runs the GMPEs for the six parameters in Rezaeian (2015)
        """
        print("RZZ2015 GMPE".center(80, '-'))

        # Load configuration, set sim_id
        install = InstallCfg.getInstance()
        sim_id = self.sim_id

        # Build directory paths
        a_tmpdir = os.path.join(install.A_TMP_DATA_DIR, str(sim_id))
        a_indir = os.path.join(install.A_IN_DATA_DIR, str(sim_id))
        a_outdir = os.path.join(install.A_OUT_DATA_DIR, str(sim_id))
        a_logdir = os.path.join(install.A_OUT_LOG_DIR, str(sim_id))
        a_validation_outdir = os.path.join(a_outdir, "validations",
                                           "rzz2015_gmpe")

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

        # Source file, parse it!
        a_srcfile = os.path.join(a_indir, self.srcfile)
        self.src_keys = bband_utils.parse_src_file(a_srcfile)

        # Now the file paths
        self.log = os.path.join(a_logdir, "%d.rzz2015gmpe.log" % (sim_id))
        sta_file = os.path.join(a_indir, self.stations)

        # Get station list
        slo = StationList(sta_file)
        site_list = slo.getStationList()

        # Initialize random seed
        np.random.seed(int(self.src_keys['seed']))

        # Create output file, add header
        out_file = open(
            os.path.join(a_validation_outdir,
                         '%d.rzz2015gmpe.txt' % (self.sim_id)), 'w')
        out_file.write("#station, r_rup, vs_30,"
                       " ai_mean, d595_mean, tmid_mean,"
                       " wmid_mean, wslp_mean, zeta_mean,"
                       " ai_stddev, d595_stddev, tmid_stddev,"
                       " wmid_stddev, wslp_stddev, zeta_stddev\n")

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

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

            vs30 = site.vs30
            mag = self.src_keys['magnitude']
            # Fault type is 1 (Reverse) unless condition below is met
            # Then it is 0 (Strike-pp)
            fault_type = 1
            rake = self.src_keys['rake']
            if ((rake >= -180 and rake < -150) or (rake >= -30 and rake <= 30)
                    or (rake > 150 and rake <= 180)):
                fault_type = 0

            #rrup = 13.94
            #fault_type = 1
            #vs30 = 659.6
            #mag = 7.35

            [ai_mean, d595_mean, tmid_mean, wmid_mean, wslp_mean,
             zeta_mean] = self.calculate_mean_values(rrup, vs30, mag,
                                                     fault_type)

            # Randomize parameters using standard deviations and correlations
            sta_ai = []
            sta_d595 = []
            sta_tmid = []
            sta_wmid = []
            sta_wslp = []
            sta_zeta = []

            # Simulate number_of_samples realizations of the error
            # term for each parameter
            for _ in range(0, self.number_of_samples):
                # Simulate zero-mean normal correlated parameters with
                # stdv = sqrt(sigmai^2+taui^2)
                # totalerror = eps+etha=[eps1+etha1 eps2+etha2 eps3+etha3 eps4+etha4
                #                        eps5+etha5 eps6+etha6]

                # mean error vector
                # m_totalerror = [0, 0, 0, 0, 0, 0]

                # Covariance matrix
                std1 = np.sqrt(self.sigma1**2 + self.tau1**2)
                std2 = np.sqrt(self.sigma2**2 + self.tau2**2)
                std3 = np.sqrt(self.sigma3**2 + self.tau3**2)
                std4 = np.sqrt(self.sigma4**2 + self.tau4**2)
                std5 = np.sqrt(self.sigma5**2 + self.tau5**2)
                std6 = np.sqrt(self.sigma6**2 + self.tau6**2)

                s_total_error = [
                    [
                        std1**2, std1 * std2 * self.rho_totalerror[0][1],
                        std1 * std3 * self.rho_totalerror[0][2],
                        std1 * std4 * self.rho_totalerror[0][3],
                        std1 * std5 * self.rho_totalerror[0][4],
                        std1 * std6 * self.rho_totalerror[0][5]
                    ],
                    [
                        std2 * std1 * self.rho_totalerror[1][0], std2**2,
                        std2 * std3 * self.rho_totalerror[1][2],
                        std2 * std4 * self.rho_totalerror[1][3],
                        std2 * std5 * self.rho_totalerror[1][4],
                        std2 * std6 * self.rho_totalerror[1][5]
                    ],
                    [
                        std3 * std1 * self.rho_totalerror[2][0],
                        std3 * std2 * self.rho_totalerror[2][1], std3**2,
                        std3 * std4 * self.rho_totalerror[2][3],
                        std3 * std5 * self.rho_totalerror[2][4],
                        std3 * std6 * self.rho_totalerror[2][5]
                    ],
                    [
                        std4 * std1 * self.rho_totalerror[3][0],
                        std4 * std2 * self.rho_totalerror[3][1],
                        std4 * std3 * self.rho_totalerror[3][2], std4**2,
                        std4 * std5 * self.rho_totalerror[3][4],
                        std4 * std6 * self.rho_totalerror[3][5]
                    ],
                    [
                        std5 * std1 * self.rho_totalerror[4][0],
                        std5 * std2 * self.rho_totalerror[4][1],
                        std5 * std3 * self.rho_totalerror[4][2],
                        std5 * std4 * self.rho_totalerror[4][3], std5**2,
                        std5 * std6 * self.rho_totalerror[4][5]
                    ],
                    [
                        std6 * std1 * self.rho_totalerror[5][0],
                        std6 * std2 * self.rho_totalerror[5][1],
                        std6 * std3 * self.rho_totalerror[5][2],
                        std6 * std4 * self.rho_totalerror[5][3],
                        std6 * std5 * self.rho_totalerror[5][4], std6**2
                    ]
                ]
                # Matlab returns upper-triangular while Python returns
                # lower-triangular by default -- no need to transpose later!
                r_total_error = np.linalg.cholesky(s_total_error)
                y_total_error = np.random.normal(0, 1, 6)
                total_error = np.dot(r_total_error, y_total_error)

                # Generate randomize parameters in the standardnormal space: ui
                u1 = (self.beta1[0] + self.beta1[1] *
                      (mag / 7.0) + self.beta1[2] * fault_type +
                      self.beta1[3] * math.log(rrup / 25.0) +
                      self.beta1[4] * math.log(vs30 / 750.0)) + total_error[0]
                u2 = (self.beta2[0] + self.beta2[1] * mag +
                      self.beta2[2] * fault_type + self.beta2[3] * rrup +
                      self.beta2[4] * vs30) + total_error[1]
                u3 = (self.beta3[0] + self.beta3[1] * mag +
                      self.beta3[2] * fault_type + self.beta3[3] * rrup +
                      self.beta3[4] * vs30) + total_error[2]
                u4 = (self.beta4[0] + self.beta4[1] * mag +
                      self.beta4[2] * fault_type + self.beta4[3] * rrup +
                      self.beta4[4] * vs30) + total_error[3]
                u5 = (self.beta5[0] + self.beta5[1] * mag +
                      self.beta5[2] * fault_type + self.beta5[3] * rrup +
                      self.beta5[4] * vs30) + total_error[4]
                u6 = (self.beta6[0] + self.beta6[1] * mag +
                      self.beta6[2] * fault_type + self.beta6[3] * rrup +
                      self.beta6[4] * vs30) + total_error[5]

                # Transform parameters ui from standardnormal to the physical space:
                # thetai (constraint: tmid < d_5_95, removed)
                theta1 = norm.ppf(norm.cdf(u1), -4.8255, 1.4318)
                theta2 = 5.0 + (45 - 5) * beta.ppf(norm.cdf(u2), 1.1314,
                                                   2.4474)
                theta3 = 0.5 + (40 - 0.5) * beta.ppf(norm.cdf(u3), 1.5792,
                                                     3.6405)
                theta4 = gamma.ppf(norm.cdf(u4), 4.0982, scale=1.4330)
                theta5 = self.slpinv(norm.cdf(u5), 17.095, 6.7729, 4.8512, -2,
                                     0.5)
                theta6 = 0.02 + (1 - 0.02) * beta.ppf(norm.cdf(u6), 1.4250,
                                                      5.7208)

                sta_ai.append(math.exp(theta1))
                sta_d595.append(theta2)
                sta_tmid.append(theta3)
                sta_wmid.append(theta4)
                sta_wslp.append(theta5)
                sta_zeta.append(theta6)

            # Write output to gmpe file
            out_file.write(
                "%s, %7.4f, %7.2f, " % (stat, rrup, vs30) +
                "%7.4f, %7.4f, %7.4f, %7.4f, %7.4f, %7.4f, " %
                (ai_mean, d595_mean, tmid_mean, wmid_mean, wslp_mean,
                 zeta_mean) + "%7.4f, %7.4f, %7.4f, %7.4f, %7.4f, %7.4f\n" %
                (np.std(sta_ai), np.std(sta_d595), np.std(sta_tmid),
                 np.std(sta_wmid), np.std(sta_wslp), np.std(sta_zeta)))

            ## Write output to file
            #sta_out_file = open(os.path.join(a_validation_outdir,
            #                                 '%d.rzz2015gmpe.%s.txt' %
            #                                 (self.sim_id, stat)), 'w')
            #sta_out_file.write("#ai(s.g^2), d595(s), tmid(s), "
            #                   "wmid(Hz), wslp(Hz/sec), zeta(ratio)\n")
            #for ai, d595, tmid, wmid, wslp, zeta in zip(sta_ai, sta_d595,
            #                                            sta_tmid, sta_wmid,
            #                                            sta_wslp, sta_zeta):
            #    sta_out_file.write("%7.4f, %7.4f, %7.4f, %7.4f, %7.4f, %7.4f\n" %
            #                       (ai, d595, tmid, wmid, wslp, zeta))
            #sta_out_file.close()

            # Generate Plots
            self.plot(stat, a_validation_outdir, rrup, fault_type, vs30, mag,
                      sta_ai, sta_d595, sta_tmid, sta_wmid, sta_wslp, sta_zeta,
                      ai_mean, d595_mean, tmid_mean, wmid_mean, wslp_mean,
                      zeta_mean)

        # Close output file
        out_file.close()
        print("RZZ2015 GMPE Completed".center(80, '-'))
Ejemplo n.º 5
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, '-'))
Ejemplo n.º 6
0
def create_resid_data_file(comp_label, input_indir, input_obsdir,
                           combined_file, temp_dir):
    """
    This function creates a file containing the combined residuals
    from the simulation data from all stations
    """
    # Copy header for first file, set logfile
    copy_header = 1
    logfile = os.path.join(temp_dir, "log.txt")

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

    # Get realizations
    realizations = sorted(os.listdir(input_indir))
    one_realization = realizations[0]
    basedir = os.path.join(input_indir, one_realization)

    # Get the station list
    a_statfile = glob.glob("%s%s*.stl" % (basedir, os.sep))
    if len(a_statfile) != 1:
        raise bband_utils.ProcessingError("Cannot get station list!")
    a_statfile = a_statfile[0]
    slo = StationList(a_statfile)
    site_list = slo.getStationList()

    # Get source file
    a_srcfile = glob.glob("%s%s*.src" % (basedir, os.sep))
    if len(a_srcfile) == 0:
        raise bband_utils.ProcessingError("Cannot get src file!")
    a_srcfile = a_srcfile[0]

    # Parse it!
    src_keys = bband_utils.parse_src_file(a_srcfile)

    # Get the obsdir
    realizations = sorted(os.listdir(input_obsdir))
    one_realization = realizations[0]
    basedir = os.path.join(input_obsdir, one_realization)
    obs_dir = glob.glob("%s%sobs_seis*" % (basedir, os.sep))
    if len(obs_dir) != 1:
        raise bband_utils.ProcessingError("Cannot get observation dir!")
    obs_dir = obs_dir[0]

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

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

        simfile1 = os.path.join(temp_dir, "%s.rd50" % (stat))
        datafile1 = os.path.join(obs_dir, "%s.rd50" % (stat))

        cmd = ("%s bbp_format=1 " %
               (os.path.join(gp_bin_dir, "gen_resid_tbl_3comp")) +
               "datafile1=%s simfile1=%s " % (datafile1, simfile1) +
               "comp1=psa5n comp2=psa5e comp3=rotd50 " +
               "eqname=%s mag=0.0 stat=%s lon=%.4f lat=%.4f " %
               (comp_label, 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" %
               (copy_header, combined_file, logfile))
        bband_utils.runprog(cmd, abort_on_error=True)

        if copy_header == 1:
            copy_header = 0
Ejemplo n.º 7
0
    def calculate_residuals(self, station, gmpe_model, gmpe_data, obs_periods,
                            obs_data, resid_file, print_headers):
        """
        This function calculates the residuals for the gmpe data
        versus the obs_data, and outputs the results to the resid_file
        """
        # Get gmpe periods
        gmpe_periods = [points[0] for points in gmpe_data]
        # Find common set
        period_set = sorted(list(set(gmpe_periods).intersection(obs_periods)))
        # Create new index arrays for observations and gmpes
        gmpe_items = []
        obs_items = []
        for period in period_set:
            gmpe_items.append(gmpe_periods.index(period))
            obs_items.append(obs_periods.index(period))
        # Get gmpe data array
        gmpe_group = gmpe_config.GMPES[self.gmpe_group_name]
        index = gmpe_group["models"].index(gmpe_model)
        res1 = [points[1][index] for points in gmpe_data]

        # Update gmpe_data, and obs_data arrays
        gmpe_points = []
        obs_points = []
        for item1, item2 in zip(gmpe_items, obs_items):
            gmpe_points.append(res1[item1])
            obs_points.append(obs_data[item2])

        # Calculate residuals
        for idx in range(0, len(obs_points)):
            if gmpe_points[idx] != 0.0:
                gmpe_points[idx] = math.log(obs_points[idx] / gmpe_points[idx])
            else:
                gmpe_points[idx] = -99

        # Now, output to file
        if print_headers:
            outf = open(resid_file, 'w')
            outf.write(
                "%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s" %
                ("EQ", "Mag", "stat", "lon", "lat", "stat_seq_no", "Vs30",
                 "close_dist", "Xcos", "Ycos", "T_min", "T_max", "comp"))
            for period in period_set:
                outf.write("\t%.5e" % (period))
            outf.write("\n")
        else:
            outf = open(resid_file, 'a')

        # 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(station.lon), float(station.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)

        outf.write("%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s" %
                   (self.comp_label, str(
                       self.src_keys['magnitude']), station.scode, station.lon,
                    station.lat, "-999", station.vs30, rrup, "-999", "-999"))

        if station.high_freq_corner > 0:
            outf.write("\t%.3f" % (1.0 / station.high_freq_corner))
        else:
            outf.write("\t-99999.999")
        if station.low_freq_corner > 0:
            outf.write("\t%.3f" % (1.0 / station.low_freq_corner))
        else:
            outf.write("\t-99999.999")
        outf.write("\t%s" % (gmpe_model.lower()))
        for value in gmpe_points:
            outf.write("\t%.5e" % (value))
        outf.write("\n")
        outf.close()

        return period_set
Ejemplo n.º 8
0
    def run(self):
        """
        This function generates velocity and/or acceleration
        seismogram plots, as requested by the user
        """
        print("Plot Seismograms".center(80, '-'))

        if not self.plot_vel and not self.plot_acc:
            # Nothing needs to be plotted
            return
        install = InstallCfg.getInstance()
        sim_id = self.sim_id

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

        a_statlist = os.path.join(a_indir, self.r_stations)
        slo = StationList(a_statlist)
        site_list = slo.getStationList()

        # Get fault information, if available
        if self.src_keys is not None:
            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'])

        for site in site_list:
            print("==> Plotting station: %s" % (site.scode))
            # Calculate Rrup
            rrup = None
            if self.src_keys is not None:
                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)

            # Check if we need to plot velocity seismograms
            if self.plot_vel:
                print("===> Plotting velocity...")
                filename = os.path.join(a_outdir,
                                        "%d.%s.vel.bbp" % (sim_id, site.scode))
                outfile = os.path.join(
                    a_outdir, "%d.%s_velocity_seis.png" % (sim_id, site.scode))
                plot_seismograms.plot_seis(site.scode,
                                           filename,
                                           sim_id,
                                           'vel',
                                           outfile,
                                           rrup=rrup)
            # Check if we need to plot acceleration seismograms
            if self.plot_acc:
                print("===> Plotting acceleration...")
                filename = os.path.join(a_outdir,
                                        "%d.%s.acc.bbp" % (sim_id, site.scode))
                outfile = os.path.join(
                    a_outdir,
                    "%d.%s_acceleration_seis.png" % (sim_id, site.scode))
                plot_seismograms.plot_seis(site.scode,
                                           filename,
                                           sim_id,
                                           'acc',
                                           outfile,
                                           rrup=rrup)

        print("Plot Seismograms Completed".center(80, '-'))
Ejemplo n.º 9
0
    def run(self):
        """
        Run the AS16 validation for all stations
        """
        print("AS2016".center(80, '-'))

        # Load configuration, set sim_id
        install = InstallCfg.getInstance()
        sim_id = self.sim_id

        # Build directory paths
        a_tmpdir = os.path.join(install.A_TMP_DATA_DIR, str(sim_id))
        a_indir = os.path.join(install.A_IN_DATA_DIR, str(sim_id))
        a_outdir = os.path.join(install.A_OUT_DATA_DIR, str(sim_id))
        a_logdir = os.path.join(install.A_OUT_LOG_DIR, str(sim_id))
        a_validation_outdir = os.path.join(a_outdir, "validations",
                                           "stewart_duration_gmpe")

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

        # Now the file paths
        self.log = os.path.join(a_logdir, "%d.as16.log" % (sim_id))
        sta_file = os.path.join(a_indir, self.stations)
        a_srcfile = os.path.join(a_indir, self.srcfile)

        # Read SRC file
        src_keys = bband_utils.parse_src_file(a_srcfile)

        # Load information from SRC file
        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'])

        # Set region to be unknown -- this has no effect in the AS16
        # method as z1 is not provided and that causes dz1 to be set
        # to zero and override the cj parameter
        cj = -999

        # Figure out what mechanism to use
        # 0 = unknown
        # 1 = normal
        # 2 = reverse
        # 3 = strike-slip
        rake = src_keys['rake']
        if abs(rake) <= 30 or abs(rake) >= 150:
            mechanism = 3
        elif rake > 30 and rake < 150:
            mechanism = 2
        elif rake < -30 and rake > -150:
            mechanism = 1
        else:
            print("Warning: unknown mechanism for rake = %f" % (rake))
            mechanism = 0

        # Get station list
        slo = StationList(sta_file)
        site_list = slo.getStationList()

        # Create output file, add header
        out_file = open(
            os.path.join(a_validation_outdir,
                         '%d.as16.%s.txt' % (self.sim_id, self.eventname)),
            'w')
        out_file.write("#station, rrup, vs30, sd575, sd595, sd2080,"
                       " tau575, tau595, tau2080, phi575, phi595, phi2080\n")

        # Go through each station
        for site in site_list:
            stat = site.scode
            vs30 = float(site.vs30)

            # Calculate Rrup
            site_geom = [site.lon, 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)

            results = calculate_as16(src_keys['magnitude'], rrup, mechanism,
                                     vs30, -999.0, cj)

            out_file.write("%s, %3.2f, %3.2f" % (stat, rrup, vs30))
            for piece in results:
                out_file.write(", %7.5f" % (piece))
            out_file.write("\n")

        # All done, close output file
        out_file.close()

        # All done!
        print("AS2016 Completed".center(80, '-'))
Ejemplo n.º 10
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
Ejemplo n.º 11
0
    def calculate_gmpe(self, station, output_file):
        """
        This function calculates the gmpe for a station and writes the
        output in the output_file
        """
        gmpe_group = gmpe_config.GMPES[self.gmpe_group_name]
        src_keys = self.src_keys
        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'])

        # Station location
        site_geom = [float(station.lon), float(station.lat), 0.0]
        (fault_trace1, upper_seis_depth, lower_seis_depth, ave_dip, dummy1,
         dummy2) = putils.FaultTraceGen(origin, dims, mech)
        rjb, rrup, rx = putils.DistanceToSimpleFaultSurface(
            site_geom, fault_trace1, upper_seis_depth, lower_seis_depth,
            ave_dip)
        # If using corrected ground observation data, calcualte GMPEs
        # with a Vs30 of 863 m/s
        if self.data_corrected:
            vs30 = 863
        else:
            vs30 = station.vs30
        z10 = None  # Let PyNGA calculate it
        z25 = None  # Let PyNGA calculate it

        # Compute PSA for this station
        station_median = []
        for period in gmpe_group["periods"]:
            period_medians = []
            for nga_model in gmpe_group["models"]:
                median = gmpe_config.calculate_gmpe(
                    self.gmpe_group_name,
                    nga_model,
                    src_keys['magnitude'],
                    rjb,
                    vs30,
                    period,
                    rake=src_keys['rake'],
                    dip=src_keys['dip'],
                    W=src_keys['fault_width'],
                    Ztor=src_keys['depth_to_top'],
                    Rrup=rrup,
                    Rx=rx,
                    Z10=z10,
                    Z25=z25)
                period_medians.append(median)
            station_median.append((period, period_medians))

        # Create label
        file_label = ""
        for nga_model in gmpe_group["models"]:
            file_label = "%s %s" % (file_label, nga_model)
        # Output data to file
        outfile = open(output_file, 'w')
        outfile.write("#station: %s\n" % (station.scode))
        outfile.write("#period%s\n" % (file_label))
        for item in station_median:
            period = item[0]
            vals = item[1]
            out_str = "%.4f" % (period)
            for method in vals:
                out_str = out_str + "\t%.6f" % (method)
            outfile.write("%s\n" % (out_str))
        outfile.close()

        # Return list
        return station_median
Ejemplo n.º 12
0
def calculate_gmpe(src_keys, station, output_file, rrups, gmpe_group_name):
    """
    Calculate the GMPE results for a given station.
    """
    gmpe_group = gmpe_config.GMPES[gmpe_group_name]
    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'])

    # Station location
    site_geom = [float(station.lon), float(station.lat), 0.0]
    (fault_trace1, upper_seis_depth,
     lower_seis_depth, ave_dip,
     dummy1, dummy2) = putils.FaultTraceGen(origin, dims, mech)
    rjb, rrup, rx = putils.DistanceToSimpleFaultSurface(site_geom,
                                                        fault_trace1,
                                                        upper_seis_depth,
                                                        lower_seis_depth,
                                                        ave_dip)

    print "station: %s, Rrup: %f" % (station.scode, rrup)
    rrups.append(rrup)

    vs30 = 1000
    z10 = None # Let PyNGA calculate it
    z25 = None # Let PyNGA calculate it

    # Compute PSA for this stations
    station_median = []
    for period in gmpe_group["periods"]:
        period_medians = []
        for nga_model in gmpe_group["models"]:
            median = gmpe_config.calculate_gmpe(gmpe_group_name,
                                                nga_model,
                                                src_keys['magnitude'],
                                                rjb, vs30,
                                                period,
                                                rake=src_keys['rake'],
                                                dip=src_keys['dip'],
                                                W=src_keys['fault_width'],
                                                Ztor=src_keys['depth_to_top'],
                                                Rrup=rrup, Rx=rx,
                                                Z10=z10, Z25=z25)
            period_medians.append(median)
        station_median.append((period, period_medians))

    # Create label
    file_label = ""
    for nga_model in gmpe_group["models"]:
        file_label = "%s %s" % (file_label, nga_model)
    # Output data to file
    outfile = open(output_file, 'w')
    outfile.write("#station: %s\n" % (station.scode))
    outfile.write("#period%s\n" % (file_label))
    for item in station_median:
        period = item[0]
        vals = item[1]
        out_str = "%.4f" % (period)
        for method in vals:
            out_str = out_str + "\t%.6f" % (method)
        outfile.write("%s\n" % (out_str))
    outfile.close()

    # Return list
    return station_median