Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Oi ensi multi #104

Merged
merged 24 commits into from
Nov 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
1debfe6
WIP
tnipen Jun 14, 2024
25b3bf5
work in progress, first version of oi_..._lr ready
Sep 2, 2024
f855ee4
work in progress
Sep 3, 2024
43aee5f
minor bug fixes in oi_ensi_lr. var name "_debug_level" changed becaus…
Sep 3, 2024
62a5300
modified such that it works with the R bindings
Sep 11, 2024
4ef665a
added structure functions and R_optimal_... functions
Sep 12, 2024
935547c
fixed bug in allow_extrapolation and added the _lr routine for static…
Sep 13, 2024
073b511
fixed bug in allow_extrapolation
Sep 16, 2024
00d03a3
added Transformation StartedBoxCox
Sep 18, 2024
be0dc99
fixed bug in definition of started Box-Cox
Sep 18, 2024
544dab7
Added oi_ensi_..._lr functions for non-R bindings
Sep 24, 2024
b97430e
modified function name ..._lr into _multi, modified function signatur…
Sep 25, 2024
eb52c14
fixed Linear structure function in Multiple structure
Sep 27, 2024
b6e0322
removed MixA structure function, removed R_... functions
Oct 2, 2024
ed3ff22
revised StartedBoxCox transformation, added scaling_factor
Oct 2, 2024
418cd01
added function staticcorr_points
Oct 9, 2024
20a2138
Added destructors to gridpp.h
Nov 7, 2024
3062e5c
minor modification in the include file
Nov 19, 2024
4e7bc81
work on ensi_multi routines
Nov 19, 2024
02b282c
work on ensi_multi
Nov 20, 2024
5429169
revised headers for ensi_multi functions
Nov 21, 2024
37cbbe6
Remove commented out destructors
tnipen Nov 26, 2024
cd78d09
Remove gridpp.i.bkp
tnipen Nov 26, 2024
0b18ff9
added vec3 functions for ..._ensi_multi_...
Nov 26, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
347 changes: 346 additions & 1 deletion include/gridpp.h

Large diffs are not rendered by default.

131 changes: 131 additions & 0 deletions src/api/corr_points.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
#include "gridpp.h"
#include <math.h>
#include <algorithm>
#include <armadillo>
#include <assert.h>
#include <exception>
#include <boost/math/distributions/normal.hpp>

using namespace gridpp;

namespace {
typedef arma::mat mattype;
typedef arma::vec vectype;
typedef arma::cx_mat cxtype;

void check_vec(vec2 input, int Y, int X);
void check_vec(vec input, int S);

template<class T1, class T2> struct sort_pair_first {
bool operator()(const std::pair<T1,T2>&left, const std::pair<T1,T2>&right) {
return left.first < right.first;
};
};
}

vec2 gridpp::staticcorr_points(const gridpp::Points& points,
const gridpp::Points& knots,
const gridpp::StructureFunction& structure,
int max_points) {

double s_time = gridpp::clock();

// Check input data
if(max_points < 0)
throw std::invalid_argument("max_points must be >= 0");

if(points.get_coordinate_type() != knots.get_coordinate_type()) {
throw std::invalid_argument("Both background grid and observations points must be of same coordinate type (lat/lon or x/y)");
}

int nY = points.size();
int nS = knots.size();

vec2 output = gridpp::init_vec2(nY, nS, 0);

vec blats = points.get_lats();
vec blons = points.get_lons();
vec belevs = points.get_elevs();
vec blafs = points.get_lafs();

vec plats = knots.get_lats();
vec plons = knots.get_lons();
vec pelevs = knots.get_elevs();
vec plafs = knots.get_lafs();

// Create all objects of type Point (to save time on creation later)
std::vector<Point> point_vec;
point_vec.reserve(nS);
for(int i = 0; i < nS; i++) {
point_vec.push_back(knots.get_point(i));
}

#pragma omp parallel for
for(int y = 0; y < nY; y++) {
float lat = blats[y];
float lon = blons[y];
Point p1 = points.get_point(y);
float localizationRadius = structure.localization_distance(p1);

// Find observations within localization radius
// TODO: Check that the chosen ones have elevation
ivec lLocIndices0 = knots.get_neighbours(lat, lon, localizationRadius);
if(lLocIndices0.size() == 0) {
// If we have too few observations though, then use the background
continue;
}
ivec lLocIndices;
lLocIndices.reserve(lLocIndices0.size());
std::vector<std::pair<float,int> > lRhos0;

// Calculate gridpoint to observation rhos
lRhos0.reserve(lLocIndices0.size());
std::vector<Point> p2;
p2.reserve(lLocIndices0.size());
for(int i = 0; i < lLocIndices0.size(); i++) {
int index = lLocIndices0[i];
p2.push_back(point_vec[index]);
}
vec rhos = structure.corr_background(p1, p2);
for(int i = 0; i < lLocIndices0.size(); i++) {
int index = lLocIndices0[i];
if(rhos[i] > 0) {
lRhos0.push_back(std::pair<float,int>(rhos[i], i));
}
}

// Make sure we don't use too many observations
arma::vec lRhos;
if(max_points > 0 && lRhos0.size() > max_points) {
// If we have too many locations, then only keep the best ones based on rho.
// Otherwise, just use the last locations added
lRhos = arma::vec(max_points);
std::sort(lRhos0.begin(), lRhos0.end(), ::sort_pair_first<float,int>());
for(int i = 0; i < max_points; i++) {
// The best values start at the end of the array
int index = lRhos0[lRhos0.size() - 1 - i].second;
lLocIndices.push_back(lLocIndices0[index]);
lRhos(i) = lRhos0[lRhos0.size() - 1 - i].first;
}
}
else {
lRhos = arma::vec(lRhos0.size());
for(int i = 0; i < lRhos0.size(); i++) {
int index = lRhos0[i].second;
lLocIndices.push_back(lLocIndices0[index]);
lRhos(i) = lRhos0[i].first;
}
}

int lS = lLocIndices.size();
if(lS > 0) {
for(int i = 0; i < lS; i++) {
int index = lLocIndices[i];
output[y][index] = lRhos(i);
}
}
} // End loop over points
double e_time = gridpp::clock();
// std::cout << count_stat << " " << e_time - s_time << " s" << std::endl;
return output;
} // End function corr_points
4 changes: 2 additions & 2 deletions src/api/gridpp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,11 @@ int gridpp::get_omp_threads() {
}

void gridpp::set_debug_level(int level) {
gridpp::_debug_level = level;
gridpp::debug_level = level;
}

int gridpp::get_debug_level() {
return gridpp::_debug_level;
return gridpp::debug_level;
}
gridpp::not_implemented_exception::not_implemented_exception() :
std::logic_error("Function not yet implemented") {
Expand Down
1 change: 1 addition & 0 deletions src/api/oi_ensi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ vec3 gridpp::optimal_interpolation_ensi(const gridpp::Grid& bgrid,
}
return output;
}

vec2 gridpp::optimal_interpolation_ensi(const gridpp::Points& bpoints,
const vec2& background,
const gridpp::Points& points,
Expand Down
Loading