source: XIOS/dev/dev_olga/src/io/nc4_data_output.cpp @ 1143

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

Updating compressed index output on using 2-level server

+) Update compressed index output with new grid distribution

Test
+) On Curie
+) test_complete:

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