2017-08-29 68 views
0

我試圖將來自CORDEX的模擬氣候模型數據與來自CRU 4.00的觀測數據進行比較。我正在Python中運行虹膜。我設法讓所有的氣候模型都能運行,但觀測數據不會。我懷疑這是因爲模型數據處於x/y軸和0.44度分辨率的旋轉極點網格中,其中觀測數據是線性網格和0.5度分辨率。註冊? CRU在Python中觀測到的數據和CORDEX數據Iris

爲了讓它們具有可比性,我認爲我需要重新編寫它們,但是我對如何做到這一點感到困惑,虹膜用戶指南讓我更加困惑......我對此相對較新!

這是簡單的代碼來創建一個線圖顯示一個模型輸出和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 

謝謝!

回答

0

原來我不需要regrid。如果有其他人想用虹膜在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()