00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 #ifdef HAVE_CONFIG_H
00029 #include <config.h>
00030 #endif
00031
00032
00033
00034
00035
00036 #include <math.h>
00037 #include <string.h>
00038
00039 #include <cpl.h>
00040
00041 #include "irplib_wavecal_impl.h"
00042
00043 #include "irplib_wlxcorr.h"
00044
00045
00055
00056
00057
00058
00059
00060
00061
00062 #ifndef CPL_SIZE_FORMAT
00063 #define CPL_SIZE_FORMAT "d"
00064 #define cpl_size int
00065 #endif
00066
00067
00068 #ifndef inline
00069 #define inline
00070 #endif
00071
00072 #define IRPLIB_MAX(A,B) ((A) > (B) ? (A) : (B))
00073 #define IRPLIB_MIN(A,B) ((A) < (B) ? (A) : (B))
00074
00075 #define IRPLIB_PTR_SWAP(a,b) \
00076 do { void * irplib_ptr_swap =(a);(a)=(b);(b)=irplib_ptr_swap; } while (0)
00077
00078
00079
00080
00081
00082 static void irplib_wlxcorr_estimate(cpl_vector *, cpl_vector *,
00083 const cpl_vector *,
00084 const cpl_bivector *,
00085 const cpl_vector *,
00086 const cpl_polynomial *,
00087 double, double);
00088
00089 static int irplib_wlxcorr_signal_resample(cpl_vector *, const cpl_vector *,
00090 const cpl_bivector *) ;
00091 static cpl_error_code cpl_vector_fill_lss_profile_symmetric(cpl_vector *,
00092 double, double);
00093 static cpl_error_code irplib_wlcalib_fill_spectrum(cpl_vector *,
00094 const cpl_bivector *,
00095 const cpl_vector *,
00096 const cpl_polynomial *, int);
00097
00098 static cpl_boolean irplib_wlcalib_is_lines(const cpl_vector *,
00099 const cpl_polynomial *,
00100 int, double);
00101
00105
00141
00142 cpl_polynomial * irplib_wlxcorr_best_poly(const cpl_vector * spectrum,
00143 const cpl_bivector * lines_catalog,
00144 int degree,
00145 const cpl_polynomial * guess_poly,
00146 const cpl_vector * wl_error,
00147 int nsamples,
00148 double slitw,
00149 double fwhm,
00150 double * xc,
00151 cpl_table ** wlres,
00152 cpl_vector ** xcorrs)
00153 {
00154 const int spec_sz = cpl_vector_get_size(spectrum);
00155 const int nfree = cpl_vector_get_size(wl_error);
00156 int ntests = 1;
00157 cpl_vector * model;
00158 cpl_vector * vxc;
00159 cpl_vector * init_pts_wl;
00160 cpl_matrix * init_pts_x;
00161 cpl_vector * pts_wl;
00162 cpl_vector * vxcorrs;
00163 cpl_vector * conv_kernel = NULL;
00164 cpl_polynomial * poly_sol;
00165 cpl_polynomial * poly_candi;
00166 const double * pwl_error = cpl_vector_get_data_const(wl_error);
00167 const double * dxc;
00168 cpl_size degree_loc ;
00169 const cpl_boolean symsamp = CPL_TRUE;
00170 const cpl_boolean is_lines
00171 = irplib_wlcalib_is_lines(cpl_bivector_get_x_const(lines_catalog),
00172 guess_poly, spec_sz, 1.0);
00173 int i;
00174
00175
00176
00177
00178 if (wlres != NULL) *wlres = NULL;
00179 if (xcorrs != NULL) *xcorrs = NULL;
00180
00181
00182 cpl_msg_debug(cpl_func, "Checking %d^%d dispersion polynomials (slitw=%g, "
00183 "fwhm=%g) against %d-point observed spectrum with%s "
00184 "catalog resampling", nsamples, nfree, slitw, fwhm, spec_sz,
00185 is_lines ? "out" : "");
00186
00187 cpl_ensure(xc != NULL, CPL_ERROR_NULL_INPUT, NULL);
00188 *xc = -1.0;
00189 cpl_ensure(spectrum != NULL, CPL_ERROR_NULL_INPUT, NULL);
00190 cpl_ensure(lines_catalog != NULL, CPL_ERROR_NULL_INPUT, NULL);
00191 cpl_ensure(guess_poly != NULL, CPL_ERROR_NULL_INPUT, NULL);
00192 cpl_ensure(wl_error != NULL, CPL_ERROR_NULL_INPUT, NULL);
00193 cpl_ensure(nfree >= 2, CPL_ERROR_ILLEGAL_INPUT, NULL);
00194 cpl_ensure(nsamples > 0, CPL_ERROR_ILLEGAL_INPUT, NULL);
00195
00196 cpl_ensure(1 + degree == nfree, CPL_ERROR_ILLEGAL_INPUT, NULL);
00197
00198 cpl_ensure(cpl_polynomial_get_dimension(guess_poly) == 1,
00199 CPL_ERROR_ILLEGAL_INPUT, NULL);
00200
00201 if (nsamples > 1) {
00202
00203
00204 for (i = 0; i < nfree; i++) {
00205 if (pwl_error[i] != 0.0) break;
00206 }
00207 cpl_ensure(i < nfree, CPL_ERROR_ILLEGAL_INPUT, NULL);
00208 }
00209
00210 if (!is_lines) {
00211
00212 conv_kernel = irplib_wlxcorr_convolve_create_kernel(slitw, fwhm);
00213 cpl_ensure(conv_kernel != NULL, CPL_ERROR_ILLEGAL_INPUT, NULL);
00214 }
00215
00216
00217 init_pts_x = cpl_matrix_new(1, nfree);
00218 init_pts_wl = cpl_vector_new(nfree);
00219 pts_wl = cpl_vector_new(nfree);
00220 for (i = 0; i < nfree; i++) {
00221 const double xpos = spec_sz * i / (double)degree;
00222 const double wlpos = cpl_polynomial_eval_1d(guess_poly, xpos, NULL)
00223 - 0.5 * pwl_error[i];
00224
00225 cpl_matrix_set(init_pts_x, 0, i, xpos);
00226 cpl_vector_set(init_pts_wl, i, wlpos);
00227
00228 ntests *= nsamples;
00229
00230 }
00231
00232 vxcorrs = xcorrs != NULL ? cpl_vector_new(ntests) : NULL;
00233
00234 poly_sol = cpl_polynomial_new(1);
00235 poly_candi = cpl_polynomial_new(1);
00236 model = cpl_vector_new(spec_sz);
00237 vxc = cpl_vector_new(1);
00238 dxc = cpl_vector_get_data_const(vxc);
00239
00240
00241 for (i=0; i < ntests; i++) {
00242 int idiv = i;
00243 int deg;
00244
00245
00246
00247 for (deg = degree; deg >= 0; deg--, idiv /= nsamples) {
00248 const int imod = idiv % nsamples;
00249 const double wlpos = cpl_vector_get(init_pts_wl, deg)
00250 + imod * pwl_error[deg] / nsamples;
00251
00252
00253
00254
00255 cpl_vector_set(pts_wl, deg, wlpos);
00256
00257 if (imod > 0) break;
00258 }
00259
00260
00261 degree_loc = (cpl_size)degree ;
00262 cpl_polynomial_fit(poly_candi, init_pts_x, &symsamp, pts_wl,
00263 NULL, CPL_FALSE, NULL, °ree_loc);
00264
00265 irplib_wlxcorr_estimate(vxc, model, spectrum, lines_catalog,
00266 conv_kernel, poly_candi, slitw, fwhm);
00267 if (vxcorrs != NULL) cpl_vector_set(vxcorrs, i, *dxc);
00268 if (*dxc > *xc) {
00269
00270 *xc = *dxc;
00271 IRPLIB_PTR_SWAP(poly_sol, poly_candi);
00272 }
00273 }
00274
00275 cpl_vector_delete(model);
00276 cpl_vector_delete(vxc);
00277 cpl_vector_delete(conv_kernel);
00278 cpl_vector_delete(pts_wl);
00279 cpl_matrix_delete(init_pts_x);
00280 cpl_vector_delete(init_pts_wl);
00281 cpl_polynomial_delete(poly_candi);
00282
00283 #ifdef CPL_WLCALIB_FAIL_ON_CONSTANT
00284
00285 if (cpl_polynomial_get_degree(poly_sol) == 0) {
00286 cpl_polynomial_delete(poly_sol);
00287 cpl_vector_delete(vxcorrs);
00288 *xc = 0.0;
00289 cpl_error_set_message_macro(cpl_func, CPL_ERROR_ILLEGAL_OUTPUT,
00290 __FILE__, __LINE__, "Found a constant "
00291 "dispersion");
00292 cpl_errorstate_dump(prestate, CPL_FALSE, NULL);
00293 return NULL;
00294 }
00295 #endif
00296
00297 if (wlres != NULL) {
00298
00299
00300
00301 cpl_errorstate prestate = cpl_errorstate_get();
00302
00303 *wlres = irplib_wlxcorr_gen_spc_table(spectrum, lines_catalog, slitw,
00304 fwhm, guess_poly, poly_sol);
00305 if (*wlres == NULL) {
00306 cpl_polynomial_delete(poly_sol);
00307 cpl_vector_delete(vxcorrs);
00308 *xc = -1.0;
00309 cpl_error_set_message_macro(cpl_func, CPL_ERROR_ILLEGAL_OUTPUT,
00310 __FILE__, __LINE__, "Cannot generate "
00311 "infos table");
00312
00313 cpl_errorstate_set(prestate);
00314 return NULL;
00315 }
00316 }
00317
00318 if (xcorrs != NULL) {
00319 *xcorrs = vxcorrs;
00320 } else {
00321
00322 }
00323
00324 return poly_sol;
00325 }
00326
00327
00345
00346 cpl_table * irplib_wlxcorr_gen_spc_table(
00347 const cpl_vector * spectrum,
00348 const cpl_bivector * lines_catalog,
00349 double slitw,
00350 double fwhm,
00351 const cpl_polynomial * guess_poly,
00352 const cpl_polynomial * corr_poly)
00353 {
00354
00355 cpl_vector * conv_kernel = NULL;
00356 cpl_bivector * gen_init ;
00357 cpl_bivector * gen_corr ;
00358 cpl_table * spc_table ;
00359 const double * pgen ;
00360 const double xtrunc = 0.5 * slitw + 5.0 * fwhm * CPL_MATH_SIG_FWHM;
00361 const int spec_sz = cpl_vector_get_size(spectrum);
00362 const cpl_boolean guess_resamp
00363 = !irplib_wlcalib_is_lines(cpl_bivector_get_x_const(lines_catalog),
00364 guess_poly, spec_sz, 1.0);
00365 const cpl_boolean corr_resamp
00366 = !irplib_wlcalib_is_lines(cpl_bivector_get_x_const(lines_catalog),
00367 corr_poly, spec_sz, 1.0);
00368 cpl_error_code error;
00369
00370 cpl_msg_debug(cpl_func, "Tabel for guess dispersion polynomial (slitw=%g, "
00371 "fwhm=%g) with %d-point observed spectrum with%s catalog re"
00372 "sampling", slitw, fwhm, spec_sz, guess_resamp ? "out" : "");
00373 cpl_msg_debug(cpl_func, "Tabel for corr. dispersion polynomial (slitw=%g, "
00374 "fwhm=%g) with %d-point observed spectrum with%s catalog re"
00375 "sampling", slitw, fwhm, spec_sz, corr_resamp ? "out" : "");
00376
00377
00378 cpl_ensure(spectrum, CPL_ERROR_NULL_INPUT, NULL) ;
00379 cpl_ensure(lines_catalog, CPL_ERROR_NULL_INPUT, NULL) ;
00380 cpl_ensure(guess_poly, CPL_ERROR_NULL_INPUT, NULL) ;
00381 cpl_ensure(corr_poly, CPL_ERROR_NULL_INPUT, NULL) ;
00382
00383
00384 if (guess_resamp || corr_resamp) {
00385 conv_kernel = irplib_wlxcorr_convolve_create_kernel(slitw, fwhm);
00386
00387 if (conv_kernel == NULL) {
00388 cpl_error_set_message_macro(cpl_func, CPL_ERROR_ILLEGAL_INPUT,
00389 __FILE__, __LINE__, "Cannot create "
00390 "convolution kernel") ;
00391 return NULL ;
00392 }
00393 }
00394
00395
00396 gen_init = cpl_bivector_new(spec_sz);
00397 if (guess_resamp) {
00398 error = irplib_wlcalib_fill_spectrum(cpl_bivector_get_y(gen_init),
00399 lines_catalog, conv_kernel,
00400 guess_poly, 0);
00401 } else {
00402 error = irplib_vector_fill_line_spectrum_model
00403 (cpl_bivector_get_y(gen_init), NULL, NULL,
00404 guess_poly, lines_catalog,
00405 slitw, fwhm, xtrunc, 0, CPL_FALSE, CPL_FALSE, NULL);
00406 }
00407
00408 if (error || cpl_vector_fill_polynomial(cpl_bivector_get_x(gen_init),
00409 guess_poly, 1, 1)) {
00410 cpl_vector_delete(conv_kernel);
00411 cpl_bivector_delete(gen_init);
00412 cpl_error_set_message_macro(cpl_func, CPL_ERROR_ILLEGAL_INPUT,
00413 __FILE__, __LINE__, "Cannot get the "
00414 "emission spectrum");
00415 return NULL;
00416 }
00417
00418
00419 gen_corr = cpl_bivector_new(spec_sz);
00420 if (corr_resamp) {
00421 error = irplib_wlcalib_fill_spectrum(cpl_bivector_get_y(gen_corr),
00422 lines_catalog, conv_kernel,
00423 corr_poly, 0);
00424 } else {
00425 error = irplib_vector_fill_line_spectrum_model
00426 (cpl_bivector_get_y(gen_corr), NULL, NULL,
00427 corr_poly, lines_catalog,
00428 slitw, fwhm, xtrunc, 0, CPL_FALSE, CPL_FALSE, NULL);
00429 }
00430
00431 if (error || cpl_vector_fill_polynomial(cpl_bivector_get_x(gen_corr),
00432 corr_poly, 1, 1)) {
00433 cpl_vector_delete(conv_kernel);
00434 cpl_bivector_delete(gen_init);
00435 cpl_bivector_delete(gen_corr) ;
00436 cpl_error_set_message_macro(cpl_func, CPL_ERROR_ILLEGAL_INPUT,
00437 __FILE__, __LINE__, "Cannot get the "
00438 "emission spectrum");
00439 return NULL;
00440 }
00441 cpl_vector_delete(conv_kernel) ;
00442
00443
00444 spc_table = cpl_table_new(spec_sz);
00445 cpl_table_new_column(spc_table, IRPLIB_WLXCORR_COL_WAVELENGTH,
00446 CPL_TYPE_DOUBLE);
00447 cpl_table_new_column(spc_table, IRPLIB_WLXCORR_COL_CAT_INIT,
00448 CPL_TYPE_DOUBLE);
00449 cpl_table_new_column(spc_table, IRPLIB_WLXCORR_COL_CAT_FINAL,
00450 CPL_TYPE_DOUBLE);
00451 cpl_table_new_column(spc_table, IRPLIB_WLXCORR_COL_OBS, CPL_TYPE_DOUBLE);
00452
00453
00454 pgen = cpl_bivector_get_x_data_const(gen_corr) ;
00455 cpl_table_copy_data_double(spc_table, IRPLIB_WLXCORR_COL_WAVELENGTH, pgen) ;
00456 pgen = cpl_bivector_get_y_data_const(gen_corr) ;
00457 cpl_table_copy_data_double(spc_table, IRPLIB_WLXCORR_COL_CAT_FINAL, pgen) ;
00458 pgen = cpl_vector_get_data_const(spectrum) ;
00459 cpl_table_copy_data_double(spc_table, IRPLIB_WLXCORR_COL_OBS, pgen) ;
00460 pgen = cpl_bivector_get_y_data_const(gen_init) ;
00461 cpl_table_copy_data_double(spc_table, IRPLIB_WLXCORR_COL_CAT_INIT, pgen);
00462 cpl_bivector_delete(gen_init);
00463 cpl_bivector_delete(gen_corr);
00464
00465 return spc_table ;
00466 }
00467
00468
00480
00481 cpl_bivector * irplib_wlxcorr_cat_extract(
00482 const cpl_bivector * lines_catalog,
00483 double wave_min,
00484 double wave_max)
00485 {
00486 const int nlines = cpl_bivector_get_size(lines_catalog);
00487 int wave_min_id, wave_max_id ;
00488 cpl_vector * sub_cat_wl ;
00489 cpl_vector * sub_cat_int ;
00490 const cpl_vector * xlines = cpl_bivector_get_x_const(lines_catalog);
00491 const double * dxlines = cpl_vector_get_data_const(xlines);
00492
00493 cpl_ensure(lines_catalog != NULL, CPL_ERROR_NULL_INPUT, NULL);
00494
00495
00496 wave_min_id = (int)cpl_vector_find(xlines, wave_min);
00497 if (wave_min_id < 0) {
00498 cpl_error_set_message_macro(cpl_func, CPL_ERROR_ILLEGAL_INPUT,
00499 __FILE__, __LINE__,
00500 "The starting wavelength cannot be found") ;
00501 return NULL ;
00502 }
00503
00504
00505 if (dxlines[wave_min_id] <= wave_min) wave_min_id++;
00506
00507
00508 wave_max_id = (int)cpl_vector_find(xlines, wave_max);
00509 if (wave_max_id < 0) {
00510 cpl_error_set_message_macro(cpl_func, CPL_ERROR_ILLEGAL_INPUT,
00511 __FILE__, __LINE__,
00512 "The ending wavelength cannot be found") ;
00513 return NULL ;
00514 }
00515
00516 if (dxlines[wave_max_id] >= wave_min) wave_max_id--;
00517
00518
00519
00520 cpl_ensure(wave_min_id <= wave_max_id, CPL_ERROR_ILLEGAL_INPUT, NULL);
00521
00522 if (wave_min_id < 0 || wave_max_id == nlines) {
00523 cpl_error_set_message_macro(cpl_func, CPL_ERROR_ILLEGAL_INPUT,
00524 __FILE__, __LINE__, "The %d-line catalogue "
00525 "has no lines in the range %g -> %g",
00526 nlines, wave_min, wave_max);
00527 return NULL ;
00528 }
00529
00530 sub_cat_wl = cpl_vector_extract(xlines, wave_min_id, wave_max_id, 1);
00531 sub_cat_int = cpl_vector_extract(cpl_bivector_get_y_const(lines_catalog),
00532 wave_min_id, wave_max_id, 1);
00533
00534 return cpl_bivector_wrap_vectors(sub_cat_wl, sub_cat_int);
00535 }
00536
00537
00554
00555 cpl_vector * irplib_wlxcorr_convolve_create_kernel(double slitw,
00556 double fwhm)
00557 {
00558 const double sigma = fwhm * CPL_MATH_SIG_FWHM;
00559 const int size = 1 + (int)(5.0 * sigma + 0.5*slitw);
00560 cpl_vector * kernel = cpl_vector_new(size);
00561
00562
00563 if (cpl_vector_fill_lss_profile_symmetric(kernel, slitw, fwhm)) {
00564 cpl_vector_delete(kernel);
00565 cpl_ensure(0, cpl_error_get_code(), NULL);
00566 }
00567
00568 return kernel;
00569 }
00570
00571
00584
00585 int irplib_wlxcorr_convolve(
00586 cpl_vector * smoothed,
00587 const cpl_vector * conv_kernel)
00588 {
00589 int nsamples ;
00590 int ihwidth ;
00591 cpl_vector * raw ;
00592 double * psmoothe ;
00593 double * praw ;
00594 const double* psymm ;
00595 int i, j ;
00596
00597
00598 cpl_ensure(smoothed, CPL_ERROR_NULL_INPUT, -1) ;
00599 cpl_ensure(conv_kernel, CPL_ERROR_NULL_INPUT, -1) ;
00600
00601
00602 nsamples = cpl_vector_get_size(smoothed) ;
00603 ihwidth = cpl_vector_get_size(conv_kernel) - 1 ;
00604 cpl_ensure(ihwidth<nsamples, CPL_ERROR_ILLEGAL_INPUT, -1) ;
00605 psymm = cpl_vector_get_data_const(conv_kernel) ;
00606 psmoothe = cpl_vector_get_data(smoothed) ;
00607
00608
00609 raw = cpl_vector_duplicate(smoothed) ;
00610 praw = cpl_vector_get_data(raw) ;
00611
00612
00613 for (i=0 ; i<ihwidth ; i++) {
00614 psmoothe[i] = praw[i] * psymm[0];
00615 for (j=1 ; j <= ihwidth ; j++) {
00616 const int k = i-j < 0 ? 0 : i-j;
00617 psmoothe[i] += (praw[k]+praw[i+j]) * psymm[j];
00618 }
00619 }
00620
00621 for (i=ihwidth ; i<nsamples-ihwidth ; i++) {
00622 psmoothe[i] = praw[i] * psymm[0];
00623 for (j=1 ; j<=ihwidth ; j++)
00624 psmoothe[i] += (praw[i-j]+praw[i+j]) * psymm[j];
00625 }
00626 for (i=nsamples-ihwidth ; i<nsamples ; i++) {
00627 psmoothe[i] = praw[i] * psymm[0];
00628 for (j=1 ; j<=ihwidth ; j++) {
00629 const int k = i+j > nsamples-1 ? nsamples - 1 : i+j;
00630 psmoothe[i] += (praw[k]+praw[i-j]) * psymm[j];
00631 }
00632 }
00633 cpl_vector_delete(raw) ;
00634 return 0 ;
00635 }
00636
00637
00647
00648 int irplib_wlxcorr_plot_solution(
00649 const cpl_polynomial * init,
00650 const cpl_polynomial * comp,
00651 const cpl_polynomial * sol,
00652 int pix_start,
00653 int pix_stop)
00654 {
00655 int nsamples, nplots ;
00656 cpl_vector ** vectors ;
00657 cpl_bivector * bivector ;
00658 double diff ;
00659 int i ;
00660
00661
00662 if (init == NULL || comp == NULL) return -1 ;
00663
00664
00665 nsamples = pix_stop - pix_start + 1 ;
00666 if (sol != NULL) nplots = 3 ;
00667 else nplots = 2 ;
00668
00669
00670 vectors = cpl_malloc((nplots+1)*sizeof(cpl_vector*)) ;
00671 for (i=0 ; i<nplots+1 ; i++) vectors[i] = cpl_vector_new(nsamples) ;
00672
00673
00674
00675 for (i=0 ; i<nsamples ; i++) {
00676 cpl_vector_set(vectors[0], i, pix_start+i) ;
00677 cpl_vector_set(vectors[1], i,
00678 cpl_polynomial_eval_1d(init, (double)(pix_start+i), NULL)) ;
00679 cpl_vector_set(vectors[2], i,
00680 cpl_polynomial_eval_1d(comp, (double)(pix_start+i), NULL)) ;
00681 if (sol != NULL)
00682 cpl_vector_set(vectors[3], i,
00683 cpl_polynomial_eval_1d(sol, (double)(pix_start+i), NULL)) ;
00684 }
00685
00686
00687 cpl_plot_vectors("set grid;set xlabel 'Position (pixels)';",
00688 "t '1-Initial / 2-Computed / 3-Solution' w lines",
00689 "", (const cpl_vector **)vectors, nplots+1);
00690
00691
00692 for (i=0 ; i<nplots+1 ; i++) cpl_vector_delete(vectors[i]) ;
00693 cpl_free(vectors) ;
00694
00695
00696 nplots -- ;
00697 vectors = cpl_malloc((nplots+1)*sizeof(cpl_vector*)) ;
00698 for (i=0 ; i<nplots+1 ; i++) vectors[i] = cpl_vector_new(nsamples) ;
00699
00700
00701
00702 for (i=0 ; i<nsamples ; i++) {
00703 cpl_vector_set(vectors[0], i, pix_start+i) ;
00704 diff = cpl_polynomial_eval_1d(comp, (double)(pix_start+i), NULL) -
00705 cpl_polynomial_eval_1d(init, (double)(pix_start+i), NULL) ;
00706 cpl_vector_set(vectors[1], i, diff) ;
00707 if (sol != NULL) {
00708 diff = cpl_polynomial_eval_1d(sol, (double)(pix_start+i), NULL) -
00709 cpl_polynomial_eval_1d(init, (double)(pix_start+i), NULL) ;
00710 cpl_vector_set(vectors[2], i, diff) ;
00711 }
00712 }
00713
00714
00715 if (sol == NULL) {
00716 bivector = cpl_bivector_wrap_vectors(vectors[0], vectors[1]) ;
00717 cpl_plot_bivector(
00718 "set grid;set xlabel 'Position (pixels)';set ylabel 'Wavelength difference';",
00719 "t 'Computed-Initial wavelenth' w lines", "", bivector);
00720 cpl_bivector_unwrap_vectors(bivector) ;
00721 } else {
00722 cpl_plot_vectors("set grid;set xlabel 'Position (pixels)';",
00723 "t '1-Computed - Initial / 2--Solution - Initial' w lines",
00724 "", (const cpl_vector **)vectors, nplots+1);
00725 }
00726
00727
00728 for (i=0 ; i<nplots+1 ; i++) cpl_vector_delete(vectors[i]) ;
00729 cpl_free(vectors) ;
00730
00731
00732 return 0 ;
00733 }
00734
00735
00746
00747 int irplib_wlxcorr_plot_spc_table(
00748 const cpl_table * spc_table,
00749 const char * title,
00750 int first_plotted_line,
00751 int last_plotted_line)
00752 {
00753 char title_loc[1024] ;
00754 cpl_vector ** vectors ;
00755 cpl_vector ** sub_vectors ;
00756 cpl_vector * tmp_vec ;
00757 int nsamples ;
00758 double max, mean1, mean3 ;
00759 int start_ind, stop_ind, hsize_pix ;
00760 int i, j ;
00761
00762
00763 if (first_plotted_line > last_plotted_line) return -1 ;
00764 if (spc_table == NULL) return -1 ;
00765
00766
00767 nsamples = cpl_table_get_nrow(spc_table) ;
00768 hsize_pix = 10 ;
00769
00770 sprintf(title_loc,
00771 "t '%s - 1-Initial catalog/2-Corrected catalog/3-Observed' w lines",
00772 title) ;
00773 title_loc[1023] = (char)0 ;
00774
00775 vectors = cpl_malloc(4*sizeof(cpl_vector*)) ;
00776 vectors[0] = cpl_vector_wrap(nsamples,
00777 cpl_table_get_data_double((cpl_table*)spc_table,
00778 IRPLIB_WLXCORR_COL_WAVELENGTH));
00779 vectors[1] = cpl_vector_wrap(nsamples,
00780 cpl_table_get_data_double((cpl_table*)spc_table,
00781 IRPLIB_WLXCORR_COL_CAT_INIT));
00782 vectors[2] = cpl_vector_wrap(nsamples,
00783 cpl_table_get_data_double((cpl_table*)spc_table,
00784 IRPLIB_WLXCORR_COL_CAT_FINAL));
00785 vectors[3] = cpl_vector_wrap(nsamples,
00786 cpl_table_get_data_double((cpl_table*)spc_table,
00787 IRPLIB_WLXCORR_COL_OBS)) ;
00788
00789
00790 mean1 = cpl_vector_get_mean(vectors[1]) ;
00791 mean3 = cpl_vector_get_mean(vectors[3]) ;
00792 if (fabs(mean3) > 1)
00793 cpl_vector_multiply_scalar(vectors[3], fabs(mean1/mean3)) ;
00794
00795 cpl_plot_vectors("set grid;set xlabel 'Wavelength (nm)';", title_loc,
00796 "", (const cpl_vector **)vectors, 4);
00797
00798
00799 if (fabs(mean3) > 1)
00800 cpl_vector_multiply_scalar(vectors[3], mean3/mean1) ;
00801
00802
00803 sprintf(title_loc,
00804 "t '%s - 1-Initial catalog/2-Corrected catalog/3-Observed (ZOOMED)' w lines",
00805 title) ;
00806 title_loc[1023] = (char)0 ;
00807 tmp_vec = cpl_vector_duplicate(vectors[2]) ;
00808 for (i=0 ; i<last_plotted_line ; i++) {
00809
00810 if ((max = cpl_vector_get_max(tmp_vec)) <= 0.0) break ;
00811 for (j=0 ; j<nsamples ; j++) {
00812 if (cpl_vector_get(tmp_vec, j) == max) break ;
00813 }
00814 if (j-hsize_pix < 0) start_ind = 0 ;
00815 else start_ind = j-hsize_pix ;
00816 if (j+hsize_pix > nsamples-1) stop_ind = nsamples-1 ;
00817 else stop_ind = j+hsize_pix ;
00818 for (j=start_ind ; j<=stop_ind ; j++) cpl_vector_set(tmp_vec, j, 0.0) ;
00819
00820 if (i+1 >= first_plotted_line) {
00821 sub_vectors = cpl_malloc(4*sizeof(cpl_vector*)) ;
00822 sub_vectors[0]=cpl_vector_extract(vectors[0],start_ind,stop_ind,1);
00823 sub_vectors[1]=cpl_vector_extract(vectors[1],start_ind,stop_ind,1);
00824 sub_vectors[2]=cpl_vector_extract(vectors[2],start_ind,stop_ind,1);
00825 sub_vectors[3]=cpl_vector_extract(vectors[3],start_ind,stop_ind,1);
00826
00827 cpl_plot_vectors("set grid;set xlabel 'Wavelength (nm)';",
00828 title_loc, "", (const cpl_vector **)sub_vectors, 4);
00829
00830 cpl_vector_delete(sub_vectors[0]) ;
00831 cpl_vector_delete(sub_vectors[1]) ;
00832 cpl_vector_delete(sub_vectors[2]) ;
00833 cpl_vector_delete(sub_vectors[3]) ;
00834 cpl_free(sub_vectors) ;
00835 }
00836 }
00837 cpl_vector_delete(tmp_vec) ;
00838
00839 cpl_vector_unwrap(vectors[0]) ;
00840 cpl_vector_unwrap(vectors[1]) ;
00841 cpl_vector_unwrap(vectors[2]) ;
00842 cpl_vector_unwrap(vectors[3]) ;
00843 cpl_free(vectors) ;
00844
00845 return 0 ;
00846 }
00847
00848
00856
00857 int irplib_wlxcorr_catalog_plot(
00858 const cpl_bivector * cat,
00859 double wmin,
00860 double wmax)
00861 {
00862 int start, stop ;
00863 cpl_bivector * subcat ;
00864 cpl_vector * subcat_x ;
00865 cpl_vector * subcat_y ;
00866 const double * pwave ;
00867 int nvals, nvals_tot ;
00868 int i ;
00869
00870
00871 if (cat == NULL) return -1 ;
00872 if (wmax <= wmin) return -1 ;
00873
00874
00875 nvals_tot = cpl_bivector_get_size(cat) ;
00876
00877
00878 pwave = cpl_bivector_get_x_data_const(cat) ;
00879 if (pwave[0] >= wmin) start = 0 ;
00880 else start = -1 ;
00881 if (pwave[nvals_tot-1] <= wmax) stop = nvals_tot-1 ;
00882 else stop = -1 ;
00883 i=0 ;
00884 while ((pwave[i] < wmin) && (i<nvals_tot-1)) i++ ;
00885 start = i ;
00886 i= nvals_tot-1 ;
00887 while ((pwave[i] > wmax) && (i>0)) i-- ;
00888 stop = i ;
00889
00890 if (start>=stop) {
00891 cpl_msg_error(cpl_func, "Cannot plot the catalog") ;
00892 return -1 ;
00893 }
00894 nvals = start - stop + 1 ;
00895
00896
00897 subcat_x = cpl_vector_extract(cpl_bivector_get_x_const(cat),start,stop, 1) ;
00898 subcat_y = cpl_vector_extract(cpl_bivector_get_y_const(cat),start,stop, 1) ;
00899 subcat = cpl_bivector_wrap_vectors(subcat_x, subcat_y) ;
00900
00901
00902 if (nvals > 500) {
00903 cpl_plot_bivector(
00904 "set grid;set xlabel 'Wavelength (nm)';set ylabel 'Emission';",
00905 "t 'Catalog Spectrum' w lines", "", subcat);
00906 } else {
00907 cpl_plot_bivector(
00908 "set grid;set xlabel 'Wavelength (nm)';set ylabel 'Emission';",
00909 "t 'Catalog Spectrum' w impulses", "", subcat);
00910 }
00911 cpl_bivector_unwrap_vectors(subcat) ;
00912 cpl_vector_delete(subcat_x) ;
00913 cpl_vector_delete(subcat_y) ;
00914
00915 return 0 ;
00916 }
00917
00920
00935
00936 static void irplib_wlxcorr_estimate(cpl_vector * vxc,
00937 cpl_vector * model,
00938 const cpl_vector * spectrum,
00939 const cpl_bivector * lines_catalog,
00940 const cpl_vector * conv_kernel,
00941 const cpl_polynomial * poly_candi,
00942 double slitw,
00943 double fwhm)
00944 {
00945 cpl_errorstate prestate = cpl_errorstate_get();
00946 const int hsize = cpl_vector_get_size(vxc) / 2;
00947
00948 if (conv_kernel != NULL) {
00949 irplib_wlcalib_fill_spectrum(model, lines_catalog, conv_kernel,
00950 poly_candi, hsize);
00951 } else {
00952 const double xtrunc = 0.5 * slitw + 5.0 * fwhm * CPL_MATH_SIG_FWHM;
00953
00954 irplib_vector_fill_line_spectrum_model(model, NULL, NULL, poly_candi,
00955 lines_catalog, slitw, fwhm,
00956 xtrunc, 0, CPL_FALSE, CPL_FALSE,
00957 NULL);
00958 }
00959
00960 if (cpl_errorstate_is_equal(prestate))
00961 cpl_vector_correlate(vxc, model, spectrum);
00962
00963 if (!cpl_errorstate_is_equal(prestate)) {
00964 cpl_vector_fill(vxc, 0.0);
00965
00966
00967 cpl_errorstate_set(prestate);
00968
00969 }
00970
00971 return;
00972 }
00973
00974
00975
00985
00986 static cpl_boolean irplib_wlcalib_is_lines(const cpl_vector * wavelengths,
00987 const cpl_polynomial * disp1d,
00988 int spec_sz,
00989 double tol)
00990 {
00991 const int nlines = cpl_vector_get_size(wavelengths);
00992
00993 const double dispersion = cpl_polynomial_eval_1d_diff(disp1d,
00994 0.5 * spec_sz + 1.0,
00995 0.5 * spec_sz,
00996 NULL);
00997 const double range = cpl_vector_get(wavelengths, nlines-1)
00998 - cpl_vector_get(wavelengths, 0);
00999
01000 cpl_ensure(wavelengths != NULL, CPL_ERROR_NULL_INPUT, CPL_FALSE);
01001 cpl_ensure(disp1d != NULL, CPL_ERROR_NULL_INPUT, CPL_FALSE);
01002 cpl_ensure(cpl_polynomial_get_dimension(disp1d) == 1,
01003 CPL_ERROR_ILLEGAL_INPUT, CPL_FALSE);
01004 cpl_ensure(range > 0.0, CPL_ERROR_ILLEGAL_INPUT, CPL_FALSE);
01005
01006 return nlines * fabs(dispersion) <= tol * fabs(range) ? CPL_TRUE
01007 : CPL_FALSE;
01008
01009 }
01010
01011
01026
01027 static
01028 cpl_error_code irplib_wlcalib_fill_spectrum(cpl_vector * self,
01029 const cpl_bivector * lines_catalog,
01030 const cpl_vector * conv_kernel,
01031 const cpl_polynomial * poly,
01032 int search_hs)
01033 {
01034
01035
01036 const int size = cpl_vector_get_size(self);
01037 const int nlines = cpl_bivector_get_size(lines_catalog);
01038 const cpl_vector * xlines = cpl_bivector_get_x_const(lines_catalog);
01039 const double * dxlines = cpl_vector_get_data_const(xlines);
01040 cpl_bivector * sub_cat ;
01041 cpl_vector * sub_cat_x;
01042 cpl_vector * sub_cat_y;
01043 cpl_vector * wl_limits;
01044 double wave_min, wave_max;
01045 int wave_min_id, wave_max_id;
01046 int nsub;
01047 int error;
01048
01049 cpl_ensure_code(self != NULL, CPL_ERROR_NULL_INPUT);
01050 cpl_ensure_code(lines_catalog != NULL, CPL_ERROR_NULL_INPUT);
01051 cpl_ensure_code(conv_kernel != NULL, CPL_ERROR_NULL_INPUT);
01052 cpl_ensure_code(poly != NULL, CPL_ERROR_NULL_INPUT);
01053 cpl_ensure_code(size > 0, CPL_ERROR_ILLEGAL_INPUT);
01054
01055
01056
01057 wl_limits = cpl_vector_new(size + 1);
01058 cpl_vector_fill_polynomial(wl_limits, poly, 0.5 - search_hs, 1);
01059
01060
01061 wave_min = cpl_vector_get(wl_limits, 0);
01062 wave_max = cpl_vector_get(wl_limits, size);
01063
01064
01065 wave_min_id = cpl_vector_find(xlines, wave_min);
01066
01067 if (dxlines[wave_min_id] > wave_min) wave_min_id--;
01068
01069 if (wave_min_id < 0) {
01070 cpl_vector_delete(wl_limits);
01071 return cpl_error_set_message_macro(cpl_func, CPL_ERROR_ILLEGAL_INPUT,
01072 __FILE__, __LINE__, "The %d-line "
01073 "catalogue only has lines above %g",
01074 nlines, wave_min);
01075 }
01076
01077
01078 wave_max_id = cpl_vector_find(xlines, wave_max);
01079
01080 if (dxlines[wave_max_id] < wave_max) wave_max_id++;
01081
01082 if (wave_max_id == nlines) {
01083 cpl_vector_delete(wl_limits);
01084 return cpl_error_set_message_macro(cpl_func, CPL_ERROR_ILLEGAL_INPUT,
01085 __FILE__, __LINE__, "The %d-line "
01086 "catalogue only has lines below %g",
01087 nlines, wave_max);
01088 }
01089
01090
01091
01092 nsub = 1 + wave_max_id - wave_min_id;
01093 cpl_ensure_code(nsub > 1, CPL_ERROR_ILLEGAL_INPUT);
01094
01095
01096
01097 sub_cat_x = cpl_vector_wrap(nsub, wave_min_id + (double*)dxlines);
01098 sub_cat_y = cpl_vector_wrap(nsub, wave_min_id + (double*)
01099 cpl_bivector_get_y_data_const(lines_catalog));
01100 sub_cat = cpl_bivector_wrap_vectors(sub_cat_x, sub_cat_y);
01101
01102
01103 error = irplib_wlxcorr_signal_resample(self, wl_limits, sub_cat);
01104
01105 cpl_vector_delete(wl_limits);
01106 cpl_bivector_unwrap_vectors(sub_cat);
01107 (void)cpl_vector_unwrap(sub_cat_x);
01108 (void)cpl_vector_unwrap(sub_cat_y);
01109
01110 cpl_ensure_code(!error, CPL_ERROR_ILLEGAL_INPUT);
01111
01112
01113 cpl_ensure_code(!irplib_wlxcorr_convolve(self, conv_kernel),
01114 cpl_error_get_code());
01115
01116 return CPL_ERROR_NONE;
01117 }
01118
01119
01120
01130
01131 static int irplib_wlxcorr_signal_resample(
01132 cpl_vector * resampled,
01133 const cpl_vector * xbounds,
01134 const cpl_bivector * hires)
01135 {
01136 const int hrsize = cpl_bivector_get_size(hires);
01137 const cpl_vector* xhires ;
01138 const cpl_vector* yhires ;
01139 const double * pxhires ;
01140 const double * pyhires ;
01141 const double * pxbounds ;
01142 cpl_vector * ybounds ;
01143 cpl_bivector * boundary ;
01144 double * pybounds ;
01145 double * presampled ;
01146 int nsamples ;
01147 int i, itt ;
01148
01149
01150 if ((!resampled) || (!xbounds) || (!hires)) return -1 ;
01151
01152
01153 nsamples = cpl_vector_get_size(resampled) ;
01154
01155
01156 presampled = cpl_vector_get_data(resampled) ;
01157 pxbounds = cpl_vector_get_data_const(xbounds) ;
01158 xhires = cpl_bivector_get_x_const(hires) ;
01159 yhires = cpl_bivector_get_y_const(hires) ;
01160 pxhires = cpl_vector_get_data_const(xhires) ;
01161 pyhires = cpl_vector_get_data_const(yhires) ;
01162
01163
01164 ybounds = cpl_vector_new(cpl_vector_get_size(xbounds)) ;
01165 boundary = cpl_bivector_wrap_vectors((cpl_vector*)xbounds,ybounds) ;
01166 pybounds = cpl_vector_get_data(ybounds) ;
01167
01168
01169 if (cpl_bivector_get_size(boundary) != nsamples + 1) {
01170 cpl_bivector_unwrap_vectors(boundary) ;
01171 cpl_vector_delete(ybounds) ;
01172 return -1 ;
01173 }
01174
01175
01176 itt = cpl_vector_find(xhires, pxbounds[0]);
01177
01178
01179 if (cpl_bivector_interpolate_linear(boundary, hires)) {
01180 cpl_bivector_unwrap_vectors(boundary) ;
01181 cpl_vector_delete(ybounds) ;
01182 return -1 ;
01183 }
01184
01185
01186
01187 while (pxhires[itt] < pxbounds[0]) itt++;
01188
01189 for (i=0; i < nsamples; i++) {
01190
01191
01192
01193
01194 double xlow = pxbounds[i];
01195 double x = pxhires[itt];
01196
01197 if (x > pxbounds[i+1]) x = pxbounds[i+1];
01198
01199
01200 presampled[i] = pybounds[i] * (x - xlow);
01201
01202
01203 while ((pxhires[itt] < pxbounds[i+1]) && (itt < hrsize)) {
01204 const double xprev = x;
01205 x = pxhires[itt+1];
01206 if (x > pxbounds[i+1]) x = pxbounds[i+1];
01207 presampled[i] += pyhires[itt] * (x - xlow);
01208 xlow = xprev;
01209 itt++;
01210 }
01211
01212
01213
01214 presampled[i] += pybounds[i+1] * (pxbounds[i+1] - xlow);
01215
01216
01217
01218 presampled[i] /= 2 * (pxbounds[i+1] - pxbounds[i]);
01219 }
01220 cpl_bivector_unwrap_vectors(boundary) ;
01221 cpl_vector_delete(ybounds) ;
01222 return 0 ;
01223 }
01224
01225
01226
01227
01248
01249 static cpl_error_code cpl_vector_fill_lss_profile_symmetric(cpl_vector * self,
01250 double slitw,
01251 double fwhm)
01252 {
01253
01254 const double sigma = fwhm * CPL_MATH_SIG_FWHM;
01255 const int n = cpl_vector_get_size(self);
01256 int i;
01257
01258
01259 cpl_ensure_code(self != NULL, CPL_ERROR_NULL_INPUT);
01260 cpl_ensure_code(slitw > 0.0, CPL_ERROR_ILLEGAL_INPUT);
01261 cpl_ensure_code(fwhm > 0.0, CPL_ERROR_ILLEGAL_INPUT);
01262
01263
01264
01265
01266 (void)cpl_vector_set(self, 0,
01267 (irplib_erf_antideriv(0.5*slitw + 0.5, sigma) -
01268 irplib_erf_antideriv(0.5*slitw - 0.5, sigma)) / slitw);
01269
01270 for (i = 1; i < n; i++) {
01271
01272 const double x1p = i + 0.5*slitw + 0.5;
01273 const double x1n = i - 0.5*slitw + 0.5;
01274 const double x0p = i + 0.5*slitw - 0.5;
01275 const double x0n = i - 0.5*slitw - 0.5;
01276 const double val = 0.5/slitw *
01277 (irplib_erf_antideriv(x1p, sigma) - irplib_erf_antideriv(x1n, sigma) -
01278 irplib_erf_antideriv(x0p, sigma) + irplib_erf_antideriv(x0n, sigma));
01279 (void)cpl_vector_set(self, i, val);
01280 }
01281
01282 return CPL_ERROR_NONE;
01283 }