Exemplo n.º 1
0
def seed_attributes(atr_in, x, y):
    atr = dict()
    for key, value in atr_in.iteritems():
        atr[key] = str(value)

    atr['ref_y'] = str(y)
    atr['ref_x'] = str(x)
    if 'X_FIRST' in atr.keys():
        atr['ref_lat'] = str(subset.coord_radar2geo(y, atr, 'y'))
        atr['ref_lon'] = str(subset.coord_radar2geo(x, atr, 'x'))

    return atr
Exemplo n.º 2
0
 def format_coord(x,y):
     col = int(x)
     row = int(y)
     if 0 <= col < data0.shape[1] and 0 <= row < data0.shape[0]:
         z = data0[row,col]
         if 'X_FIRST' in atr0.keys():
             lat = sub.coord_radar2geo(row, atr0, 'row')
             lon = sub.coord_radar2geo(col, atr0, 'col')
             return 'lon=%.4f, lat=%.4f, x=%.0f,  y=%.0f,  value=%.4f' % (lon, lat, x,y,z)
         else:
             return 'x=%.0f,  y=%.0f,  value=%.4f'%(x,y,z)
     else:
         return 'x=%.0f,  y=%.0f'%(x,y)
Exemplo n.º 3
0
def seed_attributes(atr_in,x,y):
    atr = dict()
    for key, value in atr_in.iteritems():  atr[key] = str(value)
    
    atr['ref_y']=y
    atr['ref_x']=x
    try:
        atr['X_FIRST']
        lat = subset.coord_radar2geo(y,atr,'y')
        lon = subset.coord_radar2geo(x,atr,'x')
        atr['ref_lat']=lat
        atr['ref_lon']=lon
        geocoord='yes'
    except: geocoord='no'

    return atr
Exemplo n.º 4
0
def seed_attributes(atr_in,x,y):
    atr = dict()
    for key, value in atr_in.iteritems():  atr[key] = str(value)
    
    atr['ref_y']=y
    atr['ref_x']=x
    try:
        atr['X_FIRST']
        lat = sub.coord_radar2geo(y,atr,'y')
        lon = sub.coord_radar2geo(x,atr,'x')
        atr['ref_lat']=lat
        atr['ref_lon']=lon
        geocoord='yes'
    except: geocoord='no'

    return atr
Exemplo n.º 5
0
def transect_yx(z,atr,start_yx,end_yx,interpolation='nearest'):
    '''Extract 2D matrix (z) value along the line [x0,y0;x1,y1]
    Ref link: http://stackoverflow.com/questions/7878398/how-to-e
              xtract-an-arbitrary-line-of-values-from-a-numpy-array
    
    Inputs:
        z        - (np.array)   2D data matrix
        atr      - (dictionary) 2D data matrix attribute dictionary
        start_yx - (list) y,x coordinate of start point
        end_yx   - (list) y,x coordinate of end   point
        interpolation - sampling/interpolation method, including:
                'nearest'  - nearest neighbour, by default
                'cubic'    - cubic interpolation
                'bilinear' - bilinear interpolation
    
    Output:
        transect - N*2 matrix containing distance - 1st col - and its corresponding 
                   values - 2nd col - along the line, N is the number of points.
    
    Example:
        transect = transect_yx(dem,demRsc,[10,15],[100,115])
    '''

    ## Extract the line
    [y0,x0] = start_yx
    [y1,x1] = end_yx
    length = int(np.hypot(x1-x0, y1-y0))
    x, y = np.linspace(x0, x1, length), np.linspace(y0, y1, length)
    transect = np.zeros([length,2])

    ## Y - Extract the value along the line
    if   interpolation.lower() == 'nearest' : zi = z[np.rint(y).astype(np.int), np.rint(x).astype(np.int)]
    elif interpolation.lower() == 'cubic'   : zi = scipy.ndimage.map_coordinates(z,np.vstack((y,x)))
    elif interpolation.lower() == 'bilinear': zi = scipy.ndimage.map_coordinates(z,np.vstack((y,x)),order=2)
    else:
        print 'Unrecognized interpolation method: '+interpolation
        print 'Continue with nearest ...'
        zi = z[np.rint(y).astype(np.int), np.rint(x).astype(np.int)]  # nearest neighbour
    transect[:,1] = zi

    ## X - Distance along the line
    earth_radius = 6371.0e3;    # in meter
    try:
        atr['X_FIRST']
        [lat0,lat1] = sub.coord_radar2geo([y0,y1],atr,'y')
        x_step = float(atr['X_STEP'])*np.pi/180.0*earth_radius*np.sin((lat0+lat1)/2*np.pi/180)
        y_step = float(atr['Y_STEP'])*np.pi/180.0*earth_radius
    except:
        x_step = ut.range_resolution(atr)
        y_step = ut.azimuth_resolution(atr)
    dis_x = (x-x0)*x_step
    dis_y = (y-y0)*y_step
    transect[:,0] = np.hypot(dis_x,dis_y)

    return transect
