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

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