source: XIOS/trunk/src/node/field.cpp @ 540

Last change on this file since 540 was 540, checked in by mhnguyen, 10 years ago

Reorganizing structure of grid

+) Grid doesn't have domain_ref and axis_ref anymore.
All domain and axis of a grid must be inside grid (These domain and axis can be defined or refer to others)
+) Grid contains list of domain and axis
+) Reorder some functions to make sure new functionlities work

Test
+) On Curie
+) Mode attached and detached
+) Only test_new_features
+) Passed

  • 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: 31.9 KB
RevLine 
[219]1#include "field.hpp"
2
[352]3#include "attribute_template.hpp"
4#include "object_template.hpp"
5#include "group_template.hpp"
[219]6
7#include "node_type.hpp"
8#include "calendar_util.hpp"
[352]9#include "message.hpp"
10#include "xmlioserver_spl.hpp"
11#include "type.hpp"
12#include "context_client.hpp"
[459]13#include <set>
[219]14
[335]15namespace xios{
[509]16
[219]17   /// ////////////////////// Définitions ////////////////////// ///
18
19   CField::CField(void)
20      : CObjectTemplate<CField>(), CFieldAttributes()
21      , refObject(), baseRefObject()
22      , grid(), file()
23      , freq_operation(), freq_write()
[266]24      , nstep(0)
[219]25      , last_Write(), last_operation()
[459]26      , foperation(), hasInstantData(false), hasExpression(false)
[460]27      , active(false) , hasOutputFile(false),hasFieldOut(false), slotUpdateDate(NULL)
[509]28      , processed(false), domAxisIds_("",""), areAllReferenceSolved(false), areAllExpressionBuilt(false)
[472]29      { setVirtualVariableGroup() ; }
[219]30
31   CField::CField(const StdString & id)
32      : CObjectTemplate<CField>(id), CFieldAttributes()
33      , refObject(), baseRefObject()
34      , grid(), file()
35      , freq_operation(), freq_write()
[266]36      , nstep(0)
[219]37      , last_Write(), last_operation()
[466]38      , foperation(), hasInstantData(false), hasExpression(false)
[460]39      , active(false), hasOutputFile(false), hasFieldOut(false), slotUpdateDate(NULL)
[509]40      , processed(false), domAxisIds_("",""), areAllReferenceSolved(false), areAllExpressionBuilt(false)
[472]41   { setVirtualVariableGroup() ; }
[219]42
43   CField::~CField(void)
44   {
[347]45//      this->grid.reset() ;
46//      this->file.reset() ;
[219]47      this->foperation.reset() ;
[459]48      if (hasExpression) delete expression ;
49      if (slotUpdateDate!=NULL) delete slotUpdateDate ;
[509]50
[219]51   }
52
[472]53
54  //----------------------------------------------------------------
55
56   void CField::setVirtualVariableGroup(CVariableGroup* newVVariableGroup)
[509]57   {
58      this->vVariableGroup = newVVariableGroup;
[472]59   }
[509]60
[472]61   void CField::setVirtualVariableGroup(void)
62   {
63      this->setVirtualVariableGroup(CVariableGroup::create());
64   }
[509]65
[472]66   CVariableGroup* CField::getVirtualVariableGroup(void) const
67   {
68      return (this->vVariableGroup);
69   }
70
[509]71
[472]72   std::vector<CVariable*> CField::getAllVariables(void) const
73   {
74      return (this->vVariableGroup->getAllChildren());
75   }
[509]76
[472]77   void CField::solveDescInheritance(bool apply, const CAttributeMap * const parent)
78   {
79      SuperClassAttribute::setAttributes(parent,apply);
80      this->getVirtualVariableGroup()->solveDescInheritance(apply, NULL);
81   }
[219]82   //----------------------------------------------------------------
[472]83   //----------------------------------------------------------------
[219]84
85   bool CField::updateDataServer
[343]86      (const CDate & currDate,
[369]87       const std::deque< CArray<double, 1>* > storedClient)
[219]88   {
[343]89      const CDate opeDate      = *last_operation + freq_operation;
[509]90      const CDate writeDate    = *last_Write     + freq_write;
91
[278]92      if (opeDate <= currDate)
[219]93      {
[369]94         if (this->data.numElements() != this->grid->storeIndex[0]->numElements())
[278]95         {
[369]96            this->data.resize(this->grid->storeIndex[0] ->numElements());
[509]97         }
[369]98         CArray<double,1> input(data.numElements()) ;
[509]99         this->grid->inputFieldServer(storedClient, input);
[219]100         (*this->foperation)(input);
101         *last_operation = currDate;
102      }
[278]103      if (writeDate < (currDate + freq_operation))
[219]104      {
[278]105         this->foperation->final();
106         this->incrementNStep();
107         *last_Write = writeDate;
[509]108         return (true);
[219]109      }
110      return (false);
111   }
[509]112
[300]113   bool CField::dispatchEvent(CEventServer& event)
114  {
[509]115
[300]116    if (SuperClass::dispatchEvent(event)) return true ;
117    else
118    {
119      switch(event.type)
120      {
121        case EVENT_ID_UPDATE_DATA :
122          recvUpdateData(event) ;
123          return true ;
124          break ;
[472]125
126            case EVENT_ID_ADD_VARIABLE :
127             recvAddVariable(event) ;
128             return true ;
129             break ;
[509]130
[472]131           case EVENT_ID_ADD_VARIABLE_GROUP :
132             recvAddVariableGroup(event) ;
133             return true ;
[509]134             break ;
135
[300]136        default :
137          ERROR("bool CField::dispatchEvent(CEventServer& event)",<<"Unknown Event") ;
138          return false ;
139      }
140    }
141  }
[509]142
[300]143  void CField::sendUpdateData(void)
144  {
[347]145    CContext* context = CContext::getCurrent() ;
[300]146    CContextClient* client=context->client ;
[509]147
[300]148    CEventClient event(getType(),EVENT_ID_UPDATE_DATA) ;
[509]149
[369]150    map<int,CArray<int, 1>* >::iterator it ;
[300]151    list<shared_ptr<CMessage> > list_msg ;
[369]152    list< CArray<double,1>* > list_data ;
[509]153
[300]154    for(it=grid->storeIndex_toSrv.begin();it!=grid->storeIndex_toSrv.end();it++)
155    {
156      int rank=(*it).first ;
[369]157      CArray<int,1>& index = *(it->second) ;
158      CArray<double,1> data_tmp(index.numElements()) ;
[509]159
[369]160      for(int n=0;n<data_tmp.numElements();n++) data_tmp(n)=data(index(n)) ;
[300]161      list_msg.push_back(shared_ptr<CMessage>(new CMessage)) ;
[369]162      list_data.push_back(new CArray<double,1>(data_tmp)) ;
163      *list_msg.back()<<getId()<<*list_data.back() ;
[300]164      event.push(rank,grid->nbSenders[rank],*list_msg.back()) ;
165    }
166    client->sendEvent(event) ;
[509]167
[369]168    for(list< CArray<double,1>* >::iterator it=list_data.begin();it!=list_data.end();it++) delete *it ;
[300]169  }
[509]170
[300]171  void CField::recvUpdateData(CEventServer& event)
172  {
173    vector<int> ranks ;
174    vector<CBufferIn*> buffers ;
[509]175
[300]176    list<CEventServer::SSubEvent>::iterator it ;
177    string fieldId ;
[219]178
[300]179    for (it=event.subEvents.begin();it!=event.subEvents.end();++it)
180    {
181      int rank=it->rank;
182      CBufferIn* buffer=it->buffer;
183      *buffer>>fieldId ;
184      ranks.push_back(rank) ;
185      buffers.push_back(buffer) ;
186    }
[509]187    get(fieldId)->recvUpdateData(ranks,buffers) ;
[300]188  }
[509]189
[300]190  void  CField::recvUpdateData(vector<int>& ranks, vector<CBufferIn*>& buffers)
191  {
[509]192
[300]193    if (data_srv.empty())
194    {
[369]195      for(map<int, CArray<int, 1>* >::iterator it=grid->out_i_fromClient.begin();it!=grid->out_i_fromClient.end();it++)
[300]196      {
197        int rank=it->first ;
[369]198        CArray<double,1> data_tmp(it->second->numElements()) ;
199        data_srv.insert( pair<int, CArray<double,1>* >(rank, new CArray<double,1>(data_tmp) ) ) ;
200        foperation_srv.insert(pair<int,boost::shared_ptr<func::CFunctor> >(rank,boost::shared_ptr<func::CFunctor>(new func::CInstant(*data_srv[rank])))) ;
[300]201      }
202    }
203
[347]204    CContext* context = CContext::getCurrent() ;
[343]205    const CDate & currDate = context->getCalendar()->getCurrentDate();
206    const CDate opeDate      = *last_operation_srv + freq_operation_srv;
[509]207    const CDate writeDate    = *last_Write_srv     + freq_write_srv;
[300]208
[509]209
210
[300]211    if (opeDate <= currDate)
212    {
213      for(int n=0;n<ranks.size();n++)
214      {
[369]215        CArray<double,1> data_tmp ;
[300]216        *buffers[n]>>data_tmp ;
217        (*foperation_srv[ranks[n]])(data_tmp) ;
218      }
219      *last_operation_srv = currDate;
220    }
[509]221
[300]222    if (writeDate < (currDate + freq_operation_srv))
223    {
224      for(int n=0;n<ranks.size();n++)
225      {
226        this->foperation_srv[ranks[n]]->final();
227      }
[509]228
[300]229      *last_Write_srv = writeDate;
230      writeField() ;
[334]231      *lastlast_Write_srv=*last_Write_srv;
[300]232    }
233  }
[509]234
[300]235  void CField::writeField(void)
236  {
[379]237    if (! getRelFile()->allDomainEmpty )
238      if (! grid->domain->isEmpty() || getRelFile()->type == CFile::type_attr::one_file)
239      {
240        getRelFile()->checkFile();
241        this->incrementNStep();
242        getRelFile()->getDataOutput()->writeFieldData(CField::get(this));
243      }
[300]244  }
[219]245   //----------------------------------------------------------------
246
[347]247   void CField::setRelFile(CFile* _file)
[509]248   {
[459]249      this->file = _file;
[509]250      hasOutputFile=true ;
[219]251   }
252
253   //----------------------------------------------------------------
254
255   StdString CField::GetName(void)   { return (StdString("field")); }
256   StdString CField::GetDefName(void){ return (CField::GetName()); }
257   ENodeType CField::GetType(void)   { return (eField); }
258
259   //----------------------------------------------------------------
260
[347]261   CGrid* CField::getRelGrid(void) const
[509]262   {
263      return (this->grid);
[219]264   }
265
266   //----------------------------------------------------------------
267
[347]268   CFile* CField::getRelFile(void) const
[509]269   {
[219]270      return (this->file);
271   }
[509]272
[266]273   StdSize CField::getNStep(void) const
274   {
275      return (this->nstep);
276   }
[509]277
[266]278   void CField::incrementNStep(void)
279   {
280      this->nstep++;
281   }
[509]282
[321]283   void CField::resetNStep(void)
284   {
285      this->nstep=0;
286   }
[219]287
288   //----------------------------------------------------------------
289
[343]290   const CDuration & CField::getFreqOperation(void) const
[219]291   {
292      return (this->freq_operation);
293   }
[509]294
[219]295   //----------------------------------------------------------------
[343]296   const CDuration & CField::getFreqWrite(void) const
[219]297   {
298      return (this->freq_write);
299   }
[509]300
[219]301   //----------------------------------------------------------------
[509]302
[219]303   boost::shared_ptr<func::CFunctor> CField::getFieldOperation(void) const
304   {
305      return (this->foperation);
306   }
307
308
[310]309   bool CField::isActive(void) const
[509]310   {
311      return (!this->refObject.empty());
[310]312   }
[219]313   //----------------------------------------------------------------
[509]314
[369]315   CArray<double, 1> CField::getData(void) const
[219]316   {
317      return(this->data);
318   }
319
320   //----------------------------------------------------------------
321
[343]322   boost::shared_ptr<CDate> CField::getLastWriteDate(void) const
[219]323   {
324      return(this->last_Write);
325   }
326
327   //----------------------------------------------------------------
328
[343]329   boost::shared_ptr<CDate> CField::getLastOperationDate(void) const
[219]330   {
331      return(this->last_operation);
332   }
333
334   //----------------------------------------------------------------
335
[509]336//   void CField::processEnabledField(void)
337//   {
338//      if (!processed)
339//      {
340//        processed=true ;
341//        solveRefInheritance(true) ;
342//        solveBaseReference() ;
343//        solveOperation() ;
344//        solveGridReference() ;
345//
346//        if (hasDirectFieldReference()) baseRefObject->processEnabledField() ;
347//        buildExpression();
348//        active=true;
349//      }
350//    }
351
352   void CField::solveAllReferenceEnabledField(bool doSending2Sever)
[459]353   {
[509]354     CContext* context = CContext::getCurrent();
355     if (!areAllReferenceSolved)
356     {
357        areAllReferenceSolved = true;
[510]358        if (context->hasClient)
[509]359        {
360          solveRefInheritance(true);
361          solveBaseReference();
362        }
[478]363
[509]364        solveOperation();
365        solveGridReference();
366     }
367     solveGridDomainAxisRef(doSending2Sever);
368     solveCheckMaskIndex(doSending2Sever);
369   }
370
371   std::map<int, StdSize> CField::getGridDataSize()
372   {
373     return grid->getConnectedServerDataSize();
374   }
375
376   void CField::buildAllExpressionEnabledField()
377   {
378     if (!areAllReferenceSolved) solveAllReferenceEnabledField(true);
379     if (!areAllExpressionBuilt)
380     {
381       areAllExpressionBuilt = true;
382//       solveCheckMaskIndex(true);
383//       solveCheckMaskIndex();
384       if (hasDirectFieldReference()) baseRefObject->buildAllExpressionEnabledField();
385       buildExpression();
386       active=true;
387     }
388   }
389
[219]390   //----------------------------------------------------------------
391
392   void  CField::solveOperation(void)
393   {
394      using namespace func;
[509]395
[460]396      if (!hasOutputFile && !hasFieldOut) return ;
[509]397
[459]398      StdString id ;
399      if (hasId()) id=getId();
400      else if (!name.isEmpty()) id=name ;
401      else if (hasDirectFieldReference()) id=baseRefObject->getId() ;
[509]402
[347]403      CContext* context = CContext::getCurrent();
[509]404
[538]405      if (freq_op.isEmpty()) freq_op.setValue(TimeStep);
[509]406
[459]407      if (operation.isEmpty() )
[219]408      {
409         ERROR("CField::solveOperation(void)",
410               << "[ id = " << id << "]"
[421]411               << "Impossible to define an operation for this field !");
[219]412      }
[509]413
[538]414      if (freq_offset.isEmpty())
415        freq_offset.setValue(NoneDu);
[219]416
[300]417//      if (CXIOSManager::GetStatus() == CXIOSManager::LOC_SERVER)
418      if (context->hasServer)
[219]419      {
[509]420         if (hasOutputFile)
[460]421         {
[538]422           this->freq_operation_srv = this->file->output_freq.getValue();
423           this->freq_write_srv = this->file->output_freq.getValue();
[460]424         }
[538]425         this->lastlast_Write_srv = boost::shared_ptr<CDate>
[343]426                        (new CDate(context->getCalendar()->getInitDate()));
427         this->last_Write_srv     = boost::shared_ptr<CDate>
428                        (new CDate(context->getCalendar()->getInitDate()));
429         this->last_operation_srv = boost::shared_ptr<CDate>
430                        (new CDate(context->getCalendar()->getInitDate()));
[300]431//         this->foperation_srv     =
432//             boost::shared_ptr<func::CFunctor>(new CInstant(this->data_srv));
[509]433
434         if (hasOutputFile)
[460]435         {
[538]436           const CDuration toffset = this->freq_operation_srv - freq_offset.getValue() - context->getCalendar()->getTimeStep();
[460]437           *this->last_operation_srv   = *this->last_operation_srv - toffset;
438         }
[219]439      }
[509]440
[449]441//      if (context->hasClient)
[509]442//      {
[538]443         this->freq_operation = freq_op.getValue();
444         if (hasOutputFile) this->freq_write = this->file->output_freq.getValue();
[509]445         if (hasFieldOut)
[460]446         {
[538]447           this->freq_write = this->fieldOut->freq_op.getValue();
[460]448         }
[343]449         this->last_Write     = boost::shared_ptr<CDate>
450                        (new CDate(context->getCalendar()->getInitDate()));
451         this->last_operation = boost::shared_ptr<CDate>
452                        (new CDate(context->getCalendar()->getInitDate()));
[509]453
[538]454         const CDuration toffset = this->freq_operation - freq_offset.getValue() - context->getCalendar()->getTimeStep();
[509]455         *this->last_operation   = *this->last_operation - toffset;
456
[436]457        if (operation.get()=="once") isOnceOperation=true ;
458        else isOnceOperation=false;
459        isFirstOperation=true;
[509]460
461
[219]462#define DECLARE_FUNCTOR(MType, mtype)              \
463   if  (operation.getValue().compare(#mtype) == 0) \
464   {                                               \
[470]465      if (!detect_missing_value.isEmpty() && !default_value.isEmpty() && detect_missing_value==true) \
466      { \
467        boost::shared_ptr<func::CFunctor> foperation_(new C##MType(this->data,default_value)); \
[473]468        this->foperation = foperation_; \
[470]469      } \
470      else \
471      { \
472        boost::shared_ptr<func::CFunctor> foperation_(new C##MType(this->data)); \
473        this->foperation = foperation_;  \
474      } \
[219]475      return;                                      \
476   }
[509]477
[219]478#include "functor_type.conf"
[509]479
[219]480         ERROR("CField::solveOperation(void)",
481               << "[ operation = " << operation.getValue() << "]"
[421]482               << "The operation is not defined !");
[449]483//      }
[436]484
[509]485
[219]486   }
[509]487
[219]488   //----------------------------------------------------------------
[369]489/*
[219]490   void CField::fromBinary(StdIStream & is)
491   {
492      SuperClass::fromBinary(is);
493#define CLEAR_ATT(name_)\
[369]494      SuperClassAttribute::operator[](#name_)->reset()
[219]495
496         CLEAR_ATT(domain_ref);
497         CLEAR_ATT(axis_ref);
498#undef CLEAR_ATT
499
500   }
[369]501*/
[219]502   //----------------------------------------------------------------
503
504   void CField::solveGridReference(void)
505   {
[347]506      CDomain* domain;
507      CAxis* axis;
[219]508
509      if (!domain_ref.isEmpty())
510      {
[346]511         if (CDomain::has(domain_ref.getValue()))
512            domain = CDomain::get(domain_ref.getValue()) ;
[219]513         else
514            ERROR("CField::solveGridReference(void)",
[421]515                  << "Reference to the domain \'"
516                  << domain_ref.getValue() << "\' is wrong") ;
[219]517      }
518
519      if (!axis_ref.isEmpty())
520      {
[346]521         if (CAxis::has(axis_ref.getValue()))
522            axis = CAxis::get(axis_ref.getValue()) ;
[219]523         else
524            ERROR("CField::solveGridReference(void)",
[421]525                  << "Reference to the axis \'"
526                  << axis_ref.getValue() <<"\' is wrong") ;
[219]527      }
528
529      if (!grid_ref.isEmpty())
530      {
[346]531         if (CGrid::has(grid_ref.getValue()))
532            this->grid = CGrid::get(grid_ref.getValue()) ;
[219]533         else
534            ERROR("CField::solveGridReference(void)",
[421]535                  << "Reference to the grid \'"
536                  << grid_ref.getValue() << "\' is wrong");
[219]537      }
[509]538
[540]539      if (grid_ref.isEmpty() &&  domain_ref.isEmpty() && axis_ref.isEmpty())
[219]540      {
541            ERROR("CField::solveGridReference(void)",
[540]542                  << "At least one dimension must be defined for this field.");
543      }
[418]544
[540]545//     if (!grid_ref.isEmpty())
546//     {
547//       domain = grid->domain;
548//       axis = grid->axis;
549//     }
[509]550
[540]551//     CType<string> goodDomain ;
552//     CType<string> goodAxis ;
553//     if (!grid_ref.isEmpty())
554//     {
555//       if (!grid->domain_ref.isEmpty()) goodDomain=grid->domain_ref ;
556//       if (!grid->axis_ref.isEmpty()) goodAxis=grid->axis_ref ;
557//     }
558//     if (!domain_ref.isEmpty()) goodDomain=domain_ref ;
559//     if (!axis_ref.isEmpty()) goodAxis=axis_ref ;
[509]560
[540]561//     CArray<std::string,1> domListTmp = grid->domainList.getValue();
562//     CArray<std::string,1> axisListTmp = grid->axisList.getValue();
[509]563
[540]564     std::vector<std::string> domList, axisList;
565     if (0 != grid)
[418]566     {
[540]567       domList = grid->getDomainList();
568       axisList = grid->getAxisList();
[418]569     }
[509]570
[540]571     if (domList.empty() && axisList.empty())
[418]572     {
[540]573       std::vector<CDomain*> vecDom;
574       if (0 != domain) vecDom.push_back(domain);
575       std::vector<CAxis*> vecAxis;
576       if (0 != axis) vecAxis.push_back(axis);
577       this->grid = CGrid::createGrid(vecDom, vecAxis);
[509]578     }
579
[540]580//     std::string goodDomain = domListTmp[0];
581//     std::string goodAxis = axisListTmp[0];
[509]582
[540]583//     if (goodDomain.isEmpty())
584//     if (goodDomain.empty())
585//     {
586//       ERROR("CField::solveGridReference(void)", << "The horizontal domain for this field is not defined");
587//     }
588//     else
589//     {
590//       if (CDomain::has(goodDomain)) domain = CDomain::get(goodDomain) ;
591//       else ERROR("CField::solveGridReference(void)",<< "Reference to the domain \'"
592//                  <<goodDomain << "\' is wrong") ;
593////                  <<goodDomain.get() << "\' is wrong") ;
594//     }
595//
596////     if (!goodAxis.isEmpty())
597//     if (!goodAxis.empty())
598//     {
599//       if (CAxis::has(goodAxis))  axis = CAxis::get(goodAxis) ;
600//       else  ERROR("CField::solveGridReference(void)", << "Reference to the axis \'"
601//                  << goodAxis <<"\' is wrong") ;
602//                  << goodAxis.get() <<"\' is wrong") ;
603//     }
[509]604
[540]605//     bool nothingToDo=false ;
606//
607//     if (!grid_ref.isEmpty())
608//     {
609//       if (!grid->domain_ref.isEmpty() && goodDomain.get() == grid->domain_ref.get())
610//         if (goodAxis.isEmpty()) nothingToDo=true ;
611//         else if (!grid->axis_ref.isEmpty())
612//                 if (grid->axis_ref.get()==goodAxis.get()) nothingToDo=true ;
613//     }
614//
615//     nothingToDo = true;
616//     if (!nothingToDo)
617//     {
618//       if (!goodAxis.isEmpty())
619//       {
620//         this->grid = CGrid::createGrid(domain, axis) ;
621//         this->grid_ref.setValue(this->grid->getId());
622//       }
623//       else
624//       {
625//         this->grid = CGrid::createGrid(domain) ;
626//         this->grid_ref.setValue(this->grid->getId());
627//       }
628//     }
[418]629
[509]630//     grid->solveReference() ;
631//     grid->solveDomainAxisRef();
632//     grid->checkMaskIndex();
633   }
[459]634
[509]635   void CField::solveGridDomainAxisRef(bool checkAtt)
636   {
637     grid->solveDomainAxisRef(checkAtt);
[219]638   }
639
[509]640   void CField::solveCheckMaskIndex(bool doSendingIndex)
641   {
642     grid->checkMaskIndex(doSendingIndex);
643   }
[219]644
645   ///-------------------------------------------------------------------
646
647   template <>
648      void CGroupTemplate<CField, CFieldGroup, CFieldAttributes>::solveRefInheritance(void)
649   {
650      if (this->group_ref.isEmpty()) return;
651      StdString gref = this->group_ref.getValue();
652
[346]653      if (!CFieldGroup::has(gref))
[219]654         ERROR("CGroupTemplate<CField, CFieldGroup, CFieldAttributes>::solveRefInheritance(void)",
655               << "[ gref = " << gref << "]"
656               << " invalid group name !");
657
[347]658      CFieldGroup* group = CFieldGroup::get(gref);
659      CFieldGroup* owner = CFieldGroup::get(boost::polymorphic_downcast<CFieldGroup*>(this));
[219]660
[347]661      std::vector<CField*> allChildren  = group->getAllChildren();
[509]662      std::vector<CField*>::iterator
[219]663         it = allChildren.begin(), end = allChildren.end();
[509]664
[219]665      for (; it != end; it++)
666      {
[347]667         CField* child = *it;
[346]668         if (child->hasId()) owner->createChild()->field_ref.setValue(child->getId()) ;
[509]669
[219]670      }
671   }
[509]672
[464]673   void CField::scaleFactorAddOffset(double scaleFactor, double addOffset)
674   {
675     map<int, CArray<double,1>* >::iterator it;
676     for(it=data_srv.begin();it!=data_srv.end();it++) *it->second = (*it->second -addOffset) * 1./scaleFactor  ;
677   }
[509]678
[369]679   void CField::outputField(CArray<double,3>& fieldOut)
[300]680   {
[369]681      map<int, CArray<double,1>* >::iterator it;
[300]682      for(it=data_srv.begin();it!=data_srv.end();it++)
[369]683         grid->outputField(it->first,*it->second, fieldOut) ;
[509]684
[300]685   }
[509]686
[369]687   void CField::outputField(CArray<double,2>& fieldOut)
[300]688   {
[369]689      map<int, CArray<double,1>* >::iterator it;
[219]690
[300]691      for(it=data_srv.begin();it!=data_srv.end();it++)
692      {
[369]693         grid->outputField(it->first,*it->second, fieldOut) ;
[300]694      }
695   }
[219]696   ///-------------------------------------------------------------------
697
[459]698   void CField::parse(xml::CXMLNode & node)
699   {
700      SuperClass::parse(node);
[509]701      if (! node.getContent(this->content))
[472]702      {
[476]703        if (node.goToChildElement())
[472]704        {
[476]705          do
706          {
707            if (node.getElementName()=="variable" || node.getElementName()=="variable_group") this->getVirtualVariableGroup()->parseChild(node);
708          } while (node.goToNextElement()) ;
709          node.goToParentElement();
710        }
[472]711      }
[459]712    }
[509]713
[459]714  CArray<double,1>* CField::getInstantData(void)
715  {
[509]716    if (!hasInstantData)
[459]717    {
718      instantData.resize(grid->storeIndex_client.numElements()) ;
719      hasInstantData=true ;
720    }
721    return &instantData ;
722  }
[509]723
[459]724  void CField::addDependency(CField* field, int slotId)
725  {
726    fieldDependency.push_back(pair<CField*,int>(field,slotId)) ;
727  }
[509]728
[459]729  void CField::buildExpression(void)
730  {
[509]731    if (content.size() > 0)
[459]732    {
733      CSimpleNodeExpr* simpleExpr=parseExpr(content+'\0') ;
734      expression=CFieldNode::newNode(simpleExpr) ;
735      delete simpleExpr ;
[460]736      set<string> instantFieldIds ;
737      map<string,CField*> associatedInstantFieldIds ;
738      expression->getInstantFieldIds(instantFieldIds) ;
[509]739      for (set<string>::iterator it=instantFieldIds.begin() ; it!=instantFieldIds.end();++it)
[460]740      {
[509]741        if (*it!="this")
[460]742        {
[509]743          if (CField::has(*it))
[460]744          {
745            CField* field=CField::get(*it) ;
[509]746//            field->processEnabledField() ;
747            field->buildAllExpressionEnabledField();
[460]748            associatedInstantFieldIds[*it]=field ;
749          }
750          else  ERROR("void CField::buildExpression(void)",<<" Field "<<*it<<" does not exist") ;
751        }
752      }
[509]753
[460]754      set<string> averageFieldIds ;
755      map<string,CField*> associatedAverageFieldIds ;
[459]756
[460]757      expression->getAverageFieldIds(averageFieldIds) ;
[509]758      for (set<string>::iterator it=averageFieldIds.begin() ; it!=averageFieldIds.end();++it)
759      {
760        if (CField::has(*it))
[460]761        {
762           CFieldGroup* root=CFieldGroup::get("field_definition") ;
763           CField* averageField=root->createChild() ;
764           CField* instantField=root->createChild() ;
765           averageField->field_ref=*it ;
766           averageField->hasFieldOut=true ;
767           averageField->fieldOut=instantField ;
768           instantField->freq_op=freq_op ;
[509]769//           averageField-> processEnabledField() ;
770           averageField->buildAllExpressionEnabledField();
[460]771           instantField->SuperClassAttribute::setAttributes(averageField, true);
772           instantField->field_ref.reset() ;
773           instantField->operation.reset() ;
774
[509]775//           instantField-> processEnabledField() ;
776           instantField->buildAllExpressionEnabledField();
[460]777           associatedAverageFieldIds[*it]=instantField  ;
778        }
779        else ERROR("void CField::buildExpression(void)",<<" Field "<<*it<<" does not exist") ;
780      }
781
782      expression->reduce(this,associatedInstantFieldIds,associatedAverageFieldIds) ;
783
784      slots.resize(instantFieldIds.size()+averageFieldIds.size()) ;
[459]785      resetSlots() ;
786      int slotId=0 ;
787      set<CField*> fields ;
788      expression->getFields(fields) ;
789      for (set<CField*>::iterator it=fields.begin() ; it!=fields.end();++it,++slotId) (*it)->addDependency(this,slotId) ;
[509]790      hasExpression=true;
[459]791    }
792  }
[509]793
[459]794  void CField::resetSlots(void)
795  {
796    for(vector<bool>::iterator it=slots.begin();it!=slots.end();++it) *it=false ;
797  }
[509]798
[459]799  bool CField::slotsFull(void)
800  {
801    bool ret=true ;
802    for(vector<bool>::iterator it=slots.begin();it!=slots.end();++it) ret &= *it;
803    return ret ;
804  }
805
[509]806
[459]807  void CField::setSlot(int slotId)
808  {
809    CContext* context = CContext::getCurrent() ;
810    const CDate & currDate = context->getCalendar()->getCurrentDate();
[509]811    if (slotUpdateDate==NULL || currDate!=*slotUpdateDate)
[459]812    {
813      resetSlots() ;
814      if (slotUpdateDate==NULL) slotUpdateDate=new CDate(currDate) ;
815      else *slotUpdateDate=currDate ;
816    }
817    slots[slotId]=true ;
818    if (slotsFull())
819    {
820      CArray<double,1> expr(expression->compute()) ;
[509]821
822      if (hasInstantData)
[459]823      {
824        instantData=expr ;
[509]825        for(list< pair<CField *,int> >::iterator it=fieldDependency.begin(); it!=fieldDependency.end(); ++it)
[459]826          if (it->first!=this) it->first->setSlot(it->second) ;
827      }
[509]828
[459]829      if (hasOutputFile) updateDataFromExpression(expr) ;
[509]830
[459]831    }
832  }
[472]833
[509]834   /*!
835     This function retrieves Id of corresponding domain_ref and axis_ref (if any)
836   of a field. In some cases, only domain exists but axis doesn't
837   \return pair of Domain and Axis id
838   */
839   const std::pair<StdString,StdString>& CField::getDomainAxisIds()
840   {
841     CGrid* cgPtr = getRelGrid();
842     if (NULL != cgPtr)
843     {
844       if (NULL != cgPtr->getRelDomain()) domAxisIds_.first = cgPtr->getRelDomain()->getId();
845       if (NULL != cgPtr->getRelAxis()) domAxisIds_.second = cgPtr->getRelAxis()->getId();
846     }
[472]847
[509]848     return (domAxisIds_);
849   }
[472]850
851   CVariable* CField::addVariable(const string& id)
852   {
853     return vVariableGroup->createChild(id) ;
854   }
855
856   CVariableGroup* CField::addVariableGroup(const string& id)
857   {
858     return vVariableGroup->createChildGroup(id) ;
859   }
860
861
[509]862   void CField::sendAddAllVariables()
863   {
864     if (!getAllVariables().empty())
865     {
866       // Firstly, it's necessary to add virtual variable group
867       sendAddVariableGroup(getVirtualVariableGroup()->getId());
868
869       // Okie, now we can add to this variable group
870       std::vector<CVariable*> allVar = getAllVariables();
871       std::vector<CVariable*>::const_iterator it = allVar.begin();
872       std::vector<CVariable*>::const_iterator itE = allVar.end();
873
874       for (; it != itE; ++it)
875       {
876         this->sendAddVariable((*it)->getId());
877         (*it)->sendAllAttributesToServer();
878         (*it)->sendValue();
879       }
880     }
881   }
882
[472]883   void CField::sendAddVariable(const string& id)
884   {
885    CContext* context=CContext::getCurrent() ;
[509]886
[472]887    if (! context->hasServer )
888    {
889       CContextClient* client=context->client ;
890
[509]891       CEventClient event(this->getType(),EVENT_ID_ADD_VARIABLE) ;
[472]892       if (client->isServerLeader())
893       {
894         CMessage msg ;
895         msg<<this->getId() ;
896         msg<<id ;
897         event.push(client->getServerLeader(),1,msg) ;
898         client->sendEvent(event) ;
899       }
900       else client->sendEvent(event) ;
901    }
[509]902
[472]903   }
[509]904
905
[472]906   void CField::sendAddVariableGroup(const string& id)
907   {
908    CContext* context=CContext::getCurrent() ;
909    if (! context->hasServer )
910    {
911       CContextClient* client=context->client ;
912
[509]913       CEventClient event(this->getType(),EVENT_ID_ADD_VARIABLE_GROUP) ;
[472]914       if (client->isServerLeader())
915       {
916         CMessage msg ;
917         msg<<this->getId() ;
918         msg<<id ;
919         event.push(client->getServerLeader(),1,msg) ;
920         client->sendEvent(event) ;
921       }
922       else client->sendEvent(event) ;
923    }
[509]924
[472]925   }
[509]926
[472]927   void CField::recvAddVariable(CEventServer& event)
928   {
[509]929
[472]930      CBufferIn* buffer=event.subEvents.begin()->buffer;
931      string id;
932      *buffer>>id ;
933      get(id)->recvAddVariable(*buffer) ;
934   }
[509]935
936
[472]937   void CField::recvAddVariable(CBufferIn& buffer)
938   {
939      string id ;
940      buffer>>id ;
941      addVariable(id) ;
942   }
943
944   void CField::recvAddVariableGroup(CEventServer& event)
945   {
[509]946
[472]947      CBufferIn* buffer=event.subEvents.begin()->buffer;
948      string id;
949      *buffer>>id ;
950      get(id)->recvAddVariableGroup(*buffer) ;
951   }
[509]952
953
[472]954   void CField::recvAddVariableGroup(CBufferIn& buffer)
955   {
956      string id ;
957      buffer>>id ;
958      addVariableGroup(id) ;
959   }
960
[540]961   DEFINE_REF_FUNC(Field,field)
[472]962
[540]963//  void CField::addReference(CField* field)
964//  {
965//    refObject.push_back(field) ;
966//  }
967//
968//   //----------------------------------------------------------------
969//
970//   bool CField::hasDirectFieldReference(void) const
971//   {
972//     return (!this->field_ref.isEmpty());
973//   }
974//
975//   //----------------------------------------------------------------
976//
977//   const StdString & CField::getBaseFieldId(void) const
978//   {
979//      return (this->getBaseFieldReference()->getId());
980//   }
981//
982//
983//   //----------------------------------------------------------------
984//
985//   /*!
986//   \brief Get pointer to direct field to which the current field refers.
987//   */
988//   CField* CField::getDirectFieldReference(void) const
989//   {
990//      if (this->field_ref.isEmpty())
991//         return (this->getBaseFieldReference());
992//
993//      if (! CField::has(this->field_ref.getValue()))
994//         ERROR("CField::getDirectFieldReference(void)",
995//               << "[ ref_name = " << this->field_ref.getValue() << "]"
996//               << " invalid field name !");
997//
998//      return (CField::get(this->field_ref.getValue()));
999//   }
1000//
1001//   //----------------------------------------------------------------
1002//
1003//   CField* CField::getBaseFieldReference(void) const
1004//   {
1005//      return (baseRefObject);
1006//   }
1007//
1008//   //----------------------------------------------------------------
1009//
1010//   const std::vector<CField*>& CField::getAllReference(void) const
1011//   {
1012//      return (refObject);
1013//   }
1014//
1015//
1016//   /*!
1017//   \brief Searching for all reference of a field
1018//   If a field refers to (an)other field(s), we will search for all its referenced parents.
1019//   Moreover, if any father, direct or indirect (e.g: two levels up), has non-empty attributes,
1020//   all its attributes will be added to the current field
1021//   \param [in] apply Flag to specify whether current field uses attributes of its father
1022//               in case the attribute is empty (true) or its attributes are replaced by ones of its father (false)
1023//   */
1024//   void CField::solveRefInheritance(bool apply)
1025//   {
1026//      std::set<CField *> sset;
1027//      CField* refer_sptr;
1028//      CField * refer_ptr = this;
1029//
1030//      while (refer_ptr->hasDirectFieldReference())
1031//      {
1032//         refer_sptr = refer_ptr->getDirectFieldReference();
1033//         refer_ptr  = refer_sptr;
1034//
1035//         if(sset.end() != sset.find(refer_ptr))
1036//         {
1037//            DEBUG (<< "Circular dependency stopped for field object on "
1038//                   << "\"" + refer_ptr->getId() + "\" !");
1039//            break;
1040//         }
1041//
1042//         SuperClassAttribute::setAttributes(refer_ptr, apply);
1043//         sset.insert(refer_ptr);
1044//      }
1045//   }
1046//
1047//   /*!
1048//   \brief Only on SERVER side. Remove all field_ref from current field
1049//   On creating a new field on server side, redundant "field_ref" is still kept in the attribute list
1050//   of the current field. This function removes this from current field
1051//   */
1052//   void CField::removeRefInheritance()
1053//   {
1054//     if (this->field_ref.isEmpty()) return;
1055//     this->clearAttribute("field_ref");
1056//   }
1057//
1058//   void CField::solveBaseReference(void)
1059//   {
1060//      std::set<CField *> sset;
1061//      CField* refer_sptr;
1062//      CField * refer_ptr = this;
1063//
1064//      if (this->hasDirectFieldReference())  baseRefObject = getDirectFieldReference();
1065//      else  baseRefObject = CField::get(this);
1066//
1067//      while (refer_ptr->hasDirectFieldReference())
1068//      {
1069//         refer_sptr = refer_ptr->getDirectFieldReference();
1070//         refer_ptr  = refer_sptr;
1071//
1072//         if(sset.end() != sset.find(refer_ptr))
1073//         {
1074//            DEBUG (<< "Circular dependency stopped for field object on "
1075//                   << "\"" + refer_ptr->getId() + "\" !");
1076//            break;
1077//         }
1078//
1079//         sset.insert(refer_ptr);
1080//      }
1081//
1082//      if (hasDirectFieldReference()) baseRefObject->addReference(this) ;
1083//   }
1084//
[472]1085
[335]1086} // namespace xios
Note: See TracBrowser for help on using the repository browser.