예제 #1
0
def collectSembweighted(SembList, Config, Origin, Folder, ntimes, arrays,
                        switch, weights):
    '''
    method to collect semblance matrizes from all processes and write them to file for each timestep
    '''
    Logfile.add('start collect in collectSemb')

    cfg = ConfigObj(dict=Config)
    origin = ConfigObj(dict=Origin)

    dimX = cfg.dimX()  # ('dimx')
    dimY = cfg.dimY()  # ('dimy')
    winlen = cfg.winlen()  # ('winlen')
    step = cfg.step()  # ('step')

    latv = []
    lonv = []

    gridspacing = cfg.Float('gridspacing')
    migpoints = dimX * dimY
    o_lat = origin.lat()  # float (Origin['lat'])
    o_lon = origin.lon()  # float (Origin['lon'])
    oLatul = 0
    oLonul = 0

    z = 0

    for i in xrange(dimX):
        oLatul = o_lat - ((dimX - 1) / 2) * gridspacing + i * gridspacing

        if z == 0 and i == 0:
            Latul = oLatul
        o = 0

        for j in xrange(dimY):
            oLonul = o_lon - ((dimY - 1) / 2) * gridspacing + j * gridspacing

            if o == 0 and j == 0:
                Lonul = oLonul

            latv.append(oLatul)
            lonv.append(oLonul)

    tmp = 1
    for a, w in zip(SembList, weights):
        tmp *= a
    #sys.exit()

    sembmaxvaluev = num.ndarray(ntimes, dtype=float)
    sembmaxlatv = num.ndarray(ntimes, dtype=float)
    sembmaxlonv = num.ndarray(ntimes, dtype=float)

    rc = UTCDateTime(Origin['time'])
    rcs = '%s-%s-%s_%02d:%02d:%02d' % (rc.day, rc.month, rc.year, rc.hour,
                                       rc.minute, rc.second)
    d = rc.timestamp
    usedarrays = 5

    folder = Folder['semb']
    fobjsembmax = open(os.path.join(folder, 'sembmax_%s.txt' % (switch)), 'w')

    for a, i in enumerate(tmp):
        logger.info('timestep %d' % a)

        fobj = open(
            os.path.join(
                folder, '%s-%s_%03d._weighted_semblance.ASC' %
                (switch, Origin['depth'], a)), 'w')
        #fobj = open (os.path.join (folder, '%03d.ASC'    % a),'w')

        fobj.write('# %s , %s\n' % (d, rcs))
        fobj.write('# step %ds| ntimes %d| winlen: %ds\n' %
                   (step, ntimes, winlen))
        fobj.write('# \n')
        fobj.write('# southwestlat: %.2f dlat: %f nlat: %f \n' %
                   (Latul, gridspacing, dimX))
        fobj.write('# southwestlon: %.2f dlon: %f nlon: %f \n' %
                   (Lonul, gridspacing, dimY))
        fobj.write('# ddepth: 0 ndepth: 1 \n')

        sembmax = 0
        sembmaxX = 0
        sembmaxY = 0

        origin = DataTypes.dictToLocation(Origin)
        uncert = num.std(i)  #maybe not std?
        for j in range(migpoints):
            x = latv[j]
            y = lonv[j]
            semb = i[j]

            fobj.write('%.2f %.2f %.20f\n' % (x, y, semb))

            if semb > sembmax:
                sembmax = semb
                # search for maximum and position of maximum on semblance grid for given time step
                sembmaxX = x
                sembmaxY = y

        delta = loc2degrees(Location(sembmaxX, sembmaxY), origin)
        azi = toAzimuth(float(Origin['lat']), float(Origin['lon']),
                        float(sembmaxX), float(sembmaxY))

        sembmaxvaluev[a] = sembmax
        sembmaxlatv[a] = sembmaxX
        sembmaxlonv[a] = sembmaxY

        fobjsembmax.write('%d %.2f %.2f %.20f %.20f %d %03f %f %03f\n' %
                          (a * step, sembmaxX, sembmaxY, sembmax, uncert,
                           usedarrays, delta, float(azi), delta * 119.19))
        fobj.close()

    fobjsembmax.close()

    durationpath = os.path.join(folder, "duration.txt")
    trigger.writeSembMaxValue(sembmaxvaluev, sembmaxlatv, sembmaxlonv, ntimes,
                              Config, Folder)
    print 'DD2: ', durationpath
    trigger.semblancestalta(sembmaxvaluev, sembmaxlatv, sembmaxlonv)
