29#include <cxmessages.h>
32#include <cpl_vector.h>
33#include <cpl_matrix.h>
35#include <cpl_parameterlist.h>
48#include "gimessages.h"
50#include "gilocalize.h"
67static const cxchar* _task =
"giraffe_localize_spectra";
76 GILOCALIZE_HALF_WIDTH,
80typedef enum GiLocalizeMethod GiLocalizeMethod;
89 GILOCALIZE_THRESHOLD_GLOBAL,
90 GILOCALIZE_THRESHOLD_LOCAL,
91 GILOCALIZE_THRESHOLD_ROW
94typedef enum GiThresholdMethod GiThresholdMethod;
131_giraffe_validate_pixel(cxint *pixels, cxint xsize, cxint ysize,
132 cxint xpos, cxint ypos, cxint xwidth, cxint ywidth,
137 cxint xstart = xpos - xwidth;
138 cxint ystart = ypos - ywidth;
139 cxint xend = xpos + xwidth;
140 cxint yend = ypos + ywidth;
150 xstart = CX_MAX(0, xstart);
151 ystart = CX_MAX(0, ystart);
153 xend = CX_MIN(xsize - 1, xend);
154 yend = CX_MIN(ysize - 1, yend);
156 xwidth = CX_MAX(xwidth,1 );
157 ywidth = CX_MAX(ywidth,1 );
165 for (i = ystart; i <= yend; i++) {
184 for (j = xstart; j <= xend; j++) {
185 if (pixels[row + j]) {
189 if (_count >= count) {
228inline static cpl_matrix*
229_giraffe_fit_border(cpl_matrix* mborder, cpl_matrix* mbase,
230 cpl_matrix* mxok, cxint nspectra, cxdouble sigma,
231 cxint niter, cxdouble mfrac, cpl_matrix* mcoeff)
234 const cxchar*
const fctid =
"_giraffe_fit_border";
236 register cxint x = 0;
237 register cxint naccept = 0;
238 register cxint ntotal = 0;
239 register cxint iteration = 0;
240 register cxint nx = cpl_matrix_get_ncol(mbase);
241 register cxint yorder = cpl_matrix_get_nrow(mbase);
242 register cxint nxok = cpl_matrix_get_nrow(mxok);
244 register cxdouble ratio = 1.0;
246 cpl_matrix* mtmp = NULL;
247 cpl_matrix* yraw = NULL;
248 cpl_matrix* ydiff = NULL;
249 cpl_matrix* mfit = NULL;
250 cpl_matrix* coeffs = NULL;
255 cpl_error_set(fctid, CPL_ERROR_INCOMPATIBLE_INPUT);
257 GIDEBUG(
gi_warning(
"%s: not enough points mxok[%d] for %d order fit",
258 fctid, nxok, yorder));
268 yraw = cpl_matrix_new(1, nxok);
269 ydiff = cpl_matrix_new(nxok, 1);
271 mtmp = cpl_matrix_duplicate(mxok);
277 for (x = 0; x < nxok; x++) {
278 cxdouble data = cpl_matrix_get(mborder, x, nspectra);
279 cpl_matrix_set(yraw, 0, x, data);
290 while (naccept > 0 && iteration < niter && ratio > mfrac) {
292 register cxint k = 0;
293 register cxint l = 0;
295 register cxdouble ysigma = 0.;
297 cpl_matrix* rawbase = giraffe_chebyshev_base1d(0., nx, yorder, mtmp);
298 cx_assert(rawbase != NULL);
300 if (coeffs != NULL) {
301 cpl_matrix_delete(coeffs);
305 if (coeffs == NULL) {
306 gi_warning(
"%s: error in giraffe_matrix_leastsq(), spectrum %d",
311 cpl_matrix_delete(rawbase);
315 cpl_matrix_delete(mfit);
318 mfit = cpl_matrix_product_create(coeffs, mbase);
320 for (x = 0; x < cpl_matrix_get_nrow(ydiff); x++) {
322 cxint xok = (cxint) cpl_matrix_get(mtmp, x, 0);
325 cpl_matrix_get(yraw, 0, x) - cpl_matrix_get(mfit, 0, xok);
328 cpl_matrix_set(ydiff, x , 0, diff);
340 for (l = 0; l < cpl_matrix_get_nrow(ydiff); l++) {
342 if (fabs(cpl_matrix_get(ydiff, l, 0)) <= ysigma) {
344 cxint xok = cpl_matrix_get(mtmp, l, 0);
345 cxdouble data = cpl_matrix_get(yraw, 0, l);
347 cpl_matrix_set(mtmp, k, 0, xok);
348 cpl_matrix_set(yraw, 0, k, data);
370 ratio = (cxdouble) naccept / (cxdouble) ntotal;
372 GIDEBUG(
gi_message(
"Iteration %d: Sigma %f, accepted bins: %d, "
373 "rejected %d\n", iteration, ysigma, naccept,
380 cpl_matrix_resize(mtmp, 0,
381 naccept - cpl_matrix_get_nrow(mtmp), 0, 0);
382 cpl_matrix_resize(yraw, 0,
383 0, 0, naccept - cpl_matrix_get_ncol(yraw));
384 cpl_matrix_resize(ydiff, 0,
385 naccept - cpl_matrix_get_nrow(ydiff), 0, 0);
390 if (coeffs != NULL) {
393 for (l = 0; l < cpl_matrix_get_nrow(mcoeff); l++) {
394 cpl_matrix_set(mcoeff, l, 0, cpl_matrix_get(coeffs, 0, l));
403 cpl_matrix_delete(coeffs);
404 cpl_matrix_delete(ydiff);
405 cpl_matrix_delete(yraw);
406 cpl_matrix_delete(mtmp);
414inline static cpl_image*
415_giraffe_filter_gauss1d(
const cpl_image* image, cxint radius, cxdouble width)
418 cxdouble w2 = width * width;
422 cpl_matrix* kernel = cpl_matrix_new(1, 2 * radius + 1);
424 cpl_image* fimage = NULL;
427 if (kernel == NULL) {
431 for (i = -radius; i <= radius; ++i) {
433 cxdouble y = exp(-x2 / (2. * w2));
435 cpl_matrix_set(kernel, 0, i + radius, y);
439 fimage = cpl_image_new(cpl_image_get_size_x(image),
440 cpl_image_get_size_y(image),
441 cpl_image_get_type(image));
443 if (fimage == NULL) {
444 cpl_matrix_delete(kernel);
448 cpl_image_filter(fimage, image, kernel, CPL_FILTER_LINEAR,
450 cpl_matrix_delete(kernel);
457inline static cpl_image*
458_giraffe_filter_sobel(
const cpl_image* image, cxbool vertical)
460 cpl_matrix* kernel = cpl_matrix_new(3, 3);
462 cpl_image* fimage = NULL;
465 if (kernel == NULL) {
472 cpl_matrix_set(kernel, 0, 0, -1);
473 cpl_matrix_set(kernel, 1, 0, -2);
474 cpl_matrix_set(kernel, 2, 0, -1);
476 cpl_matrix_set(kernel, 0, 2, 1);
477 cpl_matrix_set(kernel, 1, 2, 2);
478 cpl_matrix_set(kernel, 2, 2, 1);
480 cpl_matrix_set(kernel, 0, 0, 0);
481 cpl_matrix_set(kernel, 1, 0, -0.5);
482 cpl_matrix_set(kernel, 2, 0, 0);
484 cpl_matrix_set(kernel, 0, 2, 0);
485 cpl_matrix_set(kernel, 1, 2, 0.5);
486 cpl_matrix_set(kernel, 2, 2, 0);
491 cpl_matrix_set(kernel, 0, 0, 1);
492 cpl_matrix_set(kernel, 0, 1, 2);
493 cpl_matrix_set(kernel, 0, 2, 1);
495 cpl_matrix_set(kernel, 2, 0, -1);
496 cpl_matrix_set(kernel, 2, 1, -2);
497 cpl_matrix_set(kernel, 2, 2, -1);
501 fimage = cpl_image_new(cpl_image_get_size_x(image),
502 cpl_image_get_size_y(image),
503 cpl_image_get_type(image));
505 if (fimage == NULL) {
506 cpl_matrix_delete(kernel);
510 cpl_image_filter(fimage, image, kernel, CPL_FILTER_LINEAR,
512 cpl_matrix_delete(kernel);
521_giraffe_build_edge_mask(cpl_image* raw, cpl_image* bpixel, cxint nspectra,
522 cxdouble noise, GiMaskParameters* config,
523 cxint* ndetect, cpl_matrix* mxok, cpl_matrix* myup,
527 const cxint margin = 5;
535 cxint nrows = cpl_image_get_size_y(raw);
536 cxint ncols = cpl_image_get_size_x(raw);
540 cxdouble* buffer = NULL;
542 cpl_mask* kernel = NULL;
544 cpl_image* fraw = NULL;
545 cpl_image* sraw = NULL;
546 cpl_image* vertical1 = NULL;
547 cpl_image* vertical2 = NULL;
548 cpl_image* center = NULL;
561 kernel = cpl_mask_new(1, 15);
563 if (kernel != NULL) {
565 cpl_mask_not(kernel);
567 fraw = cpl_image_new(ncols, nrows, cpl_image_get_type(raw));
570 cpl_mask_delete(kernel);
576 cpl_image_filter_mask(fraw, raw, kernel, CPL_FILTER_MEDIAN,
581 cpl_mask_delete(kernel);
585 sraw = _giraffe_filter_gauss1d(fraw, 6, 1.);
589 cpl_image_delete(fraw);
596 vertical1 = _giraffe_filter_sobel(sraw, TRUE);
597 vertical2 = _giraffe_filter_sobel(vertical1, TRUE);
599 cpl_image_save(sraw,
"master_flat_smooth.fits", -32, 0, CPL_IO_DEFAULT);
600 cpl_image_save(vertical1,
"vertical.fits", -32, 0, CPL_IO_DEFAULT);
601 cpl_image_save(vertical2,
"vertical2.fits", -32, 0, CPL_IO_DEFAULT);
608 center = cpl_image_new(ncols, nrows, CPL_TYPE_INT);
610 flags = cx_calloc(ncols,
sizeof(cxint));
611 buffer = cx_calloc(ncols,
sizeof(cxdouble));
613 if ((center == NULL) || (flags ==NULL) || (buffer == NULL)) {
621 cpl_image_delete(center);
624 cpl_image_delete(vertical2);
627 cpl_image_delete(vertical1);
630 cpl_image_delete(sraw);
633 cpl_image_delete(fraw);
641 for (m = 0; m < nrows; ++m) {
643 register cxint irow = m * ncols;
644 register cxint n = 0;
649 cxint* _center = cpl_image_get_data_int(center) + irow;
651 const cxdouble* _vt1 = cpl_image_get_data_double_const(vertical1) +
653 const cxdouble* _vt2 = cpl_image_get_data_double_const(vertical2) +
655 const cxdouble* _fraw = cpl_image_get_data_double_const(fraw) +
659 memset(buffer, 0, ncols *
sizeof(cxdouble));
660 memset(flags, 0, ncols *
sizeof(cxint));
663 for (n = 0; n < ncols; ++n) {
673 if ((n - 1 >= 0) && (_vt2[n - 1] > 0.)) {
674 buffer[n - 1] = _vt1[n - 1];
676 if ((n + 1 < ncols) && (_vt2[n + 1] > 0.)) {
677 buffer[n + 1] = _vt1[n + 1];
683 while (iteration < ncols) {
687 cxdouble dx = 3. * 2. * noise;
690 for (n = 0; n < ncols; ++n) {
692 if (!flags[n] && (buffer[n] > dx)) {
702 register cxint k = 0;
709 cxdouble signal = 0.;
715 while ((k >= 0) && (buffer[k] > 0.)) {
722 while ((k < ncols) && (buffer[k] > 0.)) {
728 while ((k < ncols) && (buffer[k] < 0.)) {
733 width = end - start + 1;
741 signal = (_fraw[pos] > 0.) ? _fraw[pos] : 0.;
742 sigma = sqrt((noise * noise + signal) / config->xbin);
744 if ((signal / sigma > 10.) && (width > 1)) {
746 start = (start == pos) ? start - 1 : start;
747 end = (end == pos) ? end + 1 : end;
750 _center[start] += -1;
761 for (n = 0; n < ncols; ++n) {
763 if (_center[n] == 1) {
769 if (scount >= smax) {
785 cx_print(
"scount: %d (%d) at %d\n", smax, nspectra, mmax);
793 const cxint limit = 0.85 * nrows;
798 const cxdouble hwf = sqrt(2. * log(2.));
800 cxint* xtrace = cx_calloc(nrows,
sizeof(cxint));
801 cxint* ytrace = cx_calloc(nrows,
sizeof(cxint));
803 cpl_image* mask = cpl_image_new(ncols, nrows, CPL_TYPE_INT);
805 for (m = 0; m < ncols; ++m) {
807 const cxint* _center = cpl_image_get_data_int(center);
808 const cxint* _reference = _center + mmax * ncols;
810 cxbool out_of_bounds = FALSE;
815 if (_reference[m] == 1) {
817 register cxint j = mmax;
818 register cxint pos = m;
823 xtrace[connected] = pos;
824 ytrace[connected] = j;
830 register cxint k = 0;
831 register cxint l = j * ncols;
832 register cxint kmin = (pos - 1 >= 0) ? pos - 1 : 0;
833 register cxint kmax = (pos + 1 < ncols) ? pos + 1 : ncols - 1;
835 for (k = kmin; k <= kmax; ++k) {
837 if (_center[l + k] == 1) {
839 if ((pos <= margin) || (pos >= ncols - margin)) {
840 out_of_bounds = TRUE;
844 xtrace[connected] = k;
845 ytrace[connected] = j;
862 register cxint k = 0;
863 register cxint l = j * ncols;
864 register cxint kmin = (pos - 1 >= 0) ? pos - 1 : 0;
865 register cxint kmax = (pos + 1 < ncols) ? pos + 1 : ncols - 1;
867 for (k = kmin; k <= kmax; ++k) {
869 if (_center[l + k] == 1) {
871 if ((pos <= margin) || (pos >= ncols - margin)) {
872 out_of_bounds = TRUE;
876 xtrace[connected] = k;
877 ytrace[connected] = j;
889 if ((connected < limit) || (out_of_bounds == TRUE)) {
891 memset(xtrace, 0, nrows *
sizeof(cxint));
892 memset(ytrace, 0, nrows *
sizeof(cxint));
894 if (out_of_bounds == TRUE) {
895 cx_print(
"discarded candidate %d, going out of detector "
896 "boundaries.\n", itrace);
900 cx_print(
"discarded candidate %d, not enough connected "
901 "centers (%d, required: %d)\n", itrace, connected,
908 cxint* _mask = cpl_image_get_data_int(mask);
910 for (j = 0; j < connected; ++j) {
912 register cxint x = xtrace[j];
913 register cxint y = ytrace[j] * ncols;
914 register cxint ix = x;
918 while ((_center[y + ix] != -1) && (ix > 0)) {
924 while ((_center[y + ix] != -2) && (ix < ncols - 1)) {
939 cx_print(
"scount: %d (expected: %d)\n", ispectra, nspectra);
947 for (m = 0; m < nrows; ++m) {
949 register cxint j = 0;
950 register cxint ns = 0;
952 const cxint* _mask = cpl_image_get_data_int(mask) + m * ncols;
953 const cxint* _center = cpl_image_get_data_int(center) + m * ncols;
956 for (j = 0; j < ncols; ++j) {
960 register cxint x = j;
961 register cxint ix = x;
964 while ((_center[ix] != -1) && (ix > 0)) {
967 cpl_matrix_set(mylo, naccepted, ns, x - hwf * fabs(x - ix));
970 while ((_center[ix] != -2) && (ix < ncols - 1)) {
973 cpl_matrix_set(myup, naccepted, ns, x + hwf * fabs(ix - x));
980 if (ns == ispectra) {
981 cpl_matrix_set(mxok, naccepted, 0, m);
990 cpl_image_save(center,
"center.fits", -32, 0, CPL_IO_DEFAULT);
991 cpl_image_save(mask,
"mask.fits", -32, 0, CPL_IO_DEFAULT);
993 cpl_image_delete(mask);
994 cpl_image_delete(center);
995 cpl_image_delete(vertical2);
996 cpl_image_delete(vertical1);
997 cpl_image_delete(sraw);
998 cpl_image_delete(fraw);
1039_giraffe_build_raw_mask(cpl_image *raw, cpl_image *bpixel, cxint nspectra,
1040 cxdouble noise, GiMaskParameters *config,
1041 cxint *ndetect, cpl_matrix *mxok, cpl_matrix *myup,
1045 register cxint x = 0;
1046 register cxint y = 0;
1047 register cxint xretry = 0;
1048 register cxint xok = 0;
1053 cxint *yabove = NULL;
1054 cxint *ybelow = NULL;
1055 cxint *good_pixels = NULL;
1056 cxint ywidth = config->ywidth > 1 ? config->ywidth : 2;
1057 cxint ckwidth = config->ckdata.width;
1058 cxint ckheight = config->ckdata.height;
1059 cxint ckcount = config->ckdata.count;
1062 cxdouble* pixels = NULL;
1064 cpl_mask* med = NULL;
1066 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);
2301 cpl_matrix_delete(mxok);
2302 cpl_matrix_delete(myup);
2303 cpl_matrix_delete(mylo);
2307 cpl_msg_warning(_task,
"Invalid number of spectra detected: "
2308 "%d != %d", ndetect, nspectra);
2312 cpl_msg_warning(_task,
"No abcissa with good number "
2317 cpl_msg_warning(_task,
"Error while searching for spectra");
2325 cpl_msg_info(_task,
"%d spectra detected in %d wavelength bins",
2334 cpl_matrix_resize(mxok, 0, nxok - cpl_matrix_get_nrow(mxok), 0, 0);
2335 cpl_matrix_resize(myup, 0, nxok - cpl_matrix_get_nrow(myup), 0, 0);
2336 cpl_matrix_resize(mylo, 0, nxok - cpl_matrix_get_nrow(mylo), 0, 0);
2338 GIDEBUG(
gi_message(
"%s: mxok[0-%d]=[%g-%g]", __func__,
2339 cpl_matrix_get_nrow(mxok) - 1,
2340 cpl_matrix_get_min(mxok),
2341 cpl_matrix_get_max(mxok)));
2344 cpl_msg_info(_task,
"Computing spectrum positions and widths in "
2345 "pixel range [%g,%g]", cpl_matrix_get_min(mxok),
2346 cpl_matrix_get_max(mxok));
2348 if (cpl_matrix_get_nrow(mxok) <= config->ydeg) {
2349 cpl_msg_info(_task,
"Not enough data points %" CPL_SIZE_FORMAT
2350 " for %d order fit", cpl_matrix_get_nrow(mxok),
2357 case GILOCALIZE_HALF_WIDTH:
2358 cpl_msg_info(_task,
"Using half-width for localization");
2359 _giraffe_fit_raw_mask(mxok, myup, mylo, fibers, config,
2363 case GILOCALIZE_BARYCENTER:
2365 cpl_msg_info(_task,
"Using barycenter for localization");
2366 _giraffe_fit_raw_centroid(mz, mxok, myup, mylo, fibers, config,
2371 if (normalize == 1) {
2372 cpl_image_delete(mznorm);
2385 mwid = cpl_matrix_new(nxok, nspectra);
2387 for (n = 0, nn = 0; nn < cpl_table_get_nrow(fibers); nn++) {
2389 for (x = 0; x < nxok; x++) {
2390 register cxint lx = (cxint) cpl_matrix_get(mxok, x, 0);
2392 cxdouble lower = cpl_matrix_get(mylo, x, n);
2393 cxdouble upper = cpl_matrix_get(myup, x, n);
2394 cxdouble width = cpl_matrix_get(position->mw, lx, nn);
2396 uplost += cpl_matrix_get(position->my, lx, nn) + width - upper;
2397 lolost += cpl_matrix_get(position->my, lx, nn) - width - lower;
2399 avgborders += upper - lower;
2402 cpl_matrix_set(mwid, x, n, 2. * width);
2407 sigmean = cpl_matrix_get_mean(mwid);
2409 avglost = (lolost + uplost) / (nspectra * nxok);
2410 avgmask = 2.0 * avgmask / nspectra;
2412 cpl_msg_info(_task,
"Mask was computed using %d of %d wavelength bins",
2414 cpl_msg_info(_task,
"Average # of pixels per spectra: %.4g",
2416 cpl_msg_info(_task,
"Average # of in-borders pixels per spectra: %.4g",
2417 avgborders / nspectra);
2418 cpl_msg_info(_task,
"Average lost pixels per spectra: %.4g",
2420 cpl_msg_info(_task,
"Average lost pixels at upper border: %.4g",
2421 uplost / (nspectra * nxok));
2422 cpl_msg_info(_task,
"Average lost pixels at lower border: %.4g",
2423 lolost / (nspectra * nxok));
2424 cpl_msg_info(_task,
"Average spectrum width: %.4g +/- %.4g, "
2425 "(min, max) = (%.4g, %.4g)", sigmean, sigmask,
2426 cpl_matrix_get_min(mwid), cpl_matrix_get_max(mwid));
2428 cpl_matrix_delete(mwid);
2430 cpl_matrix_delete(mylo);
2431 cpl_matrix_delete(myup);
2432 cpl_matrix_delete(mxok);
2440_giraffe_finalize_fibers(cpl_table *fibers, cpl_matrix *locy, GiImage *mlocy,
2441 cxdouble maxoffset, cxdouble* maxshift)
2453 cxdouble max_shift = 0.;
2454 cxdouble *positions = NULL;
2456 cpl_image *_mlocy = NULL;
2459 if (fibers == NULL || locy == NULL || mlocy == NULL) {
2463 if (cpl_table_has_column(fibers,
"RINDEX") == FALSE) {
2467 nx = cpl_matrix_get_ncol(locy);
2468 ny = cpl_matrix_get_nrow(locy);
2470 nfibers = cpl_table_get_nrow(fibers);
2473 _nx = cpl_image_get_size_x(_mlocy);
2474 _ny = cpl_image_get_size_y(_mlocy);
2480 if (nfibers > _nx) {
2484 cpl_table_select_all(fibers);
2491 irow = (_ny - 1) / 2;
2492 positions = (cxdouble *)cpl_image_get_data(_mlocy) + irow * _nx;
2503 for (i = 0; i < nfibers; i++) {
2507 cxint pos = cpl_table_get_int(fibers,
"RINDEX", i, NULL) - 1;
2509 cxdouble yc = cpl_matrix_get(locy, irow, j);
2510 cxdouble shift = fabs(yc - positions[pos]);
2512 if (shift <= maxoffset) {
2513 cpl_table_unselect_row(fibers, i);
2517 max_shift = CX_MAX(max_shift, shift);
2523 cpl_table_erase_selected(fibers);
2525 if (maxshift != NULL) {
2526 *maxshift = max_shift;
2564 GiTable *fibers, GiLocalization *master,
2565 GiImage *badpixels, GiLocalizeConfig *config)
2568 const cxchar *fctid =
"giraffe_localize_spectra";
2580 cxdouble conad = 0.;
2581 cxdouble bias_ron = 0.;
2582 cxdouble mask_sigma = 0.;
2586 cpl_propertylist *properties;
2590 cpl_image *_result = NULL;
2594 cpl_table *_fibers = NULL;
2595 cpl_table *fiber_setup = NULL;
2598 GiLocalizeMethod method;
2600 GiInstrumentMode mode;
2602 GiMaskParameters mask_config;
2604 GiMaskPosition mask_position;
2605 GiMaskPosition mask_coeffs;
2613 if (result == NULL || image == NULL || fibers == NULL || config == NULL) {
2614 cpl_error_set(fctid, CPL_ERROR_NULL_INPUT);
2618 if (badpixels != NULL) {
2619 cpl_msg_debug(fctid,
"Bad pixel correction is not available. Bad "
2620 "pixel map will be ignored.");
2625 if (_fibers == NULL) {
2626 cpl_error_set(fctid, CPL_ERROR_DATA_NOT_FOUND);
2630 fiber_setup = _fibers;
2640 nfibers = cpl_table_get_nrow(_fibers);
2642 cpl_msg_info(fctid,
"Setting number of fibers (%s) to %d",
2643 GIALIAS_NFIBERS, nfibers);
2645 cpl_propertylist_update_int(properties, GIALIAS_NFIBERS, nfibers);
2646 cpl_propertylist_set_comment(properties, GIALIAS_NFIBERS,
2647 "Number of fibres");
2650 giraffe_error_push();
2654 if (cpl_error_get_code() != CPL_ERROR_NONE) {
2658 giraffe_error_pop();
2665 if (config->ron > 0.) {
2666 cpl_msg_info(fctid,
"Setting bias sigma value (%s) to %.5g",
2667 GIALIAS_BIASSIGMA, config->ron);
2668 cpl_propertylist_update_double(properties, GIALIAS_BIASSIGMA,
2673 cpl_msg_info(fctid,
"Bias sigma value: %.3g e-", bias_ron);
2676 if (cpl_propertylist_has(properties, GIALIAS_DATANCOM)) {
2677 nframes = cpl_propertylist_get_int(properties, GIALIAS_DATANCOM);
2681 if (config->noise > 0.) {
2682 cpl_msg_info(fctid,
"Noise multiplier: %.3g",
2686 cpl_msg_info(fctid,
"Threshold multiplier: %.3g",
2687 fabs(config->noise));
2695 nrows = cpl_image_get_size_y(_image);
2697 if (config->start < 0) {
2698 config->start = nrows / 2;
2708 if (config->ywidth < 1) {
2710 cpl_msg_info(fctid,
"Configuring equilizing filter width from "
2715 config->ywidth = 16;
2727 cpl_msg_error(fctid,
"Invalid instrument mode!");
2733 if (!cpl_propertylist_has(properties, GIALIAS_SLITNAME)) {
2734 cpl_msg_error(fctid,
"Property (%s) not found in raw image",
2739 const cxchar *slit =
2740 cpl_propertylist_get_string(properties, GIALIAS_SLITNAME);
2742 cpl_msg_info(fctid,
"Setting equilizing filter to %d [pxl] "
2743 "for slit configuration `%s'", config->ywidth,
2756 mwidth = GISPECTRUM_MWIDTH_MEDUSA;
2765 mwidth = GISPECTRUM_MWIDTH_IFU;
2774 mwidth = GISPECTRUM_MWIDTH_IFU;
2783 cpl_msg_error(fctid,
"Invalid instrument mode!");
2793 if (config->centroid == TRUE) {
2794 method = GILOCALIZE_BARYCENTER;
2797 method = GILOCALIZE_HALF_WIDTH;
2806 mask_config.ywidth = config->ywidth;
2807 mask_config.method = config->threshold;
2808 mask_config.threshold = config->noise;
2809 mask_config.ydeg = config->yorder;
2810 mask_config.wdeg = config->worder;
2811 mask_config.ewid = config->ewidth;
2812 mask_config.wavg = mwidth;
2813 mask_config.ckdata.width = ckwidth;
2814 mask_config.ckdata.height = ckheight;
2815 mask_config.ckdata.count = ckcount;
2816 mask_config.sigma = config->sigma;
2817 mask_config.niter = config->iterations;
2818 mask_config.mfrac = config->fraction;
2819 mask_config.start = config->start;
2820 mask_config.retry = config->retries;
2821 mask_config.xbin = config->binsize;
2831 if (config->noise > 0.) {
2832 mask_config.threshold *= sqrt(nframes * conad);
2846 if (config->full != TRUE) {
2848 cpl_msg_info(fctid,
"Computing spectrum localization using SIWC "
2851 if (!master || !master->locy || !master->locy) {
2852 cpl_msg_error(fctid,
"Required full master localization is "
2863 cpl_table_unselect_all(_fibers);
2864 cpl_table_or_selected_int(_fibers,
"RP", CPL_EQUAL_TO, -1);
2866 fiber_setup = cpl_table_extract_selected(_fibers);
2867 nfibers = cpl_table_get_nrow(fiber_setup);
2877 mask_position.type = GIMASK_FITTED_DATA;
2878 mask_position.my = cpl_matrix_new(nrows, nfibers);
2879 mask_position.mw = cpl_matrix_new(nrows, nfibers);
2881 mask_coeffs.type = GIMASK_FIT_COEFFS;
2882 mask_coeffs.my = cpl_matrix_new(mask_config.ydeg + 1, nfibers);
2883 mask_coeffs.mw = cpl_matrix_new(1, (mask_config.wdeg + 1) *
2884 (mask_config.wdeg + 1));
2892 _image = cpl_image_multiply_scalar_create(_image, nframes * conad);
2894 mask_sigma = sqrt(nframes) * bias_ron;
2901 status = _giraffe_localize_spectra(_image, _bpixel, fiber_setup,
2902 method, config->normalize,
2904 &mask_config, &mask_position,
2907 cpl_image_delete(_image);
2911 result->locy = NULL;
2912 result->locw = NULL;
2913 result->locc = NULL;
2916 cpl_matrix_delete(mask_position.my);
2917 cpl_matrix_delete(mask_position.mw);
2919 cpl_matrix_delete(mask_coeffs.my);
2920 cpl_matrix_delete(mask_coeffs.mw);
2922 if (config->full != TRUE) {
2923 cpl_table_delete(fiber_setup);
2926 cpl_msg_error(fctid,
"Spectrum localization computation failed!");
2936 if (config->full != TRUE) {
2943 cpl_table_delete(fiber_setup);
2948 if (master != NULL && master->locy != NULL) {
2950 cxint nf = cpl_table_get_nrow(_fibers);
2952 cxdouble maxoffset = 0.5 * mask_config.wavg;
2953 cxdouble maxshift = 0.;
2956 cpl_msg_info(fctid,
"Comparing detected and expected fiber "
2959 status = _giraffe_finalize_fibers(_fibers, mask_position.my,
2960 master->locy, maxoffset,
2968 cxint _nf = cpl_image_get_size_x(mlocy);
2970 cpl_msg_error(fctid,
"More fibers (%d) than expected "
2971 "(%d) were found!", nf, _nf);
2975 result->locy = NULL;
2976 result->locw = NULL;
2977 result->locc = NULL;
2980 cpl_matrix_delete(mask_position.my);
2981 cpl_matrix_delete(mask_position.mw);
2983 cpl_matrix_delete(mask_coeffs.my);
2984 cpl_matrix_delete(mask_coeffs.mw);
2986 if (config->full != TRUE) {
2987 cpl_table_delete(fiber_setup);
2990 cpl_msg_error(fctid,
"Comparison of fiber positions "
2996 cx_assert(cpl_table_get_nrow(_fibers) <= nf);
2998 cpl_msg_info(fctid,
"%" CPL_SIZE_FORMAT
" of %d expected fibers "
2999 "were detected.", cpl_table_get_nrow(_fibers), nf);
3001 if (cpl_table_get_nrow(_fibers) < nf) {
3002 cpl_msg_debug(fctid,
"Maximum offset from the expected "
3003 "position is %.2f, maximum allowed offset is %.2f",
3004 maxshift, maxoffset);
3005 cpl_msg_warning(fctid,
"%" CPL_SIZE_FORMAT
" fibers are "
3006 "missing!", nf - cpl_table_get_nrow(_fibers));
3023 cpl_matrix_get_ncol(mask_position.my),
3024 cpl_matrix_get_nrow(mask_position.my));
3027 cpl_matrix_delete(mask_position.my);
3034 cpl_propertylist_set_int(properties, GIALIAS_NAXIS1,
3035 cpl_image_get_size_x(_result));
3036 cpl_propertylist_set_int(properties, GIALIAS_NAXIS2,
3037 cpl_image_get_size_y(_result));
3038 cpl_propertylist_set_int(properties, GIALIAS_BITPIX, -32);
3039 cpl_propertylist_set_double(properties, GIALIAS_BZERO, 0.);
3040 cpl_propertylist_set_double(properties, GIALIAS_BSCALE, 1.);
3042 cpl_propertylist_append_int(properties, GIALIAS_LOCNX,
3043 cpl_image_get_size_y(_result));
3044 cpl_propertylist_append_int(properties, GIALIAS_LOCNS,
3045 cpl_image_get_size_x(_result));
3047 if (config->centroid) {
3048 cpl_propertylist_append_string(properties, GIALIAS_LMETHOD,
3052 cpl_propertylist_append_string(properties, GIALIAS_LMETHOD,
3056 if (config->normalize) {
3057 cpl_propertylist_append_int(properties, GIALIAS_LNORMALIZE,
3061 cpl_propertylist_append_int(properties, GIALIAS_LNORMALIZE,
3065 cpl_propertylist_append_bool(properties, GIALIAS_LFULLLOC, config->full);
3066 cpl_propertylist_append_int(properties, GIALIAS_LOCYDEG, config->yorder);
3067 cpl_propertylist_append_int(properties, GIALIAS_LOCWDEG, config->worder);
3068 cpl_propertylist_append_double(properties, GIALIAS_LEXTRAWID,
3070 cpl_propertylist_append_double(properties, GIALIAS_LNOISEMULT,
3073 cpl_propertylist_append_double(properties, GIALIAS_LCLIPSIGMA,
3075 cpl_propertylist_append_int(properties, GIALIAS_LCLIPNITER,
3076 config->iterations);
3077 cpl_propertylist_append_double(properties, GIALIAS_LCLIPMFRAC,
3081 if (cpl_propertylist_has(properties, GIALIAS_GIRFTYPE)) {
3082 cpl_propertylist_set_string(properties, GIALIAS_GIRFTYPE,
"LOCY");
3085 cpl_propertylist_append_string(properties, GIALIAS_GIRFTYPE,
"LOCY");
3087 cpl_propertylist_set_comment(properties, GIALIAS_GIRFTYPE,
"GIRAFFE "
3088 "localization centroid");
3095 cpl_matrix_get_ncol(mask_position.mw),
3096 cpl_matrix_get_nrow(mask_position.mw));
3099 cpl_matrix_delete(mask_position.mw);
3106 cpl_propertylist_set_int(properties, GIALIAS_NAXIS1,
3107 cpl_image_get_size_x(_result));
3108 cpl_propertylist_set_int(properties, GIALIAS_NAXIS2,
3109 cpl_image_get_size_y(_result));
3111 if (cpl_propertylist_has(properties, GIALIAS_GIRFTYPE)) {
3112 cpl_propertylist_set_string(properties, GIALIAS_GIRFTYPE,
3116 cpl_propertylist_append_string(properties, GIALIAS_GIRFTYPE,
3119 cpl_propertylist_set_comment(properties, GIALIAS_GIRFTYPE,
"GIRAFFE "
3120 "localization half-width");
3125 locc = cpl_table_new(cpl_matrix_get_ncol(mask_coeffs.my));
3127 cpl_table_new_column(locc,
"BUTTON", CPL_TYPE_INT);
3128 for (i = 0; i < cpl_table_get_nrow(locc); i++) {
3129 cpl_table_set_int(locc,
"BUTTON", i, i);
3132 for (i = 0; i < cpl_matrix_get_nrow(mask_coeffs.my); i++) {
3133 cxchar *label = NULL;
3135 cx_asprintf(&label,
"YC%d", i);
3136 cpl_table_new_column(locc, label, CPL_TYPE_DOUBLE);
3142 cpl_table_delete(locc);
3144 _my = cpl_matrix_transpose_create(mask_coeffs.my);
3146 cpl_matrix_delete(_my);
3147 cpl_matrix_delete(mask_coeffs.my);
3154 pname = cx_string_new();
3156 for (i = 0; i < cpl_matrix_get_ncol(mask_coeffs.mw); i++) {
3157 cx_string_sprintf(pname,
"%s%d", GIALIAS_LOCWIDCOEF, i);
3158 cpl_propertylist_append_double(properties, cx_string_get(pname),
3159 cpl_matrix_get(mask_coeffs.mw, 0, i));
3162 cx_string_delete(pname);
3163 cpl_matrix_delete(mask_coeffs.mw);
3165 cpl_propertylist_update_string(properties, GIALIAS_GIRFTYPE,
3167 cpl_propertylist_set_comment(properties, GIALIAS_GIRFTYPE,
"GIRAFFE "
3168 "localization fit coefficients");
3197 GiLocalizeConfig *config = NULL;
3204 config = cx_calloc(1,
sizeof *config);
3211 config->full = TRUE;
3212 config->centroid = TRUE;
3213 config->threshold = GILOCALIZE_THRESHOLD_LOCAL;
3216 p = cpl_parameterlist_find(list,
"giraffe.localization.mode");
3217 s = cpl_parameter_get_string(p);
3218 if (strcmp(s,
"siwc") == 0) {
3219 config->full = FALSE;
3222 p = cpl_parameterlist_find(list,
"giraffe.localization.start");
3223 config->start = cpl_parameter_get_int(p);
3225 p = cpl_parameterlist_find(list,
"giraffe.localization.retries");
3226 config->retries = cpl_parameter_get_int(p);
3228 p = cpl_parameterlist_find(list,
"giraffe.localization.binsize");
3229 config->binsize = cpl_parameter_get_int(p);
3231 p = cpl_parameterlist_find(list,
"giraffe.localization.ewidth");
3232 config->ewidth = cpl_parameter_get_double(p);
3234 p = cpl_parameterlist_find(list,
"giraffe.localization.ywidth");
3235 config->ywidth = cpl_parameter_get_int(p);
3237 p = cpl_parameterlist_find(list,
"giraffe.localization.center");
3238 s = cpl_parameter_get_string(p);
3239 if (!strcmp(s,
"hwidth")) {
3240 config->centroid = FALSE;
3243 p = cpl_parameterlist_find(list,
"giraffe.localization.normalize");
3244 config->normalize = cpl_parameter_get_bool(p);
3246 p = cpl_parameterlist_find(list,
"giraffe.localization.threshold");
3247 s = cpl_parameter_get_string(p);
3249 if (strncmp(s,
"global", 6) == 0) {
3250 config->threshold = GILOCALIZE_THRESHOLD_GLOBAL;
3252 else if (strncmp(s,
"row", 3) == 0) {
3253 config->threshold = GILOCALIZE_THRESHOLD_ROW;
3256 config->threshold = GILOCALIZE_THRESHOLD_LOCAL;
3259 p = cpl_parameterlist_find(list,
"giraffe.localization.noise");
3260 config->noise = cpl_parameter_get_double(p);
3262 p = cpl_parameterlist_find(list,
"giraffe.localization.ron");
3263 config->ron = cpl_parameter_get_double(p);
3265 p = cpl_parameterlist_find(list,
"giraffe.localization.yorder");
3266 config->yorder = cpl_parameter_get_int(p);
3268 p = cpl_parameterlist_find(list,
"giraffe.localization.worder");
3269 config->worder = cpl_parameter_get_int(p);
3271 p = cpl_parameterlist_find(list,
"giraffe.localization.sigma");
3272 config->sigma = cpl_parameter_get_double(p);
3274 p = cpl_parameterlist_find(list,
"giraffe.localization.iterations");
3275 config->iterations = cpl_parameter_get_int(p);
3277 p = cpl_parameterlist_find(list,
"giraffe.localization.fraction");
3278 config->fraction = cpl_parameter_get_double(p);
3332 p = cpl_parameter_new_enum(
"giraffe.localization.mode",
3334 "Localization mode: Use all spectra "
3335 "or the 5 SIWC spectra",
3336 "giraffe.localization",
3337 "all", 2,
"all",
"siwc");
3338 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI,
"sloc-mode");
3339 cpl_parameterlist_append(list, p);
3342 p = cpl_parameter_new_value(
"giraffe.localization.start",
3345 "giraffe.localization",
3347 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI,
"sloc-start");
3348 cpl_parameterlist_append(list, p);
3351 p = cpl_parameter_new_value(
"giraffe.localization.retries",
3353 "Initial localization detection "
3355 "giraffe.localization",
3357 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI,
"sloc-retries");
3358 cpl_parameterlist_append(list, p);
3361 p = cpl_parameter_new_value(
"giraffe.localization.binsize",
3363 "Initial localization detection "
3365 "giraffe.localization",
3367 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI,
"sloc-binsize");
3368 cpl_parameterlist_append(list, p);
3371 p = cpl_parameter_new_value(
"giraffe.localization.ewidth",
3373 "Localization detection extra width.",
3374 "giraffe.localization",
3376 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI,
"sloc-ewidth");
3377 cpl_parameterlist_append(list, p);
3380 p = cpl_parameter_new_value(
"giraffe.localization.ywidth",
3382 "Full width [pxl] of the equilizing "
3383 "filter (distance between two "
3384 "adjacent fibers).",
3385 "giraffe.localization",
3387 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI,
"sloc-ywidth");
3388 cpl_parameterlist_append(list, p);
3391 p = cpl_parameter_new_enum(
"giraffe.localization.center",
3393 "Method used for mask center "
3395 "giraffe.localization",
3396 "centroid", 2,
"centroid",
3398 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI,
"sloc-center");
3399 cpl_parameterlist_append(list, p);
3402 p = cpl_parameter_new_value(
"giraffe.localization.normalize",
3404 "Enable spectrum normalization along "
3405 "the dispersion axis.",
3406 "giraffe.localization",
3408 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI,
"sloc-norm");
3409 cpl_parameterlist_append(list, p);
3412 p = cpl_parameter_new_value(
"giraffe.localization.noise",
3414 "Threshold multiplier.",
3415 "giraffe.localization",
3417 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI,
"sloc-noise");
3418 cpl_parameterlist_append(list, p);
3421 p = cpl_parameter_new_enum(
"giraffe.localization.threshold",
3423 "Selects thresholding algorithm: local, "
3425 "giraffe.localization",
3426 "local", 3,
"local",
"row",
"global");
3427 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI,
"sloc-threshold");
3428 cpl_parameterlist_append(list, p);
3431 p = cpl_parameter_new_value(
"giraffe.localization.ron",
3433 "New bias sigma (RON) value for dark "
3435 "giraffe.localization",
3437 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI,
"sloc-ron");
3438 cpl_parameterlist_append(list, p);
3441 p = cpl_parameter_new_value(
"giraffe.localization.yorder",
3443 "Order of Chebyshev polynomial fit.",
3444 "giraffe.localization",
3446 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI,
"sloc-yorder");
3447 cpl_parameterlist_append(list, p);
3450 p = cpl_parameter_new_value(
"giraffe.localization.worder",
3452 "Order of Chebyshev 2D polynomial fit.",
3453 "giraffe.localization",
3455 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI,
"sloc-worder");
3456 cpl_parameterlist_append(list, p);
3459 p = cpl_parameter_new_value(
"giraffe.localization.sigma",
3461 "Localization clipping: sigma threshold "
3463 "giraffe.localization",
3465 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI,
"sloc-sigma");
3466 cpl_parameterlist_append(list, p);
3469 p = cpl_parameter_new_value(
"giraffe.localization.iterations",
3471 "Localization clipping: number of "
3473 "giraffe.localization",
3475 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI,
"sloc-niter");
3476 cpl_parameterlist_append(list, p);
3479 p = cpl_parameter_new_range(
"giraffe.localization.fraction",
3481 "Localization clipping: minimum fraction "
3482 "of points accepted/total.",
3483 "giraffe.localization",
3485 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI,
"sloc-mfrac");
3486 cpl_parameterlist_append(list, p);
cxint giraffe_array_sort(cxdouble *array, cxsize size)
Sorts an array in ascending order.
cpl_image * giraffe_image_get(const GiImage *self)
Gets the image data.
cpl_propertylist * giraffe_image_get_properties(const GiImage *self)
Get the properties of an image.
GiImage * giraffe_image_create(cpl_type type, cxint nx, cxint ny)
Creates an image container of a given type.
cxint giraffe_image_copy_matrix(GiImage *self, cpl_matrix *matrix)
Copies matrix elements into an image.
cxint giraffe_image_set_properties(GiImage *self, cpl_propertylist *properties)
Attaches a property list to an image.
GiLocalizeConfig * giraffe_localize_config_create(cpl_parameterlist *list)
Creates a setup structure for the spectrum localization.
void giraffe_localize_config_destroy(GiLocalizeConfig *config)
Destroys a spectrum localization setup structure.
void giraffe_localize_config_add(cpl_parameterlist *list)
Adds parameters for the spectrum localization.
cxint giraffe_localize_spectra(GiLocalization *result, GiImage *image, GiTable *fibers, GiLocalization *master, GiImage *badpixels, GiLocalizeConfig *config)
Finds the location of spectra in a Giraffe observation.
cpl_image * giraffe_matrix_create_image(const cpl_matrix *matrix)
Converts a matrix into an image.
cpl_matrix * giraffe_matrix_leastsq(const cpl_matrix *mA, const cpl_matrix *mB)
Computes the solution of an equation using a pseudo-inverse.
cxdouble giraffe_matrix_sigma_mean(const cpl_matrix *matrix, cxdouble mean)
Compute sigma of matrix elements, with a given mean value.
void gi_warning(const cxchar *format,...)
Log a warning.
void gi_error(const cxchar *format,...)
Log an error message.
void gi_message(const cxchar *format,...)
Log a normal message.
GiTable * giraffe_table_create(cpl_table *table, cpl_propertylist *properties)
Creates a Giraffe table from a table and a property list.
cxint giraffe_table_copy_matrix(GiTable *table, const cxchar *name, cpl_matrix *matrix)
Copies matrix elements into a table.
cpl_table * giraffe_table_get(const GiTable *self)
Get the table data from a Giraffe table.
cpl_propertylist * giraffe_table_get_properties(const GiTable *self)
Gets the table properties.
cxdouble giraffe_propertylist_get_conad(const cpl_propertylist *properties)
Retrieve the ADU to electrons conversion factor from the given properties.
cxdouble giraffe_propertylist_get_ron(const cpl_propertylist *properties)
Retrieve the read-out noise from the given properties.
GiInstrumentMode giraffe_get_mode(cpl_propertylist *properties)
Determines the instrument mode from a property list.