コード例 #1
0
def gp_subset(in_file1, in_format1, in_file2, outfile):
    """
    Takes two input stat files in a given format and outputs
    an intersection file in GP format
    """
    #Get a station list from in_file1
    if in_format1 == 'GP' or in_format1 == 'UCSB':
        stat_list = StationList(in_file1).getStationList()
        stat_names = [stat.scode for stat in stat_list]
    elif in_format1 == 'SDSU':
        stat_file_fp = open(in_file1, 'r')
        data = stat_file_fp.readlines()
        stat_file_fp.close()
        for i in range(0, len(data)):
            pieces = data[i].split()
            if len(pieces) > 1:
                if pieces[1] == 'X':
                    break
        stat_names = []
        for j in range(i + 1, len(data)):
            pieces = data[j].split()
            stat_names.append(pieces[2])
    else:
        raise bband_utils.ParameterError("Format %s is not supported." %
                                         (in_format1))

    # Use the station list to subset in_file2
    intersect_list = []
    stat2_list = StationList(in_file2).getStationList()
    for stat2 in stat2_list:
        for stat1 in stat_names:
            if stat2.scode == stat1:
                intersect_list.append(stat2)
    StationList.build(intersect_list, outfile)
コード例 #2
0
def sdsu2uc_subset(sdsu_stat_file, uc_stalist_in, uc_vs30_in,
                   uc_stalist_out, uc_vs30_out):
    # Read input file
    stat_file_fp = open(sdsu_stat_file, "r")
    data = stat_file_fp.readlines()
    stat_file_fp.close()
    for i in range(0, len(data)):
        pieces = data[i].split()
        if len(pieces) > 1:
            if pieces[1] == "X":
                break
    stat_names = []
    for j in range(i + 1, len(data)):
        pieces = data[j].split()
        stat_names.append(pieces[2])

    new_list = []
    slo = StationList(uc_stalist_in).getStationList()
    for stat in stat_names:
        for entry in slo:
            if stat == entry.scode:
                new_list.append(entry)
    StationList.build(new_list, uc_stalist_out)

    fp = open(uc_vs30_in, 'r')
    vs30_dict = dict()
    for line in fp.readlines():
        pieces = line.split()
        vs30_dict[pieces[0]] = pieces[1]
    fp.close()

    fp = open(uc_vs30_out, 'w')
    for stat in stat_names:
        if stat in vs30_dict:
            fp.write("\t%s\t%s\n" % (stat, vs30_dict[stat]))
    fp.flush()
    fp.close()