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

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

Axis/Domain?: Use the new infrastructure to get the output name.

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