Python 将Basemap转换为Cartopy,是否有Basemap';s shiftgrid()?

Python 将Basemap转换为Cartopy,是否有Basemap';s shiftgrid()?,python,matplotlib-basemap,cartopy,Python,Matplotlib Basemap,Cartopy,我正在将一个使用matplotlib工具包Basemap的应用程序转换为使用Cartopy,为从Python2迁移到Python3做准备。 我在Cartopy中找到了Basemap的“addcyclic()”和“maskoceans()”的类似函数, 但是,对于Basemap的shiftgrid()函数,我在numpy或Cartopy中都找不到类似的内容 这是使用Basemap的代码: ''' ''' 初始数据: 数据 ''' ''' 我尝试了下面的cartopy代码来重新创建Basemap s

我正在将一个使用matplotlib工具包Basemap的应用程序转换为使用Cartopy,为从Python2迁移到Python3做准备。 我在Cartopy中找到了Basemap的“addcyclic()”和“maskoceans()”的类似函数, 但是,对于Basemap的shiftgrid()函数,我在numpy或Cartopy中都找不到类似的内容

这是使用Basemap的代码: '''

'''

初始数据: 数据 '''

''' 我尝试了下面的cartopy代码来重新创建Basemap shiftgrid所做的工作。 这是Cartopy代码,在我尝试过的时候,有些东西被注释掉了: '''

