source: XMLIO_V2/dev/dev_rv/src/xmlio/node/domain.cpp @ 184

Last change on this file since 184 was 184, checked in by hozdoba, 13 years ago
File size: 21.1 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 <algorithm>
8
9namespace xmlioserver {
10namespace tree {
11   
12   /// ////////////////////// Définitions ////////////////////// ///
13
14   CDomain::CDomain(void)
15      : CObjectTemplate<CDomain>(), CDomainAttributes()
16      , isChecked(false), local_mask(new CArray<int, 2>(boost::extents[10][10])), relFiles()
17      , ibegin_sub(), iend_sub(), jbegin_sub(), jend_sub()
18      , lonvalue_sub(), latvalue_sub()
19   { /* Ne rien faire de plus */ }
20
21   CDomain::CDomain(const StdString & id)
22      : CObjectTemplate<CDomain>(id), CDomainAttributes()
23      , isChecked(false), local_mask(new CArray<int, 2>(boost::extents[10][10])), relFiles()
24      , ibegin_sub(), iend_sub(), jbegin_sub(), jend_sub()
25      , lonvalue_sub(), latvalue_sub()
26   { /* Ne rien faire de plus */ }
27
28   CDomain::~CDomain(void)
29   { 
30      this->local_mask.reset();
31      for (StdSize i = 0; i < this->lonvalue_sub.size(); i++)
32      {
33         this->lonvalue_sub[i].reset();
34         this->latvalue_sub[i].reset();
35      }     
36   }
37
38   ///---------------------------------------------------------------
39
40   const std::set<StdString> & CDomain::getRelFiles(void) const
41   {
42      return (this->relFiles);
43   }
44
45   //----------------------------------------------------------------
46   
47   bool CDomain::hasZoom(void) const
48   {
49      return ((this->zoom_ni.getValue() != this->ni_glo.getValue()) && 
50              (this->zoom_nj.getValue() != this->nj_glo.getValue()));
51   }
52   
53   //----------------------------------------------------------------
54   
55   bool CDomain::isEmpty(void) const
56   {
57      return ((this->zoom_ni_loc.getValue() == 0) || 
58              (this->zoom_nj_loc.getValue() == 0));
59   }
60
61   //----------------------------------------------------------------
62
63   bool CDomain::IsWritten(const StdString & filename) const
64   {
65      return (this->relFiles.find(filename) != this->relFiles.end());
66   }
67
68   //----------------------------------------------------------------
69
70   void CDomain::addRelFile(const StdString & filename)
71   {
72      this->relFiles.insert(filename);
73   }
74
75   //----------------------------------------------------------------
76
77   void CDomain::fromBinary(StdIStream & is)
78   {
79      SuperClass::fromBinary(is);
80     
81      this->ibegin_sub.push_back(this->ibegin.getValue());
82      this->jbegin_sub.push_back(this->jbegin.getValue());
83      this->iend_sub.push_back(this->iend.getValue());
84      this->jend_sub.push_back(this->jend.getValue()); 
85     
86      this->latvalue_sub.push_back(this->latvalue.getValue());
87      this->lonvalue_sub.push_back(this->lonvalue.getValue());   
88     
89#define CLEAR_ATT(name_)\
90      SuperClassAttribute::operator[](#name_)->clear()
91
92         CLEAR_ATT(mask);
93         CLEAR_ATT(data_n_index);
94         CLEAR_ATT(data_i_index);
95         CLEAR_ATT(data_j_index);
96         
97         CLEAR_ATT(data_ni);
98         CLEAR_ATT(data_nj);
99         CLEAR_ATT(data_ibegin);
100         CLEAR_ATT(data_jbegin);
101         
102         CLEAR_ATT(ni);
103         CLEAR_ATT(nj);
104         
105#undef CLEAR_ATT
106
107      this->ibegin.setValue(*std::min_element(this->ibegin_sub.begin(),this->ibegin_sub.end()));
108      this->jbegin.setValue(*std::min_element(this->jbegin_sub.begin(),this->jbegin_sub.end()));
109      this->iend.setValue(*std::max_element(this->iend_sub.begin(),this->iend_sub.end()));
110      this->jend.setValue(*std::max_element(this->jend_sub.begin(),this->jend_sub.end()));
111   }
112
113   //----------------------------------------------------------------
114
115   StdString CDomain::GetName(void)   { return (StdString("domain")); }
116   StdString CDomain::GetDefName(void){ return (CDomain::GetName()); }
117   ENodeType CDomain::GetType(void)   { return (eDomain); }
118
119   //----------------------------------------------------------------
120
121   void CDomain::checkGlobalDomain(void)
122   {
123      if ((ni_glo.isEmpty() || ni_glo.getValue() <= 0 ) ||
124          (ni_glo.isEmpty() || nj_glo.getValue() <= 0 ))
125         ERROR("CDomain::checkAttributes(void)",
126               << "Le domaine global est mal défini,"
127               << " vérifiez les valeurs de \'ni_glo\' et \'nj_glo\' !") ;
128   }
129
130   //----------------------------------------------------------------
131
132   void CDomain::checkLocalIDomain(void)
133   {
134      if (!ni.isEmpty() && !ibegin.isEmpty() && iend.isEmpty())
135         iend.setValue(ibegin.getValue() + ni.getValue() - 1) ;
136
137      else if (!ni.isEmpty() && !iend.isEmpty()   && ibegin.isEmpty())
138         ibegin.setValue( - ni.getValue() + iend.getValue() + 1) ;
139
140      else if (!ibegin.isEmpty() && !iend.isEmpty() && ni.isEmpty())
141         ni.setValue(iend.getValue() - ibegin.getValue() + 1) ;
142
143      else if (!ibegin.isEmpty() && !iend.isEmpty() &&
144               !ni.isEmpty() && (iend.getValue() != ibegin.getValue() + ni.getValue() - 1))
145      {
146         ERROR("CDomain::checkAttributes(void)",
147               << "Le domaine est mal défini,"
148               << " iend est différent de (ibegin + ni - 1) !") ;
149      }
150      else
151      {
152         ERROR("CDomain::checkAttributes(void)",
153               << "Le domaine est mal défini,"
154               << " deux valeurs au moins parmis iend, ibegin, ni doivent être définies !") ;
155      }
156
157
158      if (ni.getValue() < 0 || ibegin.getValue() > iend.getValue() ||
159          ibegin.getValue() < 1 || iend.getValue() > ni_glo.getValue())
160         ERROR("CDomain::checkAttributes(void)",
161               << "Domaine local mal défini,"
162               << " vérifiez les valeurs ni, ni_glo, ibegin, iend") ;
163
164   }
165
166   //----------------------------------------------------------------
167
168   void CDomain::checkLocalJDomain(void)
169   {
170      if (!nj.isEmpty() && !jbegin.isEmpty() && jend.isEmpty())
171         jend.setValue(jbegin.getValue() + nj.getValue() - 1) ;
172
173      else if (!nj.isEmpty() && !jend.isEmpty() && jbegin.isEmpty())
174         jbegin.setValue( - nj.getValue() + jend.getValue() + 1) ;
175
176      else if (!jbegin.isEmpty() && !jend.isEmpty() && nj.isEmpty())
177         nj.setValue(jend.getValue() - jbegin.getValue() + 1) ;
178
179      else if (!jbegin.isEmpty() && !jend.isEmpty() && !nj.isEmpty() &&
180               (jend.getValue() != jbegin.getValue() + nj.getValue() - 1))
181      {
182         ERROR("CDomain::checkAttributes(void)",
183               << "Le domaine est mal défini,"
184               << " iend est différent de (jbegin + nj - 1) !") ;
185      }
186      else
187      {
188         ERROR("CDomain::checkAttributes(void)",
189               << "Le domaine est mal défini,"
190               << " deux valeurs au moins parmis jend, jbegin, nj doivent être définies !") ;
191      }
192
193      if (nj.getValue() < 0 || jbegin.getValue() > jend.getValue() ||
194          jbegin.getValue() < 1 || jend.getValue() > nj_glo.getValue())
195         ERROR("CDomain::checkAttributes(void)",
196               << "Domaine local mal défini,"
197               << " vérifiez les valeurs nj, nj_glo, jbegin, jend") ;
198   }
199
200
201   //----------------------------------------------------------------
202
203   void CDomain::checkMask(void)
204   {
205      using namespace std;
206     
207      int ibegin_mask = 0,
208          jbegin_mask = 0,
209          iend_mask = iend.getValue() - ibegin.getValue(),
210          jend_mask = jend.getValue() - jbegin.getValue();
211     
212      if (!zoom_ibegin.isEmpty())
213      {
214         int zoom_iend = zoom_ibegin.getValue() + zoom_ni.getValue() - 1;
215         int zoom_jend = zoom_jbegin.getValue() + zoom_nj.getValue() - 1;
216         
217         ibegin_mask = max (ibegin.getValue(), zoom_ibegin.getValue());
218         jbegin_mask = max (jbegin.getValue(), zoom_jbegin.getValue());
219         iend_mask   = min (iend.getValue(), zoom_iend);
220         jend_mask   = min (jend.getValue(), zoom_jend);
221                 
222         ibegin_mask -= ibegin.getValue();
223         jbegin_mask -= jbegin.getValue();
224         iend_mask   -= ibegin.getValue();
225         jend_mask   -= jbegin.getValue();
226      }
227     
228      //~ std::cout << "-------------------" << std::endl
229                //~ << "zoom : " << std::boolalpha << this->hasZoom() << std::endl
230                //~ << "size : " << ni.getValue()  << " X " << nj.getValue()   << std::endl
231                //~ << "it : " << ibegin.getValue() << ", " << iend.getValue() << std::endl
232                //~ << "jt : " << jbegin.getValue() << ", " << jend.getValue() << std::endl
233                //~ << "im : " << ibegin_mask << ", " << iend_mask << std::endl
234                //~ << "jm : " << jbegin_mask << ", " << jend_mask << std::endl
235                //~ << "-------------------" << std::endl;
236
237      if (!mask.isEmpty())
238      {
239         ARRAY(bool, 2) mask_ = mask.getValue();
240         unsigned int niu = ni.getValue(), nju = nj.getValue();
241         if ((mask_->shape()[0] != niu) ||
242             (mask_->shape()[1] != nju))
243            ERROR("CDomain::checkAttributes(void)",
244                  <<"Le masque n'a pas la même taille que le domaine local") ;
245                 
246         for (int i = 0; i < ni.getValue(); i++)
247         {
248            for (int j = 0; j < nj.getValue(); j++)
249            {
250               if (i < ibegin_mask && i > iend_mask &&
251                   j < jbegin_mask && j > jend_mask )
252                     (*mask_)[i][j] = false;
253            }
254         }
255      }
256      else // (!mask.hasValue())
257      { // Si aucun masque n'est défini,
258        // on en crée un nouveau qui valide l'intégralité du domaine.
259         ARRAY_CREATE(__arr, bool, 2, [ni.getValue()][nj.getValue()]);
260         for (int i = 0; i < ni.getValue(); i++)
261         {
262            for (int j = 0; j < nj.getValue(); j++)
263            {
264               if (i >= ibegin_mask && i <= iend_mask &&
265                   j >= jbegin_mask && j <= jend_mask )
266                     (*__arr)[i][j] = true;
267               else  (*__arr)[i][j] = false;
268            }
269         }
270               
271         mask.setValue(__arr);
272         __arr.reset();
273      }
274   }
275
276
277   //----------------------------------------------------------------
278
279   void CDomain::checkDomainData(void)
280   {     
281      if (!data_dim.isEmpty() &&
282         !(data_dim.getValue() == 1 || data_dim.getValue() == 2))
283      {
284         ERROR("CDomain::checkAttributes(void)",
285               << "Dimension des données non comptatible (doit être 1 ou 2) !") ;
286      }
287      else if (data_dim.isEmpty())
288      {
289         ERROR("CDomain::checkAttributes(void)",
290               << "Dimension des données non définie !") ;
291      }
292
293      if (data_ibegin.isEmpty())
294         data_ibegin.setValue(0) ;
295      if (data_jbegin.isEmpty() && (data_dim.getValue() == 2))
296           data_jbegin.setValue(0) ;
297
298      if (!data_ni.isEmpty() && (data_ni.getValue() <= 0))
299      {
300         ERROR("CDomain::checkAttributes(void)",
301               << "Dimension des données négative (data_ni).") ;
302      }
303      else if (data_ni.isEmpty())
304      {
305         data_ni.setValue((data_dim.getValue() == 1)
306                           ? (ni.getValue() * nj.getValue())
307                           : ni.getValue());
308      }
309
310      if (data_dim.getValue() == 2)
311      {
312         if (!data_nj.isEmpty() && (data_nj.getValue() <= 0) )
313         {
314            ERROR("CDomain::checkAttributes(void)",
315                  << "Dimension des données négative (data_nj).") ;
316         }
317         else if (data_nj.isEmpty())
318            data_nj.setValue(nj.getValue()) ;
319      }
320
321   }
322
323   //----------------------------------------------------------------
324
325   void CDomain::checkCompression(void)
326   {
327      if (!data_i_index.isEmpty())
328      {
329         int ssize = data_i_index.getValue()->size();
330         if (!data_n_index.isEmpty() &&
331            (data_n_index.getValue() != ssize))
332         {
333            ERROR("CDomain::checkAttributes(void)",
334                  <<"Dimension data_i_index incompatible avec data_n_index.") ;
335         }
336         else if (data_n_index.isEmpty())
337            data_n_index.setValue(ssize) ;
338
339         if (data_dim.getValue() == 2)
340         {
341            if (!data_j_index.isEmpty() &&
342               (data_j_index.getValue()->size() != data_i_index.getValue()->size()))
343            {
344               ERROR("CDomain::checkAttributes(void)",
345                     <<"Dimension data_j_index incompatible avec data_i_index.") ;
346            }
347            else if (data_j_index.isEmpty())
348            {
349               ERROR("CDomain::checkAttributes(void)",
350                     <<"La donnée data_j_index doit être renseignée !") ;
351            }
352         }
353      }
354      else
355      {
356         if (!data_n_index.isEmpty() ||
357            ((data_dim.getValue() == 2) && (!data_j_index.isEmpty())))
358            ERROR("CDomain::checkAttributes(void)", << "data_i_index non défini") ;
359      }
360
361      if (data_n_index.isEmpty())
362      { // -> bloc re-vérifié OK
363         if (data_dim.getValue() == 1)
364         {
365            const int dni = data_ni.getValue();
366            ARRAY_CREATE(__arri, int, 1, [dni]);
367            data_n_index.setValue(dni);
368            for (int i = 0; i < dni; i++)
369               (*__arri)[i] = i+1 ;
370            data_i_index.setValue(__arri) ;
371         }
372         else   // (data_dim == 2)
373         {
374            const int dni = data_ni.getValue() * data_nj.getValue();
375           
376            ARRAY_CREATE(__arri, int, 1, [dni]);
377            ARRAY_CREATE(__arrj, int, 1, [dni]);               
378            data_n_index.setValue(dni);
379           
380            for(int count = 0, i = 0; i  < data_ni.getValue(); i++)
381            {
382               for(int j = 0; j < data_nj.getValue(); j++, count++)
383               {
384                  (*__arri)[count] = i+1 ;
385                  (*__arrj)[count] = j+1 ;
386               }
387            }
388            data_i_index.setValue(__arri) ;
389            data_j_index.setValue(__arrj) ;           
390            __arri.reset();
391            __arrj.reset();
392         }
393      }
394   }
395
396   //----------------------------------------------------------------
397   
398   void CDomain::completeLonLat(void)
399   {
400      ARRAY(double, 1) lonvalue_ = this->lonvalue.getValue(),
401                       latvalue_ = this->latvalue.getValue();
402                       
403      if (this->data_dim.getValue() == 2)
404      {
405         StdSize dn = this->ni.getValue()*this->nj.getValue();
406         lonvalue_->resize(boost::extents[dn]);
407         latvalue_->resize(boost::extents[dn]);
408
409         for (StdSize k = 0; k < lonvalue_sub.size(); k++)
410         {
411            int l = 0;
412            ARRAY(double, 1) lonvalue_loc = this->lonvalue_sub[k],
413                             latvalue_loc = this->latvalue_sub[k];
414            const int ibegin_loc = ibegin_sub[k], iend_loc = iend_sub[k],
415                      jbegin_loc = jbegin_sub[k], jend_loc = jend_sub[k];
416                                                                 
417            for (int i = ibegin_loc-1; i <= (iend_loc-1); i++)
418            {
419               for (int j = jbegin_loc-1; j <= (jend_loc-1); j++)
420               {
421                  (*lonvalue_)[i+j*this->ni.getValue()] = (*lonvalue_loc)[l];             
422                  (*latvalue_)[i+j*this->ni.getValue()] = (*latvalue_loc)[l++];
423               }
424            }
425         }   
426         
427      }
428      else
429      {
430         StdSize dn = this->ni.getValue();
431         lonvalue_->resize(boost::extents[dn]);
432         latvalue_->resize(boost::extents[dn]);
433         
434         for (StdSize k = 0; k < lonvalue_sub.size(); k++)
435         {
436            int l = 0;
437            ARRAY(double, 1) lonvalue_loc = this->lonvalue_sub[k],
438                             latvalue_loc = this->latvalue_sub[k];
439            const int ibegin_loc = ibegin_sub[k], iend_loc = iend_sub[k],
440                      jbegin_loc = jbegin_sub[k], jend_loc = jend_sub[k];
441                     
442            for (int i = ibegin_loc-1; i <= (iend_loc-1); i++)
443               (*lonvalue_)[i] = (*lonvalue_loc)[l++];
444               
445            for (int j = jbegin_loc-1, l = 0; j <= (jend_loc-1); j++)
446               (*latvalue_)[j] = (*latvalue_loc)[l++];
447         }         
448      }
449   }
450
451   //----------------------------------------------------------------
452
453   void CDomain::checkZoom(void)
454   {
455      // Résolution et vérification des données globales de zoom.
456      if (!this->zoom_ni.isEmpty() || !this->zoom_nj.isEmpty() ||
457          !this->zoom_ibegin.isEmpty() || !this->zoom_jbegin.isEmpty())
458      {
459         if (this->zoom_ni.isEmpty()     && this->zoom_nj.isEmpty() &&
460             this->zoom_ibegin.isEmpty() && this->zoom_jbegin.isEmpty())
461         {
462            ERROR("CDomain::checkZoom(void)",
463                  <<"Les attributs définissant un zoom doivent tous être définis") ;
464         }
465         else
466         {
467            int zoom_iend = zoom_ibegin.getValue() + zoom_ni.getValue() - 1;
468            int zoom_jend = zoom_jbegin.getValue() + zoom_nj.getValue() - 1;
469               
470            if (zoom_ibegin.getValue() < 1  || zoom_jbegin.getValue() < 1 ||
471                zoom_iend > ni_glo.getValue() || zoom_jend > nj_glo.getValue())
472               ERROR("CDomain::checkZoom(void)",
473                     << "Zoom mal défini,"
474                     << " vérifiez les valeurs zoom_ni, zoom_nj, zoom_ibegin, zoom_ibegin") ;
475         }
476      }
477      else
478      {
479         this->zoom_ni.setValue(this->ni_glo.getValue()); 
480         this->zoom_nj.setValue(this->nj_glo.getValue());
481         this->zoom_ibegin.setValue(1);
482         this->zoom_jbegin.setValue(1);
483      }
484      // Résolution des données locales de zoom.
485      {
486         int zoom_iend = zoom_ibegin.getValue() + zoom_ni.getValue() - 1;
487         int zoom_jend = zoom_jbegin.getValue() + zoom_nj.getValue() - 1;
488         
489         if ((zoom_ibegin.getValue() > iend.getValue()) || 
490             (zoom_iend < ibegin.getValue()))
491         {
492            zoom_ni_loc.setValue(0);
493            zoom_ibegin_loc.setValue(-1);
494         }
495         else
496         {
497            int zoom_ibegin_loc_ = (zoom_ibegin.getValue() > ibegin.getValue()) 
498                                 ? zoom_ibegin.getValue()
499                                 : ibegin.getValue();
500            int zoom_iend_loc_  = (zoom_iend < iend.getValue()) 
501                                 ? zoom_iend
502                                 : iend.getValue();
503            int zoom_ni_loc_ = zoom_iend_loc_ - zoom_ibegin_loc_ + 1;
504           
505            zoom_ni_loc.setValue(zoom_ni_loc_);
506            zoom_ibegin_loc.setValue(zoom_ibegin_loc_-ibegin.getValue()+1);
507         }
508         
509         if ((zoom_jbegin.getValue() > jend.getValue()) || 
510             (zoom_jend < jbegin.getValue()))
511         {
512            zoom_nj_loc.setValue(0);
513            zoom_jbegin_loc.setValue(-1);
514         }
515         else
516         {
517            int zoom_jbegin_loc_ = (zoom_jbegin.getValue() > jbegin.getValue()) 
518                                 ? zoom_jbegin.getValue()
519                                 : jbegin.getValue();
520            int zoom_jend_loc_  = (zoom_jend < jend.getValue()) 
521                                 ? zoom_jend
522                                 : jend.getValue();
523            int zoom_nj_loc_ = zoom_jend_loc_ - zoom_jbegin_loc_ + 1;
524           
525            zoom_nj_loc.setValue(zoom_nj_loc_);
526            zoom_jbegin_loc.setValue(zoom_jbegin_loc_-jbegin.getValue()+1);
527         }
528      }
529   }
530
531   //----------------------------------------------------------------
532
533   void CDomain::checkAttributes(void)
534   {
535      if (this->isChecked) return;
536
537      this->checkGlobalDomain();
538      this->checkLocalIDomain();
539      this->checkLocalJDomain();
540     
541      this->checkZoom();
542
543      if (this->latvalue_sub.size() == 0)
544      { // CÃŽté client uniquement
545         this->checkMask();
546         this->checkDomainData();
547         this->checkCompression();
548      }
549      else
550      { // CÃŽté serveur uniquement
551         this->completeLonLat();
552      }
553      this->completeMask();
554
555      this->isChecked = true;
556   }
557   
558   //----------------------------------------------------------------
559   
560   void CDomain::completeMask(void)
561   {
562      this->local_mask->resize(boost::extents[ni.getValue()][nj.getValue()]);
563   }
564
565   //----------------------------------------------------------------
566
567   ARRAY(int, 2) CDomain::getLocalMask(void) const
568   {
569      return (this->local_mask);
570   }
571   
572   //----------------------------------------------------------------
573   
574   const std::vector<int> & CDomain::getIBeginSub(void) const
575   {
576      return (this->ibegin_sub);
577   }
578   
579   //----------------------------------------------------------------
580   
581   const std::vector<int> & CDomain::getIEndSub(void) const
582   {
583      return (this->iend_sub);
584   }
585   
586   //----------------------------------------------------------------
587   
588   const std::vector<int> & CDomain::getJBeginSub(void) const
589   {
590      return (this->jbegin_sub);
591   }
592   
593   //----------------------------------------------------------------
594   
595   const std::vector<int> & CDomain::getJEndSub(void) const
596   {
597      return (this->iend_sub);
598   }
599   
600   //----------------------------------------------------------------
601   
602   const std::vector<ARRAY(double, 1)> & CDomain::getLonValueSub(void) const
603   {
604      return (this->lonvalue_sub);
605   }
606   
607   //----------------------------------------------------------------
608   
609   const std::vector<ARRAY(double, 1)> & CDomain::getLatValueSub(void) const
610   {
611      return (this->latvalue_sub);
612   }   
613   
614   ///---------------------------------------------------------------
615
616} // namespace tree
617} // namespace xmlioserver
Note: See TracBrowser for help on using the repository browser.