39 #include "irplib_ppm.h"
40 #include "irplib_wlxcorr.h"
41 #include "irplib_spectrum.h"
44 #ifndef CPL_SIZE_FORMAT
45 #define CPL_SIZE_FORMAT "d"
53 #ifdef IRPLIB_PPM_USE_METHOD2
54 static cpl_vector * irplib_ppm_convolve_line(
const cpl_vector *,
double,
double);
55 static cpl_vector * irplib_ppm_detect_lines(
const cpl_vector *,
double) ;
85 const cpl_vector * spectrum,
86 const cpl_bivector * lines_catalog,
87 const cpl_polynomial * poly_init,
93 cpl_table ** tab_infos)
95 #ifdef IRPLIB_PPM_USE_METHOD2
96 cpl_vector * spec_conv ;
99 cpl_vector * det_lines ;
100 double * pdet_lines ;
101 cpl_vector * cat_lines ;
102 double * pcat_lines ;
104 double disp_min, disp_max, disp ;
105 int nlines_cat, nlines ;
106 const double * plines_catalog_x ;
107 const double * plines_catalog_y ;
108 cpl_bivector * matched ;
109 cpl_matrix * matchedx;
111 cpl_polynomial * fitted ;
112 cpl_table * spc_table ;
113 const cpl_vector* vectors_plot[3];
114 cpl_vector * plot_y ;
115 int wl_ind, start_ind, stop_ind ;
119 cpl_error_code error;
122 if (spectrum == NULL)
return NULL ;
123 if (lines_catalog == NULL)
return NULL ;
124 if (poly_init == NULL)
return NULL ;
127 spec_sz = cpl_vector_get_size(spectrum) ;
128 deg_loc = (cpl_size)degree ;
130 #ifdef IRPLIB_PPM_USE_METHOD2
133 if ((spec_conv = irplib_ppm_convolve_line(spectrum, slitw, fwhm)) == NULL) {
134 cpl_msg_error(cpl_func,
"Cannot convolve the signal") ;
139 if ((det_lines = irplib_ppm_detect_lines(spec_conv, 0.9)) == NULL) {
140 cpl_msg_error(cpl_func,
"Cannot detect lines") ;
141 cpl_vector_delete(spec_conv) ;
144 cpl_vector_delete(spec_conv) ;
148 thresh, 0, NULL, NULL)) == NULL) {
149 cpl_msg_error(cpl_func,
"Cannot convolve the signal") ;
153 cpl_msg_info(cpl_func,
"Detected %"CPL_SIZE_FORMAT
" lines",
154 cpl_vector_get_size(det_lines));
157 wmin = cpl_polynomial_eval_1d(poly_init, 1.0, NULL) ;
158 wmax = cpl_polynomial_eval_1d(poly_init, spec_sz, NULL) ;
159 plines_catalog_x = cpl_bivector_get_x_data_const(lines_catalog) ;
160 plines_catalog_y = cpl_bivector_get_y_data_const(lines_catalog) ;
161 nlines = cpl_bivector_get_size(lines_catalog) ;
163 start_ind = stop_ind = -1 ;
164 for (i=0 ; i<nlines ; i++) {
165 if (plines_catalog_x[i] > wmin && plines_catalog_x[i] < wmax &&
166 plines_catalog_y[i] > 0.0) {
168 if (start_ind<0) start_ind = i ;
172 if (nlines_cat == 0) {
173 cpl_msg_error(cpl_func,
"No lines in catalog") ;
174 cpl_vector_delete(det_lines) ;
177 cat_lines = cpl_vector_new(nlines_cat) ;
178 pcat_lines = cpl_vector_get_data(cat_lines) ;
180 for (i=0 ; i<nlines ; i++) {
181 if (plines_catalog_x[i] > wmin && plines_catalog_x[i] < wmax &&
182 plines_catalog_y[i] > 0.0) {
183 pcat_lines[nlines_cat] = plines_catalog_x[i] ;
191 irplib_wlxcorr_catalog_plot(lines_catalog, wmin, wmax) ;
194 fill_val = cpl_vector_get_max(spectrum) ;
195 plot_y = cpl_vector_new(spec_sz);
196 cpl_vector_fill(plot_y, 0.0) ;
197 pdet_lines = cpl_vector_get_data(det_lines) ;
198 for (i=0 ; i<cpl_vector_get_size(det_lines) ; i++) {
199 cpl_vector_set(plot_y, (
int)pdet_lines[i], fill_val) ;
201 vectors_plot[0] = NULL ;
202 vectors_plot[1] = spectrum ;
203 vectors_plot[2] = plot_y ;
205 cpl_plot_vectors(
"set grid;set xlabel 'Position (Pixel)';set ylabel "
206 "'Intensity (ADU/sec)';",
207 "t 'Spectrum with detected lines' w lines",
"",
209 cpl_vector_delete(plot_y) ;
213 disp = (wmax-wmin) / spec_sz ;
214 disp_min = disp - (disp/10) ;
215 disp_max = disp + (disp/10) ;
216 matched = cpl_ppm_match_positions(det_lines, cat_lines, disp_min,
217 disp_max, 0.05, NULL, NULL);
218 cpl_vector_delete(det_lines) ;
219 cpl_vector_delete(cat_lines) ;
221 if (matched == NULL) {
222 cpl_msg_error(cpl_func,
"Cannot apply the point pattern matching") ;
226 match_sz = cpl_bivector_get_size(matched);
228 cpl_msg_info(cpl_func,
"Matched %d lines", match_sz) ;
230 if (match_sz <= deg_loc) {
231 cpl_msg_error(cpl_func,
"Not enough match for the fit") ;
232 cpl_bivector_delete(matched) ;
238 const double * pmatched ;
239 cpl_bivector * biplot ;
240 cpl_vector * plot_cat_x ;
241 cpl_vector * plot_cat_y ;
243 fill_val = cpl_vector_get_max(spectrum) ;
244 plot_y = cpl_vector_new(spec_sz);
245 cpl_vector_fill(plot_y, 0.0) ;
246 pmatched = cpl_bivector_get_x_data_const(matched) ;
247 for (i=0 ; i < match_sz; i++) {
248 cpl_vector_set(plot_y, (
int)pmatched[i], fill_val) ;
250 vectors_plot[0] = NULL ;
251 vectors_plot[1] = spectrum ;
252 vectors_plot[2] = plot_y ;
254 cpl_plot_vectors(
"set grid;set xlabel 'Position (Pixel)';set ylabel "
255 "'Intensity (ADU/sec)';",
256 "t 'Spectrum with matched lines' w lines",
"",
258 cpl_vector_delete(plot_y) ;
261 plot_cat_x=cpl_vector_extract(cpl_bivector_get_x_const(lines_catalog),
262 start_ind, stop_ind, 1) ;
263 plot_cat_y=cpl_vector_extract(cpl_bivector_get_y_const(lines_catalog),
264 start_ind, stop_ind, 1) ;
265 biplot = cpl_bivector_wrap_vectors(plot_cat_x, plot_cat_y) ;
266 cpl_plot_bivector(
"set grid;set xlabel 'Wavelength';set ylabel "
267 "'Emission';",
"t 'Catalog' w impulses",
"",
269 cpl_bivector_unwrap_vectors(biplot) ;
271 plot_y = cpl_vector_duplicate(plot_cat_y) ;
272 cpl_vector_fill(plot_y, 0.0) ;
273 pmatched = cpl_bivector_get_y_data_const(matched) ;
274 fill_val=cpl_vector_get_mean(plot_cat_y) ;
275 for (i=0 ; i < match_sz; i++) {
277 while (pmatched[i] > cpl_vector_get(plot_cat_x, wl_ind)
278 && wl_ind < spec_sz) wl_ind++ ;
279 if (wl_ind < spec_sz) cpl_vector_set(plot_y, wl_ind, fill_val) ;
281 biplot = cpl_bivector_wrap_vectors(plot_cat_x, plot_y) ;
282 cpl_plot_bivector(
"set grid;set xlabel 'Wavelength';set ylabel "
283 "'Emission';",
"t 'Catalog (matched lines)' w "
284 "impulses",
"", biplot) ;
285 cpl_bivector_unwrap_vectors(biplot) ;
286 cpl_vector_delete(plot_cat_x) ;
287 cpl_vector_delete(plot_cat_y) ;
288 cpl_vector_delete(plot_y) ;
292 matchedx = cpl_matrix_wrap(1, match_sz, cpl_bivector_get_x_data(matched));
293 fitted = cpl_polynomial_new(1);
294 error = cpl_polynomial_fit(fitted, matchedx, NULL,
295 cpl_bivector_get_y_const(matched), NULL,
296 CPL_FALSE, NULL, °_loc);
297 cpl_bivector_delete(matched);
298 (void)cpl_matrix_unwrap(matchedx);
300 cpl_msg_error(cpl_func,
"Cannot fit the polynomial") ;
301 cpl_polynomial_delete(fitted);
306 if ((spc_table = irplib_wlxcorr_gen_spc_table(spectrum,
307 lines_catalog, slitw, fwhm, poly_init, fitted)) == NULL) {
308 cpl_msg_error(cpl_func,
"Cannot generate the infos table") ;
309 cpl_polynomial_delete(fitted) ;
312 if (tab_infos != NULL) *tab_infos = spc_table ;
313 else cpl_table_delete(spc_table) ;
319 #ifdef IRPLIB_PPM_USE_METHOD2
332 static cpl_vector * irplib_ppm_convolve_line(
333 const cpl_vector * spectrum,
337 cpl_vector * conv_kernel ;
338 cpl_vector * line_profile ;
339 cpl_vector * xcorrs ;
340 cpl_vector * spec_ext ;
341 cpl_vector * xc_single ;
342 int hs, line_sz, sp_sz ;
346 if (spectrum == NULL)
return NULL ;
349 if ((conv_kernel = irplib_wlxcorr_convolve_create_kernel(slitw,
351 cpl_msg_error(cpl_func,
"Cannot create kernel") ;
354 hs = cpl_vector_get_size(conv_kernel) ;
355 line_sz = 2 * hs + 1 ;
358 line_profile = cpl_vector_new(line_sz) ;
359 cpl_vector_fill(line_profile, 0.0) ;
360 cpl_vector_set(line_profile, hs, 1.0) ;
361 if (irplib_wlxcorr_convolve(line_profile, conv_kernel) != 0) {
362 cpl_msg_error(cpl_func,
"Cannot create line profile") ;
363 cpl_vector_delete(line_profile) ;
364 cpl_vector_delete(conv_kernel) ;
367 cpl_vector_delete(conv_kernel) ;
370 sp_sz = cpl_vector_get_size(spectrum) ;
371 xcorrs = cpl_vector_new(sp_sz) ;
372 cpl_vector_fill(xcorrs, 0.0) ;
373 xc_single = cpl_vector_new(1) ;
376 for (i=hs ; i<sp_sz-hs ; i++) {
378 if ((spec_ext = cpl_vector_extract(spectrum, i-hs, i+hs, 1)) == NULL) {
379 cpl_msg_error(cpl_func,
"Cannot extract spectrum") ;
380 cpl_vector_delete(xc_single) ;
381 cpl_vector_delete(line_profile) ;
384 if (cpl_vector_correlate(xc_single, spec_ext, line_profile) < 0) {
385 cpl_msg_error(cpl_func,
"Cannot correlate") ;
386 cpl_vector_delete(xc_single) ;
387 cpl_vector_delete(line_profile) ;
388 cpl_vector_delete(spec_ext) ;
391 cpl_vector_set(xcorrs, i, cpl_vector_get(xc_single, 0)) ;
392 cpl_vector_delete(spec_ext) ;
394 cpl_vector_delete(xc_single) ;
395 cpl_vector_delete(line_profile) ;
410 static cpl_vector * irplib_ppm_detect_lines(
411 const cpl_vector * spec,
414 cpl_vector * spec_loc ;
418 int spec_loc_sz, max_ind, nlines ;
423 if (spec == NULL)
return NULL ;
426 spec_loc = cpl_vector_duplicate(spec) ;
427 pspec_loc = cpl_vector_get_data(spec_loc) ;
428 spec_loc_sz = cpl_vector_get_size(spec_loc) ;
431 for (i=0 ; i<spec_loc_sz ; i++)
432 if (pspec_loc[i] < threshold) pspec_loc[i] = 0.0 ;
435 lines = cpl_vector_new(spec_loc_sz) ;
436 plines = cpl_vector_get_data(lines) ;
440 while ((max = cpl_vector_get_max(spec_loc)) > threshold) {
443 while (max_ind < spec_loc_sz && pspec_loc[max_ind] < max) max_ind++ ;
444 if (max_ind == spec_loc_sz) {
445 cpl_msg_error(cpl_func,
"Cannot find maximum") ;
446 cpl_vector_delete(spec_loc) ;
447 cpl_vector_delete(lines) ;
450 if (max_ind == 0 || max_ind == spec_loc_sz-1) {
451 pspec_loc[max_ind] = 0 ;
456 plines[nlines] = pspec_loc[max_ind] * max_ind +
457 pspec_loc[max_ind-1] * (max_ind-1) +
458 pspec_loc[max_ind+1] * (max_ind+1) ;
459 plines[nlines] /= pspec_loc[max_ind] + pspec_loc[max_ind+1] +
460 pspec_loc[max_ind-1] ;
466 while (i>=0 && pspec_loc[i] > threshold) {
471 while (i<spec_loc_sz && pspec_loc[i] > threshold) {
476 cpl_vector_delete(spec_loc) ;
480 cpl_msg_error(cpl_func,
"Cannot detect any line") ;
481 cpl_vector_delete(lines) ;
486 cpl_vector_set_size(lines, nlines) ;
489 cpl_vector_sort(lines, 1) ;