Skip to content

Commit 1acb6dc

Browse files
author
Cristian Lussana
committed
modified such that it works with the R bindings
1 parent 03372e5 commit 1acb6dc

File tree

3 files changed

+67
-10
lines changed

3 files changed

+67
-10
lines changed

include/gridpp.h

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,7 @@ namespace gridpp {
310310
* @param allow_extrapolation Allow OI to extrapolate increments outside increments at observations
311311
* @returns 3D vector of analised values (Y, X, E)
312312
*/
313-
vec3 optimal_interpolation_ensi_lr(const Grid& bgrid,
313+
/* vec3 optimal_interpolation_ensi_lr(const Grid& bgrid,
314314
const vec3& background_l,
315315
const vec3& background_L,
316316
const Points& obs_points,
@@ -322,7 +322,7 @@ namespace gridpp {
322322
float std_ratios_lr,
323323
float weigth,
324324
int max_points,
325-
bool allow_extrapolation=true);
325+
bool allow_extrapolation=true); */
326326

327327
/** Optimal interpolation for an ensemble gridded field (alternative version)
328328
* Work in progress
@@ -333,7 +333,9 @@ namespace gridpp {
333333
* @param obs 2D vector of perturbed observations (S, E)
334334
* @param background 3D vector of (right) background values used to compute innovations (Y, X, E)
335335
* @param background 3D vector of (RIGHT) background values (Y, X, E) used to compute correlations
336-
* @param structure Structure function
336+
* @param dh structure Structure function par 1
337+
* @param dz structure Structure function par 2
338+
* @param dw structure Structure function par 3
337339
* @param variance_ratio (ratio of observation to right background error variance)
338340
* @param standard deviation ratio (ratio of left to right background error standard deviation)
339341
* @param weight given to the analysis increment
@@ -348,7 +350,10 @@ namespace gridpp {
348350
const vec2& obs,
349351
const vec2& pbackground_r,
350352
const vec2& pbackground_R,
351-
const StructureFunction& structure,
353+
/* const StructureFunction& structure, */
354+
float dh,
355+
float dz,
356+
float dw,
352357
float var_ratios_or,
353358
float std_ratios_lr,
354359
float weigth,
@@ -1591,6 +1596,8 @@ namespace gridpp {
15911596
float test_vec2_argout(vec2& distances);
15921597

15931598
void test_not_implemented_exception();
1599+
/* vec2 test_args_for_R(const Points& bpoints, const StructureFunction& structure, const vec2& background);*/
1600+
vec2 test_args_for_R(const Points& bpoints, const vec2& background);
15941601

15951602
/** Default value used to fill array in SWIG testing functions. Not useful for any other purpose. */
15961603
static const float swig_default_value = -1;

src/api/oi_ensi_lr.cpp

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ void print_matrix(Matrix matrix) {
3030
template void print_matrix< ::mattype>(::mattype matrix);
3131
template void print_matrix< ::cxtype>(::cxtype matrix);
3232

33-
vec3 gridpp::optimal_interpolation_ensi_lr(const gridpp::Grid& bgrid,
33+
/*vec3 gridpp::optimal_interpolation_ensi_lr(const gridpp::Grid& bgrid,
3434
const vec3& background_l,
3535
const vec3& background_L,
3636
const gridpp::Points& points,
@@ -120,15 +120,18 @@ vec3 gridpp::optimal_interpolation_ensi_lr(const gridpp::Grid& bgrid,
120120
}
121121
}
122122
return output;
123-
}
123+
} */
124124
vec2 gridpp::optimal_interpolation_ensi_lr(const gridpp::Points& bpoints,
125125
const vec2& background_l,
126126
const vec2& background_L,
127127
const gridpp::Points& points,
128128
const vec2& pobs,
129129
const vec2& pbackground_r,
130130
const vec2& pbackground_R,
131-
const gridpp::StructureFunction& structure,
131+
/* const gridpp::StructureFunction& structure, it creates problem with R bindings */
132+
float dh,
133+
float dz,
134+
float dw,
132135
float var_ratios_or,
133136
float std_ratios_lr,
134137
float weigth,
@@ -150,6 +153,9 @@ vec2 gridpp::optimal_interpolation_ensi_lr(const gridpp::Points& bpoints,
150153
if(pbackground_R.size() != points.size())
151154
throw std::invalid_argument("Background RIGTH and points size mismatch");
152155

156+
float hmax = 7 * dh;
157+
BarnesStructure structure = BarnesStructure( dh, dz, dw, hmax);
158+
153159
int nS = points.size();
154160
if(nS == 0)
155161
return background_l;
@@ -162,7 +168,8 @@ vec2 gridpp::optimal_interpolation_ensi_lr(const gridpp::Points& bpoints,
162168

163169
// Prepare output matrix
164170
float missing_value = -99999.999;
165-
vec2 output = gridpp::init_vec2(nY, nEns, missing_value);
171+
/* vec2 output = gridpp::init_vec2(nY, nEns, missing_value); */
172+
vec2 output = background_l;
166173

167174
vec blats = bpoints.get_lats();
168175
vec blons = bpoints.get_lons();
@@ -366,8 +373,7 @@ vec2 gridpp::optimal_interpolation_ensi_lr(const gridpp::Points& bpoints,
366373
// lK(1, lS): Kalman gain
367374
mattype lK = lr_lr * arma::inv(lR_rr + lR_dd);
368375
// dx(1, nValidEns): analysis increment
369-
vectype dx = std_ratios_lr * weigth * (lK * lInnov);
370-
376+
mattype dx = std_ratios_lr * weigth * (lK * lInnov);
371377
///////////////////////////////
372378
// Anti-extrapolation filter //
373379
///////////////////////////////
@@ -400,6 +406,17 @@ vec2 gridpp::optimal_interpolation_ensi_lr(const gridpp::Points& bpoints,
400406
int ei = validEns[e];
401407
output[y][ei] = background_l[y][ei] + dx[e];
402408
}
409+
// debug
410+
/* for(int i = 0; i < lS; i++) {
411+
// compute lZ_R and lInnov
412+
int index = lLocIndices[i];
413+
std::cout << i << " backg_r obs " << pbackground_r[index][0] << " " << pobs[index][0] << std::endl;
414+
} // end loop over closer observations
415+
for(int e = 0; e < nValidEns; e++) {
416+
int ei = validEns[e];
417+
std::cout << ei << " backg_l analysis " << background_l[y][ei] << " " << output[y][ei] << std::endl;
418+
} */
419+
403420
} // end loop over gridpoint
404421
return output;
405422
}

src/api/swig.cpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,3 +98,36 @@ float gridpp::test_vec2_argout(vec2& distances) {
9898
void gridpp::test_not_implemented_exception() {
9999
throw gridpp::not_implemented_exception();
100100
}
101+
vec2 gridpp::test_args_for_R(const gridpp::Points& bpoints,
102+
/* const gridpp::StructureFunction& structure,*/
103+
const vec2& background) {
104+
int nS = bpoints.size();
105+
int nEns = background[0].size();
106+
int nY = background.size();
107+
float missing_value = -99999.999;
108+
/* gridpp::StructureFunction structure = gridpp::BarnesStructure(10000, 0, 0, 0);*/
109+
BarnesStructure structure = BarnesStructure(10000,1000000,0.5,100000);
110+
vec2 output = gridpp::init_vec2(nY, nEns, missing_value);
111+
vec blats = bpoints.get_lats();
112+
vec blons = bpoints.get_lons();
113+
vec belevs = bpoints.get_elevs();
114+
vec blafs = bpoints.get_lafs();
115+
Point p2 = bpoints.get_point(0);
116+
for(int i = 0; i < nS; i++) {
117+
Point p1 = bpoints.get_point(i);
118+
float localizationRadius = structure.localization_distance(p1);
119+
float rhos = structure.corr_background(p1, p2);
120+
std::cout << "----------------------------" << std::endl;
121+
std::cout << i << " localizationRadius " << localizationRadius << std::endl;
122+
std::cout << i << " rhos " << rhos << std::endl;
123+
std::cout << i << " blats " << blats[i] << std::endl;
124+
std::cout << i << " blons " << blons[i] << std::endl;
125+
std::cout << i << " belevs " << belevs[i] << std::endl;
126+
std::cout << i << " blafs " << blafs[i] << std::endl;
127+
for(int j = 0; j < background[i].size(); j++) {
128+
std::cout << j << " bkg " << background[i][j] << std::endl;
129+
output[i][j] = background[i][j];
130+
}
131+
}
132+
return output;
133+
}

0 commit comments

Comments
 (0)