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

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

Fixing a bug on writting axis label

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

Test
+) On Curie
+) OK

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