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);
413inline static cpl_image*
414_giraffe_filter_gauss1d(
const cpl_image* image, cxint radius, cxdouble width)
417 cxdouble w2 = width * width;
421 cpl_matrix* kernel = cpl_matrix_new(1, 2 * radius + 1);
423 cpl_image* fimage = NULL;
426 if (kernel == NULL) {
430 for (i = -radius; i <= radius; ++i) {
432 cxdouble y = exp(-x2 / (2. * w2));
434 cpl_matrix_set(kernel, 0, i + radius, y);
438 fimage = cpl_image_new(cpl_image_get_size_x(image),
439 cpl_image_get_size_y(image),
440 cpl_image_get_type(image));
442 if (fimage == NULL) {
443 cpl_matrix_delete(kernel);
447 cpl_image_filter(fimage, image, kernel, CPL_FILTER_LINEAR,
449 cpl_matrix_delete(kernel);
456inline static cpl_image*
457_giraffe_filter_sobel(
const cpl_image* image, cxbool vertical)
459 cpl_matrix* kernel = cpl_matrix_new(3, 3);
461 cpl_image* fimage = NULL;
464 if (kernel == NULL) {
471 cpl_matrix_set(kernel, 0, 0, -1);
472 cpl_matrix_set(kernel, 1, 0, -2);
473 cpl_matrix_set(kernel, 2, 0, -1);
475 cpl_matrix_set(kernel, 0, 2, 1);
476 cpl_matrix_set(kernel, 1, 2, 2);
477 cpl_matrix_set(kernel, 2, 2, 1);
479 cpl_matrix_set(kernel, 0, 0, 0);
480 cpl_matrix_set(kernel, 1, 0, -0.5);
481 cpl_matrix_set(kernel, 2, 0, 0);
483 cpl_matrix_set(kernel, 0, 2, 0);
484 cpl_matrix_set(kernel, 1, 2, 0.5);
485 cpl_matrix_set(kernel, 2, 2, 0);
490 cpl_matrix_set(kernel, 0, 0, 1);
491 cpl_matrix_set(kernel, 0, 1, 2);
492 cpl_matrix_set(kernel, 0, 2, 1);
494 cpl_matrix_set(kernel, 2, 0, -1);
495 cpl_matrix_set(kernel, 2, 1, -2);
496 cpl_matrix_set(kernel, 2, 2, -1);
500 fimage = cpl_image_new(cpl_image_get_size_x(image),
501 cpl_image_get_size_y(image),
502 cpl_image_get_type(image));
504 if (fimage == NULL) {
505 cpl_matrix_delete(kernel);
509 cpl_image_filter(fimage, image, kernel, CPL_FILTER_LINEAR,
511 cpl_matrix_delete(kernel);
519_giraffe_build_edge_mask(cpl_image* raw, cpl_image* bpixel, cxint nspectra,
520 cxdouble noise, GiMaskParameters* config,
521 cxint* ndetect, cpl_matrix* mxok, cpl_matrix* myup,
525 const cxint margin = 5;
533 cxint nrows = cpl_image_get_size_y(raw);
534 cxint ncols = cpl_image_get_size_x(raw);
538 cxdouble* buffer = NULL;
540 cpl_mask* kernel = NULL;
542 cpl_image* fraw = NULL;
543 cpl_image* sraw = NULL;
544 cpl_image* vertical1 = NULL;
545 cpl_image* vertical2 = NULL;
546 cpl_image* center = NULL;
559 kernel = cpl_mask_new(1, 15);
561 if (kernel != NULL) {
563 cpl_mask_not(kernel);
565 fraw = cpl_image_new(ncols, nrows, cpl_image_get_type(raw));
568 cpl_mask_delete(kernel);
574 cpl_image_filter_mask(fraw, raw, kernel, CPL_FILTER_MEDIAN,
579 cpl_mask_delete(kernel);
583 sraw = _giraffe_filter_gauss1d(fraw, 6, 1.);
587 cpl_image_delete(fraw);
594 vertical1 = _giraffe_filter_sobel(sraw, TRUE);
595 vertical2 = _giraffe_filter_sobel(vertical1, TRUE);
597 cpl_image_save(sraw,
"master_flat_smooth.fits", -32, 0, CPL_IO_DEFAULT);
598 cpl_image_save(vertical1,
"vertical.fits", -32, 0, CPL_IO_DEFAULT);
599 cpl_image_save(vertical2,
"vertical2.fits", -32, 0, CPL_IO_DEFAULT);
606 center = cpl_image_new(ncols, nrows, CPL_TYPE_INT);
608 flags = cx_calloc(ncols,
sizeof(cxint));
609 buffer = cx_calloc(ncols,
sizeof(cxdouble));
611 if ((center == NULL) || (flags ==NULL) || (buffer == NULL)) {
619 cpl_image_delete(center);
622 cpl_image_delete(vertical2);
625 cpl_image_delete(vertical1);
628 cpl_image_delete(sraw);
631 cpl_image_delete(fraw);
639 for (m = 0; m < nrows; ++m) {
641 register cxint irow = m * ncols;
642 register cxint n = 0;
647 cxint* _center = cpl_image_get_data_int(center) + irow;
649 const cxdouble* _vt1 = cpl_image_get_data_double_const(vertical1) +
651 const cxdouble* _vt2 = cpl_image_get_data_double_const(vertical2) +
653 const cxdouble* _fraw = cpl_image_get_data_double_const(fraw) +
657 memset(buffer, 0, ncols *
sizeof(cxdouble));
658 memset(flags, 0, ncols *
sizeof(cxint));
661 for (n = 0; n < ncols; ++n) {
671 if ((n - 1 >= 0) && (_vt2[n - 1] > 0.)) {
672 buffer[n - 1] = _vt1[n - 1];
674 if ((n + 1 < ncols) && (_vt2[n + 1] > 0.)) {
675 buffer[n + 1] = _vt1[n + 1];
681 while (iteration < ncols) {
685 cxdouble dx = 3. * 2. * noise;
688 for (n = 0; n < ncols; ++n) {
690 if (!flags[n] && (buffer[n] > dx)) {
700 register cxint k = 0;
707 cxdouble signal = 0.;
713 while ((k >= 0) && (buffer[k] > 0.)) {
720 while ((k < ncols) && (buffer[k] > 0.)) {
726 while ((k < ncols) && (buffer[k] < 0.)) {
731 width = end - start + 1;
739 signal = (_fraw[pos] > 0.) ? _fraw[pos] : 0.;
740 sigma = sqrt((noise * noise + signal) / config->xbin);
742 if ((signal / sigma > 10.) && (width > 1)) {
744 start = (start == pos) ? start - 1 : start;
745 end = (end == pos) ? end + 1 : end;
748 _center[start] += -1;
759 for (n = 0; n < ncols; ++n) {
761 if (_center[n] == 1) {
767 if (scount >= smax) {
783 cx_print(
"scount: %d (%d) at %d\n", smax, nspectra, mmax);
791 const cxint limit = 0.85 * nrows;
796 const cxdouble hwf = sqrt(2. * log(2.));
798 cxint* xtrace = cx_calloc(nrows,
sizeof(cxint));
799 cxint* ytrace = cx_calloc(nrows,
sizeof(cxint));
801 cpl_image* mask = cpl_image_new(ncols, nrows, CPL_TYPE_INT);
803 for (m = 0; m < ncols; ++m) {
805 const cxint* _center = cpl_image_get_data_int(center);
806 const cxint* _reference = _center + mmax * ncols;
808 cxbool out_of_bounds = FALSE;
813 if (_reference[m] == 1) {
815 register cxint j = mmax;
816 register cxint pos = m;
821 xtrace[connected] = pos;
822 ytrace[connected] = j;
828 register cxint k = 0;
829 register cxint l = j * ncols;
830 register cxint kmin = (pos - 1 >= 0) ? pos - 1 : 0;
831 register cxint kmax = (pos + 1 < ncols) ? pos + 1 : ncols - 1;
833 for (k = kmin; k <= kmax; ++k) {
835 if (_center[l + k] == 1) {
837 if ((pos <= margin) || (pos >= ncols - margin)) {
838 out_of_bounds = TRUE;
842 xtrace[connected] = k;
843 ytrace[connected] = j;
860 register cxint k = 0;
861 register cxint l = j * ncols;
862 register cxint kmin = (pos - 1 >= 0) ? pos - 1 : 0;
863 register cxint kmax = (pos + 1 < ncols) ? pos + 1 : ncols - 1;
865 for (k = kmin; k <= kmax; ++k) {
867 if (_center[l + k] == 1) {
869 if ((pos <= margin) || (pos >= ncols - margin)) {
870 out_of_bounds = TRUE;
874 xtrace[connected] = k;
875 ytrace[connected] = j;
887 if ((connected < limit) || (out_of_bounds == TRUE)) {
889 memset(xtrace, 0, nrows *
sizeof(cxint));
890 memset(ytrace, 0, nrows *
sizeof(cxint));
892 if (out_of_bounds == TRUE) {
893 cx_print(
"discarded candidate %d, going out of detector "
894 "boundaries.\n", itrace);
898 cx_print(
"discarded candidate %d, not enough connected "
899 "centers (%d, required: %d)\n", itrace, connected,
906 cxint* _mask = cpl_image_get_data_int(mask);
908 for (j = 0; j < connected; ++j) {
910 register cxint x = xtrace[j];
911 register cxint y = ytrace[j] * ncols;
912 register cxint ix = x;
916 while ((_center[y + ix] != -1) && (ix > 0)) {
922 while ((_center[y + ix] != -2) && (ix < ncols - 1)) {
937 cx_print(
"scount: %d (expected: %d)\n", ispectra, nspectra);
945 for (m = 0; m < nrows; ++m) {
947 register cxint j = 0;
948 register cxint ns = 0;
950 const cxint* _mask = cpl_image_get_data_int(mask) + m * ncols;
951 const cxint* _center = cpl_image_get_data_int(center) + m * ncols;
954 for (j = 0; j < ncols; ++j) {
958 register cxint x = j;
959 register cxint ix = x;
962 while ((_center[ix] != -1) && (ix > 0)) {
965 cpl_matrix_set(mylo, naccepted, ns, x - hwf * fabs(x - ix));
968 while ((_center[ix] != -2) && (ix < ncols - 1)) {
971 cpl_matrix_set(myup, naccepted, ns, x + hwf * fabs(ix - x));
978 if (ns == ispectra) {
979 cpl_matrix_set(mxok, naccepted, 0, m);
988 cpl_image_save(center,
"center.fits", -32, 0, CPL_IO_DEFAULT);
989 cpl_image_save(mask,
"mask.fits", -32, 0, CPL_IO_DEFAULT);
991 cpl_image_delete(mask);
992 cpl_image_delete(center);
993 cpl_image_delete(vertical2);
994 cpl_image_delete(vertical1);
995 cpl_image_delete(sraw);
996 cpl_image_delete(fraw);
1037_giraffe_build_raw_mask(cpl_image *raw, cpl_image *bpixel, cxint nspectra,
1038 cxdouble noise, GiMaskParameters *config,
1039 cxint *ndetect, cpl_matrix *mxok, cpl_matrix *myup,
1043 register cxint x = 0;
1044 register cxint y = 0;
1045 register cxint xretry = 0;
1046 register cxint xok = 0;
1051 cxint *yabove = NULL;
1052 cxint *ybelow = NULL;
1053 cxint *good_pixels = NULL;
1054 cxint ywidth = config->ywidth > 1 ? config->ywidth : 2;
1055 cxint ckwidth = config->ckdata.width;
1056 cxint ckheight = config->ckdata.height;
1057 cxint ckcount = config->ckdata.count;
1060 cxdouble* pixels = NULL;
1062 cpl_mask* med = NULL;
1064 cpl_image* img = raw;
1069 med = cpl_mask_new(1, 15);
1075 img = cpl_image_new(cpl_image_get_size_x(raw),
1076 cpl_image_get_size_y(raw),
1077 cpl_image_get_type(raw));
1079 cpl_image_filter_mask(img, raw, med, CPL_FILTER_MEDIAN,
1084 cpl_mask_delete(med);
1089 GIDEBUG(
gi_message(
"noise = %g start = %d tries = %d xbin = %d "
1090 "ywidth = %d", noise, config->start, config->retry,
1091 config->xbin, ywidth));
1093 pixels = cpl_image_get_data_double(img);
1095 nrows = cpl_image_get_size_y(img);
1096 ncols = cpl_image_get_size_x(img);
1099 if (config->xbin > 1) {
1103 cxdouble* _pixels = NULL;
1106 nrows = (cxint) ceil(nrows / config->xbin);
1107 config->start = (cxint) ceil(config->start / config->xbin);
1109 _pixels = cx_calloc(ncols * nrows,
sizeof(cxdouble));
1111 for (y = 0; y < ncols; ++y) {
1113 for (x = 0; x < nrows; ++x) {
1115 register cxint xx = 0;
1116 register cxint zx = x * ncols;
1117 register cxint xr = x * config->xbin;
1118 register cxint zr = xr * ncols;
1121 _pixels[zx + y] = 0.;
1123 for (xx = 0; xx < config->xbin && xr < nx; ++xx) {
1124 _pixels[zx + y] += pixels[zr + y];
1127 _pixels[zx + y] /= config->xbin;
1137 good_pixels = cx_calloc(nrows * ncols,
sizeof(cxint));
1139 switch (config->method) {
1141 case GILOCALIZE_THRESHOLD_LOCAL:
1144 cxint ywidth2 = ywidth / 2;
1145 cxint sz = 2 * ywidth2 + 1;
1147 cpl_vector* ymins = cpl_vector_new(sz);
1156 for (x = 0; x < nrows; x++) {
1158 cpl_vector_fill(ymins, 0.);
1160 for (y = 0; y < ncols; y++) {
1162 register cxint k = 0;
1163 register cxint kk = 0;
1165 cxdouble value = 0.;
1167 cxdouble threshold = 0.;
1170 for (kk = 0, k = -ywidth2; k <= ywidth2; k++) {
1172 register cxint ky = y + k;
1174 if (ky < 0 || ky >= ncols) {
1178 cpl_vector_set(ymins, kk, pixels[x * ncols + ky]);
1186 if (config->threshold > 0.) {
1188 const cxint count = 2;
1203 for (i = 0; i < count; i++) {
1204 bkg += fabs(cpl_vector_get(ymins, i));
1206 bkg /= (cxdouble)count;
1208 threshold = sqrt((2. * noise * noise +
1209 fabs(pixels[x * ncols + y]) + bkg / count) / config->xbin);
1215 register cxdouble mean = 0.;
1218 for (i = 0; i < kk; i++) {
1219 mean += cpl_vector_get(ymins, i);
1225 bkg = (cpl_vector_get(ymins, 0) +
1226 cpl_vector_get(ymins, 1)) / 2.0;
1227 threshold = mean - bkg;
1236 value = pixels[x * ncols + y] - bkg;
1242 if (value > fabs(config->threshold) * threshold) {
1243 good_pixels[x * ncols + y] = 1;
1248 cpl_vector_delete(ymins);
1255 case GILOCALIZE_THRESHOLD_ROW:
1258 cpl_image* snr = cpl_image_abs_create(raw);
1260 cxint sx = cpl_image_get_size_x(snr);
1263 cpl_image_power(snr, 0.5);
1265 for (x = 0; x < nrows; ++x) {
1267 const cxdouble* _snr = cpl_image_get_data_double_const(snr);
1269 cxdouble avsnr = giraffe_array_median(_snr + x * sx, sx);
1272 for (y = 0; y < ncols; ++y) {
1274 if (pixels[x * ncols + y] <= 0.) {
1278 if (_snr[x * ncols + y] > avsnr * fabs(config->threshold)) {
1279 good_pixels[x * ncols + y] = 1;
1286 cpl_image_delete(snr);
1296 cxdouble threshold = 0.;
1303 if (config->threshold > 0.) {
1304 threshold = config->threshold * noise;
1308 cxdouble mean = cpl_image_get_mean(raw);
1310 threshold = -config->threshold * mean *
1311 (nspectra * config->wavg / ncols);
1315 for (x = 0; x < nrows; x++) {
1317 for (y = 0; y < ncols; y++) {
1319 if (pixels[x * ncols + y] > threshold) {
1320 good_pixels[x * ncols + y] = 1;
1333 GIDEBUG(cxint *data = cx_calloc(nrows * ncols,
sizeof(cxint));
1334 memcpy(data, good_pixels, nrows * ncols *
sizeof(cxint));
1335 cpl_image *gp = cpl_image_wrap_int(ncols, nrows, data);
1336 cpl_image_save(gp,
"locmask.fits", 32, NULL, CPL_IO_DEFAULT);
1337 cpl_image_unwrap(gp);
1345 yabove = cx_calloc(nspectra + 1,
sizeof(cxint));
1346 ybelow = cx_calloc(nspectra + 1,
sizeof(cxint));
1358 for (x = config->start; (x >= 0) && (xretry <= config->retry); x--) {
1360 register cxint zx = x * ncols;
1361 register cxint nborders = 0;
1362 register cxint nbelow = 0;
1363 register cxint nabove = 0;
1364 register cxint in_spectrum = 0;
1367 for (y = 1; y < ny; y++) {
1369 register cxint tmp = 2 * good_pixels[zx + y];
1375 nborders = CX_MAX(CX_MAX(nborders, nbelow), nabove);
1377 if (nborders > nspectra) {
1387 if (good_pixels[zx + y + 1]) {
1394 if ((tmp - good_pixels[zx + y - 1]) == 2) {
1408 ybelow[nbelow++] = y;
1417 if (good_pixels[zx + y - 1]) {
1424 if ((tmp - good_pixels[zx + y + 1]) == 2) {
1438 yabove[nabove++] = y;
1450 !good_pixels[zx + y - 1] && !good_pixels[zx + y + 1]) {
1452 if (_giraffe_validate_pixel(good_pixels, ncols, nrows, y, x,
1453 ckwidth, ckheight, ckcount)) {
1455 yabove[nabove++] = y;
1456 ybelow[nbelow++] = y;
1469 *ndetect = nborders;
1471 if (!in_spectrum && (nbelow == nspectra) && (nbelow == nabove)) {
1480 for (y = 0; y < nspectra; y++) {
1481 cpl_matrix_set(mylo, xok, y, (cxdouble) ybelow[y]);
1482 cpl_matrix_set(myup, xok, y, (cxdouble) yabove[y]);
1483 cpl_matrix_set(mxok, xok, 0, (config->xbin > 1) ?
1484 (cxdouble) (x + 0.5) * config->xbin :
1490 else if (xretry++ < config->retry) {
1520 for (x = config->start + 1; (x < nrows) &&
1521 (xretry <= config->retry); x++) {
1523 register cxint zx = x * ncols;
1524 register cxint nborders = 0;
1525 register cxint nbelow = 0;
1526 register cxint nabove = 0;
1527 register cxint in_spectrum = 0;
1530 for (y = 1; y < ny; y++) {
1532 register cxint tmp = 2 * good_pixels[zx + y];
1534 nborders = CX_MAX(CX_MAX(nborders, nbelow), nabove);
1536 if (nborders > nspectra) {
1540 if (good_pixels[zx + y + 1]) {
1541 if ((tmp - good_pixels[zx + y - 1]) == 2) {
1543 ybelow[nbelow++] = y;
1549 if (good_pixels[zx + y - 1]) {
1550 if ((tmp - good_pixels[zx + y + 1]) == 2) {
1552 yabove[nabove++] = y;
1561 !good_pixels[zx + y - 1] && !good_pixels[zx + y + 1]) {
1563 if (_giraffe_validate_pixel(good_pixels, ncols, nrows, y, x,
1564 ckwidth, ckheight, ckcount)) {
1566 yabove[nabove++] = y;
1567 ybelow[nbelow++] = y;
1580 *ndetect = nborders;
1582 if (!in_spectrum && (nbelow == nspectra) && (nbelow == nabove)) {
1584 for (y = 0; y < nspectra; y++) {
1585 cpl_matrix_set(mylo, xok, y, (cxdouble) ybelow[y]);
1586 cpl_matrix_set(myup, xok, y, (cxdouble) yabove[y]);
1587 cpl_matrix_set(mxok, xok, 0, (config->xbin > 1) ?
1588 (cxdouble) (x + 0.5) * config->xbin :
1594 else if (xretry++ < config->retry) {
1605 cx_free(good_pixels);
1607 if (pixels != cpl_image_get_data_double(img)) {
1613 cpl_image_delete(img);
1618 if (*ndetect < nspectra) {
1621 else if (*ndetect > nspectra) {
1629 *ndetect = nspectra;
1663_giraffe_fit_raw_mask(cpl_matrix *mxok, cpl_matrix *myup, cpl_matrix *mylo,
1664 cpl_table *fibers, GiMaskParameters *config,
1665 GiMaskPosition *position)
1668 register cxint nn, x, nspectra;
1669 register cxint nx = cpl_matrix_get_nrow(position->my);
1670 register cxint ns = cpl_table_get_nrow(fibers);
1678 mxraw = cpl_matrix_new(nx, 1);
1679 mcoeff = cpl_matrix_new(config->ydeg + 1, 1);
1686 for (x = 0; x < nx; x++) {
1687 cpl_matrix_set(mxraw, x, 0, x);
1694 base = giraffe_chebyshev_base1d(0., nx, config->ydeg + 1, mxraw);
1695 cpl_matrix_delete(mxraw);
1698 for (nn = 0; nn < ns; nn++) {
1699 cpl_matrix *ylofit = NULL;
1700 cpl_matrix *yupfit = NULL;
1713 ylofit = _giraffe_fit_border(mylo, base, mxok, nspectra,
1714 config->sigma, config->niter,
1715 config->mfrac, mcoeff);
1716 if (ylofit == NULL) {
1717 cpl_msg_warning(_task,
"Could not compute low border for "
1724 yupfit = _giraffe_fit_border(myup, base, mxok, nspectra,
1725 config->sigma, config->niter,
1726 config->mfrac, mcoeff);
1727 if (yupfit == NULL) {
1728 cpl_msg_warning(_task,
"Could not compute up border for "
1740 for (x = 0; x < nx; x++) {
1742 cpl_matrix_set(position->my, x, nn, 0.5 *
1743 (cpl_matrix_get(yupfit, x, 0) +
1744 cpl_matrix_get(ylofit, x, 0)));
1746 cpl_matrix_set(position->my, x, nn, 0.5 *
1747 (cpl_matrix_get(yupfit, x, 0) -
1748 cpl_matrix_get(ylofit, x, 0)) + config->ewid);
1751 cpl_matrix_delete(ylofit);
1752 cpl_matrix_delete(yupfit);
1757 cpl_msg_info(_task,
"%03d spectrum positions fitted", nspectra);
1759 cpl_matrix_delete(base);
1760 cpl_matrix_delete(mcoeff);
1762 if (nspectra == 0) {
1763 cpl_msg_warning(_task,
"could not fit any spectra, check number "
1764 "of good wavelength bins");
1808_giraffe_fit_raw_centroid(cpl_image* mz, cpl_matrix* mxok, cpl_matrix* myup,
1809 cpl_matrix* mylo, cpl_table* fibers,
1810 GiMaskParameters* config, GiMaskPosition* position,
1811 GiMaskPosition* coeffs)
1814 const cxchar*
const fctid =
"_giraffe_fit_raw_centroid";
1816 register cxint nn = 0;
1817 register cxint x = 0;
1818 register cxint y = 0;
1819 register cxint nspectra = 0;
1820 register cxint nx = cpl_image_get_size_y(mz);
1821 register cxint ny = cpl_image_get_size_x(mz);
1822 register cxint ns = cpl_table_get_nrow(fibers);
1824 cxint yorder = config->ydeg + 1;
1825 cxint worder = config->wdeg + 1;
1827 cpl_matrix* mxraw = NULL;
1828 cpl_matrix* base = NULL;
1829 cpl_matrix* mycenter = NULL;
1830 cpl_matrix* mywidth = NULL;
1831 cpl_matrix* mx = NULL;
1832 cpl_matrix* my = NULL;
1833 cpl_matrix* mw = NULL;
1834 cpl_matrix* chebcoeff = NULL;
1835 cpl_matrix* mfitlocw = NULL;
1836 cpl_matrix* ycenfit = NULL;
1837 cpl_matrix* ycencoeff = NULL;
1841 if (cpl_matrix_get_nrow(position->my) != nx ||
1842 cpl_matrix_get_ncol(position->my) != ns) {
1843 gi_error(
"%s: invalid size for position->my[%" CPL_SIZE_FORMAT
",%"
1844 CPL_SIZE_FORMAT
"], expected [%d,%d]", fctid,
1845 cpl_matrix_get_nrow(position->my),
1846 cpl_matrix_get_ncol(position->my), nx, ns);
1850 if (cpl_matrix_get_nrow(position->mw) != nx ||
1851 cpl_matrix_get_ncol(position->mw) != ns) {
1852 gi_error(
"%s: invalid size for position->mw[%" CPL_SIZE_FORMAT
",%"
1853 CPL_SIZE_FORMAT
"], expected [%d,%d]", fctid,
1854 cpl_matrix_get_nrow(position->my),
1855 cpl_matrix_get_ncol(position->my), nx, ns);
1864 mxraw = cpl_matrix_new(nx, 1);
1866 for (x = 0; x < nx; x++) {
1867 cpl_matrix_set(mxraw, x, 0, x);
1875 base = giraffe_chebyshev_base1d(0., nx, yorder, mxraw);
1876 cpl_matrix_delete(mxraw);
1878 mycenter = cpl_matrix_new(cpl_matrix_get_nrow(mxok), ns);
1879 mywidth = cpl_matrix_new(1, cpl_matrix_get_nrow(mxok) * ns);
1881 ycencoeff = cpl_matrix_new(yorder, 1);
1883 for (nn = 0; nn < ns; nn++) {
1900 cxdouble* pixels = cpl_image_get_data_double(mz);
1902 for (x = 0; x < cpl_matrix_get_nrow(mxok); x++) {
1904 register cxint zx = (cxint) cpl_matrix_get(mxok, x, 0);
1906 register cxdouble zz = 0.;
1907 register cxdouble yy = 0.;
1909 cxdouble lower = cpl_matrix_get(mylo, x, nspectra);
1910 cxdouble upper = cpl_matrix_get(myup, x, nspectra);
1913 for (y = (cxint) lower; y <= (cxint) upper; y++) {
1914 yy += pixels[zx * ny + y] * y;
1915 zz += pixels[zx * ny + y];
1918 cpl_matrix_set(mycenter, x, nspectra, yy / zz);
1919 cpl_matrix_set(mywidth, 0, x * ns + nspectra, config->ewid +
1920 (upper - lower) / 2.0);
1928 cpl_matrix_fill(ycencoeff, 0.);
1929 ycenfit = _giraffe_fit_border(mycenter, base, mxok, nspectra,
1930 config->sigma, config->niter,
1931 config->mfrac, ycencoeff);
1932 if (ycenfit == NULL) {
1933 cpl_msg_warning(_task,
"Could not fit centroid for spectrum %d",
1943 for (x = 0; x < yorder; x++) {
1944 cpl_matrix_set(coeffs->my, x, nn,
1945 cpl_matrix_get(ycencoeff, x, 0));
1953 for (x = 0; x < nx; x++) {
1954 cpl_matrix_set(position->my, x, nn,
1955 cpl_matrix_get(ycenfit, 0, x));
1958 cpl_matrix_delete(ycenfit);
1964 cpl_image_save(lycenter,
"lycenter.fits", -32, NULL,
1966 cpl_image_delete(lycenter);
1969 cpl_image_save(lycenter,
"lycenterfit.fits", -32, NULL,
1971 cpl_image_delete(lycenter);
1974 cpl_image_save(lyxok,
"lyxok.fits", -32, NULL,
1976 cpl_image_delete(lyxok));
1979 cpl_msg_info(_task,
"%03d spectrum positions fitted", nspectra);
1981 cpl_matrix_delete(base);
1982 cpl_matrix_delete(mycenter);
1983 cpl_matrix_delete(ycencoeff);
1985 if (nspectra == 0) {
1986 cpl_msg_warning(_task,
"Could not fit any spectra, check number of "
1987 "good wavelength bins");
1989 cpl_matrix_delete(mywidth);
1997 cpl_msg_info(_task,
"2D fit (order %dx%d) of mask width", worder,
2004 mx = cpl_matrix_new(cpl_matrix_get_nrow(mxok) * nspectra, 1);
2005 my = cpl_matrix_new(cpl_matrix_get_nrow(mxok) * nspectra, 1);
2006 mw = cpl_matrix_new(1, cpl_matrix_get_nrow(mxok) * nspectra);
2008 for (y = 0, nn = 0; nn < nspectra; nn++) {
2020 for (x = 0; x < cpl_matrix_get_nrow(mxok); x++) {
2022 register cxint zx = (cxint) cpl_matrix_get(mxok, x, 0);
2023 register cxint lx = x * nspectra + y;
2026 cpl_matrix_set(mx, lx, 0, cpl_matrix_get(mxok, x, 0));
2027 cpl_matrix_set(my, lx, 0, cpl_matrix_get(position->my, zx, nn));
2028 cpl_matrix_set(mw, 0, lx, cpl_matrix_get(mywidth, 0, x * ns + y));
2033 base = giraffe_chebyshev_base2d(0., 0., nx, ny, worder, worder, mx, my);
2035 cpl_matrix_delete(my);
2036 cpl_matrix_delete(mx);
2039 cpl_matrix_delete(base);
2040 cpl_matrix_delete(mw);
2042 cpl_matrix_delete(mywidth);
2044 if (chebcoeff == NULL) {
2045 gi_warning(
"%s: error in giraffe_matrix_leastsq() for width 2D fit",
2054 for (nn = 0; nn < cpl_matrix_get_ncol(chebcoeff); nn++) {
2055 cpl_matrix_set(coeffs->mw, 0, nn, cpl_matrix_get(chebcoeff, 0, nn));
2062 mx = cpl_matrix_new(nx * nspectra, 1);
2063 my = cpl_matrix_new(nx * nspectra, 1);
2065 for (y = 0, nn = 0; nn < nspectra; nn++) {
2077 for (x = 0; x < nx; x++) {
2079 register cxint lx = x * nspectra + y;
2081 cpl_matrix_set(mx, lx, 0, x);
2082 cpl_matrix_set(my, lx, 0, cpl_matrix_get(position->my, x, nn));
2088 cpl_matrix_set_size(chebcoeff, worder, worder);
2090 mfitlocw = giraffe_chebyshev_fit2d(0., 0., nx, ny, chebcoeff, mx, my);
2091 cpl_matrix_delete(chebcoeff);
2093 cpl_matrix_delete(my);
2094 cpl_matrix_delete(mx);
2096 for (y = 0, nn = 0; nn < nspectra; nn++) {
2108 for (x = 0; x < nx; x++) {
2110 register cxint lx = x * nspectra + y;
2112 cpl_matrix_set(position->mw, x, nn,
2113 cpl_matrix_get(mfitlocw, lx, 0));
2119 cpl_matrix_delete(mfitlocw);
2152_giraffe_localize_spectra(cpl_image *mzraw, cpl_image *bpixel,
2153 cpl_table *fibers, GiLocalizeMethod method,
2154 cxbool normalize, cxdouble noise,
2155 GiMaskParameters *config, GiMaskPosition *position,
2156 GiMaskPosition *coeffs)
2161 cxint ndetect, nspectra;
2164 cxdouble uplost = 0.;
2165 cxdouble lolost = 0.;
2166 cxdouble avglost = 0.;
2167 cxdouble avgmask = 0.;
2168 cxdouble sigmask = 0.;
2169 cxdouble sigmean = 0.;
2170 cxdouble avgborders = 0.;
2179 cpl_image *mz = NULL;
2180 cpl_image *mznorm = NULL;
2184 nx = cpl_image_get_size_y(mzraw);
2185 ny = cpl_image_get_size_x(mzraw);
2186 _mzraw = cpl_image_get_data_double(mzraw);
2189 if (normalize == TRUE) {
2191 cxdouble zxmax = 0.0;
2192 cxdouble *_mzx = NULL;
2193 cxdouble *_mznorm = NULL;
2195 cpl_image *mzx = NULL;
2198 cpl_msg_info(_task,
"Using normalized spectra for localization");
2207 mznorm = cpl_image_new(ny, nx, CPL_TYPE_DOUBLE);
2208 _mznorm = cpl_image_get_data_double(mznorm);
2210 mzx = cpl_image_new(1, nx, CPL_TYPE_DOUBLE);
2211 _mzx = cpl_image_get_data_double(mzx);
2218 for (x = 0 ; x < nx; x++) {
2219 for (y = 0 ; y < ny; y++) {
2220 _mzx[x] += _mzraw[x * ny + y];
2227 if (_mzx[x] > zxmax) {
2232 GIDEBUG(cpl_image_save(mzx,
"mzx.fits", -32, NULL, CPL_IO_DEFAULT));
2234 for (x = 0 ; x < nx; x++) {
2236 register cxdouble zxnorm = zxmax / _mzx[x];
2238 for (y = 0 ; y < ny; y++) {
2239 _mznorm[x * ny + y] = _mzraw[x * ny + y] * zxnorm;
2244 cpl_image_delete(mzx);
2253 cpl_msg_info(_task,
"Using raw spectra for localization");
2262 nspectra = cpl_table_get_nrow(fibers);
2264 mxok = cpl_matrix_new(nx, 1);
2265 myup = cpl_matrix_new(nx, nspectra);
2266 mylo = cpl_matrix_new(nx, nspectra);
2273 config->xbin = (config->xbin > 1) ? 2 * (config->xbin / 2) : 1;
2275 GIDEBUG(cpl_image_save(mz,
"mz.fits", -32, NULL, CPL_IO_DEFAULT));
2282 cpl_msg_info(_task,
"Generating mask (%d spectra expected) ...",
2288 nxok = _giraffe_build_edge_mask(mz, bpixel, nspectra, noise, config,
2289 &ndetect, mxok, myup, mylo);
2294 nxok = _giraffe_build_raw_mask(mz, bpixel, nspectra, noise, config,
2295 &ndetect, mxok, myup, mylo);
2299 cpl_matrix_delete(mxok);
2300 cpl_matrix_delete(myup);
2301 cpl_matrix_delete(mylo);
2305 cpl_msg_warning(_task,
"Invalid number of spectra detected: "
2306 "%d != %d", ndetect, nspectra);
2310 cpl_msg_warning(_task,
"No abcissa with good number "
2315 cpl_msg_warning(_task,
"Error while searching for spectra");
2323 cpl_msg_info(_task,
"%d spectra detected in %d wavelength bins",
2332 cpl_matrix_resize(mxok, 0, nxok - cpl_matrix_get_nrow(mxok), 0, 0);
2333 cpl_matrix_resize(myup, 0, nxok - cpl_matrix_get_nrow(myup), 0, 0);
2334 cpl_matrix_resize(mylo, 0, nxok - cpl_matrix_get_nrow(mylo), 0, 0);
2336 GIDEBUG(
gi_message(
"%s: mxok[0-%d]=[%g-%g]", __func__,
2337 cpl_matrix_get_nrow(mxok) - 1,
2338 cpl_matrix_get_min(mxok),
2339 cpl_matrix_get_max(mxok)));
2342 cpl_msg_info(_task,
"Computing spectrum positions and widths in "
2343 "pixel range [%g,%g]", cpl_matrix_get_min(mxok),
2344 cpl_matrix_get_max(mxok));
2346 if (cpl_matrix_get_nrow(mxok) <= config->ydeg) {
2347 cpl_msg_info(_task,
"Not enough data points %" CPL_SIZE_FORMAT
2348 " for %d order fit", cpl_matrix_get_nrow(mxok),
2355 case GILOCALIZE_HALF_WIDTH:
2356 cpl_msg_info(_task,
"Using half-width for localization");
2357 _giraffe_fit_raw_mask(mxok, myup, mylo, fibers, config,
2361 case GILOCALIZE_BARYCENTER:
2363 cpl_msg_info(_task,
"Using barycenter for localization");
2364 _giraffe_fit_raw_centroid(mz, mxok, myup, mylo, fibers, config,
2369 if (normalize == 1) {
2370 cpl_image_delete(mznorm);
2383 mwid = cpl_matrix_new(nxok, nspectra);
2385 for (n = 0, nn = 0; nn < cpl_table_get_nrow(fibers); nn++) {
2387 for (x = 0; x < nxok; x++) {
2388 register cxint lx = (cxint) cpl_matrix_get(mxok, x, 0);
2390 cxdouble lower = cpl_matrix_get(mylo, x, n);
2391 cxdouble upper = cpl_matrix_get(myup, x, n);
2392 cxdouble width = cpl_matrix_get(position->mw, lx, nn);
2394 uplost += cpl_matrix_get(position->my, lx, nn) + width - upper;
2395 lolost += cpl_matrix_get(position->my, lx, nn) - width - lower;
2397 avgborders += upper - lower;
2400 cpl_matrix_set(mwid, x, n, 2. * width);
2405 sigmean = cpl_matrix_get_mean(mwid);
2407 avglost = (lolost + uplost) / (nspectra * nxok);
2408 avgmask = 2.0 * avgmask / nspectra;
2410 cpl_msg_info(_task,
"Mask was computed using %d of %d wavelength bins",
2412 cpl_msg_info(_task,
"Average # of pixels per spectra: %.4g",
2414 cpl_msg_info(_task,
"Average # of in-borders pixels per spectra: %.4g",
2415 avgborders / nspectra);
2416 cpl_msg_info(_task,
"Average lost pixels per spectra: %.4g",
2418 cpl_msg_info(_task,
"Average lost pixels at upper border: %.4g",
2419 uplost / (nspectra * nxok));
2420 cpl_msg_info(_task,
"Average lost pixels at lower border: %.4g",
2421 lolost / (nspectra * nxok));
2422 cpl_msg_info(_task,
"Average spectrum width: %.4g +/- %.4g, "
2423 "(min, max) = (%.4g, %.4g)", sigmean, sigmask,
2424 cpl_matrix_get_min(mwid), cpl_matrix_get_max(mwid));
2426 cpl_matrix_delete(mwid);
2428 cpl_matrix_delete(mylo);
2429 cpl_matrix_delete(myup);
2430 cpl_matrix_delete(mxok);
2438_giraffe_finalize_fibers(cpl_table *fibers, cpl_matrix *locy, GiImage *mlocy,
2439 cxdouble maxoffset, cxdouble* maxshift)
2451 cxdouble max_shift = 0.;
2452 cxdouble *positions = NULL;
2454 cpl_image *_mlocy = NULL;
2457 if (fibers == NULL || locy == NULL || mlocy == NULL) {
2461 if (cpl_table_has_column(fibers,
"RINDEX") == FALSE) {
2465 nx = cpl_matrix_get_ncol(locy);
2466 ny = cpl_matrix_get_nrow(locy);
2468 nfibers = cpl_table_get_nrow(fibers);
2471 _nx = cpl_image_get_size_x(_mlocy);
2472 _ny = cpl_image_get_size_y(_mlocy);
2478 if (nfibers > _nx) {
2482 cpl_table_select_all(fibers);
2489 irow = (_ny - 1) / 2;
2490 positions = (cxdouble *)cpl_image_get_data(_mlocy) + irow * _nx;
2501 for (i = 0; i < nfibers; i++) {
2505 cxint pos = cpl_table_get_int(fibers,
"RINDEX", i, NULL) - 1;
2507 cxdouble yc = cpl_matrix_get(locy, irow, j);
2508 cxdouble shift = fabs(yc - positions[pos]);
2510 if (shift <= maxoffset) {
2511 cpl_table_unselect_row(fibers, i);
2515 max_shift = CX_MAX(max_shift, shift);
2521 cpl_table_erase_selected(fibers);
2523 if (maxshift != NULL) {
2524 *maxshift = max_shift;
2562 GiTable *fibers, GiLocalization *master,
2563 GiImage *badpixels, GiLocalizeConfig *config)
2566 const cxchar *fctid =
"giraffe_localize_spectra";
2578 cxdouble conad = 0.;
2579 cxdouble bias_ron = 0.;
2580 cxdouble mask_sigma = 0.;
2584 cpl_propertylist *properties;
2588 cpl_image *_result = NULL;
2592 cpl_table *_fibers = NULL;
2593 cpl_table *fiber_setup = NULL;
2596 GiLocalizeMethod method;
2598 GiInstrumentMode mode;
2600 GiMaskParameters mask_config;
2602 GiMaskPosition mask_position;
2603 GiMaskPosition mask_coeffs;
2611 if (result == NULL || image == NULL || fibers == NULL || config == NULL) {
2612 cpl_error_set(fctid, CPL_ERROR_NULL_INPUT);
2616 if (badpixels != NULL) {
2617 cpl_msg_debug(fctid,
"Bad pixel correction is not available. Bad "
2618 "pixel map will be ignored.");
2623 if (_fibers == NULL) {
2624 cpl_error_set(fctid, CPL_ERROR_DATA_NOT_FOUND);
2628 fiber_setup = _fibers;
2638 nfibers = cpl_table_get_nrow(_fibers);
2640 cpl_msg_info(fctid,
"Setting number of fibers (%s) to %d",
2641 GIALIAS_NFIBERS, nfibers);
2643 cpl_propertylist_update_int(properties, GIALIAS_NFIBERS, nfibers);
2644 cpl_propertylist_set_comment(properties, GIALIAS_NFIBERS,
2645 "Number of fibres");
2648 giraffe_error_push();
2652 if (cpl_error_get_code() != CPL_ERROR_NONE) {
2656 giraffe_error_pop();
2663 if (config->ron > 0.) {
2664 cpl_msg_info(fctid,
"Setting bias sigma value (%s) to %.5g",
2665 GIALIAS_BIASSIGMA, config->ron);
2666 cpl_propertylist_update_double(properties, GIALIAS_BIASSIGMA,
2671 cpl_msg_info(fctid,
"Bias sigma value: %.3g e-", bias_ron);
2674 if (cpl_propertylist_has(properties, GIALIAS_DATANCOM)) {
2675 nframes = cpl_propertylist_get_int(properties, GIALIAS_DATANCOM);
2679 if (config->noise > 0.) {
2680 cpl_msg_info(fctid,
"Noise multiplier: %.3g",
2684 cpl_msg_info(fctid,
"Threshold multiplier: %.3g",
2685 fabs(config->noise));
2693 nrows = cpl_image_get_size_y(_image);
2695 if (config->start < 0) {
2696 config->start = nrows / 2;
2706 if (config->ywidth < 1) {
2708 cpl_msg_info(fctid,
"Configuring equilizing filter width from "
2713 config->ywidth = 16;
2725 cpl_msg_error(fctid,
"Invalid instrument mode!");
2731 if (!cpl_propertylist_has(properties, GIALIAS_SLITNAME)) {
2732 cpl_msg_error(fctid,
"Property (%s) not found in raw image",
2737 const cxchar *slit =
2738 cpl_propertylist_get_string(properties, GIALIAS_SLITNAME);
2740 cpl_msg_info(fctid,
"Setting equilizing filter to %d [pxl] "
2741 "for slit configuration `%s'", config->ywidth,
2754 mwidth = GISPECTRUM_MWIDTH_MEDUSA;
2763 mwidth = GISPECTRUM_MWIDTH_IFU;
2772 mwidth = GISPECTRUM_MWIDTH_IFU;
2781 cpl_msg_error(fctid,
"Invalid instrument mode!");
2791 if (config->centroid == TRUE) {
2792 method = GILOCALIZE_BARYCENTER;
2795 method = GILOCALIZE_HALF_WIDTH;
2804 mask_config.ywidth = config->ywidth;
2805 mask_config.method = config->threshold;
2806 mask_config.threshold = config->noise;
2807 mask_config.ydeg = config->yorder;
2808 mask_config.wdeg = config->worder;
2809 mask_config.ewid = config->ewidth;
2810 mask_config.wavg = mwidth;
2811 mask_config.ckdata.width = ckwidth;
2812 mask_config.ckdata.height = ckheight;
2813 mask_config.ckdata.count = ckcount;
2814 mask_config.sigma = config->sigma;
2815 mask_config.niter = config->iterations;
2816 mask_config.mfrac = config->fraction;
2817 mask_config.start = config->start;
2818 mask_config.retry = config->retries;
2819 mask_config.xbin = config->binsize;
2829 if (config->noise > 0.) {
2830 mask_config.threshold *= sqrt(nframes * conad);
2844 if (config->full != TRUE) {
2846 cpl_msg_info(fctid,
"Computing spectrum localization using SIWC "
2849 if (!master || !master->locy || !master->locy) {
2850 cpl_msg_error(fctid,
"Required full master localization is "
2861 cpl_table_unselect_all(_fibers);
2862 cpl_table_or_selected_int(_fibers,
"RP", CPL_EQUAL_TO, -1);
2864 fiber_setup = cpl_table_extract_selected(_fibers);
2865 nfibers = cpl_table_get_nrow(fiber_setup);
2875 mask_position.type = GIMASK_FITTED_DATA;
2876 mask_position.my = cpl_matrix_new(nrows, nfibers);
2877 mask_position.mw = cpl_matrix_new(nrows, nfibers);
2879 mask_coeffs.type = GIMASK_FIT_COEFFS;
2880 mask_coeffs.my = cpl_matrix_new(mask_config.ydeg + 1, nfibers);
2881 mask_coeffs.mw = cpl_matrix_new(1, (mask_config.wdeg + 1) *
2882 (mask_config.wdeg + 1));
2890 _image = cpl_image_multiply_scalar_create(_image, nframes * conad);
2892 mask_sigma = sqrt(nframes) * bias_ron;
2899 status = _giraffe_localize_spectra(_image, _bpixel, fiber_setup,
2900 method, config->normalize,
2902 &mask_config, &mask_position,
2905 cpl_image_delete(_image);
2909 result->locy = NULL;
2910 result->locw = NULL;
2911 result->locc = NULL;
2914 cpl_matrix_delete(mask_position.my);
2915 cpl_matrix_delete(mask_position.mw);
2917 cpl_matrix_delete(mask_coeffs.my);
2918 cpl_matrix_delete(mask_coeffs.mw);
2920 if (config->full != TRUE) {
2921 cpl_table_delete(fiber_setup);
2924 cpl_msg_error(fctid,
"Spectrum localization computation failed!");
2934 if (config->full != TRUE) {
2941 cpl_table_delete(fiber_setup);
2946 if (master != NULL && master->locy != NULL) {
2948 cxint nf = cpl_table_get_nrow(_fibers);
2950 cxdouble maxoffset = 0.5 * mask_config.wavg;
2951 cxdouble maxshift = 0.;
2954 cpl_msg_info(fctid,
"Comparing detected and expected fiber "
2957 status = _giraffe_finalize_fibers(_fibers, mask_position.my,
2958 master->locy, maxoffset,
2966 cxint _nf = cpl_image_get_size_x(mlocy);
2968 cpl_msg_error(fctid,
"More fibers (%d) than expected "
2969 "(%d) were found!", nf, _nf);
2973 result->locy = NULL;
2974 result->locw = NULL;
2975 result->locc = NULL;
2978 cpl_matrix_delete(mask_position.my);
2979 cpl_matrix_delete(mask_position.mw);
2981 cpl_matrix_delete(mask_coeffs.my);
2982 cpl_matrix_delete(mask_coeffs.mw);
2984 if (config->full != TRUE) {
2985 cpl_table_delete(fiber_setup);
2988 cpl_msg_error(fctid,
"Comparison of fiber positions "
2994 cx_assert(cpl_table_get_nrow(_fibers) <= nf);
2996 cpl_msg_info(fctid,
"%" CPL_SIZE_FORMAT
" of %d expected fibers "
2997 "were detected.", cpl_table_get_nrow(_fibers), nf);
2999 if (cpl_table_get_nrow(_fibers) < nf) {
3000 cpl_msg_debug(fctid,
"Maximum offset from the expected "
3001 "position is %.2f, maximum allowed offset is %.2f",
3002 maxshift, maxoffset);
3003 cpl_msg_warning(fctid,
"%" CPL_SIZE_FORMAT
" fibers are "
3004 "missing!", nf - cpl_table_get_nrow(_fibers));
3021 cpl_matrix_get_ncol(mask_position.my),
3022 cpl_matrix_get_nrow(mask_position.my));
3025 cpl_matrix_delete(mask_position.my);
3032 cpl_propertylist_set_int(properties, GIALIAS_NAXIS1,
3033 cpl_image_get_size_x(_result));
3034 cpl_propertylist_set_int(properties, GIALIAS_NAXIS2,
3035 cpl_image_get_size_y(_result));
3036 cpl_propertylist_set_int(properties, GIALIAS_BITPIX, -32);
3037 cpl_propertylist_set_double(properties, GIALIAS_BZERO, 0.);
3038 cpl_propertylist_set_double(properties, GIALIAS_BSCALE, 1.);
3040 cpl_propertylist_append_int(properties, GIALIAS_LOCNX,
3041 cpl_image_get_size_y(_result));
3042 cpl_propertylist_append_int(properties, GIALIAS_LOCNS,
3043 cpl_image_get_size_x(_result));
3045 if (config->centroid) {
3046 cpl_propertylist_append_string(properties, GIALIAS_LMETHOD,
3050 cpl_propertylist_append_string(properties, GIALIAS_LMETHOD,
3054 if (config->normalize) {
3055 cpl_propertylist_append_int(properties, GIALIAS_LNORMALIZE,
3059 cpl_propertylist_append_int(properties, GIALIAS_LNORMALIZE,
3063 cpl_propertylist_append_bool(properties, GIALIAS_LFULLLOC, config->full);
3064 cpl_propertylist_append_int(properties, GIALIAS_LOCYDEG, config->yorder);
3065 cpl_propertylist_append_int(properties, GIALIAS_LOCWDEG, config->worder);
3066 cpl_propertylist_append_double(properties, GIALIAS_LEXTRAWID,
3068 cpl_propertylist_append_double(properties, GIALIAS_LNOISEMULT,
3071 cpl_propertylist_append_double(properties, GIALIAS_LCLIPSIGMA,
3073 cpl_propertylist_append_int(properties, GIALIAS_LCLIPNITER,
3074 config->iterations);
3075 cpl_propertylist_append_double(properties, GIALIAS_LCLIPMFRAC,
3079 if (cpl_propertylist_has(properties, GIALIAS_GIRFTYPE)) {
3080 cpl_propertylist_set_string(properties, GIALIAS_GIRFTYPE,
"LOCY");
3083 cpl_propertylist_append_string(properties, GIALIAS_GIRFTYPE,
"LOCY");
3085 cpl_propertylist_set_comment(properties, GIALIAS_GIRFTYPE,
"GIRAFFE "
3086 "localization centroid");
3093 cpl_matrix_get_ncol(mask_position.mw),
3094 cpl_matrix_get_nrow(mask_position.mw));
3097 cpl_matrix_delete(mask_position.mw);
3104 cpl_propertylist_set_int(properties, GIALIAS_NAXIS1,
3105 cpl_image_get_size_x(_result));
3106 cpl_propertylist_set_int(properties, GIALIAS_NAXIS2,
3107 cpl_image_get_size_y(_result));
3109 if (cpl_propertylist_has(properties, GIALIAS_GIRFTYPE)) {
3110 cpl_propertylist_set_string(properties, GIALIAS_GIRFTYPE,
3114 cpl_propertylist_append_string(properties, GIALIAS_GIRFTYPE,
3117 cpl_propertylist_set_comment(properties, GIALIAS_GIRFTYPE,
"GIRAFFE "
3118 "localization half-width");
3123 locc = cpl_table_new(cpl_matrix_get_ncol(mask_coeffs.my));
3125 cpl_table_new_column(locc,
"BUTTON", CPL_TYPE_INT);
3126 for (i = 0; i < cpl_table_get_nrow(locc); i++) {
3127 cpl_table_set_int(locc,
"BUTTON", i, i);
3130 for (i = 0; i < cpl_matrix_get_nrow(mask_coeffs.my); i++) {
3131 cxchar *label = NULL;
3133 cx_asprintf(&label,
"YC%d", i);
3134 cpl_table_new_column(locc, label, CPL_TYPE_DOUBLE);
3140 cpl_table_delete(locc);
3142 _my = cpl_matrix_transpose_create(mask_coeffs.my);
3144 cpl_matrix_delete(_my);
3145 cpl_matrix_delete(mask_coeffs.my);
3152 pname = cx_string_new();
3154 for (i = 0; i < cpl_matrix_get_ncol(mask_coeffs.mw); i++) {
3155 cx_string_sprintf(pname,
"%s%d", GIALIAS_LOCWIDCOEF, i);
3156 cpl_propertylist_append_double(properties, cx_string_get(pname),
3157 cpl_matrix_get(mask_coeffs.mw, 0, i));
3160 cx_string_delete(pname);
3161 cpl_matrix_delete(mask_coeffs.mw);
3163 cpl_propertylist_update_string(properties, GIALIAS_GIRFTYPE,
3165 cpl_propertylist_set_comment(properties, GIALIAS_GIRFTYPE,
"GIRAFFE "
3166 "localization fit coefficients");
3195 GiLocalizeConfig *config = NULL;
3202 config = cx_calloc(1,
sizeof *config);
3209 config->full = TRUE;
3210 config->centroid = TRUE;
3211 config->threshold = GILOCALIZE_THRESHOLD_LOCAL;
3214 p = cpl_parameterlist_find(list,
"giraffe.localization.mode");
3215 s = cpl_parameter_get_string(p);
3216 if (strcmp(s,
"siwc") == 0) {
3217 config->full = FALSE;
3220 p = cpl_parameterlist_find(list,
"giraffe.localization.start");
3221 config->start = cpl_parameter_get_int(p);
3223 p = cpl_parameterlist_find(list,
"giraffe.localization.retries");
3224 config->retries = cpl_parameter_get_int(p);
3226 p = cpl_parameterlist_find(list,
"giraffe.localization.binsize");
3227 config->binsize = cpl_parameter_get_int(p);
3229 p = cpl_parameterlist_find(list,
"giraffe.localization.ewidth");
3230 config->ewidth = cpl_parameter_get_double(p);
3232 p = cpl_parameterlist_find(list,
"giraffe.localization.ywidth");
3233 config->ywidth = cpl_parameter_get_int(p);
3235 p = cpl_parameterlist_find(list,
"giraffe.localization.center");
3236 s = cpl_parameter_get_string(p);
3237 if (!strcmp(s,
"hwidth")) {
3238 config->centroid = FALSE;
3241 p = cpl_parameterlist_find(list,
"giraffe.localization.normalize");
3242 config->normalize = cpl_parameter_get_bool(p);
3244 p = cpl_parameterlist_find(list,
"giraffe.localization.threshold");
3245 s = cpl_parameter_get_string(p);
3247 if (strncmp(s,
"global", 6) == 0) {
3248 config->threshold = GILOCALIZE_THRESHOLD_GLOBAL;
3250 else if (strncmp(s,
"row", 3) == 0) {
3251 config->threshold = GILOCALIZE_THRESHOLD_ROW;
3254 config->threshold = GILOCALIZE_THRESHOLD_LOCAL;
3257 p = cpl_parameterlist_find(list,
"giraffe.localization.noise");
3258 config->noise = cpl_parameter_get_double(p);
3260 p = cpl_parameterlist_find(list,
"giraffe.localization.ron");
3261 config->ron = cpl_parameter_get_double(p);
3263 p = cpl_parameterlist_find(list,
"giraffe.localization.yorder");
3264 config->yorder = cpl_parameter_get_int(p);
3266 p = cpl_parameterlist_find(list,
"giraffe.localization.worder");
3267 config->worder = cpl_parameter_get_int(p);
3269 p = cpl_parameterlist_find(list,
"giraffe.localization.sigma");
3270 config->sigma = cpl_parameter_get_double(p);
3272 p = cpl_parameterlist_find(list,
"giraffe.localization.iterations");
3273 config->iterations = cpl_parameter_get_int(p);
3275 p = cpl_parameterlist_find(list,
"giraffe.localization.fraction");
3276 config->fraction = cpl_parameter_get_double(p);
3330 p = cpl_parameter_new_enum(
"giraffe.localization.mode",
3332 "Localization mode: Use all spectra "
3333 "or the 5 SIWC spectra",
3334 "giraffe.localization",
3335 "all", 2,
"all",
"siwc");
3336 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI,
"sloc-mode");
3337 cpl_parameterlist_append(list, p);
3340 p = cpl_parameter_new_value(
"giraffe.localization.start",
3343 "giraffe.localization",
3345 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI,
"sloc-start");
3346 cpl_parameterlist_append(list, p);
3349 p = cpl_parameter_new_value(
"giraffe.localization.retries",
3351 "Initial localization detection "
3353 "giraffe.localization",
3355 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI,
"sloc-retries");
3356 cpl_parameterlist_append(list, p);
3359 p = cpl_parameter_new_value(
"giraffe.localization.binsize",
3361 "Initial localization detection "
3363 "giraffe.localization",
3365 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI,
"sloc-binsize");
3366 cpl_parameterlist_append(list, p);
3369 p = cpl_parameter_new_value(
"giraffe.localization.ewidth",
3371 "Localization detection extra width.",
3372 "giraffe.localization",
3374 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI,
"sloc-ewidth");
3375 cpl_parameterlist_append(list, p);
3378 p = cpl_parameter_new_value(
"giraffe.localization.ywidth",
3380 "Full width [pxl] of the equilizing "
3381 "filter (distance between two "
3382 "adjacent fibers).",
3383 "giraffe.localization",
3385 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI,
"sloc-ywidth");
3386 cpl_parameterlist_append(list, p);
3389 p = cpl_parameter_new_enum(
"giraffe.localization.center",
3391 "Method used for mask center "
3393 "giraffe.localization",
3394 "centroid", 2,
"centroid",
3396 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI,
"sloc-center");
3397 cpl_parameterlist_append(list, p);
3400 p = cpl_parameter_new_value(
"giraffe.localization.normalize",
3402 "Enable spectrum normalization along "
3403 "the dispersion axis.",
3404 "giraffe.localization",
3406 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI,
"sloc-norm");
3407 cpl_parameterlist_append(list, p);
3410 p = cpl_parameter_new_value(
"giraffe.localization.noise",
3412 "Threshold multiplier.",
3413 "giraffe.localization",
3415 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI,
"sloc-noise");
3416 cpl_parameterlist_append(list, p);
3419 p = cpl_parameter_new_enum(
"giraffe.localization.threshold",
3421 "Selects thresholding algorithm: local, "
3423 "giraffe.localization",
3424 "local", 3,
"local",
"row",
"global");
3425 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI,
"sloc-threshold");
3426 cpl_parameterlist_append(list, p);
3429 p = cpl_parameter_new_value(
"giraffe.localization.ron",
3431 "New bias sigma (RON) value for dark "
3433 "giraffe.localization",
3435 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI,
"sloc-ron");
3436 cpl_parameterlist_append(list, p);
3439 p = cpl_parameter_new_value(
"giraffe.localization.yorder",
3441 "Order of Chebyshev polynomial fit.",
3442 "giraffe.localization",
3444 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI,
"sloc-yorder");
3445 cpl_parameterlist_append(list, p);
3448 p = cpl_parameter_new_value(
"giraffe.localization.worder",
3450 "Order of Chebyshev 2D polynomial fit.",
3451 "giraffe.localization",
3453 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI,
"sloc-worder");
3454 cpl_parameterlist_append(list, p);
3457 p = cpl_parameter_new_value(
"giraffe.localization.sigma",
3459 "Localization clipping: sigma threshold "
3461 "giraffe.localization",
3463 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI,
"sloc-sigma");
3464 cpl_parameterlist_append(list, p);
3467 p = cpl_parameter_new_value(
"giraffe.localization.iterations",
3469 "Localization clipping: number of "
3471 "giraffe.localization",
3473 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI,
"sloc-niter");
3474 cpl_parameterlist_append(list, p);
3477 p = cpl_parameter_new_range(
"giraffe.localization.fraction",
3479 "Localization clipping: minimum fraction "
3480 "of points accepted/total.",
3481 "giraffe.localization",
3483 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI,
"sloc-mfrac");
3484 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.