source: XIOS/trunk/src/node/domain.cpp @ 314

Last change on this file since 314 was 314, checked in by ymipsl, 12 years ago

Removing obsolete files and some cleaning...

YM

File size: 36.8 KB
Line 
1#include "domain.hpp"
2
3#include "attribute_template_impl.hpp"
4#include "object_template_impl.hpp"
5#include "group_template_impl.hpp"
6
7#include "tree_manager.hpp"
8#include "xmlioserver_spl.hpp"
9#include "event_client.hpp"
10#include "event_server.hpp"
11#include "buffer_in.hpp"
12
13namespace xmlioserver {
14namespace tree {
15   
16   /// ////////////////////// Définitions ////////////////////// ///
17
18   CDomain::CDomain(void)
19      : CObjectTemplate<CDomain>(), CDomainAttributes()
20      , isChecked(false), local_mask(new CArray<int, 2>(boost::extents[0][0])), relFiles()
21      , ibegin_sub(), iend_sub(), jbegin_sub(), jend_sub()
22      , ibegin_zoom_sub(), jbegin_zoom_sub(), ni_zoom_sub(), nj_zoom_sub()
23      , lonvalue_sub(), latvalue_sub(),lonvalue_srv(new CArray<double,1>())
24      , latvalue_srv(new CArray<double,1>())
25   { /* Ne rien faire de plus */ }
26
27   CDomain::CDomain(const StdString & id)
28      : CObjectTemplate<CDomain>(id), CDomainAttributes()
29      , isChecked(false), local_mask(new CArray<int, 2>(boost::extents[0][0])), relFiles()
30      , ibegin_sub(), iend_sub(), jbegin_sub(), jend_sub()
31      , ibegin_zoom_sub(), jbegin_zoom_sub(),ni_zoom_sub(), nj_zoom_sub()
32      , lonvalue_sub(), latvalue_sub(),lonvalue_srv(new CArray<double,1>())
33      , latvalue_srv(new CArray<double,1>())
34   { /* Ne rien faire de plus */ }
35
36   CDomain::~CDomain(void)
37   { 
38      this->local_mask.reset();
39      for (StdSize i = 0; i < this->lonvalue_sub.size(); i++)
40      {
41         this->lonvalue_sub[i].reset();
42         this->latvalue_sub[i].reset();
43      }     
44   }
45
46   ///---------------------------------------------------------------
47
48   const std::set<StdString> & CDomain::getRelFiles(void) const
49   {
50      return (this->relFiles);
51   }
52
53   //----------------------------------------------------------------
54   
55   bool CDomain::hasZoom(void) const
56   {
57      return ((this->zoom_ni.getValue() != this->ni_glo.getValue()) && 
58              (this->zoom_nj.getValue() != this->nj_glo.getValue()));
59   }
60   
61   //----------------------------------------------------------------
62   
63   bool CDomain::isEmpty(void) const
64   {
65      return ((this->zoom_ni_srv == 0) || 
66              (this->zoom_nj_srv == 0));
67   }
68
69   //----------------------------------------------------------------
70
71   bool CDomain::IsWritten(const StdString & filename) const
72   {
73      return (this->relFiles.find(filename) != this->relFiles.end());
74   }
75
76   //----------------------------------------------------------------
77
78   void CDomain::addRelFile(const StdString & filename)
79   {
80      this->relFiles.insert(filename);
81   }
82
83   //----------------------------------------------------------------
84
85   void CDomain::fromBinary(StdIStream & is)
86   {
87      SuperClass::fromBinary(is);
88     
89      if ( !this->ibegin.isEmpty()   &&
90           !this->jbegin.isEmpty()   &&
91           !this->iend.isEmpty()     &&
92           !this->jend.isEmpty()     &&
93           !this->latvalue.isEmpty() &&
94           !this->lonvalue.isEmpty())
95      {
96     
97         this->ibegin_sub.push_back(this->ibegin.getValue());
98         this->jbegin_sub.push_back(this->jbegin.getValue());
99         this->iend_sub.push_back(this->iend.getValue());
100         this->jend_sub.push_back(this->jend.getValue()); 
101         
102         this->ibegin_zoom_sub.push_back(this->zoom_ibegin_loc.getValue());
103         this->jbegin_zoom_sub.push_back(this->zoom_jbegin_loc.getValue());
104         this->ni_zoom_sub.push_back(this->zoom_ni_loc.getValue());
105         this->nj_zoom_sub.push_back(this->zoom_nj_loc.getValue());
106     
107         this->latvalue_sub.push_back(this->latvalue.getValue());
108         this->lonvalue_sub.push_back(this->lonvalue.getValue());
109      }
110     
111#define CLEAR_ATT(name_)\
112      SuperClassAttribute::operator[](#name_)->clear()
113
114         CLEAR_ATT(mask);
115         CLEAR_ATT(data_n_index);
116         CLEAR_ATT(data_i_index);
117         CLEAR_ATT(data_j_index);
118         
119         CLEAR_ATT(data_ni);
120         CLEAR_ATT(data_nj);
121         CLEAR_ATT(data_ibegin);
122         CLEAR_ATT(data_jbegin);
123         
124         CLEAR_ATT(ni);
125         CLEAR_ATT(nj);
126         
127#undef CLEAR_ATT
128
129      if ( !this->ibegin.isEmpty()   &&
130           !this->jbegin.isEmpty()   &&
131           !this->iend.isEmpty()     &&
132           !this->jend.isEmpty()     &&
133           !this->latvalue.isEmpty() &&
134           !this->lonvalue.isEmpty())
135      {
136
137         this->ibegin.setValue(*std::min_element(this->ibegin_sub.begin(),this->ibegin_sub.end()));
138         this->jbegin.setValue(*std::min_element(this->jbegin_sub.begin(),this->jbegin_sub.end()));
139         this->iend.setValue(*std::max_element(this->iend_sub.begin(),this->iend_sub.end()));
140         this->jend.setValue(*std::max_element(this->jend_sub.begin(),this->jend_sub.end()));
141      }
142   }
143
144   //----------------------------------------------------------------
145
146   StdString CDomain::GetName(void)   { return (StdString("domain")); }
147   StdString CDomain::GetDefName(void){ return (CDomain::GetName()); }
148   ENodeType CDomain::GetType(void)   { return (eDomain); }
149
150   //----------------------------------------------------------------
151
152   void CDomain::checkGlobalDomain(void)
153   {
154      if ((ni_glo.isEmpty() || ni_glo.getValue() <= 0 ) ||
155          (nj_glo.isEmpty() || nj_glo.getValue() <= 0 ))
156      {
157         ERROR("CDomain::checkAttributes(void)",
158               << "[ Id = " << this->getId() << " ] "
159               << "Le domaine global est mal défini,"
160               << " vérifiez les valeurs de \'ni_glo\' et \'nj_glo\' !") 
161      }
162   }
163
164   //----------------------------------------------------------------
165
166   void CDomain::checkLocalIDomain(void)
167   {
168      if (!ni.isEmpty() && !ibegin.isEmpty() && iend.isEmpty())
169         iend.setValue(ibegin.getValue() + ni.getValue() - 1) ;
170
171      else if (!ni.isEmpty() && !iend.isEmpty()   && ibegin.isEmpty())
172         ibegin.setValue( - ni.getValue() + iend.getValue() + 1) ;
173
174      else if (!ibegin.isEmpty() && !iend.isEmpty() && ni.isEmpty())
175         ni.setValue(iend.getValue() - ibegin.getValue() + 1) ;
176
177      else if (!ibegin.isEmpty() && !iend.isEmpty() &&
178               !ni.isEmpty() && (iend.getValue() != ibegin.getValue() + ni.getValue() - 1))
179      {
180         ERROR("CDomain::checkAttributes(void)",
181               << "Le domaine est mal défini,"
182               << " iend est différent de (ibegin + ni - 1) !") ;
183      }
184      else
185      {
186         ERROR("CDomain::checkAttributes(void)",
187               << "Le domaine est mal défini,"
188               << " deux valeurs au moins parmis iend, ibegin, ni doivent être définies !") ;
189      }
190
191
192      if (ni.getValue() < 0 || ibegin.getValue() > iend.getValue() ||
193          ibegin.getValue() < 1 || iend.getValue() > ni_glo.getValue())
194         ERROR("CDomain::checkAttributes(void)",
195               << "[ Id = " << this->getId() << " ] "
196               << "Domaine local mal défini,"
197               << " vérifiez les valeurs ni, ni_glo, ibegin, iend") ;
198
199   }
200
201   //----------------------------------------------------------------
202
203   void CDomain::checkLocalJDomain(void)
204   {
205      if (!nj.isEmpty() && !jbegin.isEmpty() && jend.isEmpty())
206         jend.setValue(jbegin.getValue() + nj.getValue() - 1) ;
207
208      else if (!nj.isEmpty() && !jend.isEmpty() && jbegin.isEmpty())
209         jbegin.setValue( - nj.getValue() + jend.getValue() + 1) ;
210
211      else if (!jbegin.isEmpty() && !jend.isEmpty() && nj.isEmpty())
212         nj.setValue(jend.getValue() - jbegin.getValue() + 1) ;
213
214      else if (!jbegin.isEmpty() && !jend.isEmpty() && !nj.isEmpty() &&
215               (jend.getValue() != jbegin.getValue() + nj.getValue() - 1))
216      {
217         ERROR("CDomain::checkAttributes(void)",
218               << "Le domaine est mal défini,"
219               << " iend est différent de (jbegin + nj - 1) !") ;
220      }
221      else
222      {
223         ERROR("CDomain::checkAttributes(void)",
224               << "Le domaine est mal défini,"
225               << " deux valeurs au moins parmis jend, jbegin, nj doivent être définies !") ;
226      }
227
228      if (nj.getValue() < 0 || jbegin.getValue() > jend.getValue() ||
229          jbegin.getValue() < 1 || jend.getValue() > nj_glo.getValue())
230         ERROR("CDomain::checkAttributes(void)",
231               << "Domaine local mal défini,"
232               << " vérifiez les valeurs nj, nj_glo, jbegin, jend") ;
233   }
234
235   //----------------------------------------------------------------
236
237   void CDomain::checkMask(void)
238   {
239      using namespace std;
240     
241      int ibegin_mask = 0,
242          jbegin_mask = 0,
243          iend_mask = iend.getValue() - ibegin.getValue(),
244          jend_mask = jend.getValue() - jbegin.getValue();
245     
246      if (!zoom_ibegin.isEmpty())
247      {
248         int zoom_iend = zoom_ibegin.getValue() + zoom_ni.getValue() - 1;
249         int zoom_jend = zoom_jbegin.getValue() + zoom_nj.getValue() - 1;
250         
251         ibegin_mask = max (ibegin.getValue(), zoom_ibegin.getValue());
252         jbegin_mask = max (jbegin.getValue(), zoom_jbegin.getValue());
253         iend_mask   = min (iend.getValue(), zoom_iend);
254         jend_mask   = min (jend.getValue(), zoom_jend);
255                 
256         ibegin_mask -= ibegin.getValue();
257         jbegin_mask -= jbegin.getValue();
258         iend_mask   -= ibegin.getValue();
259         jend_mask   -= jbegin.getValue();
260      }
261     
262
263      if (!mask.isEmpty())
264      {
265         ARRAY(bool, 2) mask_ = mask.getValue();
266         unsigned int niu = ni.getValue(), nju = nj.getValue();
267         if ((mask_->shape()[0] != niu) ||
268             (mask_->shape()[1] != nju))
269            ERROR("CDomain::checkAttributes(void)",
270                  <<"Le masque n'a pas la même taille que le domaine local") ;
271                 
272         for (int i = 0; i < ni.getValue(); i++)
273         {
274            for (int j = 0; j < nj.getValue(); j++)
275            {
276               if (i < ibegin_mask && i > iend_mask &&
277                   j < jbegin_mask && j > jend_mask )
278                     (*mask_)[i][j] = false;
279            }
280         }
281      }
282      else // (!mask.hasValue())
283      { // Si aucun masque n'est défini,
284        // on en crée un nouveau qui valide l'intégralité du domaine.
285         ARRAY_CREATE(__arr, bool, 2, [ni.getValue()][nj.getValue()]);
286         for (int i = 0; i < ni.getValue(); i++)
287         {
288            for (int j = 0; j < nj.getValue(); j++)
289            {
290               if (i >= ibegin_mask && i <= iend_mask &&
291                   j >= jbegin_mask && j <= jend_mask )
292                     (*__arr)[i][j] = true;
293               else  (*__arr)[i][j] = false;
294            }
295         }
296               
297         mask.setValue(__arr);
298         __arr.reset();
299      }
300   }
301
302
303   //----------------------------------------------------------------
304
305   void CDomain::checkDomainData(void)
306   {     
307      if (!data_dim.isEmpty() &&
308         !(data_dim.getValue() == 1 || data_dim.getValue() == 2))
309      {
310         ERROR("CDomain::checkAttributes(void)",
311               << "Dimension des données non comptatible (doit être 1 ou 2) !") ;
312      }
313      else if (data_dim.isEmpty())
314      {
315         ERROR("CDomain::checkAttributes(void)",
316               << "Dimension des données non définie !") ;
317      }
318
319      if (data_ibegin.isEmpty())
320         data_ibegin.setValue(0) ;
321      if (data_jbegin.isEmpty() && (data_dim.getValue() == 2))
322         data_jbegin.setValue(0) ;
323
324      if (!data_ni.isEmpty() && (data_ni.getValue() <= 0))
325      {
326         ERROR("CDomain::checkAttributes(void)",
327               << "Dimension des données négative (data_ni).") ;
328      }
329      else if (data_ni.isEmpty())
330      {
331         data_ni.setValue((data_dim.getValue() == 1)
332                           ? (ni.getValue() * nj.getValue())
333                           : ni.getValue());
334      }
335
336      if (data_dim.getValue() == 2)
337      {
338         if (!data_nj.isEmpty() && (data_nj.getValue() <= 0) )
339         {
340            ERROR("CDomain::checkAttributes(void)",
341                  << "Dimension des données négative (data_nj).") ;
342         }
343         else if (data_nj.isEmpty())
344            data_nj.setValue(nj.getValue()) ;
345      }
346
347   }
348
349   //----------------------------------------------------------------
350
351   void CDomain::checkCompression(void)
352   {
353      if (!data_i_index.isEmpty())
354      {
355         int ssize = data_i_index.getValue()->size();
356         if (!data_n_index.isEmpty() &&
357            (data_n_index.getValue() != ssize))
358         {
359            ERROR("CDomain::checkAttributes(void)",
360                  <<"Dimension data_i_index incompatible avec data_n_index.") ;
361         }
362         else if (data_n_index.isEmpty())
363            data_n_index.setValue(ssize) ;
364
365         if (data_dim.getValue() == 2)
366         {
367            if (!data_j_index.isEmpty() &&
368               (data_j_index.getValue()->size() != data_i_index.getValue()->size()))
369            {
370               ERROR("CDomain::checkAttributes(void)",
371                     <<"Dimension data_j_index incompatible avec data_i_index.") ;
372            }
373            else if (data_j_index.isEmpty())
374            {
375               ERROR("CDomain::checkAttributes(void)",
376                     <<"La donnée data_j_index doit être renseignée !") ;
377            }
378         }
379      }
380      else
381      {
382         if (!data_n_index.isEmpty() ||
383            ((data_dim.getValue() == 2) && (!data_j_index.isEmpty())))
384            ERROR("CDomain::checkAttributes(void)", << "data_i_index non défini") ;
385      }
386
387      if (data_n_index.isEmpty())
388      { // -> bloc re-vérifié OK
389         if (data_dim.getValue() == 1)
390         {
391            const int dni = data_ni.getValue();
392            ARRAY_CREATE(__arri, int, 1, [dni]);
393            data_n_index.setValue(dni);
394            for (int i = 0; i < dni; i++)
395               (*__arri)[i] = i+1 ;
396            data_i_index.setValue(__arri) ;
397         }
398         else   // (data_dim == 2)
399         {
400            const int dni = data_ni.getValue() * data_nj.getValue();
401           
402            ARRAY_CREATE(__arri, int, 1, [dni]);
403            ARRAY_CREATE(__arrj, int, 1, [dni]);               
404            data_n_index.setValue(dni);
405           
406            //for(int count = 0, i = 0; i  < data_ni.getValue(); i++)
407            //for(int j = 0; j < data_nj.getValue(); j++, count++)
408           
409            for(int count = 0, j = 0; j  < data_nj.getValue(); j++)
410            {
411               for(int i = 0; i < data_ni.getValue(); i++, count++)
412               {
413                  (*__arri)[count] = i+1 ;
414                  (*__arrj)[count] = j+1 ;
415               }
416            }
417            data_i_index.setValue(__arri) ;
418            data_j_index.setValue(__arrj) ;           
419            __arri.reset();
420            __arrj.reset();
421         }
422      }
423   }
424
425   //----------------------------------------------------------------
426   
427   void CDomain::completeLonLatClient(void)
428   {
429      ARRAY_CREATE(lonvalue_temp, double, 1, [0]);
430      ARRAY_CREATE(latvalue_temp, double, 1, [0]);
431     
432      const int zoom_ibegin_client  = zoom_ibegin_loc.getValue(),
433                zoom_jbegin_client  = zoom_jbegin_loc.getValue(),
434                zoom_ni_client      = zoom_ni_loc.getValue(),
435                zoom_nj_client      = zoom_nj_loc.getValue();
436               
437      ARRAY(double, 1) lonvalue_ = this->lonvalue.getValue(),
438                       latvalue_ = this->latvalue.getValue();
439               
440      if (this->data_dim.getValue() == 2)
441      {
442         StdSize dm = zoom_ni_client * zoom_nj_client;
443
444         lonvalue_temp->resize(boost::extents[dm]);
445         latvalue_temp->resize(boost::extents[dm]);
446         
447         for (int i = 0; i < zoom_ni_client; i++)
448         {
449            for (int j = 0; j < zoom_nj_client; j++)
450            {
451               (*lonvalue_temp)[i + j * zoom_ni_client] =
452               (*lonvalue_)[(i + zoom_ibegin_client -1)+(j + zoom_jbegin_client -1)*ni.getValue()];             
453               (*latvalue_temp)[i + j * zoom_ni_client] =
454               (*latvalue_)[(i + zoom_ibegin_client -1)+(j + zoom_jbegin_client -1)*ni.getValue()];
455            }
456         }
457         this->lonvalue.setValue(lonvalue_temp);
458         this->latvalue.setValue(latvalue_temp);
459      }
460      else
461      {
462         lonvalue_temp->resize(boost::extents[zoom_ni_client]);
463         latvalue_temp->resize(boost::extents[zoom_nj_client]);
464         
465         for (int i = zoom_ibegin_client - 1; i < (zoom_ni_client - zoom_ibegin_client + 1); i++)
466         {
467            (*lonvalue_temp)[i] = (*lonvalue_)[i]; 
468         }
469         
470         for (int j = zoom_ibegin_client - 1; j < (zoom_nj_client - zoom_jbegin_client + 1); j++)
471         {
472            (*latvalue_temp)[j] = (*latvalue_)[j];
473         }
474         
475         this->lonvalue.setValue(lonvalue_temp);
476         this->latvalue.setValue(latvalue_temp);
477      } 
478   }
479 
480   //----------------------------------------------------------------
481     
482   void CDomain::completeLonLatServer(void)
483   {
484      ARRAY_CREATE(lonvalue_temp, double, 1, [0]);
485      ARRAY_CREATE(latvalue_temp, double, 1, [0]);
486     
487      const int ibegin_serv     = ibegin.getValue(),
488                jbegin_serv     = jbegin.getValue(),
489                zoom_ni_serv    = zoom_ni_loc.getValue(),
490                zoom_nj_serv    = zoom_nj_loc.getValue(),
491                ibegin_zoom_srv = zoom_ibegin_loc.getValue(),
492                jbegin_zoom_srv = zoom_jbegin_loc.getValue();
493                     
494                       
495      if (this->data_dim.getValue() == 2)
496      {
497         StdSize dm = zoom_ni_serv * zoom_nj_serv;     
498         
499         lonvalue_temp->resize(boost::extents[dm]);
500         latvalue_temp->resize(boost::extents[dm]);
501         
502         for (StdSize k = 0; k < lonvalue_sub.size(); k++)
503         {
504            ARRAY(double, 1) lonvalue_loc = this->lonvalue_sub[k],
505                             latvalue_loc = this->latvalue_sub[k];
506            const int zoom_ibegin_cl = ibegin_zoom_sub[k], zoom_ni_cl = ni_zoom_sub[k],
507                      zoom_jbegin_cl = jbegin_zoom_sub[k], zoom_nj_cl = nj_zoom_sub[k],
508                      ibegin_cl = ibegin_sub[k] ,
509                      jbegin_cl = jbegin_sub[k] ,
510                      ni_cl = iend_sub[k] - ibegin_sub[k] + 1;
511                     
512            for (int i = 0; i < zoom_ni_cl; i++)
513            {
514               for (int j = 0; j < zoom_nj_cl; j++)
515               {
516                  int ii = i + (ibegin_cl-1) - (ibegin_serv - 1) + (zoom_ibegin_cl - 1) - (ibegin_zoom_srv - 1);
517                  int jj = j + (jbegin_cl-1) - (jbegin_serv - 1) + (zoom_jbegin_cl - 1) - (jbegin_zoom_srv - 1);
518                  (*lonvalue_temp)[ii + jj * zoom_ni_serv] =
519                  (*lonvalue_loc)[i + j * zoom_ni_cl];
520                  (*latvalue_temp)[ii + jj * zoom_ni_serv] = 
521                  (*latvalue_loc)[i + j * zoom_ni_cl];
522               }
523            }
524         }
525         this->lonvalue.setValue(lonvalue_temp);
526         this->latvalue.setValue(latvalue_temp);
527      }
528      else
529      {
530         lonvalue_temp->resize(boost::extents[zoom_ni_serv]);
531         latvalue_temp->resize(boost::extents[zoom_nj_serv]);
532         
533         for (StdSize k = 0; k < lonvalue_sub.size(); k++)
534         {
535            ARRAY(double, 1) lonvalue_loc = this->lonvalue_sub[k],
536                             latvalue_loc = this->latvalue_sub[k];
537            const int zoom_ibegin_cl = ibegin_zoom_sub[k], zoom_ni_cl = ni_zoom_sub[k],
538                      zoom_jbegin_cl = jbegin_zoom_sub[k], zoom_nj_cl = nj_zoom_sub[k];
539                     
540            for (int i = 0; i < zoom_ni_cl; i++)
541               (*lonvalue_temp)[i /*- (ibegin_serv - 1)*/ + (zoom_ibegin_cl - 1) - (ibegin_zoom_srv - 1)] =
542               (*lonvalue_loc)[i];
543               
544            for (int j = 0; j < zoom_nj_cl; j++)
545               (*latvalue_temp)[j /*- (jbegin_serv - 1)*/ + (zoom_jbegin_cl - 1) - (jbegin_zoom_srv - 1)] =
546               (*latvalue_loc)[j];
547         }       
548         this->lonvalue.setValue(lonvalue_temp);
549         this->latvalue.setValue(latvalue_temp);
550      }
551   }
552
553   //----------------------------------------------------------------
554
555   void CDomain::checkZoom(void)
556   {
557      // Résolution et vérification des données globales de zoom.
558      if (!this->zoom_ni.isEmpty() || !this->zoom_nj.isEmpty() ||
559          !this->zoom_ibegin.isEmpty() || !this->zoom_jbegin.isEmpty())
560      {
561         if (this->zoom_ni.isEmpty()     && this->zoom_nj.isEmpty() &&
562             this->zoom_ibegin.isEmpty() && this->zoom_jbegin.isEmpty())
563         {
564            ERROR("CDomain::checkZoom(void)",
565                  <<"Les attributs définissant un zoom doivent tous être définis") ;
566         }
567         else
568         {
569            int zoom_iend = zoom_ibegin.getValue() + zoom_ni.getValue() - 1;
570            int zoom_jend = zoom_jbegin.getValue() + zoom_nj.getValue() - 1;
571               
572            if (zoom_ibegin.getValue() < 1  || zoom_jbegin.getValue() < 1 ||
573                zoom_iend > ni_glo.getValue() || zoom_jend > nj_glo.getValue())
574               ERROR("CDomain::checkZoom(void)",
575                     << "Zoom mal défini,"
576                     << " vérifiez les valeurs zoom_ni, zoom_nj, zoom_ibegin, zoom_ibegin") ;
577         }
578      }
579      else
580      {
581         this->zoom_ni.setValue(this->ni_glo.getValue()); 
582         this->zoom_nj.setValue(this->nj_glo.getValue());
583         this->zoom_ibegin.setValue(1);
584         this->zoom_jbegin.setValue(1);
585      }
586      // Résolution des données locales de zoom.
587      {
588         int zoom_iend = zoom_ibegin.getValue() + zoom_ni.getValue() - 1;
589         int zoom_jend = zoom_jbegin.getValue() + zoom_nj.getValue() - 1;
590         
591         if ((zoom_ibegin.getValue() > iend.getValue()) || 
592             (zoom_iend < ibegin.getValue()))
593         {
594            zoom_ni_loc.setValue(0);
595            zoom_ibegin_loc.setValue(zoom_ibegin.getValue());
596         }
597         else
598         {
599            int zoom_ibegin_loc_ = (zoom_ibegin.getValue() > ibegin.getValue()) 
600                                 ? zoom_ibegin.getValue()
601                                 : ibegin.getValue();
602            int zoom_iend_loc_  = (zoom_iend < iend.getValue()) 
603                                 ? zoom_iend
604                                 : iend.getValue();
605            int zoom_ni_loc_ = zoom_iend_loc_ - zoom_ibegin_loc_ + 1;
606           
607            zoom_ni_loc.setValue(zoom_ni_loc_);
608            zoom_ibegin_loc.setValue(zoom_ibegin_loc_-ibegin.getValue()+1);
609         }
610         
611         if ((zoom_jbegin.getValue() > jend.getValue()) || 
612             (zoom_jend < jbegin.getValue()))
613         {
614            zoom_nj_loc.setValue(0);
615            zoom_jbegin_loc.setValue(zoom_jbegin.getValue());
616         }
617         else
618         {
619            int zoom_jbegin_loc_ = (zoom_jbegin.getValue() > jbegin.getValue()) 
620                                 ? zoom_jbegin.getValue()
621                                 : jbegin.getValue();
622            int zoom_jend_loc_  = (zoom_jend < jend.getValue()) 
623                                 ? zoom_jend
624                                 : jend.getValue();
625            int zoom_nj_loc_ = zoom_jend_loc_ - zoom_jbegin_loc_ + 1;
626           
627            zoom_nj_loc.setValue(zoom_nj_loc_);
628            zoom_jbegin_loc.setValue(zoom_jbegin_loc_-jbegin.getValue()+1);
629         }
630      }
631   }
632
633   //----------------------------------------------------------------
634
635   void CDomain::checkAttributes(void)
636   {
637      if (this->isChecked) return;
638      shared_ptr<CContext> context=CObjectFactory::GetObject<CContext>(CObjectFactory::GetCurrentContextId()) ;
639
640      this->checkGlobalDomain();
641      this->checkLocalIDomain();
642      this->checkLocalJDomain();
643     
644      this->checkZoom();
645     
646      if (context->hasClient)
647      { // CÃŽté client uniquement
648         this->checkMask();
649         this->checkDomainData();
650         this->checkCompression();
651         
652         this->ibegin_sub.push_back(this->ibegin.getValue());
653         this->jbegin_sub.push_back(this->jbegin.getValue());
654         this->iend_sub.push_back(this->iend.getValue());
655         this->jend_sub.push_back(this->jend.getValue()); 
656
657         this->ibegin_zoom_sub.push_back(this->zoom_ibegin_loc.getValue());
658         this->jbegin_zoom_sub.push_back(this->zoom_jbegin_loc.getValue());
659         this->ni_zoom_sub.push_back(this->zoom_ni_loc.getValue());
660         this->nj_zoom_sub.push_back(this->zoom_nj_loc.getValue());
661     
662         this->latvalue_sub.push_back(this->latvalue.getValue());
663         this->lonvalue_sub.push_back(this->lonvalue.getValue()); 
664
665
666//         if (!this->isEmpty())
667//         {
668            this->completeLonLatClient();
669//         }
670         this->completeMask();
671
672      }
673      else
674      { // CÃŽté serveur uniquement
675//         if (!this->isEmpty())
676// ne sert plus //   this->completeLonLatServer();
677      }
678   
679      if (context->hasClient)
680      {
681        computeConnectedServer() ;
682        sendServerAttribut() ;
683        sendLonLat() ;
684      }
685     
686      this->isChecked = true;
687   }
688   
689  void CDomain::sendServerAttribut(void)
690  {
691    int ni_srv=ni_glo.getValue() ;
692    int ibegin_srv=1 ;
693    int iend_srv=ni_glo.getValue() ;
694     
695    int nj_srv ;
696    int jbegin_srv ;
697    int jend_srv ;
698   
699    shared_ptr<CContext> context=CObjectFactory::GetObject<CContext>(CObjectFactory::GetCurrentContextId()) ;
700    CContextClient* client=context->client ;
701    int nbServer=client->serverSize ;
702    int serverRank=client->getServerLeader() ;
703   
704    jend_srv=0 ;
705    for(int i=0;i<=serverRank;i++)
706    {
707      jbegin_srv=jend_srv+1 ;
708      nj_srv=nj_glo.getValue()/nbServer ;
709      if (i<nj_glo.getValue()%nbServer) nj_srv++ ;
710      jend_srv=jbegin_srv+nj_srv-1 ;
711    }
712   
713     CEventClient event(getType(),EVENT_ID_SERVER_ATTRIBUT) ;   
714     if (client->isServerLeader())
715     {
716       CMessage msg ;
717       msg<<this->getId() ;
718       msg<<ni_srv<<ibegin_srv<<iend_srv<<nj_srv<<jbegin_srv<<jend_srv;
719       event.push(client->getServerLeader(),1,msg) ;
720       client->sendEvent(event) ;
721     }
722     else client->sendEvent(event) ;
723  }
724 
725  void CDomain::computeConnectedServer(void)
726  {
727    int ib,ie,in;
728    int jb,je,jn ;
729   
730    int ni_srv=ni_glo.getValue() ;
731    int ibegin_srv=1 ;
732    int iend_srv=ni_glo.getValue() ;
733     
734    int nj_serv,jbegin_srv, jend_srv ;
735    int zoom_ibegin_srv,zoom_iend_srv,zoom_ni_srv ;
736    int zoom_jbegin_srv,zoom_jend_srv,zoom_nj_srv ;
737   
738    ibegin_client=ibegin.getValue() ; iend_client=iend.getValue() ; ni_client=ni.getValue() ;
739    jbegin_client=jbegin.getValue() ; jend_client=jend.getValue() ; nj_client=nj.getValue() ;
740     
741    shared_ptr<CContext> context=CObjectFactory::GetObject<CContext>(CObjectFactory::GetCurrentContextId()) ;
742    CContextClient* client=context->client ;
743    int nbServer=client->serverSize ;
744   
745    // compute client zoom indices
746    int zoom_iend=zoom_ibegin.getValue()+zoom_ni.getValue()-1 ;
747    zoom_ibegin_client = ibegin_client > zoom_ibegin.getValue() ? ibegin_client : zoom_ibegin.getValue() ;
748    zoom_iend_client = iend_client < zoom_iend ? iend_client : zoom_iend ;
749    zoom_ni_client=zoom_iend_client-zoom_ibegin_client+1 ;
750    if (zoom_ni_client<0) zoom_ni_client=0 ;
751
752    int zoom_jend=zoom_jbegin.getValue()+zoom_nj.getValue()-1 ;
753    zoom_jbegin_client = jbegin_client > zoom_jbegin.getValue() ? jbegin_client : zoom_jbegin.getValue() ;
754    zoom_jend_client = jend_client < zoom_jend ? jend_client : zoom_jend ;
755    zoom_nj_client=zoom_jend_client-zoom_jbegin_client+1 ;
756    if (zoom_nj_client<0) zoom_nj_client=0 ;
757 
758    // find how much client are connected to a server
759    jend_srv=0 ;
760    for(int ns=0;ns<nbServer;ns++)
761    {
762      jbegin_srv=jend_srv+1 ;
763      nj_srv=nj_glo.getValue()/nbServer ;
764      if (ns<nj_glo.getValue()%nbServer) nj_srv++ ;
765      jend_srv=jbegin_srv+nj_srv-1 ;
766     
767      ib = ibegin_client>ibegin_srv ? ibegin_client : ibegin_srv ;
768      ie=  iend_client< iend_srv? iend_client : iend_srv ;
769      in=ie-ib+1 ;
770      if (in<0) in=0 ;
771     
772      jb= jbegin_client>jbegin_srv ? jbegin_client : jbegin_srv ;
773      je= jend_client<jend_srv ? jend_client : jend_srv ;
774      jn=je-jb+1 ;
775      if (jn<0) jn=0 ;
776       
777      if (in>0 && jn>0)
778      {
779        zoom_ibegin_srv = zoom_ibegin.getValue() > ibegin_srv ? zoom_ibegin.getValue() : ibegin_srv ;
780        zoom_iend_srv = zoom_iend < iend_srv ? zoom_iend : iend_srv ;
781        zoom_ni_srv=zoom_iend_srv-zoom_ibegin_srv+1 ;
782        if (zoom_ni_srv<0) zoom_ni_srv=0 ;
783     
784        zoom_jbegin_srv = zoom_jbegin.getValue() > jbegin_srv ? zoom_jbegin.getValue() : jbegin_srv ;
785        zoom_jend_srv = zoom_jend < jend_srv ? zoom_jend : jend_srv ;
786        zoom_nj_srv=zoom_jend_srv-zoom_jbegin_srv+1 ;
787        if (zoom_nj_srv<0) zoom_nj_srv=0 ;
788 
789        if (zoom_ni_srv>0 && zoom_nj_srv>0 && zoom_ni_client>0 && zoom_nj_client>0)
790        {
791          ib = zoom_ibegin_client>zoom_ibegin_srv ? zoom_ibegin_client : zoom_ibegin_srv ;
792          ie=zoom_iend_client<zoom_iend_srv?zoom_iend_client:zoom_iend_srv ;
793          in=ie-ib+1 ;
794          if (in<0) in=0 ;
795       
796          jb=zoom_jbegin_client>zoom_jbegin_srv?zoom_jbegin_client:zoom_jbegin_srv ;
797          je=zoom_jend_client<zoom_jend_srv?zoom_jend_client:zoom_jend_srv ;
798          jn=je-jb+1 ;
799          if (jn<0) jn=0 ;
800        }
801        else
802        {
803          ib=1 ; ie=0 ; in=0 ;
804          jb=1 ; je=0 ; jn=0 ;
805        }
806       
807//          if (in>0 && jn>0)
808//          {
809            connectedServer.push_back(ns) ;
810            ib_srv.push_back(ib) ;
811            ie_srv.push_back(ie) ;
812            in_srv.push_back(in) ;
813            jb_srv.push_back(jb) ;
814            je_srv.push_back(je) ;
815            jn_srv.push_back(jn) ;
816//           }
817       
818      }
819    }
820    int nbConnectedServer=connectedServer.size() ;
821    int* recvCount=new int[client->clientSize] ;
822    int* displ=new int[client->clientSize] ;
823    int* sendBuff=new int[nbConnectedServer] ;
824    valarray<int> nbClient(0,client->serverSize) ;
825   
826    for(int n=0;n<nbConnectedServer;n++) sendBuff[n]=connectedServer[n] ;
827   
828    // get connected server for everybody
829    MPI_Allgather(&nbConnectedServer,1,MPI_INT,recvCount,1,MPI_INT,client->intraComm) ;
830   
831    displ[0]=0 ;
832    for(int n=1;n<client->clientSize;n++) displ[n]=displ[n-1]+recvCount[n-1] ;
833    int recvSize=displ[client->clientSize-1]+recvCount[client->clientSize-1] ;
834    int* recvBuff=new int[recvSize] ;
835 
836   
837    MPI_Allgatherv(sendBuff,nbConnectedServer,MPI_INT,recvBuff,recvCount,displ,MPI_INT,client->intraComm) ;
838    for(int n=0;n<recvSize;n++) nbClient[recvBuff[n]]++ ;
839   
840    for(int n=0;n<nbConnectedServer;n++) nbSenders.push_back(nbClient[connectedServer[n]]) ;
841   
842    delete [] recvCount ;
843    delete [] displ ;
844    delete [] sendBuff ;
845    delete [] recvBuff ;
846  }
847 
848  void CDomain::sendLonLat(void)
849  {
850    shared_ptr<CContext> context=CObjectFactory::GetObject<CContext>(CObjectFactory::GetCurrentContextId()) ;
851    CContextClient* client=context->client ;
852    // send lon lat for each connected server
853    CEventClient event(getType(),EVENT_ID_LON_LAT) ;
854   
855    ARRAY(double, 1) lonvalue_client = lonvalue.getValue(),
856                     latvalue_client = latvalue.getValue();
857   
858    int ib,ie,in ;
859    int jb,je,jn ;
860 
861    list<shared_ptr<CMessage> > list_msg ;   
862    list<ARRAY(double,1)> list_indi,list_indj,list_lon,list_lat ;
863
864    for(int ns=0;ns<connectedServer.size();ns++)
865    {
866      ib=ib_srv[ns] ; ie=ie_srv[ns] ; in=in_srv[ns] ;
867      jb=jb_srv[ns] ; je=je_srv[ns] ; jn=jn_srv[ns] ;
868     
869      ARRAY_CREATE(indi,double,1,[in*jn]) ;
870      ARRAY_CREATE(indj,double,1,[in*jn]) ;
871      ARRAY_CREATE(lon,double,1,[in*jn]) ;
872      ARRAY_CREATE(lat,double,1,[in*jn]) ;
873
874         
875      int ind_client,ind_loc ;
876     
877      for(int j=jb;j<=je;j++)
878        for(int i=ib;i<=ie;i++)
879        {
880          ind_client=(i-zoom_ibegin_client)+(j-zoom_jbegin_client)*zoom_ni_client ;
881          ind_loc=(i-ib)+(j-jb)*in ;
882          (*lon)[ind_loc]=(*lonvalue_client)[ind_client] ;
883          (*lat)[ind_loc]=(*latvalue_client)[ind_client] ;
884          (*indi)[ind_loc]=i ;
885          (*indj)[ind_loc]=j ;
886        }
887     
888      list_indi.push_back(indi) ; list_indj.push_back(indj) ;
889      list_lon.push_back(lon) ; list_lat.push_back(lat) ;
890      list_msg.push_back(shared_ptr<CMessage>(new CMessage)) ;
891
892      *list_msg.back()<<this->getId() ;
893      *list_msg.back()<<list_indi.back()<<list_indj.back()<<list_lon.back()<<list_lat.back() ;
894      event.push(connectedServer[ns],nbSenders[ns],*list_msg.back()) ;
895    }
896
897    client->sendEvent(event) ;
898  }
899 
900  bool CDomain::dispatchEvent(CEventServer& event)
901   {
902     
903      if (SuperClass::dispatchEvent(event)) return true ;
904      else
905      {
906        switch(event.type)
907        {
908           case EVENT_ID_SERVER_ATTRIBUT :
909             recvServerAttribut(event) ;
910             return true ;
911             break ;
912           case EVENT_ID_LON_LAT :
913             recvLonLat(event) ;
914             return true ;
915             break ;
916           default :
917             ERROR("bool CContext::dispatchEvent(CEventServer& event)",
918                    <<"Unknown Event") ;
919           return false ;
920         }
921      }
922   }
923   
924  void CDomain::recvServerAttribut(CEventServer& event)
925  {
926    CBufferIn* buffer=event.subEvents.begin()->buffer;
927    string domainId ;
928    *buffer>>domainId ;
929    get(domainId)->recvServerAttribut(*buffer) ;
930  }
931 
932  void CDomain::recvServerAttribut(CBufferIn& buffer)
933  {
934    int zoom_iend=zoom_ibegin.getValue()+zoom_ni.getValue()-1 ;
935    int zoom_jend=zoom_jbegin.getValue()+zoom_nj.getValue()-1 ;
936
937     buffer>>ni_srv>>ibegin_srv>>iend_srv>>nj_srv>>jbegin_srv>>jend_srv;
938   
939    zoom_ibegin_srv = zoom_ibegin.getValue() > ibegin_srv ? zoom_ibegin.getValue() : ibegin_srv ;
940    zoom_iend_srv = zoom_iend < iend_srv ? zoom_iend : iend_srv ;
941    zoom_ni_srv=zoom_iend_srv-zoom_ibegin_srv+1 ;
942     
943    zoom_jbegin_srv = zoom_jbegin.getValue() > jbegin_srv ? zoom_jbegin.getValue() : jbegin_srv ;
944    zoom_jend_srv = zoom_jend < jend_srv ? zoom_jend : jend_srv ;
945    zoom_nj_srv=zoom_jend_srv-zoom_jbegin_srv+1 ;
946
947    if (zoom_ni_srv<=0 || zoom_nj_srv<=0) 
948    {
949      zoom_ibegin_srv=1 ; zoom_iend_srv=0 ; zoom_ni_srv=0 ;
950      zoom_jbegin_srv=1 ; zoom_jend_srv=0 ; zoom_nj_srv=0 ;
951    }
952   
953    lonvalue_srv->resize(extents[zoom_ni_srv*zoom_nj_srv]) ;
954    latvalue_srv->resize(extents[zoom_ni_srv*zoom_nj_srv]) ;
955  }
956   
957  void CDomain::recvLonLat(CEventServer& event)
958  {
959    list<CEventServer::SSubEvent>::iterator it ;
960    for (it=event.subEvents.begin();it!=event.subEvents.end();++it)
961    {
962      CBufferIn* buffer=it->buffer;
963      string domainId ;
964      *buffer>>domainId ;
965      get(domainId)->recvLonLat(*buffer) ;
966    }
967  }
968 
969  void CDomain::recvLonLat(CBufferIn& buffer)
970  {
971    ARRAY_CREATE(indi,double,1,[0]) ;
972    ARRAY_CREATE(indj,double,1,[0]) ;
973    ARRAY_CREATE(lon,double,1,[0]) ;
974    ARRAY_CREATE(lat,double,1,[0]) ;
975   
976    buffer>>indi>>indj>>lon>>lat ;
977    int i,j,ind_srv ;
978
979    for(int ind=0;ind<indi->num_elements();ind++)
980    {
981      i=(*indi)[ind] ; j=(*indj)[ind] ;
982      ind_srv=(i-zoom_ibegin_srv)+(j-zoom_jbegin_srv)*zoom_ni_srv ;
983      (*lonvalue_srv)[ind_srv]=(*lon)[ind] ;
984      (*latvalue_srv)[ind_srv]=(*lat)[ind] ;
985    }
986  }
987   //----------------------------------------------------------------
988   
989   void CDomain::completeMask(void)
990   {
991      this->local_mask->resize(boost::extents[zoom_ni_loc.getValue()][zoom_nj_loc.getValue()]);
992   }
993
994   //----------------------------------------------------------------
995
996   ARRAY(int, 2) CDomain::getLocalMask(void) const
997   {
998      return (this->local_mask);
999   }
1000   
1001   //----------------------------------------------------------------
1002   
1003   const std::vector<int> & CDomain::getIBeginSub(void) const
1004   {
1005      return (this->ibegin_sub);
1006   }
1007   
1008   //----------------------------------------------------------------
1009   
1010   const std::vector<int> & CDomain::getIBeginZoomSub(void) const
1011   {
1012      return (this->ibegin_zoom_sub);
1013   }
1014
1015   const std::vector<int> & CDomain::getNiZoomSub(void) const
1016   {
1017      return (this->ni_zoom_sub);
1018   }
1019               
1020   //----------------------------------------------------------------
1021                     
1022   const std::vector<int> & CDomain::getIEndSub(void) const
1023   {
1024      return (this->iend_sub);
1025   }
1026   
1027   //----------------------------------------------------------------
1028   
1029   const std::vector<int> & CDomain::getJBeginSub(void) const
1030   {
1031      return (this->jbegin_sub);
1032   }
1033   
1034   //----------------------------------------------------------------
1035     
1036   const std::vector<int> & CDomain::getJBeginZoomSub(void) const
1037   {
1038      return (this->jbegin_zoom_sub);
1039   }
1040
1041   const std::vector<int> & CDomain::getNjZoomSub(void) const
1042   {
1043      return (this->nj_zoom_sub);
1044   }
1045                 
1046   
1047   //----------------------------------------------------------------
1048   
1049   const std::vector<int> & CDomain::getJEndSub(void) const
1050   {
1051      return (this->jend_sub);
1052   }
1053   
1054   //----------------------------------------------------------------
1055   
1056   const std::vector<ARRAY(double, 1)> & CDomain::getLonValueSub(void) const
1057   {
1058      return (this->lonvalue_sub);
1059   }
1060   
1061   //----------------------------------------------------------------
1062   
1063   const std::vector<ARRAY(double, 1)> & CDomain::getLatValueSub(void) const
1064   {
1065      return (this->latvalue_sub);
1066   }   
1067   
1068   ///---------------------------------------------------------------
1069
1070} // namespace tree
1071} // namespace xmlioserver
Note: See TracBrowser for help on using the repository browser.