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

Last change on this file since 180 was 180, checked in by hozdoba, 14 years ago
File size: 11.7 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 CMask()), relFiles()
17      , ibegin_sub(), iend_sub(), jbegin_sub(), jend_sub()
18   { /* Ne rien faire de plus */ }
19
20   CDomain::CDomain(const StdString & id)
21      : CObjectTemplate<CDomain>(id), CDomainAttributes()
22      , isChecked(false), local_mask(new CMask()), relFiles()
23      , ibegin_sub(), iend_sub(), jbegin_sub(), jend_sub()
24   { /* Ne rien faire de plus */ }
25
26   CDomain::~CDomain(void)
27   { /* Ne rien faire de plus */ }
28
29   ///---------------------------------------------------------------
30
31   const std::set<StdString> & CDomain::getRelFiles(void) const
32   {
33      return (this->relFiles);
34   }
35
36   //----------------------------------------------------------------
37
38   bool CDomain::IsWritten(const StdString & filename) const
39   {
40      return (this->relFiles.find(filename) != this->relFiles.end());
41   }
42
43   //----------------------------------------------------------------
44
45   void CDomain::addRelFile(const StdString & filename)
46   {
47      this->relFiles.insert(filename);
48   }
49
50   //----------------------------------------------------------------
51
52   void CDomain::fromBinary(StdIStream & is)
53   {
54      SuperClass::fromBinary(is);
55     
56      this->ibegin_sub.push_back(this->ibegin.getValue());
57      this->jbegin_sub.push_back(this->jbegin.getValue());
58      this->iend_sub.push_back(this->iend.getValue());
59      this->jend_sub.push_back(this->jend.getValue());     
60     
61#define CLEAR_ATT(name_)\
62      SuperClassAttribute::operator[](#name_)->clear()
63
64         CLEAR_ATT(mask);
65         CLEAR_ATT(data_n_index);
66         CLEAR_ATT(data_i_index);
67         CLEAR_ATT(data_j_index);
68         
69         CLEAR_ATT(data_ni);
70         CLEAR_ATT(data_nj);
71         CLEAR_ATT(data_ibegin);
72         CLEAR_ATT(data_jbegin);
73         
74         CLEAR_ATT(ni);
75         CLEAR_ATT(nj);
76         
77         CLEAR_ATT(latvalue);
78         CLEAR_ATT(lonvalue);
79#undef CLEAR_ATT
80
81      this->ibegin.setValue(*std::min_element(this->ibegin_sub.begin(),this->ibegin_sub.end()));
82      this->jbegin.setValue(*std::min_element(this->jbegin_sub.begin(),this->jbegin_sub.end()));
83      this->iend.setValue(*std::max_element(this->iend_sub.begin(),this->iend_sub.end()));
84      this->jend.setValue(*std::max_element(this->jend_sub.begin(),this->jend_sub.end()));
85     
86      this->checkGlobalDomain();
87      this->checkLocalIDomain();
88      this->checkLocalJDomain();
89      this->completeMask();
90     
91      this->isChecked = true;
92   }
93
94   //----------------------------------------------------------------
95
96   StdString CDomain::GetName(void)   { return (StdString("domain")); }
97   StdString CDomain::GetDefName(void){ return (CDomain::GetName()); }
98   ENodeType CDomain::GetType(void)   { return (eDomain); }
99
100   //----------------------------------------------------------------
101
102   void CDomain::checkGlobalDomain(void)
103   {
104      if ((ni_glo.isEmpty() || ni_glo.getValue() <= 0 ) ||
105          (ni_glo.isEmpty() || nj_glo.getValue() <= 0 ))
106         ERROR("CDomain::checkAttributes(void)",
107               << "Le domaine global est mal défini,"
108               << " vérifiez les valeurs de \'ni_glo\' et \'nj_glo\' !") ;
109   }
110
111
112   //----------------------------------------------------------------
113
114   void CDomain::checkLocalIDomain(void)
115   {
116      if (!ni.isEmpty() && !ibegin.isEmpty() && iend.isEmpty())
117         iend.setValue(ibegin.getValue() + ni.getValue() - 1) ;
118
119      else if (!ni.isEmpty() && !iend.isEmpty()   && ibegin.isEmpty())
120         ibegin.setValue( - ni.getValue() + iend.getValue() + 1) ;
121
122      else if (!ibegin.isEmpty() && !iend.isEmpty() && ni.isEmpty())
123         ni.setValue(iend.getValue() - ibegin.getValue() + 1) ;
124
125      else if (!ibegin.isEmpty() && !iend.isEmpty() &&
126               !ni.isEmpty() && (iend.getValue() != ibegin.getValue() + ni.getValue() - 1))
127      {
128         ERROR("CDomain::checkAttributes(void)",
129               << "Le domaine est mal défini,"
130               << " iend est différent de (ibegin + ni - 1) !") ;
131      }
132      else
133      {
134         ERROR("CDomain::checkAttributes(void)",
135               << "Le domaine est mal défini,"
136               << " deux valeurs au moins parmis iend, ibegin, ni doivent être définies !") ;
137      }
138
139
140      if (ni.getValue() < 0 || ibegin.getValue() > iend.getValue() ||
141          ibegin.getValue() < 1 || iend.getValue() > ni_glo.getValue())
142         ERROR("CDomain::checkAttributes(void)",
143               << "Domaine local mal défini,"
144               << " vérifiez les valeurs ni, ni_glo, ibegin, iend") ;
145
146   }
147
148   //----------------------------------------------------------------
149
150   void CDomain::checkLocalJDomain(void)
151   {
152      if (!nj.isEmpty() && !jbegin.isEmpty() && jend.isEmpty())
153         jend.setValue(jbegin.getValue() + nj.getValue() - 1) ;
154
155      else if (!nj.isEmpty() && !jend.isEmpty() && jbegin.isEmpty())
156         jbegin.setValue( - nj.getValue() + jend.getValue() + 1) ;
157
158      else if (!jbegin.isEmpty() && !jend.isEmpty() && nj.isEmpty())
159         nj.setValue(jend.getValue() - jbegin.getValue() + 1) ;
160
161      else if (!jbegin.isEmpty() && !jend.isEmpty() &&
162               !nj.isEmpty() && (jend.getValue() != jbegin.getValue() + nj.getValue() - 1))
163      {
164         ERROR("CDomain::checkAttributes(void)",
165               << "Le domaine est mal défini,"
166               << " iend est différent de (jbegin + nj - 1) !") ;
167      }
168      else
169      {
170         ERROR("CDomain::checkAttributes(void)",
171               << "Le domaine est mal défini,"
172               << " deux valeurs au moins parmis jend, jbegin, nj doivent être définies !") ;
173      }
174
175      if (nj.getValue() < 0 || jbegin.getValue() > jend.getValue() ||
176          jbegin.getValue() < 1 || jend.getValue() > nj_glo.getValue())
177         ERROR("CDomain::checkAttributes(void)",
178               << "Domaine local mal défini,"
179               << " vérifiez les valeurs nj, nj_glo, jbegin, jend") ;
180   }
181
182
183   //----------------------------------------------------------------
184
185   void CDomain::checkMask(void)
186   {
187      if (!mask.isEmpty())
188      {
189         unsigned int niu = ni.getValue(), nju = nj.getValue();
190         if ((mask.getValue()->shape()[0] != niu) ||
191             (mask.getValue()->shape()[1] != nju))
192            ERROR("CDomain::checkAttributes(void)",
193                  <<"Le masque n'a pas la même taille que le domaine local") ;
194      }
195      else // (!mask.hasValue())
196      { // Si aucun masque n'est défini,
197        // on en crée un nouveau qui valide l'intégralité du domaine.
198         ARRAY_CREATE(__arr, bool, 2, [ni.getValue()][nj.getValue()]);
199         for (int i = 0; i < ni.getValue(); i++)
200            for (int j = 0; j < nj.getValue(); j++)
201               (*__arr)[i][j] = true;
202         mask.setValue(__arr);
203      }
204   }
205
206
207   //----------------------------------------------------------------
208
209   void CDomain::checkDomainData(void)
210   {     
211      if (!data_dim.isEmpty() &&
212         !(data_dim.getValue() == 1 || data_dim.getValue() == 2))
213      {
214         ERROR("CDomain::checkAttributes(void)",
215               << "Dimension des données non comptatible (doit être 1 ou 2) !") ;
216      }
217      else if (data_dim.isEmpty())
218      {
219         ERROR("CDomain::checkAttributes(void)",
220               << "Dimension des données non définie !") ;
221      }
222
223      if (data_ibegin.isEmpty())
224         data_ibegin.setValue(0) ;
225      if (data_jbegin.isEmpty() && (data_dim.getValue() == 2))
226           data_jbegin.setValue(0) ;
227
228      if (!data_ni.isEmpty() && (data_ni.getValue() <= 0))
229      {
230         ERROR("CDomain::checkAttributes(void)",
231               << "Dimension des données négative (data_ni).") ;
232      }
233      else if (data_ni.isEmpty())
234      {
235         data_ni.setValue((data_dim.getValue() == 1)
236                           ? (ni.getValue() * nj.getValue())
237                           : ni.getValue());
238      }
239
240      if (data_dim.getValue() == 2)
241      {
242         if (!data_nj.isEmpty() && (data_nj.getValue() <= 0) )
243         {
244            ERROR("CDomain::checkAttributes(void)",
245                  << "Dimension des données négative (data_nj).") ;
246         }
247         else if (data_nj.isEmpty())
248            data_nj.setValue(nj.getValue()) ;
249      }
250
251   }
252
253   //----------------------------------------------------------------
254
255   void CDomain::checkCompression(void)
256   {
257      if (!data_i_index.isEmpty())
258      {
259         int ssize = data_i_index.getValue()->size();
260         if (!data_n_index.isEmpty() &&
261            (data_n_index.getValue() != ssize))
262         {
263            ERROR("CDomain::checkAttributes(void)",
264                  <<"Dimension data_i_index incompatible avec data_n_index.") ;
265         }
266         else if (data_n_index.isEmpty())
267            data_n_index.setValue(ssize) ;
268
269         if (data_dim.getValue() == 2)
270         {
271            if (!data_j_index.isEmpty() &&
272               (data_j_index.getValue()->size() != data_i_index.getValue()->size()))
273            {
274               ERROR("CDomain::checkAttributes(void)",
275                     <<"Dimension data_j_index incompatible avec data_i_index.") ;
276            }
277            else if (data_j_index.isEmpty())
278            {
279               ERROR("CDomain::checkAttributes(void)",
280                     <<"La donnée data_j_index doit être renseignée !") ;
281            }
282         }
283      }
284      else
285      {
286         if (!data_n_index.isEmpty() ||
287            ((data_dim.getValue() == 2) && (!data_j_index.isEmpty())))
288            ERROR("CDomain::checkAttributes(void)", << "data_i_index non défini") ;
289      }
290
291      if (data_n_index.isEmpty())
292      { // -> bloc re-vérifié OK
293         if (data_dim.getValue() == 1)
294         {
295            const int dni = data_ni.getValue();
296            ARRAY_CREATE(__arri, int, 1, [dni]);
297            data_n_index.setValue(dni);
298            for (int i = 0; i < dni; i++)
299               (*__arri)[i] = i+1 ;
300            data_i_index.setValue(__arri) ;
301         }
302         else   // (data_dim == 2)
303         {
304            const int dni = data_ni.getValue() * data_nj.getValue();
305           
306            ARRAY_CREATE(__arri, int, 1, [dni]);
307            ARRAY_CREATE(__arrj, int, 1, [dni]);               
308            data_n_index.setValue(dni);
309           
310            for(int count = 0, i = 0; i  < data_ni.getValue(); i++)
311               for(int j = 0; j < data_nj.getValue(); j++, count++)
312               { 
313                  (*__arri)[count] = i+1 ;
314                  (*__arrj)[count] = j+1 ;
315               }
316               
317            data_i_index.setValue(__arri) ;
318            data_j_index.setValue(__arrj) ;
319         }
320      }
321   }
322
323   //----------------------------------------------------------------
324
325   void CDomain::checkAttributes(void)
326   {
327      if (this->isChecked) return;
328
329      this->checkGlobalDomain();
330      this->checkLocalIDomain();
331      this->checkLocalJDomain();
332
333      this->checkMask();
334      this->checkDomainData();
335      this->checkCompression();
336      this->completeMask();
337
338      this->isChecked = true;
339   }
340
341   void CDomain::completeMask(void)
342   {
343      this->local_mask->resize(ni.getValue(), nj.getValue());
344      /*this->local_mask->setDataPosition
345         (data_dim.getValue(), data_ni.getValue(), data_nj.getValue(),
346          data_ibegin.getValue(), data_jbegin.getValue(), data_n_index.getValue(),
347          data_i_index.getValue(), data_j_index.getValue());*/
348   }
349
350   boost::shared_ptr<CMask> CDomain::getLocalMask(void) const
351   {
352      return (this->local_mask);
353   }
354   ///---------------------------------------------------------------
355
356} // namespace tree
357} // namespace xmlioserver
Note: See TracBrowser for help on using the repository browser.