def __getattr__(self, name): """ Lookup the given variable in the local variables and the ARTS workspace. Args: name(str): Name of the attribute (variable) Raises: ValueError: If the variable is not found. """ group_id = None if name in self._vars: var = self._vars[name] var.update() return var else: i = arts_api.lookup_workspace_variable(name.encode()) if i < 0: raise AttributeError("No workspace variable " + str(name) + " found.") vs = arts_api.get_variable(i) group_id = vs.group description = vs.description.decode("utf8") # Get its symbolic representation wsv = WorkspaceVariable(i, name, group_names[group_id], description, self) return wsv
def iter(): """ Iterator returning a WorkspaceVariable object for each ARTS WSV available. """ for i in range(arts_api.get_number_of_variables()): s = arts_api.get_variable(i) name = s.name.decode("utf8") description = s.description.decode("utf") group = group_names[s.group] yield WorkspaceVariable(i, name, group, description)
def get_variable_name(i): """ Lookup the name of a variable given its workspace index. Args: i(int): The index of the workspace variable. Returns: str: The name of the workspace variable. """ s = arts_api.get_variable(i) name = s.name.decode("utf8") return name
def add_method(*args, **kwargs): """ Add a workspace method call to the agenda. Parameters: ws(arts.workspace.Workspace): A (dummy) workspace object. m(arts.workspace.WorkspaceMethod): The method to add to the agenda *args: Positional arguments of the WSM call to add to the agenda. **kwargs: Key-word arguments of the WSM call to add to the agenda. """ from pyarts.workspace.variables import group_names if len(args) < 3: raise Exception("Need at least self, a workspace and the method to" " add as arguments.") self = args[0] ws = args[1] m = args[2] m_id, args_out, args_in, temps = m._parse_output_input_lists( ws, args[3:], kwargs) arg_out_ptr = c.cast((c.c_long * len(args_out))(*args_out), c.POINTER(c.c_long)) arg_in_ptr = c.cast((c.c_long * len(args_in))(*args_in), c.POINTER(c.c_long)) if not m.name[-3:] == "Set" or not m.name[:-3] in group_names: for t in temps: arts_api.agenda_insert_set(ws.ptr, self.ptr, t.ws_id, t.group_id) arts_api.agenda_add_method(c.c_void_p(self.ptr), m_id, len(args_out), arg_out_ptr, len(args_in), arg_in_ptr) else: from pyarts.workspace.variables import WorkspaceVariable name_out = WorkspaceVariable.get_variable_name(args_out[0]) name_in = WorkspaceVariable.get_variable_name(args_in[0]) wsv_out = getattr(ws, name_out) wsv_in = getattr(ws, name_in) ws.Copy(wsv_out, wsv_in) group_id = arts_api.get_variable(args_out[0]).group arts_api.agenda_insert_set(ws.ptr, self.ptr, args_out[0], group_id)
def create(self, ws, name=None): """ Call to <Group>Create WSMs are handled differently. This method simply determines the group type from the function name and then add a variable of this type to the workspace ws. A handle of this variable is then added to as attribute to the arts.workspace.variables module. Args: ws(Workspace): Workspace object to add the variable to name(str): Name of the variable to add to the workspace """ group = WorkspaceMethod.create_regexp.match(self.name).group(1) group_id = group_ids[group] if not name: name = "__anonymous_" + str(len(ws._vars)) ws_id = arts_api.add_variable(ws.ptr, group_id, name.encode()) else: # Is there a WSM with that name? if name in workspace_methods.keys(): raise Exception("A WSM with the name " + name + " already exists.") # Is there a WSV with that name? ws_id = arts_api.lookup_workspace_variable(name.encode()) # Is yes, check that it is of the same group? if not ws_id == -1: v = arts_api.get_variable(ws_id) if not v.group == group_id: raise Exception("A WSV with the name " + name + " but of goup " + group_names[v.group] + " already exists.") # Otherwise we add the variable. else: ws_id = arts_api.add_variable(ws.ptr, group_id, name.encode()) wsv = WorkspaceVariable(ws_id, name, group, "User defined variable.", ws) setattr(variables, name, wsv) ws._vars[name] = wsv return wsv
def create_variable(self, group, name): """ Create a workspace variable. Args: group: The group name of the variable to create. name: The name of the variable to create. If None, the ARTS API will assign a unique name. """ if not name is None: name = name.encode() group_id = group_ids[group] ws_id = arts_api.add_variable(self.ptr, group_id, name) v = arts_api.get_variable(ws_id) wsv = WorkspaceVariable(ws_id, v.name.decode(), group_names[group_id], "User defined variable.", self) self._vars[wsv.name] = wsv return wsv