Пример #1
0
    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
Пример #2
0
    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}
Пример #3
0
    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.

        """
        from pyarts.types import classes as arts_classes
        from pyarts import classes as native_classes

        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 in arts_classes:
            cls = arts_classes[self.group]
            if hasattr(cls, "__from_variable_value_struct__"):
                return cls.__from_variable_value_struct__(v)
        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:
            if self.group in arts_classes:
                try:
                    return self.to_arts()
                except:
                    raise Exception(
                        "Type of workspace variable is not supported " +
                        " by the interface.")
            else:
                return native_classes.from_workspace(self)