Skip to content

Commit 3c54287

Browse files
committed
Allow a halo to be added by indices and subspace
1 parent 4dbbfdf commit 3c54287

File tree

13 files changed

+649
-1084
lines changed

13 files changed

+649
-1084
lines changed

cf/docstring/docstring.py

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,14 @@
8080
Whether `esmpy` logging is enabled or not is determined by
8181
`cf.regrid_logging`. If it is enabled then logging takes place
8282
after every call. By default logging is disabled.""",
83+
# subspace halos
84+
"{{subspace halos}}": """
85+
If a halo is defined via a positional argument, then each
86+
subspaced axis will be extended to include that many extra
87+
elements at each "side" of the axis. The number of extra
88+
elements will be automatically reduced if including the full
89+
amount defined by the halo would extend the subspace beyond
90+
the axis limits.""",
8391
# ----------------------------------------------------------------
8492
# Method description substitutions (3 levels of indentation)
8593
# ----------------------------------------------------------------
@@ -613,6 +621,40 @@
613621
"{{to_size: `int`, optional}}": """to_size: `int`, optional
614622
Pad the axis after so that the new axis has the given
615623
size.""",
624+
# subspace mode options
625+
"{{mode: optional}}": """mode: optional
626+
Specify the mode of operation (``mode``) and a halo to
627+
be added to the subspaced axes (``halo``) with
628+
positional arguments in format ``mode``, or ``halo``,
629+
or ``mode, halo``, or with no positional arguments at
630+
all.
631+
632+
A mode of operation is given as a `str`, and a halo as
633+
a non-negative `int` (or any object that can be
634+
converted to one):
635+
636+
============== ======================================
637+
*mode* Description
638+
============== ======================================
639+
```` If no positional arguments are
640+
provided then assume the
641+
``'compress'`` mode of operation with
642+
no halo added to the subspaced
643+
axes.
644+
645+
``mode`` Define the mode of operation with no
646+
halo added to the subspaced axes.
647+
648+
``mode, halo`` Define a mode of operation, as well as
649+
a halo to be added to the subspaced
650+
axes.
651+
652+
``halo`` Assume the ``'compress'`` mode of
653+
operation and define a halo to be
654+
added to the subspaced axes. Note that
655+
``halo`` is equivalent to
656+
``'compress', halo``.
657+
============== ======================================""",
616658
# ----------------------------------------------------------------
617659
# Method description substitutions (4 levels of indentation)
618660
# ----------------------------------------------------------------
@@ -663,4 +705,42 @@
663705
The removed CFA-netCDF file name substitution. If the
664706
substitution was not defined then an empty dictionary
665707
is returned.""",
708+
# subspace valid modes Field
709+
"{{subspace valid modes Field}}": """Valid modes are:
710+
711+
* ``'compress'`` This the default.
712+
Unselected locations are removed to create the
713+
subspace. If the result is not hyperrectangular
714+
then the minimum amount of unselected locations
715+
required to make it so will also be specially
716+
selected. Missing data is inserted at the
717+
specially selected locations, unless a halo has
718+
been defined (of any size, including 0).
719+
720+
* ``'envelope'``
721+
The subspace is the smallest hyperrectangular
722+
subspace that contains all of the selected
723+
locations. Missing data is inserted at unselected
724+
locations within the envelope, unless a halo has
725+
been defined (of any size, including 0).
726+
727+
* ``'full'``
728+
The subspace has the same domain as the original
729+
construct. Missing data is inserted at unselected
730+
locations, unless a halo has been defined (of any
731+
size, including 0).""",
732+
# subspace valid modes Domain
733+
"{{subspace valid modes Domain}}": """Valid modes are:
734+
735+
* ``'compress'`` This the default.
736+
Unselected locations are removed to create the
737+
subspace. If the result is not hyperrectangular
738+
then the minimum amount of unselected locations
739+
required to make it so will also be specially
740+
selected.
741+
742+
* ``'envelope'``
743+
The subspace is the smallest hyperrectangular
744+
subspace that contains all of the selected
745+
locations.""",
666746
}

cf/domain.py

