コード例 #1
0
def _generate_map_prepare_data(data=None,
                               lat=None,
                               lon=None,
                               projection='cyl',
                               mapbound='all',
                               rlat=None,
                               rlon=None,
                               gridstep=(30, 30),
                               shift=False,
                               map_threshold=None,
                               mask=None,
                               levels=None,
                               cmap=None,
                               smartlevel=None,
                               data_transform=False,
                               gmapkw={},
                               extend='neither',
                               ax=None):
    """
    This function makes the map, and transform data for ready
        use of m.contourf or m.imshow
    """
    if shift == True:
        data, lon = bmp.shiftgrid(180, data, lon, start=False)
    mgmap = gmap(ax,
                 projection,
                 mapbound,
                 lat,
                 lon,
                 gridstep,
                 rlat=rlat,
                 rlon=rlon,
                 **gmapkw)
    m, lonpro, latpro, latind, lonind = (mgmap.m, mgmap.lonpro, mgmap.latpro,
                                         mgmap.latind, mgmap.lonind)

    pdata = data[latind[0]:latind[-1] + 1, lonind[0]:lonind[-1] + 1]
    #mask by map_threshold
    pdata = mathex.ndarray_mask_by_threshold(pdata, map_threshold)
    #apply mask
    if mask is not None:
        pdata = np.ma.masked_array(pdata, mask=mask)
    #generate the smartlevel
    if smartlevel == True:
        if levels is not None:
            raise ValueError("levels must be None when smartlevel is True!")
        else:
            levels = _generate_smartlevel(pdata)
            data_transform = True
    #prepare the data for contourf
    pdata,plotlev,plotlab,extend,trans_base_list = \
        _transform_data(pdata,levels,data_transform,extend=extend)
    return (mgmap, pdata, plotlev, plotlab, extend, trans_base_list,
            data_transform)
コード例 #2
0
ファイル: bmap.py プロジェクト: bnordgren/pylsce
def _generate_map_prepare_data(
    data=None,
    lat=None,
    lon=None,
    projection="cyl",
    mapbound="all",
    gridstep=(30, 30),
    shift=False,
    map_threshold=None,
    levels=None,
    cmap=None,
    smartlevel=None,
    data_transform=False,
    gmapkw={},
    ax=None,
):
    """
    This function makes the map, and transform data for ready
        use of m.contourf or m.imshow
    """
    if shift == True:
        data, lon = bmp.shiftgrid(180, data, lon, start=False)
    mgmap = gmap(ax, projection, mapbound, lat, lon, gridstep, **gmapkw)
    m, lonpro, latpro, latind, lonind = (mgmap.m, mgmap.lonpro, mgmap.latpro, mgmap.latind, mgmap.lonind)

    pdata = data[latind[0] : latind[-1] + 1, lonind[0] : lonind[-1] + 1]
    # mask by map_threshold
    pdata = mathex.ndarray_mask_by_threshold(pdata, map_threshold)
    # generate the smartlevel
    if smartlevel == True:
        if levels != None:
            raise ValueError("levels must be None when smartlevel is True!")
        else:
            levels = _generate_smartlevel(pdata)
            data_transform = True
    # prepare the data for contourf
    pdata, plotlev, plotlab, extend, trans_base_list = _transform_data(pdata, levels, data_transform)
    return (mgmap, pdata, plotlev, plotlab, extend, trans_base_list, data_transform)
