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

Last change on this file since 185 was 185, checked in by hozdoba, 13 years ago
File size: 21.2 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_CREATE(value, valuetype, numdims, extent)// todo
401      //ARRAY_CREATE(value, valuetype, numdims, extent)
402     
403      ARRAY(double, 1) lonvalue_ = this->lonvalue.getValue(),
404                       latvalue_ = this->latvalue.getValue();
405                       
406      if (this->data_dim.getValue() == 2)
407      {
408         StdSize dn = this->ni.getValue() * this->nj.getValue();
409         lonvalue_->resize(boost::extents[dn]);
410         latvalue_->resize(boost::extents[dn]);
411
412         for (StdSize k = 0; k < lonvalue_sub.size(); k++)
413         {
414            int l = 0;
415            ARRAY(double, 1) lonvalue_loc = this->lonvalue_sub[k],
416                             latvalue_loc = this->latvalue_sub[k];
417            const int ibegin_loc = ibegin_sub[k], iend_loc = iend_sub[k],
418                      jbegin_loc = jbegin_sub[k], jend_loc = jend_sub[k];
419                                                                 
420            for (int i = ibegin_loc-1; i <= (iend_loc-1); i++)
421            {
422               for (int j = jbegin_loc-1; j <= (jend_loc-1); j++)
423               {
424                  (*lonvalue_)[i+j*this->ni.getValue()] = (*lonvalue_loc)[l];             
425                  (*latvalue_)[i+j*this->ni.getValue()] = (*latvalue_loc)[l++];
426               }
427            }
428         }   
429      }
430      else
431      {
432         StdSize dn = this->ni.getValue();
433         lonvalue_->resize(boost::extents[dn]);
434         latvalue_->resize(boost::extents[dn]);
435         
436         for (StdSize k = 0; k < lonvalue_sub.size(); k++)
437         {
438            int l = 0;
439            ARRAY(double, 1) lonvalue_loc = this->lonvalue_sub[k],
440                             latvalue_loc = this->latvalue_sub[k];
441            const int ibegin_loc = ibegin_sub[k], iend_loc = iend_sub[k],
442                      jbegin_loc = jbegin_sub[k], jend_loc = jend_sub[k];
443                     
444            for (int i = ibegin_loc-1; i <= (iend_loc-1); i++)
445               (*lonvalue_)[i] = (*lonvalue_loc)[l++];
446               
447            for (int j = jbegin_loc-1, l = 0; j <= (jend_loc-1); j++)
448               (*latvalue_)[j] = (*latvalue_loc)[l++];
449         }         
450      }
451   }
452
453   //----------------------------------------------------------------
454
455   void CDomain::checkZoom(void)
456   {
457      // Résolution et vérification des données globales de zoom.
458      if (!this->zoom_ni.isEmpty() || !this->zoom_nj.isEmpty() ||
459          !this->zoom_ibegin.isEmpty() || !this->zoom_jbegin.isEmpty())
460      {
461         if (this->zoom_ni.isEmpty()     && this->zoom_nj.isEmpty() &&
462             this->zoom_ibegin.isEmpty() && this->zoom_jbegin.isEmpty())
463         {
464            ERROR("CDomain::checkZoom(void)",
465                  <<"Les attributs définissant un zoom doivent tous être définis") ;
466         }
467         else
468         {
469            int zoom_iend = zoom_ibegin.getValue() + zoom_ni.getValue() - 1;
470            int zoom_jend = zoom_jbegin.getValue() + zoom_nj.getValue() - 1;
471               
472            if (zoom_ibegin.getValue() < 1  || zoom_jbegin.getValue() < 1 ||
473                zoom_iend > ni_glo.getValue() || zoom_jend > nj_glo.getValue())
474               ERROR("CDomain::checkZoom(void)",
475                     << "Zoom mal défini,"
476                     << " vérifiez les valeurs zoom_ni, zoom_nj, zoom_ibegin, zoom_ibegin") ;
477         }
478      }
479      else
480      {
481         this->zoom_ni.setValue(this->ni_glo.getValue()); 
482         this->zoom_nj.setValue(this->nj_glo.getValue());
483         this->zoom_ibegin.setValue(1);
484         this->zoom_jbegin.setValue(1);
485      }
486      // Résolution des données locales de zoom.
487      {
488         int zoom_iend = zoom_ibegin.getValue() + zoom_ni.getValue() - 1;
489         int zoom_jend = zoom_jbegin.getValue() + zoom_nj.getValue() - 1;
490         
491         if ((zoom_ibegin.getValue() > iend.getValue()) || 
492             (zoom_iend < ibegin.getValue()))
493         {
494            zoom_ni_loc.setValue(0);
495            zoom_ibegin_loc.setValue(-1);
496         }
497         else
498         {
499            int zoom_ibegin_loc_ = (zoom_ibegin.getValue() > ibegin.getValue()) 
500                                 ? zoom_ibegin.getValue()
501                                 : ibegin.getValue();
502            int zoom_iend_loc_  = (zoom_iend < iend.getValue()) 
503                                 ? zoom_iend
504                                 : iend.getValue();
505            int zoom_ni_loc_ = zoom_iend_loc_ - zoom_ibegin_loc_ + 1;
506           
507            zoom_ni_loc.setValue(zoom_ni_loc_);
508            zoom_ibegin_loc.setValue(zoom_ibegin_loc_-ibegin.getValue()+1);
509         }
510         
511         if ((zoom_jbegin.getValue() > jend.getValue()) || 
512             (zoom_jend < jbegin.getValue()))
513         {
514            zoom_nj_loc.setValue(0);
515            zoom_jbegin_loc.setValue(-1);
516         }
517         else
518         {
519            int zoom_jbegin_loc_ = (zoom_jbegin.getValue() > jbegin.getValue()) 
520                                 ? zoom_jbegin.getValue()
521                                 : jbegin.getValue();
522            int zoom_jend_loc_  = (zoom_jend < jend.getValue()) 
523                                 ? zoom_jend
524                                 : jend.getValue();
525            int zoom_nj_loc_ = zoom_jend_loc_ - zoom_jbegin_loc_ + 1;
526           
527            zoom_nj_loc.setValue(zoom_nj_loc_);
528            zoom_jbegin_loc.setValue(zoom_jbegin_loc_-jbegin.getValue()+1);
529         }
530      }
531   }
532
533   //----------------------------------------------------------------
534
535   void CDomain::checkAttributes(void)
536   {
537      if (this->isChecked) return;
538
539      this->checkGlobalDomain();
540      this->checkLocalIDomain();
541      this->checkLocalJDomain();
542     
543      this->checkZoom();
544
545      if (this->latvalue_sub.size() == 0)
546      { // CÃŽté client uniquement
547         this->checkMask();
548         this->checkDomainData();
549         this->checkCompression();
550      }
551      else
552      { // CÃŽté serveur uniquement
553         this->completeLonLat();
554      }
555      this->completeMask();
556
557      this->isChecked = true;
558   }
559   
560   //----------------------------------------------------------------
561   
562   void CDomain::completeMask(void)
563   {
564      this->local_mask->resize(boost::extents[zoom_ni_loc.getValue()][zoom_nj_loc.getValue()]);
565   }
566
567   //----------------------------------------------------------------
568
569   ARRAY(int, 2) CDomain::getLocalMask(void) const
570   {
571      return (this->local_mask);
572   }
573   
574   //----------------------------------------------------------------
575   
576   const std::vector<int> & CDomain::getIBeginSub(void) const
577   {
578      return (this->ibegin_sub);
579   }
580   
581   //----------------------------------------------------------------
582   
583   const std::vector<int> & CDomain::getIEndSub(void) const
584   {
585      return (this->iend_sub);
586   }
587   
588   //----------------------------------------------------------------
589   
590   const std::vector<int> & CDomain::getJBeginSub(void) const
591   {
592      return (this->jbegin_sub);
593   }
594   
595   //----------------------------------------------------------------
596   
597   const std::vector<int> & CDomain::getJEndSub(void) const
598   {
599      return (this->iend_sub);
600   }
601   
602   //----------------------------------------------------------------
603   
604   const std::vector<ARRAY(double, 1)> & CDomain::getLonValueSub(void) const
605   {
606      return (this->lonvalue_sub);
607   }
608   
609   //----------------------------------------------------------------
610   
611   const std::vector<ARRAY(double, 1)> & CDomain::getLatValueSub(void) const
612   {
613      return (this->latvalue_sub);
614   }   
615   
616   ///---------------------------------------------------------------
617
618} // namespace tree
619} // namespace xmlioserver
Note: See TracBrowser for help on using the repository browser.