Salome HOME
Indentation and white spaces ...
[tools/medcoupling.git] / src / MEDLoader / MEDLoader.cxx
index 79bc806824f9ed256b744f68cdc8d4209a9f95e0..8b1ffc7c63e135d9d86c4c8430190cd36070d1ce 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2007-2016  CEA/DEN, EDF R&D
+// Copyright (C) 2007-2019  CEA/DEN, EDF R&D
 //
 // This library is free software; you can redistribute it and/or
 // modify it under the terms of the GNU Lesser General Public
@@ -32,6 +32,7 @@
 #include "MEDCouplingFieldFloat.hxx"
 #include "MEDCouplingFieldInt.hxx"
 #include "MEDCouplingGaussLocalization.hxx"
+#include "MEDCouplingTraits.hxx"
 #include "MCAuto.hxx"
 
 #include "InterpKernelAutoPtr.hxx"
@@ -345,7 +346,7 @@ void MEDCoupling::MEDFileVersion(int& major, int& minor, int& release)
 }
 
 /*!
- * This method sets the epsilon value used for node comparison when trying to buid a profile for a field on node/cell on an already written mesh.
+ * This method sets the epsilon value used for node comparison when trying to build a profile for a field on node/cell on an already written mesh.
  */
 void MEDCoupling::SetEpsilonForNodeComp(double val)
 {
@@ -1232,24 +1233,21 @@ MCAuto<MEDCoupling::MEDCouplingField> MEDCoupling::ReadField(const std::string&
       }
   }
   throw INTERP_KERNEL::Exception("MEDCoupling::ReadField : only FLOAT32, FLOAT64 and INT32 supported for the moment !");
-  //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)
+MCAuto<MEDCoupling::MEDCouplingField> 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 MCAuto<MEDCoupling::MEDCouplingFieldDouble>(ReadFieldCell(fileName,meshName,meshDimRelToMax,fieldName,iteration,order));
+      return ReadFieldCell(fileName,meshName,meshDimRelToMax,fieldName,iteration,order);
     case ON_NODES:
-      return MCAuto<MEDCoupling::MEDCouplingFieldDouble>(ReadFieldNode(fileName,meshName,meshDimRelToMax,fieldName,iteration,order));
+      return ReadFieldNode(fileName,meshName,meshDimRelToMax,fieldName,iteration,order);
     case ON_GAUSS_PT:
-      return MCAuto<MEDCoupling::MEDCouplingFieldDouble>(ReadFieldGauss(fileName,meshName,meshDimRelToMax,fieldName,iteration,order));
+      return ReadFieldGauss(fileName,meshName,meshDimRelToMax,fieldName,iteration,order);
     case ON_GAUSS_NE:
-      return MCAuto<MEDCoupling::MEDCouplingFieldDouble>(ReadFieldGaussNE(fileName,meshName,meshDimRelToMax,fieldName,iteration,order));
+      return 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 !");
   }
@@ -1316,97 +1314,139 @@ std::vector<MEDCoupling::MEDCouplingFieldDouble *> MEDCoupling::ReadFieldsGaussN
   return ReadFieldsOnSameMesh(ON_GAUSS_NE,fileName,meshName,meshDimRelToMax,fieldName,its);
 }
 