コード例 #3
0
ファイル: bmap.py プロジェクト: sunshine1204/pylsce
def contourfmap(ax=None,
                lat=None,
                lon=None,
                indata=None,
                projection='cyl',
                mapbound='all',
                gridstep=(30, 30),
                shift=False,
                map_threshold=None,
                cmap=None,
                colorbarlabel=None,
                forcelabel=None,
                levels=None,
                data_transform=False,
                return_lev_lab=False,
                colorbardic={},
                cbarkw={}):
    """
    Purpose: plot a map on 'cyl' or 'npstere' projection.
    Arguments:
        ax --> An axes instance
        projection --> for now two projections have been added:
            1. 'cyl' -- for global and regional mapping
            2. 'npstere' -- for north polar centered mapping.
        lat,lon --> geographic coordinate variables; lat must be in
            desceding order and lon must be ascending.
        mapbound --> specify the bound for mapping;
            1. 'cyl'
                tuple containing (lat1,lat2,lon1,lon2); lat1 --> lower
                parallel; lat2 --> upper parallel; lon1 --> left meridian;
                lon2 --> right meridian; default 'all' means plot
                the extent of input lat, lon coordinate variables;
                for global mapping, set (-90,90,-180,180) or (-90,90,0,360).
            2. 'npstere'
                mapbound={'blat':45,'lon_0':0,'para0':40}
                blat --> boundinglat in the bmp.Basemap method.
                    The souther limit for mapping.
                lon_0 --> center of desired map domain.
                para0 --> souther boundary for parallel ticks, the default
                    norther limit is 90; default longitude 0-360 (or -180-180)
        gridstep --> the step for parallel and meridian grid for the map,
            tuple containing (parallel_step, meridian_step).
        levels --> default None; levels=[-5,-2,-1,0,1,2,5] ;
            or levels=[(-10,-4,-2,-1,-0.4),(-0.2,-0.1,0,0.1,0.2),
                        (0.4,1,2,4,10)].
            1.  Anything that can work as input for function pb.iteflat()
                will work.
            2.  If the first and last element of pb.iteflat(levels) is
                np.NINF and np.PINF, the colorbar of contourf plot will
                use the 'two-arrow' shape.
            3.  If data_transform==True, the input data will be transformed
                from pb.iteflat(levels) to
                np.linspace(1,len(pb.iteflat(interval_original)). this can
                help to create arbitrary contrasting in the plot.
                cf. mathex.plot_array_transg
        data_transform:
            1. set as True if increased contrast in the plot is desired.
                In this case the function mathex.plot_array_transg will
                be called and pb.iteflat(levels) will be used as original
                interval for data transformation.
            2. In case of data_transform==False, pb.iteflat(levels)
                will be used directly in the plt.contour function for
                ploting and hence no data transformation is made. The
                treatment by this way allows very flexible
                (in a mixed way) to set levels.
            3. In any case, if np.NINF and np.PINF as used as two
                extremes of levels, arrowed colorbar will be returned.

        colorbarlabel:
            1. used to put customized colorbar label and this will override
                using levels as colorbar. IF colorbarlabel!=None,
                colorbar ticks and labels will be set using colorbarlabel.
                so this means colorbarlabel could only be array or
                list of numbers.
            2. If data_transform==True, colorbar will also be transformed
                accordingly. In this case, the colorbar ticks will use
                transformed colorbarlabel data, but colorbar ticklables
                will use non-transformed colorbarlabel data. This means
                the actual ticks numbers and labels are not the same.

        forcelabel --> to force the colorbar label as specified by forcelabel.
            This is used in case to set the labels not in numbers but in
            other forms (eg. strings).
            In case of data_transform = True, levels will be used to
            specifiy levels for the original colorbar, colorbarlabel will
            be used to create ticks on colrobar which will be labeled,
            if forcelabel=None, then colorbarlabel will agined be used
            to label the ticks, otherwise forcelabel will be used to
            label the ticks on the colorbar. So this means forcelabel will
            mainly be list of strings.

        indata --> numpy array with dimension of len(lat)Xlen(lon)
        map_threshold --> dictionary like {'lb':2000,'ub':5000}, data
            less than 2000 and greater than 5000 will be masked.
            Note this will be applied before data.
               transform.
        shift --> boolean value. False for longtitude data ranging [-180,180];
            for longtitude data ranging [0,360] set shift to True if a
            180 east shift is desired. if shift as True, the mapbound
            range should be set using shifted longtitude
            (use -180,180 rather than 0,360).
        colorbardic --> dictionary to specify the attributes for colorbar,
            translate all the keys in function bmp.Basemap.colorbar()
            into keys in colorbardic to manipulation.

    Note:
        1.  lat must be descending, and lon must be ascending.
        2*. NOTE use both data_transform=True and impose unequal
            colorbarlabel could be very confusing! Because normaly in
            case of data_transform as True the labels are ALREADY
            UNEQUALLY distributed!

            an example to use colorbarlabel and forcelabel:
            data_transform=True,
            levels=[0,1,2,3,4,5,6,7,8]
            colorbarlabel=[0,2,4,6,8]
            forcelabel=['extreme low','low','middle','high','extreme high']

            So colorbarlabel will set both ticks and labels, but forcelabel
            will further overwrite the labels.

        3. This function has been test using data, the script and
            generated PNG files are availabe at ~/python/bmaptest
    See also:
        mathex.plot_array_transg
    """
    print "Deprecating Warning!: use mapcontourf instead!"
    if ax == None:
        fig, ax = g.Create_1Axes()
    if shift == True:
        indata, lon = bmp.shiftgrid(180, indata, lon, start=False)
    m, lonpro, latpro, latind, lonind = makemap(ax, projection, mapbound, lat,
                                                lon, gridstep)
    pdata = indata[latind[0]:latind[-1] + 1, lonind[0]:lonind[-1] + 1]
    #mask by map_threshold
    pdata = mathex.ndarray_mask_by_threshold(pdata, map_threshold)
    #prepare the data for contourf
    pdata,plotlev,plotlab,extend,trans_base_list = \
        _transform_data(pdata,levels,data_transform)
    #make the contourf plot
    if cmap == None:
        cmap = mat.cm.jet
    cs = m.contourf(lonpro,
                    latpro,
                    pdata,
                    levels=plotlev,
                    extend=extend,
                    cmap=cmap)

    ##handle colorbar
    #handle the colorbar attributes by using dictionary which flexibility.
    location = colorbardic.get('location', 'right')
    size = colorbardic.get('size', '3%')
    pad = colorbardic.get('pad', '2%')
    cbar = m.colorbar(cs, location=location, size=size, pad=pad, **cbarkw)
    #set colorbar ticks and colorbar label
    if levels == None:
        pass
    else:
        ticks,labels = \
            _generate_colorbar_ticks_label(data_transform=data_transform,
                                           colorbarlabel=colorbarlabel,
                                           trans_base_list=trans_base_list,
                                           forcelabel=forcelabel,
                                           plotlev=plotlev,
                                           plotlab=plotlab)
        cbar.set_ticks(ticks)
        cbar.set_ticklabels(labels)
    #return
    if return_lev_lab == True:
        return m, cbar, plotlev, plotlab
    else:
        return m, cbar
