38 #include <cpl_image.h>
39 #include <cpl_vector.h>
40 #include <cpl_matrix.h>
42 #include <cpl_parameterlist.h>
54 #include "gimessages.h"
56 #include "gilocalize.h"
73 static const cxchar* _task =
"giraffe_localize_spectra";
82 GILOCALIZE_HALF_WIDTH,
86 typedef enum GiLocalizeMethod GiLocalizeMethod;
93 enum GiThresholdMethod
95 GILOCALIZE_THRESHOLD_GLOBAL,
96 GILOCALIZE_THRESHOLD_LOCAL,
97 GILOCALIZE_THRESHOLD_ROW
100 typedef enum GiThresholdMethod GiThresholdMethod;
137 _giraffe_validate_pixel(cxint *pixels, cxint xsize, cxint ysize,
138 cxint xpos, cxint ypos, cxint xwidth, cxint ywidth,
143 cxint xstart = xpos - xwidth;
144 cxint ystart = ypos - ywidth;
145 cxint xend = xpos + xwidth;
146 cxint yend = ypos + ywidth;
156 xstart = CX_MAX(0, xstart);
157 ystart = CX_MAX(0, ystart);
159 xend = CX_MIN(xsize - 1, xend);
160 yend = CX_MIN(ysize - 1, yend);
162 xwidth = CX_MAX(xwidth,1 );
163 ywidth = CX_MAX(ywidth,1 );
171 for (i = ystart; i <= yend; i++) {
190 for (j = xstart; j <= xend; j++) {
191 if (pixels[row + j]) {
195 if (_count >= count) {
234 inline static cpl_matrix*
235 _giraffe_fit_border(cpl_matrix* mborder, cpl_matrix* mbase,
236 cpl_matrix* mxok, cxint nspectra, cxdouble sigma,
237 cxint niter, cxdouble mfrac, cpl_matrix* mcoeff)
240 const cxchar*
const fctid =
"_giraffe_fit_border";
242 register cxint x = 0;
243 register cxint naccept = 0;
244 register cxint ntotal = 0;
245 register cxint iteration = 0;
246 register cxint nx = cpl_matrix_get_ncol(mbase);
247 register cxint yorder = cpl_matrix_get_nrow(mbase);
248 register cxint nxok = cpl_matrix_get_nrow(mxok);
250 register cxdouble ratio = 1.0;
252 cpl_matrix* mtmp = NULL;
253 cpl_matrix* yraw = NULL;
254 cpl_matrix* ydiff = NULL;
255 cpl_matrix* mfit = NULL;
256 cpl_matrix* coeffs = NULL;
261 cpl_error_set(fctid, CPL_ERROR_INCOMPATIBLE_INPUT);
263 GIDEBUG(
gi_warning(
"%s: not enough points mxok[%d] for %d order fit",
264 fctid, nxok, yorder));
274 yraw = cpl_matrix_new(1, nxok);
275 ydiff = cpl_matrix_new(nxok, 1);
277 mtmp = cpl_matrix_duplicate(mxok);
283 for (x = 0; x < nxok; x++) {
284 cxdouble data = cpl_matrix_get(mborder, x, nspectra);
285 cpl_matrix_set(yraw, 0, x, data);
296 while (naccept > 0 && iteration < niter && ratio > mfrac) {
298 register cxint k = 0;
299 register cxint l = 0;
301 register cxdouble ysigma = 0.;
303 cpl_matrix* rawbase = giraffe_chebyshev_base1d(0., nx, yorder, mtmp);
304 cx_assert(rawbase != NULL);
306 if (coeffs != NULL) {
307 cpl_matrix_delete(coeffs);
311 if (coeffs == NULL) {
312 gi_warning(
"%s: error in giraffe_matrix_leastsq(), spectrum %d",
317 cpl_matrix_delete(rawbase);
321 cpl_matrix_delete(mfit);
324 mfit = cpl_matrix_product_create(coeffs, mbase);
326 for (x = 0; x < cpl_matrix_get_nrow(ydiff); x++) {
328 cxint xok = (cxint) cpl_matrix_get(mtmp, x, 0);
331 cpl_matrix_get(yraw, 0, x) - cpl_matrix_get(mfit, 0, xok);
334 cpl_matrix_set(ydiff, x , 0, diff);
346 for (l = 0; l < cpl_matrix_get_nrow(ydiff); l++) {
348 if (fabs(cpl_matrix_get(ydiff, l, 0)) <= ysigma) {
350 cxint xok = cpl_matrix_get(mtmp, l, 0);
351 cxdouble data = cpl_matrix_get(yraw, 0, l);
353 cpl_matrix_set(mtmp, k, 0, xok);
354 cpl_matrix_set(yraw, 0, k, data);
376 ratio = (cxdouble) naccept / (cxdouble) ntotal;
378 GIDEBUG(
gi_message(
"Iteration %d: Sigma %f, accepted bins: %d, "
379 "rejected %d\n", iteration, ysigma, naccept,
386 cpl_matrix_resize(mtmp, 0,
387 naccept - cpl_matrix_get_nrow(mtmp), 0, 0);
388 cpl_matrix_resize(yraw, 0,
389 0, 0, naccept - cpl_matrix_get_ncol(yraw));
390 cpl_matrix_resize(ydiff, 0,
391 naccept - cpl_matrix_get_nrow(ydiff), 0, 0);
396 if (coeffs != NULL) {
399 for (l = 0; l < cpl_matrix_get_nrow(mcoeff); l++) {
400 cpl_matrix_set(mcoeff, l, 0, cpl_matrix_get(coeffs, 0, l));
409 cpl_matrix_delete(coeffs);
410 cpl_matrix_delete(ydiff);
411 cpl_matrix_delete(yraw);
412 cpl_matrix_delete(mtmp);
419 inline static cpl_image*
420 _giraffe_filter_gauss1d(
const cpl_image* image, cxint radius, cxdouble width)
423 cxdouble w2 = width * width;
427 cpl_matrix* kernel = cpl_matrix_new(1, 2 * radius + 1);
429 cpl_image* fimage = NULL;
432 if (kernel == NULL) {
436 for (i = -radius; i <= radius; ++i) {
438 cxdouble y = exp(-x2 / (2. * w2));
440 cpl_matrix_set(kernel, 0, i + radius, y);
444 fimage = cpl_image_new(cpl_image_get_size_x(image),
445 cpl_image_get_size_y(image),
446 cpl_image_get_type(image));
448 if (fimage == NULL) {
449 cpl_matrix_delete(kernel);
453 cpl_image_filter(fimage, image, kernel, CPL_FILTER_LINEAR,
455 cpl_matrix_delete(kernel);
462 inline static cpl_image*
463 _giraffe_filter_sobel(
const cpl_image* image, cxbool vertical)
465 cpl_matrix* kernel = cpl_matrix_new(3, 3);
467 cpl_image* fimage = NULL;
470 if (kernel == NULL) {
477 cpl_matrix_set(kernel, 0, 0, -1);
478 cpl_matrix_set(kernel, 1, 0, -2);
479 cpl_matrix_set(kernel, 2, 0, -1);
481 cpl_matrix_set(kernel, 0, 2, 1);
482 cpl_matrix_set(kernel, 1, 2, 2);
483 cpl_matrix_set(kernel, 2, 2, 1);
485 cpl_matrix_set(kernel, 0, 0, 0);
486 cpl_matrix_set(kernel, 1, 0, -0.5);
487 cpl_matrix_set(kernel, 2, 0, 0);
489 cpl_matrix_set(kernel, 0, 2, 0);
490 cpl_matrix_set(kernel, 1, 2, 0.5);
491 cpl_matrix_set(kernel, 2, 2, 0);
496 cpl_matrix_set(kernel, 0, 0, 1);
497 cpl_matrix_set(kernel, 0, 1, 2);
498 cpl_matrix_set(kernel, 0, 2, 1);
500 cpl_matrix_set(kernel, 2, 0, -1);
501 cpl_matrix_set(kernel, 2, 1, -2);
502 cpl_matrix_set(kernel, 2, 2, -1);
506 fimage = cpl_image_new(cpl_image_get_size_x(image),
507 cpl_image_get_size_y(image),
508 cpl_image_get_type(image));
510 if (fimage == NULL) {
511 cpl_matrix_delete(kernel);
515 cpl_image_filter(fimage, image, kernel, CPL_FILTER_LINEAR,
517 cpl_matrix_delete(kernel);
525 _giraffe_build_edge_mask(cpl_image* raw, cpl_image* bpixel, cxint nspectra,
526 cxdouble noise, GiMaskParameters* config,
527 cxint* ndetect, cpl_matrix* mxok, cpl_matrix* myup,
531 const cxint margin = 5;
539 cxint nrows = cpl_image_get_size_y(raw);
540 cxint ncols = cpl_image_get_size_x(raw);
544 cxdouble* buffer = NULL;
546 cpl_mask* kernel = NULL;
548 cpl_image* fraw = NULL;
549 cpl_image* sraw = NULL;
550 cpl_image* vertical1 = NULL;
551 cpl_image* vertical2 = NULL;
552 cpl_image* center = NULL;
563 kernel = cpl_mask_new(1, 15);
565 if (kernel != NULL) {
567 cpl_mask_not(kernel);
569 fraw = cpl_image_new(ncols, nrows, cpl_image_get_type(raw));
572 cpl_mask_delete(kernel);
578 cpl_image_filter_mask(fraw, raw, kernel, CPL_FILTER_MEDIAN,
583 cpl_mask_delete(kernel);
587 sraw = _giraffe_filter_gauss1d(fraw, 6, 1.);
591 cpl_image_delete(fraw);
598 vertical1 = _giraffe_filter_sobel(sraw, TRUE);
599 vertical2 = _giraffe_filter_sobel(vertical1, TRUE);
601 cpl_image_save(sraw,
"master_flat_smooth.fits", -32, 0, CPL_IO_DEFAULT);
602 cpl_image_save(vertical1,
"vertical.fits", -32, 0, CPL_IO_DEFAULT);
603 cpl_image_save(vertical2,
"vertical2.fits", -32, 0, CPL_IO_DEFAULT);
610 center = cpl_image_new(ncols, nrows, CPL_TYPE_INT);
612 flags = cx_calloc(ncols,
sizeof(cxint));
613 buffer = cx_calloc(ncols,
sizeof(cxdouble));
615 if ((center == NULL) || (flags ==NULL) || (buffer == NULL)) {
623 cpl_image_delete(center);
626 cpl_image_delete(vertical2);
629 cpl_image_delete(vertical1);
632 cpl_image_delete(sraw);
635 cpl_image_delete(fraw);
643 for (m = 0; m < nrows; ++m) {
645 register cxint irow = m * ncols;
646 register cxint n = 0;
651 cxint* _center = cpl_image_get_data_int(center) + irow;
653 const cxdouble* _vt1 = cpl_image_get_data_double_const(vertical1) +
655 const cxdouble* _vt2 = cpl_image_get_data_double_const(vertical2) +
657 const cxdouble* _fraw = cpl_image_get_data_double_const(fraw) +
661 memset(buffer, 0, ncols *
sizeof(cxdouble));
662 memset(flags, 0, ncols *
sizeof(cxint));
665 for (n = 0; n < ncols; ++n) {
675 if ((n - 1 >= 0) && (_vt2[n - 1] > 0.)) {
676 buffer[n - 1] = _vt1[n - 1];
678 if ((n + 1 < ncols) && (_vt2[n + 1] > 0.)) {
679 buffer[n + 1] = _vt1[n + 1];
685 while (iteration < ncols) {
689 cxdouble dx = 3. * 2. * noise;
692 for (n = 0; n < ncols; ++n) {
694 if (!flags[n] && (buffer[n] > dx)) {
704 register cxint k = 0;
711 cxdouble signal = 0.;
717 while ((k >= 0) && (buffer[k] > 0.)) {
724 while ((k < ncols) && (buffer[k] > 0.)) {
730 while ((k < ncols) && (buffer[k] < 0.)) {
735 width = end - start + 1;
743 signal = (_fraw[pos] > 0.) ? _fraw[pos] : 0.;
744 sigma = sqrt((noise * noise + signal) / config->xbin);
746 if ((signal / sigma > 10.) && (width > 1)) {
748 start = (start == pos) ? start - 1 : start;
749 end = (end == pos) ? end + 1 : end;
752 _center[start] += -1;
763 for (n = 0; n < ncols; ++n) {
765 if (_center[n] == 1) {
771 if (scount >= smax) {
787 cx_print(
"scount: %d (%d) at %d\n", smax, nspectra, mmax);
795 const cxint limit = 0.85 * nrows;
800 const cxdouble hwf = sqrt(2. * log(2.));
802 cxint* xtrace = cx_calloc(nrows,
sizeof(cxint));
803 cxint* ytrace = cx_calloc(nrows,
sizeof(cxint));
805 cpl_image* mask = cpl_image_new(ncols, nrows, CPL_TYPE_INT);
807 for (m = 0; m < ncols; ++m) {
809 const cxint* _center = cpl_image_get_data_int(center);
810 const cxint* _reference = _center + mmax * ncols;
812 cxbool out_of_bounds = FALSE;
817 if (_reference[m] == 1) {
819 register cxint j = mmax;
820 register cxint pos = m;
825 xtrace[connected] = pos;
826 ytrace[connected] = j;
832 register cxint k = 0;
833 register cxint l = j * ncols;
834 register cxint kmin = (pos - 1 >= 0) ? pos - 1 : 0;
835 register cxint kmax = (pos + 1 < ncols) ? pos + 1 : ncols - 1;
837 for (k = kmin; k <= kmax; ++k) {
839 if (_center[l + k] == 1) {
841 if ((pos <= margin) || (pos >= ncols - margin)) {
842 out_of_bounds = TRUE;
846 xtrace[connected] = k;
847 ytrace[connected] = j;
864 register cxint k = 0;
865 register cxint l = j * ncols;
866 register cxint kmin = (pos - 1 >= 0) ? pos - 1 : 0;
867 register cxint kmax = (pos + 1 < ncols) ? pos + 1 : ncols - 1;
869 for (k = kmin; k <= kmax; ++k) {
871 if (_center[l + k] == 1) {
873 if ((pos <= margin) || (pos >= ncols - margin)) {
874 out_of_bounds = TRUE;
878 xtrace[connected] = k;
879 ytrace[connected] = j;
891 if ((connected < limit) || (out_of_bounds == TRUE)) {
893 memset(xtrace, 0, nrows *
sizeof(cxint));
894 memset(ytrace, 0, nrows *
sizeof(cxint));
896 if (out_of_bounds == TRUE) {
897 cx_print(
"discarded candidate %d, going out of detector "
898 "boundaries.\n", itrace);
902 cx_print(
"discarded candidate %d, not enough connected "
903 "centers (%d, required: %d)\n", itrace, connected,
910 cxint* _mask = cpl_image_get_data_int(mask);
912 for (j = 0; j < connected; ++j) {
914 register cxint x = xtrace[j];
915 register cxint y = ytrace[j] * ncols;
916 register cxint ix = x;
920 while ((_center[y + ix] != -1) && (ix > 0)) {
926 while ((_center[y + ix] != -2) && (ix < ncols - 1)) {
941 cx_print(
"scount: %d (expected: %d)\n", ispectra, nspectra);
949 for (m = 0; m < nrows; ++m) {
951 register cxint j = 0;
952 register cxint ns = 0;
954 const cxint* _mask = cpl_image_get_data_int(mask) + m * ncols;
955 const cxint* _center = cpl_image_get_data_int(center) + m * ncols;
958 for (j = 0; j < ncols; ++j) {
962 register cxint x = j;
963 register cxint ix = x;
966 while ((_center[ix] != -1) && (ix > 0)) {
969 cpl_matrix_set(mylo, naccepted, ns, x - hwf * fabs(x - ix));
972 while ((_center[ix] != -2) && (ix < ncols - 1)) {
975 cpl_matrix_set(myup, naccepted, ns, x + hwf * fabs(ix - x));
982 if (ns == ispectra) {
983 cpl_matrix_set(mxok, naccepted, 0, m);
992 cpl_image_save(center,
"center.fits", -32, 0, CPL_IO_DEFAULT);
993 cpl_image_save(mask,
"mask.fits", -32, 0, CPL_IO_DEFAULT);
995 cpl_image_delete(mask);
996 cpl_image_delete(center);
997 cpl_image_delete(vertical2);
998 cpl_image_delete(vertical1);
999 cpl_image_delete(sraw);
1000 cpl_image_delete(fraw);
1041 _giraffe_build_raw_mask(cpl_image *raw, cpl_image *bpixel, cxint nspectra,
1042 cxdouble noise, GiMaskParameters *config,
1043 cxint *ndetect, cpl_matrix *mxok, cpl_matrix *myup,
1047 register cxint x = 0;
1048 register cxint y = 0;
1049 register cxint xretry = 0;
1050 register cxint xok = 0;
1055 cxint *yabove = NULL;
1056 cxint *ybelow = NULL;
1057 cxint *good_pixels = NULL;
1058 cxint ywidth = config->ywidth > 1 ? config->ywidth : 2;
1059 cxint ckwidth = config->ckdata.width;
1060 cxint ckheight = config->ckdata.height;
1061 cxint ckcount = config->ckdata.count;
1064 cxdouble* pixels = NULL;
1066 cpl_mask* med = NULL;
1068 cpl_image* img = raw;
1071 med = cpl_mask_new(1, 15);
1077 img = cpl_image_new(cpl_image_get_size_x(raw),
1078 cpl_image_get_size_y(raw),
1079 cpl_image_get_type(raw));
1081 cpl_image_filter_mask(img, raw, med, CPL_FILTER_MEDIAN,
1086 cpl_mask_delete(med);
1091 GIDEBUG(
gi_message(
"noise = %g start = %d tries = %d xbin = %d "
1092 "ywidth = %d", noise, config->start, config->retry,
1093 config->xbin, ywidth));
1095 pixels = cpl_image_get_data_double(img);
1097 nrows = cpl_image_get_size_y(img);
1098 ncols = cpl_image_get_size_x(img);
1101 if (config->xbin > 1) {
1105 cxdouble* _pixels = NULL;
1108 nrows = (cxint) ceil(nrows / config->xbin);
1109 config->start = (cxint) ceil(config->start / config->xbin);
1111 _pixels = cx_calloc(ncols * nrows,
sizeof(cxdouble));
1113 for (y = 0; y < ncols; ++y) {
1115 for (x = 0; x < nrows; ++x) {
1117 register cxint xx = 0;
1118 register cxint zx = x * ncols;
1119 register cxint xr = x * config->xbin;
1120 register cxint zr = xr * ncols;
1123 _pixels[zx + y] = 0.;
1125 for (xx = 0; xx < config->xbin && xr < nx; ++xx) {
1126 _pixels[zx + y] += pixels[zr + y];
1129 _pixels[zx + y] /= config->xbin;
1139 good_pixels = cx_calloc(nrows * ncols,
sizeof(cxint));
1141 switch (config->method) {
1143 case GILOCALIZE_THRESHOLD_LOCAL:
1146 cxint ywidth2 = ywidth / 2;
1147 cxint sz = 2 * ywidth2 + 1;
1149 cpl_vector* ymins = cpl_vector_new(sz);
1158 for (x = 0; x < nrows; x++) {
1160 cpl_vector_fill(ymins, 0.);
1162 for (y = 0; y < ncols; y++) {
1164 register cxint k = 0;
1165 register cxint kk = 0;
1167 cxdouble value = 0.;
1169 cxdouble threshold = 0.;
1172 for (kk = 0, k = -ywidth2; k <= ywidth2; k++) {
1174 register cxint ky = y + k;
1176 if (ky < 0 || ky >= ncols) {
1180 cpl_vector_set(ymins, kk, pixels[x * ncols + ky]);
1188 if (config->threshold > 0.) {
1190 const cxint count = 2;
1205 for (i = 0; i < count; i++) {
1206 bkg += fabs(cpl_vector_get(ymins, i));
1208 bkg /= (cxdouble)count;
1210 threshold = sqrt((2. * noise * noise +
1211 fabs(pixels[x * ncols + y]) + bkg / count) / config->xbin);
1217 register cxdouble mean = 0.;
1220 for (i = 0; i < kk; i++) {
1221 mean += cpl_vector_get(ymins, i);
1227 bkg = (cpl_vector_get(ymins, 0) +
1228 cpl_vector_get(ymins, 1)) / 2.0;
1229 threshold = mean - bkg;
1238 value = pixels[x * ncols + y] - bkg;
1244 if (value > fabs(config->threshold) * threshold) {
1245 good_pixels[x * ncols + y] = 1;
1250 cpl_vector_delete(ymins);
1257 case GILOCALIZE_THRESHOLD_ROW:
1260 cpl_image* snr = cpl_image_abs_create(raw);
1262 cxint sx = cpl_image_get_size_x(snr);
1265 cpl_image_power(snr, 0.5);
1267 for (x = 0; x < nrows; ++x) {
1269 const cxdouble* _snr = cpl_image_get_data_double_const(snr);
1271 cxdouble avsnr = giraffe_array_median(_snr + x * sx, sx);
1274 for (y = 0; y < ncols; ++y) {
1276 if (pixels[x * ncols + y] <= 0.) {
1280 if (_snr[x * ncols + y] > avsnr * fabs(config->threshold)) {
1281 good_pixels[x * ncols + y] = 1;
1288 cpl_image_delete(snr);
1298 cxdouble threshold = 0.;
1305 if (config->threshold > 0.) {
1306 threshold = config->threshold * noise;
1310 cxdouble mean = cpl_image_get_mean(raw);
1312 threshold = -config->threshold * mean *
1313 (nspectra * config->wavg / ncols);
1317 for (x = 0; x < nrows; x++) {
1319 for (y = 0; y < ncols; y++) {
1321 if (pixels[x * ncols + y] > threshold) {
1322 good_pixels[x * ncols + y] = 1;
1335 GIDEBUG(cxint *data = cx_calloc(nrows * ncols,
sizeof(cxint));
1336 memcpy(data, good_pixels, nrows * ncols *
sizeof(cxint));
1337 cpl_image *gp = cpl_image_wrap_int(ncols, nrows, data);
1338 cpl_image_save(gp,
"locmask.fits", 32, NULL, CPL_IO_DEFAULT);
1339 cpl_image_unwrap(gp);
1347 yabove = cx_calloc(nspectra + 1,
sizeof(cxint));
1348 ybelow = cx_calloc(nspectra + 1,
sizeof(cxint));
1360 for (x = config->start; (x >= 0) && (xretry <= config->retry); x--) {
1362 register cxint zx = x * ncols;
1363 register cxint nborders = 0;
1364 register cxint nbelow = 0;
1365 register cxint nabove = 0;
1366 register cxint in_spectrum = 0;
1369 for (y = 1; y < ny; y++) {
1371 register cxint tmp = 2 * good_pixels[zx + y];
1377 nborders = CX_MAX(CX_MAX(nborders, nbelow), nabove);
1379 if (nborders > nspectra) {
1389 if (good_pixels[zx + y + 1]) {
1396 if ((tmp - good_pixels[zx + y - 1]) == 2) {
1410 ybelow[nbelow++] = y;
1419 if (good_pixels[zx + y - 1]) {
1426 if ((tmp - good_pixels[zx + y + 1]) == 2) {
1440 yabove[nabove++] = y;
1452 !good_pixels[zx + y - 1] && !good_pixels[zx + y + 1]) {
1454 if (_giraffe_validate_pixel(good_pixels, ncols, nrows, y, x,
1455 ckwidth, ckheight, ckcount)) {
1457 yabove[nabove++] = y;
1458 ybelow[nbelow++] = y;
1471 *ndetect = nborders;
1473 if (!in_spectrum && (nbelow == nspectra) && (nbelow == nabove)) {
1482 for (y = 0; y < nspectra; y++) {
1483 cpl_matrix_set(mylo, xok, y, (cxdouble) ybelow[y]);
1484 cpl_matrix_set(myup, xok, y, (cxdouble) yabove[y]);
1485 cpl_matrix_set(mxok, xok, 0, (config->xbin > 1) ?
1486 (cxdouble) (x + 0.5) * config->xbin :
1492 else if (xretry++ < config->retry) {
1522 for (x = config->start + 1; (x < nrows) &&
1523 (xretry <= config->retry); x++) {
1525 register cxint zx = x * ncols;
1526 register cxint nborders = 0;
1527 register cxint nbelow = 0;
1528 register cxint nabove = 0;
1529 register cxint in_spectrum = 0;
1532 for (y = 1; y < ny; y++) {
1534 register cxint tmp = 2 * good_pixels[zx + y];
1536 nborders = CX_MAX(CX_MAX(nborders, nbelow), nabove);
1538 if (nborders > nspectra) {
1542 if (good_pixels[zx + y + 1]) {
1543 if ((tmp - good_pixels[zx + y - 1]) == 2) {
1545 ybelow[nbelow++] = y;
1551 if (good_pixels[zx + y - 1]) {
1552 if ((tmp - good_pixels[zx + y + 1]) == 2) {
1554 yabove[nabove++] = y;
1563 !good_pixels[zx + y - 1] && !good_pixels[zx + y + 1]) {
1565 if (_giraffe_validate_pixel(good_pixels, ncols, nrows, y, x,
1566 ckwidth, ckheight, ckcount)) {
1568 yabove[nabove++] = y;
1569 ybelow[nbelow++] = y;
1582 *ndetect = nborders;
1584 if (!in_spectrum && (nbelow == nspectra) && (nbelow == nabove)) {
1586 for (y = 0; y < nspectra; y++) {
1587 cpl_matrix_set(mylo, xok, y, (cxdouble) ybelow[y]);
1588 cpl_matrix_set(myup, xok, y, (cxdouble) yabove[y]);
1589 cpl_matrix_set(mxok, xok, 0, (config->xbin > 1) ?
1590 (cxdouble) (x + 0.5) * config->xbin :
1596 else if (xretry++ < config->retry) {
1607 cx_free(good_pixels);
1609 if (pixels != cpl_image_get_data_double(img)) {
1615 cpl_image_delete(img);
1620 if (*ndetect < nspectra) {
1623 else if (*ndetect > nspectra) {
1631 *ndetect = nspectra;
1665 _giraffe_fit_raw_mask(cpl_matrix *mxok, cpl_matrix *myup, cpl_matrix *mylo,
1666 cpl_table *fibers, GiMaskParameters *config,
1667 GiMaskPosition *position)
1670 register cxint nn, x, nspectra;
1671 register cxint nx = cpl_matrix_get_nrow(position->my);
1672 register cxint ns = cpl_table_get_nrow(fibers);
1680 mxraw = cpl_matrix_new(nx, 1);
1681 mcoeff = cpl_matrix_new(config->ydeg + 1, 1);
1688 for (x = 0; x < nx; x++) {
1689 cpl_matrix_set(mxraw, x, 0, x);
1696 base = giraffe_chebyshev_base1d(0., nx, config->ydeg + 1, mxraw);
1697 cpl_matrix_delete(mxraw);
1700 for (nn = 0; nn < ns; nn++) {
1701 cpl_matrix *ylofit = NULL;
1702 cpl_matrix *yupfit = NULL;
1715 ylofit = _giraffe_fit_border(mylo, base, mxok, nspectra,
1716 config->sigma, config->niter,
1717 config->mfrac, mcoeff);
1718 if (ylofit == NULL) {
1719 cpl_msg_warning(_task,
"Could not compute low border for "
1726 yupfit = _giraffe_fit_border(myup, base, mxok, nspectra,
1727 config->sigma, config->niter,
1728 config->mfrac, mcoeff);
1729 if (yupfit == NULL) {
1730 cpl_msg_warning(_task,
"Could not compute up border for "
1742 for (x = 0; x < nx; x++) {
1744 cpl_matrix_set(position->my, x, nn, 0.5 *
1745 (cpl_matrix_get(yupfit, x, 0) +
1746 cpl_matrix_get(ylofit, x, 0)));
1748 cpl_matrix_set(position->my, x, nn, 0.5 *
1749 (cpl_matrix_get(yupfit, x, 0) -
1750 cpl_matrix_get(ylofit, x, 0)) + config->ewid);
1753 cpl_matrix_delete(ylofit);
1754 cpl_matrix_delete(yupfit);
1759 cpl_msg_info(_task,
"%03d spectrum positions fitted", nspectra);
1761 cpl_matrix_delete(base);
1762 cpl_matrix_delete(mcoeff);
1764 if (nspectra == 0) {
1765 cpl_msg_warning(_task,
"could not fit any spectra, check number "
1766 "of good wavelength bins");
1810 _giraffe_fit_raw_centroid(cpl_image* mz, cpl_matrix* mxok, cpl_matrix* myup,
1811 cpl_matrix* mylo, cpl_table* fibers,
1812 GiMaskParameters* config, GiMaskPosition* position,
1813 GiMaskPosition* coeffs)
1816 const cxchar*
const fctid =
"_giraffe_fit_raw_centroid";
1818 register cxint nn = 0;
1819 register cxint x = 0;
1820 register cxint y = 0;
1821 register cxint nspectra = 0;
1822 register cxint nx = cpl_image_get_size_y(mz);
1823 register cxint ny = cpl_image_get_size_x(mz);
1824 register cxint ns = cpl_table_get_nrow(fibers);
1826 cxint yorder = config->ydeg + 1;
1827 cxint worder = config->wdeg + 1;
1829 cpl_matrix* mxraw = NULL;
1830 cpl_matrix* base = NULL;
1831 cpl_matrix* mycenter = NULL;
1832 cpl_matrix* mywidth = NULL;
1833 cpl_matrix* mx = NULL;
1834 cpl_matrix* my = NULL;
1835 cpl_matrix* mw = NULL;
1836 cpl_matrix* chebcoeff = NULL;
1837 cpl_matrix* mfitlocw = NULL;
1838 cpl_matrix* ycenfit = NULL;
1839 cpl_matrix* ycencoeff = NULL;
1843 if (cpl_matrix_get_nrow(position->my) != nx ||
1844 cpl_matrix_get_ncol(position->my) != ns) {
1845 gi_error(
"%s: invalid size for position->my[%" CPL_SIZE_FORMAT
",%"
1846 CPL_SIZE_FORMAT
"], expected [%d,%d]", fctid,
1847 cpl_matrix_get_nrow(position->my),
1848 cpl_matrix_get_ncol(position->my), nx, ns);
1852 if (cpl_matrix_get_nrow(position->mw) != nx ||
1853 cpl_matrix_get_ncol(position->mw) != ns) {
1854 gi_error(
"%s: invalid size for position->mw[%" CPL_SIZE_FORMAT
",%"
1855 CPL_SIZE_FORMAT
"], expected [%d,%d]", fctid,
1856 cpl_matrix_get_nrow(position->my),
1857 cpl_matrix_get_ncol(position->my), nx, ns);
1866 mxraw = cpl_matrix_new(nx, 1);
1868 for (x = 0; x < nx; x++) {
1869 cpl_matrix_set(mxraw, x, 0, x);
1877 base = giraffe_chebyshev_base1d(0., nx, yorder, mxraw);
1878 cpl_matrix_delete(mxraw);
1880 mycenter = cpl_matrix_new(cpl_matrix_get_nrow(mxok), ns);
1881 mywidth = cpl_matrix_new(1, cpl_matrix_get_nrow(mxok) * ns);
1883 ycencoeff = cpl_matrix_new(yorder, 1);
1885 for (nn = 0; nn < ns; nn++) {
1902 cxdouble* pixels = cpl_image_get_data_double(mz);
1904 for (x = 0; x < cpl_matrix_get_nrow(mxok); x++) {
1906 register cxint zx = (cxint) cpl_matrix_get(mxok, x, 0);
1908 register cxdouble zz = 0.;
1909 register cxdouble yy = 0.;
1911 cxdouble lower = cpl_matrix_get(mylo, x, nspectra);
1912 cxdouble upper = cpl_matrix_get(myup, x, nspectra);
1915 for (y = (cxint) lower; y <= (cxint) upper; y++) {
1916 yy += pixels[zx * ny + y] * y;
1917 zz += pixels[zx * ny + y];
1920 cpl_matrix_set(mycenter, x, nspectra, yy / zz);
1921 cpl_matrix_set(mywidth, 0, x * ns + nspectra, config->ewid +
1922 (upper - lower) / 2.0);
1930 cpl_matrix_fill(ycencoeff, 0.);
1931 ycenfit = _giraffe_fit_border(mycenter, base, mxok, nspectra,
1932 config->sigma, config->niter,
1933 config->mfrac, ycencoeff);
1934 if (ycenfit == NULL) {
1935 cpl_msg_warning(_task,
"Could not fit centroid for spectrum %d",
1945 for (x = 0; x < yorder; x++) {
1946 cpl_matrix_set(coeffs->my, x, nn,
1947 cpl_matrix_get(ycencoeff, x, 0));
1955 for (x = 0; x < nx; x++) {
1956 cpl_matrix_set(position->my, x, nn,
1957 cpl_matrix_get(ycenfit, 0, x));
1960 cpl_matrix_delete(ycenfit);
1966 cpl_image_save(lycenter,
"lycenter.fits", -32, NULL,
1968 cpl_image_delete(lycenter);
1971 cpl_image_save(lycenter,
"lycenterfit.fits", -32, NULL,
1973 cpl_image_delete(lycenter);
1976 cpl_image_save(lyxok,
"lyxok.fits", -32, NULL,
1978 cpl_image_delete(lyxok));
1981 cpl_msg_info(_task,
"%03d spectrum positions fitted", nspectra);
1983 cpl_matrix_delete(base);
1984 cpl_matrix_delete(mycenter);
1985 cpl_matrix_delete(ycencoeff);
1987 if (nspectra == 0) {
1988 cpl_msg_warning(_task,
"Could not fit any spectra, check number of "
1989 "good wavelength bins");
1991 cpl_matrix_delete(mywidth);
1999 cpl_msg_info(_task,
"2D fit (order %dx%d) of mask width", worder,
2006 mx = cpl_matrix_new(cpl_matrix_get_nrow(mxok) * nspectra, 1);
2007 my = cpl_matrix_new(cpl_matrix_get_nrow(mxok) * nspectra, 1);
2008 mw = cpl_matrix_new(1, cpl_matrix_get_nrow(mxok) * nspectra);
2010 for (y = 0, nn = 0; nn < nspectra; nn++) {
2022 for (x = 0; x < cpl_matrix_get_nrow(mxok); x++) {
2024 register cxint zx = (cxint) cpl_matrix_get(mxok, x, 0);
2025 register cxint lx = x * nspectra + y;
2028 cpl_matrix_set(mx, lx, 0, cpl_matrix_get(mxok, x, 0));
2029 cpl_matrix_set(my, lx, 0, cpl_matrix_get(position->my, zx, nn));
2030 cpl_matrix_set(mw, 0, lx, cpl_matrix_get(mywidth, 0, x * ns + y));
2035 base = giraffe_chebyshev_base2d(0., 0., nx, ny, worder, worder, mx, my);
2037 cpl_matrix_delete(my);
2038 cpl_matrix_delete(mx);
2041 cpl_matrix_delete(base);
2042 cpl_matrix_delete(mw);
2044 cpl_matrix_delete(mywidth);
2046 if (chebcoeff == NULL) {
2047 gi_warning(
"%s: error in giraffe_matrix_leastsq() for width 2D fit",
2056 for (nn = 0; nn < cpl_matrix_get_ncol(chebcoeff); nn++) {
2057 cpl_matrix_set(coeffs->mw, 0, nn, cpl_matrix_get(chebcoeff, 0, nn));
2064 mx = cpl_matrix_new(nx * nspectra, 1);
2065 my = cpl_matrix_new(nx * nspectra, 1);
2067 for (y = 0, nn = 0; nn < nspectra; nn++) {
2079 for (x = 0; x < nx; x++) {
2081 register cxint lx = x * nspectra + y;
2083 cpl_matrix_set(mx, lx, 0, x);
2084 cpl_matrix_set(my, lx, 0, cpl_matrix_get(position->my, x, nn));
2090 cpl_matrix_set_size(chebcoeff, worder, worder);
2092 mfitlocw = giraffe_chebyshev_fit2d(0., 0., nx, ny, chebcoeff, mx, my);
2093 cpl_matrix_delete(chebcoeff);
2095 cpl_matrix_delete(my);
2096 cpl_matrix_delete(mx);
2098 for (y = 0, nn = 0; nn < nspectra; nn++) {
2110 for (x = 0; x < nx; x++) {
2112 register cxint lx = x * nspectra + y;
2114 cpl_matrix_set(position->mw, x, nn,
2115 cpl_matrix_get(mfitlocw, lx, 0));
2121 cpl_matrix_delete(mfitlocw);
2154 _giraffe_localize_spectra(cpl_image *mzraw, cpl_image *bpixel,
2155 cpl_table *fibers, GiLocalizeMethod method,
2156 cxbool normalize, cxdouble noise,
2157 GiMaskParameters *config, GiMaskPosition *position,
2158 GiMaskPosition *coeffs)
2163 cxint ndetect, nspectra;
2166 cxdouble uplost = 0.;
2167 cxdouble lolost = 0.;
2168 cxdouble avglost = 0.;
2169 cxdouble avgmask = 0.;
2170 cxdouble sigmask = 0.;
2171 cxdouble sigmean = 0.;
2172 cxdouble avgborders = 0.;
2181 cpl_image *mz = NULL;
2182 cpl_image *mznorm = NULL;
2186 nx = cpl_image_get_size_y(mzraw);
2187 ny = cpl_image_get_size_x(mzraw);
2188 _mzraw = cpl_image_get_data_double(mzraw);
2191 if (normalize == TRUE) {
2193 cxdouble zxmax = 0.0;
2194 cxdouble *_mzx = NULL;
2195 cxdouble *_mznorm = NULL;
2197 cpl_image *mzx = NULL;
2200 cpl_msg_info(_task,
"Using normalized spectra for localization");
2209 mznorm = cpl_image_new(ny, nx, CPL_TYPE_DOUBLE);
2210 _mznorm = cpl_image_get_data_double(mznorm);
2212 mzx = cpl_image_new(1, nx, CPL_TYPE_DOUBLE);
2213 _mzx = cpl_image_get_data_double(mzx);
2220 for (x = 0 ; x < nx; x++) {
2221 for (y = 0 ; y < ny; y++) {
2222 _mzx[x] += _mzraw[x * ny + y];
2229 if (_mzx[x] > zxmax) {
2234 GIDEBUG(cpl_image_save(mzx,
"mzx.fits", -32, NULL, CPL_IO_DEFAULT));
2236 for (x = 0 ; x < nx; x++) {
2238 register cxdouble zxnorm = zxmax / _mzx[x];
2240 for (y = 0 ; y < ny; y++) {
2241 _mznorm[x * ny + y] = _mzraw[x * ny + y] * zxnorm;
2246 cpl_image_delete(mzx);
2255 cpl_msg_info(_task,
"Using raw spectra for localization");
2264 nspectra = cpl_table_get_nrow(fibers);
2266 mxok = cpl_matrix_new(nx, 1);
2267 myup = cpl_matrix_new(nx, nspectra);
2268 mylo = cpl_matrix_new(nx, nspectra);
2275 config->xbin = (config->xbin > 1) ? 2 * (config->xbin / 2) : 1;
2277 GIDEBUG(cpl_image_save(mz,
"mz.fits", -32, NULL, CPL_IO_DEFAULT));
2284 cpl_msg_info(_task,
"Generating mask (%d spectra expected) ...",
2290 nxok = _giraffe_build_edge_mask(mz, bpixel, nspectra, noise, config,
2291 &ndetect, mxok, myup, mylo);
2296 nxok = _giraffe_build_raw_mask(mz, bpixel, nspectra, noise, config,
2297 &ndetect, mxok, myup, mylo);
2303 cpl_msg_warning(_task,
"Invalid number of spectra detected: "
2304 "%d != %d", ndetect, nspectra);
2308 cpl_msg_warning(_task,
"No abcissa with good number "
2313 cpl_msg_warning(_task,
"Error while searching for spectra");
2321 cpl_msg_info(_task,
"%d spectra detected in %d wavelength bins",
2330 cpl_matrix_resize(mxok, 0, nxok - cpl_matrix_get_nrow(mxok), 0, 0);
2331 cpl_matrix_resize(myup, 0, nxok - cpl_matrix_get_nrow(myup), 0, 0);
2332 cpl_matrix_resize(mylo, 0, nxok - cpl_matrix_get_nrow(mylo), 0, 0);
2334 GIDEBUG(
gi_message(
"%s: mxok[0-%d]=[%g-%g]", __func__,
2335 cpl_matrix_get_nrow(mxok) - 1,
2336 cpl_matrix_get_min(mxok),
2337 cpl_matrix_get_max(mxok)));
2340 cpl_msg_info(_task,
"Computing spectrum positions and widths in "
2341 "pixel range [%g,%g]", cpl_matrix_get_min(mxok),
2342 cpl_matrix_get_max(mxok));
2344 if (cpl_matrix_get_nrow(mxok) <= config->ydeg) {
2345 cpl_msg_info(_task,
"Not enough data points %" CPL_SIZE_FORMAT
2346 " for %d order fit", cpl_matrix_get_nrow(mxok),
2353 case GILOCALIZE_HALF_WIDTH:
2354 cpl_msg_info(_task,
"Using half-width for localization");
2355 _giraffe_fit_raw_mask(mxok, myup, mylo, fibers, config,
2359 case GILOCALIZE_BARYCENTER:
2361 cpl_msg_info(_task,
"Using barycenter for localization");
2362 _giraffe_fit_raw_centroid(mz, mxok, myup, mylo, fibers, config,
2367 if (normalize == 1) {
2368 cpl_image_delete(mznorm);
2381 mwid = cpl_matrix_new(nxok, nspectra);
2383 for (n = 0, nn = 0; nn < cpl_table_get_nrow(fibers); nn++) {
2385 for (x = 0; x < nxok; x++) {
2386 register cxint lx = (cxint) cpl_matrix_get(mxok, x, 0);
2388 cxdouble lower = cpl_matrix_get(mylo, x, n);
2389 cxdouble upper = cpl_matrix_get(myup, x, n);
2390 cxdouble width = cpl_matrix_get(position->mw, lx, nn);
2392 uplost += cpl_matrix_get(position->my, lx, nn) + width - upper;
2393 lolost += cpl_matrix_get(position->my, lx, nn) - width - lower;
2395 avgborders += upper - lower;
2398 cpl_matrix_set(mwid, x, n, 2. * width);
2403 sigmean = cpl_matrix_get_mean(mwid);
2405 avglost = (lolost + uplost) / (nspectra * nxok);
2406 avgmask = 2.0 * avgmask / nspectra;
2408 cpl_msg_info(_task,
"Mask was computed using %d of %d wavelength bins",
2410 cpl_msg_info(_task,
"Average # of pixels per spectra: %.4g",
2412 cpl_msg_info(_task,
"Average # of in-borders pixels per spectra: %.4g",
2413 avgborders / nspectra);
2414 cpl_msg_info(_task,
"Average lost pixels per spectra: %.4g",
2416 cpl_msg_info(_task,
"Average lost pixels at upper border: %.4g",
2417 uplost / (nspectra * nxok));
2418 cpl_msg_info(_task,
"Average lost pixels at lower border: %.4g",
2419 lolost / (nspectra * nxok));
2420 cpl_msg_info(_task,
"Average spectrum width: %.4g +/- %.4g, "
2421 "(min, max) = (%.4g, %.4g)", sigmean, sigmask,
2422 cpl_matrix_get_min(mwid), cpl_matrix_get_max(mwid));
2424 cpl_matrix_delete(mwid);
2426 cpl_matrix_delete(mylo);
2427 cpl_matrix_delete(myup);
2428 cpl_matrix_delete(mxok);
2436 _giraffe_finalize_fibers(cpl_table *fibers, cpl_matrix *locy, GiImage *mlocy,
2437 cxdouble maxoffset, cxdouble* maxshift)
2449 cxdouble max_shift = 0.;
2450 cxdouble *positions = NULL;
2452 cpl_image *_mlocy = NULL;
2455 if (fibers == NULL || locy == NULL || mlocy == NULL) {
2459 if (cpl_table_has_column(fibers,
"RINDEX") == FALSE) {
2463 nx = cpl_matrix_get_ncol(locy);
2464 ny = cpl_matrix_get_nrow(locy);
2466 nfibers = cpl_table_get_nrow(fibers);
2469 _nx = cpl_image_get_size_x(_mlocy);
2470 _ny = cpl_image_get_size_y(_mlocy);
2476 if (nfibers > _nx) {
2480 cpl_table_select_all(fibers);
2487 irow = (_ny - 1) / 2;
2488 positions = (cxdouble *)cpl_image_get_data(_mlocy) + irow * _nx;
2499 for (i = 0; i < nfibers; i++) {
2503 cxint pos = cpl_table_get_int(fibers,
"RINDEX", i, NULL) - 1;
2505 cxdouble yc = cpl_matrix_get(locy, irow, j);
2506 cxdouble shift = fabs(yc - positions[pos]);
2508 if (shift <= maxoffset) {
2509 cpl_table_unselect_row(fibers, i);
2513 max_shift = CX_MAX(max_shift, shift);
2519 cpl_table_erase_selected(fibers);
2521 if (maxshift != NULL) {
2522 *maxshift = max_shift;
2560 GiTable *fibers, GiLocalization *master,
2561 GiImage *badpixels, GiLocalizeConfig *config)
2564 const cxchar *fctid =
"giraffe_localize_spectra";
2576 cxdouble conad = 0.;
2577 cxdouble bias_ron = 0.;
2578 cxdouble mask_sigma = 0.;
2582 cpl_propertylist *properties;
2586 cpl_image *_result = NULL;
2590 cpl_table *_fibers = NULL;
2591 cpl_table *fiber_setup = NULL;
2594 GiLocalizeMethod method;
2596 GiInstrumentMode mode;
2598 GiMaskParameters mask_config;
2600 GiMaskPosition mask_position;
2601 GiMaskPosition mask_coeffs;
2609 if (result == NULL || image == NULL || fibers == NULL || config == NULL) {
2610 cpl_error_set(fctid, CPL_ERROR_NULL_INPUT);
2614 if (badpixels != NULL) {
2615 cpl_msg_debug(fctid,
"Bad pixel correction is not available. Bad "
2616 "pixel map will be ignored.");
2621 if (_fibers == NULL) {
2622 cpl_error_set(fctid, CPL_ERROR_DATA_NOT_FOUND);
2626 fiber_setup = _fibers;
2636 nfibers = cpl_table_get_nrow(_fibers);
2638 cpl_msg_info(fctid,
"Setting number of fibers (%s) to %d",
2639 GIALIAS_NFIBERS, nfibers);
2641 cpl_propertylist_update_int(properties, GIALIAS_NFIBERS, nfibers);
2642 cpl_propertylist_set_comment(properties, GIALIAS_NFIBERS,
2643 "Number of fibres");
2646 if (!cpl_propertylist_has(properties, GIALIAS_CONAD)) {
2647 cpl_msg_error(fctid,
"Missing detector gain property (%s)! ",
2652 conad = cpl_propertylist_get_double(properties, GIALIAS_CONAD);
2660 if (config->ron > 0.) {
2661 cpl_msg_info(fctid,
"Setting bias sigma value (%s) to %.5g",
2662 GIALIAS_BIASSIGMA, config->ron);
2663 cpl_propertylist_update_double(properties, GIALIAS_BIASSIGMA,
2668 cpl_msg_info(fctid,
"Bias sigma value: %.3g e-", bias_ron);
2671 if (cpl_propertylist_has(properties, GIALIAS_DATANCOM)) {
2672 nframes = cpl_propertylist_get_int(properties, GIALIAS_DATANCOM);
2676 if (config->noise > 0.) {
2677 cpl_msg_info(fctid,
"Noise multiplier: %.3g",
2681 cpl_msg_info(fctid,
"Threshold multiplier: %.3g",
2682 fabs(config->noise));
2690 nrows = cpl_image_get_size_y(_image);
2692 if (config->start < 0) {
2693 config->start = nrows / 2;
2703 if (config->ywidth < 1) {
2705 cpl_msg_info(fctid,
"Configuring equilizing filter width from "
2710 config->ywidth = 16;
2722 cpl_msg_error(fctid,
"Invalid instrument mode!");
2728 if (!cpl_propertylist_has(properties, GIALIAS_SLITNAME)) {
2729 cpl_msg_error(fctid,
"Property (%s) not found in raw image",
2734 const cxchar *slit =
2735 cpl_propertylist_get_string(properties, GIALIAS_SLITNAME);
2737 cpl_msg_info(fctid,
"Setting equilizing filter to %d [pxl] "
2738 "for slit configuration `%s'", config->ywidth,
2751 mwidth = GISPECTRUM_MWIDTH_MEDUSA;
2760 mwidth = GISPECTRUM_MWIDTH_IFU;
2769 mwidth = GISPECTRUM_MWIDTH_IFU;
2778 cpl_msg_error(fctid,
"Invalid instrument mode!");
2788 if (config->centroid == TRUE) {
2789 method = GILOCALIZE_BARYCENTER;
2792 method = GILOCALIZE_HALF_WIDTH;
2801 mask_config.ywidth = config->ywidth;
2802 mask_config.method = config->threshold;
2803 mask_config.threshold = config->noise;
2804 mask_config.ydeg = config->yorder;
2805 mask_config.wdeg = config->worder;
2806 mask_config.ewid = config->ewidth;
2807 mask_config.wavg = mwidth;
2808 mask_config.ckdata.width = ckwidth;
2809 mask_config.ckdata.height = ckheight;
2810 mask_config.ckdata.count = ckcount;
2811 mask_config.sigma = config->sigma;
2812 mask_config.niter = config->iterations;
2813 mask_config.mfrac = config->fraction;
2814 mask_config.start = config->start;
2815 mask_config.retry = config->retries;
2816 mask_config.xbin = config->binsize;
2826 if (config->noise > 0.) {
2827 mask_config.threshold *= sqrt(nframes * conad);
2841 if (config->full != TRUE) {
2843 cpl_msg_info(fctid,
"Computing spectrum localization using SIWC "
2846 if (!master || !master->locy || !master->locy) {
2847 cpl_msg_error(fctid,
"Required full master localization is "
2858 cpl_table_unselect_all(_fibers);
2859 cpl_table_or_selected_int(_fibers,
"RP", CPL_EQUAL_TO, -1);
2861 fiber_setup = cpl_table_extract_selected(_fibers);
2862 nfibers = cpl_table_get_nrow(fiber_setup);
2872 mask_position.type = GIMASK_FITTED_DATA;
2873 mask_position.my = cpl_matrix_new(nrows, nfibers);
2874 mask_position.mw = cpl_matrix_new(nrows, nfibers);
2876 mask_coeffs.type = GIMASK_FIT_COEFFS;
2877 mask_coeffs.my = cpl_matrix_new(mask_config.ydeg + 1, nfibers);
2878 mask_coeffs.mw = cpl_matrix_new(1, (mask_config.wdeg + 1) *
2879 (mask_config.wdeg + 1));
2887 _image = cpl_image_multiply_scalar_create(_image, nframes * conad);
2889 mask_sigma = sqrt(nframes) * bias_ron;
2896 status = _giraffe_localize_spectra(_image, _bpixel, fiber_setup,
2897 method, config->normalize,
2899 &mask_config, &mask_position,
2902 cpl_image_delete(_image);
2906 result->locy = NULL;
2907 result->locw = NULL;
2908 result->locc = NULL;
2911 cpl_matrix_delete(mask_position.my);
2912 cpl_matrix_delete(mask_position.mw);
2914 cpl_matrix_delete(mask_coeffs.my);
2915 cpl_matrix_delete(mask_coeffs.mw);
2917 if (config->full != TRUE) {
2918 cpl_table_delete(fiber_setup);
2921 cpl_msg_error(fctid,
"Spectrum localization computation failed!");
2931 if (config->full != TRUE) {
2938 cpl_table_delete(fiber_setup);
2943 if (master != NULL && master->locy != NULL) {
2945 cxint nf = cpl_table_get_nrow(_fibers);
2947 cxdouble maxoffset = 0.5 * mask_config.wavg;
2948 cxdouble maxshift = 0.;
2951 cpl_msg_info(fctid,
"Comparing detected and expected fiber "
2954 status = _giraffe_finalize_fibers(_fibers, mask_position.my,
2955 master->locy, maxoffset,
2963 cxint _nf = cpl_image_get_size_x(mlocy);
2965 cpl_msg_error(fctid,
"More fibers (%d) than expected "
2966 "(%d) were found!", nf, _nf);
2970 result->locy = NULL;
2971 result->locw = NULL;
2972 result->locc = NULL;
2975 cpl_matrix_delete(mask_position.my);
2976 cpl_matrix_delete(mask_position.mw);
2978 cpl_matrix_delete(mask_coeffs.my);
2979 cpl_matrix_delete(mask_coeffs.mw);
2981 if (config->full != TRUE) {
2982 cpl_table_delete(fiber_setup);
2985 cpl_msg_error(fctid,
"Comparison of fiber positions "
2991 cx_assert(cpl_table_get_nrow(_fibers) <= nf);
2993 cpl_msg_info(fctid,
"%" CPL_SIZE_FORMAT
" of %d expected fibers "
2994 "were detected.", cpl_table_get_nrow(_fibers), nf);
2996 if (cpl_table_get_nrow(_fibers) < nf) {
2997 cpl_msg_debug(fctid,
"Maximum offset from the expected "
2998 "position is %.2f, maximum allowed offset is %.2f",
2999 maxshift, maxoffset);
3000 cpl_msg_warning(fctid,
"%" CPL_SIZE_FORMAT
" fibers are "
3001 "missing!", nf - cpl_table_get_nrow(_fibers));
3018 cpl_matrix_get_ncol(mask_position.my),
3019 cpl_matrix_get_nrow(mask_position.my));
3022 cpl_matrix_delete(mask_position.my);
3029 cpl_propertylist_set_int(properties, GIALIAS_NAXIS1,
3030 cpl_image_get_size_x(_result));
3031 cpl_propertylist_set_int(properties, GIALIAS_NAXIS2,
3032 cpl_image_get_size_y(_result));
3033 cpl_propertylist_set_int(properties, GIALIAS_BITPIX, -32);
3034 cpl_propertylist_set_double(properties, GIALIAS_BZERO, 0.);
3035 cpl_propertylist_set_double(properties, GIALIAS_BSCALE, 1.);
3037 cpl_propertylist_append_int(properties, GIALIAS_LOCNX,
3038 cpl_image_get_size_y(_result));
3039 cpl_propertylist_append_int(properties, GIALIAS_LOCNS,
3040 cpl_image_get_size_x(_result));
3042 if (config->centroid) {
3043 cpl_propertylist_append_string(properties, GIALIAS_LMETHOD,
3047 cpl_propertylist_append_string(properties, GIALIAS_LMETHOD,
3051 if (config->normalize) {
3052 cpl_propertylist_append_int(properties, GIALIAS_LNORMALIZE,
3056 cpl_propertylist_append_int(properties, GIALIAS_LNORMALIZE,
3060 cpl_propertylist_append_bool(properties, GIALIAS_LFULLLOC, config->full);
3061 cpl_propertylist_append_int(properties, GIALIAS_LOCYDEG, config->yorder);
3062 cpl_propertylist_append_int(properties, GIALIAS_LOCWDEG, config->worder);
3063 cpl_propertylist_append_double(properties, GIALIAS_LEXTRAWID,
3065 cpl_propertylist_append_double(properties, GIALIAS_LNOISEMULT,
3068 cpl_propertylist_append_double(properties, GIALIAS_LCLIPSIGMA,
3070 cpl_propertylist_append_int(properties, GIALIAS_LCLIPNITER,
3071 config->iterations);
3072 cpl_propertylist_append_double(properties, GIALIAS_LCLIPMFRAC,
3076 if (cpl_propertylist_has(properties, GIALIAS_GIRFTYPE)) {
3077 cpl_propertylist_set_string(properties, GIALIAS_GIRFTYPE,
"LOCY");
3080 cpl_propertylist_append_string(properties, GIALIAS_GIRFTYPE,
"LOCY");
3082 cpl_propertylist_set_comment(properties, GIALIAS_GIRFTYPE,
"GIRAFFE "
3083 "localization centroid");
3090 cpl_matrix_get_ncol(mask_position.mw),
3091 cpl_matrix_get_nrow(mask_position.mw));
3094 cpl_matrix_delete(mask_position.mw);
3101 cpl_propertylist_set_int(properties, GIALIAS_NAXIS1,
3102 cpl_image_get_size_x(_result));
3103 cpl_propertylist_set_int(properties, GIALIAS_NAXIS2,
3104 cpl_image_get_size_y(_result));
3106 if (cpl_propertylist_has(properties, GIALIAS_GIRFTYPE)) {
3107 cpl_propertylist_set_string(properties, GIALIAS_GIRFTYPE,
3111 cpl_propertylist_append_string(properties, GIALIAS_GIRFTYPE,
3114 cpl_propertylist_set_comment(properties, GIALIAS_GIRFTYPE,
"GIRAFFE "
3115 "localization half-width");
3120 locc = cpl_table_new(cpl_matrix_get_ncol(mask_coeffs.my));
3122 cpl_table_new_column(locc,
"BUTTON", CPL_TYPE_INT);
3123 for (i = 0; i < cpl_table_get_nrow(locc); i++) {
3124 cpl_table_set_int(locc,
"BUTTON", i, i);
3127 for (i = 0; i < cpl_matrix_get_nrow(mask_coeffs.my); i++) {
3128 cxchar *label = NULL;
3130 cx_asprintf(&label,
"YC%d", i);
3131 cpl_table_new_column(locc, label, CPL_TYPE_DOUBLE);
3137 cpl_table_delete(locc);
3139 _my = cpl_matrix_transpose_create(mask_coeffs.my);
3141 cpl_matrix_delete(_my);
3142 cpl_matrix_delete(mask_coeffs.my);
3149 pname = cx_string_new();
3151 for (i = 0; i < cpl_matrix_get_ncol(mask_coeffs.mw); i++) {
3152 cx_string_sprintf(pname,
"%s%d", GIALIAS_LOCWIDCOEF, i);
3153 cpl_propertylist_append_double(properties, cx_string_get(pname),
3154 cpl_matrix_get(mask_coeffs.mw, 0, i));
3157 cx_string_delete(pname);
3158 cpl_matrix_delete(mask_coeffs.mw);
3160 cpl_propertylist_update_string(properties, GIALIAS_GIRFTYPE,
3162 cpl_propertylist_set_comment(properties, GIALIAS_GIRFTYPE,
"GIRAFFE "
3163 "localization fit coefficients");
3192 GiLocalizeConfig *config = NULL;
3199 config = cx_calloc(1,
sizeof *config);
3206 config->full = TRUE;
3207 config->centroid = TRUE;
3208 config->threshold = GILOCALIZE_THRESHOLD_LOCAL;
3211 p = cpl_parameterlist_find(list,
"giraffe.localization.mode");
3212 s = cpl_parameter_get_string(p);
3213 if (strcmp(s,
"siwc") == 0) {
3214 config->full = FALSE;
3217 p = cpl_parameterlist_find(list,
"giraffe.localization.start");
3218 config->start = cpl_parameter_get_int(p);
3220 p = cpl_parameterlist_find(list,
"giraffe.localization.retries");
3221 config->retries = cpl_parameter_get_int(p);
3223 p = cpl_parameterlist_find(list,
"giraffe.localization.binsize");
3224 config->binsize = cpl_parameter_get_int(p);
3226 p = cpl_parameterlist_find(list,
"giraffe.localization.ewidth");
3227 config->ewidth = cpl_parameter_get_double(p);
3229 p = cpl_parameterlist_find(list,
"giraffe.localization.ywidth");
3230 config->ywidth = cpl_parameter_get_int(p);
3232 p = cpl_parameterlist_find(list,
"giraffe.localization.center");
3233 s = cpl_parameter_get_string(p);
3234 if (!strcmp(s,
"hwidth")) {
3235 config->centroid = FALSE;
3238 p = cpl_parameterlist_find(list,
"giraffe.localization.normalize");
3239 config->normalize = cpl_parameter_get_bool(p);
3241 p = cpl_parameterlist_find(list,
"giraffe.localization.threshold");
3242 s = cpl_parameter_get_string(p);
3244 if (strncmp(s,
"global", 6) == 0) {
3245 config->threshold = GILOCALIZE_THRESHOLD_GLOBAL;
3247 else if (strncmp(s,
"row", 3) == 0) {
3248 config->threshold = GILOCALIZE_THRESHOLD_ROW;
3251 config->threshold = GILOCALIZE_THRESHOLD_LOCAL;
3254 p = cpl_parameterlist_find(list,
"giraffe.localization.noise");
3255 config->noise = cpl_parameter_get_double(p);
3257 p = cpl_parameterlist_find(list,
"giraffe.localization.ron");
3258 config->ron = cpl_parameter_get_double(p);
3260 p = cpl_parameterlist_find(list,
"giraffe.localization.yorder");
3261 config->yorder = cpl_parameter_get_int(p);
3263 p = cpl_parameterlist_find(list,
"giraffe.localization.worder");
3264 config->worder = cpl_parameter_get_int(p);
3266 p = cpl_parameterlist_find(list,
"giraffe.localization.sigma");
3267 config->sigma = cpl_parameter_get_double(p);
3269 p = cpl_parameterlist_find(list,
"giraffe.localization.iterations");
3270 config->iterations = cpl_parameter_get_int(p);
3272 p = cpl_parameterlist_find(list,
"giraffe.localization.fraction");
3273 config->fraction = cpl_parameter_get_double(p);
3327 p = cpl_parameter_new_enum(
"giraffe.localization.mode",
3329 "Localization mode: Use all spectra "
3330 "or the 5 SIWC spectra",
3331 "giraffe.localization",
3332 "all", 2,
"all",
"siwc");
3333 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI,
"sloc-mode");
3334 cpl_parameterlist_append(list, p);
3337 p = cpl_parameter_new_value(
"giraffe.localization.start",
3340 "giraffe.localization",
3342 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI,
"sloc-start");
3343 cpl_parameterlist_append(list, p);
3346 p = cpl_parameter_new_value(
"giraffe.localization.retries",
3348 "Initial localization detection "
3350 "giraffe.localization",
3352 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI,
"sloc-retries");
3353 cpl_parameterlist_append(list, p);
3356 p = cpl_parameter_new_value(
"giraffe.localization.binsize",
3358 "Initial localization detection "
3360 "giraffe.localization",
3362 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI,
"sloc-binsize");
3363 cpl_parameterlist_append(list, p);
3366 p = cpl_parameter_new_value(
"giraffe.localization.ewidth",
3368 "Localization detection extra width.",
3369 "giraffe.localization",
3371 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI,
"sloc-ewidth");
3372 cpl_parameterlist_append(list, p);
3375 p = cpl_parameter_new_value(
"giraffe.localization.ywidth",
3377 "Full width [pxl] of the equilizing "
3378 "filter (distance between two "
3379 "adjacent fibers).",
3380 "giraffe.localization",
3382 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI,
"sloc-ywidth");
3383 cpl_parameterlist_append(list, p);
3386 p = cpl_parameter_new_enum(
"giraffe.localization.center",
3388 "Method used for mask center "
3390 "giraffe.localization",
3391 "centroid", 2,
"centroid",
3393 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI,
"sloc-center");
3394 cpl_parameterlist_append(list, p);
3397 p = cpl_parameter_new_value(
"giraffe.localization.normalize",
3399 "Enable spectrum normalization along "
3400 "the dispersion axis.",
3401 "giraffe.localization",
3403 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI,
"sloc-norm");
3404 cpl_parameterlist_append(list, p);
3407 p = cpl_parameter_new_value(
"giraffe.localization.noise",
3409 "Threshold multiplier.",
3410 "giraffe.localization",
3412 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI,
"sloc-noise");
3413 cpl_parameterlist_append(list, p);
3416 p = cpl_parameter_new_enum(
"giraffe.localization.threshold",
3418 "Selects thresholding algorithm: local, "
3420 "giraffe.localization",
3421 "local", 3,
"local",
"row",
"global");
3422 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI,
"sloc-threshold");
3423 cpl_parameterlist_append(list, p);
3426 p = cpl_parameter_new_value(
"giraffe.localization.ron",
3428 "New bias sigma (RON) value for dark "
3430 "giraffe.localization",
3432 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI,
"sloc-ron");
3433 cpl_parameterlist_append(list, p);
3436 p = cpl_parameter_new_value(
"giraffe.localization.yorder",
3438 "Order of Chebyshev polynomial fit.",
3439 "giraffe.localization",
3441 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI,
"sloc-yorder");
3442 cpl_parameterlist_append(list, p);
3445 p = cpl_parameter_new_value(
"giraffe.localization.worder",
3447 "Order of Chebyshev 2D polynomial fit.",
3448 "giraffe.localization",
3450 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI,
"sloc-worder");
3451 cpl_parameterlist_append(list, p);
3454 p = cpl_parameter_new_value(
"giraffe.localization.sigma",
3456 "Localization clipping: sigma threshold "
3458 "giraffe.localization",
3460 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI,
"sloc-sigma");
3461 cpl_parameterlist_append(list, p);
3464 p = cpl_parameter_new_value(
"giraffe.localization.iterations",
3466 "Localization clipping: number of "
3468 "giraffe.localization",
3470 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI,
"sloc-niter");
3471 cpl_parameterlist_append(list, p);
3474 p = cpl_parameter_new_range(
"giraffe.localization.fraction",
3476 "Localization clipping: minimum fraction "
3477 "of points accepted/total.",
3478 "giraffe.localization",
3480 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI,
"sloc-mfrac");
3481 cpl_parameterlist_append(list, p);