cf.Field.creation_commands

Field.creation_commands(representative_data=False, namespace=None, indent=0, string=True, name='field', data_name='data', header=True)[source]

Return the commands that would create the field construct.

Construct keys

The key parameter of the output set_construct commands is utilised in order minimise the number of commands needed to implement cross-referencing between constructs (e.g. between a coordinate reference construct and coordinate constructs). This is usually not necessary when building field constructs, as by default the set_construct method returns a unique construct key for the construct being set.

New in version (cfdm): 1.8.7.0

Parameters
representative_data: bool, optional

Return one-line representations of Data instances, which are not executable code but prevent the data being converted in its entirety to a string representation.

namespace: str, optional

The name space containing classes of the cf package. This is prefixed to the class name in commands that instantiate instances of cf objects. By default, or if None, the name space is assumed to be consistent with cf being imported as import cf.

Parameter example:

If cf was imported as import cf as xyz then set namespace='xyz'

Parameter example:

If cf was imported as from cf import * then set namespace=''

indent: int, optional

Indent each line by this many spaces. By default no indentation is applied. Ignored if string is False.

string: bool, optional

If False then return each command as an element of a list. By default the commands are concatenated into a string, with a new line inserted between each command.

name: str, optional

The name of the Field instance created by the returned commands.

Parameter example:

name='var1'

data_name: str, optional

The name of the construct’s Data instance created by the returned commands.

Parameter example:

name='data1'

header: bool, optional

If False then do not output a comment describing the components.

Returns
str or list

The commands in a string, with a new line inserted between each command. If string is False then the separate commands are returned as each element of a list.

Examples:

>>> q = cf.example_field(0)
>>> print(q)
Field: specific_humidity (ncvar%q)
----------------------------------
Data            : specific_humidity(latitude(5), longitude(8)) 1
Cell methods    : area: mean
Dimension coords: latitude(5) = [-75.0, ..., 75.0] degrees_north
                : longitude(8) = [22.5, ..., 337.5] degrees_east
                : time(1) = [2019-01-01 00:00:00]
