]> SALOME platform Git repositories - tools/medcoupling.git/commitdiff
Salome HOME
delitem on MEDFileFieldMultiTS
authorageay <ageay>
Tue, 3 Jul 2012 07:37:42 +0000 (07:37 +0000)
committerageay <ageay>
Tue, 3 Jul 2012 07:37:42 +0000 (07:37 +0000)
src/MEDLoader/MEDFileField.cxx
src/MEDLoader/MEDFileField.hxx
src/MEDLoader/Swig/MEDLoaderCommon.i
src/MEDLoader/Swig/MEDLoaderTest3.py

index cfd72d2ff51dcde7163e9e62510facdbb8519055..1c3036f32ff1b2691a80824def1a1bb2e0e5a704 100644 (file)
@@ -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<MEDFileField1TSWithoutDAS>  > newTS;
+  int maxId=(int)_time_steps.size();
+  int ii=0;
+  std::set<int> idsToDel;
+  for(const int *id=startIds;id!=endIds;id++,ii++)
+    {
+      if(*id>=0 && *id<maxId)
+        {
+          idsToDel.insert(*id);
+        }
+      else
+        {
+          std::ostringstream oss; oss << "MEDFileFieldMultiTSWithoutDAS::eraseTimeStepIds : At pos #" << ii << " request for id=" << *id << " not in [0," << maxId << ") !";
+          throw INTERP_KERNEL::Exception(oss.str().c_str());
+        }
+    }
+  for(int iii=0;iii<maxId;iii++)
+    if(idsToDel.find(iii)==idsToDel.end())
+      newTS.push_back(_time_steps[iii]);
+  _time_steps=newTS;
+}
+
 int MEDFileFieldMultiTSWithoutDAS::getPosOfTimeStep(int iteration, int order) const throw(INTERP_KERNEL::Exception)
 {
   int ret=0;
index 9528395bfcf29a0ef3cdf3298416ff9fb2b6e887..95bba60f57d5136815a084ab71fc1a292f0d71a3 100644 (file)
@@ -489,6 +489,7 @@ namespace ParaMEDMEM
     static MEDFileFieldMultiTSWithoutDAS *New(med_idt fid, const char *fieldName, int id, int ft, const std::vector<std::string>& 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<int,int> > 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);
index 5744ec244157fa76f2b3905ac6952667af322b8e..93ebbc8b1e15dd11e06163c9cbb09d8c872fa43a 100644 (file)
@@ -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<int> getTimeIds(PyObject *elts) const throw(INTERP_KERNEL::Exception)
+         {
+           if(PyList_Check(elts))
+             {
+               int sz=PyList_Size(elts);
+               std::vector<int> ret(sz);
+               for(int i=0;i<sz;i++)
+                 {
+                   PyObject *elt=PyList_GetItem(elts,i);
+                   ret[i]=ParaMEDMEM_MEDFileFieldMultiTSWithoutDAS_getTimeId(self,elt);
+                 }
+               return ret;
+             }
+           else
+             {
+               std::vector<int> ret(1);
+               ret[0]=ParaMEDMEM_MEDFileFieldMultiTSWithoutDAS_getTimeId(self,elts);
+               return ret;
+             }
+         }
+
+         void __delitem__(PyObject *elts) throw(INTERP_KERNEL::Exception)
+         {
+           std::vector<int> 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<int> 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 !");
+             }
+         }
        }
   };
 
index 49e259cc8a07762ab14b9d22397614da071db8e7..1c977148fab0c52d137794317c3c32575bd8bb52 100644 (file)
@@ -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