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 *);
17static char kmos_test_description[] =
"Testing\n" ;
21 cpl_recipe *recipe = cpl_calloc(1,
sizeof *recipe);
22 cpl_plugin *plugin = &recipe->interface;
24 cpl_plugin_init(plugin,
27 CPL_PLUGIN_TYPE_RECIPE,
30 kmos_test_description,
38 cpl_pluginlist_append(list, plugin);
43static int kmos_test_create(cpl_plugin *plugin)
48 if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
49 recipe = (cpl_recipe *)plugin;
54 recipe->parameters = cpl_parameterlist_new();
59static int kmos_test_exec(cpl_plugin *plugin)
64 if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
65 recipe = (cpl_recipe *)plugin;
68 return kmos_test(recipe->parameters, recipe->frames);
71static int kmos_test_destroy(cpl_plugin *plugin)
76 if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
77 recipe = (cpl_recipe *)plugin;
80 cpl_parameterlist_delete(recipe->parameters);
84static int kmo_priv_lorentz1d_fnc(
const double x[],
const double a[],
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];
91static int kmo_priv_lorentz1d_fncd(
const double x[],
const double a[],
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);
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);
106static int kmos_test(cpl_parameterlist * parlist, cpl_frameset * frameset)
108 cpl_frame * frame_x ;
109 cpl_frame * frame_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 ;
121 double line_center, line_width ;
122 int lowIndex, highIndex, i ;
124 cpl_matrix * covariance ;
129 if (parlist == NULL || frameset == NULL) {
130 cpl_msg_error(__func__,
"Null Inputs") ;
131 cpl_error_set(__func__, CPL_ERROR_NULL_INPUT) ;
136 line_center = 0.88635 ;
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) ;
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) ;
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);
160 cpl_plot_vector(
"set grid;",
"t 'line to fit' w lines",
"", vec_y_extract);
163 fit_par = cpl_vector_new(5);
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);
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),
191 pguess_vec[i] = y_array[0];
193 cpl_plot_vector(
"set grid;",
"t 'Guess' w lines",
"", guess_vec);
194 cpl_vector_delete(guess_vec) ;
198 x_mat = cpl_matrix_wrap(cpl_vector_get_size(vec_x_extract), 1,
199 cpl_vector_get_data(vec_x_extract));
201 sigma_y = cpl_vector_new(cpl_vector_get_size(vec_x_extract));
202 cpl_vector_fill(sigma_y, 1.0);
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,
209 CPL_FIT_LVMQ_MAXITER,
210 NULL, &red_chisq, &covariance) != CPL_ERROR_NONE) {
213 cpl_vector_dump(fit_par, stdout);
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),
222 pfit[i] = y_array[0];
224 cpl_plot_vector(
"set grid;",
"t 'Fitted' w lines",
"", fit);
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) ;
231 return CPL_ERROR_NONE;
int cpl_plugin_get_info(cpl_pluginlist *list)
Build the list of available plugins, for this module.