-MEDCoupling::MEDCouplingFieldDouble *MEDCoupling::ReadFieldCell(const std::string& fileName, const std::string& meshName, int meshDimRelToMax, const std::string& fieldName, int iteration, int order)
+namespace MEDCoupling
 {
-  MCAuto<MEDFileField1TS> ff(MEDFileField1TS::New(fileName,fieldName,iteration,order));
-  MCAuto<MEDFileMesh> mm(MEDFileMesh::New(fileName,meshName));
-  MCAuto<MEDCouplingMesh> m(mm->getMeshAtLevel(meshDimRelToMax,false));
-  MEDFileMesh *mPtr(mm);
-  MEDFileUMesh *muPtr=dynamic_cast<MEDFileUMesh *>(mPtr);
-  MCAuto<MEDCouplingFieldDouble> ret(ff->getFieldOnMeshAtLevel(ON_CELLS,m));
-  if(muPtr)
-    {
-      const DataArrayInt *num(muPtr->getNumberFieldAtLevel(meshDimRelToMax));
-      if(num)
-        ret->renumberCells(num->begin());
-    }
-  return ret.retn();
-}
+  template<class T>
+  MCAuto<typename Traits<T>::FieldType> ReadFieldCellLikeT(typename MLFieldTraits<T>::F1TSType *ff, MEDCoupling::TypeOfField type, const std::string& fileName, const std::string& meshName, int meshDimRelToMax, const std::string& fieldName, int iteration, int order)
+  {
+    MCAuto<MEDFileMesh> mm(MEDFileMesh::New(fileName,meshName));
+    MCAuto<MEDFileUMesh> muPtr(MEDCoupling::DynamicCast<MEDFileMesh,MEDFileUMesh>(mm));
+    MCAuto<MEDCouplingMesh> m(mm->getMeshAtLevel(meshDimRelToMax,false));
+    MCAuto<typename Traits<T>::FieldType> ret(ff->getFieldOnMeshAtLevel(type,m));
+    if(muPtr.isNotNull())
+      {
+        const DataArrayInt *num(muPtr->getNumberFieldAtLevel(meshDimRelToMax));
+        if(num)
+          ret->renumberCells(num->begin());
+      }
+    return ret;
+  }
 
