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

Last change on this file since 1358 was 1358, checked in by rlacroix, 6 years ago

Support reentrant workflows and workflows with temporal integration for fields read from files.

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