source: XIOS/trunk/src/node/file.cpp @ 609

Last change on this file since 609 was 609, checked in by rlacroix, 9 years ago

Improve CF compliance: add a new axis attribute "bounds".

Fixes ticket #67.

  • 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
File size: 32.9 KB
Line 
1#include "file.hpp"
2
3#include "attribute_template.hpp"
4#include "object_template.hpp"
5#include "group_template.hpp"
6#include "object_factory.hpp"
7#include "context.hpp"
8#include "context_server.hpp"
9#include "nc4_data_output.hpp"
10#include "nc4_data_input.hpp"
11#include "calendar_util.hpp"
12#include "date.hpp"
13#include "message.hpp"
14#include "type.hpp"
15#include "xios_spl.hpp"
16#include "context_client.hpp"
17#include "mpi.hpp"
18
19namespace xios {
20
21   /// ////////////////////// Définitions ////////////////////// ///
22
23   CFile::CFile(void)
24      : CObjectTemplate<CFile>(), CFileAttributes()
25      , vFieldGroup(), data_out(), enabledFields(), fileComm(MPI_COMM_NULL)
26   {
27     setVirtualFieldGroup() ;
28     setVirtualVariableGroup() ;
29   }
30
31   CFile::CFile(const StdString & id)
32      : CObjectTemplate<CFile>(id), CFileAttributes()
33      , vFieldGroup(), data_out(), enabledFields(), fileComm(MPI_COMM_NULL)
34    {
35      setVirtualFieldGroup() ;
36      setVirtualVariableGroup() ;
37    }
38
39   CFile::~CFile(void)
40   { /* Ne rien faire de plus */ }
41
42   ///---------------------------------------------------------------
43  //! Get name of file
44   StdString CFile::GetName(void)   { return (StdString("file")); }
45   StdString CFile::GetDefName(void){ return (CFile::GetName()); }
46   ENodeType CFile::GetType(void)   { return (eFile); }
47
48   //----------------------------------------------------------------
49   /*!
50   \brief Get data writer object.
51   Each enabled file in xml represents a physical netcdf file.
52   This function allows to access the data writer object.
53   \return data writer object.
54   */
55   boost::shared_ptr<CDataOutput> CFile::getDataOutput(void) const
56   {
57      return data_out;
58   }
59
60   /*!
61   \brief Get data reader object.
62   Each enabled file in xml represents a physical netcdf file.
63   This function allows to access the data reader object.
64   \return data reader object.
65   */
66   boost::shared_ptr<CDataInput> CFile::getDataInput(void) const
67   {
68      return data_in;
69   }
70
71   /*!
72   \brief Get virtual field group
73      In each file, there always exists a field group which is the ancestor of all
74   fields in the file. This is considered be virtual because it is created automatically during
75   file initialization and it normally doesn't appear on xml file
76   \return Pointer to field group
77   */
78   CFieldGroup* CFile::getVirtualFieldGroup(void) const
79   {
80      return (this->vFieldGroup);
81   }
82
83   /*!
84   \brief Get virtual variable group
85      In each file, there always exists a variable group which is the ancestor of all
86   variable in the file. This is considered be virtual because it is created automatically during
87   file initialization and it normally doesn't appear on xml file
88   \return Pointer to variable group
89   */
90   CVariableGroup* CFile::getVirtualVariableGroup(void) const
91   {
92      return (this->vVariableGroup);
93   }
94
95   //! Get all fields of a file
96   std::vector<CField*> CFile::getAllFields(void) const
97   {
98      return (this->vFieldGroup->getAllChildren());
99   }
100
101   //! Get all variables of a file
102   std::vector<CVariable*> CFile::getAllVariables(void) const
103   {
104      return (this->vVariableGroup->getAllChildren());
105   }
106
107   //----------------------------------------------------------------
108   /*!
109   \brief Get all enabled fields of file
110      A field is considered to be enabled if it fullfil these conditions: it is enabled, inside a enabled file
111   and its own level is not larger than file output level.
112   \param [in] default_outputlevel default value output level of file
113   \param [in] default_level default value level of field
114   \param [in] default_enabled flag determine by default if field is enabled
115   \return Vector of pointers of enabled fields
116   */
117   std::vector<CField*> CFile::getEnabledFields(int default_outputlevel,
118                                                int default_level,
119                                                bool default_enabled)
120   {
121      if (!this->enabledFields.empty())
122         return (this->enabledFields);
123
124      const int _outputlevel =
125         (!output_level.isEmpty()) ? output_level.getValue() : default_outputlevel;
126      std::vector<CField*>::iterator it;
127      this->enabledFields = this->getAllFields();
128
129      std::vector<CField*> newEnabledFields;
130
131      for ( it = this->enabledFields.begin() ; it != this->enabledFields.end(); it++ )
132      {
133         if (!(*it)->enabled.isEmpty()) // Si l'attribut 'enabled' est défini ...
134         {
135            if (! (*it)->enabled.getValue()) continue;
136//            { it--; this->enabledFields.erase(it+1); continue; }
137         }
138         else // Si l'attribut 'enabled' n'est pas défini ...
139         {
140            if (!default_enabled) continue ;
141//            { it--; this->enabledFields.erase(it+1); continue; }
142         }
143
144         if (!(*it)->level.isEmpty()) // Si l'attribut 'level' est défini ...
145         {
146            if ((*it)->level.getValue() > _outputlevel) continue ;
147//            { it--; this->enabledFields.erase(it+1); continue; }
148         }
149         else // Si l'attribut 'level' n'est pas défini ...
150         {
151            if (default_level > _outputlevel) continue ;
152//            { it--; this->enabledFields.erase(it+1); continue; }
153         }
154
155//         CField* field_tmp=(*it).get() ;
156//         shared_ptr<CField> sptfield=*it ;
157//         field_tmp->refObject.push_back(sptfield) ;
158         newEnabledFields.push_back(*it) ;
159         // Le champ est finalement actif, on y ajoute sa propre reference.
160//         (*it)->refObject.push_back(*it);
161         // Le champ est finalement actif, on y ajoute la référence au champ de base.
162         (*it)->setRelFile(CFile::get(this));
163//         (*it)->baseRefObject->refObject.push_back(*it);
164         // A faire, ajouter les references intermediaires...
165      }
166      enabledFields=newEnabledFields ;
167
168      return (this->enabledFields);
169   }
170
171   //----------------------------------------------------------------
172   //! Change virtual field group to a new one
173   void CFile::setVirtualFieldGroup(CFieldGroup* newVFieldGroup)
174   {
175      this->vFieldGroup = newVFieldGroup;
176   }
177
178   //! Change virtual variable group to new one
179   void CFile::setVirtualVariableGroup(CVariableGroup* newVVariableGroup)
180   {
181      this->vVariableGroup = newVVariableGroup;
182   }
183
184   //----------------------------------------------------------------
185   //! Create virtual field group, which is done normally on initializing file
186   void CFile::setVirtualFieldGroup(void)
187   {
188      this->setVirtualFieldGroup(CFieldGroup::create());
189   }
190
191   //! Create virtual variable group, which is done normally on initializing file
192   void CFile::setVirtualVariableGroup(void)
193   {
194      this->setVirtualVariableGroup(CVariableGroup::create());
195   }
196
197   //----------------------------------------------------------------
198   bool CFile::isSyncTime(void)
199   {
200     CContext* context = CContext::getCurrent() ;
201     const CDate& currentDate = context->calendar->getCurrentDate() ;
202     if (!sync_freq.isEmpty())
203     {
204       if (*lastSync + sync_freq.getValue() < currentDate)
205       {
206         *lastSync=currentDate ;
207         return true ;
208        }
209      }
210      return false ;
211    }
212
213   //! Initialize a file in order to write into it
214   void CFile::initFile(void)
215   {
216      CContext* context = CContext::getCurrent() ;
217      const CDate& currentDate = context->calendar->getCurrentDate() ;
218      CContextServer* server=context->server ;
219
220      lastSync=new CDate(currentDate) ;
221      lastSplit=new CDate(currentDate) ;
222      isOpen=false ;
223
224      allDomainEmpty=true ;
225
226      set<CAxis*> setAxis;
227      set<CDomain*> setDomains;
228
229      std::vector<CField*>::iterator it, end = this->enabledFields.end();
230      for (it = this->enabledFields.begin(); it != end; it++)
231      {
232         CField* field = *it;
233         allDomainEmpty &= !field->grid->doGridHaveDataToWrite();
234         std::vector<CAxis*> vecAxis = field->grid->getAxis();
235         for (size_t i = 0; i < vecAxis.size(); ++i)
236            setAxis.insert(vecAxis[i]);
237         std::vector<CDomain*> vecDomains = field->grid->getDomains();
238         for (size_t i = 0; i < vecDomains.size(); ++i)
239            setDomains.insert(vecDomains[i]);
240      }
241      nbAxis = setAxis.size();
242      nbDomains = setDomains.size();
243
244      // create sub communicator for file
245      int color=allDomainEmpty?0:1 ;
246      MPI_Comm_split(server->intraComm,color,server->intraCommRank,&fileComm) ;
247      if (allDomainEmpty) MPI_Comm_free(&fileComm) ;
248      //
249
250    }
251
252    //! Verify state of a file
253    void CFile::checkFile(void)
254    {
255      if (mode.isEmpty() || mode.getValue() == mode_attr::write)
256      {
257        if (!isOpen) createHeader();
258        checkSync();
259      }
260      else
261      {
262        if (!isOpen) openInReadMode();
263      }
264      checkSplit();
265    }
266
267    /*!
268    \brief Verify if synchronisation should be done
269        If syn option is enabled, syn frequence and current time will be used to
270    calculate the moment to syn file(s)
271    \return True if it is the moment to synchronize file, otherwise false
272    */
273   bool CFile::checkSync(void)
274   {
275     CContext* context = CContext::getCurrent() ;
276     const CDate& currentDate = context->calendar->getCurrentDate() ;
277     if (!sync_freq.isEmpty())
278     {
279       if (*lastSync + sync_freq.getValue() <= currentDate)
280       {
281         *lastSync=currentDate ;
282         data_out->syncFile() ;
283         return true ;
284        }
285      }
286      return false ;
287    }
288
289    /*!
290    \brief Verify if splitting should be done
291        If split option is enabled, split frequence and current time will be used to
292    calculate the moment to split file
293    \return True if it is the moment to split file, otherwise false
294    */
295    bool CFile::checkSplit(void)
296    {
297      CContext* context = CContext::getCurrent() ;
298      const CDate& currentDate = context->calendar->getCurrentDate() ;
299      if (!split_freq.isEmpty())
300      {
301        if (currentDate > *lastSplit + split_freq.getValue())
302        {
303          *lastSplit = *lastSplit + split_freq.getValue();
304          std::vector<CField*>::iterator it, end = this->enabledFields.end();
305          for (it = this->enabledFields.begin(); it != end; it++)
306          {
307            (*it)->resetNStep();
308            (*it)->resetNStepMax();
309          }
310          if (mode.isEmpty() || mode.getValue() == mode_attr::write)
311            createHeader() ;
312          else
313            openInReadMode();
314          return true ;
315        }
316      }
317      return false ;
318    }
319
320   /*!
321   \brief Create header of netcdf file
322   There are some information to fill in header of each netcdf.
323   */
324   void CFile::createHeader(void)
325   {
326      CContext* context = CContext::getCurrent() ;
327      CContextServer* server=context->server ;
328
329      if (!allDomainEmpty)
330      {
331         StdString filename = (!name.isEmpty()) ?   name.getValue() : getId();
332         StdOStringStream oss;
333         oss << filename;
334         if (!name_suffix.isEmpty()) oss << name_suffix.getValue();
335
336         if (!split_freq.isEmpty())
337         {
338           string splitFormat ;
339           if (split_freq_format.isEmpty())
340           {
341             if (split_freq.getValue().second != 0) splitFormat = "%y%mo%d%h%mi%s";
342             else if (split_freq.getValue().minute != 0) splitFormat = "%y%mo%d%h%mi";
343             else if (split_freq.getValue().hour != 0) splitFormat = "%y%mo%d%h";
344             else if (split_freq.getValue().day != 0) splitFormat = "%y%mo%d";
345             else if (split_freq.getValue().month != 0) splitFormat = "%y%mo";
346             else splitFormat = "%y";
347           }
348           else splitFormat=split_freq_format ;
349           oss << "_" << lastSplit->getStr(splitFormat)
350               << "-" << (*lastSplit + split_freq.getValue() - 1 * Second).getStr(splitFormat);
351         }
352
353        bool append = !this->append.isEmpty() && this->append.getValue();
354
355         bool useClassicFormat = !format.isEmpty() && format == format_attr::netcdf4_classic;
356
357         bool multifile=true ;
358         if (!type.isEmpty())
359         {
360           if (type==type_attr::one_file) multifile=false ;
361           else if (type==type_attr::multiple_file) multifile=true ;
362
363         }
364#ifndef USING_NETCDF_PAR
365         if (!multifile)
366         {
367            info(0)<<"!!! Warning -> Using non parallel version of netcdf, switching in multiple_file mode for file : "<<filename<<" ..."<<endl ;
368            multifile=true ;
369          }
370#endif
371         if (multifile)
372         {
373            int commSize, commRank ;
374            MPI_Comm_size(fileComm,&commSize) ;
375            MPI_Comm_rank(fileComm,&commRank) ;
376
377            if (server->intraCommSize > 1)
378            {
379              oss << "_"  ;
380              int width=0 ; int n=commSize-1 ;
381              while(n != 0) { n=n/10 ; width++ ;}
382              if (!min_digits.isEmpty())
383                if (width<min_digits) width=min_digits ;
384              oss.width(width) ;
385              oss.fill('0') ;
386              oss<<right<< commRank;
387            }
388         }
389         oss << ".nc";
390
391         bool isCollective = par_access.isEmpty() ||  par_access == par_access_attr::collective;
392
393         if (isOpen) data_out->closeFile();
394
395        data_out = shared_ptr<CDataOutput>(new CNc4DataOutput(oss.str(), append, useClassicFormat,
396                                                              fileComm, multifile, isCollective));
397        isOpen = true;
398
399        data_out->writeFile(CFile::get(this));
400
401        // Do not recreate the file structure if opening an existing file
402        if (!data_out->IsInAppendMode())
403        {
404          std::vector<CField*>::iterator it, end = this->enabledFields.end();
405          for (it = this->enabledFields.begin(); it != end; it++)
406          {
407            CField* field = *it;
408            this->data_out->writeFieldGrid(field);
409          }
410          this->data_out->writeTimeDimension();
411
412          for (it = this->enabledFields.begin(); it != end; it++)
413          {
414            CField* field = *it;
415            this->data_out->writeField(field);
416          }
417
418          vector<CVariable*> listVars = getAllVariables() ;
419          for (vector<CVariable*>::iterator it = listVars.begin(); it != listVars.end(); it++)
420            this->data_out->writeAttribute(*it);
421
422          this->data_out->definition_end();
423        }
424      }
425   }
426
427  /*!
428  \brief Open an existing NetCDF file in read-only mode
429  */
430  void CFile::openInReadMode(void)
431  {
432    CContext* context = CContext::getCurrent();
433    CContextServer* server=context->server;
434
435    if (!allDomainEmpty)
436    {
437      StdString filename = (!name.isEmpty()) ? name.getValue() : getId();
438      StdOStringStream oss;
439      oss << filename;
440      if (!name_suffix.isEmpty()) oss << name_suffix.getValue();
441
442      if (!split_freq.isEmpty())
443      {
444        string splitFormat;
445        if (split_freq_format.isEmpty())
446        {
447          if (split_freq.getValue().second != 0) splitFormat = "%y%mo%d%h%mi%s";
448          else if (split_freq.getValue().minute != 0) splitFormat = "%y%mo%d%h%mi";
449          else if (split_freq.getValue().hour != 0) splitFormat = "%y%mo%d%h";
450          else if (split_freq.getValue().day != 0) splitFormat = "%y%mo%d";
451          else if (split_freq.getValue().month != 0) splitFormat = "%y%mo";
452          else splitFormat = "%y";
453        }
454        else splitFormat=split_freq_format;
455        oss << "_" << lastSplit->getStr(splitFormat)
456        << "-" << (*lastSplit + split_freq.getValue() - 1 * Second).getStr(splitFormat);
457      }
458
459      bool multifile = true;
460      if (!type.isEmpty())
461      {
462        if (type == type_attr::one_file) multifile = false;
463        else if (type == type_attr::multiple_file) multifile = true;
464      }
465  #ifndef USING_NETCDF_PAR
466      if (!multifile)
467      {
468        info(0) << "!!! Warning -> Using non parallel version of netcdf, switching in multiple_file mode for file : " << filename << " ..." << endl;
469        multifile = true;
470      }
471  #endif
472      if (multifile)
473      {
474        int commSize, commRank;
475        MPI_Comm_size(fileComm, &commSize);
476        MPI_Comm_rank(fileComm, &commRank);
477
478        if (server->intraCommSize > 1)
479        {
480          oss << "_" ;
481          int width = 0, n = commSize - 1;
482          while (n != 0) { n = n / 10; width++; }
483          if (!min_digits.isEmpty() && width < min_digits)
484            width = min_digits;
485          oss.width(width);
486          oss.fill('0');
487          oss << right << commRank;
488        }
489      }
490      oss << ".nc";
491
492      bool isCollective = par_access.isEmpty() || par_access == par_access_attr::collective;
493
494      if (isOpen) data_out->closeFile();
495
496      data_in = shared_ptr<CDataInput>(new CNc4DataInput(oss.str(), fileComm, multifile, isCollective));
497      isOpen = true;
498    }
499  }
500
501   //! Close file
502   void CFile::close(void)
503   {
504     delete lastSync ;
505     delete lastSplit ;
506     if (!allDomainEmpty)
507       if (isOpen)
508       {
509         if (mode.isEmpty() || mode.getValue() == mode_attr::write)
510          this->data_out->closeFile();
511         else
512          this->data_in->closeFile();
513       }
514      if (fileComm != MPI_COMM_NULL) MPI_Comm_free(&fileComm) ;
515   }
516   //----------------------------------------------------------------
517
518   /*!
519   \brief Parse xml file and write information into file object
520   \param [in] node xmld node corresponding in xml file
521   */
522   void CFile::parse(xml::CXMLNode & node)
523   {
524      SuperClass::parse(node);
525
526      if (node.goToChildElement())
527      {
528        do
529        {
530           if (node.getElementName()=="field" || node.getElementName()=="field_group") this->getVirtualFieldGroup()->parseChild(node);
531           else if (node.getElementName()=="variable" || node.getElementName()=="variable_group") this->getVirtualVariableGroup()->parseChild(node);
532        } while (node.goToNextElement()) ;
533        node.goToParentElement();
534      }
535
536   }
537   //----------------------------------------------------------------
538
539   /*!
540   \brief Represent a file in form of string with all its info
541   \return String
542   */
543   StdString CFile::toString(void) const
544   {
545      StdOStringStream oss;
546
547      oss << "<" << CFile::GetName() << " ";
548      if (this->hasId())
549         oss << " id=\"" << this->getId() << "\" ";
550      oss << SuperClassAttribute::toString() << ">" << std::endl;
551      if (this->getVirtualFieldGroup() != NULL)
552         oss << *this->getVirtualFieldGroup() << std::endl;
553      oss << "</" << CFile::GetName() << " >";
554      return (oss.str());
555   }
556
557   //----------------------------------------------------------------
558
559   /*!
560   \brief Find all inheritace among objects in a file.
561   \param [in] apply (true) write attributes of parent into ones of child if they are empty
562                     (false) write attributes of parent into a new container of child
563   \param [in] parent
564   */
565   void CFile::solveDescInheritance(bool apply, const CAttributeMap * const parent)
566   {
567      SuperClassAttribute::setAttributes(parent,apply);
568      this->getVirtualFieldGroup()->solveDescInheritance(apply, NULL);
569      this->getVirtualVariableGroup()->solveDescInheritance(apply, NULL);
570   }
571
572   //----------------------------------------------------------------
573
574//   void CFile::processEnabledFile(void)
575//   {
576//     if (output_freq.isEmpty()) ERROR("void CFile::processEnabledFile(void)",
577//                                       <<"File attribute <<output_freq>> is undefined");
578//     solveFieldRefInheritance(true) ;
579//     getEnabledFields() ;
580//     processEnabledFields() ;
581//   }
582
583//   void CFile::processEnabledFields(void)
584//   {
585//      for (unsigned int i = 0; i < this->enabledFields.size(); i++)
586//      {
587//        this->enabledFields[i]->processEnabledField() ;
588//      }
589//   }
590
591   /*!
592   \brief Resolve all reference of active fields.
593      In order to know exactly which data each active field has, a search for all its
594   reference to find its parents or/and its base reference object must be done. Moreover
595   during this search, there are some information that can only be sent to server AFTER
596   all information of active fields are created on server side, e.g: checking mask or index
597   \param [in] sendToServer: Send all info to server (true) or only a part of it (false)
598   */
599   void CFile::solveAllRefOfEnabledFields(bool sendToServer)
600   {
601     int size = this->enabledFields.size();
602     for (int i = 0; i < size; ++i)
603     {
604       this->enabledFields[i]->solveAllReferenceEnabledField(sendToServer);
605     }
606   }
607
608   /*!
609   \brief Contruct all expression related to active fields.
610      Each field can do some expressions which appear on the xml file, and itself can be
611   a result of an expression among some other fields. This function builds all possible expression
612   relating to active fields.
613   */
614   void CFile::buildAllExpressionOfEnabledFields()
615   {
616     int size = this->enabledFields.size();
617     for (int i = 0; i < size; ++i)
618     {
619       this->enabledFields[i]->buildAllExpressionEnabledField();
620     }
621   }
622
623   /*!
624     Prefetching the data for enabled fields read from file.
625   */
626   void CFile::prefetchEnabledReadModeFields(void)
627   {
628     if (mode.isEmpty() || mode.getValue() != mode_attr::read)
629       return;
630
631     int size = this->enabledFields.size();
632     for (int i = 0; i < size; ++i)
633       this->enabledFields[i]->sendReadDataRequest();
634   }
635
636   /*!
637     Prefetching the data for enabled fields read from file whose data is out-of-date.
638   */
639   void CFile::prefetchEnabledReadModeFieldsIfNeeded(void)
640   {
641     if (mode.isEmpty() || mode.getValue() != mode_attr::read)
642       return;
643
644     int size = this->enabledFields.size();
645     for (int i = 0; i < size; ++i)
646       this->enabledFields[i]->sendReadDataRequestIfNeeded();
647   }
648
649   void CFile::solveFieldRefInheritance(bool apply)
650   {
651      // Résolution des héritages par référence de chacun des champs contenus dans le fichier.
652      std::vector<CField*> allF = this->getAllFields();
653      for (unsigned int i = 0; i < allF.size(); i++)
654         allF[i]->solveRefInheritance(apply);
655   }
656
657   //----------------------------------------------------------------
658
659   void CFile::solveEFGridRef(void)
660   {
661      for (unsigned int i = 0; i < this->enabledFields.size(); i++)
662         this->enabledFields[i]->solveGridReference();
663   }
664
665   //----------------------------------------------------------------
666
667   void CFile::solveEFOperation(void)
668   {
669      for (unsigned int i = 0; i < this->enabledFields.size(); i++)
670         this->enabledFields[i]->solveOperation();
671   }
672
673   void CFile::solveEFExpression(void)
674   {
675      for (unsigned int i = 0; i < this->enabledFields.size(); i++)
676         this->enabledFields[i]->buildExpression();
677   }
678
679   /*!
680   \brief Add a field into file.
681      A field is added into file and it will be written out if the file is enabled and
682   level of this field is smaller than level_output. A new field won't be created if one
683   with id has already existed
684   \param [in] id String identity of new field
685   \return Pointer to added (or already existed) field
686   */
687   CField* CFile::addField(const string& id)
688   {
689     return vFieldGroup->createChild(id) ;
690   }
691
692   /*!
693   \brief Add a field group into file.
694      A field group is added into file and it will play a role as parents for fields.
695   A new field group won't be created if one with id has already existed
696   \param [in] id String identity of new field group
697   \return Pointer to added (or already existed) field group
698   */
699   CFieldGroup* CFile::addFieldGroup(const string& id)
700   {
701     return vFieldGroup->createChildGroup(id) ;
702   }
703
704   /*!
705   \brief Add a variable into file.
706      A variable is added into file and if one with id has already existed, pointer to
707   it will be returned.
708      Variable as long as attributes are information container of file.
709   However, whereas attributes are "fixed" information, variables provides a more flexible way to user
710   to fill in (extra) information for a file.
711   \param [in] id String identity of new variable
712   \return Pointer to added (or already existed) variable
713   */
714   CVariable* CFile::addVariable(const string& id)
715   {
716     return vVariableGroup->createChild(id) ;
717   }
718
719   /*!
720   \brief Add a variable group into file.
721      A variable group is added into file and it will play a role as parents for variables.
722   A new variable group won't be created if one with id has already existed
723   \param [in] id String identity of new variable group
724   \return Pointer to added (or already existed) variable group
725   */
726   CVariableGroup* CFile::addVariableGroup(const string& id)
727   {
728     return vVariableGroup->createChildGroup(id) ;
729   }
730
731   /*!
732   \brief Send a message to create a field on server side
733   \param[in] id String identity of field that will be created on server
734   */
735   void CFile::sendAddField(const string& id)
736   {
737    CContext* context=CContext::getCurrent() ;
738
739    if (! context->hasServer )
740    {
741       CContextClient* client=context->client ;
742
743       CEventClient event(this->getType(),EVENT_ID_ADD_FIELD) ;
744       if (client->isServerLeader())
745       {
746         CMessage msg ;
747         msg<<this->getId() ;
748         msg<<id ;
749         const std::list<int>& ranks = client->getRanksServerLeader();
750         for (std::list<int>::const_iterator itRank = ranks.begin(), itRankEnd = ranks.end(); itRank != itRankEnd; ++itRank)
751           event.push(*itRank,1,msg);
752         client->sendEvent(event) ;
753       }
754       else client->sendEvent(event) ;
755    }
756
757   }
758
759   /*!
760   \brief Send a message to create a field group on server side
761   \param[in] id String identity of field group that will be created on server
762   */
763   void CFile::sendAddFieldGroup(const string& id)
764   {
765    CContext* context=CContext::getCurrent() ;
766    if (! context->hasServer )
767    {
768       CContextClient* client=context->client ;
769
770       CEventClient event(this->getType(),EVENT_ID_ADD_FIELD_GROUP) ;
771       if (client->isServerLeader())
772       {
773         CMessage msg ;
774         msg<<this->getId() ;
775         msg<<id ;
776         const std::list<int>& ranks = client->getRanksServerLeader();
777         for (std::list<int>::const_iterator itRank = ranks.begin(), itRankEnd = ranks.end(); itRank != itRankEnd; ++itRank)
778           event.push(*itRank,1,msg);
779         client->sendEvent(event) ;
780       }
781       else client->sendEvent(event) ;
782    }
783
784   }
785
786   /*!
787   \brief Receive a message annoucing the creation of a field on server side
788   \param[in] event Received event
789   */
790   void CFile::recvAddField(CEventServer& event)
791   {
792
793      CBufferIn* buffer=event.subEvents.begin()->buffer;
794      string id;
795      *buffer>>id ;
796      get(id)->recvAddField(*buffer) ;
797   }
798
799   /*!
800   \brief Receive a message annoucing the creation of a field on server side
801   \param[in] buffer Buffer containing message
802   */
803   void CFile::recvAddField(CBufferIn& buffer)
804   {
805      string id ;
806      buffer>>id ;
807      addField(id) ;
808   }
809
810   /*!
811   \brief Receive a message annoucing the creation of a field group on server side
812   \param[in] event Received event
813   */
814   void CFile::recvAddFieldGroup(CEventServer& event)
815   {
816
817      CBufferIn* buffer=event.subEvents.begin()->buffer;
818      string id;
819      *buffer>>id ;
820      get(id)->recvAddFieldGroup(*buffer) ;
821   }
822
823   /*!
824   \brief Receive a message annoucing the creation of a field group on server side
825   \param[in] buffer Buffer containing message
826   */
827   void CFile::recvAddFieldGroup(CBufferIn& buffer)
828   {
829      string id ;
830      buffer>>id ;
831      addFieldGroup(id) ;
832   }
833
834   /*!
835   \brief Send messages to duplicate all variables on server side
836      Because each variable has also its attributes. So first thing to do is replicate
837   all these attributes on server side. Because variable can have a value, the second thing
838   is to duplicate this value on server, too.
839   */
840   void CFile::sendAddAllVariables()
841   {
842     if (!getAllVariables().empty())
843     {
844       // Firstly, it's necessary to add virtual variable group
845       sendAddVariableGroup(getVirtualVariableGroup()->getId());
846
847       // Okie, now we can add to this variable group
848       std::vector<CVariable*> allVar = getAllVariables();
849       std::vector<CVariable*>::const_iterator it = allVar.begin();
850       std::vector<CVariable*>::const_iterator itE = allVar.end();
851
852       for (; it != itE; ++it)
853       {
854         this->sendAddVariable((*it)->getId());
855         (*it)->sendAllAttributesToServer();
856         (*it)->sendValue();
857       }
858     }
859   }
860
861   /*!
862   \brief Send a message to create a variable on server side
863      A variable always belongs to a variable group
864   \param[in] id String identity of variable that will be created on server
865   */
866   void CFile::sendAddVariable(const string& id)
867   {
868    CContext* context=CContext::getCurrent() ;
869
870    if (! context->hasServer )
871    {
872       CContextClient* client=context->client ;
873
874       CEventClient event(this->getType(),EVENT_ID_ADD_VARIABLE) ;
875       if (client->isServerLeader())
876       {
877         CMessage msg ;
878         msg<<this->getId() ;
879         msg<<id ;
880         const std::list<int>& ranks = client->getRanksServerLeader();
881         for (std::list<int>::const_iterator itRank = ranks.begin(), itRankEnd = ranks.end(); itRank != itRankEnd; ++itRank)
882           event.push(*itRank,1,msg);
883         client->sendEvent(event) ;
884       }
885       else client->sendEvent(event) ;
886    }
887
888   }
889
890   /*!
891   \brief Send a message to create a variable group on server side
892   \param[in] id String identity of variable group that will be created on server
893   */
894   void CFile::sendAddVariableGroup(const string& id)
895   {
896    CContext* context=CContext::getCurrent() ;
897    if (! context->hasServer )
898    {
899       CContextClient* client=context->client ;
900
901       CEventClient event(this->getType(),EVENT_ID_ADD_VARIABLE_GROUP) ;
902       if (client->isServerLeader())
903       {
904         CMessage msg ;
905         msg<<this->getId() ;
906         msg<<id ;
907         const std::list<int>& ranks = client->getRanksServerLeader();
908         for (std::list<int>::const_iterator itRank = ranks.begin(), itRankEnd = ranks.end(); itRank != itRankEnd; ++itRank)
909           event.push(*itRank,1,msg);
910         client->sendEvent(event) ;
911       }
912       else client->sendEvent(event) ;
913    }
914
915   }
916
917   /*!
918   \brief Receive a message annoucing the creation of a variable on server side
919   \param[in] event Received event
920   */
921   void CFile::recvAddVariable(CEventServer& event)
922   {
923
924      CBufferIn* buffer=event.subEvents.begin()->buffer;
925      string id;
926      *buffer>>id ;
927      get(id)->recvAddVariable(*buffer) ;
928   }
929
930   /*!
931   \brief Receive a message annoucing the creation of a variable on server side
932   \param[in] buffer Buffer containing message
933   */
934   void CFile::recvAddVariable(CBufferIn& buffer)
935   {
936      string id ;
937      buffer>>id ;
938      addVariable(id) ;
939   }
940
941   /*!
942   \brief Receive a message annoucing the creation of a variable group on server side
943   \param[in] event Received event
944   */
945   void CFile::recvAddVariableGroup(CEventServer& event)
946   {
947
948      CBufferIn* buffer=event.subEvents.begin()->buffer;
949      string id;
950      *buffer>>id ;
951      get(id)->recvAddVariableGroup(*buffer) ;
952   }
953
954   /*!
955   \brief Receive a message annoucing the creation of a variable group on server side
956   \param[in] buffer Buffer containing message
957   */
958   void CFile::recvAddVariableGroup(CBufferIn& buffer)
959   {
960      string id ;
961      buffer>>id ;
962      addVariableGroup(id) ;
963   }
964
965   /*!
966     \brief Sending all active (enabled) fields from client to server.
967   Each field is identified uniquely by its string identity. Not only should we
968   send the id to server but also we need to send ids of reference domain and reference axis.
969   With these two id, it's easier to make reference to grid where all data should be written.
970   Remark: This function must be called AFTER all active (enabled) files have been created on the server side
971   */
972   void CFile::sendEnabledFields()
973   {
974     int size = this->enabledFields.size();
975     CField* fieldPtr(0);
976     for (int i = 0; i < size; ++i)
977     {
978       fieldPtr = this->enabledFields[i];
979       if (fieldPtr->name.isEmpty()) fieldPtr->name.setValue(fieldPtr->getBaseFieldReference()->getId());
980       this->sendAddField(fieldPtr->getId());
981       fieldPtr->sendAllAttributesToServer();
982       fieldPtr->sendAddAllVariables();
983     }
984   }
985
986   /*!
987   \brief Dispatch event received from client
988      Whenever a message is received in buffer of server, it will be processed depending on
989   its event type. A new event type should be added in the switch list to make sure
990   it processed on server side.
991   \param [in] event: Received message
992   */
993   bool CFile::dispatchEvent(CEventServer& event)
994   {
995      if (SuperClass::dispatchEvent(event)) return true ;
996      else
997      {
998        switch(event.type)
999        {
1000           case EVENT_ID_ADD_FIELD :
1001             recvAddField(event) ;
1002             return true ;
1003             break ;
1004
1005           case EVENT_ID_ADD_FIELD_GROUP :
1006             recvAddFieldGroup(event) ;
1007             return true ;
1008             break ;
1009
1010            case EVENT_ID_ADD_VARIABLE :
1011             recvAddVariable(event) ;
1012             return true ;
1013             break ;
1014
1015           case EVENT_ID_ADD_VARIABLE_GROUP :
1016             recvAddVariableGroup(event) ;
1017             return true ;
1018             break ;
1019           default :
1020              ERROR("bool CFile::dispatchEvent(CEventServer& event)", <<"Unknown Event") ;
1021           return false ;
1022        }
1023      }
1024   }
1025
1026
1027
1028
1029   ///---------------------------------------------------------------
1030
1031} // namespace xios
Note: See TracBrowser for help on using the repository browser.