-MEDCoupling::MEDCouplingFieldDouble *MEDCoupling::ReadFieldNode(const std::string& fileName, const std::string& meshName, int meshDimRelToMax, const std::string& fieldName, int iteration, int order)
-{
-  MCAuto<MEDFileField1TS> ff(MEDFileField1TS::New(fileName,fieldName,iteration,order));
-  MCAuto<MEDFileMesh> mm(MEDFileMesh::New(fileName,meshName));
-  MCAuto<MEDCouplingMesh> m(mm->getMeshAtLevel(meshDimRelToMax,false));
-  MEDFileMesh *mPtr(mm);
-  MCAuto<MEDCouplingFieldDouble> ret(ff->getFieldOnMeshAtLevel(ON_NODES,m));
-  MEDFileUMesh *muPtr=dynamic_cast<MEDFileUMesh *>(mPtr);
-  if(ff->getPflsReallyUsed().empty())
+  MEDCoupling::MEDCouplingField *ReadFieldCellLike(MEDCoupling::TypeOfField type, const std::string& fileName, const std::string& meshName, int meshDimRelToMax, const std::string& fieldName, int iteration, int order)
+  {
+    MCAuto<MEDFileAnyTypeField1TS> f(MEDFileAnyTypeField1TS::New(fileName,fieldName,iteration,order));
     {
-      if(muPtr)
+      MCAuto<MEDFileField1TS> f1(MEDCoupling::DynamicCast<MEDFileAnyTypeField1TS,MEDFileField1TS>(f));
+      if(f1.isNotNull())
         {
-          const DataArrayInt *num(muPtr->getNumberFieldAtLevel(meshDimRelToMax));
-          if(num)
-            ret->renumberCells(num->begin());
+          MCAuto<MEDCoupling::MEDCouplingFieldDouble> ret(ReadFieldCellLikeT<double>(f1,type,fileName,meshName,meshDimRelToMax,fieldName,iteration,order));
+          return ret.retn();
         }
     }
-  else
     {
-      DataArrayInt *pfl=0,*arr2=0;
-      MCAuto<DataArrayDouble> arr(ff->getFieldWithProfile(ON_NODES,meshDimRelToMax,mm,pfl));
-      MCAuto<DataArrayInt> pflSafe(pfl);
-      MCAuto<DataArrayInt> mp(m->getCellIdsFullyIncludedInNodeIds(pfl->begin(),pfl->end()));
-      MCAuto<MEDCouplingUMesh> mzip(static_cast<MEDCouplingUMesh *>(m->buildPartAndReduceNodes(mp->begin(),mp->end(),arr2)));
-      MCAuto<DataArrayInt> arr2Safe(arr2);
-      MCAuto<DataArrayInt> arr3(arr2->invertArrayO2N2N2O(mzip->getNumberOfNodes()));
-      MCAuto<DataArrayInt> pflSorted(pflSafe->deepCopy()); pflSorted->sort(true);
-      if(!arr3->isEqualWithoutConsideringStr(*pflSorted))
-        throw INTERP_KERNEL::Exception("ReadFieldNode : not implemented yet !");
-      if(!arr3->isEqualWithoutConsideringStr(*pflSafe))
+      MCAuto<MEDFileIntField1TS> f1(MEDCoupling::DynamicCast<MEDFileAnyTypeField1TS,MEDFileIntField1TS>(f));
+      if(f1.isNotNull())
         {
-          MCAuto<DataArrayInt> o2n2(pflSafe->checkAndPreparePermutation());
-          MCAuto<DataArrayInt> n2o2(o2n2->invertArrayO2N2N2O(o2n2->getNumberOfTuples()));
-          mzip->renumberNodes(n2o2->begin(),n2o2->getNumberOfTuples());
-          arr->setName("");
-          ret->setArray(arr);
+          MCAuto<MEDCoupling::MEDCouplingFieldInt> ret(ReadFieldCellLikeT<int>(f1,type,fileName,meshName,meshDimRelToMax,fieldName,iteration,order));
+          return ret.retn();
         }
-      ret->setMesh(mzip);
     }
-  return ret.retn();
+    {
+      MCAuto<MEDFileFloatField1TS> f1(MEDCoupling::DynamicCast<MEDFileAnyTypeField1TS,MEDFileFloatField1TS>(f));
+      if(f1.isNotNull())
+        {
+          MCAuto<MEDCoupling::MEDCouplingFieldFloat> ret(ReadFieldCellLikeT<float>(f1,type,fileName,meshName,meshDimRelToMax,fieldName,iteration,order));
+          return ret.retn();
+        }
+    }
+    throw INTERP_KERNEL::Exception("MEDCoupling::ReadFieldCell : only FLOAT32, FLOAT64 and INT32 supported for the moment !");
+  }
+
+  template<class T>
+  MCAuto<typename Traits<T>::FieldType> ReadFieldNodeT(typename MLFieldTraits<T>::F1TSType *ff, const std::string& fileName, const std::string& meshName, int meshDimRelToMax, const std::string& fieldName, int iteration, int order)
+  {
+    MCAuto<MEDFileMesh> mm(MEDFileMesh::New(fileName,meshName));
+    MCAuto<MEDCouplingMesh> m(mm->getMeshAtLevel(meshDimRelToMax,false));
+    MCAuto<typename Traits<T>::FieldType> ret(ff->getFieldOnMeshAtLevel(ON_NODES,m));
+    MCAuto<MEDFileUMesh> muPtr(MEDCoupling::DynamicCast<MEDFileMesh,MEDFileUMesh>(mm));
+    if(ff->getPflsReallyUsed().empty())
+      {
+        if(muPtr.isNotNull())
+          {
+            const DataArrayInt *num(muPtr->getNumberFieldAtLevel(meshDimRelToMax));
+            if(num)
+              ret->renumberCells(num->begin());
+          }
+      }
+    else
+      {
+        DataArrayInt *pfl(nullptr),*arr2(nullptr);
+        MCAuto<typename Traits<T>::ArrayType> arr(ff->getFieldWithProfile(ON_NODES,meshDimRelToMax,mm,pfl));
+        MCAuto<DataArrayInt> pflSafe(pfl);
+        MCAuto<DataArrayInt> mp(m->getCellIdsFullyIncludedInNodeIds(pfl->begin(),pfl->end()));
+        MCAuto<MEDCouplingUMesh> mzip(static_cast<MEDCouplingUMesh *>(m->buildPartAndReduceNodes(mp->begin(),mp->end(),arr2)));
+        MCAuto<DataArrayInt> arr2Safe(arr2);
+        MCAuto<DataArrayInt> arr3(arr2->invertArrayO2N2N2O(mzip->getNumberOfNodes()));
+        MCAuto<DataArrayInt> pflSorted(pflSafe->deepCopy()); pflSorted->sort(true);
+        if(!arr3->isEqualWithoutConsideringStr(*pflSorted))
+          throw INTERP_KERNEL::Exception("ReadFieldNode : not implemented yet !");
+        if(!arr3->isEqualWithoutConsideringStr(*pflSafe))
+          {
+            MCAuto<DataArrayInt> o2n2(pflSafe->checkAndPreparePermutation());
+            MCAuto<DataArrayInt> n2o2(o2n2->invertArrayO2N2N2O(o2n2->getNumberOfTuples()));
+            mzip->renumberNodes(n2o2->begin(),n2o2->getNumberOfTuples());
+            arr->setName("");
+            ret->setArray(arr);
+          }
+        ret->setMesh(mzip);
+      }
+    return ret;
+  }
 }
 
