source: XIOS/dev/dev_ym/XIOS_COUPLING/src/transformation/domain_algorithm/domain_algorithm_reorder.cpp @ 2190

Last change on this file since 2190 was 2190, checked in by jderouillat, 3 years ago

Add bound checking when the workflow view is scaned (highlighted by blitz check in debug mode)

  • Property svn:eol-style set to native
  • Property svn:executable set to *
File size: 10.7 KB
Line 
1/*!
2   \file domain_algorithm_reorder.cpp
3   \brief Algorithm for reorder a domain.
4 */
5#include "domain_algorithm_reorder.hpp"
6#include "reorder_domain.hpp"
7#include "domain.hpp"
8#include "grid.hpp"
9#include "grid_transformation_factory_impl.hpp"
10
11namespace xios {
12CGenericAlgorithmTransformation* CDomainAlgorithmReorder::create(bool isSource, CGrid* gridDst, CGrid* gridSrc,
13                                                             CTransformation<CDomain>* transformation,
14                                                             int elementPositionInGrid,
15                                                             std::map<int, int>& elementPositionInGridSrc2ScalarPosition,
16                                                             std::map<int, int>& elementPositionInGridSrc2AxisPosition,
17                                                             std::map<int, int>& elementPositionInGridSrc2DomainPosition,
18                                                             std::map<int, int>& elementPositionInGridDst2ScalarPosition,
19                                                             std::map<int, int>& elementPositionInGridDst2AxisPosition,
20                                                             std::map<int, int>& elementPositionInGridDst2DomainPosition)
21TRY
22{
23  std::vector<CDomain*> domainListDestP = gridDst->getDomains();
24  std::vector<CDomain*> domainListSrcP  = gridSrc->getDomains();
25
26  CReorderDomain* reorderDomain = dynamic_cast<CReorderDomain*> (transformation);
27  int domainDstIndex = elementPositionInGridDst2DomainPosition[elementPositionInGrid];
28  int domainSrcIndex = elementPositionInGridSrc2DomainPosition[elementPositionInGrid];
29
30  return (new CDomainAlgorithmReorder(isSource, domainListDestP[domainDstIndex], domainListSrcP[domainSrcIndex], reorderDomain));
31}
32CATCH
33
34bool CDomainAlgorithmReorder::dummyRegistered_ = CDomainAlgorithmReorder::registerTrans();
35bool CDomainAlgorithmReorder::registerTrans()
36TRY
37{
38  return CGridTransformationFactory<CDomain>::registerTransformation(TRANS_REORDER_DOMAIN, create);
39}
40CATCH
41
42CDomainAlgorithmReorder::CDomainAlgorithmReorder(bool isSource, CDomain* domainDestination, CDomain* domainSource, CReorderDomain* reorderDomain)
43: CAlgorithmTransformationNoDataModification(isSource)
44TRY
45{
46   // Reset geometrical attributes to avoid incompatible (user/domainSource) attributs
47   //   attributs will be defined using domainSource and/or transformation attributs
48   domainDestination->type.reset();
49   domainDestination->ni_glo.reset();
50   domainDestination->nj_glo.reset();
51   
52   domainDestination->i_index.reset();   // defined using domainSource->getLocalElement()
53   domainDestination->j_index.reset();   // "
54   domainDestination->ibegin.reset();    // will be computed in domainDestination->checkDomain() (from checkAttributes())
55   domainDestination->ni.reset();        // "
56   domainDestination->jbegin.reset();    // "
57   domainDestination->nj.reset();        // "
58   
59   domainDestination->mask_1d.reset();   // defined scanning domainSource->getFullView() & domainSource->getWorkflowView() differencies
60   domainDestination->mask_2d.reset();   //   in all case domainDestination->mask_1d used as reference 
61
62   // domainDestination->data_* attributes will be computed in :
63   domainDestination->data_dim.reset();     // domainDestination->checkDomainData() (from checkAttributes())
64   domainDestination->data_ni.reset();
65   domainDestination->data_nj.reset();
66   domainDestination->data_ibegin.reset();
67   domainDestination->data_jbegin.reset();
68   domainDestination->data_i_index.reset(); // domainDestination->checkCompression() (from checkAttributes())
69   domainDestination->data_j_index.reset();
70
71   // Next attributes will be set using domainSource->attributes
72   domainDestination->lonvalue_1d.reset();
73   domainDestination->latvalue_1d.reset();
74   domainDestination->lonvalue_2d.reset();
75   domainDestination->latvalue_2d.reset();
76   domainDestination->nvertex.reset();
77   domainDestination->bounds_lon_1d.reset();
78   domainDestination->bounds_lat_1d.reset();
79   domainDestination->bounds_lon_2d.reset();
80   domainDestination->bounds_lat_2d.reset();
81   domainDestination->area.reset();
82   domainDestination->radius.reset();
83
84 
85   // Set attributes for this transformation
86   domainDestination->type.setValue( domainSource->type );
87   
88   // Keep a 2D point of view for this transformation which is intrinsically 2D
89   domainDestination->ni_glo = domainSource->ni_glo;
90   domainDestination->nj_glo = domainSource->nj_glo;
91   // Set attributes required to define domainDestination->localElement_ and associated views, full and workflow)
92   CArray<size_t,1> sourceGlobalIdx = domainSource->getLocalElement()->getGlobalIndex();
93   int indexSize = sourceGlobalIdx.numElements();
94   domainDestination->i_index.resize( indexSize );
95   domainDestination->j_index.resize( indexSize );
96   for (size_t i = 0; i < indexSize ; ++i) {
97     domainDestination->i_index(i) = sourceGlobalIdx(i)%domainSource->ni_glo;
98     domainDestination->j_index(i) = sourceGlobalIdx(i)/domainSource->ni_glo;
99   }
100   // else
101   //   - domainDestination->ni_glo = domainSource->ni_glo * domainSource->nj_glo;
102   //   - domainDestination->nj_glo = 1;
103   //   - domainDestination->i_index = sourceGlobalIdx;
104   //   - domainDestination->j_index = 0;
105
106   // set mask_1d to enable domainMask computing (in checkMask())
107   CArray<int,1> sourceWorkflowIdx = domainSource->getLocalView(CElementView::WORKFLOW)->getIndex();
108   CArray<int,1> sourceFullIdx     = domainSource->getLocalView(CElementView::FULL    )->getIndex();
109   domainDestination->mask_1d.resize( indexSize );
110   int countMasked(0); // countMasked will store the offset index between full and workflow views
111   for (size_t i = 0; i < indexSize ; ++i) {
112       if ( ( (i-countMasked) >= sourceWorkflowIdx.numElements() )
113         || ( sourceFullIdx(i)!=sourceWorkflowIdx(i-countMasked) ) ) {
114       domainDestination->mask_1d(i) = 0;
115       countMasked++;
116     }
117     else {
118       domainDestination->mask_1d(i) = 1;
119     }
120   }
121
122   
123   // Set lon/lat values
124   if (!domainSource->lonvalue_1d.isEmpty() )
125   {
126     domainDestination->latvalue_1d.resize( domainSource->latvalue_1d.numElements() );
127     domainDestination->lonvalue_1d.resize( domainSource->lonvalue_1d.numElements() );
128     domainDestination->latvalue_1d = domainSource->latvalue_1d;
129     domainDestination->lonvalue_1d = domainSource->lonvalue_1d;
130   }
131   else if (!domainSource->lonvalue_2d.isEmpty() )
132   {
133     domainDestination->latvalue_2d.resize( domainSource->latvalue_2d.numElements() );
134     domainDestination->lonvalue_2d.resize( domainSource->lonvalue_2d.numElements() );
135     domainDestination->latvalue_2d = domainSource->latvalue_2d;
136     domainDestination->lonvalue_2d = domainSource->lonvalue_2d;
137   }
138   // Set bounds_lon/lat values
139   if (!domainSource->nvertex.isEmpty() )
140     domainDestination->nvertex = domainSource->nvertex;
141   if (!domainSource->bounds_lon_1d.isEmpty() )
142   {
143     domainDestination->bounds_lon_1d.resize( domainSource->bounds_lon_1d.numElements() );
144     domainDestination->bounds_lat_1d.resize( domainSource->bounds_lat_1d.numElements() );
145     domainDestination->bounds_lon_1d = domainSource->bounds_lon_1d;
146     domainDestination->bounds_lat_1d = domainSource->bounds_lat_1d;
147   }
148   else if (!domainSource->bounds_lon_2d.isEmpty() )
149   {
150     domainDestination->bounds_lon_2d.resize( domainSource->bounds_lon_2d.numElements() );
151     domainDestination->bounds_lat_2d.resize( domainSource->bounds_lat_2d.numElements() );
152     domainDestination->bounds_lon_2d = domainSource->bounds_lon_2d;
153     domainDestination->bounds_lat_2d = domainSource->bounds_lat_2d;
154   }
155   // set area
156   if (!domainSource->area.isEmpty() )
157   {
158     domainDestination->area.resize( domainSource->area.numElements() );
159     domainDestination->area = domainSource->area;   
160   }
161   if (!domainSource->radius.isEmpty() )
162     domainDestination->radius = domainSource->radius;
163
164   
165  reorderDomain->checkValid(domainSource);
166  // domainDestination->checkAttributes() will be operated at the end of the transformation definition to define correctly domainDestination views
167
168  if (domainSource->type !=  CDomain::type_attr::rectilinear)
169  {
170      ERROR("CDomainAlgorithmReorder::CDomainAlgorithmReorder(CDomain* domainDestination, CDomain* domainSource, CReorderDomain* reorderDomain)",
171           << "Domain destination is not rectilinear. This filter work only for rectilinear domain and destination domain with < id = "
172           <<domainDestination->getId() <<" > is of type "<<domainDestination->type<<std::endl);
173  }
174 
175  if (domainDestination == domainSource)
176  {
177    ERROR("CDomainAlgorithmReorder::CDomainAlgorithmReorder(CDomain* domainDestination, CDomain* domainSource, CReorderDomain* reorderDomain)",
178           << "Domain source and domain destination are the same. Please make sure domain destination refers to domain source" << std::endl
179           << "Domain source " <<domainSource->getId() << std::endl
180           << "Domain destination " <<domainDestination->getId() << std::endl);
181  }
182 
183  if (!reorderDomain->invert_lat.isEmpty() && reorderDomain->invert_lat.getValue() )
184  {
185    CArray<int,1>& j_index=domainDestination->j_index ;
186    int nglo = j_index.numElements() ;
187    int nj_glo =domainDestination->nj_glo ;
188    for (size_t i = 0; i < nglo ; ++i)
189    {
190      j_index(i)=(nj_glo-1)-j_index(i) ;
191    }
192  }
193
194  if (!reorderDomain->shift_lon_fraction.isEmpty())
195  {
196    int ni_glo =domainDestination->ni_glo ;
197    int  offset = ni_glo*reorderDomain->shift_lon_fraction ;
198    CArray<int,1>& i_index=domainDestination->i_index ;
199    int nglo = i_index.numElements() ;
200    for (size_t i = 0; i < nglo ; ++i)
201    {
202      i_index(i)=  (i_index(i)+offset+ni_glo)%ni_glo ;
203    }
204  }
205
206  if (!reorderDomain->min_lon.isEmpty() && !reorderDomain->max_lon.isEmpty())
207  {
208    double min_lon=reorderDomain->min_lon ;
209    double max_lon=reorderDomain->max_lon ;
210    double delta=max_lon-min_lon ;
211   
212    if (!domainDestination->lonvalue_1d.isEmpty() )
213    {
214      CArray<double,1>& lon=domainDestination->lonvalue_1d ;
215      for (int i=0;i<lon.numElements();++i)
216      {
217        while  (lon(i) > max_lon) lon(i)=lon(i)-delta ;
218        while  (lon(i) < min_lon) lon(i)=lon(i)+delta ;
219      }
220    }
221
222    if (!domainDestination->bounds_lon_1d.isEmpty() )
223    {
224      CArray<double,2>& bounds_lon=domainDestination->bounds_lon_1d ;
225      for (int i=0;i<bounds_lon.extent(0);++i)
226      {
227        while  (bounds_lon(0,i) > max_lon) bounds_lon(0,i)=bounds_lon(0,i)-delta ;
228        while  (bounds_lon(1,i) > max_lon) bounds_lon(1,i)=bounds_lon(1,i)-delta ;
229
230        while  (bounds_lon(0,i) < min_lon) bounds_lon(0,i)=bounds_lon(0,i)+delta ;
231        while  (bounds_lon(1,i) < min_lon) bounds_lon(1,i)=bounds_lon(1,i)+delta ;
232      }
233    }
234  }
235
236  domainDestination->checkAttributes() ;
237}
238CATCH
239
240
241}
Note: See TracBrowser for help on using the repository browser.