Changeset 2601 in lmdz_wrf for trunk/tools/generic_tools.py


Ignore:
Timestamp:
Jun 12, 2019, 8:40:32 PM (5 years ago)
Author:
lfita
Message:

Adding:

  • `point_georef': Function to georeference the matrix coordinates of a given point using a couple of 2D longitude,latitude fields
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tools/generic_tools.py

    r2540 r2601  
    191191#  [YYYY][MM][DD][HH][MI][SS] format) in a 360 years calendar
    192192# points_2Dline: Function to define a line from a pair of points on a plane
     193# point_georef: Function to georeference the matrix coordinates of a given point using a
     194#   couple of 2D longitude,latitude fields
    193195# PolyArea: Function to compute the area of the polygon following 'Shoelace formula'
    194196# pretty_int: Function to plot nice intervals
     
    1676816770    return values
    1676916771
     16772def point_georef(lonv, latv, ivalue, jvalue, kind):
     16773    """ Function to georeference the matrix coordinates of a given point using a
     16774        couple of 2D longitude,latitude fields
     16775      lonv: 2D longitude values
     16776      latv: 2D latitude values
     16777      ivalue: x-coordinate of the point to interpolate
     16778      jvalue: y-coordinate of the point to interpolate
     16779      kind: kind of georefernce methodology
     16780        - 'nearest': using the nearest values
     16781        - '4invdist': using the inverted distance of the 4 closest ones
     16782      >>> lon1D = [5., 10., 15.]
     16783      >>> lat1D = [-25., -30., -35.]
     16784      >>> lon, lat = np.meshgrid(lon1D, lat1D)
     16785      >>> ipt = 1.25
     16786      >>> jpt = 0.75
     16787      >>> point_georef(lon, lat, ipt, jpt, 'nearest')
     16788      (10.0, -30.0)
     16789      >>> point_georef(lon, lat, ipt, jpt, '4invdist')
     16790      (11.751864530113494, -28.248135469886506)
     16791    """
     16792    fname = 'point_georef'
     16793    availkind = ['4invdist', 'nearest']
     16794   
     16795    iix = int(ivalue)
     16796    iiy = int(jvalue)
     16797    dimx = lonv.shape[1]
     16798    dimy = lonv.shape[0]
     16799    iix1 = np.min([dimx,iix+1])
     16800    iiy1 = np.min([dimy,iiy+1])
     16801    if kind == '4invdist':
     16802        dists = np.zeros((4), dtype=np.float)
     16803        lons = np.zeros((4), dtype=np.float)
     16804        lats = np.zeros((4), dtype=np.float)
     16805
     16806        dists[0] = np.sqrt((iix*1.-ivalue*1.)**2+(iiy*1.-jvalue*1.)**2)
     16807        dists[1] = np.sqrt((iix1*1.-ivalue*1.)**2+(iiy*1.-jvalue*1.)**2)
     16808        dists[2] = np.sqrt((iix*1.-ivalue*1.)**2+(iiy1*1.-jvalue*1.)**2)
     16809        dists[3] = np.sqrt((iix1*1.-ivalue*1.)**2+(iiy1*1.-jvalue*1.)**2)
     16810
     16811        lons = [lonv[iiy,iix], lonv[iiy,iix1], lonv[iiy1,iix], lonv[iiy1,iix1]]
     16812        lats = [latv[iiy,iix], latv[iiy,iix1], latv[iiy1,iix], latv[iiy1,iix1]]
     16813
     16814        distsum = np.sum(dists)
     16815        wghts = dists/distsum
     16816
     16817        distsum = np.sum(1./dists)
     16818        wghts = 1./dists/distsum
     16819
     16820        plon = np.sum(lons*wghts)
     16821        plat = np.sum(lats*wghts)
     16822
     16823    elif kind == 'nearest':
     16824        if (ivalue - int(ivalue)) <= 0.5: iix = int(ivalue)
     16825        else: iix = iix1
     16826        if (jvalue - int(jvalue)) <= 0.5: iiy = int(jvalue)
     16827        else: iiy = iiy1
     16828
     16829        plon = lonv[iiy, iix]
     16830        plat = latv[iiy, iix]
     16831    else:
     16832        print errormsg
     16833        print '  ' + fname + ": kind of georeference method '" + kind +              \
     16834          "' not ready !!"
     16835        print '   available ones:', availkind
     16836        quit(-1)
     16837
     16838    return plon, plat
     16839
    1677016840#quit()
    1677116841
    1677216842
     16843
Note: See TracChangeset for help on using the changeset viewer.