예제 #2
0
def collectSemb(SembList, Config, Origin, Folder, ntimes, arrays, switch,
                array_centers):
    '''
    method to collect semblance matrizes from all processes and write them to file for each timestep
    '''
    Logfile.add('start collect in collectSemb')
    cfg = ConfigObj(dict=Config)
    origin = ConfigObj(dict=Origin)

    dimX = cfg.dimX()  #('dimx')
    dimY = cfg.dimY()  #('dimy')
    if switch == 0:
        winlen = cfg.winlen()  #('winlen')
        step = cfg.step()  #('step')
    if switch == 1:
        winlen = cfg.winlen_f2()  #('winlen')
        step = cfg.step_f2()  #('step')

    latv = []
    lonv = []

    gridspacing = cfg.Float('gridspacing')
    migpoints = dimX * dimY
    o_lat = origin.lat()  # float(Origin['lat'])
    o_lon = origin.lon()  # float(Origin['lon'])
    oLatul = 0
    oLonul = 0

    z = 0

    for i in xrange(dimX):
        oLatul = o_lat - ((dimX - 1) / 2) * gridspacing + i * gridspacing

        if z == 0 and i == 0:
            Latul = oLatul
        o = 0

        for j in xrange(dimY):
            oLonul = o_lon - ((dimY - 1) / 2) * gridspacing + j * gridspacing

            if o == 0 and j == 0:
                Lonul = oLonul

            latv.append(oLatul)
            lonv.append(oLonul)

    tmp = 1
    origin = DataTypes.dictToLocation(Origin)
    i = 0

    #for a in SembList:
    #    tmp = num.zeros(num.shape(a))
    azis = []
    for a in SembList:
        x = array_centers[i][0]
        y = array_centers[i][1]
        delta = orthodrome.distance_accurate50m_numpy(x, y, origin.lat,
                                                      origin.lon)
        #a = a*((1./delta**2)*1.e+15)
        tmp *= a

        #azis.append(toAzimuth(float(Origin['lat']), float(Origin['lon']),x, y))
        i = i + 1

    #min_coor = num.zeros([i,2])
    #i = 0
    #for a in SembList:
    #    deltas = []
#        x = array_centers[i][0]
#        y = array_centers[i][1]
#        for k in range(0,len(latv)):
#            delta = orthodrome.distance_accurate50m_numpy(x, y, latv[k], lonv[k])
#            deltas.append(orthodrome.distance_accurate50m_numpy(x, y, latv[k], lonv[k]))
#            if delta <= num.min(deltas):
#                min_coor[i]= [latv[k], lonv[k]]
#        i = i+1
#    array_overlap = num.average(min_coor, axis=0)
#    delta_center = orthodrome.distance_accurate50m_numpy(array_overlap[0], array_overlap[1], origin.lat, origin.lon)

#    print(array_overlap)

#    print(delta_center)
#    diff_center_lat = origin.lat-array_overlap[0]
#    diff_center_lon = origin.lon-array_overlap[1]
#    print(diff_center_lat)
#    print(diff_center_lon)
#for a in SembList:
#if num.mean(a)>0:
#        tmp *= a

    sembmaxvaluev = num.ndarray(ntimes, dtype=float)
    sembmaxlatv = num.ndarray(ntimes, dtype=float)
    sembmaxlonv = num.ndarray(ntimes, dtype=float)

    rc = UTCDateTime(Origin['time'])
    rcs = '%s-%s-%s_%02d:%02d:%02d' % (rc.day, rc.month, rc.year, rc.hour,
                                       rc.minute, rc.second)
    d = rc.timestamp

    usedarrays = arrays
    folder = Folder['semb']
    fobjsembmax = open(os.path.join(folder, 'sembmax_%s.txt' % (switch)), 'w')
    norm = num.max(num.max(tmp, axis=1))
    max_p = 0.
    sum_i = 0.
    for a, i in enumerate(tmp):
        if a < 1:
            sum_i *= i
    for a, i in enumerate(tmp):
        if a < 1:
            max = num.max(sum_i[:])
            for j in range(migpoints):
                if i[j] > num.max(i[:]) * 0.9 and i[j] > max_p:
                    latvmax = latv[j]
                    lonvmax = lonv[j]
                    max_p = i[j]

#    delta_lat = origin.lat-latvmax
#    delta_lon = origin.lon-lonvmax

