Regridding? CRU Observed data and CORDEX data in Python Iris

555 views Asked by At

I am trying to compare simulated climate model data from CORDEX to Observed data from CRU 4.00. I am doing this in Python running iris. I have managed to get all of my climate models to run, but the observed data won't. I suspect this is because the model data are in rotated pole grid with x/y axis and 0.44 degree resolution where as the observed data is on a linear grid and 0.5 degree resolution.

In order to make them comparable I think I need to regrid them, but I am a bit confused on how to do this, and the iris userguide is confusing me further... I am relatively new to this!

This is the simplified code to create a line graph showing one model output and the CRU data:

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()

And this is the error I get:

    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

Thank you!

1

There are 1 answers

0
ErikaAWT On

So turns out I didn't need to regrid. In case anyone else wants to run a line graph with CRU data in python with iris. Here is the code to do it. In my case I was limiting the lat/lons to only look at Malawi and I was only interested in some years.

    #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()