예제 #1
0
def plotTidalGauge(outputFilename, saveData=True, **args):
    """
    Plot tidal gauge data
    """

    tidalGaugeId = args['tidalGaugeId']
    tidalGaugeName = args['tidalGaugeName']

    data = TideGauge(tidalGaugeId)

    figure = plt.figure()

    plt.rc('font', size=8)
    plt.title("Monthly sea levels for " + tidalGaugeName)
    plt.ylabel("Sea Level Height (metres)")
    plt.xlabel("Year")

    ax = figure.gca()
    ax.grid(True)

    maxPlot, = ax.plot(data.date, data.maximum, 'r-')
    meanPlot, = ax.plot(data.date, data.mean_, 'k-')
    minPlot, = ax.plot(data.date, data.minimum, 'b-')

    # add legend
    ax.legend([maxPlot, meanPlot, minPlot], ['Max', 'Mean', 'Min'],
              ncol=3,
              loc='lower right')

    plt.axhline(y=0, color='k')
    plt.figtext(0.02, 0.02, getCopyright(), fontsize=6)
    plt.figtext(0.90,
                0.05,
                "0.0 = Tidal Gauge Zero",
                fontsize=8,
                horizontalalignment='right')

    plt.savefig(outputFilename + '.png',
                dpi=150,
                bbox_inches='tight',
                pad_inches=.1)

    plt.close()
    pngcrush(outputFilename + '.png')

    with open(outputFilename + '.csv', 'w') as f:

        writer = csv.writer(f)

        writer.writerow(["# Monthly sea levels for " + tidalGaugeName])
        writer.writerow(["# Data from tidalGaugeId"])
        writer.writerow(
            ["# Sea Level Height (metres) relative to Tidal Gauge Zero"])

        writer.writerow(data.headers[1:])

        for row in data:
            writer.writerow(list(row)[1:])
