In [ ]:
    
%matplotlib inline
    
Learning Outcome: by the end of this section, you will be able to use Iris to load datasets from disk as Iris cubes and save Iris cubes back to disk.
Duration: 30 minutes.
Overview:
2.1 Iris Load Functions
2.2 Saving Cubes
2.3 Exercise
2.4 Summary of the Section
In [ ]:
    
import iris
    
There are three main load functions in Iris: load, load_cube and load_cubes.
Note: load_cube is a special case of load.
Let's compare the result of calling load and load_cube. We start by selecting the air_temp.pp file from Iris' sample data.
In [ ]:
    
fname = iris.sample_data_path('air_temp.pp')
    
If we give this filepath to load, we see that Iris returns a cubelist.
In [ ]:
    
cubes = iris.load(fname)
print(type(cubes))
print(cubes)
    
A CubeList is a specialised version of a Python list object : 
If you look at the CubeList reference documentation, you can see that it has the behaviours of an ordinary list, like len(cubes), cubes.remove(x), cubes[index].  However, it prints out in a special way and has additional cube-specific methods, some of which we discuss in later sections (extract, merge and concatenate).
If we pass this filepath instead to iris.load_cube, we see that Iris then returns a cube.
In [ ]:
    
cube = iris.load_cube(fname)
print(type(cube))
print(cube)
    
If we compare the first cube in the cubelist returned by calling load and the cube returned by calling load_cube we see that they are equal.
In [ ]:
    
cubes[0] == cube
    
Try loading in the `uk_hires.pp` from Iris' sample data, first with iris.load then iris.load_cube. Why does iris.load_cube fail if you only supply the filepath?
In [ ]:
    
#
# edit space for user code ...
#
    
The iris.save function provides a convenient interface to save Cube and CubeList instances.
Below we load the uk_hires.pp file from Iris' provided sample data which returns a cubelist of the cubes that were produced from that file. We then save this cubelist out to netcdf.
In [ ]:
    
fname = iris.sample_data_path('uk_hires.pp')
cubes = iris.load(fname)
iris.save(cubes, 'saved_cubes.nc')
    
We can check the ncdump to see what Iris saved:
In [ ]:
    
!ncdump -h saved_cubes.nc | head -n 20
!rm saved_cubes.nc
    
Extra keywords can be passed to specific fileformat savers.
Take a look at the above link to the `iris.save` documentation, to see which fileformats iris can save to.
In [ ]:
    
# space for user code ...
    
In [ ]:
    
# SAMPLE SOLUTION
# %load solutions/iris_exercise_2.3a
    
2. Go to the Iris reference documentation for iris.save. What keywords are accepted to iris.save when saving a PP file?
In [ ]:
    
# space for user code ...
    
In [ ]:
    
# SAMPLE SOLUTION
# %load solutions/iris_exercise_2.3b