X-shooter Pipeline Reference Manual 3.8.15
test-xsh_cpl_vector_correl_hdelta.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-07-23 13:11:02 $
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/*---------------------------------------------------------------------------
53 Defines
54 ---------------------------------------------------------------------------*/
55#define MODULE_ID "XSH_CPL_VECTOR_CORREL_HDELTA"
56
57enum {
59};
60
61static struct option LongOptions[] = {
62 {"debug", required_argument, 0, DEBUG_OPT},
63 {"help", 0, 0, HELP_OPT},
64 {NULL, 0, 0, 0}
65};
66
67static void Help( void )
68{
69 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");
70 puts( "Usage : ./tetst_xsh_correl_gaussians [options]");
71
72 puts( "Options" ) ;
73 puts( " --debug=<n> : Level of debug LOW | MEDIUM | HIGH [MEDIUM]" );
74 puts( " --help : What you see" ) ;
75
76 puts( "The input files argument MUST be in this order:" );
77 puts( " 1. PRE frame");
78 puts( " 2. SOF a) MODEL : [XSH_MOD_CFG_TAB_UVB]");
79 puts( " b) POLYNOMIAL: [DISP_TAB, ORDER_TAB_EDGES]");
80
81 TEST_END();
82 exit(0);
83}
84
85static void HandleOptions( int argc, char ** argv)
86{
87 int opt ;
88 int option_index = 0;
89
90 while( (opt = getopt_long( argc, argv, "debug:help",
91 LongOptions, &option_index )) != EOF){
92 switch( opt ) {
93 case DEBUG_OPT:
94 if ( strcmp( optarg, "LOW")==0){
96 }
97 else if ( strcmp( optarg, "HIGH")==0){
99 }
100 break;
101 case HELP_OPT:
102 Help();
103 break;
104 default:
105 break;
106 }
107 }
108}
109
110cpl_vector*
111xsh_vector_supersample(cpl_vector* v, const int factor)
112{
113 cpl_vector* result=NULL;
114 int size=cpl_vector_get_size(v);
115 double* dv=NULL;
116 double* dr=NULL;
117 int size_new=size*factor;
118 double dfactor=factor;
119 int i=0,j=0;
120 double m=0;
121
122 xsh_msg("rk1 size_new=%d",size_new);
123 result=cpl_vector_new(size_new);
124 xsh_msg("rk2");
125 dv=cpl_vector_get_data(v);
126 xsh_msg("rk3");
127 dr=cpl_vector_get_data(result);
128 xsh_msg("rk4");
129 xsh_msg("0-val=%g",dv[0]);
130 xsh_msg("1-val=%g",dv[1]);
131
132 for(i=0;i<size-1;i++) {
133 m=(dv[i+1]-dv[i])/dfactor;
134 //xsh_msg("ok1=%d dv[%d]=%g dv[%d]=%g",i,i+1,dv[i+1],i,dv[i]);
135 for(j=0;j<factor;j++) {
136 dr[i*factor+j]=dv[i]+m*j;
137 //xsh_msg("ok2=%d dr=%g",j,dr[i*size+j]);
138 }
139 }
140
141 return result;
142}
143/*---------------------------------------------------------------------------
144 Functions prototypes
145 ---------------------------------------------------------------------------*/
146
147/*--------------------------------------------------------------------------*/
154/*--------------------------------------------------------------------------*/
155
156int main( int argc, char** argv)
157{
158 /* Initialize libraries */
160 cpl_msg_set_level( CPL_MSG_DEBUG);
162
163 HandleOptions( argc, argv);
164
165 /* Analyse parameters */
166 if ( (argc-optind) >= 2) {
167 Help();
168 }
169
170 int ret=0;
171 int size=100;
172 int size_obs=0;
173 int size_ref=0;
174
175 double shift_i=5.15;
176 double shift_o=0;
177 double gauss_c=0.5*size;
178 double gauss_s=10.;
179 double* dobs=NULL;
180 double* dref=NULL;
181 const int file_size=256;
182 char dir[file_size];
183 char file_obs[file_size];
184 char file_ref[file_size];
185
186 sprintf(dir,"%s","/media/SCRATCH_BACKUP/data7/xsh/spr/RESPONSE_SLIT_NOD_UVB_GD71/");
187 sprintf(file_obs,"%svof.fits",dir);
188 sprintf(file_ref,"%svrf.fits",dir);
189 xsh_msg("file_obs=%s",file_obs);
190 xsh_msg("file_ref=%s",file_ref);
191
192 cpl_vector* vobs=NULL;
193 cpl_vector* vref=NULL;
194 cpl_vector* vobs_res=NULL;
195 cpl_vector* vref_res=NULL;
196 check(vobs=cpl_vector_load(file_obs,0));
197 check(vref=cpl_vector_load(file_ref,0));
198 size_obs=cpl_vector_get_size(vobs);
199 size_ref=cpl_vector_get_size(vref);
200 int factor=1000;
201 xsh_msg("ok1");
202 dobs=cpl_vector_get_data(vobs);
203 dref=cpl_vector_get_data(vref);
204
205 xsh_msg("vk1");
206 check(vobs_res=xsh_vector_supersample(vobs,factor));
207 xsh_msg("vk2");
208 check(vref_res=xsh_vector_supersample(vref,factor));
209 xsh_msg("vk3");
210
211
212
213
214 xsh_msg("ok1 size_obs=%d size_ref=%d",size_obs,size_ref);
215
216 int len_corr=factor*(size_obs+size_ref)-5;
217 xsh_msg("len_corr=%d",len_corr);
218 /* CPL VECTOR */
219 cpl_vector* correl=cpl_vector_new(len_corr);
220
221
222 cpl_vector* filter=NULL;
223 double radius=2;
224 cpl_vector_save(vobs,"vobs.fits",CPL_BPP_IEEE_FLOAT,NULL,CPL_IO_DEFAULT);
225 cpl_vector_save(vref,"vref.fits",CPL_BPP_IEEE_FLOAT,NULL,CPL_IO_DEFAULT);
226
227 cpl_vector_save(vobs_res,"vobs_res.fits",CPL_BPP_IEEE_FLOAT,NULL,CPL_IO_DEFAULT);
228 cpl_vector_save(vref_res,"vref_res.fits",CPL_BPP_IEEE_FLOAT,NULL,CPL_IO_DEFAULT);
229
230 double shift=cpl_vector_correlate(correl,vobs_res,vref_res);
231
232
233 filter=cpl_vector_new(size_obs);
234 /*
235 check(cpl_vector_fill_kernel_profile(filter,CPL_KERNEL_HAMMING,radius));
236 cpl_vector_multiply(vobs,filter);
237 cpl_vector_multiply(vref,filter);
238 xsh_msg("ok3");
239 cpl_vector_save(filter,"filter.fits",CPL_BPP_IEEE_FLOAT,NULL,CPL_IO_DEFAULT);
240 */
241 xsh_msg("ok4");
242 cpl_vector_save(vobs,"vobs_filt.fits",CPL_BPP_IEEE_FLOAT,NULL,CPL_IO_DEFAULT);
243 xsh_msg("ok5");
244 cpl_vector_save(vref,"vref_filt.fits",CPL_BPP_IEEE_FLOAT,NULL,CPL_IO_DEFAULT);
245 xsh_msg("ok6");
246
247 cpl_vector_save(correl,"correl.fits",CPL_BPP_IEEE_FLOAT,NULL,CPL_IO_DEFAULT);
248 xsh_msg("ok1");
249 xsh_msg("shift=%g",shift);
250 //cpl_vector_dump(correl,stdout);
251 double max=0;
252 int maxpos=0;
253 double* pvec=NULL;
254 /* get maxpos */
255 pvec=cpl_vector_get_data(correl);
256 max=-100;
257 int i=0;
258 for(i=0;i<len_corr;i++) {
259 if(max<pvec[i] ) {
260 max=pvec[i];
261 maxpos=i;
262 }
263 }
264 xsh_msg("maxpos my determination: %d",maxpos);
265 xsh_msg("ok1");
266
267 double a=0;
268 double b=0;
269 double c=0;
270 double fraction=0;
271 //maxpos +=1;
272 a=cpl_vector_get(correl,maxpos-1);
273 b=cpl_vector_get(correl,maxpos+1);
274 c=cpl_vector_get(correl,maxpos);
275 fraction=(a-b)/(2.*a+2.*b-4.*c);
276 xsh_msg("len_corr=%d",len_corr);
277 xsh_msg("a=%g b=%g c=%g fraction=%g",a,b,c,fraction);
278 xsh_msg("shift - (size-1)=%g",shift -(size-1));
279 xsh_msg("fraction shift - (size-1)=%g",
280 (shift -(size*factor-1)+fraction)/factor);
281 xsh_msg("ok1");
282
283
284 cleanup:
285 xsh_free_vector(&vobs);
286 xsh_free_vector(&vref);
287 xsh_free_vector(&vobs_res);
288 xsh_free_vector(&vref_res);
289 xsh_free_vector(&correl);
291
292
293 if (cpl_error_get_code() != CPL_ERROR_NONE) {
294 xsh_error_dump( CPL_MSG_ERROR);
295 ret=1;
296 }
297 TEST_END();
298 return ret;
299}
300
int main()
Unit test of xsh_bspline_interpol.
static struct option LongOptions[]
cpl_vector * xsh_vector_supersample(cpl_vector *v, const int factor)
#define check(COMMAND)
Definition: xsh_error.h:71
#define xsh_error_dump(level)
Definition: xsh_error.h:92
int size
#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
int m
Definition: xsh_detmon_lg.c:91
int filter
#define max(a, b)
@ 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