重新网格化? CRU在Python Iris中观察到的数据和CORDEX数据

时间:2017-08-29 10:50:45

标签: python-2.7 python-iris

我正在尝试将CORDEX的模拟气候模型数据与CRU 4.00的观测数据进行比较。我在运行虹膜的Python中这样做。我设法让所有的气候模型运行,但观察到的数据不会。我怀疑这是因为模型数据是旋转的极点网格,具有x / y轴和0.44度分辨率,其中观察到的数据是在线性网格和0.5度分辨率上。

为了使它们具有可比性,我认为我需要对它们进行重新划分,但我对如何做到这一点感到困惑,而虹膜用户指南让我更加困惑......我对此比较陌生!

这是创建线图的简化代码,显示了一个模型输出和CRU数据:

import matplotlib.pyplot as plt
import iris
import iris.coord_categorisation as iriscc
import iris.plot as iplt
import iris.quickplot as qplt
import iris.analysis.cartography
import matplotlib.dates as mdates

def main():
    #bring in all the files we need and give them a name
    CCCma = '/exports/csce/datastore/geos/users/s0XXXX/Climate_Modelling/AFR_44_tas/ERAINT/1979-2012/tas_AFR-44_ECMWF-ERAINT_evaluation_r1i1p1_CCCma-CanRCM4_r2_mon_198901-200912.nc'
    CRU = '/exports/csce/datastore/geos/users/s0XXXX/Climate_Modelling/Actual_Data/cru_ts4.00.1901.2015.tmp.dat.nc'

    #Load exactly one cube from given file
    CCCma = iris.load_cube(CCCma)
    CRU = iris.load_cube(CRU, 'near-surface temperature')

    #remove flat latitude and longitude and only use grid latitude and grid longitude

    lats = iris.coords.DimCoord(CCCma.coord('latitude').points[:,0], \
                                standard_name='latitude', units='degrees')
    lons = CCCma.coord('longitude').points[0]
    for i in range(len(lons)):
        if lons[i]>100.:
            lons[i] = lons[i]-360.
    lons = iris.coords.DimCoord(lons, \
                                standard_name='longitude', units='degrees')

    CCCma.remove_coord('latitude')
    CCCma.remove_coord('longitude')
    CCCma.remove_coord('grid_latitude')
    CCCma.remove_coord('grid_longitude')
    CCCma.add_dim_coord(lats, 1)
    CCCma.add_dim_coord(lons, 2)

    lats = iris.coords.DimCoord(CRU.coord('latitude').points[:,0], \
                                standard_name='latitude', units='degrees')
    lons = CRU.coord('longitude').points[0]
    for i in range(len(lons)):
        if lons[i]>100.:
            lons[i] = lons[i]-360.
    lons = iris.coords.DimCoord(lons, \
                                standard_name='longitude', units='degrees')

    CRU.remove_coord('latitude')
    CRU.remove_coord('longitude')
    CRU.remove_coord('grid_latitude')
    CRU.remove_coord('grid_longitude')
    CRU.add_dim_coord(lats, 1)
    CRU.add_dim_coord(lons, 2)

    #we are only interested in the latitude and longitude relevant to Malawi

    Malawi = iris.Constraint(longitude=lambda v: 32.5 <= v <= 36., \
                         latitude=lambda v: -17. <= v <= -9.) 

    CCCma = CCCma.extract(Malawi)
    CRU=CRU.extract(Malawi)

    #time constraignt to make all series the same
    iris.FUTURE.cell_datetime_objects = True
    t_constraint = iris.Constraint(time=lambda cell: 1989 <= cell.point.year <= 2008)

    CCCma = CCCma.extract(t_constraint)
    CRU=CRU.extract(t_constraint)

    #data is in Kelvin, but we would like to show it in Celcius
    CCCma.convert_units('Celsius')
    #CRU.convert_units('Celsius')

    #We are interested in plotting the graph with time along the x ais, so we need a mean of all the coordinates, i.e. mean temperature across whole country    
    iriscc.add_year(CCCma, 'time') 

    CCCma = CCCma.aggregated_by('year', iris.analysis.MEAN)

    CCCma.coord('latitude').guess_bounds()
    CCCma.coord('longitude').guess_bounds()
    CCCma_grid_areas = iris.analysis.cartography.area_weights(CCCma)
    CCCma_mean = CCCma.collapsed(['latitude', 'longitude'],
                                                   iris.analysis.MEAN,
                                                   weights=CCCma_grid_areas)

    iriscc.add_year(CRU, 'time') 

    CRU = CRU.aggregated_by('year', iris.analysis.MEAN)

    CRU.coord('latitude').guess_bounds()
    CRU.coord('longitude').guess_bounds()
    CRU_grid_areas = iris.analysis.cartography.area_weights(CRU)
    CRU_mean = CRU.collapsed(['latitude', 'longitude'],
                                                   iris.analysis.MEAN,
                                                   weights=CRU_grid_areas)                                             

    #set major plot indicators for x-axis                                              
    plt.gca().xaxis.set_major_locator(mdates.YearLocator(5))

    #assign the line colours
    qplt.plot(CCCma_mean, label='CanRCM4_ERAINT', lw=1.5, color='blue')
    qplt.plot(CRU_mean, label='Observed', lw=1.5, color='black')


    #create a legend and set its location to under the graph
    plt.legend(loc="upper center", bbox_to_anchor=(0.5,-0.05), fancybox=True, shadow=True, ncol=2)

    #create a title
    plt.title('Mean Near Surface Temperature for Malawi 1989-2008', fontsize=11)   

    #add grid lines
    plt.grid()

    #show the graph in the console
    iplt.show()    

