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 | |
---|
11 | namespace xios { |
---|
12 | 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) |
---|
21 | TRY |
---|
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 | } |
---|
32 | CATCH |
---|
33 | |
---|
34 | bool CDomainAlgorithmReorder::dummyRegistered_ = CDomainAlgorithmReorder::registerTrans(); |
---|
35 | bool CDomainAlgorithmReorder::registerTrans() |
---|
36 | TRY |
---|
37 | { |
---|
38 | return CGridTransformationFactory<CDomain>::registerTransformation(TRANS_REORDER_DOMAIN, create); |
---|
39 | } |
---|
40 | CATCH |
---|
41 | |
---|
42 | CDomainAlgorithmReorder::CDomainAlgorithmReorder(bool isSource, CDomain* domainDestination, CDomain* domainSource, CReorderDomain* reorderDomain) |
---|
43 | : CAlgorithmTransformationNoDataModification(isSource) |
---|
44 | TRY |
---|
45 | { |
---|
46 | // Input data for checkAttributes() |
---|
47 | // checkDomain |
---|
48 | domainDestination->type.setValue( CDomain::type_attr::rectilinear ); |
---|
49 | // Keep a 2D point of view for this transformation |
---|
50 | domainDestination->ni_glo = domainSource->ni_glo; |
---|
51 | domainDestination->nj_glo = domainSource->nj_glo; |
---|
52 | //domainDestination->ni = domainSource->ni; // Will be computed by checkAttributes |
---|
53 | //domainDestination->nj = domainSource->nj; // in function of : |
---|
54 | //domainDestination->ibegin = domainSource->ibegin;>ibegin; // - domainDestination->i_index |
---|
55 | //domainDestination->jbegin = domainSource->jbegin;>jbegin; // - domainDestination->j_index |
---|
56 | CArray<size_t,1> sourceGlobalIdx = domainSource->getLocalElement()->getGlobalIndex(); |
---|
57 | int indexSize = sourceGlobalIdx.numElements(); |
---|
58 | domainDestination->i_index.resize( indexSize ); |
---|
59 | domainDestination->j_index.resize( indexSize ); |
---|
60 | for (size_t i = 0; i < indexSize ; ++i) { |
---|
61 | domainDestination->i_index(i) = sourceGlobalIdx(i)%domainSource->ni_glo; |
---|
62 | domainDestination->j_index(i) = sourceGlobalIdx(i)/domainSource->ni_glo; |
---|
63 | } |
---|
64 | // else |
---|
65 | // - domainDestination->ni_glo = domainSource->ni_glo * domainSource->nj_glo; |
---|
66 | // - domainDestination->nj_glo = 1; |
---|
67 | // - domainDestination->i_index = sourceGlobalIdx; |
---|
68 | // - domainDestination->i_index = 0; |
---|
69 | |
---|
70 | CArray<int,1> sourceWorkflowIdx = domainSource->getLocalView(CElementView::WORKFLOW)->getIndex(); |
---|
71 | CArray<int,1> sourceFullIdx = domainSource->getLocalView(CElementView::FULL )->getIndex(); |
---|
72 | |
---|
73 | // checkMask -> define domainMask |
---|
74 | domainDestination->mask_1d.resize( indexSize ); |
---|
75 | //domainDestination->mask_2d.resize( domainSource->mask_2d.numElements() ); |
---|
76 | //domainDestination->mask_2d = domainSource->mask_2d; |
---|
77 | |
---|
78 | // checkDomainData |
---|
79 | domainDestination->data_dim = 1;//domainSource->data_dim; |
---|
80 | //domainDestination->data_ni = domainSource->data_ni; |
---|
81 | //domainDestination->data_nj = domainSource->data_nj; |
---|
82 | //domainDestination->data_ibegin = domainSource->data_ibegin; |
---|
83 | //domainDestination->data_jbegin = domainSource->data_ibegin; |
---|
84 | // checkCompression |
---|
85 | domainDestination->data_i_index.resize( indexSize ); |
---|
86 | domainDestination->data_j_index.resize( indexSize ); |
---|
87 | domainDestination->data_j_index = 0; |
---|
88 | int countMasked(0); |
---|
89 | for (size_t i = 0; i < indexSize ; ++i) { |
---|
90 | if ( sourceFullIdx(i)==sourceWorkflowIdx(i-countMasked) ) { |
---|
91 | domainDestination->mask_1d(i) = 1; |
---|
92 | domainDestination->data_i_index(i) = sourceFullIdx(i); |
---|
93 | //domainDestination->data_i_index(i) = sourceFullIdx(i)%domainSource->ni - domainSource->data_ibegin; |
---|
94 | //domainDestination->data_j_index(i) = sourceFullIdx(i)/domainSource->ni - domainSource->data_jbegin; |
---|
95 | } |
---|
96 | else { |
---|
97 | domainDestination->mask_1d(i) = 0; |
---|
98 | domainDestination->data_i_index(i) = -1; |
---|
99 | //domainDestination->data_j_index(i) = -1; |
---|
100 | countMasked++; |
---|
101 | } |
---|
102 | } |
---|
103 | |
---|
104 | |
---|
105 | // checkLonLat -> define (bounds_)lon/latvalue |
---|
106 | domainDestination->latvalue_1d.resize( domainSource->latvalue_1d.numElements() ); |
---|
107 | domainDestination->lonvalue_1d.resize( domainSource->lonvalue_1d.numElements() ); |
---|
108 | domainDestination->latvalue_1d = domainSource->latvalue_1d; |
---|
109 | domainDestination->lonvalue_1d = domainSource->lonvalue_1d; |
---|
110 | domainDestination->latvalue_2d.resize( domainSource->latvalue_2d.numElements() ); |
---|
111 | domainDestination->lonvalue_2d.resize( domainSource->lonvalue_2d.numElements() ); |
---|
112 | domainDestination->latvalue_2d = domainSource->latvalue_2d; |
---|
113 | domainDestination->lonvalue_2d = domainSource->lonvalue_2d; |
---|
114 | // checkBounds |
---|
115 | domainDestination->bounds_lon_1d.resize( domainSource->bounds_lon_1d.numElements() ); |
---|
116 | domainDestination->bounds_lat_1d.resize( domainSource->bounds_lat_1d.numElements() ); |
---|
117 | domainDestination->bounds_lon_1d = domainSource->bounds_lon_1d; |
---|
118 | domainDestination->bounds_lat_1d = domainSource->bounds_lat_1d; |
---|
119 | domainDestination->bounds_lon_2d.resize( domainSource->bounds_lon_2d.numElements() ); |
---|
120 | domainDestination->bounds_lat_2d.resize( domainSource->bounds_lat_2d.numElements() ); |
---|
121 | domainDestination->bounds_lon_2d = domainSource->bounds_lon_2d; |
---|
122 | domainDestination->bounds_lat_2d = domainSource->bounds_lat_2d; |
---|
123 | // checkArea |
---|
124 | |
---|
125 | reorderDomain->checkValid(domainSource); |
---|
126 | // domainDestination->checkAttributes() will be operated at the end of the transformation definition to define correctly domainDestination views |
---|
127 | //domainDestination->checkAttributes() ; // for now but maybe use domainSource as template for domain destination |
---|
128 | |
---|
129 | if (domainSource->type != CDomain::type_attr::rectilinear) |
---|
130 | { |
---|
131 | ERROR("CDomainAlgorithmReorder::CDomainAlgorithmReorder(CDomain* domainDestination, CDomain* domainSource, CReorderDomain* reorderDomain)", |
---|
132 | << "Domain destination is not rectilinear. This filter work only for rectilinear domain and destination domain with < id = " |
---|
133 | <<domainDestination->getId() <<" > is of type "<<domainDestination->type<<std::endl); |
---|
134 | } |
---|
135 | |
---|
136 | if (domainDestination == domainSource) |
---|
137 | { |
---|
138 | ERROR("CDomainAlgorithmReorder::CDomainAlgorithmReorder(CDomain* domainDestination, CDomain* domainSource, CReorderDomain* reorderDomain)", |
---|
139 | << "Domain source and domain destination are the same. Please make sure domain destination refers to domain source" << std::endl |
---|
140 | << "Domain source " <<domainSource->getId() << std::endl |
---|
141 | << "Domain destination " <<domainDestination->getId() << std::endl); |
---|
142 | } |
---|
143 | |
---|
144 | if (!reorderDomain->invert_lat.isEmpty() && reorderDomain->invert_lat.getValue() ) |
---|
145 | { |
---|
146 | CArray<int,1>& j_index=domainDestination->j_index ; |
---|
147 | int nglo = j_index.numElements() ; |
---|
148 | int nj_glo =domainDestination->nj_glo ; |
---|
149 | for (size_t i = 0; i < nglo ; ++i) |
---|
150 | { |
---|
151 | j_index(i)=(nj_glo-1)-j_index(i) ; |
---|
152 | } |
---|
153 | } |
---|
154 | |
---|
155 | if (!reorderDomain->shift_lon_fraction.isEmpty()) |
---|
156 | { |
---|
157 | int ni_glo =domainDestination->ni_glo ; |
---|
158 | int offset = ni_glo*reorderDomain->shift_lon_fraction ; |
---|
159 | CArray<int,1>& i_index=domainDestination->i_index ; |
---|
160 | int nglo = i_index.numElements() ; |
---|
161 | for (size_t i = 0; i < nglo ; ++i) |
---|
162 | { |
---|
163 | i_index(i)= (i_index(i)+offset+ni_glo)%ni_glo ; |
---|
164 | } |
---|
165 | } |
---|
166 | |
---|
167 | if (!reorderDomain->min_lon.isEmpty() && !reorderDomain->max_lon.isEmpty()) |
---|
168 | { |
---|
169 | double min_lon=reorderDomain->min_lon ; |
---|
170 | double max_lon=reorderDomain->max_lon ; |
---|
171 | double delta=max_lon-min_lon ; |
---|
172 | |
---|
173 | if (!domainDestination->lonvalue_1d.isEmpty() ) |
---|
174 | { |
---|
175 | CArray<double,1>& lon=domainDestination->lonvalue_1d ; |
---|
176 | for (int i=0;i<lon.numElements();++i) |
---|
177 | { |
---|
178 | while (lon(i) > max_lon) lon(i)=lon(i)-delta ; |
---|
179 | while (lon(i) < min_lon) lon(i)=lon(i)+delta ; |
---|
180 | } |
---|
181 | } |
---|
182 | |
---|
183 | if (!domainDestination->bounds_lon_1d.isEmpty() ) |
---|
184 | { |
---|
185 | CArray<double,2>& bounds_lon=domainDestination->bounds_lon_1d ; |
---|
186 | for (int i=0;i<bounds_lon.extent(0);++i) |
---|
187 | { |
---|
188 | while (bounds_lon(0,i) > max_lon) bounds_lon(0,i)=bounds_lon(0,i)-delta ; |
---|
189 | while (bounds_lon(1,i) > max_lon) bounds_lon(1,i)=bounds_lon(1,i)-delta ; |
---|
190 | |
---|
191 | while (bounds_lon(0,i) < min_lon) bounds_lon(0,i)=bounds_lon(0,i)+delta ; |
---|
192 | while (bounds_lon(1,i) < min_lon) bounds_lon(1,i)=bounds_lon(1,i)+delta ; |
---|
193 | } |
---|
194 | } |
---|
195 | } |
---|
196 | |
---|
197 | domainDestination->checkAttributes() ; |
---|
198 | } |
---|
199 | CATCH |
---|
200 | |
---|
201 | |
---|
202 | } |
---|