29#include <cxstrutils.h>
31#include <cpl_parameterlist.h>
32#include <cpl_matrix.h>
47#include "gilocalization.h"
48#include "gimessages.h"
49#include "gifiberutils.h"
63 PROFILE_PSFEXP = 1 << 1,
64 PROFILE_PSFEXP2 = 1 << 2,
65 PROFILE_GAUSSIAN = 1 << 3
68typedef enum GiProfileId GiProfileId;
75struct GiExtractOptimalConfig {
89typedef struct GiExtractOptimalConfig GiExtractOptimalConfig;
96struct GiExtractHorneConfig {
105typedef struct GiExtractHorneConfig GiExtractHorneConfig;
108struct GiExtractionData {
115typedef struct GiExtractionData GiExtractionData;
118struct GiExtractionSlice {
126 cpl_matrix* variance;
130typedef struct GiExtractionSlice GiExtractionSlice;
133struct GiExtractionPsfLimits {
140typedef struct GiExtractionPsfLimits GiExtractionPsfLimits;
143struct GiExtractionWorkspace {
151typedef struct GiExtractionWorkspace GiExtractionWorkspace;
154struct GiVirtualSlit {
158 cxdouble extra_width;
169typedef struct GiVirtualSlit GiVirtualSlit;
176inline static GiExtractionSlice*
177_giraffe_extractionslice_new(cxint nflx, cxint ndata, cxint nbkg)
180 GiExtractionSlice* self = cx_malloc(
sizeof *self);
185 self->fsize = nflx + nbkg;
188 self->flux = cpl_matrix_new(self->fsize, 1);
189 self->variance = cpl_matrix_new(self->fsize, 1);
190 self->model = cpl_matrix_new(self->msize, 1);
198_giraffe_extractionslice_delete(GiExtractionSlice* self)
202 if (self->model != NULL) {
203 cpl_matrix_delete(self->model);
207 if (self->variance != NULL) {
208 cpl_matrix_delete(self->variance);
209 self->variance = NULL;
212 if (self->flux != NULL) {
213 cpl_matrix_delete(self->flux);
225inline static GiExtractionPsfLimits*
226_giraffe_extraction_psflimits_new(cxint size)
229 GiExtractionPsfLimits* self = cx_malloc(
sizeof *self);
233 self->ymin = cx_calloc(self->size,
sizeof(cxint));
234 self->ymax = cx_calloc(self->size,
sizeof(cxint));
242_giraffe_extraction_psflimits_delete(GiExtractionPsfLimits* self)
246 if (self->ymin != NULL) {
250 if (self->ymax != NULL) {
262inline static GiExtractionWorkspace*
263_giraffe_optimal_workspace_new(cxint m, cxint n)
266 GiExtractionWorkspace* self = cx_malloc(
sizeof *self);
269 self->atw = cpl_matrix_new(m, n);
270 self->atwa = cpl_matrix_new(m, m);
271 self->c = cpl_matrix_new(m, m);
272 self->atws = cpl_matrix_new(m, 1);
274 self->tmp = cpl_matrix_new(m, m);
282_giraffe_optimal_workspace_delete(GiExtractionWorkspace* self)
286 if (self->atws != NULL) {
287 cpl_matrix_delete(self->atws);
290 if (self->atwa != NULL) {
291 cpl_matrix_delete(self->atwa);
294 if (self->c != NULL) {
295 cpl_matrix_delete(self->c);
298 if (self->atw != NULL) {
299 cpl_matrix_delete(self->atw);
302 if (self->tmp != NULL) {
303 cpl_matrix_delete(self->tmp);
320_giraffe_virtualslit_allocate(GiVirtualSlit* self)
323 if ((self != NULL) && (self->width > 0)) {
325 self->position = cx_calloc(self->width,
sizeof(cxdouble));
326 self->signal = cx_calloc(self->width,
sizeof(cxdouble));
327 self->variance = cx_calloc(self->width,
sizeof(cxdouble));
328 self->fraction = cx_calloc(self->width,
sizeof(cxdouble));
330 self->mask = cx_calloc(self->width,
sizeof(cxdouble));
331 self->offset = cx_calloc(self->width,
sizeof(cxdouble));
340inline static GiVirtualSlit*
341_giraffe_virtualslit_new(cxdouble extra_width)
344 GiVirtualSlit* self = cx_calloc(1,
sizeof *self);
348 self->extra_width = extra_width;
350 self->position = NULL;
352 self->variance = NULL;
353 self->fraction = NULL;
363_giraffe_virtualslit_clear(GiVirtualSlit* self)
368 if (self->position != NULL) {
369 cx_free(self->position);
370 self->position = NULL;
373 if (self->signal != NULL) {
374 cx_free(self->signal);
378 if (self->variance != NULL) {
379 cx_free(self->variance);
380 self->variance = NULL;
383 if (self->fraction != NULL) {
384 cx_free(self->fraction);
385 self->fraction = NULL;
388 if (self->mask != NULL) {
393 if (self->offset != NULL) {
394 cx_free(self->offset);
398 self->extra_width = 0.;
410_giraffe_virtualslit_delete(GiVirtualSlit* self)
414 _giraffe_virtualslit_clear(self);
425_giraffe_virtualslit_setup(GiVirtualSlit* self, cxint bin,
426 cxdouble center, cxdouble width,
427 const cpl_image* signal,
const cpl_image* variance,
428 const cpl_image* bpixel)
431 register cxint ny = cpl_image_get_size_x(signal);
432 register cxint offset = bin * cpl_image_get_size_x(signal);
434 register cxdouble lower = center - (width + self->extra_width);
435 register cxdouble upper = center + (width + self->extra_width);
437 register cxint first = (cxint) floor(lower);
438 register cxint last = (cxint) ceil(upper);
440 const cxdouble* s = cpl_image_get_data_double_const(signal);
441 const cxdouble* v = cpl_image_get_data_double_const(variance);
448 lower = CX_MAX(0., lower);
449 upper = CX_MIN(ny, upper);
451 first = CX_MAX(0, first);
452 last = CX_MIN(ny, last);
454 self->center = center;
455 self->width = last - first + 1;
462 _giraffe_virtualslit_allocate(self);
464 if (bpixel != NULL) {
466 register cxint k = 0;
467 register cxint y = 0;
469 const cxint* _bpixel = cpl_image_get_data_int_const(bpixel);
472 for (y = first; y <= last; y++) {
474 register cxint ypos = offset + y;
476 cxint ok = (_bpixel[ypos] & GIR_M_PIX_SET) == 0 ? 1 : 0;
479 self->position[k] = y - center;
480 self->fraction[k] = 1.;
482 self->signal[k] = s[ypos];
483 self->variance[k] = v[ypos];
486 self->offset[k] = ypos;
494 register cxint k = 0;
495 register cxint y = 0;
498 for (y = first; y <= last; y++) {
500 register cxint ypos = offset + y;
505 self->position[k] = y - center;
506 self->fraction[k] = 1.;
508 self->signal[k] = s[ypos];
509 self->variance[k] = v[ypos];
512 self->offset[k] = ypos;
526 self->fraction[0] = ((cxdouble)first + 1.) - lower;
527 self->fraction[self->width - 1] = upper - ((cxdouble)last - 1.);
539_giraffe_matrix_invert(cpl_matrix* m_inv,
const cpl_matrix* m, cpl_matrix* lu)
544 cxint n = cpl_matrix_get_ncol(m);
546 register cxint sz = n * n *
sizeof(cxdouble);
548 const cxdouble* _m = cpl_matrix_get_data_const(m);
550 cxdouble* _m_inv = cpl_matrix_get_data(m_inv);
551 cxdouble* _m_lu = cpl_matrix_get_data(lu);
553 cpl_array* perm = cpl_array_new(n, CPL_TYPE_INT);
555 register cxint* perm_data = cpl_array_get_data_int(perm);
558 memset(_m_inv, 0, sz);
559 memcpy(_m_lu, _m, sz);
561 if (cpl_matrix_decomp_lu(lu, perm, &i) != 0) {
562 cpl_array_delete(perm);
571 for (i = 0; i < n; ++i) {
572 _m_inv[i * n + perm_data[i]] = 1.;
575 cpl_array_delete(perm);
578 status = cpl_matrix_solve_lu(lu, m_inv, NULL);
581 cpl_matrix_delete(m_inv);
594inline static cpl_matrix*
595_giraffe_compute_psf(GiModel* psf,
const cpl_matrix* x)
598 register cxint i = 0;
599 register cxint n = 0;
603 const cxdouble* _x = NULL;
607 cpl_matrix* y = NULL;
609 cx_assert(psf != NULL);
610 cx_assert(x != NULL);
611 cx_assert(cpl_matrix_get_ncol(x) == 1);
613 n = cpl_matrix_get_nrow(x);
615 y = cpl_matrix_new(n, 1);
617 _x = cpl_matrix_get_data_const(x);
618 _y = cpl_matrix_get_data(y);
620 for (i = 0; i < n; i++) {
621 giraffe_model_set_argument(psf,
"x", _x[i]);
622 giraffe_model_evaluate(psf, &_y[i], &status);
625 cpl_matrix_delete(y);
641_giraffe_horne_extract_slit(GiExtractionData* result,
642 const GiVirtualSlit* vslit, GiModel* psf,
643 const GiExtractHorneConfig* config)
653 cxdouble* tdata = NULL;
654 cxdouble* _mnpsf = NULL;
656 cpl_matrix* mnpsf = NULL;
657 cpl_matrix* mvslit = NULL;
665 mvslit = cpl_matrix_wrap(vslit->width, 1, vslit->position);
666 mnpsf = _giraffe_compute_psf(psf, mvslit);
668 cpl_matrix_unwrap(mvslit);
680 _mnpsf = cpl_matrix_get_data(mnpsf);
684 for (i = 0; i < vslit->width; ++i) {
685 _mnpsf[i] = CX_MAX(_mnpsf[i], 0.);
689 for (i = 0; i < vslit->width; ++i) {
698 tdata = cx_malloc(vslit->width *
sizeof(cxdouble));
703 while (i < vslit->width) {
704 if (vslit->mask[i] > 0) {
705 tdata[ngood] = CX_MAX(vslit->signal[i], 0.);
713 bkg = 0.5 * (tdata[0] + tdata[1]);
730 cxint niter = config->clip.iterations;
731 cxint nmin = (cxint)config->clip.fraction;
733 cxdouble sigma = config->clip.level * config->clip.level;
734 cxdouble* variance = NULL;
745 for (i = 0; i < vslit->width; ++i) {
746 if (vslit->mask[i] != 0) {
747 flx += (vslit->signal[i] - bkg) * vslit->fraction[i];
748 norm += vslit->fraction[i] * _mnpsf[i];
760 variance = cx_calloc(vslit->width,
sizeof(cxdouble));
762 for (i = 0; i < vslit->width; ++i) {
764 register cxdouble ve = flx * _mnpsf[i] + bkg;
766 variance[i] = vslit->variance[i] + fabs(vslit->fraction[i] * ve);
777 while ((iteration < niter) && (ngood > nmin) && (nreject != 0)) {
794 for (i = 0; i < vslit->width; ++i) {
796 if (vslit->mask[i] != 0) {
798 cxdouble m = vslit->signal[i] - bkg - flx * _mnpsf[i];
800 m *= vslit->fraction[i];
801 m *= m / variance[i] ;
812 if ((sigma > 0.) && (mmax > sigma)) {
813 vslit->mask[imax] = 0;
823 for (i = 0; i < vslit->width; ++i) {
825 if (vslit->mask[i] != 0) {
827 register cxdouble data = vslit->signal[i] - bkg;
828 register cxdouble p = _mnpsf[i];
830 data *= vslit->fraction[i];
831 p *= vslit->fraction[i];
833 norm += p * p / variance[i];
834 _flx += p * data / variance[i];
849 for (i = 0; i < vslit->width; ++i) {
851 register cxdouble ve = flx * _mnpsf[i] + bkg;
853 variance[i] = vslit->variance[i] + fabs(vslit->fraction[i] * ve);
866 cpl_matrix_delete(mnpsf);
870 result->error = sqrt(var);
871 result->position = vslit->center;
872 result->npixels = ngood;
874 return ngood == 0 ? 1 : 0;
923_giraffe_optimal_extract_slice(GiExtractionSlice* slice,
924 const cpl_matrix* AT,
927 GiExtractionPsfLimits* limits,
928 GiExtractionWorkspace* ws)
931 register cxint i = 0;
932 register cxint n = cpl_matrix_get_ncol(AT);
933 register cxint m = cpl_matrix_get_nrow(AT);
937 const cxdouble* at = cpl_matrix_get_data_const(AT);
938 const cxdouble* w = cpl_matrix_get_data_const(W);
939 const cxdouble* s = cpl_matrix_get_data_const(S);
940 const cxdouble* c = cpl_matrix_get_data_const(ws->c);
942 cxdouble* atw = cpl_matrix_get_data(ws->atw);
943 cxdouble* atwa = cpl_matrix_get_data(ws->atwa);
944 cxdouble* atws = cpl_matrix_get_data(ws->atws);
945 cxdouble* sf = cpl_matrix_get_data(slice->flux);
946 cxdouble* sv = cpl_matrix_get_data(slice->variance);
947 cxdouble* sm = cpl_matrix_get_data(slice->model);
950 for (i = 0; i < m; ++i) {
952 register cxint j = 0;
953 register cxint im = i * m;
954 register cxint in = i * n;
955 register cxint ymin = limits->ymin[i];
956 register cxint ymax = limits->ymax[i];
961 for (j = 0; j < n; ++j) {
963 register cxint k = in + j;
966 atw[k] = w[j] * at[k];
967 atws[i] += atw[k] * s[j];
971 for (j = 0; j < i; ++j) {
973 register cxint k = 0;
974 register cxint l = im + j;
977 for (k = ymin; k < ymax; ++k) {
978 atwa[l] += atw[in + k] * at[j * n + k];
981 atwa[j * m + i] = atwa[l];
987 for (j = ymin; j < ymax; ++j) {
988 atwa[im + i] += atw[in + j] * at[in + j];
994 status = _giraffe_matrix_invert(ws->c, ws->atwa, ws->tmp);
1000 for (i = 0; i < m; ++i) {
1002 register cxint j = 0;
1003 register cxint im = i * m;
1009 for (j = 0; j < m; ++j) {
1010 sf[i] += c[im + j] * atws[j];
1015 for (i = 0; i < n; ++i) {
1017 register cxint j = 0;
1022 for (j = 0; j < m; ++j) {
1023 sm[i] += at[j * n + i] * sf[j];
1057_giraffe_extract_summation(
const cpl_image* mz,
const cpl_image* mvarz,
1058 const cpl_table* fibers,
const cpl_image* my,
1059 const cpl_image* mw, cpl_image* mbpx,
1060 cpl_image* ms, cpl_image* mse,
1061 cpl_image* msn, cpl_image* msy)
1066 const cxchar* idx = NULL;
1068 cxint ny = cpl_image_get_size_x(mz);
1069 cxint nfibers = cpl_table_get_nrow(fibers);
1070 cxint nspectra = cpl_image_get_size_x(my);
1071 cxint nbins = cpl_image_get_size_y(my);
1073 const cxdouble* pixels = cpl_image_get_data_double_const(mz);
1074 const cxdouble* variances = cpl_image_get_data_double_const(mvarz);
1075 const cxdouble* locy = cpl_image_get_data_double_const(my);
1076 const cxdouble* locw = cpl_image_get_data_double_const(mw);
1078 cxdouble* flux = cpl_image_get_data_double(ms);
1079 cxdouble* flux_error = cpl_image_get_data_double(mse);
1080 cxdouble* flux_npixels = cpl_image_get_data_double(msn);
1081 cxdouble* flux_ypos = cpl_image_get_data_double(msy);
1089 cx_assert(nfibers <= nspectra);
1093 cx_assert(cpl_table_has_column(fibers, idx) != 0);
1097 const cxint* bpx = cpl_image_get_data_int(mbpx);
1099 for (nn = 0; nn < nfibers; nn++) {
1102 register cxint ns = cpl_table_get_int(fibers, idx, nn, NULL) - 1;
1105 for (x = 0; x < cpl_image_get_size_y(mz) && x < nbins; x++) {
1109 cxint lx = x * nspectra + ns;
1110 cxint sx = x * nfibers + nn;
1112 cxdouble ylower = locy[lx] - locw[lx];
1113 cxdouble yupper = locy[lx] + locw[lx];
1116 cxdouble error2 = 0.;
1120 flux_npixels[sx] = 0.;
1121 flux_error[sx] = 0.;
1129 if (locw[lx] <= 0.0) {
1141 ylo = (cxint) ceil(ylower);
1142 yup = (cxint) floor(yupper);
1145 if (yup < 0. || ylo - 1 >= ny) {
1165 if (!(bpx[x * ny + y] & GIR_M_PIX_SET)) {
1167 cxdouble extcoeff = (cxdouble)ylo - ylower;
1168 cxdouble extcoeff2 = extcoeff * extcoeff;
1169 cxdouble px = CX_MAX(pixels[x * ny + y], 0.);
1171 flux[sx] = pixels[x * ny + y] * extcoeff;
1172 flux_npixels[sx] = extcoeff;
1173 error2 = variances[x * ny + y] * extcoeff2;
1175 zsum = px * extcoeff;
1176 ysum = y * px * extcoeff;
1187 for (y = CX_MAX(ylo, 0); (y < yup) && (y < ny); y++) {
1189 if (!(bpx[x * ny + y] & GIR_M_PIX_SET)) {
1191 cxdouble px = CX_MAX(pixels[x * ny + y], 0.);
1193 flux[sx] += pixels[x * ny + y];
1194 flux_npixels[sx] += 1.0;
1195 error2 += variances[x * ny + y];
1213 if (!(bpx[x * ny + y] & GIR_M_PIX_SET)) {
1215 cxdouble extcoeff = yupper - (cxdouble)yup;
1216 cxdouble extcoeff2 = extcoeff * extcoeff;
1217 cxdouble px = CX_MAX(pixels[x * ny + y], 0.);
1219 flux[sx] += pixels[x * ny + y] * extcoeff;
1220 flux_npixels[sx] += extcoeff;
1221 error2 += variances[x * ny + y] * extcoeff2;
1223 zsum += px * extcoeff;
1224 ysum += y * px * extcoeff;
1230 flux_error[sx] = sqrt(error2);
1235 if (fabs(ysum) < DBL_EPSILON || fabs(zsum) < DBL_EPSILON) {
1236 flux_ypos[sx] = 0.5 * (yupper + ylower);
1239 flux_ypos[sx] = ysum / zsum;
1249 for (nn = 0; nn < nfibers; nn++) {
1252 register cxint ns = cpl_table_get_int(fibers, idx,
1256 for (x = 0; x < cpl_image_get_size_y(mz) && x < nbins; x++) {
1260 cxint lx = x * nspectra + ns;
1261 cxint sx = x * nfibers + nn;
1263 cxdouble yupper, ylower;
1266 cxdouble error2 = 0.;
1270 flux_npixels[sx] = 0.;
1271 flux_error[sx] = 0.;
1279 if (locw[lx] <= 0.0) {
1291 yupper = locy[lx] + locw[lx];
1292 ylower = locy[lx] - locw[lx];
1294 ylo = (cxint) ceil(ylower);
1295 yup = (cxint) floor(yupper);
1298 if (yup < 0. || ylo - 1 >= ny) {
1318 cxdouble extcoeff = (cxdouble)ylo - ylower;
1319 cxdouble extcoeff2 = extcoeff * extcoeff;
1320 cxdouble px = CX_MAX(pixels[x * ny + y], 0.);
1322 flux[sx] = pixels[x * ny + y] * extcoeff;
1323 flux_npixels[sx] = extcoeff;
1324 error2 = variances[x * ny + y] * extcoeff2;
1326 zsum = px * extcoeff;
1327 ysum = y * px * extcoeff;
1336 for (y = CX_MAX(ylo, 0); (y < yup) && (y < ny); y++) {
1338 cxdouble px = CX_MAX(pixels[x * ny + y], 0.);
1340 flux[sx] += pixels[x * ny + y];
1341 flux_npixels[sx] += 1.0;
1342 error2 += variances[x * ny + y];
1357 cxdouble extcoeff = yupper - (cxdouble)yup;
1358 cxdouble extcoeff2 = extcoeff * extcoeff;
1359 cxdouble px = CX_MAX(pixels[x * ny + y], 0.);
1361 flux[sx] += pixels[x * ny + y] * extcoeff;
1362 flux_npixels[sx] += extcoeff;
1363 error2 += variances[x * ny + y] * extcoeff2;
1365 zsum += px * extcoeff;
1366 ysum += y * px * extcoeff;
1370 flux_error[sx] = sqrt(error2);
1375 if (fabs(ysum) < DBL_EPSILON || fabs(zsum) < DBL_EPSILON) {
1376 flux_ypos[sx] = 0.5 * (yupper + ylower);
1379 flux_ypos[sx] = ysum / zsum;
1415_giraffe_extract_horne(
const cpl_image* mz,
const cpl_image* mzvar,
1416 const cpl_table* fibers,
const cpl_image* my,
1417 const cpl_image* mw,
const GiPsfData* psfdata,
1418 cpl_image* mbpx, cpl_image* ms, cpl_image* mse,
1419 cpl_image* msn, cpl_image* msy,
1420 const GiExtractHorneConfig* config)
1423 const cxchar* idx = NULL;
1430 const cxdouble* locy = NULL;
1431 const cxdouble* locw = NULL;
1432 const cxdouble* width = NULL;
1433 const cxdouble* exponent = NULL;
1435 GiModel* psfmodel = NULL;
1438 cx_assert(mz != NULL);
1439 cx_assert(mzvar != NULL);
1441 cx_assert(fibers != NULL);
1443 cx_assert(my != NULL);
1444 cx_assert(mw != NULL);
1446 cx_assert(psfdata != NULL);
1448 cx_assert(ms != NULL);
1449 cx_assert(mse != NULL);
1450 cx_assert(msn != NULL);
1451 cx_assert(msy != NULL);
1453 cx_assert(config != NULL);
1455 ny = cpl_image_get_size_x(mz);
1456 nx = cpl_image_get_size_y(mz);
1457 nfibers = cpl_table_get_nrow(fibers);
1459 locy = cpl_image_get_data_double_const(my);
1460 locw = cpl_image_get_data_double_const(mw);
1462 cx_assert((ny == cpl_image_get_size_x(mzvar)) &&
1463 (nx == cpl_image_get_size_y(mzvar)));
1465 cx_assert(cpl_image_get_size_x(my) == cpl_image_get_size_x(mw));
1466 cx_assert(cpl_image_get_size_y(my) == cpl_image_get_size_y(mw));
1468 cx_assert(giraffe_psfdata_fibers(psfdata) ==
1469 (cxsize)cpl_image_get_size_x(my));
1470 cx_assert(giraffe_psfdata_bins(psfdata) ==
1471 (cxsize)cpl_image_get_size_y(my));
1473 cx_assert((nfibers == cpl_image_get_size_x(ms)) &&
1474 (nx == cpl_image_get_size_y(ms)));
1475 cx_assert((nfibers == cpl_image_get_size_x(mse)) &&
1476 (nx == cpl_image_get_size_y(mse)));
1477 cx_assert((nfibers == cpl_image_get_size_x(msn)) &&
1478 (nx == cpl_image_get_size_y(msn)));
1479 cx_assert((nfibers == cpl_image_get_size_x(msy)) &&
1480 (nx == cpl_image_get_size_y(msy)));
1482 cx_assert((mbpx == NULL) || ((ny == cpl_image_get_size_x(mbpx)) &&
1483 (nx == cpl_image_get_size_y(mbpx))));
1493 cx_assert(cpl_table_has_column(fibers, idx) != 0);
1501 if (giraffe_psfdata_contains(psfdata,
"Center") == FALSE) {
1505 if (giraffe_psfdata_contains(psfdata,
"Width2") == TRUE) {
1506 exponent = cpl_image_get_data_const(giraffe_psfdata_get_data(psfdata,
1510 width = cpl_image_get_data_const(giraffe_psfdata_get_data(psfdata,
1518 psfmodel = giraffe_model_new(giraffe_psfdata_get_model(psfdata));
1520 if (psfmodel == NULL) {
1524 giraffe_model_set_parameter(psfmodel,
"Center", 0.);
1525 giraffe_model_set_parameter(psfmodel,
"Amplitude", 1.);
1526 giraffe_model_set_parameter(psfmodel,
"Background", 0.);
1533 for (fiber = 0; fiber < nfibers; ++fiber) {
1535 register cxint bin = 0;
1536 register cxint fidx = cpl_table_get_int(fibers, idx, fiber, NULL) - 1;
1538 cxint nbins = CX_MIN(nx, cpl_image_get_size_y(my));
1540 cxdouble* _ms = cpl_image_get_data_double(ms);
1541 cxdouble* _mse = cpl_image_get_data_double(mse);
1542 cxdouble* _msy = cpl_image_get_data_double(msy);
1543 cxdouble* _msn = cpl_image_get_data_double(msn);
1546 for (bin = 0; bin < nbins; bin++) {
1548 register cxint lpos = bin * cpl_image_get_size_x(my) + fidx;
1549 register cxint spos = bin * nfibers + fiber;
1554 register cxdouble lcenter = locy[lpos];
1555 register cxdouble lwidth = locw[lpos];
1557 register cxdouble ylower = lcenter - lwidth;
1558 register cxdouble yupper = lcenter + lwidth;
1560 GiVirtualSlit* vslit = NULL;
1562 GiExtractionData result = {0., 0., 0., 0.};
1569 if ((lwidth <= 0.) || (yupper < 0.) || (ylower > ny)) {
1577 vslit = _giraffe_virtualslit_new(config->ewidth);
1579 vwidth = _giraffe_virtualslit_setup(vslit, bin, lcenter, lwidth,
1583 _giraffe_virtualslit_delete(vslit);
1594 giraffe_model_set_parameter(psfmodel,
"Width1", width[lpos]);
1596 if (exponent != NULL) {
1597 giraffe_model_set_parameter(psfmodel,
"Width2",
1607 status = _giraffe_horne_extract_slit(&result, vslit, psfmodel,
1610 _giraffe_virtualslit_delete(vslit);
1615 giraffe_model_delete(psfmodel);
1621 _ms[spos] = result.value;
1622 _mse[spos] = result.error;
1623 _msy[spos] = result.position;
1624 _msn[spos] = result.npixels;
1631 giraffe_model_delete(psfmodel);
1645_giraffe_optimal_build_profiles(cpl_matrix* profiles,
1646 GiExtractionPsfLimits* limits,
1647 const cpl_image* my,
const cpl_image* mw,
1648 const cpl_table* fibers, cxint bin,
1649 GiModel* psf,
const cxdouble* width,
1650 const cxdouble* exponent, cxdouble wfactor)
1656 cxint nfibers = cpl_table_get_nrow(fibers);
1657 cxint ny = cpl_matrix_get_ncol(profiles);
1659 const cxdouble* locy = cpl_image_get_data_double_const(my);
1660 const cxdouble* locw = cpl_image_get_data_double_const(mw);
1662 cxdouble* _profiles = cpl_matrix_get_data(profiles);
1664 cxdouble* ypos = NULL;
1667 cx_assert(cpl_table_has_column(fibers, idx) != 0);
1668 cx_assert((limits == NULL) ||
1669 (cpl_matrix_get_nrow(profiles) == limits->size));
1671 ypos = cx_calloc(ny,
sizeof(cxdouble));
1673 for (fiber = 0; fiber < nfibers; ++fiber) {
1675 register cxint i = 0;
1676 register cxint y = 0;
1677 register cxint k = 0;
1679 cxint fidx = cpl_table_get_int(fibers, idx, fiber, NULL) - 1;
1680 cxint lpos = bin * cpl_image_get_size_x(my) + fidx;
1682 register cxdouble lcenter = locy[lpos];
1683 register cxdouble lwidth = locw[lpos];
1685 register cxdouble ylower = lcenter - fabs(wfactor) * lwidth;
1686 register cxdouble yupper = lcenter + fabs(wfactor) * lwidth;
1688 register cxint first = (cxint) floor(ylower);
1689 register cxint last = (cxint) ceil(yupper);
1691 register cxint vwidth = 0;
1694 cxdouble* _mnpsf = NULL;
1696 cpl_matrix* positions = NULL;
1697 cpl_matrix* mnpsf = NULL;
1704 ylower = CX_MAX(0., ylower);
1705 yupper = CX_MIN(ny - 1., yupper);
1707 first = CX_MAX(0, first);
1708 last = CX_MIN(ny - 1, last);
1710 vwidth = last - first + 1;
1716 if (limits != NULL) {
1717 limits->ymin[fiber] = first;
1718 limits->ymax[fiber] = last + 1;
1726 giraffe_model_set_parameter(psf,
"Width1", width[lpos]);
1728 if (exponent != NULL) {
1729 giraffe_model_set_parameter(psf,
"Width2", exponent[lpos]);
1738 for (y = first; y <= last; ++y) {
1739 ypos[k] = y - lcenter;
1743 positions = cpl_matrix_wrap(vwidth, 1, ypos);
1744 mnpsf = _giraffe_compute_psf(psf, positions);
1746 cpl_matrix_unwrap(positions);
1749 if (mnpsf == NULL) {
1756 _mnpsf = cpl_matrix_get_data(mnpsf);
1758 for (i = 0; i < vwidth; ++i) {
1759 _mnpsf[i] = CX_MAX(_mnpsf[i], 0.);
1763 for (i = 0; i < vwidth; ++i) {
1767 k = fiber * ny + first;
1768 for (y = 0; y < vwidth; ++y) {
1769 _profiles[k + y] = _mnpsf[y];
1772 cpl_matrix_delete(mnpsf);
1786_giraffe_extract_optimal(
const cpl_image* mz,
const cpl_image* mzvar,
1787 const cpl_table* fibers,
const cpl_image* my,
1788 const cpl_image* mw,
const GiPsfData* psfdata,
1789 cpl_image* mbpx, cpl_image* ms, cpl_image* mse,
1790 cpl_image* msm, cpl_image* msy,
1791 const GiExtractOptimalConfig* config)
1794 const cxbool nolimits = (config->limits == TRUE) ? FALSE : TRUE;
1796 const cxint bkg_nc = config->bkgorder + 1;
1797 const cxint niter = config->clip.iterations;
1799 register cxint i = 0;
1807 const cxdouble wfactor = config->ewidth;
1808 const cxdouble sigma = config->clip.level * config->clip.level;
1809 const cxdouble fraction = config->clip.fraction;
1811 const cxdouble* width = NULL;
1812 const cxdouble* exponent = NULL;
1814 cxdouble* _ypos = NULL;
1815 cxdouble* _bkg_base = NULL;
1816 cxdouble* _profiles = NULL;
1817 cxdouble* _signal = NULL;
1818 cxdouble* _variance = NULL;
1819 cxdouble* _mask = NULL;
1820 cxdouble* _weights = NULL;
1822 cpl_matrix* ypos = NULL;
1823 cpl_matrix* bkg_base = NULL;
1824 cpl_matrix* profiles = NULL;
1825 cpl_matrix* weights = NULL;
1826 cpl_matrix* signal = NULL;
1827 cpl_matrix* variance = NULL;
1828 cpl_matrix* mask = NULL;
1830 GiModel* psfmodel = NULL;
1832 GiExtractionPsfLimits* limits = NULL;
1834 GiExtractionSlice* slice = NULL;
1836 GiExtractionWorkspace* workspace;
1839 cx_assert(mz != NULL);
1840 cx_assert(mzvar != NULL);
1842 cx_assert(fibers != NULL);
1844 cx_assert(my != NULL);
1845 cx_assert(mw != NULL);
1847 cx_assert(psfdata != NULL);
1849 cx_assert(ms != NULL);
1850 cx_assert(mse != NULL);
1851 cx_assert(msm != NULL);
1852 cx_assert(msy != NULL);
1854 ny = cpl_image_get_size_x(mz);
1855 nx = cpl_image_get_size_y(mz);
1857 nfibers = cpl_table_get_nrow(fibers);
1858 nbins = CX_MIN(nx, cpl_image_get_size_y(my));
1860 cx_assert((ny == cpl_image_get_size_x(mzvar)) &&
1861 (nx == cpl_image_get_size_y(mzvar)));
1863 cx_assert(cpl_image_get_size_x(my) == cpl_image_get_size_x(mw));
1864 cx_assert(cpl_image_get_size_y(my) == cpl_image_get_size_y(mw));
1866 cx_assert(giraffe_psfdata_fibers(psfdata) ==
1867 (cxsize)cpl_image_get_size_x(my));
1868 cx_assert(giraffe_psfdata_bins(psfdata) ==
1869 (cxsize)cpl_image_get_size_y(my));
1871 cx_assert((nfibers == cpl_image_get_size_x(ms)) &&
1872 (nx == cpl_image_get_size_y(ms)));
1873 cx_assert((nfibers == cpl_image_get_size_x(mse)) &&
1874 (nx == cpl_image_get_size_y(mse)));
1875 cx_assert((nfibers == cpl_image_get_size_x(msy)) &&
1876 (nx == cpl_image_get_size_y(msy)));
1877 cx_assert((ny == cpl_image_get_size_x(msm)) &&
1878 (nx == cpl_image_get_size_y(msm)));
1880 cx_assert((mbpx == NULL) || ((ny == cpl_image_get_size_x(mbpx)) &&
1881 (nx == cpl_image_get_size_y(mbpx))));
1889 if (giraffe_psfdata_contains(psfdata,
"Center") == FALSE) {
1893 if (giraffe_psfdata_contains(psfdata,
"Width2") == TRUE) {
1894 exponent = cpl_image_get_data_const(giraffe_psfdata_get_data(psfdata,
1898 width = cpl_image_get_data_const(giraffe_psfdata_get_data(psfdata,
1906 psfmodel = giraffe_model_new(giraffe_psfdata_get_model(psfdata));
1908 if (psfmodel == NULL) {
1912 giraffe_model_set_parameter(psfmodel,
"Amplitude", 1.);
1913 giraffe_model_set_parameter(psfmodel,
"Background", 0.);
1914 giraffe_model_set_parameter(psfmodel,
"Center", 0.);
1921 ypos = cpl_matrix_new(ny, 1);
1924 giraffe_model_delete(psfmodel);
1930 _ypos = cpl_matrix_get_data(ypos);
1932 for (i = 0; i < ny; ++i) {
1942 profiles = cpl_matrix_new(nfibers + bkg_nc, ny);
1944 if (profiles == NULL) {
1945 cpl_matrix_delete(ypos);
1948 giraffe_model_delete(psfmodel);
1954 _profiles = cpl_matrix_get_data(profiles);
1957 signal = cpl_matrix_new(ny, 1);
1959 if (signal == NULL) {
1960 cpl_matrix_delete(profiles);
1963 cpl_matrix_delete(ypos);
1966 giraffe_model_delete(psfmodel);
1972 _signal = cpl_matrix_get_data(signal);
1975 variance = cpl_matrix_new(ny, 1);
1977 if (variance == NULL) {
1978 cpl_matrix_delete(signal);
1981 cpl_matrix_delete(profiles);
1984 cpl_matrix_delete(ypos);
1987 giraffe_model_delete(psfmodel);
1993 _variance = cpl_matrix_get_data(variance);
1996 mask = cpl_matrix_new(ny, 1);
1999 cpl_matrix_delete(variance);
2002 cpl_matrix_delete(signal);
2005 cpl_matrix_delete(profiles);
2008 cpl_matrix_delete(ypos);
2011 giraffe_model_delete(psfmodel);
2017 _mask = cpl_matrix_get_data(mask);
2020 weights = cpl_matrix_new(ny, 1);
2022 if (weights == NULL) {
2023 cpl_matrix_delete(mask);
2026 cpl_matrix_delete(variance);
2029 cpl_matrix_delete(signal);
2032 cpl_matrix_delete(profiles);
2035 cpl_matrix_delete(ypos);
2038 giraffe_model_delete(psfmodel);
2044 _weights = cpl_matrix_get_data(weights);
2052 bkg_base = giraffe_chebyshev_base1d(0., ny, bkg_nc, ypos);
2054 cpl_matrix_delete(ypos);
2057 if (bkg_base == NULL) {
2058 cpl_matrix_delete(weights);
2061 cpl_matrix_delete(mask);
2064 cpl_matrix_delete(variance);
2067 cpl_matrix_delete(signal);
2070 cpl_matrix_delete(profiles);
2073 cpl_matrix_delete(ypos);
2076 giraffe_model_delete(psfmodel);
2082 _bkg_base = cpl_matrix_get_data(bkg_base);
2084 for (i = 0; i < bkg_nc; ++i) {
2086 register cxint j = 0;
2087 register cxint offset = nfibers * ny;
2089 for (j = 0; j < ny; ++j) {
2090 _profiles[i * ny + j + offset] = _bkg_base[i * ny + j];
2097 cpl_matrix_delete(bkg_base);
2105 slice = _giraffe_extractionslice_new(nfibers, ny, bkg_nc);
2107 if (slice == NULL) {
2108 cpl_matrix_delete(weights);
2111 cpl_matrix_delete(mask);
2114 cpl_matrix_delete(variance);
2117 cpl_matrix_delete(signal);
2120 cpl_matrix_delete(profiles);
2123 cpl_matrix_delete(ypos);
2126 giraffe_model_delete(psfmodel);
2133 limits = _giraffe_extraction_psflimits_new(nfibers + bkg_nc);
2135 if (limits == NULL) {
2137 _giraffe_extractionslice_delete(slice);
2140 cpl_matrix_delete(weights);
2143 cpl_matrix_delete(mask);
2146 cpl_matrix_delete(variance);
2149 cpl_matrix_delete(signal);
2152 cpl_matrix_delete(profiles);
2155 cpl_matrix_delete(ypos);
2158 giraffe_model_delete(psfmodel);
2165 for (i = 0; i < limits->size; ++i) {
2166 limits->ymin[i] = 0;
2167 limits->ymax[i] = ny;
2175 workspace = _giraffe_optimal_workspace_new(nfibers + bkg_nc, ny);
2177 for (bin = 0; bin < nbins; ++bin) {
2179 cxbool stop = FALSE;
2185 const cxdouble* _my = cpl_image_get_data_double_const(my);
2186 const cxdouble* _mz = cpl_image_get_data_double_const(mz);
2187 const cxdouble* _mzvar = cpl_image_get_data_double_const(mzvar);
2189 cxdouble* _ms = cpl_image_get_data_double(ms);
2190 cxdouble* _mse = cpl_image_get_data_double(mse);
2191 cxdouble* _msy = cpl_image_get_data_double(msy);
2192 cxdouble* _msm = cpl_image_get_data_double(msm);
2196 GiExtractionPsfLimits* _limits = (nolimits == FALSE) ? limits : NULL;
2198 cx_assert(_mz != NULL);
2199 cx_assert(_mzvar != NULL);
2207 status = _giraffe_optimal_build_profiles(profiles, _limits, my, mw,
2208 fibers, bin, psfmodel, width,
2212 _giraffe_optimal_workspace_delete(workspace);
2215 _giraffe_extraction_psflimits_delete(limits);
2218 _giraffe_extractionslice_delete(slice);
2221 cpl_matrix_delete(weights);
2224 cpl_matrix_delete(mask);
2227 cpl_matrix_delete(variance);
2230 cpl_matrix_delete(signal);
2233 cpl_matrix_delete(profiles);
2236 cpl_matrix_delete(ypos);
2239 giraffe_model_delete(psfmodel);
2253 const cxint* _mbpx = cpl_image_get_data_int_const(mbpx);
2256 cx_assert(_mbpx != NULL);
2258 for (i = 0; i < ny; ++i) {
2260 cxbool bad = (_mbpx[bin * ny + i] & GIR_M_PIX_SET) ||
2261 (_mz[bin * ny + i] < 0.);
2263 _signal[i] = _mz[bin * ny + i];
2264 _variance[i] = _signal[i] + _mzvar[bin * ny + i];
2272 _weights[i] = _mask[i] / _variance[i];
2279 for (i = 0; i < ny; ++i) {
2281 cxbool bad = (_mz[bin * ny + i] < 0.);
2283 _signal[i] = _mz[bin * ny + i];
2284 _variance[i] = _signal[i] + _mzvar[bin * ny + i];
2292 _weights[i] = _mask[i] / _variance[i];
2304 nmin = (cxint)(fraction * ngood);
2306 while ((iter < niter) && (stop == FALSE)) {
2310 const cxdouble* _model = NULL;
2313 status = _giraffe_optimal_extract_slice(slice, profiles,
2314 signal, weights, limits, workspace);
2317 _giraffe_optimal_workspace_delete(workspace);
2320 _giraffe_extraction_psflimits_delete(limits);
2323 _giraffe_extractionslice_delete(slice);
2326 cpl_matrix_delete(weights);
2329 cpl_matrix_delete(mask);
2332 cpl_matrix_delete(variance);
2335 cpl_matrix_delete(signal);
2338 cpl_matrix_delete(profiles);
2341 cpl_matrix_delete(ypos);
2344 giraffe_model_delete(psfmodel);
2355 _model = cpl_matrix_get_data(slice->model);
2357 for (i = 0; i < ny; ++i) {
2359 if (_mask[i] > 0.) {
2362 cxdouble residual = _signal[i] - _model[i];
2365 _variance[i] = _model[i] + _mzvar[bin * ny + i];
2367 bad = (residual * residual) > (sigma * _variance[i]) ?
2376 _weights[i] = _mask[i] / _variance[i];
2382 if ((nreject == 0) || (ngood <= nmin)) {
2396 memcpy(&_ms[bin * nfibers], cpl_matrix_get_data(slice->flux),
2397 slice->nflx *
sizeof(cxdouble));
2398 memcpy(&_mse[bin * nfibers], cpl_matrix_get_data(slice->variance),
2399 slice->nflx *
sizeof(cxdouble));
2400 memcpy(&_msm[bin * ny], cpl_matrix_get_data(slice->model),
2401 slice->msize *
sizeof(cxdouble));
2403 memcpy(&_msy[bin * nfibers], &_my[bin * nfibers],
2404 nfibers *
sizeof(cxdouble));
2411 cpl_matrix_fill_window(profiles, 0., 0, 0, nfibers, ny);
2420 cpl_image_power(mse, 0.5);
2422 _giraffe_optimal_workspace_delete(workspace);
2425 _giraffe_extraction_psflimits_delete(limits);
2428 _giraffe_extractionslice_delete(slice);
2431 cpl_matrix_delete(weights);
2434 cpl_matrix_delete(mask);
2437 cpl_matrix_delete(variance);
2440 cpl_matrix_delete(signal);
2443 cpl_matrix_delete(profiles);
2446 giraffe_model_delete(psfmodel);
2480 GiTable* fibers, GiLocalization* sloc,
2481 GiImage* bpixel, GiImage* slight,
2482 GiExtractConfig* config)
2485 const cxchar *fctid =
"giraffe_extract_spectra";
2494 cxdouble bias_ron = 0.;
2495 cxdouble bias_sigma = 0.;
2496 cxdouble dark_value = 0.;
2497 cxdouble exptime = 0.;
2498 cxdouble conad = 1.;
2500 cpl_propertylist *properties;
2502 cpl_image* _image = NULL;
2503 cpl_image* _locy = NULL;
2504 cpl_image* _locw = NULL;
2505 cpl_image* _spectra = NULL;
2506 cpl_image* _error = NULL;
2507 cpl_image* _npixels = NULL;
2508 cpl_image* _centroid = NULL;
2509 cpl_image* _model = NULL;
2511 cpl_table* _fibers = NULL;
2518 if (!result || !image || !fibers || !sloc || !config) {
2519 cpl_error_set(fctid, CPL_ERROR_NULL_INPUT);
2524 if ((sloc->locy == NULL) || (sloc->locw == NULL)) {
2525 cpl_error_set(fctid, CPL_ERROR_NULL_INPUT);
2530 if (result->spectra != NULL || result->error != NULL ||
2531 result->npixels != NULL || result->centroid != NULL ||
2532 result->model != NULL) {
2533 gi_warning(
"%s: Results structure at %p is not empty! Contents "
2534 "might be lost.", fctid, result);
2540 if (_fibers == NULL) {
2541 cpl_error_set(fctid, CPL_ERROR_DATA_NOT_FOUND);
2546 if ((config->emethod == GIEXTRACT_OPTIMAL) && (sloc->psf == NULL)) {
2547 cpl_msg_error(fctid,
"Missing data: PSF profile data is required "
2548 "for optimal spectrum extraction!");
2549 cpl_error_set(fctid, CPL_ERROR_NULL_INPUT);
2557 if (properties == NULL) {
2558 cpl_error_set(fctid, CPL_ERROR_DATA_NOT_FOUND);
2563 giraffe_error_push();
2567 if (cpl_error_get_code() != CPL_ERROR_NONE) {
2571 giraffe_error_pop();
2574 if (!cpl_propertylist_has(properties, GIALIAS_BIASERROR)) {
2575 cpl_msg_warning(fctid,
"Missing bias error property (%s)! Setting "
2576 "bias error to 0.", GIALIAS_BIASERROR);
2580 bias_sigma = cpl_propertylist_get_double(properties, GIALIAS_BIASERROR);
2584 if (config->ron > 0.) {
2586 cpl_msg_info(fctid,
"Setting bias RMS property (%s) to %.4g ADU",
2587 GIALIAS_BIASSIGMA, config->ron);
2589 cpl_propertylist_update_double(properties, GIALIAS_BIASSIGMA,
2596 if (!cpl_propertylist_has(properties, GIALIAS_DARKVALUE)) {
2600 cpl_msg_warning(fctid,
"Missing dark value property (%s), will be "
2601 "set to 0.!", GIALIAS_DARKVALUE);
2602 cpl_propertylist_append_double(properties, GIALIAS_DARKVALUE,
2607 dark_value = cpl_propertylist_get_double(properties,
2612 if (!cpl_propertylist_has(properties, GIALIAS_EXPTIME)) {
2613 cpl_msg_error(fctid,
"Missing exposure time property (%s)!",
2618 exptime = cpl_propertylist_get_double(properties, GIALIAS_EXPTIME);
2622 if (cpl_propertylist_has(properties, GIALIAS_DATANCOM)) {
2623 nframes = cpl_propertylist_get_int(properties, GIALIAS_DATANCOM);
2635 bias_sigma *= conad;
2636 dark_value *= conad;
2656 ny = cpl_image_get_size_x(_image);
2657 nx = cpl_image_get_size_y(_locw);
2658 ns = cpl_table_get_nrow(_fibers);
2661 switch (config->emethod) {
2665 cxint xsize = cpl_image_get_size_x(_image);
2666 cxint ysize = cpl_image_get_size_y(_image);
2668 cxdouble ron_variance = bias_ron * bias_ron;
2669 cxdouble bias_variance = bias_sigma * bias_sigma;
2670 cxdouble dark_variance = dark_value * exptime;
2672 cpl_image* bpixmap = NULL;
2673 cpl_image* variance = NULL;
2680 result->model = NULL;
2687 if (bpixel != NULL) {
2691 if (cpl_image_get_size_x(bpixmap) != xsize ||
2692 cpl_image_get_size_y(bpixmap) != ysize) {
2694 cxbool crop = FALSE;
2696 cpl_propertylist *p =
2699 GiWindow w = {1, 1, 0, 0};
2702 w.x1 = cpl_image_get_size_x(bpixmap);
2703 w.y1 = cpl_image_get_size_y(bpixmap);
2705 if (cpl_propertylist_has(p, GIALIAS_PRSCX)) {
2706 w.x0 += cpl_propertylist_get_int(p, GIALIAS_PRSCX);
2710 if (cpl_propertylist_has(p, GIALIAS_OVSCX)) {
2711 w.x1 -= cpl_propertylist_get_int(p, GIALIAS_OVSCX);
2715 if (cpl_propertylist_has(p, GIALIAS_PRSCY)) {
2716 w.y0 += cpl_propertylist_get_int(p, GIALIAS_PRSCY);
2720 if (cpl_propertylist_has(p, GIALIAS_OVSCY)) {
2721 w.y1 -= cpl_propertylist_get_int(p, GIALIAS_OVSCY);
2725 if ((w.x1 - w.x0 + 1) != xsize ||
2726 (w.y1 - w.y0 + 1) != ysize) {
2727 cpl_msg_error(fctid,
"Invalid bad pixel map! Image "
2728 "sizes do not match!");
2731 result->spectra = NULL;
2734 result->error = NULL;
2737 result->npixels = NULL;
2740 result->centroid = NULL;
2743 result->model = NULL;
2745 cpl_image_delete(_image);
2752 bpixmap = cpl_image_extract(bpixmap, w.x0, w.y0,
2760 if (slight != NULL) {
2761 cpl_msg_warning(fctid,
"Scattered light model will be "
2762 "ignored for extraction method `SUM'");
2765 variance = cpl_image_abs_create(_image);
2773 cpl_image_add_scalar(variance, nframes * (ron_variance + nframes *
2774 (bias_variance + dark_variance)));
2776 status = _giraffe_extract_summation(_image, variance, _fibers,
2777 _locy, _locw, bpixmap,
2778 _spectra, _error, _npixels,
2781 cpl_image_delete(variance);
2783 cpl_image_delete(bpixmap);
2791 case GIEXTRACT_OPTIMAL:
2794 cxint xsize = cpl_image_get_size_x(_image);
2795 cxint ysize = cpl_image_get_size_y(_image);
2798 cxdouble ron_variance = bias_ron * bias_ron;
2799 cxdouble bias_variance = bias_sigma * bias_sigma;
2800 cxdouble dark_variance = dark_value * exptime;
2802 cpl_image* variance = NULL;
2803 cpl_image* bpixmap = NULL;
2805 GiExtractOptimalConfig setup;
2810 result->npixels = NULL;
2819 setup.clip.iterations = config->psf.iterations;
2820 setup.clip.level = config->psf.sigma;
2821 setup.clip.fraction = config->optimal.fraction;
2822 setup.limits = config->optimal.wfactor < 0. ? FALSE : TRUE;
2823 setup.ewidth = CX_MAX(1., fabs(config->optimal.wfactor));
2824 setup.bkgorder = config->optimal.bkgorder;
2825 setup.exptime = exptime;
2826 setup.ron = bias_sigma;
2827 setup.dark = dark_value;
2830 if (bpixel != NULL) {
2834 if (cpl_image_get_size_x(bpixmap) != xsize ||
2835 cpl_image_get_size_y(bpixmap) != ysize) {
2837 cxbool crop = FALSE;
2839 cpl_propertylist *p =
2842 GiWindow w = {1, 1, 0, 0};
2845 w.x1 = cpl_image_get_size_x(bpixmap);
2846 w.y1 = cpl_image_get_size_y(bpixmap);
2848 if (cpl_propertylist_has(p, GIALIAS_PRSCX)) {
2849 w.x0 += cpl_propertylist_get_int(p, GIALIAS_PRSCX);
2853 if (cpl_propertylist_has(p, GIALIAS_OVSCX)) {
2854 w.x1 -= cpl_propertylist_get_int(p, GIALIAS_OVSCX);
2858 if (cpl_propertylist_has(p, GIALIAS_PRSCY)) {
2859 w.y0 += cpl_propertylist_get_int(p, GIALIAS_PRSCY);
2863 if (cpl_propertylist_has(p, GIALIAS_OVSCY)) {
2864 w.y1 -= cpl_propertylist_get_int(p, GIALIAS_OVSCY);
2868 if ((w.x1 - w.x0 + 1) != xsize ||
2869 (w.y1 - w.y0 + 1) != ysize) {
2871 cpl_msg_error(fctid,
"Invalid bad pixel map! "
2872 "Image sizes do not match!");
2875 result->spectra = NULL;
2878 result->error = NULL;
2881 result->npixels = NULL;
2884 result->centroid = NULL;
2887 result->model = NULL;
2889 cpl_image_delete(_image);
2897 bpixmap = cpl_image_extract(bpixmap, w.x0, w.y0,
2905 variance = cpl_image_new(xsize, ysize, CPL_TYPE_DOUBLE);
2913 v0 = nframes * (ron_variance + nframes *
2914 (bias_variance + dark_variance));
2923 if (slight != NULL) {
2925 register cxsize i = 0;
2926 register cxsize npixels = xsize * ysize;
2928 const cxdouble* _slight =
2931 cxdouble* _variance = cpl_image_get_data_double(variance);
2933 for (i = 0; i < npixels; i++) {
2934 _variance[i] = v0 + fabs(_slight[i]) * conad * nframes;
2940 register cxsize i = 0;
2941 register cxsize npixels = xsize * ysize;
2943 cxdouble* _variance = cpl_image_get_data_double(variance);
2945 for (i = 0; i < npixels; i++) {
2952 status = _giraffe_extract_optimal(_image, variance, _fibers,
2953 _locy, _locw, sloc->psf,
2954 bpixmap, _spectra, _error,
2955 _model, _centroid, &setup);
2957 cpl_image_delete(variance);
2961 cpl_image_delete(bpixmap);
2969 case GIEXTRACT_HORNE:
2972 cxint xsize = cpl_image_get_size_x(_image);
2973 cxint ysize = cpl_image_get_size_y(_image);
2976 cxdouble ron_variance = bias_ron * bias_ron;
2977 cxdouble bias_variance = bias_sigma * bias_sigma;
2978 cxdouble dark_variance = dark_value * exptime;
2980 cpl_image* variance = NULL;
2981 cpl_image* bpixmap = NULL;
2983 GiExtractHorneConfig setup;
2990 result->model = NULL;
2997 setup.clip.iterations = config->psf.iterations;
2998 setup.clip.level = config->psf.sigma;
2999 setup.clip.fraction = config->horne.mingood;
3000 setup.ewidth = config->horne.ewidth;
3001 setup.exptime = exptime;
3002 setup.ron = bias_sigma;
3003 setup.dark = dark_value;
3005 if (bpixel != NULL) {
3009 if (cpl_image_get_size_x(bpixmap) != xsize ||
3010 cpl_image_get_size_y(bpixmap) != ysize) {
3012 cxbool crop = FALSE;
3014 cpl_propertylist *p =
3017 GiWindow w = {1, 1, 0, 0};
3020 w.x1 = cpl_image_get_size_x(bpixmap);
3021 w.y1 = cpl_image_get_size_y(bpixmap);
3023 if (cpl_propertylist_has(p, GIALIAS_PRSCX)) {
3024 w.x0 += cpl_propertylist_get_int(p, GIALIAS_PRSCX);
3028 if (cpl_propertylist_has(p, GIALIAS_OVSCX)) {
3029 w.x1 -= cpl_propertylist_get_int(p, GIALIAS_OVSCX);
3033 if (cpl_propertylist_has(p, GIALIAS_PRSCY)) {
3034 w.y0 += cpl_propertylist_get_int(p, GIALIAS_PRSCY);
3038 if (cpl_propertylist_has(p, GIALIAS_OVSCY)) {
3039 w.y1 -= cpl_propertylist_get_int(p, GIALIAS_OVSCY);
3043 if ((w.x1 - w.x0 + 1) != xsize ||
3044 (w.y1 - w.y0 + 1) != ysize) {
3046 cpl_msg_error(fctid,
"Invalid bad pixel map! "
3047 "Image sizes do not match!");
3050 result->spectra = NULL;
3053 result->error = NULL;
3056 result->npixels = NULL;
3059 result->centroid = NULL;
3062 result->model = NULL;
3064 cpl_image_delete(_image);
3072 bpixmap = cpl_image_extract(bpixmap, w.x0, w.y0,
3080 variance = cpl_image_new(xsize, ysize, CPL_TYPE_DOUBLE);
3088 v0 = nframes * (ron_variance + nframes *
3089 (bias_variance + dark_variance));
3099 if (slight != NULL) {
3101 register cxsize i = 0;
3102 register cxsize npixels = xsize * ysize;
3104 const cxdouble* _slight =
3107 cxdouble* _variance = cpl_image_get_data_double(variance);
3109 for (i = 0; i < npixels; i++) {
3110 _variance[i] = v0 + fabs(_slight[i]) * nframes * conad;
3116 register cxsize i = 0;
3117 register cxsize npixels = xsize * ysize;
3119 cxdouble* _variance = cpl_image_get_data_double(variance);
3121 for (i = 0; i < npixels; i++) {
3128 status = _giraffe_extract_horne(_image, variance, _fibers,
3129 _locy, _locw, sloc->psf,
3130 bpixmap, _spectra, _error,
3131 _npixels, _centroid, &setup);
3133 cpl_image_delete(variance);
3137 cpl_image_delete(bpixmap);
3146 gi_message(
"%s: Method %d selected for spectrum extraction.",
3147 fctid, config->emethod);
3148 cpl_msg_error(fctid,
"Invalid extraction method!");
3154 cpl_image_delete(_image);
3160 result->spectra = NULL;
3163 result->error = NULL;
3166 result->npixels = NULL;
3169 result->centroid = NULL;
3172 result->model = NULL;
3174 cpl_msg_error(fctid,
"Spectrum extraction (method %d) failed!",
3177 cpl_image_delete(_image);
3195 if (result->spectra) {
3200 if (result->model) {
3205 if (result->error) {
3230 cpl_propertylist_set_int(properties, GIALIAS_NAXIS1,
3231 cpl_image_get_size_x(_spectra));
3232 cpl_propertylist_set_int(properties, GIALIAS_NAXIS2,
3233 cpl_image_get_size_y(_spectra));
3235 cpl_propertylist_set_int(properties, GIALIAS_BITPIX, -32);
3236 cpl_propertylist_set_double(properties, GIALIAS_BZERO, 0.);
3237 cpl_propertylist_set_double(properties, GIALIAS_BSCALE, 1.);
3239 cpl_propertylist_update_int(properties, GIALIAS_NFIBERS,
3240 cpl_image_get_size_x(_spectra));
3242 cpl_propertylist_append_int(properties, GIALIAS_EXT_NX,
3243 cpl_image_get_size_y(_spectra));
3244 cpl_propertylist_append_int(properties, GIALIAS_EXT_NS,
3245 cpl_image_get_size_x(_spectra));
3247 switch (config->emethod) {
3249 cpl_propertylist_append_string(properties, GIALIAS_EXT_METHOD,
3251 cpl_propertylist_set_comment(properties, GIALIAS_EXT_METHOD,
3252 "Spectrum extraction method");
3255 case GIEXTRACT_HORNE:
3258 cpl_propertylist_append_string(properties, GIALIAS_EXT_METHOD,
3260 cpl_propertylist_set_comment(properties, GIALIAS_EXT_METHOD,
3261 "Spectrum extraction method");
3263 cpl_propertylist_append_string(properties, GIALIAS_EXTPSF_MODEL,
3265 cpl_propertylist_set_comment(properties, GIALIAS_EXTPSF_MODEL,
3267 cpl_propertylist_append_double(properties, GIALIAS_EXTPSF_SIGMA,
3269 cpl_propertylist_set_comment(properties, GIALIAS_EXTPSF_SIGMA,
3270 "PSF fit sigma clipping threshold");
3271 cpl_propertylist_append_int(properties, GIALIAS_EXTPSF_NITER,
3272 config->psf.iterations);
3273 cpl_propertylist_set_comment(properties, GIALIAS_EXTPSF_NITER,
3274 "PSF fit maximum number of "
3277 cpl_propertylist_append_int(properties, GIALIAS_EXTHRN_EWIDTH,
3278 config->horne.ewidth);
3279 cpl_propertylist_set_comment(properties, GIALIAS_EXTHRN_EWIDTH,
3280 "Number of extra pixels used");
3281 cpl_propertylist_append_int(properties, GIALIAS_EXTHRN_MINGOOD,
3282 config->horne.mingood);
3283 cpl_propertylist_set_comment(properties, GIALIAS_EXTHRN_MINGOOD,
3284 "Minimum number of pixels to keep");
3290 case GIEXTRACT_OPTIMAL:
3291 cpl_propertylist_append_string(properties, GIALIAS_EXT_METHOD,
3293 cpl_propertylist_set_comment(properties, GIALIAS_EXT_METHOD,
3294 "Spectrum extraction method");
3296 cpl_propertylist_append_string(properties, GIALIAS_EXTPSF_MODEL,
3298 cpl_propertylist_set_comment(properties, GIALIAS_EXTPSF_MODEL,
3300 cpl_propertylist_append_double(properties, GIALIAS_EXTPSF_SIGMA,
3302 cpl_propertylist_set_comment(properties, GIALIAS_EXTPSF_SIGMA,
3303 "PSF fit sigma clipping threshold");
3304 cpl_propertylist_append_int(properties, GIALIAS_EXTPSF_NITER,
3305 config->psf.iterations);
3306 cpl_propertylist_set_comment(properties, GIALIAS_EXTPSF_NITER,
3307 "PSF fit maximum number of "
3310 cpl_propertylist_append_double(properties, GIALIAS_EXTOPT_FRACTION,
3311 config->optimal.fraction);
3312 cpl_propertylist_set_comment(properties, GIALIAS_EXTOPT_FRACTION,
3313 "Minimum fraction of pixels used.");
3314 cpl_propertylist_append_double(properties, GIALIAS_EXTOPT_WFACTOR,
3315 config->optimal.wfactor);
3316 cpl_propertylist_set_comment(properties, GIALIAS_EXTOPT_WFACTOR,
3317 "Multiple of the fiber PSF half "
3318 "width used for spectrum "
3320 cpl_propertylist_append_int(properties, GIALIAS_EXTOPT_BGORDER,
3321 config->optimal.bkgorder);
3322 cpl_propertylist_set_comment(properties, GIALIAS_EXTOPT_BGORDER,
3323 "Order of the background polynomial "
3324 "model along the spatial direction.");
3332 cpl_propertylist_update_string(properties, GIALIAS_GIRFTYPE,
"EXTSP");
3333 cpl_propertylist_set_comment(properties, GIALIAS_GIRFTYPE,
3334 "Extracted spectra");
3344 cpl_propertylist_set_string(properties, GIALIAS_GIRFTYPE,
"EXTERRS");
3345 cpl_propertylist_set_comment(properties, GIALIAS_GIRFTYPE,
3346 "Extracted spectra errors");
3356 cpl_propertylist_set_string(properties, GIALIAS_GIRFTYPE,
"EXTYCEN");
3357 cpl_propertylist_set_comment(properties, GIALIAS_GIRFTYPE,
3358 "Extracted spectra centroids");
3365 if (result->npixels != NULL) {
3369 cpl_propertylist_set_string(properties, GIALIAS_GIRFTYPE,
"EXTNPIX");
3370 cpl_propertylist_set_comment(properties, GIALIAS_GIRFTYPE,
3371 "Extracted spectra npixels");
3379 if (result->model != NULL) {
3383 cpl_propertylist_set_string(properties, GIALIAS_GIRFTYPE,
"EXTMODEL");
3384 cpl_propertylist_set_comment(properties, GIALIAS_GIRFTYPE,
3385 "Model spectra used for extraction");
3410 GiExtractConfig* config = NULL;
3417 config = cx_calloc(1,
sizeof *config);
3419 p = cpl_parameterlist_find(list,
"giraffe.extraction.method");
3420 s = cpl_parameter_get_string(p);
3421 if (!strcmp(s,
"OPTIMAL")) {
3422 config->emethod = GIEXTRACT_OPTIMAL;
3424 else if (!strcmp(s,
"HORNE")) {
3425 config->emethod = GIEXTRACT_HORNE;
3428 config->emethod = GIEXTRACT_SUM;
3431 p = cpl_parameterlist_find(list,
"giraffe.extraction.ron");
3432 config->ron = cpl_parameter_get_double(p);
3434 p = cpl_parameterlist_find(list,
"giraffe.extraction.psf.model");
3435 config->psf.model = cx_strdup(cpl_parameter_get_string(p));
3437 p = cpl_parameterlist_find(list,
"giraffe.extraction.psf.sigma");
3438 config->psf.sigma = cpl_parameter_get_double(p);
3440 p = cpl_parameterlist_find(list,
"giraffe.extraction.psf.iterations");
3441 config->psf.iterations = cpl_parameter_get_int(p);
3444 p = cpl_parameterlist_find(list,
"giraffe.extraction.horne.extrawidth");
3445 config->horne.ewidth = cpl_parameter_get_int(p);
3447 p = cpl_parameterlist_find(list,
"giraffe.extraction.horne.mingood");
3448 config->horne.mingood = cpl_parameter_get_double(p);
3451 p = cpl_parameterlist_find(list,
"giraffe.extraction.optimal.fraction");
3452 config->optimal.fraction = cpl_parameter_get_double(p);
3454 p = cpl_parameterlist_find(list,
"giraffe.extraction.optimal.wfactor");
3455 config->optimal.wfactor = cpl_parameter_get_double(p);
3457 p = cpl_parameterlist_find(list,
"giraffe.extraction.optimal.bkgorder");
3458 config->optimal.bkgorder = cpl_parameter_get_int(p);
3483 if (config->psf.model) {
3484 cx_free(config->psf.model);
3511 cpl_parameter* p = NULL;
3518 p = cpl_parameter_new_enum(
"giraffe.extraction.method",
3520 "Extraction method: 'SUM', 'HORNE' or "
3522 "giraffe.extraction",
3523 "SUM", 3,
"SUM",
"OPTIMAL",
"HORNE");
3524 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI,
"extr-method");
3525 cpl_parameterlist_append(list, p);
3528 p = cpl_parameter_new_value(
"giraffe.extraction.ron",
3530 "New bias sigma (RON) value for "
3533 "giraffe.extraction",
3535 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI,
"extr-ron");
3536 cpl_parameterlist_append(list, p);
3539 p = cpl_parameter_new_enum(
"giraffe.extraction.psf.model",
3541 "PSF profile model: `psfexp', `psfexp2'",
3542 "giraffe.extraction.psf",
3543 "psfexp2", 2,
"psfexp",
"psfexp2");
3544 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI,
"extr-psfmodel");
3545 cpl_parameterlist_append(list, p);
3548 p = cpl_parameter_new_value(
"giraffe.extraction.psf.sigma",
3550 "Sigma clippging threshold used for "
3551 "rejecting data points during PSF fitting "
3552 "(Horne's sigma). It is used to reject bad "
3553 "pixels and cosmics.",
3554 "giraffe.extraction.psf",
3556 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI,
"extr-psfsigma");
3557 cpl_parameterlist_append(list, p);
3560 p = cpl_parameter_new_value(
"giraffe.extraction.psf.iterations",
3562 "Maximum number of iterations used for "
3563 "fitting the PSF profile.",
3564 "giraffe.extraction.psf",
3566 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI,
"extr-psfniter");
3567 cpl_parameterlist_append(list, p);
3570 p = cpl_parameter_new_value(
"giraffe.extraction.horne.extrawidth",
3572 "Horne extraction method: Number of "
3573 "extra pixels added to the fiber "
3575 "giraffe.extraction.horne",
3577 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI,
"extr-hewidth");
3578 cpl_parameterlist_append(list, p);
3581 p = cpl_parameter_new_value(
"giraffe.extraction.horne.mingood",
3583 "Horne extraction method: Minimum number of "
3584 "points used for the profile fit. It sets "
3585 "the lower limit of data points for the "
3587 "giraffe.extraction.horne",
3589 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI,
"extr-hmingood");
3590 cpl_parameterlist_append(list, p);
3593 p = cpl_parameter_new_range(
"giraffe.extraction.optimal.fraction",
3595 "Optimal extraction method: Minimum fraction "
3596 "of the data points used for fitting the "
3597 "fiber profiles. It sets the lower limit "
3598 "for the pixel rejection.",
3599 "giraffe.extraction.optimal",
3601 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI,
"extr-omfrac");
3602 cpl_parameterlist_append(list, p);
3605 p = cpl_parameter_new_value(
"giraffe.extraction.optimal.wfactor",
3607 "Optimal extraction method: Factor by which "
3608 "the fiber PSF half width is multiplied. "
3609 "Adjacent spectra within this area are "
3610 "assumed to affect the spectrum being "
3612 "giraffe.extraction.optimal",
3614 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI,
"extr-owfactor");
3615 cpl_parameterlist_append(list, p);
3618 p = cpl_parameter_new_value(
"giraffe.extraction.optimal.bkgorder",
3620 "Optimal extraction method: Order of the "
3621 "polynomial background model, which is "
3622 "fitted for each wavelength bin along the "
3623 "spatial direction.",
3624 "giraffe.extraction.optimal",
3626 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI,
"extr-obkgorder");
3627 cpl_parameterlist_append(list, p);
cxint giraffe_array_sort(cxdouble *array, cxsize size)
Sorts an array in ascending order.
const cxchar * giraffe_fiberlist_query_index(const cpl_table *fibers)
Query a fiber list for the name of the fiber reference index column.
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.
void giraffe_image_delete(GiImage *self)
Destroys an image.
GiImage * giraffe_image_create(cpl_type type, cxint nx, cxint ny)
Creates an image container of a given type.
cxint giraffe_image_set_properties(GiImage *self, cpl_propertylist *properties)
Attaches a property list to an image.
void gi_warning(const cxchar *format,...)
Log a warning.
void gi_message(const cxchar *format,...)
Log a normal message.
cpl_table * giraffe_table_get(const GiTable *self)
Get the table data from a Giraffe table.
cxdouble giraffe_propertylist_get_conad(const cpl_propertylist *properties)
Retrieve the ADU to electrons conversion factor from the given properties.
cxint giraffe_propertylist_update(cpl_propertylist *self, cpl_propertylist *properties, const cxchar *regexp)
Update a property list.
cxdouble giraffe_propertylist_get_ron(const cpl_propertylist *properties)
Retrieve the read-out noise from the given properties.