if __name__ == '__main__':
    main()

这是我得到的错误:

    runfile('/exports/csce/datastore/geos/users/s0XXXX/Climate_Modelling/Python Code and Output Images/Line_Graph_Annual_Tas_Play.py', wdir='/exports/csce/datastore/geos/users/s0XXXX/Climate_Modelling/Python Code and Output Images')
Traceback (most recent call last):

  File "<ipython-input-8-2976c65ebce5>", line 1, in <module>
    runfile('/exports/csce/datastore/geos/users/s0XXXX/Climate_Modelling/Python Code and Output Images/Line_Graph_Annual_Tas_Play.py', wdir='/exports/csce/datastore/geos/users/s0XXXX/Climate_Modelling/Python Code and Output Images')

  File "/usr/lib/python2.7/site-packages/spyderlib/widgets/externalshell/sitecustomize.py", line 685, in runfile
    execfile(filename, namespace)

  File "/usr/lib/python2.7/site-packages/spyderlib/widgets/externalshell/sitecustomize.py", line 78, in execfile
    builtins.execfile(filename, *where)

  File "/exports/csce/datastore/geos/users/s0XXXX/Climate_Modelling/Python Code and Output Images/Line_Graph_Annual_Tas_Play.py", line 124, in <module>
    main()

  File "/exports/csce/datastore/geos/users/s0XXXX/Climate_Modelling/Python Code and Output Images/Line_Graph_Annual_Tas_Play.py", line 42, in main
    lats = iris.coords.DimCoord(CRU.coord('latitude').points[:,0], \

IndexError: too many indices

谢谢!

1 个答案:

答案 0 :(得分:0)

事实证明我不需要重新训练。如果其他人想用虹膜在python中运行带有CRU数据的折线图。这是执行此操作的代码。在我的情况下,我限制了纬度/经度只看马拉维,我只对几年感兴趣。

    #bring in all the files we need and give them a name
    CRU= '/exports/csce/datastore/geos/users/s0XXXX/Climate_Modelling/Actual_Data/cru_ts4.00.1901.2015.tmp.dat.nc'

    #Load exactly one cube from given file
    CRU = iris.load_cube(CRU, 'near-surface temperature')

    #define the latitude and longitude
    lats = iris.coords.DimCoord(CRU.coord('latitude').points, \
                                standard_name='latitude', units='degrees')
    lons = CRU.coord('longitude').points

    #we are only interested in the latitude and longitude relevant to Malawi 
    Malawi = iris.Constraint(longitude=lambda v: 32.5 <= v <= 36., \
                         latitude=lambda v: -17. <= v <= -9.) 
    CRU = CRU.extract(Malawi)

    #time constraignt to make all series the same
    iris.FUTURE.cell_datetime_objects = True
    t_constraint = iris.Constraint(time=lambda cell: 1950 <= cell.point.year <= 2005)
    CRU = CRU.extract(t_constraint)

    #We are interested in plotting the graph with time along the x ais, so we need a mean of all the coordinates, i.e. mean temperature across whole country    
    iriscc.add_year(CRU, 'time') 

    CRU = CRU.aggregated_by('year', iris.analysis.MEAN)

    CRU.coord('latitude').guess_bounds()
    CRU.coord('longitude').guess_bounds()
    CRU_grid_areas = iris.analysis.cartography.area_weights(CRU)
    CRU_mean = CRU.collapsed(['latitude', 'longitude'],
                                                   iris.analysis.MEAN,
                                                   weights=CRU_grid_areas
    #set major plot indicators for x-axis                                              
    plt.gca().xaxis.set_major_locator(mdates.YearLocator(5))

    #assign the line colours
    qplt.plot(CRU_mean, label='Observed', lw=1.5, color='black')

    #create a legend and set its location to under the graph
    plt.legend(loc="upper center", bbox_to_anchor=(0.5,-0.05), fancybox=True, shadow=True, ncol=2)

    #create a title
    plt.title('Mean Near Surface Temperature for Malawi 1950-2005', fontsize=11)   

    #add grid lines
    plt.grid()

    #save the image of the graph and include full legend
    plt.savefig('Historical_Temperature_LineGraph_Annual', bbox_inches='tight')

    #show the graph in the console
    iplt.show()