Index: XIOS/dev/XIOS_DEV_CMIP6/src/node/axis.cpp
===================================================================
--- XIOS/dev/XIOS_DEV_CMIP6/src/node/axis.cpp	(revision 1212)
+++ XIOS/dev/XIOS_DEV_CMIP6/src/node/axis.cpp	(revision 1215)
@@ -310,4 +310,10 @@
      if (zoom_begin.isEmpty()) zoom_begin.setValue(begin);
    }
+
+    size_t CAxis::getGlobalWrittenSize(void)
+    {
+      if (zoomByIndex()) return  zoom_index.numElements();
+      else return global_zoom_n ;
+    }
 
    /*!
Index: XIOS/dev/XIOS_DEV_CMIP6/src/node/axis.hpp
===================================================================
--- XIOS/dev/XIOS_DEV_CMIP6/src/node/axis.hpp	(revision 1212)
+++ XIOS/dev/XIOS_DEV_CMIP6/src/node/axis.hpp	(revision 1215)
@@ -111,4 +111,5 @@
 
          void checkEligibilityForCompressedOutput();
+         size_t getGlobalWrittenSize(void) ;
 
          void computeWrittenIndex();
Index: XIOS/dev/XIOS_DEV_CMIP6/src/node/context.cpp
===================================================================
--- XIOS/dev/XIOS_DEV_CMIP6/src/node/context.cpp	(revision 1212)
+++ XIOS/dev/XIOS_DEV_CMIP6/src/node/context.cpp	(revision 1215)
@@ -16,5 +16,5 @@
 #include "timer.hpp"
 #include "memtrack.hpp"
-
+#include <limits>
 #include "server.hpp"
 
@@ -822,4 +822,6 @@
    void CContext::distributeFiles(void)
    {
+     double eps=std::numeric_limits<double>::epsilon()*10 ;
+     
      // If primary server
      if (hasServer && hasClient)
@@ -836,33 +838,21 @@
        // (2) Estimate the data volume for each file
        int size = this->enabledWriteModeFiles.size();
-       std::vector<std::pair<StdSize, CFile*> > dataSizeMap;
-       StdSize dataPerPool = 0;
+       std::vector<std::pair<double, CFile*> > dataSizeMap;
+       double dataPerPool = 0;
+       int nfield=0 ;
        for (size_t i = 0; i < size; ++i)
        {
          CFile* file = this->enabledWriteModeFiles[i];
-         StdSize dataSize;
+         StdSize dataSize=0;
          std::vector<CField*> enabledFields = file->getEnabledFields();
          size_t numEnabledFields = enabledFields.size();
-         for (size_t j = 0; j < numEnabledFields; ++j)
-         {
-           const std::vector<std::map<int, StdSize> > mapSize = enabledFields[j]->getGridDataBufferSize();
-           for (size_t c = 0; c < mapSize.size(); ++c)
-           {
-             std::map<int, StdSize>::const_iterator it = mapSize[c].begin(), itE = mapSize[c].end();
-             for (; it != itE; ++it)
-             {
-               dataSize += it->second;
-             }
-           }
-         }
-         CDuration outFreq = file->output_freq.getValue();
-         if (outFreq.timestep != 0.0)
-         {
-           outFreq = calendar->getTimeStep();
-         }
-         double outFreqSec = outFreq.second + 60.*(outFreq.minute + 60.*(outFreq.hour + 24.*(outFreq.day + outFreq.year*365.25) ) );
-         dataSize /= outFreqSec;
-         dataSizeMap.push_back(make_pair(dataSize,file));
-         dataPerPool += dataSize;
+         for (size_t j = 0; j < numEnabledFields; ++j) dataSize += enabledFields[j]->getGlobalWrittenSize() ;
+
+         double outFreqSec = (Time)(calendar->getCurrentDate()+file->output_freq)-(Time)(calendar->getCurrentDate()) ;
+         double dataSizeSec= dataSize/ outFreqSec;
+         nfield++ ;
+// add epsilon*nField to dataSizeSec in order to  preserve reproductive ordering when sorting
+         dataSizeMap.push_back(make_pair(dataSizeSec + dataSizeSec * eps * nfield , file));
+         dataPerPool += dataSizeSec;
        }
        dataPerPool /= nbPools;
@@ -870,30 +860,24 @@
 
        // (3) Assign contextClient to each enabled file
-       std::vector<StdSize> poolData(nbPools);
-       int lazyPool = 0;
+
+       std::multimap<double,int> poolDataSize ;
+// multimap is not garanty to preserve stable sorting in c++98 but it seems it does for c++11
+
+       int j;
+       double dataSize ;
+       for (j = 0 ; j < nbPools ; ++j) poolDataSize.insert(std::pair<double,int>(0.,j)) ;  
+              
        for (int i = dataSizeMap.size()-1; i >= 0; --i)
        {
-         bool contextSet = false;
-         for (int j = 0; j < nbPools; ++j)
-         {
-           if (!contextSet)
-           {
-             if (dataSizeMap[i].first + poolData[j] < dataPerPool)
-             {
-               dataSizeMap[i].second->setContextClient(clientPrimServer[j]);
-               poolData[j] += dataSizeMap[i].first;
-               contextSet = true;
-             }
-           }
-           if (poolData[j] < poolData[lazyPool])
-             lazyPool = j;
-         }
-         if (!contextSet)
-         {
-           dataSizeMap[i].second->setContextClient(clientPrimServer[lazyPool]);
-           poolData[lazyPool] += dataSizeMap[i].first;
-         }
-       }
-
+         dataSize=(*poolDataSize.begin()).first ;
+         j=(*poolDataSize.begin()).second ;
+         dataSizeMap[i].second->setContextClient(clientPrimServer[j]);
+         dataSize+=dataSizeMap[i].first;
+         poolDataSize.erase(poolDataSize.begin()) ;
+         poolDataSize.insert(std::pair<double,int>(dataSize,j)) ; 
+       }
+
+       for (std::multimap<double,int>:: iterator it=poolDataSize.begin() ; it!=poolDataSize.end(); ++it) info(30)<<"Load Balancing for servers (perfect=1) : "<<it->second<<" :  ratio "<<it->first*1./dataPerPool<<endl ;
+ 
        for (int i = 0; i < this->enabledReadModeFiles.size(); ++i)
          enabledReadModeFiles[i]->setContextClient(client);
Index: XIOS/dev/XIOS_DEV_CMIP6/src/node/domain.cpp
===================================================================
--- XIOS/dev/XIOS_DEV_CMIP6/src/node/domain.cpp	(revision 1212)
+++ XIOS/dev/XIOS_DEV_CMIP6/src/node/domain.cpp	(revision 1215)
@@ -876,4 +876,8 @@
    }
 
+   size_t CDomain::getGlobalWrittenSize(void)
+   {
+      return global_zoom_ni*global_zoom_nj ;
+   }
    //----------------------------------------------------------------
 
Index: XIOS/dev/XIOS_DEV_CMIP6/src/node/domain.hpp
===================================================================
--- XIOS/dev/XIOS_DEV_CMIP6/src/node/domain.hpp	(revision 1212)
+++ XIOS/dev/XIOS_DEV_CMIP6/src/node/domain.hpp	(revision 1215)
@@ -153,5 +153,5 @@
          void recvArea(std::map<int, CBufferIn*>& rankBuffers);         
          void recvDataIndex(std::map<int, CBufferIn*>& rankBuffers);
-
+         size_t getGlobalWrittenSize() ;
          /// Destructeur ///
          virtual ~CDomain(void);
Index: XIOS/dev/XIOS_DEV_CMIP6/src/node/field.cpp
===================================================================
--- XIOS/dev/XIOS_DEV_CMIP6/src/node/field.cpp	(revision 1212)
+++ XIOS/dev/XIOS_DEV_CMIP6/src/node/field.cpp	(revision 1215)
@@ -796,4 +796,9 @@
    }
 
+   size_t CField::getGlobalWrittenSize()
+   {
+     return grid->getGlobalWrittenSize();
+   }
+
    //----------------------------------------------------------------
 
Index: XIOS/dev/XIOS_DEV_CMIP6/src/node/field.hpp
===================================================================
--- XIOS/dev/XIOS_DEV_CMIP6/src/node/field.hpp	(revision 1212)
+++ XIOS/dev/XIOS_DEV_CMIP6/src/node/field.hpp	(revision 1215)
@@ -127,4 +127,7 @@
 
          void buildFilterGraph(CGarbageCollector& gc, bool enableOutput);
+         size_t getGlobalWrittenSize(void) ;
+         
+         
          boost::shared_ptr<COutputPin> getFieldReference(CGarbageCollector& gc);
          boost::shared_ptr<COutputPin> getSelfReference(CGarbageCollector& gc);
Index: XIOS/dev/XIOS_DEV_CMIP6/src/node/grid.cpp
===================================================================
--- XIOS/dev/XIOS_DEV_CMIP6/src/node/grid.cpp	(revision 1212)
+++ XIOS/dev/XIOS_DEV_CMIP6/src/node/grid.cpp	(revision 1215)
@@ -170,4 +170,5 @@
          dataSizes[p].insert(std::make_pair(rank, extraSize + CArray<double,1>::size(count)));
        }
+       
      }
 
@@ -175,4 +176,16 @@
    }
 
+   size_t CGrid::getGlobalWrittenSize(void)
+   {
+	 std::vector<CDomain*> domainP = this->getDomains();
+     std::vector<CAxis*> axisP = this->getAxis();
+     
+     size_t globalGridSize=1 ;
+     for (std::vector<CDomain*>::iterator it=domainP.begin(); it!=domainP.end();++it) globalGridSize*=(*it)->getGlobalWrittenSize() ;
+     for (std::vector<CAxis*>::iterator it=axisP.begin(); it!=axisP.end();++it) globalGridSize*=(*it)->getGlobalWrittenSize() ;
+     return globalGridSize ;
+   }
+   
+   
    void CGrid::checkAttributesAfterTransformation()
    {
Index: XIOS/dev/XIOS_DEV_CMIP6/src/node/grid.hpp
===================================================================
--- XIOS/dev/XIOS_DEV_CMIP6/src/node/grid.hpp	(revision 1212)
+++ XIOS/dev/XIOS_DEV_CMIP6/src/node/grid.hpp	(revision 1215)
@@ -199,5 +199,5 @@
          std::map<CGrid*, std::pair<bool,StdString> >& getTransGridSource();
          bool hasTransform();
-
+         size_t getGlobalWrittenSize(void) ;
       public:
 
