KMOS Pipeline Reference Manual 4.5.10
kmos_test.c
1#ifdef HAVE_CONFIG_H
2#include <config.h>
3#endif
4
5#include <string.h>
6#include <math.h>
7
8#include <cpl.h>
9
10#include "kmo_dfs.h"
11
12static int kmos_test_create(cpl_plugin *);
13static int kmos_test_exec(cpl_plugin *);
14static int kmos_test_destroy(cpl_plugin *);
15static int kmos_test(cpl_parameterlist *, cpl_frameset *);
16
17static char kmos_test_description[] = "Testing\n" ;
18
19int cpl_plugin_get_info(cpl_pluginlist *list)
20{
21 cpl_recipe *recipe = cpl_calloc(1, sizeof *recipe);
22 cpl_plugin *plugin = &recipe->interface;
23
24 cpl_plugin_init(plugin,
25 CPL_PLUGIN_API,
26 KMOS_BINARY_VERSION,
27 CPL_PLUGIN_TYPE_RECIPE,
28 "kmos_test",
29 "Testing",
30 kmos_test_description,
31 "Yves Jung",
32 "yjung@eso.org",
33 kmos_get_license(),
34 kmos_test_create,
35 kmos_test_exec,
36 kmos_test_destroy);
37
38 cpl_pluginlist_append(list, plugin);
39
40 return 0;
41}
42
43static int kmos_test_create(cpl_plugin *plugin)
44{
45 cpl_recipe *recipe;
46
47 /* Check that the plugin is part of a valid recipe */
48 if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
49 recipe = (cpl_recipe *)plugin;
50 else
51 return -1;
52
53 /* Create the parameters list in the cpl_recipe object */
54 recipe->parameters = cpl_parameterlist_new();
55
56 return 0 ;
57}
58
59static int kmos_test_exec(cpl_plugin *plugin)
60{
61 cpl_recipe *recipe;
62
63 /* Get the recipe out of the plugin */
64 if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
65 recipe = (cpl_recipe *)plugin;
66 else return -1;
67
68 return kmos_test(recipe->parameters, recipe->frames);
69}
70
71static int kmos_test_destroy(cpl_plugin *plugin)
72{
73 cpl_recipe *recipe;
74
75 /* Get the recipe out of the plugin */
76 if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
77 recipe = (cpl_recipe *)plugin;
78 else return -1 ;
79
80 cpl_parameterlist_delete(recipe->parameters);
81 return 0 ;
82}
83
84static int kmo_priv_lorentz1d_fnc(const double x[], const double a[],
85 double *result)
86{
87 *result = a[0] + (a[1]*a[3]) / (2*CPL_MATH_PI*(pow((x[0]-a[2]), 2) +
88 pow((a[3]/2), 2))) +a[4]*x[0];
89 return 0;
90}
91static int kmo_priv_lorentz1d_fncd(const double x[], const double a[],
92 double result[])
93{
94 if (a == NULL) result = NULL;
95 double aa = pow((x[0]-a[2]), 2) + pow((a[3]/2),2);
96 double pow2aa = pow(aa, 2);
97 result[0] = 1;
98 result[1] = a[3] / (2*aa*CPL_MATH_PI);
99 result[2] = (a[1]*a[3]*(x[0]-a[2])) / (CPL_MATH_PI*pow2aa);
100 result[3] = a[1]/(CPL_MATH_2PI*aa) - (a[1]*pow(a[3], 2)) /
101 (4*CPL_MATH_PI*pow2aa);
102 result[4] = x[0];
103 return 0;
104}
105
106static int kmos_test(cpl_parameterlist * parlist, cpl_frameset * frameset)
107{
108 cpl_frame * frame_x ;
109 cpl_frame * frame_y ;
110 cpl_vector * vec_x ;
111 cpl_vector * vec_y ;
112 cpl_vector * sigma_y ;
113 cpl_vector * vec_x_extract ;
114 cpl_vector * vec_y_extract ;
115 cpl_vector * fit_par ;
116 cpl_vector * guess_vec ;
117 double * pguess_vec ;
118 cpl_vector * fit ;
119 double * pfit ;
120 cpl_matrix * x_mat ;
121 double line_center, line_width ;
122 int lowIndex, highIndex, i ;
123 double red_chisq ;
124 cpl_matrix * covariance ;
125 double x_array[1] ;
126 double y_array[1] ;
127
128 /* Check entries */
129 if (parlist == NULL || frameset == NULL) {
130 cpl_msg_error(__func__, "Null Inputs") ;
131 cpl_error_set(__func__, CPL_ERROR_NULL_INPUT) ;
132 return -1 ;
133 }
134
135 /* Initialise */
136 line_center = 0.88635 ;
137 line_width = 0.013 ;
138 red_chisq = 0.0 ;
139 covariance = NULL ;
140
141 /* Identify the RAW and CALIB frames in the input frameset */
142 if (kmo_dfs_set_groups(frameset) != 1) {
143 cpl_msg_error(__func__, "Cannot identify RAW and CALIB frames") ;
144 cpl_error_set(__func__, CPL_ERROR_ILLEGAL_INPUT) ;
145 return -1 ;
146 }
147
148 /* Load */
149 frame_x = cpl_frameset_get_position(frameset, 0);
150 frame_y = cpl_frameset_get_position(frameset, 1);
151 vec_x = cpl_vector_load(cpl_frame_get_filename(frame_x), 0) ;
152 vec_y = cpl_vector_load(cpl_frame_get_filename(frame_y), 0) ;
153
154 /* Extract the part to fit */
155 lowIndex = cpl_vector_find(vec_x, line_center - line_width/2);
156 highIndex = cpl_vector_find(vec_x, line_center + line_width/2);
157 vec_x_extract = cpl_vector_extract(vec_x, lowIndex, highIndex, 1);
158 vec_y_extract = cpl_vector_extract(vec_y, lowIndex, highIndex, 1);
159
160 cpl_plot_vector("set grid;", "t 'line to fit' w lines", "", vec_y_extract);
161
162 /* Fitting parameters */
163 fit_par = cpl_vector_new(5);
164
165 /* Initial estimates */
166 cpl_vector_set(fit_par, 0, 0.0);
167 cpl_vector_set(fit_par, 0, 17000.0);
168 cpl_vector_set(fit_par, 1, cpl_vector_get_min(vec_y_extract));
169 cpl_vector_set(fit_par, 1, 17310.7);
170 cpl_vector_set(fit_par, 1, 6000);
171 cpl_vector_set(fit_par, 2, line_center);
172 cpl_vector_set(fit_par, 2, 0.88635);
173 cpl_vector_set(fit_par, 3, 0.000151367);
174 cpl_vector_set(fit_par, 4, 0.0);
175 cpl_vector_dump(fit_par, stdout);
176
177
178 /* cpl_vector_set(fit_par, 0, 23000.0); */
179 /* cpl_vector_set(fit_par, 1, 0.0); */
180 /* cpl_vector_set(fit_par, 2, 0.0); */
181 /* cpl_vector_set(fit_par, 3, 0.0); */
182 /* cpl_vector_set(fit_par, 4, 0.0); */
183
184 /* Compute the guess vector for plotting*/
185 guess_vec = cpl_vector_new(cpl_vector_get_size(vec_y_extract));
186 pguess_vec = cpl_vector_get_data(guess_vec);
187 for (i = 0; i < cpl_vector_get_size(guess_vec); i++) {
188 x_array[0] = cpl_vector_get(vec_x_extract, i);
189 kmo_priv_lorentz1d_fnc(x_array, cpl_vector_get_data(fit_par),
190 y_array);
191 pguess_vec[i] = y_array[0];
192 }
193 cpl_plot_vector("set grid;", "t 'Guess' w lines", "", guess_vec);
194 cpl_vector_delete(guess_vec) ;
195
196
197 /* Prepare data for fitting */
198 x_mat = cpl_matrix_wrap(cpl_vector_get_size(vec_x_extract), 1,
199 cpl_vector_get_data(vec_x_extract));
200
201 sigma_y = cpl_vector_new(cpl_vector_get_size(vec_x_extract));
202 cpl_vector_fill(sigma_y, 1.0);
203
204 /* Run fitting */
205 if (cpl_fit_lvmq(x_mat, NULL, vec_y_extract, sigma_y, fit_par,
206 NULL, &kmo_priv_lorentz1d_fnc, &kmo_priv_lorentz1d_fncd,
207 CPL_FIT_LVMQ_TOLERANCE, /* 0.01 */
208 CPL_FIT_LVMQ_COUNT, /* 5 */
209 CPL_FIT_LVMQ_MAXITER, /* 1000 */
210 NULL, &red_chisq, &covariance) != CPL_ERROR_NONE) {
211 return -1 ;
212 }
213 cpl_vector_dump(fit_par, stdout);
214
215 /* Compute the fitted spectrum */
216 fit = cpl_vector_new(cpl_vector_get_size(vec_y_extract));
217 pfit = cpl_vector_get_data(fit);
218 for (i = 0; i < cpl_vector_get_size(fit); i++) {
219 x_array[0] = cpl_vector_get(vec_x_extract, i);
220 kmo_priv_lorentz1d_fnc(x_array, cpl_vector_get_data(fit_par),
221 y_array);
222 pfit[i] = y_array[0];
223 }
224 cpl_plot_vector("set grid;", "t 'Fitted' w lines", "", fit);
225
226 cpl_vector_delete(vec_x) ;
227 cpl_vector_delete(vec_x_extract) ;
228 cpl_vector_delete(vec_y) ;
229 cpl_vector_delete(vec_y_extract) ;
230
231 return CPL_ERROR_NONE;
232}
233
234
int cpl_plugin_get_info(cpl_pluginlist *list)
Build the list of available plugins, for this module.