From 37a0d43dcd70faa3aaeef7f850c1d76307d2d1e5 Mon Sep 17 00:00:00 2001 From: ageay Date: Wed, 20 Mar 2013 18:21:06 +0000 Subject: [PATCH] Addition of 2 new constructors of MEDFileField1TS --- src/MEDLoader/MEDFileField.cxx | 174 ++++++++++++++++++++++----- src/MEDLoader/MEDFileField.hxx | 5 + src/MEDLoader/Swig/MEDLoaderCommon.i | 12 ++ src/MEDLoader/Swig/MEDLoaderTest3.py | 7 ++ 4 files changed, 166 insertions(+), 32 deletions(-) diff --git a/src/MEDLoader/MEDFileField.cxx b/src/MEDLoader/MEDFileField.cxx index d1da7a2b8..2be217a1f 100644 --- a/src/MEDLoader/MEDFileField.cxx +++ b/src/MEDLoader/MEDFileField.cxx @@ -3860,6 +3860,26 @@ const DataArrayDouble *MEDFileField1TSWithoutSDA::getOrCreateAndGetArray() const return ret2; } +/*! + * This methods returns a new instance (to be dealt by the caller). + * This method returns for the first field in the file \a fileName the first time step of this first field, if + * such field exists and time step exists. If not, an INTERP_KERNEL::Exception will be thrown. + */ +MEDFileField1TS *MEDFileField1TS::New(const char *fileName) throw(INTERP_KERNEL::Exception) +{ + return new MEDFileField1TS(fileName); +} + +/*! + * This methods returns a new instance (to be dealt by the caller). + * This method returns the first time step of the field \a fieldName in file \a fieldName, if + * such field exists. If not, an INTERP_KERNEL::Exception will be thrown. + */ +MEDFileField1TS *MEDFileField1TS::New(const char *fileName, const char *fieldName) throw(INTERP_KERNEL::Exception) +{ + return new MEDFileField1TS(fileName,fieldName); +} + MEDFileField1TS *MEDFileField1TS::New(const char *fileName, const char *fieldName, int iteration, int order) throw(INTERP_KERNEL::Exception) { return new MEDFileField1TS(fileName,fieldName,iteration,order); @@ -3913,47 +3933,91 @@ void MEDFileField1TS::write(const char *fileName, int mode) const throw(INTERP_K writeLL(fid); } -MEDFileField1TS::MEDFileField1TS(const char *fileName, const char *fieldName, int iteration, int order) throw(INTERP_KERNEL::Exception) -try:MEDFileFieldGlobsReal(fileName),_content(MEDFileField1TSWithoutSDA::New(fieldName,-1,-1,iteration,order,std::vector())) +MEDFileField1TS::MEDFileField1TS(const char *fileName) throw(INTERP_KERNEL::Exception) +try:MEDFileFieldGlobsReal(fileName) { MEDFileUtilities::CheckFileForRead(fileName); MEDFileUtilities::AutoFid fid=MEDfileOpen(fileName,MED_ACC_RDONLY); - int nbFields=MEDnField(fid); med_field_type typcha; - bool found=false; - std::vector fns(nbFields); - int nbOfStep2=-1; - for(int i=0;i comp=MEDLoaderBase::buildEmptyString(ncomp*MED_SNAME_SIZE); - INTERP_KERNEL::AutoPtr unit=MEDLoaderBase::buildEmptyString(ncomp*MED_SNAME_SIZE); - INTERP_KERNEL::AutoPtr dtunit=MEDLoaderBase::buildEmptyString(MED_LNAME_SIZE); - INTERP_KERNEL::AutoPtr nomcha=MEDLoaderBase::buildEmptyString(MED_NAME_SIZE); - INTERP_KERNEL::AutoPtr nomMaa=MEDLoaderBase::buildEmptyString(MED_NAME_SIZE); - med_bool localMesh; - int nbOfStep; - MEDfieldInfo(fid,i+1,nomcha,nomMaa,&localMesh,&typcha,comp,unit,dtunit,&nbOfStep); - std::string tmp(nomcha); - fns[i]=tmp; - found=(tmp==fieldName); - if(found) - { - nbOfStep2=nbOfStep; - std::string mname=MEDLoaderBase::buildStringFromFortran(nomMaa,MED_NAME_SIZE); - std::vector infos(ncomp); - for(int j=0;jgetOrCreateAndGetArray()->setInfoAndChangeNbOfCompo(infos); - } + std::ostringstream oss; oss << "MEDFileField1TS(fileName) : no field present in file \'" << fileName << "\' !"; + throw INTERP_KERNEL::Exception(oss.str().c_str()); } - if(!found) + int ncomp=MEDfieldnComponent(fid,1); + INTERP_KERNEL::AutoPtr comp=MEDLoaderBase::buildEmptyString(ncomp*MED_SNAME_SIZE); + INTERP_KERNEL::AutoPtr unit=MEDLoaderBase::buildEmptyString(ncomp*MED_SNAME_SIZE); + INTERP_KERNEL::AutoPtr dtunit=MEDLoaderBase::buildEmptyString(MED_LNAME_SIZE); + INTERP_KERNEL::AutoPtr nomcha=MEDLoaderBase::buildEmptyString(MED_NAME_SIZE); + INTERP_KERNEL::AutoPtr nomMaa=MEDLoaderBase::buildEmptyString(MED_NAME_SIZE); + med_bool localMesh; + int nbOfStep; + MEDfieldInfo(fid,1,nomcha,nomMaa,&localMesh,&typcha,comp,unit,dtunit,&nbOfStep); + std::string fieldName(nomcha); + if(nbOfStep<1) { - std::ostringstream oss; oss << "No such field '" << fieldName << "' in file '" << fileName << "' ! Available fields are : "; - std::copy(fns.begin(),fns.end(),std::ostream_iterator(oss," ")); + std::ostringstream oss; oss << "MEDFileField1TS(fileName) : file \'" << fileName << "\' contains field with name \'" << fieldName << "\' but there is no time steps on it !"; throw INTERP_KERNEL::Exception(oss.str().c_str()); } - found=false; + std::vector infos(ncomp); + for(int j=0;j()); + _content->getOrCreateAndGetArray()->setInfoAndChangeNbOfCompo(infos); + // + med_int numdt,numit; + med_float dt; + MEDfieldComputingStepInfo(fid,fieldName.c_str(),1,&numdt,&numit,&dt); + _content->setTime(numdt,numit,dt); + _content->_csit=1; + _content->_field_type=MEDFileUtilities::TraduceFieldType(typcha); + _content->finishLoading(fid); + // + loadGlobals(fid); +} +catch(INTERP_KERNEL::Exception& e) + { + throw e; + } + +MEDFileField1TS::MEDFileField1TS(const char *fileName, const char *fieldName) throw(INTERP_KERNEL::Exception) +try:MEDFileFieldGlobsReal(fileName),_content(MEDFileField1TSWithoutSDA::New(fieldName,-1,-1,-1/*iteration*/,-1/*order*/,std::vector())) +{ + MEDFileUtilities::CheckFileForRead(fileName); + MEDFileUtilities::AutoFid fid=MEDfileOpen(fileName,MED_ACC_RDONLY); + med_field_type typcha; + int nbSteps=locateField(fid,fileName,fieldName,typcha); + if(nbSteps<1) + { + std::ostringstream oss; oss << "MEDFileField1TS(fileName,fieldName) : file \'" << fileName << "\' contains field with name \'" << fieldName << "\' but there is no time steps on it !"; + throw INTERP_KERNEL::Exception(oss.str().c_str()); + } + // + med_int numdt,numit; + med_float dt; + MEDfieldComputingStepInfo(fid,fieldName,1,&numdt,&numit,&dt); + _content->setTime(numdt,numit,dt); + _content->_csit=1; + _content->_field_type=MEDFileUtilities::TraduceFieldType(typcha); + _content->finishLoading(fid); + // + loadGlobals(fid); +} +catch(INTERP_KERNEL::Exception& e) + { + throw e; + } + +MEDFileField1TS::MEDFileField1TS(const char *fileName, const char *fieldName, int iteration, int order) throw(INTERP_KERNEL::Exception) +try:MEDFileFieldGlobsReal(fileName),_content(MEDFileField1TSWithoutSDA::New(fieldName,-1,-1,iteration,order,std::vector())) +{ + MEDFileUtilities::CheckFileForRead(fileName); + MEDFileUtilities::AutoFid fid=MEDfileOpen(fileName,MED_ACC_RDONLY); + med_field_type typcha; + int nbOfStep2=locateField(fid,fileName,fieldName,typcha); + bool found=false; std::vector< std::pair > dtits(nbOfStep2); for(int i=0;i fns(nbFields); + int nbOfStep2=-1; + for(int i=0;i comp=MEDLoaderBase::buildEmptyString(ncomp*MED_SNAME_SIZE); + INTERP_KERNEL::AutoPtr unit=MEDLoaderBase::buildEmptyString(ncomp*MED_SNAME_SIZE); + INTERP_KERNEL::AutoPtr dtunit=MEDLoaderBase::buildEmptyString(MED_LNAME_SIZE); + INTERP_KERNEL::AutoPtr nomcha=MEDLoaderBase::buildEmptyString(MED_NAME_SIZE); + INTERP_KERNEL::AutoPtr nomMaa=MEDLoaderBase::buildEmptyString(MED_NAME_SIZE); + med_bool localMesh; + int nbOfStep; + MEDfieldInfo(fid,i+1,nomcha,nomMaa,&localMesh,&typcha,comp,unit,dtunit,&nbOfStep); + std::string tmp(nomcha); + fns[i]=tmp; + found=(tmp==fieldName); + if(found) + { + nbOfStep2=nbOfStep; + std::string mname=MEDLoaderBase::buildStringFromFortran(nomMaa,MED_NAME_SIZE); + std::vector infos(ncomp); + for(int j=0;jgetOrCreateAndGetArray()->setInfoAndChangeNbOfCompo(infos); + } + } + if(!found) + { + std::ostringstream oss; oss << "No such field '" << fieldName << "' in file '" << fileName << "' ! Available fields are : "; + for(std::vector::const_iterator it=fns.begin();it!=fns.end();it++) + oss << "\"" << *it << "\" "; + throw INTERP_KERNEL::Exception(oss.str().c_str()); + } + return nbOfStep2; +} + /*! * This method returns all profiles whose name is non empty used. * \b WARNING If profile is used several times it will be reported \b only \b once. diff --git a/src/MEDLoader/MEDFileField.hxx b/src/MEDLoader/MEDFileField.hxx index fc98cbcda..7efcae797 100644 --- a/src/MEDLoader/MEDFileField.hxx +++ b/src/MEDLoader/MEDFileField.hxx @@ -497,6 +497,8 @@ namespace ParaMEDMEM class MEDLOADER_EXPORT MEDFileField1TS : public RefCountObject, public MEDFileWritable, public MEDFileFieldGlobsReal { public: + static MEDFileField1TS *New(const char *fileName) throw(INTERP_KERNEL::Exception); + static MEDFileField1TS *New(const char *fileName, const char *fieldName) throw(INTERP_KERNEL::Exception); static MEDFileField1TS *New(const char *fileName, const char *fieldName, int iteration, int order) throw(INTERP_KERNEL::Exception); static MEDFileField1TS *New(const MEDFileField1TSWithoutSDA& other, bool shallowCopyOfContent); static MEDFileField1TS *New(); @@ -560,9 +562,12 @@ namespace ParaMEDMEM void changeLocsRefsNamesGen(const std::vector< std::pair, std::string > >& mapOfModif) throw(INTERP_KERNEL::Exception); private: void writeLL(med_idt fid) const throw(INTERP_KERNEL::Exception); + MEDFileField1TS(const char *fileName) throw(INTERP_KERNEL::Exception); + MEDFileField1TS(const char *fileName, const char *fieldName) throw(INTERP_KERNEL::Exception); MEDFileField1TS(const char *fileName, const char *fieldName, int iteration, int order) throw(INTERP_KERNEL::Exception); MEDFileField1TS(const MEDFileField1TSWithoutSDA& other, bool shallowCopyOfContent); MEDFileField1TS(); + int locateField(med_idt fid, const char *fileName, const char *fieldName, med_field_type& typcha) throw(INTERP_KERNEL::Exception); protected: MEDCouplingAutoRefCountObjectPtr _content; }; diff --git a/src/MEDLoader/Swig/MEDLoaderCommon.i b/src/MEDLoader/Swig/MEDLoaderCommon.i index 1707f6625..e2b7bf265 100644 --- a/src/MEDLoader/Swig/MEDLoaderCommon.i +++ b/src/MEDLoader/Swig/MEDLoaderCommon.i @@ -1020,6 +1020,8 @@ namespace ParaMEDMEM { public: static MEDFileField1TS *New(const char *fileName, const char *fieldName, int iteration, int order) throw(INTERP_KERNEL::Exception); + static MEDFileField1TS *New(const char *fileName, const char *fieldName) throw(INTERP_KERNEL::Exception); + static MEDFileField1TS *New(const char *fileName) throw(INTERP_KERNEL::Exception); static MEDFileField1TS *New(); void write(const char *fileName, int mode) const throw(INTERP_KERNEL::Exception); MEDCouplingFieldDouble *getFieldAtLevel(TypeOfField type, int meshDimRelToMax, int renumPol=0) const throw(INTERP_KERNEL::Exception); @@ -1048,6 +1050,16 @@ namespace ParaMEDMEM void setTime(int iteration, int order, double val) throw(INTERP_KERNEL::Exception); %extend { + MEDFileField1TS(const char *fileName) throw(INTERP_KERNEL::Exception) + { + return MEDFileField1TS::New(fileName); + } + + MEDFileField1TS(const char *fileName, const char *fieldName) throw(INTERP_KERNEL::Exception) + { + return MEDFileField1TS::New(fileName,fieldName); + } + MEDFileField1TS(const char *fileName, const char *fieldName, int iteration, int order) throw(INTERP_KERNEL::Exception) { return MEDFileField1TS::New(fileName,fieldName,iteration,order); diff --git a/src/MEDLoader/Swig/MEDLoaderTest3.py b/src/MEDLoader/Swig/MEDLoaderTest3.py index 6725975be..0a1746a54 100644 --- a/src/MEDLoader/Swig/MEDLoaderTest3.py +++ b/src/MEDLoader/Swig/MEDLoaderTest3.py @@ -375,6 +375,7 @@ class MEDLoaderTest(unittest.TestCase): self.assertEqual(('DALLE','DALQ1','DALQ2','DALT3','MESH'),m.getGroupsOnSpecifiedLev(0)) # m.write(fileName,2) + self.assertRaises(InterpKernelException,MEDFileField1TS,fileName)#throw because no field in file fileName pass def funcToTestDelItem(self,ff): @@ -424,6 +425,12 @@ class MEDLoaderTest(unittest.TestCase): f=ff.getFieldAtLevel(ON_GAUSS_PT,0) f2=MEDLoader.ReadFieldGauss("Pyfile13.med",'2DMesh_2',0,'MyFirstFieldOnGaussPoint',1,5) self.assertTrue(f.isEqual(f2,1e-12,1e-12)) + ff3=MEDFileField1TS.New("Pyfile13.med","MyFirstFieldOnGaussPoint") + f3=ff3.getFieldAtLevel(ON_GAUSS_PT,0) + self.assertTrue(f.isEqual(f3,1e-12,1e-12)) + ff4=MEDFileField1TS.New("Pyfile13.med") + f4=ff4.getFieldAtLevel(ON_GAUSS_PT,0) + self.assertTrue(f.isEqual(f4,1e-12,1e-12)) pass #gauss NE -- 2.39.2