VAPOR3 3.9.4
PyEngine.h
Go to the documentation of this file.
1#include <vector>
2#include <vapor/MyPython.h>
3#include <vapor/DataMgr.h>
4#include <vapor/DC.h>
5
6#pragma once
7
8namespace VAPoR {
9
19//
21public:
26 //
27 PyEngine(DataMgr *dataMgr)
28 {
29 VAssert(dataMgr != NULL);
30 _dataMgr = dataMgr;
31 }
32
34
43 //
44 static int Initialize();
45
88 //
89 int AddFunction(string name, string script, const vector<string> &inputs, const vector<string> &outputs, const vector<string> &outMeshes, bool coordFlag = false);
90
97 //
98 void RemoveFunction(string name);
99
107 std::vector<string> GetFunctionNames() const;
108
118 //
119 string GetFunctionScript(string name) const;
120
121 bool GetFunctionScript(string name, string &script, std::vector<string> &inputVarNames, std::vector<string> &outputVarNames, std::vector<string> &outputMeshNames, bool &coordFlag) const;
122
127 //
128 string GetFunctionStdout(string name) const;
129
165 static int Calculate(const string &script, vector<string> inputVarNames, vector<DimsType> inputVarDims, vector<float *> inputVarArrays, vector<string> outputVarNames,
166 vector<DimsType> outputVarDims, vector<float *> outputVarArrays);
167
168private:
169 class RENDER_API DerivedPythonVar : public DerivedDataVar {
170 public:
171 DerivedPythonVar(string varName, string units, DC::XType type, string mesh, string time_coord_var, bool hasMissing, std::vector<string> inNames, string script, DataMgr *dataMgr,
172 bool coordFlag);
173
174 ~DerivedPythonVar() {}
175
176 int Initialize();
177
178 bool GetBaseVarInfo(DC::BaseVar &var) const;
179
180 std::vector<string> GetInputs() const { return (_inNames); }
181
182 int GetDimLensAtLevel(int level, std::vector<size_t> &dims_at_level, std::vector<size_t> &bs_at_level) const;
183
184 virtual size_t GetNumRefLevels() const;
185
186 virtual std::vector<size_t> GetCRatios() const { return (_varInfo.GetCRatios()); }
187
188 int OpenVariableRead(size_t ts, int level = 0, int lod = 0);
189
190 int CloseVariable(int fd);
191
192 int ReadRegion(int fd, const std::vector<size_t> &min, const std::vector<size_t> &max, float *region);
193
194 bool VariableExists(size_t ts, int reflevel, int lod) const;
195
196 bool GetDataVarInfo(DC::DataVar &cvar) const;
197
200 string GetScriptStdout() const { return (_stdoutString); }
201
202 private:
203 DC::DataVar _varInfo;
204 std::vector<string> _inNames;
205 string _script;
206 DataMgr * _dataMgr;
207 bool _coordFlag;
208 DC::FileTable _fileTable;
209 DimsType _dims;
210 bool _meshMatchFlag;
211 string _stdoutString;
212
213 int _readRegionAll(int fd, const DimsType &min, const DimsType &max, float *region);
214
215 int _readRegionSubset(int fd, const DimsType &min, const DimsType &max, float *region);
216 };
217
218 class func_c {
219 public:
220 func_c() {}
221 func_c(const string &name, const string &script, const std::vector<string> &inputVarNames, const std::vector<string> &outputVarNames, const std::vector<string> &outputMeshNames,
222 const std::vector<DerivedPythonVar *> &derivedVars, bool coordFlag)
223 : _name(name), _script(script), _inputVarNames(inputVarNames), _outputVarNames(outputVarNames), _outputMeshNames(outputMeshNames), _derivedVars(derivedVars), _coordFlag(coordFlag)
224 {
225 }
226
227 string _name;
228 string _script;
229 std::vector<string> _inputVarNames;
230 std::vector<string> _outputVarNames;
231 std::vector<string> _outputMeshNames;
232 std::vector<DerivedPythonVar *> _derivedVars;
233 bool _coordFlag;
234 };
235
236 std::map<string, func_c> _functions;
237 std::map<string, string> _functionsStdio;
238 DataMgr * _dataMgr;
239 static bool _isInitialized;
240
241 PyEngine() : _dataMgr(NULL) {}
242
243 static void _cleanupDict(PyObject *mainDict, vector<string> keynames);
244
245 static int _c2python(PyObject *dict, vector<string> inputVarNames, vector<DimsType> inputVarDims, vector<float *> inputVarArrays);
246
247 static int _python2c(PyObject *dict, vector<string> outputVarNames, vector<DimsType> outputVarDims, vector<float *> outputVarArrays);
248
249 bool _validOutputVar(string name) const;
250 int _checkOutVars(const vector<string> &outputVarNames) const;
251 bool _validOutputMesh(string name) const;
252 int _checkOutMeshes(const vector<string> &outputMeshNames) const;
253
254 string _getTimeCoordVarName(const vector<string> &varNames) const;
255};
256}; // namespace VAPoR
#define VAssert(expr)
Definition: VAssert.h:9
Base class for storing variable metadata.
Definition: DC.h:754
XType
External storage types for primitive data.
Definition: DC.h:154
A cache based data reader.
Definition: DataMgr.h:110
Derived data variable abstract class.
Definition: DerivedVar.h:234
A class for managing derived variables computed with Python.
Definition: PyEngine.h:20
static int Initialize()
string GetFunctionScript(string name) const
int AddFunction(string name, string script, const vector< string > &inputs, const vector< string > &outputs, const vector< string > &outMeshes, bool coordFlag=false)
static int Calculate(const string &script, vector< string > inputVarNames, vector< DimsType > inputVarDims, vector< float * > inputVarArrays, vector< string > outputVarNames, vector< DimsType > outputVarDims, vector< float * > outputVarArrays)
void RemoveFunction(string name)
string GetFunctionStdout(string name) const
PyEngine(DataMgr *dataMgr)
Definition: PyEngine.h:27
std::vector< string > GetFunctionNames() const
bool GetFunctionScript(string name, string &script, std::vector< string > &inputVarNames, std::vector< string > &outputVarNames, std::vector< string > &outputMeshNames, bool &coordFlag) const
Wasp base class.
Definition: MyBase.h:67
#define RENDER_API
Definition: common.h:78
std::array< size_t, 3 > DimsType
Type for specifying integer indices.
Definition: Grid.h:26