Cool stuff in this tutorial:

1) use iris package to understand netcdf


In [1]:
fname = '~brodzik/cartopy-tutorial/resources/HadCRUT/fogg_mean_surface_temp.nc'

In [2]:
!ncdump -h $fname


netcdf fogg_mean_surface_temp {
dimensions:
	time = 3 ;
	latitude = 36 ;
	longitude = 72 ;
	bnds = 2 ;
variables:
	double mean_surface_temperature(time, latitude, longitude) ;
		mean_surface_temperature:_FillValue = 1.e+20 ;
		mean_surface_temperature:long_name = "Mean Surface Temperature" ;
		mean_surface_temperature:units = "K" ;
		mean_surface_temperature:cell_methods = "decade: month_number: mean" ;
		mean_surface_temperature:coordinates = "decade month_number" ;
	float time(time) ;
		time:axis = "T" ;
		time:bounds = "time_bnds" ;
		time:units = "days since 1850-1-1 00:00:00" ;
		time:standard_name = "time" ;
		time:long_name = "time" ;
		time:calendar = "gregorian" ;
	float time_bnds(time, bnds) ;
	float latitude(latitude) ;
		latitude:axis = "Y" ;
		latitude:units = "degrees_north" ;
		latitude:standard_name = "latitude" ;
		latitude:long_name = "latitude" ;
		latitude:point_spacing = "even" ;
	float longitude(longitude) ;
		longitude:axis = "X" ;
		longitude:units = "degrees_east" ;
		longitude:standard_name = "longitude" ;
		longitude:long_name = "longitude" ;
		longitude:point_spacing = "even" ;
	int64 decade(time) ;
		decade:units = "1" ;
		decade:long_name = "decade" ;
	int64 month_number(time) ;
		month_number:units = "1" ;
		month_number:long_name = "month_number" ;

// global attributes:
		:comment = "Decadal mean surface temperature for Oct-Dec 1870s" ;
		:history = "" ;
		:institution = "Climatic Research Unit, University of East Anglia/Met Office Hadley Centre" ;
		:reference = "Jones, P. D., D. H. Lister, T. J. Osborn, C. Harpham, M. Salmon, and C. P. Morice (2012), Hemispheric and large-scale land-surface air temperature variations: An extensive revision and an update to 2010, J. Geophys. Res., 117, D05127, doi:10.1029/2011JD017139" ;
		:reference_period = 1961s, 1990s ;
		:source = "" ;
		:title = "CRUTEM4 temperature anomalies" ;
		:version = "CRUTEM.4.6.0.0" ;
		:Conventions = "CF-1.5" ;
}

In [3]:
import iris
cube = iris.load_cube(fname)


/Users/brodzik/.conda/envs/cartopy/lib/python3.6/site-packages/iris/fileformats/_pyke_rules/compiled_krb/fc_rules_cf_fc.py:2029: UserWarning: Gracefully filling 'latitude' dimension coordinate masked points
  warnings.warn(msg.format(str(cf_coord_var.cf_name)))
/Users/brodzik/.conda/envs/cartopy/lib/python3.6/site-packages/iris/fileformats/_pyke_rules/compiled_krb/fc_rules_cf_fc.py:2029: UserWarning: Gracefully filling 'longitude' dimension coordinate masked points
  warnings.warn(msg.format(str(cf_coord_var.cf_name)))
/Users/brodzik/.conda/envs/cartopy/lib/python3.6/site-packages/iris/fileformats/_pyke_rules/compiled_krb/fc_rules_cf_fc.py:2029: UserWarning: Gracefully filling 'time' dimension coordinate masked points
  warnings.warn(msg.format(str(cf_coord_var.cf_name)))
/Users/brodzik/.conda/envs/cartopy/lib/python3.6/site-packages/iris/fileformats/_pyke_rules/compiled_krb/fc_rules_cf_fc.py:2036: UserWarning: Gracefully filling 'time' dimension coordinate masked bounds
  warnings.warn(msg.format(str(cf_coord_var.cf_name)))

In [4]:
cube


