|
4 | 4 | Merge and Concatenate |
5 | 5 | ===================== |
6 | 6 |
|
7 | | -Sometimes you need to combine multiple Iris cubes to form a single larger Iris cube. You can achieve this with :meth:`merge <iris.cube.CubeList.merge>` and :meth:`concatenate <iris.cube.CubeList.concatenate>`. |
| 7 | +We saw in the :doc:`loading_iris_cubes` section that Iris tries to load as few cubes as |
| 8 | +possible. This is done by collecting together multiple fields with a shared standard |
| 9 | +name (and other key metadata) into a single multidimensional cube. The processes that |
| 10 | +perform this behaviour in Iris are known as ``merge`` and ``concatenate``. |
8 | 11 |
|
9 | | -This chapter of the user guide describes :meth:`merge <iris.cube.CubeList.merge>` and :meth:`concatenate <iris.cube.CubeList.concatenate>`. |
10 | | -It outlines use cases for :meth:`merge <iris.cube.CubeList.merge>` and :meth:`concatenate <iris.cube.CubeList.concatenate>` and describes the differences between them. |
11 | | -It also explains why some common issues occur and how to avoid these issues occurring. |
| 12 | +This section describes the ``merge`` and ``concatenate`` processes; it explains |
| 13 | +why common issues occur when using them and gives advice on how prevent these |
| 14 | +issues from occurring. |
12 | 15 |
|
13 | | -The uses of :meth:`merge <iris.cube.CubeList.merge>` and :meth:`concatenate <iris.cube.CubeList.concatenate>` are as follows: |
| 16 | +Both ``merge`` and ``concatenate`` take multiple cubes as input and |
| 17 | +result in fewer cubes as output. The following diagram illustrates the two processes: |
14 | 18 |
|
15 | | - * **Merge:** stacks the instances of a common scalar coordinate on each of a series of input cubes. This makes a resultant cube with a new dimension coordinate created by merging these scalar coordinates. |
16 | | - * **Concatenate:** joins the instances of a common dimension coordinate from each of a series of input cubes. This makes a resultant cube with this dimension coordinate extended. |
| 19 | + ||| -> MERGE -> |__| |
| 20 | + |
| 21 | + |_| |_| |_| -> CONCATENATE -> |___| |
17 | 22 |
|
| 23 | +There is one major difference between the ``merge`` and ``concatenate`` processes. |
| 24 | +The ``merge`` process combines multiple input cubes into a |
| 25 | +single resultant cube with new dimensions created from the |
| 26 | +*scalar coordinate values* of the input cubes. |
| 27 | +The ``concatenate`` process process combines multiple input cubes into a |
| 28 | +single resultant cube with the same *number of dimensions* as the input cubes, |
| 29 | +but with the length of one or more dimensions extended by *joining together |
| 30 | +sequential dimension coordinates*. |
18 | 31 |
|
19 | | -Merge |
20 | | ------ |
21 | 32 |
|
22 | | -:meth:`Merge <iris.cube.CubeList.merge>` stacks the instances of a common scalar coordinate on each of a series of input cubes. |
23 | | -This makes a resultant cube with a new dimension coordinate created by merging these scalar coordinates. |
24 | | -This has the following effects: |
| 33 | +Let's imagine 28 individual cubes representing the |
| 34 | +temperature at a location ``(y, x)``, one cube for each day of February. We can use |
| 35 | +:meth:`merge <iris.cube.CubeList.merge>` to combine the 28 ``(y, x)`` cubes into |
| 36 | +a single ``(t, y, x)`` cube, where the length of the ``t`` dimension is 28. |
25 | 37 |
|
26 | | - * A new dimension coordinate is formed from the series of scalar coordinates from the input cubes. Its data is composed of the scalar coordinates' data values. |
27 | | - * A single resultant cube is formed from the series of input cubes. It will have one more dimension (the new dimension coordinate described above) than the input cubes. |
| 38 | +Now imagine 12 individual cubes representing daily temperature at a time and |
| 39 | +location ``(t, y, x)``, one cube for each month in the year. We can use |
| 40 | +:meth:`concatenate <iris.cube.CubeList.concatenate>` to combine the 12 |
| 41 | +``(t, y, x)`` cubes into a single ``(t, y, x)`` cube, where the length |
| 42 | +of the ``t`` dimension is now 365. |
28 | 43 |
|
29 | | -.. note:: |
30 | 44 |
|
31 | | - The data values in the series of scalar coordinates must describe a single monotonic series. |
| 45 | +Merge |
| 46 | +----- |
32 | 47 |
|
33 | | -.. note:: |
| 48 | +We've seen that the ``merge`` combines multiple input cubes into a |
| 49 | +single resultant cube with new dimensions created from the |
| 50 | +*scalar coordinate values* of the input cubes. |
| 51 | + |
| 52 | +In order to construct new coordinates for the new dimensions, the ``merge`` process requires input cubes |
| 53 | +with scalar coordinates that can be combined together into monotonic sequences. |
| 54 | +The order of the input cubes does not affect the ``merge`` process. |
| 55 | + |
| 56 | +The ``merge`` process can produce a cube that has more than one new dimension, |
| 57 | +if the scalar coordinate sequences form an orthogonal basis. |
34 | 58 |
|
35 | | - The order of the data values in the series of scalar coordinates does not affect the :meth:`merge <iris.cube.CubeList.merge>` process. |
36 | | - The data values in the series of scalar coordinates will be re-ordered into a montonically increasing series as part of the :meth:`merge <iris.cube.CubeList.merge>` process. |
37 | 59 |
|
38 | 60 | .. warning:: |
39 | 61 |
|
40 | | - The shape, metadata, attributes, coordinates, coordinates metadata, fill value and other aspects of your input cubes must be consistent across all the input cubes. |
41 | | - The :meth:`merge <iris.cube.CubeList.merge>` process will fail if they are not consistent. Such failures are covered in the common issues section towards the end of this chapter. |
| 62 | + The shape, metadata, attributes, coordinates, coordinates metadata, fill value and |
| 63 | + other aspects of the a cube must be consistent across all of the input cubes. |
42 | 64 |
|
43 | | -Let's have a look at :meth:`merge <iris.cube.CubeList.merge>` in operation. In this example we have three lateral (*x*, *y*) cubes in an :class:`cubelist <iris.cube.CubeList>`, each with a scalar *z* coordinate of differing value. |
44 | | -We can merge these cubes by stacking the scalar *z* coordinates to make a new *z* dimension coordinate:: |
| 65 | + The :meth:`merge <iris.cube.CubeList.merge>` process will fail if they are not |
| 66 | + consistent. Such failures are covered in the common issues section towards the end of this chapter. |
45 | 67 |
|
46 | | - >>> print cubelist |
| 68 | +Let's have a look at :meth:`merge <iris.cube.CubeList.merge>` in operation. In this example we have |
| 69 | +three lateral (*x*, *y*) cubes in an :class:`cubelist <iris.cube.CubeList>`, each with a scalar *z* |
| 70 | +coordinate of differing value. We can merge these cubes by stacking the scalar *z* coordinates to |
| 71 | +make a new *z* dimension coordinate:: |
| 72 | + |
| 73 | + >>> print cubes |
47 | 74 | 0: air_temperature / (kelvin) (x: 10; y: 10) |
48 | 75 | 1: air_temperature / (kelvin) (x: 10; y: 10) |
49 | 76 | 2: air_temperature / (kelvin) (x: 10; y: 10) |
50 | | - >>> print cubelist[0] |
| 77 | + >>> print cubes. |
51 | 78 | air_temperature / (kelvin) (x: 10; y: 10) |
52 | 79 | Dimension coordinates: |
53 | 80 | x x - |
54 | 81 | y - x |
55 | 82 | Scalar coordinates: |
56 | 83 | z: 1 |
57 | | - print cubelist.merge()[0] # Merge returns a cubelist. |
58 | | - air_temperature / (kelvin) (z: 3; x: 10; y: 10) |
59 | | - Dimension coordinates: |
60 | | - z x - - |
61 | | - x - x - |
62 | | - y - - x |
| 84 | + print cubes.merge() |
| 85 | + 0: air_temperature / (kelvin) (z: 3; x: 10; y: 10) |
63 | 86 |
|
64 | 87 | The following diagram illustrates how :meth:`merge <iris.cube.CubeList.merge>` works: |
65 | 88 |
|
|
0 commit comments