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

Last change on this file since 2270 was 2270, checked in by ymipsl, 3 years ago

Tracking memory leak :
Tranformations and algorithms are now managed with shared_ptr.

YM

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