cf.Field.flatten

Field.flatten(axes=None, return_axis=False, inplace=False)[source]

Flatten axes of the field.

Any subset of the domain axes may be flattened.

The shape of the data may change, but the size will not.

Metadata constructs whose data spans the flattened axes will either themselves be flattened, or else removed.

Cell method constructs that apply to the flattened axes will be removed or, if possible, have their axis specifications changed to standard names.

The flattening is executed in row-major (C-style) order. For example, the array [[1, 2], [3, 4]] would be flattened across both dimensions to [1 2 3 4].

New in version 3.0.2.

Parameters
axes: (sequence of) str or int, optional

Select the domain axes to be flattened, defined by the domain axes that would be selected by passing each given axis description to a call of the field construct’s domain_axis method. For example, for a value of 'X', the domain axis construct returned by f.domain_axis('X') is selected.

If no axes are provided then all axes spanned by the field construct’s data are flattened.

No axes are flattened if axes is an empty sequence.

return_axis: bool, optional

If True then also return either the key of the flattened domain axis construct; or None if the axes to be flattened do not span the data.

inplace: bool, optional

If True then do the operation in-place and return None.

Returns
Field or None, [str or None]

The new, flattened field construct, or None if the operation was in-place.

If return_axis is True then also return either the key of the flattened domain axis construct; or None if the axes to be flattened do not span the data.

Examples

See cf.Data.flatten for more examples of how the data are flattened.

>>> f.shape
(1, 2, 3, 4)
>>> f.flatten().shape
(24,)
>>> f.flatten([]).shape
(1, 2, 3, 4)
>>> f.flatten([1, 3]).shape
(1, 8, 3)
>>> f.flatten([0, -1], inplace=True)
>>> f.shape
(4, 2, 3)
>>> print(t)
Field: air_temperature (ncvar%ta)
---------------------------------
Data            : air_temperature(atmosphere_hybrid_height_coordinate(1), grid_latitude(10), grid_longitude(9)) K
Cell methods    : grid_latitude(10): grid_longitude(9): mean where land (interval: 0.1 degrees) time(1): maximum
Field ancils    : air_temperature standard_error(grid_latitude(10), grid_longitude(9)) = [[0.76, ..., 0.32]] K
Dimension coords: atmosphere_hybrid_height_coordinate(1) = [1.5]
                : grid_latitude(10) = [2.2, ..., -1.76] degrees
                : grid_longitude(9) = [-4.7, ..., -1.18] degrees
                : time(1) = [2019-01-01 00:00:00]
Auxiliary coords: latitude(grid_latitude(10), grid_longitude(9)) = [[53.941, ..., 50.225]] degrees_N
                : longitude(grid_longitude(9), grid_latitude(10)) = [[2.004, ..., 8.156]] degrees_E
                : long_name=Grid latitude name(grid_latitude(10)) = [--, ..., kappa]
Cell measures   : measure:area(grid_longitude(9), grid_latitude(10)) = [[2391.9657, ..., 2392.6009]] km2
Coord references: grid_mapping_name:rotated_latitude_longitude
                : standard_name:atmosphere_hybrid_height_coordinate
Domain ancils   : ncvar%a(atmosphere_hybrid_height_coordinate(1)) = [10.0] m
                : ncvar%b(atmosphere_hybrid_height_coordinate(1)) = [20.0]
                : surface_altitude(grid_latitude(10), grid_longitude(9)) = [[0.0, ..., 270.0]] m
>>> print(t.flatten())
Field: air_temperature (ncvar%ta)
---------------------------------
Data            : air_temperature(key%domainaxis4(90)) K
Cell methods    : grid_latitude: grid_longitude: mean where land (interval: 0.1 degrees) time(1): maximum
Field ancils    : air_temperature standard_error(key%domainaxis4(90)) = [0.76, ..., 0.32] K
Dimension coords: time(1) = [2019-01-01 00:00:00]
Auxiliary coords: latitude(key%domainaxis4(90)) = [53.941, ..., 50.225] degrees_N
                : longitude(key%domainaxis4(90)) = [2.004, ..., 8.156] degrees_E
Cell measures   : measure:area(key%domainaxis4(90)) = [2391.9657, ..., 2392.6009] km2
Coord references: grid_mapping_name:rotated_latitude_longitude
                : standard_name:atmosphere_hybrid_height_coordinate
Domain ancils   : surface_altitude(key%domainaxis4(90)) = [0.0, ..., 270.0] m
>>> print(t.flatten(['grid_latitude', 'grid_longitude']))
Field: air_temperature (ncvar%ta)
---------------------------------
Data            : air_temperature(atmosphere_hybrid_height_coordinate(1), key%domainaxis4(90)) K
Cell methods    : grid_latitude: grid_longitude: mean where land (interval: 0.1 degrees) time(1): maximum
Field ancils    : air_temperature standard_error(key%domainaxis4(90)) = [0.76, ..., 0.32] K
Dimension coords: atmosphere_hybrid_height_coordinate(1) = [1.5]
                : time(1) = [2019-01-01 00:00:00]
Auxiliary coords: latitude(key%domainaxis4(90)) = [53.941, ..., 50.225] degrees_N
                : longitude(key%domainaxis4(90)) = [2.004, ..., 8.156] degrees_E
Cell measures   : measure:area(key%domainaxis4(90)) = [2391.9657, ..., 2392.6009] km2
Coord references: grid_mapping_name:rotated_latitude_longitude
                : standard_name:atmosphere_hybrid_height_coordinate
Domain ancils   : ncvar%a(atmosphere_hybrid_height_coordinate(1)) = [10.0] m
                : ncvar%b(atmosphere_hybrid_height_coordinate(1)) = [20.0]
                : surface_altitude(key%domainaxis4(90)) = [0.0, ..., 270.0] m
>>> t.domain_axes.keys()
>>> dict_keys(
...     ['domainaxis0', 'domainaxis1', 'domainaxis2', 'domainaxis3'])
>>> t.flatten(return_axis=True)
(<CF Field: air_temperature(key%domainaxis4(90)) K>,
 'domainaxis4')
>>> t.flatten('grid_longitude', return_axis=True)
(<CF Field: air_temperature(atmosphere_hybrid_height_coordinate(1), grid_latitude(10), grid_longitude(9)) K>,
 'domainaxis2')
>>> t.flatten('time', return_axis=True)
(<CF Field: air_temperature(atmosphere_hybrid_height_coordinate(1), grid_latitude(10), grid_longitude(9)) K>,
 None)