예제 #2
0
        def _plot_surface_data(lats,
                               lons,
                               data,
                               lat_min,
                               lat_max,
                               lon_min,
                               lon_max,
                               output_filename='noname.png',
                               title='',
                               units='',
                               cm_edge_values=None,
                               cb_tick_fmt="%.0f",
                               cb_labels=None,
                               cb_label_pos=None,
                               colormap_strategy='nonlinear',
                               cmp_name='jet',
                               colors=None,
                               extend='both',
                               fill_color='1.0',
                               plotStyle='contourf',
                               contourLines=True,
                               contourLabels=True,
                               smoothFactor=1,
                               proj=self._DEFAULT_PROJ,
                               product_label_str=None,
                               vlat=None,
                               vlon=None,
                               u=None,
                               v=None,
                               draw_every=1,
                               arrow_scale=10,
                               resolution=None,
                               area=None,
                               boundaryInUse='True',
                               shapefile=None):
            '''
            TODO
            color map needs to be consilidated into one method. The existing discrete_colormap method is
            less flexible. Actually, the overall plot method needs to be more flexible.
            1. Introduce the colormap strategy
                discrete: discrete_cmap
                levels: from_levels_and_colors
            2. Depending on the strategy, various combination of the arguments should be passed in.
                discrete: cmp_name
                          extend
                          cm_edge_values
                levels: color_array
                        extend
                        cm_edge_values
            '''

            if resolution is None and area is not None:
                # try and get a resolution from the area default
                resolution = regions[area][3].get('resolution', None)

            if resolution is None:
                # still no resolution? try and guess
                resolution = guess_resolution(lat_min, lat_max, lon_min,
                                              lon_max)
            m = Basemap(projection=proj,
                        llcrnrlat=lat_min,
                        llcrnrlon=lon_min,
                        urcrnrlat=lat_max,
                        urcrnrlon=lon_max,
                        resolution=resolution)

            #GAS this was removed because different colour ranges makes comparison difficult
            #if cm_edge_values is None:
            #    cm_edge_values = get_tick_values(data.min(), data.max(), 10)[0]
            n_colours = cm_edge_values.size - 1
            if colormap_strategy == 'discrete':
                d_cmap = discrete_cmap(cmp_name, n_colours, extend=extend)
                norm = None
            elif colormap_strategy == 'levels':
                d_cmap, norm = from_levels_and_colors(cm_edge_values,
                                                      np.array(colors) / 255.0,
                                                      None,
                                                      extend=extend)
            elif colormap_strategy == 'nonlinear':
                d_cmap, norm = from_levels_and_colors(cm_edge_values,
                                                      None,
                                                      cmp_name,
                                                      extend=extend)

            #GAS Smoothing section based on smoothFactor
            if smoothFactor > 1:
                #if smoothFactor > 1 and (lat_extent>20 or lon_extent>20):
                size = int(smoothFactor)
                x, y = np.mgrid[-size:size + 1, -size:size + 1]
                g = np.exp(-(x**2 / float(size) + y**2 / float(size)))
                g = g / g.sum()
                #data=np.ma.masked_less(data,-998)a
                data[data < -9.9] = 0
                data[data > 1000] = 5
                data = convolve2d(data, g, mode='same', boundary='symm')
                #a=ma.masked_less(data,-998)
            #np.savetxt('/data/comp/raster/filename.txt',data,delimiter=",")a

            # Plot data
            x, y = None, None
            if plotStyle == 'contourf':
                x, y = m(*np.meshgrid(lons, lats))
                img = plt.contourf(x,
                                   y,
                                   data,
                                   levels=cm_edge_values,
                                   norm=norm,
                                   shading='flat',
                                   cmap=d_cmap,
                                   extend=extend)
            elif plotStyle == 'pcolormesh':
                # Convert centre lat/lons to corner values required for
                # pcolormesh
                lons2 = get_grid_edges(lons)
                lats2 = get_grid_edges(lats)
                x2, y2 = m(*np.meshgrid(lons2, lats2))
                img = m.pcolormesh(x2,
                                   y2,
                                   data,
                                   shading='flat',
                                   cmap=d_cmap,
                                   norm=norm)
            # Draw contours
            if contourLines:
                if x is None:
                    x, y = m(*np.meshgrid(lons, lats))
                #GAS negative contour not to be dashed
                plt.rcParams['contour.negative_linestyle'] = 'solid'
                cnt = plt.contour(x,
                                  y,
                                  data,
                                  levels=cm_edge_values,
                                  norm=norm,
                                  colors='k',
                                  linewidths=0.4,
                                  hold='on')
                if contourLabels:
                    plt.clabel(cnt, inline=True, fmt=cb_tick_fmt, fontsize=8)

            img.set_clim(cm_edge_values.min(), cm_edge_values.max())
            # Plot vector data if provided

            if (u is not None) and (v is not None) and \
               (vlat is not None) and (vlon is not None):
                # Draw vectors
                if draw_every is not None:
                    draw_vector_plot(m,
                                     vlon,
                                     vlat,
                                     u,
                                     v,
                                     draw_every=draw_every,
                                     arrow_scale=arrow_scale)

            if shapefile is not None:
                if os.path.exists(shapefile + ".shp"):
                    front_info = m.readshapefile(shapefile, 'front')
                marker = '_'
                if hasattr(m, 'front'):
                    for front in m.front:
                        m.plot(front[0] if front[0] > 0 else front[0] + 360,
                               front[1],
                               color='k',
                               marker=marker,
                               markersize=1,
                               markeredgewidth=1)

            # Draw land, coastlines, parallels, meridians and add title
            m.drawmapboundary(linewidth=1.0, fill_color=fill_color)
            m.drawcoastlines(linewidth=0.5, color='#505050', zorder=8)
            #            m.fillcontinents(color='#F1EBB7', zorder=7)
            m.fillcontinents(color='0.58', zorder=7)

            parallels, p_dec_places = get_tick_values(lat_min, lat_max)
            meridians, m_dec_places = get_tick_values(lon_min, lon_max)
            m.drawparallels(parallels,
                            labels=[True, False, False, False],
                            fmt='%.' + str(p_dec_places) + 'f',
                            fontsize=6,
                            dashes=[3, 3],
                            color='gray')
            m.drawmeridians(meridians,
                            labels=[False, False, False, True],
                            fmt='%.' + str(m_dec_places) + 'f',
                            fontsize=6,
                            dashes=[3, 3],
                            color='gray')
            plt.title(title, fontsize=9)

            # Draw colorbar
            ax = plt.gca()
            divider = make_axes_locatable(ax)
            cax = divider.append_axes("right", size=0.2, pad=0.3)
            if cb_label_pos is None:
                tick_pos = cm_edge_values
            else:
                tick_pos = cb_label_pos

            if boundaryInUse == 'True':
                cb = plt.colorbar(img,
                                  cax=cax,
                                  spacing='uniform',
                                  drawedges='False',
                                  norm=norm,
                                  orientation='vertical',
                                  extend=extend,
                                  ticks=tick_pos)
                #boundaries=cm_edge_values)
            else:
                cb = plt.colorbar(img,
                                  cax=cax,
                                  spacing='uniform',
                                  drawedges='False',
                                  orientation='vertical',
                                  extend=extend,
                                  ticks=tick_pos)

            if cb_labels is None:
                cb.set_ticklabels([cb_tick_fmt % k for k in cm_edge_values])
            else:
                cb.set_ticklabels(cb_labels)
            for tick in cb.ax.get_yticklabels():
                tick.set_fontsize(7)
            cb.set_label(units, fontsize=8)

            # Patch for graphics bug that affects label positions for
            # long/narrow plots
            lat_extent = np.float(lat_max) - np.float(lat_min)
            lon_extent = np.float(lon_max) - np.float(lon_min)
            aspect_ratio = abs(lon_extent / lat_extent)
            if aspect_ratio > 1.7:
                copyright_label_yadj = -0.25
            else:
                copyright_label_yadj = -0.15
            if aspect_ratio < 0.7:
                copyright_label_xadj = -0.2
                product_label_xadj = 1.4
            else:
                copyright_label_xadj = -0.1
                product_label_xadj = 1.04

            # Draw copyright and product labels
            box = TextArea(getCopyright(),
                           textprops=dict(color='k', fontsize=6))
            copyrightBox = AnchoredOffsetbox(
                loc=3,
                child=box,
                borderpad=0.1,
                bbox_to_anchor=(copyright_label_xadj, copyright_label_yadj),
                frameon=False,
                bbox_transform=ax.transAxes)
            ax.add_artist(copyrightBox)

            if product_label_str is not None:
                box = TextArea(product_label_str,
                               textprops=dict(color='k', fontsize=6))
                copyrightBox = AnchoredOffsetbox(
                    loc=4,
                    child=box,
                    borderpad=0.1,
                    bbox_to_anchor=(product_label_xadj, copyright_label_yadj),
                    frameon=False,
                    bbox_transform=ax.transAxes)
                ax.add_artist(copyrightBox)

            # Save figure
            plt.savefig(output_filename,
                        dpi=150,
                        bbox_inches='tight',
                        pad_inches=0.6)
            plt.close()

            pngcrush(output_filename)
