36 '''Module containing the FieldArgMetadata class which captures the metadata
37 associated with a field argument. Supports the creation, modification
38 and Fortran output of a Field argument.
47 '''Class to capture LFRic kernel metadata information for a field
50 :param str datatype: the datatype of this field (GH_INTEGER, ...).
51 :param str access: the way the kernel accesses this field (GH_WRITE, ...).
52 :param str function_space: the function space that this field is \
54 :param Optional[str] stencil: the type of stencil used by the \
55 kernel when accessing this field.
65 datatype_arg_index = 1
67 function_space_arg_index = 3
75 def __init__(self, datatype, access, function_space, stencil=None):
76 super().__init__(datatype, access)
81 def _get_metadata(cls, fparser2_tree):
82 '''Extract the required metadata from the fparser2 tree and return it
83 as strings. Also check that the metadata is in the expected
84 form (but do not check the metadata values as that is done
87 :param fparser2_tree: fparser2 tree containing the metadata \
89 :type fparser2_tree: :py:class:`fparser.two.Fortran2003.Part_Ref` | \
90 :py:class:`fparser.two.Fortran2003.Structure_Constructor`
92 :returns: a tuple containing the datatype, access, function \
93 space and stencil metadata.
94 :rtype: Tuple[str, str, str, Optional[str]]
97 datatype, access = super()._get_metadata(fparser2_tree)
98 function_space = cls.
get_argget_arg(
100 stencil = cls.
get_stencilget_stencil(fparser2_tree)
101 return (datatype, access, function_space, stencil)
105 '''Retrieves the stencil metadata value found within the supplied
106 fparser2 tree (if there is one) and checks that it is valid.
108 :param fparser2_tree: fparser2 tree capturing the required metadata.
109 :type fparser2_tree: :py:class:`fparser.two.Fortran2003.Part_Ref`
111 :returns: the stencil value extracted from the fparser2 tree \
112 if there is one, or None if not.
113 :rtype: Optional[str]
115 :raises TypeError: if the stencil metadata is not in the \
119 raw_stencil_text = FieldArgMetadata.get_arg(
121 if not raw_stencil_text:
123 raw_stencil_text = raw_stencil_text.strip().lower()
124 if not (raw_stencil_text.startswith(
"stencil(")
and
125 raw_stencil_text.endswith(
")")
and len(raw_stencil_text) > 9):
126 raise TypeError(f
"The stencil metadata should be in the form "
127 f
"'stencil(type)' but found '{raw_stencil_text}'.")
128 stencil = raw_stencil_text[8:-1]
133 :returns: the metadata represented by this class as Fortran.
137 return (f
"arg_type({self.form}, {self.datatype}, {self.access}, "
138 f
"{self.function_space}, stencil({self.stencil}))")
139 return (f
"arg_type({self.form}, {self.datatype}, {self.access}, "
140 f
"{self.function_space})")
145 :param str value: the datatype to check for validity.
147 :raises ValueError: if the provided value is not a valid \
152 FieldArgMetadata.validate_scalar_value(
153 value, const.VALID_FIELD_DATA_TYPES,
"datatype descriptor")
158 :param str value: the access descriptor to validate.
161 FieldArgMetadata.validate_scalar_value(
162 value, const.VALID_FIELD_ACCESS_TYPES,
"access descriptor")
165 def function_space(self):
167 :returns: the function space for this field argument.
172 @function_space.setter
173 def function_space(self, value):
175 :param str value: set the function space to the \
179 FieldArgMetadata.validate_scalar_value(
180 value, const.VALID_FUNCTION_SPACE_NAMES,
"function space")
186 :returns: the stencil for this field argument, or None if there
188 :rtype: Optional[str]
194 def stencil(self, value):
196 :param str value: set the stencil to the specified value.
202 FieldArgMetadata.validate_scalar_value(
203 value, const.VALID_STENCIL_TYPES,
"stencil")
204 self.
_stencil_stencil = value.lower()
207 __all__ = [
"FieldArgMetadata"]
def get_arg(fparser2_tree, index)
int function_space_arg_index
int function_space_arg_index
def check_datatype(value)
def get_stencil(cls, fparser2_tree)
def function_space(self, value)