28#include "irplib_wavecal_impl.h"
30#include "irplib_wlxcorr.h"
57#define IRPLIB_MAX(A,B) ((A) > (B) ? (A) : (B))
58#define IRPLIB_MIN(A,B) ((A) < (B) ? (A) : (B))
60#define IRPLIB_PTR_SWAP(a,b) \
61 do { void * irplib_ptr_swap =(a);(a)=(b);(b)=irplib_ptr_swap; } while (0)
67static void irplib_wlxcorr_estimate(cpl_vector *, cpl_vector *,
71 const cpl_polynomial *,
74static int irplib_wlxcorr_signal_resample(cpl_vector *,
const cpl_vector *,
75 const cpl_bivector *) ;
76static cpl_error_code cpl_vector_fill_lss_profile_symmetric(cpl_vector *,
78static cpl_error_code irplib_wlcalib_fill_spectrum(cpl_vector *,
81 const cpl_polynomial *,
int);
83static cpl_boolean irplib_wlcalib_is_lines(
const cpl_vector *,
84 const cpl_polynomial *,
127cpl_polynomial * irplib_wlxcorr_best_poly(
const cpl_vector * spectrum,
128 const cpl_bivector * lines_catalog,
130 const cpl_polynomial * guess_poly,
131 const cpl_vector * wl_error,
137 cpl_vector ** xcorrs)
139 const int spec_sz = cpl_vector_get_size(spectrum);
140 const int nfree = cpl_vector_get_size(wl_error);
144 cpl_vector * init_pts_wl;
145 cpl_matrix * init_pts_x;
147 cpl_vector * vxcorrs;
148 cpl_vector * conv_kernel = NULL;
149 cpl_polynomial * poly_sol;
150 cpl_polynomial * poly_candi;
151 const double * pwl_error = cpl_vector_get_data_const(wl_error);
153 cpl_size degree_loc ;
154 const cpl_boolean symsamp = CPL_TRUE;
155 const cpl_boolean is_lines
156 = irplib_wlcalib_is_lines(cpl_bivector_get_x_const(lines_catalog),
157 guess_poly, spec_sz, 1.0);
163 if (wlres != NULL) *wlres = NULL;
164 if (xcorrs != NULL) *xcorrs = NULL;
167 cpl_msg_debug(cpl_func,
"Checking %d^%d dispersion polynomials (slitw=%g, "
168 "fwhm=%g) against %d-point observed spectrum with%s "
169 "catalog resampling", nsamples, nfree, slitw, fwhm, spec_sz,
170 is_lines ?
"out" :
"");
172 cpl_ensure(xc != NULL, CPL_ERROR_NULL_INPUT, NULL);
174 cpl_ensure(spectrum != NULL, CPL_ERROR_NULL_INPUT, NULL);
175 cpl_ensure(lines_catalog != NULL, CPL_ERROR_NULL_INPUT, NULL);
176 cpl_ensure(guess_poly != NULL, CPL_ERROR_NULL_INPUT, NULL);
177 cpl_ensure(wl_error != NULL, CPL_ERROR_NULL_INPUT, NULL);
178 cpl_ensure(nfree >= 2, CPL_ERROR_ILLEGAL_INPUT, NULL);
179 cpl_ensure(nsamples > 0, CPL_ERROR_ILLEGAL_INPUT, NULL);
181 cpl_ensure(1 + degree == nfree, CPL_ERROR_ILLEGAL_INPUT, NULL);
183 cpl_ensure(cpl_polynomial_get_dimension(guess_poly) == 1,
184 CPL_ERROR_ILLEGAL_INPUT, NULL);
189 for (i = 0; i < nfree; i++) {
190 if (pwl_error[i] != 0.0)
break;
192 cpl_ensure(i < nfree, CPL_ERROR_ILLEGAL_INPUT, NULL);
197 conv_kernel = irplib_wlxcorr_convolve_create_kernel(slitw, fwhm);
198 cpl_ensure(conv_kernel != NULL, CPL_ERROR_ILLEGAL_INPUT, NULL);
202 init_pts_x = cpl_matrix_new(1, nfree);
203 init_pts_wl = cpl_vector_new(nfree);
204 pts_wl = cpl_vector_new(nfree);
205 for (i = 0; i < nfree; i++) {
206 const double xpos = spec_sz * i / (double)degree;
207 const double wlpos = cpl_polynomial_eval_1d(guess_poly, xpos, NULL)
208 - 0.5 * pwl_error[i];
210 cpl_matrix_set(init_pts_x, 0, i, xpos);
211 cpl_vector_set(init_pts_wl, i, wlpos);
217 vxcorrs = xcorrs != NULL ? cpl_vector_new(ntests) : NULL;
219 poly_sol = cpl_polynomial_new(1);
220 poly_candi = cpl_polynomial_new(1);
221 model = cpl_vector_new(spec_sz);
222 vxc = cpl_vector_new(1);
223 dxc = cpl_vector_get_data_const(vxc);
226 for (i=0; i < ntests; i++) {
232 for (deg = degree; deg >= 0; deg--, idiv /= nsamples) {
233 const int imod = idiv % nsamples;
234 const double wlpos = cpl_vector_get(init_pts_wl, deg)
235 + imod * pwl_error[deg] / nsamples;
240 cpl_vector_set(pts_wl, deg, wlpos);
246 degree_loc = (cpl_size)degree ;
247 cpl_polynomial_fit(poly_candi, init_pts_x, &symsamp, pts_wl,
248 NULL, CPL_FALSE, NULL, °ree_loc);
250 irplib_wlxcorr_estimate(vxc, model, spectrum, lines_catalog,
251 conv_kernel, poly_candi, slitw, fwhm);
252 if (vxcorrs != NULL) cpl_vector_set(vxcorrs, i, *dxc);
256 IRPLIB_PTR_SWAP(poly_sol, poly_candi);
260 cpl_vector_delete(model);
261 cpl_vector_delete(vxc);
262 cpl_vector_delete(conv_kernel);
263 cpl_vector_delete(pts_wl);
264 cpl_matrix_delete(init_pts_x);
265 cpl_vector_delete(init_pts_wl);
266 cpl_polynomial_delete(poly_candi);
268#ifdef CPL_WLCALIB_FAIL_ON_CONSTANT
270 if (cpl_polynomial_get_degree(poly_sol) == 0) {
271 cpl_polynomial_delete(poly_sol);
272 cpl_vector_delete(vxcorrs);
274 cpl_error_set_message_macro(cpl_func, CPL_ERROR_ILLEGAL_OUTPUT,
275 __FILE__, __LINE__,
"Found a constant "
277 cpl_errorstate_dump(prestate, CPL_FALSE, NULL);
286 cpl_errorstate prestate = cpl_errorstate_get();
288 *wlres = irplib_wlxcorr_gen_spc_table(spectrum, lines_catalog, slitw,
289 fwhm, guess_poly, poly_sol);
290 if (*wlres == NULL) {
291 cpl_polynomial_delete(poly_sol);
292 cpl_vector_delete(vxcorrs);
294 cpl_error_set_message_macro(cpl_func, CPL_ERROR_ILLEGAL_OUTPUT,
295 __FILE__, __LINE__,
"Cannot generate "
298 cpl_errorstate_set(prestate);
303 if (xcorrs != NULL) {
346cpl_polynomial * irplib_wlxcorr_best_poly_prop(
const cpl_vector * spectrum,
347 const cpl_bivector * lines_catalog,
349 const cpl_polynomial * guess_poly,
350 const cpl_vector * wl_error,
356 cpl_vector ** xcorrs)
358 const int spec_sz = cpl_vector_get_size(spectrum);
359 const int nfree = cpl_vector_get_size(wl_error);
363 cpl_vector * init_pts_wl;
364 cpl_matrix * init_pts_x;
366 cpl_vector * vxcorrs;
367 cpl_vector * conv_kernel = NULL;
368 cpl_polynomial * poly_sol;
369 cpl_polynomial * poly_candi;
370 const double * pwl_error = cpl_vector_get_data_const(wl_error);
372 cpl_size degree_loc ;
373 const cpl_boolean symsamp = CPL_TRUE;
374 const cpl_boolean is_lines
375 = irplib_wlcalib_is_lines(cpl_bivector_get_x_const(lines_catalog),
376 guess_poly, spec_sz, 1.0);
382 if (wlres != NULL) *wlres = NULL;
383 if (xcorrs != NULL) *xcorrs = NULL;
386 cpl_msg_debug(cpl_func,
"Checking %d^%d dispersion polynomials (slitw=%g, "
387 "fwhm=%g) against %d-point observed spectrum with%s "
388 "catalog resampling", nsamples, nfree, slitw, fwhm, spec_sz,
389 is_lines ?
"out" :
"");
391 cpl_ensure(xc != NULL, CPL_ERROR_NULL_INPUT, NULL);
393 cpl_ensure(spectrum != NULL, CPL_ERROR_NULL_INPUT, NULL);
394 cpl_ensure(lines_catalog != NULL, CPL_ERROR_NULL_INPUT, NULL);
395 cpl_ensure(guess_poly != NULL, CPL_ERROR_NULL_INPUT, NULL);
396 cpl_ensure(wl_error != NULL, CPL_ERROR_NULL_INPUT, NULL);
397 cpl_ensure(nfree >= 1, CPL_ERROR_ILLEGAL_INPUT, NULL);
398 cpl_ensure(nsamples > 0, CPL_ERROR_ILLEGAL_INPUT, NULL);
400 cpl_ensure(1 + degree == nfree, CPL_ERROR_ILLEGAL_INPUT, NULL);
402 cpl_ensure(cpl_polynomial_get_dimension(guess_poly) == 1,
403 CPL_ERROR_ILLEGAL_INPUT, NULL);
408 for (i = 0; i < nfree; i++) {
409 if (pwl_error[i] != 0.0)
break;
411 cpl_ensure(i < nfree, CPL_ERROR_ILLEGAL_INPUT, NULL);
416 conv_kernel = irplib_wlxcorr_convolve_create_kernel(slitw, fwhm);
417 cpl_ensure(conv_kernel != NULL, CPL_ERROR_ILLEGAL_INPUT, NULL);
421 init_pts_x = cpl_matrix_new(1, nfree);
422 init_pts_wl = cpl_vector_new(nfree);
423 pts_wl = cpl_vector_new(nfree);
424 const double degree_loc2 = degree == 0 ? 1 : (double)degree ;
425 for (i = 0; i < nfree; i++) {
426 const double xpos = spec_sz * i / degree_loc2;
427 const double wlpos = cpl_polynomial_eval_1d(guess_poly, xpos, NULL)
428 - 0.5 * pwl_error[i];
430 cpl_matrix_set(init_pts_x, 0, i, xpos);
431 cpl_vector_set(init_pts_wl, i, wlpos);
437 vxcorrs = xcorrs != NULL ? cpl_vector_new(ntests) : NULL;
439 poly_sol = cpl_polynomial_new(1);
440 poly_candi = cpl_polynomial_new(1);
441 model = cpl_vector_new(spec_sz);
442 vxc = cpl_vector_new(1);
443 dxc = cpl_vector_get_data_const(vxc);
446 for (i=0; i < ntests; i++) {
453 for (deg = degree; deg >= 0; deg--, idiv /= nsamples) {
454 const int imod = idiv % nsamples;
455 const double wlpos = cpl_vector_get(init_pts_wl, deg)
456 + imod * pwl_error[deg] / nsamples;
461 cpl_vector_set(pts_wl, deg, wlpos);
467 degree_loc = (cpl_size)degree ;
468 cpl_polynomial_fit(poly_candi, init_pts_x, &symsamp, pts_wl,
469 NULL, CPL_FALSE, NULL, °ree_loc);
472 degree_loc = cpl_polynomial_get_degree(guess_poly);
473 for (power = degree + 1; power < degree_loc + 1; power++){
474 cpl_polynomial_set_coeff(poly_candi, &power,
475 cpl_polynomial_get_coeff(guess_poly, &power));
479 irplib_wlxcorr_estimate(vxc, model, spectrum, lines_catalog,
480 conv_kernel, poly_candi, slitw, fwhm);
481 if (vxcorrs != NULL) cpl_vector_set(vxcorrs, i, *dxc);
485 IRPLIB_PTR_SWAP(poly_sol, poly_candi);
496 cpl_vector_delete(model);
497 cpl_vector_delete(vxc);
498 cpl_vector_delete(conv_kernel);
499 cpl_vector_delete(pts_wl);
500 cpl_matrix_delete(init_pts_x);
501 cpl_vector_delete(init_pts_wl);
502 cpl_polynomial_delete(poly_candi);
504#ifdef CPL_WLCALIB_FAIL_ON_CONSTANT
506 if (cpl_polynomial_get_degree(poly_sol) == 0) {
507 cpl_polynomial_delete(poly_sol);
508 cpl_vector_delete(vxcorrs);
510 cpl_error_set_message_macro(cpl_func, CPL_ERROR_ILLEGAL_OUTPUT,
511 __FILE__, __LINE__,
"Found a constant "
513 cpl_errorstate_dump(prestate, CPL_FALSE, NULL);
522 cpl_errorstate prestate = cpl_errorstate_get();
524 *wlres = irplib_wlxcorr_gen_spc_table(spectrum, lines_catalog, slitw,
525 fwhm, guess_poly, poly_sol);
526 if (*wlres == NULL) {
527 cpl_polynomial_delete(poly_sol);
528 cpl_vector_delete(vxcorrs);
530 cpl_error_set_message_macro(cpl_func, CPL_ERROR_ILLEGAL_OUTPUT,
531 __FILE__, __LINE__,
"Cannot generate "
534 cpl_errorstate_set(prestate);
539 if (xcorrs != NULL) {
567cpl_table * irplib_wlxcorr_gen_spc_table(
568 const cpl_vector * spectrum,
569 const cpl_bivector * lines_catalog,
572 const cpl_polynomial * guess_poly,
573 const cpl_polynomial * corr_poly)
576 cpl_vector * conv_kernel = NULL;
577 cpl_bivector * gen_init ;
578 cpl_bivector * gen_corr ;
579 cpl_table * spc_table ;
580 const double * pgen ;
581 const double xtrunc = 0.5 * slitw + 5.0 * fwhm * CPL_MATH_SIG_FWHM;
582 const int spec_sz = cpl_vector_get_size(spectrum);
583 const cpl_boolean guess_resamp
584 = !irplib_wlcalib_is_lines(cpl_bivector_get_x_const(lines_catalog),
585 guess_poly, spec_sz, 1.0);
586 const cpl_boolean corr_resamp
587 = !irplib_wlcalib_is_lines(cpl_bivector_get_x_const(lines_catalog),
588 corr_poly, spec_sz, 1.0);
589 cpl_error_code error;
591 cpl_msg_debug(cpl_func,
"Table for guess dispersion polynomial (slitw=%g, "
592 "fwhm=%g) with %d-point observed spectrum with%s catalog re"
593 "sampling", slitw, fwhm, spec_sz, guess_resamp ?
"out" :
"");
594 cpl_msg_debug(cpl_func,
"Table for corr. dispersion polynomial (slitw=%g, "
595 "fwhm=%g) with %d-point observed spectrum with%s catalog re"
596 "sampling", slitw, fwhm, spec_sz, corr_resamp ?
"out" :
"");
599 cpl_ensure(spectrum, CPL_ERROR_NULL_INPUT, NULL) ;
600 cpl_ensure(lines_catalog, CPL_ERROR_NULL_INPUT, NULL) ;
601 cpl_ensure(guess_poly, CPL_ERROR_NULL_INPUT, NULL) ;
602 cpl_ensure(corr_poly, CPL_ERROR_NULL_INPUT, NULL) ;
605 if (guess_resamp || corr_resamp) {
606 conv_kernel = irplib_wlxcorr_convolve_create_kernel(slitw, fwhm);
608 if (conv_kernel == NULL) {
609 cpl_error_set_message_macro(cpl_func, CPL_ERROR_ILLEGAL_INPUT,
610 __FILE__, __LINE__,
"Cannot create "
611 "convolution kernel") ;
617 gen_init = cpl_bivector_new(spec_sz);
619 error = irplib_wlcalib_fill_spectrum(cpl_bivector_get_y(gen_init),
620 lines_catalog, conv_kernel,
624 (cpl_bivector_get_y(gen_init), NULL, NULL,
625 guess_poly, lines_catalog,
626 slitw, fwhm, xtrunc, 0, CPL_FALSE, CPL_FALSE, NULL);
629 if (error || cpl_vector_fill_polynomial(cpl_bivector_get_x(gen_init),
631 cpl_vector_delete(conv_kernel);
632 cpl_bivector_delete(gen_init);
633 cpl_error_set_message_macro(cpl_func, CPL_ERROR_ILLEGAL_INPUT,
634 __FILE__, __LINE__,
"Cannot get the "
635 "emission spectrum");
640 gen_corr = cpl_bivector_new(spec_sz);
642 error = irplib_wlcalib_fill_spectrum(cpl_bivector_get_y(gen_corr),
643 lines_catalog, conv_kernel,
647 (cpl_bivector_get_y(gen_corr), NULL, NULL,
648 corr_poly, lines_catalog,
649 slitw, fwhm, xtrunc, 0, CPL_FALSE, CPL_FALSE, NULL);
652 if (error || cpl_vector_fill_polynomial(cpl_bivector_get_x(gen_corr),
654 cpl_vector_delete(conv_kernel);
655 cpl_bivector_delete(gen_init);
656 cpl_bivector_delete(gen_corr) ;
657 cpl_error_set_message_macro(cpl_func, CPL_ERROR_ILLEGAL_INPUT,
658 __FILE__, __LINE__,
"Cannot get the "
659 "emission spectrum");
662 cpl_vector_delete(conv_kernel) ;
665 spc_table = cpl_table_new(spec_sz);
666 cpl_table_new_column(spc_table, IRPLIB_WLXCORR_COL_WAVELENGTH,
668 cpl_table_new_column(spc_table, IRPLIB_WLXCORR_COL_CAT_INIT,
670 cpl_table_new_column(spc_table, IRPLIB_WLXCORR_COL_CAT_FINAL,
672 cpl_table_new_column(spc_table, IRPLIB_WLXCORR_COL_OBS, CPL_TYPE_DOUBLE);
675 pgen = cpl_bivector_get_x_data_const(gen_corr) ;
676 cpl_table_copy_data_double(spc_table, IRPLIB_WLXCORR_COL_WAVELENGTH, pgen) ;
677 pgen = cpl_bivector_get_y_data_const(gen_corr) ;
678 cpl_table_copy_data_double(spc_table, IRPLIB_WLXCORR_COL_CAT_FINAL, pgen) ;
679 pgen = cpl_vector_get_data_const(spectrum) ;
680 cpl_table_copy_data_double(spc_table, IRPLIB_WLXCORR_COL_OBS, pgen) ;
681 pgen = cpl_bivector_get_y_data_const(gen_init) ;
682 cpl_table_copy_data_double(spc_table, IRPLIB_WLXCORR_COL_CAT_INIT, pgen);
683 cpl_bivector_delete(gen_init);
684 cpl_bivector_delete(gen_corr);
702cpl_bivector * irplib_wlxcorr_cat_extract(
703 const cpl_bivector * lines_catalog,
707 const int nlines = cpl_bivector_get_size(lines_catalog);
708 int wave_min_id, wave_max_id ;
709 cpl_vector * sub_cat_wl ;
710 cpl_vector * sub_cat_int ;
711 const cpl_vector * xlines = cpl_bivector_get_x_const(lines_catalog);
712 const double * dxlines = cpl_vector_get_data_const(xlines);
714 cpl_ensure(lines_catalog != NULL, CPL_ERROR_NULL_INPUT, NULL);
717 wave_min_id = (int)cpl_vector_find(xlines, wave_min);
718 if (wave_min_id < 0) {
719 cpl_error_set_message_macro(cpl_func, CPL_ERROR_ILLEGAL_INPUT,
721 "The starting wavelength cannot be found") ;
726 if (dxlines[wave_min_id] <= wave_min) wave_min_id++;
729 wave_max_id = (int)cpl_vector_find(xlines, wave_max);
730 if (wave_max_id < 0) {
731 cpl_error_set_message_macro(cpl_func, CPL_ERROR_ILLEGAL_INPUT,
733 "The ending wavelength cannot be found") ;
737 if (dxlines[wave_max_id] >= wave_max) wave_max_id--;
741 cpl_ensure(wave_min_id <= wave_max_id, CPL_ERROR_ILLEGAL_INPUT, NULL);
743 if (wave_min_id < 0 || wave_max_id == nlines) {
744 cpl_error_set_message_macro(cpl_func, CPL_ERROR_ILLEGAL_INPUT,
745 __FILE__, __LINE__,
"The %d-line catalogue "
746 "has no lines in the range %g -> %g",
747 nlines, wave_min, wave_max);
751 sub_cat_wl = cpl_vector_extract(xlines, wave_min_id, wave_max_id, 1);
752 sub_cat_int = cpl_vector_extract(cpl_bivector_get_y_const(lines_catalog),
753 wave_min_id, wave_max_id, 1);
755 return cpl_bivector_wrap_vectors(sub_cat_wl, sub_cat_int);
776cpl_vector * irplib_wlxcorr_convolve_create_kernel(
double slitw,
779 const double sigma = fwhm * CPL_MATH_SIG_FWHM;
780 const int size = 1 + (int)(5.0 * sigma + 0.5*slitw);
781 cpl_vector * kernel = cpl_vector_new(size);
784 if (cpl_vector_fill_lss_profile_symmetric(kernel, slitw, fwhm)) {
785 cpl_vector_delete(kernel);
787 (void)cpl_error_set_where(cpl_func);
807int irplib_wlxcorr_convolve(
808 cpl_vector * smoothed,
809 const cpl_vector * conv_kernel)
816 const double* psymm ;
820 cpl_ensure(smoothed, CPL_ERROR_NULL_INPUT, -1) ;
821 cpl_ensure(conv_kernel, CPL_ERROR_NULL_INPUT, -1) ;
824 nsamples = cpl_vector_get_size(smoothed) ;
825 ihwidth = cpl_vector_get_size(conv_kernel) - 1 ;
826 cpl_ensure(ihwidth<nsamples, CPL_ERROR_ILLEGAL_INPUT, -1) ;
827 psymm = cpl_vector_get_data_const(conv_kernel) ;
828 psmoothe = cpl_vector_get_data(smoothed) ;
831 raw = cpl_vector_duplicate(smoothed) ;
832 praw = cpl_vector_get_data(raw) ;
835 for (i=0 ; i<ihwidth ; i++) {
836 psmoothe[i] = praw[i] * psymm[0];
837 for (j=1 ; j <= ihwidth ; j++) {
838 const int k = i-j < 0 ? 0 : i-j;
839 psmoothe[i] += (praw[k]+praw[i+j]) * psymm[j];
843 for (i=ihwidth ; i<nsamples-ihwidth ; i++) {
844 psmoothe[i] = praw[i] * psymm[0];
845 for (j=1 ; j<=ihwidth ; j++)
846 psmoothe[i] += (praw[i-j]+praw[i+j]) * psymm[j];
848 for (i=nsamples-ihwidth ; i<nsamples ; i++) {
849 psmoothe[i] = praw[i] * psymm[0];
850 for (j=1 ; j<=ihwidth ; j++) {
851 const int k = i+j > nsamples-1 ? nsamples - 1 : i+j;
852 psmoothe[i] += (praw[k]+praw[i-j]) * psymm[j];
855 cpl_vector_delete(raw) ;
870int irplib_wlxcorr_plot_solution(
871 const cpl_polynomial * init,
872 const cpl_polynomial * comp,
873 const cpl_polynomial * sol,
877 int nsamples, nplots ;
878 cpl_vector ** vectors ;
882 if (init == NULL || comp == NULL)
return -1 ;
885 nsamples = pix_stop - pix_start + 1 ;
886 if (sol != NULL) nplots = 3 ;
890 vectors = cpl_malloc((nplots+1)*
sizeof(cpl_vector*)) ;
891 for (i=0 ; i<nplots+1 ; i++) vectors[i] = cpl_vector_new(nsamples) ;
895 for (i=0 ; i<nsamples ; i++) {
896 cpl_vector_set(vectors[0], i, pix_start+i) ;
897 cpl_vector_set(vectors[1], i,
898 cpl_polynomial_eval_1d(init, (
double)(pix_start+i), NULL)) ;
899 cpl_vector_set(vectors[2], i,
900 cpl_polynomial_eval_1d(comp, (
double)(pix_start+i), NULL)) ;
902 cpl_vector_set(vectors[3], i,
903 cpl_polynomial_eval_1d(sol, (
double)(pix_start+i), NULL)) ;
907 cpl_plot_vectors(
"set grid;set xlabel 'Position (pixels)';",
908 "t '1-Initial / 2-Computed / 3-Solution' w lines",
909 "", (
const cpl_vector **)vectors, nplots+1);
912 for (i=0 ; i<nplots+1 ; i++) cpl_vector_delete(vectors[i]) ;
917 vectors = cpl_malloc((nplots+1)*
sizeof(cpl_vector*)) ;
918 for (i=0 ; i<nplots+1 ; i++) vectors[i] = cpl_vector_new(nsamples) ;
922 for (i=0 ; i<nsamples ; i++) {
924 cpl_vector_set(vectors[0], i, pix_start+i) ;
925 diff = cpl_polynomial_eval_1d(comp, (
double)(pix_start+i), NULL) -
926 cpl_polynomial_eval_1d(init, (
double)(pix_start+i), NULL) ;
927 cpl_vector_set(vectors[1], i, diff) ;
929 diff = cpl_polynomial_eval_1d(sol, (
double)(pix_start+i), NULL) -
930 cpl_polynomial_eval_1d(init, (
double)(pix_start+i), NULL) ;
931 cpl_vector_set(vectors[2], i, diff) ;
937 cpl_bivector * bivector ;
938 bivector = cpl_bivector_wrap_vectors(vectors[0], vectors[1]) ;
940"set grid;set xlabel 'Position (pixels)';set ylabel 'Wavelength difference';",
941 "t 'Computed-Initial wavelenth' w lines",
"", bivector);
942 cpl_bivector_unwrap_vectors(bivector) ;
944 cpl_plot_vectors(
"set grid;set xlabel 'Position (pixels)';",
945 "t '1-Computed - Initial / 2--Solution - Initial' w lines",
946 "", (
const cpl_vector **)vectors, nplots+1);
950 for (i=0 ; i<nplots+1 ; i++) cpl_vector_delete(vectors[i]) ;
969int irplib_wlxcorr_plot_spc_table(
970 const cpl_table * spc_table,
972 int first_plotted_line,
973 int last_plotted_line)
975 char title_loc[1024] ;
976 cpl_vector ** vectors ;
977 cpl_vector ** sub_vectors ;
978 cpl_vector * tmp_vec ;
980 double mean1, mean3 ;
981 int start_ind, stop_ind, hsize_pix ;
985 if (first_plotted_line > last_plotted_line)
return -1 ;
986 if (spc_table == NULL)
return -1 ;
989 nsamples = cpl_table_get_nrow(spc_table) ;
993 "t '%s - 1-Initial catalog/2-Corrected catalog/3-Observed' w lines",
995 title_loc[1023] = (char)0 ;
997 vectors = cpl_malloc(4*
sizeof(cpl_vector*)) ;
998 vectors[0] = cpl_vector_wrap(nsamples,
999 cpl_table_get_data_double((cpl_table*)spc_table,
1000 IRPLIB_WLXCORR_COL_WAVELENGTH));
1001 vectors[1] = cpl_vector_wrap(nsamples,
1002 cpl_table_get_data_double((cpl_table*)spc_table,
1003 IRPLIB_WLXCORR_COL_CAT_INIT));
1004 vectors[2] = cpl_vector_wrap(nsamples,
1005 cpl_table_get_data_double((cpl_table*)spc_table,
1006 IRPLIB_WLXCORR_COL_CAT_FINAL));
1007 vectors[3] = cpl_vector_wrap(nsamples,
1008 cpl_table_get_data_double((cpl_table*)spc_table,
1009 IRPLIB_WLXCORR_COL_OBS)) ;
1012 mean1 = cpl_vector_get_mean(vectors[1]) ;
1013 mean3 = cpl_vector_get_mean(vectors[3]) ;
1014 if (fabs(mean3) > 1)
1015 cpl_vector_multiply_scalar(vectors[3], fabs(mean1/mean3)) ;
1017 cpl_plot_vectors(
"set grid;set xlabel 'Wavelength (nm)';", title_loc,
1018 "", (
const cpl_vector **)vectors, 4);
1021 if (fabs(mean3) > 1)
1022 cpl_vector_multiply_scalar(vectors[3], mean3/mean1) ;
1026"t '%s - 1-Initial catalog/2-Corrected catalog/3-Observed (ZOOMED)' w lines",
1028 title_loc[1023] = (char)0 ;
1029 tmp_vec = cpl_vector_duplicate(vectors[2]) ;
1030 for (i=0 ; i<last_plotted_line ; i++) {
1033 if ((max = cpl_vector_get_max(tmp_vec)) <= 0.0) break ;
1034 for (j=0 ; j<nsamples ; j++) {
1035 if (cpl_vector_get(tmp_vec, j) == max) break ;
1037 if (j-hsize_pix < 0) start_ind = 0 ;
1038 else start_ind = j-hsize_pix ;
1039 if (j+hsize_pix > nsamples-1) stop_ind = nsamples-1 ;
1040 else stop_ind = j+hsize_pix ;
1041 for (j=start_ind ; j<=stop_ind ; j++) cpl_vector_set(tmp_vec, j, 0.0) ;
1043 if (i+1 >= first_plotted_line) {
1044 sub_vectors = cpl_malloc(4*
sizeof(cpl_vector*)) ;
1045 sub_vectors[0]=cpl_vector_extract(vectors[0],start_ind,stop_ind,1);
1046 sub_vectors[1]=cpl_vector_extract(vectors[1],start_ind,stop_ind,1);
1047 sub_vectors[2]=cpl_vector_extract(vectors[2],start_ind,stop_ind,1);
1048 sub_vectors[3]=cpl_vector_extract(vectors[3],start_ind,stop_ind,1);
1050 cpl_plot_vectors(
"set grid;set xlabel 'Wavelength (nm)';",
1051 title_loc,
"", (
const cpl_vector **)sub_vectors, 4);
1053 cpl_vector_delete(sub_vectors[0]) ;
1054 cpl_vector_delete(sub_vectors[1]) ;
1055 cpl_vector_delete(sub_vectors[2]) ;
1056 cpl_vector_delete(sub_vectors[3]) ;
1057 cpl_free(sub_vectors) ;
1060 cpl_vector_delete(tmp_vec) ;
1062 cpl_vector_unwrap(vectors[0]) ;
1063 cpl_vector_unwrap(vectors[1]) ;
1064 cpl_vector_unwrap(vectors[2]) ;
1065 cpl_vector_unwrap(vectors[3]) ;
1080int irplib_wlxcorr_catalog_plot(
1081 const cpl_bivector * cat,
1086 cpl_bivector * subcat ;
1087 cpl_vector * subcat_x ;
1088 cpl_vector * subcat_y ;
1089 const double * pwave ;
1090 int nvals, nvals_tot ;
1094 if (cat == NULL)
return -1 ;
1095 if (wmax <= wmin)
return -1 ;
1098 nvals_tot = cpl_bivector_get_size(cat) ;
1101 pwave = cpl_bivector_get_x_data_const(cat) ;
1102 if (pwave[0] >= wmin) start = 0 ;
1104 if (pwave[nvals_tot-1] <= wmax) stop = nvals_tot-1 ;
1107 while ((i<nvals_tot-1) && (pwave[i] < wmin)) i++ ;
1110 while ((i>0) && (pwave[i] > wmax)) i-- ;
1114 cpl_msg_error(cpl_func,
"Cannot plot the catalog") ;
1117 nvals = stop - start + 1 ;
1120 subcat_x = cpl_vector_extract(cpl_bivector_get_x_const(cat),start,stop, 1) ;
1121 subcat_y = cpl_vector_extract(cpl_bivector_get_y_const(cat),start,stop, 1) ;
1122 subcat = cpl_bivector_wrap_vectors(subcat_x, subcat_y) ;
1127 "set grid;set xlabel 'Wavelength (nm)';set ylabel 'Emission';",
1128 "t 'Catalog Spectrum' w lines",
"", subcat);
1131 "set grid;set xlabel 'Wavelength (nm)';set ylabel 'Emission';",
1132 "t 'Catalog Spectrum' w impulses",
"", subcat);
1134 cpl_bivector_unwrap_vectors(subcat) ;
1135 cpl_vector_delete(subcat_x) ;
1136 cpl_vector_delete(subcat_y) ;
1159static void irplib_wlxcorr_estimate(cpl_vector * vxc,
1161 const cpl_vector * spectrum,
1162 const cpl_bivector * lines_catalog,
1163 const cpl_vector * conv_kernel,
1164 const cpl_polynomial * poly_candi,
1168 cpl_errorstate prestate = cpl_errorstate_get();
1169 const int hsize = cpl_vector_get_size(vxc) / 2;
1171 if (conv_kernel != NULL) {
1172 irplib_wlcalib_fill_spectrum(model, lines_catalog, conv_kernel,
1175 const double xtrunc = 0.5 * slitw + 5.0 * fwhm * CPL_MATH_SIG_FWHM;
1178 lines_catalog, slitw, fwhm,
1179 xtrunc, 0, CPL_FALSE, CPL_FALSE,
1183 if (cpl_errorstate_is_equal(prestate))
1184 cpl_vector_correlate(vxc, model, spectrum);
1186 if (!cpl_errorstate_is_equal(prestate)) {
1187 cpl_vector_fill(vxc, 0.0);
1190 cpl_errorstate_set(prestate);
1209static cpl_boolean irplib_wlcalib_is_lines(
const cpl_vector * wavelengths,
1210 const cpl_polynomial * disp1d,
1214 const int nlines = cpl_vector_get_size(wavelengths);
1216 const double dispersion = cpl_polynomial_eval_1d_diff(disp1d,
1217 0.5 * spec_sz + 1.0,
1220 const double range = cpl_vector_get(wavelengths, nlines-1)
1221 - cpl_vector_get(wavelengths, 0);
1223 cpl_ensure(wavelengths != NULL, CPL_ERROR_NULL_INPUT, CPL_FALSE);
1224 cpl_ensure(disp1d != NULL, CPL_ERROR_NULL_INPUT, CPL_FALSE);
1225 cpl_ensure(cpl_polynomial_get_dimension(disp1d) == 1,
1226 CPL_ERROR_ILLEGAL_INPUT, CPL_FALSE);
1227 cpl_ensure(range > 0.0, CPL_ERROR_ILLEGAL_INPUT, CPL_FALSE);
1229 return nlines * fabs(dispersion) <= tol * fabs(range) ? CPL_TRUE
1251cpl_error_code irplib_wlcalib_fill_spectrum(cpl_vector * self,
1252 const cpl_bivector * lines_catalog,
1253 const cpl_vector * conv_kernel,
1254 const cpl_polynomial * poly,
1259 const int size = cpl_vector_get_size(self);
1260 const int nlines = cpl_bivector_get_size(lines_catalog);
1261 const cpl_vector * xlines = cpl_bivector_get_x_const(lines_catalog);
1262 const double * dxlines = cpl_vector_get_data_const(xlines);
1263 cpl_bivector * sub_cat ;
1264 cpl_vector * sub_cat_x;
1265 cpl_vector * sub_cat_y;
1266 cpl_vector * wl_limits;
1267 double wave_min, wave_max;
1268 int wave_min_id, wave_max_id;
1272 cpl_ensure_code(self != NULL, CPL_ERROR_NULL_INPUT);
1273 cpl_ensure_code(lines_catalog != NULL, CPL_ERROR_NULL_INPUT);
1274 cpl_ensure_code(conv_kernel != NULL, CPL_ERROR_NULL_INPUT);
1275 cpl_ensure_code(poly != NULL, CPL_ERROR_NULL_INPUT);
1276 cpl_ensure_code(size > 0, CPL_ERROR_ILLEGAL_INPUT);
1280 wl_limits = cpl_vector_new(size + 1);
1281 cpl_vector_fill_polynomial(wl_limits, poly, 0.5 - search_hs, 1);
1284 wave_min = cpl_vector_get(wl_limits, 0);
1285 wave_max = cpl_vector_get(wl_limits, size);
1288 wave_min_id = cpl_vector_find(xlines, wave_min);
1290 if (dxlines[wave_min_id] > wave_min) wave_min_id--;
1292 if (wave_min_id < 0) {
1293 cpl_vector_delete(wl_limits);
1294 return cpl_error_set_message_macro(cpl_func, CPL_ERROR_ILLEGAL_INPUT,
1295 __FILE__, __LINE__,
"The %d-line "
1296 "catalogue only has lines above %g",
1301 wave_max_id = cpl_vector_find(xlines, wave_max);
1303 if (dxlines[wave_max_id] < wave_max) wave_max_id++;
1305 if (wave_max_id == nlines) {
1306 cpl_vector_delete(wl_limits);
1307 return cpl_error_set_message_macro(cpl_func, CPL_ERROR_ILLEGAL_INPUT,
1308 __FILE__, __LINE__,
"The %d-line "
1309 "catalogue only has lines below %g",
1315 nsub = 1 + wave_max_id - wave_min_id;
1316 cpl_ensure_code(nsub > 1, CPL_ERROR_ILLEGAL_INPUT);
1320 sub_cat_x = cpl_vector_wrap(nsub, wave_min_id + (
double*)dxlines);
1321 sub_cat_y = cpl_vector_wrap(nsub, wave_min_id + (
double*)
1322 cpl_bivector_get_y_data_const(lines_catalog));
1323 sub_cat = cpl_bivector_wrap_vectors(sub_cat_x, sub_cat_y);
1326 error = irplib_wlxcorr_signal_resample(self, wl_limits, sub_cat);
1328 cpl_vector_delete(wl_limits);
1329 cpl_bivector_unwrap_vectors(sub_cat);
1330 (void)cpl_vector_unwrap(sub_cat_x);
1331 (void)cpl_vector_unwrap(sub_cat_y);
1333 cpl_ensure_code(!error, CPL_ERROR_ILLEGAL_INPUT);
1336 cpl_ensure_code(!irplib_wlxcorr_convolve(self, conv_kernel),
1337 cpl_error_get_code());
1339 return CPL_ERROR_NONE;
1354static int irplib_wlxcorr_signal_resample(
1355 cpl_vector * resampled,
1356 const cpl_vector * xbounds,
1357 const cpl_bivector * hires)
1359 const int hrsize = cpl_bivector_get_size(hires);
1360 const cpl_vector* xhires ;
1361 const cpl_vector* yhires ;
1362 const double * pxhires ;
1363 const double * pyhires ;
1364 const double * pxbounds ;
1365 cpl_vector * ybounds ;
1366 cpl_bivector * boundary ;
1368 double * presampled ;
1373 if ((!resampled) || (!xbounds) || (!hires))
return -1 ;
1376 nsamples = cpl_vector_get_size(resampled) ;
1379 presampled = cpl_vector_get_data(resampled) ;
1380 pxbounds = cpl_vector_get_data_const(xbounds) ;
1381 xhires = cpl_bivector_get_x_const(hires) ;
1382 yhires = cpl_bivector_get_y_const(hires) ;
1383 pxhires = cpl_vector_get_data_const(xhires) ;
1384 pyhires = cpl_vector_get_data_const(yhires) ;
1387 ybounds = cpl_vector_new(cpl_vector_get_size(xbounds)) ;
1388 boundary = cpl_bivector_wrap_vectors((cpl_vector*)xbounds,ybounds) ;
1389 pybounds = cpl_vector_get_data(ybounds) ;
1392 if (cpl_bivector_get_size(boundary) != nsamples + 1) {
1393 cpl_bivector_unwrap_vectors(boundary) ;
1394 cpl_vector_delete(ybounds) ;
1399 itt = cpl_vector_find(xhires, pxbounds[0]);
1402 if (cpl_bivector_interpolate_linear(boundary, hires)) {
1403 cpl_bivector_unwrap_vectors(boundary) ;
1404 cpl_vector_delete(ybounds) ;
1410 while (pxhires[itt] < pxbounds[0]) itt++;
1412 for (i=0; i < nsamples; i++) {
1417 double xlow = pxbounds[i];
1418 double x = pxhires[itt];
1420 if (x > pxbounds[i+1]) x = pxbounds[i+1];
1423 presampled[i] = pybounds[i] * (x - xlow);
1426 while ((pxhires[itt] < pxbounds[i+1]) && (itt < hrsize)) {
1427 const double xprev = x;
1429 if (x > pxbounds[i+1]) x = pxbounds[i+1];
1430 presampled[i] += pyhires[itt] * (x - xlow);
1437 presampled[i] += pybounds[i+1] * (pxbounds[i+1] - xlow);
1441 presampled[i] /= 2 * (pxbounds[i+1] - pxbounds[i]);
1443 cpl_bivector_unwrap_vectors(boundary) ;
1444 cpl_vector_delete(ybounds) ;
1472static cpl_error_code cpl_vector_fill_lss_profile_symmetric(cpl_vector * self,
1477 const double sigma = fwhm * CPL_MATH_SIG_FWHM;
1478 const int n = cpl_vector_get_size(self);
1482 cpl_ensure_code(self != NULL, CPL_ERROR_NULL_INPUT);
1483 cpl_ensure_code(slitw > 0.0, CPL_ERROR_ILLEGAL_INPUT);
1484 cpl_ensure_code(fwhm > 0.0, CPL_ERROR_ILLEGAL_INPUT);
1489 (void)cpl_vector_set(self, 0,
1493 for (i = 1; i < n; i++) {
1495 const double x1p = i + 0.5*slitw + 0.5;
1496 const double x1n = i - 0.5*slitw + 0.5;
1497 const double x0p = i + 0.5*slitw - 0.5;
1498 const double x0n = i - 0.5*slitw - 0.5;
1499 const double val = 0.5/slitw *
1502 (void)cpl_vector_set(self, i, val);
1505 return CPL_ERROR_NONE;
double irplib_erf_antideriv(double x, double sigma)
The antiderivative of erx(x/sigma/sqrt(2)) with respect to x.
cpl_error_code irplib_vector_fill_line_spectrum_model(cpl_vector *self, cpl_vector *linepix, cpl_vector *erftmp, const cpl_polynomial *disp, const cpl_bivector *lines, double wslit, double wfwhm, double xtrunc, int hsize, cpl_boolean dofast, cpl_boolean dolog, cpl_size *pulines)
Generate a 1D spectrum from (arc) lines and a dispersion relation.