X-shooter Pipeline Reference Manual 3.8.15
test-xsh_gsl_correl_gaussians.c
Go to the documentation of this file.
1/* *
2 * This file is part of the ESO X-shooter Pipeline *
3 * Copyright (C) 2006 European Southern Observatory *
4 * *
5 * This library is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the Free Software *
17 * Foundation, 51 Franklin St, Fifth Floor, Boston, MA 02111-1307 USA *
18 * */
19
20/*
21 * $Author: amodigli $
22 * $Date: 2012-09-08 18:39:06 $
23 * $Revision: 1.1 $
24 * $Name: not supported by cvs2svn $
25 */
26#ifdef HAVE_CONFIG_H
27# include <config.h>
28#endif
29
30/*--------------------------------------------------------------------------*/
36/*--------------------------------------------------------------------------*/
39/*---------------------------------------------------------------------------
40 Includes
41 ---------------------------------------------------------------------------*/
42
43
44#include <xsh_error.h>
45#include <xsh_msg.h>
46#include <xsh_dfs.h>
47#include <tests.h>
48#include <cpl.h>
49#include <math.h>
50#include <getopt.h>
51#include <string.h>
52#include <gsl/gsl_statistics.h>
53/*---------------------------------------------------------------------------
54 Defines
55 ---------------------------------------------------------------------------*/
56#define MODULE_ID "XSH_GSL_CORRELATE_GAUSSIANS"
57
58enum {
60};
61
62static struct option LongOptions[] = {
63 {"debug", required_argument, 0, DEBUG_OPT},
64 {"help", 0, 0, HELP_OPT},
65 {NULL, 0, 0, 0}
66};
67
68static void Help( void )
69{
70 puts ("Unitary test : Create two Gaussians one shifted to the other of a given quantity, then correlate them to check if correlation returns expected shift");
71 puts( "Usage : ./tetst_xsh_correl_gaussians [options]");
72
73 puts( "Options" ) ;
74 puts( " --debug=<n> : Level of debug LOW | MEDIUM | HIGH [MEDIUM]" );
75 puts( " --help : What you see" ) ;
76
77 puts( "The input files argument MUST be in this order:" );
78 puts( " 1. PRE frame");
79 puts( " 2. SOF a) MODEL : [XSH_MOD_CFG_TAB_UVB]");
80 puts( " b) POLYNOMIAL: [DISP_TAB, ORDER_TAB_EDGES]");
81
82 TEST_END();
83 exit(0);
84}
85
86static void HandleOptions( int argc, char ** argv)
87{
88 int opt ;
89 int option_index = 0;
90
91 while( (opt = getopt_long( argc, argv, "debug:help",
92 LongOptions, &option_index )) != EOF){
93 switch( opt ) {
94 case DEBUG_OPT:
95 if ( strcmp( optarg, "LOW")==0){
97 }
98 else if ( strcmp( optarg, "HIGH")==0){
100 }
101 break;
102 case HELP_OPT:
103 Help();
104 break;
105 default:
106 break;
107 }
108 }
109}
110
111cpl_error_code
112xsh_gauss_gen(double* data,const double center,const double sigma, const int size)
113{
114
115 int i=0;
116 double x=0;
117 double inv_2_c2=0.5/sigma/sigma;
118 double norm=sigma*sqrt(2*CPL_MATH_PI);
119 double a=1./norm;
120
121 for(i=0;i<size;i++) {
122 x=i;
123 data[i]=a*exp(-(x-center)*(x-center)*inv_2_c2);
124 }
125
126 return cpl_error_get_code();
127}
128
129/*---------------------------------------------------------------------------
130 Functions prototypes
131 ---------------------------------------------------------------------------*/
132
133/*--------------------------------------------------------------------------*/
140/*--------------------------------------------------------------------------*/
141
142int main( int argc, char** argv)
143{
144 /* Initialize libraries */
146 cpl_msg_set_level( CPL_MSG_DEBUG);
148
149 HandleOptions( argc, argv);
150
151 /* Analyse parameters */
152 if ( (argc-optind) >= 2) {
153 Help();
154 }
155
156 int ret=0;
157 int size=100000;
158 double shift_i=5.15;
159 double shift_o=0;
160 double gauss_c=0.5*size;
161 double gauss_s=10.;
162 double* gauss_d1=NULL;
163 double* gauss_d2=NULL;
164
165 cpl_vector* gauss_v1=cpl_vector_new(size);
166 cpl_vector* gauss_v2=cpl_vector_new(size);
167
168 gauss_d1=cpl_vector_get_data(gauss_v1);
169 gauss_d2=cpl_vector_get_data(gauss_v2);
170
171 check(xsh_gauss_gen(gauss_d1,gauss_c,gauss_s,size));
172 check(xsh_gauss_gen(gauss_d2,gauss_c+shift_i,gauss_s,size));
173
174 /* here starts correlation using GSL */
175 double shift=gsl_stats_correlation(gauss_d1,1., gauss_d2,1.,size);
176 xsh_msg("shift: %g",shift);
177
178 cleanup:
179 xsh_free_vector(&gauss_v1);
180 xsh_free_vector(&gauss_v2);
181
182
183 if (cpl_error_get_code() != CPL_ERROR_NONE) {
184 xsh_error_dump( CPL_MSG_ERROR);
185 ret=1;
186 }
187 TEST_END();
188 return ret;
189}
190
int main()
Unit test of xsh_bspline_interpol.
static double sigma
cpl_error_code xsh_gauss_gen(double *data, const double center, const double sigma, const int size)
#define MODULE_ID
static struct option LongOptions[]
#define check(COMMAND)
Definition: xsh_error.h:71
#define xsh_error_dump(level)
Definition: xsh_error.h:92
int size
int * x
#define xsh_msg(...)
Print a message on info level.
Definition: xsh_msg.h:121
void xsh_free_vector(cpl_vector **v)
Deallocate a vector and set the pointer to NULL.
Definition: xsh_utils.c:2284
int xsh_debug_level_set(int level)
set debug level
Definition: xsh_utils.c:3125
static void HandleOptions(int argc, char **argv)
static void Help(void)
#define TEST_END()
Definition: tests.h:111
#define TESTS_INIT(DRL_ID)
Definition: tests.h:105
@ XSH_DEBUG_LEVEL_HIGH
Definition: xsh_utils.h:138
@ XSH_DEBUG_LEVEL_LOW
Definition: xsh_utils.h:137
@ XSH_DEBUG_LEVEL_MEDIUM
Definition: xsh_utils.h:138