-MEDCoupling::MEDCouplingFieldDouble *MEDCoupling::ReadFieldGauss(const std::string& fileName, const std::string& meshName, int meshDimRelToMax, const std::string& fieldName, int iteration, int order)
+MEDCoupling::MEDCouplingField *MEDCoupling::ReadFieldCell(const std::string& fileName, const std::string& meshName, int meshDimRelToMax, const std::string& fieldName, int iteration, int order)
 {
-  MCAuto<MEDFileField1TS> ff(MEDFileField1TS::New(fileName,fieldName,iteration,order));
-  MCAuto<MEDFileMesh> mm(MEDFileMesh::New(fileName,meshName));
-  MCAuto<MEDCouplingMesh> m(mm->getMeshAtLevel(meshDimRelToMax,false));
-  MEDFileMesh *mPtr(mm);
-  MEDFileUMesh *muPtr=dynamic_cast<MEDFileUMesh *>(mPtr);
-  MCAuto<MEDCouplingFieldDouble> ret(ff->getFieldOnMeshAtLevel(ON_GAUSS_PT,m));
-  if(muPtr)
-    {
-      const DataArrayInt *num(muPtr->getNumberFieldAtLevel(meshDimRelToMax));
-      if(num)
-        ret->renumberCells(num->begin());
-    }
-  return ret.retn();
+  return ReadFieldCellLike(ON_CELLS,fileName,meshName,meshDimRelToMax,fieldName,iteration,order);
 }
 
-MEDCoupling::MEDCouplingFieldDouble *MEDCoupling::ReadFieldGaussNE(const std::string& fileName, const std::string& meshName, int meshDimRelToMax, const std::string& fieldName, int iteration, int order)
+MEDCoupling::MEDCouplingField *MEDCoupling::ReadFieldNode(const std::string& fileName, const std::string& meshName, int meshDimRelToMax, const std::string& fieldName, int iteration, int order)
 {
-  MCAuto<MEDFileField1TS> ff(MEDFileField1TS::New(fileName,fieldName,iteration,order));
-  MCAuto<MEDFileMesh> mm(MEDFileMesh::New(fileName,meshName));
-  MCAuto<MEDCouplingMesh> m(mm->getMeshAtLevel(meshDimRelToMax,false));
-  MEDFileMesh *mPtr(mm);
-  MEDFileUMesh *muPtr=dynamic_cast<MEDFileUMesh *>(mPtr);
-  MCAuto<MEDCouplingFieldDouble> ret(ff->getFieldOnMeshAtLevel(ON_GAUSS_NE,m));
-  if(muPtr)
-    {
-      const DataArrayInt *num(muPtr->getNumberFieldAtLevel(meshDimRelToMax));
-      if(num)
-        ret->renumberCells(num->begin());
-    }
-  return ret.retn();
+  MCAuto<MEDFileAnyTypeField1TS> f(MEDFileAnyTypeField1TS::New(fileName,fieldName,iteration,order));
+  {
+    MCAuto<MEDFileField1TS> f1(MEDCoupling::DynamicCast<MEDFileAnyTypeField1TS,MEDFileField1TS>(f));
+    if(f1.isNotNull())
+      {
+        MCAuto<MEDCoupling::MEDCouplingFieldDouble> ret(ReadFieldNodeT<double>(f1,fileName,meshName,meshDimRelToMax,fieldName,iteration,order));
+        return ret.retn();
+      }
+  }
+  {
+    MCAuto<MEDFileIntField1TS> f1(MEDCoupling::DynamicCast<MEDFileAnyTypeField1TS,MEDFileIntField1TS>(f));
+    if(f1.isNotNull())
+      {
+        MCAuto<MEDCoupling::MEDCouplingFieldInt> ret(ReadFieldNodeT<int>(f1,fileName,meshName,meshDimRelToMax,fieldName,iteration,order));
+        return ret.retn();
+      }
+  }
+  {
+    MCAuto<MEDFileFloatField1TS> f1(MEDCoupling::DynamicCast<MEDFileAnyTypeField1TS,MEDFileFloatField1TS>(f));
+    if(f1.isNotNull())
+      {
+        MCAuto<MEDCoupling::MEDCouplingFieldFloat> ret(ReadFieldNodeT<float>(f1,fileName,meshName,meshDimRelToMax,fieldName,iteration,order));
+        return ret.retn();
+      }
+  }
+  throw INTERP_KERNEL::Exception("MEDCoupling::ReadFieldNode : only FLOAT32, FLOAT64 and INT32 supported for the moment !");
+}
+
+MEDCoupling::MEDCouplingField *MEDCoupling::ReadFieldGauss(const std::string& fileName, const std::string& meshName, int meshDimRelToMax, const std::string& fieldName, int iteration, int order)
+{
+  return ReadFieldCellLike(ON_GAUSS_PT,fileName,meshName,meshDimRelToMax,fieldName,iteration,order);
+}
+
+MEDCoupling::MEDCouplingField *MEDCoupling::ReadFieldGaussNE(const std::string& fileName, const std::string& meshName, int meshDimRelToMax, const std::string& fieldName, int iteration, int order)
+{
+  return ReadFieldCellLike(ON_GAUSS_NE,fileName,meshName,meshDimRelToMax,fieldName,iteration,order);
 }
 
 void MEDCoupling::WriteMesh(const std::string& fileName, const MEDCoupling::MEDCouplingMesh *mesh, bool writeFromScratch)
