def __setattr__(self, name, value): """ Set workspace variable. This will lookup the workspace variable name and try to set it to value. Args: name(str): Name of the attribute (variable) value(obj): The value to set the workspace variable to. Raises: ValueError: If the variable is not found or if value cannot uniquely converted to a value of a workspace variable. """ try: v = self.__getattr__(name) except: self.__dict__[name] = value return None # Handle empty list or None values. if value is None or (isinstance(value, list) and not value): arts_api.set_variable_value(self.ptr, v.ws_id, v.group_id, VariableValueStruct.empty()) return None if type(value) == Agenda: arts_api.set_variable_value(self.ptr, v.ws_id, v.group_id, VariableValueStruct(value)) return None t = self.add_variable(value) if not t.group_id == v.group_id: raise Exception("Incompatible groups: Workspace variable " + name + " of group " + group_names[v.group_id] + " and value " + str(value) + " of group " + group_names[t.group_id] + ".") self.Copy(v, t) # Remove t only if it wasn't an existing WSV already before. if not type(value) == WorkspaceVariable: t.erase()
def add_variable(self, var): """ This will try to copy a given python variable to the ARTS workspace and return a WorkspaceVariable object representing this newly created variable. Currently supported types are int, str, [str], [int], and numpy.ndarrays, which will automatically converted to the corresponding values ARTS groups. The user should not have to call this method explicitly, but instead it is used by the WorkspaceMethod call function to transfer python variable arguments to the ARTS workspace. Args: var: Python variable of type int, str, [str], [int] or np.ndarray which should be copied to the workspace. """ if type(var) == WorkspaceVariable: return var group_id = WorkspaceVariable.get_group_id(var) s = VariableValueStruct(var) ws_id = arts_api.add_variable(self.ptr, group_id, None) arts_api.set_variable_value(self.ptr, ws_id, group_id, s) return WorkspaceVariable(ws_id, str(id(var)), group_names[group_id], "User defined variable.", self)
def add_variable(self, var): """ This will try to copy a given python variable to the ARTS workspace and return a WorkspaceVariable object representing this newly created variable. Types are natively supported by the C API are int, str, [str], [int], and numpy.ndarrays. These will be copied directly into the newly created WSV. In addition to that all typhon ARTS types the can be stored to XML can be set to a WSV, but in this case the communication will happen through the file systs (cf. WorkspaceVariable.from_typhon). The user should not have to call this method explicitly, but instead it is used by the WorkspaceMethod call function to transfer python variable arguments to the ARTS workspace. Args: var: Python variable of type int, str, [str], [int] or np.ndarray which should be copied to the workspace. """ if type(var) == WorkspaceVariable: return var # Create WSV in ARTS Workspace group = group_names[WorkspaceVariable.get_group_id(var)] wsv = self.create_variable(group, None) # Set WSV value using the ARTS C API s = VariableValueStruct(var) if s.ptr: e = arts_api.set_variable_value(self.ptr, wsv.ws_id, wsv.group_id, s) if e: arts_api.erase_variable(self.ptr, wsv.ws_id, wsv.group_id) raise Exception("Setting of workspace variable through C API " " failed with the " + "following error:\n" + e.decode("utf8")) # If the type is not supported by the C API try to write the type to XML # and read into ARTS workspace. else: try: wsv.from_typhon(var) except: raise Exception("Could not add variable since + " + str(type(var)) + " is neither supported by " + "the C API nor typhon XML IO.") self._vars[wsv.name] = wsv return wsv