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

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

Modify reorder implementation : reset all geometrical attributes, limit setting of attributes only necessary

  • Property svn:eol-style set to native
  • Property svn:executable set to *
File size: 10.6 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 ( sourceFullIdx(i)==sourceWorkflowIdx(i-countMasked) ) {
113       domainDestination->mask_1d(i) = 1;
114     }
115     else {
116       domainDestination->mask_1d(i) = 0;
117       countMasked++;
118     }
119   }
120
121   
122   // Set lon/lat values
123   if (!domainSource->lonvalue_1d.isEmpty() )
124   {
125     domainDestination->latvalue_1d.resize( domainSource->latvalue_1d.numElements() );
126     domainDestination->lonvalue_1d.resize( domainSource->lonvalue_1d.numElements() );
127     domainDestination->latvalue_1d = domainSource->latvalue_1d;
128     domainDestination->lonvalue_1d = domainSource->lonvalue_1d;
129   }
130   else if (!domainSource->lonvalue_2d.isEmpty() )
131   {
132     domainDestination->latvalue_2d.resize( domainSource->latvalue_2d.numElements() );
133     domainDestination->lonvalue_2d.resize( domainSource->lonvalue_2d.numElements() );
134     domainDestination->latvalue_2d = domainSource->latvalue_2d;
135     domainDestination->lonvalue_2d = domainSource->lonvalue_2d;
136   }
137   // Set bounds_lon/lat values
138   if (!domainSource->nvertex.isEmpty() )
139     domainDestination->nvertex = domainSource->nvertex;
140   if (!domainSource->bounds_lon_1d.isEmpty() )
141   {
142     domainDestination->bounds_lon_1d.resize( domainSource->bounds_lon_1d.numElements() );
143     domainDestination->bounds_lat_1d.resize( domainSource->bounds_lat_1d.numElements() );
144     domainDestination->bounds_lon_1d = domainSource->bounds_lon_1d;
145     domainDestination->bounds_lat_1d = domainSource->bounds_lat_1d;
146   }
147   else if (!domainSource->bounds_lon_2d.isEmpty() )
148   {
149     domainDestination->bounds_lon_2d.resize( domainSource->bounds_lon_2d.numElements() );
150     domainDestination->bounds_lat_2d.resize( domainSource->bounds_lat_2d.numElements() );
151     domainDestination->bounds_lon_2d = domainSource->bounds_lon_2d;
152     domainDestination->bounds_lat_2d = domainSource->bounds_lat_2d;
153   }
154   // set area
155   if (!domainSource->area.isEmpty() )
156   {
157     domainDestination->area.resize( domainSource->area.numElements() );
158     domainDestination->area = domainSource->area;   
159   }
160   if (!domainSource->radius.isEmpty() )
161     domainDestination->radius = domainSource->radius;
162
163   
164  reorderDomain->checkValid(domainSource);
165  // domainDestination->checkAttributes() will be operated at the end of the transformation definition to define correctly domainDestination views
166
167  if (domainSource->type !=  CDomain::type_attr::rectilinear)
168  {
169      ERROR("CDomainAlgorithmReorder::CDomainAlgorithmReorder(CDomain* domainDestination, CDomain* domainSource, CReorderDomain* reorderDomain)",
170           << "Domain destination is not rectilinear. This filter work only for rectilinear domain and destination domain with < id = "
171           <<domainDestination->getId() <<" > is of type "<<domainDestination->type<<std::endl);
172  }
173 
174  if (domainDestination == domainSource)
175  {
176    ERROR("CDomainAlgorithmReorder::CDomainAlgorithmReorder(CDomain* domainDestination, CDomain* domainSource, CReorderDomain* reorderDomain)",
177           << "Domain source and domain destination are the same. Please make sure domain destination refers to domain source" << std::endl
178           << "Domain source " <<domainSource->getId() << std::endl
179           << "Domain destination " <<domainDestination->getId() << std::endl);
180  }
181 
182  if (!reorderDomain->invert_lat.isEmpty() && reorderDomain->invert_lat.getValue() )
183  {
184    CArray<int,1>& j_index=domainDestination->j_index ;
185    int nglo = j_index.numElements() ;
186    int nj_glo =domainDestination->nj_glo ;
187    for (size_t i = 0; i < nglo ; ++i)
188    {
189      j_index(i)=(nj_glo-1)-j_index(i) ;
190    }
191  }
192
193  if (!reorderDomain->shift_lon_fraction.isEmpty())
194  {
195    int ni_glo =domainDestination->ni_glo ;
196    int  offset = ni_glo*reorderDomain->shift_lon_fraction ;
197    CArray<int,1>& i_index=domainDestination->i_index ;
198    int nglo = i_index.numElements() ;
199    for (size_t i = 0; i < nglo ; ++i)
200    {
201      i_index(i)=  (i_index(i)+offset+ni_glo)%ni_glo ;
202    }
203  }
204
205  if (!reorderDomain->min_lon.isEmpty() && !reorderDomain->max_lon.isEmpty())
206  {
207    double min_lon=reorderDomain->min_lon ;
208    double max_lon=reorderDomain->max_lon ;
209    double delta=max_lon-min_lon ;
210   
211    if (!domainDestination->lonvalue_1d.isEmpty() )
212    {
213      CArray<double,1>& lon=domainDestination->lonvalue_1d ;
214      for (int i=0;i<lon.numElements();++i)
215      {
216        while  (lon(i) > max_lon) lon(i)=lon(i)-delta ;
217        while  (lon(i) < min_lon) lon(i)=lon(i)+delta ;
218      }
219    }
220
221    if (!domainDestination->bounds_lon_1d.isEmpty() )
222    {
223      CArray<double,2>& bounds_lon=domainDestination->bounds_lon_1d ;
224      for (int i=0;i<bounds_lon.extent(0);++i)
225      {
226        while  (bounds_lon(0,i) > max_lon) bounds_lon(0,i)=bounds_lon(0,i)-delta ;
227        while  (bounds_lon(1,i) > max_lon) bounds_lon(1,i)=bounds_lon(1,i)-delta ;
228
229        while  (bounds_lon(0,i) < min_lon) bounds_lon(0,i)=bounds_lon(0,i)+delta ;
230        while  (bounds_lon(1,i) < min_lon) bounds_lon(1,i)=bounds_lon(1,i)+delta ;
231      }
232    }
233  }
234
235  domainDestination->checkAttributes() ;
236}
237CATCH
238
239
240}
Note: See TracBrowser for help on using the repository browser.