Salome HOME
e2f3ecba5b772d8e8149977cb117ecc8c6c2fd18
[modules/med.git] / src / MEDCalc / tui / medconsole.py
1 # Copyright (C) 2012-2024  CEA, EDF
2 #
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.
7 #
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.
12 #
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
16 #
17 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 #
19
20 import medcalc
21 import MEDCALC
22
23 dataManager = medcalc.medcorba.factory.getDataManager()
24
25 # IMPORTANT NOTE:
26 # the pyConsoleGlobals variable should hold the globals() dictionary of
27 # the python console context
28 pyConsoleGlobals = None
29
30 #-----
31 # This function is to be called from the working python console to
32 # specify the globals() dictionary (used in fieldtools for stat analysis)
33 #
34 # >>> medcalc.setConsoleGlobals(globals())
35 #
36 def setConsoleGlobals(pyconsole_globals):
37   global pyConsoleGlobals
38   pyConsoleGlobals = pyconsole_globals
39 #
40
41 import SALOME
42 def saveWorkspace(filename):
43   """
44   Dump your job in a med file. Only the fields marked as persistent
45   are saved in the specified file.
46   """
47   try:
48     dataManager.savePersistentFields(filename)
49   except SALOME.SALOME_Exception as ex:
50     medcalc.err(ex.details.text)
51 #
52
53 # Clean workspace
54 from .medevents import notifyGui_cleanWorkspace
55 def cleanWorkspace():
56   dvars = pyConsoleGlobals
57   if dvars is None:
58     return
59   all_keys = []
60   for varkey, var in list(dvars.items()):
61     if isinstance(var, medcalc.FieldProxy):
62       all_keys.append("%s"%varkey)
63   if len(all_keys) > 0:
64     exec("del "+",".join(all_keys), pyConsoleGlobals)
65   notifyGui_cleanWorkspace()
66 #
67
68 # Remove variable from console
69 from .medevents import notifyGui_removeFromWorkspace
70 def removeFromWorkspace(fieldProxy):
71   dvars = pyConsoleGlobals
72   if dvars is None:
73     return
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)
78 #
79
80 # Get list a field with information using the commands ls and/or la
81 def getEnvironment(local=True, remote=False):
82   """
83   This function return the status of the medcalc context, i.e. the
84   list of fields defined in this python session.
85   """
86   status=""
87   if local is True:
88     dvars = pyConsoleGlobals
89     if dvars is None:
90       medcalc.wrn("The stat function required the specification of the python context")
91       medcalc.inf("Type this command \"import medcalc; medcalc.setConsoleGlobals(globals())")
92     if remote is True:
93       status="========= Fields used in the current context ===\n"
94     for varkey in list(dvars.keys()):
95       var = dvars[varkey]
96       if isinstance(var, medcalc.FieldProxy):
97         status+="%s \t(id=%s, name=%s)\n"%(varkey,var.id,var.fieldname)
98
99   if remote is True:
100     if local is True:
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"%(
105         fieldHandler.id,
106         fieldHandler.fieldname,
107         fieldHandler.meshname,
108         fieldHandler.iteration,
109         fieldHandler.order,
110         fieldHandler.source)
111       status+="---------\n"
112
113     if len(fieldHandlerList) > 0:
114       status+="(use 'f=accessField(id)' to get a field in the current context)"
115
116   return status
117 #
118
119 # For simpler typing, one can create a python command for status
120 # (avoid to type "print(getEnvironment())")
121 class ListFields(object):
122   """
123   A stat object displays the status of the med operating context, i.e. the
124   list of fields defined in this python session.
125   """
126   def __init__(self, all=False):
127     self.__local  = True
128     self.__remote = all
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.
133   #
134   def __repr__(self):
135     return getEnvironment(self.__local, self.__remote)
136   #
137 #
138 # Creating the commands list (ls) and list all (la)
139 ls = ListFields(all=False)
140 la = ListFields(all=True)
141
142 #
143 # Add variable to workspace
144 from .medevents import notifyGui_putInWorkspace
145 def putInWorkspace(fieldProxy):
146   """
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.
151   """
152   dataManager.markAsPersistent(fieldProxy.id, True)
153   notifyGui_putInWorkspace(fieldProxy.id)
154 #
155
156 def accessField(fieldHandlerId):
157   """
158   This return a field proxy on the field identified by the specified
159   field handler id.
160   """
161   return medcalc.newFieldProxy(fieldHandlerId)
162 #
163
164 def view(field):
165   """
166   Display the field at its timestep
167   """
168   medcalc.MakeScalarMap(field, viewMode=MEDCALC.VIEW_MODE_REPLACE, scalarBarRange=MEDCALC.SCALAR_BAR_CURRENT_TIMESTEP)
169   pass