'''

数据和经度是原始的,我只是在投影中使用了“中心经度”。 底图图像显示了整个地球,但Cartopy图像仅显示赤道上方。 数据的颜色看起来很相似,除了最右边,所以我担心数据在Cartopy中的映射与在Basemap中的不一样

所以,问题是。。。是否有与Basemap的shiftgrid()等效的东西,或者我需要找出与Basemap的shiftgrid()类似的东西,或者只在投影中使用“中心经度”? 我似乎无法粘贴.png文件。 非常感谢您的帮助。 我在web上搜索了等效函数,但没有找到shiftgrid()的函数。
谢谢。

我不知道有任何类似的
shiftgrid
。在请求这样一个特性时,在服务器上打开一个问题可能是值得的。提到一个可靠的用例来帮助驱动功能,这会有所帮助。

我找到了basemap的shiftgrid函数 您可以将其作为单独的函数与cartopy一起调用

import numpy as np
import numpy.ma as ma
def shiftgrid(lon0,datain,lonsin,start=True,cyclic=360.0):
    """
    Shift global lat/lon grid east or west.
    .. tabularcolumns:: |l|L|
    ==============   ====================================================
    Arguments        Description
    ==============   ====================================================
    lon0             starting longitude for shifted grid
                     (ending longitude if start=False). lon0 must be on
                     input grid (within the range of lonsin).
    datain           original data with longitude the right-most
                     dimension.
    lonsin           original longitudes.
    ==============   ====================================================
    .. tabularcolumns:: |l|L|
    ==============   ====================================================
    Keywords         Description
    ==============   ====================================================
    start            if True, lon0 represents the starting longitude
                     of the new grid. if False, lon0 is the ending
                     longitude. Default True.
    cyclic           width of periodic domain (default 360)
    ==============   ====================================================
    returns ``dataout,lonsout`` (data and longitudes on shifted grid).
    """
    if np.fabs(lonsin[-1]-lonsin[0]-cyclic) > 1.e-4:
        # Use all data instead of raise ValueError, 'cyclic point not included'
        start_idx = 0
    else:
        # If cyclic, remove the duplicate point
        start_idx = 1
    if lon0 < lonsin[0] or lon0 > lonsin[-1]:
        raise ValueError('lon0 outside of range of lonsin')
    i0 = np.argmin(np.fabs(lonsin-lon0))
    i0_shift = len(lonsin)-i0
    if ma.isMA(datain):
        dataout  = ma.zeros(datain.shape,datain.dtype)
    else:
        dataout  = np.zeros(datain.shape,datain.dtype)
    if ma.isMA(lonsin):
        lonsout = ma.zeros(lonsin.shape,lonsin.dtype)
    else:
        lonsout = np.zeros(lonsin.shape,lonsin.dtype)
    if start:
        lonsout[0:i0_shift] = lonsin[i0:]
    else:
        lonsout[0:i0_shift] = lonsin[i0:]-cyclic
    dataout[...,0:i0_shift] = datain[...,i0:]
    if start:
        lonsout[i0_shift:] = lonsin[start_idx:i0+start_idx]+cyclic
    else:
        lonsout[i0_shift:] = lonsin[start_idx:i0+start_idx]
    dataout[...,i0_shift:] = datain[...,start_idx:i0+start_idx]
    return dataout,lonsout
将numpy导入为np
将numpy.ma导入为ma
def shiftgrid(lon0、datain、lonsin、start=True、cyclic=360.0):
"""
将全球纬度/经度网格向东或向西移动。
…表格列::| l | l|
==============   ====================================================
参数描述
==============   ====================================================
移位网格的lon0起始经度
(如果开始=False,则结束经度)。lon0必须处于启用状态
输入网格(在lonsin范围内)。
原始数据中最右边为经度的数据
维
朗辛原始经度。
==============   ====================================================
…表格列::| l | l|
==============   ====================================================
关键词描述
==============   ====================================================
开始如果为True,则lon0表示开始经度
如果为False,则lon0为结尾
经度。默认为True。
周期域的循环宽度(默认值360)
==============   ====================================================
返回``dataout,lonsout``(移位网格上的数据和经度)。
"""
如果np.fabs(lonsin[-1]-lonsin[0]-循环)>1.e-4:
#使用所有数据,而不是raise VALUE ERROR,“不包括循环点”
start_idx=0
其他:
#如果是循环的,请删除重复点
start_idx=1
如果lon0lonsin[-1]:
raise VALUE ERROR('lon0超出lonsin'的范围)
i0=np.argmin(np.fabs(lonsin-lon0))
i0_shift=len(lonsin)-i0
如果ma.isMA(数据输入):
dataout=ma.zeros(datain.shape,datain.dtype)
其他:
dataout=np.zeros(datain.shape,datain.dtype)
如果ma.isMA(lonsin):
lonsout=ma.zeros(lonsin.shape,lonsin.dtype)
其他:
lonsout=np.zero(lonsin.shape,lonsin.dtype)
如果启动:
lonsout[0:i0\u shift]=lonsin[i0:]
其他:
lonsout[0:i0_shift]=lonsin[i0:]-循环
数据输出[…,0:i0_移位]=数据输入[…,i0:]
如果启动:
lonsout[i0\u shift:]=lonsin[start\u idx:i0+start\u idx]+循环
其他:
lonsout[i0\u shift:]=lonsin[start\u idx:i0+start\u idx]
dataout[…,i0\u shift:]=datain[…,start\u idx:i0+start\u idx]
返回数据输出,lonsout

这一定是最不雅观的解决方案,但我为Basemap的一些有用功能所做的只是从Basemap的源代码中复制函数定义,而这些功能在cartopy中还没有出现。它很好用。例如,shiftgrid:

def shiftgrid(lon0,datain,lonsin,start=True,cyclic=360.0):
"""
Shift global lat/lon grid east or west.
.. tabularcolumns:: |l|L|
==============   ====================================================
Arguments        Description
==============   ====================================================
lon0             starting longitude for shifted grid
                 (ending longitude if start=False). lon0 must be on
                 input grid (within the range of lonsin).
datain           original data with longitude the right-most
                 dimension.
lonsin           original longitudes.
==============   ====================================================
.. tabularcolumns:: |l|L|
==============   ====================================================
Keywords         Description
==============   ====================================================
start            if True, lon0 represents the starting longitude
                 of the new grid. if False, lon0 is the ending
                 longitude. Default True.
cyclic           width of periodic domain (default 360)
==============   ====================================================
returns ``dataout,lonsout`` (data and longitudes on shifted grid).
"""
if np.fabs(lonsin[-1]-lonsin[0]-cyclic) > 1.e-4:
    # Use all data instead of raise ValueError, 'cyclic point not included'
    start_idx = 0
else:
    # If cyclic, remove the duplicate point
    start_idx = 1
if lon0 < lonsin[0] or lon0 > lonsin[-1]:
    raise ValueError('lon0 outside of range of lonsin')
i0 = np.argmin(np.fabs(lonsin-lon0))
i0_shift = len(lonsin)-i0
if ma.isMA(datain):
    dataout  = ma.zeros(datain.shape,datain.dtype)
else:
    dataout  = np.zeros(datain.shape,datain.dtype)
if ma.isMA(lonsin):
    lonsout = ma.zeros(lonsin.shape,lonsin.dtype)
else:
    lonsout = np.zeros(lonsin.shape,lonsin.dtype)
if start:
    lonsout[0:i0_shift] = lonsin[i0:]
else:
    lonsout[0:i0_shift] = lonsin[i0:]-cyclic
dataout[...,0:i0_shift] = datain[...,i0:]
if start:
    lonsout[i0_shift:] = lonsin[start_idx:i0+start_idx]+cyclic
else:
    lonsout[i0_shift:] = lonsin[start_idx:i0+start_idx]
dataout[...,i0_shift:] = datain[...,start_idx:i0+start_idx]
return dataout,lonsout
def shiftgrid(lon0、datain、lonsin、start=True、cyclic=360.0):
"""
将全球纬度/经度网格向东或向西移动。
…表格列::| l | l|
==============   ====================================================
参数描述
==============   ====================================================
移位网格的lon0起始经度
(如果开始=False,则结束经度)。lon0必须处于启用状态
输入网格(在lonsin范围内)。
原始数据中最右边为经度的数据
维
朗辛原始经度。
==============   ====================================================
…表格列::| l | l|
==============   ====================================================
关键词描述
==============   ====================================================
开始如果为True,则lon0表示开始经度
如果为False,则lon0为结尾
经度。默认为True。
周期域的循环宽度(默认值360)
==============   ====================================================
返回``dataout,lonsout``(移位网格上的数据和经度)。
"""
如果np.fabs(lonsin[-1]-lonsin[0]-循环)>1.e-4:
#使用所有数据,而不是raise VALUE ERROR,“不包括循环点”
start_idx=0
其他:
#如果是循环的,请删除重复点
sta
    DATA_CRS = ccrs.PlateCarree()
    lons = np.arange(30, 410, 30)
    lons[1] = 70
    lats = np.arange(0, 100, 10)

    data = np.indices((lats.shape[0], lons.shape[0]))
    data = data[0] + data[1]
    # data2 = np.roll(data, -5)
    # lons2 = np.mod(lons2 - 180.0, 360.0) - 180.0
    cm_lon = 0
    #llons, llats = np.meshgrid(lons2, lats)
    llons, llats = np.meshgrid(lons, lats)
    PROJECTION = ccrs.Orthographic(central_longitude=cm_lon)
    fig1 = plt.figure(num=1, figsize=(11, 8.5), dpi=150)
    ax = plt.axes(projection=PROJECTION)
    ax.add_feature(cfeature.COASTLINE, linewidths=0.7)
    ax.add_feature(cfeature.BORDERS, edgecolor='black', linewidths=0.7)
    ax.contourf(llons, llats, data, transform=ccrs.PlateCarree())
import numpy as np
import numpy.ma as ma
def shiftgrid(lon0,datain,lonsin,start=True,cyclic=360.0):
    """
    Shift global lat/lon grid east or west.
    .. tabularcolumns:: |l|L|
    ==============   ====================================================
    Arguments        Description
    ==============   ====================================================
    lon0             starting longitude for shifted grid
                     (ending longitude if start=False). lon0 must be on
                     input grid (within the range of lonsin).
    datain           original data with longitude the right-most
                     dimension.
    lonsin           original longitudes.
    ==============   ====================================================
    .. tabularcolumns:: |l|L|
    ==============   ====================================================
    Keywords         Description
    ==============   ====================================================
    start            if True, lon0 represents the starting longitude
                     of the new grid. if False, lon0 is the ending
                     longitude. Default True.
    cyclic           width of periodic domain (default 360)
    ==============   ====================================================
    returns ``dataout,lonsout`` (data and longitudes on shifted grid).
    """
    if np.fabs(lonsin[-1]-lonsin[0]-cyclic) > 1.e-4:
        # Use all data instead of raise ValueError, 'cyclic point not included'
        start_idx = 0
    else:
        # If cyclic, remove the duplicate point
        start_idx = 1
    if lon0 < lonsin[0] or lon0 > lonsin[-1]:
        raise ValueError('lon0 outside of range of lonsin')
    i0 = np.argmin(np.fabs(lonsin-lon0))
    i0_shift = len(lonsin)-i0
    if ma.isMA(datain):
        dataout  = ma.zeros(datain.shape,datain.dtype)
    else:
        dataout  = np.zeros(datain.shape,datain.dtype)
    if ma.isMA(lonsin):
        lonsout = ma.zeros(lonsin.shape,lonsin.dtype)
    else:
        lonsout = np.zeros(lonsin.shape,lonsin.dtype)
    if start:
        lonsout[0:i0_shift] = lonsin[i0:]
    else:
        lonsout[0:i0_shift] = lonsin[i0:]-cyclic
    dataout[...,0:i0_shift] = datain[...,i0:]
    if start:
        lonsout[i0_shift:] = lonsin[start_idx:i0+start_idx]+cyclic
    else:
        lonsout[i0_shift:] = lonsin[start_idx:i0+start_idx]
    dataout[...,i0_shift:] = datain[...,start_idx:i0+start_idx]
    return dataout,lonsout
def shiftgrid(lon0,datain,lonsin,start=True,cyclic=360.0):
"""
Shift global lat/lon grid east or west.
.. tabularcolumns:: |l|L|
==============   ====================================================
Arguments        Description
==============   ====================================================
lon0             starting longitude for shifted grid
                 (ending longitude if start=False). lon0 must be on
                 input grid (within the range of lonsin).
datain           original data with longitude the right-most
                 dimension.
lonsin           original longitudes.
==============   ====================================================
.. tabularcolumns:: |l|L|
==============   ====================================================
Keywords         Description
==============   ====================================================
start            if True, lon0 represents the starting longitude
                 of the new grid. if False, lon0 is the ending
                 longitude. Default True.
cyclic           width of periodic domain (default 360)
==============   ====================================================
returns ``dataout,lonsout`` (data and longitudes on shifted grid).
"""
if np.fabs(lonsin[-1]-lonsin[0]-cyclic) > 1.e-4:
    # Use all data instead of raise ValueError, 'cyclic point not included'
    start_idx = 0
else:
    # If cyclic, remove the duplicate point
    start_idx = 1
if lon0 < lonsin[0] or lon0 > lonsin[-1]:
    raise ValueError('lon0 outside of range of lonsin')
i0 = np.argmin(np.fabs(lonsin-lon0))
i0_shift = len(lonsin)-i0
if ma.isMA(datain):
    dataout  = ma.zeros(datain.shape,datain.dtype)
else:
    dataout  = np.zeros(datain.shape,datain.dtype)
if ma.isMA(lonsin):
    lonsout = ma.zeros(lonsin.shape,lonsin.dtype)
else:
    lonsout = np.zeros(lonsin.shape,lonsin.dtype)
if start:
    lonsout[0:i0_shift] = lonsin[i0:]
else:
    lonsout[0:i0_shift] = lonsin[i0:]-cyclic
dataout[...,0:i0_shift] = datain[...,i0:]
if start:
    lonsout[i0_shift:] = lonsin[start_idx:i0+start_idx]+cyclic
else:
    lonsout[i0_shift:] = lonsin[start_idx:i0+start_idx]
dataout[...,i0_shift:] = datain[...,start_idx:i0+start_idx]
return dataout,lonsout