source: XIOS/dev/XIOS_DEV_CMIP6/src/io/nc4_data_output.cpp @ 1243

Last change on this file since 1243 was 1235, checked in by mhnguyen, 7 years ago

Fixing a bug on writting axis label

+) Axis label is correctly processed before being written
+) Do some code cleaning and add some comments

Test
+) On Curie
+) OK

  • Property copyright set to
    Software name : XIOS (Xml I/O Server)
    http://forge.ipsl.jussieu.fr/ioserver
    Creation date : January 2009
    Licence : CeCCIL version2
    see license file in root directory : Licence_CeCILL_V2-en.txt
    or http://www.cecill.info/licences/Licence_CeCILL_V2-en.html
    Holder : CEA/LSCE (Laboratoire des Sciences du CLimat et de l'Environnement)
    CNRS/IPSL (Institut Pierre Simon Laplace)
    Project Manager : Yann Meurdesoif
    yann.meurdesoif@cea.fr
  • Property svn:executable set to *
File size: 124.9 KB
RevLine 
[219]1#include "nc4_data_output.hpp"
2
3#include <boost/lexical_cast.hpp>
[352]4#include "attribute_template.hpp"
5#include "group_template.hpp"
[219]6
7#include "file.hpp"
8#include "calendar.hpp"
[278]9#include "context.hpp"
[300]10#include "context_server.hpp"
[498]11#include "netCdfException.hpp"
12#include "exception.hpp"
[1158]13#include "timer.hpp"
14#include "uuid.hpp"
[335]15namespace xios
[219]16{
[878]17      /// ////////////////////// Dfinitions ////////////////////// ///
[219]18      CNc4DataOutput::CNc4DataOutput
[1158]19         (CFile* file, const StdString & filename, bool exist)
[219]20            : SuperClass()
21            , SuperClassWriter(filename, exist)
22            , filename(filename)
[1158]23            , file(file),hasTimeInstant(false),hasTimeCentered(false), timeCounterType(none)
[219]24      {
[1158]25        SuperClass::type = MULTI_FILE;
[219]26      }
27
28      CNc4DataOutput::CNc4DataOutput
[1158]29         (CFile* file, const StdString & filename, bool exist, bool useClassicFormat, bool useCFConvention,
[924]30          MPI_Comm comm_file, bool multifile, bool isCollective, const StdString& timeCounterName)
[219]31            : SuperClass()
[878]32            , SuperClassWriter(filename, exist, useClassicFormat, useCFConvention, &comm_file, multifile, timeCounterName)
[379]33            , comm_file(comm_file)
[219]34            , filename(filename)
[335]35            , isCollective(isCollective)
[1158]36            , file(file),hasTimeInstant(false),hasTimeCentered(false), timeCounterType(none)
[219]37      {
[1158]38        SuperClass::type = (multifile) ? MULTI_FILE : ONE_FILE;
[219]39      }
40
41      CNc4DataOutput::~CNc4DataOutput(void)
[879]42    { /* Ne rien faire de plus */ }
[219]43
44      ///--------------------------------------------------------------
45
46      const StdString & CNc4DataOutput::getFileName(void) const
47      {
48         return (this->filename);
49      }
50
51      //---------------------------------------------------------------
52
[347]53      void CNc4DataOutput::writeDomain_(CDomain* domain)
[219]54      {
[1129]55        domain->computeWrittenIndex();
[881]56        if (domain->type == CDomain::type_attr::unstructured)
57        {
58          if (SuperClassWriter::useCFConvention)
59            writeUnstructuredDomain(domain) ;
60          else
61            writeUnstructuredDomainUgrid(domain) ;
62          return ;
63        }
[488]64
[347]65         CContext* context = CContext::getCurrent() ;
[300]66         CContextServer* server=context->server ;
[488]67
[219]68         if (domain->IsWritten(this->filename)) return;
69         domain->checkAttributes();
[488]70
71         if (domain->isEmpty())
[881]72           if (SuperClass::type==MULTI_FILE) return;
[219]73
74         std::vector<StdString> dim0, dim1;
[772]75         StdString domid = domain->getDomainOutputName();
[318]76         StdString appendDomid  = (singleDomain) ? "" : "_"+domid ;
[706]77         if (isWrittenDomain(domid)) return ;
[774]78         else setWrittenDomain(domid);
[1132]79       
80         int nvertex = (domain->nvertex.isEmpty()) ? 0 : domain->nvertex;
[449]81
[488]82
[1158]83        StdString dimXid, dimYid ;
84
85        nc_type typePrec ;
86        if (domain->prec.isEmpty()) typePrec =  NC_FLOAT ;
87        else if (domain->prec==4)  typePrec =  NC_FLOAT ;
88        else if (domain->prec==8)   typePrec =  NC_DOUBLE ;
89         
[664]90         bool isRegularDomain = (domain->type == CDomain::type_attr::rectilinear);
[449]91         switch (domain->type)
[433]92         {
[449]93           case CDomain::type_attr::curvilinear :
94             dimXid     = StdString("x").append(appendDomid);
95             dimYid     = StdString("y").append(appendDomid);
96             break ;
[664]97           case CDomain::type_attr::rectilinear :
[449]98             dimXid     = StdString("lon").append(appendDomid);
99             dimYid     = StdString("lat").append(appendDomid);
100             break;
[488]101         }
102
[617]103         StdString dimVertId = StdString("nvertex").append(appendDomid);
104
[449]105         string lonid,latid,bounds_lonid,bounds_latid ;
[611]106         string areaId = "area" + appendDomid;
[391]107/*
[300]108         StdString lonid_loc = (server->intraCommSize > 1)
[318]109                             ? StdString("lon").append(appendDomid).append("_local")
[278]110                             : lonid;
[300]111         StdString latid_loc = (server->intraCommSize > 1)
[318]112                             ? StdString("lat").append(appendDomid).append("_local")
[278]113                             : latid;
[391]114*/
[219]115
[1129]116         CArray<size_t, 1>& indexToWrite = domain->localIndexToWriteOnServer;
117         int nbWritten = indexToWrite.numElements();
118         CArray<double,1> writtenLat, writtenLon;
119         CArray<double,2> writtenBndsLat, writtenBndsLon;
120         CArray<double,1> writtenArea;
121
122         if (domain->hasLonLat)
123         {
124           writtenLat.resize(nbWritten);
125           writtenLon.resize(nbWritten);
126           for (int idx = 0; idx < nbWritten; ++idx)
127           {
128              writtenLat(idx) = domain->latvalue(indexToWrite(idx));
129              writtenLon(idx) = domain->lonvalue(indexToWrite(idx));
130           }
131         
132
133           if (domain->hasBounds)
134           {         
135             int nvertex = domain->nvertex, idx;
136             writtenBndsLat.resize(nvertex, nbWritten);
137             writtenBndsLon.resize(nvertex, nbWritten);
138             CArray<double,2>& boundslat = domain->bounds_latvalue;
139             CArray<double,2>& boundslon = domain->bounds_lonvalue;   
140             for (idx = 0; idx < nbWritten; ++idx)
141               for (int nv = 0; nv < nvertex; ++nv)
142               {
143                 writtenBndsLat(nv, idx) = boundslat(nv, int(indexToWrite(idx)));
144                 writtenBndsLon(nv, idx) = boundslon(nv, int(indexToWrite(idx)));
145               }
146           }
147         }
148
149         if (domain->hasArea)
150         {
151           writtenArea.resize(nbWritten);           
152           for (int idx = 0; idx < nbWritten; ++idx)
153           {
154              writtenArea(idx) = domain->areavalue(indexToWrite(idx));                     
155           }
156         }
157
[498]158         try
[219]159         {
[498]160           switch (SuperClass::type)
161           {
162              case (MULTI_FILE) :
163              {
164  //               if (domain->isEmpty()) return;
[488]165
[498]166                 if (server->intraCommSize > 1)
167                 {
168  //                 SuperClassWriter::addDimension(lonid, domain->zoom_ni.getValue());
169  //                 SuperClassWriter::addDimension(latid, domain->zoom_nj.getValue());
170                 }
[286]171
[498]172                 switch (domain->type)
173                 {
174                   case CDomain::type_attr::curvilinear :
175                     dim0.push_back(dimYid); dim0.push_back(dimXid);
176                     lonid = StdString("nav_lon").append(appendDomid);
177                     latid = StdString("nav_lat").append(appendDomid);
178                     break ;
[664]179                   case CDomain::type_attr::rectilinear :
[498]180                     lonid = StdString("lon").append(appendDomid);
181                     latid = StdString("lat").append(appendDomid);
182                     dim0.push_back(dimYid);
183                     dim1.push_back(dimXid);
184                     break;
185                 }
[488]186
[617]187                 bounds_lonid = StdString("bounds_lon").append(appendDomid);
188                 bounds_latid = StdString("bounds_lat").append(appendDomid);
189
[1099]190                 SuperClassWriter::addDimension(dimXid, domain->zoom_ni);
191                 SuperClassWriter::addDimension(dimYid, domain->zoom_nj);
[488]192
[617]193                 if (domain->hasBounds)
194                   SuperClassWriter::addDimension(dimVertId, domain->nvertex);
195
[498]196                 if (server->intraCommSize > 1)
197                 {
[1099]198                   this->writeLocalAttributes(domain->zoom_ibegin,
199                                              domain->zoom_ni,
200                                              domain->zoom_jbegin,
201                                              domain->zoom_nj,
[616]202                                              appendDomid);
[488]203
[628]204                   if (singleDomain)
205                    this->writeLocalAttributes_IOIPSL(dimXid, dimYid,
[1099]206                                                      domain->zoom_ibegin,
207                                                      domain->zoom_ni,
208                                                      domain->zoom_jbegin,
209                                                      domain->zoom_nj,
[628]210                                                      domain->ni_glo,domain->nj_glo,
211                                                      server->intraCommRank,server->intraCommSize);
[449]212                 }
[488]213
[665]214                 if (domain->hasLonLat)
[498]215                 {
[665]216                   switch (domain->type)
217                   {
218                     case CDomain::type_attr::curvilinear :
[1158]219                       SuperClassWriter::addVariable(latid, typePrec, dim0);
220                       SuperClassWriter::addVariable(lonid, typePrec, dim0);
[665]221                       break ;
222                      case CDomain::type_attr::rectilinear :
[1158]223                        SuperClassWriter::addVariable(latid, typePrec, dim0);
224                        SuperClassWriter::addVariable(lonid, typePrec, dim1);
[665]225                        break ;
226                   }
[488]227
[665]228                   this->writeAxisAttributes(lonid, isRegularDomain ? "X" : "", "longitude", "Longitude", "degrees_east", domid);
229                   this->writeAxisAttributes(latid, isRegularDomain ? "Y" : "", "latitude", "Latitude", "degrees_north", domid);
[219]230
[665]231                   if (domain->hasBounds)
232                   {
233                     SuperClassWriter::addAttribute("bounds", bounds_lonid, &lonid);
234                     SuperClassWriter::addAttribute("bounds", bounds_latid, &latid);
[617]235
[665]236                     dim0.clear();
237                     dim0.push_back(dimYid);
238                     dim0.push_back(dimXid);
239                     dim0.push_back(dimVertId);
[1158]240                     SuperClassWriter::addVariable(bounds_lonid, typePrec, dim0);
241                     SuperClassWriter::addVariable(bounds_latid, typePrec, dim0);
[665]242                   }
[617]243                 }
244
[498]245                 dim0.clear();
[616]246                 dim0.push_back(dimYid);
[498]247                 dim0.push_back(dimXid);
[219]248
249
[498]250  // supress mask               if (server->intraCommSize > 1)
251  // supress mask               {
252  // supress mask                  SuperClassWriter::addVariable(maskid, NC_INT, dim0);
253  // supress mask
254  // supress mask                  this->writeMaskAttributes(maskid,
255  // supress mask                     domain->data_dim.getValue()/*,
256  // supress mask                     domain->data_ni.getValue(),
257  // supress mask                     domain->data_nj.getValue(),
258  // supress mask                     domain->data_ibegin.getValue(),
259  // supress mask                     domain->data_jbegin.getValue()*/);
260  // supress mask               }
[488]261
[498]262                 //SuperClassWriter::setDefaultValue(maskid, &dvm);
[219]263
[611]264                 if (domain->hasArea)
265                 {
[1158]266                   SuperClassWriter::addVariable(areaId, typePrec, dim0);
[614]267                   SuperClassWriter::addAttribute("standard_name", StdString("cell_area"), &areaId);
[611]268                   SuperClassWriter::addAttribute("units", StdString("m2"), &areaId);
269                 }
270
[498]271                 SuperClassWriter::definition_end();
[449]272
[665]273                 if (domain->hasLonLat)
[498]274                 {
[665]275                   switch (domain->type)
276                   {
[1129]277                     case CDomain::type_attr::curvilinear :                       
278                       SuperClassWriter::writeData(writtenLat, latid, isCollective, 0);
279                       SuperClassWriter::writeData(writtenLon, lonid, isCollective, 0);
[665]280                       break;
281                     case CDomain::type_attr::rectilinear :
[1129]282                       CArray<double,1> lat = writtenLat(Range(fromStart,toEnd,domain->zoom_ni)) ;
[665]283                       SuperClassWriter::writeData(CArray<double,1>(lat.copy()), latid, isCollective, 0);
[1129]284                       CArray<double,1> lon = writtenLon(Range(0,domain->zoom_ni-1)) ;
[665]285                       SuperClassWriter::writeData(CArray<double,1>(lon.copy()), lonid, isCollective, 0);
286                       break;
287                   }
[611]288
[665]289                   if (domain->hasBounds)
290                   {
[1129]291                     SuperClassWriter::writeData(writtenBndsLon, bounds_lonid, isCollective, 0);
292                     SuperClassWriter::writeData(writtenBndsLat, bounds_latid, isCollective, 0);
[665]293                   }
[617]294                 }
295
[611]296                 if (domain->hasArea)
[1129]297                 {
[1132]298                   SuperClassWriter::writeData(writtenArea, areaId, isCollective, 0);                   
[1129]299                 }
300
[498]301                 SuperClassWriter::definition_start();
[219]302
[498]303                 break;
304              }
305              case (ONE_FILE) :
306              {
[1099]307                 SuperClassWriter::addDimension(dimXid, domain->global_zoom_ni);
308                 SuperClassWriter::addDimension(dimYid, domain->global_zoom_nj);
[286]309
[617]310                 if (domain->hasBounds)
311                   SuperClassWriter::addDimension(dimVertId, domain->nvertex);
312
[665]313                 if (domain->hasLonLat)
[498]314                 {
[665]315                   switch (domain->type)
316                   {
317                     case CDomain::type_attr::curvilinear :
318                       dim0.push_back(dimYid); dim0.push_back(dimXid);
319                       lonid = StdString("nav_lon").append(appendDomid);
320                       latid = StdString("nav_lat").append(appendDomid);
[1158]321                       SuperClassWriter::addVariable(latid, typePrec, dim0);
322                       SuperClassWriter::addVariable(lonid, typePrec, dim0);
[665]323                       break;
[449]324
[665]325                     case CDomain::type_attr::rectilinear :
326                       dim0.push_back(dimYid);
327                       dim1.push_back(dimXid);
328                       lonid = StdString("lon").append(appendDomid);
329                       latid = StdString("lat").append(appendDomid);
[1158]330                       SuperClassWriter::addVariable(latid, typePrec, dim0);
331                       SuperClassWriter::addVariable(lonid, typePrec, dim1);
[665]332                       break;
333                   }
334
335                   bounds_lonid = StdString("bounds_lon").append(appendDomid);
336                   bounds_latid = StdString("bounds_lat").append(appendDomid);
337
338                   this->writeAxisAttributes
339                      (lonid, isRegularDomain ? "X" : "", "longitude", "Longitude", "degrees_east", domid);
340                   this->writeAxisAttributes
341                      (latid, isRegularDomain ? "Y" : "", "latitude", "Latitude", "degrees_north", domid);
342
343                   if (domain->hasBounds)
344                   {
345                     SuperClassWriter::addAttribute("bounds", bounds_lonid, &lonid);
346                     SuperClassWriter::addAttribute("bounds", bounds_latid, &latid);
347
348                     dim0.clear();
[498]349                     dim0.push_back(dimYid);
[665]350                     dim0.push_back(dimXid);
351                     dim0.push_back(dimVertId);
[1158]352                     SuperClassWriter::addVariable(bounds_lonid, typePrec, dim0);
353                     SuperClassWriter::addVariable(bounds_latid, typePrec, dim0);
[665]354                   }
[498]355                 }
[611]356
357                 if (domain->hasArea)
358                 {
359                   dim0.clear();
360                   dim0.push_back(dimYid); dim0.push_back(dimXid);
[1158]361                   SuperClassWriter::addVariable(areaId, typePrec, dim0);
[614]362                   SuperClassWriter::addAttribute("standard_name", StdString("cell_area"), &areaId);
[611]363                   SuperClassWriter::addAttribute("units", StdString("m2"), &areaId);
364                   dim0.clear();
365                 }
366
367                 SuperClassWriter::definition_end();
[286]368
[498]369                 switch (domain->type)
[384]370                 {
[498]371                   case CDomain::type_attr::curvilinear :
[449]372                   {
[498]373                     std::vector<StdSize> start(2) ;
374                     std::vector<StdSize> count(2) ;
375                     if (domain->isEmpty())
376                     {
[611]377                       start[0]=0 ; start[1]=0 ;
[498]378                       count[0]=0 ; count[1]=0 ;
379                     }
380                     else
381                     {
[1099]382                       start[1]=domain->zoom_ibegin-domain->global_zoom_ibegin;
383                       start[0]=domain->zoom_jbegin-domain->global_zoom_jbegin;
384                       count[1]=domain->zoom_ni ; count[0]=domain->zoom_nj ;
[498]385                     }
[488]386
[665]387                     if (domain->hasLonLat)
388                     {
[1129]389                       SuperClassWriter::writeData(writtenLat, latid, isCollective, 0,&start,&count);
390                       SuperClassWriter::writeData(writtenLon, lonid, isCollective, 0,&start,&count);
[665]391                     }
[498]392                     break;
393                   }
[664]394                   case CDomain::type_attr::rectilinear :
[449]395                   {
[665]396                     if (domain->hasLonLat)
[498]397                     {
[665]398                       std::vector<StdSize> start(1) ;
399                       std::vector<StdSize> count(1) ;
400                       if (domain->isEmpty())
401                       {
402                         start[0]=0 ;
403                         count[0]=0 ;
[1129]404                         SuperClassWriter::writeData(writtenLat, latid, isCollective, 0,&start,&count);
405                         SuperClassWriter::writeData(writtenLon, lonid, isCollective, 0,&start,&count);
[665]406                       }
407                       else
[1129]408                       { 
[1099]409                         start[0]=domain->zoom_jbegin-domain->global_zoom_jbegin;
[1143]410                         count[0]=domain->zoom_nj;                         
[1129]411                         CArray<double,1> lat = writtenLat(Range(fromStart,toEnd,domain->zoom_ni));
[665]412                         SuperClassWriter::writeData(CArray<double,1>(lat.copy()), latid, isCollective, 0,&start,&count);
413
[1099]414                         start[0]=domain->zoom_ibegin-domain->global_zoom_ibegin;
[1143]415                         count[0]=domain->zoom_ni;                         
[1129]416                         CArray<double,1> lon = writtenLon(Range(0,domain->zoom_ni-1));
[665]417                         SuperClassWriter::writeData(CArray<double,1>(lon.copy()), lonid, isCollective, 0,&start,&count);
418                       }
[498]419                     }
420                     break;
[449]421                   }
[384]422                 }
[611]423
[617]424                 if (domain->hasBounds)
425                 {
426                   std::vector<StdSize> start(3);
427                   std::vector<StdSize> count(3);
428                   if (domain->isEmpty())
429                   {
430                     start[2] = start[1] = start[0] = 0;
431                     count[2] = count[1] = count[0] = 0;
432                   }
433                   else
434                   {
435                     start[2] = 0;
[1099]436                     start[1] = domain->zoom_ibegin - domain->global_zoom_ibegin;
437                     start[0] = domain->zoom_jbegin - domain->global_zoom_jbegin;
438                     count[2] = domain->nvertex;
439                     count[1] = domain->zoom_ni;
440                     count[0] = domain->zoom_nj;
[617]441                   }
[1143]442                 
[1129]443                   SuperClassWriter::writeData(writtenBndsLon, bounds_lonid, isCollective, 0, &start, &count);
444                   SuperClassWriter::writeData(writtenBndsLat, bounds_latid, isCollective, 0, &start, &count);
[617]445                 }
446
[611]447                 if (domain->hasArea)
448                 {
449                   std::vector<StdSize> start(2);
450                   std::vector<StdSize> count(2);
451
452                   if (domain->isEmpty())
453                   {
454                     start[0] = 0; start[1] = 0;
455                     count[0] = 0; count[1] = 0;
456                   }
457                   else
458                   {
[1099]459                     start[1] = domain->zoom_ibegin - domain->global_zoom_ibegin;
460                     start[0] = domain->zoom_jbegin - domain->global_zoom_jbegin;
461                     count[1] = domain->zoom_ni;
462                     count[0] = domain->zoom_nj;
[611]463                   }
[1143]464                   
[1129]465                   SuperClassWriter::writeData(writtenArea, areaId, isCollective, 0, &start, &count);
[611]466                 }
467
[498]468                 SuperClassWriter::definition_start();
469                 break;
470              }
471              default :
472                 ERROR("CNc4DataOutput::writeDomain(domain)",
473                       << "[ type = " << SuperClass::type << "]"
474                       << " not implemented yet !");
475           }
[449]476         }
[498]477         catch (CNetCdfException& e)
478         {
479           StdString msg("On writing the domain : ");
480           msg.append(domid); msg.append("\n");
481           msg.append("In the context : ");
482           msg.append(context->getId()); msg.append("\n");
483           msg.append(e.what());
484           ERROR("CNc4DataOutput::writeDomain_(CDomain* domain)", << msg);
485         }
486
[449]487         domain->addRelFile(this->filename);
488      }
489
[879]490    //--------------------------------------------------------------
[878]491
[879]492    void CNc4DataOutput::writeUnstructuredDomainUgrid(CDomain* domain)
493    {
494      CContext* context = CContext::getCurrent() ;
495      CContextServer* server=context->server ;
[878]496
[879]497      if (domain->IsWritten(this->filename)) return;
498      domain->checkAttributes();
499      if (domain->isEmpty())
500        if (SuperClass::type==MULTI_FILE) return ;
[878]501
[1158]502     nc_type typePrec ;
503     if (domain->prec.isEmpty()) typePrec =  NC_FLOAT ;
504     else if (domain->prec==4)  typePrec =  NC_FLOAT ;
505     else if (domain->prec==8)   typePrec =  NC_DOUBLE ;
506
[879]507      std::vector<StdString> dim0;
508      StdString domid = domain->getDomainOutputName();
509      StdString domainName = domain->name;
[924]510      domain->assignMesh(domainName, domain->nvertex);
[1025]511      domain->mesh->createMeshEpsilon(server->intraComm, domain->lonvalue, domain->latvalue, domain->bounds_lonvalue, domain->bounds_latvalue);
[878]512
[879]513      StdString node_x = domainName + "_node_x";
514      StdString node_y = domainName + "_node_y";
[878]515
[879]516      StdString edge_x = domainName + "_edge_x";
517      StdString edge_y = domainName + "_edge_y";
518      StdString edge_nodes = domainName + "_edge_nodes";
[878]519
[879]520      StdString face_x = domainName + "_face_x";
521      StdString face_y = domainName + "_face_y";
522      StdString face_nodes = domainName + "_face_nodes";
[902]523      StdString face_edges = domainName + "_face_edges";
524      StdString edge_faces = domainName + "_edge_face_links";
525      StdString face_faces = domainName + "_face_links";
[878]526
[879]527      StdString dimNode = "n" + domainName + "_node";
528      StdString dimEdge = "n" + domainName + "_edge";
529      StdString dimFace = "n" + domainName + "_face";
530      StdString dimVertex = "n" + domainName + "_vertex";
531      StdString dimTwo = "Two";
[878]532
[879]533      if (!SuperClassWriter::dimExist(dimTwo)) SuperClassWriter::addDimension(dimTwo, 2);
534      if (!isWrittenDomain(domid))
535      {
536        dim0.clear();
537        SuperClassWriter::addVariable(domainName, NC_INT, dim0);
538        SuperClassWriter::addAttribute("cf_role", StdString("mesh_topology"), &domainName);
[924]539        SuperClassWriter::addAttribute("long_name", StdString("Topology data of 2D unstructured mesh"), &domainName);
540        SuperClassWriter::addAttribute("topology_dimension", 2, &domainName);
[879]541        SuperClassWriter::addAttribute("node_coordinates", node_x + " " + node_y, &domainName);
542      }
[878]543
[879]544      try
545      {
546        switch (SuperClass::type)
547        {
548          case (ONE_FILE) :
549          {
550            // Adding nodes
551            if (domain->nvertex == 1)
552            {
553              if (!SuperClassWriter::varExist(node_x) || !SuperClassWriter::varExist(node_y))
554              {
[902]555                SuperClassWriter::addDimension(dimNode, domain->ni_glo);
[879]556                dim0.clear();
557                dim0.push_back(dimNode);
[1158]558                SuperClassWriter::addVariable(node_x, typePrec, dim0);
[879]559                SuperClassWriter::addAttribute("standard_name", StdString("longitude"), &node_x);
[924]560                SuperClassWriter::addAttribute("long_name", StdString("Longitude of mesh nodes."), &node_x);
[879]561                SuperClassWriter::addAttribute("units", StdString("degrees_east"), &node_x);
[1158]562                SuperClassWriter::addVariable(node_y, typePrec, dim0);
[879]563                SuperClassWriter::addAttribute("standard_name", StdString("latitude"), &node_y);
[924]564                SuperClassWriter::addAttribute("long_name", StdString("Latitude of mesh nodes."), &node_y);
[879]565                SuperClassWriter::addAttribute("units", StdString("degrees_north"), &node_y);
566              }
567            } // domain->nvertex == 1
[878]568
[879]569            // Adding edges and nodes, if nodes have not been defined previously
570            if (domain->nvertex == 2)
571            {
572              if (!SuperClassWriter::varExist(node_x) || !SuperClassWriter::varExist(node_y))
573              {
[924]574                SuperClassWriter::addDimension(dimNode, domain->mesh->nbNodesGlo);
[879]575                dim0.clear();
576                dim0.push_back(dimNode);
[1158]577                SuperClassWriter::addVariable(node_x, typePrec, dim0);
[879]578                SuperClassWriter::addAttribute("standard_name", StdString("longitude"), &node_x);
[924]579                SuperClassWriter::addAttribute("long_name", StdString("Longitude of mesh nodes."), &node_x);
[879]580                SuperClassWriter::addAttribute("units", StdString("degrees_east"), &node_x);
[1158]581                SuperClassWriter::addVariable(node_y, typePrec, dim0);
[879]582                SuperClassWriter::addAttribute("standard_name", StdString("latitude"), &node_y);
[924]583                SuperClassWriter::addAttribute("long_name", StdString("Latitude of mesh nodes."), &node_y);
[879]584                SuperClassWriter::addAttribute("units", StdString("degrees_north"), &node_y);
585              }
[924]586              SuperClassWriter::addAttribute("edge_node_connectivity", edge_nodes, &domainName);
587              SuperClassWriter::addAttribute("edge_coordinates", edge_x + " " + edge_y, &domainName);
588              SuperClassWriter::addDimension(dimEdge, domain->ni_glo);
589              dim0.clear();
590              dim0.push_back(dimEdge);
[1158]591              SuperClassWriter::addVariable(edge_x, typePrec, dim0);
[924]592              SuperClassWriter::addAttribute("standard_name", StdString("longitude"), &edge_x);
593              SuperClassWriter::addAttribute("long_name", StdString("Characteristic longitude of mesh edges."), &edge_x);
594              SuperClassWriter::addAttribute("units", StdString("degrees_east"), &edge_x);
[1158]595              SuperClassWriter::addVariable(edge_y, typePrec, dim0);
[924]596              SuperClassWriter::addAttribute("standard_name", StdString("latitude"), &edge_y);
597              SuperClassWriter::addAttribute("long_name", StdString("Characteristic latitude of mesh edges."), &edge_y);
598              SuperClassWriter::addAttribute("units", StdString("degrees_north"), &edge_y);
599              dim0.clear();
600              dim0.push_back(dimEdge);
601              dim0.push_back(dimTwo);
602              SuperClassWriter::addVariable(edge_nodes, NC_INT, dim0);
603              SuperClassWriter::addAttribute("cf_role", StdString("edge_node_connectivity"), &edge_nodes);
604              SuperClassWriter::addAttribute("long_name", StdString("Maps every edge/link to two nodes that it connects."), &edge_nodes);
605              SuperClassWriter::addAttribute("start_index", 0, &edge_nodes);
[879]606            } // domain->nvertex == 2
[878]607
[879]608            // Adding faces, edges, and nodes, if edges and nodes have not been defined previously
609            if (domain->nvertex > 2)
610            {
611              // Nodes
612              if (!SuperClassWriter::varExist(node_x) || !SuperClassWriter::varExist(node_y))
613              {
[924]614                SuperClassWriter::addDimension(dimNode, domain->mesh->nbNodesGlo);
[879]615                dim0.clear();
616                dim0.push_back(dimNode);
[1158]617                SuperClassWriter::addVariable(node_x, typePrec, dim0);
[879]618                SuperClassWriter::addAttribute("standard_name", StdString("longitude"), &node_x);
[924]619                SuperClassWriter::addAttribute("long_name", StdString("Longitude of mesh nodes."), &node_x);
[879]620                SuperClassWriter::addAttribute("units", StdString("degrees_east"), &node_x);
[1158]621                SuperClassWriter::addVariable(node_y, typePrec, dim0);
[879]622                SuperClassWriter::addAttribute("standard_name", StdString("latitude"), &node_y);
[924]623                SuperClassWriter::addAttribute("long_name", StdString("Latitude of mesh nodes."), &node_y);
[879]624                SuperClassWriter::addAttribute("units", StdString("degrees_north"), &node_y);
625              }
626              if (!SuperClassWriter::varExist(edge_x) || !SuperClassWriter::varExist(edge_y))
627              {
628                SuperClassWriter::addAttribute("edge_coordinates", edge_x + " " + edge_y, &domainName);
629                SuperClassWriter::addAttribute("edge_node_connectivity", edge_nodes, &domainName);
[924]630                SuperClassWriter::addDimension(dimEdge, domain->mesh->nbEdgesGlo);
[879]631                dim0.clear();
632                dim0.push_back(dimEdge);
[1158]633                SuperClassWriter::addVariable(edge_x, typePrec, dim0);
[879]634                SuperClassWriter::addAttribute("standard_name", StdString("longitude"), &edge_x);
[924]635                SuperClassWriter::addAttribute("long_name", StdString("Characteristic longitude of mesh edges."), &edge_x);
[879]636                SuperClassWriter::addAttribute("units", StdString("degrees_east"), &edge_x);
[1158]637                SuperClassWriter::addVariable(edge_y, typePrec, dim0);
[879]638                SuperClassWriter::addAttribute("standard_name", StdString("latitude"), &edge_y);
[924]639                SuperClassWriter::addAttribute("long_name", StdString("Characteristic latitude of mesh edges."), &edge_y);
[879]640                SuperClassWriter::addAttribute("units", StdString("degrees_north"), &edge_y);
641                dim0.clear();
642                dim0.push_back(dimEdge);
643                dim0.push_back(dimTwo);
644                SuperClassWriter::addVariable(edge_nodes, NC_INT, dim0);
645                SuperClassWriter::addAttribute("cf_role", StdString("edge_node_connectivity"), &edge_nodes);
646                SuperClassWriter::addAttribute("long_name", StdString("Maps every edge/link to two nodes that it connects."), &edge_nodes);
647                SuperClassWriter::addAttribute("start_index", 0, &edge_nodes);
648              }
[924]649              SuperClassWriter::addAttribute("face_coordinates", face_x + " " + face_y, &domainName);
650              SuperClassWriter::addAttribute("face_node_connectivity", face_nodes, &domainName);
651              SuperClassWriter::addDimension(dimFace, domain->ni_glo);
652              SuperClassWriter::addDimension(dimVertex, domain->nvertex);
653              dim0.clear();
654              dim0.push_back(dimFace);
[1158]655              SuperClassWriter::addVariable(face_x, typePrec, dim0);
[924]656              SuperClassWriter::addAttribute("standard_name", StdString("longitude"), &face_x);
657              SuperClassWriter::addAttribute("long_name", StdString("Characteristic longitude of mesh faces."), &face_x);
658              SuperClassWriter::addAttribute("units", StdString("degrees_east"), &face_x);
[1158]659              SuperClassWriter::addVariable(face_y, typePrec, dim0);
[924]660              SuperClassWriter::addAttribute("standard_name", StdString("latitude"), &face_y);
661              SuperClassWriter::addAttribute("long_name", StdString("Characteristic latitude of mesh faces."), &face_y);
662              SuperClassWriter::addAttribute("units", StdString("degrees_north"), &face_y);
663              dim0.clear();
664              dim0.push_back(dimFace);
665              dim0.push_back(dimVertex);
666              SuperClassWriter::addVariable(face_nodes, NC_INT, dim0);
667              SuperClassWriter::addAttribute("cf_role", StdString("face_node_connectivity"), &face_nodes);
668              SuperClassWriter::addAttribute("long_name", StdString("Maps every face to its corner nodes."), &face_nodes);
669              SuperClassWriter::addAttribute("start_index", 0, &face_nodes);
670              dim0.clear();
671              dim0.push_back(dimFace);
672              dim0.push_back(dimVertex);
673              SuperClassWriter::addVariable(face_edges, NC_INT, dim0);
674              SuperClassWriter::addAttribute("cf_role", StdString("face_edge_connectivity"), &face_edges);
675              SuperClassWriter::addAttribute("long_name", StdString("Maps every face to its edges."), &face_edges);
676              SuperClassWriter::addAttribute("start_index", 0, &face_edges);
[929]677              SuperClassWriter::addAttribute("_FillValue", 999999, &face_edges);
[924]678              dim0.clear();
679              dim0.push_back(dimEdge);
680              dim0.push_back(dimTwo);
681              SuperClassWriter::addVariable(edge_faces, NC_INT, dim0);
682              SuperClassWriter::addAttribute("cf_role", StdString("edge_face connectivity"), &edge_faces);
683              SuperClassWriter::addAttribute("long_name", StdString("neighbor faces for edges"), &edge_faces);
684              SuperClassWriter::addAttribute("start_index", 0, &edge_faces);
685              SuperClassWriter::addAttribute("_FillValue", -999, &edge_faces);
686              SuperClassWriter::addAttribute("comment", StdString("missing neighbor faces are indicated using _FillValue"), &edge_faces);
687              dim0.clear();
688              dim0.push_back(dimFace);
689              dim0.push_back(dimVertex);
690              SuperClassWriter::addVariable(face_faces, NC_INT, dim0);
691              SuperClassWriter::addAttribute("cf_role", StdString("face_face connectivity"), &face_faces);
692              SuperClassWriter::addAttribute("long_name", StdString("Indicates which other faces neighbor each face"), &face_faces);
693              SuperClassWriter::addAttribute("start_index", 0, &face_faces);
[929]694              SuperClassWriter::addAttribute("_FillValue", 999999, &face_faces);
[924]695              SuperClassWriter::addAttribute("flag_values", -1, &face_faces);
696              SuperClassWriter::addAttribute("flag_meanings", StdString("out_of_mesh"), &face_faces);
[879]697            } // domain->nvertex > 2
[878]698
[879]699            SuperClassWriter::definition_end();
[878]700
[924]701            std::vector<StdSize> startEdges(1) ;
702            std::vector<StdSize> countEdges(1) ;
703            std::vector<StdSize> startNodes(1) ;
704            std::vector<StdSize> countNodes(1) ;
705            std::vector<StdSize> startFaces(1) ;
706            std::vector<StdSize> countFaces(1) ;
707            std::vector<StdSize> startEdgeNodes(2) ;
708            std::vector<StdSize> countEdgeNodes(2) ;
709            std::vector<StdSize> startEdgeFaces(2) ;
710            std::vector<StdSize> countEdgeFaces(2) ;
711            std::vector<StdSize> startFaceConctv(2) ;
712            std::vector<StdSize> countFaceConctv(2) ;
[902]713
[879]714            if (!isWrittenDomain(domid))
715            {
716              if (domain->nvertex == 1)
717              {
[924]718                if (domain->isEmpty())
719                 {
720                   startNodes[0]=0 ;
721                   countNodes[0]=0 ;
722                 }
723                 else
724                 {
[1132]725                   startNodes[0] = domain->zoom_ibegin-domain->global_zoom_ibegin;
726                   countNodes[0] = domain->zoom_ni ;
[924]727                 }
728
729                SuperClassWriter::writeData(domain->mesh->node_lat, node_y, isCollective, 0, &startNodes, &countNodes);
730                SuperClassWriter::writeData(domain->mesh->node_lon, node_x, isCollective, 0, &startNodes, &countNodes);
[879]731              }
732              else if (domain->nvertex == 2)
[924]733              {
734                if (domain->isEmpty())
735                 {
736                  startEdges[0]=0 ;
737                  countEdges[0]=0 ;
738                  startNodes[0]=0 ;
739                  countNodes[0]=0 ;
740                  startEdgeNodes[0]=0;
741                  startEdgeNodes[1]=0;
742                  countEdgeNodes[0]=0;
743                  countEdgeNodes[1]=0;
744
745                 }
746                 else
747                 {
[1132]748                   startEdges[0] = domain->zoom_ibegin-domain->global_zoom_ibegin;
749                   countEdges[0] = domain->zoom_ni;
[924]750                   startNodes[0] = domain->mesh->node_start;
751                   countNodes[0] = domain->mesh->node_count;
[1132]752                   startEdgeNodes[0] = domain->zoom_ibegin-domain->global_zoom_ibegin;
[924]753                   startEdgeNodes[1] = 0;
[1132]754                   countEdgeNodes[0] = domain->zoom_ni;
755                   countEdgeNodes[1] = 2;
[924]756                 }
757                SuperClassWriter::writeData(domain->mesh->node_lat, node_y, isCollective, 0, &startNodes, &countNodes);
758                SuperClassWriter::writeData(domain->mesh->node_lon, node_x, isCollective, 0, &startNodes, &countNodes);
759                SuperClassWriter::writeData(domain->mesh->edge_lat, edge_y, isCollective, 0, &startEdges, &countEdges);
760                SuperClassWriter::writeData(domain->mesh->edge_lon, edge_x, isCollective, 0, &startEdges, &countEdges);
761                SuperClassWriter::writeData(domain->mesh->edge_nodes, edge_nodes, isCollective, 0, &startEdgeNodes, &countEdgeNodes);
762              }
[879]763              else
764              {
[924]765                if (domain->isEmpty())
766                 {
767                   startFaces[0] = 0 ;
768                   countFaces[0] = 0 ;
769                   startNodes[0] = 0;
770                   countNodes[0] = 0;
771                   startEdges[0] = 0;
772                   countEdges[0] = 0;
773                   startEdgeFaces[0] = 0;
774                   startEdgeFaces[1] = 0;
775                   countEdgeFaces[0] = 0;
776                   countEdgeFaces[1] = 0;
777                   startFaceConctv[0] = 0;
778                   startFaceConctv[1] = 0;
779                   countFaceConctv[0] = 0;
780                   countFaceConctv[1] = 0;
781                 }
782                 else
783                 {
[1132]784                   startFaces[0] = domain->zoom_ibegin-domain->global_zoom_ibegin;
785                   countFaces[0] = domain->zoom_ni ;
[924]786                   startNodes[0] = domain->mesh->node_start;
787                   countNodes[0] = domain->mesh->node_count;
788                   startEdges[0] = domain->mesh->edge_start;
789                   countEdges[0] = domain->mesh->edge_count;
790                   startEdgeNodes[0] = domain->mesh->edge_start;
791                   startEdgeNodes[1] = 0;
792                   countEdgeNodes[0] = domain->mesh->edge_count;
793                   countEdgeNodes[1]= 2;
794                   startEdgeFaces[0] = domain->mesh->edge_start;
795                   startEdgeFaces[1]= 0;
796                   countEdgeFaces[0] = domain->mesh->edge_count;
797                   countEdgeFaces[1]= 2;
[1132]798                   startFaceConctv[0] = domain->zoom_ibegin-domain->global_zoom_ibegin;
799                   startFaceConctv[1] = 0; 
800                   countFaceConctv[0] = domain->zoom_ni;
[924]801                   countFaceConctv[1] = domain->nvertex;
802                 }
803                SuperClassWriter::writeData(domain->mesh->node_lat, node_y, isCollective, 0, &startNodes, &countNodes);
804                SuperClassWriter::writeData(domain->mesh->node_lon, node_x, isCollective, 0, &startNodes, &countNodes);
805                SuperClassWriter::writeData(domain->mesh->edge_lat, edge_y, isCollective, 0, &startEdges, &countEdges);
806                SuperClassWriter::writeData(domain->mesh->edge_lon, edge_x, isCollective, 0, &startEdges, &countEdges);
807                SuperClassWriter::writeData(domain->mesh->edge_nodes, edge_nodes, isCollective, 0, &startEdgeNodes, &countEdgeNodes);
808                SuperClassWriter::writeData(domain->mesh->face_lat, face_y, isCollective, 0, &startFaces, &countFaces);
809                SuperClassWriter::writeData(domain->mesh->face_lon, face_x, isCollective, 0, &startFaces, &countFaces);
810                SuperClassWriter::writeData(domain->mesh->face_nodes, face_nodes, isCollective, 0, &startFaceConctv, &countFaceConctv);
811                SuperClassWriter::writeData(domain->mesh->face_edges, face_edges, isCollective, 0, &startFaceConctv, &countFaceConctv);
812                SuperClassWriter::writeData(domain->mesh->edge_faces, edge_faces, isCollective, 0, &startEdgeFaces, &countEdgeFaces);
813                SuperClassWriter::writeData(domain->mesh->face_faces, face_faces, isCollective, 0, &startFaceConctv, &countFaceConctv);
[879]814              }
815              setWrittenDomain(domid);
816            } // !isWrittenDomain
817            else
818            {
819              if (domain->nvertex == 2)
820              {
[1132]821                startEdges[0] = domain->zoom_ibegin-domain->global_zoom_ibegin;
822                countEdges[0] = domain->zoom_ni;
823                startEdgeNodes[0] = domain->zoom_ibegin-domain->global_zoom_ibegin;
824                startEdgeNodes[1] = 0;
825                countEdgeNodes[0] = domain->zoom_ni;
[924]826                countEdgeNodes[1]= 2;
827                SuperClassWriter::writeData(domain->mesh->edge_lat, edge_y, isCollective, 0, &startEdges, &countEdges);
828                SuperClassWriter::writeData(domain->mesh->edge_lon, edge_x, isCollective, 0, &startEdges, &countEdges);
829                SuperClassWriter::writeData(domain->mesh->edge_nodes, edge_nodes, isCollective, 0, &startEdgeNodes, &countEdgeNodes);
[879]830              } 
831              if (domain->nvertex > 2)
832              {
[924]833
[879]834                if (!domain->mesh->edgesAreWritten)
835                {
[924]836                  startEdges[0] = domain->mesh->edge_start;
837                  countEdges[0] = domain->mesh->edge_count;
838                  startEdgeNodes[0] = domain->mesh->edge_start;
839                  startEdgeNodes[1] = 0;
840                  countEdgeNodes[0] = domain->mesh->edge_count;
841                  countEdgeNodes[1]= 2;
842                  SuperClassWriter::writeData(domain->mesh->edge_lat, edge_y, isCollective, 0, &startEdges, &countEdges);
843                  SuperClassWriter::writeData(domain->mesh->edge_lon, edge_x, isCollective, 0, &startEdges, &countEdges);
844                  SuperClassWriter::writeData(domain->mesh->edge_nodes, edge_nodes, isCollective, 0, &startEdgeNodes, &countEdgeNodes);
[879]845                }
[1132]846                startFaces[0] = domain->zoom_ibegin-domain->global_zoom_ibegin;
847                countFaces[0] = domain->zoom_ni;
[924]848                startEdgeFaces[0] = domain->mesh->edge_start;
849                startEdgeFaces[1]= 0;
850                countEdgeFaces[0] = domain->mesh->edge_count;
851                countEdgeFaces[1]= 2;
[1132]852                startFaceConctv[0] = domain->zoom_ibegin-domain->global_zoom_ibegin;
853                startFaceConctv[1] = 0;
854                countFaceConctv[0] = domain->zoom_ni;
855                countFaceConctv[1] = domain->nvertex;
[924]856                SuperClassWriter::writeData(domain->mesh->face_lat, face_y, isCollective, 0, &startFaces, &countFaces);
857                SuperClassWriter::writeData(domain->mesh->face_lon, face_x, isCollective, 0, &startFaces, &countFaces);
858                SuperClassWriter::writeData(domain->mesh->face_nodes, face_nodes, isCollective, 0, &startFaceConctv, &countFaceConctv);
859                SuperClassWriter::writeData(domain->mesh->face_edges, face_edges, isCollective, 0, &startFaceConctv, &countFaceConctv);
860                SuperClassWriter::writeData(domain->mesh->edge_faces, edge_faces, isCollective, 0, &startEdgeFaces, &countEdgeFaces);
861                SuperClassWriter::writeData(domain->mesh->face_faces, face_faces, isCollective, 0, &startFaceConctv, &countFaceConctv);
[879]862              }
863            }// isWrittenDomain
[878]864
[879]865            SuperClassWriter::definition_start();
[878]866
[879]867            break;
868          } // ONE_FILE
[878]869
[879]870          case (MULTI_FILE) :
871          {
872            break;
873          }
[878]874
[879]875          default :
876          ERROR("CNc4DataOutput::writeDomain(domain)",
877          << "[ type = " << SuperClass::type << "]"
878          << " not implemented yet !");
879          } // switch
880        } // try
[878]881
[879]882        catch (CNetCdfException& e)
883        {
884          StdString msg("On writing the domain : ");
885          msg.append(domid); msg.append("\n");
886          msg.append("In the context : ");
887          msg.append(context->getId()); msg.append("\n");
888          msg.append(e.what());
[924]889          ERROR("CNc4DataOutput::writeUnstructuredDomainUgrid(CDomain* domain)", << msg);
[879]890        }
[878]891
[879]892  domain->addRelFile(this->filename);
893  }
[878]894
[879]895    //--------------------------------------------------------------
[878]896
[879]897    void CNc4DataOutput::writeUnstructuredDomain(CDomain* domain)
[449]898      {
899         CContext* context = CContext::getCurrent() ;
900         CContextServer* server=context->server ;
[488]901
[449]902         if (domain->IsWritten(this->filename)) return;
903         domain->checkAttributes();
[488]904
905         if (domain->isEmpty())
[449]906           if (SuperClass::type==MULTI_FILE) return ;
907
908         std::vector<StdString> dim0, dim1;
[772]909         StdString domid = domain->getDomainOutputName();
[705]910         if (isWrittenDomain(domid)) return ;
[774]911         else setWrittenDomain(domid);
[705]912
[449]913         StdString appendDomid  = (singleDomain) ? "" : "_"+domid ;
914
915         StdString dimXid = StdString("cell").append(appendDomid);
916         StdString dimVertId = StdString("nvertex").append(appendDomid);
[488]917
[449]918         string lonid,latid,bounds_lonid,bounds_latid ;
[611]919         string areaId = "area" + appendDomid;
[449]920
[1158]921         nc_type typePrec ;
922         if (domain->prec.isEmpty()) typePrec =  NC_FLOAT ;
923         else if (domain->prec==4)  typePrec =  NC_FLOAT ;
924         else if (domain->prec==8)   typePrec =  NC_DOUBLE ;
925
[1132]926         int nvertex = (domain->nvertex.isEmpty()) ? 0 : domain->nvertex;
[1025]927
[1132]928         CArray<size_t, 1>& indexToWrite = domain->localIndexToWriteOnServer;
929         int nbWritten = indexToWrite.numElements();
930         CArray<double,1> writtenLat, writtenLon;
931         CArray<double,2> writtenBndsLat, writtenBndsLon;
932         CArray<double,1> writtenArea;
933
934         if (domain->hasLonLat)
935         {
936           writtenLat.resize(nbWritten);
937           writtenLon.resize(nbWritten);
938           for (int idx = 0; idx < nbWritten; ++idx)
939           {
940              writtenLat(idx) = domain->latvalue(indexToWrite(idx));
941              writtenLon(idx) = domain->lonvalue(indexToWrite(idx));
942           }
943         
944
945           if (domain->hasBounds)
946           {         
947             int nvertex = domain->nvertex, idx;
948             writtenBndsLat.resize(nvertex, nbWritten);
949             writtenBndsLon.resize(nvertex, nbWritten);
950             CArray<double,2>& boundslat = domain->bounds_latvalue;
951             CArray<double,2>& boundslon = domain->bounds_lonvalue;   
952             for (idx = 0; idx < nbWritten; ++idx)
953               for (int nv = 0; nv < nvertex; ++nv)
954               {
955                 writtenBndsLat(nv, idx) = boundslat(nv, int(indexToWrite(idx)));
956                 writtenBndsLon(nv, idx) = boundslon(nv, int(indexToWrite(idx)));
957               }
958           }
959         }
960
961         if (domain->hasArea)
962         {
963           writtenArea.resize(nbWritten);           
964           for (int idx = 0; idx < nbWritten; ++idx)
965           {
966              writtenArea(idx) = domain->areavalue(indexToWrite(idx));                     
967           }
968         }
969
[498]970         try
[449]971         {
[498]972           switch (SuperClass::type)
973           {
974              case (MULTI_FILE) :
975              {
976                 dim0.push_back(dimXid);
[1132]977                 SuperClassWriter::addDimension(dimXid, domain->zoom_ni);
[488]978
[665]979                 lonid = StdString("lon").append(appendDomid);
980                 latid = StdString("lat").append(appendDomid);
[498]981                 bounds_lonid = StdString("bounds_lon").append(appendDomid);
982                 bounds_latid = StdString("bounds_lat").append(appendDomid);
[665]983                 if (domain->hasLonLat)
984                 {
[1158]985                   SuperClassWriter::addVariable(latid, typePrec, dim0);
986                   SuperClassWriter::addVariable(lonid, typePrec, dim0);
[665]987                   this->writeAxisAttributes(lonid, "", "longitude", "Longitude", "degrees_east", domid);
988                   if (domain->hasBounds) SuperClassWriter::addAttribute("bounds",bounds_lonid, &lonid);
989                   this->writeAxisAttributes(latid, "", "latitude", "Latitude", "degrees_north", domid);
990                   if (domain->hasBounds) SuperClassWriter::addAttribute("bounds",bounds_latid, &latid);
991                   if (domain->hasBounds) SuperClassWriter::addDimension(dimVertId, domain->nvertex);
992                 }
[498]993                 dim0.clear();
994                 if (domain->hasBounds)
995                 {
996                   dim0.push_back(dimXid);
997                   dim0.push_back(dimVertId);
[1158]998                   SuperClassWriter::addVariable(bounds_lonid, typePrec, dim0);
999                   SuperClassWriter::addVariable(bounds_latid, typePrec, dim0);
[498]1000                 }
1001
1002                 dim0.clear();
[449]1003                 dim0.push_back(dimXid);
[611]1004                 if (domain->hasArea)
1005                 {
[1158]1006                   SuperClassWriter::addVariable(areaId, typePrec, dim0);
[614]1007                   SuperClassWriter::addAttribute("standard_name", StdString("cell_area"), &areaId);
[611]1008                   SuperClassWriter::addAttribute("units", StdString("m2"), &areaId);
1009                 }
1010
[498]1011                 SuperClassWriter::definition_end();
[449]1012
[665]1013                 if (domain->hasLonLat)
[498]1014                 {
[1132]1015                   SuperClassWriter::writeData(writtenLat, latid, isCollective, 0);
1016                   SuperClassWriter::writeData(writtenLon, lonid, isCollective, 0);
[665]1017                   if (domain->hasBounds)
1018                   {
[1132]1019                     SuperClassWriter::writeData(writtenBndsLon, bounds_lonid, isCollective, 0);
1020                     SuperClassWriter::writeData(writtenBndsLat, bounds_latid, isCollective, 0);
[665]1021                   }
[498]1022                 }
[611]1023
1024                 if (domain->hasArea)
[1132]1025                   SuperClassWriter::writeData(writtenArea, areaId, isCollective, 0);
[611]1026
[498]1027                 SuperClassWriter::definition_start();
1028                 break ;
1029              }
[488]1030
[498]1031              case (ONE_FILE) :
1032              {
1033                 lonid = StdString("lon").append(appendDomid);
1034                 latid = StdString("lat").append(appendDomid);
1035                 bounds_lonid = StdString("bounds_lon").append(appendDomid);
1036                 bounds_latid = StdString("bounds_lat").append(appendDomid);
1037                 dim0.push_back(dimXid);
[1132]1038                 SuperClassWriter::addDimension(dimXid, domain->ni_glo);
[665]1039                 if (domain->hasLonLat)
1040                 {
[1158]1041                   SuperClassWriter::addVariable(latid, typePrec, dim0);
1042                   SuperClassWriter::addVariable(lonid, typePrec, dim0);
[665]1043
1044                   this->writeAxisAttributes(lonid, "", "longitude", "Longitude", "degrees_east", domid);
1045                   if (domain->hasBounds) SuperClassWriter::addAttribute("bounds",bounds_lonid, &lonid);
1046                   this->writeAxisAttributes(latid, "", "latitude", "Latitude", "degrees_north", domid);
1047                   if (domain->hasBounds) SuperClassWriter::addAttribute("bounds",bounds_latid, &latid);
[1025]1048                   if (domain->hasBounds) SuperClassWriter::addDimension(dimVertId, nvertex);
[665]1049                 }
[498]1050                 dim0.clear();
[449]1051
[498]1052                 if (domain->hasBounds)
1053                 {
1054                   dim0.push_back(dimXid);
1055                   dim0.push_back(dimVertId);
[1158]1056                   SuperClassWriter::addVariable(bounds_lonid, typePrec, dim0);
1057                   SuperClassWriter::addVariable(bounds_latid, typePrec, dim0);
[498]1058                 }
[488]1059
[611]1060                 if (domain->hasArea)
1061                 {
1062                   dim0.clear();
1063                   dim0.push_back(dimXid);
[1158]1064                   SuperClassWriter::addVariable(areaId, typePrec, dim0);
[614]1065                   SuperClassWriter::addAttribute("standard_name", StdString("cell_area"), &areaId);
[611]1066                   SuperClassWriter::addAttribute("units", StdString("m2"), &areaId);
1067                 }
1068
[498]1069                 SuperClassWriter::definition_end();
[488]1070
[498]1071                 std::vector<StdSize> start(1), startBounds(2) ;
1072                 std::vector<StdSize> count(1), countBounds(2) ;
1073                 if (domain->isEmpty())
1074                 {
1075                   start[0]=0 ;
1076                   count[0]=0 ;
1077                   startBounds[1]=0 ;
[1025]1078                   countBounds[1]=nvertex ;
[498]1079                   startBounds[0]=0 ;
1080                   countBounds[0]=0 ;
1081                 }
1082                 else
1083                 {
[1132]1084                   start[0]=domain->zoom_ibegin - domain->global_zoom_ibegin;
1085                   count[0]=domain->zoom_ni;
1086                   startBounds[0]=domain->zoom_ibegin-domain->global_zoom_ibegin;
[498]1087                   startBounds[1]=0 ;
[1132]1088                   countBounds[0]=domain->zoom_ni;
[1025]1089                   countBounds[1]=nvertex ;
[498]1090                 }
[665]1091
1092                 if (domain->hasLonLat)
[498]1093                 {
[1132]1094                   SuperClassWriter::writeData(writtenLat, latid, isCollective, 0,&start,&count);
1095                   SuperClassWriter::writeData(writtenLon, lonid, isCollective, 0,&start,&count);
[665]1096                   if (domain->hasBounds)
1097                   {
[1132]1098                     SuperClassWriter::writeData(writtenBndsLon, bounds_lonid, isCollective, 0,&startBounds,&countBounds);
1099                     SuperClassWriter::writeData(writtenBndsLat, bounds_latid, isCollective, 0,&startBounds,&countBounds);
[665]1100                   }
[498]1101                 }
[488]1102
[611]1103                 if (domain->hasArea)
[1132]1104                   SuperClassWriter::writeData(writtenArea, areaId, isCollective, 0, &start, &count);
[488]1105
[498]1106                 SuperClassWriter::definition_start();
[488]1107
[498]1108                 break;
1109              }
1110              default :
1111                 ERROR("CNc4DataOutput::writeDomain(domain)",
1112                       << "[ type = " << SuperClass::type << "]"
1113                       << " not implemented yet !");
1114           }
[219]1115         }
[498]1116         catch (CNetCdfException& e)
1117         {
1118           StdString msg("On writing the domain : ");
1119           msg.append(domid); msg.append("\n");
1120           msg.append("In the context : ");
1121           msg.append(context->getId()); msg.append("\n");
1122           msg.append(e.what());
1123           ERROR("CNc4DataOutput::writeUnstructuredDomain(CDomain* domain)", << msg);
1124         }
[219]1125         domain->addRelFile(this->filename);
1126      }
1127      //--------------------------------------------------------------
1128
[347]1129      void CNc4DataOutput::writeAxis_(CAxis* axis)
[219]1130      {
[1030]1131        if (axis->IsWritten(this->filename)) return;
1132        axis->checkAttributes();
[488]1133
[1129]1134        axis->computeWrittenIndex();
1135       
[1099]1136        int zoom_size  = (MULTI_FILE == SuperClass::type) ? axis->zoom_n
1137                                                          : axis->global_zoom_n;
1138
[1235]1139        int zoom_count = axis->zoom_n;                                                 
1140        int zoom_begin = axis->zoom_begin;
1141        // int zoom_begin = (MULTI_FILE == SuperClass::type) ? axis->global_zoom_begin
1142        //                                                   : axis->zoom_begin;
1143
[1099]1144        if ((0 == axis->zoom_n) && (MULTI_FILE == SuperClass::type)) return;
1145
[1030]1146        std::vector<StdString> dims;
1147        StdString axisid = axis->getAxisOutputName();
1148        if (isWrittenAxis(axisid)) return ;
1149        else setWrittenAxis(axisid);
[705]1150
[1158]1151        nc_type typePrec ;
1152        if (axis->prec.isEmpty()) typePrec =  NC_FLOAT ;
[1235]1153        else if (axis->prec==4)   typePrec =  NC_FLOAT ;
[1158]1154        else if (axis->prec==8)   typePrec =  NC_DOUBLE ;
1155         
1156        if (!axis->label.isEmpty()) typePrec = NC_CHAR ;
1157        string strId="str_len" ;
[1030]1158        try
1159        {
[1099]1160          SuperClassWriter::addDimension(axisid, zoom_size);
[1158]1161          if (!axis->label.isEmpty()) SuperClassWriter::addDimension(strId, stringArrayLen);
[1030]1162          if (axis->hasValue)
[816]1163          {
[1030]1164            dims.push_back(axisid);
[1158]1165            if (!axis->label.isEmpty()) dims.push_back(strId);
1166            SuperClassWriter::addVariable(axisid, typePrec, dims);
[219]1167
[1030]1168            if (!axis->name.isEmpty())
1169              SuperClassWriter::addAttribute("name", axis->name.getValue(), &axisid);
[219]1170
[1030]1171            if (!axis->standard_name.isEmpty())
1172              SuperClassWriter::addAttribute("standard_name", axis->standard_name.getValue(), &axisid);
[540]1173
[1030]1174            if (!axis->long_name.isEmpty())
1175              SuperClassWriter::addAttribute("long_name", axis->long_name.getValue(), &axisid);
[219]1176
[1030]1177            if (!axis->unit.isEmpty())
1178              SuperClassWriter::addAttribute("units", axis->unit.getValue(), &axisid);
[219]1179
[1030]1180            if (!axis->positive.isEmpty())
1181            {
1182              SuperClassWriter::addAttribute("axis", string("Z"), &axisid);
1183              SuperClassWriter::addAttribute("positive",
1184                                             (axis->positive == CAxis::positive_attr::up) ? string("up") : string("down"),
1185                                             &axisid);
1186            }
[399]1187
[1030]1188            StdString axisBoundsId = axisid + "_bounds";
1189            if (!axis->bounds.isEmpty())
1190            {
1191              dims.push_back("axis_nbounds");
[1158]1192              SuperClassWriter::addVariable(axisBoundsId, typePrec, dims);
[1030]1193              SuperClassWriter::addAttribute("bounds", axisBoundsId, &axisid);
1194            }
[488]1195
[1030]1196            SuperClassWriter::definition_end();
[816]1197
[1129]1198            CArray<size_t, 1>& indexToWrite = axis->localIndexToWriteOnServer;
1199            int nbWritten = indexToWrite.numElements();
1200            CArray<double,1> axis_value(indexToWrite.numElements());
1201            for (int i = 0; i < nbWritten; i++) axis_value(i) = axis->value(indexToWrite(i));
1202            CArray<double,2> axis_bounds;
[1235]1203            CArray<string,1> axis_label;
1204            if (!axis->label.isEmpty())
1205            {
1206              axis_label.resize(indexToWrite.numElements());
1207              for (int i = 0; i < nbWritten; i++) axis_label(i) = axis->label(indexToWrite(i));
1208            }
[1129]1209
[1030]1210            switch (SuperClass::type)
1211            {
1212              case MULTI_FILE:
[816]1213              {
[1235]1214                if (axis->label.isEmpty())
1215                  SuperClassWriter::writeData(axis_value, axisid, isCollective, 0);
[633]1216
[1158]1217                if (!axis->bounds.isEmpty() && axis->label.isEmpty())
[1030]1218                  SuperClassWriter::writeData(axis->bounds, axisBoundsId, isCollective, 0);
[488]1219
[1158]1220                // Need to check after
[1235]1221                if (!axis->label.isEmpty()) 
1222                  SuperClassWriter::writeData(axis_label, axisid, isCollective, 0);
[1158]1223 
[1030]1224                SuperClassWriter::definition_start();
1225                break;
1226              }
1227              case ONE_FILE:
1228              {
1229                std::vector<StdSize> start(1), startBounds(2) ;
1230                std::vector<StdSize> count(1), countBounds(2) ;
[1099]1231                start[0] = startBounds[0] = zoom_begin - axis->global_zoom_begin;
[1235]1232                count[0] = countBounds[0] = zoom_count; // zoom_size
[1030]1233                startBounds[1] = 0;
1234                countBounds[1] = 2;
[633]1235
[1235]1236                if (axis->label.isEmpty())
1237                  SuperClassWriter::writeData(axis_value, axisid, isCollective, 0, &start, &count);
1238
1239                if (!axis->bounds.isEmpty() && axis->label.isEmpty())
[1129]1240                {
1241                  axis_bounds.resize(2, indexToWrite.numElements());
1242                  for (int i = 0; i < nbWritten; ++i)
1243                  {
1244                    axis_bounds(0, i) = axis->bounds(0, int(indexToWrite(i)));
1245                    axis_bounds(1, i) = axis->bounds(1, int(indexToWrite(i)));
1246                  }
[1030]1247                  SuperClassWriter::writeData(axis->bounds, axisBoundsId, isCollective, 0, &startBounds, &countBounds);
[1129]1248                }
[609]1249
[1158]1250                // Need to check after
[1235]1251                if (!axis->label.isEmpty())
1252                {
1253                  std::vector<StdSize> startLabel(2), countLabel(2);
1254                  startLabel[0] = start[0]; startLabel[1] = 0;
1255                  countLabel[0] = count[0]; countLabel[1] = stringArrayLen;
1256                  SuperClassWriter::writeData(axis_label, axisid, isCollective, 0, &startLabel, &countLabel);
1257                }
[1158]1258
[1030]1259                SuperClassWriter::definition_start();
[609]1260
[1030]1261                break;
[816]1262              }
[1030]1263              default :
1264                ERROR("CNc4DataOutput::writeAxis_(CAxis* axis)",
1265                      << "[ type = " << SuperClass::type << "]"
1266                      << " not implemented yet !");
[609]1267            }
1268          }
[1030]1269        }
1270        catch (CNetCdfException& e)
1271        {
1272          StdString msg("On writing the axis : ");
1273          msg.append(axisid); msg.append("\n");
1274          msg.append("In the context : ");
1275          CContext* context = CContext::getCurrent() ;
1276          msg.append(context->getId()); msg.append("\n");
1277          msg.append(e.what());
1278          ERROR("CNc4DataOutput::writeAxis_(CAxis* axis)", << msg);
1279        }
[1158]1280        axis->addRelFile(this->filename);
[391]1281     }
[488]1282
[887]1283      void CNc4DataOutput::writeScalar_(CScalar* scalar)
1284      {
1285        if (scalar->IsWritten(this->filename)) return;
1286        scalar->checkAttributes();
1287        int scalarSize = 1;
1288
1289        StdString scalaId = scalar->getScalarOutputName();
1290        if (isWrittenAxis(scalaId)) return ;
1291        else setWrittenAxis(scalaId);
1292
[1158]1293        nc_type typePrec ;
1294        if (scalar->prec.isEmpty()) typePrec =  NC_FLOAT ;
1295        else if (scalar->prec==4)  typePrec =  NC_FLOAT ;
1296        else if (scalar->prec==8)   typePrec =  NC_DOUBLE ;
1297
1298       
[887]1299        try
1300        {
1301          if (!scalar->value.isEmpty())
1302          {
1303//            dims.push_back(scalaId);
1304            std::vector<StdString> dims;
[1158]1305            SuperClassWriter::addVariable(scalaId, typePrec, dims);
[887]1306
1307            if (!scalar->name.isEmpty())
1308              SuperClassWriter::addAttribute("name", scalar->name.getValue(), &scalaId);
1309
1310            if (!scalar->standard_name.isEmpty())
1311              SuperClassWriter::addAttribute("standard_name", scalar->standard_name.getValue(), &scalaId);
1312
1313            if (!scalar->long_name.isEmpty())
1314              SuperClassWriter::addAttribute("long_name", scalar->long_name.getValue(), &scalaId);
1315
1316            if (!scalar->unit.isEmpty())
1317              SuperClassWriter::addAttribute("units", scalar->unit.getValue(), &scalaId);
1318
1319            SuperClassWriter::definition_end();
1320
1321            switch (SuperClass::type)
1322            {
1323              case MULTI_FILE:
1324              {
1325                CArray<double,1> scalarValue(scalarSize);
1326                scalarValue(0) = scalar->value;
1327                SuperClassWriter::writeData(scalarValue, scalaId, isCollective, 0);
1328                SuperClassWriter::definition_start();
1329
1330                break;
1331              }
1332              case ONE_FILE:
1333              {
1334                CArray<double,1> scalarValue(scalarSize);
1335                scalarValue(0) = scalar->value;
1336
1337                std::vector<StdSize> start(1);
1338                std::vector<StdSize> count(1);
1339                start[0] = 0;
1340                count[0] = 1;
1341                SuperClassWriter::writeData(scalarValue, scalaId, isCollective, 0, &start, &count);
1342                SuperClassWriter::definition_start();
1343
1344                break;
1345              }
1346              default :
1347                ERROR("CNc4DataOutput::writeAxis_(CAxis* scalar)",
1348                      << "[ type = " << SuperClass::type << "]"
1349                      << " not implemented yet !");
1350            }
1351          }
1352        }
1353        catch (CNetCdfException& e)
1354        {
1355          StdString msg("On writing the scalar : ");
1356          msg.append(scalaId); msg.append("\n");
1357          msg.append("In the context : ");
1358          CContext* context = CContext::getCurrent() ;
1359          msg.append(context->getId()); msg.append("\n");
1360          msg.append(e.what());
1361          ERROR("CNc4DataOutput::writeScalar_(CScalar* scalar)", << msg);
1362        }
1363        scalar->addRelFile(this->filename);
1364     }
1365
[676]1366     //--------------------------------------------------------------
1367
1368     void CNc4DataOutput::writeGridCompressed_(CGrid* grid)
1369     {
1370       if (grid->isScalarGrid() || grid->isWrittenCompressed(this->filename)) return;
1371
1372       try
1373       {
[887]1374         CArray<int,1> axisDomainOrder = grid->axis_domain_order;
[676]1375         std::vector<StdString> domainList = grid->getDomainList();
1376         std::vector<StdString> axisList   = grid->getAxisList();
[887]1377         std::vector<StdString> scalarList = grid->getScalarList();
1378         int numElement = axisDomainOrder.numElements(), idxDomain = 0, idxAxis = 0, idxScalar = 0;
[676]1379
1380         std::vector<StdString> dims;
1381
1382         if (grid->isCompressible())
1383         {
1384           StdString varId = grid->getId() + "_points";
1385
1386           int nbIndexes = (SuperClass::type == MULTI_FILE) ? grid->getNumberWrittenIndexes() : grid->getTotalNumberWrittenIndexes();
1387           SuperClassWriter::addDimension(varId, nbIndexes);
1388
1389           dims.push_back(varId);
1390           SuperClassWriter::addVariable(varId, NC_INT, dims);
1391
1392           StdOStringStream compress;
1393           for (int i = numElement - 1; i >= 0; --i)
1394           {
[887]1395             if (2 == axisDomainOrder(i))
[676]1396             {
1397               CDomain* domain = CDomain::get(domainList[domainList.size() - idxDomain - 1]);
[772]1398               StdString domId = domain->getDomainOutputName();
[676]1399               StdString appendDomId  = singleDomain ? "" : "_" + domId;
1400
1401               switch (domain->type)
1402               {
1403                 case CDomain::type_attr::curvilinear:
1404                   compress << "y" << appendDomId << " x" << appendDomId;
1405                   break;
1406                 case CDomain::type_attr::rectilinear:
1407                   compress << "lat" << appendDomId << " lon" << appendDomId;
1408                   break;
1409                 case CDomain::type_attr::unstructured:
1410                   compress << "cell" << appendDomId;
1411                   break;
1412               }
1413               ++idxDomain;
1414             }
[887]1415             else if (1 == axisDomainOrder(i))
[676]1416             {
1417               CAxis* axis = CAxis::get(axisList[axisList.size() - idxAxis - 1]);
[772]1418               compress << axis->getAxisOutputName();
[676]1419               ++idxAxis;
1420             }
[887]1421             else
1422             {
1423               CScalar* scalar = CScalar::get(scalarList[scalarList.size() - idxScalar - 1]);
1424               compress << scalar->getScalarOutputName();
1425               ++idxScalar;
1426             }
[676]1427
1428             if (i != 0) compress << ' ';
1429           }
[1144]1430           SuperClassWriter::addAttribute("compress", compress.str(), &varId);         
[676]1431
1432           CArray<int, 1> indexes(grid->getNumberWrittenIndexes());
[1143]1433           indexes = grid->localIndexToWriteOnServer;
[676]1434
1435           switch (SuperClass::type)
1436           {
1437             case (MULTI_FILE):
1438             {
1439               SuperClassWriter::writeData(indexes, varId, isCollective, 0);
1440               break;
1441             }
1442             case (ONE_FILE):
1443             {
1444               if (grid->doGridHaveDataDistributed())
1445                 grid->getDistributionServer()->computeGlobalIndex(indexes);
1446
1447               std::vector<StdSize> start, count;
1448               start.push_back(grid->getOffsetWrittenIndexes());
1449               count.push_back(grid->getNumberWrittenIndexes());
1450
1451               SuperClassWriter::writeData(indexes, varId, isCollective, 0, &start, &count);
1452               break;
1453             }
1454           }
1455         }
1456         else
1457         {
1458           for (int i = 0; i < numElement; ++i)
1459           {
1460             StdString varId, compress;
1461             CArray<int, 1> indexes;
1462             bool isDistributed;
1463             StdSize nbIndexes, totalNbIndexes, offset;
1464             int firstGlobalIndex;
1465
[887]1466             if (2 == axisDomainOrder(i))
[676]1467             {
1468               CDomain* domain = CDomain::get(domainList[idxDomain]);
[774]1469               StdString domId = domain->getDomainOutputName();
1470
[676]1471               if (!domain->isCompressible()
1472                    || domain->type == CDomain::type_attr::unstructured
[774]1473                    || domain->isWrittenCompressed(this->filename)
1474                    || isWrittenCompressedDomain(domId))
[676]1475                 continue;
1476
1477               StdString appendDomId  = singleDomain ? "" : "_" + domId;
1478
1479               varId = domId + "_points";
1480               switch (domain->type)
1481               {
1482                 case CDomain::type_attr::curvilinear:
1483                   compress = "y" + appendDomId + " x" + appendDomId;
1484                   break;
1485                 case CDomain::type_attr::rectilinear:
1486                   compress = "lat" + appendDomId + " lon" + appendDomId;
1487                   break;
1488               }
1489
[1143]1490               indexes.resize(domain->compressedIndexToWriteOnServer.numElements());
1491               indexes = domain->compressedIndexToWriteOnServer;
1492
[676]1493               isDistributed = domain->isDistributed();
1494               nbIndexes = domain->getNumberWrittenIndexes();
1495               totalNbIndexes = domain->getTotalNumberWrittenIndexes();
1496               offset = domain->getOffsetWrittenIndexes();
1497               firstGlobalIndex = domain->ibegin + domain->jbegin * domain->ni_glo;
1498
1499               domain->addRelFileCompressed(this->filename);
[774]1500               setWrittenCompressedDomain(domId);
[676]1501               ++idxDomain;
1502             }
[887]1503             else if (1 == axisDomainOrder(i))
[676]1504             {
1505               CAxis* axis = CAxis::get(axisList[idxAxis]);
[774]1506               StdString axisId = axis->getAxisOutputName();
1507
1508               if (!axis->isCompressible()
1509                    || axis->isWrittenCompressed(this->filename)
1510                    || isWrittenCompressedAxis(axisId))
[676]1511                 continue;
1512
1513               varId = axisId + "_points";
1514               compress = axisId;
1515
[1143]1516               indexes.resize(axis->compressedIndexToWriteOnServer.numElements());
1517               indexes = axis->compressedIndexToWriteOnServer;
1518
[676]1519               isDistributed = axis->isDistributed();
1520               nbIndexes = axis->getNumberWrittenIndexes();
1521               totalNbIndexes = axis->getTotalNumberWrittenIndexes();
1522               offset = axis->getOffsetWrittenIndexes();
1523               firstGlobalIndex = axis->begin;
1524
1525               axis->addRelFileCompressed(this->filename);
[774]1526               setWrittenCompressedAxis(axisId);
[676]1527               ++idxAxis;
1528             }
[887]1529             else
1530             {
1531             }
[676]1532
1533             if (!varId.empty())
1534             {
1535               SuperClassWriter::addDimension(varId, (SuperClass::type == MULTI_FILE) ? nbIndexes : totalNbIndexes);
1536
1537               dims.clear();
1538               dims.push_back(varId);
1539               SuperClassWriter::addVariable(varId, NC_INT, dims);
1540
1541               SuperClassWriter::addAttribute("compress", compress, &varId);
1542
1543               switch (SuperClass::type)
1544               {
1545                 case (MULTI_FILE):
1546                 {
1547                   indexes -= firstGlobalIndex;
1548                   SuperClassWriter::writeData(indexes, varId, isCollective, 0);
1549                   break;
1550                 }
1551                 case (ONE_FILE):
1552                 {
1553                   std::vector<StdSize> start, count;
1554                   start.push_back(offset);
1555                   count.push_back(nbIndexes);
1556
1557                   SuperClassWriter::writeData(indexes, varId, isCollective, 0, &start, &count);
1558                   break;
1559                 }
1560               }
1561             }
[1144]1562            }
[676]1563         }
1564
1565         grid->addRelFileCompressed(this->filename);
1566       }
1567       catch (CNetCdfException& e)
1568       {
1569         StdString msg("On writing compressed grid : ");
1570         msg.append(grid->getId()); msg.append("\n");
1571         msg.append("In the context : ");
1572         CContext* context = CContext::getCurrent();
1573         msg.append(context->getId()); msg.append("\n");
1574         msg.append(e.what());
1575         ERROR("CNc4DataOutput::writeGridCompressed_(CGrid* grid)", << msg);
1576       }
1577     }
1578
1579     //--------------------------------------------------------------
1580
[391]1581     void CNc4DataOutput::writeTimeDimension_(void)
1582     {
[498]1583       try
1584       {
[802]1585        SuperClassWriter::addDimension(getTimeCounterName());
[498]1586       }
1587       catch (CNetCdfException& e)
1588       {
[614]1589         StdString msg("On writing time dimension : time_couter\n");
[498]1590         msg.append("In the context : ");
1591         CContext* context = CContext::getCurrent() ;
1592         msg.append(context->getId()); msg.append("\n");
1593         msg.append(e.what());
1594         ERROR("CNc4DataOutput::writeTimeDimension_(void)", << msg);
1595       }
[391]1596     }
[676]1597
[219]1598      //--------------------------------------------------------------
1599
[347]1600      void CNc4DataOutput::writeField_(CField* field)
[219]1601      {
[1158]1602        CContext* context = CContext::getCurrent() ;
1603        CContextServer* server=context->server ;
[300]1604
[1158]1605        std::vector<StdString> dims, coodinates;
1606        CGrid* grid = field->grid;
1607        if (!grid->doGridHaveDataToWrite())
[567]1608          if (SuperClass::type==MULTI_FILE) return ;
[488]1609
[1158]1610        CArray<int,1> axisDomainOrder = grid->axis_domain_order;
1611        int numElement = axisDomainOrder.numElements(), idxDomain = 0, idxAxis = 0, idxScalar = 0;
1612        std::vector<StdString> domainList = grid->getDomainList();
1613        std::vector<StdString> axisList   = grid->getAxisList();
1614        std::vector<StdString> scalarList = grid->getScalarList();       
[219]1615
[1158]1616        StdString timeid  = getTimeCounterName();
1617        StdString dimXid,dimYid;
1618        std::deque<StdString> dimIdList, dimCoordList;
1619        bool hasArea = false;
1620        StdString cellMeasures = "area:";
1621        bool compressedOutput = !field->indexed_output.isEmpty() && field->indexed_output;
[488]1622
[1158]1623        for (int i = 0; i < numElement; ++i)
1624        {
1625          if (2 == axisDomainOrder(i))
1626          {
1627            CDomain* domain = CDomain::get(domainList[idxDomain]);
1628            StdString domId = domain->getDomainOutputName();
1629            StdString appendDomId  = singleDomain ? "" : "_" + domId ;
[676]1630
[1158]1631            if (compressedOutput && domain->isCompressible() && domain->type != CDomain::type_attr::unstructured)
1632            {
1633              dimIdList.push_back(domId + "_points");
1634              field->setUseCompressedOutput();
1635            }
[676]1636
[1158]1637            switch (domain->type)
[879]1638            {
[1158]1639              case CDomain::type_attr::curvilinear:
1640                if (!compressedOutput || !domain->isCompressible())
1641                {
1642                  dimXid     = StdString("x").append(appendDomId);
1643                  dimIdList.push_back(dimXid);
1644                  dimYid     = StdString("y").append(appendDomId);
1645                  dimIdList.push_back(dimYid);
1646                }
1647                dimCoordList.push_back(StdString("nav_lon").append(appendDomId));
1648                dimCoordList.push_back(StdString("nav_lat").append(appendDomId));
1649              break ;
1650              case CDomain::type_attr::rectilinear:
1651                if (!compressedOutput || !domain->isCompressible())
1652                {
1653                  dimXid     = StdString("lon").append(appendDomId);
1654                  dimIdList.push_back(dimXid);
1655                  dimYid     = StdString("lat").append(appendDomId);
1656                  dimIdList.push_back(dimYid);
1657                }
1658              break ;
1659              case CDomain::type_attr::unstructured:
1660              {
1661                if (SuperClassWriter::useCFConvention)
1662                {
1663                  dimXid     = StdString("cell").append(appendDomId);
1664                  dimIdList.push_back(dimXid);
1665                  dimCoordList.push_back(StdString("lon").append(appendDomId));
1666                  dimCoordList.push_back(StdString("lat").append(appendDomId));
1667                }
1668                else
1669                {
1670                  StdString domainName = domain->name;
1671                  if (domain->nvertex == 1)
1672                  {
1673                    dimXid     = "n" + domainName + "_node";
1674                    dimIdList.push_back(dimXid);
1675                    dimCoordList.push_back(StdString(domainName + "_node_x"));
1676                    dimCoordList.push_back(StdString(domainName + "_node_y"));
1677                  }
1678                  else if (domain->nvertex == 2)
1679                  {
1680                    dimXid     = "n" + domainName + "_edge";
1681                    dimIdList.push_back(dimXid);
1682                    dimCoordList.push_back(StdString(domainName + "_edge_x"));
1683                    dimCoordList.push_back(StdString(domainName + "_edge_y"));
1684                  }
1685                  else
1686                  {
1687                    dimXid     = "n" + domainName + "_face";
1688                    dimIdList.push_back(dimXid);
1689                    dimCoordList.push_back(StdString(domainName + "_face_x"));
1690                    dimCoordList.push_back(StdString(domainName + "_face_y"));
1691                  }
1692                }  // ugrid convention
1693              }  // case unstructured domain
[879]1694            }
[1158]1695
1696            if (domain->hasArea)
[879]1697            {
[1158]1698              hasArea = true;
1699              cellMeasures += " area" + appendDomId;
[879]1700            }
[1158]1701            ++idxDomain;
1702          }
1703          else if (1 == axisDomainOrder(i))
1704          {
1705            CAxis* axis = CAxis::get(axisList[idxAxis]);
1706            StdString axisId = axis->getAxisOutputName();
1707
1708            if (compressedOutput && axis->isCompressible())
[879]1709            {
[1158]1710              dimIdList.push_back(axisId + "_points");
1711              field->setUseCompressedOutput();
[879]1712            }
[1158]1713            else
1714              dimIdList.push_back(axisId);
[878]1715
[1158]1716            dimCoordList.push_back(axisId);
1717            ++idxAxis;
1718          }
1719          else // scalar
1720          {
[887]1721             /* Do nothing here */
[1158]1722          }
1723        }
[488]1724
[1158]1725        StdString fieldid = field->getFieldOutputName();
[219]1726
[1158]1727        nc_type type ;
1728        if (field->prec.isEmpty()) type =  NC_FLOAT ;
1729        else
1730        {
1731          if (field->prec==2) type = NC_SHORT ;
1732          else if (field->prec==4)  type =  NC_FLOAT ;
1733          else if (field->prec==8)   type =  NC_DOUBLE ;
1734        }
[488]1735
[1158]1736        bool wtime   = !(!field->operation.isEmpty() && field->getOperationTimeType() == func::CFunctor::once);
[488]1737
[1158]1738        if (wtime)
1739        {
1740          if (field->hasTimeInstant && hasTimeInstant) coodinates.push_back(string("time_instant"));
1741          else if (field->hasTimeCentered && hasTimeCentered)  coodinates.push_back(string("time_centered"));
1742          dims.push_back(timeid);
1743        }
[488]1744
[1158]1745        if (compressedOutput && grid->isCompressible())
1746        {
1747          dims.push_back(grid->getId() + "_points");
1748          field->setUseCompressedOutput();
1749        }
1750        else
1751        {
1752          while (!dimIdList.empty())
1753          {
1754            dims.push_back(dimIdList.back());
1755            dimIdList.pop_back();
1756          }
1757        }
[219]1758
[1158]1759        while (!dimCoordList.empty())
1760        {
1761          coodinates.push_back(dimCoordList.back());
1762          dimCoordList.pop_back();
1763        }
[219]1764
[1158]1765        try
1766        {
[498]1767           SuperClassWriter::addVariable(fieldid, type, dims);
[488]1768
[498]1769           if (!field->standard_name.isEmpty())
1770              SuperClassWriter::addAttribute
1771                 ("standard_name",  field->standard_name.getValue(), &fieldid);
[219]1772
[498]1773           if (!field->long_name.isEmpty())
1774              SuperClassWriter::addAttribute
1775                 ("long_name", field->long_name.getValue(), &fieldid);
[219]1776
[498]1777           if (!field->unit.isEmpty())
1778              SuperClassWriter::addAttribute
1779                 ("units", field->unit.getValue(), &fieldid);
[463]1780
[1158]1781           // Ugrid field attributes "mesh" and "location"
1782           if (!SuperClassWriter::useCFConvention)
1783           {
1784            if (!domainList.empty())
1785            {
1786              CDomain* domain = CDomain::get(domainList[0]); // Suppose that we have only domain
1787              StdString mesh = domain->name;
1788              SuperClassWriter::addAttribute("mesh", mesh, &fieldid);
1789              StdString location;
1790              if (domain->nvertex == 1)
1791                location = "node";
1792              else if (domain->nvertex == 2)
1793                location = "edge";
1794              else if (domain->nvertex > 2)
1795                location = "face";
1796              SuperClassWriter::addAttribute("location", location, &fieldid);
1797            }
1798
1799           }
1800
1801           if (!field->valid_min.isEmpty())
[498]1802              SuperClassWriter::addAttribute
1803                 ("valid_min", field->valid_min.getValue(), &fieldid);
[463]1804
[498]1805           if (!field->valid_max.isEmpty())
1806              SuperClassWriter::addAttribute
1807                 ("valid_max", field->valid_max.getValue(), &fieldid);
[464]1808
[498]1809            if (!field->scale_factor.isEmpty())
1810              SuperClassWriter::addAttribute
1811                 ("scale_factor", field->scale_factor.getValue(), &fieldid);
[464]1812
[498]1813             if (!field->add_offset.isEmpty())
1814              SuperClassWriter::addAttribute
1815                 ("add_offset", field->add_offset.getValue(), &fieldid);
[488]1816
[498]1817           SuperClassWriter::addAttribute
1818                 ("online_operation", field->operation.getValue(), &fieldid);
[472]1819
[498]1820          // write child variables as attributes
[488]1821
1822
[1021]1823           bool alreadyAddCellMethod = false;
1824           StdString cellMethodsPrefix(""), cellMethodsSuffix("");
1825           if (!field->cell_methods.isEmpty())
1826           {
[1158]1827              StdString cellMethodString = field->cell_methods;
1828              if (field->cell_methods_mode.isEmpty() ||
[1021]1829                 (CField::cell_methods_mode_attr::overwrite == field->cell_methods_mode))
[1158]1830              {
1831                SuperClassWriter::addAttribute("cell_methods", cellMethodString, &fieldid);
1832                alreadyAddCellMethod = true;
1833              }
1834              else
1835              {
1836                switch (field->cell_methods_mode)
1837                {
1838                  case (CField::cell_methods_mode_attr::prefix):
1839                    cellMethodsPrefix = cellMethodString;
1840                    cellMethodsPrefix += " ";
1841                    break;
1842                  case (CField::cell_methods_mode_attr::suffix):
1843                    cellMethodsSuffix = " ";
1844                    cellMethodsSuffix += cellMethodString;
1845                    break;
1846                  case (CField::cell_methods_mode_attr::none):
1847                    break;
1848                  default:
1849                    break;
1850                }
1851              }
[1021]1852           }
[488]1853
[1021]1854
[498]1855           if (wtime)
1856           {
[614]1857              CDuration freqOp = field->freq_op.getValue();
1858              freqOp.solveTimeStep(*context->calendar);
1859              StdString freqOpStr = freqOp.toStringUDUnits();
[612]1860              SuperClassWriter::addAttribute("interval_operation", freqOpStr, &fieldid);
[437]1861
[614]1862              CDuration freqOut = field->getRelFile()->output_freq.getValue();
1863              freqOut.solveTimeStep(*context->calendar);
1864              SuperClassWriter::addAttribute("interval_write", freqOut.toStringUDUnits(), &fieldid);
[612]1865
[1021]1866              StdString cellMethods(cellMethodsPrefix + "time: ");
[612]1867              if (field->operation.getValue() == "instant") cellMethods += "point";
1868              else if (field->operation.getValue() == "average") cellMethods += "mean";
1869              else if (field->operation.getValue() == "accumulate") cellMethods += "sum";
1870              else cellMethods += field->operation;
[614]1871              if (freqOp.resolve(*context->calendar) != freqOut.resolve(*context->calendar))
1872                cellMethods += " (interval: " + freqOpStr + ")";
[1021]1873              cellMethods += cellMethodsSuffix;
1874              if (!alreadyAddCellMethod)
1875                SuperClassWriter::addAttribute("cell_methods", cellMethods, &fieldid);
[498]1876           }
[488]1877
[611]1878           if (hasArea)
1879             SuperClassWriter::addAttribute("cell_measures", cellMeasures, &fieldid);
1880
[498]1881           if (!field->default_value.isEmpty())
1882           {
1883              double default_value = field->default_value.getValue();
1884              float fdefault_value = (float)default_value;
1885              if (type == NC_DOUBLE)
1886                 SuperClassWriter::setDefaultValue(fieldid, &default_value);
1887              else
1888                 SuperClassWriter::setDefaultValue(fieldid, &fdefault_value);
1889           }
1890           else
[517]1891              SuperClassWriter::setDefaultValue(fieldid, (double*)NULL);
[219]1892
[606]1893            if (field->compression_level.isEmpty())
1894              field->compression_level = field->file->compression_level.isEmpty() ? 0 : field->file->compression_level;
1895            SuperClassWriter::setCompressionLevel(fieldid, field->compression_level);
1896
[878]1897           {  // Ecriture des coordonnes
[488]1898
[498]1899              StdString coordstr; //boost::algorithm::join(coodinates, " ")
1900              std::vector<StdString>::iterator
1901                 itc = coodinates.begin(), endc = coodinates.end();
[488]1902
[498]1903              for (; itc!= endc; itc++)
1904              {
1905                 StdString & coord = *itc;
1906                 if (itc+1 != endc)
1907                       coordstr.append(coord).append(" ");
1908                 else  coordstr.append(coord);
1909              }
[219]1910
[498]1911              SuperClassWriter::addAttribute("coordinates", coordstr, &fieldid);
[219]1912
[498]1913           }
[1222]1914
1915           vector<CVariable*> listVars = field->getAllVariables() ;
1916           for (vector<CVariable*>::iterator it = listVars.begin() ;it != listVars.end(); it++) writeAttribute_(*it, fieldid) ;
1917
[219]1918         }
[498]1919         catch (CNetCdfException& e)
1920         {
1921           StdString msg("On writing field : ");
1922           msg.append(fieldid); msg.append("\n");
1923           msg.append("In the context : ");
1924           msg.append(context->getId()); msg.append("\n");
1925           msg.append(e.what());
1926           ERROR("CNc4DataOutput::writeField_(CField* field)", << msg);
1927         }
[879]1928      } // writeField_()
[219]1929
1930      //--------------------------------------------------------------
1931
[347]1932      void CNc4DataOutput::writeFile_ (CFile* file)
[219]1933      {
[773]1934         StdString filename = file->getFileOutputName();
[219]1935         StdString description = (!file->description.isEmpty())
1936                               ? file->description.getValue()
[335]1937                               : StdString("Created by xios");
[609]1938
1939         singleDomain = (file->nbDomains == 1);
1940
[1158]1941         StdString conv_str ;
1942         if (file->convention_str.isEmpty())
1943         {
1944            if (SuperClassWriter::useCFConvention) conv_str="CF-1.6" ;
1945            else conv_str="UGRID" ;
1946         }
1947         else conv_str=file->convention_str ;
1948           
[498]1949         try
1950         {
[1158]1951           this->writeFileAttributes(filename, description,
1952                                      conv_str,
1953                                      StdString("An IPSL model"),
1954                                      this->getTimeStamp());
[609]1955
[701]1956           if (!appendMode)
1957             SuperClassWriter::addDimension("axis_nbounds", 2);
[498]1958         }
1959         catch (CNetCdfException& e)
1960         {
1961           StdString msg("On writing file : ");
1962           msg.append(filename); msg.append("\n");
1963           msg.append("In the context : ");
1964           CContext* context = CContext::getCurrent() ;
1965           msg.append(context->getId()); msg.append("\n");
1966           msg.append(e.what());
1967           ERROR("CNc4DataOutput::writeFile_ (CFile* file)", << msg);
1968         }
[219]1969      }
[488]1970
[472]1971      void CNc4DataOutput::writeAttribute_ (CVariable* var, const string& fieldId)
1972      {
[773]1973        StdString name = var->getVariableOutputName();
[488]1974
[498]1975        try
1976        {
[527]1977          if (var->type.getValue() == CVariable::type_attr::t_int || var->type.getValue() == CVariable::type_attr::t_int32)
1978            addAttribute(name, var->getData<int>(), &fieldId);
1979          else if (var->type.getValue() == CVariable::type_attr::t_int16)
1980            addAttribute(name, var->getData<short int>(), &fieldId);
1981          else if (var->type.getValue() == CVariable::type_attr::t_float)
1982            addAttribute(name, var->getData<float>(), &fieldId);
1983          else if (var->type.getValue() == CVariable::type_attr::t_double)
1984            addAttribute(name, var->getData<double>(), &fieldId);
1985          else if (var->type.getValue() == CVariable::type_attr::t_string)
1986            addAttribute(name, var->getData<string>(), &fieldId);
1987          else
1988            ERROR("CNc4DataOutput::writeAttribute_ (CVariable* var, const string& fieldId)",
1989                  << "Unsupported variable of type " << var->type.getStringValue());
[498]1990        }
1991       catch (CNetCdfException& e)
1992       {
1993         StdString msg("On writing attributes of variable with name : ");
1994         msg.append(name); msg.append("in the field "); msg.append(fieldId); msg.append("\n");
1995         msg.append("In the context : ");
1996         CContext* context = CContext::getCurrent() ;
1997         msg.append(context->getId()); msg.append("\n");
1998         msg.append(e.what());
1999         ERROR("CNc4DataOutput::writeAttribute_ (CVariable* var, const string& fieldId)", << msg);
2000       }
[472]2001     }
[488]2002
[472]2003     void CNc4DataOutput::writeAttribute_ (CVariable* var)
2004     {
[773]2005        StdString name = var->getVariableOutputName();
2006
[498]2007        try
2008        {
[527]2009          if (var->type.getValue() == CVariable::type_attr::t_int || var->type.getValue() == CVariable::type_attr::t_int32)
2010            addAttribute(name, var->getData<int>());
2011          else if (var->type.getValue() == CVariable::type_attr::t_int16)
2012            addAttribute(name, var->getData<short int>());
2013          else if (var->type.getValue() == CVariable::type_attr::t_float)
2014            addAttribute(name, var->getData<float>());
2015          else if (var->type.getValue() == CVariable::type_attr::t_double)
2016            addAttribute(name, var->getData<double>());
2017          else if (var->type.getValue() == CVariable::type_attr::t_string)
2018            addAttribute(name, var->getData<string>());
2019          else
2020            ERROR("CNc4DataOutput::writeAttribute_ (CVariable* var)",
2021                  << "Unsupported variable of type " << var->type.getStringValue());
[498]2022        }
2023       catch (CNetCdfException& e)
2024       {
2025         StdString msg("On writing attributes of variable with name : ");
2026         msg.append(name); msg.append("\n");
2027         msg.append("In the context : ");
2028         CContext* context = CContext::getCurrent() ;
2029         msg.append(context->getId()); msg.append("\n");
2030         msg.append(e.what());
2031         ERROR("CNc4DataOutput::writeAttribute_ (CVariable* var)", << msg);
2032       }
[488]2033     }
2034
[321]2035      void CNc4DataOutput::syncFile_ (void)
2036      {
[498]2037        try
2038        {
2039          SuperClassWriter::sync() ;
2040        }
2041        catch (CNetCdfException& e)
2042        {
2043         StdString msg("On synchronizing the write among processes");
2044         msg.append("In the context : ");
2045         CContext* context = CContext::getCurrent() ;
2046         msg.append(context->getId()); msg.append("\n");
2047         msg.append(e.what());
2048         ERROR("CNc4DataOutput::syncFile_ (void)", << msg);
2049        }
[321]2050      }
[219]2051
[286]2052      void CNc4DataOutput::closeFile_ (void)
2053      {
[498]2054        try
2055        {
2056          SuperClassWriter::close() ;
2057        }
2058        catch (CNetCdfException& e)
2059        {
2060         StdString msg("On closing file");
2061         msg.append("In the context : ");
2062         CContext* context = CContext::getCurrent() ;
2063         msg.append(context->getId()); msg.append("\n");
2064         msg.append(e.what());
2065         ERROR("CNc4DataOutput::syncFile_ (void)", << msg);
2066        }
2067
[286]2068      }
2069
[219]2070      //---------------------------------------------------------------
2071
2072      StdString CNc4DataOutput::getTimeStamp(void) const
2073      {
2074         const int buffer_size = 100;
2075         time_t rawtime;
2076         struct tm * timeinfo = NULL;
2077         char buffer [buffer_size];
[1158]2078         StdString formatStr;
2079         if (file->time_stamp_format.isEmpty()) formatStr="%Y-%b-%d %H:%M:%S %Z" ;
2080         else formatStr=file->time_stamp_format;
[219]2081
[1158]2082//         time ( &rawtime );
2083//         timeinfo = localtime ( &rawtime );
[219]2084         time ( &rawtime );
[1158]2085         timeinfo = gmtime ( &rawtime );
2086         strftime (buffer, buffer_size, formatStr.c_str(), timeinfo);
[219]2087
2088         return (StdString(buffer));
2089      }
[488]2090
[219]2091      //---------------------------------------------------------------
[488]2092
[347]2093      void CNc4DataOutput::writeFieldData_ (CField*  field)
[219]2094      {
[707]2095        CContext* context = CContext::getCurrent();
2096        CContextServer* server = context->server;
2097        CGrid* grid = field->grid;
[300]2098
[1158]2099        if (field->getNStep()<1) 
2100        {
2101          return;
2102        }
[952]2103       
[707]2104        if (!grid->doGridHaveDataToWrite())
[1158]2105          if (SuperClass::type == MULTI_FILE || !isCollective)
2106          {
2107            return;
2108          }
[488]2109
[770]2110        StdString fieldid = field->getFieldOutputName();
[286]2111
[707]2112        StdOStringStream oss;
2113        string timeAxisId;
[1158]2114        if (field->hasTimeInstant) timeAxisId = "time_instant";
2115        else if (field->hasTimeCentered) timeAxisId = "time_centered";
[488]2116
[802]2117        StdString timeBoundId = getTimeCounterName() + "_bounds";
[449]2118
[707]2119        StdString timeAxisBoundId;
[1158]2120        if (field->hasTimeInstant) timeAxisBoundId = "time_instant_bounds";
2121        else if (field->hasTimeCentered) timeAxisBoundId = "time_centered_bounds";
[488]2122
[707]2123        if (!field->wasWritten())
2124        {
[1158]2125          if (appendMode && field->file->record_offset.isEmpty() && 
2126              field->getOperationTimeType() != func::CFunctor::once)
[707]2127          {
[1158]2128            double factorUnit;
2129            if (!field->file->time_units.isEmpty() && field->file->time_units==CFile::time_units_attr::days)
2130            factorUnit=context->getCalendar()->getDayLengthInSeconds() ;
2131            else factorUnit=1 ;
2132            field->resetNStep(getRecordFromTime(field->last_Write_srv,factorUnit) + 1);
[707]2133          }
[488]2134
[707]2135          field->setWritten();
2136        }
[488]2137
2138
[707]2139        CArray<double,1> time_data(1);
2140        CArray<double,1> time_data_bound(2);
2141        CArray<double,1> time_counter(1);
2142        CArray<double,1> time_counter_bound(2);
2143
2144        bool wtime = (field->getOperationTimeType() != func::CFunctor::once);
[1158]2145        bool wtimeCounter =false ;
2146        bool wtimeData =false ;
2147       
[707]2148
[444]2149        if (wtime)
2150        {
[692]2151          Time lastWrite = field->last_Write_srv;
2152          Time lastLastWrite = field->lastlast_Write_srv;
2153
[1158]2154         
2155          if (field->hasTimeInstant)
2156          {
2157            time_data(0) = time_data_bound(1) = lastWrite;
[692]2158            time_data_bound(0) = time_data_bound(1) = lastWrite;
[1158]2159            if (timeCounterType==instant)
2160            {
2161              time_counter(0) = time_data(0);
2162              time_counter_bound(0) = time_data_bound(0);
2163              time_counter_bound(1) = time_data_bound(1);
2164              wtimeCounter=true ;
2165            }
2166            if (hasTimeInstant) wtimeData=true ;
2167          }
2168          else if (field->hasTimeCentered)
[488]2169          {
[1158]2170            time_data(0) = (lastWrite + lastLastWrite) / 2;
[692]2171            time_data_bound(0) = lastLastWrite;
2172            time_data_bound(1) = lastWrite;
[1158]2173            if (timeCounterType==centered)
2174            {
2175              time_counter(0) = time_data(0) ;
2176              time_counter_bound(0) = time_data_bound(0) ;
2177              time_counter_bound(1) = time_data_bound(1) ;
2178              wtimeCounter=true ;
2179            }
2180            if (hasTimeCentered) wtimeData=true ;
[488]2181          }
2182
[1158]2183          if (timeCounterType==record)
2184          {
[692]2185            time_counter(0) = field->getNStep() - 1;
[1158]2186            time_counter_bound(0) = time_counter_bound(1) = field->getNStep() - 1;
2187            wtimeCounter=true ;
2188          }
[692]2189
[1158]2190          if (!field->file->time_units.isEmpty() && field->file->time_units==CFile::time_units_attr::days)
[692]2191          {
[1158]2192            double secByDay=context->getCalendar()->getDayLengthInSeconds() ;
2193            time_data/=secByDay;
2194            time_data_bound/=secByDay;
2195            time_counter/=secByDay;
2196            time_counter_bound/=secByDay;
[692]2197          }
2198        }
2199
[707]2200         bool isRoot = (server->intraCommRank == 0);
[488]2201
[464]2202         if (!field->scale_factor.isEmpty() || !field->add_offset.isEmpty())
2203         {
[707]2204           double scaleFactor = 1.0;
2205           double addOffset = 0.0;
2206           if (!field->scale_factor.isEmpty()) scaleFactor = field->scale_factor;
2207           if (!field->add_offset.isEmpty()) addOffset = field->add_offset;
2208           field->scaleFactorAddOffset(scaleFactor, addOffset);
[464]2209         }
[488]2210
[498]2211         try
[219]2212         {
[676]2213           size_t writtenSize;
2214           if (field->getUseCompressedOutput())
2215             writtenSize = grid->getNumberWrittenIndexes();
2216           else
2217             writtenSize = grid->getWrittenDataSize();
2218
2219           CArray<double,1> fieldData(writtenSize);
[567]2220           if (!field->default_value.isEmpty()) fieldData = field->default_value;
[676]2221
2222           if (field->getUseCompressedOutput())
2223             field->outputCompressedField(fieldData);
2224           else
2225             field->outputField(fieldData);
2226
[707]2227           if (!field->prec.isEmpty() && field->prec == 2) fieldData = round(fieldData);
[567]2228
2229           switch (SuperClass::type)
[498]2230           {
[567]2231              case (MULTI_FILE) :
2232              {
[1158]2233                 CTimer::get("Files : writing data").resume();
[692]2234                 SuperClassWriter::writeData(fieldData, fieldid, isCollective, field->getNStep() - 1);
[1158]2235                 CTimer::get("Files : writing data").suspend();
[567]2236                 if (wtime)
2237                 {
[1158]2238                   CTimer::get("Files : writing time axis").resume();
2239                   if ( wtimeData)
[692]2240                   {
[1158]2241//                     SuperClassWriter::writeData(time_data, timeAxisId, isCollective, field->getNStep() - 1);
2242//                     SuperClassWriter::writeData(time_data_bound, timeAxisBoundId, isCollective, field->getNStep() - 1);
2243                       SuperClassWriter::writeTimeAxisData(time_data, timeAxisId, isCollective, field->getNStep() - 1, isRoot);
2244                       SuperClassWriter::writeTimeAxisDataBounds(time_data_bound, timeAxisBoundId, isCollective, field->getNStep() - 1, isRoot);
2245                  }
2246                   if (wtimeCounter)
2247                   {
2248//                     SuperClassWriter::writeData(time_counter, getTimeCounterName(), isCollective, field->getNStep() - 1);
2249//                     if (timeCounterType!=record) SuperClassWriter::writeData(time_counter_bound, timeBoundId, isCollective, field->getNStep() - 1);
2250                     SuperClassWriter::writeTimeAxisData(time_counter, getTimeCounterName(), isCollective, field->getNStep() - 1,isRoot);
2251                     if (timeCounterType!=record) SuperClassWriter::writeTimeAxisDataBounds(time_counter_bound, timeBoundId, isCollective, field->getNStep() - 1, isRoot);
[692]2252                   }
[1158]2253                   CTimer::get("Files : writing time axis").suspend();
[567]2254                 }
[707]2255                 break;
[567]2256              }
2257              case (ONE_FILE) :
2258              {
[676]2259                const std::vector<int>& nZoomBeginGlobal = grid->getDistributionServer()->getZoomBeginGlobal();
2260                const std::vector<int>& nZoomBeginServer = grid->getDistributionServer()->getZoomBeginServer();
2261                const std::vector<int>& nZoomSizeServer  = grid->getDistributionServer()->getZoomSizeServer();
[464]2262
[676]2263                std::vector<StdSize> start, count;
[464]2264
[676]2265                if (field->getUseCompressedOutput())
2266                {
2267                  if (grid->isCompressible())
2268                  {
2269                    start.push_back(grid->getOffsetWrittenIndexes());
2270                    count.push_back(grid->getNumberWrittenIndexes());
2271                  }
2272                  else
2273                  {
[887]2274                    CArray<int,1> axisDomainOrder = grid->axis_domain_order;
[676]2275                    std::vector<StdString> domainList = grid->getDomainList();
2276                    std::vector<StdString> axisList   = grid->getAxisList();
2277                    int numElement = axisDomainOrder.numElements();
2278                    int idxDomain = domainList.size() - 1, idxAxis = axisList.size() - 1;
2279                    int idx = nZoomBeginGlobal.size() - 1;
[464]2280
[676]2281                    start.reserve(nZoomBeginGlobal.size());
2282                    count.reserve(nZoomBeginGlobal.size());
2283
2284
2285                    for (int i = numElement - 1; i >= 0; --i)
2286                    {
[887]2287                      if (2 == axisDomainOrder(i))
[676]2288                      {
2289                        CDomain* domain = CDomain::get(domainList[idxDomain]);
2290
2291                        if (domain->isCompressible())
2292                        {
2293                          start.push_back(domain->getOffsetWrittenIndexes());
2294                          count.push_back(domain->getNumberWrittenIndexes());
[710]2295                          idx -= 2;
[676]2296                        }
2297                        else
2298                        {
[710]2299                          if ((domain->type) != CDomain::type_attr::unstructured)
2300                          {
2301                            start.push_back(nZoomBeginServer[idx] - nZoomBeginGlobal[idx]);
2302                            count.push_back(nZoomSizeServer[idx]);
2303                          }
2304                          --idx;
[676]2305                          start.push_back(nZoomBeginServer[idx] - nZoomBeginGlobal[idx]);
2306                          count.push_back(nZoomSizeServer[idx]);
[710]2307                          --idx;
[676]2308                        }
2309                        --idxDomain;
2310                      }
[887]2311                      else if (1 == axisDomainOrder(i))
[676]2312                      {
2313                        CAxis* axis = CAxis::get(axisList[idxAxis]);
2314
2315                        if (axis->isCompressible())
2316                        {
2317                          start.push_back(axis->getOffsetWrittenIndexes());
2318                          count.push_back(axis->getNumberWrittenIndexes());
2319                        }
2320                        else
2321                        {
2322                          start.push_back(nZoomBeginServer[idx] - nZoomBeginGlobal[idx]);
2323                          count.push_back(nZoomSizeServer[idx]);
2324                        }
2325
2326                        --idxAxis;
2327                        --idx;
2328                      }
2329                    }
[1158]2330                  }
[676]2331                }
2332                else
[498]2333                {
[887]2334                  CArray<int,1> axisDomainOrder = grid->axis_domain_order;
[705]2335                  std::vector<StdString> domainList = grid->getDomainList();
2336                  std::vector<StdString> axisList   = grid->getAxisList();
2337                  int numElement = axisDomainOrder.numElements();
2338                  int idxDomain = domainList.size() - 1, idxAxis = axisList.size() - 1;
[1025]2339                  int idx = domainList.size() * 2 + axisList.size() - 1;// nZoomBeginGlobal.size() - 1;
[705]2340
2341                  start.reserve(nZoomBeginGlobal.size());
2342                  count.reserve(nZoomBeginGlobal.size());
2343
2344                  for (int i = numElement - 1; i >= 0; --i)
2345                  {
[887]2346                    if (2 == axisDomainOrder(i))
[705]2347                    {
2348                      CDomain* domain = CDomain::get(domainList[idxDomain]);
[710]2349                      if ((domain->type) != CDomain::type_attr::unstructured)
[705]2350                      {
[1099]2351                        start.push_back(domain->zoom_jbegin - domain->global_zoom_jbegin);
2352                        count.push_back(domain->zoom_nj);
[705]2353                      }
2354                      --idx ;
[1143]2355
[1099]2356                        start.push_back(domain->zoom_ibegin - domain->global_zoom_ibegin);
2357                        count.push_back(domain->zoom_ni);
[705]2358                      --idx ;
2359                      --idxDomain;
2360                    }
[887]2361                    else if (1 == axisDomainOrder(i))
[705]2362                    {
[1143]2363                        CAxis* axis = CAxis::get(axisList[idxAxis]);
[1099]2364                        start.push_back(axis->zoom_begin - axis->global_zoom_begin);
2365                        count.push_back(axis->zoom_n);
[705]2366                      --idx;
[1025]2367                      --idxAxis;
[887]2368                    }
2369                    else
2370                    {
2371                      if (1 == axisDomainOrder.numElements())
2372                      {
2373                        start.push_back(0);
2374                        count.push_back(1);
2375                      }
2376                      --idx;
2377                    }
[705]2378                  }
[498]2379                }
[488]2380
[1158]2381
2382                CTimer::get("Files : writing data").resume();
[676]2383                SuperClassWriter::writeData(fieldData, fieldid, isCollective, field->getNStep() - 1, &start, &count);
[1158]2384                CTimer::get("Files : writing data").suspend();
2385
2386                 if (wtime)
2387                 {
2388                   CTimer::get("Files : writing time axis").resume();
2389                   if ( wtimeData)
[692]2390                   {
[1158]2391//                     SuperClassWriter::writeData(time_data, timeAxisId, isCollective, field->getNStep() - 1);
2392//                     SuperClassWriter::writeData(time_data_bound, timeAxisBoundId, isCollective, field->getNStep() - 1);
2393                     SuperClassWriter::writeTimeAxisData(time_data, timeAxisId, isCollective, field->getNStep() - 1, isRoot);
2394                     SuperClassWriter::writeTimeAxisDataBounds(time_data_bound, timeAxisBoundId, isCollective, field->getNStep() - 1, isRoot);
[692]2395                   }
[1158]2396                   if (wtimeCounter)
2397                   {
2398//                     SuperClassWriter::writeData(time_counter, getTimeCounterName(), isCollective, field->getNStep() - 1);
2399//                     if (timeCounterType!=record) SuperClassWriter::writeData(time_counter_bound, timeBoundId, isCollective, field->getNStep() - 1);
2400                     SuperClassWriter::writeTimeAxisData(time_counter, getTimeCounterName(), isCollective, field->getNStep() - 1,isRoot);
2401                     if (timeCounterType!=record) SuperClassWriter::writeTimeAxisDataBounds(time_counter_bound, timeBoundId, isCollective, field->getNStep() - 1, isRoot);
[586]2402
[1158]2403                   }
2404                   CTimer::get("Files : writing time axis").suspend(); 
2405                 }
2406
[586]2407                break;
[286]2408              }
[567]2409            }
[219]2410         }
[498]2411         catch (CNetCdfException& e)
2412         {
2413           StdString msg("On writing field data: ");
2414           msg.append(fieldid); msg.append("\n");
2415           msg.append("In the context : ");
2416           msg.append(context->getId()); msg.append("\n");
2417           msg.append(e.what());
[1130]2418           ERROR("CNc4DataOutput::writeFieldData_ (CField*  field)", << msg);
[498]2419         }
[219]2420      }
2421
2422      //---------------------------------------------------------------
2423
2424      void CNc4DataOutput::writeTimeAxis_
[347]2425                  (CField*    field,
[343]2426                   const boost::shared_ptr<CCalendar> cal)
[219]2427      {
2428         StdOStringStream oss;
[1158]2429         bool createInstantAxis=false ;
2430         bool createCenteredAxis=false ;
2431         bool createTimeCounterAxis=false ;
2432         
[645]2433         if (field->getOperationTimeType() == func::CFunctor::once) return ;
[488]2434
2435
[1158]2436         StdString axisId ;
2437         StdString axisBoundId;
[802]2438         StdString timeid(getTimeCounterName());
[614]2439         StdString timeBoundId("axis_nbounds");
[488]2440
[1158]2441         StdString strTimeUnits ;
2442         if (!field->file->time_units.isEmpty() && field->file->time_units==CFile::time_units_attr::days) strTimeUnits="days since " ;
2443         else  strTimeUnits="seconds since " ;
2444 
2445         if (field->getOperationTimeType() == func::CFunctor::instant) field->hasTimeInstant = true;
2446         if (field->getOperationTimeType() == func::CFunctor::centered) field->hasTimeCentered = true;
2447
2448
2449         if (field->file->time_counter.isEmpty())
[488]2450         {
[1158]2451           if (timeCounterType==none) createTimeCounterAxis=true ;
2452           if (field->hasTimeCentered)
2453           {
2454             timeCounterType=centered ;
2455             if (!hasTimeCentered) createCenteredAxis=true ;
2456           }
2457           if (field->hasTimeInstant)
2458           {
2459             if (timeCounterType==none) timeCounterType=instant ;
2460             if (!hasTimeInstant) createInstantAxis=true ;
2461           }
[488]2462         }
[1158]2463         else if (field->file->time_counter==CFile::time_counter_attr::instant)
2464         {
2465           if (field->hasTimeCentered)
2466           {
2467             if (!hasTimeCentered) createCenteredAxis=true ;
2468           }
2469           if (field->hasTimeInstant)
2470           {
2471             if (timeCounterType==none) createTimeCounterAxis=true ;
2472             timeCounterType=instant ;
2473             if (!hasTimeInstant) createInstantAxis=true ;
2474           }
2475         }
2476         else if (field->file->time_counter==CFile::time_counter_attr::centered)
2477         {
2478           if (field->hasTimeCentered)
2479           {
2480             if (timeCounterType==none) createTimeCounterAxis=true ;
2481             timeCounterType=centered ;
2482             if (!hasTimeCentered) createCenteredAxis=true ;
2483           }
2484           if (field->hasTimeInstant)
2485           {
2486             if (!hasTimeInstant) createInstantAxis=true ;
2487           }
2488         }
2489         else if (field->file->time_counter==CFile::time_counter_attr::instant_exclusive)
2490         {
2491           if (field->hasTimeCentered)
2492           {
2493             if (!hasTimeCentered) createCenteredAxis=true ;
2494           }
2495           if (field->hasTimeInstant)
2496           {
2497             if (timeCounterType==none) createTimeCounterAxis=true ;
2498             timeCounterType=instant ;
2499           }
2500         }
2501         else if (field->file->time_counter==CFile::time_counter_attr::centered_exclusive)
2502         {
2503           if (field->hasTimeCentered)
2504           {
2505             if (timeCounterType==none) createTimeCounterAxis=true ;
2506             timeCounterType=centered ;
2507           }
2508           if (field->hasTimeInstant)
2509           {
2510             if (!hasTimeInstant) createInstantAxis=true ;
2511           }
2512         }
2513         else if (field->file->time_counter==CFile::time_counter_attr::exclusive)
2514         {
2515           if (field->hasTimeCentered)
2516           {
2517             if (timeCounterType==none) createTimeCounterAxis=true ;
2518             if (timeCounterType==instant) createInstantAxis=true ;
2519             timeCounterType=centered ;
2520           }
2521           if (field->hasTimeInstant)
2522           {
2523             if (timeCounterType==none)
2524             {
2525               createTimeCounterAxis=true ;
2526               timeCounterType=instant ;
2527             }
2528             if (timeCounterType==centered)
2529             {
2530               if (!hasTimeInstant) createInstantAxis=true ;
2531             }
2532           }
2533         }
2534         else if (field->file->time_counter==CFile::time_counter_attr::none)
2535         {
2536           if (field->hasTimeCentered)
2537           {
2538             if (!hasTimeCentered) createCenteredAxis=true ;
2539           }
2540           if (field->hasTimeInstant)
2541           {
2542             if (!hasTimeInstant) createInstantAxis=true ;
2543           }
2544         }
2545         else if (field->file->time_counter==CFile::time_counter_attr::record)
2546         {
2547           if (timeCounterType==none) createTimeCounterAxis=true ;
2548           timeCounterType=record ;
2549           if (field->hasTimeCentered)
2550           {
2551             if (!hasTimeCentered) createCenteredAxis=true ;
2552           }
2553           if (field->hasTimeInstant)
2554           {
2555             if (!hasTimeInstant) createInstantAxis=true ;
2556           }
2557         }
2558         
2559         if (createInstantAxis)
2560         {
2561           axisId="time_instant" ;
2562           axisBoundId="time_instant_bounds";
2563           hasTimeInstant=true ;
2564         }
[488]2565
[1158]2566         if (createCenteredAxis)
2567         {
2568           axisId="time_centered" ;
2569           axisBoundId="time_centered_bounds";
2570           hasTimeCentered=true ;
2571         }
2572
2573         
[498]2574         try
[219]2575         {
[1158]2576            std::vector<StdString> dims;
2577           
2578            if (createInstantAxis || createCenteredAxis)
2579            {
2580              // Adding time_instant or time_centered
2581              dims.push_back(timeid);
2582              if (!SuperClassWriter::varExist(axisId))
2583              {
2584                SuperClassWriter::addVariable(axisId, NC_DOUBLE, dims);
[488]2585
[1158]2586                CDate timeOrigin=cal->getTimeOrigin() ;
2587                StdOStringStream oss2;
2588                StdString strInitdate=oss2.str() ;
2589                StdString strTimeOrigin=timeOrigin.toString() ;
2590                this->writeTimeAxisAttributes(axisId, cal->getType(),strTimeUnits+strTimeOrigin,
2591                                              strTimeOrigin, axisBoundId);
2592             }
[219]2593
[1158]2594             // Adding time_instant_bounds or time_centered_bounds variables
2595             if (!SuperClassWriter::varExist(axisBoundId))
2596             {
2597                dims.clear() ;
2598                dims.push_back(timeid);
2599                dims.push_back(timeBoundId);
2600                SuperClassWriter::addVariable(axisBoundId, NC_DOUBLE, dims);
2601             }
[498]2602           }
[488]2603
[1158]2604           if (createTimeCounterAxis)
[498]2605           {
[692]2606             // Adding time_counter
[1158]2607             axisId = getTimeCounterName();
[802]2608             axisBoundId = getTimeCounterName() + "_bounds";
[692]2609             dims.clear();
2610             dims.push_back(timeid);
[1158]2611             if (!SuperClassWriter::varExist(axisId))
[692]2612             {
[1158]2613                SuperClassWriter::addVariable(axisId, NC_DOUBLE, dims);
2614                SuperClassWriter::addAttribute("axis", string("T"), &axisId);
[488]2615
[1158]2616                if (field->file->time_counter.isEmpty() || 
2617                   (field->file->time_counter != CFile::time_counter_attr::record))
[692]2618                {
2619                  CDate timeOrigin = cal->getTimeOrigin();
2620                  StdString strTimeOrigin = timeOrigin.toString();
[498]2621
[1158]2622                  this->writeTimeAxisAttributes(axisId, cal->getType(),
2623                                                strTimeUnits+strTimeOrigin,
[692]2624                                                strTimeOrigin, axisBoundId);
2625                }
2626             }
2627
2628             // Adding time_counter_bound dimension
[1158]2629             if (field->file->time_counter.isEmpty() || (field->file->time_counter != CFile::time_counter_attr::record))
[692]2630             {
2631                if (!SuperClassWriter::varExist(axisBoundId))
2632                {
2633                  dims.clear();
2634                  dims.push_back(timeid);
2635                  dims.push_back(timeBoundId);
2636                  SuperClassWriter::addVariable(axisBoundId, NC_DOUBLE, dims);
2637                }
2638             }
[498]2639           }
[488]2640         }
[498]2641         catch (CNetCdfException& e)
[488]2642         {
[498]2643           StdString msg("On writing time axis data: ");
2644           msg.append("In the context : ");
2645           CContext* context = CContext::getCurrent() ;
2646           msg.append(context->getId()); msg.append("\n");
2647           msg.append(e.what());
2648           ERROR("CNc4DataOutput::writeTimeAxis_ (CField*    field, \
2649                  const boost::shared_ptr<CCalendar> cal)", << msg);
[488]2650         }
[219]2651      }
2652
2653      //---------------------------------------------------------------
[488]2654
[219]2655      void CNc4DataOutput::writeTimeAxisAttributes(const StdString & axis_name,
2656                                                   const StdString & calendar,
2657                                                   const StdString & units,
2658                                                   const StdString & time_origin,
[488]2659                                                   const StdString & time_bounds,
[219]2660                                                   const StdString & standard_name,
[613]2661                                                   const StdString & long_name)
[219]2662      {
[498]2663         try
2664         {
2665           SuperClassWriter::addAttribute("standard_name", standard_name, &axis_name);
2666           SuperClassWriter::addAttribute("long_name",     long_name    , &axis_name);
2667           SuperClassWriter::addAttribute("calendar",      calendar     , &axis_name);
2668           SuperClassWriter::addAttribute("units",         units        , &axis_name);
2669           SuperClassWriter::addAttribute("time_origin",   time_origin  , &axis_name);
2670           SuperClassWriter::addAttribute("bounds",        time_bounds  , &axis_name);
2671         }
2672         catch (CNetCdfException& e)
2673         {
2674           StdString msg("On writing time axis Attribute: ");
2675           msg.append("In the context : ");
2676           CContext* context = CContext::getCurrent() ;
2677           msg.append(context->getId()); msg.append("\n");
2678           msg.append(e.what());
2679           ERROR("CNc4DataOutput::writeTimeAxisAttributes(const StdString & axis_name, \
[613]2680                                                          const StdString & calendar,\
2681                                                          const StdString & units, \
2682                                                          const StdString & time_origin, \
2683                                                          const StdString & time_bounds, \
2684                                                          const StdString & standard_name, \
2685                                                          const StdString & long_name)", << msg);
[498]2686         }
[219]2687      }
[488]2688
[219]2689      //---------------------------------------------------------------
2690
2691      void CNc4DataOutput::writeAxisAttributes(const StdString & axis_name,
2692                                               const StdString & axis,
2693                                               const StdString & standard_name,
2694                                               const StdString & long_name,
2695                                               const StdString & units,
2696                                               const StdString & nav_model)
2697      {
[498]2698         try
2699         {
[613]2700          if (!axis.empty())
2701            SuperClassWriter::addAttribute("axis"       , axis         , &axis_name);
2702
[498]2703          SuperClassWriter::addAttribute("standard_name", standard_name, &axis_name);
2704          SuperClassWriter::addAttribute("long_name"    , long_name    , &axis_name);
2705          SuperClassWriter::addAttribute("units"        , units        , &axis_name);
[973]2706//          SuperClassWriter::addAttribute("nav_model"    , nav_model    , &axis_name);
[498]2707         }
2708         catch (CNetCdfException& e)
2709         {
2710           StdString msg("On writing Axis Attribute: ");
2711           msg.append("In the context : ");
2712           CContext* context = CContext::getCurrent() ;
2713           msg.append(context->getId()); msg.append("\n");
2714           msg.append(e.what());
2715           ERROR("CNc4DataOutput::writeAxisAttributes(const StdString & axis_name, \
[613]2716                                                      const StdString & axis, \
2717                                                      const StdString & standard_name, \
2718                                                      const StdString & long_name, \
2719                                                      const StdString & units, \
2720                                                      const StdString & nav_model)", << msg);
[498]2721         }
[219]2722      }
2723
2724      //---------------------------------------------------------------
[488]2725
[219]2726      void CNc4DataOutput::writeLocalAttributes
2727         (int ibegin, int ni, int jbegin, int nj, StdString domid)
2728      {
[498]2729        try
2730        {
[318]2731         SuperClassWriter::addAttribute(StdString("ibegin").append(domid), ibegin);
2732         SuperClassWriter::addAttribute(StdString("ni"    ).append(domid), ni);
2733         SuperClassWriter::addAttribute(StdString("jbegin").append(domid), jbegin);
2734         SuperClassWriter::addAttribute(StdString("nj"    ).append(domid), nj);
[498]2735        }
2736        catch (CNetCdfException& e)
2737        {
2738           StdString msg("On writing Local Attributes: ");
2739           msg.append("In the context : ");
2740           CContext* context = CContext::getCurrent() ;
2741           msg.append(context->getId()); msg.append("\n");
2742           msg.append(e.what());
2743           ERROR("CNc4DataOutput::writeLocalAttributes \
2744                  (int ibegin, int ni, int jbegin, int nj, StdString domid)", << msg);
2745        }
2746
[219]2747      }
2748
[628]2749      void CNc4DataOutput::writeLocalAttributes_IOIPSL(const StdString& dimXid, const StdString& dimYid,
2750                                                       int ibegin, int ni, int jbegin, int nj, int ni_glo, int nj_glo, int rank, int size)
[391]2751      {
2752         CArray<int,1> array(2) ;
2753
[498]2754         try
2755         {
2756           SuperClassWriter::addAttribute("DOMAIN_number_total",size ) ;
2757           SuperClassWriter::addAttribute("DOMAIN_number", rank) ;
[629]2758           array = SuperClassWriter::getDimension(dimXid) + 1, SuperClassWriter::getDimension(dimYid) + 1;
[498]2759           SuperClassWriter::addAttribute("DOMAIN_dimensions_ids",array) ;
2760           array=ni_glo,nj_glo ;
2761           SuperClassWriter::addAttribute("DOMAIN_size_global", array) ;
2762           array=ni,nj ;
2763           SuperClassWriter::addAttribute("DOMAIN_size_local", array) ;
[819]2764           array=ibegin+1,jbegin+1 ;
[498]2765           SuperClassWriter::addAttribute("DOMAIN_position_first", array) ;
[819]2766           array=ibegin+ni-1+1,jbegin+nj-1+1 ;
[498]2767           SuperClassWriter::addAttribute("DOMAIN_position_last",array) ;
2768           array=0,0 ;
2769           SuperClassWriter::addAttribute("DOMAIN_halo_size_start", array) ;
2770           SuperClassWriter::addAttribute("DOMAIN_halo_size_end", array);
2771           SuperClassWriter::addAttribute("DOMAIN_type",string("box")) ;
2772  /*
2773           SuperClassWriter::addAttribute("DOMAIN_DIM_N001",string("x")) ;
2774           SuperClassWriter::addAttribute("DOMAIN_DIM_N002",string("y")) ;
2775           SuperClassWriter::addAttribute("DOMAIN_DIM_N003",string("axis_A")) ;
2776           SuperClassWriter::addAttribute("DOMAIN_DIM_N004",string("time_counter")) ;
2777  */
2778         }
2779         catch (CNetCdfException& e)
2780         {
[628]2781           StdString msg("On writing Local Attributes IOIPSL \n");
[498]2782           msg.append("In the context : ");
2783           CContext* context = CContext::getCurrent() ;
2784           msg.append(context->getId()); msg.append("\n");
2785           msg.append(e.what());
2786           ERROR("CNc4DataOutput::writeLocalAttributes_IOIPSL \
2787                  (int ibegin, int ni, int jbegin, int nj, int ni_glo, int nj_glo, int rank, int size)", << msg);
2788         }
[391]2789      }
[219]2790      //---------------------------------------------------------------
2791
2792      void CNc4DataOutput:: writeFileAttributes(const StdString & name,
2793                                                const StdString & description,
2794                                                const StdString & conventions,
2795                                                const StdString & production,
2796                                                const StdString & timeStamp)
2797      {
[498]2798         try
2799         {
2800           SuperClassWriter::addAttribute("name"       , name);
2801           SuperClassWriter::addAttribute("description", description);
[613]2802           SuperClassWriter::addAttribute("title"      , description);
2803           SuperClassWriter::addAttribute("Conventions", conventions);
[1158]2804           // SuperClassWriter::addAttribute("production" , production);
2805
2806           StdString timeStampStr ;
2807           if (file->time_stamp_name.isEmpty()) timeStampStr="timeStamp" ;
2808           else timeStampStr=file->time_stamp_name ;
2809           SuperClassWriter::addAttribute(timeStampStr, timeStamp);
2810
2811           StdString uuidName ;
2812           if (file->uuid_name.isEmpty()) uuidName="uuid" ;
2813           else uuidName=file->uuid_name ;
2814
2815           if (file->uuid_format.isEmpty()) SuperClassWriter::addAttribute(uuidName, getUuidStr());
2816           else SuperClassWriter::addAttribute(uuidName, getUuidStr(file->uuid_format));
2817         
[498]2818         }
2819         catch (CNetCdfException& e)
2820         {
2821           StdString msg("On writing File Attributes \n ");
2822           msg.append("In the context : ");
2823           CContext* context = CContext::getCurrent() ;
2824           msg.append(context->getId()); msg.append("\n");
2825           msg.append(e.what());
2826           ERROR("CNc4DataOutput:: writeFileAttributes(const StdString & name, \
2827                                                const StdString & description, \
2828                                                const StdString & conventions, \
2829                                                const StdString & production, \
2830                                                const StdString & timeStamp)", << msg);
2831         }
[219]2832      }
2833
2834      //---------------------------------------------------------------
2835
2836      void CNc4DataOutput::writeMaskAttributes(const StdString & mask_name,
2837                                               int data_dim,
2838                                               int data_ni,
2839                                               int data_nj,
2840                                               int data_ibegin,
2841                                               int data_jbegin)
2842      {
[498]2843         try
2844         {
2845           SuperClassWriter::addAttribute("data_dim"   , data_dim   , &mask_name);
2846           SuperClassWriter::addAttribute("data_ni"    , data_ni    , &mask_name);
2847           SuperClassWriter::addAttribute("data_nj"    , data_nj    , &mask_name);
2848           SuperClassWriter::addAttribute("data_ibegin", data_ibegin, &mask_name);
2849           SuperClassWriter::addAttribute("data_jbegin", data_jbegin, &mask_name);
2850         }
2851         catch (CNetCdfException& e)
2852         {
2853           StdString msg("On writing Mask Attributes \n ");
2854           msg.append("In the context : ");
2855           CContext* context = CContext::getCurrent() ;
2856           msg.append(context->getId()); msg.append("\n");
2857           msg.append(e.what());
2858           ERROR("CNc4DataOutput::writeMaskAttributes(const StdString & mask_name, \
2859                                               int data_dim, \
2860                                               int data_ni, \
2861                                               int data_nj, \
2862                                               int data_ibegin, \
2863                                               int data_jbegin)", << msg);
2864         }
[219]2865      }
2866
2867      ///--------------------------------------------------------------
2868
[1158]2869      StdSize CNc4DataOutput::getRecordFromTime(Time time, double factorUnit)
[707]2870      {
2871        std::map<Time, StdSize>::const_iterator it = timeToRecordCache.find(time);
2872        if (it == timeToRecordCache.end())
2873        {
[802]2874          StdString timeAxisBoundsId(getTimeCounterName() + "_bounds");
[1158]2875          if (!SuperClassWriter::varExist(timeAxisBoundsId)) timeAxisBoundsId = "time_centered_bounds";
2876          if (!SuperClassWriter::varExist(timeAxisBoundsId)) timeAxisBoundsId = "time_instant_bounds";
[707]2877
2878          CArray<double,2> timeAxisBounds;
[1158]2879          std::vector<StdSize> dimSize(SuperClassWriter::getDimensions(timeAxisBoundsId)) ;
2880         
[707]2881          StdSize record = 0;
2882          double dtime(time);
[1158]2883          for (int n = dimSize[0] - 1; n >= 0; n--)
[707]2884          {
[1158]2885            SuperClassWriter::getTimeAxisBounds(timeAxisBounds, timeAxisBoundsId, isCollective, n);
2886            timeAxisBounds*=factorUnit ;
2887            if (timeAxisBounds(1, 0) < dtime)
[707]2888            {
2889              record = n + 1;
2890              break;
2891            }
2892          }
2893          it = timeToRecordCache.insert(std::make_pair(time, record)).first;
2894        }
2895        return it->second;
2896      }
[774]2897
2898      ///--------------------------------------------------------------
2899
2900      bool CNc4DataOutput::isWrittenDomain(const std::string& domainName) const
2901      {
2902        return (this->writtenDomains.find(domainName) != this->writtenDomains.end());
2903      }
2904
2905      bool CNc4DataOutput::isWrittenCompressedDomain(const std::string& domainName) const
2906      {
2907        return (this->writtenCompressedDomains.find(domainName) != this->writtenCompressedDomains.end());
2908      }
2909
2910      bool CNc4DataOutput::isWrittenAxis(const std::string& axisName) const
2911      {
2912        return (this->writtenAxis.find(axisName) != this->writtenAxis.end());
2913      }
2914
2915      bool CNc4DataOutput::isWrittenCompressedAxis(const std::string& axisName) const
2916      {
2917        return (this->writtenCompressedAxis.find(axisName) != this->writtenCompressedAxis.end());
2918      }
2919
[887]2920      bool CNc4DataOutput::isWrittenScalar(const std::string& scalarName) const
2921      {
2922        return (this->writtenScalar.find(scalarName) != this->writtenScalar.end());
2923      }
2924
[774]2925      void CNc4DataOutput::setWrittenDomain(const std::string& domainName)
2926      {
2927        this->writtenDomains.insert(domainName);
2928      }
2929
2930      void CNc4DataOutput::setWrittenCompressedDomain(const std::string& domainName)
2931      {
2932        this->writtenCompressedDomains.insert(domainName);
2933      }
2934
2935      void CNc4DataOutput::setWrittenAxis(const std::string& axisName)
2936      {
2937        this->writtenAxis.insert(axisName);
2938      }
2939
2940      void CNc4DataOutput::setWrittenCompressedAxis(const std::string& axisName)
2941      {
2942        this->writtenCompressedAxis.insert(axisName);
2943      }
[887]2944
2945      void CNc4DataOutput::setWrittenScalar(const std::string& scalarName)
2946      {
2947        this->writtenScalar.insert(scalarName);
2948      }
[335]2949} // namespace xios
Note: See TracBrowser for help on using the repository browser.