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

Last change on this file since 1436 was 1436, checked in by oabramkina, 6 years ago

Corrections for scalar bounds.

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