def initialized(self): ws = self.ws if ws is None: raise ValueError("WorkspaceVariable object needs associated" " Workspace to determine value.") v = arts_api.get_variable_value(ws.ptr, self.ws_id, self.group_id) return v.initialized
def value(self): """ Return the value of the variable in a given workspace. By default this function will check the value in the workspace associated with the variable of in the workspace object provided as argument to the function call. If the variable has an associated workspace the workspace provided as argument will be ignored. Returns: The value of the workspace variable represented by an object of the corresponding python types. Raises: Exception: If the type of the workspace variable is not supported by the interface. """ if (self.ws): ws = self.ws if not ws: raise ValueError("WorkspaceVariable object need Workspace to determine value.") v = arts_api.get_variable_value(ws.ptr, self.ws_id, self.group_id) if not v.initialized: raise Exception("WorkspaceVariable " + self.name + " is uninitialized.") # TODO: Use group attribute here istead of lookup in group_names. # TODO: Move this to VariableValueStruct class if group_names[self.group_id] == "Index": return c.cast(v.ptr, c.POINTER(c.c_long))[0] if group_names[self.group_id] == "Numeric": return c.cast(v.ptr, c.POINTER(c.c_double))[0] if group_names[self.group_id] == "String": return (c.cast(v.ptr, c.c_char_p)).value.decode("utf8") if group_names[self.group_id] == "ArrayOfIndex": return [c.cast(v.ptr, c.POINTER(c.c_long))[i] for i in range(v.dimensions[0])] if group_names[self.group_id] == "Sparse": m = v.dimensions[0] n = v.dimensions[1] nnz = v.dimensions[2] data = np.ctypeslib.as_array(c.cast(v.ptr, c.POINTER(c.c_double)), (nnz,)) row_indices = np.ctypeslib.as_array(v.inner_ptr, (nnz,)) col_starts = np.ctypeslib.as_array(v.outer_ptr, (m + 1,)) return sp.sparse.csr_matrix((data, row_indices, col_starts), shape=(m,n)) if group_names[self.group_id] == "Agenda": return Agenda(v.ptr) try: self.update() a = np.asarray(self) return a except: raise Exception("Type of workspace variable is not supported by the interface.")
def update(self): """ Update data references of the object. References to vector, matrices and tensors may change and must therefore be updated dynamically to ensure they are consistent with the state of the associated workspace. This method takes care of that. """ if not self.ws==None and self.ndim: v = arts_api.get_variable_value(self.ws.ptr, self.ws_id, self.group_id) shape = [] for i in range(self.ndim): shape.append(v.dimensions[i]) self.__array_interface__ = {"shape" : tuple(shape), "typestr" : "|f8", "data" : (v.ptr, False), "version" : 3}
def value(self): """ Return the value of the variable in a given workspace. By default this function will check the value in the workspace associated with the variable of in the workspace object provided as argument to the function call. If the variable has an associated workspace the workspace provided as argument will be ignored. Returns: The value of the workspace variable represented by an object of the corresponding python types. Raises: Exception: If the type of the workspace variable is not supported by the interface. """ if (self.ws): ws = self.ws if not ws: raise ValueError("WorkspaceVariable object need Workspace to determine value.") v = arts_api.get_variable_value(ws.ptr, self.ws_id, self.group_id) if not v.initialized: raise Exception("WorkspaceVariable " + self.name + " is uninitialized.") if self.group == "Index": return c.cast(v.ptr, c.POINTER(c.c_long))[0] elif self.group == "Numeric": return c.cast(v.ptr, c.POINTER(c.c_double))[0] elif self.group == "String": return (c.cast(v.ptr, c.c_char_p)).value.decode("utf8") elif self.group == "ArrayOfIndex": return [c.cast(v.ptr, c.POINTER(c.c_long))[i] for i in range(v.dimensions[0])] elif self.group == "Sparse": m = v.dimensions[0] n = v.dimensions[1] nnz = v.dimensions[2] if nnz == 0: return sp.sparse.csr_matrix(0) else: data = np.ctypeslib.as_array(c.cast(v.ptr, c.POINTER(c.c_double)), (nnz,)) row_indices = np.ctypeslib.as_array(v.inner_ptr, (nnz,)) col_starts = np.ctypeslib.as_array(v.outer_ptr, (m + 1,)) return sp.sparse.csr_matrix((data, row_indices, col_starts), shape=(m,n)) elif self.group == "Agenda": return Agenda(v.ptr) elif self.ndim: shape = [] size = 1 for i in range(self.ndim): shape.append(v.dimensions[i]) size *= v.dimensions[i] if size > 0: self.__array_interface__ = {"shape" : tuple(shape), "typestr" : "|f8", "data" : (v.ptr, False), "version" : 3} return np.asarray(self) else: return np.zeros(shape) else: try: return self.to_typhon() except: raise Exception("Type of workspace variable is not supported " + " by the interface.")