예제 #3
0
        def _plot_surface_data(lats,
                               lons,
                               data,
                               lat_min,
                               lat_max,
                               lon_min,
                               lon_max,
                               output_filename='noname.png',
                               title='',
                               units='',
                               cm_edge_values=None,
                               cb_tick_fmt="%.0f",
                               cb_labels=None,
                               cb_label_pos=None,
                               colormap_strategy='nonlinear',
                               cmp_name='jet',
                               colors=None,
                               extend='both',
                               fill_color='1.0',
                               plotStyle='contourf',
                               contourLines=True,
                               contourLabels=True,
                               smoothFactor=1,
                               proj=self._DEFAULT_PROJ,
                               product_label_str=None,
                               vlat=None,
                               vlon=None,
                               u=None,
                               v=None,
                               draw_every=1,
                               arrow_scale=10,
                               vector=False,
                               overlay_grid=None,
                               resolution=None,
                               area=None,
                               boundaryInUse='True'):

            cmArray = customColorMap[cmp_name]

            if colormap_strategy == 'discrete':
                n_colours = cm_edge_values.size - 1
                d_cmap = discrete_cmap(cmp_name, n_colours, extend)
                norm = None
            elif colormap_strategy == 'levels':
                d_cmap, norm = from_levels_and_colors(
                    customTicks[cm_edge_values],
                    np.array(cmArray) / 255.0)
            elif colormap_strategy == 'nonlinear':
                d_cmap, norm = from_levels_and_colors(
                    customTicks[cm_edge_values], None, cmp_name, extend)

            cm_edge_values = np.array(customTicks[cm_edge_values])

            m = Basemap(projection=proj,
                        llcrnrlat=lat_min,
                        llcrnrlon=lon_min,
                        urcrnrlat=lat_max,
                        urcrnrlon=lon_max,
                        resolution='i')

            # Plot data
            x2, y2 = m(*np.meshgrid(lons, lats))

            img = m.contourf(x2,
                             y2,
                             data,
                             levels=cm_edge_values,
                             cmap=d_cmap,
                             norm=norm,
                             antialiased=True,
                             zorder=0)
            img.set_clim(cm_edge_values.min(), cm_edge_values.max())

            #plot contouring labels
            if contourLabels:
                labels = plt.clabel(img,
                                    cm_edge_values[::4],
                                    inline=True,
                                    fmt='%.0f',
                                    colors='k',
                                    fontsize=5,
                                    zorder=2)
                bbox_props = dict(boxstyle="round", fc="w", ec="w", alpha=0.9)
                for text in labels:
                    text.set_linespacing(1)
                    text.set_bbox(bbox_props)

            #plot vectors
            if vector:
                every = 10
                lons = lons[::every]
                lats = lats[::every]
                x2, y2 = m(*np.meshgrid(lons, lats))
                if overlay_grid is not None:
                    radians_array = np.radians(overlay_grid)
                    radians_array = np.pi + radians_array
                    radians_array = radians_array[::every, ::every]
                m.quiver(x2,
                         y2,
                         np.sin(radians_array),
                         np.cos(radians_array),
                         scale=60,
                         zorder=3)

            # Draw land, coastlines, parallels, meridians and add title
            m.drawmapboundary(linewidth=1.0, fill_color=fill_color)
            m.drawcoastlines(linewidth=0.5, color='#505050', zorder=8)
            m.fillcontinents(color='0.58', zorder=7)

            parallels, p_dec_places = get_tick_values(lat_min, lat_max)
            meridians, m_dec_places = get_tick_values(lon_min, lon_max)
            m.drawparallels(parallels,
                            labels=[True, False, False, False],
                            fmt='%.' + str(p_dec_places) + 'f',
                            fontsize=6,
                            dashes=[3, 3],
                            color='gray')
            m.drawmeridians(meridians,
                            labels=[False, False, False, True],
                            fmt='%.' + str(m_dec_places) + 'f',
                            fontsize=6,
                            dashes=[3, 3],
                            color='gray')

            plt.title(title, fontsize=9)

            # Draw colorbar
            ax = plt.gca()
            divider = make_axes_locatable(ax)
            cax = divider.append_axes("right", size=0.2, pad=0.3)
            if cb_label_pos is None:
                tick_pos = cm_edge_values
            else:
                tick_pos = cb_label_pos

            if boundaryInUse == 'True':
                cb = plt.colorbar(
                    img,
                    cax=cax,
                    #                             spacing='proportional',
                    spacing='uniform',
                    drawedges='False',
                    orientation='vertical',
                    extend=extend,
                    ticks=tick_pos,
                    boundaries=cm_edge_values)
            else:
                cb = plt.colorbar(img,
                                  cax=cax,
                                  spacing='uniform',
                                  drawedges='False',
                                  orientation='vertical',
                                  extend=extend,
                                  ticks=tick_pos)

            if cb_labels is None:
                cb.set_ticklabels([cb_tick_fmt % k for k in cm_edge_values])
            else:
                cb.set_ticklabels(cb_labels)

            for tick in cb.ax.get_yticklabels():
                tick.set_fontsize(7)

            cb.set_label(units, fontsize=8)

            # Patch for graphics bug that affects label positions for
            # long/narrow plots
            lat_extent = np.float(lat_max) - np.float(lat_min)
            lon_extent = np.float(lon_max) - np.float(lon_min)
            aspect_ratio = abs(lon_extent / lat_extent)

            if aspect_ratio > 1.7:
                copyright_label_yadj = -0.25
            else:
                copyright_label_yadj = -0.10
            if aspect_ratio < 0.7:
                copyright_label_xadj = -0.2
                product_label_xadj = 1.4
            else:
                copyright_label_xadj = -0.1
                product_label_xadj = 1.04

            # Draw copyright and product labels
            box = TextArea(getCopyright(),
                           textprops=dict(color='k', fontsize=6))
            copyrightBox = AnchoredOffsetbox(
                loc=3,
                child=box,
                borderpad=0.1,
                bbox_to_anchor=(copyright_label_xadj, copyright_label_yadj),
                frameon=False,
                bbox_transform=ax.transAxes)
            ax.add_artist(copyrightBox)

            if product_label_str is not None:
                box = TextArea(product_label_str,
                               textprops=dict(color='k', fontsize=6))
                copyrightBox = AnchoredOffsetbox(
                    loc=4,
                    child=box,
                    borderpad=0.1,
                    bbox_to_anchor=(product_label_xadj, copyright_label_yadj),
                    frameon=False,
                    bbox_transform=ax.transAxes)
                ax.add_artist(copyrightBox)

            # Save figure
            plt.savefig(output_filename,
                        dpi=150,
                        bbox_inches='tight',
                        pad_inches=0.6)

            plt.close()

            pngcrush(output_filename)
