source: XIOS/trunk/src/io/nc4_data_output.cpp @ 796

Last change on this file since 796 was 796, checked in by mhnguyen, 9 years ago

Fixing minor bug on writing axis_bounds

+) Use the correct start and count for axis bounds

Test
+) On Curie
+) test with NEMO passed

  • Property copyright set to
    Software name : XIOS (Xml I/O Server)
    http://forge.ipsl.jussieu.fr/ioserver
    Creation date : January 2009
    Licence : CeCCIL version2
    see license file in root directory : Licence_CeCILL_V2-en.txt
    or http://www.cecill.info/licences/Licence_CeCILL_V2-en.html
    Holder : CEA/LSCE (Laboratoire des Sciences du CLimat et de l'Environnement)
    CNRS/IPSL (Institut Pierre Simon Laplace)
    Project Manager : Yann Meurdesoif
    yann.meurdesoif@cea.fr
  • Property svn:executable set to *
File size: 82.9 KB
Line 
1
2#include "nc4_data_output.hpp"
3
4#include <boost/lexical_cast.hpp>
5#include "attribute_template.hpp"
6#include "group_template.hpp"
7
8#include "file.hpp"
9#include "calendar.hpp"
10#include "context.hpp"
11#include "context_server.hpp"
12#include "netCdfException.hpp"
13#include "exception.hpp"
14
15namespace xios
16{
17      /// ////////////////////// Définitions ////////////////////// ///
18      CNc4DataOutput::CNc4DataOutput
19         (const StdString & filename, bool exist)
20            : SuperClass()
21            , SuperClassWriter(filename, exist)
22            , filename(filename)
23      {
24         StdString timeid = StdString("time_counter");
25         SuperClass::type = MULTI_FILE;
26      }
27
28      CNc4DataOutput::CNc4DataOutput
29         (const StdString & filename, bool exist, bool useClassicFormat,
30          MPI_Comm comm_file,bool multifile, bool isCollective)
31            : SuperClass()
32            , SuperClassWriter(filename, exist, useClassicFormat, &comm_file, multifile)
33            , comm_file(comm_file)
34            , filename(filename)
35            , isCollective(isCollective)
36      {
37         StdString timeid = StdString("time_counter");
38
39         SuperClass::type = (multifile) ? MULTI_FILE : ONE_FILE;
40      }
41
42
43      CNc4DataOutput::~CNc4DataOutput(void)
44      { /* Ne rien faire de plus */ }
45
46      ///--------------------------------------------------------------
47
48      const StdString & CNc4DataOutput::getFileName(void) const
49      {
50         return (this->filename);
51      }
52
53      //---------------------------------------------------------------
54
55      void CNc4DataOutput::writeDomain_(CDomain* domain)
56      {
57         if (domain->type == CDomain::type_attr::unstructured)
58         {
59           writeUnstructuredDomain(domain) ;
60           return ;
61         }
62
63         CContext* context = CContext::getCurrent() ;
64         CContextServer* server=context->server ;
65
66         if (domain->IsWritten(this->filename)) return;
67         domain->checkAttributes();
68
69         if (domain->isEmpty())
70           if (SuperClass::type==MULTI_FILE) return ;
71
72         std::vector<StdString> dim0, dim1;
73         StdString domid = domain->getDomainOutputName();
74         StdString appendDomid  = (singleDomain) ? "" : "_"+domid ;
75         if (isWrittenDomain(domid)) return ;
76         else setWrittenDomain(domid);
77
78
79         StdString dimXid, dimYid ;
80
81         bool isRegularDomain = (domain->type == CDomain::type_attr::rectilinear);
82         switch (domain->type)
83         {
84           case CDomain::type_attr::curvilinear :
85             dimXid     = StdString("x").append(appendDomid);
86             dimYid     = StdString("y").append(appendDomid);
87             break ;
88           case CDomain::type_attr::rectilinear :
89             dimXid     = StdString("lon").append(appendDomid);
90             dimYid     = StdString("lat").append(appendDomid);
91             break;
92         }
93
94         StdString dimVertId = StdString("nvertex").append(appendDomid);
95
96         string lonid,latid,bounds_lonid,bounds_latid ;
97         string areaId = "area" + appendDomid;
98/*
99         StdString lonid_loc = (server->intraCommSize > 1)
100                             ? StdString("lon").append(appendDomid).append("_local")
101                             : lonid;
102         StdString latid_loc = (server->intraCommSize > 1)
103                             ? StdString("lat").append(appendDomid).append("_local")
104                             : latid;
105*/
106
107         try
108         {
109           switch (SuperClass::type)
110           {
111              case (MULTI_FILE) :
112              {
113  //               if (domain->isEmpty()) return;
114
115                 if (server->intraCommSize > 1)
116                 {
117  //                 SuperClassWriter::addDimension(lonid, domain->zoom_ni.getValue());
118  //                 SuperClassWriter::addDimension(latid, domain->zoom_nj.getValue());
119                 }
120
121                 switch (domain->type)
122                 {
123                   case CDomain::type_attr::curvilinear :
124                     dim0.push_back(dimYid); dim0.push_back(dimXid);
125                     lonid = StdString("nav_lon").append(appendDomid);
126                     latid = StdString("nav_lat").append(appendDomid);
127                     break ;
128                   case CDomain::type_attr::rectilinear :
129                     lonid = StdString("lon").append(appendDomid);
130                     latid = StdString("lat").append(appendDomid);
131                     dim0.push_back(dimYid);
132                     dim1.push_back(dimXid);
133                     break;
134                 }
135
136                 bounds_lonid = StdString("bounds_lon").append(appendDomid);
137                 bounds_latid = StdString("bounds_lat").append(appendDomid);
138
139                 SuperClassWriter::addDimension(dimXid, domain->zoom_ni_srv);
140                 SuperClassWriter::addDimension(dimYid, domain->zoom_nj_srv);
141
142                 if (domain->hasBounds)
143                   SuperClassWriter::addDimension(dimVertId, domain->nvertex);
144
145                 if (server->intraCommSize > 1)
146                 {
147                   this->writeLocalAttributes(domain->zoom_ibegin_srv,
148                                              domain->zoom_ni_srv,
149                                              domain->zoom_jbegin_srv,
150                                              domain->zoom_nj_srv,
151                                              appendDomid);
152
153                   if (singleDomain)
154                    this->writeLocalAttributes_IOIPSL(dimXid, dimYid,
155                                                      domain->zoom_ibegin_srv,
156                                                      domain->zoom_ni_srv,
157                                                      domain->zoom_jbegin_srv,
158                                                      domain->zoom_nj_srv,
159                                                      domain->ni_glo,domain->nj_glo,
160                                                      server->intraCommRank,server->intraCommSize);
161                 }
162
163                 if (domain->hasLonLat)
164                 {
165                   switch (domain->type)
166                   {
167                     case CDomain::type_attr::curvilinear :
168                       SuperClassWriter::addVariable(latid, NC_FLOAT, dim0);
169                       SuperClassWriter::addVariable(lonid, NC_FLOAT, dim0);
170                       break ;
171                      case CDomain::type_attr::rectilinear :
172                        SuperClassWriter::addVariable(latid, NC_FLOAT, dim0);
173                        SuperClassWriter::addVariable(lonid, NC_FLOAT, dim1);
174                        break ;
175                   }
176
177                   this->writeAxisAttributes(lonid, isRegularDomain ? "X" : "", "longitude", "Longitude", "degrees_east", domid);
178                   this->writeAxisAttributes(latid, isRegularDomain ? "Y" : "", "latitude", "Latitude", "degrees_north", domid);
179
180                   if (domain->hasBounds)
181                   {
182                     SuperClassWriter::addAttribute("bounds", bounds_lonid, &lonid);
183                     SuperClassWriter::addAttribute("bounds", bounds_latid, &latid);
184
185                     dim0.clear();
186                     dim0.push_back(dimYid);
187                     dim0.push_back(dimXid);
188                     dim0.push_back(dimVertId);
189                     SuperClassWriter::addVariable(bounds_lonid, NC_FLOAT, dim0);
190                     SuperClassWriter::addVariable(bounds_latid, NC_FLOAT, dim0);
191                   }
192                 }
193
194                 dim0.clear();
195                 dim0.push_back(dimYid);
196                 dim0.push_back(dimXid);
197
198
199  // supress mask               if (server->intraCommSize > 1)
200  // supress mask               {
201  // supress mask                  SuperClassWriter::addVariable(maskid, NC_INT, dim0);
202  // supress mask
203  // supress mask                  this->writeMaskAttributes(maskid,
204  // supress mask                     domain->data_dim.getValue()/*,
205  // supress mask                     domain->data_ni.getValue(),
206  // supress mask                     domain->data_nj.getValue(),
207  // supress mask                     domain->data_ibegin.getValue(),
208  // supress mask                     domain->data_jbegin.getValue()*/);
209  // supress mask               }
210
211                 //SuperClassWriter::setDefaultValue(maskid, &dvm);
212
213                 if (domain->hasArea)
214                 {
215                   SuperClassWriter::addVariable(areaId, NC_FLOAT, dim0);
216                   SuperClassWriter::addAttribute("standard_name", StdString("cell_area"), &areaId);
217                   SuperClassWriter::addAttribute("units", StdString("m2"), &areaId);
218                 }
219
220                 SuperClassWriter::definition_end();
221
222                 if (domain->hasLonLat)
223                 {
224                   switch (domain->type)
225                   {
226                     case CDomain::type_attr::curvilinear :
227                       SuperClassWriter::writeData(domain->latvalue_srv, latid, isCollective, 0);
228                       SuperClassWriter::writeData(domain->lonvalue_srv, lonid, isCollective, 0);
229                       break;
230                     case CDomain::type_attr::rectilinear :
231                       CArray<double,1> lat = domain->latvalue_srv(Range(fromStart,toEnd,domain->zoom_ni_srv)) ;
232                       SuperClassWriter::writeData(CArray<double,1>(lat.copy()), latid, isCollective, 0);
233                       CArray<double,1> lon=domain->lonvalue_srv(Range(0,domain->zoom_ni_srv-1)) ;
234                       SuperClassWriter::writeData(CArray<double,1>(lon.copy()), lonid, isCollective, 0);
235                       break;
236                   }
237
238                   if (domain->hasBounds)
239                   {
240                     SuperClassWriter::writeData(domain->bounds_lon_srv, bounds_lonid, isCollective, 0);
241                     SuperClassWriter::writeData(domain->bounds_lat_srv, bounds_latid, isCollective, 0);
242                   }
243                 }
244
245                 if (domain->hasArea)
246                   SuperClassWriter::writeData(domain->area_srv, areaId, isCollective, 0);
247
248                 SuperClassWriter::definition_start();
249
250                 break;
251              }
252              case (ONE_FILE) :
253              {
254                 SuperClassWriter::addDimension(dimXid, domain->global_zoom_ni);
255                 SuperClassWriter::addDimension(dimYid, domain->global_zoom_nj);
256
257                 if (domain->hasBounds)
258                   SuperClassWriter::addDimension(dimVertId, domain->nvertex);
259
260                 if (domain->hasLonLat)
261                 {
262                   switch (domain->type)
263                   {
264                     case CDomain::type_attr::curvilinear :
265                       dim0.push_back(dimYid); dim0.push_back(dimXid);
266                       lonid = StdString("nav_lon").append(appendDomid);
267                       latid = StdString("nav_lat").append(appendDomid);
268                       SuperClassWriter::addVariable(latid, NC_FLOAT, dim0);
269                       SuperClassWriter::addVariable(lonid, NC_FLOAT, dim0);
270                       break;
271
272                     case CDomain::type_attr::rectilinear :
273                       dim0.push_back(dimYid);
274                       dim1.push_back(dimXid);
275                       lonid = StdString("lon").append(appendDomid);
276                       latid = StdString("lat").append(appendDomid);
277                       SuperClassWriter::addVariable(latid, NC_FLOAT, dim0);
278                       SuperClassWriter::addVariable(lonid, NC_FLOAT, dim1);
279                       break;
280                   }
281
282                   bounds_lonid = StdString("bounds_lon").append(appendDomid);
283                   bounds_latid = StdString("bounds_lat").append(appendDomid);
284
285                   this->writeAxisAttributes
286                      (lonid, isRegularDomain ? "X" : "", "longitude", "Longitude", "degrees_east", domid);
287                   this->writeAxisAttributes
288                      (latid, isRegularDomain ? "Y" : "", "latitude", "Latitude", "degrees_north", domid);
289
290                   if (domain->hasBounds)
291                   {
292                     SuperClassWriter::addAttribute("bounds", bounds_lonid, &lonid);
293                     SuperClassWriter::addAttribute("bounds", bounds_latid, &latid);
294
295                     dim0.clear();
296                     dim0.push_back(dimYid);
297                     dim0.push_back(dimXid);
298                     dim0.push_back(dimVertId);
299                     SuperClassWriter::addVariable(bounds_lonid, NC_FLOAT, dim0);
300                     SuperClassWriter::addVariable(bounds_latid, NC_FLOAT, dim0);
301                   }
302                 }
303
304                 if (domain->hasArea)
305                 {
306                   dim0.clear();
307                   dim0.push_back(dimYid); dim0.push_back(dimXid);
308                   SuperClassWriter::addVariable(areaId, NC_FLOAT, dim0);
309                   SuperClassWriter::addAttribute("standard_name", StdString("cell_area"), &areaId);
310                   SuperClassWriter::addAttribute("units", StdString("m2"), &areaId);
311                   dim0.clear();
312                 }
313
314                 SuperClassWriter::definition_end();
315
316                 switch (domain->type)
317                 {
318                   case CDomain::type_attr::curvilinear :
319                   {
320                     std::vector<StdSize> start(2) ;
321                     std::vector<StdSize> count(2) ;
322                     if (domain->isEmpty())
323                     {
324                       start[0]=0 ; start[1]=0 ;
325                       count[0]=0 ; count[1]=0 ;
326                     }
327                     else
328                     {
329                       start[1]=domain->zoom_ibegin_srv-domain->global_zoom_ibegin;
330                       start[0]=domain->zoom_jbegin_srv-domain->global_zoom_jbegin;
331                       count[1]=domain->zoom_ni_srv ; count[0]=domain->zoom_nj_srv ;
332                     }
333
334                     if (domain->hasLonLat)
335                     {
336                       SuperClassWriter::writeData(domain->latvalue_srv, latid, isCollective, 0,&start,&count);
337                       SuperClassWriter::writeData(domain->lonvalue_srv, lonid, isCollective, 0,&start,&count);
338                     }
339                     break;
340                   }
341                   case CDomain::type_attr::rectilinear :
342                   {
343                     if (domain->hasLonLat)
344                     {
345                       std::vector<StdSize> start(1) ;
346                       std::vector<StdSize> count(1) ;
347                       if (domain->isEmpty())
348                       {
349                         start[0]=0 ;
350                         count[0]=0 ;
351                         SuperClassWriter::writeData(domain->latvalue_srv, latid, isCollective, 0,&start,&count);
352                         SuperClassWriter::writeData(domain->lonvalue_srv, lonid, isCollective, 0,&start,&count);
353
354                       }
355                       else
356                       {
357                         start[0]=domain->zoom_jbegin_srv-domain->global_zoom_jbegin;
358                         count[0]=domain->zoom_nj_srv ;
359                         CArray<double,1> lat = domain->latvalue_srv(Range(fromStart,toEnd,domain->zoom_ni_srv)) ;
360                         SuperClassWriter::writeData(CArray<double,1>(lat.copy()), latid, isCollective, 0,&start,&count);
361
362                         start[0]=domain->zoom_ibegin_srv-domain->global_zoom_ibegin;
363                         count[0]=domain->zoom_ni_srv ;
364                         CArray<double,1> lon=domain->lonvalue_srv(Range(0,domain->zoom_ni_srv-1)) ;
365                         SuperClassWriter::writeData(CArray<double,1>(lon.copy()), lonid, isCollective, 0,&start,&count);
366                       }
367                     }
368                     break;
369                   }
370                 }
371
372                 if (domain->hasBounds)
373                 {
374                   std::vector<StdSize> start(3);
375                   std::vector<StdSize> count(3);
376                   if (domain->isEmpty())
377                   {
378                     start[2] = start[1] = start[0] = 0;
379                     count[2] = count[1] = count[0] = 0;
380                   }
381                   else
382                   {
383                     start[2] = 0;
384                     start[1] = domain->zoom_ibegin_srv - domain->global_zoom_ibegin;
385                     start[0] = domain->zoom_jbegin_srv - domain->global_zoom_jbegin;
386                     count[2] = domain->nvertex;
387                     count[1] = domain->zoom_ni_srv;
388                     count[0] = domain->zoom_nj_srv;
389                   }
390
391                   SuperClassWriter::writeData(domain->bounds_lon_srv, bounds_lonid, isCollective, 0, &start, &count);
392                   SuperClassWriter::writeData(domain->bounds_lat_srv, bounds_latid, isCollective, 0, &start, &count);
393                 }
394
395                 if (domain->hasArea)
396                 {
397                   std::vector<StdSize> start(2);
398                   std::vector<StdSize> count(2);
399
400                   if (domain->isEmpty())
401                   {
402                     start[0] = 0; start[1] = 0;
403                     count[0] = 0; count[1] = 0;
404                   }
405                   else
406                   {
407                     start[1] = domain->zoom_ibegin_srv - domain->global_zoom_ibegin;
408                     start[0] = domain->zoom_jbegin_srv - domain->global_zoom_jbegin;
409                     count[1] = domain->zoom_ni_srv;
410                     count[0] = domain->zoom_nj_srv;
411                   }
412
413                   SuperClassWriter::writeData(domain->area_srv, areaId, isCollective, 0, &start, &count);
414                 }
415
416                 SuperClassWriter::definition_start();
417                 break;
418              }
419              default :
420                 ERROR("CNc4DataOutput::writeDomain(domain)",
421                       << "[ type = " << SuperClass::type << "]"
422                       << " not implemented yet !");
423           }
424         }
425         catch (CNetCdfException& e)
426         {
427           StdString msg("On writing the domain : ");
428           msg.append(domid); msg.append("\n");
429           msg.append("In the context : ");
430           msg.append(context->getId()); msg.append("\n");
431           msg.append(e.what());
432           ERROR("CNc4DataOutput::writeDomain_(CDomain* domain)", << msg);
433         }
434
435         domain->addRelFile(this->filename);
436      }
437
438      void CNc4DataOutput::writeUnstructuredDomain(CDomain* domain)
439      {
440         CContext* context = CContext::getCurrent() ;
441         CContextServer* server=context->server ;
442
443         if (domain->IsWritten(this->filename)) return;
444         domain->checkAttributes();
445
446         if (domain->isEmpty())
447           if (SuperClass::type==MULTI_FILE) return ;
448
449         std::vector<StdString> dim0, dim1;
450         StdString domid = domain->getDomainOutputName();
451         if (isWrittenDomain(domid)) return ;
452         else setWrittenDomain(domid);
453
454         StdString appendDomid  = (singleDomain) ? "" : "_"+domid ;
455
456
457         StdString dimXid = StdString("cell").append(appendDomid);
458         StdString dimVertId = StdString("nvertex").append(appendDomid);
459
460         string lonid,latid,bounds_lonid,bounds_latid ;
461         string areaId = "area" + appendDomid;
462
463         try
464         {
465           switch (SuperClass::type)
466           {
467              case (MULTI_FILE) :
468              {
469                 dim0.push_back(dimXid);
470                 SuperClassWriter::addDimension(dimXid, domain->zoom_ni_srv);
471
472                 lonid = StdString("lon").append(appendDomid);
473                 latid = StdString("lat").append(appendDomid);
474                 bounds_lonid = StdString("bounds_lon").append(appendDomid);
475                 bounds_latid = StdString("bounds_lat").append(appendDomid);
476                 if (domain->hasLonLat)
477                 {
478                   SuperClassWriter::addVariable(latid, NC_FLOAT, dim0);
479                   SuperClassWriter::addVariable(lonid, NC_FLOAT, dim0);
480                   this->writeAxisAttributes(lonid, "", "longitude", "Longitude", "degrees_east", domid);
481                   if (domain->hasBounds) SuperClassWriter::addAttribute("bounds",bounds_lonid, &lonid);
482                   this->writeAxisAttributes(latid, "", "latitude", "Latitude", "degrees_north", domid);
483                   if (domain->hasBounds) SuperClassWriter::addAttribute("bounds",bounds_latid, &latid);
484                   if (domain->hasBounds) SuperClassWriter::addDimension(dimVertId, domain->nvertex);
485                 }
486
487                 dim0.clear();
488                 if (domain->hasBounds)
489                 {
490                   dim0.push_back(dimXid);
491                   dim0.push_back(dimVertId);
492                   SuperClassWriter::addVariable(bounds_lonid, NC_FLOAT, dim0);
493                   SuperClassWriter::addVariable(bounds_latid, NC_FLOAT, dim0);
494                 }
495
496                 dim0.clear();
497                 dim0.push_back(dimXid);
498                 if (domain->hasArea)
499                 {
500                   SuperClassWriter::addVariable(areaId, NC_FLOAT, dim0);
501                   SuperClassWriter::addAttribute("standard_name", StdString("cell_area"), &areaId);
502                   SuperClassWriter::addAttribute("units", StdString("m2"), &areaId);
503                 }
504
505                 SuperClassWriter::definition_end();
506
507                 if (domain->hasLonLat)
508                 {
509                   SuperClassWriter::writeData(domain->latvalue_srv, latid, isCollective, 0);
510                   SuperClassWriter::writeData(domain->lonvalue_srv, lonid, isCollective, 0);
511                   if (domain->hasBounds)
512                   {
513                     SuperClassWriter::writeData(domain->bounds_lon_srv, bounds_lonid, isCollective, 0);
514                     SuperClassWriter::writeData(domain->bounds_lat_srv, bounds_latid, isCollective, 0);
515                   }
516                 }
517
518                 if (domain->hasArea)
519                   SuperClassWriter::writeData(domain->area_srv, areaId, isCollective, 0);
520
521                 SuperClassWriter::definition_start();
522                 break ;
523              }
524
525              case (ONE_FILE) :
526              {
527                 lonid = StdString("lon").append(appendDomid);
528                 latid = StdString("lat").append(appendDomid);
529                 bounds_lonid = StdString("bounds_lon").append(appendDomid);
530                 bounds_latid = StdString("bounds_lat").append(appendDomid);
531                 dim0.push_back(dimXid);
532                 SuperClassWriter::addDimension(dimXid, domain->ni_glo);
533                 if (domain->hasLonLat)
534                 {
535                   SuperClassWriter::addVariable(latid, NC_FLOAT, dim0);
536                   SuperClassWriter::addVariable(lonid, NC_FLOAT, dim0);
537
538                   this->writeAxisAttributes(lonid, "", "longitude", "Longitude", "degrees_east", domid);
539                   if (domain->hasBounds) SuperClassWriter::addAttribute("bounds",bounds_lonid, &lonid);
540                   this->writeAxisAttributes(latid, "", "latitude", "Latitude", "degrees_north", domid);
541                   if (domain->hasBounds) SuperClassWriter::addAttribute("bounds",bounds_latid, &latid);
542                   if (domain->hasBounds) SuperClassWriter::addDimension(dimVertId, domain->nvertex);
543                 }
544                 dim0.clear();
545
546                 if (domain->hasBounds)
547                 {
548                   dim0.push_back(dimXid);
549                   dim0.push_back(dimVertId);
550                   SuperClassWriter::addVariable(bounds_lonid, NC_FLOAT, dim0);
551                   SuperClassWriter::addVariable(bounds_latid, NC_FLOAT, dim0);
552                 }
553
554                 if (domain->hasArea)
555                 {
556                   dim0.clear();
557                   dim0.push_back(dimXid);
558                   SuperClassWriter::addVariable(areaId, NC_FLOAT, dim0);
559                   SuperClassWriter::addAttribute("standard_name", StdString("cell_area"), &areaId);
560                   SuperClassWriter::addAttribute("units", StdString("m2"), &areaId);
561                 }
562
563                 SuperClassWriter::definition_end();
564
565                 std::vector<StdSize> start(1), startBounds(2) ;
566                 std::vector<StdSize> count(1), countBounds(2) ;
567                 if (domain->isEmpty())
568                 {
569                   start[0]=0 ;
570                   count[0]=0 ;
571                   startBounds[1]=0 ;
572                   countBounds[1]=domain->nvertex ;
573                   startBounds[0]=0 ;
574                   countBounds[0]=0 ;
575                 }
576                 else
577                 {
578                   start[0]=domain->zoom_ibegin_srv-domain->global_zoom_ibegin;
579                   count[0]=domain->zoom_ni_srv ;
580                   startBounds[0]=domain->zoom_ibegin_srv-domain->global_zoom_ibegin;
581                   startBounds[1]=0 ;
582                   countBounds[0]=domain->zoom_ni_srv ;
583                   countBounds[1]=domain->nvertex ;
584                 }
585
586                 if (domain->hasLonLat)
587                 {
588                   SuperClassWriter::writeData(domain->latvalue_srv, latid, isCollective, 0,&start,&count);
589                   SuperClassWriter::writeData(domain->lonvalue_srv, lonid, isCollective, 0,&start,&count);
590                   if (domain->hasBounds)
591                   {
592                     SuperClassWriter::writeData(domain->bounds_lon_srv, bounds_lonid, isCollective, 0,&startBounds,&countBounds);
593                     SuperClassWriter::writeData(domain->bounds_lat_srv, bounds_latid, isCollective, 0,&startBounds,&countBounds);
594                   }
595                 }
596
597                 if (domain->hasArea)
598                   SuperClassWriter::writeData(domain->area_srv, areaId, isCollective, 0, &start, &count);
599
600                 SuperClassWriter::definition_start();
601
602                 break;
603              }
604              default :
605                 ERROR("CNc4DataOutput::writeDomain(domain)",
606                       << "[ type = " << SuperClass::type << "]"
607                       << " not implemented yet !");
608           }
609         }
610         catch (CNetCdfException& e)
611         {
612           StdString msg("On writing the domain : ");
613           msg.append(domid); msg.append("\n");
614           msg.append("In the context : ");
615           msg.append(context->getId()); msg.append("\n");
616           msg.append(e.what());
617           ERROR("CNc4DataOutput::writeUnstructuredDomain(CDomain* domain)", << msg);
618         }
619         domain->addRelFile(this->filename);
620      }
621      //--------------------------------------------------------------
622
623      void CNc4DataOutput::writeAxis_(CAxis* axis)
624      {
625        if (axis->IsWritten(this->filename)) return;
626        axis->checkAttributes();
627        int zoom_size_srv  = axis->zoom_size_srv;
628        int zoom_begin_srv = axis->zoom_begin_srv;
629        int zoom_size  = (MULTI_FILE == SuperClass::type) ? zoom_size_srv
630                                                              : axis->global_zoom_size;
631        int zoom_begin = (MULTI_FILE == SuperClass::type) ? zoom_begin_srv
632                                                              : axis->global_zoom_begin;
633
634        if ((0 == zoom_size_srv) && (MULTI_FILE == SuperClass::type)) return;
635
636        std::vector<StdString> dims;
637        StdString axisid = axis->getAxisOutputName();
638        if (isWrittenAxis(axisid)) return ;
639        else setWrittenAxis(axisid);
640
641        try
642        {
643          SuperClassWriter::addDimension(axisid, zoom_size);
644          dims.push_back(axisid);
645          SuperClassWriter::addVariable(axisid, NC_FLOAT, dims);
646
647          if (!axis->name.isEmpty())
648            SuperClassWriter::addAttribute("name", axis->name.getValue(), &axisid);
649
650          if (!axis->standard_name.isEmpty())
651            SuperClassWriter::addAttribute("standard_name", axis->standard_name.getValue(), &axisid);
652
653          if (!axis->long_name.isEmpty())
654            SuperClassWriter::addAttribute("long_name", axis->long_name.getValue(), &axisid);
655
656          if (!axis->unit.isEmpty())
657            SuperClassWriter::addAttribute("units", axis->unit.getValue(), &axisid);
658
659          if (!axis->positive.isEmpty())
660          {
661            SuperClassWriter::addAttribute("axis", string("Z"), &axisid);
662            SuperClassWriter::addAttribute("positive",
663                                           (axis->positive == CAxis::positive_attr::up) ? string("up") : string("down"),
664                                           &axisid);
665          }
666
667          StdString axisBoundsId = axisid + "_bounds";
668          if (!axis->bounds.isEmpty())
669          {
670            dims.push_back("axis_nbounds");
671            SuperClassWriter::addVariable(axisBoundsId, NC_FLOAT, dims);
672            SuperClassWriter::addAttribute("bounds", axisBoundsId, &axisid);
673          }
674
675          SuperClassWriter::definition_end();
676          switch (SuperClass::type)
677          {
678            case MULTI_FILE:
679            {
680              CArray<double,1> axis_value(zoom_size_srv);
681              for (int i = 0; i < zoom_size_srv; i++) axis_value(i) = axis->value_srv(i);
682              SuperClassWriter::writeData(axis_value, axisid, isCollective, 0);
683
684              if (!axis->bounds.isEmpty())
685                SuperClassWriter::writeData(axis->bound_srv, axisBoundsId, isCollective, 0);
686
687              SuperClassWriter::definition_start();
688
689              break;
690            }
691            case ONE_FILE:
692            {
693              CArray<double,1> axis_value(zoom_size_srv);
694              axis_value = axis->value_srv;
695
696              std::vector<StdSize> start(1), startBounds(2) ;
697              std::vector<StdSize> count(1), countBounds(2) ;
698              start[0] = startBounds[0] = zoom_begin_srv-axis->global_zoom_begin;
699              count[0] = countBounds[0] = zoom_size_srv;
700              startBounds[1] = 0;
701              countBounds[1] = 2;
702              SuperClassWriter::writeData(axis_value, axisid, isCollective, 0, &start, &count);
703
704              if (!axis->bounds.isEmpty())
705                SuperClassWriter::writeData(axis->bound_srv, axisBoundsId, isCollective, 0, &startBounds, &countBounds);
706
707              SuperClassWriter::definition_start();
708
709              break;
710            }
711            default :
712              ERROR("CNc4DataOutput::writeDomain(domain)",
713                    << "[ type = " << SuperClass::type << "]"
714                    << " not implemented yet !");
715          }
716        }
717        catch (CNetCdfException& e)
718        {
719          StdString msg("On writing the axis : ");
720          msg.append(axisid); msg.append("\n");
721          msg.append("In the context : ");
722          CContext* context = CContext::getCurrent() ;
723          msg.append(context->getId()); msg.append("\n");
724          msg.append(e.what());
725          ERROR("CNc4DataOutput::writeAxis_(CAxis* axis)", << msg);
726        }
727        axis->addRelFile(this->filename);
728     }
729
730     //--------------------------------------------------------------
731
732     void CNc4DataOutput::writeGridCompressed_(CGrid* grid)
733     {
734       if (grid->isScalarGrid() || grid->isWrittenCompressed(this->filename)) return;
735
736       try
737       {
738         CArray<bool,1> axisDomainOrder = grid->axis_domain_order;
739         std::vector<StdString> domainList = grid->getDomainList();
740         std::vector<StdString> axisList   = grid->getAxisList();
741         int numElement = axisDomainOrder.numElements(), idxDomain = 0, idxAxis = 0;
742
743         std::vector<StdString> dims;
744
745         if (grid->isCompressible())
746         {
747           StdString varId = grid->getId() + "_points";
748
749           int nbIndexes = (SuperClass::type == MULTI_FILE) ? grid->getNumberWrittenIndexes() : grid->getTotalNumberWrittenIndexes();
750           SuperClassWriter::addDimension(varId, nbIndexes);
751
752           dims.push_back(varId);
753           SuperClassWriter::addVariable(varId, NC_INT, dims);
754
755           StdOStringStream compress;
756           for (int i = numElement - 1; i >= 0; --i)
757           {
758             if (axisDomainOrder(i))
759             {
760               CDomain* domain = CDomain::get(domainList[domainList.size() - idxDomain - 1]);
761               StdString domId = domain->getDomainOutputName();
762               StdString appendDomId  = singleDomain ? "" : "_" + domId;
763
764               switch (domain->type)
765               {
766                 case CDomain::type_attr::curvilinear:
767                   compress << "y" << appendDomId << " x" << appendDomId;
768                   break;
769                 case CDomain::type_attr::rectilinear:
770                   compress << "lat" << appendDomId << " lon" << appendDomId;
771                   break;
772                 case CDomain::type_attr::unstructured:
773                   compress << "cell" << appendDomId;
774                   break;
775               }
776               ++idxDomain;
777             }
778             else
779             {
780               CAxis* axis = CAxis::get(axisList[axisList.size() - idxAxis - 1]);
781               compress << axis->getAxisOutputName();
782               ++idxAxis;
783             }
784
785             if (i != 0) compress << ' ';
786           }
787           SuperClassWriter::addAttribute("compress", compress.str(), &varId);
788
789           grid->computeCompressedIndex();
790
791           CArray<int, 1> indexes(grid->getNumberWrittenIndexes());
792           std::map<int, CArray<size_t, 1> >::const_iterator it;
793           for (it = grid->outIndexFromClient.begin(); it != grid->outIndexFromClient.end(); ++it)
794           {
795             const CArray<size_t, 1> compressedIndexes = grid->compressedOutIndexFromClient[it->first];
796             for (int i = 0; i < it->second.numElements(); i++)
797               indexes(compressedIndexes(i)) = it->second(i);
798           }
799
800           switch (SuperClass::type)
801           {
802             case (MULTI_FILE):
803             {
804               SuperClassWriter::writeData(indexes, varId, isCollective, 0);
805               break;
806             }
807             case (ONE_FILE):
808             {
809               if (grid->doGridHaveDataDistributed())
810                 grid->getDistributionServer()->computeGlobalIndex(indexes);
811
812               std::vector<StdSize> start, count;
813               start.push_back(grid->getOffsetWrittenIndexes());
814               count.push_back(grid->getNumberWrittenIndexes());
815
816               SuperClassWriter::writeData(indexes, varId, isCollective, 0, &start, &count);
817               break;
818             }
819           }
820         }
821         else
822         {
823           for (int i = 0; i < numElement; ++i)
824           {
825             StdString varId, compress;
826             CArray<int, 1> indexes;
827             bool isDistributed;
828             StdSize nbIndexes, totalNbIndexes, offset;
829             int firstGlobalIndex;
830
831             if (axisDomainOrder(i))
832             {
833               CDomain* domain = CDomain::get(domainList[idxDomain]);
834               StdString domId = domain->getDomainOutputName();
835
836               if (!domain->isCompressible()
837                    || domain->type == CDomain::type_attr::unstructured
838                    || domain->isWrittenCompressed(this->filename)
839                    || isWrittenCompressedDomain(domId))
840                 continue;
841
842               StdString appendDomId  = singleDomain ? "" : "_" + domId;
843
844               varId = domId + "_points";
845               switch (domain->type)
846               {
847                 case CDomain::type_attr::curvilinear:
848                   compress = "y" + appendDomId + " x" + appendDomId;
849                   break;
850                 case CDomain::type_attr::rectilinear:
851                   compress = "lat" + appendDomId + " lon" + appendDomId;
852                   break;
853               }
854
855               const std::vector<int>& indexesToWrite = domain->getIndexesToWrite();
856               indexes.resize(indexesToWrite.size());
857               for (int n = 0; n < indexes.numElements(); ++n)
858                 indexes(n) = indexesToWrite[n];
859
860               isDistributed = domain->isDistributed();
861               nbIndexes = domain->getNumberWrittenIndexes();
862               totalNbIndexes = domain->getTotalNumberWrittenIndexes();
863               offset = domain->getOffsetWrittenIndexes();
864               firstGlobalIndex = domain->ibegin + domain->jbegin * domain->ni_glo;
865
866               domain->addRelFileCompressed(this->filename);
867               setWrittenCompressedDomain(domId);
868               ++idxDomain;
869             }
870             else
871             {
872               CAxis* axis = CAxis::get(axisList[idxAxis]);
873               StdString axisId = axis->getAxisOutputName();
874
875               if (!axis->isCompressible()
876                    || axis->isWrittenCompressed(this->filename)
877                    || isWrittenCompressedAxis(axisId))
878                 continue;
879
880               varId = axisId + "_points";
881               compress = axisId;
882
883               const std::vector<int>& indexesToWrite = axis->getIndexesToWrite();
884               indexes.resize(indexesToWrite.size());
885               for (int n = 0; n < indexes.numElements(); ++n)
886                 indexes(n) = indexesToWrite[n];
887
888               isDistributed = axis->isDistributed();
889               nbIndexes = axis->getNumberWrittenIndexes();
890               totalNbIndexes = axis->getTotalNumberWrittenIndexes();
891               offset = axis->getOffsetWrittenIndexes();
892               firstGlobalIndex = axis->begin;
893
894               axis->addRelFileCompressed(this->filename);
895               setWrittenCompressedAxis(axisId);
896               ++idxAxis;
897             }
898
899             if (!varId.empty())
900             {
901               SuperClassWriter::addDimension(varId, (SuperClass::type == MULTI_FILE) ? nbIndexes : totalNbIndexes);
902
903               dims.clear();
904               dims.push_back(varId);
905               SuperClassWriter::addVariable(varId, NC_INT, dims);
906
907               SuperClassWriter::addAttribute("compress", compress, &varId);
908
909               switch (SuperClass::type)
910               {
911                 case (MULTI_FILE):
912                 {
913                   indexes -= firstGlobalIndex;
914                   SuperClassWriter::writeData(indexes, varId, isCollective, 0);
915                   break;
916                 }
917                 case (ONE_FILE):
918                 {
919                   std::vector<StdSize> start, count;
920                   start.push_back(offset);
921                   count.push_back(nbIndexes);
922
923                   SuperClassWriter::writeData(indexes, varId, isCollective, 0, &start, &count);
924                   break;
925                 }
926               }
927             }
928           }
929
930           if (!dims.empty())
931             grid->computeCompressedIndex();
932         }
933
934         grid->addRelFileCompressed(this->filename);
935       }
936       catch (CNetCdfException& e)
937       {
938         StdString msg("On writing compressed grid : ");
939         msg.append(grid->getId()); msg.append("\n");
940         msg.append("In the context : ");
941         CContext* context = CContext::getCurrent();
942         msg.append(context->getId()); msg.append("\n");
943         msg.append(e.what());
944         ERROR("CNc4DataOutput::writeGridCompressed_(CGrid* grid)", << msg);
945       }
946     }
947
948     //--------------------------------------------------------------
949
950     void CNc4DataOutput::writeTimeDimension_(void)
951     {
952       try
953       {
954        SuperClassWriter::addDimension("time_counter");
955       }
956       catch (CNetCdfException& e)
957       {
958         StdString msg("On writing time dimension : time_couter\n");
959         msg.append("In the context : ");
960         CContext* context = CContext::getCurrent() ;
961         msg.append(context->getId()); msg.append("\n");
962         msg.append(e.what());
963         ERROR("CNc4DataOutput::writeTimeDimension_(void)", << msg);
964       }
965     }
966
967      //--------------------------------------------------------------
968
969      void CNc4DataOutput::writeField_(CField* field)
970      {
971         CContext* context = CContext::getCurrent() ;
972         CContextServer* server=context->server ;
973
974         std::vector<StdString> dims, coodinates;
975         CGrid* grid = field->grid;
976         if (!grid->doGridHaveDataToWrite())
977          if (SuperClass::type==MULTI_FILE) return ;
978
979         CArray<bool,1> axisDomainOrder = grid->axis_domain_order;
980         int numElement = axisDomainOrder.numElements(), idxDomain = 0, idxAxis = 0;
981         std::vector<StdString> domainList = grid->getDomainList();
982         std::vector<StdString> axisList   = grid->getAxisList();
983
984         StdString timeid  = StdString("time_counter");
985         StdString dimXid,dimYid;
986         std::deque<StdString> dimIdList, dimCoordList;
987         bool hasArea = false;
988         StdString cellMeasures = "area:";
989         bool compressedOutput = !field->indexed_output.isEmpty() && field->indexed_output;
990
991         for (int i = 0; i < numElement; ++i)
992         {
993           if (axisDomainOrder(i))
994           {
995             CDomain* domain = CDomain::get(domainList[idxDomain]);
996             StdString domId = domain->getDomainOutputName();
997             StdString appendDomId  = singleDomain ? "" : "_" + domId ;
998
999             if (compressedOutput && domain->isCompressible() && domain->type != CDomain::type_attr::unstructured)
1000             {
1001               dimIdList.push_back(domId + "_points");
1002               field->setUseCompressedOutput();
1003             }
1004
1005             switch (domain->type)
1006             {
1007               case CDomain::type_attr::curvilinear:
1008                 if (!compressedOutput || !domain->isCompressible())
1009                 {
1010                   dimXid     = StdString("x").append(appendDomId);
1011                   dimIdList.push_back(dimXid);
1012                   dimYid     = StdString("y").append(appendDomId);
1013                   dimIdList.push_back(dimYid);
1014                 }
1015                 dimCoordList.push_back(StdString("nav_lon").append(appendDomId));
1016                 dimCoordList.push_back(StdString("nav_lat").append(appendDomId));
1017                 break ;
1018               case CDomain::type_attr::rectilinear:
1019                 if (!compressedOutput || !domain->isCompressible())
1020                 {
1021                   dimXid     = StdString("lon").append(appendDomId);
1022                   dimIdList.push_back(dimXid);
1023                   dimYid     = StdString("lat").append(appendDomId);
1024                   dimIdList.push_back(dimYid);
1025                 }
1026                 break ;
1027               case CDomain::type_attr::unstructured:
1028                 dimXid     = StdString("cell").append(appendDomId);
1029                 dimIdList.push_back(dimXid);
1030                 dimCoordList.push_back(StdString("lon").append(appendDomId));
1031                 dimCoordList.push_back(StdString("lat").append(appendDomId));
1032                 break ;
1033             }
1034             if (domain->hasArea)
1035             {
1036               hasArea = true;
1037               cellMeasures += " area" + appendDomId;
1038             }
1039             ++idxDomain;
1040           }
1041           else
1042           {
1043             CAxis* axis = CAxis::get(axisList[idxAxis]);
1044             StdString axisId = axis->getAxisOutputName();
1045
1046             if (compressedOutput && axis->isCompressible())
1047             {
1048               dimIdList.push_back(axisId + "_points");
1049               field->setUseCompressedOutput();
1050             }
1051             else
1052               dimIdList.push_back(axisId);
1053
1054             dimCoordList.push_back(axisId);
1055             ++idxAxis;
1056           }
1057         }
1058
1059/*
1060         StdString lonid_loc = (server->intraCommSize > 1)
1061                             ? StdString("lon").append(appendDomid).append("_local")
1062                             : lonid;
1063         StdString latid_loc = (server->intraCommSize > 1)
1064                             ? StdString("lat").append(appendDomid).append("_local")
1065                             : latid;
1066*/
1067         StdString fieldid = field->getFieldOutputName();
1068
1069//         unsigned int ssize = domain->zoom_ni_loc.getValue() * domain->zoom_nj_loc.getValue();
1070//         bool isCurvilinear = (domain->lonvalue.getValue()->size() == ssize);
1071//          bool isCurvilinear = domain->isCurvilinear ;
1072
1073         nc_type type ;
1074         if (field->prec.isEmpty()) type =  NC_FLOAT ;
1075         else
1076         {
1077           if (field->prec==2) type = NC_SHORT ;
1078           else if (field->prec==4)  type =  NC_FLOAT ;
1079           else if (field->prec==8)   type =  NC_DOUBLE ;
1080         }
1081
1082         bool wtime   = !(!field->operation.isEmpty() && field->getOperationTimeType() == func::CFunctor::once);
1083
1084         if (wtime)
1085         {
1086
1087            //StdOStringStream oss;
1088           // oss << "time_" << field->operation.getValue()
1089           //     << "_" << field->getRelFile()->output_freq.getValue();
1090          //oss
1091            if (field->getOperationTimeType() == func::CFunctor::instant) coodinates.push_back(string("time_instant"));
1092            else if (field->getOperationTimeType() == func::CFunctor::centered) coodinates.push_back(string("time_centered"));
1093            dims.push_back(timeid);
1094         }
1095
1096         if (compressedOutput && grid->isCompressible())
1097         {
1098           dims.push_back(grid->getId() + "_points");
1099           field->setUseCompressedOutput();
1100         }
1101         else
1102         {
1103           while (!dimIdList.empty())
1104           {
1105             dims.push_back(dimIdList.back());
1106             dimIdList.pop_back();
1107           }
1108         }
1109
1110         while (!dimCoordList.empty())
1111         {
1112           coodinates.push_back(dimCoordList.back());
1113           dimCoordList.pop_back();
1114         }
1115
1116         try
1117         {
1118           SuperClassWriter::addVariable(fieldid, type, dims);
1119
1120           if (!field->standard_name.isEmpty())
1121              SuperClassWriter::addAttribute
1122                 ("standard_name",  field->standard_name.getValue(), &fieldid);
1123
1124           if (!field->long_name.isEmpty())
1125              SuperClassWriter::addAttribute
1126                 ("long_name", field->long_name.getValue(), &fieldid);
1127
1128           if (!field->unit.isEmpty())
1129              SuperClassWriter::addAttribute
1130                 ("units", field->unit.getValue(), &fieldid);
1131
1132            if (!field->valid_min.isEmpty())
1133              SuperClassWriter::addAttribute
1134                 ("valid_min", field->valid_min.getValue(), &fieldid);
1135
1136           if (!field->valid_max.isEmpty())
1137              SuperClassWriter::addAttribute
1138                 ("valid_max", field->valid_max.getValue(), &fieldid);
1139
1140            if (!field->scale_factor.isEmpty())
1141              SuperClassWriter::addAttribute
1142                 ("scale_factor", field->scale_factor.getValue(), &fieldid);
1143
1144             if (!field->add_offset.isEmpty())
1145              SuperClassWriter::addAttribute
1146                 ("add_offset", field->add_offset.getValue(), &fieldid);
1147
1148           SuperClassWriter::addAttribute
1149                 ("online_operation", field->operation.getValue(), &fieldid);
1150
1151          // write child variables as attributes
1152
1153
1154           vector<CVariable*> listVars = field->getAllVariables() ;
1155           for (vector<CVariable*>::iterator it = listVars.begin() ;it != listVars.end(); it++) writeAttribute_(*it, fieldid) ;
1156
1157
1158           if (wtime)
1159           {
1160              CDuration freqOp = field->freq_op.getValue();
1161              freqOp.solveTimeStep(*context->calendar);
1162              StdString freqOpStr = freqOp.toStringUDUnits();
1163              SuperClassWriter::addAttribute("interval_operation", freqOpStr, &fieldid);
1164
1165              CDuration freqOut = field->getRelFile()->output_freq.getValue();
1166              freqOut.solveTimeStep(*context->calendar);
1167              SuperClassWriter::addAttribute("interval_write", freqOut.toStringUDUnits(), &fieldid);
1168
1169              StdString cellMethods = "time: ";
1170              if (field->operation.getValue() == "instant") cellMethods += "point";
1171              else if (field->operation.getValue() == "average") cellMethods += "mean";
1172              else if (field->operation.getValue() == "accumulate") cellMethods += "sum";
1173              else cellMethods += field->operation;
1174              if (freqOp.resolve(*context->calendar) != freqOut.resolve(*context->calendar))
1175                cellMethods += " (interval: " + freqOpStr + ")";
1176              SuperClassWriter::addAttribute("cell_methods", cellMethods, &fieldid);
1177           }
1178
1179           if (hasArea)
1180             SuperClassWriter::addAttribute("cell_measures", cellMeasures, &fieldid);
1181
1182           if (!field->default_value.isEmpty())
1183           {
1184              double default_value = field->default_value.getValue();
1185              float fdefault_value = (float)default_value;
1186              if (type == NC_DOUBLE)
1187                 SuperClassWriter::setDefaultValue(fieldid, &default_value);
1188              else
1189                 SuperClassWriter::setDefaultValue(fieldid, &fdefault_value);
1190           }
1191           else
1192              SuperClassWriter::setDefaultValue(fieldid, (double*)NULL);
1193
1194            if (field->compression_level.isEmpty())
1195              field->compression_level = field->file->compression_level.isEmpty() ? 0 : field->file->compression_level;
1196            SuperClassWriter::setCompressionLevel(fieldid, field->compression_level);
1197
1198           {  // Ecriture des coordonnées
1199
1200              StdString coordstr; //boost::algorithm::join(coodinates, " ")
1201              std::vector<StdString>::iterator
1202                 itc = coodinates.begin(), endc = coodinates.end();
1203
1204              for (; itc!= endc; itc++)
1205              {
1206                 StdString & coord = *itc;
1207                 if (itc+1 != endc)
1208                       coordstr.append(coord).append(" ");
1209                 else  coordstr.append(coord);
1210              }
1211
1212              SuperClassWriter::addAttribute("coordinates", coordstr, &fieldid);
1213
1214           }
1215         }
1216         catch (CNetCdfException& e)
1217         {
1218           StdString msg("On writing field : ");
1219           msg.append(fieldid); msg.append("\n");
1220           msg.append("In the context : ");
1221           msg.append(context->getId()); msg.append("\n");
1222           msg.append(e.what());
1223           ERROR("CNc4DataOutput::writeField_(CField* field)", << msg);
1224         }
1225      }
1226
1227      //--------------------------------------------------------------
1228
1229      void CNc4DataOutput::writeFile_ (CFile* file)
1230      {
1231         StdString filename = file->getFileOutputName();
1232         StdString description = (!file->description.isEmpty())
1233                               ? file->description.getValue()
1234                               : StdString("Created by xios");
1235
1236         singleDomain = (file->nbDomains == 1);
1237
1238         try
1239         {
1240           this->writeFileAttributes(filename, description,
1241                                     StdString("CF-1.5"),
1242                                     StdString("An IPSL model"),
1243                                     this->getTimeStamp());
1244
1245           if (!appendMode)
1246             SuperClassWriter::addDimension("axis_nbounds", 2);
1247         }
1248         catch (CNetCdfException& e)
1249         {
1250           StdString msg("On writing file : ");
1251           msg.append(filename); msg.append("\n");
1252           msg.append("In the context : ");
1253           CContext* context = CContext::getCurrent() ;
1254           msg.append(context->getId()); msg.append("\n");
1255           msg.append(e.what());
1256           ERROR("CNc4DataOutput::writeFile_ (CFile* file)", << msg);
1257         }
1258      }
1259
1260      void CNc4DataOutput::writeAttribute_ (CVariable* var, const string& fieldId)
1261      {
1262        StdString name = var->getVariableOutputName();
1263
1264        try
1265        {
1266          if (var->type.getValue() == CVariable::type_attr::t_int || var->type.getValue() == CVariable::type_attr::t_int32)
1267            addAttribute(name, var->getData<int>(), &fieldId);
1268          else if (var->type.getValue() == CVariable::type_attr::t_int16)
1269            addAttribute(name, var->getData<short int>(), &fieldId);
1270          else if (var->type.getValue() == CVariable::type_attr::t_float)
1271            addAttribute(name, var->getData<float>(), &fieldId);
1272          else if (var->type.getValue() == CVariable::type_attr::t_double)
1273            addAttribute(name, var->getData<double>(), &fieldId);
1274          else if (var->type.getValue() == CVariable::type_attr::t_string)
1275            addAttribute(name, var->getData<string>(), &fieldId);
1276          else
1277            ERROR("CNc4DataOutput::writeAttribute_ (CVariable* var, const string& fieldId)",
1278                  << "Unsupported variable of type " << var->type.getStringValue());
1279        }
1280       catch (CNetCdfException& e)
1281       {
1282         StdString msg("On writing attributes of variable with name : ");
1283         msg.append(name); msg.append("in the field "); msg.append(fieldId); msg.append("\n");
1284         msg.append("In the context : ");
1285         CContext* context = CContext::getCurrent() ;
1286         msg.append(context->getId()); msg.append("\n");
1287         msg.append(e.what());
1288         ERROR("CNc4DataOutput::writeAttribute_ (CVariable* var, const string& fieldId)", << msg);
1289       }
1290     }
1291
1292     void CNc4DataOutput::writeAttribute_ (CVariable* var)
1293     {
1294        StdString name = var->getVariableOutputName();
1295
1296        try
1297        {
1298          if (var->type.getValue() == CVariable::type_attr::t_int || var->type.getValue() == CVariable::type_attr::t_int32)
1299            addAttribute(name, var->getData<int>());
1300          else if (var->type.getValue() == CVariable::type_attr::t_int16)
1301            addAttribute(name, var->getData<short int>());
1302          else if (var->type.getValue() == CVariable::type_attr::t_float)
1303            addAttribute(name, var->getData<float>());
1304          else if (var->type.getValue() == CVariable::type_attr::t_double)
1305            addAttribute(name, var->getData<double>());
1306          else if (var->type.getValue() == CVariable::type_attr::t_string)
1307            addAttribute(name, var->getData<string>());
1308          else
1309            ERROR("CNc4DataOutput::writeAttribute_ (CVariable* var)",
1310                  << "Unsupported variable of type " << var->type.getStringValue());
1311        }
1312       catch (CNetCdfException& e)
1313       {
1314         StdString msg("On writing attributes of variable with name : ");
1315         msg.append(name); msg.append("\n");
1316         msg.append("In the context : ");
1317         CContext* context = CContext::getCurrent() ;
1318         msg.append(context->getId()); msg.append("\n");
1319         msg.append(e.what());
1320         ERROR("CNc4DataOutput::writeAttribute_ (CVariable* var)", << msg);
1321       }
1322     }
1323
1324      void CNc4DataOutput::syncFile_ (void)
1325      {
1326        try
1327        {
1328          SuperClassWriter::sync() ;
1329        }
1330        catch (CNetCdfException& e)
1331        {
1332         StdString msg("On synchronizing the write among processes");
1333         msg.append("In the context : ");
1334         CContext* context = CContext::getCurrent() ;
1335         msg.append(context->getId()); msg.append("\n");
1336         msg.append(e.what());
1337         ERROR("CNc4DataOutput::syncFile_ (void)", << msg);
1338        }
1339      }
1340
1341      void CNc4DataOutput::closeFile_ (void)
1342      {
1343        try
1344        {
1345          SuperClassWriter::close() ;
1346        }
1347        catch (CNetCdfException& e)
1348        {
1349         StdString msg("On closing file");
1350         msg.append("In the context : ");
1351         CContext* context = CContext::getCurrent() ;
1352         msg.append(context->getId()); msg.append("\n");
1353         msg.append(e.what());
1354         ERROR("CNc4DataOutput::syncFile_ (void)", << msg);
1355        }
1356
1357      }
1358
1359      //---------------------------------------------------------------
1360
1361      StdString CNc4DataOutput::getTimeStamp(void) const
1362      {
1363         const int buffer_size = 100;
1364         time_t rawtime;
1365         struct tm * timeinfo = NULL;
1366         char buffer [buffer_size];
1367
1368         time ( &rawtime );
1369         timeinfo = localtime ( &rawtime );
1370         strftime (buffer, buffer_size, "%Y-%b-%d %H:%M:%S %Z", timeinfo);
1371
1372         return (StdString(buffer));
1373      }
1374
1375      //---------------------------------------------------------------
1376
1377      void CNc4DataOutput::writeFieldData_ (CField*  field)
1378      {
1379        CContext* context = CContext::getCurrent();
1380        CContextServer* server = context->server;
1381        CGrid* grid = field->grid;
1382
1383        if (!grid->doGridHaveDataToWrite())
1384          if (SuperClass::type == MULTI_FILE || !isCollective) return;
1385
1386        StdString fieldid = field->getFieldOutputName();
1387
1388        StdOStringStream oss;
1389        string timeAxisId;
1390        if (field->getOperationTimeType() == func::CFunctor::instant) timeAxisId = "time_instant";
1391        else if (field->getOperationTimeType() == func::CFunctor::centered) timeAxisId = "time_centered";
1392
1393        StdString timeBoundId("time_counter_bounds");
1394
1395        StdString timeAxisBoundId;
1396        if (field->getOperationTimeType() == func::CFunctor::instant) timeAxisBoundId = "time_instant_bounds";
1397        else if (field->getOperationTimeType() == func::CFunctor::centered) timeAxisBoundId = "time_centered_bounds";
1398
1399        if (!field->wasWritten())
1400        {
1401          if (appendMode && field->file->record_offset.isEmpty())
1402          {
1403            field->resetNStep(getRecordFromTime(field->last_Write_srv) + 1);
1404          }
1405
1406          field->setWritten();
1407        }
1408
1409
1410        CArray<double,1> time_data(1);
1411        CArray<double,1> time_data_bound(2);
1412        CArray<double,1> time_counter(1);
1413        CArray<double,1> time_counter_bound(2);
1414
1415        bool wtime = (field->getOperationTimeType() != func::CFunctor::once);
1416
1417        if (wtime)
1418        {
1419          Time lastWrite = field->last_Write_srv;
1420          Time lastLastWrite = field->lastlast_Write_srv;
1421
1422          if (field->getOperationTimeType() == func::CFunctor::instant)
1423            time_data(0) = lastWrite;
1424          else if (field->getOperationTimeType() == func::CFunctor::centered)
1425            time_data(0) = (lastWrite + lastLastWrite) / 2;
1426
1427          if (field->getOperationTimeType() == func::CFunctor::instant)
1428            time_data_bound(0) = time_data_bound(1) = lastWrite;
1429          else if (field->getOperationTimeType() == func::CFunctor::centered)
1430          {
1431            time_data_bound(0) = lastLastWrite;
1432            time_data_bound(1) = lastWrite;
1433          }
1434
1435          if (field->file->time_counter == CFile::time_counter_attr::instant)
1436            time_counter(0) = lastWrite;
1437          else if (field->file->time_counter == CFile::time_counter_attr::centered)
1438            time_counter(0) = (lastWrite + lastLastWrite) / 2;
1439          else if (field->file->time_counter == CFile::time_counter_attr::record)
1440            time_counter(0) = field->getNStep() - 1;
1441
1442
1443          if (field->file->time_counter == CFile::time_counter_attr::instant)
1444            time_counter_bound(0) = time_counter_bound(1) = lastWrite;
1445          else if (field->file->time_counter == CFile::time_counter_attr::centered)
1446          {
1447            time_counter_bound(0) = lastLastWrite;
1448            time_counter_bound(1) = lastWrite;
1449          }
1450          else if (field->file->time_counter == CFile::time_counter_attr::record)
1451            time_counter_bound(0) = time_counter_bound(1) = field->getNStep() - 1;
1452        }
1453
1454         bool isRoot = (server->intraCommRank == 0);
1455
1456         if (!field->scale_factor.isEmpty() || !field->add_offset.isEmpty())
1457         {
1458           double scaleFactor = 1.0;
1459           double addOffset = 0.0;
1460           if (!field->scale_factor.isEmpty()) scaleFactor = field->scale_factor;
1461           if (!field->add_offset.isEmpty()) addOffset = field->add_offset;
1462           field->scaleFactorAddOffset(scaleFactor, addOffset);
1463         }
1464
1465         try
1466         {
1467           size_t writtenSize;
1468           if (field->getUseCompressedOutput())
1469             writtenSize = grid->getNumberWrittenIndexes();
1470           else
1471             writtenSize = grid->getWrittenDataSize();
1472
1473           CArray<double,1> fieldData(writtenSize);
1474           if (!field->default_value.isEmpty()) fieldData = field->default_value;
1475
1476           if (field->getUseCompressedOutput())
1477             field->outputCompressedField(fieldData);
1478           else
1479             field->outputField(fieldData);
1480
1481           if (!field->prec.isEmpty() && field->prec == 2) fieldData = round(fieldData);
1482
1483           switch (SuperClass::type)
1484           {
1485              case (MULTI_FILE) :
1486              {
1487                 SuperClassWriter::writeData(fieldData, fieldid, isCollective, field->getNStep() - 1);
1488                 if (wtime)
1489                 {
1490                   SuperClassWriter::writeData(time_data, timeAxisId, isCollective, field->getNStep() - 1);
1491                   SuperClassWriter::writeData(time_data_bound, timeAxisBoundId, isCollective, field->getNStep() - 1);
1492                   if (field->file->time_counter != CFile::time_counter_attr::none)
1493                   {
1494                     SuperClassWriter::writeData(time_counter, string("time_counter"), isCollective, field->getNStep() - 1);
1495                     if (field->file->time_counter != CFile::time_counter_attr::record)
1496                       SuperClassWriter::writeData(time_counter_bound, timeBoundId, isCollective, field->getNStep() - 1);
1497                   }
1498                 }
1499                 break;
1500              }
1501              case (ONE_FILE) :
1502              {
1503                const std::vector<int>& nZoomBeginGlobal = grid->getDistributionServer()->getZoomBeginGlobal();
1504                const std::vector<int>& nZoomBeginServer = grid->getDistributionServer()->getZoomBeginServer();
1505                const std::vector<int>& nZoomSizeServer  = grid->getDistributionServer()->getZoomSizeServer();
1506
1507                std::vector<StdSize> start, count;
1508
1509                if (field->getUseCompressedOutput())
1510                {
1511                  if (grid->isCompressible())
1512                  {
1513                    start.push_back(grid->getOffsetWrittenIndexes());
1514                    count.push_back(grid->getNumberWrittenIndexes());
1515                  }
1516                  else
1517                  {
1518                    CArray<bool,1> axisDomainOrder = grid->axis_domain_order;
1519                    std::vector<StdString> domainList = grid->getDomainList();
1520                    std::vector<StdString> axisList   = grid->getAxisList();
1521                    int numElement = axisDomainOrder.numElements();
1522                    int idxDomain = domainList.size() - 1, idxAxis = axisList.size() - 1;
1523                    int idx = nZoomBeginGlobal.size() - 1;
1524
1525                    start.reserve(nZoomBeginGlobal.size());
1526                    count.reserve(nZoomBeginGlobal.size());
1527
1528
1529                    for (int i = numElement - 1; i >= 0; --i)
1530                    {
1531                      if (axisDomainOrder(i))
1532                      {
1533                        CDomain* domain = CDomain::get(domainList[idxDomain]);
1534
1535                        if (domain->isCompressible())
1536                        {
1537                          start.push_back(domain->getOffsetWrittenIndexes());
1538                          count.push_back(domain->getNumberWrittenIndexes());
1539                          idx -= 2;
1540                        }
1541                        else
1542                        {
1543                          if ((domain->type) != CDomain::type_attr::unstructured)
1544                          {
1545                            start.push_back(nZoomBeginServer[idx] - nZoomBeginGlobal[idx]);
1546                            count.push_back(nZoomSizeServer[idx]);
1547                          }
1548                          --idx;
1549                          start.push_back(nZoomBeginServer[idx] - nZoomBeginGlobal[idx]);
1550                          count.push_back(nZoomSizeServer[idx]);
1551                          --idx;
1552                        }
1553                        --idxDomain;
1554                      }
1555                      else
1556                      {
1557                        CAxis* axis = CAxis::get(axisList[idxAxis]);
1558
1559                        if (axis->isCompressible())
1560                        {
1561                          start.push_back(axis->getOffsetWrittenIndexes());
1562                          count.push_back(axis->getNumberWrittenIndexes());
1563                        }
1564                        else
1565                        {
1566                          start.push_back(nZoomBeginServer[idx] - nZoomBeginGlobal[idx]);
1567                          count.push_back(nZoomSizeServer[idx]);
1568                        }
1569
1570                        --idxAxis;
1571                        --idx;
1572                      }
1573                    }
1574                  }
1575                }
1576                else
1577                {
1578
1579                  CArray<bool,1> axisDomainOrder = grid->axis_domain_order;
1580                  std::vector<StdString> domainList = grid->getDomainList();
1581                  std::vector<StdString> axisList   = grid->getAxisList();
1582                  int numElement = axisDomainOrder.numElements();
1583                  int idxDomain = domainList.size() - 1, idxAxis = axisList.size() - 1;
1584                  int idx = nZoomBeginGlobal.size() - 1;
1585
1586                  start.reserve(nZoomBeginGlobal.size());
1587                  count.reserve(nZoomBeginGlobal.size());
1588
1589                  for (int i = numElement - 1; i >= 0; --i)
1590                  {
1591                    if (axisDomainOrder(i))
1592                    {
1593                      CDomain* domain = CDomain::get(domainList[idxDomain]);
1594                      if ((domain->type) != CDomain::type_attr::unstructured)
1595                      {
1596                        start.push_back(nZoomBeginServer[idx] - nZoomBeginGlobal[idx]);
1597                        count.push_back(nZoomSizeServer[idx]);
1598                      }
1599                      --idx ;
1600                      start.push_back(nZoomBeginServer[idx] - nZoomBeginGlobal[idx]);
1601                      count.push_back(nZoomSizeServer[idx]);
1602                      --idx ;
1603                      --idxDomain;
1604                    }
1605                    else
1606                    {
1607                      start.push_back(nZoomBeginServer[idx] - nZoomBeginGlobal[idx]);
1608                      count.push_back(nZoomSizeServer[idx]);
1609                      --idx;
1610                     }
1611                  }
1612                }
1613
1614                SuperClassWriter::writeData(fieldData, fieldid, isCollective, field->getNStep() - 1, &start, &count);
1615                if (wtime)
1616                {
1617                   SuperClassWriter::writeTimeAxisData(time_data, timeAxisId, isCollective, field->getNStep() - 1, isRoot);
1618                   SuperClassWriter::writeTimeAxisData(time_data_bound, timeAxisBoundId, isCollective, field->getNStep() - 1, isRoot);
1619                   if (field->file->time_counter != CFile::time_counter_attr::none)
1620                   {
1621                     SuperClassWriter::writeTimeAxisData(time_counter, string("time_counter"), isCollective, field->getNStep() - 1, isRoot);
1622                     if (field->file->time_counter != CFile::time_counter_attr::record)
1623                       SuperClassWriter::writeTimeAxisData(time_counter_bound, timeBoundId, isCollective, field->getNStep() - 1, isRoot);
1624                   }
1625                }
1626
1627                break;
1628              }
1629            }
1630         }
1631         catch (CNetCdfException& e)
1632         {
1633           StdString msg("On writing field data: ");
1634           msg.append(fieldid); msg.append("\n");
1635           msg.append("In the context : ");
1636           msg.append(context->getId()); msg.append("\n");
1637           msg.append(e.what());
1638           ERROR("CNc4DataOutput::writeFieldData_ (CField*  field)", << msg);
1639         }
1640      }
1641
1642      //---------------------------------------------------------------
1643
1644      void CNc4DataOutput::writeTimeAxis_
1645                  (CField*    field,
1646                   const boost::shared_ptr<CCalendar> cal)
1647      {
1648         StdOStringStream oss;
1649
1650         if (field->getOperationTimeType() == func::CFunctor::once) return ;
1651
1652//         oss << "time_" << field->operation.getValue()
1653//             << "_" << field->getRelFile()->output_freq.getValue();
1654
1655//         StdString axisid = oss.str();
1656//         if (field->getOperationTimeType() == func::CFunctor::centered) axisid="time_centered" ;
1657//         else if (field->getOperationTimeType() == func::CFunctor::instant) axisid="time_instant" ;
1658
1659         StdString axisid("time_centered") ;
1660         StdString axisBoundId("time_centered_bounds");
1661         StdString timeid("time_counter");
1662         StdString timeBoundId("axis_nbounds");
1663
1664         if (field->getOperationTimeType() == func::CFunctor::instant)
1665         {
1666            axisid = "time_instant";
1667            axisBoundId = "time_instant_bounds";
1668         }
1669
1670         try
1671         {
1672          // Adding time_instant or time_centered
1673           std::vector<StdString> dims;
1674           dims.push_back(timeid);
1675           if (!SuperClassWriter::varExist(axisid))
1676           {
1677              SuperClassWriter::addVariable(axisid, NC_DOUBLE, dims);
1678
1679              CDate timeOrigin=cal->getTimeOrigin() ;
1680              StdOStringStream oss2;
1681  //            oss2<<initDate.getYear()<<"-"<<initDate.getMonth()<<"-"<<initDate.getDay()<<" "
1682  //                <<initDate.getHour()<<"-"<<initDate.getMinute()<<"-"<<initDate.getSecond() ;
1683              StdString strInitdate=oss2.str() ;
1684              StdString strTimeOrigin=timeOrigin.toString() ;
1685              this->writeTimeAxisAttributes
1686                 (axisid, cal->getType(),
1687                  StdString("seconds since ").append(strTimeOrigin),
1688                  strTimeOrigin, axisBoundId);
1689           }
1690
1691           // Adding time_instant_bounds or time_centered_bounds variables
1692           if (!SuperClassWriter::varExist(axisBoundId))
1693           {
1694              dims.clear() ;
1695              dims.push_back(timeid);
1696              dims.push_back(timeBoundId);
1697              SuperClassWriter::addVariable(axisBoundId, NC_DOUBLE, dims);
1698           }
1699
1700           if (field->file->time_counter != CFile::time_counter_attr::none)
1701           {
1702             // Adding time_counter
1703             axisid = "time_counter";
1704             axisBoundId = "time_counter_bounds";
1705             dims.clear();
1706             dims.push_back(timeid);
1707             if (!SuperClassWriter::varExist(axisid))
1708             {
1709                SuperClassWriter::addVariable(axisid, NC_DOUBLE, dims);
1710                SuperClassWriter::addAttribute("axis", string("T"), &axisid);
1711
1712                if (field->file->time_counter != CFile::time_counter_attr::record)
1713                {
1714                  CDate timeOrigin = cal->getTimeOrigin();
1715                  StdString strTimeOrigin = timeOrigin.toString();
1716
1717                  this->writeTimeAxisAttributes(axisid, cal->getType(),
1718                                                StdString("seconds since ").append(strTimeOrigin),
1719                                                strTimeOrigin, axisBoundId);
1720                }
1721             }
1722
1723             // Adding time_counter_bound dimension
1724             if (field->file->time_counter != CFile::time_counter_attr::record)
1725             {
1726                if (!SuperClassWriter::varExist(axisBoundId))
1727                {
1728                  dims.clear();
1729                  dims.push_back(timeid);
1730                  dims.push_back(timeBoundId);
1731                  SuperClassWriter::addVariable(axisBoundId, NC_DOUBLE, dims);
1732                }
1733             }
1734           }
1735         }
1736         catch (CNetCdfException& e)
1737         {
1738           StdString msg("On writing time axis data: ");
1739           msg.append("In the context : ");
1740           CContext* context = CContext::getCurrent() ;
1741           msg.append(context->getId()); msg.append("\n");
1742           msg.append(e.what());
1743           ERROR("CNc4DataOutput::writeTimeAxis_ (CField*    field, \
1744                  const boost::shared_ptr<CCalendar> cal)", << msg);
1745         }
1746      }
1747
1748      //---------------------------------------------------------------
1749
1750      void CNc4DataOutput::writeTimeAxisAttributes(const StdString & axis_name,
1751                                                   const StdString & calendar,
1752                                                   const StdString & units,
1753                                                   const StdString & time_origin,
1754                                                   const StdString & time_bounds,
1755                                                   const StdString & standard_name,
1756                                                   const StdString & long_name)
1757      {
1758         try
1759         {
1760           SuperClassWriter::addAttribute("standard_name", standard_name, &axis_name);
1761           SuperClassWriter::addAttribute("long_name",     long_name    , &axis_name);
1762           SuperClassWriter::addAttribute("calendar",      calendar     , &axis_name);
1763           SuperClassWriter::addAttribute("units",         units        , &axis_name);
1764           SuperClassWriter::addAttribute("time_origin",   time_origin  , &axis_name);
1765           SuperClassWriter::addAttribute("bounds",        time_bounds  , &axis_name);
1766         }
1767         catch (CNetCdfException& e)
1768         {
1769           StdString msg("On writing time axis Attribute: ");
1770           msg.append("In the context : ");
1771           CContext* context = CContext::getCurrent() ;
1772           msg.append(context->getId()); msg.append("\n");
1773           msg.append(e.what());
1774           ERROR("CNc4DataOutput::writeTimeAxisAttributes(const StdString & axis_name, \
1775                                                          const StdString & calendar,\
1776                                                          const StdString & units, \
1777                                                          const StdString & time_origin, \
1778                                                          const StdString & time_bounds, \
1779                                                          const StdString & standard_name, \
1780                                                          const StdString & long_name)", << msg);
1781         }
1782      }
1783
1784      //---------------------------------------------------------------
1785
1786      void CNc4DataOutput::writeAxisAttributes(const StdString & axis_name,
1787                                               const StdString & axis,
1788                                               const StdString & standard_name,
1789                                               const StdString & long_name,
1790                                               const StdString & units,
1791                                               const StdString & nav_model)
1792      {
1793         try
1794         {
1795          if (!axis.empty())
1796            SuperClassWriter::addAttribute("axis"       , axis         , &axis_name);
1797
1798          SuperClassWriter::addAttribute("standard_name", standard_name, &axis_name);
1799          SuperClassWriter::addAttribute("long_name"    , long_name    , &axis_name);
1800          SuperClassWriter::addAttribute("units"        , units        , &axis_name);
1801          SuperClassWriter::addAttribute("nav_model"    , nav_model    , &axis_name);
1802         }
1803         catch (CNetCdfException& e)
1804         {
1805           StdString msg("On writing Axis Attribute: ");
1806           msg.append("In the context : ");
1807           CContext* context = CContext::getCurrent() ;
1808           msg.append(context->getId()); msg.append("\n");
1809           msg.append(e.what());
1810           ERROR("CNc4DataOutput::writeAxisAttributes(const StdString & axis_name, \
1811                                                      const StdString & axis, \
1812                                                      const StdString & standard_name, \
1813                                                      const StdString & long_name, \
1814                                                      const StdString & units, \
1815                                                      const StdString & nav_model)", << msg);
1816         }
1817      }
1818
1819      //---------------------------------------------------------------
1820
1821      void CNc4DataOutput::writeLocalAttributes
1822         (int ibegin, int ni, int jbegin, int nj, StdString domid)
1823      {
1824        try
1825        {
1826         SuperClassWriter::addAttribute(StdString("ibegin").append(domid), ibegin);
1827         SuperClassWriter::addAttribute(StdString("ni"    ).append(domid), ni);
1828         SuperClassWriter::addAttribute(StdString("jbegin").append(domid), jbegin);
1829         SuperClassWriter::addAttribute(StdString("nj"    ).append(domid), nj);
1830        }
1831        catch (CNetCdfException& e)
1832        {
1833           StdString msg("On writing Local Attributes: ");
1834           msg.append("In the context : ");
1835           CContext* context = CContext::getCurrent() ;
1836           msg.append(context->getId()); msg.append("\n");
1837           msg.append(e.what());
1838           ERROR("CNc4DataOutput::writeLocalAttributes \
1839                  (int ibegin, int ni, int jbegin, int nj, StdString domid)", << msg);
1840        }
1841
1842      }
1843
1844      void CNc4DataOutput::writeLocalAttributes_IOIPSL(const StdString& dimXid, const StdString& dimYid,
1845                                                       int ibegin, int ni, int jbegin, int nj, int ni_glo, int nj_glo, int rank, int size)
1846      {
1847         CArray<int,1> array(2) ;
1848
1849         try
1850         {
1851           SuperClassWriter::addAttribute("DOMAIN_number_total",size ) ;
1852           SuperClassWriter::addAttribute("DOMAIN_number", rank) ;
1853           array = SuperClassWriter::getDimension(dimXid) + 1, SuperClassWriter::getDimension(dimYid) + 1;
1854           SuperClassWriter::addAttribute("DOMAIN_dimensions_ids",array) ;
1855           array=ni_glo,nj_glo ;
1856           SuperClassWriter::addAttribute("DOMAIN_size_global", array) ;
1857           array=ni,nj ;
1858           SuperClassWriter::addAttribute("DOMAIN_size_local", array) ;
1859           array=ibegin,jbegin ;
1860           SuperClassWriter::addAttribute("DOMAIN_position_first", array) ;
1861           array=ibegin+ni-1,jbegin+nj-1 ;
1862           SuperClassWriter::addAttribute("DOMAIN_position_last",array) ;
1863           array=0,0 ;
1864           SuperClassWriter::addAttribute("DOMAIN_halo_size_start", array) ;
1865           SuperClassWriter::addAttribute("DOMAIN_halo_size_end", array);
1866           SuperClassWriter::addAttribute("DOMAIN_type",string("box")) ;
1867  /*
1868           SuperClassWriter::addAttribute("DOMAIN_DIM_N001",string("x")) ;
1869           SuperClassWriter::addAttribute("DOMAIN_DIM_N002",string("y")) ;
1870           SuperClassWriter::addAttribute("DOMAIN_DIM_N003",string("axis_A")) ;
1871           SuperClassWriter::addAttribute("DOMAIN_DIM_N004",string("time_counter")) ;
1872  */
1873         }
1874         catch (CNetCdfException& e)
1875         {
1876           StdString msg("On writing Local Attributes IOIPSL \n");
1877           msg.append("In the context : ");
1878           CContext* context = CContext::getCurrent() ;
1879           msg.append(context->getId()); msg.append("\n");
1880           msg.append(e.what());
1881           ERROR("CNc4DataOutput::writeLocalAttributes_IOIPSL \
1882                  (int ibegin, int ni, int jbegin, int nj, int ni_glo, int nj_glo, int rank, int size)", << msg);
1883         }
1884      }
1885      //---------------------------------------------------------------
1886
1887      void CNc4DataOutput:: writeFileAttributes(const StdString & name,
1888                                                const StdString & description,
1889                                                const StdString & conventions,
1890                                                const StdString & production,
1891                                                const StdString & timeStamp)
1892      {
1893         try
1894         {
1895           SuperClassWriter::addAttribute("name"       , name);
1896           SuperClassWriter::addAttribute("description", description);
1897           SuperClassWriter::addAttribute("title"      , description);
1898           SuperClassWriter::addAttribute("Conventions", conventions);
1899           SuperClassWriter::addAttribute("production" , production);
1900           SuperClassWriter::addAttribute("timeStamp"  , timeStamp);
1901         }
1902         catch (CNetCdfException& e)
1903         {
1904           StdString msg("On writing File Attributes \n ");
1905           msg.append("In the context : ");
1906           CContext* context = CContext::getCurrent() ;
1907           msg.append(context->getId()); msg.append("\n");
1908           msg.append(e.what());
1909           ERROR("CNc4DataOutput:: writeFileAttributes(const StdString & name, \
1910                                                const StdString & description, \
1911                                                const StdString & conventions, \
1912                                                const StdString & production, \
1913                                                const StdString & timeStamp)", << msg);
1914         }
1915      }
1916
1917      //---------------------------------------------------------------
1918
1919      void CNc4DataOutput::writeMaskAttributes(const StdString & mask_name,
1920                                               int data_dim,
1921                                               int data_ni,
1922                                               int data_nj,
1923                                               int data_ibegin,
1924                                               int data_jbegin)
1925      {
1926         try
1927         {
1928           SuperClassWriter::addAttribute("data_dim"   , data_dim   , &mask_name);
1929           SuperClassWriter::addAttribute("data_ni"    , data_ni    , &mask_name);
1930           SuperClassWriter::addAttribute("data_nj"    , data_nj    , &mask_name);
1931           SuperClassWriter::addAttribute("data_ibegin", data_ibegin, &mask_name);
1932           SuperClassWriter::addAttribute("data_jbegin", data_jbegin, &mask_name);
1933         }
1934         catch (CNetCdfException& e)
1935         {
1936           StdString msg("On writing Mask Attributes \n ");
1937           msg.append("In the context : ");
1938           CContext* context = CContext::getCurrent() ;
1939           msg.append(context->getId()); msg.append("\n");
1940           msg.append(e.what());
1941           ERROR("CNc4DataOutput::writeMaskAttributes(const StdString & mask_name, \
1942                                               int data_dim, \
1943                                               int data_ni, \
1944                                               int data_nj, \
1945                                               int data_ibegin, \
1946                                               int data_jbegin)", << msg);
1947         }
1948      }
1949
1950      ///--------------------------------------------------------------
1951
1952      StdSize CNc4DataOutput::getRecordFromTime(Time time)
1953      {
1954        std::map<Time, StdSize>::const_iterator it = timeToRecordCache.find(time);
1955        if (it == timeToRecordCache.end())
1956        {
1957          StdString timeAxisBoundsId("time_counter_bounds");
1958          if (!SuperClassWriter::varExist(timeAxisBoundsId))
1959            timeAxisBoundsId = "time_instant_bounds";
1960
1961          CArray<double,2> timeAxisBounds;
1962          SuperClassWriter::getTimeAxisBounds(timeAxisBounds, timeAxisBoundsId, isCollective);
1963
1964          StdSize record = 0;
1965          double dtime(time);
1966          for (int n = timeAxisBounds.extent(1) - 1; n >= 0; n--)
1967          {
1968            if (timeAxisBounds(1, n) < dtime)
1969            {
1970              record = n + 1;
1971              break;
1972            }
1973          }
1974          it = timeToRecordCache.insert(std::make_pair(time, record)).first;
1975        }
1976        return it->second;
1977      }
1978
1979      ///--------------------------------------------------------------
1980
1981      bool CNc4DataOutput::isWrittenDomain(const std::string& domainName) const
1982      {
1983        return (this->writtenDomains.find(domainName) != this->writtenDomains.end());
1984      }
1985
1986      bool CNc4DataOutput::isWrittenCompressedDomain(const std::string& domainName) const
1987      {
1988        return (this->writtenCompressedDomains.find(domainName) != this->writtenCompressedDomains.end());
1989      }
1990
1991      bool CNc4DataOutput::isWrittenAxis(const std::string& axisName) const
1992      {
1993        return (this->writtenAxis.find(axisName) != this->writtenAxis.end());
1994      }
1995
1996      bool CNc4DataOutput::isWrittenCompressedAxis(const std::string& axisName) const
1997      {
1998        return (this->writtenCompressedAxis.find(axisName) != this->writtenCompressedAxis.end());
1999      }
2000
2001      void CNc4DataOutput::setWrittenDomain(const std::string& domainName)
2002      {
2003        this->writtenDomains.insert(domainName);
2004      }
2005
2006      void CNc4DataOutput::setWrittenCompressedDomain(const std::string& domainName)
2007      {
2008        this->writtenCompressedDomains.insert(domainName);
2009      }
2010
2011      void CNc4DataOutput::setWrittenAxis(const std::string& axisName)
2012      {
2013        this->writtenAxis.insert(axisName);
2014      }
2015
2016      void CNc4DataOutput::setWrittenCompressedAxis(const std::string& axisName)
2017      {
2018        this->writtenCompressedAxis.insert(axisName);
2019      }
2020} // namespace xios
Note: See TracBrowser for help on using the repository browser.