return mmuPtr->getGroups(meshDimRelToMax,grps,true);
}
-MEDCoupling::MEDCouplingFieldDouble *MEDCoupling::ReadField(MEDCoupling::TypeOfField type, const std::string& fileName, const std::string& meshName, int meshDimRelToMax, const std::string& fieldName, int iteration, int order)
+MCAuto<MEDCoupling::MEDCouplingFieldDouble> MEDCoupling::ReadField(const std::string& fileName)
+{
+ std::vector<std::string> fieldNames(GetAllFieldNames(fileName));
+ std::size_t sz(fieldNames.size());
+ if(sz==0)
+ {
+ std::ostringstream oss;
+ oss << "The file \"" << fileName << "\" contains no field !";
+ throw INTERP_KERNEL::Exception(oss.str());
+ }
+ if(sz>1)
+ {
+ std::ostringstream oss;
+ oss << "In file \"" << fileName << "\" there are more than one field !" << std::endl;
+ oss << "You are invited to use ReadField(fileName, fieldName) instead to avoid misleading concerning field you want to read !" << std::endl;
+ oss << "For information, fields available are :" << std::endl;
+ for(std::vector<std::string>::const_iterator it=fieldNames.begin();it!=fieldNames.end();it++)
+ oss << " - \"" << *it << "\"" << std::endl;
+ throw INTERP_KERNEL::Exception(oss.str());
+ }
+ return ReadField(fileName,fieldNames[0]);
+}
+
+MCAuto<MEDCoupling::MEDCouplingFieldDouble> MEDCoupling::ReadField(const std::string& fileName, const std::string& fieldName)
+{
+ std::vector< std::pair< std::pair<int,int>, double> > iterations(GetAllFieldIterations(fileName,fieldName));
+ std::size_t sz(iterations.size());
+ if(sz==0)
+ {
+ std::ostringstream oss;
+ oss << "In file \"" << fileName << "\" field \"" << fieldName << "\" exists but with no time steps !";
+ throw INTERP_KERNEL::Exception(oss.str());
+ }
+ if(sz>1)
+ {
+ std::ostringstream oss;
+ oss << "In file \"" << fileName << "\" field \"" << fieldName << "\" exists but with more than one time steps !" << std::endl;
+ oss << "You are invited to use ReadField(fileName, fieldName, iteration, order) instead to avoid misleading concerning time steps." << std::endl;
+ oss << "For information, time steps available for field \"" << fieldName << "\" are :" << std::endl;
+ for(std::vector< std::pair< std::pair<int,int>, double> >::const_iterator it=iterations.begin();it!=iterations.end();it++)
+ oss << " - " << (*it).first.first << ", " << (*it).first.second << " (" << (*it).second << ")" << std::endl;
+ throw INTERP_KERNEL::Exception(oss.str());
+ }
+ return ReadField(fileName,fieldName,iterations[0].first.first,iterations[0].first.second);
+}
+
+MCAuto<MEDCoupling::MEDCouplingFieldDouble> MEDCoupling::ReadField(const std::string& fileName, const std::string& fieldName, int iteration, int order)
+{
+ MCAuto<MEDFileField1TS> f(MEDFileField1TS::New(fileName,fieldName,iteration,order));
+ MCAuto<MEDFileMesh> mesh(MEDFileMesh::New(fileName,f->getMeshName()));
+ MCAuto<MEDCoupling::MEDCouplingFieldDouble> ret(f->field(mesh));
+ return ret;
+}
+
+MCAuto<MEDCoupling::MEDCouplingFieldDouble> MEDCoupling::ReadField(MEDCoupling::TypeOfField type, const std::string& fileName, const std::string& meshName, int meshDimRelToMax, const std::string& fieldName, int iteration, int order)
{
MEDCoupling::CheckFileForRead(fileName);
switch(type)
{
case ON_CELLS:
- return ReadFieldCell(fileName,meshName,meshDimRelToMax,fieldName,iteration,order);
+ return MCAuto<MEDCoupling::MEDCouplingFieldDouble>(ReadFieldCell(fileName,meshName,meshDimRelToMax,fieldName,iteration,order));
case ON_NODES:
- return ReadFieldNode(fileName,meshName,meshDimRelToMax,fieldName,iteration,order);
+ return MCAuto<MEDCoupling::MEDCouplingFieldDouble>(ReadFieldNode(fileName,meshName,meshDimRelToMax,fieldName,iteration,order));
case ON_GAUSS_PT:
- return ReadFieldGauss(fileName,meshName,meshDimRelToMax,fieldName,iteration,order);
+ return MCAuto<MEDCoupling::MEDCouplingFieldDouble>(ReadFieldGauss(fileName,meshName,meshDimRelToMax,fieldName,iteration,order));
case ON_GAUSS_NE:
- return ReadFieldGaussNE(fileName,meshName,meshDimRelToMax,fieldName,iteration,order);
+ return MCAuto<MEDCoupling::MEDCouplingFieldDouble>(ReadFieldGaussNE(fileName,meshName,meshDimRelToMax,fieldName,iteration,order));
default:
throw INTERP_KERNEL::Exception("Type of field specified not managed ! manages are ON_NODES, ON_CELLS, ON_GAUSS_PT or ON_GAUSS_NE !");
}
%newobject ReadUMeshFromFamiliesSwig;
%newobject ReadUMeshFromGroupsSwig;
+%newobject ReadFieldSwig;
%newobject MEDCoupling::ReadUMeshFromFile;
%newobject MEDCoupling::ReadMeshFromFile;
-%newobject MEDCoupling::ReadField;
%newobject MEDCoupling::ReadFieldCell;
%newobject MEDCoupling::ReadFieldNode;
%newobject MEDCoupling::ReadFieldGauss;
MEDCoupling::MEDCouplingUMesh *ReadUMeshFromFile(const std::string& fileName, const std::string& meshName, int meshDimRelToMax=0) throw(INTERP_KERNEL::Exception);
MEDCoupling::MEDCouplingUMesh *ReadUMeshFromFile(const std::string& fileName, int meshDimRelToMax=0) throw(INTERP_KERNEL::Exception);
int ReadUMeshDimFromFile(const std::string& fileName, const std::string& meshName) throw(INTERP_KERNEL::Exception);
- MEDCoupling::MEDCouplingFieldDouble *ReadField(MEDCoupling::TypeOfField type, const std::string& fileName, const std::string& meshName, int meshDimRelToMax, const std::string& fieldName, int iteration, int order) throw(INTERP_KERNEL::Exception);
MEDCoupling::MEDCouplingFieldDouble *ReadFieldCell(const std::string& fileName, const std::string& meshName, int meshDimRelToMax, const std::string& fieldName, int iteration, int order) throw(INTERP_KERNEL::Exception);
MEDCoupling::MEDCouplingFieldDouble *ReadFieldNode(const std::string& fileName, const std::string& meshName, int meshDimRelToMax, const std::string& fieldName, int iteration, int order) throw(INTERP_KERNEL::Exception);
MEDCoupling::MEDCouplingFieldDouble *ReadFieldGauss(const std::string& fileName, const std::string& meshName, int meshDimRelToMax, const std::string& fieldName, int iteration, int order) throw(INTERP_KERNEL::Exception);
%rename (GetTypesOfField) GetTypesOfFieldSwig;
%rename (ReadUMeshFromGroups) ReadUMeshFromGroupsSwig;
%rename (ReadUMeshFromFamilies) ReadUMeshFromFamiliesSwig;
+%rename (ReadField) ReadFieldSwig;
%inline
{
return ret;
}
+ MEDCoupling::MEDCouplingFieldDouble *ReadFieldSwig(const std::string& fileName) throw(INTERP_KERNEL::Exception)
+ {
+ MCAuto<MEDCoupling::MEDCouplingFieldDouble> ret(MEDCoupling::ReadField(fileName));
+ return ret.retn();
+ }
+
+ MEDCoupling::MEDCouplingFieldDouble *ReadFieldSwig(const std::string& fileName, const std::string& fieldName) throw(INTERP_KERNEL::Exception)
+ {
+ MCAuto<MEDCoupling::MEDCouplingFieldDouble> ret(MEDCoupling::ReadField(fileName,fieldName));
+ return ret.retn();
+ }
+
+ MEDCoupling::MEDCouplingFieldDouble *ReadFieldSwig(const std::string& fileName, const std::string& fieldName, int iteration, int order) throw(INTERP_KERNEL::Exception)
+ {
+ MCAuto<MEDCoupling::MEDCouplingFieldDouble> ret(MEDCoupling::ReadField(fileName,fieldName,iteration,order));
+ return ret.retn();
+ }
+
+ MEDCoupling::MEDCouplingFieldDouble *ReadFieldSwig(MEDCoupling::TypeOfField type, const std::string& fileName, const std::string& meshName, int meshDimRelToMax, const std::string& fieldName, int iteration, int order) throw(INTERP_KERNEL::Exception)
+ {
+ MCAuto<MEDCoupling::MEDCouplingFieldDouble> ret(MEDCoupling::ReadField(type,fileName,meshName,meshDimRelToMax,fieldName,iteration,order));
+ return ret.retn();
+ }
+
PyObject *GetFieldIterationsSwig(MEDCoupling::TypeOfField type, const std::string& fileName, const std::string& meshName, const std::string& fieldName) throw(INTERP_KERNEL::Exception)
{
std::vector< std::pair<int,int> > res=MEDCoupling::GetFieldIterations(type,fileName,meshName,fieldName);
self.assertTrue(f11.isEqual(f3r,1e-12,1e-12))
self.assertTrue(f3r.getArray().isEqual(MEDLoader.DataArrayDouble([0.,4.,8.,12.,16.,20.,24.,28.,32.,36.,40.,44.,48.,52.,56.,60.,64.,68.,72.,76.]),1e-12))
pass
+
+ def testEasyFieldRead1(self):
+ fname="Pyfile111.med"
+ arr=MEDLoader.DataArrayDouble(4) ; arr.iota()
+ m=MEDLoader.MEDCouplingCMesh() ; m.setCoords(arr,arr)
+ m=m.buildUnstructured()
+ m.setName("mesh")
+ f=MEDLoader.MEDCouplingFieldDouble(MEDLoader.ON_CELLS)
+ f.setName("field")
+ f.setTime(3.,1,2)
+ da=MEDLoader.DataArrayDouble([2,3,4,5,6,7,8,9,10])
+ f.setArray(da) ; f.setMesh(m)
+ MEDLoader.WriteField(fname,f,True)
+ #
+ self.assertTrue(f.isEqual(MEDLoader.ReadField(fname),1e-12,1e-12))
+ self.assertTrue(f.isEqual(MEDLoader.ReadField(fname,"field"),1e-12,1e-12))
+ self.assertTrue(f.isEqual(MEDLoader.ReadField(fname,"field",1,2),1e-12,1e-12))
+ self.assertTrue(f.isEqual(MEDLoader.ReadField(MEDLoader.ON_CELLS,fname,"mesh",0,"field",1,2),1e-12,1e-12))
+ #
+ f3=f.deepCopy()
+ f3.setArray(f.getArray()+30)
+ f3.setName("field2")
+ f3.setTime(5.,4,5)
+ MEDLoader.WriteFieldUsingAlreadyWrittenMesh(fname,f3)
+ #
+ self.assertRaises(Exception,MEDLoader.ReadField,fname) # because several fields in fname now
+ self.assertTrue(f.isEqual(MEDLoader.ReadField(fname,"field"),1e-12,1e-12))
+ self.assertTrue(f.isEqual(MEDLoader.ReadField(fname,"field",1,2),1e-12,1e-12))
+ self.assertTrue(f.isEqual(MEDLoader.ReadField(MEDLoader.ON_CELLS,fname,"mesh",0,"field",1,2),1e-12,1e-12))
+ self.assertTrue(f3.isEqual(MEDLoader.ReadField(fname,"field2"),1e-12,1e-12))
+ self.assertTrue(f3.isEqual(MEDLoader.ReadField(fname,"field2",4,5),1e-12,1e-12))
+ self.assertTrue(f3.isEqual(MEDLoader.ReadField(MEDLoader.ON_CELLS,fname,"mesh",0,"field2",4,5),1e-12,1e-12))
+ #
+ f2=f.deepCopy()
+ f2.setTime(4.,3,4)
+ f2.setArray(f2.getArray()+10)
+ MEDLoader.WriteFieldUsingAlreadyWrittenMesh(fname,f2)
+ #
+ self.assertRaises(Exception,MEDLoader.ReadField,fname) # because unique field "field" has more than one time step
+ self.assertRaises(Exception,MEDLoader.ReadField,fname,"field") # because unique field "field" has more than one time step
+ self.assertTrue(f.isEqual(MEDLoader.ReadField(fname,"field",1,2),1e-12,1e-12))
+ self.assertTrue(f.isEqual(MEDLoader.ReadField(MEDLoader.ON_CELLS,fname,"mesh",0,"field",1,2),1e-12,1e-12))
+ self.assertTrue(f2.isEqual(MEDLoader.ReadField(fname,"field",3,4),1e-12,1e-12))
+ self.assertTrue(f2.isEqual(MEDLoader.ReadField(MEDLoader.ON_CELLS,fname,"mesh",0,"field",3,4),1e-12,1e-12))
+ #
+ self.assertTrue(f.isEqual(MEDLoader.ReadField(fname,"field",1,2),1e-12,1e-12))
+ self.assertTrue(f.isEqual(MEDLoader.ReadField(MEDLoader.ON_CELLS,fname,"mesh",0,"field",1,2),1e-12,1e-12))
+ self.assertTrue(f2.isEqual(MEDLoader.ReadField(fname,"field",3,4),1e-12,1e-12))
+ self.assertTrue(f2.isEqual(MEDLoader.ReadField(MEDLoader.ON_CELLS,fname,"mesh",0,"field",3,4),1e-12,1e-12))
+ #
+ self.assertTrue(f3.isEqual(MEDLoader.ReadField(fname,"field2"),1e-12,1e-12))
+ self.assertTrue(f3.isEqual(MEDLoader.ReadField(fname,"field2",4,5),1e-12,1e-12))
+ self.assertRaises(Exception,MEDLoader.ReadField,fname,"field2",5,5) # invalid time step
+ self.assertTrue(f3.isEqual(MEDLoader.ReadField(MEDLoader.ON_CELLS,fname,"mesh",0,"field2",4,5),1e-12,1e-12))
+ self.assertRaises(Exception,MEDLoader.ReadField,MEDLoader.ON_CELLS,fname,"mesh",0,"field2",5,5) # invalid time step
+ # Test on profile - restart from scratch
+ mm=MEDLoader.MEDFileUMesh()
+ mm[0]=m
+ mm.write(fname,2)
+ #
+ pfl=MEDLoader.DataArrayInt(range(8))
+ pfl.setName("PFL")
+ #
+ f=MEDLoader.MEDCouplingFieldDouble(MEDLoader.ON_CELLS)
+ f.setName("field")
+ f.setTime(3.,1,2)
+ da=MEDLoader.DataArrayDouble([2,3,4,5,6,7,8,9])
+ f.setArray(da) ; f.setMesh(m[pfl])
+ f.checkConsistencyLight()
+ #
+ f1ts=MEDLoader.MEDFileField1TS()
+ f1ts.setFieldProfile(f,mm,0,pfl)
+ f1ts.write(fname,0)
+ #
+ self.assertTrue(f.isEqual(MEDLoader.ReadField(fname),1e-12,1e-12))
+ self.assertTrue(f.isEqual(MEDLoader.ReadField(fname,"field"),1e-12,1e-12))
+ self.assertTrue(f.isEqual(MEDLoader.ReadField(fname,"field",1,2),1e-12,1e-12))
+ self.assertTrue(f.isEqual(MEDLoader.ReadField(MEDLoader.ON_CELLS,fname,"mesh",0,"field",1,2),1e-12,1e-12))
+ #
+ f3=f.deepCopy()
+ f3.setArray(f.getArray()+30)
+ f3.setName("field2")
+ f3.setTime(5.,4,5)
+ f1ts=MEDLoader.MEDFileField1TS()
+ f1ts.setFieldProfile(f3,mm,0,pfl)
+ f1ts.write(fname,0)
+ #
+ self.assertRaises(Exception,MEDLoader.ReadField,fname) # because several fields in fname now
+ self.assertTrue(f.isEqual(MEDLoader.ReadField(fname,"field"),1e-12,1e-12))
+ self.assertTrue(f.isEqual(MEDLoader.ReadField(fname,"field",1,2),1e-12,1e-12))
+ self.assertTrue(f.isEqual(MEDLoader.ReadField(MEDLoader.ON_CELLS,fname,"mesh",0,"field",1,2),1e-12,1e-12))
+ self.assertTrue(f3.isEqual(MEDLoader.ReadField(fname,"field2"),1e-12,1e-12))
+ self.assertTrue(f3.isEqual(MEDLoader.ReadField(fname,"field2",4,5),1e-12,1e-12))
+ self.assertTrue(f3.isEqual(MEDLoader.ReadField(MEDLoader.ON_CELLS,fname,"mesh",0,"field2",4,5),1e-12,1e-12))
+ #
+ f2=f.deepCopy()
+ f2.setTime(4.,3,4)
+ f2.setArray(f2.getArray()+10)
+ f1ts=MEDLoader.MEDFileField1TS()
+ f1ts.setFieldProfile(f2,mm,0,pfl)
+ f1ts.write(fname,0)
+ #
+ self.assertRaises(Exception,MEDLoader.ReadField,fname) # because unique field "field" has more than one time step
+ self.assertRaises(Exception,MEDLoader.ReadField,fname,"field") # because unique field "field" has more than one time step
+ self.assertTrue(f.isEqual(MEDLoader.ReadField(fname,"field",1,2),1e-12,1e-12))
+ self.assertTrue(f.isEqual(MEDLoader.ReadField(MEDLoader.ON_CELLS,fname,"mesh",0,"field",1,2),1e-12,1e-12))
+ self.assertTrue(f2.isEqual(MEDLoader.ReadField(fname,"field",3,4),1e-12,1e-12))
+ self.assertTrue(f2.isEqual(MEDLoader.ReadField(MEDLoader.ON_CELLS,fname,"mesh",0,"field",3,4),1e-12,1e-12))
+ #
+ self.assertTrue(f.isEqual(MEDLoader.ReadField(fname,"field",1,2),1e-12,1e-12))
+ self.assertTrue(f.isEqual(MEDLoader.ReadField(MEDLoader.ON_CELLS,fname,"mesh",0,"field",1,2),1e-12,1e-12))
+ self.assertTrue(f2.isEqual(MEDLoader.ReadField(fname,"field",3,4),1e-12,1e-12))
+ self.assertTrue(f2.isEqual(MEDLoader.ReadField(MEDLoader.ON_CELLS,fname,"mesh",0,"field",3,4),1e-12,1e-12))
+ #
+ self.assertTrue(f3.isEqual(MEDLoader.ReadField(fname,"field2"),1e-12,1e-12))
+ self.assertTrue(f3.isEqual(MEDLoader.ReadField(fname,"field2",4,5),1e-12,1e-12))
+ self.assertRaises(Exception,MEDLoader.ReadField,fname,"field2",5,5) # invalid time step
+ self.assertTrue(f3.isEqual(MEDLoader.ReadField(MEDLoader.ON_CELLS,fname,"mesh",0,"field2",4,5),1e-12,1e-12))
+ self.assertRaises(Exception,MEDLoader.ReadField,MEDLoader.ON_CELLS,fname,"mesh",0,"field2",5,5) # invalid time step
+ pass
+
pass
if __name__ == "__main__":