Salome HOME
Structure element inside Gauss Points management
[tools/medcoupling.git] / src / MEDLoader / MEDLoader.cxx
index 1c2dcb50170d7884a9b4e2e7698046d3af45065a..61ee8bbc2bdecc8b014297731a3d2129d0c3a91f 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2007-2015  CEA/DEN, EDF R&D
+// Copyright (C) 2007-2016  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
@@ -21,6 +21,7 @@
 #include "MEDLoader.hxx"
 #include "MEDLoaderBase.hxx"
 #include "MEDFileUtilities.hxx"
+#include "MEDLoaderNS.hxx"
 #include "MEDFileSafeCaller.txx"
 #include "MEDFileMesh.hxx"
 #include "MEDFileField.hxx"
@@ -139,20 +140,6 @@ int _TOO_LONG_STR=0;
 
 using namespace MEDCoupling;
 
-/// @cond INTERNAL
-
-namespace MEDLoaderNS
-{
-  int readUMeshDimFromFile(const std::string& fileName, const std::string& meshName, std::vector<int>& possibilities);
-  void dispatchElems(int nbOfElemCell, int nbOfElemFace, int& nbOfElem, med_entity_type& whichEntity);
-  void writeFieldWithoutReadingAndMappingOfMeshInFile(const std::string& fileName, const MEDCoupling::MEDCouplingFieldDouble *f, bool writeFromScratch);
-  med_int getIdFromMeshName(med_idt fid, const std::string& meshName, std::string& trueMeshName);
-  std::vector<std::string> getMeshNamesFid(med_idt fid);
-}
-
-/// @endcond
-
-
 /// @cond INTERNAL
 
 /*!
@@ -163,7 +150,7 @@ namespace MEDLoaderNS
 int MEDLoaderNS::readUMeshDimFromFile(const std::string& fileName, const std::string& meshName, std::vector<int>& possibilities)
 {
   possibilities.clear();
-  MEDFileUtilities::AutoFid fid=MEDfileOpen(fileName.c_str(),MED_ACC_RDONLY);
+  MEDFileUtilities::AutoFid fid(MEDCoupling::OpenMEDFileForRead(fileName));
   int ret;
   std::set<int> poss;
   char nommaa[MED_NAME_SIZE+1];
@@ -302,14 +289,55 @@ bool MEDCoupling::HasXDR()
 
 std::string MEDCoupling::MEDFileVersionStr()
 {
-  return std::string(MED_VERSION_STR);
+  const int SZ=20;
+  char buf[SZ];
+  std::fill(buf,buf+SZ,'\0');
+  const char START_EXPECTED[]="MED-";
+  med_err ret(MEDlibraryStrVersion(buf));
+  if(ret!=0)
+    throw INTERP_KERNEL::Exception("MEDFileVersionStr : fail to find version of MED file ! It looks very bad !");
+  std::string zeRet(buf);
+  std::size_t pos(zeRet.find(START_EXPECTED,0));
+  if(pos!=0)
+    {
+      std::ostringstream oss; oss << "MEDFileVersionStr : internal error ! The MEDFile returned version (\"" << zeRet << "\") has not the right pattern !";
+      throw INTERP_KERNEL::Exception(oss.str());
+    }
+  return zeRet.substr(sizeof(START_EXPECTED)-1,std::string::npos);
+}
+
+std::string MEDCoupling::MEDFileVersionOfFileStr(const std::string& fileName)
+{
+#if MED_NUM_MAJEUR>=3 && MED_NUM_MINEUR>=2 && MED_NUM_RELEASE>=1
+  MEDFileUtilities::AutoFid fid(MEDCoupling::OpenMEDFileForRead(fileName));
+  const int SZ=20;
+  const char START_EXPECTED[]="MED-";
+  char buf[SZ];
+  std::fill(buf,buf+SZ,'\0');
+  MEDFILESAFECALLERRD0(MEDfileStrVersionRd,(fid,buf));
+  std::string ret(buf);
+  std::size_t pos(ret.find(START_EXPECTED,0));
+  if(pos!=0)
+    {
+      std::ostringstream oss; oss << "MEDFileVersionOfFileStr : internal error ! The MEDFile returned version (\"" << ret << "\") has not the right pattern !";
+      throw INTERP_KERNEL::Exception(oss.str());
+    }
+  return ret.substr(sizeof(START_EXPECTED)-1,std::string::npos);
+#else
+  std::ostringstream oss; oss << "MEDFileVersionOfFileStr : is implemented with MEDFile " << MEDFileVersionStr() << " ! If you need this feature please use version >= 3.2.1.";
+  throw INTERP_KERNEL::Exception(oss.str());
+#endif
 }
 
 void MEDCoupling::MEDFileVersion(int& major, int& minor, int& release)
 {
-  major=MED_NUM_MAJEUR;
-  minor=MED_NUM_MINEUR;
-  release=MED_NUM_RELEASE;
+  med_int majj,minn,rell;
+  med_err ret(MEDlibraryNumVersion(&majj,&minn,&rell));
+  if(ret!=0)
+    throw INTERP_KERNEL::Exception("MEDFileVersion : fail to call MEDlibraryNumVersion ! It looks very bad !");
+  major=majj;
+  minor=minn;
+  release=rell;
 }
 
 /*!
@@ -347,8 +375,7 @@ void MEDCoupling::SetTooLongStrPolicy(int val)
  */
 std::vector< std::vector< std::pair<INTERP_KERNEL::NormalizedCellType,int> > > MEDCoupling::GetUMeshGlobalInfo(const std::string& fileName, const std::string& meshName, int &meshDim, int& spaceDim, int& numberOfNodes)
 {
-  CheckFileForRead(fileName);
-  MEDFileUtilities::AutoFid fid=MEDfileOpen(fileName.c_str(),MED_ACC_RDONLY);
+  MEDFileUtilities::AutoFid fid(MEDCoupling::OpenMEDFileForRead(fileName));
   std::set<int> poss;
   char nommaa[MED_NAME_SIZE+1];
   char maillage_description[MED_COMMENT_SIZE+1];
@@ -410,16 +437,13 @@ void MEDCoupling::CheckFileForRead(const std::string& fileName)
 
 std::vector<std::string> MEDCoupling::GetMeshNames(const std::string& fileName)
 {
-  MEDCoupling::CheckFileForRead(fileName);
-  MEDFileUtilities::AutoFid fid=MEDfileOpen(fileName.c_str(),MED_ACC_RDONLY);
-  std::vector<std::string> ret=MEDLoaderNS::getMeshNamesFid(fid);
-  return ret;
+  MEDFileUtilities::AutoFid fid(MEDCoupling::OpenMEDFileForRead(fileName));
+  return MEDLoaderNS::getMeshNamesFid(fid);
 }
 
 std::vector< std::pair<std::string,std::string> > MEDCoupling::GetComponentsNamesOfField(const std::string& fileName, const std::string& fieldName)
 {
-  MEDCoupling::CheckFileForRead(fileName);
-  MEDFileUtilities::AutoFid fid=MEDfileOpen(fileName.c_str(),MED_ACC_RDONLY);
+  MEDFileUtilities::AutoFid fid(MEDCoupling::OpenMEDFileForRead(fileName));
   med_int nbFields(MEDnField(fid));
   std::vector<std::string> fields(nbFields);
   med_field_type typcha;
@@ -454,10 +478,9 @@ std::vector< std::pair<std::string,std::string> > MEDCoupling::GetComponentsName
 
 std::vector<std::string> MEDCoupling::GetMeshNamesOnField(const std::string& fileName, const std::string& fieldName)
 {
-  MEDCoupling::CheckFileForRead(fileName);
   std::vector<std::string> ret;
   //
-  MEDFileUtilities::AutoFid fid=MEDfileOpen(fileName.c_str(),MED_ACC_RDONLY);
+  MEDFileUtilities::AutoFid fid(MEDCoupling::OpenMEDFileForRead(fileName));
   med_int nbFields=MEDnField(fid);
   //
   med_field_type typcha;
@@ -483,8 +506,7 @@ std::vector<std::string> MEDCoupling::GetMeshNamesOnField(const std::string& fil
 
 std::vector<std::string> MEDCoupling::GetMeshFamiliesNames(const std::string& fileName, const std::string& meshName)
 {
-  MEDCoupling::CheckFileForRead(fileName);
-  MEDFileUtilities::AutoFid fid=MEDfileOpen(fileName.c_str(),MED_ACC_RDONLY);
+  MEDFileUtilities::AutoFid fid(MEDCoupling::OpenMEDFileForRead(fileName));
   med_int nfam=MEDnFamily(fid,meshName.c_str());
   std::vector<std::string> ret(nfam);
   char nomfam[MED_NAME_SIZE+1];
@@ -507,8 +529,7 @@ std::vector<std::string> MEDCoupling::GetMeshFamiliesNames(const std::string& fi
 
 std::vector<std::string> MEDCoupling::GetMeshFamiliesNamesOnGroup(const std::string& fileName, const std::string& meshName, const std::string& grpName)
 {
-  MEDCoupling::CheckFileForRead(fileName);
-  MEDFileUtilities::AutoFid fid=MEDfileOpen(fileName.c_str(),MED_ACC_RDONLY);
+  MEDFileUtilities::AutoFid fid(MEDCoupling::OpenMEDFileForRead(fileName));
   med_int nfam=MEDnFamily(fid,meshName.c_str());
   std::vector<std::string> ret;
   char nomfam[MED_NAME_SIZE+1];
@@ -535,9 +556,8 @@ std::vector<std::string> MEDCoupling::GetMeshFamiliesNamesOnGroup(const std::str
 
 std::vector<std::string> MEDCoupling::GetMeshGroupsNamesOnFamily(const std::string& fileName, const std::string& meshName, const std::string& famName)
 {
-  MEDCoupling::CheckFileForRead(fileName);
-  MEDFileUtilities::AutoFid fid=MEDfileOpen(fileName.c_str(),MED_ACC_RDONLY);
-  med_int nfam=MEDnFamily(fid,meshName.c_str());
+  MEDFileUtilities::AutoFid fid(MEDCoupling::OpenMEDFileForRead(fileName));
+  med_int nfam(MEDnFamily(fid,meshName.c_str()));
   std::vector<std::string> ret;
   char nomfam[MED_NAME_SIZE+1];
   med_int numfam;
@@ -572,8 +592,7 @@ std::vector<std::string> MEDCoupling::GetMeshGroupsNamesOnFamily(const std::stri
 
 std::vector<std::string> MEDCoupling::GetMeshGroupsNames(const std::string& fileName, const std::string& meshName)
 {
-  MEDCoupling::CheckFileForRead(fileName);
-  MEDFileUtilities::AutoFid fid=MEDfileOpen(fileName.c_str(),MED_ACC_RDONLY);
+  MEDFileUtilities::AutoFid fid(MEDCoupling::OpenMEDFileForRead(fileName));
   med_int nfam=MEDnFamily(fid,meshName.c_str());
   std::vector<std::string> ret;
   char nomfam[MED_NAME_SIZE+1];
@@ -632,9 +651,8 @@ std::vector<MEDCoupling::TypeOfField> MEDCoupling::GetTypesOfField(const std::st
 
 std::vector<std::string> MEDCoupling::GetAllFieldNames(const std::string& fileName)
 {
-  MEDCoupling::CheckFileForRead(fileName);
   std::vector<std::string> ret;
-  MEDFileUtilities::AutoFid fid=MEDfileOpen(fileName.c_str(),MED_ACC_RDONLY);
+  MEDFileUtilities::AutoFid fid(MEDCoupling::OpenMEDFileForRead(fileName));
   med_int nbFields=MEDnField(fid);
   med_field_type typcha;
   for(int i=0;i<nbFields;i++)
@@ -655,9 +673,8 @@ std::vector<std::string> MEDCoupling::GetAllFieldNames(const std::string& fileNa
 
 std::vector<std::string> MEDCoupling::GetAllFieldNamesOnMesh(const std::string& fileName, const std::string& meshName)
 {
-  MEDCoupling::CheckFileForRead(fileName);
   std::vector<std::string> ret;
-  MEDFileUtilities::AutoFid fid=MEDfileOpen(fileName.c_str(),MED_ACC_RDONLY);
+  MEDFileUtilities::AutoFid fid(MEDCoupling::OpenMEDFileForRead(fileName));
   med_int nbFields=MEDnField(fid);
   //
   med_field_type typcha;
@@ -700,9 +717,8 @@ std::vector<std::string> MEDCoupling::GetFieldNamesOnMesh(MEDCoupling::TypeOfFie
 
 std::vector<std::string> MEDCoupling::GetCellFieldNamesOnMesh(const std::string& fileName, const std::string& meshName)
 {
-  MEDCoupling::CheckFileForRead(fileName);
   std::vector<std::string> ret;
-  MEDFileUtilities::AutoFid fid=MEDfileOpen(fileName.c_str(),MED_ACC_RDONLY);
+  MEDFileUtilities::AutoFid fid(MEDCoupling::OpenMEDFileForRead(fileName));
   med_int nbFields=MEDnField(fid);
   //
   med_field_type typcha;
@@ -750,9 +766,8 @@ std::vector<std::string> MEDCoupling::GetCellFieldNamesOnMesh(const std::string&
 
 std::vector<std::string> MEDCoupling::GetNodeFieldNamesOnMesh(const std::string& fileName, const std::string& meshName)
 {
-  MEDCoupling::CheckFileForRead(fileName);
   std::vector<std::string> ret;
-  MEDFileUtilities::AutoFid fid=MEDfileOpen(fileName.c_str(),MED_ACC_RDONLY);
+  MEDFileUtilities::AutoFid fid(MEDCoupling::OpenMEDFileForRead(fileName));
   med_int nbFields=MEDnField(fid);
   char pflname[MED_NAME_SIZE+1]="";
   char locname[MED_NAME_SIZE+1]="";
@@ -791,9 +806,8 @@ std::vector<std::string> MEDCoupling::GetNodeFieldNamesOnMesh(const std::string&
 
 std::vector< std::pair< std::pair<int,int>, double> > MEDCoupling::GetAllFieldIterations(const std::string& fileName, const std::string& fieldName)
 {
-  MEDCoupling::CheckFileForRead(fileName);
   std::vector< std::pair< std::pair<int,int>, double > > ret;
-  MEDFileUtilities::AutoFid fid=MEDfileOpen(fileName.c_str(),MED_ACC_RDONLY);
+  MEDFileUtilities::AutoFid fid(MEDCoupling::OpenMEDFileForRead(fileName));
   med_int nbFields=MEDnField(fid);
   //
   med_field_type typcha;
@@ -834,8 +848,7 @@ std::vector< std::pair< std::pair<int,int>, double> > MEDCoupling::GetAllFieldIt
 
 double MEDCoupling::GetTimeAttachedOnFieldIteration(const std::string& fileName, const std::string& fieldName, int iteration, int order)
 {
-  MEDCoupling::CheckFileForRead(fileName);
-  MEDFileUtilities::AutoFid fid=MEDfileOpen(fileName.c_str(),MED_ACC_RDONLY);
+  MEDFileUtilities::AutoFid fid(MEDCoupling::OpenMEDFileForRead(fileName));
   med_int nbFields=MEDnField(fid);
   //
   med_field_type typcha;
@@ -896,10 +909,9 @@ std::vector< std::pair<int,int> > MEDCoupling::GetFieldIterations(MEDCoupling::T
 
 std::vector< std::pair<int,int> > MEDCoupling::GetCellFieldIterations(const std::string& fileName, const std::string& meshName, const std::string& fieldName)
 {
-  MEDCoupling::CheckFileForRead(fileName);
   std::string meshNameCpp(meshName);
   std::vector< std::pair<int,int> > ret;
-  MEDFileUtilities::AutoFid fid=MEDfileOpen(fileName.c_str(),MED_ACC_RDONLY);
+  MEDFileUtilities::AutoFid fid(MEDCoupling::OpenMEDFileForRead(fileName));
   med_int nbFields=MEDnField(fid);
   //
   med_field_type typcha;
@@ -968,10 +980,9 @@ std::vector< std::pair<int,int> > MEDCoupling::GetCellFieldIterations(const std:
 
 std::vector< std::pair<int,int> > MEDCoupling::GetNodeFieldIterations(const std::string& fileName, const std::string& meshName, const std::string& fieldName)
 {
-  MEDCoupling::CheckFileForRead(fileName);
   std::string meshNameCpp(meshName);
   std::vector< std::pair<int,int> > ret;
-  MEDFileUtilities::AutoFid fid=MEDfileOpen(fileName.c_str(),MED_ACC_RDONLY);
+  MEDFileUtilities::AutoFid fid(MEDCoupling::OpenMEDFileForRead(fileName));
   med_int nbFields=MEDnField(fid);
   //
   med_field_type typcha;