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

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

File/Variable?: Add an helper function 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.1 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->getFileOutputName();
1223         StdString description = (!file->description.isEmpty())
1224                               ? file->description.getValue()
1225                               : StdString("Created by xios");
1226
1227         singleDomain = (file->nbDomains == 1);
1228
1229         try
1230         {
1231           this->writeFileAttributes(filename, description,
1232                                     StdString("CF-1.5"),
1233                                     StdString("An IPSL model"),
1234                                     this->getTimeStamp());
1235
1236           if (!appendMode)
1237             SuperClassWriter::addDimension("axis_nbounds", 2);
1238         }
1239         catch (CNetCdfException& e)
1240         {
1241           StdString msg("On writing file : ");
1242           msg.append(filename); msg.append("\n");
1243           msg.append("In the context : ");
1244           CContext* context = CContext::getCurrent() ;
1245           msg.append(context->getId()); msg.append("\n");
1246           msg.append(e.what());
1247           ERROR("CNc4DataOutput::writeFile_ (CFile* file)", << msg);
1248         }
1249      }
1250
1251      void CNc4DataOutput::writeAttribute_ (CVariable* var, const string& fieldId)
1252      {
1253        StdString name = var->getVariableOutputName();
1254
1255        try
1256        {
1257          if (var->type.getValue() == CVariable::type_attr::t_int || var->type.getValue() == CVariable::type_attr::t_int32)
1258            addAttribute(name, var->getData<int>(), &fieldId);
1259          else if (var->type.getValue() == CVariable::type_attr::t_int16)
1260            addAttribute(name, var->getData<short int>(), &fieldId);
1261          else if (var->type.getValue() == CVariable::type_attr::t_float)
1262            addAttribute(name, var->getData<float>(), &fieldId);
1263          else if (var->type.getValue() == CVariable::type_attr::t_double)
1264            addAttribute(name, var->getData<double>(), &fieldId);
1265          else if (var->type.getValue() == CVariable::type_attr::t_string)
1266            addAttribute(name, var->getData<string>(), &fieldId);
1267          else
1268            ERROR("CNc4DataOutput::writeAttribute_ (CVariable* var, const string& fieldId)",
1269                  << "Unsupported variable of type " << var->type.getStringValue());
1270        }
1271       catch (CNetCdfException& e)
1272       {
1273         StdString msg("On writing attributes of variable with name : ");
1274         msg.append(name); msg.append("in the field "); msg.append(fieldId); msg.append("\n");
1275         msg.append("In the context : ");
1276         CContext* context = CContext::getCurrent() ;
1277         msg.append(context->getId()); msg.append("\n");
1278         msg.append(e.what());
1279         ERROR("CNc4DataOutput::writeAttribute_ (CVariable* var, const string& fieldId)", << msg);
1280       }
1281     }
1282
1283     void CNc4DataOutput::writeAttribute_ (CVariable* var)
1284     {
1285        StdString name = var->getVariableOutputName();
1286
1287        try
1288        {
1289          if (var->type.getValue() == CVariable::type_attr::t_int || var->type.getValue() == CVariable::type_attr::t_int32)
1290            addAttribute(name, var->getData<int>());
1291          else if (var->type.getValue() == CVariable::type_attr::t_int16)
1292            addAttribute(name, var->getData<short int>());
1293          else if (var->type.getValue() == CVariable::type_attr::t_float)
1294            addAttribute(name, var->getData<float>());
1295          else if (var->type.getValue() == CVariable::type_attr::t_double)
1296            addAttribute(name, var->getData<double>());
1297          else if (var->type.getValue() == CVariable::type_attr::t_string)
1298            addAttribute(name, var->getData<string>());
1299          else
1300            ERROR("CNc4DataOutput::writeAttribute_ (CVariable* var)",
1301                  << "Unsupported variable of type " << var->type.getStringValue());
1302        }
1303       catch (CNetCdfException& e)
1304       {
1305         StdString msg("On writing attributes of variable with name : ");
1306         msg.append(name); msg.append("\n");
1307         msg.append("In the context : ");
1308         CContext* context = CContext::getCurrent() ;
1309         msg.append(context->getId()); msg.append("\n");
1310         msg.append(e.what());
1311         ERROR("CNc4DataOutput::writeAttribute_ (CVariable* var)", << msg);
1312       }
1313     }
1314
1315      void CNc4DataOutput::syncFile_ (void)
1316      {
1317        try
1318        {
1319          SuperClassWriter::sync() ;
1320        }
1321        catch (CNetCdfException& e)
1322        {
1323         StdString msg("On synchronizing the write among processes");
1324         msg.append("In the context : ");
1325         CContext* context = CContext::getCurrent() ;
1326         msg.append(context->getId()); msg.append("\n");
1327         msg.append(e.what());
1328         ERROR("CNc4DataOutput::syncFile_ (void)", << msg);
1329        }
1330      }
1331
1332      void CNc4DataOutput::closeFile_ (void)
1333      {
1334        try
1335        {
1336          SuperClassWriter::close() ;
1337        }
1338        catch (CNetCdfException& e)
1339        {
1340         StdString msg("On closing file");
1341         msg.append("In the context : ");
1342         CContext* context = CContext::getCurrent() ;
1343         msg.append(context->getId()); msg.append("\n");
1344         msg.append(e.what());
1345         ERROR("CNc4DataOutput::syncFile_ (void)", << msg);
1346        }
1347
1348      }
1349
1350      //---------------------------------------------------------------
1351
1352      StdString CNc4DataOutput::getTimeStamp(void) const
1353      {
1354         const int buffer_size = 100;
1355         time_t rawtime;
1356         struct tm * timeinfo = NULL;
1357         char buffer [buffer_size];
1358
1359         time ( &rawtime );
1360         timeinfo = localtime ( &rawtime );
1361         strftime (buffer, buffer_size, "%Y-%b-%d %H:%M:%S %Z", timeinfo);
1362
1363         return (StdString(buffer));
1364      }
1365
1366      //---------------------------------------------------------------
1367
1368      void CNc4DataOutput::writeFieldData_ (CField*  field)
1369      {
1370        CContext* context = CContext::getCurrent();
1371        CContextServer* server = context->server;
1372        CGrid* grid = field->grid;
1373
1374        if (!grid->doGridHaveDataToWrite())
1375          if (SuperClass::type == MULTI_FILE || !isCollective) return;
1376
1377        StdString fieldid = field->getFieldOutputName();
1378
1379        StdOStringStream oss;
1380        string timeAxisId;
1381        if (field->getOperationTimeType() == func::CFunctor::instant) timeAxisId = "time_instant";
1382        else if (field->getOperationTimeType() == func::CFunctor::centered) timeAxisId = "time_centered";
1383
1384        StdString timeBoundId("time_counter_bounds");
1385
1386        StdString timeAxisBoundId;
1387        if (field->getOperationTimeType() == func::CFunctor::instant) timeAxisBoundId = "time_instant_bounds";
1388        else if (field->getOperationTimeType() == func::CFunctor::centered) timeAxisBoundId = "time_centered_bounds";
1389
1390        if (!field->wasWritten())
1391        {
1392          if (appendMode && field->file->record_offset.isEmpty())
1393          {
1394            field->resetNStep(getRecordFromTime(field->last_Write_srv) + 1);
1395          }
1396
1397          field->setWritten();
1398        }
1399
1400
1401        CArray<double,1> time_data(1);
1402        CArray<double,1> time_data_bound(2);
1403        CArray<double,1> time_counter(1);
1404        CArray<double,1> time_counter_bound(2);
1405
1406        bool wtime = (field->getOperationTimeType() != func::CFunctor::once);
1407
1408        if (wtime)
1409        {
1410          Time lastWrite = field->last_Write_srv;
1411          Time lastLastWrite = field->lastlast_Write_srv;
1412
1413          if (field->getOperationTimeType() == func::CFunctor::instant)
1414            time_data(0) = lastWrite;
1415          else if (field->getOperationTimeType() == func::CFunctor::centered)
1416            time_data(0) = (lastWrite + lastLastWrite) / 2;
1417
1418          if (field->getOperationTimeType() == func::CFunctor::instant)
1419            time_data_bound(0) = time_data_bound(1) = lastWrite;
1420          else if (field->getOperationTimeType() == func::CFunctor::centered)
1421          {
1422            time_data_bound(0) = lastLastWrite;
1423            time_data_bound(1) = lastWrite;
1424          }
1425
1426          if (field->file->time_counter == CFile::time_counter_attr::instant)
1427            time_counter(0) = lastWrite;
1428          else if (field->file->time_counter == CFile::time_counter_attr::centered)
1429            time_counter(0) = (lastWrite + lastLastWrite) / 2;
1430          else if (field->file->time_counter == CFile::time_counter_attr::record)
1431            time_counter(0) = field->getNStep() - 1;
1432
1433
1434          if (field->file->time_counter == CFile::time_counter_attr::instant)
1435            time_counter_bound(0) = time_counter_bound(1) = lastWrite;
1436          else if (field->file->time_counter == CFile::time_counter_attr::centered)
1437          {
1438            time_counter_bound(0) = lastLastWrite;
1439            time_counter_bound(1) = lastWrite;
1440          }
1441          else if (field->file->time_counter == CFile::time_counter_attr::record)
1442            time_counter_bound(0) = time_counter_bound(1) = field->getNStep() - 1;
1443        }
1444
1445         bool isRoot = (server->intraCommRank == 0);
1446
1447         if (!field->scale_factor.isEmpty() || !field->add_offset.isEmpty())
1448         {
1449           double scaleFactor = 1.0;
1450           double addOffset = 0.0;
1451           if (!field->scale_factor.isEmpty()) scaleFactor = field->scale_factor;
1452           if (!field->add_offset.isEmpty()) addOffset = field->add_offset;
1453           field->scaleFactorAddOffset(scaleFactor, addOffset);
1454         }
1455
1456         try
1457         {
1458           size_t writtenSize;
1459           if (field->getUseCompressedOutput())
1460             writtenSize = grid->getNumberWrittenIndexes();
1461           else
1462             writtenSize = grid->getWrittenDataSize();
1463
1464           CArray<double,1> fieldData(writtenSize);
1465           if (!field->default_value.isEmpty()) fieldData = field->default_value;
1466
1467           if (field->getUseCompressedOutput())
1468             field->outputCompressedField(fieldData);
1469           else
1470             field->outputField(fieldData);
1471
1472           if (!field->prec.isEmpty() && field->prec == 2) fieldData = round(fieldData);
1473
1474           switch (SuperClass::type)
1475           {
1476              case (MULTI_FILE) :
1477              {
1478                 SuperClassWriter::writeData(fieldData, fieldid, isCollective, field->getNStep() - 1);
1479                 if (wtime)
1480                 {
1481                   SuperClassWriter::writeData(time_data, timeAxisId, isCollective, field->getNStep() - 1);
1482                   SuperClassWriter::writeData(time_data_bound, timeAxisBoundId, isCollective, field->getNStep() - 1);
1483                   if (field->file->time_counter != CFile::time_counter_attr::none)
1484                   {
1485                     SuperClassWriter::writeData(time_counter, string("time_counter"), isCollective, field->getNStep() - 1);
1486                     if (field->file->time_counter != CFile::time_counter_attr::record)
1487                       SuperClassWriter::writeData(time_counter_bound, timeBoundId, isCollective, field->getNStep() - 1);
1488                   }
1489                 }
1490                 break;
1491              }
1492              case (ONE_FILE) :
1493              {
1494                const std::vector<int>& nZoomBeginGlobal = grid->getDistributionServer()->getZoomBeginGlobal();
1495                const std::vector<int>& nZoomBeginServer = grid->getDistributionServer()->getZoomBeginServer();
1496                const std::vector<int>& nZoomSizeServer  = grid->getDistributionServer()->getZoomSizeServer();
1497
1498                std::vector<StdSize> start, count;
1499
1500                if (field->getUseCompressedOutput())
1501                {
1502                  if (grid->isCompressible())
1503                  {
1504                    start.push_back(grid->getOffsetWrittenIndexes());
1505                    count.push_back(grid->getNumberWrittenIndexes());
1506                  }
1507                  else
1508                  {
1509                    CArray<bool,1> axisDomainOrder = grid->axis_domain_order;
1510                    std::vector<StdString> domainList = grid->getDomainList();
1511                    std::vector<StdString> axisList   = grid->getAxisList();
1512                    int numElement = axisDomainOrder.numElements();
1513                    int idxDomain = domainList.size() - 1, idxAxis = axisList.size() - 1;
1514                    int idx = nZoomBeginGlobal.size() - 1;
1515
1516                    start.reserve(nZoomBeginGlobal.size());
1517                    count.reserve(nZoomBeginGlobal.size());
1518
1519
1520                    for (int i = numElement - 1; i >= 0; --i)
1521                    {
1522                      if (axisDomainOrder(i))
1523                      {
1524                        CDomain* domain = CDomain::get(domainList[idxDomain]);
1525
1526                        if (domain->isCompressible())
1527                        {
1528                          start.push_back(domain->getOffsetWrittenIndexes());
1529                          count.push_back(domain->getNumberWrittenIndexes());
1530                          idx -= 2;
1531                        }
1532                        else
1533                        {
1534                          if ((domain->type) != CDomain::type_attr::unstructured)
1535                          {
1536                            start.push_back(nZoomBeginServer[idx] - nZoomBeginGlobal[idx]);
1537                            count.push_back(nZoomSizeServer[idx]);
1538                          }
1539                          --idx;
1540                          start.push_back(nZoomBeginServer[idx] - nZoomBeginGlobal[idx]);
1541                          count.push_back(nZoomSizeServer[idx]);
1542                          --idx;
1543                        }
1544                        --idxDomain;
1545                      }
1546                      else
1547                      {
1548                        CAxis* axis = CAxis::get(axisList[idxAxis]);
1549
1550                        if (axis->isCompressible())
1551                        {
1552                          start.push_back(axis->getOffsetWrittenIndexes());
1553                          count.push_back(axis->getNumberWrittenIndexes());
1554                        }
1555                        else
1556                        {
1557                          start.push_back(nZoomBeginServer[idx] - nZoomBeginGlobal[idx]);
1558                          count.push_back(nZoomSizeServer[idx]);
1559                        }
1560
1561                        --idxAxis;
1562                        --idx;
1563                      }
1564                    }
1565                  }
1566                }
1567                else
1568                {
1569
1570                  CArray<bool,1> axisDomainOrder = grid->axis_domain_order;
1571                  std::vector<StdString> domainList = grid->getDomainList();
1572                  std::vector<StdString> axisList   = grid->getAxisList();
1573                  int numElement = axisDomainOrder.numElements();
1574                  int idxDomain = domainList.size() - 1, idxAxis = axisList.size() - 1;
1575                  int idx = nZoomBeginGlobal.size() - 1;
1576
1577                  start.reserve(nZoomBeginGlobal.size());
1578                  count.reserve(nZoomBeginGlobal.size());
1579
1580                  for (int i = numElement - 1; i >= 0; --i)
1581                  {
1582                    if (axisDomainOrder(i))
1583                    {
1584                      CDomain* domain = CDomain::get(domainList[idxDomain]);
1585                      if ((domain->type) != CDomain::type_attr::unstructured)
1586                      {
1587                        start.push_back(nZoomBeginServer[idx] - nZoomBeginGlobal[idx]);
1588                        count.push_back(nZoomSizeServer[idx]);
1589                      }
1590                      --idx ;
1591                      start.push_back(nZoomBeginServer[idx] - nZoomBeginGlobal[idx]);
1592                      count.push_back(nZoomSizeServer[idx]);
1593                      --idx ;
1594                      --idxDomain;
1595                    }
1596                    else
1597                    {
1598                      start.push_back(nZoomBeginServer[idx] - nZoomBeginGlobal[idx]);
1599                      count.push_back(nZoomSizeServer[idx]);
1600                      --idx;
1601                     }
1602                  }
1603                }
1604
1605                SuperClassWriter::writeData(fieldData, fieldid, isCollective, field->getNStep() - 1, &start, &count);
1606                if (wtime)
1607                {
1608                   SuperClassWriter::writeTimeAxisData(time_data, timeAxisId, isCollective, field->getNStep() - 1, isRoot);
1609                   SuperClassWriter::writeTimeAxisData(time_data_bound, timeAxisBoundId, isCollective, field->getNStep() - 1, isRoot);
1610                   if (field->file->time_counter != CFile::time_counter_attr::none)
1611                   {
1612                     SuperClassWriter::writeTimeAxisData(time_counter, string("time_counter"), isCollective, field->getNStep() - 1, isRoot);
1613                     if (field->file->time_counter != CFile::time_counter_attr::record)
1614                       SuperClassWriter::writeTimeAxisData(time_counter_bound, timeBoundId, isCollective, field->getNStep() - 1, isRoot);
1615                   }
1616                }
1617
1618                break;
1619              }
1620            }
1621         }
1622         catch (CNetCdfException& e)
1623         {
1624           StdString msg("On writing field data: ");
1625           msg.append(fieldid); msg.append("\n");
1626           msg.append("In the context : ");
1627           msg.append(context->getId()); msg.append("\n");
1628           msg.append(e.what());
1629           ERROR("CNc4DataOutput::writeFieldData_ (CField*  field)", << msg);
1630         }
1631      }
1632
1633      //---------------------------------------------------------------
1634
1635      void CNc4DataOutput::writeTimeAxis_
1636                  (CField*    field,
1637                   const boost::shared_ptr<CCalendar> cal)
1638      {
1639         StdOStringStream oss;
1640
1641         if (field->getOperationTimeType() == func::CFunctor::once) return ;
1642
1643//         oss << "time_" << field->operation.getValue()
1644//             << "_" << field->getRelFile()->output_freq.getValue();
1645
1646//         StdString axisid = oss.str();
1647//         if (field->getOperationTimeType() == func::CFunctor::centered) axisid="time_centered" ;
1648//         else if (field->getOperationTimeType() == func::CFunctor::instant) axisid="time_instant" ;
1649
1650         StdString axisid("time_centered") ;
1651         StdString axisBoundId("time_centered_bounds");
1652         StdString timeid("time_counter");
1653         StdString timeBoundId("axis_nbounds");
1654
1655         if (field->getOperationTimeType() == func::CFunctor::instant)
1656         {
1657            axisid = "time_instant";
1658            axisBoundId = "time_instant_bounds";
1659         }
1660
1661         try
1662         {
1663          // Adding time_instant or time_centered
1664           std::vector<StdString> dims;
1665           dims.push_back(timeid);
1666           if (!SuperClassWriter::varExist(axisid))
1667           {
1668              SuperClassWriter::addVariable(axisid, NC_DOUBLE, dims);
1669
1670              CDate timeOrigin=cal->getTimeOrigin() ;
1671              StdOStringStream oss2;
1672  //            oss2<<initDate.getYear()<<"-"<<initDate.getMonth()<<"-"<<initDate.getDay()<<" "
1673  //                <<initDate.getHour()<<"-"<<initDate.getMinute()<<"-"<<initDate.getSecond() ;
1674              StdString strInitdate=oss2.str() ;
1675              StdString strTimeOrigin=timeOrigin.toString() ;
1676              this->writeTimeAxisAttributes
1677                 (axisid, cal->getType(),
1678                  StdString("seconds since ").append(strTimeOrigin),
1679                  strTimeOrigin, axisBoundId);
1680           }
1681
1682           // Adding time_instant_bounds or time_centered_bounds variables
1683           if (!SuperClassWriter::varExist(axisBoundId))
1684           {
1685              dims.clear() ;
1686              dims.push_back(timeid);
1687              dims.push_back(timeBoundId);
1688              SuperClassWriter::addVariable(axisBoundId, NC_DOUBLE, dims);
1689           }
1690
1691           if (field->file->time_counter != CFile::time_counter_attr::none)
1692           {
1693             // Adding time_counter
1694             axisid = "time_counter";
1695             axisBoundId = "time_counter_bounds";
1696             dims.clear();
1697             dims.push_back(timeid);
1698             if (!SuperClassWriter::varExist(axisid))
1699             {
1700                SuperClassWriter::addVariable(axisid, NC_DOUBLE, dims);
1701                SuperClassWriter::addAttribute("axis", string("T"), &axisid);
1702
1703                if (field->file->time_counter != CFile::time_counter_attr::record)
1704                {
1705                  CDate timeOrigin = cal->getTimeOrigin();
1706                  StdString strTimeOrigin = timeOrigin.toString();
1707
1708                  this->writeTimeAxisAttributes(axisid, cal->getType(),
1709                                                StdString("seconds since ").append(strTimeOrigin),
1710                                                strTimeOrigin, axisBoundId);
1711                }
1712             }
1713
1714             // Adding time_counter_bound dimension
1715             if (field->file->time_counter != CFile::time_counter_attr::record)
1716             {
1717                if (!SuperClassWriter::varExist(axisBoundId))
1718                {
1719                  dims.clear();
1720                  dims.push_back(timeid);
1721                  dims.push_back(timeBoundId);
1722                  SuperClassWriter::addVariable(axisBoundId, NC_DOUBLE, dims);
1723                }
1724             }
1725           }
1726         }
1727         catch (CNetCdfException& e)
1728         {
1729           StdString msg("On writing time axis data: ");
1730           msg.append("In the context : ");
1731           CContext* context = CContext::getCurrent() ;
1732           msg.append(context->getId()); msg.append("\n");
1733           msg.append(e.what());
1734           ERROR("CNc4DataOutput::writeTimeAxis_ (CField*    field, \
1735                  const boost::shared_ptr<CCalendar> cal)", << msg);
1736         }
1737      }
1738
1739      //---------------------------------------------------------------
1740
1741      void CNc4DataOutput::writeTimeAxisAttributes(const StdString & axis_name,
1742                                                   const StdString & calendar,
1743                                                   const StdString & units,
1744                                                   const StdString & time_origin,
1745                                                   const StdString & time_bounds,
1746                                                   const StdString & standard_name,
1747                                                   const StdString & long_name)
1748      {
1749         try
1750         {
1751           SuperClassWriter::addAttribute("standard_name", standard_name, &axis_name);
1752           SuperClassWriter::addAttribute("long_name",     long_name    , &axis_name);
1753           SuperClassWriter::addAttribute("calendar",      calendar     , &axis_name);
1754           SuperClassWriter::addAttribute("units",         units        , &axis_name);
1755           SuperClassWriter::addAttribute("time_origin",   time_origin  , &axis_name);
1756           SuperClassWriter::addAttribute("bounds",        time_bounds  , &axis_name);
1757         }
1758         catch (CNetCdfException& e)
1759         {
1760           StdString msg("On writing time axis Attribute: ");
1761           msg.append("In the context : ");
1762           CContext* context = CContext::getCurrent() ;
1763           msg.append(context->getId()); msg.append("\n");
1764           msg.append(e.what());
1765           ERROR("CNc4DataOutput::writeTimeAxisAttributes(const StdString & axis_name, \
1766                                                          const StdString & calendar,\
1767                                                          const StdString & units, \
1768                                                          const StdString & time_origin, \
1769                                                          const StdString & time_bounds, \
1770                                                          const StdString & standard_name, \
1771                                                          const StdString & long_name)", << msg);
1772         }
1773      }
1774
1775      //---------------------------------------------------------------
1776
1777      void CNc4DataOutput::writeAxisAttributes(const StdString & axis_name,
1778                                               const StdString & axis,
1779                                               const StdString & standard_name,
1780                                               const StdString & long_name,
1781                                               const StdString & units,
1782                                               const StdString & nav_model)
1783      {
1784         try
1785         {
1786          if (!axis.empty())
1787            SuperClassWriter::addAttribute("axis"       , axis         , &axis_name);
1788
1789          SuperClassWriter::addAttribute("standard_name", standard_name, &axis_name);
1790          SuperClassWriter::addAttribute("long_name"    , long_name    , &axis_name);
1791          SuperClassWriter::addAttribute("units"        , units        , &axis_name);
1792          SuperClassWriter::addAttribute("nav_model"    , nav_model    , &axis_name);
1793         }
1794         catch (CNetCdfException& e)
1795         {
1796           StdString msg("On writing Axis Attribute: ");
1797           msg.append("In the context : ");
1798           CContext* context = CContext::getCurrent() ;
1799           msg.append(context->getId()); msg.append("\n");
1800           msg.append(e.what());
1801           ERROR("CNc4DataOutput::writeAxisAttributes(const StdString & axis_name, \
1802                                                      const StdString & axis, \
1803                                                      const StdString & standard_name, \
1804                                                      const StdString & long_name, \
1805                                                      const StdString & units, \
1806                                                      const StdString & nav_model)", << msg);
1807         }
1808      }
1809
1810      //---------------------------------------------------------------
1811
1812      void CNc4DataOutput::writeLocalAttributes
1813         (int ibegin, int ni, int jbegin, int nj, StdString domid)
1814      {
1815        try
1816        {
1817         SuperClassWriter::addAttribute(StdString("ibegin").append(domid), ibegin);
1818         SuperClassWriter::addAttribute(StdString("ni"    ).append(domid), ni);
1819         SuperClassWriter::addAttribute(StdString("jbegin").append(domid), jbegin);
1820         SuperClassWriter::addAttribute(StdString("nj"    ).append(domid), nj);
1821        }
1822        catch (CNetCdfException& e)
1823        {
1824           StdString msg("On writing Local Attributes: ");
1825           msg.append("In the context : ");
1826           CContext* context = CContext::getCurrent() ;
1827           msg.append(context->getId()); msg.append("\n");
1828           msg.append(e.what());
1829           ERROR("CNc4DataOutput::writeLocalAttributes \
1830                  (int ibegin, int ni, int jbegin, int nj, StdString domid)", << msg);
1831        }
1832
1833      }
1834
1835      void CNc4DataOutput::writeLocalAttributes_IOIPSL(const StdString& dimXid, const StdString& dimYid,
1836                                                       int ibegin, int ni, int jbegin, int nj, int ni_glo, int nj_glo, int rank, int size)
1837      {
1838         CArray<int,1> array(2) ;
1839
1840         try
1841         {
1842           SuperClassWriter::addAttribute("DOMAIN_number_total",size ) ;
1843           SuperClassWriter::addAttribute("DOMAIN_number", rank) ;
1844           array = SuperClassWriter::getDimension(dimXid) + 1, SuperClassWriter::getDimension(dimYid) + 1;
1845           SuperClassWriter::addAttribute("DOMAIN_dimensions_ids",array) ;
1846           array=ni_glo,nj_glo ;
1847           SuperClassWriter::addAttribute("DOMAIN_size_global", array) ;
1848           array=ni,nj ;
1849           SuperClassWriter::addAttribute("DOMAIN_size_local", array) ;
1850           array=ibegin,jbegin ;
1851           SuperClassWriter::addAttribute("DOMAIN_position_first", array) ;
1852           array=ibegin+ni-1,jbegin+nj-1 ;
1853           SuperClassWriter::addAttribute("DOMAIN_position_last",array) ;
1854           array=0,0 ;
1855           SuperClassWriter::addAttribute("DOMAIN_halo_size_start", array) ;
1856           SuperClassWriter::addAttribute("DOMAIN_halo_size_end", array);
1857           SuperClassWriter::addAttribute("DOMAIN_type",string("box")) ;
1858  /*
1859           SuperClassWriter::addAttribute("DOMAIN_DIM_N001",string("x")) ;
1860           SuperClassWriter::addAttribute("DOMAIN_DIM_N002",string("y")) ;
1861           SuperClassWriter::addAttribute("DOMAIN_DIM_N003",string("axis_A")) ;
1862           SuperClassWriter::addAttribute("DOMAIN_DIM_N004",string("time_counter")) ;
1863  */
1864         }
1865         catch (CNetCdfException& e)
1866         {
1867           StdString msg("On writing Local Attributes IOIPSL \n");
1868           msg.append("In the context : ");
1869           CContext* context = CContext::getCurrent() ;
1870           msg.append(context->getId()); msg.append("\n");
1871           msg.append(e.what());
1872           ERROR("CNc4DataOutput::writeLocalAttributes_IOIPSL \
1873                  (int ibegin, int ni, int jbegin, int nj, int ni_glo, int nj_glo, int rank, int size)", << msg);
1874         }
1875      }
1876      //---------------------------------------------------------------
1877
1878      void CNc4DataOutput:: writeFileAttributes(const StdString & name,
1879                                                const StdString & description,
1880                                                const StdString & conventions,
1881                                                const StdString & production,
1882                                                const StdString & timeStamp)
1883      {
1884         try
1885         {
1886           SuperClassWriter::addAttribute("name"       , name);
1887           SuperClassWriter::addAttribute("description", description);
1888           SuperClassWriter::addAttribute("title"      , description);
1889           SuperClassWriter::addAttribute("Conventions", conventions);
1890           SuperClassWriter::addAttribute("production" , production);
1891           SuperClassWriter::addAttribute("timeStamp"  , timeStamp);
1892         }
1893         catch (CNetCdfException& e)
1894         {
1895           StdString msg("On writing File Attributes \n ");
1896           msg.append("In the context : ");
1897           CContext* context = CContext::getCurrent() ;
1898           msg.append(context->getId()); msg.append("\n");
1899           msg.append(e.what());
1900           ERROR("CNc4DataOutput:: writeFileAttributes(const StdString & name, \
1901                                                const StdString & description, \
1902                                                const StdString & conventions, \
1903                                                const StdString & production, \
1904                                                const StdString & timeStamp)", << msg);
1905         }
1906      }
1907
1908      //---------------------------------------------------------------
1909
1910      void CNc4DataOutput::writeMaskAttributes(const StdString & mask_name,
1911                                               int data_dim,
1912                                               int data_ni,
1913                                               int data_nj,
1914                                               int data_ibegin,
1915                                               int data_jbegin)
1916      {
1917         try
1918         {
1919           SuperClassWriter::addAttribute("data_dim"   , data_dim   , &mask_name);
1920           SuperClassWriter::addAttribute("data_ni"    , data_ni    , &mask_name);
1921           SuperClassWriter::addAttribute("data_nj"    , data_nj    , &mask_name);
1922           SuperClassWriter::addAttribute("data_ibegin", data_ibegin, &mask_name);
1923           SuperClassWriter::addAttribute("data_jbegin", data_jbegin, &mask_name);
1924         }
1925         catch (CNetCdfException& e)
1926         {
1927           StdString msg("On writing Mask Attributes \n ");
1928           msg.append("In the context : ");
1929           CContext* context = CContext::getCurrent() ;
1930           msg.append(context->getId()); msg.append("\n");
1931           msg.append(e.what());
1932           ERROR("CNc4DataOutput::writeMaskAttributes(const StdString & mask_name, \
1933                                               int data_dim, \
1934                                               int data_ni, \
1935                                               int data_nj, \
1936                                               int data_ibegin, \
1937                                               int data_jbegin)", << msg);
1938         }
1939      }
1940
1941      ///--------------------------------------------------------------
1942
1943      StdSize CNc4DataOutput::getRecordFromTime(Time time)
1944      {
1945        std::map<Time, StdSize>::const_iterator it = timeToRecordCache.find(time);
1946        if (it == timeToRecordCache.end())
1947        {
1948          StdString timeAxisBoundsId("time_counter_bounds");
1949          if (!SuperClassWriter::varExist(timeAxisBoundsId))
1950            timeAxisBoundsId = "time_instant_bounds";
1951
1952          CArray<double,2> timeAxisBounds;
1953          SuperClassWriter::getTimeAxisBounds(timeAxisBounds, timeAxisBoundsId, isCollective);
1954
1955          StdSize record = 0;
1956          double dtime(time);
1957          for (int n = timeAxisBounds.extent(1) - 1; n >= 0; n--)
1958          {
1959            if (timeAxisBounds(1, n) < dtime)
1960            {
1961              record = n + 1;
1962              break;
1963            }
1964          }
1965          it = timeToRecordCache.insert(std::make_pair(time, record)).first;
1966        }
1967        return it->second;
1968      }
1969} // namespace xios
Note: See TracBrowser for help on using the repository browser.