Salome HOME
Merge from V6_main 01/04/2013
[modules/med.git] / src / MEDOP / tui / xmedpy / fieldtools.py
1 #  -*- coding: iso-8859-1 -*-
2 # Copyright (C) 2011-2013  CEA/DEN, EDF R&D
3 #
4 # This library is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU Lesser General Public
6 # License as published by the Free Software Foundation; either
7 # version 2.1 of the License.
8 #
9 # This library is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12 # Lesser General Public License for more details.
13 #
14 # You should have received a copy of the GNU Lesser General Public
15 # License along with this library; if not, write to the Free Software
16 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
17 #
18 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 #
20 # Author : Guillaume Boulant (EDF) 
21
22 #
23 # ===================================================================
24 # The functions of this module must be imported as global functions in
25 # the SALOME python console. It provides the console context with Tool
26 # functions to manipulate field throw there fieldproxy.
27 # ===================================================================
28 #
29
30 import xmed
31 from xmed.fieldproxy import FieldProxy, newFieldProxy
32     
33 # ===================================================================
34 # Operations on fields
35
36 def dup(aFieldProxy):
37     """
38     This function returns a duplicate of the specified field.
39     It's equivalent to the call '>>> copy = f.dup()'.
40     """
41     try:
42         clone = aFieldProxy.dup()
43     except Exception:
44         print "ERR: the argument can't be duplicated as a field object"
45         return None
46     return clone
47
48 # ===================================================================
49 # Get list a field with information using the commands ls and/or la
50
51 # IMPORTANT NOTE:
52 # the pyConsoleGlobals variable should holds the globals() dictionnary of
53 # the python console context
54 pyConsoleGlobals=None
55
56 def status(local=True,remote=False):
57     """
58     This function return the status of the medop context, i.e. the
59     list of fields defined in this python session.
60     """
61     status=""
62     if local is True:
63         dvars = pyConsoleGlobals
64         if dvars is None:
65             xmed.wrn("The stat function required the specification of the python context")
66             xmed.inf("Type this command \"import xmed; xmed.setConsoleGlobals(globals())")
67         if remote is True:
68             status="========= Fields used in the current context ===\n"
69         for varkey in dvars.keys():
70             var = dvars[varkey]
71             if isinstance(var, FieldProxy):
72                 status+="%s \t(id=%s, name=%s)\n"%(varkey,var.id,var.fieldname)
73
74     if remote is True:
75         if local is True:
76             status+="\n========= Fields available in the data manager ===\n"
77         fieldHandlerList = xmed.dataManager.getFieldHandlerList()
78         for fieldHandler in fieldHandlerList:
79             status+="id=%s\tname\t= %s\n\tmesh\t= %s\n\t(it,dt)\t= (%s,%s)\n\tsource\t= %s\n"%(
80                 fieldHandler.id,
81                 fieldHandler.fieldname,
82                 fieldHandler.meshname,
83                 fieldHandler.iteration,
84                 fieldHandler.order,
85                 fieldHandler.source)
86             status+="---------\n"
87
88         if len(fieldHandlerList) > 0:
89             status+="(use 'f=get(id)' to get a field in the current context)"
90     
91     return status
92
93 # For simpler typing, one can create a python command for status
94 # (avoid to type "print status()")
95 class ListFields(object):
96     """
97     A stat object displays the status of the med operating context, i.e. the
98     list of fields defined in this python session.
99     """
100     def __init__(self,all=False):
101         self.__local  = True
102         self.__remote = all
103         # all = True means that the local metadata (fieldproxy) and the
104         # remote metadata on the engine (MEDCouplingFieldDouble) are
105         # displayed by the stat command. Otherwise, only the local
106         # metadata are displayed.
107         
108     def __repr__(self):
109         return status(self.__local, self.__remote)
110
111 # Creating the commands list (ls) and list all (la)
112 ls=ListFields(all=False)
113 la=ListFields(all=True)
114
115 # ===================================================================
116 # Field Data Management
117 from xmed import properties
118 filepath  = properties.testFilePath
119
120 def load(medFileName=filepath):
121     """
122     This function indicates that we want to use the fields from the
123     specified med file. The fields meta-data are loaded in the engine
124     part of the module. To get a fieldproxy on a field, call the get 
125     function with the id of the required field. To display the whole
126     list of fields loaded in the engine, type 'ls' (la verbose).
127     """
128     xmed.dataManager.addDatasource(filepath)
129     print status(local=False,remote=True)
130     
131 def get(fieldHandlerId):
132     """
133     This return a field proxy on the field identified by the specified
134     field handler id.
135     """
136     return newFieldProxy(fieldHandlerId)
137
138 from xmed.fieldproxy import notifyGui_add
139 def put(aFieldProxy):
140     """
141     This function puts a reference to this field in the GUI data
142     model. When a field is referenced in the GUI data model, then it
143     belongs to the workspace. When the workspace is saved, all the
144     field that belongs to the workspace are saved.
145     """
146     xmed.dataManager.markAsPersistent(aFieldProxy.id, True)
147     notifyGui_add(aFieldProxy.id)
148
149 import SALOME
150 def save(filename):
151     """
152     Dump your job in a med file. Only the fields marked as persistent
153     are saved in the specified file.
154     """
155     try:
156         xmed.dataManager.savePersistentFields(filename)
157     except SALOME.SALOME_Exception, ex:
158         xmed.err(ex.details.text)
159
160 # ===================================================================
161 # Field Data visualization
162 VIEWER_VISU    = "VISU"
163 VIEWER_PARAVIS = "PARAVIS"
164 VIEWER_DEFAULT = VIEWER_VISU
165
166 VIEWER_TMP_FILE = "/tmp/medop_viewer.med"
167
168 def view_using_paravis(aFieldProxy):
169     # __GBO__ TO BE IMPROVED: we used a tmp file in this first step of
170     # development, but we should used at last a MEDCoupling corba
171     # object (see how to use the stuff in PARAVIS/src/Plugins)
172     xmed.dataManager.saveFields(VIEWER_TMP_FILE, [aFieldProxy.id])
173     
174     from xmed.driver_pvis import pvis_scalarmap
175     pvis_scalarmap(VIEWER_TMP_FILE,
176                    aFieldProxy.meshname,
177                    aFieldProxy.fieldname,
178                    aFieldProxy.type,
179                    aFieldProxy.iteration)
180
181 def view_using_visu(aFieldProxy):
182     # __GBO__ TO BE IMPROVED: we used a tmp file in this first step of
183     # development, but we should used at last a MEDCoupling corba
184     # object (see how to use the stuff in PARAVIS/src/Plugins)
185     xmed.dataManager.saveFields(VIEWER_TMP_FILE, [aFieldProxy.id])
186
187     # __GBO__: WARN due to a specific feature of VISU, when only one
188     # field timestamps exists in the med file, we have to specify an
189     # iteration number of 1, whatever the iteration value is in the
190     # med file.
191     iteration = 1 # __GBO__ for bug VISU
192     # instead of:
193     # iteration = aFieldProxy.iteration
194
195     from xmed.driver_visu import visu_scalarmap
196     result = visu_scalarmap(VIEWER_TMP_FILE,
197                             aFieldProxy.meshname,
198                             aFieldProxy.fieldname,
199                             aFieldProxy.type,
200                             iteration)
201     if result is False:
202         xmed.err("the field can't be displayed")
203     
204 def view(aFieldProxy, using=VIEWER_DEFAULT):
205     """
206     This displays a 3D view of the field using VISU or PARAVIS,
207     depending on the configuration. This view is for quick visual
208     control of a field and is not intended to be parametrizable. For
209     custom display, you should import the fields in VISU or PARAVIS
210     and continue the job in one of this SALOME module.
211     """
212     if using == VIEWER_PARAVIS:
213         view_using_paravis(aFieldProxy)
214     else:
215         view_using_visu(aFieldProxy)
216