>>> print(q.creation_commands())
#
# field: specific_humidity
field = cf.Field()
field.set_properties({'Conventions': 'CF-1.8', 'project': 'research', 'standard_name': 'specific_humidity', 'units': '1'})
field.nc_set_variable('q')
data = cf.Data([[0.007, 0.034, 0.003, 0.014, 0.018, 0.037, 0.024, 0.029], [0.023, 0.036, 0.045, 0.062, 0.046, 0.073, 0.006, 0.066], [0.11, 0.131, 0.124, 0.146, 0.087, 0.103, 0.057, 0.011], [0.029, 0.059, 0.039, 0.07, 0.058, 0.072, 0.009, 0.017], [0.006, 0.036, 0.019, 0.035, 0.018, 0.037, 0.034, 0.013]], units='1', dtype='f8')
field.set_data(data)
#
# domain_axis: ncdim%lat
c = cf.DomainAxis()
c.set_size(5)
c.nc_set_dimension('lat')
field.set_construct(c, key='domainaxis0', copy=False)
#
# domain_axis: ncdim%lon
c = cf.DomainAxis()
c.set_size(8)
c.nc_set_dimension('lon')
field.set_construct(c, key='domainaxis1', copy=False)
#
# domain_axis:
c = cf.DomainAxis()
c.set_size(1)
field.set_construct(c, key='domainaxis2', copy=False)
#
# dimension_coordinate: latitude
c = cf.DimensionCoordinate()
c.set_properties({'units': 'degrees_north', 'standard_name': 'latitude'})
c.nc_set_variable('lat')
data = cf.Data([-75.0, -45.0, 0.0, 45.0, 75.0], units='degrees_north', dtype='f8')
c.set_data(data)
b = cf.Bounds()
b.nc_set_variable('lat_bnds')
data = cf.Data([[-90.0, -60.0], [-60.0, -30.0], [-30.0, 30.0], [30.0, 60.0], [60.0, 90.0]], units='degrees_north', dtype='f8')
b.set_data(data)
c.set_bounds(b)
field.set_construct(c, axes=('domainaxis0',), key='dimensioncoordinate0', copy=False)
#
# dimension_coordinate: longitude
c = cf.DimensionCoordinate()
c.set_properties({'units': 'degrees_east', 'standard_name': 'longitude'})
c.nc_set_variable('lon')
data = cf.Data([22.5, 67.5, 112.5, 157.5, 202.5, 247.5, 292.5, 337.5], units='degrees_east', dtype='f8')
c.set_data(data)
b = cf.Bounds()
b.nc_set_variable('lon_bnds')
data = cf.Data([[0.0, 45.0], [45.0, 90.0], [90.0, 135.0], [135.0, 180.0], [180.0, 225.0], [225.0, 270.0], [270.0, 315.0], [315.0, 360.0]], units='degrees_east', dtype='f8')
b.set_data(data)
c.set_bounds(b)
field.set_construct(c, axes=('domainaxis1',), key='dimensioncoordinate1', copy=False)
#
# dimension_coordinate: time
c = cf.DimensionCoordinate()
c.set_properties({'units': 'days since 2018-12-01', 'standard_name': 'time'})
c.nc_set_variable('time')
data = cf.Data([31.0], units='days since 2018-12-01', dtype='f8')
c.set_data(data)
field.set_construct(c, axes=('domainaxis2',), key='dimensioncoordinate2', copy=False)
#
# cell_method: mean
c = cf.CellMethod()
c.set_method('mean')
c.set_axes(('area',))
field.set_construct(c)
#
# field data axes
field.set_data_axes(('domainaxis0', 'domainaxis1'))
>>> print(q.creation_commands(representative_data=True, namespace='',
...                           indent=4, header=False))
    field = Field()
    field.set_properties({'Conventions': 'CF-1.8', 'project': 'research', 'standard_name': 'specific_humidity', 'units': '1'})
    field.nc_set_variable('q')
    data = <Data(5, 8): [[0.007, ..., 0.013]] 1>  # Representative data
    field.set_data(data)
    c = DomainAxis()
    c.set_size(5)
    c.nc_set_dimension('lat')
    field.set_construct(c, key='domainaxis0', copy=False)
    c = DomainAxis()
    c.set_size(8)
    c.nc_set_dimension('lon')
    field.set_construct(c, key='domainaxis1', copy=False)
    c = DomainAxis()
    c.set_size(1)
    field.set_construct(c, key='domainaxis2', copy=False)
    c = DimensionCoordinate()
    c.set_properties({'units': 'degrees_north', 'standard_name': 'latitude'})
    c.nc_set_variable('lat')
    data = <Data(5): [-75.0, ..., 75.0] degrees_north>  # Representative data
    c.set_data(data)
    b = Bounds()
    b.nc_set_variable('lat_bnds')
    data = <Data(5, 2): [[-90.0, ..., 90.0]] degrees_north>  # Representative data
    b.set_data(data)
    c.set_bounds(b)
    field.set_construct(c, axes=('domainaxis0',), key='dimensioncoordinate0', copy=False)
    c = DimensionCoordinate()
    c.set_properties({'units': 'degrees_east', 'standard_name': 'longitude'})
    c.nc_set_variable('lon')
    data = <Data(8): [22.5, ..., 337.5] degrees_east>  # Representative data
    c.set_data(data)
    b = Bounds()
    b.nc_set_variable('lon_bnds')
    data = <Data(8, 2): [[0.0, ..., 360.0]] degrees_east>  # Representative data
    b.set_data(data)
    c.set_bounds(b)
    field.set_construct(c, axes=('domainaxis1',), key='dimensioncoordinate1', copy=False)
    c = DimensionCoordinate()
    c.set_properties({'units': 'days since 2018-12-01', 'standard_name': 'time'})
    c.nc_set_variable('time')
    data = <Data(1): [2019-01-01 00:00:00]>  # Representative data
    c.set_data(data)
    field.set_construct(c, axes=('domainaxis2',), key='dimensioncoordinate2', copy=False)
    c = CellMethod()
    c.set_method('mean')
    c.set_axes(('area',))
    field.set_construct(c)
    field.set_data_axes(('domainaxis0', 'domainaxis1'))