Titanlib
Library for quality control algorithms
sct_smart_boxes.h
Go to the documentation of this file.
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <math.h>
4 #include <stdbool.h>
5 #include <string.h>
6 #include <time.h>
7 #include <assert.h>
8 #include <gsl/gsl_linalg.h>
9 #include <gsl/gsl_multimin.h>
10 #include <gsl/gsl_rstat.h>
11 #include <gsl/gsl_statistics.h>
12 #include <gsl/gsl_sort.h>
13 #include <gsl/gsl_blas.h>
14 
15 /* Runs the SCT by splitting into boxes first
16 
17 Arguments:
18  n: Number of stations, specifies lengths of x, y, z, t arrays:
19  x: Array of x-position [m]
20  y: Array of y-position [m]
21  z: Array of station altitudes [m]
22  t: Array of temperatures (in any units)
23  nmax: Pointer to one int. Split a box into two if it contains more than this number of stations. Suggested value: 1000.
24  nmin: Don't allow boxes to have fewer than this. Suggested value: 100.
25  nminprof: Revert to basic atmospheric profile if fewer than this number of stations. Suggested value: 50.
26  dzmin: minimum elevation range to fit a vertical profile [m]. Suggested value: 30
27  dhmin: minimum value for OI horizontal decorellation length [m]. Suggested value: 10000
28  dz: OI vertical decorellation length [m]. Suggested value: 200
29  t2pos: Flag an observation if it exceeds this SCT value above the background [^oC]
30  One value for each station. Suggested values: 4
31  t2neg: Flag an observation if it is below this SCT value below the background [^oC]
32  One value for each station. Suggested values: 4
33  eps2: Epsilon squared. One value for each station. Suggested values: 0.5
34 
35 Outputs:
36  flags: Output array of flags, one for each station. 0 means passed the SCT, 1 means fails.
37  sct: Output array of sct-values (cross validation deviation), one for each station.
38  rep: Output array of station coefficient of representativity, one for each station.
39 
40 Compiling to library:
41  gcc SCT_wrapper.c -fPIC -lgslcblas -lgsl -lblas -L/usr/lib -lm -shared -o SCT_wrapper.so
42 */
43 void sct_smart_boxes(int *n,
44  double *x,
45  double *y,
46  double *z,
47  double *t,
48  int *nmax,
49  int *nmin,
50  int *nminprof,
51  double* dzmin,
52  double* dhmin,
53  double* dz,
54  double *t2pos,
55  double *t2neg,
56  double *eps2,
57  int *flags,
58  double *sct,
59  double *rep,
60  int *boxids);
61 
62 // Structure to contain station information for a box
63 struct Box {
64  int n; // Number of stations in box
65  double *x;
66  double *y;
67  double *z;
68  double *t;
69  int *i; // Index into global array
70 };
71 
72 // Structure to contain a list of boxes
73 struct BoxList {
74  int n; // Number of boxes
75  struct Box* boxes;
76 };
77 
78 // Run the SCT on a particular box
79 void spatial_consistency_test(struct Box *currentBox, int *nminprof, double* dzmin, double* dhmin, double* dz, double *t2pos, double *t2neg, double *eps2, int *flags, double* sct_out, double* rep_out);
80 
81 int compute_vertical_profile(struct Box *box, double meanT, double gamma, double a, double exact_p10, double exact_p90, int nminprof, double dzmin, double *vp);
82 // optimizer functions
83 double basic_vertical_profile_optimizer_function(const gsl_vector *v, void *data);
84 double vertical_profile_optimizer_function(const gsl_vector *v, void *data); // GSL format
85 // vp profile calculations
86 void basic_vertical_profile(int nz, double *z, double t0, double *t_out);
87 void vertical_profile(int nz, double *z, double t0, double gamma, double a, double h0, double h1i, double *t_out);
88 
89 // box division controller (other functions used by this controller not forward declared)
90 struct BoxList control_box_division(int maxNumStationsInBox, int minNumStationsInBox, struct Box inputBox);
91 
92 // Helper functions
93 double compute_quantile(double quantile, double *array, int sizeArray);
94 double mean(const double *array, int sizeArray);
95 double max(const double *array, int sizeArray);
96 double min(const double *array, int sizeArray);
97 void print_vector(double *vector, int size);
98 void print_gsl_vector(gsl_vector *vector, int size);
99 void print_matrix(double **matrix, int rows, int columns);
100 void print_gsl_matrix(gsl_matrix *matrix, int rows, int columns);
101 void print_sub_gsl_matrix(gsl_matrix *matrix, int start, int stop);
102 gsl_matrix* inverse_matrix(const gsl_matrix *matrix);
void print_matrix(double **matrix, int rows, int columns)
Definition: sct_smart_boxes.c:1044
void print_gsl_matrix(gsl_matrix *matrix, int rows, int columns)
Definition: sct_smart_boxes.c:1055
double compute_quantile(double quantile, double *array, int sizeArray)
Definition: sct_smart_boxes.c:984
int n
Definition: sct_smart_boxes.h:74
void print_vector(double *vector, int size)
Definition: sct_smart_boxes.c:1028
double * z
Definition: sct_smart_boxes.h:67
double vertical_profile_optimizer_function(const gsl_vector *v, void *data)
Definition: sct.cpp:376
ivec sct(const vec &lats, const vec &lons, const vec &elevs, const vec &values, int num_min, int num_max, float inner_radius, float outer_radius, int num_iterations, int num_min_prof, float min_elev_diff, float min_horizontal_scale, float vertical_scale, const vec &pos, const vec &neg, const vec &eps2, vec &prob_gross_error, vec &rep)
Spatial Consistency Test.
Definition: sct.cpp:28
double * x
Definition: sct_smart_boxes.h:65
void print_gsl_vector(gsl_vector *vector, int size)
Definition: sct_smart_boxes.c:1036
double * t
Definition: sct_smart_boxes.h:68
double max(const double *array, int sizeArray)
Definition: sct_smart_boxes.c:1006
Definition: sct_smart_boxes.h:73
void spatial_consistency_test(struct Box *currentBox, int *nminprof, double *dzmin, double *dhmin, double *dz, double *t2pos, double *t2neg, double *eps2, int *flags, double *sct_out, double *rep_out)
Definition: sct_smart_boxes.c:330
gsl_matrix * inverse_matrix(const gsl_matrix *matrix)
Definition: sct_smart_boxes.c:1077
struct BoxList control_box_division(int maxNumStationsInBox, int minNumStationsInBox, struct Box inputBox)
Definition: sct_smart_boxes.c:951
double min(const double *array, int sizeArray)
Definition: sct_smart_boxes.c:1017
int * i
Definition: sct_smart_boxes.h:69
struct Box * boxes
Definition: sct_smart_boxes.h:75
void vertical_profile(int nz, double *z, double t0, double gamma, double a, double h0, double h1i, double *t_out)
Definition: sct_smart_boxes.c:301
void sct_smart_boxes(int *n, double *x, double *y, double *z, double *t, int *nmax, int *nmin, int *nminprof, double *dzmin, double *dhmin, double *dz, double *t2pos, double *t2neg, double *eps2, int *flags, double *sct, double *rep, int *boxids)
Definition: sct_smart_boxes.c:16
int n
Definition: sct_smart_boxes.h:64
double mean(const double *array, int sizeArray)
Definition: sct_smart_boxes.c:996
int compute_vertical_profile(struct Box *box, double meanT, double gamma, double a, double exact_p10, double exact_p90, int nminprof, double dzmin, double *vp)
Definition: sct_smart_boxes.c:91
double * y
Definition: sct_smart_boxes.h:66
void basic_vertical_profile(int nz, double *z, double t0, double *t_out)
Definition: sct_smart_boxes.c:228
void print_sub_gsl_matrix(gsl_matrix *matrix, int start, int stop)
Definition: sct_smart_boxes.c:1066
Definition: sct_smart_boxes.h:63
double basic_vertical_profile_optimizer_function(const gsl_vector *v, void *data)
Definition: sct.cpp:348