Changeset 545 in lmdz_wrf for trunk/tools/drawing_tools.py


Ignore:
Timestamp:
Jun 30, 2015, 6:24:47 PM (10 years ago)
Author:
lfita
Message:

Adding `draw_points' to draw points on a shadded map

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tools/drawing_tools.py

    r544 r545  
    5858
    5959# From nc_var_tools.py
     60def reduce_spaces(string):
     61    """ Function to give words of a line of text removing any extra space
     62    """
     63    values = string.replace('\n','').split(' ')
     64    vals = []
     65    for val in values:
     66         if len(val) > 0:
     67             vals.append(val)
     68
     69    return vals
     70
    6071def searchInlist(listname, nameFind):
    6172    """ Function to search a value within a list
     
    13681379    return varvals
    13691380
     1381def lonlat2D(lon,lat):
     1382    """ Function to return lon, lat 2D matrices from any lon,lat matrix
     1383      lon= matrix with longitude values
     1384      lat= matrix with latitude values
     1385    """
     1386    fname = 'lonlat2D'
     1387
     1388    if len(lon.shape) != len(lat.shape):
     1389        print errormsg
     1390        print '  ' + fname + ': longitude values with shape:', lon.shape,            \
     1391          'is different that latitude values with shape:', lat.shape, '(dif. size) !!'
     1392        quit(-1)
     1393
     1394    if len(lon.shape) == 3:
     1395        lonvv = lon[0,:,:]
     1396        latvv = lat[0,:,:]
     1397    elif len(lon.shape) == 2:
     1398        lonvv = lon[:]
     1399        latvv = lat[:]
     1400    elif len(lon.shape) == 1:
     1401        lonlatv = np.meshgrid(lon[:],lat[:])
     1402        lonvv = lonlatv[0]
     1403        latvv = lonlatv[1]
     1404
     1405    return lonvv, latvv
     1406
    13701407#######    #######    #######    #######    #######    #######    #######    #######    #######    #######
    13711408
     
    25002537#quit()
    25012538
    2502 def plot_points(xval, yval, ifile, vtit, kfig, mapv):
     2539def plot_points(xval, yval, vlon, vlat, extravals, extrapar, vtit, mapv, color,      \
     2540  labels, lloc, kfig, figname):
    25032541    """ plotting points
    25042542      [x/yval]: x,y values to plot
    2505       vn,vm= minmum and maximum values to plot
    2506       unit= units of the variable
    2507       ifile= name of the input file
    2508       vtit= title of the variable
    2509       kfig= kind of figure (jpg, pdf, png)
     2543      vlon= 2D-matrix with the longitudes
     2544      vlat= 2D-matrix with the latitudes
     2545      extravals= extra values to be added into the plot (None for nothing)
     2546      extrapar= [varname, min, max, cbar, varunits] of the extra variable
     2547      vtit= title of the graph ('|' for spaces)
    25102548      mapv= map characteristics: [proj],[res]
    25112549        see full documentation: http://matplotlib.org/basemap/
    25122550        [proj]: projection
    25132551          * 'cyl', cilindric
     2552          * 'lcc', lambert-conformal
    25142553        [res]: resolution:
    25152554          * 'c', crude
     
    25182557          * 'h', high
    25192558          * 'f', full
     2559      color= color for the points/labels ('auto', for "red")
     2560      labels= list of labels for the points (None, no labels)
     2561      lloc = localisation of the legend
     2562      kfig= kind of figure (jpg, pdf, png)
     2563      figname= name of the figure
     2564
    25202565    """
    25212566    fname = 'plot_points'
    2522 
    2523     dx=xval.shape[0]
    2524     dy=yval.shape[0]
     2567# Canging line kinds every 7 pts (end of standard colors)
     2568    ptkinds=['.','x','o','*','+','8','>','D','h','p','s']
     2569
     2570    Npts = len(xval)
     2571    if Npts > len(ptkinds)*7:
     2572        print errormsg
     2573        print '  ' + fname + ': too many',Npts,'points!!'
     2574        print "    enlarge 'ptkinds' list"
     2575        quit(-1)
     2576
     2577    N7pts = 0
     2578
     2579    if color == 'auto':
     2580        ptcol = "red"
     2581    else:
     2582        ptcol = color
     2583
     2584    dx=vlon.shape[1]
     2585    dy=vlat.shape[0]
    25252586
    25262587    plt.rc('text', usetex=True)
    25272588
    25282589    if not mapv is None:
    2529         lon00 = np.where(xval[:] < 0., 360. + olon[:], olon[:])
    2530         lat00 = yval[:]
    2531         lon0 = np.zeros( (len(lat00),len(lon00)), dtype=np.float )
    2532         lat0 = np.zeros( (len(lat00),len(lon00)), dtype=np.float )
    2533 
    2534         for iy in range(len(lat00)):
    2535             lon0[iy,:] = lon00
    2536         for ix in range(len(lon00)):
    2537             lat0[:,ix] = lat00
     2590        vlon = np.where(vlon[:] < 0., 360. + vlon[:], vlon[:])
    25382591
    25392592        map_proj=mapv.split(',')[0]
    25402593        map_res=mapv.split(',')[1]
    25412594
    2542         nlon = lon0[0,0]
    2543         xlon = lon0[dy-1,dx-1]
    2544         nlat = lat0[0,0]
    2545         xlat = lat0[dy-1,dx-1]
    2546 
    2547         lon2 = lon0[dy/2,dx/2]
    2548         lat2 = lat0[dy/2,dx/2]
     2595        nlon = np.min(vlon)
     2596        xlon = np.max(vlon)
     2597        nlat = np.min(vlat)
     2598        xlat = np.max(vlat)
     2599
     2600        lon2 = vlon[dy/2,dx/2]
     2601        lat2 = vlat[dy/2,dx/2]
    25492602
    25502603        print 'lon2:', lon2, 'lat2:', lat2, 'SW pt:', nlon, ',', nlat, 'NE pt:',     \
     
    25582611              llcrnrlat=nlat, urcrnrlon=xlon, urcrnrlat= xlat, resolution=map_res)
    25592612
    2560         lons, lats = np.meshgrid(olon[:], olat[:])
    2561         lons = np.where(lons < 0., lons + 360., lons)
    2562 
    2563         x,y = m(lons,lats)
    2564         plt.plot(x,y)
     2613#        lons, lats = np.meshgrid(vlon, vlat)
     2614#        lons = np.where(lons < 0., lons + 360., lons)
     2615
     2616        x,y = m(vlon,vlat)
    25652617
    25662618        m.drawcoastlines()
     
    25712623        parallels = pretty_int(nlat,xlat,5)
    25722624        m.drawparallels(parallels,labels=[False,True,True,False])
    2573     else:
     2625#    else:
    25742626#        plt.xlim(0,dx-1)
    25752627#        plt.ylim(0,dy-1)
    25762628
    2577         plt.plot(xval, yval, '.')
    2578 
    2579     figname = ifile.replace('.','_') + '_' + vtit
     2629# Extra values
     2630    if extravals is not None:
     2631        plt.pcolormesh(x, y, extravals, cmap=plt.get_cmap(extrapar[3]),              \
     2632          vmin=extrapar[1], vmax=extrapar[2])
     2633        cbar = plt.colorbar()
     2634        cbar.set_label(extrapar[0].replace('_','\_') +'('+ units_lunits(extrapar[4])+\
     2635          ')')
     2636
     2637    if labels is not None:
     2638        for iv in range(len(xval)):
     2639#            plt.annotate(labels[iv], xy=(xval[iv],yval[iv]), xycoords='data')
     2640
     2641            if np.mod(iv,7) == 0: N7pts = N7pts + 1
     2642#            print iv,xval[iv],yval[iv],labels[iv],ptkinds[N7pts]
     2643
     2644            plt.plot(xval[iv],yval[iv], ptkinds[N7pts],label=labels[iv])
     2645        plt.legend(loc=lloc)
     2646
     2647    else:
     2648        plt.plot(xval, yval, '.', color=ptcol)
     2649
    25802650    graphtit = vtit.replace('_','\_').replace('&','\&')
    25812651
    2582     plt.title(graphtit)
     2652    plt.title(graphtit.replace('|', ' '))
    25832653   
    25842654    output_kind(kfig, figname, True)
Note: See TracChangeset for help on using the changeset viewer.