예제 #4
0
def plotTimeseries(outputFilename, saveData=True, **args):
    """
    Plot altimetry/reconstruction timeseries 
    """
    tidalGaugeName = args["tidalGaugeName"]
    variable = args['variable']
    lat = args['lat']
    lon = args['lon']

    titlePrefix = {
        'alt': "Altimetry",
        'rec': "Reconstruction",
    }[variable]

    grid = SeaLevelSeries(variable, latrange=(lat, lat), lonrange=(lon, lon))

    if saveData:
        with open(outputFilename + ".txt", 'w') as file:
            writer = csv.writer(file, delimiter='\t')
            writer.writerow(
                ('# Sea Level %s for %s' % (titlePrefix, tidalGaugeName), ))
            if variable == 'alt':
                writer.writerow([
                    '# Corrections: IB not removed; seasonal not removed; global trend not removed; GIA removed'
                ])
                writer.writerow(['# Data from CSIRO CMAR'])
            else:
                writer.writerow([
                    '# Corrections: IB removed; seasonal cycle removed; global trend not removed; GIA removed'
                ])
                writer.writerow(
                    ['# Data from CSIRO CMAR, Church and White 2009'])
            writer.writerow(['Date (YYYY-MM)', '%s (mm)' % titlePrefix])

            for date, height in zip(grid.time, grid.data):
                writer.writerow([date.strftime('%Y-%m'), height])

    figure = plt.figure()
    plt.rc('font', size=8)
    plt.title("Sea Level %s for %s" % (titlePrefix, tidalGaugeName))
    plt.ylabel('Sea-Surface Height (mm)')
    plt.xlabel('Year')
    ax = figure.gca()
    ax.grid(True)
    ax.set_ylim(-350, 350)
    ax.plot(grid.time, grid.data, 'b-')
    plt.axhline(y=0, color='k')

    if variable == 'alt':
        PRODUCT_NAME = """
        Data from CSIRO CMAR
        IB not removed; seasonal not removed; global trend not removed; GIA removed
        """
    else:
        PRODUCT_NAME = """
        Data from CSIRO CMAR
        IB removed; seasonal cycle removed; global trend not removed; GIA removed
        """

    plt.figtext(0.02, 0.02, getCopyright(), fontsize=6)
    plt.figtext(0.90,
                0.02,
                PRODUCT_NAME.strip(),
                fontsize=6,
                horizontalalignment='right')

    plt.savefig(outputFilename + ".png",
                dpi=150,
                bbox_inches='tight',
                pad_inches=.1)

    plt.close()
    pngcrush(outputFilename + ".png")

    return 0
