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

Last change on this file since 774 was 774, checked in by rlacroix, 8 years ago

Fix: Improve the detection of already outputed compressed axis/domain.

  • 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.8 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) ;
697              std::vector<StdSize> count(1) ;
698              start[0] = zoom_begin_srv-axis->global_zoom_begin;
699              count[0] = zoom_size_srv;
700              SuperClassWriter::writeData(axis_value, axisid, isCollective, 0, &start, &count);
701
702              if (!axis->bounds.isEmpty())
703                SuperClassWriter::writeData(axis->bound_srv, axisBoundsId, isCollective, 0, &start, &count);
704
705              SuperClassWriter::definition_start();
706
707              break;
708            }
709            default :
710              ERROR("CNc4DataOutput::writeDomain(domain)",
711                    << "[ type = " << SuperClass::type << "]"
712                    << " not implemented yet !");
713          }
714        }
715        catch (CNetCdfException& e)
716        {
717          StdString msg("On writing the axis : ");
718          msg.append(axisid); msg.append("\n");
719          msg.append("In the context : ");
720          CContext* context = CContext::getCurrent() ;
721          msg.append(context->getId()); msg.append("\n");
722          msg.append(e.what());
723          ERROR("CNc4DataOutput::writeAxis_(CAxis* axis)", << msg);
724        }
725        axis->addRelFile(this->filename);
726     }
727
728     //--------------------------------------------------------------
729
730     void CNc4DataOutput::writeGridCompressed_(CGrid* grid)
731     {
732       if (grid->isScalarGrid() || grid->isWrittenCompressed(this->filename)) return;
733
734       try
735       {
736         CArray<bool,1> axisDomainOrder = grid->axis_domain_order;
737         std::vector<StdString> domainList = grid->getDomainList();
738         std::vector<StdString> axisList   = grid->getAxisList();
739         int numElement = axisDomainOrder.numElements(), idxDomain = 0, idxAxis = 0;
740
741         std::vector<StdString> dims;
742
743         if (grid->isCompressible())
744         {
745           StdString varId = grid->getId() + "_points";
746
747           int nbIndexes = (SuperClass::type == MULTI_FILE) ? grid->getNumberWrittenIndexes() : grid->getTotalNumberWrittenIndexes();
748           SuperClassWriter::addDimension(varId, nbIndexes);
749
750           dims.push_back(varId);
751           SuperClassWriter::addVariable(varId, NC_INT, dims);
752
753           StdOStringStream compress;
754           for (int i = numElement - 1; i >= 0; --i)
755           {
756             if (axisDomainOrder(i))
757             {
758               CDomain* domain = CDomain::get(domainList[domainList.size() - idxDomain - 1]);
759               StdString domId = domain->getDomainOutputName();
760               StdString appendDomId  = singleDomain ? "" : "_" + domId;
761
762               switch (domain->type)
763               {
764                 case CDomain::type_attr::curvilinear:
765                   compress << "y" << appendDomId << " x" << appendDomId;
766                   break;
767                 case CDomain::type_attr::rectilinear:
768                   compress << "lat" << appendDomId << " lon" << appendDomId;
769                   break;
770                 case CDomain::type_attr::unstructured:
771                   compress << "cell" << appendDomId;
772                   break;
773               }
774               ++idxDomain;
775             }
776             else
777             {
778               CAxis* axis = CAxis::get(axisList[axisList.size() - idxAxis - 1]);
779               compress << axis->getAxisOutputName();
780               ++idxAxis;
781             }
782
783             if (i != 0) compress << ' ';
784           }
785           SuperClassWriter::addAttribute("compress", compress.str(), &varId);
786
787           grid->computeCompressedIndex();
788
789           CArray<int, 1> indexes(grid->getNumberWrittenIndexes());
790           std::map<int, CArray<size_t, 1> >::const_iterator it;
791           for (it = grid->outIndexFromClient.begin(); it != grid->outIndexFromClient.end(); ++it)
792           {
793             const CArray<size_t, 1> compressedIndexes = grid->compressedOutIndexFromClient[it->first];
794             for (int i = 0; i < it->second.numElements(); i++)
795               indexes(compressedIndexes(i)) = it->second(i);
796           }
797
798           switch (SuperClass::type)
799           {
800             case (MULTI_FILE):
801             {
802               SuperClassWriter::writeData(indexes, varId, isCollective, 0);
803               break;
804             }
805             case (ONE_FILE):
806             {
807               if (grid->doGridHaveDataDistributed())
808                 grid->getDistributionServer()->computeGlobalIndex(indexes);
809
810               std::vector<StdSize> start, count;
811               start.push_back(grid->getOffsetWrittenIndexes());
812               count.push_back(grid->getNumberWrittenIndexes());
813
814               SuperClassWriter::writeData(indexes, varId, isCollective, 0, &start, &count);
815               break;
816             }
817           }
818         }
819         else
820         {
821           for (int i = 0; i < numElement; ++i)
822           {
823             StdString varId, compress;
824             CArray<int, 1> indexes;
825             bool isDistributed;
826             StdSize nbIndexes, totalNbIndexes, offset;
827             int firstGlobalIndex;
828
829             if (axisDomainOrder(i))
830             {
831               CDomain* domain = CDomain::get(domainList[idxDomain]);
832               StdString domId = domain->getDomainOutputName();
833
834               if (!domain->isCompressible()
835                    || domain->type == CDomain::type_attr::unstructured
836                    || domain->isWrittenCompressed(this->filename)
837                    || isWrittenCompressedDomain(domId))
838                 continue;
839
840               StdString appendDomId  = singleDomain ? "" : "_" + domId;
841
842               varId = domId + "_points";
843               switch (domain->type)
844               {
845                 case CDomain::type_attr::curvilinear:
846                   compress = "y" + appendDomId + " x" + appendDomId;
847                   break;
848                 case CDomain::type_attr::rectilinear:
849                   compress = "lat" + appendDomId + " lon" + appendDomId;
850                   break;
851               }
852
853               const std::vector<int>& indexesToWrite = domain->getIndexesToWrite();
854               indexes.resize(indexesToWrite.size());
855               for (int n = 0; n < indexes.numElements(); ++n)
856                 indexes(n) = indexesToWrite[n];
857
858               isDistributed = domain->isDistributed();
859               nbIndexes = domain->getNumberWrittenIndexes();
860               totalNbIndexes = domain->getTotalNumberWrittenIndexes();
861               offset = domain->getOffsetWrittenIndexes();
862               firstGlobalIndex = domain->ibegin + domain->jbegin * domain->ni_glo;
863
864               domain->addRelFileCompressed(this->filename);
865               setWrittenCompressedDomain(domId);
866               ++idxDomain;
867             }
868             else
869             {
870               CAxis* axis = CAxis::get(axisList[idxAxis]);
871               StdString axisId = axis->getAxisOutputName();
872
873               if (!axis->isCompressible()
874                    || axis->isWrittenCompressed(this->filename)
875                    || isWrittenCompressedAxis(axisId))
876                 continue;
877
878               varId = axisId + "_points";
879               compress = axisId;
880
881               const std::vector<int>& indexesToWrite = axis->getIndexesToWrite();
882               indexes.resize(indexesToWrite.size());
883               for (int n = 0; n < indexes.numElements(); ++n)
884                 indexes(n) = indexesToWrite[n];
885
886               isDistributed = axis->isDistributed();
887               nbIndexes = axis->getNumberWrittenIndexes();
888               totalNbIndexes = axis->getTotalNumberWrittenIndexes();
889               offset = axis->getOffsetWrittenIndexes();
890               firstGlobalIndex = axis->begin;
891
892               axis->addRelFileCompressed(this->filename);
893               setWrittenCompressedAxis(axisId);
894               ++idxAxis;
895             }
896
897             if (!varId.empty())
898             {
899               SuperClassWriter::addDimension(varId, (SuperClass::type == MULTI_FILE) ? nbIndexes : totalNbIndexes);
900
901               dims.clear();
902               dims.push_back(varId);
903               SuperClassWriter::addVariable(varId, NC_INT, dims);
904
905               SuperClassWriter::addAttribute("compress", compress, &varId);
906
907               switch (SuperClass::type)
908               {
909                 case (MULTI_FILE):
910                 {
911                   indexes -= firstGlobalIndex;
912                   SuperClassWriter::writeData(indexes, varId, isCollective, 0);
913                   break;
914                 }
915                 case (ONE_FILE):
916                 {
917                   std::vector<StdSize> start, count;
918                   start.push_back(offset);
919                   count.push_back(nbIndexes);
920
921                   SuperClassWriter::writeData(indexes, varId, isCollective, 0, &start, &count);
922                   break;
923                 }
924               }
925             }
926           }
927
928           if (!dims.empty())
929             grid->computeCompressedIndex();
930         }
931
932         grid->addRelFileCompressed(this->filename);
933       }
934       catch (CNetCdfException& e)
935       {
936         StdString msg("On writing compressed grid : ");
937         msg.append(grid->getId()); msg.append("\n");
938         msg.append("In the context : ");
939         CContext* context = CContext::getCurrent();
940         msg.append(context->getId()); msg.append("\n");
941         msg.append(e.what());
942         ERROR("CNc4DataOutput::writeGridCompressed_(CGrid* grid)", << msg);
943       }
944     }
945
946     //--------------------------------------------------------------
947
948     void CNc4DataOutput::writeTimeDimension_(void)
949     {
950       try
951       {
952        SuperClassWriter::addDimension("time_counter");
953       }
954       catch (CNetCdfException& e)
955       {
956         StdString msg("On writing time dimension : time_couter\n");
957         msg.append("In the context : ");
958         CContext* context = CContext::getCurrent() ;
959         msg.append(context->getId()); msg.append("\n");
960         msg.append(e.what());
961         ERROR("CNc4DataOutput::writeTimeDimension_(void)", << msg);
962       }
963     }
964
965      //--------------------------------------------------------------
966
967      void CNc4DataOutput::writeField_(CField* field)
968      {
969         CContext* context = CContext::getCurrent() ;
970         CContextServer* server=context->server ;
971
972         std::vector<StdString> dims, coodinates;
973         CGrid* grid = field->grid;
974         if (!grid->doGridHaveDataToWrite())
975          if (SuperClass::type==MULTI_FILE) return ;
976
977         CArray<bool,1> axisDomainOrder = grid->axis_domain_order;
978         int numElement = axisDomainOrder.numElements(), idxDomain = 0, idxAxis = 0;
979         std::vector<StdString> domainList = grid->getDomainList();
980         std::vector<StdString> axisList   = grid->getAxisList();
981
982         StdString timeid  = StdString("time_counter");
983         StdString dimXid,dimYid;
984         std::deque<StdString> dimIdList, dimCoordList;
985         bool hasArea = false;
986         StdString cellMeasures = "area:";
987         bool compressedOutput = !field->indexed_output.isEmpty() && field->indexed_output;
988
989         for (int i = 0; i < numElement; ++i)
990         {
991           if (axisDomainOrder(i))
992           {
993             CDomain* domain = CDomain::get(domainList[idxDomain]);
994             StdString domId = domain->getDomainOutputName();
995             StdString appendDomId  = singleDomain ? "" : "_" + domId ;
996
997             if (compressedOutput && domain->isCompressible() && domain->type != CDomain::type_attr::unstructured)
998             {
999               dimIdList.push_back(domId + "_points");
1000               field->setUseCompressedOutput();
1001             }
1002
1003             switch (domain->type)
1004             {
1005               case CDomain::type_attr::curvilinear:
1006                 if (!compressedOutput || !domain->isCompressible())
1007                 {
1008                   dimXid     = StdString("x").append(appendDomId);
1009                   dimIdList.push_back(dimXid);
1010                   dimYid     = StdString("y").append(appendDomId);
1011                   dimIdList.push_back(dimYid);
1012                 }
1013                 dimCoordList.push_back(StdString("nav_lon").append(appendDomId));
1014                 dimCoordList.push_back(StdString("nav_lat").append(appendDomId));
1015                 break ;
1016               case CDomain::type_attr::rectilinear:
1017                 if (!compressedOutput || !domain->isCompressible())
1018                 {
1019                   dimXid     = StdString("lon").append(appendDomId);
1020                   dimIdList.push_back(dimXid);
1021                   dimYid     = StdString("lat").append(appendDomId);
1022                   dimIdList.push_back(dimYid);
1023                 }
1024                 break ;
1025               case CDomain::type_attr::unstructured:
1026                 dimXid     = StdString("cell").append(appendDomId);
1027                 dimIdList.push_back(dimXid);
1028                 dimCoordList.push_back(StdString("lon").append(appendDomId));
1029                 dimCoordList.push_back(StdString("lat").append(appendDomId));
1030                 break ;
1031             }
1032             if (domain->hasArea)
1033             {
1034               hasArea = true;
1035               cellMeasures += " area" + appendDomId;
1036             }
1037             ++idxDomain;
1038           }
1039           else
1040           {
1041             CAxis* axis = CAxis::get(axisList[idxAxis]);
1042             StdString axisId = axis->getAxisOutputName();
1043
1044             if (compressedOutput && axis->isCompressible())
1045             {
1046               dimIdList.push_back(axisId + "_points");
1047               field->setUseCompressedOutput();
1048             }
1049             else
1050               dimIdList.push_back(axisId);
1051
1052             dimCoordList.push_back(axisId);
1053             ++idxAxis;
1054           }
1055         }
1056
1057/*
1058         StdString lonid_loc = (server->intraCommSize > 1)
1059                             ? StdString("lon").append(appendDomid).append("_local")
1060                             : lonid;
1061         StdString latid_loc = (server->intraCommSize > 1)
1062                             ? StdString("lat").append(appendDomid).append("_local")
1063                             : latid;
1064*/
1065         StdString fieldid = field->getFieldOutputName();
1066
1067//         unsigned int ssize = domain->zoom_ni_loc.getValue() * domain->zoom_nj_loc.getValue();
1068//         bool isCurvilinear = (domain->lonvalue.getValue()->size() == ssize);
1069//          bool isCurvilinear = domain->isCurvilinear ;
1070
1071         nc_type type ;
1072         if (field->prec.isEmpty()) type =  NC_FLOAT ;
1073         else
1074         {
1075           if (field->prec==2) type = NC_SHORT ;
1076           else if (field->prec==4)  type =  NC_FLOAT ;
1077           else if (field->prec==8)   type =  NC_DOUBLE ;
1078         }
1079
1080         bool wtime   = !(!field->operation.isEmpty() && field->getOperationTimeType() == func::CFunctor::once);
1081
1082         if (wtime)
1083         {
1084
1085            //StdOStringStream oss;
1086           // oss << "time_" << field->operation.getValue()
1087           //     << "_" << field->getRelFile()->output_freq.getValue();
1088          //oss
1089            if (field->getOperationTimeType() == func::CFunctor::instant) coodinates.push_back(string("time_instant"));
1090            else if (field->getOperationTimeType() == func::CFunctor::centered) coodinates.push_back(string("time_centered"));
1091            dims.push_back(timeid);
1092         }
1093
1094         if (compressedOutput && grid->isCompressible())
1095         {
1096           dims.push_back(grid->getId() + "_points");
1097           field->setUseCompressedOutput();
1098         }
1099         else
1100         {
1101           while (!dimIdList.empty())
1102           {
1103             dims.push_back(dimIdList.back());
1104             dimIdList.pop_back();
1105           }
1106         }
1107
1108         while (!dimCoordList.empty())
1109         {
1110           coodinates.push_back(dimCoordList.back());
1111           dimCoordList.pop_back();
1112         }
1113
1114         try
1115         {
1116           SuperClassWriter::addVariable(fieldid, type, dims);
1117
1118           if (!field->standard_name.isEmpty())
1119              SuperClassWriter::addAttribute
1120                 ("standard_name",  field->standard_name.getValue(), &fieldid);
1121
1122           if (!field->long_name.isEmpty())
1123              SuperClassWriter::addAttribute
1124                 ("long_name", field->long_name.getValue(), &fieldid);
1125
1126           if (!field->unit.isEmpty())
1127              SuperClassWriter::addAttribute
1128                 ("units", field->unit.getValue(), &fieldid);
1129
1130            if (!field->valid_min.isEmpty())
1131              SuperClassWriter::addAttribute
1132                 ("valid_min", field->valid_min.getValue(), &fieldid);
1133
1134           if (!field->valid_max.isEmpty())
1135              SuperClassWriter::addAttribute
1136                 ("valid_max", field->valid_max.getValue(), &fieldid);
1137
1138            if (!field->scale_factor.isEmpty())
1139              SuperClassWriter::addAttribute
1140                 ("scale_factor", field->scale_factor.getValue(), &fieldid);
1141
1142             if (!field->add_offset.isEmpty())
1143              SuperClassWriter::addAttribute
1144                 ("add_offset", field->add_offset.getValue(), &fieldid);
1145
1146           SuperClassWriter::addAttribute
1147                 ("online_operation", field->operation.getValue(), &fieldid);
1148
1149          // write child variables as attributes
1150
1151
1152           vector<CVariable*> listVars = field->getAllVariables() ;
1153           for (vector<CVariable*>::iterator it = listVars.begin() ;it != listVars.end(); it++) writeAttribute_(*it, fieldid) ;
1154
1155
1156           if (wtime)
1157           {
1158              CDuration freqOp = field->freq_op.getValue();
1159              freqOp.solveTimeStep(*context->calendar);
1160              StdString freqOpStr = freqOp.toStringUDUnits();
1161              SuperClassWriter::addAttribute("interval_operation", freqOpStr, &fieldid);
1162
1163              CDuration freqOut = field->getRelFile()->output_freq.getValue();
1164              freqOut.solveTimeStep(*context->calendar);
1165              SuperClassWriter::addAttribute("interval_write", freqOut.toStringUDUnits(), &fieldid);
1166
1167              StdString cellMethods = "time: ";
1168              if (field->operation.getValue() == "instant") cellMethods += "point";
1169              else if (field->operation.getValue() == "average") cellMethods += "mean";
1170              else if (field->operation.getValue() == "accumulate") cellMethods += "sum";
1171              else cellMethods += field->operation;
1172              if (freqOp.resolve(*context->calendar) != freqOut.resolve(*context->calendar))
1173                cellMethods += " (interval: " + freqOpStr + ")";
1174              SuperClassWriter::addAttribute("cell_methods", cellMethods, &fieldid);
1175           }
1176
1177           if (hasArea)
1178             SuperClassWriter::addAttribute("cell_measures", cellMeasures, &fieldid);
1179
1180           if (!field->default_value.isEmpty())
1181           {
1182              double default_value = field->default_value.getValue();
1183              float fdefault_value = (float)default_value;
1184              if (type == NC_DOUBLE)
1185                 SuperClassWriter::setDefaultValue(fieldid, &default_value);
1186              else
1187                 SuperClassWriter::setDefaultValue(fieldid, &fdefault_value);
1188           }
1189           else
1190              SuperClassWriter::setDefaultValue(fieldid, (double*)NULL);
1191
1192            if (field->compression_level.isEmpty())
1193              field->compression_level = field->file->compression_level.isEmpty() ? 0 : field->file->compression_level;
1194            SuperClassWriter::setCompressionLevel(fieldid, field->compression_level);
1195
1196           {  // Ecriture des coordonnées
1197
1198              StdString coordstr; //boost::algorithm::join(coodinates, " ")
1199              std::vector<StdString>::iterator
1200                 itc = coodinates.begin(), endc = coodinates.end();
1201
1202              for (; itc!= endc; itc++)
1203              {
1204                 StdString & coord = *itc;
1205                 if (itc+1 != endc)
1206                       coordstr.append(coord).append(" ");
1207                 else  coordstr.append(coord);
1208              }
1209
1210              SuperClassWriter::addAttribute("coordinates", coordstr, &fieldid);
1211
1212           }
1213         }
1214         catch (CNetCdfException& e)
1215         {
1216           StdString msg("On writing field : ");
1217           msg.append(fieldid); msg.append("\n");
1218           msg.append("In the context : ");
1219           msg.append(context->getId()); msg.append("\n");
1220           msg.append(e.what());
1221           ERROR("CNc4DataOutput::writeField_(CField* field)", << msg);
1222         }
1223      }
1224
1225      //--------------------------------------------------------------
1226
1227      void CNc4DataOutput::writeFile_ (CFile* file)
1228      {
1229         StdString filename = file->getFileOutputName();
1230         StdString description = (!file->description.isEmpty())
1231                               ? file->description.getValue()
1232                               : StdString("Created by xios");
1233
1234         singleDomain = (file->nbDomains == 1);
1235
1236         try
1237         {
1238           this->writeFileAttributes(filename, description,
1239                                     StdString("CF-1.5"),
1240                                     StdString("An IPSL model"),
1241                                     this->getTimeStamp());
1242
1243           if (!appendMode)
1244             SuperClassWriter::addDimension("axis_nbounds", 2);
1245         }
1246         catch (CNetCdfException& e)
1247         {
1248           StdString msg("On writing file : ");
1249           msg.append(filename); msg.append("\n");
1250           msg.append("In the context : ");
1251           CContext* context = CContext::getCurrent() ;
1252           msg.append(context->getId()); msg.append("\n");
1253           msg.append(e.what());
1254           ERROR("CNc4DataOutput::writeFile_ (CFile* file)", << msg);
1255         }
1256      }
1257
1258      void CNc4DataOutput::writeAttribute_ (CVariable* var, const string& fieldId)
1259      {
1260        StdString name = var->getVariableOutputName();
1261
1262        try
1263        {
1264          if (var->type.getValue() == CVariable::type_attr::t_int || var->type.getValue() == CVariable::type_attr::t_int32)
1265            addAttribute(name, var->getData<int>(), &fieldId);
1266          else if (var->type.getValue() == CVariable::type_attr::t_int16)
1267            addAttribute(name, var->getData<short int>(), &fieldId);
1268          else if (var->type.getValue() == CVariable::type_attr::t_float)
1269            addAttribute(name, var->getData<float>(), &fieldId);
1270          else if (var->type.getValue() == CVariable::type_attr::t_double)
1271            addAttribute(name, var->getData<double>(), &fieldId);
1272          else if (var->type.getValue() == CVariable::type_attr::t_string)
1273            addAttribute(name, var->getData<string>(), &fieldId);
1274          else
1275            ERROR("CNc4DataOutput::writeAttribute_ (CVariable* var, const string& fieldId)",
1276                  << "Unsupported variable of type " << var->type.getStringValue());
1277        }
1278       catch (CNetCdfException& e)
1279       {
1280         StdString msg("On writing attributes of variable with name : ");
1281         msg.append(name); msg.append("in the field "); msg.append(fieldId); msg.append("\n");
1282         msg.append("In the context : ");
1283         CContext* context = CContext::getCurrent() ;
1284         msg.append(context->getId()); msg.append("\n");
1285         msg.append(e.what());
1286         ERROR("CNc4DataOutput::writeAttribute_ (CVariable* var, const string& fieldId)", << msg);
1287       }
1288     }
1289
1290     void CNc4DataOutput::writeAttribute_ (CVariable* var)
1291     {
1292        StdString name = var->getVariableOutputName();
1293
1294        try
1295        {
1296          if (var->type.getValue() == CVariable::type_attr::t_int || var->type.getValue() == CVariable::type_attr::t_int32)
1297            addAttribute(name, var->getData<int>());
1298          else if (var->type.getValue() == CVariable::type_attr::t_int16)
1299            addAttribute(name, var->getData<short int>());
1300          else if (var->type.getValue() == CVariable::type_attr::t_float)
1301            addAttribute(name, var->getData<float>());
1302          else if (var->type.getValue() == CVariable::type_attr::t_double)
1303            addAttribute(name, var->getData<double>());
1304          else if (var->type.getValue() == CVariable::type_attr::t_string)
1305            addAttribute(name, var->getData<string>());
1306          else
1307            ERROR("CNc4DataOutput::writeAttribute_ (CVariable* var)",
1308                  << "Unsupported variable of type " << var->type.getStringValue());
1309        }
1310       catch (CNetCdfException& e)
1311       {
1312         StdString msg("On writing attributes of variable with name : ");
1313         msg.append(name); msg.append("\n");
1314         msg.append("In the context : ");
1315         CContext* context = CContext::getCurrent() ;
1316         msg.append(context->getId()); msg.append("\n");
1317         msg.append(e.what());
1318         ERROR("CNc4DataOutput::writeAttribute_ (CVariable* var)", << msg);
1319       }
1320     }
1321
1322      void CNc4DataOutput::syncFile_ (void)
1323      {
1324        try
1325        {
1326          SuperClassWriter::sync() ;
1327        }
1328        catch (CNetCdfException& e)
1329        {
1330         StdString msg("On synchronizing the write among processes");
1331         msg.append("In the context : ");
1332         CContext* context = CContext::getCurrent() ;
1333         msg.append(context->getId()); msg.append("\n");
1334         msg.append(e.what());
1335         ERROR("CNc4DataOutput::syncFile_ (void)", << msg);
1336        }
1337      }
1338
1339      void CNc4DataOutput::closeFile_ (void)
1340      {
1341        try
1342        {
1343          SuperClassWriter::close() ;
1344        }
1345        catch (CNetCdfException& e)
1346        {
1347         StdString msg("On closing file");
1348         msg.append("In the context : ");
1349         CContext* context = CContext::getCurrent() ;
1350         msg.append(context->getId()); msg.append("\n");
1351         msg.append(e.what());
1352         ERROR("CNc4DataOutput::syncFile_ (void)", << msg);
1353        }
1354
1355      }
1356
1357      //---------------------------------------------------------------
1358
1359      StdString CNc4DataOutput::getTimeStamp(void) const
1360      {
1361         const int buffer_size = 100;
1362         time_t rawtime;
1363         struct tm * timeinfo = NULL;
1364         char buffer [buffer_size];
1365
1366         time ( &rawtime );
1367         timeinfo = localtime ( &rawtime );
1368         strftime (buffer, buffer_size, "%Y-%b-%d %H:%M:%S %Z", timeinfo);
1369
1370         return (StdString(buffer));
1371      }
1372
1373      //---------------------------------------------------------------
1374
1375      void CNc4DataOutput::writeFieldData_ (CField*  field)
1376      {
1377        CContext* context = CContext::getCurrent();
1378        CContextServer* server = context->server;
1379        CGrid* grid = field->grid;
1380
1381        if (!grid->doGridHaveDataToWrite())
1382          if (SuperClass::type == MULTI_FILE || !isCollective) return;
1383
1384        StdString fieldid = field->getFieldOutputName();
1385
1386        StdOStringStream oss;
1387        string timeAxisId;
1388        if (field->getOperationTimeType() == func::CFunctor::instant) timeAxisId = "time_instant";
1389        else if (field->getOperationTimeType() == func::CFunctor::centered) timeAxisId = "time_centered";
1390
1391        StdString timeBoundId("time_counter_bounds");
1392
1393        StdString timeAxisBoundId;
1394        if (field->getOperationTimeType() == func::CFunctor::instant) timeAxisBoundId = "time_instant_bounds";
1395        else if (field->getOperationTimeType() == func::CFunctor::centered) timeAxisBoundId = "time_centered_bounds";
1396
1397        if (!field->wasWritten())
1398        {
1399          if (appendMode && field->file->record_offset.isEmpty())
1400          {
1401            field->resetNStep(getRecordFromTime(field->last_Write_srv) + 1);
1402          }
1403
1404          field->setWritten();
1405        }
1406
1407
1408        CArray<double,1> time_data(1);
1409        CArray<double,1> time_data_bound(2);
1410        CArray<double,1> time_counter(1);
1411        CArray<double,1> time_counter_bound(2);
1412
1413        bool wtime = (field->getOperationTimeType() != func::CFunctor::once);
1414
1415        if (wtime)
1416        {
1417          Time lastWrite = field->last_Write_srv;
1418          Time lastLastWrite = field->lastlast_Write_srv;
1419
1420          if (field->getOperationTimeType() == func::CFunctor::instant)
1421            time_data(0) = lastWrite;
1422          else if (field->getOperationTimeType() == func::CFunctor::centered)
1423            time_data(0) = (lastWrite + lastLastWrite) / 2;
1424
1425          if (field->getOperationTimeType() == func::CFunctor::instant)
1426            time_data_bound(0) = time_data_bound(1) = lastWrite;
1427          else if (field->getOperationTimeType() == func::CFunctor::centered)
1428          {
1429            time_data_bound(0) = lastLastWrite;
1430            time_data_bound(1) = lastWrite;
1431          }
1432
1433          if (field->file->time_counter == CFile::time_counter_attr::instant)
1434            time_counter(0) = lastWrite;
1435          else if (field->file->time_counter == CFile::time_counter_attr::centered)
1436            time_counter(0) = (lastWrite + lastLastWrite) / 2;
1437          else if (field->file->time_counter == CFile::time_counter_attr::record)
1438            time_counter(0) = field->getNStep() - 1;
1439
1440
1441          if (field->file->time_counter == CFile::time_counter_attr::instant)
1442            time_counter_bound(0) = time_counter_bound(1) = lastWrite;
1443          else if (field->file->time_counter == CFile::time_counter_attr::centered)
1444          {
1445            time_counter_bound(0) = lastLastWrite;
1446            time_counter_bound(1) = lastWrite;
1447          }
1448          else if (field->file->time_counter == CFile::time_counter_attr::record)
1449            time_counter_bound(0) = time_counter_bound(1) = field->getNStep() - 1;
1450        }
1451
1452         bool isRoot = (server->intraCommRank == 0);
1453
1454         if (!field->scale_factor.isEmpty() || !field->add_offset.isEmpty())
1455         {
1456           double scaleFactor = 1.0;
1457           double addOffset = 0.0;
1458           if (!field->scale_factor.isEmpty()) scaleFactor = field->scale_factor;
1459           if (!field->add_offset.isEmpty()) addOffset = field->add_offset;
1460           field->scaleFactorAddOffset(scaleFactor, addOffset);
1461         }
1462
1463         try
1464         {
1465           size_t writtenSize;
1466           if (field->getUseCompressedOutput())
1467             writtenSize = grid->getNumberWrittenIndexes();
1468           else
1469             writtenSize = grid->getWrittenDataSize();
1470
1471           CArray<double,1> fieldData(writtenSize);
1472           if (!field->default_value.isEmpty()) fieldData = field->default_value;
1473
1474           if (field->getUseCompressedOutput())
1475             field->outputCompressedField(fieldData);
1476           else
1477             field->outputField(fieldData);
1478
1479           if (!field->prec.isEmpty() && field->prec == 2) fieldData = round(fieldData);
1480
1481           switch (SuperClass::type)
1482           {
1483              case (MULTI_FILE) :
1484              {
1485                 SuperClassWriter::writeData(fieldData, fieldid, isCollective, field->getNStep() - 1);
1486                 if (wtime)
1487                 {
1488                   SuperClassWriter::writeData(time_data, timeAxisId, isCollective, field->getNStep() - 1);
1489                   SuperClassWriter::writeData(time_data_bound, timeAxisBoundId, isCollective, field->getNStep() - 1);
1490                   if (field->file->time_counter != CFile::time_counter_attr::none)
1491                   {
1492                     SuperClassWriter::writeData(time_counter, string("time_counter"), isCollective, field->getNStep() - 1);
1493                     if (field->file->time_counter != CFile::time_counter_attr::record)
1494                       SuperClassWriter::writeData(time_counter_bound, timeBoundId, isCollective, field->getNStep() - 1);
1495                   }
1496                 }
1497                 break;
1498              }
1499              case (ONE_FILE) :
1500              {
1501                const std::vector<int>& nZoomBeginGlobal = grid->getDistributionServer()->getZoomBeginGlobal();
1502                const std::vector<int>& nZoomBeginServer = grid->getDistributionServer()->getZoomBeginServer();
1503                const std::vector<int>& nZoomSizeServer  = grid->getDistributionServer()->getZoomSizeServer();
1504
1505                std::vector<StdSize> start, count;
1506
1507                if (field->getUseCompressedOutput())
1508                {
1509                  if (grid->isCompressible())
1510                  {
1511                    start.push_back(grid->getOffsetWrittenIndexes());
1512                    count.push_back(grid->getNumberWrittenIndexes());
1513                  }
1514                  else
1515                  {
1516                    CArray<bool,1> axisDomainOrder = grid->axis_domain_order;
1517                    std::vector<StdString> domainList = grid->getDomainList();
1518                    std::vector<StdString> axisList   = grid->getAxisList();
1519                    int numElement = axisDomainOrder.numElements();
1520                    int idxDomain = domainList.size() - 1, idxAxis = axisList.size() - 1;
1521                    int idx = nZoomBeginGlobal.size() - 1;
1522
1523                    start.reserve(nZoomBeginGlobal.size());
1524                    count.reserve(nZoomBeginGlobal.size());
1525
1526
1527                    for (int i = numElement - 1; i >= 0; --i)
1528                    {
1529                      if (axisDomainOrder(i))
1530                      {
1531                        CDomain* domain = CDomain::get(domainList[idxDomain]);
1532
1533                        if (domain->isCompressible())
1534                        {
1535                          start.push_back(domain->getOffsetWrittenIndexes());
1536                          count.push_back(domain->getNumberWrittenIndexes());
1537                          idx -= 2;
1538                        }
1539                        else
1540                        {
1541                          if ((domain->type) != CDomain::type_attr::unstructured)
1542                          {
1543                            start.push_back(nZoomBeginServer[idx] - nZoomBeginGlobal[idx]);
1544                            count.push_back(nZoomSizeServer[idx]);
1545                          }
1546                          --idx;
1547                          start.push_back(nZoomBeginServer[idx] - nZoomBeginGlobal[idx]);
1548                          count.push_back(nZoomSizeServer[idx]);
1549                          --idx;
1550                        }
1551                        --idxDomain;
1552                      }
1553                      else
1554                      {
1555                        CAxis* axis = CAxis::get(axisList[idxAxis]);
1556
1557                        if (axis->isCompressible())
1558                        {
1559                          start.push_back(axis->getOffsetWrittenIndexes());
1560                          count.push_back(axis->getNumberWrittenIndexes());
1561                        }
1562                        else
1563                        {
1564                          start.push_back(nZoomBeginServer[idx] - nZoomBeginGlobal[idx]);
1565                          count.push_back(nZoomSizeServer[idx]);
1566                        }
1567
1568                        --idxAxis;
1569                        --idx;
1570                      }
1571                    }
1572                  }
1573                }
1574                else
1575                {
1576
1577                  CArray<bool,1> axisDomainOrder = grid->axis_domain_order;
1578                  std::vector<StdString> domainList = grid->getDomainList();
1579                  std::vector<StdString> axisList   = grid->getAxisList();
1580                  int numElement = axisDomainOrder.numElements();
1581                  int idxDomain = domainList.size() - 1, idxAxis = axisList.size() - 1;
1582                  int idx = nZoomBeginGlobal.size() - 1;
1583
1584                  start.reserve(nZoomBeginGlobal.size());
1585                  count.reserve(nZoomBeginGlobal.size());
1586
1587                  for (int i = numElement - 1; i >= 0; --i)
1588                  {
1589                    if (axisDomainOrder(i))
1590                    {
1591                      CDomain* domain = CDomain::get(domainList[idxDomain]);
1592                      if ((domain->type) != CDomain::type_attr::unstructured)
1593                      {
1594                        start.push_back(nZoomBeginServer[idx] - nZoomBeginGlobal[idx]);
1595                        count.push_back(nZoomSizeServer[idx]);
1596                      }
1597                      --idx ;
1598                      start.push_back(nZoomBeginServer[idx] - nZoomBeginGlobal[idx]);
1599                      count.push_back(nZoomSizeServer[idx]);
1600                      --idx ;
1601                      --idxDomain;
1602                    }
1603                    else
1604                    {
1605                      start.push_back(nZoomBeginServer[idx] - nZoomBeginGlobal[idx]);
1606                      count.push_back(nZoomSizeServer[idx]);
1607                      --idx;
1608                     }
1609                  }
1610                }
1611
1612                SuperClassWriter::writeData(fieldData, fieldid, isCollective, field->getNStep() - 1, &start, &count);
1613                if (wtime)
1614                {
1615                   SuperClassWriter::writeTimeAxisData(time_data, timeAxisId, isCollective, field->getNStep() - 1, isRoot);
1616                   SuperClassWriter::writeTimeAxisData(time_data_bound, timeAxisBoundId, isCollective, field->getNStep() - 1, isRoot);
1617                   if (field->file->time_counter != CFile::time_counter_attr::none)
1618                   {
1619                     SuperClassWriter::writeTimeAxisData(time_counter, string("time_counter"), isCollective, field->getNStep() - 1, isRoot);
1620                     if (field->file->time_counter != CFile::time_counter_attr::record)
1621                       SuperClassWriter::writeTimeAxisData(time_counter_bound, timeBoundId, isCollective, field->getNStep() - 1, isRoot);
1622                   }
1623                }
1624
1625                break;
1626              }
1627            }
1628         }
1629         catch (CNetCdfException& e)
1630         {
1631           StdString msg("On writing field data: ");
1632           msg.append(fieldid); msg.append("\n");
1633           msg.append("In the context : ");
1634           msg.append(context->getId()); msg.append("\n");
1635           msg.append(e.what());
1636           ERROR("CNc4DataOutput::writeFieldData_ (CField*  field)", << msg);
1637         }
1638      }
1639
1640      //---------------------------------------------------------------
1641
1642      void CNc4DataOutput::writeTimeAxis_
1643                  (CField*    field,
1644                   const boost::shared_ptr<CCalendar> cal)
1645      {
1646         StdOStringStream oss;
1647
1648         if (field->getOperationTimeType() == func::CFunctor::once) return ;
1649
1650//         oss << "time_" << field->operation.getValue()
1651//             << "_" << field->getRelFile()->output_freq.getValue();
1652
1653//         StdString axisid = oss.str();
1654//         if (field->getOperationTimeType() == func::CFunctor::centered) axisid="time_centered" ;
1655//         else if (field->getOperationTimeType() == func::CFunctor::instant) axisid="time_instant" ;
1656
1657         StdString axisid("time_centered") ;
1658         StdString axisBoundId("time_centered_bounds");
1659         StdString timeid("time_counter");
1660         StdString timeBoundId("axis_nbounds");
1661
1662         if (field->getOperationTimeType() == func::CFunctor::instant)
1663         {
1664            axisid = "time_instant";
1665            axisBoundId = "time_instant_bounds";
1666         }
1667
1668         try
1669         {
1670          // Adding time_instant or time_centered
1671           std::vector<StdString> dims;
1672           dims.push_back(timeid);
1673           if (!SuperClassWriter::varExist(axisid))
1674           {
1675              SuperClassWriter::addVariable(axisid, NC_DOUBLE, dims);
1676
1677              CDate timeOrigin=cal->getTimeOrigin() ;
1678              StdOStringStream oss2;
1679  //            oss2<<initDate.getYear()<<"-"<<initDate.getMonth()<<"-"<<initDate.getDay()<<" "
1680  //                <<initDate.getHour()<<"-"<<initDate.getMinute()<<"-"<<initDate.getSecond() ;
1681              StdString strInitdate=oss2.str() ;
1682              StdString strTimeOrigin=timeOrigin.toString() ;
1683              this->writeTimeAxisAttributes
1684                 (axisid, cal->getType(),
1685                  StdString("seconds since ").append(strTimeOrigin),
1686                  strTimeOrigin, axisBoundId);
1687           }
1688
1689           // Adding time_instant_bounds or time_centered_bounds variables
1690           if (!SuperClassWriter::varExist(axisBoundId))
1691           {
1692              dims.clear() ;
1693              dims.push_back(timeid);
1694              dims.push_back(timeBoundId);
1695              SuperClassWriter::addVariable(axisBoundId, NC_DOUBLE, dims);
1696           }
1697
1698           if (field->file->time_counter != CFile::time_counter_attr::none)
1699           {
1700             // Adding time_counter
1701             axisid = "time_counter";
1702             axisBoundId = "time_counter_bounds";
1703             dims.clear();
1704             dims.push_back(timeid);
1705             if (!SuperClassWriter::varExist(axisid))
1706             {
1707                SuperClassWriter::addVariable(axisid, NC_DOUBLE, dims);
1708                SuperClassWriter::addAttribute("axis", string("T"), &axisid);
1709
1710                if (field->file->time_counter != CFile::time_counter_attr::record)
1711                {
1712                  CDate timeOrigin = cal->getTimeOrigin();
1713                  StdString strTimeOrigin = timeOrigin.toString();
1714
1715                  this->writeTimeAxisAttributes(axisid, cal->getType(),
1716                                                StdString("seconds since ").append(strTimeOrigin),
1717                                                strTimeOrigin, axisBoundId);
1718                }
1719             }
1720
1721             // Adding time_counter_bound dimension
1722             if (field->file->time_counter != CFile::time_counter_attr::record)
1723             {
1724                if (!SuperClassWriter::varExist(axisBoundId))
1725                {
1726                  dims.clear();
1727                  dims.push_back(timeid);
1728                  dims.push_back(timeBoundId);
1729                  SuperClassWriter::addVariable(axisBoundId, NC_DOUBLE, dims);
1730                }
1731             }
1732           }
1733         }
1734         catch (CNetCdfException& e)
1735         {
1736           StdString msg("On writing time axis data: ");
1737           msg.append("In the context : ");
1738           CContext* context = CContext::getCurrent() ;
1739           msg.append(context->getId()); msg.append("\n");
1740           msg.append(e.what());
1741           ERROR("CNc4DataOutput::writeTimeAxis_ (CField*    field, \
1742                  const boost::shared_ptr<CCalendar> cal)", << msg);
1743         }
1744      }
1745
1746      //---------------------------------------------------------------
1747
1748      void CNc4DataOutput::writeTimeAxisAttributes(const StdString & axis_name,
1749                                                   const StdString & calendar,
1750                                                   const StdString & units,
1751                                                   const StdString & time_origin,
1752                                                   const StdString & time_bounds,
1753                                                   const StdString & standard_name,
1754                                                   const StdString & long_name)
1755      {
1756         try
1757         {
1758           SuperClassWriter::addAttribute("standard_name", standard_name, &axis_name);
1759           SuperClassWriter::addAttribute("long_name",     long_name    , &axis_name);
1760           SuperClassWriter::addAttribute("calendar",      calendar     , &axis_name);
1761           SuperClassWriter::addAttribute("units",         units        , &axis_name);
1762           SuperClassWriter::addAttribute("time_origin",   time_origin  , &axis_name);
1763           SuperClassWriter::addAttribute("bounds",        time_bounds  , &axis_name);
1764         }
1765         catch (CNetCdfException& e)
1766         {
1767           StdString msg("On writing time axis Attribute: ");
1768           msg.append("In the context : ");
1769           CContext* context = CContext::getCurrent() ;
1770           msg.append(context->getId()); msg.append("\n");
1771           msg.append(e.what());
1772           ERROR("CNc4DataOutput::writeTimeAxisAttributes(const StdString & axis_name, \
1773                                                          const StdString & calendar,\
1774                                                          const StdString & units, \
1775                                                          const StdString & time_origin, \
1776                                                          const StdString & time_bounds, \
1777                                                          const StdString & standard_name, \
1778                                                          const StdString & long_name)", << msg);
1779         }
1780      }
1781
1782      //---------------------------------------------------------------
1783
1784      void CNc4DataOutput::writeAxisAttributes(const StdString & axis_name,
1785                                               const StdString & axis,
1786                                               const StdString & standard_name,
1787                                               const StdString & long_name,
1788                                               const StdString & units,
1789                                               const StdString & nav_model)
1790      {
1791         try
1792         {
1793          if (!axis.empty())
1794            SuperClassWriter::addAttribute("axis"       , axis         , &axis_name);
1795
1796          SuperClassWriter::addAttribute("standard_name", standard_name, &axis_name);
1797          SuperClassWriter::addAttribute("long_name"    , long_name    , &axis_name);
1798          SuperClassWriter::addAttribute("units"        , units        , &axis_name);
1799          SuperClassWriter::addAttribute("nav_model"    , nav_model    , &axis_name);
1800         }
1801         catch (CNetCdfException& e)
1802         {
1803           StdString msg("On writing Axis Attribute: ");
1804           msg.append("In the context : ");
1805           CContext* context = CContext::getCurrent() ;
1806           msg.append(context->getId()); msg.append("\n");
1807           msg.append(e.what());
1808           ERROR("CNc4DataOutput::writeAxisAttributes(const StdString & axis_name, \
1809                                                      const StdString & axis, \
1810                                                      const StdString & standard_name, \
1811                                                      const StdString & long_name, \
1812                                                      const StdString & units, \
1813                                                      const StdString & nav_model)", << msg);
1814         }
1815      }
1816
1817      //---------------------------------------------------------------
1818
1819      void CNc4DataOutput::writeLocalAttributes
1820         (int ibegin, int ni, int jbegin, int nj, StdString domid)
1821      {
1822        try
1823        {
1824         SuperClassWriter::addAttribute(StdString("ibegin").append(domid), ibegin);
1825         SuperClassWriter::addAttribute(StdString("ni"    ).append(domid), ni);
1826         SuperClassWriter::addAttribute(StdString("jbegin").append(domid), jbegin);
1827         SuperClassWriter::addAttribute(StdString("nj"    ).append(domid), nj);
1828        }
1829        catch (CNetCdfException& e)
1830        {
1831           StdString msg("On writing Local Attributes: ");
1832           msg.append("In the context : ");
1833           CContext* context = CContext::getCurrent() ;
1834           msg.append(context->getId()); msg.append("\n");
1835           msg.append(e.what());
1836           ERROR("CNc4DataOutput::writeLocalAttributes \
1837                  (int ibegin, int ni, int jbegin, int nj, StdString domid)", << msg);
1838        }
1839
1840      }
1841
1842      void CNc4DataOutput::writeLocalAttributes_IOIPSL(const StdString& dimXid, const StdString& dimYid,
1843                                                       int ibegin, int ni, int jbegin, int nj, int ni_glo, int nj_glo, int rank, int size)
1844      {
1845         CArray<int,1> array(2) ;
1846
1847         try
1848         {
1849           SuperClassWriter::addAttribute("DOMAIN_number_total",size ) ;
1850           SuperClassWriter::addAttribute("DOMAIN_number", rank) ;
1851           array = SuperClassWriter::getDimension(dimXid) + 1, SuperClassWriter::getDimension(dimYid) + 1;
1852           SuperClassWriter::addAttribute("DOMAIN_dimensions_ids",array) ;
1853           array=ni_glo,nj_glo ;
1854           SuperClassWriter::addAttribute("DOMAIN_size_global", array) ;
1855           array=ni,nj ;
1856           SuperClassWriter::addAttribute("DOMAIN_size_local", array) ;
1857           array=ibegin,jbegin ;
1858           SuperClassWriter::addAttribute("DOMAIN_position_first", array) ;
1859           array=ibegin+ni-1,jbegin+nj-1 ;
1860           SuperClassWriter::addAttribute("DOMAIN_position_last",array) ;
1861           array=0,0 ;
1862           SuperClassWriter::addAttribute("DOMAIN_halo_size_start", array) ;
1863           SuperClassWriter::addAttribute("DOMAIN_halo_size_end", array);
1864           SuperClassWriter::addAttribute("DOMAIN_type",string("box")) ;
1865  /*
1866           SuperClassWriter::addAttribute("DOMAIN_DIM_N001",string("x")) ;
1867           SuperClassWriter::addAttribute("DOMAIN_DIM_N002",string("y")) ;
1868           SuperClassWriter::addAttribute("DOMAIN_DIM_N003",string("axis_A")) ;
1869           SuperClassWriter::addAttribute("DOMAIN_DIM_N004",string("time_counter")) ;
1870  */
1871         }
1872         catch (CNetCdfException& e)
1873         {
1874           StdString msg("On writing Local Attributes IOIPSL \n");
1875           msg.append("In the context : ");
1876           CContext* context = CContext::getCurrent() ;
1877           msg.append(context->getId()); msg.append("\n");
1878           msg.append(e.what());
1879           ERROR("CNc4DataOutput::writeLocalAttributes_IOIPSL \
1880                  (int ibegin, int ni, int jbegin, int nj, int ni_glo, int nj_glo, int rank, int size)", << msg);
1881         }
1882      }
1883      //---------------------------------------------------------------
1884
1885      void CNc4DataOutput:: writeFileAttributes(const StdString & name,
1886                                                const StdString & description,
1887                                                const StdString & conventions,
1888                                                const StdString & production,
1889                                                const StdString & timeStamp)
1890      {
1891         try
1892         {
1893           SuperClassWriter::addAttribute("name"       , name);
1894           SuperClassWriter::addAttribute("description", description);
1895           SuperClassWriter::addAttribute("title"      , description);
1896           SuperClassWriter::addAttribute("Conventions", conventions);
1897           SuperClassWriter::addAttribute("production" , production);
1898           SuperClassWriter::addAttribute("timeStamp"  , timeStamp);
1899         }
1900         catch (CNetCdfException& e)
1901         {
1902           StdString msg("On writing File Attributes \n ");
1903           msg.append("In the context : ");
1904           CContext* context = CContext::getCurrent() ;
1905           msg.append(context->getId()); msg.append("\n");
1906           msg.append(e.what());
1907           ERROR("CNc4DataOutput:: writeFileAttributes(const StdString & name, \
1908                                                const StdString & description, \
1909                                                const StdString & conventions, \
1910                                                const StdString & production, \
1911                                                const StdString & timeStamp)", << msg);
1912         }
1913      }
1914
1915      //---------------------------------------------------------------
1916
1917      void CNc4DataOutput::writeMaskAttributes(const StdString & mask_name,
1918                                               int data_dim,
1919                                               int data_ni,
1920                                               int data_nj,
1921                                               int data_ibegin,
1922                                               int data_jbegin)
1923      {
1924         try
1925         {
1926           SuperClassWriter::addAttribute("data_dim"   , data_dim   , &mask_name);
1927           SuperClassWriter::addAttribute("data_ni"    , data_ni    , &mask_name);
1928           SuperClassWriter::addAttribute("data_nj"    , data_nj    , &mask_name);
1929           SuperClassWriter::addAttribute("data_ibegin", data_ibegin, &mask_name);
1930           SuperClassWriter::addAttribute("data_jbegin", data_jbegin, &mask_name);
1931         }
1932         catch (CNetCdfException& e)
1933         {
1934           StdString msg("On writing Mask Attributes \n ");
1935           msg.append("In the context : ");
1936           CContext* context = CContext::getCurrent() ;
1937           msg.append(context->getId()); msg.append("\n");
1938           msg.append(e.what());
1939           ERROR("CNc4DataOutput::writeMaskAttributes(const StdString & mask_name, \
1940                                               int data_dim, \
1941                                               int data_ni, \
1942                                               int data_nj, \
1943                                               int data_ibegin, \
1944                                               int data_jbegin)", << msg);
1945         }
1946      }
1947
1948      ///--------------------------------------------------------------
1949
1950      StdSize CNc4DataOutput::getRecordFromTime(Time time)
1951      {
1952        std::map<Time, StdSize>::const_iterator it = timeToRecordCache.find(time);
1953        if (it == timeToRecordCache.end())
1954        {
1955          StdString timeAxisBoundsId("time_counter_bounds");
1956          if (!SuperClassWriter::varExist(timeAxisBoundsId))
1957            timeAxisBoundsId = "time_instant_bounds";
1958
1959          CArray<double,2> timeAxisBounds;
1960          SuperClassWriter::getTimeAxisBounds(timeAxisBounds, timeAxisBoundsId, isCollective);
1961
1962          StdSize record = 0;
1963          double dtime(time);
1964          for (int n = timeAxisBounds.extent(1) - 1; n >= 0; n--)
1965          {
1966            if (timeAxisBounds(1, n) < dtime)
1967            {
1968              record = n + 1;
1969              break;
1970            }
1971          }
1972          it = timeToRecordCache.insert(std::make_pair(time, record)).first;
1973        }
1974        return it->second;
1975      }
1976
1977      ///--------------------------------------------------------------
1978
1979      bool CNc4DataOutput::isWrittenDomain(const std::string& domainName) const
1980      {
1981        return (this->writtenDomains.find(domainName) != this->writtenDomains.end());
1982      }
1983
1984      bool CNc4DataOutput::isWrittenCompressedDomain(const std::string& domainName) const
1985      {
1986        return (this->writtenCompressedDomains.find(domainName) != this->writtenCompressedDomains.end());
1987      }
1988
1989      bool CNc4DataOutput::isWrittenAxis(const std::string& axisName) const
1990      {
1991        return (this->writtenAxis.find(axisName) != this->writtenAxis.end());
1992      }
1993
1994      bool CNc4DataOutput::isWrittenCompressedAxis(const std::string& axisName) const
1995      {
1996        return (this->writtenCompressedAxis.find(axisName) != this->writtenCompressedAxis.end());
1997      }
1998
1999      void CNc4DataOutput::setWrittenDomain(const std::string& domainName)
2000      {
2001        this->writtenDomains.insert(domainName);
2002      }
2003
2004      void CNc4DataOutput::setWrittenCompressedDomain(const std::string& domainName)
2005      {
2006        this->writtenCompressedDomains.insert(domainName);
2007      }
2008
2009      void CNc4DataOutput::setWrittenAxis(const std::string& axisName)
2010      {
2011        this->writtenAxis.insert(axisName);
2012      }
2013
2014      void CNc4DataOutput::setWrittenCompressedAxis(const std::string& axisName)
2015      {
2016        this->writtenCompressedAxis.insert(axisName);
2017      }
2018} // namespace xios
Note: See TracBrowser for help on using the repository browser.