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

Last change on this file since 421 was 421, checked in by ymipsl, 11 years ago

suppress warning due to french accents -> english traduction of error messages

YM

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