Beispiel #1
0
def plotArray(x,y,data,llLon=None, llLat=None, urLon=None,
              urLat=None, res='i', dl=10., datarange=(-1.,1.),
              cmap='jet_r',title=None, xlab='Longitude',
              ylab='Latitude', clab=None, maskland=False,
              maskocean=False, outputFile=None):
    """
    Plot a grid of values using pyplot.pcolormesh() to create a pseudocolour
    plot of a 2-dimensional array, with a basemap included.
    """
    #pyplot.rcdefaults()

    if NO_BASEMAP:
        return

    pyplot.figure()
    if (len(x.shape)<2)and(len(y.shape)<2):
        [xx,yy] = meshgrid(x,y)
    else:
        xx = x
        yy = y
    if llLon is not None:
        llcrnrlon = llLon
    else:
        llcrnrlon = x.min()

    if llLat is not None:
        llcrnrlat = llLat
    else:
        llcrnrlat = y.min()

    if urLon is not None:
        urcrnrlon = urLon
    else:
        urcrnrlon = x.max()

    if urLat is not None:
        urcrnrlat = urLat
    else:
        urcrnrlat = y.max()
    meridians = arange(dl*floor(llcrnrlon/dl), dl*ceil(urcrnrlon/dl), dl)
    parallels = arange(dl*floor(llcrnrlat/dl), dl*ceil(urcrnrlat/dl), dl)
    m = Basemap(projection='cyl',
                resolution=res,
                llcrnrlon=llcrnrlon,
                urcrnrlon=urcrnrlon,
                llcrnrlat=llcrnrlat,
                urcrnrlat=urcrnrlat)

    # Set the colour map:
    if hasattr(cm, cmap):
        cmap = getattr(cm, cmap)
    else:
        cmap = colours.colourMap(cmap, 'stretched')

    if maskocean:
        try:
            from mpl_toolkits.basemap import maskoceans
        except ImportError:
            logger.debug("Maskoceans module unavailable, skipping this command")
            pass
        else:
            datam = maskoceans(xx,yy,data,inlands=False)
            m.pcolormesh(xx,yy,datam,edgecolors='None',vmin=datarange[0],vmax=datarange[1],cmap=cmap)
    else:
        m.pcolormesh(xx,yy,data,edgecolors='None',vmin=datarange[0],vmax=datarange[1],cmap=cmap)
    cb = pyplot.colorbar(shrink=0.5, orientation='horizontal', extend='both',pad=0.05)
    if cb.orientation=='horizontal':
        for t in cb.ax.get_xticklabels():
            t.set_fontsize(10)
    if clab is not None:
        cb.set_label(clab)

    m.drawcoastlines(linewidth=0.5)
    if maskland:
        m.fillcontinents(color='white')
    m.drawparallels(parallels, labels=[1,0,0,0], fontsize=9, linewidth=0.2)
    m.drawmeridians(meridians, labels=[0,0,0,1], fontsize=9, linewidth=0.2)
    pyplot.grid(True)

    # Save the image:
    if outputFile:
        pyplot.savefig(outputFile)
    else:
        pyplot.show()
Beispiel #2
0
def plotField(x, y, data, llLon=None, llLat=None, urLon=None, urLat=None,
              res='i', dl=10., levels=10, cmap='jet', smoothing=False,
              title=None, xlab='Longitude', ylab='Latitude', clab=None,
              maskland=False,maskocean=False,outputFile=None,fill=True):
    """
    plotField:
    The main function. This function sets up the map instance (using
    Basemap), contours the data over the map, applies labels, titles
    and a colourbar.
    """
    if NO_BASEMAP:
        return

    pyplot.rcdefaults()
    pyplot.figure()
    if (len(x.shape)<2)and(len(y.shape)<2):
        [xx,yy] = meshgrid(x,y)
    else:
        xx = x
        yy = y
    if llLon is not None:
        llcrnrlon = llLon
    else:
        llcrnrlon = x.min()

    if llLat is not None:
        llcrnrlat = llLat
    else:
        llcrnrlat = y.min()

    if urLon is not None:
        urcrnrlon = urLon
    else:
        urcrnrlon = x.max()

    if urLat is not None:
        urcrnrlat = urLat
    else:
        urcrnrlat = y.max()

    # Apply smoothing:
    if smoothing:
        logger.debug("Applying smoothing to data")
        if hasattr(data,'mask'):
            # Masked array
            data.data[:] = smooth(data.data, smoothing)
        else:
            data = smooth(data, smoothing)

    # Set the colour map:
    if hasattr(cm, cmap):
        cmap = getattr(cm, cmap)
    else:
        cmap = colours.colourMap(cmap, 'stretched')

    meridians = arange(dl*floor(llcrnrlon/dl), dl*ceil(urcrnrlon/dl), dl)
    parallels = arange(dl*floor(llcrnrlat/dl), dl*ceil(urcrnrlat/dl), dl)
    logger.debug("Generating map object")
    # Create the map object:
    try:
        m = Basemap(projection='cyl',
                resolution=res,
                llcrnrlon=llcrnrlon,
                urcrnrlon=urcrnrlon,
                llcrnrlat=llcrnrlat,
                urcrnrlat=urcrnrlat)
    except IOError:
        flLogFatalError(traceback.format_exc().splitlines())

    # Plot the field - by default, we specify the range to extend both
    # above and below the max & min values specified.
    if fill:
        if maskocean:
            try:
                from mpl_toolkits.basemap import maskoceans
            except ImportError:
                logger.debug("Maskoceans module unavailable, skipping this command")
                pass
            else:
                datam = maskoceans(x,y,data,inlands=False)
                m.contourf(x, y, datam, levels, extend='both', cmap=cmap)
        else:
            m.contourf(xx, yy, data, levels, extend='both', cmap=cmap)
        cb = pyplot.colorbar(shrink=0.5, orientation='horizontal', extend='both',
                             ticks=levels[::2], pad=0.05)
        cb.set_label(clab, fontsize=10)
        if cb.orientation=='horizontal':
            for t in cb.ax.get_xticklabels():
                t.set_fontsize(8)
    else:
        CS = m.contour(xx,yy,data,levels,colors='k',linewidth=0.5)
        pyplot.clabel(CS, CS.levels,inline=True,fontsize=7)

    # Add meridians, parallels, coastlines, etc:
    m.drawcoastlines(linewidth=0.5)
    if maskland:
        m.fillcontinents()
    m.drawparallels(parallels, labels=[1,0,0,0], fontsize=9, linewidth=0.2)
    m.drawmeridians(meridians, labels=[0,0,0,1], fontsize=9, linewidth=0.2)
    pyplot.grid(True)

    if title is not None:
        pyplot.title(title)

    # Save the image:
    if outputFile:
        pyplot.savefig(outputFile)
    else:
        pyplot.show()