예제 #5
0
def plot_BRAN_depth_slice(depths, lats, lons, zonal_data, meridional_data, lats_all, lons_all, data_sf,
                          lat_cnt, lon_cnt, output_filename='noname.png', title='', units='m/s',
                          cb_ticks=None, cb_tick_fmt="%.0f", cmp_name='jet', proj='cyl',
                          product_label_str=None):
    fg = plt.figure()
    gs = mpl.gridspec.GridSpec(2,5)
    ax1=plt.subplot(gs[1,0:2])
        
    n_colours = cb_ticks.size - 1
    d_cmap = discrete_cmap(cmp_name, n_colours)
    
    # Draw contour
    x, y = np.meshgrid(lons, depths)
    ctr = plt.contour(x, y, zonal_data, levels=cb_ticks, colors='k', linewidths=0.4)
    plt.clabel(ctr, inline=True, fmt=cb_tick_fmt, fontsize=8)

    lons2 = get_grid_edges(lons)
    depths2 = get_grid_edges(depths)
        
    # Plot data
    x2, y2 = np.meshgrid(lons2, depths2)
    img = plt.pcolormesh(x2, y2, zonal_data, shading='flat', cmap=d_cmap, vmin=cb_ticks.min(), vmax=cb_ticks.max())
    
    ax = plt.gca()
    ax.set_ylim(0,300)
    ax.set_ylim(ax.get_ylim()[::-1]) 
    ax.xaxis.set_major_formatter(mpl.ticker.FuncFormatter(format_longitude))
    ax.tick_params(axis='x', labelsize=8)
    ax.tick_params(axis='y', labelsize=9)

    plt.ylabel("Depth (m)", fontsize=10)
    plt.xlabel("Longitude", fontsize=10)
    plt.subplots_adjust(right=1.0)
    ax2=plt.subplot(gs[1,2:4])

    # Draw contour
    x, y = np.meshgrid(lats, depths)
    ctr = plt.contour(x, y, meridional_data, levels=cb_ticks, colors='k', linewidths=0.4)
    plt.clabel(ctr, inline=True, fmt=cb_tick_fmt, fontsize=8)

    lats2 = get_grid_edges(lats)
    depths2 = get_grid_edges(depths)
        
    # Plot data
    x2, y2 = np.meshgrid(lats2, depths2)
    img = plt.pcolormesh(x2, y2, meridional_data, shading='flat', cmap=d_cmap, vmin=cb_ticks.min(), vmax=cb_ticks.max())
        
    ax = plt.gca()
    ax.set_ylim(0,300)
    ax.set_ylim(ax.get_ylim()[::-1])
    ax.set_yticklabels([''])
    ax.xaxis.set_major_formatter(mpl.ticker.FuncFormatter(format_latitude))
    ax.tick_params(axis='x', labelsize=8)
    
    plt.xlabel("Latitude", fontsize=10)
    
    ax3=plt.subplot(gs[0,:-1])

    lat_min = lats[0] - 5.0
    lat_max = lats[-1] + 5.0
    lon_min = lons[0] - 20.0
    lon_max = lons[-1] + 20.0
    
    m = Basemap(projection=proj, llcrnrlat=lat_min, llcrnrlon=lon_min, \
                urcrnrlat=lat_max, urcrnrlon=lon_max, resolution='h')
    m.drawcoastlines(linewidth=0.5, zorder=8, color='#505050')
    m.fillcontinents(color='#F1EBB7', zorder=7)
    plt.hold(True)
    plt.plot([lon_cnt,lon_cnt],[lats[0],lats[-1]], color='k', linestyle='--', linewidth=2, zorder=8)
    plt.plot([lons[0],lons[-1]],[lat_cnt,lat_cnt], color='k', linestyle='--', linewidth=2, zorder=8)
    
    # Convert centre lat/lons to corner values required for pcolormesh
    lons2 = get_grid_edges(lons_all)
    lats2 = get_grid_edges(lats_all)
        
    # Plot data
    x2, y2 = m(*np.meshgrid(lons2, lats2))
    img = m.pcolormesh(x2, y2, data_sf, shading='flat', cmap=d_cmap, vmin=cb_ticks.min(), vmax=cb_ticks.max())
    plt.title(title, fontsize=12)
    
    parallels, p_dec_places = get_tick_values(lat_min, lat_max, 3)
    meridians, m_dec_places = get_tick_values(lon_min, lon_max)
    m.drawparallels(parallels, labels=[True, False, False, False], fmt='%.' + str(p_dec_places) + 'f', 
                    fontsize=8, dashes=[3, 3], color='gray')
    m.drawmeridians(meridians, labels=[False, False, False, True], fmt='%.' + str(m_dec_places) + 'f',
                    fontsize=8, dashes=[3, 3], color='gray')
    cbaxes = fg.add_axes([0.85, 0.15, 0.03, 0.7]) # setup colorbar axes.
    
    cb = fg.colorbar(img, spacing='proportional', drawedges='True', cax=cbaxes,orientation='vertical',
                     extend='both', ticks=cb_ticks)
    cb.set_ticklabels([cb_tick_fmt % k for k in cb_ticks])
    cb.set_label(units)
    
    box = TextArea(getCopyright(), textprops=dict(color='k', fontsize=6))
    copyrightBox = AnchoredOffsetbox(loc=3, child=box, bbox_to_anchor=(-1.3, -0.45), frameon=False, bbox_transform=ax.transAxes)
    ax.add_artist(copyrightBox)

    box = TextArea(product_label_str, textprops=dict(color='k', fontsize=8))
    copyrightBox = AnchoredOffsetbox(loc=4, child=box, bbox_to_anchor=(1.4, -0.45), frameon=False, bbox_transform=ax.transAxes)
    ax.add_artist(copyrightBox)
    
    plt.savefig(output_filename, dpi=150, bbox_inches='tight', pad_inches=1.)
    plt.close()
    
    pngcrush(output_filename)

    return
