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
RevLine 
[266]1
[219]2#include "nc4_data_output.hpp"
3
4#include <boost/lexical_cast.hpp>
[352]5#include "attribute_template.hpp"
6#include "group_template.hpp"
[219]7
8#include "file.hpp"
9#include "calendar.hpp"
[278]10#include "context.hpp"
[300]11#include "context_server.hpp"
[498]12#include "netCdfException.hpp"
13#include "exception.hpp"
[219]14
[335]15namespace xios
[219]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
[517]29         (const StdString & filename, bool exist, bool useClassicFormat,
30          MPI_Comm comm_file,bool multifile, bool isCollective)
[219]31            : SuperClass()
[517]32            , SuperClassWriter(filename, exist, useClassicFormat, &comm_file, multifile)
[379]33            , comm_file(comm_file)
[219]34            , filename(filename)
[335]35            , isCollective(isCollective)
[219]36      {
37         StdString timeid = StdString("time_counter");
38
[286]39         SuperClass::type = (multifile) ? MULTI_FILE : ONE_FILE;
[219]40      }
41
[286]42
[219]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
[347]55      void CNc4DataOutput::writeDomain_(CDomain* domain)
[219]56      {
[449]57         if (domain->type == CDomain::type_attr::unstructured)
58         {
59           writeUnstructuredDomain(domain) ;
60           return ;
61         }
[488]62
[347]63         CContext* context = CContext::getCurrent() ;
[300]64         CContextServer* server=context->server ;
[488]65
[219]66         if (domain->IsWritten(this->filename)) return;
67         domain->checkAttributes();
[488]68
69         if (domain->isEmpty())
[300]70           if (SuperClass::type==MULTI_FILE) return ;
[219]71
72         std::vector<StdString> dim0, dim1;
[772]73         StdString domid = domain->getDomainOutputName();
[318]74         StdString appendDomid  = (singleDomain) ? "" : "_"+domid ;
[706]75         if (isWrittenDomain(domid)) return ;
[710]76         else writtenDomains.insert(domid) ;
[449]77
78
79         StdString dimXid, dimYid ;
[488]80
[664]81         bool isRegularDomain = (domain->type == CDomain::type_attr::rectilinear);
[449]82         switch (domain->type)
[433]83         {
[449]84           case CDomain::type_attr::curvilinear :
85             dimXid     = StdString("x").append(appendDomid);
86             dimYid     = StdString("y").append(appendDomid);
87             break ;
[664]88           case CDomain::type_attr::rectilinear :
[449]89             dimXid     = StdString("lon").append(appendDomid);
90             dimYid     = StdString("lat").append(appendDomid);
91             break;
[488]92         }
93
[617]94         StdString dimVertId = StdString("nvertex").append(appendDomid);
95
[449]96         string lonid,latid,bounds_lonid,bounds_latid ;
[611]97         string areaId = "area" + appendDomid;
[391]98/*
[300]99         StdString lonid_loc = (server->intraCommSize > 1)
[318]100                             ? StdString("lon").append(appendDomid).append("_local")
[278]101                             : lonid;
[300]102         StdString latid_loc = (server->intraCommSize > 1)
[318]103                             ? StdString("lat").append(appendDomid).append("_local")
[278]104                             : latid;
[391]105*/
[219]106
[498]107         try
[219]108         {
[498]109           switch (SuperClass::type)
110           {
111              case (MULTI_FILE) :
112              {
113  //               if (domain->isEmpty()) return;
[488]114
[498]115                 if (server->intraCommSize > 1)
116                 {
117  //                 SuperClassWriter::addDimension(lonid, domain->zoom_ni.getValue());
118  //                 SuperClassWriter::addDimension(latid, domain->zoom_nj.getValue());
119                 }
[286]120
[498]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 ;
[664]128                   case CDomain::type_attr::rectilinear :
[498]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                 }
[488]135
[617]136                 bounds_lonid = StdString("bounds_lon").append(appendDomid);
137                 bounds_latid = StdString("bounds_lat").append(appendDomid);
138
[616]139                 SuperClassWriter::addDimension(dimXid, domain->zoom_ni_srv);
140                 SuperClassWriter::addDimension(dimYid, domain->zoom_nj_srv);
[488]141
[617]142                 if (domain->hasBounds)
143                   SuperClassWriter::addDimension(dimVertId, domain->nvertex);
144
[498]145                 if (server->intraCommSize > 1)
146                 {
[616]147                   this->writeLocalAttributes(domain->zoom_ibegin_srv,
148                                              domain->zoom_ni_srv,
149                                              domain->zoom_jbegin_srv,
150                                              domain->zoom_nj_srv,
151                                              appendDomid);
[488]152
[628]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);
[449]161                 }
[488]162
[665]163                 if (domain->hasLonLat)
[498]164                 {
[665]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                   }
[488]176
[665]177                   this->writeAxisAttributes(lonid, isRegularDomain ? "X" : "", "longitude", "Longitude", "degrees_east", domid);
178                   this->writeAxisAttributes(latid, isRegularDomain ? "Y" : "", "latitude", "Latitude", "degrees_north", domid);
[219]179
[665]180                   if (domain->hasBounds)
181                   {
182                     SuperClassWriter::addAttribute("bounds", bounds_lonid, &lonid);
183                     SuperClassWriter::addAttribute("bounds", bounds_latid, &latid);
[617]184
[665]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                   }
[617]192                 }
193
[498]194                 dim0.clear();
[616]195                 dim0.push_back(dimYid);
[498]196                 dim0.push_back(dimXid);
[219]197
198
[498]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               }
[488]210
[498]211                 //SuperClassWriter::setDefaultValue(maskid, &dvm);
[219]212
[611]213                 if (domain->hasArea)
214                 {
215                   SuperClassWriter::addVariable(areaId, NC_FLOAT, dim0);
[614]216                   SuperClassWriter::addAttribute("standard_name", StdString("cell_area"), &areaId);
[611]217                   SuperClassWriter::addAttribute("units", StdString("m2"), &areaId);
218                 }
219
[498]220                 SuperClassWriter::definition_end();
[449]221
[665]222                 if (domain->hasLonLat)
[498]223                 {
[665]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                   }
[611]237
[665]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                   }
[617]243                 }
244
[611]245                 if (domain->hasArea)
246                   SuperClassWriter::writeData(domain->area_srv, areaId, isCollective, 0);
247
[498]248                 SuperClassWriter::definition_start();
[219]249
[498]250                 break;
251              }
252              case (ONE_FILE) :
253              {
[631]254                 SuperClassWriter::addDimension(dimXid, domain->global_zoom_ni);
255                 SuperClassWriter::addDimension(dimYid, domain->global_zoom_nj);
[286]256
[617]257                 if (domain->hasBounds)
258                   SuperClassWriter::addDimension(dimVertId, domain->nvertex);
259
[665]260                 if (domain->hasLonLat)
[498]261                 {
[665]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;
[449]271
[665]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();
[498]296                     dim0.push_back(dimYid);
[665]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                   }
[498]302                 }
[611]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);
[614]309                   SuperClassWriter::addAttribute("standard_name", StdString("cell_area"), &areaId);
[611]310                   SuperClassWriter::addAttribute("units", StdString("m2"), &areaId);
311                   dim0.clear();
312                 }
313
314                 SuperClassWriter::definition_end();
[286]315
[498]316                 switch (domain->type)
[384]317                 {
[498]318                   case CDomain::type_attr::curvilinear :
[449]319                   {
[498]320                     std::vector<StdSize> start(2) ;
321                     std::vector<StdSize> count(2) ;
322                     if (domain->isEmpty())
323                     {
[611]324                       start[0]=0 ; start[1]=0 ;
[498]325                       count[0]=0 ; count[1]=0 ;
326                     }
327                     else
328                     {
[631]329                       start[1]=domain->zoom_ibegin_srv-domain->global_zoom_ibegin;
330                       start[0]=domain->zoom_jbegin_srv-domain->global_zoom_jbegin;
[498]331                       count[1]=domain->zoom_ni_srv ; count[0]=domain->zoom_nj_srv ;
332                     }
[488]333
[665]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                     }
[498]339                     break;
340                   }
[664]341                   case CDomain::type_attr::rectilinear :
[449]342                   {
[665]343                     if (domain->hasLonLat)
[498]344                     {
[665]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);
[449]353
[665]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                       }
[498]367                     }
368                     break;
[449]369                   }
[384]370                 }
[611]371
[617]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;
[631]384                     start[1] = domain->zoom_ibegin_srv - domain->global_zoom_ibegin;
385                     start[0] = domain->zoom_jbegin_srv - domain->global_zoom_jbegin;
[617]386                     count[2] = domain->nvertex;
387                     count[1] = domain->zoom_ni_srv;
388                     count[0] = domain->zoom_nj_srv;
389                   }
[625]390
[617]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
[611]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                   {
[631]407                     start[1] = domain->zoom_ibegin_srv - domain->global_zoom_ibegin;
408                     start[0] = domain->zoom_jbegin_srv - domain->global_zoom_jbegin;
[611]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
[498]416                 SuperClassWriter::definition_start();
417                 break;
418              }
419              default :
420                 ERROR("CNc4DataOutput::writeDomain(domain)",
421                       << "[ type = " << SuperClass::type << "]"
422                       << " not implemented yet !");
423           }
[449]424         }
[498]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
[449]435         domain->addRelFile(this->filename);
436      }
437
438      void CNc4DataOutput::writeUnstructuredDomain(CDomain* domain)
439      {
440         CContext* context = CContext::getCurrent() ;
441         CContextServer* server=context->server ;
[488]442
[449]443         if (domain->IsWritten(this->filename)) return;
444         domain->checkAttributes();
[488]445
446         if (domain->isEmpty())
[449]447           if (SuperClass::type==MULTI_FILE) return ;
448
449         std::vector<StdString> dim0, dim1;
[772]450         StdString domid = domain->getDomainOutputName();
[705]451         if (isWrittenDomain(domid)) return ;
[710]452         else writtenDomains.insert(domid) ;
[705]453
[449]454         StdString appendDomid  = (singleDomain) ? "" : "_"+domid ;
455
456
457         StdString dimXid = StdString("cell").append(appendDomid);
458         StdString dimVertId = StdString("nvertex").append(appendDomid);
[488]459
[449]460         string lonid,latid,bounds_lonid,bounds_latid ;
[611]461         string areaId = "area" + appendDomid;
[449]462
[498]463         try
[449]464         {
[498]465           switch (SuperClass::type)
466           {
467              case (MULTI_FILE) :
468              {
469                 dim0.push_back(dimXid);
[657]470                 SuperClassWriter::addDimension(dimXid, domain->zoom_ni_srv);
[488]471
[665]472                 lonid = StdString("lon").append(appendDomid);
473                 latid = StdString("lat").append(appendDomid);
[498]474                 bounds_lonid = StdString("bounds_lon").append(appendDomid);
475                 bounds_latid = StdString("bounds_lat").append(appendDomid);
[665]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                 }
[488]486
[498]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();
[449]497                 dim0.push_back(dimXid);
[611]498                 if (domain->hasArea)
499                 {
500                   SuperClassWriter::addVariable(areaId, NC_FLOAT, dim0);
[614]501                   SuperClassWriter::addAttribute("standard_name", StdString("cell_area"), &areaId);
[611]502                   SuperClassWriter::addAttribute("units", StdString("m2"), &areaId);
503                 }
504
[498]505                 SuperClassWriter::definition_end();
[449]506
[665]507                 if (domain->hasLonLat)
[498]508                 {
[665]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                   }
[498]516                 }
[611]517
518                 if (domain->hasArea)
519                   SuperClassWriter::writeData(domain->area_srv, areaId, isCollective, 0);
520
[498]521                 SuperClassWriter::definition_start();
522                 break ;
523              }
[488]524
[498]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);
[705]532                 SuperClassWriter::addDimension(dimXid, domain->ni_glo);
[665]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                 }
[498]544                 dim0.clear();
[449]545
[498]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                 }
[488]553
[611]554                 if (domain->hasArea)
555                 {
556                   dim0.clear();
557                   dim0.push_back(dimXid);
558                   SuperClassWriter::addVariable(areaId, NC_FLOAT, dim0);
[614]559                   SuperClassWriter::addAttribute("standard_name", StdString("cell_area"), &areaId);
[611]560                   SuperClassWriter::addAttribute("units", StdString("m2"), &areaId);
561                 }
562
[498]563                 SuperClassWriter::definition_end();
[488]564
[498]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                 {
[705]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;
[498]581                   startBounds[1]=0 ;
[705]582                   countBounds[0]=domain->zoom_ni_srv ;
[498]583                   countBounds[1]=domain->nvertex ;
584                 }
[665]585
586                 if (domain->hasLonLat)
[498]587                 {
[665]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                   }
[498]595                 }
[488]596
[611]597                 if (domain->hasArea)
598                   SuperClassWriter::writeData(domain->area_srv, areaId, isCollective, 0, &start, &count);
[488]599
[498]600                 SuperClassWriter::definition_start();
[488]601
[498]602                 break;
603              }
604              default :
605                 ERROR("CNc4DataOutput::writeDomain(domain)",
606                       << "[ type = " << SuperClass::type << "]"
607                       << " not implemented yet !");
608           }
[219]609         }
[498]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         }
[219]619         domain->addRelFile(this->filename);
620      }
621      //--------------------------------------------------------------
622
[347]623      void CNc4DataOutput::writeAxis_(CAxis* axis)
[219]624      {
[609]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;
[633]629        int zoom_size  = (MULTI_FILE == SuperClass::type) ? zoom_size_srv
[623]630                                                              : axis->global_zoom_size;
[633]631        int zoom_begin = (MULTI_FILE == SuperClass::type) ? zoom_begin_srv
[623]632                                                              : axis->global_zoom_begin;
[488]633
[633]634        if ((0 == zoom_size_srv) && (MULTI_FILE == SuperClass::type)) return;
[498]635
[609]636        std::vector<StdString> dims;
[772]637        StdString axisid = axis->getAxisOutputName();
[705]638        if (isWrittenAxis(axisid)) return ;
[710]639        else writtenAxis.insert(axisid) ;
[705]640
[609]641        try
642        {
643          SuperClassWriter::addDimension(axisid, zoom_size);
644          dims.push_back(axisid);
[633]645          SuperClassWriter::addVariable(axisid, NC_FLOAT, dims);
[219]646
[633]647          if (!axis->name.isEmpty())
648            SuperClassWriter::addAttribute("name", axis->name.getValue(), &axisid);
[219]649
[633]650          if (!axis->standard_name.isEmpty())
651            SuperClassWriter::addAttribute("standard_name", axis->standard_name.getValue(), &axisid);
[540]652
[633]653          if (!axis->long_name.isEmpty())
654            SuperClassWriter::addAttribute("long_name", axis->long_name.getValue(), &axisid);
[219]655
[633]656          if (!axis->unit.isEmpty())
657            SuperClassWriter::addAttribute("units", axis->unit.getValue(), &axisid);
[219]658
[633]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          }
[399]666
[633]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          }
[488]674
[633]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
[609]684              if (!axis->bounds.isEmpty())
[713]685                SuperClassWriter::writeData(axis->bound_srv, axisBoundsId, isCollective, 0);
[488]686
[633]687              SuperClassWriter::definition_start();
[219]688
[633]689              break;
690            }
691            case ONE_FILE:
692            {
693              CArray<double,1> axis_value(zoom_size_srv);
694              axis_value = axis->value_srv;
[498]695
[633]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
[609]702              if (!axis->bounds.isEmpty())
[713]703                SuperClassWriter::writeData(axis->bound_srv, axisBoundsId, isCollective, 0, &start, &count);
[609]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);
[391]726     }
[488]727
[676]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]);
[772]759               StdString domId = domain->getDomainOutputName();
[676]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]);
[772]779               compress << axis->getAxisOutputName();
[676]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
[772]837               StdString domId = domain->getDomainOutputName();
[676]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
[772]871               StdString axisId = axis->getAxisOutputName();
[676]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
[391]941     void CNc4DataOutput::writeTimeDimension_(void)
942     {
[498]943       try
944       {
945        SuperClassWriter::addDimension("time_counter");
946       }
947       catch (CNetCdfException& e)
948       {
[614]949         StdString msg("On writing time dimension : time_couter\n");
[498]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       }
[391]956     }
[676]957
[219]958      //--------------------------------------------------------------
959
[347]960      void CNc4DataOutput::writeField_(CField* field)
[219]961      {
[347]962         CContext* context = CContext::getCurrent() ;
[300]963         CContextServer* server=context->server ;
964
[219]965         std::vector<StdString> dims, coodinates;
[347]966         CGrid* grid = field->grid;
[567]967         if (!grid->doGridHaveDataToWrite())
968          if (SuperClass::type==MULTI_FILE) return ;
[488]969
[575]970         CArray<bool,1> axisDomainOrder = grid->axis_domain_order;
[567]971         int numElement = axisDomainOrder.numElements(), idxDomain = 0, idxAxis = 0;
972         std::vector<StdString> domainList = grid->getDomainList();
973         std::vector<StdString> axisList   = grid->getAxisList();
[219]974
[567]975         StdString timeid  = StdString("time_counter");
976         StdString dimXid,dimYid;
977         std::deque<StdString> dimIdList, dimCoordList;
[611]978         bool hasArea = false;
979         StdString cellMeasures = "area:";
[676]980         bool compressedOutput = !field->indexed_output.isEmpty() && field->indexed_output;
[488]981
[567]982         for (int i = 0; i < numElement; ++i)
[434]983         {
[567]984           if (axisDomainOrder(i))
985           {
986             CDomain* domain = CDomain::get(domainList[idxDomain]);
[772]987             StdString domId = domain->getDomainOutputName();
[676]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
[567]996             switch (domain->type)
997             {
[676]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));
[567]1008                 break ;
[676]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                 }
[567]1017                 break ;
[676]1018               case CDomain::type_attr::unstructured:
1019                 dimXid     = StdString("cell").append(appendDomId);
[567]1020                 dimIdList.push_back(dimXid);
[676]1021                 dimCoordList.push_back(StdString("lon").append(appendDomId));
1022                 dimCoordList.push_back(StdString("lat").append(appendDomId));
[567]1023                 break ;
[676]1024             }
1025             if (domain->hasArea)
1026             {
1027               hasArea = true;
1028               cellMeasures += " area" + appendDomId;
1029             }
1030             ++idxDomain;
[567]1031           }
1032           else
1033           {
1034             CAxis* axis = CAxis::get(axisList[idxAxis]);
[772]1035             StdString axisId = axis->getAxisOutputName();
[676]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;
[567]1047           }
1048         }
[488]1049
1050/*
[300]1051         StdString lonid_loc = (server->intraCommSize > 1)
[318]1052                             ? StdString("lon").append(appendDomid).append("_local")
[278]1053                             : lonid;
[300]1054         StdString latid_loc = (server->intraCommSize > 1)
[318]1055                             ? StdString("lat").append(appendDomid).append("_local")
[278]1056                             : latid;
[391]1057*/
[770]1058         StdString fieldid = field->getFieldOutputName();
[219]1059
[300]1060//         unsigned int ssize = domain->zoom_ni_loc.getValue() * domain->zoom_nj_loc.getValue();
1061//         bool isCurvilinear = (domain->lonvalue.getValue()->size() == ssize);
[488]1062//          bool isCurvilinear = domain->isCurvilinear ;
1063
[464]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 ;
[488]1070           else if (field->prec==8)   type =  NC_DOUBLE ;
[464]1071         }
[488]1072
[645]1073         bool wtime   = !(!field->operation.isEmpty() && field->getOperationTimeType() == func::CFunctor::once);
[488]1074
[219]1075         if (wtime)
1076         {
[488]1077
[449]1078            //StdOStringStream oss;
1079           // oss << "time_" << field->operation.getValue()
1080           //     << "_" << field->getRelFile()->output_freq.getValue();
[488]1081          //oss
[645]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"));
[219]1084            dims.push_back(timeid);
1085         }
1086
[676]1087         if (compressedOutput && grid->isCompressible())
[219]1088         {
[676]1089           dims.push_back(grid->getId() + "_points");
1090           field->setUseCompressedOutput();
[219]1091         }
[676]1092         else
1093         {
1094           while (!dimIdList.empty())
1095           {
1096             dims.push_back(dimIdList.back());
1097             dimIdList.pop_back();
1098           }
1099         }
[219]1100
[567]1101         while (!dimCoordList.empty())
[219]1102         {
[567]1103           coodinates.push_back(dimCoordList.back());
1104           dimCoordList.pop_back();
[219]1105         }
1106
[498]1107         try
1108         {
1109           SuperClassWriter::addVariable(fieldid, type, dims);
[488]1110
[498]1111           if (!field->standard_name.isEmpty())
1112              SuperClassWriter::addAttribute
1113                 ("standard_name",  field->standard_name.getValue(), &fieldid);
[219]1114
[498]1115           if (!field->long_name.isEmpty())
1116              SuperClassWriter::addAttribute
1117                 ("long_name", field->long_name.getValue(), &fieldid);
[219]1118
[498]1119           if (!field->unit.isEmpty())
1120              SuperClassWriter::addAttribute
1121                 ("units", field->unit.getValue(), &fieldid);
[463]1122
[498]1123            if (!field->valid_min.isEmpty())
1124              SuperClassWriter::addAttribute
1125                 ("valid_min", field->valid_min.getValue(), &fieldid);
[463]1126
[498]1127           if (!field->valid_max.isEmpty())
1128              SuperClassWriter::addAttribute
1129                 ("valid_max", field->valid_max.getValue(), &fieldid);
[464]1130
[498]1131            if (!field->scale_factor.isEmpty())
1132              SuperClassWriter::addAttribute
1133                 ("scale_factor", field->scale_factor.getValue(), &fieldid);
[464]1134
[498]1135             if (!field->add_offset.isEmpty())
1136              SuperClassWriter::addAttribute
1137                 ("add_offset", field->add_offset.getValue(), &fieldid);
[488]1138
[498]1139           SuperClassWriter::addAttribute
1140                 ("online_operation", field->operation.getValue(), &fieldid);
[472]1141
[498]1142          // write child variables as attributes
[488]1143
1144
[498]1145           vector<CVariable*> listVars = field->getAllVariables() ;
1146           for (vector<CVariable*>::iterator it = listVars.begin() ;it != listVars.end(); it++) writeAttribute_(*it, fieldid) ;
[472]1147
[488]1148
[498]1149           if (wtime)
1150           {
[614]1151              CDuration freqOp = field->freq_op.getValue();
1152              freqOp.solveTimeStep(*context->calendar);
1153              StdString freqOpStr = freqOp.toStringUDUnits();
[612]1154              SuperClassWriter::addAttribute("interval_operation", freqOpStr, &fieldid);
[437]1155
[614]1156              CDuration freqOut = field->getRelFile()->output_freq.getValue();
1157              freqOut.solveTimeStep(*context->calendar);
1158              SuperClassWriter::addAttribute("interval_write", freqOut.toStringUDUnits(), &fieldid);
[612]1159
[618]1160              StdString cellMethods = "time: ";
[612]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;
[614]1165              if (freqOp.resolve(*context->calendar) != freqOut.resolve(*context->calendar))
1166                cellMethods += " (interval: " + freqOpStr + ")";
[612]1167              SuperClassWriter::addAttribute("cell_methods", cellMethods, &fieldid);
[498]1168           }
[488]1169
[611]1170           if (hasArea)
1171             SuperClassWriter::addAttribute("cell_measures", cellMeasures, &fieldid);
1172
[498]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
[517]1183              SuperClassWriter::setDefaultValue(fieldid, (double*)NULL);
[219]1184
[606]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
[498]1189           {  // Ecriture des coordonnées
[488]1190
[498]1191              StdString coordstr; //boost::algorithm::join(coodinates, " ")
1192              std::vector<StdString>::iterator
1193                 itc = coodinates.begin(), endc = coodinates.end();
[488]1194
[498]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              }
[219]1202
[498]1203              SuperClassWriter::addAttribute("coordinates", coordstr, &fieldid);
[219]1204
[498]1205           }
[219]1206         }
[498]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         }
[219]1216      }
1217
1218      //--------------------------------------------------------------
1219
[347]1220      void CNc4DataOutput::writeFile_ (CFile* file)
[219]1221      {
[773]1222         StdString filename = file->getFileOutputName();
[219]1223         StdString description = (!file->description.isEmpty())
1224                               ? file->description.getValue()
[335]1225                               : StdString("Created by xios");
[609]1226
1227         singleDomain = (file->nbDomains == 1);
1228
[498]1229         try
1230         {
1231           this->writeFileAttributes(filename, description,
[614]1232                                     StdString("CF-1.5"),
[498]1233                                     StdString("An IPSL model"),
1234                                     this->getTimeStamp());
[609]1235
[701]1236           if (!appendMode)
1237             SuperClassWriter::addDimension("axis_nbounds", 2);
[498]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         }
[219]1249      }
[488]1250
[472]1251      void CNc4DataOutput::writeAttribute_ (CVariable* var, const string& fieldId)
1252      {
[773]1253        StdString name = var->getVariableOutputName();
[488]1254
[498]1255        try
1256        {
[527]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());
[498]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       }
[472]1281     }
[488]1282
[472]1283     void CNc4DataOutput::writeAttribute_ (CVariable* var)
1284     {
[773]1285        StdString name = var->getVariableOutputName();
1286
[498]1287        try
1288        {
[527]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());
[498]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       }
[488]1313     }
1314
[321]1315      void CNc4DataOutput::syncFile_ (void)
1316      {
[498]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        }
[321]1330      }
[219]1331
[286]1332      void CNc4DataOutput::closeFile_ (void)
1333      {
[498]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
[286]1348      }
1349
[219]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      }
[488]1365
[219]1366      //---------------------------------------------------------------
[488]1367
[347]1368      void CNc4DataOutput::writeFieldData_ (CField*  field)
[219]1369      {
[707]1370        CContext* context = CContext::getCurrent();
1371        CContextServer* server = context->server;
1372        CGrid* grid = field->grid;
[300]1373
[707]1374        if (!grid->doGridHaveDataToWrite())
1375          if (SuperClass::type == MULTI_FILE || !isCollective) return;
[488]1376
[770]1377        StdString fieldid = field->getFieldOutputName();
[286]1378
[707]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";
[488]1383
[707]1384        StdString timeBoundId("time_counter_bounds");
[449]1385
[707]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";
[488]1389
[707]1390        if (!field->wasWritten())
1391        {
[757]1392          if (appendMode && field->file->record_offset.isEmpty())
[707]1393          {
1394            field->resetNStep(getRecordFromTime(field->last_Write_srv) + 1);
1395          }
[488]1396
[707]1397          field->setWritten();
1398        }
[488]1399
1400
[707]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
[444]1408        if (wtime)
1409        {
[692]1410          Time lastWrite = field->last_Write_srv;
1411          Time lastLastWrite = field->lastlast_Write_srv;
1412
[645]1413          if (field->getOperationTimeType() == func::CFunctor::instant)
[692]1414            time_data(0) = lastWrite;
1415          else if (field->getOperationTimeType() == func::CFunctor::centered)
1416            time_data(0) = (lastWrite + lastLastWrite) / 2;
[488]1417
[645]1418          if (field->getOperationTimeType() == func::CFunctor::instant)
[692]1419            time_data_bound(0) = time_data_bound(1) = lastWrite;
[645]1420          else if (field->getOperationTimeType() == func::CFunctor::centered)
[488]1421          {
[692]1422            time_data_bound(0) = lastLastWrite;
1423            time_data_bound(1) = lastWrite;
[488]1424          }
1425
[692]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
[707]1445         bool isRoot = (server->intraCommRank == 0);
[488]1446
[464]1447         if (!field->scale_factor.isEmpty() || !field->add_offset.isEmpty())
1448         {
[707]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);
[464]1454         }
[488]1455
[498]1456         try
[219]1457         {
[676]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);
[567]1465           if (!field->default_value.isEmpty()) fieldData = field->default_value;
[676]1466
1467           if (field->getUseCompressedOutput())
1468             field->outputCompressedField(fieldData);
1469           else
1470             field->outputField(fieldData);
1471
[707]1472           if (!field->prec.isEmpty() && field->prec == 2) fieldData = round(fieldData);
[567]1473
1474           switch (SuperClass::type)
[498]1475           {
[567]1476              case (MULTI_FILE) :
1477              {
[692]1478                 SuperClassWriter::writeData(fieldData, fieldid, isCollective, field->getNStep() - 1);
[567]1479                 if (wtime)
1480                 {
[692]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                   }
[567]1489                 }
[707]1490                 break;
[567]1491              }
1492              case (ONE_FILE) :
1493              {
[676]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();
[464]1497
[676]1498                std::vector<StdSize> start, count;
[464]1499
[676]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;
[464]1515
[676]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());
[710]1530                          idx -= 2;
[676]1531                        }
1532                        else
1533                        {
[710]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;
[676]1540                          start.push_back(nZoomBeginServer[idx] - nZoomBeginGlobal[idx]);
1541                          count.push_back(nZoomSizeServer[idx]);
[710]1542                          --idx;
[676]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
[498]1568                {
[705]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]);
[710]1585                      if ((domain->type) != CDomain::type_attr::unstructured)
[705]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                  }
[498]1603                }
[488]1604
[676]1605                SuperClassWriter::writeData(fieldData, fieldid, isCollective, field->getNStep() - 1, &start, &count);
[586]1606                if (wtime)
1607                {
[692]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                   }
[586]1616                }
1617
1618                break;
[286]1619              }
[567]1620            }
[219]1621         }
[498]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         }
[219]1631      }
1632
1633      //---------------------------------------------------------------
1634
1635      void CNc4DataOutput::writeTimeAxis_
[347]1636                  (CField*    field,
[343]1637                   const boost::shared_ptr<CCalendar> cal)
[219]1638      {
1639         StdOStringStream oss;
[488]1640
[645]1641         if (field->getOperationTimeType() == func::CFunctor::once) return ;
[488]1642
[449]1643//         oss << "time_" << field->operation.getValue()
1644//             << "_" << field->getRelFile()->output_freq.getValue();
[488]1645
[449]1646//         StdString axisid = oss.str();
[645]1647//         if (field->getOperationTimeType() == func::CFunctor::centered) axisid="time_centered" ;
1648//         else if (field->getOperationTimeType() == func::CFunctor::instant) axisid="time_instant" ;
[219]1649
[488]1650         StdString axisid("time_centered") ;
1651         StdString axisBoundId("time_centered_bounds");
1652         StdString timeid("time_counter");
[614]1653         StdString timeBoundId("axis_nbounds");
[488]1654
[645]1655         if (field->getOperationTimeType() == func::CFunctor::instant)
[488]1656         {
1657            axisid = "time_instant";
1658            axisBoundId = "time_instant_bounds";
1659         }
1660
[498]1661         try
[219]1662         {
[498]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);
[488]1669
[498]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           }
[219]1681
[498]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           }
[488]1690
[692]1691           if (field->file->time_counter != CFile::time_counter_attr::none)
[498]1692           {
[692]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);
[488]1702
[692]1703                if (field->file->time_counter != CFile::time_counter_attr::record)
1704                {
1705                  CDate timeOrigin = cal->getTimeOrigin();
1706                  StdString strTimeOrigin = timeOrigin.toString();
[498]1707
[692]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             }
[498]1725           }
[488]1726         }
[498]1727         catch (CNetCdfException& e)
[488]1728         {
[498]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);
[488]1736         }
[219]1737      }
1738
1739      //---------------------------------------------------------------
[488]1740
[219]1741      void CNc4DataOutput::writeTimeAxisAttributes(const StdString & axis_name,
1742                                                   const StdString & calendar,
1743                                                   const StdString & units,
1744                                                   const StdString & time_origin,
[488]1745                                                   const StdString & time_bounds,
[219]1746                                                   const StdString & standard_name,
[613]1747                                                   const StdString & long_name)
[219]1748      {
[498]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, \
[613]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);
[498]1772         }
[219]1773      }
[488]1774
[219]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      {
[498]1784         try
1785         {
[613]1786          if (!axis.empty())
1787            SuperClassWriter::addAttribute("axis"       , axis         , &axis_name);
1788
[498]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, \
[613]1802                                                      const StdString & axis, \
1803                                                      const StdString & standard_name, \
1804                                                      const StdString & long_name, \
1805                                                      const StdString & units, \
1806                                                      const StdString & nav_model)", << msg);
[498]1807         }
[219]1808      }
1809
1810      //---------------------------------------------------------------
[488]1811
[219]1812      void CNc4DataOutput::writeLocalAttributes
1813         (int ibegin, int ni, int jbegin, int nj, StdString domid)
1814      {
[498]1815        try
1816        {
[318]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);
[498]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
[219]1833      }
1834
[628]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)
[391]1837      {
1838         CArray<int,1> array(2) ;
1839
[498]1840         try
1841         {
1842           SuperClassWriter::addAttribute("DOMAIN_number_total",size ) ;
1843           SuperClassWriter::addAttribute("DOMAIN_number", rank) ;
[629]1844           array = SuperClassWriter::getDimension(dimXid) + 1, SuperClassWriter::getDimension(dimYid) + 1;
[498]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         {
[628]1867           StdString msg("On writing Local Attributes IOIPSL \n");
[498]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         }
[391]1875      }
[219]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      {
[498]1884         try
1885         {
1886           SuperClassWriter::addAttribute("name"       , name);
1887           SuperClassWriter::addAttribute("description", description);
[613]1888           SuperClassWriter::addAttribute("title"      , description);
1889           SuperClassWriter::addAttribute("Conventions", conventions);
[498]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         }
[219]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      {
[498]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         }
[219]1939      }
1940
1941      ///--------------------------------------------------------------
1942
[707]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      }
[335]1969} // namespace xios
Note: See TracBrowser for help on using the repository browser.