コード例 #1
0
                                  ["MSLP (hPa)", "Max 10m Wind (kts)"]):
            # NOTE : Passing x_label messes up x axis formatting
            plt.figure(metric)
            tcv_plot_helper.plot_tcv_metric_vs_fhr(
                forecast_track,
                metric,
                y_label=yLabel,
                time_axis_parameter="epoch_zeta",
                label=label,
                color=colors[i])
        #plt.legend()
        #plt.figure("maxwind_value")
        #plt.plot(maxwinds, color=colors[i], lw=line_width, label=label)
        #plt.legend()
    plt.figure("tracks")
    bling.decorate_map(m)
    # options worth testing: water_color='#99ffff',lake_color='#99ffff',
    #continent_color='#cc9966'

    # plot legend. The skip_duplicates arg must be True since the plot_track()
    # function actually plots multiple lines for each track in order to vary
    # the intensity of the color based on the max wind value
    bling.create_simple_legend(skip_duplicates=True, position='upper right')

    plt.savefig('tracks.png')

    plt.figure("mslp_value")
    plt.legend(loc='best')
    plt.savefig("mslp.png")

    plt.figure("maxwind_value")
コード例 #2
0
def plot_track(lats, lons, **kwargs):
    '''
    Given a list of latitudes and a list of longitudes corresponding to
    a hurricane track, plot the track using Basemap.
    
    RETURNS
     - the basemap object plotted on
    REQUIRED ARGUMENTS
     - lats - List of latitude points
     - lons - List of longitude points

    OPTIONAL KEYWORD ARGUMENTS (and their corresponding functionality):
     - line_color - Color to use for the line. Default: black
     - line_width - Width to use for the line: Default: 1.5
     - line_style  - Line style. Default: solid
     - zorder - Z order of the line. Default: 0
     - label - label to apply to the data being passed
     - windspeeds - List of windspeeds corresponding to the lat/lon pairs.
                    If set, the shade of the track lines will be adjusted
                    according to the strength of the storm.
                    *NOTE: The scheme for this is currently very crude.
                           If you do not want this feature, do not 
                           pass in the windspeeds*
     - flagged_idc - List of indices in the lat/lon that are marked as
                     flagged. If passed in, these points will be indicated
                     using circular markers
     - ax - Axes object to plot on. This overrides the 'basemap' argument.
     - basemap - If set, use it as the Basemap object for plotting.
                 Otherwise instantiate a new one.
     - extents - If set and basemap is not passed in, use these extents
                 for the map. Should be a 4-element list as follows:
                     [lowerLat, westLon, upperLat, eastLon]
                 If not passed in, use the global variables defined in
                 this module
     - indicator_freq - If set, mark points corresponding to this
                        interval.
                        e.g. if passing in values ever 6 hours and you want
                        to indicate every 24 hours, set this to 4
     - alpha - Alpha level to use for the line
     
      - Plus, whatever arguments are accepted by the decorate_map function, 
        since it will be called afterwards

    '''
    # Set basic optional values

    # array of arguments to pass to the plot() call
    mpl_plot_kwargs = {}

    if kwargs.has_key('line_color'):
        line_color = kwargs['line_color']
    else:
        line_color = TRACK_LINE_COLOR
    if kwargs.has_key('line_style'):
        line_style = kwargs['line_style']
    else:
        line_style = '-'
    if kwargs.has_key('zorder'):
        zorder = kwargs['zorder']
    else:
        zorder = 10  # keep lines in front of land
    if kwargs.has_key('alpha'):
        line_alpha = kwargs['alpha']
    else:
        line_alpha = 1.0
    if kwargs.has_key('line_width'):
        line_width = kwargs['line_width']
    else:
        line_width = TRACK_LINE_WIDTH
    if kwargs.has_key('label'):
        mpl_plot_kwargs['label'] = kwargs['label']
    # sanity checks
    assert len(lats) == len(lons)
    #import pdb ;pdb.set_trace()
    # Set the axes
    if kwargs.has_key('ax'):
        ax = kwargs['ax']
    else:
        ax = gca()

    # Instantiate/set the Basemap object
    if kwargs.has_key('basemap'):
        m = kwargs['basemap']
    else:
        if kwargs.has_key('extents'):
            lowerLat = kwargs['extents'][0]
            westLon = kwargs['extents'][1]
            upperLat = kwargs['extents'][2]
            eastLon = kwargs['extents'][3]
        else:
            lowerLat = SOUTH_CORNER
            westLon = WEST_CORNER
            upperLat = NORTH_CORNER
            eastLon = EAST_CORNER
        m = Basemap(llcrnrlon=westLon,
                    llcrnrlat=lowerLat,
                    urcrnrlon=eastLon,
                    urcrnrlat=upperLat,
                    projection=MAP_PROJECTION,
                    resolution=MAP_RESOLUTION,
                    area_thresh=AREA_THRESHOLD,
                    ax=ax)

    #
    # plot track
    #
    maxwinds = []
    if kwargs.has_key('windspeeds') and kwargs['windspeeds'] is not None:
        maxwinds = kwargs['windspeeds']
        assert len(maxwinds) == len(lats)
        rgb = ColorConverter().to_rgb(line_color)
        max_maxwind = max(maxwinds)
    # To support different colors depending on speed, we need to plot multiple
    # small lines. Due to a technicality in Basemap::plot, we need to
    # plot in increments of at least 3 points. As a result, the last two points'
    # wind speed will not affect the line color
    # NOTE That since all points (except the first and last 2) are being plotted
    # multiple times, the effect of different colors/alphas is greatly diminished.
    # a better temporary scheme is to only do each 3-point 'subline' once
    # and use the average of the 3 points' maxwind values to determine the shading.
    #for i in range(len(lats) - 2):
    subline_length = 3
    for i in range(0, len(lats) - 2, subline_length):
        # if next subline would have less than 3 elements, extend the
        # current subline correspondingly
        if (i + subline_length) >= (len(lats) - (subline_length - 1)):
            subline_length = subline_length + (len(lats) % subline_length)
        if len(maxwinds) > 0:
            #scale_factor = maxwinds[i] / max_maxwind
            scale_factor = np.mean(
                maxwinds[i:i + subline_length + 1]) / max_maxwind
            # This scheme only works with black and other mixed colors...
            # not the common red,green,blue, etc.
            #currColor = [ max(0,c - scale_factor) for c in rgb]
            #print currColor
            # for now, just use alpha
            if line_alpha != 1:
                log.debug('Note: currently using a crude scheme for line color'\
                       ' that just modifies the alpha')
            currColor = line_color
            line_alpha = scale_factor
            log.debug(line_alpha)
        else:
            currColor = line_color
        log.info("Commenting out code that sets line alpha according to speed")
        '''
        m.plot(lons[i:i+subline_length+1], lats[i:i+subline_length+1],
               latlon=True, color=currColor,
               linewidth=line_width, alpha=line_alpha, ls=line_style,
               zorder=zorder,
               **mpl_plot_kwargs)
       '''
    # uncomment below to do the color by speed
    m.plot(lons,
           lats,
           latlon=True,
           color=currColor,
           linewidth=line_width,
           alpha=line_alpha,
           ls=line_style,
           zorder=zorder,
           **mpl_plot_kwargs)
    # plot markers on flagged points - this is slow!
    if kwargs.has_key('flagged_idc') and len(kwargs['flagged_idc']) > 0:
        # Due to an issue with Basemap, cannot indicate flagged
        # indices unless there are at least 3 of them, so pad the list
        if len(kwargs['flagged_idc']) < 3:
            flagged_idc = kwargs['flagged_idc']
            flagged_idc.append(flagged_idc[0])
            flagged_idc.append(flagged_idc[0])
        flaggedLats = []
        flaggedLons = []
        for x in kwargs['flagged_idc']:
            flaggedLats.append(lats[x])
            flaggedLons.append(lons[x])
        m.scatter(
            flaggedLons,
            flaggedLats,
            latlon=True,
            s=35.0,
            #facecolors='none', edgecolors=line_color)
            facecolors='red',
            edgecolors=line_color)

    # plot markers every 24 hours
    if kwargs.has_key('indicator_freq'):
        for i in kwargs['indicator_freq']:
            sz = line_width * 8
            m.scatter(lons[i], lats[i], latlon=True, color=line_color, s=sz)

    bling.decorate_map(m, **kwargs)

    return m