예제 #6
0
        def _plot_surface_data(lats,
                               lons,
                               data,
                               time,
                               lat_min,
                               lat_max,
                               lon_min,
                               lon_max,
                               output_filename='noname.png',
                               title='',
                               units='',
                               cm_edge_values=None,
                               cb_tick_fmt="%.0f",
                               cb_labels=None,
                               cb_label_pos=None,
                               colormap_strategy='nonlinear',
                               cmp_name='jet',
                               colors=None,
                               extend='both',
                               fill_color='1.0',
                               plotStyle='contourf',
                               contourLines=True,
                               contourLabels=True,
                               smoothFactor=1,
                               proj=self._DEFAULT_PROJ,
                               product_label_str=None,
                               vlat=None,
                               vlon=None,
                               u=None,
                               v=None,
                               draw_every=1,
                               arrow_scale=10,
                               resolution=None,
                               area=None,
                               boundaryInUse='True'):

            d_cmap, norm = from_levels_and_colors(cm_edge_values,
                                                  None,
                                                  cmp_name,
                                                  extend=extend)

            m = Basemap(projection=proj,
                        llcrnrlat=lat_min,
                        llcrnrlon=lon_min,
                        urcrnrlat=lat_max,
                        urcrnrlon=lon_max,
                        resolution='i')

            # Plot data
            x2, y2 = m(*np.meshgrid(lons, lats))

            u = data[0][time]
            v = data[1][time]
            mag = np.sqrt(u**2 + v**2)

            img = m.pcolormesh(x2,
                               y2,
                               mag,
                               shading='flat',
                               cmap=d_cmap,
                               norm=norm)
            img.set_clim(cm_edge_values.min(), cm_edge_values.max())

            #plot vectors
            if area == 'pac':
                every = 50
                scale = 10
            else:
                every = 10
                scale = 10

            lons = lons[::every]
            lats = lats[::every]
            x2, y2 = m(*np.meshgrid(lons, lats))
            u2 = u[::every, ::every]
            v2 = v[::every, ::every]
            rad = np.arctan2(v2, u2)
            m.quiver(x2,
                     y2,
                     np.cos(rad),
                     np.sin(rad),
                     scale=scale,
                     zorder=3,
                     scale_units='inches',
                     pivot='middle')

            # Draw land, coastlines, parallels, meridians and add title
            m.drawmapboundary(linewidth=1.0, fill_color=fill_color)
            m.drawcoastlines(linewidth=0.5, color='#505050', zorder=8)
            m.fillcontinents(color='0.58', zorder=7)

            parallels, p_dec_places = get_tick_values(lat_min, lat_max)
            meridians, m_dec_places = get_tick_values(lon_min, lon_max)
            m.drawparallels(parallels,
                            labels=[True, False, False, False],
                            fmt='%.' + str(p_dec_places) + 'f',
                            fontsize=6,
                            dashes=[3, 3],
                            color='gray')
            m.drawmeridians(meridians,
                            labels=[False, False, False, True],
                            fmt='%.' + str(m_dec_places) + 'f',
                            fontsize=6,
                            dashes=[3, 3],
                            color='gray')

            plt.title(title, fontsize=9)

            # Draw colorbar
            ax = plt.gca()
            divider = make_axes_locatable(ax)
            cax = divider.append_axes("right", size=0.2, pad=0.3)
            if cb_label_pos is None:
                tick_pos = cm_edge_values
            else:
                tick_pos = cb_label_pos

            if boundaryInUse == 'True':
                cb = plt.colorbar(
                    img,
                    cax=cax,
                    #                             spacing='proportional',
                    spacing='uniform',
                    drawedges='False',
                    orientation='vertical',
                    extend=extend,
                    ticks=tick_pos,
                    boundaries=cm_edge_values)
            else:
                cb = plt.colorbar(img,
                                  cax=cax,
                                  spacing='uniform',
                                  drawedges='False',
                                  orientation='vertical',
                                  extend=extend,
                                  ticks=tick_pos)

            if cb_labels is None:
                cb.set_ticklabels([cb_tick_fmt % k for k in cm_edge_values])
            else:
                cb.set_ticklabels(cb_labels)
            for tick in cb.ax.get_yticklabels():
                tick.set_fontsize(7)
            cb.set_label(units, fontsize=8)

            # Patch for graphics bug that affects label positions for
            # long/narrow plots
            lat_extent = np.float(lat_max) - np.float(lat_min)
            lon_extent = np.float(lon_max) - np.float(lon_min)
            aspect_ratio = abs(lon_extent / lat_extent)

            if aspect_ratio > 1.7:
                copyright_label_yadj = -0.25
            else:
                copyright_label_yadj = -0.10
            if aspect_ratio < 0.7:
                copyright_label_xadj = -0.2
                product_label_xadj = 1.4
            else:
                copyright_label_xadj = -0.1
                product_label_xadj = 1.04

            # Draw copyright and product labels
            box = TextArea(getCopyright(),
                           textprops=dict(color='k', fontsize=6))
            copyrightBox = AnchoredOffsetbox(
                loc=3,
                child=box,
                borderpad=0.1,
                bbox_to_anchor=(copyright_label_xadj, copyright_label_yadj),
                frameon=False,
                bbox_transform=ax.transAxes)
            ax.add_artist(copyrightBox)

            if product_label_str is not None:
                box = TextArea(product_label_str,
                               textprops=dict(color='k', fontsize=6))
                copyrightBox = AnchoredOffsetbox(
                    loc=4,
                    child=box,
                    borderpad=0.1,
                    bbox_to_anchor=(product_label_xadj, copyright_label_yadj),
                    frameon=False,
                    bbox_transform=ax.transAxes)
                ax.add_artist(copyrightBox)

            # Save figure
            plt.savefig(output_filename,
                        dpi=150,
                        bbox_inches='tight',
                        pad_inches=0.6)

            plt.close()

            pngcrush(output_filename)
