From e8ce038587f0d6522ba6d3cf8be291e89b55b998 Mon Sep 17 00:00:00 2001 From: ageay Date: Tue, 3 Jul 2012 07:37:42 +0000 Subject: [PATCH] delitem on MEDFileFieldMultiTS --- src/MEDLoader/MEDFileField.cxx | 24 +++++++ src/MEDLoader/MEDFileField.hxx | 1 + src/MEDLoader/Swig/MEDLoaderCommon.i | 94 ++++++++++++++++++++++++++++ src/MEDLoader/Swig/MEDLoaderTest3.py | 10 +++ 4 files changed, 129 insertions(+) diff --git a/src/MEDLoader/MEDFileField.cxx b/src/MEDLoader/MEDFileField.cxx index cfd72d2ff..1c3036f32 100644 --- a/src/MEDLoader/MEDFileField.cxx +++ b/src/MEDLoader/MEDFileField.cxx @@ -3797,6 +3797,30 @@ void MEDFileFieldMultiTSWithoutDAS::eraseEmptyTS() throw(INTERP_KERNEL::Exceptio _time_steps=newTS; } +void MEDFileFieldMultiTSWithoutDAS::eraseTimeStepIds(const int *startIds, const int *endIds) throw(INTERP_KERNEL::Exception) +{ + std::vector< MEDCouplingAutoRefCountObjectPtr > newTS; + int maxId=(int)_time_steps.size(); + int ii=0; + std::set idsToDel; + for(const int *id=startIds;id!=endIds;id++,ii++) + { + if(*id>=0 && *id& infos, int nbOfStep) throw(INTERP_KERNEL::Exception); int getNumberOfTS() const; void eraseEmptyTS() throw(INTERP_KERNEL::Exception); + void eraseTimeStepIds(const int *startIds, const int *endIds) throw(INTERP_KERNEL::Exception); std::vector< std::pair > getIterations() const; int getPosOfTimeStep(int iteration, int order) const throw(INTERP_KERNEL::Exception); int getPosGivenTime(double time, double eps=1e-8) const throw(INTERP_KERNEL::Exception); diff --git a/src/MEDLoader/Swig/MEDLoaderCommon.i b/src/MEDLoader/Swig/MEDLoaderCommon.i index 5744ec244..93ebbc8b1 100644 --- a/src/MEDLoader/Swig/MEDLoaderCommon.i +++ b/src/MEDLoader/Swig/MEDLoaderCommon.i @@ -1232,6 +1232,100 @@ namespace ParaMEDMEM PyTuple_SetItem(ret,1,elt); return ret; } + + int getTimeId(PyObject *elt0) const throw(INTERP_KERNEL::Exception) + { + if(elt0 && PyInt_Check(elt0)) + {//fmts[3] + int pos=PyInt_AS_LONG(elt0); + return pos; + } + else if(elt0 && PyTuple_Check(elt0)) + { + if(PyTuple_Size(elt0)==2) + { + PyObject *o0=PyTuple_GetItem(elt0,0); + PyObject *o1=PyTuple_GetItem(elt0,1); + if(PyInt_Check(o0) && PyInt_Check(o1)) + {//fmts(1,-1) + int iter=PyInt_AS_LONG(o0); + int order=PyInt_AS_LONG(o1); + return self->getPosOfTimeStep(iter,order); + } + else + throw INTERP_KERNEL::Exception("MEDFileFieldMultiTS::__getitem__ : invalid input param ! input is a tuple of size 2 but two integers are expected in this tuple to request a time steps !"); + } + else + throw INTERP_KERNEL::Exception("MEDFileFieldMultiTS::__getitem__ : invalid input param ! input is a tuple of size != 2 ! two integers are expected in this tuple to request a time steps !"); + } + else if(elt0 && PyFloat_Check(elt0)) + { + double val=PyFloat_AS_DOUBLE(elt0); + return self->getPosGivenTime(val); + } + else + throw INTERP_KERNEL::Exception("MEDFileFieldMultiTS::__getitem__ : invalid input params ! expected fmts[int], fmts[int,int] or fmts[double] to request time step !"); + } + + std::vector getTimeIds(PyObject *elts) const throw(INTERP_KERNEL::Exception) + { + if(PyList_Check(elts)) + { + int sz=PyList_Size(elts); + std::vector ret(sz); + for(int i=0;i ret(1); + ret[0]=ParaMEDMEM_MEDFileFieldMultiTSWithoutDAS_getTimeId(self,elts); + return ret; + } + } + + void __delitem__(PyObject *elts) throw(INTERP_KERNEL::Exception) + { + std::vector idsToRemove=ParaMEDMEM_MEDFileFieldMultiTSWithoutDAS_getTimeIds(self,elts); + if(!idsToRemove.empty()) + self->eraseTimeStepIds(&idsToRemove[0],&idsToRemove[0]+idsToRemove.size()); + } + + void eraseTimeStepIds(PyObject *li) throw(INTERP_KERNEL::Exception) + { + int sw; + int pos1; + std::vector pos2; + DataArrayInt *pos3=0; + DataArrayIntTuple *pos4=0; + convertObjToPossibleCpp1(li,sw,pos1,pos2,pos3,pos4); + switch(sw) + { + case 1: + { + self->eraseTimeStepIds(&pos1,&pos1+1); + return; + } + case 2: + { + if(pos2.empty()) + return; + self->eraseTimeStepIds(&pos2[0],&pos2[0]+pos2.size()); + return ; + } + case 3: + { + self->eraseTimeStepIds(pos3->begin(),pos3->end()); + return ; + } + default: + throw INTERP_KERNEL::Exception("MEDFileFieldMultiTSWithoutDAS::eraseTimeStepIds : unexpected input array type recognized !"); + } + } } }; diff --git a/src/MEDLoader/Swig/MEDLoaderTest3.py b/src/MEDLoader/Swig/MEDLoaderTest3.py index 49e259cc8..1c977148f 100644 --- a/src/MEDLoader/Swig/MEDLoaderTest3.py +++ b/src/MEDLoader/Swig/MEDLoaderTest3.py @@ -373,6 +373,10 @@ class MEDLoaderTest(unittest.TestCase): m.write(fileName,2) pass + def funcToTestDelItem(self,ff): + del ff[[0.02,(3,4)]] + pass + #emulation of pointe.med file. def testMEDField1(self): mm=MEDFileMesh.New("Pyfile17.med") @@ -383,6 +387,12 @@ class MEDLoaderTest(unittest.TestCase): self.assertEqual([3,4],ff[3,4].getTime()[:-1]) self.assertEqual([3,4],ff[0.01].getTime()[:-1]) ff.write("Pyfile17_bis.med",0) + # + ts=ff.getTimeSteps() ; ts=[elt[:-1] for elt in ts] + self.assertEqual([(1,2),(3,4),(5,6)],ts) + self.funcToTestDelItem(ff) + ts=ff.getTimeSteps() ; ts=[elt[:-1] for elt in ts] + self.assertEqual([(1,2)],ts) pass #profiles -- 2.39.2