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

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

Very minor correction of r1332.

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