Exemplo n.º 6
0
def seed_file_inps(File, inps=None, outFile=None):
    '''Seed input file with option from input namespace
    Return output file name if succeed; otherwise, return None
    '''
    # Optional inputs
    if not outFile:  outFile = 'Seeded_'+os.path.basename(File)
    if not inps:  inps = cmdLineParse([''])
    print '----------------------------------------------------'
    print 'seeding file: '+File
    
    # Get stack and mask
    stack = ut.get_file_stack(File, inps.mask_file)
    mask = ~np.isnan(stack)
    if np.nansum(mask) == 0.0:
        print '\n*****************************************************'
        print   'ERROR:'
        print   'There is no pixel that has valid phase value in all datasets.' 
        print   'Check the file!'
        print   'Seeding failed'
        sys.exit(1)

    atr = readfile.read_attribute(File)
    # 1. Reference using global average 
    if inps.method == 'global-average':
        print '\n---------------------------------------------------------'
        print 'Automatically Seeding using Global Spatial Average Value '
        print '---------------------------------------------------------'
        print 'Calculating the global spatial average value for each epoch'+\
              ' of all valid pixels ...'
        width = int(atr['WIDTH'])
        length = int(atr['FILE_LENGTH'])
        box = (0,0,width,length)
        meanList = ut.spatial_average(File, mask, box)[0]
        inps.ref_y = ''
        inps.ref_x = ''
        outFile = seed_file_reference_value(File, outFile, meanList, inps.ref_y, inps.ref_x)
        return outFile

    # 2. Reference using specific pixel
    # 2.1 Find reference y/x
    if not inps.ref_y or not inps.ref_x:
        if inps.coherence_file:
            inps.method = 'max-coherence'
            inps.ref_y, inps.ref_x = select_max_coherence_yx(inps.coherence_file, mask, inps.min_coherence)
        elif inps.method == 'random':
            inps.ref_y, inps.ref_x = random_select_reference_yx(mask)
        elif inps.method == 'manual':
            inps = manual_select_reference_yx(stack, inps)

    # 2.2 Seeding file with reference y/x
    if inps.ref_y and inps.ref_x and mask[inps.ref_y, inps.ref_x]:
        if inps.mark_attribute:
            re_select = True
            try:
                ref_x_orig == int(atr['ref_x'])
                ref_y_orig == int(atr['ref_y'])
                if inps.ref_x == ref_x_orig and inps.ref_y == ref_y_orig:
                    re_select = False
                    print 'Same reference pixel is already selected/saved in file, skip updating file attributes'
            except: pass
            if re_select:
                print 'Add/update ref_x/y attribute to file: '+File
                atr_ref = dict()
                atr_ref['ref_x'] = str(inps.ref_x)
                atr_ref['ref_y'] = str(inps.ref_y)
                if 'X_FIRST' in atr.keys():
                    atr_ref['ref_lat'] = str(subset.coord_radar2geo(inps.ref_y, atr, 'y'))
                    atr_ref['ref_lon'] = str(subset.coord_radar2geo(inps.ref_x, atr, 'x'))
                print atr_ref
                outFile = ut.add_attribute(File, atr_ref)
        else:
            print 'Referencing input file to pixel in y/x: (%d, %d)'%(inps.ref_y, inps.ref_x)
            box = (inps.ref_x, inps.ref_y, inps.ref_x+1, inps.ref_y+1)
            refList = ut.spatial_average(File, mask, box)[0]
            outFile = seed_file_reference_value(File, outFile, refList, inps.ref_y, inps.ref_x)
    else:
        raise ValueError('Can not find reference y/x or Nan value.')

    return outFile