Lines changed: 31 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -743,7 +743,7 @@ def indices(self, *mode, **kwargs):
743743
"""Create indices that define a subspace of the domain
744744
construct.
745745
746-
The indices returned by this method be used to create the
746+
The indices returned by this method may be used to create the
747747
subspace by passing them to the `subspace` method of the
748748
original domain construct.
749749
@@ -778,34 +778,22 @@ def indices(self, *mode, **kwargs):
778778
may still need to be inserted into the field construct's
779779
data.
780780
781+
**Halos**
782+
783+
{{subspace halos}}
784+
781785
.. versionadded:: 3.11.0
782786
783787
.. seealso:: `subspace`, `where`, `__getitem__`,
784788
`__setitem__`, `cf.Field.indices`
785789
786790
:Parameters:
787791
788-
mode: `str`, *optional*
789-
There are two modes of operation, each of which provides
790-
indices for a different type of subspace:
791-
792-
============== ======================================
793-
*mode* Description
794-
============== ======================================
795-
``'compress'`` This is the default mode. Unselected
796-
locations are removed to create the
797-
returned subspace. Note that if a
798-
multi-dimensional metadata construct
799-
is being used to define the indices
800-
then some missing data may still be
801-
inserted at unselected locations.
802-
803-
``'envelope'`` The returned subspace is the smallest
804-
that contains all of the selected
805-
indices.
806-
============== ======================================
807-
808-
kwargs: *optional*
792+
{{mode: optional}}
793+
794+
{{subspace valid modes Domain}}
795+
796+
kwargs: optional
809797
A keyword name is an identity of a metadata construct,
810798
and the keyword value provides a condition for
811799
inferring indices that apply to the dimension (or
@@ -857,19 +845,6 @@ def indices(self, *mode, **kwargs):
857845
: time(1) = [2019-01-01 00:00:00]
858846
859847
"""
860-
if len(mode) > 1:
861-
raise ValueError(
862-
"Can't provide more than one positional argument. "
863-
f"Got: {', '.join(repr(x) for x in mode)}"
864-
)
865-
866-
if not mode or "compress" in mode:
867-
mode = "compress"
868-
elif "envelope" in mode:
869-
mode = "envelope"
870-
else:
871-
raise ValueError(f"Invalid value for 'mode' argument: {mode[0]!r}")
872-
873848
# Get the indices for every domain axis in the domain, without
874849
# any auxiliary masks.
875850
domain_indices = self._indices(mode, None, False, kwargs)
@@ -1120,19 +1095,19 @@ def roll(self, axis, shift, inplace=False):
11201095
return d
11211096

11221097
def subspace(self, *mode, **kwargs):
1123-
"""Create indices that define a subspace of the domain
1124-
construct.
1098+
"""Create a subspace of the field construct.
11251099
1126-
The indices returned by this method be used to create the subspace
1127-
by passing them to the `subspace` method of the original domain
1128-
construct.
1100+
Creation of a new domain construct which spans a subspace of
1101+
the domain of an existing domain construct is achieved by
1102+
identifying indices based on the metadata constructs
1103+
(subspacing by metadata). The new domain construct is created
1104+
with the same properties as the original domain construct.
11291105
1130-
The subspace is defined by identifying indices based on the
1131-
metadata constructs.
1106+
**Subspacing by metadata**
11321107
1133-
Metadata constructs are selected conditions are specified on their
1134-
data. Indices for subspacing are then automatically inferred from
1135-
where the conditions are met.
1108+
Subspacing by metadata selects metadata constructs and
1109+
specifies conditions on their data. Indices for subspacing are
1110+
then automatically inferred from where the conditions are met.
11361111
11371112
Metadata constructs and the conditions on their data are defined
11381113
by keyword parameters.
@@ -1142,6 +1117,9 @@ def subspace(self, *mode, **kwargs):
11421117
* Multiple domain axes may be subspaced simultaneously, and it
11431118
doesn't matter which order they are specified in.
11441119
1120+
* Subspace criteria may be provided for size 1 domain axes that
1121+
are not spanned by the field construct's data.
1122+
11451123
* Explicit indices may also be assigned to a domain axis
11461124
identified by a metadata construct, with either a Python `slice`
11471125
object, or a sequence of integers or booleans.
@@ -1156,41 +1134,21 @@ def subspace(self, *mode, **kwargs):
11561134
acting along orthogonal dimensions, some missing data may still
11571135
need to be inserted into the field construct's data.
11581136
1159-
.. versionadded:: 3.11.0
1160-
1161-
.. seealso:: `indices`
1162-
1163-
:Parameters:
1137+
**Halos**
11641138
1165-
mode: `str`, *optional*
1166-
There are two modes of operation, each of which provides
1167-
indices for a different type of subspace:
1139+
{{subspace halos}}
11681140
1169-
============== ==========================================
1170-
*mode* Description
1171-
============== ==========================================
1172-
``'compress'`` Return indices that identify only the
1173-
requested locations.
1141+
.. versionadded:: 3.11.0
11741142
1175-
This is the default mode.
1143+
.. seealso:: `indices`, `cf.Field.subspace`
11761144
1177-
Note that if a multi-dimensional metadata
1178-
construct is being used to define the
1179-
indices then some unrequested locations
1180-
may also be selected.
1145+
:Parameters:
11811146
1182-
``'envelope'`` The returned subspace is the smallest that
1183-
contains all of the requested locations.
1147+
{{mode: optional}}
11841148
1185-
``'test'`` May be used on its own or in addition to
1186-
one of the other positional arguments. Do
1187-
not create a subspace, but return `True`
1188-
or `False` depending on whether or not it
1189-
is possible to create the specified
1190-
subspace.
1191-
============== ==========================================
1149+
{{subspace valid modes Domain}}
11921150
1193-
kwargs: *optional*
1151+
kwargs: optional
11941152
A keyword name is an identity of a metadata construct, and
11951153
the keyword value provides a condition for inferring
11961154
indices that apply to the dimension (or dimensions)

0 commit comments

Comments
 (0)