#for a, i in enumerate(tmp):
#    max_pos = [l for l, k in enumerate(i) if k == i.max()][0]
#        delta_lat = origin.lat-latv[max_pos]
#        delta_lon = origin.lon-lonv[max_pos]
    for j in range(migpoints):
        latv[j] = latv[j]  #+delta_lat
        lonv[j] = lonv[j]  #+delta_lon
    #        latv.append(latv[j]-delta_lat)
    #        lonv.append(lonv[j]-delta_lon)

    #nix = []
    #for a, i in enumerate(tmp):
    #    for j in range(migpoints):
    #            if i[j]/norm > num.max(sum_i/norm)*0.4:
    #                if j in nix:
    #                    pass
    #                else:
    #                    latv[j] = latv[j]+delta_lat
    #                    lonv[j] = lonv[j]+delta_lon
    #                    nix.append(j)
    #if i[j]/norm > num.max(sum_i/norm)*0.4:
    #    print('yes')
    #    delta_lat = origin.lat-latv[j]
    #    delta_lon = origin.lon-lonv[j]
    #    print delta_lat, delta_lon, latvmax, lonvmax
    #    print latv[j], lonv[j], origin.lat, origin.lon
    #    ix = num.where(latv[j]+delta_lat)[0][0]
    #    iy = num.where(lonv[j]+delta_lon)[0][0]
    #    lat = latv[j].copy()
    #    lon = lonv[j].copy()
    #    latv[j] = latv[ix]
    ##    lonv[j] =  lonv[iy]
    #    lonv[iy]
    #    #latv[j] = latv[j]+delta_lat
    #lonv[j] = lonv[j]+delta_lon
    #    print latv[j], lonv[j]


#

    for a, i in enumerate(tmp):
        logger.info('timestep %d' % a)
        print(a)

        fobj = open(
            os.path.join(folder,
                         '%s-%s_%03d.ASC' % (switch, Origin['depth'], a)), 'w')

        fobj.write('# %s , %s\n' % (d, rcs))
        fobj.write('# step %ds| ntimes %d| winlen: %ds\n' %
                   (step, ntimes, winlen))
        fobj.write('# \n')
        fobj.write('# southwestlat: %.2f dlat: %f nlat: %f \n' %
                   (Latul, gridspacing, dimX))
        fobj.write('# southwestlon: %.2f dlon: %f nlon: %f \n' %
                   (Lonul, gridspacing, dimY))
        fobj.write('# ddepth: 0 ndepth: 1 \n')

        sembmax = 0
        sembmaxX = 0
        sembmaxY = 0

        uncert = num.std(i)  #maybe not std?
        for j in range(migpoints):

            x = latv[j]  #+delta_lat
            y = lonv[j]  #+delta_lon
            #    if i[j]/norm > num.max(i[:]/norm)*0.1:
            #            delta_lat = origin.lat-latv[max_pos]
            #            delta_lon = origin.lon-lonv[max_pos]
            #            print delta_lat, delta_lon, latv[max_pos], lonv[max_pos]
            #            print latv[j], lonv[j], origin.lat, origin.lon
            #        x = latv[j]+delta_lat
            #            y = lonv[j]+delta_lon
            #            print x, y
            semb = i[j] / norm
            fobj.write('%.2f %.2f %.20f\n' % (x, y, semb))
            #    xd= latv[j]-delta_lat
            #        yd= lonv[j]-delta_lon
            #            sembd = 0.
            #            fobj.write('%.2f %.2f %.20f\n' %(xd,yd,sembd))

            if semb > sembmax:
                sembmax = semb
                # search for maximum and position of maximum on semblance grid for given time step
                sembmaxX = x
                sembmaxY = y

        delta = orthodrome.distance_accurate50m_numpy(x, y, origin.lat,
                                                      origin.lon)
        azi = toAzimuth(float(Origin['lat']), float(Origin['lon']),
                        float(sembmaxX), float(sembmaxY))

        sembmaxvaluev[a] = sembmax
        sembmaxlatv[a] = sembmaxX
        sembmaxlonv[a] = sembmaxY
        fobjsembmax.write('%d %.3f %.3f %.30f %.30f %d %03f %f %03f\n' %
                          (a * step, sembmaxX, sembmaxY, sembmax, uncert,
                           usedarrays, delta, float(azi), delta * 119.19))
        fobj.close()

    fobjsembmax.close()

    trigger.writeSembMaxValue(sembmaxvaluev, sembmaxlatv, sembmaxlonv, ntimes,
                              Config, Folder)
    inspect_semb = cfg.Bool('inspect_semb')
    if inspect_semb is True:
        trigger.semblancestalta(sembmaxvaluev, sembmaxlatv, sembmaxlonv)
    return sembmaxvaluev