コード例 #1
0
ファイル: workspace.py プロジェクト: wangjianzju/typhon
    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
コード例 #2
0
ファイル: methods.py プロジェクト: jakobdoerr/typhon
    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 typhon.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
コード例 #3
0
    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_id = WorkspaceVariable.get_group_id(var)
        ws_id = arts_api.add_variable(self.ptr, group_id, None)
        wsv = WorkspaceVariable(ws_id, str(id(var)), group_names[group_id],
                                "User defined variable.", self)
        # Set WSV value using the ARTS C API
        s = VariableValueStruct(var)
        if s.ptr:
            e = arts_api.set_variable_value(self.ptr, ws_id, group_id, s)
            if e:
                arts_api.erase_variable(self.ptr, ws_id, 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.")
        return wsv
コード例 #4
0
ファイル: methods.py プロジェクト: simonpf/furry-guacamole
    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 typhon.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
        """
        if not name:
            name = "__anonymous_" + str(len(ws.vars))
        group = WorkspaceMethod.create_regexp.match(self.name).group(1)
        group_id = group_ids[group]
        ws_id = arts_api.add_variable(ws.ptr, group_id)
        wsv = WorkspaceVariable(ws_id, name, group, "User defined variable.",
                                ws)
        setattr(variables, name, wsv)
        ws.vars[name] = wsv
        return wsv
コード例 #5
0
    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)