1 # Copyright (C) 2012-2022 CEA/DEN, EDF R&D
3 # This library is free software; you can redistribute it and/or
4 # modify it under the terms of the GNU Lesser General Public
5 # License as published by the Free Software Foundation; either
6 # version 2.1 of the License, or (at your option) any later version.
8 # This library is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 # Lesser General Public License for more details.
13 # You should have received a copy of the GNU Lesser General Public
14 # License along with this library; if not, write to the Free Software
15 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
23 dataManager = medcalc.medcorba.factory.getDataManager()
26 # the pyConsoleGlobals variable should hold the globals() dictionary of
27 # the python console context
28 pyConsoleGlobals = None
31 # This function is to be called from the working python console to
32 # specify the globals() dictionary (used in fieldtools for stat analysis)
34 # >>> medcalc.setConsoleGlobals(globals())
36 def setConsoleGlobals(pyconsole_globals):
37 global pyConsoleGlobals
38 pyConsoleGlobals = pyconsole_globals
42 def saveWorkspace(filename):
44 Dump your job in a med file. Only the fields marked as persistent
45 are saved in the specified file.
48 dataManager.savePersistentFields(filename)
49 except SALOME.SALOME_Exception as ex:
50 medcalc.err(ex.details.text)
54 from .medevents import notifyGui_cleanWorkspace
56 dvars = pyConsoleGlobals
60 for varkey, var in list(dvars.items()):
61 if isinstance(var, medcalc.FieldProxy):
62 all_keys.append("%s"%varkey)
64 exec("del "+",".join(all_keys), pyConsoleGlobals)
65 notifyGui_cleanWorkspace()
68 # Remove variable from console
69 from .medevents import notifyGui_removeFromWorkspace
70 def removeFromWorkspace(fieldProxy):
71 dvars = pyConsoleGlobals
74 for varkey, var in list(dvars.items()):
75 if isinstance(var, medcalc.FieldProxy) and var.id == fieldProxy.id:
76 exec(("del %s"%varkey), pyConsoleGlobals)
77 notifyGui_removeFromWorkspace(fieldProxy.id)
80 # Get list a field with information using the commands ls and/or la
81 def getEnvironment(local=True, remote=False):
83 This function return the status of the medcalc context, i.e. the
84 list of fields defined in this python session.
88 dvars = pyConsoleGlobals
90 medcalc.wrn("The stat function required the specification of the python context")
91 medcalc.inf("Type this command \"import medcalc; medcalc.setConsoleGlobals(globals())")
93 status="========= Fields used in the current context ===\n"
94 for varkey in list(dvars.keys()):
96 if isinstance(var, medcalc.FieldProxy):
97 status+="%s \t(id=%s, name=%s)\n"%(varkey,var.id,var.fieldname)
101 status+="\n========= Fields available in the data manager ===\n"
102 fieldHandlerList = dataManager.getFieldHandlerList()
103 for fieldHandler in fieldHandlerList:
104 status+="id=%s\tname\t= %s\n\tmesh\t= %s\n\t(it,dt)\t= (%s,%s)\n\tsource\t= %s\n"%(
106 fieldHandler.fieldname,
107 fieldHandler.meshname,
108 fieldHandler.iteration,
111 status+="---------\n"
113 if len(fieldHandlerList) > 0:
114 status+="(use 'f=accessField(id)' to get a field in the current context)"
119 # For simpler typing, one can create a python command for status
120 # (avoid to type "print(getEnvironment())")
121 class ListFields(object):
123 A stat object displays the status of the med operating context, i.e. the
124 list of fields defined in this python session.
126 def __init__(self, all=False):
129 # all = True means that the local metadata (fieldproxy) and the
130 # remote metadata on the engine (MEDCouplingFieldDouble) are
131 # displayed by the stat command. Otherwise, only the local
132 # metadata are displayed.
135 return getEnvironment(self.__local, self.__remote)
138 # Creating the commands list (ls) and list all (la)
139 ls = ListFields(all=False)
140 la = ListFields(all=True)
143 # Add variable to workspace
144 from .medevents import notifyGui_putInWorkspace
145 def putInWorkspace(fieldProxy):
147 This function puts a reference to this field in the GUI data
148 model. When a field is referenced in the GUI data model, then it
149 belongs to the workspace. When the workspace is saved, all the
150 field that belongs to the workspace are saved.
152 dataManager.markAsPersistent(fieldProxy.id, True)
153 notifyGui_putInWorkspace(fieldProxy.id)
156 def accessField(fieldHandlerId):
158 This return a field proxy on the field identified by the specified
161 return medcalc.newFieldProxy(fieldHandlerId)
166 Display the field at its timestep
168 medcalc.MakeScalarMap(field, viewMode=MEDCALC.VIEW_MODE_REPLACE, scalarBarRange=MEDCALC.SCALAR_BAR_CURRENT_TIMESTEP)