|
| 1 | +import cfdm |
| 2 | + |
| 3 | +from . import mixin |
| 4 | +from .decorators import _deprecated_kwarg_check |
| 5 | + |
| 6 | + |
| 7 | +class CellConnectivity(mixin.PropertiesData, cfdm.CellConnectivity): |
| 8 | + """A cell connectivity construct of the CF data model. |
| 9 | +
|
| 10 | + TODOUGRID |
| 11 | +
|
| 12 | + A cell measure construct provides information that is needed about |
| 13 | + the size or shape of the cells and that depends on a subset of the |
| 14 | + domain axis constructs. Cell measure constructs have to be used |
| 15 | + when the size or shape of the cells cannot be deduced from the |
| 16 | + dimension or auxiliary coordinate constructs without special |
| 17 | + knowledge that a generic application cannot be expected to have. |
| 18 | +
|
| 19 | + The connectivity measure construct consists of a numeric array of the |
| 20 | + metric data which spans a subset of the domain axis constructs, |
| 21 | + and properties to describe the data. The connectivity measure construct |
| 22 | + specifies a "measure" to indicate which metric of the space it |
| 23 | + supplies, e.g. connectivity horizontal areas, and must have a units |
| 24 | + property consistent with the measure, e.g. square metres. It is |
| 25 | + assumed that the metric does not depend on axes of the domain |
| 26 | + which are not spanned by the array, along which the values are |
| 27 | + implicitly propagated. CF-netCDF connectivity measure variables correspond |
| 28 | + to connectivity measure constructs. |
| 29 | +
|
| 30 | + **NetCDF interface** |
| 31 | +
|
| 32 | + {{netCDF variable}} |
| 33 | +
|
| 34 | + .. versionadded:: TODOUGRIDVER |
| 35 | +
|
| 36 | + """ |
| 37 | + |
| 38 | + def __repr__(self): |
| 39 | + """Called by the `repr` built-in function. |
| 40 | +
|
| 41 | + x.__repr__() <==> repr(x) |
| 42 | +
|
| 43 | + """ |
| 44 | + return super().__repr__().replace("<", "<CF ", 1) |
| 45 | + |
| 46 | + @property |
| 47 | + def connectivity(self): |
| 48 | + """TODOUGRID Measure which indicates the metric of space supplied. |
| 49 | +
|
| 50 | + .. versionadded:: TODOUGRIDVER |
| 51 | +
|
| 52 | + """ |
| 53 | + return self.get_connectivity(default=AttributeError()) |
| 54 | + |
| 55 | + @connectivity.setter |
| 56 | + def connectivity(self, value): |
| 57 | + self.set_connectivity(value) |
| 58 | + |
| 59 | + @connectivity.deleter |
| 60 | + def connectivity(self): |
| 61 | + self.del_connectivity(default=AttributeError()) |
| 62 | + |
| 63 | + def identity( |
| 64 | + self, |
| 65 | + default="", |
| 66 | + strict=None, |
| 67 | + relaxed=False, |
| 68 | + nc_only=False, |
| 69 | + relaxed_identity=None, |
| 70 | + ): |
| 71 | + """Return the canonical identity. |
| 72 | +
|
| 73 | + By default the identity is the first found of the following: |
| 74 | +
|
| 75 | + * The connectivity type type, preceded by ``'connectivity:'``. |
| 76 | + * The `standard_name` property. |
| 77 | + * The `id` attribute, preceded by ``'id%'``. |
| 78 | + * The `long_name` property, preceded by ``'long_name='``. |
| 79 | + * The netCDF variable name, preceded by ``'ncvar%'``. |
| 80 | + * The value of the *default* parameter. |
| 81 | +
|
| 82 | + .. versionadded:: TODOUGRIDVER |
| 83 | +
|
| 84 | + .. seealso:: `id`, `identities`, `long_name`, `connectivity`, |
| 85 | + `nc_get_variable`, `standard_name` |
| 86 | +
|
| 87 | + :Parameters: |
| 88 | +
|
| 89 | + default: optional |
| 90 | + If no identity can be found then return the value of the |
| 91 | + default parameter. |
| 92 | +
|
| 93 | + strict: `bool`, optional |
| 94 | + If True then the identity is the first found of only |
| 95 | + the `connectivity` attribute, "standard_name" property |
| 96 | + or the "id" attribute. |
| 97 | +
|
| 98 | + relaxed: `bool`, optional |
| 99 | + If True then the identity is the first found of only |
| 100 | + the `connectivity `attribute, the "standard_name" |
| 101 | + property, the "id" attribute, the "long_name" property |
| 102 | + or the netCDF variable name. |
| 103 | +
|
| 104 | + nc_only: `bool`, optional |
| 105 | + If True then only take the identity from the netCDF |
| 106 | + variable name. |
| 107 | +
|
| 108 | + :Returns: |
| 109 | +
|
| 110 | + The identity. |
| 111 | +
|
| 112 | + **Examples** |
| 113 | +
|
| 114 | + TODOUGRID |
| 115 | +
|
| 116 | + >>> c.measure |
| 117 | + 'area' |
| 118 | + >>> c.properties() |
| 119 | + {'long_name': 'connectivity_area', |
| 120 | + 'foo': 'bar'} |
| 121 | + >>> c.nc_get_variable() |
| 122 | + 'areaconnectivityo' |
| 123 | + >>> c.identity() |
| 124 | + 'measure:area' |
| 125 | + >>> del c.measure |
| 126 | + >>> c.identity() |
| 127 | + 'long_name=connectivity_area' |
| 128 | + >>> del c.long_name |
| 129 | + >>> c.identity() |
| 130 | + 'ncvar%areaconnectivityo' |
| 131 | + >>> c.nc_del_variable() |
| 132 | + 'areaconnectivityo' |
| 133 | + >>> c.identity() |
| 134 | + '' |
| 135 | + >>> c.identity('no identity') |
| 136 | + 'no identity' |
| 137 | +
|
| 138 | + """ |
| 139 | + if nc_only: |
| 140 | + if strict: |
| 141 | + raise ValueError( |
| 142 | + "'strict' and 'nc_only' parameters cannot both be True") |
| 143 | + |
| 144 | + if relaxed: |
| 145 | + raise ValueError( |
| 146 | + "'relaxed' and 'nc_only' parameters cannot both be True") |
| 147 | + |
| 148 | + n = self.nc_get_variable(None) |
| 149 | + if n is not None: |
| 150 | + return f"ncvar%{n}" |
| 151 | + |
| 152 | + return default |
| 153 | + |
| 154 | + n = self.get_connectivity(default=None) |
| 155 | + if n is not None: |
| 156 | + return f"connectivity:{n}" |
| 157 | + |
| 158 | + n = self.get_property("standard_name", None) |
| 159 | + if n is not None: |
| 160 | + return f"{n}" |
| 161 | + |
| 162 | + n = getattr(self, "id", None) |
| 163 | + if n is not None: |
| 164 | + return f"id%{n}" |
| 165 | + |
| 166 | + if relaxed: |
| 167 | + n = self.get_property("long_name", None) |
| 168 | + if n is not None: |
| 169 | + return f"long_name={n}" |
| 170 | + |
| 171 | + n = self.nc_get_variable(None) |
| 172 | + if n is not None: |
| 173 | + return f"ncvar%{n}" |
| 174 | + |
| 175 | + return default |
| 176 | + |
| 177 | + if strict: |
| 178 | + return default |
| 179 | + |
| 180 | + n = self.get_property("long_name", None) |
| 181 | + if n is not None: |
| 182 | + return f"long_name={n}" |
| 183 | + |
| 184 | + n = self.nc_get_variable(None) |
| 185 | + if n is not None: |
| 186 | + return f"ncvar%{n}" |
| 187 | + |
| 188 | + return default |
0 commit comments