예제 #7
0
def RosePlot(opath, wdir, units, lat, lon, ilat, ilon, xstr, title, var, binwd):

    '''Plots a windrose of angular values in wdir, with annotations

       Arguments:
       wdir -- an array of angles in the range (0,360). (NumPy Array)
       units -- measurements units for wdir variable. (float)
       lat -- latitude of point. (float)
       lon -- longitude of point. (float)
       xstr -- x-axis label. (string)
       title -- plot title. (string)
       var -- the variable that is measured by wdir. (string)
       binwd -- width of histogram bins. (float)

       Returns:
       rosefig -- An annotated windrose of wdir values.
       '''

    #set number of bins and max bin value.
    N,wdnbin,wdmax = 8,8,2*np.pi
    degree = ur'\u00b0'
    if wdir[0] == -999.0:
       raise LandError()
    #flip directions as WWIII direction are given in meteorological convention.
    if var == "Dm":
        wdir = conv.dirflip(wdir)
        wdir = conv.dirshift(wdir)
    else:
        wdir = conv.dirshift(wdir)
    #make a basic histogram of wave directions over the range (-22.5,337.5).
    try:
        whist = np.histogram(wdir, wdnbin, (-22.5,337.5), density=False)
    except TypeError:
        # support older numpy
        whist = np.histogram(wdir, wdnbin, (-22.5,337.5), normed=False)

    #calculate mean bearing
    meanb = meanbearing(wdir)
    #force square figure and square axes looks better for polar
    rosefig = figure(figsize=(10,7.5))
    ax = rosefig.add_axes([0.075, 0.1, 0.6, 0.725], polar=True)
    #plot radial gridlines at seperations of 45 degrees
    plt.thetagrids((0,45,90,135,180,225,270,315),('N', '45' + degree, 'E', '135' + degree, 'S', '215' + degree, 'W', '315' + degree),frac = 1.1)
    #rotate axis to zero at North and set direction of increasing angle clockwise.
    plt.rgrids((0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9), angle=0)
    plt.ylim(0.0,1.0)
    try:
        ax.set_theta_direction(-1)
        ax.set_theta_zero_location('N')
    except:
        # hack around older matplotlib
        pass

    #lines, labels = plt.rgrids()
    #set Nmax
    Nmax = 1
    #initialize prob and perc arrays
    prob = [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]
    perc = [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]
    #variable to normalize bins so that they sum to 1 (probability)
    normalizer = 1/float(len(wdir))
    #normalize histogram to calculate probabilities and percentages
    for i in range(0,N):
        prob[i] = float(whist[0][i])*normalizer
        perc[i] = round(100*prob[i],2)

    units = '%'
    theta = np.linspace(-2*np.pi/(2*N), 2*np.pi-2*np.pi/(2*N), N, endpoint=False)
    #some basic settings for graphics
    width = 2*np.pi/N
    length = np.size(wdir)
    #rounded mean bearing for labelling purposes
    meanbr = round((meanb*180)/(np.pi),1)
    #set up bar chart properties
    bars = ax.bar(theta, prob, width, bottom=0.0)
    my_cmap = hc.decile_rose()
    #plot a line to indicate mean bearing
    ax.arrow(0, 0, meanb, 1.0, edgecolor = 'r', facecolor = 'r', lw='3')
    #plot bar chart of histogram in theta space
    for r,bar in zip(prob, bars):
        bar.set_facecolor(my_cmap(r))
        bar.set_alpha(0.5)
    lpack.rosepack(title)
    formilat,formilon =  NESWformat(ilat,ilon)
    formlat,formlon = NESWformat(lat,lon)
    #various annotations to plot
    plt.figtext(0.76, 0.82,'Probabilities:', fontsize = 10, weight = 550)
    plt.figtext(0.76, 0.575,'Location & Plot Data:', fontsize=10, weight = 550)

    plt.figtext(0.76, 0.51, 'Input Lat/Lon:\n %s %s' % (formilat,formilon), fontsize=10)
    plt.figtext(0.76, 0.46, 'Grid Lat/Lon:\n %s %s' % (formlat, formlon), fontsize=10)

    plt.figtext(0.76, 0.425, 'Data points: %s' % length, fontsize=10)
    plt.figtext(0.76, 0.4, 'Bins: %s' % int(N), fontsize=10)
    plt.figtext(0.76, 0.375, 'Bin Width: %s %s' % (binwd, degree), fontsize=10)

    plt.figtext(0.76, 0.275, 'Mean Bearing: %s%s %s' % (meanbr, degree,'T'), color='r', fontsize=10)
    #display wave direction percentages for 8 primary compass points in the windrose
    plt.figtext(0.76, 0.325, 'Directional Statistics:', fontsize=10, weight=550)
    plt.figtext(0.76, 0.25, 'North: %s %s' % (perc[0], units), fontsize=10)
    plt.figtext(0.76, 0.225, 'North East: %s %s' % (perc[1], units), fontsize=10)
    plt.figtext(0.76, 0.2, 'East: %s %s' % (perc[2], units), fontsize=10)
    plt.figtext(0.76, 0.175, 'South East: %s %s' % (perc[3], units), fontsize=10)
    plt.figtext(0.76, 0.15, 'South: %s %s' % (perc[4], units), fontsize=10)
    plt.figtext(0.76, 0.125, 'South West: %s %s' % (perc[5], units), fontsize=10)
    plt.figtext(0.76, 0.1, 'West: %s %s' % (perc[6], units), fontsize=10)
    plt.figtext(0.76, 0.075, 'North West: %s %s' % (perc[7], units), fontsize=10)
    #Bureau of Meteorology Copyright
    plt.figtext(0.58, 0.03, u'WAVEWATCH III$^{\u24C7}$', fontsize=10)
    plt.figtext(0.02, 0.02, getCopyright(), fontsize=8)
    #define image name
    imgname = opath + '.png'
     #write image file
    plt.savefig(imgname)
    plt.close()

    pngcrush(imgname)

    return