コード例 #4
0
ファイル: bmap.py プロジェクト: bnordgren/pylsce
def contourfmap(
    ax=None,
    lat=None,
    lon=None,
    indata=None,
    projection="cyl",
    mapbound="all",
    gridstep=(30, 30),
    shift=False,
    map_threshold=None,
    cmap=None,
    colorbarlabel=None,
    forcelabel=None,
    levels=None,
    data_transform=False,
    return_lev_lab=False,
    colorbardic={},
    cbarkw={},
):
    """
    Purpose: plot a map on 'cyl' or 'npstere' projection.
    Arguments:
        ax --> An axes instance
        projection --> for now two projections have been added:
            1. 'cyl' -- for global and regional mapping
            2. 'npstere' -- for north polar centered mapping.
        lat,lon --> geographic coordinate variables; lat must be in
            desceding order and lon must be ascending.
        mapbound --> specify the bound for mapping;
            1. 'cyl'
                tuple containing (lat1,lat2,lon1,lon2); lat1 --> lower
                parallel; lat2 --> upper parallel; lon1 --> left meridian;
                lon2 --> right meridian; default 'all' means plot
                the extent of input lat, lon coordinate variables;
                for global mapping, set (-90,90,-180,180) or (-90,90,0,360).
            2. 'npstere'
                mapbound={'blat':45,'lon_0':0,'para0':40}
                blat --> boundinglat in the bmp.Basemap method.
                    The souther limit for mapping.
                lon_0 --> center of desired map domain.
                para0 --> souther boundary for parallel ticks, the default
                    norther limit is 90; default longitude 0-360 (or -180-180)
        gridstep --> the step for parallel and meridian grid for the map,
            tuple containing (parallel_step, meridian_step).
        levels --> default None; levels=[-5,-2,-1,0,1,2,5] ;
            or levels=[(-10,-4,-2,-1,-0.4),(-0.2,-0.1,0,0.1,0.2),
                        (0.4,1,2,4,10)].
            1.  Anything that can work as input for function pb.iteflat()
                will work.
            2.  If the first and last element of pb.iteflat(levels) is
                np.NINF and np.PINF, the colorbar of contourf plot will
                use the 'two-arrow' shape.
            3.  If data_transform==True, the input data will be transformed
                from pb.iteflat(levels) to
                np.linspace(1,len(pb.iteflat(interval_original)). this can
                help to create arbitrary contrasting in the plot.
                cf. mathex.plot_array_transg
        data_transform:
            1. set as True if increased contrast in the plot is desired.
                In this case the function mathex.plot_array_transg will
                be called and pb.iteflat(levels) will be used as original
                interval for data transformation.
            2. In case of data_transform==False, pb.iteflat(levels)
                will be used directly in the plt.contour function for
                ploting and hence no data transformation is made. The
                treatment by this way allows very flexible
                (in a mixed way) to set levels.
            3. In any case, if np.NINF and np.PINF as used as two
                extremes of levels, arrowed colorbar will be returned.

        colorbarlabel:
            1. used to put customized colorbar label and this will override
                using levels as colorbar. IF colorbarlabel!=None,
                colorbar ticks and labels will be set using colorbarlabel.
                so this means colorbarlabel could only be array or
                list of numbers.
            2. If data_transform==True, colorbar will also be transformed
                accordingly. In this case, the colorbar ticks will use
                transformed colorbarlabel data, but colorbar ticklables
                will use non-transformed colorbarlabel data. This means
                the actual ticks numbers and labels are not the same.

        forcelabel --> to force the colorbar label as specified by forcelabel.
            This is used in case to set the labels not in numbers but in
            other forms (eg. strings).
            In case of data_transform = True, levels will be used to
            specifiy levels for the original colorbar, colorbarlabel will
            be used to create ticks on colrobar which will be labeled,
            if forcelabel=None, then colorbarlabel will agined be used
            to label the ticks, otherwise forcelabel will be used to
            label the ticks on the colorbar. So this means forcelabel will
            mainly be list of strings.

        indata --> numpy array with dimension of len(lat)Xlen(lon)
        map_threshold --> dictionary like {'lb':2000,'ub':5000}, data
            less than 2000 and greater than 5000 will be masked.
            Note this will be applied before data.
               transform.
        shift --> boolean value. False for longtitude data ranging [-180,180];
            for longtitude data ranging [0,360] set shift to True if a
            180 east shift is desired. if shift as True, the mapbound
            range should be set using shifted longtitude
            (use -180,180 rather than 0,360).
        colorbardic --> dictionary to specify the attributes for colorbar,
            translate all the keys in function bmp.Basemap.colorbar()
            into keys in colorbardic to manipulation.

    Note:
        1.  lat must be descending, and lon must be ascending.
        2*. NOTE use both data_transform=True and impose unequal
            colorbarlabel could be very confusing! Because normaly in
            case of data_transform as True the labels are ALREADY
            UNEQUALLY distributed!

            an example to use colorbarlabel and forcelabel:
            data_transform=True,
            levels=[0,1,2,3,4,5,6,7,8]
            colorbarlabel=[0,2,4,6,8]
            forcelabel=['extreme low','low','middle','high','extreme high']

            So colorbarlabel will set both ticks and labels, but forcelabel
            will further overwrite the labels.

        3. This function has been test using data, the script and
            generated PNG files are availabe at ~/python/bmaptest
    See also:
        mathex.plot_array_transg
    """
    print "Deprecating Warning!: use mapcontourf instead!"
    if ax == None:
        fig, ax = g.Create_1Axes()
    if shift == True:
        indata, lon = bmp.shiftgrid(180, indata, lon, start=False)
    m, lonpro, latpro, latind, lonind = makemap(ax, projection, mapbound, lat, lon, gridstep)
    pdata = indata[latind[0] : latind[-1] + 1, lonind[0] : lonind[-1] + 1]
    # mask by map_threshold
    pdata = mathex.ndarray_mask_by_threshold(pdata, map_threshold)
    # prepare the data for contourf
    pdata, plotlev, plotlab, extend, trans_base_list = _transform_data(pdata, levels, data_transform)
    # make the contourf plot
    if cmap == None:
        cmap = mat.cm.jet
    cs = m.contourf(lonpro, latpro, pdata, levels=plotlev, extend=extend, cmap=cmap)

    ##handle colorbar
    # handle the colorbar attributes by using dictionary which flexibility.
    location = colorbardic.get("location", "right")
    size = colorbardic.get("size", "3%")
    pad = colorbardic.get("pad", "2%")
    cbar = m.colorbar(cs, location=location, size=size, pad=pad, **cbarkw)
    # set colorbar ticks and colorbar label
    if levels == None:
        pass
    else:
        ticks, labels = _generate_colorbar_ticks_label(
            data_transform=data_transform,
            colorbarlabel=colorbarlabel,
            trans_base_list=trans_base_list,
            forcelabel=forcelabel,
            plotlev=plotlev,
            plotlab=plotlab,
        )
        cbar.set_ticks(ticks)
        cbar.set_ticklabels(labels)
    # return
    if return_lev_lab == True:
        return m, cbar, plotlev, plotlab
    else:
        return m, cbar