Out[4]:
Mean Surface Temperature (K) time latitude longitude
Shape 3 36 72
Dimension coordinates
time x - -
latitude - x -
longitude - - x
Auxiliary coordinates
decade x - -
month_number x - -
Attributes
Conventions CF-1.5
comment Decadal mean surface temperature for Oct-Dec 1870s
history
institution Climatic Research Unit, University of East Anglia/Met Office Hadley Ce...
reference Jones, P. D., D. H. Lister, T. J. Osborn, C. Harpham, M. Salmon, and C....
reference_period [1961 1990]
source
title CRUTEM4 temperature anomalies
version CRUTEM.4.6.0.0
Cell methods
mean decade, month_number

In [5]:
print(cube.coord('decade').points)


[1870 1870 1870]

In [6]:
print(cube.coord('month_number').points)


[10 11 12]

In [7]:
print(cube.coord('time'))


DimCoord([1875-04-17 00:00:00, 1875-05-17 12:00:00, 1875-06-17 00:00:00], bounds=[[1870-10-16 12:00:00, 1879-10-16 12:00:00],
       [1870-11-16 00:00:00, 1879-11-16 00:00:00],
       [1870-12-16 12:00:00, 1879-12-16 12:00:00]], standard_name='time', calendar='gregorian', long_name='time', var_name='time')

In [8]:
%matplotlib notebook

import cartopy.crs as ccrs
import iris.plot as iplt
import matplotlib.pyplot as plt

In [9]:
plt.figure()

october_cube = cube.extract(iris.Constraint(month_number=10))

ax1 = plt.subplot(211, projection=ccrs.Robinson())
iplt.pcolormesh(october_cube)
plt.title('pcolormesh')
ax1.coastlines()
ax1.set_global()

ax2 = plt.subplot(212, projection=ccrs.Robinson())
iplt.contourf(october_cube)
plt.title('contourf')
ax2.coastlines()
ax2.set_global()

plt.tight_layout()

plt.show()


/Users/brodzik/.conda/envs/cartopy/lib/python3.6/site-packages/iris/coords.py:1000: UserWarning: Coordinate 'longitude' is not bounded, guessing contiguous bounds.
  'contiguous bounds.'.format(self.name()))
/Users/brodzik/.conda/envs/cartopy/lib/python3.6/site-packages/iris/coords.py:1000: UserWarning: Coordinate 'latitude' is not bounded, guessing contiguous bounds.
  'contiguous bounds.'.format(self.name()))

Recall the exercise to plot the geolocated Wikipedia image of Foggs proposed route in its native Robinson projection...


In [10]:
# Our calculated geolocated raster image extents.
extent = [-13636707, 17044670,
          -6308712, 8565930]

plt.figure()

fname = '/Users/brodzik/cartopy-tutorial/resources/640px-Around_the_World_in_Eighty_Days_map.png'

rob = ccrs.Robinson(central_longitude=11.25)
ax = plt.axes(projection=rob)

ax.gridlines(color='gray', linestyle='--')
ax.coastlines()
img = plt.imread(fname)
ax.imshow(img, extent=extent, transform=rob, origin='upper')
ax.set_global()

plt.tight_layout()
plt.show()


Now use the wikipedia image, but reproject it to Platte-Carree...


In [11]:
plt.figure()

# this kills the kernel, not sure why:
#ax = plt.axes(projection=ccrs.LambertAzimuthalEqualArea(central_latitude=90.))
ax = plt.axes(projection=ccrs.PlateCarree())
ax.gridlines(color='gray', linestyle='--')
ax.coastlines()

img = plt.imread(fname)
ax.imshow(img, extent=extent, transform=rob, origin='upper')
ax.set_global()

plt.tight_layout()
plt.show()


Exercise 3.1: Use iris to plot the decadal mean surface temperatures for October 1870s, using either a block or contour plot, over the above Wikipedia image in a Plate Carree projection.


In [18]:
plt.figure()

ax = plt.axes(projection=ccrs.PlateCarree())
ax.gridlines(color='gray', linestyle='--')
ax.coastlines()

# wikipedia image
img = plt.imread(fname)
ax.imshow(img, extent=extent, transform=rob, origin='upper')

# October 1870s mean surf temps
iplt.contourf(october_cube, alpha=0.25)

plt.title('October 1870s decadal mean surface temps')

ax.set_global()

plt.tight_layout()
plt.show()


No handles with labels found to put in legend.

In [ ]: