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 #include <string.h>
00033 #include <math.h>
00034
00035 #include <cxstring.h>
00036 #include <cxmemory.h>
00037
00038 #include <cpl_image.h>
00039 #include <cpl_vector.h>
00040 #include <cpl_matrix.h>
00041 #include <cpl_parameterlist.h>
00042 #include <cpl_msg.h>
00043
00044 #include "gimacros.h"
00045 #include "gialias.h"
00046 #include "giarray.h"
00047 #include "giimage.h"
00048 #include "gitable.h"
00049 #include "gimatrix.h"
00050 #include "giarray.h"
00051 #include "gimask.h"
00052 #include "gimath.h"
00053 #include "gimessages.h"
00054 #include "giutils.h"
00055 #include "gilocalize.h"
00056 #include "gidebug.h"
00057
00058
00059
00068
00069
00070 enum GiLocalizeMethod {
00071 GILOCALIZE_HALF_WIDTH,
00072 GILOCALIZE_BARYCENTER
00073 };
00074
00075 typedef enum GiLocalizeMethod GiLocalizeMethod;
00076
00077
00078
00079
00080
00081
00082 static const cxchar *_task = "giraffe_localize_spectra";
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117 inline static cxbool
00118 _giraffe_validate_pixel(cxint *pixels, cxint xsize, cxint ysize,
00119 cxint xpos, cxint ypos, cxint xwidth, cxint ywidth,
00120 cxsize count)
00121 {
00122
00123 cxint i;
00124 cxint xstart = xpos - xwidth;
00125 cxint ystart = ypos - ywidth;
00126 cxint xend = xpos + xwidth;
00127 cxint yend = ypos + ywidth;
00128
00129 cxsize _count = 0;
00130
00131
00132
00133
00134
00135
00136
00137 xstart = CX_MAX(0, xstart);
00138 ystart = CX_MAX(0, ystart);
00139
00140 xend = CX_MIN(xsize - 1, xend);
00141 yend = CX_MIN(ysize - 1, yend);
00142
00143 xwidth = CX_MAX(xwidth,1 );
00144 ywidth = CX_MAX(ywidth,1 );
00145
00146
00147
00148
00149
00150
00151
00152 for (i = ystart; i <= yend; i++) {
00153
00154 cxint j;
00155 cxint row;
00156
00157
00158
00159
00160
00161
00162
00163
00164
00165 if (i == ypos) {
00166 continue;
00167 }
00168
00169 row = i * xsize;
00170
00171 for (j = xstart; j <= xend; j++) {
00172 if (pixels[row + j]) {
00173 ++_count;
00174 }
00175
00176 if (_count >= count) {
00177 return 1;
00178 }
00179 }
00180
00181 }
00182
00183 return 0;
00184
00185 }
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196
00197
00198
00199
00200
00201
00202
00203
00204
00205
00206
00207
00208
00209
00210
00211
00212
00213
00214
00215 inline static cpl_matrix*
00216 _giraffe_fit_border(cpl_matrix* mborder, cpl_matrix* mbase,
00217 cpl_matrix* mxok, cxint nspectra, cxdouble sigma,
00218 cxint niter, cxdouble mfrac, cpl_matrix* mcoeff)
00219 {
00220
00221 const cxchar* const fctid = "_giraffe_fit_border";
00222
00223 register cxint x = 0;
00224 register cxint naccept = 0;
00225 register cxint ntotal = 0;
00226 register cxint iteration = 0;
00227 register cxint nx = cpl_matrix_get_ncol(mbase);
00228 register cxint yorder = cpl_matrix_get_nrow(mbase);
00229 register cxint nxok = cpl_matrix_get_nrow(mxok);
00230
00231 register cxdouble ratio = 1.0;
00232
00233 cpl_matrix* mtmp = NULL;
00234 cpl_matrix* yraw = NULL;
00235 cpl_matrix* ydiff = NULL;
00236 cpl_matrix* mfit = NULL;
00237 cpl_matrix* coeffs = NULL;
00238
00239
00240
00241 if (nxok < yorder) {
00242 cpl_error_set(fctid, CPL_ERROR_INCOMPATIBLE_INPUT);
00243
00244 GIDEBUG(gi_warning("%s: not enough points mxok[%d] for %d order fit",
00245 fctid, nxok, yorder));
00246
00247 return NULL;
00248 }
00249
00250
00251
00252
00253
00254
00255 yraw = cpl_matrix_new(1, nxok);
00256 ydiff = cpl_matrix_new(nxok, 1);
00257
00258 mtmp = cpl_matrix_duplicate(mxok);
00259
00260
00261
00262
00263
00264 for (x = 0; x < nxok; x++) {
00265 cxdouble data = cpl_matrix_get(mborder, x, nspectra);
00266 cpl_matrix_set(yraw, 0, x, data);
00267 }
00268
00269
00270
00271
00272
00273
00274 ntotal = nxok;
00275 naccept = ntotal;
00276
00277 while (naccept > 0 && iteration < niter && ratio > mfrac) {
00278
00279 register cxint k = 0;
00280 register cxint l = 0;
00281
00282 register cxdouble ysigma = 0.;
00283
00284 cpl_matrix* rawbase = giraffe_chebyshev_base1d(0., nx, yorder, mtmp);
00285 cx_assert(rawbase != NULL);
00286
00287 if (coeffs != NULL) {
00288 cpl_matrix_delete(coeffs);
00289 }
00290
00291 coeffs = giraffe_matrix_leastsq(rawbase, yraw);
00292 if (coeffs == NULL) {
00293 gi_warning("%s: error in giraffe_matrix_leastsq(), spectrum %d",
00294 fctid, nspectra);
00295 break;
00296 }
00297
00298 cpl_matrix_delete(rawbase);
00299 rawbase = NULL;
00300
00301 if (mfit != NULL) {
00302 cpl_matrix_delete(mfit);
00303 }
00304
00305 mfit = cpl_matrix_product_create(coeffs, mbase);
00306
00307
00308
00309
00310
00311
00312
00313
00314
00315
00316
00317
00318 for (x = 0; x < cpl_matrix_get_nrow(ydiff); x++) {
00319
00320 cxint xok = (cxint) cpl_matrix_get(mtmp, x, 0);
00321
00322 cxdouble diff =
00323 cpl_matrix_get(yraw, 0, x) - cpl_matrix_get(mfit, 0, xok);
00324
00325
00326 cpl_matrix_set(ydiff, x , 0, diff);
00327
00328 }
00329
00330 ysigma = sigma * giraffe_matrix_sigma_mean(ydiff, 0.);
00331
00332
00333
00334
00335
00336
00337
00338
00339
00340 k = 0;
00341 for (l = 0; l < cpl_matrix_get_nrow(ydiff); l++) {
00342
00343 if (fabs(cpl_matrix_get(ydiff, l, 0)) <= ysigma) {
00344
00345
00346
00347
00348 cxint xok = cpl_matrix_get(mtmp, l, 0);
00349 cxdouble data = cpl_matrix_get(yraw, 0, l);
00350
00351 cpl_matrix_set(mtmp, k, 0, xok);
00352 cpl_matrix_set(yraw, 0, k, data);
00353
00354 ++k;
00355 }
00356
00357 }
00358
00359
00360
00361
00362
00363
00364 if (k == naccept) {
00365 break;
00366 }
00367
00368
00369
00370
00371
00372 naccept = k;
00373 ratio = (cxdouble) naccept / (cxdouble) ntotal;
00374
00375 GIDEBUG(gi_message("Iteration %d: Sigma %f, accepted bins: %d, "
00376 "rejected %d\n", iteration, ysigma, naccept,
00377 ntotal - naccept));
00378
00379
00380
00381
00382
00383
00384
00385
00386 cpl_matrix_resize(mtmp, 0,
00387 naccept - cpl_matrix_get_nrow(mtmp), 0, 0);
00388 cpl_matrix_resize(yraw, 0,
00389 0, 0, naccept - cpl_matrix_get_ncol(yraw));
00390 cpl_matrix_resize(ydiff, 0,
00391 naccept - cpl_matrix_get_nrow(ydiff), 0, 0);
00392
00393 iteration++;
00394 }
00395
00396 if (coeffs != NULL) {
00397 register cxint l;
00398
00399 for (l = 0; l < cpl_matrix_get_nrow(mcoeff); l++) {
00400 cpl_matrix_set(mcoeff, l, 0, cpl_matrix_get(coeffs, 0, l));
00401 }
00402 }
00403
00404
00405
00406
00407
00408
00409 cpl_matrix_delete(coeffs);
00410 cpl_matrix_delete(ydiff);
00411 cpl_matrix_delete(yraw);
00412 cpl_matrix_delete(mtmp);
00413
00414 return mfit;
00415
00416 }
00417
00418
00419
00420
00421
00422
00423
00424
00425
00426
00427
00428
00429
00430
00431
00432
00433
00434
00435
00436
00437
00438
00439
00440
00441
00442
00443
00444
00445
00446
00447
00448
00449
00450
00451
00452
00453 inline static cxint
00454 _giraffe_build_raw_mask(cpl_image *raw, cpl_image *bpixel, cxint nspectra,
00455 cxdouble noise, GiMaskParameters *config,
00456 cxint *ndetect, cpl_matrix *mxok, cpl_matrix *myup,
00457 cpl_matrix *mylo)
00458 {
00459
00460 register cxint x, y;
00461 register cxint xretry, xok;
00462 register cxint ny;
00463
00464 cxint nrows, ncols;
00465 cxint *yabove = NULL;
00466 cxint *ybelow = NULL;
00467 cxint *good_pixels = NULL;
00468 cxint ywidth = config->ywidth > 1 ? config->ywidth : 2;
00469 cxint ckwidth = config->ckdata.width;
00470 cxint ckheight = config->ckdata.height;
00471 cxint ckcount = config->ckdata.count;
00472
00473
00474 cxdouble *pixels = NULL;
00475
00476 cpl_image *img = raw;
00477
00478
00479 *ndetect = 0;
00480
00481 cpl_msg_info(_task, "Generating mask (%d spectra expected) ...",
00482 nspectra);
00483
00484 GIDEBUG(gi_message("noise = %g start = %d tries = %d xbin = %d "
00485 "ywidth = %d", noise, config->start, config->retry,
00486 config->xbin, ywidth));
00487
00488 pixels = cpl_image_get_data_double(raw);
00489
00490 nrows = cpl_image_get_size_y(raw);
00491 ncols = cpl_image_get_size_x(raw);
00492
00493 if (config->xbin > 1) {
00494
00495 cxdouble *_pixels = NULL;
00496
00497 nrows = (cxint) ceil(nrows / config->xbin);
00498 config->start = (cxint) ceil(config->start / config->xbin);
00499
00500 img = cpl_image_new(ncols, nrows, CPL_TYPE_DOUBLE);
00501 _pixels = cpl_image_get_data_double(img);
00502
00503 for (y = 0; y < ncols; y++) {
00504 for (x = 0; x < nrows; x++) {
00505
00506 register cxint xx, xraw;
00507 register cxint zx = x * config->xbin;
00508
00509
00510 _pixels[x * ncols + y] = 0.;
00511
00512 for (xx = 0, xraw = zx; xx < config->xbin && xraw < nrows;
00513 xx++, xraw++) {
00514 _pixels[x * ncols + y] += pixels[xraw * ncols + y];
00515 }
00516 }
00517 }
00518
00519 pixels = _pixels;
00520
00521 }
00522
00523 good_pixels = cx_calloc(nrows * ncols, sizeof(cxint));
00524
00525 if (config->local) {
00526
00527 cxint ywidth2 = ywidth >> 1;
00528 cxint sz = 2 * ywidth2 + 1;
00529
00530 cpl_vector *ymins = cpl_vector_new(sz);
00531
00532
00533
00534
00535
00536
00537
00538
00539 for (x = 0; x < nrows; x++) {
00540
00541 cpl_vector_fill(ymins, 0.);
00542
00543 for (y = 0; y < ncols; y++) {
00544
00545 register cxint k, kk;
00546
00547 cxdouble value = 0.;
00548 cxdouble bkg = 0.;
00549 cxdouble threshold = 0.;
00550
00551
00552 for (kk = 0, k = -ywidth2; k <= ywidth2; k++) {
00553
00554 register cxint ky = y + k;
00555
00556 if (ky < 0 || ky >= ncols) {
00557 continue;
00558 }
00559
00560 cpl_vector_set(ymins, kk, pixels[x * ncols + ky]);
00561 ++kk;
00562 }
00563
00564 if (kk == 0) {
00565 continue;
00566 }
00567
00568 if (config->threshold > 0.) {
00569
00570 const cxint count = 2;
00571
00572 cxint i = 0;
00573
00574
00575
00576
00577
00578
00579
00580 giraffe_array_sort(cpl_vector_get_data(ymins), kk);
00581
00582 bkg = 0.;
00583
00584 for (i = 0; i < count; i++) {
00585 bkg += cpl_vector_get(ymins, i);
00586 }
00587 bkg /= (cxdouble)count;
00588 threshold = sqrt(noise * noise +
00589 fabs(pixels[x * ncols + y]));
00590
00591 }
00592 else {
00593
00594 register cxint i;
00595 register cxdouble mean = 0.;
00596
00597
00598 for (i = 0; i < kk; i++) {
00599 mean += cpl_vector_get(ymins, i);
00600 }
00601 mean /= kk;
00602
00603 giraffe_array_sort(cpl_vector_get_data(ymins), kk);
00604
00605 bkg = (cpl_vector_get(ymins, 0) +
00606 cpl_vector_get(ymins, 1)) / 2.0;
00607 threshold = mean - bkg;
00608
00609 }
00610
00611
00612
00613
00614
00615
00616 value = pixels[x * ncols + y] - bkg;
00617
00618 if (value < 0.) {
00619 continue;
00620 }
00621
00622 if (value > fabs(config->threshold) * threshold) {
00623 good_pixels[x * ncols + y] = 1;
00624 }
00625 }
00626 }
00627
00628 cpl_vector_delete(ymins);
00629 ymins = NULL;
00630
00631 }
00632 else {
00633
00634 cxdouble threshold = 0.;
00635
00636
00637
00638
00639
00640
00641 if (config->threshold > 0.) {
00642 threshold = config->threshold * noise;
00643 }
00644 else {
00645 cxdouble mean = cpl_image_get_mean(raw);
00646
00647 threshold = -config->threshold * mean * nspectra *
00648 config->wavg / ncols;
00649 }
00650
00651 for (x = 0; x < nrows; x++) {
00652 for (y = 0; y < ncols; y++) {
00653
00654 if (pixels[x * ncols + y] > threshold) {
00655 good_pixels[x * ncols + y] = 1;
00656 }
00657
00658 }
00659 }
00660
00661 }
00662
00663 GIDEBUG(cxint *data = cx_calloc(nrows * ncols, sizeof(cxint));
00664
00665 memcpy(data, good_pixels, nrows * ncols * sizeof(cxint));
00666 cpl_image *gp = cpl_image_wrap_int(ncols, nrows, data);
00667 cpl_image_save(gp, "locmask.fits", 32, NULL, CPL_IO_DEFAULT);
00668 cpl_image_unwrap(gp);
00669 cx_free(data));
00670
00671
00672
00673
00674
00675
00676 yabove = cx_calloc(nspectra + 1, sizeof(cxint));
00677 ybelow = cx_calloc(nspectra + 1, sizeof(cxint));
00678
00679
00680
00681
00682
00683
00684 ny = ncols - 1;
00685
00686 xretry = 0;
00687 xok = 0;
00688
00689 for (x = config->start; (x >= 0) && (xretry <= config->retry); x--) {
00690
00691 register cxint zx = x * ncols;
00692 register cxint nborders = 0;
00693 register cxint nbelow = 0;
00694 register cxint nabove = 0;
00695 register cxint in_spectrum = 0;
00696
00697
00698 for (y = 1; y < ny; y++) {
00699
00700 register cxint tmp = 2 * good_pixels[zx + y];
00701
00702
00703
00704
00705
00706 nborders = CX_MAX(CX_MAX(nborders, nbelow), nabove);
00707
00708 if (nborders > nspectra) {
00709 break;
00710 }
00711
00712
00713
00714
00715
00716
00717
00718 if (good_pixels[zx + y + 1]) {
00719
00720
00721
00722
00723
00724
00725 if ((tmp - good_pixels[zx + y - 1]) == 2) {
00726
00727
00728
00729
00730
00731
00732 if (!in_spectrum) {
00733
00734
00735
00736
00737
00738
00739 ybelow[nbelow++] = y;
00740 in_spectrum = 1;
00741
00742 }
00743
00744 }
00745
00746 }
00747
00748 if (good_pixels[zx + y - 1]) {
00749
00750
00751
00752
00753
00754
00755 if ((tmp - good_pixels[zx + y + 1]) == 2) {
00756
00757
00758
00759
00760
00761
00762 if (in_spectrum) {
00763
00764
00765
00766
00767
00768
00769 yabove[nabove++] = y;
00770 in_spectrum = 0;
00771
00772 }
00773
00774 }
00775
00776 }
00777
00778
00779
00780 if (tmp &&
00781 !good_pixels[zx + y - 1] && !good_pixels[zx + y + 1]) {
00782
00783 if (_giraffe_validate_pixel(good_pixels, ncols, nrows, y, x,
00784 ckwidth, ckheight, ckcount)) {
00785
00786 yabove[nabove++] = y;
00787 ybelow[nbelow++] = y;
00788 }
00789
00790 }
00791
00792 }
00793
00794 if (in_spectrum) {
00795 nborders--;
00796 nbelow--;
00797 in_spectrum = 0;
00798 }
00799
00800 *ndetect = nborders;
00801
00802 if (!in_spectrum && (nbelow == nspectra) && (nbelow == nabove)) {
00803
00804
00805
00806
00807
00808
00809
00810
00811 for (y = 0; y < nspectra; y++) {
00812 cpl_matrix_set(mylo, xok, y, (cxdouble) ybelow[y]);
00813 cpl_matrix_set(myup, xok, y, (cxdouble) yabove[y]);
00814 cpl_matrix_set(mxok, xok, 0, (config->xbin > 1) ?
00815 (cxdouble) (x + 0.5) * config->xbin :
00816 (cxdouble) x);
00817 }
00818 xok++;
00819 xretry = 0;
00820 }
00821 else if (xretry++ < config->retry) {
00822
00823
00824
00825
00826
00827
00828 continue;
00829 }
00830 else {
00831
00832
00833
00834
00835
00836 break;
00837 }
00838 }
00839
00840
00841
00842
00843
00844
00845
00846
00847
00848
00849 xretry = 0;
00850
00851 for (x = config->start + 1; (x < nrows) &&
00852 (xretry <= config->retry); x++) {
00853
00854 register cxint zx = x * ncols;
00855 register cxint nborders = 0;
00856 register cxint nbelow = 0;
00857 register cxint nabove = 0;
00858 register cxint in_spectrum = 0;
00859
00860
00861 for (y = 1; y < ny; y++) {
00862
00863 register cxint tmp = 2 * good_pixels[zx + y];
00864
00865 nborders = CX_MAX(CX_MAX(nborders, nbelow), nabove);
00866
00867 if (nborders > nspectra) {
00868 break;
00869 }
00870
00871 if (good_pixels[zx + y + 1]) {
00872 if ((tmp - good_pixels[zx + y - 1]) == 2) {
00873 if (!in_spectrum) {
00874 ybelow[nbelow++] = y;
00875 in_spectrum = 1;
00876 }
00877 }
00878 }
00879
00880 if (good_pixels[zx + y - 1]) {
00881 if ((tmp - good_pixels[zx + y + 1]) == 2) {
00882 if (in_spectrum) {
00883 yabove[nabove++] = y;
00884 in_spectrum = 0;
00885 }
00886 }
00887 }
00888
00889
00890
00891 if (tmp &&
00892 !good_pixels[zx + y - 1] && !good_pixels[zx + y + 1]) {
00893
00894 if (_giraffe_validate_pixel(good_pixels, ncols, nrows, y, x,
00895 ckwidth, ckheight, ckcount)) {
00896
00897 yabove[nabove++] = y;
00898 ybelow[nbelow++] = y;
00899 }
00900
00901 }
00902
00903 }
00904
00905 if (in_spectrum) {
00906 nborders--;
00907 nbelow--;
00908 in_spectrum = 0;
00909 }
00910
00911 *ndetect = nborders;
00912
00913 if (!in_spectrum && (nbelow == nspectra) && (nbelow == nabove)) {
00914
00915 for (y = 0; y < nspectra; y++) {
00916 cpl_matrix_set(mylo, xok, y, (cxdouble) ybelow[y]);
00917 cpl_matrix_set(myup, xok, y, (cxdouble) yabove[y]);
00918 cpl_matrix_set(mxok, xok, 0, (config->xbin > 1) ?
00919 (cxdouble) (x + 0.5) * config->xbin :
00920 (cxdouble) x);
00921 }
00922 xok++;
00923 xretry = 0;
00924 }
00925 else if (xretry++ < config->retry) {
00926 continue;
00927 }
00928 else {
00929 break;
00930 }
00931
00932 }
00933
00934 cx_free(ybelow);
00935 cx_free(yabove);
00936 cx_free(good_pixels);
00937
00938 if (config->xbin > 1) {
00939 cpl_image_delete(img);
00940 }
00941
00942 if (xok == 0) {
00943 if (*ndetect < nspectra) {
00944 return -1;
00945 }
00946 else if (*ndetect > nspectra) {
00947 return -1;
00948 }
00949 else {
00950 return -2;
00951 }
00952 }
00953 else {
00954 *ndetect = nspectra;
00955 cpl_msg_info(_task, "%d spectra detected in %d wavelength bins",
00956 *ndetect, xok);
00957 }
00958
00959 return xok;
00960
00961 }
00962
00963
00964
00965
00966
00967
00968
00969
00970
00971
00972
00973
00974
00975
00976
00977
00978
00979
00980
00981
00982
00983
00984
00985
00986
00987
00988
00989 inline static void
00990 _giraffe_fit_raw_mask(cpl_matrix *mxok, cpl_matrix *myup, cpl_matrix *mylo,
00991 cpl_table *fibers, GiMaskParameters *config,
00992 GiMaskPosition *position)
00993 {
00994
00995 register cxint nn, x, nspectra;
00996 register cxint nx = cpl_matrix_get_nrow(position->my);
00997 register cxint ns = cpl_table_get_nrow(fibers);
00998
00999 cpl_matrix *mxraw;
01000 cpl_matrix *base;
01001 cpl_matrix *mcoeff;
01002
01003
01004
01005 mxraw = cpl_matrix_new(nx, 1);
01006 mcoeff = cpl_matrix_new(config->ydeg + 1, 1);
01007
01008
01009
01010
01011
01012
01013 for (x = 0; x < nx; x++) {
01014 cpl_matrix_set(mxraw, x, 0, x);
01015 }
01016
01017
01018
01019
01020
01021 base = giraffe_chebyshev_base1d(0., nx, config->ydeg + 1, mxraw);
01022 cpl_matrix_delete(mxraw);
01023
01024 nspectra = 0;
01025 for (nn = 0; nn < ns; nn++) {
01026 cpl_matrix *ylofit = NULL;
01027 cpl_matrix *yupfit = NULL;
01028
01029
01030
01031
01032
01033
01034
01035
01036
01037
01038
01039
01040 ylofit = _giraffe_fit_border(mylo, base, mxok, nspectra,
01041 config->sigma, config->niter,
01042 config->mfrac, mcoeff);
01043 if (ylofit == NULL) {
01044 cpl_msg_warning(_task, "Could not compute low border for "
01045 "spectrum %d", nn);
01046 nspectra++;
01047 continue;
01048 }
01049
01050
01051 yupfit = _giraffe_fit_border(myup, base, mxok, nspectra,
01052 config->sigma, config->niter,
01053 config->mfrac, mcoeff);
01054 if (yupfit == NULL) {
01055 cpl_msg_warning(_task, "Could not compute up border for "
01056 "spectrum %d", nn);
01057 nspectra++;
01058 continue;
01059 }
01060
01061
01062
01063
01064
01065
01066
01067 for (x = 0; x < nx; x++) {
01068
01069 cpl_matrix_set(position->my, x, nn, 0.5 *
01070 (cpl_matrix_get(yupfit, x, 0) +
01071 cpl_matrix_get(ylofit, x, 0)));
01072
01073 cpl_matrix_set(position->my, x, nn, 0.5 *
01074 (cpl_matrix_get(yupfit, x, 0) -
01075 cpl_matrix_get(ylofit, x, 0)) + config->ewid);
01076
01077 }
01078 cpl_matrix_delete(ylofit);
01079 cpl_matrix_delete(yupfit);
01080 nspectra++;
01081
01082 }
01083
01084 cpl_msg_info(_task, "%03d spectrum positions fitted", nspectra);
01085
01086 cpl_matrix_delete(base);
01087 cpl_matrix_delete(mcoeff);
01088
01089 if (nspectra == 0) {
01090 cpl_msg_warning(_task, "could not fit any spectra, check number "
01091 "of good wavelength bins");
01092 return;
01093 }
01094
01095 return;
01096
01097 }
01098
01099
01100
01101
01102
01103
01104
01105
01106
01107
01108
01109
01110
01111
01112
01113
01114
01115
01116
01117
01118
01119
01120
01121
01122
01123
01124
01125
01126
01127
01128
01129
01130
01131
01132
01133
01134 inline static void
01135 _giraffe_fit_raw_centroid(cpl_image* mz, cpl_matrix* mxok, cpl_matrix* myup,
01136 cpl_matrix* mylo, cpl_table* fibers,
01137 GiMaskParameters* config, GiMaskPosition* position,
01138 GiMaskPosition* coeffs)
01139 {
01140
01141 const cxchar* const fctid = "_giraffe_fit_raw_centroid";
01142
01143 register cxint nn = 0;
01144 register cxint x = 0;
01145 register cxint y = 0;
01146 register cxint nspectra = 0;
01147 register cxint nx = cpl_image_get_size_y(mz);
01148 register cxint ny = cpl_image_get_size_x(mz);
01149 register cxint ns = cpl_table_get_nrow(fibers);
01150
01151 cxint yorder = config->ydeg + 1;
01152 cxint worder = config->wdeg + 1;
01153
01154 cpl_matrix* mxraw = NULL;
01155 cpl_matrix* base = NULL;
01156 cpl_matrix* mycenter = NULL;
01157 cpl_matrix* mywidth = NULL;
01158 cpl_matrix* mx = NULL;
01159 cpl_matrix* my = NULL;
01160 cpl_matrix* mw = NULL;
01161 cpl_matrix* chebcoeff = NULL;
01162 cpl_matrix* mfitlocw = NULL;
01163 cpl_matrix* ycenfit = NULL;
01164 cpl_matrix* ycencoeff = NULL;
01165
01166
01167
01168 if (cpl_matrix_get_nrow(position->my) != nx ||
01169 cpl_matrix_get_ncol(position->my) != ns) {
01170 gi_error("%s: invalid size for position->my[%d,%d], expected "
01171 "[%d,%d]", fctid, cpl_matrix_get_nrow(position->my),
01172 cpl_matrix_get_ncol(position->my), nx, ns);
01173 return;
01174 }
01175
01176 if (cpl_matrix_get_nrow(position->mw) != nx ||
01177 cpl_matrix_get_ncol(position->mw) != ns) {
01178 gi_error("%s: invalid size for position->mw[%d,%d], expected "
01179 "[%d,%d]", fctid, cpl_matrix_get_nrow(position->my),
01180 cpl_matrix_get_ncol(position->my), nx, ns);
01181 return;
01182 }
01183
01184
01185
01186
01187
01188
01189 mxraw = cpl_matrix_new(nx, 1);
01190
01191 for (x = 0; x < nx; x++) {
01192 cpl_matrix_set(mxraw, x, 0, x);
01193 }
01194
01195
01196
01197
01198
01199
01200 base = giraffe_chebyshev_base1d(0., nx, yorder, mxraw);
01201 cpl_matrix_delete(mxraw);
01202
01203 mycenter = cpl_matrix_new(cpl_matrix_get_nrow(mxok), ns);
01204 mywidth = cpl_matrix_new(1, cpl_matrix_get_nrow(mxok) * ns);
01205
01206 ycencoeff = cpl_matrix_new(yorder, 1);
01207
01208 for (nn = 0; nn < ns; nn++) {
01209
01210
01211
01212
01213
01214
01215
01216
01217
01218
01219
01220
01221
01222
01223
01224
01225 cxdouble* pixels = cpl_image_get_data_double(mz);
01226
01227 for (x = 0; x < cpl_matrix_get_nrow(mxok); x++) {
01228
01229 register cxint zx = (cxint) cpl_matrix_get(mxok, x, 0);
01230
01231 register cxdouble zz = 0.;
01232 register cxdouble yy = 0.;
01233
01234 cxdouble lower = cpl_matrix_get(mylo, x, nspectra);
01235 cxdouble upper = cpl_matrix_get(myup, x, nspectra);
01236
01237
01238 for (y = (cxint) lower; y <= (cxint) upper; y++) {
01239 yy += pixels[zx * ny + y] * y;
01240 zz += pixels[zx * ny + y];
01241 }
01242
01243 cpl_matrix_set(mycenter, x, nspectra, yy / zz);
01244 cpl_matrix_set(mywidth, 0, x * ns + nspectra, config->ewid +
01245 (upper - lower) / 2.0);
01246
01247 }
01248
01249
01250
01251
01252
01253 cpl_matrix_fill(ycencoeff, 0.);
01254 ycenfit = _giraffe_fit_border(mycenter, base, mxok, nspectra,
01255 config->sigma, config->niter,
01256 config->mfrac, ycencoeff);
01257 if (ycenfit == NULL) {
01258 cpl_msg_warning(_task, "Could not fit centroid for spectrum %d",
01259 nn);
01260 nspectra++;
01261 continue;
01262 }
01263
01264
01265
01266
01267
01268 for (x = 0; x < yorder; x++) {
01269 cpl_matrix_set(coeffs->my, x, nn,
01270 cpl_matrix_get(ycencoeff, x, 0));
01271 }
01272
01273
01274
01275
01276
01277
01278 for (x = 0; x < nx; x++) {
01279 cpl_matrix_set(position->my, x, nn,
01280 cpl_matrix_get(ycenfit, 0, x));
01281 }
01282
01283 cpl_matrix_delete(ycenfit);
01284 nspectra++;
01285
01286 }
01287
01288 GIDEBUG(cpl_image *lycenter = giraffe_matrix_create_image(mycenter);
01289 cpl_image_save(lycenter, "lycenter.fits", -32, NULL,
01290 CPL_IO_DEFAULT);
01291 cpl_image_delete(lycenter);
01292
01293 lycenter = giraffe_matrix_create_image(position->my);
01294 cpl_image_save(lycenter, "lycenterfit.fits", -32, NULL,
01295 CPL_IO_DEFAULT);
01296 cpl_image_delete(lycenter);
01297
01298 cpl_image *lyxok = giraffe_matrix_create_image(mxok);
01299 cpl_image_save(lyxok, "lyxok.fits", -32, NULL,
01300 CPL_IO_DEFAULT);
01301 cpl_image_delete(lyxok));
01302
01303
01304 cpl_msg_info(_task, "%03d spectrum positions fitted", nspectra);
01305
01306 cpl_matrix_delete(base);
01307 cpl_matrix_delete(mycenter);
01308 cpl_matrix_delete(ycencoeff);
01309
01310 if (nspectra == 0) {
01311 cpl_msg_warning(_task, "Could not fit any spectra, check number of "
01312 "good wavelength bins");
01313
01314 cpl_matrix_delete(mywidth);
01315 return;
01316 }
01317
01318
01319
01320
01321
01322 cpl_msg_info(_task, "2D fit (order %dx%d) of mask width", worder,
01323 worder);
01324
01325
01326
01327
01328
01329 mx = cpl_matrix_new(cpl_matrix_get_nrow(mxok) * nspectra, 1);
01330 my = cpl_matrix_new(cpl_matrix_get_nrow(mxok) * nspectra, 1);
01331 mw = cpl_matrix_new(1, cpl_matrix_get_nrow(mxok) * nspectra);
01332
01333 for (y = 0, nn = 0; nn < nspectra; nn++) {
01334
01335
01336
01337
01338
01339
01340
01341
01342
01343
01344
01345 for (x = 0; x < cpl_matrix_get_nrow(mxok); x++) {
01346
01347 register cxint zx = (cxint) cpl_matrix_get(mxok, x, 0);
01348 register cxint lx = x * nspectra + y;
01349
01350
01351 cpl_matrix_set(mx, lx, 0, cpl_matrix_get(mxok, x, 0));
01352 cpl_matrix_set(my, lx, 0, cpl_matrix_get(position->my, zx, nn));
01353 cpl_matrix_set(mw, 0, lx, cpl_matrix_get(mywidth, 0, x * ns + y));
01354 }
01355 y++;
01356 }
01357
01358 base = giraffe_chebyshev_base2d(0., 0., nx, ny, worder, worder, mx, my);
01359
01360 cpl_matrix_delete(my);
01361 cpl_matrix_delete(mx);
01362
01363 chebcoeff = giraffe_matrix_leastsq(base, mw);
01364 cpl_matrix_delete(base);
01365 cpl_matrix_delete(mw);
01366
01367 cpl_matrix_delete(mywidth);
01368
01369 if (chebcoeff == NULL) {
01370 gi_warning("%s: error in giraffe_matrix_leastsq() for width 2D fit",
01371 fctid);
01372 return;
01373 }
01374
01375
01376
01377
01378
01379 for (nn = 0; nn < cpl_matrix_get_ncol(chebcoeff); nn++) {
01380 cpl_matrix_set(coeffs->mw, 0, nn, cpl_matrix_get(chebcoeff, 0, nn));
01381 }
01382
01383
01384
01385
01386
01387 mx = cpl_matrix_new(nx * nspectra, 1);
01388 my = cpl_matrix_new(nx * nspectra, 1);
01389
01390 for (y = 0, nn = 0; nn < nspectra; nn++) {
01391
01392
01393
01394
01395
01396
01397
01398
01399
01400
01401
01402 for (x = 0; x < nx; x++) {
01403
01404 register cxint lx = x * nspectra + y;
01405
01406 cpl_matrix_set(mx, lx, 0, x);
01407 cpl_matrix_set(my, lx, 0, cpl_matrix_get(position->my, x, nn));
01408
01409 }
01410 y++;
01411 }
01412
01413 cpl_matrix_set_size(chebcoeff, worder, worder);
01414
01415 mfitlocw = giraffe_chebyshev_fit2d(0., 0., nx, ny, chebcoeff, mx, my);
01416 cpl_matrix_delete(chebcoeff);
01417
01418 cpl_matrix_delete(my);
01419 cpl_matrix_delete(mx);
01420
01421 for (y = 0, nn = 0; nn < nspectra; nn++) {
01422
01423
01424
01425
01426
01427
01428
01429
01430
01431
01432
01433 for (x = 0; x < nx; x++) {
01434
01435 register cxint lx = x * nspectra + y;
01436
01437 cpl_matrix_set(position->mw, x, nn,
01438 cpl_matrix_get(mfitlocw, lx, 0));
01439
01440 }
01441 y++;
01442 }
01443
01444 cpl_matrix_delete(mfitlocw);
01445
01446 return;
01447
01448 }
01449
01450
01451
01452
01453
01454
01455
01456
01457
01458
01459
01460
01461
01462
01463
01464
01465
01466
01467
01468
01469
01470
01471
01472
01473
01474
01475
01476 inline static cxint
01477 _giraffe_localize_spectra(cpl_image *mzraw, cpl_image *bpixel,
01478 cpl_table *fibers, GiLocalizeMethod method,
01479 cxbool normalize, cxdouble noise,
01480 GiMaskParameters *config, GiMaskPosition *position,
01481 GiMaskPosition *coeffs)
01482 {
01483
01484 cxint n, nn;
01485 cxint nx, ny, nxok;
01486 cxint ndetect, nspectra;
01487 cxint x, y;
01488
01489 cxdouble uplost = 0.;
01490 cxdouble lolost = 0.;
01491 cxdouble avglost = 0.;
01492 cxdouble avgmask = 0.;
01493 cxdouble sigmask = 0.;
01494 cxdouble sigmean = 0.;
01495 cxdouble avgborders = 0.;
01496
01497 cxdouble *_mzraw;
01498
01499 cpl_matrix *mxok;
01500 cpl_matrix *myup;
01501 cpl_matrix *mylo;
01502 cpl_matrix *mwid;
01503
01504 cpl_image *mz = NULL;
01505 cpl_image *mznorm = NULL;
01506
01507
01508
01509 nx = cpl_image_get_size_y(mzraw);
01510 ny = cpl_image_get_size_x(mzraw);
01511 _mzraw = cpl_image_get_data_double(mzraw);
01512
01513
01514 if (normalize == TRUE) {
01515
01516 cxdouble zxmax = 0.0;
01517 cxdouble *_mzx = NULL;
01518 cxdouble *_mznorm = NULL;
01519
01520 cpl_image *mzx = NULL;
01521
01522
01523 cpl_msg_info(_task, "Using normalized spectra for localization");
01524
01525
01526
01527
01528
01529
01530
01531
01532 mznorm = cpl_image_new(ny, nx, CPL_TYPE_DOUBLE);
01533 _mznorm = cpl_image_get_data_double(mznorm);
01534
01535 mzx = cpl_image_new(1, nx, CPL_TYPE_DOUBLE);
01536 _mzx = cpl_image_get_data_double(mzx);
01537
01538
01539
01540
01541
01542
01543 for (x = 0 ; x < nx; x++) {
01544 for (y = 0 ; y < ny; y++) {
01545 _mzx[x] += _mzraw[x * ny + y];
01546 }
01547
01548
01549
01550
01551
01552 if (_mzx[x] > zxmax) {
01553 zxmax = _mzx[x];
01554 }
01555 }
01556
01557 GIDEBUG(cpl_image_save(mzx, "mzx.fits", -32, NULL, CPL_IO_DEFAULT));
01558
01559 for (x = 0 ; x < nx; x++) {
01560
01561 register cxdouble zxnorm = zxmax / _mzx[x];
01562
01563 for (y = 0 ; y < ny; y++) {
01564 _mznorm[x * ny + y] = _mzraw[x * ny + y] * zxnorm;
01565 }
01566
01567 }
01568
01569 cpl_image_delete(mzx);
01570 mz = mznorm;
01571 }
01572 else {
01573
01574
01575
01576
01577
01578 cpl_msg_info(_task, "Using raw spectra for localization");
01579 mz = mzraw;
01580 }
01581
01582
01583
01584
01585
01586
01587 nspectra = cpl_table_get_nrow(fibers);
01588
01589 mxok = cpl_matrix_new(nx, 1);
01590 myup = cpl_matrix_new(nx, nspectra);
01591 mylo = cpl_matrix_new(nx, nspectra);
01592
01593 config->xbin = CX_MAX(config->xbin, 0);
01594 config->xbin = 2 * (config->xbin / 2);
01595
01596 GIDEBUG(cpl_image_save(mz, "mz.fits", -32, NULL, CPL_IO_DEFAULT));
01597
01598 nxok = _giraffe_build_raw_mask(mz, bpixel, nspectra, noise, config,
01599 &ndetect, mxok, myup, mylo);
01600
01601 if (nxok < 0) {
01602 switch (nxok) {
01603 case -1:
01604 cpl_msg_warning(_task, "Invalid number of spectra detected: "
01605 "%d != %d", ndetect, nspectra);
01606 break;
01607
01608 case -2:
01609 cpl_msg_warning(_task, "No abcissa with good number "
01610 "of spectra");
01611 break;
01612
01613 default:
01614 cpl_msg_warning(_task, "Error while searching for spectra");
01615 }
01616
01617 return nxok;
01618 }
01619
01620
01621
01622
01623
01624
01625 cpl_matrix_resize(mxok, 0, nxok - cpl_matrix_get_nrow(mxok), 0, 0);
01626 cpl_matrix_resize(myup, 0, nxok - cpl_matrix_get_nrow(myup), 0, 0);
01627 cpl_matrix_resize(mylo, 0, nxok - cpl_matrix_get_nrow(mylo), 0, 0);
01628
01629 GIDEBUG(gi_message("%s: mxok[0-%d]=[%g-%g]", __func__,
01630 cpl_matrix_get_nrow(mxok) - 1,
01631 cpl_matrix_get_min(mxok),
01632 cpl_matrix_get_max(mxok)));
01633
01634
01635 cpl_msg_info(_task, "Computing spectrum positions and widths in "
01636 "pixel range [%g,%g]", cpl_matrix_get_min(mxok),
01637 cpl_matrix_get_max(mxok));
01638
01639 if (cpl_matrix_get_nrow(mxok) <= config->ydeg) {
01640 cpl_msg_info(_task, "Not enough data points %d for %d order fit",
01641 cpl_matrix_get_nrow(mxok), config->ydeg);
01642
01643 return -1;
01644 }
01645
01646 switch (method) {
01647 case GILOCALIZE_HALF_WIDTH:
01648 cpl_msg_info(_task, "Using half-width for localization");
01649 _giraffe_fit_raw_mask(mxok, myup, mylo, fibers, config,
01650 position);
01651 break;
01652
01653 case GILOCALIZE_BARYCENTER:
01654 default:
01655 cpl_msg_info(_task, "Using barycenter for localization");
01656 _giraffe_fit_raw_centroid(mz, mxok, myup, mylo, fibers, config,
01657 position, coeffs);
01658 break;
01659 }
01660
01661 if (normalize == 1) {
01662 cpl_image_delete(mznorm);
01663 }
01664
01665
01666
01667
01668
01669
01670
01671
01672
01673
01674
01675 mwid = cpl_matrix_new(nx, nspectra);
01676
01677 for (n = 0, nn = 0; nn < cpl_table_get_nrow(fibers); nn++) {
01678
01679 for (x = 0; x < nxok; x++) {
01680 register cxint lx = (cxint) cpl_matrix_get(mxok, x, 0);
01681
01682 cxdouble lower = cpl_matrix_get(mylo, x, n);
01683 cxdouble upper = cpl_matrix_get(myup, x, n);
01684 cxdouble width = cpl_matrix_get(position->mw, lx, nn);
01685
01686 uplost += cpl_matrix_get(position->my, lx, nn) + width - upper;
01687 lolost += cpl_matrix_get(position->my, lx, nn) - width - lower;
01688
01689 avgborders += upper - lower;
01690 avgmask += width;
01691
01692 cpl_matrix_set(mwid, x, n, 2. * width);
01693 }
01694 n++;
01695 }
01696
01697 sigmean = cpl_matrix_get_mean(mwid);
01698 sigmask = giraffe_matrix_sigma_mean(mwid, sigmean);
01699 avglost = (lolost + uplost) / (nspectra * nxok);
01700 avgmask = 2.0 * avgmask / nspectra;
01701
01702 cpl_msg_info(_task, "Mask was computed using %d of %d wavelength bins",
01703 nxok, nx);
01704 cpl_msg_info(_task, "Average # of pixels per spectra: %.4g",
01705 avgmask);
01706 cpl_msg_info(_task, "Average # of in-borders pixels per spectra: %.4g",
01707 avgborders / nspectra);
01708 cpl_msg_info(_task, "Average lost pixels per spectra: %.4g",
01709 avglost);
01710 cpl_msg_info(_task, "Average lost pixels at upper border: %.4g",
01711 uplost / (nspectra * nxok));
01712 cpl_msg_info(_task, "Average lost pixels at lower border: %.4g",
01713 lolost / (nspectra * nxok));
01714 cpl_msg_info(_task, "Average spectrum width: %.4g +/- %.4g, "
01715 "(min, max) = (%.4g, %.4g)", sigmean, sigmask,
01716 cpl_matrix_get_min(mwid), cpl_matrix_get_max(mwid));
01717
01718 cpl_matrix_delete(mwid);
01719
01720 cpl_matrix_delete(mylo);
01721 cpl_matrix_delete(myup);
01722 cpl_matrix_delete(mxok);
01723
01724 return 0;
01725
01726 }
01727
01728
01729 inline static cxint
01730 _giraffe_finalize_fibers(cpl_table *fibers, cpl_matrix *locy, GiImage *mlocy,
01731 cxdouble maxoffset)
01732 {
01733
01734 cxint i = 0;
01735 cxint j = 0;
01736 cxint nx = 0;
01737 cxint ny = 0;
01738 cxint _nx = 0;
01739 cxint _ny = 0;
01740 cxint nfibers = 0;
01741 cxint irow = 0;
01742
01743 cxdouble *positions = NULL;
01744
01745 cpl_image *_mlocy = NULL;
01746
01747
01748 if (fibers == NULL || locy == NULL || mlocy == NULL) {
01749 return -1;
01750 }
01751
01752 if (cpl_table_has_column(fibers, "RINDEX") == FALSE) {
01753 return -1;
01754 }
01755
01756 nx = cpl_matrix_get_ncol(locy);
01757 ny = cpl_matrix_get_nrow(locy);
01758
01759 nfibers = cpl_table_get_nrow(fibers);
01760
01761 _mlocy = giraffe_image_get(mlocy);
01762 _nx = cpl_image_get_size_x(_mlocy);
01763 _ny = cpl_image_get_size_y(_mlocy);
01764
01765 if (nfibers > _nx || ny != _ny) {
01766 return -2;
01767 }
01768
01769 cpl_table_select_all(fibers);
01770
01771
01772
01773
01774
01775
01776 irow = (_ny - 1) / 2;
01777 positions = (cxdouble *)cpl_image_get_data(_mlocy) + irow * _nx;
01778
01779
01780
01781
01782
01783
01784
01785
01786
01787
01788 for (i = 0; i < nfibers; i++) {
01789
01790 if (j < nx) {
01791
01792 cxint pos = cpl_table_get_int(fibers, "RINDEX", i, NULL) - 1;
01793
01794 cxdouble yc = cpl_matrix_get(locy, irow, j);
01795
01796 if (fabs(yc - positions[pos]) <= maxoffset) {
01797 cpl_table_unselect_row(fibers, i);
01798 ++j;
01799 }
01800
01801 }
01802 }
01803
01804 cpl_table_erase_selected(fibers);
01805
01806 return 0;
01807
01808 }
01809
01810
01839 cxint
01840 giraffe_localize_spectra(GiLocalization *result, GiImage *image,
01841 GiTable *fibers, GiLocalization *master,
01842 GiImage *badpixels, GiLocalizeConfig *config)
01843 {
01844
01845 const cxchar *fctid = "giraffe_localize_spectra";
01846
01847 cxint i;
01848 cxint status;
01849 cxint nrows;
01850 cxint nfibers;
01851 cxint ckwidth;
01852 cxint ckheight;
01853 cxint ckcount;
01854
01855 cxdouble mwidth;
01856 cxdouble sigma;
01857
01858 cx_string *pname;
01859
01860 cpl_propertylist *properties;
01861
01862 cpl_image *_image = giraffe_image_get(image);
01863 cpl_image *_bpixel = giraffe_image_get(badpixels);
01864 cpl_image *_result = NULL;
01865
01866 cpl_matrix *_my;
01867
01868 cpl_table *_fibers = NULL;
01869 cpl_table *fiber_setup = NULL;
01870 cpl_table *locc;
01871
01872 GiLocalizeMethod method;
01873
01874 GiInstrumentMode mode;
01875
01876 GiMaskParameters mask_config;
01877
01878 GiMaskPosition mask_position;
01879 GiMaskPosition mask_coeffs;
01880
01881
01882
01883
01884
01885
01886
01887 if (result == NULL || image == NULL || fibers == NULL || config == NULL) {
01888 cpl_error_set(fctid, CPL_ERROR_NULL_INPUT);
01889 return 1;
01890 }
01891
01892 if (badpixels != NULL) {
01893 #if 1
01894 cpl_msg_debug(fctid,"Bad pixel correction is not available. Bad "
01895 "pixel map will be ignored.");
01896 #else
01897 gi_warning("%s: Bad pixel correction is not available. Bad "
01898 "pixel map at %p will be ignored.", fctid, badpixels);
01899 #endif
01900 }
01901
01902 _fibers = giraffe_table_get(fibers);
01903
01904 if (_fibers == NULL) {
01905 cpl_error_set(fctid, CPL_ERROR_DATA_NOT_FOUND);
01906 return 1;
01907 }
01908 else {
01909 fiber_setup = _fibers;
01910 }
01911
01912 properties = giraffe_image_get_properties(image);
01913
01914
01915
01916
01917
01918
01919 nfibers = cpl_table_get_nrow(_fibers);
01920
01921 cpl_msg_info(fctid, "Setting number of fibers (%s) to %d",
01922 GIALIAS_NFIBERS, nfibers);
01923
01924 cpl_propertylist_update_int(properties, GIALIAS_NFIBERS, nfibers);
01925 cpl_propertylist_set_comment(properties, GIALIAS_NFIBERS,
01926 "Number of fibres");
01927
01928
01929
01930
01931
01932
01933 if (config->ron > 0.) {
01934 cpl_msg_info(fctid, "Setting bias sigma value (%s) to %.5g",
01935 GIALIAS_BIASSIGMA, config->ron);
01936 cpl_propertylist_update_double(properties, GIALIAS_BIASSIGMA,
01937 config->ron);
01938 }
01939
01940
01941 sigma = cpl_propertylist_get_double(properties, GIALIAS_BIASSIGMA);
01942
01943 if (cpl_propertylist_has(properties, GIALIAS_CONAD)) {
01944
01945 cxdouble conad = cpl_propertylist_get_double(properties,
01946 GIALIAS_CONAD);
01947
01948
01949 if (conad > 0.) {
01950 sigma *= conad;
01951 }
01952 else {
01953 cpl_msg_error(fctid, "Invalid conversion factor "
01954 "(ADU to e-): %.2g", conad);
01955 return 1;
01956 }
01957
01958 }
01959
01960 cpl_msg_info(fctid, "Bias sigma value: %.3g e-", sigma);
01961
01962 if (config->noise > 0.) {
01963 cpl_msg_info(fctid, "Noise multiplier: %.3g",
01964 config->noise);
01965 }
01966 else {
01967 cpl_msg_info(fctid, "Threshold multiplier: %.3g",
01968 fabs(config->noise));
01969 }
01970
01971
01972
01973
01974
01975
01976 nrows = cpl_image_get_size_y(_image);
01977
01978 if (config->start < 0) {
01979 config->start = nrows / 2;
01980 }
01981
01982
01983
01984
01985
01986
01987 mode = giraffe_get_mode(properties);
01988
01989 if (config->ywidth < 1) {
01990
01991 cpl_msg_info(fctid, "Configuring equilizing filter width from "
01992 "instrument mode");
01993
01994 switch (mode) {
01995 case GIMODE_MEDUSA:
01996 config->ywidth = 16;
01997 break;
01998
01999 case GIMODE_IFU:
02000 config->ywidth = 6;
02001 break;
02002
02003 case GIMODE_ARGUS:
02004 config->ywidth = 6;
02005 break;
02006
02007 default:
02008 cpl_msg_error(fctid, "Invalid instrument mode!");
02009 return 1;
02010 break;
02011 }
02012
02013
02014 if (!cpl_propertylist_has(properties, GIALIAS_SLITNAME)) {
02015 cpl_msg_error(fctid, "Property (%s) not found in raw image",
02016 GIALIAS_SLITNAME);
02017 return 1;
02018 }
02019 else {
02020 const cxchar *slit =
02021 cpl_propertylist_get_string(properties, GIALIAS_SLITNAME);
02022
02023 cpl_msg_info(fctid, "Setting equilizing filter to %d [pxl] "
02024 "for slit configuration `%s'", config->ywidth,
02025 slit);
02026 }
02027
02028 }
02029
02030
02031
02032
02033
02034
02035 switch (mode) {
02036 case GIMODE_MEDUSA:
02037 mwidth = GISPECTRUM_MWIDTH_MEDUSA;
02038
02039 ckwidth = 1;
02040 ckheight = 3;
02041 ckcount = 8;
02042
02043 break;
02044
02045 case GIMODE_IFU:
02046 mwidth = GISPECTRUM_MWIDTH_IFU;
02047
02048 ckwidth = 0;
02049 ckheight = 3;
02050 ckcount = 4;
02051
02052 break;
02053
02054 case GIMODE_ARGUS:
02055 mwidth = GISPECTRUM_MWIDTH_IFU;
02056
02057 ckwidth = 0;
02058 ckheight = 3;
02059 ckcount = 4;
02060
02061 break;
02062
02063 default:
02064 cpl_msg_error(fctid, "Invalid instrument mode!");
02065 return 1;
02066 break;
02067 }
02068
02069
02070
02071
02072
02073
02074 if (config->centroid == TRUE) {
02075 method = GILOCALIZE_BARYCENTER;
02076 }
02077 else {
02078 method = GILOCALIZE_HALF_WIDTH;
02079 }
02080
02081
02082
02083
02084
02085
02086
02087 mask_config.ywidth = config->ywidth;
02088 mask_config.local = config->local;
02089 mask_config.threshold = config->noise;
02090 mask_config.ydeg = config->yorder;
02091 mask_config.wdeg = config->worder;
02092 mask_config.ewid = config->ewidth;
02093 mask_config.wavg = mwidth;
02094 mask_config.ckdata.width = ckwidth;
02095 mask_config.ckdata.height = ckheight;
02096 mask_config.ckdata.count = ckcount;
02097 mask_config.sigma = config->sigma;
02098 mask_config.niter = config->iterations;
02099 mask_config.mfrac = config->fraction;
02100 mask_config.start = config->start;
02101 mask_config.retry = config->retries;
02102 mask_config.xbin = config->binsize;
02103
02104
02105
02106
02107
02108
02109
02110
02111
02112
02113
02114
02115 if (config->full != TRUE) {
02116
02117 cpl_msg_info(fctid, "Computing spectrum localization using SIWC "
02118 "spectra");
02119
02120 if (!master || !master->locy || !master->locy) {
02121 cpl_msg_error(fctid, "Required full master localization is "
02122 "missing!");
02123 return 1;
02124 }
02125
02126
02127
02128
02129
02130
02131
02132 cpl_table_unselect_all(_fibers);
02133 cpl_table_or_selected_int(_fibers, "RP", CPL_EQUAL_TO, -1);
02134
02135 fiber_setup = cpl_table_extract_selected(_fibers);
02136 nfibers = cpl_table_get_nrow(fiber_setup);
02137
02138 }
02139
02140
02141
02142
02143
02144
02145
02146 mask_position.type = GIMASK_FITTED_DATA;
02147 mask_position.my = cpl_matrix_new(nrows, nfibers);
02148 mask_position.mw = cpl_matrix_new(nrows, nfibers);
02149
02150 mask_coeffs.type = GIMASK_FIT_COEFFS;
02151 mask_coeffs.my = cpl_matrix_new(mask_config.ydeg + 1, nfibers);
02152 mask_coeffs.mw = cpl_matrix_new(1, (mask_config.wdeg + 1) *
02153 (mask_config.wdeg + 1));
02154
02155
02156
02157
02158
02159
02160 status = _giraffe_localize_spectra(_image, _bpixel, fiber_setup,
02161 method, config->normalize, sigma,
02162 &mask_config, &mask_position,
02163 &mask_coeffs);
02164
02165 if (status) {
02166 result->locy = NULL;
02167 result->locw = NULL;
02168 result->locc = NULL;
02169 result->psf = NULL;
02170
02171 cpl_matrix_delete(mask_position.my);
02172 cpl_matrix_delete(mask_position.mw);
02173
02174 cpl_matrix_delete(mask_coeffs.my);
02175 cpl_matrix_delete(mask_coeffs.mw);
02176
02177 if (config->full != TRUE) {
02178 cpl_table_delete(fiber_setup);
02179 }
02180
02181 cpl_msg_error(fctid, "Spectrum localization computation failed!");
02182
02183 return 1;
02184 }
02185
02186
02187
02188
02189
02190
02191 if (config->full != TRUE) {
02192
02193
02194
02195
02196
02197
02198 cpl_table_delete(fiber_setup);
02199
02200 }
02201 else {
02202
02203 if (master != NULL && master->locy != NULL) {
02204
02205 cxint nf = cpl_table_get_nrow(_fibers);
02206
02207 cxdouble maxoffset = 0.5 * mask_config.wavg;
02208
02209
02210 cpl_msg_info(fctid, "Comparing detected and expected fiber "
02211 "positions.");
02212
02213 status = _giraffe_finalize_fibers(_fibers, mask_position.my,
02214 master->locy, maxoffset);
02215
02216 if (status != 0) {
02217 result->locy = NULL;
02218 result->locw = NULL;
02219 result->locc = NULL;
02220 result->psf = NULL;
02221
02222 cpl_matrix_delete(mask_position.my);
02223 cpl_matrix_delete(mask_position.mw);
02224
02225 cpl_matrix_delete(mask_coeffs.my);
02226 cpl_matrix_delete(mask_coeffs.mw);
02227
02228 if (config->full != TRUE) {
02229 cpl_table_delete(fiber_setup);
02230 }
02231
02232 cpl_msg_error(fctid, "Comparison of fiber positions "
02233 "failed!");
02234
02235 return 1;
02236 }
02237
02238 cx_assert(cpl_table_get_nrow(_fibers) <= nf);
02239
02240 cpl_msg_info(fctid, "%d of %d expected fibers were detected.",
02241 cpl_table_get_nrow(_fibers), nf);
02242
02243 if (cpl_table_get_nrow(_fibers) < nf) {
02244 cpl_msg_warning(fctid, "%d fibers are missing!",
02245 nf - cpl_table_get_nrow(_fibers));
02246 }
02247
02248 }
02249
02250 }
02251
02252
02253
02254
02255
02256
02257
02258
02259
02260 result->locy =
02261 giraffe_image_create(CPL_TYPE_DOUBLE,
02262 cpl_matrix_get_ncol(mask_position.my),
02263 cpl_matrix_get_nrow(mask_position.my));
02264
02265 giraffe_image_copy_matrix(result->locy, mask_position.my);
02266 cpl_matrix_delete(mask_position.my);
02267
02268 giraffe_image_set_properties(result->locy, properties);
02269 properties = giraffe_image_get_properties(result->locy);
02270
02271 _result = giraffe_image_get(result->locy);
02272
02273 cpl_propertylist_set_int(properties, GIALIAS_NAXIS1,
02274 cpl_image_get_size_x(_result));
02275 cpl_propertylist_set_int(properties, GIALIAS_NAXIS2,
02276 cpl_image_get_size_y(_result));
02277 cpl_propertylist_set_int(properties, GIALIAS_BITPIX, -32);
02278 cpl_propertylist_set_double(properties, GIALIAS_BZERO, 0.);
02279 cpl_propertylist_set_double(properties, GIALIAS_BSCALE, 1.);
02280
02281 cpl_propertylist_append_int(properties, GIALIAS_LOCNX,
02282 cpl_image_get_size_y(_result));
02283 cpl_propertylist_append_int(properties, GIALIAS_LOCNS,
02284 cpl_image_get_size_x(_result));
02285
02286 if (config->centroid) {
02287 cpl_propertylist_append_string(properties, GIALIAS_LMETHOD,
02288 "BARYCENTER");
02289 }
02290 else {
02291 cpl_propertylist_append_string(properties, GIALIAS_LMETHOD,
02292 "HALF_WIDTH");
02293 }
02294
02295 if (config->normalize) {
02296 cpl_propertylist_append_int(properties, GIALIAS_LNORMALIZE,
02297 config->ywidth);
02298 }
02299 else {
02300 cpl_propertylist_append_int(properties, GIALIAS_LNORMALIZE,
02301 -config->ywidth);
02302 }
02303
02304 cpl_propertylist_append_bool(properties, GIALIAS_LFULLLOC, config->full);
02305 cpl_propertylist_append_int(properties, GIALIAS_LOCYDEG, config->yorder);
02306 cpl_propertylist_append_int(properties, GIALIAS_LOCWDEG, config->worder);
02307 cpl_propertylist_append_double(properties, GIALIAS_LEXTRAWID,
02308 config->ewidth);
02309 cpl_propertylist_append_double(properties, GIALIAS_LNOISEMULT,
02310 config->noise);
02311
02312 cpl_propertylist_append_double(properties, GIALIAS_LCLIPSIGMA,
02313 config->sigma);
02314 cpl_propertylist_append_int(properties, GIALIAS_LCLIPNITER,
02315 config->iterations);
02316 cpl_propertylist_append_double(properties, GIALIAS_LCLIPMFRAC,
02317 config->fraction);
02318
02319
02320 if (cpl_propertylist_has(properties, GIALIAS_GIRFTYPE)) {
02321 cpl_propertylist_set_string(properties, GIALIAS_GIRFTYPE, "LOCY");
02322 }
02323 else {
02324 cpl_propertylist_append_string(properties, GIALIAS_GIRFTYPE, "LOCY");
02325 }
02326 cpl_propertylist_set_comment(properties, GIALIAS_GIRFTYPE, "GIRAFFE "
02327 "localization centroid");
02328
02329
02330
02331
02332 result->locw =
02333 giraffe_image_create(CPL_TYPE_DOUBLE,
02334 cpl_matrix_get_ncol(mask_position.mw),
02335 cpl_matrix_get_nrow(mask_position.mw));
02336
02337 giraffe_image_copy_matrix(result->locw, mask_position.mw);
02338 cpl_matrix_delete(mask_position.mw);
02339
02340 giraffe_image_set_properties(result->locw, properties);
02341 properties = giraffe_image_get_properties(result->locw);
02342
02343 _result = giraffe_image_get(result->locw);
02344
02345 cpl_propertylist_set_int(properties, GIALIAS_NAXIS1,
02346 cpl_image_get_size_x(_result));
02347 cpl_propertylist_set_int(properties, GIALIAS_NAXIS2,
02348 cpl_image_get_size_y(_result));
02349
02350 if (cpl_propertylist_has(properties, GIALIAS_GIRFTYPE)) {
02351 cpl_propertylist_set_string(properties, GIALIAS_GIRFTYPE,
02352 "LOCWY");
02353 }
02354 else {
02355 cpl_propertylist_append_string(properties, GIALIAS_GIRFTYPE,
02356 "LOCWY");
02357 }
02358 cpl_propertylist_set_comment(properties, GIALIAS_GIRFTYPE, "GIRAFFE "
02359 "localization half-width");
02360
02361
02362
02363
02364 locc = cpl_table_new(cpl_matrix_get_ncol(mask_coeffs.my));
02365
02366 cpl_table_new_column(locc, "BUTTON", CPL_TYPE_INT);
02367 for (i = 0; i < cpl_table_get_nrow(locc); i++) {
02368 cpl_table_set_int(locc, "BUTTON", i, i);
02369 }
02370
02371 for (i = 0; i < cpl_matrix_get_nrow(mask_coeffs.my); i++) {
02372 cxchar *label = NULL;
02373
02374 cx_asprintf(&label, "YC%d", i);
02375 cpl_table_new_column(locc, label, CPL_TYPE_DOUBLE);
02376 cx_free(label);
02377 }
02378
02379
02380 result->locc = giraffe_table_create(locc, properties);
02381 cpl_table_delete(locc);
02382
02383 _my = cpl_matrix_transpose_create(mask_coeffs.my);
02384 giraffe_table_copy_matrix(result->locc, "YC0", _my);
02385 cpl_matrix_delete(_my);
02386 cpl_matrix_delete(mask_coeffs.my);
02387
02388 properties = giraffe_table_get_properties(result->locc);
02389
02390
02391
02392
02393 pname = cx_string_new();
02394
02395 for (i = 0; i < cpl_matrix_get_ncol(mask_coeffs.mw); i++) {
02396 cx_string_sprintf(pname, "%s%d", GIALIAS_LOCWIDCOEF, i);
02397 cpl_propertylist_append_double(properties, cx_string_get(pname),
02398 cpl_matrix_get(mask_coeffs.mw, 0, i));
02399 }
02400
02401 cx_string_delete(pname);
02402 cpl_matrix_delete(mask_coeffs.mw);
02403
02404 cpl_propertylist_update_string(properties, GIALIAS_GIRFTYPE,
02405 "LOCYWCHEB");
02406 cpl_propertylist_set_comment(properties, GIALIAS_GIRFTYPE, "GIRAFFE "
02407 "localization fit coefficients");
02408
02409
02410
02411
02412 result->psf = NULL;
02413
02414 return 0;
02415
02416 }
02417
02418
02429 GiLocalizeConfig *
02430 giraffe_localize_config_create(cpl_parameterlist *list)
02431 {
02432
02433 const cxchar *s;
02434 cpl_parameter *p;
02435
02436 GiLocalizeConfig *config = NULL;
02437
02438
02439 if (list == NULL) {
02440 return NULL;
02441 }
02442
02443 config = cx_calloc(1, sizeof *config);
02444
02445
02446
02447
02448
02449
02450 config->full = TRUE;
02451 config->centroid = TRUE;
02452 config->local = TRUE;
02453
02454
02455 p = cpl_parameterlist_find(list, "giraffe.localization.mode");
02456 s = cpl_parameter_get_string(p);
02457 if (strcmp(s, "siwc") == 0) {
02458 config->full = FALSE;
02459 }
02460
02461 p = cpl_parameterlist_find(list, "giraffe.localization.start");
02462 config->start = cpl_parameter_get_int(p);
02463
02464 p = cpl_parameterlist_find(list, "giraffe.localization.retries");
02465 config->retries = cpl_parameter_get_int(p);
02466
02467 p = cpl_parameterlist_find(list, "giraffe.localization.binsize");
02468 config->binsize = cpl_parameter_get_int(p);
02469
02470 p = cpl_parameterlist_find(list, "giraffe.localization.ewidth");
02471 config->ewidth = cpl_parameter_get_double(p);
02472
02473 p = cpl_parameterlist_find(list, "giraffe.localization.ywidth");
02474 config->ywidth = cpl_parameter_get_int(p);
02475
02476 p = cpl_parameterlist_find(list, "giraffe.localization.center");
02477 s = cpl_parameter_get_string(p);
02478 if (!strcmp(s, "hwidth")) {
02479 config->centroid = FALSE;
02480 }
02481
02482 p = cpl_parameterlist_find(list, "giraffe.localization.normalize");
02483 config->normalize = cpl_parameter_get_bool(p);
02484
02485 p = cpl_parameterlist_find(list, "giraffe.localization.threshold");
02486 s = cpl_parameter_get_string(p);
02487 if (!strcmp(s, "global")) {
02488 config->local = FALSE;
02489 }
02490
02491 p = cpl_parameterlist_find(list, "giraffe.localization.noise");
02492 config->noise = cpl_parameter_get_double(p);
02493
02494 p = cpl_parameterlist_find(list, "giraffe.localization.ron");
02495 config->ron = cpl_parameter_get_double(p);
02496
02497 p = cpl_parameterlist_find(list, "giraffe.localization.yorder");
02498 config->yorder = cpl_parameter_get_int(p);
02499
02500 p = cpl_parameterlist_find(list, "giraffe.localization.worder");
02501 config->worder = cpl_parameter_get_int(p);
02502
02503 p = cpl_parameterlist_find(list, "giraffe.localization.sigma");
02504 config->sigma = cpl_parameter_get_double(p);
02505
02506 p = cpl_parameterlist_find(list, "giraffe.localization.iterations");
02507 config->iterations = cpl_parameter_get_int(p);
02508
02509 p = cpl_parameterlist_find(list, "giraffe.localization.fraction");
02510 config->fraction = cpl_parameter_get_double(p);
02511
02512 return config;
02513
02514 }
02515
02516
02529 void
02530 giraffe_localize_config_destroy(GiLocalizeConfig *config)
02531 {
02532
02533 if (config) {
02534 cx_free(config);
02535 }
02536
02537 return;
02538
02539 }
02540
02541
02553 void
02554 giraffe_localize_config_add(cpl_parameterlist *list)
02555 {
02556
02557 cpl_parameter *p;
02558
02559
02560 if (list == NULL) {
02561 return;
02562 }
02563
02564 p = cpl_parameter_new_enum("giraffe.localization.mode",
02565 CPL_TYPE_STRING,
02566 "Localization mode: Use all spectra "
02567 "or the 5 SIWC spectra",
02568 "giraffe.localization",
02569 "all", 2, "all", "siwc");
02570 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "sloc-mode");
02571 cpl_parameterlist_append(list, p);
02572
02573
02574 p = cpl_parameter_new_value("giraffe.localization.start",
02575 CPL_TYPE_INT,
02576 "Bin along x-axis",
02577 "giraffe.localization",
02578 -1);
02579 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "sloc-start");
02580 cpl_parameterlist_append(list, p);
02581
02582
02583 p = cpl_parameter_new_value("giraffe.localization.retries",
02584 CPL_TYPE_INT,
02585 "Initial localization detection "
02586 "xbin retries.",
02587 "giraffe.localization",
02588 10);
02589 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "sloc-retries");
02590 cpl_parameterlist_append(list, p);
02591
02592
02593 p = cpl_parameter_new_value("giraffe.localization.binsize",
02594 CPL_TYPE_INT,
02595 "Initial localization detection "
02596 "xbin size.",
02597 "giraffe.localization",
02598 -1);
02599 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "sloc-binsize");
02600 cpl_parameterlist_append(list, p);
02601
02602
02603 p = cpl_parameter_new_value("giraffe.localization.ewidth",
02604 CPL_TYPE_DOUBLE,
02605 "Localization detection extra width.",
02606 "giraffe.localization",
02607 1.0);
02608 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "sloc-ewidth");
02609 cpl_parameterlist_append(list, p);
02610
02611
02612 p = cpl_parameter_new_value("giraffe.localization.ywidth",
02613 CPL_TYPE_INT,
02614 "Full width [pxl] of the equilizing "
02615 "filter (distance between two "
02616 "adjacent fibers).",
02617 "giraffe.localization",
02618 -1);
02619 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "sloc-ywidth");
02620 cpl_parameterlist_append(list, p);
02621
02622
02623 p = cpl_parameter_new_enum("giraffe.localization.center",
02624 CPL_TYPE_STRING,
02625 "Method used for mask center "
02626 "computation.",
02627 "giraffe.localization",
02628 "centroid", 2, "centroid",
02629 "hwidth");
02630 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "sloc-center");
02631 cpl_parameterlist_append(list, p);
02632
02633
02634 p = cpl_parameter_new_value("giraffe.localization.normalize",
02635 CPL_TYPE_BOOL,
02636 "Enable spectrum normalization along "
02637 "the dispersion axis.",
02638 "giraffe.localization",
02639 FALSE);
02640 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "sloc-norm");
02641 cpl_parameterlist_append(list, p);
02642
02643
02644 p = cpl_parameter_new_value("giraffe.localization.noise",
02645 CPL_TYPE_DOUBLE,
02646 "Threshold multiplier.",
02647 "giraffe.localization",
02648 5.0);
02649 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "sloc-noise");
02650 cpl_parameterlist_append(list, p);
02651
02652
02653 p = cpl_parameter_new_enum("giraffe.localization.threshold",
02654 CPL_TYPE_STRING,
02655 "Selects thresholding algorithm: local or "
02656 "global",
02657 "giraffe.localization",
02658 "local", 2, "local", "global");
02659 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "sloc-threshold");
02660 cpl_parameterlist_append(list, p);
02661
02662
02663 p = cpl_parameter_new_value("giraffe.localization.ron",
02664 CPL_TYPE_DOUBLE,
02665 "New bias sigma (RON) value for dark "
02666 "subtraction",
02667 "giraffe.localization",
02668 -1.);
02669 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "sloc-ron");
02670 cpl_parameterlist_append(list, p);
02671
02672
02673 p = cpl_parameter_new_value("giraffe.localization.yorder",
02674 CPL_TYPE_INT,
02675 "Order of Chebyshev polynomial fit.",
02676 "giraffe.localization",
02677 4);
02678 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "sloc-yorder");
02679 cpl_parameterlist_append(list, p);
02680
02681
02682 p = cpl_parameter_new_value("giraffe.localization.worder",
02683 CPL_TYPE_INT,
02684 "Order of Chebyshev 2D polynomial fit.",
02685 "giraffe.localization",
02686 2);
02687 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "sloc-worder");
02688 cpl_parameterlist_append(list, p);
02689
02690
02691 p = cpl_parameter_new_value("giraffe.localization.sigma",
02692 CPL_TYPE_DOUBLE,
02693 "Localization clipping: sigma threshold "
02694 "factor",
02695 "giraffe.localization",
02696 2.5);
02697 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "sloc-sigma");
02698 cpl_parameterlist_append(list, p);
02699
02700
02701 p = cpl_parameter_new_value("giraffe.localization.iterations",
02702 CPL_TYPE_INT,
02703 "Localization clipping: number of "
02704 "iterations",
02705 "giraffe.localization",
02706 5);
02707 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "sloc-niter");
02708 cpl_parameterlist_append(list, p);
02709
02710
02711 p = cpl_parameter_new_range("giraffe.localization.fraction",
02712 CPL_TYPE_DOUBLE,
02713 "Localization clipping: minimum fraction "
02714 "of points accepted/total.",
02715 "giraffe.localization",
02716 0.9, 0.0, 1.0);
02717 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "sloc-mfrac");
02718 cpl_parameterlist_append(list, p);
02719
02720 return;
02721
02722 }