@@ -1493,25 +1533,26 @@ void MEDCoupling::WriteUMeshesPartitionDep(const std::string& fileName, const st
 
 void MEDCoupling::WriteUMeshes(const std::string& fileName, const std::vector<const MEDCoupling::MEDCouplingUMesh *>& meshes, bool writeFromScratch)
 {
-  int mod=writeFromScratch?2:0;
+  int mod(writeFromScratch?2:0);
   MCAuto<MEDFileUMesh> m(MEDFileUMesh::New());
   AssignStaticWritePropertiesTo(*m);
   m->setMeshes(meshes,true);
   m->write(fileName,mod);
 }
 
-void MEDLoaderNS::writeFieldWithoutReadingAndMappingOfMeshInFile(const std::string& fileName, const MEDCoupling::MEDCouplingFieldDouble *f, bool writeFromScratch)
+template<class T>
+void MEDLoaderNS::writeFieldWithoutReadingAndMappingOfMeshInFile(const std::string& fileName, const typename MEDCoupling::Traits<T>::FieldType *f, bool writeFromScratch)
 {
-  MCAuto<MEDFileField1TS> ff(MEDFileField1TS::New());
+  MCAuto< typename MLFieldTraits<T>::F1TSType > ff(MLFieldTraits<T>::F1TSType::New());
   AssignStaticWritePropertiesTo(*ff);
-  MCAuto<MEDCouplingFieldDouble> f2(f->deepCopy());
+  MCAuto<typename MEDCoupling::Traits<T>::FieldType> f2(f->deepCopy());
   const MEDCouplingMesh *m(f2->getMesh());
   const MEDCouplingUMesh *um(dynamic_cast<const MEDCouplingUMesh *>(m));
   const MEDCoupling1GTUMesh *um2(dynamic_cast<const MEDCoupling1GTUMesh *>(m));
   const MEDCouplingCMesh *um3(dynamic_cast<const MEDCouplingCMesh *>(m));
   const MEDCouplingCurveLinearMesh *um4(dynamic_cast<const MEDCouplingCurveLinearMesh *>(m));
   MCAuto<MEDFileMesh> mm;
-  int mod=writeFromScratch?2:0;
+  int mod(writeFromScratch?2:0);
   if(um)
     {
       MCAuto<MEDFileUMesh> mmu(MEDFileUMesh::New());
@@ -1553,12 +1594,13 @@ void MEDLoaderNS::writeFieldWithoutReadingAndMappingOfMeshInFile(const std::stri
   ff->write(fileName,0);
 }
 
-void MEDCoupling::WriteField(const std::string& fileName, const MEDCoupling::MEDCouplingFieldDouble *f, bool writeFromScratch)
+template<class T>
+void WriteFieldT(const std::string& fileName, const typename MEDCoupling::Traits<T>::FieldType *f, bool writeFromScratch)
 {
   if(!f)
     throw INTERP_KERNEL::Exception("WriteField : input field is NULL !");
   f->checkConsistencyLight();
-  int status=MEDLoaderBase::getStatusOfFile(fileName);
+  int status(MEDLoaderBase::getStatusOfFile(fileName));
   if(status!=MEDLoaderBase::EXIST_RW && status!=MEDLoaderBase::NOT_EXIST)
     {
       std::ostringstream oss; oss << "File with name \'" << fileName << "\' has not valid permissions !";
@@ -1566,44 +1608,47 @@ void MEDCoupling::WriteField(const std::string& fileName, const MEDCoupling::MED
     }
   if(writeFromScratch || (!writeFromScratch && status==MEDLoaderBase::NOT_EXIST))
     {
-      MEDLoaderNS::writeFieldWithoutReadingAndMappingOfMeshInFile(fileName,f,true);
+      MEDLoaderNS::writeFieldWithoutReadingAndMappingOfMeshInFile<T>(fileName,f,true);
     }
   else
     {
-      std::vector<std::string> meshNames=GetMeshNames(fileName);
+      std::vector<std::string> meshNames(GetMeshNames(fileName));
       if(!f->getMesh())
         throw INTERP_KERNEL::Exception("WriteField : trying to write a field with no mesh !");
       std::string fileNameCpp(f->getMesh()->getName());
       if(std::find(meshNames.begin(),meshNames.end(),fileNameCpp)==meshNames.end())
-        MEDLoaderNS::writeFieldWithoutReadingAndMappingOfMeshInFile(fileName,f,false);
+        MEDLoaderNS::writeFieldWithoutReadingAndMappingOfMeshInFile<T>(fileName,f,false);
       else
         {
           MCAuto<MEDFileMesh> mm(MEDFileMesh::New(fileName,f->getMesh()->getName().c_str()));
           AssignStaticWritePropertiesTo(*mm);
           const MEDFileMesh *mmPtr(mm);
-          const MEDFileUMesh *mmuPtr=dynamic_cast<const MEDFileUMesh *>(mmPtr);
+          const MEDFileUMesh *mmuPtr(dynamic_cast<const MEDFileUMesh *>(mmPtr));
           if(!mmuPtr)
             throw INTERP_KERNEL::Exception("WriteField : only umeshes are supported now !");
-          MCAuto<MEDCouplingFieldDouble> f2(f->deepCopy());
-          MEDCouplingUMesh *m=dynamic_cast<MEDCouplingUMesh *>(const_cast<MEDCouplingMesh *>(f2->getMesh()));
+          MCAuto< typename MEDCoupling::Traits<T>::FieldType > f2(f->deepCopy());
+          MEDCouplingUMesh *m(dynamic_cast<MEDCouplingUMesh *>(const_cast<MEDCouplingMesh *>(f2->getMesh())));
           if(!m)
             throw INTERP_KERNEL::Exception("WriteField : only umesh in input field supported !");
-          MCAuto<DataArrayInt> o2n=m->getRenumArrForMEDFileFrmt();
+          MCAuto<DataArrayInt> o2n(m->getRenumArrForMEDFileFrmt());
           f2->renumberCells(o2n->begin(),false);
           m=static_cast<MEDCouplingUMesh *>(const_cast<MEDCouplingMesh *>(f2->getMesh()));
-          MCAuto<MEDCouplingUMesh> mread=mmuPtr->getMeshAtLevel(m->getMeshDimension()-mm->getMeshDimension());
+          MCAuto<MEDCouplingUMesh> mread(mmuPtr->getMeshAtLevel(m->getMeshDimension()-mm->getMeshDimension()));
           if(f2->getTypeOfField()!=ON_NODES)
             {
-              m->tryToShareSameCoordsPermute(*mread,_EPS_FOR_NODE_COMP);
-              DataArrayInt *part=0;
-              bool b=mread->areCellsIncludedIn(m,_COMP_FOR_CELL,part);
+              if(!m->getCoords()->isEqualWithoutConsideringStr(*mread->getCoords(),_EPS_FOR_NODE_COMP))
+                m->tryToShareSameCoordsPermute(*mread,_EPS_FOR_NODE_COMP);
+              else
+                mread->setCoords(m->getCoords());
+              DataArrayInt *part(NULL);
+              bool b(mread->areCellsIncludedIn(m,_COMP_FOR_CELL,part));
               MCAuto<DataArrayInt> partSafe(part);
               if(!b)
                 {
                   std::ostringstream oss; oss << "WriteField : The file \""<< fileName << "\" already contains a mesh named \""<< f->getMesh()->getName() << "\" and this mesh in the file is not compatible (a subpart) with the mesh you intend to write ! This is maybe due to a too strict policy ! Try with to lease it by calling SetCompPolicyForCell !";
                   throw INTERP_KERNEL::Exception(oss.str().c_str());
                 }
-              MCAuto<MEDFileField1TS> f1ts(MEDFileField1TS::New());
+              MCAuto< typename MLFieldTraits<T>::F1TSType > f1ts(MLFieldTraits<T>::F1TSType::New());
               AssignStaticWritePropertiesTo(*f1ts);
               if(part->isIota(mread->getNumberOfCells()))
                 f1ts->setFieldNoProfileSBT(f2);
@@ -1617,15 +1662,15 @@ void MEDCoupling::WriteField(const std::string& fileName, const MEDCoupling::MED
             }
           else
             {
-              DataArrayInt *part=0;
-              bool b=mread->getCoords()->areIncludedInMe(m->getCoords(),_EPS_FOR_NODE_COMP,part);
+              DataArrayInt *part(NULL);
+              bool b(mread->getCoords()->areIncludedInMe(m->getCoords(),_EPS_FOR_NODE_COMP,part));
               MCAuto<DataArrayInt> partSafe(part);
               if(!b)
                 {
                   std::ostringstream oss; oss << "WriteField : The file \""<< fileName << "\" already contains a mesh named \""<< f->getMesh()->getName() << "\" and this mesh in the file is not compatible (a subpart regarding nodes) with the mesh you intend to write ! This is maybe due to a too strict epsilon ! Try with to lease it by calling SetEpsilonForNodeComp !";
                   throw INTERP_KERNEL::Exception(oss.str().c_str());
                 }
-              MCAuto<MEDFileField1TS> f1ts(MEDFileField1TS::New());
+              MCAuto< typename MLFieldTraits<T>::F1TSType > f1ts(MLFieldTraits<T>::F1TSType::New());
               AssignStaticWritePropertiesTo(*f1ts);
               if(part->isIota(mread->getNumberOfNodes()))
                 f1ts->setFieldNoProfileSBT(f2);
@@ -1640,29 +1685,61 @@ void MEDCoupling::WriteField(const std::string& fileName, const MEDCoupling::MED
     }
 }
 
-void MEDCoupling::WriteFieldDep(const std::string& fileName, const MEDCoupling::MEDCouplingFieldDouble *f, bool writeFromScratch)
+void MEDCoupling::WriteField(const std::string& fileName, const MEDCoupling::MEDCouplingField *f, bool writeFromScratch)
+{
+  if(!f)
+    throw INTERP_KERNEL::Exception("WriteField : input field is null !");
+  {
+    const MEDCoupling::MEDCouplingFieldDouble *f1(dynamic_cast<const MEDCoupling::MEDCouplingFieldDouble *>(f));
+    if(f1)
+      {
+        WriteFieldT<double>(fileName,f1,writeFromScratch);
+        return ;
+      }
+  }
+  {
+    const MEDCoupling::MEDCouplingFieldInt *f1(dynamic_cast<const MEDCoupling::MEDCouplingFieldInt *>(f));
+    if(f1)
+      {
+        WriteFieldT<int>(fileName,f1,writeFromScratch);
+        return ;
+      }
+  }
+  {
+    const MEDCoupling::MEDCouplingFieldFloat *f1(dynamic_cast<const MEDCoupling::MEDCouplingFieldFloat *>(f));
+    if(f1)
+      {
+        WriteFieldT<float>(fileName,f1,writeFromScratch);
+        return ;
+      }
+  }
+  throw INTERP_KERNEL::Exception("WriteField : input field is not in FLOAT32, FLOAT64, INT32 !");
+}
+
+void MEDCoupling::WriteFieldDep(const std::string& fileName, const MEDCoupling::MEDCouplingField *f, bool writeFromScratch)
 {
   WriteField(fileName,f,writeFromScratch);
 }
 
-void MEDCoupling::WriteFieldUsingAlreadyWrittenMesh(const std::string& fileName, const MEDCoupling::MEDCouplingFieldDouble *f)
+template<class T>
+void WriteFieldUsingAlreadyWrittenMeshT(const std::string& fileName, const typename MEDCoupling::Traits<T>::FieldType *f)
 {
   if(!f)
-    throw INTERP_KERNEL::Exception("WriteFieldUsingAlreadyWrittenMesh : input field is null !");
+    throw INTERP_KERNEL::Exception("WriteFieldUsingAlreadyWrittenMeshT : input field is null !");
   f->checkConsistencyLight();
-  int status=MEDLoaderBase::getStatusOfFile(fileName);
+  int status(MEDLoaderBase::getStatusOfFile(fileName));
   if(status!=MEDLoaderBase::EXIST_RW)
     {
       std::ostringstream oss; oss << "File with name \'" << fileName << "\' has not valid permissions or not exists !";
       throw INTERP_KERNEL::Exception(oss.str().c_str());
     }
-  MCAuto<MEDFileField1TS> f1ts(MEDFileField1TS::New());
+  MCAuto< typename MLFieldTraits<T>::F1TSType > f1ts(MLFieldTraits<T>::F1TSType::New());
   AssignStaticWritePropertiesTo(*f1ts);
   MEDCouplingUMesh *m(dynamic_cast<MEDCouplingUMesh *>(const_cast<MEDCouplingMesh *>(f->getMesh())));
   if(m)
     {
       MCAuto<DataArrayInt> o2n(m->getRenumArrForMEDFileFrmt());
-      MCAuto<MEDCouplingFieldDouble> f2(f->deepCopy());
+      MCAuto< typename MEDCoupling::Traits<T>::FieldType > f2(f->deepCopy());
       f2->renumberCells(o2n->begin(),false);
       f1ts->setFieldNoProfileSBT(f2);
     }
@@ -1670,3 +1747,28 @@ void MEDCoupling::WriteFieldUsingAlreadyWrittenMesh(const std::string& fileName,
     f1ts->setFieldNoProfileSBT(f);
   f1ts->write(fileName,0);
 }
+
+void MEDCoupling::WriteFieldUsingAlreadyWrittenMesh(const std::string& fileName, const MEDCoupling::MEDCouplingField *f)
+{
+  if(!f)
+    throw INTERP_KERNEL::Exception("WriteFieldUsingAlreadyWrittenMesh : input field is null !");
+  {
+    const MEDCoupling::MEDCouplingFieldDouble *f1(dynamic_cast<const MEDCoupling::MEDCouplingFieldDouble *>(f));
+    if(f1)
+      WriteFieldUsingAlreadyWrittenMeshT<double>(fileName,f1);
+    return ;
+  }
+  {
+    const MEDCoupling::MEDCouplingFieldInt *f1(dynamic_cast<const MEDCoupling::MEDCouplingFieldInt *>(f));
+    if(f1)
+      WriteFieldUsingAlreadyWrittenMeshT<int>(fileName,f1);
+    return ;
+  }
+  {
+    const MEDCoupling::MEDCouplingFieldFloat *f1(dynamic_cast<const MEDCoupling::MEDCouplingFieldFloat *>(f));
+    if(f1)
+      WriteFieldUsingAlreadyWrittenMeshT<float>(fileName,f1);
+    return ;
+  }
+  throw INTERP_KERNEL::Exception("WriteFieldUsingAlreadyWrittenMesh : input field is not in FLOAT32, FLOAT64, INT32 !");
+}