source: XIOS3/dev/XIOS_ATTACHED/src/transformation/domain_algorithm/domain_algorithm_reorder.cpp @ 2482

Last change on this file since 2482 was 2482, checked in by ymipsl, 15 months ago

First guess in supression of attached mode replaced by online reader and write filters

YM

  • 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 {
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.shape() );
131     domainDestination->lonvalue_2d.resize( domainSource->lonvalue_2d.shape() );
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.shape() );
148     domainDestination->bounds_lat_2d.resize( domainSource->bounds_lat_2d.shape() );
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_1d.isEmpty() )
154   {
155     domainDestination->area_1d.resize( domainSource->area_1d.numElements() );
156     domainDestination->area_1d = domainSource->area_1d;   
157   }
158   else if (!domainSource->area_2d.isEmpty() )
159   {
160     domainDestination->area_2d.resize( domainSource->area_2d.shape() );
161     domainDestination->area_2d = domainSource->area_2d;   
162   }
163
164   if (!domainSource->radius.isEmpty() )
165     domainDestination->radius = domainSource->radius;
166
167   
168  reorderDomain->checkValid(domainSource);
169  // domainDestination->checkAttributes() will be operated at the end of the transformation definition to define correctly domainDestination views
170
171  if (domainSource->type !=  CDomain::type_attr::rectilinear)
172  {
173      ERROR("CDomainAlgorithmReorder::CDomainAlgorithmReorder(CDomain* domainDestination, CDomain* domainSource, CReorderDomain* reorderDomain)",
174           << "Domain destination is not rectilinear. This filter work only for rectilinear domain and destination domain with < id = "
175           <<domainDestination->getId() <<" > is of type "<<domainDestination->type<<std::endl);
176  }
177 
178  if (domainDestination == domainSource)
179  {
180    ERROR("CDomainAlgorithmReorder::CDomainAlgorithmReorder(CDomain* domainDestination, CDomain* domainSource, CReorderDomain* reorderDomain)",
181           << "Domain source and domain destination are the same. Please make sure domain destination refers to domain source" << std::endl
182           << "Domain source " <<domainSource->getId() << std::endl
183           << "Domain destination " <<domainDestination->getId() << std::endl);
184  }
185 
186  if (!reorderDomain->invert_lat.isEmpty() && reorderDomain->invert_lat.getValue() )
187  {
188    CArray<int,1>& j_index=domainDestination->j_index ;
189    int nglo = j_index.numElements() ;
190    int nj_glo =domainDestination->nj_glo ;
191    for (size_t i = 0; i < nglo ; ++i)
192    {
193      j_index(i)=(nj_glo-1)-j_index(i) ;
194    }
195  }
196
197  if (!reorderDomain->shift_lon_fraction.isEmpty())
198  {
199    int ni_glo =domainDestination->ni_glo ;
200    int  offset = ni_glo*reorderDomain->shift_lon_fraction ;
201    CArray<int,1>& i_index=domainDestination->i_index ;
202    int nglo = i_index.numElements() ;
203    for (size_t i = 0; i < nglo ; ++i)
204    {
205      i_index(i)=  (i_index(i)+offset+ni_glo)%ni_glo ;
206    }
207  }
208
209  if (!reorderDomain->min_lon.isEmpty() && !reorderDomain->max_lon.isEmpty())
210  {
211    double min_lon=reorderDomain->min_lon ;
212    double max_lon=reorderDomain->max_lon ;
213    double delta=max_lon-min_lon ;
214   
215    if (!domainDestination->lonvalue_1d.isEmpty() )
216    {
217      CArray<double,1>& lon=domainDestination->lonvalue_1d ;
218      for (int i=0;i<lon.numElements();++i)
219      {
220        while  (lon(i) > max_lon) lon(i)=lon(i)-delta ;
221        while  (lon(i) < min_lon) lon(i)=lon(i)+delta ;
222      }
223    }
224
225    if (!domainDestination->bounds_lon_1d.isEmpty() )
226    {
227      CArray<double,2>& bounds_lon=domainDestination->bounds_lon_1d ;
228      for (int i=0;i<bounds_lon.extent(0);++i)
229      {
230        while  (bounds_lon(0,i) > max_lon) bounds_lon(0,i)=bounds_lon(0,i)-delta ;
231        while  (bounds_lon(1,i) > max_lon) bounds_lon(1,i)=bounds_lon(1,i)-delta ;
232
233        while  (bounds_lon(0,i) < min_lon) bounds_lon(0,i)=bounds_lon(0,i)+delta ;
234        while  (bounds_lon(1,i) < min_lon) bounds_lon(1,i)=bounds_lon(1,i)+delta ;
235      }
236    }
237  }
238
239  domainDestination->checkAttributes() ;
240}
241CATCH
242
243
244}
Note: See TracBrowser for help on using the repository browser.