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)
67 static void irplib_wlxcorr_estimate(cpl_vector *, cpl_vector *,
71 const cpl_polynomial *,
74 static int irplib_wlxcorr_signal_resample(cpl_vector *,
const cpl_vector *,
75 const cpl_bivector *) ;
76 static cpl_error_code cpl_vector_fill_lss_profile_symmetric(cpl_vector *,
78 static cpl_error_code irplib_wlcalib_fill_spectrum(cpl_vector *,
81 const cpl_polynomial *,
int);
83 static cpl_boolean irplib_wlcalib_is_lines(
const cpl_vector *,
84 const cpl_polynomial *,
127 cpl_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) {
331 cpl_table * irplib_wlxcorr_gen_spc_table(
332 const cpl_vector * spectrum,
333 const cpl_bivector * lines_catalog,
336 const cpl_polynomial * guess_poly,
337 const cpl_polynomial * corr_poly)
340 cpl_vector * conv_kernel = NULL;
341 cpl_bivector * gen_init ;
342 cpl_bivector * gen_corr ;
343 cpl_table * spc_table ;
344 const double * pgen ;
345 const double xtrunc = 0.5 * slitw + 5.0 * fwhm * CPL_MATH_SIG_FWHM;
346 const int spec_sz = cpl_vector_get_size(spectrum);
347 const cpl_boolean guess_resamp
348 = !irplib_wlcalib_is_lines(cpl_bivector_get_x_const(lines_catalog),
349 guess_poly, spec_sz, 1.0);
350 const cpl_boolean corr_resamp
351 = !irplib_wlcalib_is_lines(cpl_bivector_get_x_const(lines_catalog),
352 corr_poly, spec_sz, 1.0);
353 cpl_error_code error;
355 cpl_msg_debug(cpl_func,
"Tabel for guess dispersion polynomial (slitw=%g, "
356 "fwhm=%g) with %d-point observed spectrum with%s catalog re"
357 "sampling", slitw, fwhm, spec_sz, guess_resamp ?
"out" :
"");
358 cpl_msg_debug(cpl_func,
"Tabel for corr. dispersion polynomial (slitw=%g, "
359 "fwhm=%g) with %d-point observed spectrum with%s catalog re"
360 "sampling", slitw, fwhm, spec_sz, corr_resamp ?
"out" :
"");
363 cpl_ensure(spectrum, CPL_ERROR_NULL_INPUT, NULL) ;
364 cpl_ensure(lines_catalog, CPL_ERROR_NULL_INPUT, NULL) ;
365 cpl_ensure(guess_poly, CPL_ERROR_NULL_INPUT, NULL) ;
366 cpl_ensure(corr_poly, CPL_ERROR_NULL_INPUT, NULL) ;
369 if (guess_resamp || corr_resamp) {
370 conv_kernel = irplib_wlxcorr_convolve_create_kernel(slitw, fwhm);
372 if (conv_kernel == NULL) {
373 cpl_error_set_message_macro(cpl_func, CPL_ERROR_ILLEGAL_INPUT,
374 __FILE__, __LINE__,
"Cannot create "
375 "convolution kernel") ;
381 gen_init = cpl_bivector_new(spec_sz);
383 error = irplib_wlcalib_fill_spectrum(cpl_bivector_get_y(gen_init),
384 lines_catalog, conv_kernel,
388 (cpl_bivector_get_y(gen_init), NULL, NULL,
389 guess_poly, lines_catalog,
390 slitw, fwhm, xtrunc, 0, CPL_FALSE, CPL_FALSE, NULL);
393 if (error || cpl_vector_fill_polynomial(cpl_bivector_get_x(gen_init),
395 cpl_vector_delete(conv_kernel);
396 cpl_bivector_delete(gen_init);
397 cpl_error_set_message_macro(cpl_func, CPL_ERROR_ILLEGAL_INPUT,
398 __FILE__, __LINE__,
"Cannot get the "
399 "emission spectrum");
404 gen_corr = cpl_bivector_new(spec_sz);
406 error = irplib_wlcalib_fill_spectrum(cpl_bivector_get_y(gen_corr),
407 lines_catalog, conv_kernel,
411 (cpl_bivector_get_y(gen_corr), NULL, NULL,
412 corr_poly, lines_catalog,
413 slitw, fwhm, xtrunc, 0, CPL_FALSE, CPL_FALSE, NULL);
416 if (error || cpl_vector_fill_polynomial(cpl_bivector_get_x(gen_corr),
418 cpl_vector_delete(conv_kernel);
419 cpl_bivector_delete(gen_init);
420 cpl_bivector_delete(gen_corr) ;
421 cpl_error_set_message_macro(cpl_func, CPL_ERROR_ILLEGAL_INPUT,
422 __FILE__, __LINE__,
"Cannot get the "
423 "emission spectrum");
426 cpl_vector_delete(conv_kernel) ;
429 spc_table = cpl_table_new(spec_sz);
430 cpl_table_new_column(spc_table, IRPLIB_WLXCORR_COL_WAVELENGTH,
432 cpl_table_new_column(spc_table, IRPLIB_WLXCORR_COL_CAT_INIT,
434 cpl_table_new_column(spc_table, IRPLIB_WLXCORR_COL_CAT_FINAL,
436 cpl_table_new_column(spc_table, IRPLIB_WLXCORR_COL_OBS, CPL_TYPE_DOUBLE);
439 pgen = cpl_bivector_get_x_data_const(gen_corr) ;
440 cpl_table_copy_data_double(spc_table, IRPLIB_WLXCORR_COL_WAVELENGTH, pgen) ;
441 pgen = cpl_bivector_get_y_data_const(gen_corr) ;
442 cpl_table_copy_data_double(spc_table, IRPLIB_WLXCORR_COL_CAT_FINAL, pgen) ;
443 pgen = cpl_vector_get_data_const(spectrum) ;
444 cpl_table_copy_data_double(spc_table, IRPLIB_WLXCORR_COL_OBS, pgen) ;
445 pgen = cpl_bivector_get_y_data_const(gen_init) ;
446 cpl_table_copy_data_double(spc_table, IRPLIB_WLXCORR_COL_CAT_INIT, pgen);
447 cpl_bivector_delete(gen_init);
448 cpl_bivector_delete(gen_corr);
466 cpl_bivector * irplib_wlxcorr_cat_extract(
467 const cpl_bivector * lines_catalog,
471 const int nlines = cpl_bivector_get_size(lines_catalog);
472 int wave_min_id, wave_max_id ;
473 cpl_vector * sub_cat_wl ;
474 cpl_vector * sub_cat_int ;
475 const cpl_vector * xlines = cpl_bivector_get_x_const(lines_catalog);
476 const double * dxlines = cpl_vector_get_data_const(xlines);
478 cpl_ensure(lines_catalog != NULL, CPL_ERROR_NULL_INPUT, NULL);
481 wave_min_id = (int)cpl_vector_find(xlines, wave_min);
482 if (wave_min_id < 0) {
483 cpl_error_set_message_macro(cpl_func, CPL_ERROR_ILLEGAL_INPUT,
485 "The starting wavelength cannot be found") ;
490 if (dxlines[wave_min_id] <= wave_min) wave_min_id++;
493 wave_max_id = (int)cpl_vector_find(xlines, wave_max);
494 if (wave_max_id < 0) {
495 cpl_error_set_message_macro(cpl_func, CPL_ERROR_ILLEGAL_INPUT,
497 "The ending wavelength cannot be found") ;
501 if (dxlines[wave_max_id] >= wave_min) wave_max_id--;
505 cpl_ensure(wave_min_id <= wave_max_id, CPL_ERROR_ILLEGAL_INPUT, NULL);
507 if (wave_min_id < 0 || wave_max_id == nlines) {
508 cpl_error_set_message_macro(cpl_func, CPL_ERROR_ILLEGAL_INPUT,
509 __FILE__, __LINE__,
"The %d-line catalogue "
510 "has no lines in the range %g -> %g",
511 nlines, wave_min, wave_max);
515 sub_cat_wl = cpl_vector_extract(xlines, wave_min_id, wave_max_id, 1);
516 sub_cat_int = cpl_vector_extract(cpl_bivector_get_y_const(lines_catalog),
517 wave_min_id, wave_max_id, 1);
519 return cpl_bivector_wrap_vectors(sub_cat_wl, sub_cat_int);
540 cpl_vector * irplib_wlxcorr_convolve_create_kernel(
double slitw,
543 const double sigma = fwhm * CPL_MATH_SIG_FWHM;
544 const int size = 1 + (int)(5.0 * sigma + 0.5*slitw);
545 cpl_vector * kernel = cpl_vector_new(size);
548 if (cpl_vector_fill_lss_profile_symmetric(kernel, slitw, fwhm)) {
549 cpl_vector_delete(kernel);
550 cpl_ensure(0, cpl_error_get_code(), NULL);
570 int irplib_wlxcorr_convolve(
571 cpl_vector * smoothed,
572 const cpl_vector * conv_kernel)
579 const double* psymm ;
583 cpl_ensure(smoothed, CPL_ERROR_NULL_INPUT, -1) ;
584 cpl_ensure(conv_kernel, CPL_ERROR_NULL_INPUT, -1) ;
587 nsamples = cpl_vector_get_size(smoothed) ;
588 ihwidth = cpl_vector_get_size(conv_kernel) - 1 ;
589 cpl_ensure(ihwidth<nsamples, CPL_ERROR_ILLEGAL_INPUT, -1) ;
590 psymm = cpl_vector_get_data_const(conv_kernel) ;
591 psmoothe = cpl_vector_get_data(smoothed) ;
594 raw = cpl_vector_duplicate(smoothed) ;
595 praw = cpl_vector_get_data(raw) ;
598 for (i=0 ; i<ihwidth ; i++) {
599 psmoothe[i] = praw[i] * psymm[0];
600 for (j=1 ; j <= ihwidth ; j++) {
601 const int k = i-j < 0 ? 0 : i-j;
602 psmoothe[i] += (praw[k]+praw[i+j]) * psymm[j];
606 for (i=ihwidth ; i<nsamples-ihwidth ; i++) {
607 psmoothe[i] = praw[i] * psymm[0];
608 for (j=1 ; j<=ihwidth ; j++)
609 psmoothe[i] += (praw[i-j]+praw[i+j]) * psymm[j];
611 for (i=nsamples-ihwidth ; i<nsamples ; i++) {
612 psmoothe[i] = praw[i] * psymm[0];
613 for (j=1 ; j<=ihwidth ; j++) {
614 const int k = i+j > nsamples-1 ? nsamples - 1 : i+j;
615 psmoothe[i] += (praw[k]+praw[i-j]) * psymm[j];
618 cpl_vector_delete(raw) ;
633 int irplib_wlxcorr_plot_solution(
634 const cpl_polynomial * init,
635 const cpl_polynomial * comp,
636 const cpl_polynomial * sol,
640 int nsamples, nplots ;
641 cpl_vector ** vectors ;
642 cpl_bivector * bivector ;
647 if (init == NULL || comp == NULL)
return -1 ;
650 nsamples = pix_stop - pix_start + 1 ;
651 if (sol != NULL) nplots = 3 ;
655 vectors = cpl_malloc((nplots+1)*
sizeof(cpl_vector*)) ;
656 for (i=0 ; i<nplots+1 ; i++) vectors[i] = cpl_vector_new(nsamples) ;
660 for (i=0 ; i<nsamples ; i++) {
661 cpl_vector_set(vectors[0], i, pix_start+i) ;
662 cpl_vector_set(vectors[1], i,
663 cpl_polynomial_eval_1d(init, (
double)(pix_start+i), NULL)) ;
664 cpl_vector_set(vectors[2], i,
665 cpl_polynomial_eval_1d(comp, (
double)(pix_start+i), NULL)) ;
667 cpl_vector_set(vectors[3], i,
668 cpl_polynomial_eval_1d(sol, (
double)(pix_start+i), NULL)) ;
672 cpl_plot_vectors(
"set grid;set xlabel 'Position (pixels)';",
673 "t '1-Initial / 2-Computed / 3-Solution' w lines",
674 "", (
const cpl_vector **)vectors, nplots+1);
677 for (i=0 ; i<nplots+1 ; i++) cpl_vector_delete(vectors[i]) ;
682 vectors = cpl_malloc((nplots+1)*
sizeof(cpl_vector*)) ;
683 for (i=0 ; i<nplots+1 ; i++) vectors[i] = cpl_vector_new(nsamples) ;
687 for (i=0 ; i<nsamples ; i++) {
688 cpl_vector_set(vectors[0], i, pix_start+i) ;
689 diff = cpl_polynomial_eval_1d(comp, (
double)(pix_start+i), NULL) -
690 cpl_polynomial_eval_1d(init, (
double)(pix_start+i), NULL) ;
691 cpl_vector_set(vectors[1], i, diff) ;
693 diff = cpl_polynomial_eval_1d(sol, (
double)(pix_start+i), NULL) -
694 cpl_polynomial_eval_1d(init, (
double)(pix_start+i), NULL) ;
695 cpl_vector_set(vectors[2], i, diff) ;
701 bivector = cpl_bivector_wrap_vectors(vectors[0], vectors[1]) ;
703 "set grid;set xlabel 'Position (pixels)';set ylabel 'Wavelength difference';",
704 "t 'Computed-Initial wavelenth' w lines",
"", bivector);
705 cpl_bivector_unwrap_vectors(bivector) ;
707 cpl_plot_vectors(
"set grid;set xlabel 'Position (pixels)';",
708 "t '1-Computed - Initial / 2--Solution - Initial' w lines",
709 "", (
const cpl_vector **)vectors, nplots+1);
713 for (i=0 ; i<nplots+1 ; i++) cpl_vector_delete(vectors[i]) ;
732 int irplib_wlxcorr_plot_spc_table(
733 const cpl_table * spc_table,
735 int first_plotted_line,
736 int last_plotted_line)
738 char title_loc[1024] ;
739 cpl_vector ** vectors ;
740 cpl_vector ** sub_vectors ;
741 cpl_vector * tmp_vec ;
743 double max, mean1, mean3 ;
744 int start_ind, stop_ind, hsize_pix ;
748 if (first_plotted_line > last_plotted_line)
return -1 ;
749 if (spc_table == NULL)
return -1 ;
752 nsamples = cpl_table_get_nrow(spc_table) ;
756 "t '%s - 1-Initial catalog/2-Corrected catalog/3-Observed' w lines",
758 title_loc[1023] = (char)0 ;
760 vectors = cpl_malloc(4*
sizeof(cpl_vector*)) ;
761 vectors[0] = cpl_vector_wrap(nsamples,
762 cpl_table_get_data_double((cpl_table*)spc_table,
763 IRPLIB_WLXCORR_COL_WAVELENGTH));
764 vectors[1] = cpl_vector_wrap(nsamples,
765 cpl_table_get_data_double((cpl_table*)spc_table,
766 IRPLIB_WLXCORR_COL_CAT_INIT));
767 vectors[2] = cpl_vector_wrap(nsamples,
768 cpl_table_get_data_double((cpl_table*)spc_table,
769 IRPLIB_WLXCORR_COL_CAT_FINAL));
770 vectors[3] = cpl_vector_wrap(nsamples,
771 cpl_table_get_data_double((cpl_table*)spc_table,
772 IRPLIB_WLXCORR_COL_OBS)) ;
775 mean1 = cpl_vector_get_mean(vectors[1]) ;
776 mean3 = cpl_vector_get_mean(vectors[3]) ;
778 cpl_vector_multiply_scalar(vectors[3], fabs(mean1/mean3)) ;
780 cpl_plot_vectors(
"set grid;set xlabel 'Wavelength (nm)';", title_loc,
781 "", (
const cpl_vector **)vectors, 4);
785 cpl_vector_multiply_scalar(vectors[3], mean3/mean1) ;
789 "t '%s - 1-Initial catalog/2-Corrected catalog/3-Observed (ZOOMED)' w lines",
791 title_loc[1023] = (char)0 ;
792 tmp_vec = cpl_vector_duplicate(vectors[2]) ;
793 for (i=0 ; i<last_plotted_line ; i++) {
795 if ((max = cpl_vector_get_max(tmp_vec)) <= 0.0) break ;
796 for (j=0 ; j<nsamples ; j++) {
797 if (cpl_vector_get(tmp_vec, j) == max) break ;
799 if (j-hsize_pix < 0) start_ind = 0 ;
800 else start_ind = j-hsize_pix ;
801 if (j+hsize_pix > nsamples-1) stop_ind = nsamples-1 ;
802 else stop_ind = j+hsize_pix ;
803 for (j=start_ind ; j<=stop_ind ; j++) cpl_vector_set(tmp_vec, j, 0.0) ;
805 if (i+1 >= first_plotted_line) {
806 sub_vectors = cpl_malloc(4*
sizeof(cpl_vector*)) ;
807 sub_vectors[0]=cpl_vector_extract(vectors[0],start_ind,stop_ind,1);
808 sub_vectors[1]=cpl_vector_extract(vectors[1],start_ind,stop_ind,1);
809 sub_vectors[2]=cpl_vector_extract(vectors[2],start_ind,stop_ind,1);
810 sub_vectors[3]=cpl_vector_extract(vectors[3],start_ind,stop_ind,1);
812 cpl_plot_vectors(
"set grid;set xlabel 'Wavelength (nm)';",
813 title_loc,
"", (
const cpl_vector **)sub_vectors, 4);
815 cpl_vector_delete(sub_vectors[0]) ;
816 cpl_vector_delete(sub_vectors[1]) ;
817 cpl_vector_delete(sub_vectors[2]) ;
818 cpl_vector_delete(sub_vectors[3]) ;
819 cpl_free(sub_vectors) ;
822 cpl_vector_delete(tmp_vec) ;
824 cpl_vector_unwrap(vectors[0]) ;
825 cpl_vector_unwrap(vectors[1]) ;
826 cpl_vector_unwrap(vectors[2]) ;
827 cpl_vector_unwrap(vectors[3]) ;
842 int irplib_wlxcorr_catalog_plot(
843 const cpl_bivector * cat,
848 cpl_bivector * subcat ;
849 cpl_vector * subcat_x ;
850 cpl_vector * subcat_y ;
851 const double * pwave ;
852 int nvals, nvals_tot ;
856 if (cat == NULL)
return -1 ;
857 if (wmax <= wmin)
return -1 ;
860 nvals_tot = cpl_bivector_get_size(cat) ;
863 pwave = cpl_bivector_get_x_data_const(cat) ;
864 if (pwave[0] >= wmin) start = 0 ;
866 if (pwave[nvals_tot-1] <= wmax) stop = nvals_tot-1 ;
869 while ((pwave[i] < wmin) && (i<nvals_tot-1)) i++ ;
872 while ((pwave[i] > wmax) && (i>0)) i-- ;
876 cpl_msg_error(cpl_func,
"Cannot plot the catalog") ;
879 nvals = stop - start + 1 ;
882 subcat_x = cpl_vector_extract(cpl_bivector_get_x_const(cat),start,stop, 1) ;
883 subcat_y = cpl_vector_extract(cpl_bivector_get_y_const(cat),start,stop, 1) ;
884 subcat = cpl_bivector_wrap_vectors(subcat_x, subcat_y) ;
889 "set grid;set xlabel 'Wavelength (nm)';set ylabel 'Emission';",
890 "t 'Catalog Spectrum' w lines",
"", subcat);
893 "set grid;set xlabel 'Wavelength (nm)';set ylabel 'Emission';",
894 "t 'Catalog Spectrum' w impulses",
"", subcat);
896 cpl_bivector_unwrap_vectors(subcat) ;
897 cpl_vector_delete(subcat_x) ;
898 cpl_vector_delete(subcat_y) ;
921 static void irplib_wlxcorr_estimate(cpl_vector * vxc,
923 const cpl_vector * spectrum,
924 const cpl_bivector * lines_catalog,
925 const cpl_vector * conv_kernel,
926 const cpl_polynomial * poly_candi,
930 cpl_errorstate prestate = cpl_errorstate_get();
931 const int hsize = cpl_vector_get_size(vxc) / 2;
933 if (conv_kernel != NULL) {
934 irplib_wlcalib_fill_spectrum(model, lines_catalog, conv_kernel,
937 const double xtrunc = 0.5 * slitw + 5.0 * fwhm * CPL_MATH_SIG_FWHM;
940 lines_catalog, slitw, fwhm,
941 xtrunc, 0, CPL_FALSE, CPL_FALSE,
945 if (cpl_errorstate_is_equal(prestate))
946 cpl_vector_correlate(vxc, model, spectrum);
948 if (!cpl_errorstate_is_equal(prestate)) {
949 cpl_vector_fill(vxc, 0.0);
952 cpl_errorstate_set(prestate);
971 static cpl_boolean irplib_wlcalib_is_lines(
const cpl_vector * wavelengths,
972 const cpl_polynomial * disp1d,
976 const int nlines = cpl_vector_get_size(wavelengths);
978 const double dispersion = cpl_polynomial_eval_1d_diff(disp1d,
982 const double range = cpl_vector_get(wavelengths, nlines-1)
983 - cpl_vector_get(wavelengths, 0);
985 cpl_ensure(wavelengths != NULL, CPL_ERROR_NULL_INPUT, CPL_FALSE);
986 cpl_ensure(disp1d != NULL, CPL_ERROR_NULL_INPUT, CPL_FALSE);
987 cpl_ensure(cpl_polynomial_get_dimension(disp1d) == 1,
988 CPL_ERROR_ILLEGAL_INPUT, CPL_FALSE);
989 cpl_ensure(range > 0.0, CPL_ERROR_ILLEGAL_INPUT, CPL_FALSE);
991 return nlines * fabs(dispersion) <= tol * fabs(range) ? CPL_TRUE
1013 cpl_error_code irplib_wlcalib_fill_spectrum(cpl_vector *
self,
1014 const cpl_bivector * lines_catalog,
1015 const cpl_vector * conv_kernel,
1016 const cpl_polynomial * poly,
1021 const int size = cpl_vector_get_size(
self);
1022 const int nlines = cpl_bivector_get_size(lines_catalog);
1023 const cpl_vector * xlines = cpl_bivector_get_x_const(lines_catalog);
1024 const double * dxlines = cpl_vector_get_data_const(xlines);
1025 cpl_bivector * sub_cat ;
1026 cpl_vector * sub_cat_x;
1027 cpl_vector * sub_cat_y;
1028 cpl_vector * wl_limits;
1029 double wave_min, wave_max;
1030 int wave_min_id, wave_max_id;
1034 cpl_ensure_code(
self != NULL, CPL_ERROR_NULL_INPUT);
1035 cpl_ensure_code(lines_catalog != NULL, CPL_ERROR_NULL_INPUT);
1036 cpl_ensure_code(conv_kernel != NULL, CPL_ERROR_NULL_INPUT);
1037 cpl_ensure_code(poly != NULL, CPL_ERROR_NULL_INPUT);
1038 cpl_ensure_code(size > 0, CPL_ERROR_ILLEGAL_INPUT);
1042 wl_limits = cpl_vector_new(size + 1);
1043 cpl_vector_fill_polynomial(wl_limits, poly, 0.5 - search_hs, 1);
1046 wave_min = cpl_vector_get(wl_limits, 0);
1047 wave_max = cpl_vector_get(wl_limits, size);
1050 wave_min_id = cpl_vector_find(xlines, wave_min);
1052 if (dxlines[wave_min_id] > wave_min) wave_min_id--;
1054 if (wave_min_id < 0) {
1055 cpl_vector_delete(wl_limits);
1056 return cpl_error_set_message_macro(cpl_func, CPL_ERROR_ILLEGAL_INPUT,
1057 __FILE__, __LINE__,
"The %d-line "
1058 "catalogue only has lines above %g",
1063 wave_max_id = cpl_vector_find(xlines, wave_max);
1065 if (dxlines[wave_max_id] < wave_max) wave_max_id++;
1067 if (wave_max_id == nlines) {
1068 cpl_vector_delete(wl_limits);
1069 return cpl_error_set_message_macro(cpl_func, CPL_ERROR_ILLEGAL_INPUT,
1070 __FILE__, __LINE__,
"The %d-line "
1071 "catalogue only has lines below %g",
1077 nsub = 1 + wave_max_id - wave_min_id;
1078 cpl_ensure_code(nsub > 1, CPL_ERROR_ILLEGAL_INPUT);
1082 sub_cat_x = cpl_vector_wrap(nsub, wave_min_id + (
double*)dxlines);
1083 sub_cat_y = cpl_vector_wrap(nsub, wave_min_id + (
double*)
1084 cpl_bivector_get_y_data_const(lines_catalog));
1085 sub_cat = cpl_bivector_wrap_vectors(sub_cat_x, sub_cat_y);
1088 error = irplib_wlxcorr_signal_resample(
self, wl_limits, sub_cat);
1090 cpl_vector_delete(wl_limits);
1091 cpl_bivector_unwrap_vectors(sub_cat);
1092 (void)cpl_vector_unwrap(sub_cat_x);
1093 (void)cpl_vector_unwrap(sub_cat_y);
1095 cpl_ensure_code(!error, CPL_ERROR_ILLEGAL_INPUT);
1098 cpl_ensure_code(!irplib_wlxcorr_convolve(
self, conv_kernel),
1099 cpl_error_get_code());
1101 return CPL_ERROR_NONE;
1116 static int irplib_wlxcorr_signal_resample(
1117 cpl_vector * resampled,
1118 const cpl_vector * xbounds,
1119 const cpl_bivector * hires)
1121 const int hrsize = cpl_bivector_get_size(hires);
1122 const cpl_vector* xhires ;
1123 const cpl_vector* yhires ;
1124 const double * pxhires ;
1125 const double * pyhires ;
1126 const double * pxbounds ;
1127 cpl_vector * ybounds ;
1128 cpl_bivector * boundary ;
1130 double * presampled ;
1135 if ((!resampled) || (!xbounds) || (!hires))
return -1 ;
1138 nsamples = cpl_vector_get_size(resampled) ;
1141 presampled = cpl_vector_get_data(resampled) ;
1142 pxbounds = cpl_vector_get_data_const(xbounds) ;
1143 xhires = cpl_bivector_get_x_const(hires) ;
1144 yhires = cpl_bivector_get_y_const(hires) ;
1145 pxhires = cpl_vector_get_data_const(xhires) ;
1146 pyhires = cpl_vector_get_data_const(yhires) ;
1149 ybounds = cpl_vector_new(cpl_vector_get_size(xbounds)) ;
1150 boundary = cpl_bivector_wrap_vectors((cpl_vector*)xbounds,ybounds) ;
1151 pybounds = cpl_vector_get_data(ybounds) ;
1154 if (cpl_bivector_get_size(boundary) != nsamples + 1) {
1155 cpl_bivector_unwrap_vectors(boundary) ;
1156 cpl_vector_delete(ybounds) ;
1161 itt = cpl_vector_find(xhires, pxbounds[0]);
1164 if (cpl_bivector_interpolate_linear(boundary, hires)) {
1165 cpl_bivector_unwrap_vectors(boundary) ;
1166 cpl_vector_delete(ybounds) ;
1172 while (pxhires[itt] < pxbounds[0]) itt++;
1174 for (i=0; i < nsamples; i++) {
1179 double xlow = pxbounds[i];
1180 double x = pxhires[itt];
1182 if (x > pxbounds[i+1]) x = pxbounds[i+1];
1185 presampled[i] = pybounds[i] * (x - xlow);
1188 while ((pxhires[itt] < pxbounds[i+1]) && (itt < hrsize)) {
1189 const double xprev = x;
1191 if (x > pxbounds[i+1]) x = pxbounds[i+1];
1192 presampled[i] += pyhires[itt] * (x - xlow);
1199 presampled[i] += pybounds[i+1] * (pxbounds[i+1] - xlow);
1203 presampled[i] /= 2 * (pxbounds[i+1] - pxbounds[i]);
1205 cpl_bivector_unwrap_vectors(boundary) ;
1206 cpl_vector_delete(ybounds) ;
1234 static cpl_error_code cpl_vector_fill_lss_profile_symmetric(cpl_vector *
self,
1239 const double sigma = fwhm * CPL_MATH_SIG_FWHM;
1240 const int n = cpl_vector_get_size(
self);
1244 cpl_ensure_code(
self != NULL, CPL_ERROR_NULL_INPUT);
1245 cpl_ensure_code(slitw > 0.0, CPL_ERROR_ILLEGAL_INPUT);
1246 cpl_ensure_code(fwhm > 0.0, CPL_ERROR_ILLEGAL_INPUT);
1251 (void)cpl_vector_set(
self, 0,
1255 for (i = 1; i < n; i++) {
1257 const double x1p = i + 0.5*slitw + 0.5;
1258 const double x1n = i - 0.5*slitw + 0.5;
1259 const double x0p = i + 0.5*slitw - 0.5;
1260 const double x0n = i - 0.5*slitw - 0.5;
1261 const double val = 0.5/slitw *
1264 (void)cpl_vector_set(
self, i, val);
1267 return CPL_ERROR_NONE;
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.
double irplib_erf_antideriv(double x, double sigma)
The antiderivative of erx(x/sigma/sqrt(2)) with respect to x.