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

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

enhancement : error message more explicit with non compliant mask size

YM

File size: 35.8 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"<<endl
268                   <<"Local size is "<<niu<<"x"<<nju<<endl
269                  <<"Mask size is "<<mask.extent(0)<<"x"<<mask.extent(1));                 
270         for (int i = 0; i < ni.getValue(); i++)
271         {
272            for (int j = 0; j < nj.getValue(); j++)
273            {
274               if (i < ibegin_mask && i > iend_mask &&
275                   j < jbegin_mask && j > jend_mask )
276                     mask(i,j) = false;
277            }
278         }
279      }
280      else // (!mask.hasValue())
281      { // Si aucun masque n'est défini,
282        // on en crée un nouveau qui valide l'intégralité du domaine.
283         mask.resize(ni,nj) ;
284         for (int i = 0; i < ni.getValue(); i++)
285         {
286            for (int j = 0; j < nj.getValue(); j++)
287            {
288               if (i >= ibegin_mask && i <= iend_mask &&
289                   j >= jbegin_mask && j <= jend_mask )
290                     mask(i,j) = true;
291               else  mask(i,j) = false;
292            }
293         }
294      }
295   }
296
297
298   //----------------------------------------------------------------
299
300   void CDomain::checkDomainData(void)
301   {     
302      if (!data_dim.isEmpty() &&
303         !(data_dim.getValue() == 1 || data_dim.getValue() == 2))
304      {
305         ERROR("CDomain::checkAttributes(void)",
306               << "Data dimension incompatible (must be 1 or 2) !") ;
307      }
308      else if (data_dim.isEmpty())
309      {
310         ERROR("CDomain::checkAttributes(void)",
311               << "Data dimension undefined !") ;
312      }
313
314      if (data_ibegin.isEmpty())
315         data_ibegin.setValue(0) ;
316      if (data_jbegin.isEmpty() && (data_dim.getValue() == 2))
317         data_jbegin.setValue(0) ;
318
319      if (!data_ni.isEmpty() && (data_ni.getValue() <= 0))
320      {
321         ERROR("CDomain::checkAttributes(void)",
322               << "Data dimension is negative (data_ni).") ;
323      }
324      else if (data_ni.isEmpty())
325      {
326         data_ni.setValue((data_dim.getValue() == 1)
327                           ? (ni.getValue() * nj.getValue())
328                           : ni.getValue());
329      }
330
331      if (data_dim.getValue() == 2)
332      {
333         if (!data_nj.isEmpty() && (data_nj.getValue() <= 0) )
334         {
335            ERROR("CDomain::checkAttributes(void)",
336                  << "Data dimension is negative (data_nj).") ;
337         }
338         else if (data_nj.isEmpty())
339            data_nj.setValue(nj.getValue()) ;
340      }
341
342   }
343
344   //----------------------------------------------------------------
345
346   void CDomain::checkCompression(void)
347   {
348      if (!data_i_index.isEmpty())
349      {
350         int ssize = data_i_index.numElements();
351         if (!data_n_index.isEmpty() &&
352            (data_n_index.getValue() != ssize))
353         {
354            ERROR("CDomain::checkAttributes(void)",
355                  <<"Dimension data_i_index incompatible with data_n_index.") ;
356         }
357         else if (data_n_index.isEmpty())
358            data_n_index.setValue(ssize) ;
359
360         if (data_dim.getValue() == 2)
361         {
362            if (!data_j_index.isEmpty() &&
363               (data_j_index.numElements() != data_i_index.numElements()))
364            {
365               ERROR("CDomain::checkAttributes(void)",
366                     <<"Dimension data_j_index incompatible with data_i_index.") ;
367            }
368            else if (data_j_index.isEmpty())
369            {
370               ERROR("CDomain::checkAttributes(void)",
371                     <<"data_j_index must be defined !") ;
372            }
373         }
374      }
375      else
376      {
377         if (!data_n_index.isEmpty() ||
378            ((data_dim.getValue() == 2) && (!data_j_index.isEmpty())))
379            ERROR("CDomain::checkAttributes(void)", << "data_i_index undefined") ;
380      }
381
382      if (data_n_index.isEmpty())
383      { // -> bloc re-vérifié OK
384         if (data_dim.getValue() == 1)
385         {
386            const int dni = data_ni.getValue();
387            data_i_index.resize(dni) ;
388            data_n_index.setValue(dni);
389            for (int i = 0; i < dni; i++) data_i_index(i) = i+1 ;
390         }
391         else   // (data_dim == 2)
392         {
393            const int dni = data_ni.getValue() * data_nj.getValue();
394            data_i_index.resize(dni) ;
395            data_j_index.resize(dni) ;
396           
397            data_n_index.setValue(dni);
398           
399            for(int count = 0, j = 0; j  < data_nj.getValue(); j++)
400            {
401               for(int i = 0; i < data_ni.getValue(); i++, count++)
402               {
403                  data_i_index(count) = i+1 ;
404                  data_j_index(count) = j+1 ;
405               }
406            }
407         }
408      }
409   }
410
411   //----------------------------------------------------------------
412   
413   void CDomain::completeLonLatClient(void)
414   {
415      int i,j,k ;
416      CArray<double,1> lonvalue_temp(ni*nj) ;
417      CArray<double,1> latvalue_temp(ni*nj) ;
418     
419      if ( lonvalue.numElements() == ni*nj && latvalue.numElements() == ni*nj ) isCurvilinear=true ;
420      else if ( lonvalue.numElements() == ni && latvalue.numElements() == nj ) isCurvilinear=false ;
421      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
422                                                            <<"lonvalue size = " << lonvalue.numElements() << "different of ni or ni*nj"<<endl
423                                                            <<"latvalue size = " << latvalue.numElements() << "different of nj or ni*nj" ) ;
424      if (isCurvilinear)
425      {
426        lonvalue_temp=lonvalue ;
427        latvalue_temp=latvalue ;
428      }
429      else
430      {
431        for(j=0;j<nj;j++)
432          for(i=0;i<ni;i++) 
433          {
434            k=j*ni+i ;
435            lonvalue_temp(k)=lonvalue(i) ;
436            latvalue_temp(k)=latvalue(j) ;
437          }
438      }
439         
440       
441      const int zoom_ibegin_client  = zoom_ibegin_loc.getValue(),
442                zoom_jbegin_client  = zoom_jbegin_loc.getValue(),
443                zoom_ni_client      = zoom_ni_loc.getValue(),
444                zoom_nj_client      = zoom_nj_loc.getValue();
445               
446      StdSize dm = zoom_ni_client * zoom_nj_client;
447
448      lonvalue.resize(dm);
449      latvalue.resize(dm);
450         
451      for (int i = 0; i < zoom_ni_client; i++)
452      {
453        for (int j = 0; j < zoom_nj_client; j++)
454        {
455          lonvalue(i + j * zoom_ni_client) = lonvalue_temp( (i + zoom_ibegin_client -1) + (j + zoom_jbegin_client -1)*ni.getValue() ); 
456          latvalue(i + j * zoom_ni_client) = latvalue_temp( (i + zoom_ibegin_client -1)+(j + zoom_jbegin_client -1)*ni.getValue() );
457        }
458      }
459   }
460 
461   //----------------------------------------------------------------
462     
463   void CDomain::completeLonLatServer(void)
464   {
465      CArray<double,1> lonvalue_temp ;
466      CArray<double,1> latvalue_temp ;
467     
468      const int ibegin_serv     = ibegin.getValue(),
469                jbegin_serv     = jbegin.getValue(),
470                zoom_ni_serv    = zoom_ni_loc.getValue(),
471                zoom_nj_serv    = zoom_nj_loc.getValue(),
472                ibegin_zoom_srv = zoom_ibegin_loc.getValue(),
473                jbegin_zoom_srv = zoom_jbegin_loc.getValue();
474                     
475                       
476      if (this->data_dim.getValue() == 2)
477      {
478         StdSize dm = zoom_ni_serv * zoom_nj_serv;     
479         
480         lonvalue_temp.resize(dm);
481         latvalue_temp.resize(dm);
482         
483         for (StdSize k = 0; k < lonvalue_sub.size(); k++)
484         {
485            CArray<double,1> lonvalue_loc(*(lonvalue_sub[k])) ;
486            CArray<double,1> latvalue_loc (*(latvalue_sub[k]));
487           
488            const int zoom_ibegin_cl = ibegin_zoom_sub[k], zoom_ni_cl = ni_zoom_sub[k],
489                      zoom_jbegin_cl = jbegin_zoom_sub[k], zoom_nj_cl = nj_zoom_sub[k],
490                      ibegin_cl = ibegin_sub[k] ,
491                      jbegin_cl = jbegin_sub[k] ,
492                      ni_cl = iend_sub[k] - ibegin_sub[k] + 1;
493                     
494            for (int i = 0; i < zoom_ni_cl; i++)
495            {
496               for (int j = 0; j < zoom_nj_cl; j++)
497               {
498                  int ii = i + (ibegin_cl-1) - (ibegin_serv - 1) + (zoom_ibegin_cl - 1) - (ibegin_zoom_srv - 1);
499                  int jj = j + (jbegin_cl-1) - (jbegin_serv - 1) + (zoom_jbegin_cl - 1) - (jbegin_zoom_srv - 1);
500                  lonvalue_temp(ii + jj * zoom_ni_serv) = lonvalue_loc(i + j * zoom_ni_cl);
501                  latvalue_temp(ii + jj * zoom_ni_serv) = latvalue_loc(i + j * zoom_ni_cl);
502               }
503            }
504         }
505         
506         lonvalue.reference(lonvalue_temp.copy()) ;
507         latvalue.reference(latvalue_temp.copy()) ;
508      }
509      else
510      {
511         lonvalue_temp.resize(zoom_ni_serv);
512         latvalue_temp.resize(zoom_nj_serv);
513         
514         for (StdSize k = 0; k < lonvalue_sub.size(); k++)
515         {
516            CArray<double,1> lonvalue_loc(*(lonvalue_sub[k]));
517            CArray<double,1> latvalue_loc(*(latvalue_sub[k]));
518           
519            const int zoom_ibegin_cl = ibegin_zoom_sub[k], zoom_ni_cl = ni_zoom_sub[k],
520                      zoom_jbegin_cl = jbegin_zoom_sub[k], zoom_nj_cl = nj_zoom_sub[k];
521                     
522            for (int i = 0; i < zoom_ni_cl; i++)
523              lonvalue_temp(i /*- (ibegin_serv - 1)*/ + (zoom_ibegin_cl - 1) - (ibegin_zoom_srv - 1)) = lonvalue_loc(i);
524               
525            for (int j = 0; j < zoom_nj_cl; j++)
526              latvalue_temp(j /*- (jbegin_serv - 1)*/ + (zoom_jbegin_cl - 1) - (jbegin_zoom_srv - 1)) = latvalue_loc(j);
527         }       
528         lonvalue.reference(lonvalue_temp.copy()) ;
529         latvalue.reference(latvalue_temp.copy()) ;
530      }
531   }
532
533   //----------------------------------------------------------------
534
535   void CDomain::checkZoom(void)
536   {
537      // Résolution et vérification des données globales de zoom.
538      if (!this->zoom_ni.isEmpty() || !this->zoom_nj.isEmpty() ||
539          !this->zoom_ibegin.isEmpty() || !this->zoom_jbegin.isEmpty())
540      {
541         if (this->zoom_ni.isEmpty()     || this->zoom_nj.isEmpty() ||
542             this->zoom_ibegin.isEmpty() || this->zoom_jbegin.isEmpty())
543         {
544            ERROR("CDomain::checkZoom(void)",
545                  <<"if one of zoom attributes is defined then all zoom attributes must be defined") ;
546         }
547         else
548         {
549            int zoom_iend = zoom_ibegin.getValue() + zoom_ni.getValue() - 1;
550            int zoom_jend = zoom_jbegin.getValue() + zoom_nj.getValue() - 1;
551               
552            if (zoom_ibegin.getValue() < 1  || zoom_jbegin.getValue() < 1 ||
553                zoom_iend > ni_glo.getValue() || zoom_jend > nj_glo.getValue())
554               ERROR("CDomain::checkZoom(void)",
555                     << "Zoom is wrong defined,"
556                     << " Check the values : zoom_ni, zoom_nj, zoom_ibegin, zoom_ibegin") ;
557         }
558      }
559      else
560      {
561         this->zoom_ni.setValue(this->ni_glo.getValue()); 
562         this->zoom_nj.setValue(this->nj_glo.getValue());
563         this->zoom_ibegin.setValue(1);
564         this->zoom_jbegin.setValue(1);
565      }
566      // Résolution des données locales de zoom.
567      {
568         int zoom_iend = zoom_ibegin.getValue() + zoom_ni.getValue() - 1;
569         int zoom_jend = zoom_jbegin.getValue() + zoom_nj.getValue() - 1;
570         
571         if ((zoom_ibegin.getValue() > iend.getValue()) || 
572             (zoom_iend < ibegin.getValue()))
573         {
574            zoom_ni_loc.setValue(0);
575            zoom_ibegin_loc.setValue(zoom_ibegin.getValue());
576         }
577         else
578         {
579            int zoom_ibegin_loc_ = (zoom_ibegin.getValue() > ibegin.getValue()) 
580                                 ? zoom_ibegin.getValue()
581                                 : ibegin.getValue();
582            int zoom_iend_loc_  = (zoom_iend < iend.getValue()) 
583                                 ? zoom_iend
584                                 : iend.getValue();
585            int zoom_ni_loc_ = zoom_iend_loc_ - zoom_ibegin_loc_ + 1;
586           
587            zoom_ni_loc.setValue(zoom_ni_loc_);
588            zoom_ibegin_loc.setValue(zoom_ibegin_loc_-ibegin.getValue()+1);
589         }
590         
591         if ((zoom_jbegin.getValue() > jend.getValue()) || 
592             (zoom_jend < jbegin.getValue()))
593         {
594            zoom_nj_loc.setValue(0);
595            zoom_jbegin_loc.setValue(zoom_jbegin.getValue());
596         }
597         else
598         {
599            int zoom_jbegin_loc_ = (zoom_jbegin.getValue() > jbegin.getValue()) 
600                                 ? zoom_jbegin.getValue()
601                                 : jbegin.getValue();
602            int zoom_jend_loc_  = (zoom_jend < jend.getValue()) 
603                                 ? zoom_jend
604                                 : jend.getValue();
605            int zoom_nj_loc_ = zoom_jend_loc_ - zoom_jbegin_loc_ + 1;
606           
607            zoom_nj_loc.setValue(zoom_nj_loc_);
608            zoom_jbegin_loc.setValue(zoom_jbegin_loc_-jbegin.getValue()+1);
609         }
610      }
611   }
612
613   //----------------------------------------------------------------
614
615   void CDomain::checkAttributes(void)
616   {
617      if (this->isChecked) return;
618      CContext* context=CContext::getCurrent() ;
619
620      this->checkGlobalDomain();
621      this->checkLocalIDomain();
622      this->checkLocalJDomain();
623     
624      this->checkZoom();
625     
626      if (context->hasClient)
627      { // CÃŽté client uniquement
628         this->checkMask();
629         this->checkDomainData();
630         this->checkCompression();
631         
632         this->ibegin_sub.push_back(this->ibegin.getValue());
633         this->jbegin_sub.push_back(this->jbegin.getValue());
634         this->iend_sub.push_back(this->iend.getValue());
635         this->jend_sub.push_back(this->jend.getValue()); 
636
637         this->ibegin_zoom_sub.push_back(this->zoom_ibegin_loc.getValue());
638         this->jbegin_zoom_sub.push_back(this->zoom_jbegin_loc.getValue());
639         this->ni_zoom_sub.push_back(this->zoom_ni_loc.getValue());
640         this->nj_zoom_sub.push_back(this->zoom_nj_loc.getValue());
641     
642         this->latvalue_sub.push_back(new CArray<double,1>(latvalue.copy()));
643         this->lonvalue_sub.push_back(new CArray<double,1>(lonvalue.copy())); 
644
645
646//         if (!this->isEmpty())
647//         {
648            this->completeLonLatClient();
649//         }
650         this->completeMask();
651
652      }
653      else
654      { // CÃŽté serveur uniquement
655//         if (!this->isEmpty())
656// ne sert plus //   this->completeLonLatServer();
657      }
658   
659      if (context->hasClient)
660      {
661        computeConnectedServer() ;
662        sendServerAttribut() ;
663        sendLonLat() ;
664      }
665     
666      this->isChecked = true;
667   }
668   
669  void CDomain::sendServerAttribut(void)
670  {
671    int ni_srv=ni_glo.getValue() ;
672    int ibegin_srv=1 ;
673    int iend_srv=ni_glo.getValue() ;
674     
675    int nj_srv ;
676    int jbegin_srv ;
677    int jend_srv ;
678   
679    CContext* context=CContext::getCurrent() ;
680    CContextClient* client=context->client ;
681    int nbServer=client->serverSize ;
682    int serverRank=client->getServerLeader() ;
683   
684    jend_srv=0 ;
685    for(int i=0;i<=serverRank;i++)
686    {
687      jbegin_srv=jend_srv+1 ;
688      nj_srv=nj_glo.getValue()/nbServer ;
689      if (i<nj_glo.getValue()%nbServer) nj_srv++ ;
690      jend_srv=jbegin_srv+nj_srv-1 ;
691    }
692   
693     CEventClient event(getType(),EVENT_ID_SERVER_ATTRIBUT) ;   
694     if (client->isServerLeader())
695     {
696       CMessage msg ;
697       msg<<this->getId() ;
698       msg<<ni_srv<<ibegin_srv<<iend_srv<<nj_srv<<jbegin_srv<<jend_srv;
699       event.push(client->getServerLeader(),1,msg) ;
700       client->sendEvent(event) ;
701     }
702     else client->sendEvent(event) ;
703  }
704 
705  void CDomain::computeConnectedServer(void)
706  {
707    int ib,ie,in;
708    int jb,je,jn ;
709   
710    int ni_srv=ni_glo.getValue() ;
711    int ibegin_srv=1 ;
712    int iend_srv=ni_glo.getValue() ;
713     
714    int nj_serv,jbegin_srv, jend_srv ;
715    int zoom_ibegin_srv,zoom_iend_srv,zoom_ni_srv ;
716    int zoom_jbegin_srv,zoom_jend_srv,zoom_nj_srv ;
717   
718    ibegin_client=ibegin.getValue() ; iend_client=iend.getValue() ; ni_client=ni.getValue() ;
719    jbegin_client=jbegin.getValue() ; jend_client=jend.getValue() ; nj_client=nj.getValue() ;
720     
721    CContext* context = CContext::getCurrent() ;
722    CContextClient* client=context->client ;
723    int nbServer=client->serverSize ;
724   
725    // compute client zoom indices
726    int zoom_iend=zoom_ibegin.getValue()+zoom_ni.getValue()-1 ;
727    zoom_ibegin_client = ibegin_client > zoom_ibegin.getValue() ? ibegin_client : zoom_ibegin.getValue() ;
728    zoom_iend_client = iend_client < zoom_iend ? iend_client : zoom_iend ;
729    zoom_ni_client=zoom_iend_client-zoom_ibegin_client+1 ;
730    if (zoom_ni_client<0) zoom_ni_client=0 ;
731
732    int zoom_jend=zoom_jbegin.getValue()+zoom_nj.getValue()-1 ;
733    zoom_jbegin_client = jbegin_client > zoom_jbegin.getValue() ? jbegin_client : zoom_jbegin.getValue() ;
734    zoom_jend_client = jend_client < zoom_jend ? jend_client : zoom_jend ;
735    zoom_nj_client=zoom_jend_client-zoom_jbegin_client+1 ;
736    if (zoom_nj_client<0) zoom_nj_client=0 ;
737 
738    // find how much client are connected to a server
739    jend_srv=0 ;
740    for(int ns=0;ns<nbServer;ns++)
741    {
742      jbegin_srv=jend_srv+1 ;
743      nj_srv=nj_glo.getValue()/nbServer ;
744      if (ns<nj_glo.getValue()%nbServer) nj_srv++ ;
745      jend_srv=jbegin_srv+nj_srv-1 ;
746     
747      ib = ibegin_client>ibegin_srv ? ibegin_client : ibegin_srv ;
748      ie=  iend_client< iend_srv? iend_client : iend_srv ;
749      in=ie-ib+1 ;
750      if (in<0) in=0 ;
751     
752      jb= jbegin_client>jbegin_srv ? jbegin_client : jbegin_srv ;
753      je= jend_client<jend_srv ? jend_client : jend_srv ;
754      jn=je-jb+1 ;
755      if (jn<0) jn=0 ;
756       
757      if (in>0 && jn>0)
758      {
759        zoom_ibegin_srv = zoom_ibegin.getValue() > ibegin_srv ? zoom_ibegin.getValue() : ibegin_srv ;
760        zoom_iend_srv = zoom_iend < iend_srv ? zoom_iend : iend_srv ;
761        zoom_ni_srv=zoom_iend_srv-zoom_ibegin_srv+1 ;
762        if (zoom_ni_srv<0) zoom_ni_srv=0 ;
763     
764        zoom_jbegin_srv = zoom_jbegin.getValue() > jbegin_srv ? zoom_jbegin.getValue() : jbegin_srv ;
765        zoom_jend_srv = zoom_jend < jend_srv ? zoom_jend : jend_srv ;
766        zoom_nj_srv=zoom_jend_srv-zoom_jbegin_srv+1 ;
767        if (zoom_nj_srv<0) zoom_nj_srv=0 ;
768 
769        if (zoom_ni_srv>0 && zoom_nj_srv>0 && zoom_ni_client>0 && zoom_nj_client>0)
770        {
771          ib = zoom_ibegin_client>zoom_ibegin_srv ? zoom_ibegin_client : zoom_ibegin_srv ;
772          ie=zoom_iend_client<zoom_iend_srv?zoom_iend_client:zoom_iend_srv ;
773          in=ie-ib+1 ;
774          if (in<0) in=0 ;
775       
776          jb=zoom_jbegin_client>zoom_jbegin_srv?zoom_jbegin_client:zoom_jbegin_srv ;
777          je=zoom_jend_client<zoom_jend_srv?zoom_jend_client:zoom_jend_srv ;
778          jn=je-jb+1 ;
779          if (jn<0) jn=0 ;
780        }
781        else
782        {
783          ib=1 ; ie=0 ; in=0 ;
784          jb=1 ; je=0 ; jn=0 ;
785        }
786       
787//          if (in>0 && jn>0)
788//          {
789            connectedServer.push_back(ns) ;
790            ib_srv.push_back(ib) ;
791            ie_srv.push_back(ie) ;
792            in_srv.push_back(in) ;
793            jb_srv.push_back(jb) ;
794            je_srv.push_back(je) ;
795            jn_srv.push_back(jn) ;
796//           }
797       
798      }
799    }
800    int nbConnectedServer=connectedServer.size() ;
801    int* recvCount=new int[client->clientSize] ;
802    int* displ=new int[client->clientSize] ;
803    int* sendBuff=new int[nbConnectedServer] ;
804    valarray<int> nbClient(0,client->serverSize) ;
805   
806    for(int n=0;n<nbConnectedServer;n++) sendBuff[n]=connectedServer[n] ;
807   
808    // get connected server for everybody
809    MPI_Allgather(&nbConnectedServer,1,MPI_INT,recvCount,1,MPI_INT,client->intraComm) ;
810   
811    displ[0]=0 ;
812    for(int n=1;n<client->clientSize;n++) displ[n]=displ[n-1]+recvCount[n-1] ;
813    int recvSize=displ[client->clientSize-1]+recvCount[client->clientSize-1] ;
814    int* recvBuff=new int[recvSize] ;
815 
816   
817    MPI_Allgatherv(sendBuff,nbConnectedServer,MPI_INT,recvBuff,recvCount,displ,MPI_INT,client->intraComm) ;
818    for(int n=0;n<recvSize;n++) nbClient[recvBuff[n]]++ ;
819   
820    for(int n=0;n<nbConnectedServer;n++) nbSenders.push_back(nbClient[connectedServer[n]]) ;
821   
822    delete [] recvCount ;
823    delete [] displ ;
824    delete [] sendBuff ;
825    delete [] recvBuff ;
826  }
827 
828  void CDomain::sendLonLat(void)
829  {
830    CContext* context = CContext::getCurrent() ;
831    CContextClient* client=context->client ;
832    // send lon lat for each connected server
833    CEventClient event(getType(),EVENT_ID_LON_LAT) ;
834   
835    int ib,ie,in ;
836    int jb,je,jn ;
837 
838    list<shared_ptr<CMessage> > list_msg ;   
839    list<CArray<double,1>* > list_indi,list_indj,list_lon,list_lat ;
840
841    for(int ns=0;ns<connectedServer.size();ns++)
842    {
843      ib=ib_srv[ns] ; ie=ie_srv[ns] ; in=in_srv[ns] ;
844      jb=jb_srv[ns] ; je=je_srv[ns] ; jn=jn_srv[ns] ;
845     
846      CArray<double,1> indi(in*jn) ;
847      CArray<double,1> indj(in*jn) ;
848      CArray<double,1> lon(in*jn) ;
849      CArray<double,1> lat(in*jn) ;
850
851         
852      int ind_client,ind_loc ;
853     
854      for(int j=jb;j<=je;j++)
855        for(int i=ib;i<=ie;i++)
856        {
857          ind_client=(i-zoom_ibegin_client)+(j-zoom_jbegin_client)*zoom_ni_client ;
858          ind_loc=(i-ib)+(j-jb)*in ;
859          lon(ind_loc)=lonvalue(ind_client) ;
860          lat(ind_loc)=latvalue(ind_client) ;
861          indi(ind_loc)=i ;
862          indj(ind_loc)=j ;
863        }
864     
865      list_indi.push_back(new CArray<double,1>(indi.copy())) ;
866      list_indj.push_back(new CArray<double,1>(indj.copy())) ;
867      list_lon.push_back(new CArray<double,1>(lon.copy())) ;
868      list_lat.push_back(new CArray<double,1>(lat.copy())) ;
869
870      list_msg.push_back(shared_ptr<CMessage>(new CMessage)) ;
871
872      *list_msg.back()<<this->getId()<<isCurvilinear ;
873      *list_msg.back()<<*list_indi.back()<<*list_indj.back()<<*list_lon.back()<<*list_lat.back() ;
874      event.push(connectedServer[ns],nbSenders[ns],*list_msg.back()) ;
875    }
876
877    client->sendEvent(event) ;
878   
879    list<CArray<double,1>* >::iterator it;
880    for(it=list_indi.begin();it!=list_indi.end();it++) delete *it;
881    for(it=list_indj.begin();it!=list_indj.end();it++) delete *it;
882    for(it=list_lon.begin();it!=list_lon.end();it++)   delete *it;
883    for(it=list_lat.begin();it!=list_lat.end();it++)   delete *it;
884   
885  }
886 
887  bool CDomain::dispatchEvent(CEventServer& event)
888   {
889     
890      if (SuperClass::dispatchEvent(event)) return true ;
891      else
892      {
893        switch(event.type)
894        {
895           case EVENT_ID_SERVER_ATTRIBUT :
896             recvServerAttribut(event) ;
897             return true ;
898             break ;
899           case EVENT_ID_LON_LAT :
900             recvLonLat(event) ;
901             return true ;
902             break ;
903           default :
904             ERROR("bool CContext::dispatchEvent(CEventServer& event)",
905                    <<"Unknown Event") ;
906           return false ;
907         }
908      }
909   }
910   
911  void CDomain::recvServerAttribut(CEventServer& event)
912  {
913    CBufferIn* buffer=event.subEvents.begin()->buffer;
914    string domainId ;
915    *buffer>>domainId ;
916    get(domainId)->recvServerAttribut(*buffer) ;
917  }
918 
919  void CDomain::recvServerAttribut(CBufferIn& buffer)
920  {
921    int zoom_iend=zoom_ibegin.getValue()+zoom_ni.getValue()-1 ;
922    int zoom_jend=zoom_jbegin.getValue()+zoom_nj.getValue()-1 ;
923
924     buffer>>ni_srv>>ibegin_srv>>iend_srv>>nj_srv>>jbegin_srv>>jend_srv;
925   
926    zoom_ibegin_srv = zoom_ibegin.getValue() > ibegin_srv ? zoom_ibegin.getValue() : ibegin_srv ;
927    zoom_iend_srv = zoom_iend < iend_srv ? zoom_iend : iend_srv ;
928    zoom_ni_srv=zoom_iend_srv-zoom_ibegin_srv+1 ;
929     
930    zoom_jbegin_srv = zoom_jbegin.getValue() > jbegin_srv ? zoom_jbegin.getValue() : jbegin_srv ;
931    zoom_jend_srv = zoom_jend < jend_srv ? zoom_jend : jend_srv ;
932    zoom_nj_srv=zoom_jend_srv-zoom_jbegin_srv+1 ;
933
934    if (zoom_ni_srv<=0 || zoom_nj_srv<=0) 
935    {
936      zoom_ibegin_srv=1 ; zoom_iend_srv=0 ; zoom_ni_srv=0 ;
937      zoom_jbegin_srv=1 ; zoom_jend_srv=0 ; zoom_nj_srv=0 ;
938    }
939   
940    lonvalue_srv.resize(zoom_ni_srv*zoom_nj_srv) ;
941    latvalue_srv.resize(zoom_ni_srv*zoom_nj_srv) ;
942  }
943   
944  void CDomain::recvLonLat(CEventServer& event)
945  {
946    list<CEventServer::SSubEvent>::iterator it ;
947    for (it=event.subEvents.begin();it!=event.subEvents.end();++it)
948    {
949      CBufferIn* buffer=it->buffer;
950      string domainId ;
951      *buffer>>domainId ;
952      get(domainId)->recvLonLat(*buffer) ;
953    }
954  }
955 
956  void CDomain::recvLonLat(CBufferIn& buffer)
957  {
958    CArray<double,1> indi ;
959    CArray<double,1> indj ;
960    CArray<double,1> lon ;
961    CArray<double,1> lat ;
962     
963    buffer>>isCurvilinear>>indi>>indj>>lon>>lat ;
964
965    int i,j,ind_srv ;
966
967    for(int ind=0;ind<indi.numElements();ind++)
968    {
969      i=indi(ind) ; j=indj(ind) ;
970      ind_srv=(i-zoom_ibegin_srv)+(j-zoom_jbegin_srv)*zoom_ni_srv ;
971      lonvalue_srv(ind_srv)=lon(ind) ;
972      latvalue_srv(ind_srv)=lat(ind) ;
973    }
974  }
975   //----------------------------------------------------------------
976   
977   void CDomain::completeMask(void)
978   {
979      this->local_mask.resize(zoom_ni_loc,zoom_nj_loc);
980   }
981
982   //----------------------------------------------------------------
983
984   CArray<int,2> CDomain::getLocalMask(void) const
985   {
986      return (this->local_mask);
987   }
988   
989   //----------------------------------------------------------------
990   
991   const std::vector<int> & CDomain::getIBeginSub(void) const
992   {
993      return (this->ibegin_sub);
994   }
995   
996   //----------------------------------------------------------------
997   
998   const std::vector<int> & CDomain::getIBeginZoomSub(void) const
999   {
1000      return (this->ibegin_zoom_sub);
1001   }
1002
1003   const std::vector<int> & CDomain::getNiZoomSub(void) const
1004   {
1005      return (this->ni_zoom_sub);
1006   }
1007               
1008   //----------------------------------------------------------------
1009                     
1010   const std::vector<int> & CDomain::getIEndSub(void) const
1011   {
1012      return (this->iend_sub);
1013   }
1014   
1015   //----------------------------------------------------------------
1016   
1017   const std::vector<int> & CDomain::getJBeginSub(void) const
1018   {
1019      return (this->jbegin_sub);
1020   }
1021   
1022   //----------------------------------------------------------------
1023     
1024   const std::vector<int> & CDomain::getJBeginZoomSub(void) const
1025   {
1026      return (this->jbegin_zoom_sub);
1027   }
1028
1029   const std::vector<int> & CDomain::getNjZoomSub(void) const
1030   {
1031      return (this->nj_zoom_sub);
1032   }
1033                 
1034   
1035   //----------------------------------------------------------------
1036   
1037   const std::vector<int> & CDomain::getJEndSub(void) const
1038   {
1039      return (this->jend_sub);
1040   }
1041   
1042   //----------------------------------------------------------------
1043   
1044   const std::vector<CArray<double, 1>* > & CDomain::getLonValueSub(void) const
1045   {
1046      return (this->lonvalue_sub);
1047   }
1048   
1049   //----------------------------------------------------------------
1050   
1051   const std::vector<CArray<double,1>*> & CDomain::getLatValueSub(void) const
1052   {
1053      return (this->latvalue_sub);
1054   }   
1055   
1056   ///---------------------------------------------------------------
1057
1058} // namespace xios
Note: See TracBrowser for help on using the repository browser.