예제 #8
0
def HistPlot(opath, wheight, units, lat, lon, ilat, ilon, xstr, title, var, binwd):
    '''Returns a normalized, annotated histogram for values of wheight

       Arguments:
       wheight -- an array of positive doubles. (NumPy Array)
       units -- units associated with wheight. (string)
       lat -- latitude of the point. (float)
       lon -- longitude of the point. (float)
       xstr -- x-axis label. (string)
       title -- title of the plot. (string)
       var -- variable associated with the values in wheight. (string)
       binwd -- width of histogram bins. (float)

       Returns:
       histfig -- An annotated histogram of wheight values.
       '''
    #calculate the values of some pertinent statistical quantities
    wavg = np.average(wheight)
    wavgr = round(wavg,2)
    maxwave = round(np.max(wheight),2)
    minwave = round(np.min(wheight),2)
    imaxwave = int(maxwave)+ 1
    iminwave = int(minwave)- 1
    Nmax = imaxwave
    Nmin = iminwave
    length = np.size(wheight)
    debug = False
    #calculate range of data
    #binthresh = maxwave - minwave
    #Error message if selected coordinates are out of range or on land
    if maxwave == -999.0:
       raise LandError()
    #alter bin width depending on range of data
    #if binthresh < 10:
     #  binwd = 0.1
    #elif binthresh > 10:
      # binwd = 0.2
    #else:
      # print "Error"
    #bins per unit and total number of bins
    binperunit = float(1/binwd)
    binnum = (Nmax - Nmin)*binperunit
    initial = round(0.001,3)
    final = round(Nmax+0.001,3)

    #histogram of selected variable
    try:
        whist,wbins = np.histogram(wheight, Nmax * binperunit,
                                   (initial, final), density=True)
    except TypeError:
        # support older numpy
        whist,wbins = np.histogram(wheight, Nmax * binperunit,
                                   (initial, final), normed=True)

    #print wbins
    #calculate max and mins of histogram
    maxy = np.max(whist*binwd)
    maxx = np.max(wheight)
    minx = np.min(wheight)
    #do some basic statistics on histogram, std. deviation and quartiles.
    q1 = round(sci.stats.scoreatpercentile(wheight,25),1)
    q3 = round(sci.stats.scoreatpercentile(wheight,75),1)
    x=linspace(0,maxx+0.5,Nmax*binperunit)
    #set up figure
    histfig=plt.figure(figsize = (10,7.5))
    histax=histfig.add_axes([0.1,0.1,0.675,0.75])
    plt.xlim(0,maxx+0.5)
    plt.ylim(0,maxy+0.01)
    whistnorm = whist*binwd
    #np.hist(wheight,100,(0,Nmax),color='b',normed=True, histtype = 'stepfille)
    #set up bar chart properties
    bars=plt.bar(wbins[:-1],whistnorm,binwd,0)
    if debug == True:

        FILE = open('/home/jsmith/Test/verification/WW3' + '_' + str(lat) + '_' + str(lon) + '_' + var + '_array.txt', "w")
        FILE.writelines(list( "%s \n" % i for i in whist))
        FILE.close()

    my_cmap = hc.quartile_colors(wheight,wavg,Nmax,binwd)

    #bar chart of histogram
    for r,bar in zip(wbins[:-1], bars):
        bar.set_facecolor(my_cmap(r/Nmax))
        bar.set_alpha(0.5)

    #specify plot properties and plot guassian kde and average line
    plt.xlim(minx-0.5,maxx+0.5)
    plt.ylim(0,maxy+0.01)
    #histax.plot(x, approximate_pdf(x), color = 'black', linewidth=3, alpha=1)
    histax.axvline(wavg, color='r', lw='3')
    #choose which legend to display based on variable
    if var == 'Hs':
        lpack.heightpack(title,wavg)
    #plt.figtext(0.79,0.175, 'Rogue Wave Height: %s %s' % (2*wavgr,units), fontsize = 10, color = 'm')
    elif var == 'Tm':
        lpack.timepack(title)
    formilat, formilon = NESWformat(ilat,ilon)
    formlat, formlon = NESWformat(lat,lon)
    #specify graph grid properties
    xticks = [10,20,30,40,50,60,70,80,90,100]
    plt.xticks = xticks, [('%s' % str(x/10))]
    plt.grid(True)
    #x,y axis labels
    plt.xlabel('%s (%s)' % (xstr,units), fontsize=12)
    plt.ylabel('Frequency', fontsize=12)
    #various annotations for graphics
    plt.figtext(0.79, 0.775, 'Distribution:', fontsize=10, weight=550)
    plt.figtext(0.79, 0.615, 'Location & Plot Data:',fontsize = 10, weight = 550)
    plt.figtext(0.79, 0.55, 'Input Lat/Lon:\n %s %s' % (formilat,formilon), fontsize=10)
    plt.figtext(0.79, 0.50, 'Grid Lat/Lon:\n %s %s' % (formlat,formlon), fontsize=10)

    plt.figtext(0.79, 0.465, 'Data points: %s' % length ,fontsize=10)
    plt.figtext(0.79, 0.44, 'Bins: %s'  % int(binnum), fontsize=10)
    plt.figtext(0.79, 0.415, 'Bin Width: %s %s' % (binwd,units), fontsize=10)
    plt.figtext(0.79, 0.365, 'Statistical Information:', fontsize=10, weight=550)

    plt.figtext(0.79, 0.265, 'Mean: %s %s' % (round(wavgr,1), units), color='r', fontsize=10)
    plt.figtext(0.79, 0.315, 'Max: %s %s' % (round(maxwave,1), units), fontsize=10)
    plt.figtext(0.79, 0.215, 'Min: %s %s' % (round(minwave,1), units), fontsize=10)

    plt.figtext(0.79, 0.24,'25th Percentile: %s %s' % (round(q1,1),units), fontsize=10)
    plt.figtext(0.79, 0.29,'75th Percentile: %s %s' % (round(q3,1),units), fontsize=10)
    #Bureau of Meteorology Copyright
    plt.figtext(0.68, 0.03, u'WAVEWATCH III$^{\u24C7}$', fontsize=10)
    plt.figtext(0.02, 0.02, getCopyright(), fontsize=8)
     #define image name
    imgname = opath + '.png'
     #write image file
    plt.savefig(imgname)
    plt.close()

    pngcrush(imgname)

    return