From: ageay Date: Fri, 24 Jan 2014 07:29:43 +0000 (+0000) Subject: Ease the use of loadArraysIfNecessary for new MEDReader. X-Git-Tag: V7_3_1b1^2~14 X-Git-Url: http://git.salome-platform.org/gitweb/?a=commitdiff_plain;h=d16c2cf3ff23f9f8a56397d850d850fb2dcffaca;p=tools%2Fmedcoupling.git Ease the use of loadArraysIfNecessary for new MEDReader. --- diff --git a/src/MEDLoader/MEDFileField.cxx b/src/MEDLoader/MEDFileField.cxx index 2421cbc37..1df7f7114 100644 --- a/src/MEDLoader/MEDFileField.cxx +++ b/src/MEDLoader/MEDFileField.cxx @@ -5566,21 +5566,26 @@ void MEDFileAnyTypeField1TS::write(const std::string& fileName, int mode) const */ void MEDFileAnyTypeField1TS::loadArrays() { + if(getFileName().empty()) + throw INTERP_KERNEL::Exception("MEDFileAnyTypeField1TS::loadArrays : the structure does not come from a file !"); MEDFileUtilities::AutoFid fid=MEDfileOpen(getFileName().c_str(),MED_ACC_RDONLY); contentNotNullBase()->loadBigArraysRecursively(fid,*contentNotNullBase()); } /*! * This method behaves as MEDFileAnyTypeField1TS::loadArrays does, the first call, if \a this was built using a file without loading big arrays. - * But once data loaded once, this method does nothing. + * But once data loaded once, this method does nothing. Contrary to MEDFileAnyTypeField1TS::loadArrays and MEDFileAnyTypeField1TS::unloadArrays + * this method does not throw if \a this does not come from file read. * - * \throw If the fileName is not set or points to a non readable MED file. * \sa MEDFileAnyTypeField1TS::loadArrays, MEDFileAnyTypeField1TS::unloadArrays */ void MEDFileAnyTypeField1TS::loadArraysIfNecessary() { - MEDFileUtilities::AutoFid fid=MEDfileOpen(getFileName().c_str(),MED_ACC_RDONLY); - contentNotNullBase()->loadBigArraysRecursivelyIfNecessary(fid,*contentNotNullBase()); + if(!getFileName().empty()) + { + MEDFileUtilities::AutoFid fid=MEDfileOpen(getFileName().c_str(),MED_ACC_RDONLY); + contentNotNullBase()->loadBigArraysRecursivelyIfNecessary(fid,*contentNotNullBase()); + } } /*! @@ -8144,6 +8149,8 @@ void MEDFileAnyTypeFieldMultiTS::write(const std::string& fileName, int mode) co */ void MEDFileAnyTypeFieldMultiTS::loadArrays() { + if(getFileName().empty()) + throw INTERP_KERNEL::Exception("MEDFileAnyTypeFieldMultiTS::loadArrays : the structure does not come from a file !"); MEDFileUtilities::AutoFid fid=MEDfileOpen(getFileName().c_str(),MED_ACC_RDONLY); contentNotNullBase()->loadBigArraysRecursively(fid,*contentNotNullBase()); } @@ -8157,8 +8164,11 @@ void MEDFileAnyTypeFieldMultiTS::loadArrays() */ void MEDFileAnyTypeFieldMultiTS::loadArraysIfNecessary() { - MEDFileUtilities::AutoFid fid=MEDfileOpen(getFileName().c_str(),MED_ACC_RDONLY); - contentNotNullBase()->loadBigArraysRecursivelyIfNecessary(fid,*contentNotNullBase()); + if(!getFileName().empty()) + { + MEDFileUtilities::AutoFid fid=MEDfileOpen(getFileName().c_str(),MED_ACC_RDONLY); + contentNotNullBase()->loadBigArraysRecursivelyIfNecessary(fid,*contentNotNullBase()); + } } /*! @@ -9587,6 +9597,8 @@ void MEDFileFields::write(const std::string& fileName, int mode) const */ void MEDFileFields::loadArrays() { + if(getFileName().empty()) + throw INTERP_KERNEL::Exception("MEDFileFields::loadArrays : the structure does not come from a file !"); MEDFileUtilities::AutoFid fid=MEDfileOpen(getFileName().c_str(),MED_ACC_RDONLY); for(std::vector< MEDCouplingAutoRefCountObjectPtr >::iterator it=_fields.begin();it!=_fields.end();it++) { @@ -9605,12 +9617,15 @@ void MEDFileFields::loadArrays() */ void MEDFileFields::loadArraysIfNecessary() { - MEDFileUtilities::AutoFid fid=MEDfileOpen(getFileName().c_str(),MED_ACC_RDONLY); - for(std::vector< MEDCouplingAutoRefCountObjectPtr >::iterator it=_fields.begin();it!=_fields.end();it++) + if(!getFileName().empty()) { - MEDFileAnyTypeFieldMultiTSWithoutSDA *elt(*it); - if(elt) - elt->loadBigArraysRecursivelyIfNecessary(fid,*elt); + MEDFileUtilities::AutoFid fid=MEDfileOpen(getFileName().c_str(),MED_ACC_RDONLY); + for(std::vector< MEDCouplingAutoRefCountObjectPtr >::iterator it=_fields.begin();it!=_fields.end();it++) + { + MEDFileAnyTypeFieldMultiTSWithoutSDA *elt(*it); + if(elt) + elt->loadBigArraysRecursivelyIfNecessary(fid,*elt); + } } } @@ -9622,7 +9637,6 @@ void MEDFileFields::loadArraysIfNecessary() */ void MEDFileFields::unloadArrays() { - MEDFileUtilities::AutoFid fid=MEDfileOpen(getFileName().c_str(),MED_ACC_RDONLY); for(std::vector< MEDCouplingAutoRefCountObjectPtr >::iterator it=_fields.begin();it!=_fields.end();it++) { MEDFileAnyTypeFieldMultiTSWithoutSDA *elt(*it); diff --git a/src/MEDLoader/Swig/MEDLoaderTest3.py b/src/MEDLoader/Swig/MEDLoaderTest3.py index 2d49e5ed3..0e397fc48 100644 --- a/src/MEDLoader/Swig/MEDLoaderTest3.py +++ b/src/MEDLoader/Swig/MEDLoaderTest3.py @@ -3421,6 +3421,61 @@ class MEDLoaderTest(unittest.TestCase): self.assertTrue(f_read.isEqual(f,1e-12,1e-12)) pass + def testLoadIfNecessaryOnFromScratchFields0(self): + """ + This test checks that a call to loadArraysIfNecessary works (does nothing) on field data structure whatever its level 1TS, MTS, Fields. + """ + fname="Pyfile77.med" + coords=DataArrayDouble([(0,0,0),(2,1,0),(1,0,0),(1,1,0),(2,0,0),(0,1,0)]) + m=MEDCouplingUMesh("mesh",2) ; m.setCoords(coords) + m.allocateCells() + m.insertNextCell(NORM_QUAD4,[0,5,3,2]) + m.insertNextCell(NORM_QUAD4,[4,2,3,1]) + m.finishInsertingCells() + # + mm=MEDFileUMesh() ; mm.setMeshAtLevel(0,m) + ms=MEDFileMeshes() ; ms.pushMesh(mm) + fs=MEDFileFields() + arrs=4*[None] + # + ff0=MEDFileFieldMultiTS() ; fs.pushField(ff0) + f0=MEDCouplingFieldDouble(ON_GAUSS_NE) ; f0.setMesh(m) ; f0.setTimeUnit("ms") + f0.setTime(1.1,1,1) + f0.setName("myELNOField") + arrs[0]=DataArrayDouble([7,5,3,1,5,3,1,7]) ; arrs[0].setInfoOnComponent(0,"Comp0") + f0.setArray(arrs[0]) + ff0.appendFieldNoProfileSBT(f0) + # + f0.setTime(2.2,2,1) + arrs[1]=DataArrayDouble([1,7,5,3,7,5,3,1]) ; arrs[1].setInfoOnComponent(0,"Comp0") + f0.setArray(arrs[1]) + ff0.appendFieldNoProfileSBT(f0) + # + f0.setTime(3.3,3,1) + arrs[2]=DataArrayDouble([3,1,7,5,1,7,5,3]) ; arrs[2].setInfoOnComponent(0,"Comp0") + f0.setArray(arrs[2]) + ff0.appendFieldNoProfileSBT(f0) + # + f0.setTime(4.4,4,1) + arrs[3]=DataArrayDouble([5,3,1,7,3,1,7,5]) ; arrs[3].setInfoOnComponent(0,"Comp0") + f0.setArray(arrs[3]) + ff0.appendFieldNoProfileSBT(f0) + # + for i,arr in enumerate(arrs): + self.assertTrue(fs[0][i].getUndergroundDataArray().isEqual(arr,1e-12)) + fs[0][i].loadArraysIfNecessary() + self.assertTrue(fs[0][i].getUndergroundDataArray().isEqual(arr,1e-12)) + pass + fs.loadArraysIfNecessary() + for i,arr in enumerate(arrs): + self.assertTrue(fs[0][i].getUndergroundDataArray().isEqual(arr,1e-12)) + pass + fs[0].loadArraysIfNecessary() + for i,arr in enumerate(arrs): + self.assertTrue(fs[0][i].getUndergroundDataArray().isEqual(arr,1e-12)) + pass + pass + pass unittest.main()