36 #include "muse_exp_align_z.h" 39 typedef struct muse_indexpair muse_indexpair;
41 struct muse_indexpair {
46 typedef void (*muse_free_func)(
void *);
50 static const double deg2as = 3600.;
58 muse_vfree(
void **array, cpl_size size, muse_free_func deallocator)
62 for (idx = 0; idx < size; ++idx) {
64 deallocator(array[idx]);
73 _muse_condition_less_than(
double aValue1,
double aValue2)
75 return (aValue1 < aValue2) ? TRUE : FALSE;
94 muse_utils_replace_nan(
muse_image *aImage,
float aValue)
96 cpl_ensure(aImage, CPL_ERROR_NULL_INPUT, CPL_ERROR_NULL_INPUT);
97 cpl_ensure(aImage->
data, CPL_ERROR_ILLEGAL_INPUT, CPL_ERROR_ILLEGAL_INPUT);
99 cpl_size npixel = cpl_image_get_size_x(aImage->
data) *
100 cpl_image_get_size_y(aImage->
data);
102 float *data = cpl_image_get_data_float(aImage->
data);
103 if (cpl_error_get_code() == CPL_ERROR_TYPE_MISMATCH) {
104 cpl_error_set_message(__func__, CPL_ERROR_TYPE_MISMATCH,
105 "MUSE image data buffer has invalid type!");
106 return CPL_ERROR_TYPE_MISMATCH;
110 for (ipixel = 0; ipixel < npixel; ++ipixel) {
111 if (isnan(data[ipixel])) {
112 data[ipixel] = aValue;
117 npixel = cpl_image_get_size_x(aImage->
stat) *
118 cpl_image_get_size_y(aImage->
stat);
120 data = cpl_image_get_data_float(aImage->
stat);
121 if (cpl_error_get_code() == CPL_ERROR_TYPE_MISMATCH) {
122 cpl_error_set_message(__func__, CPL_ERROR_TYPE_MISMATCH,
123 "MUSE image data buffer has invalid type!");
124 return CPL_ERROR_TYPE_MISMATCH;
127 for (ipixel = 0; ipixel < npixel; ++ipixel) {
128 if (isnan(data[ipixel])) {
129 data[ipixel] = aValue;
134 return CPL_ERROR_NONE;
162 image->
header = cpl_propertylist_load(aFilename, 0);
164 cpl_error_set_message(__func__, cpl_error_get_code(),
"Loading primary FITS " 165 "header of \"%s\" did not succeed", aFilename);
171 int extension = cpl_fits_find_extension(aFilename, EXTNAME_DATA);
172 cpl_propertylist *hdata = cpl_propertylist_load(aFilename, extension);
176 cpl_msg_debug(__func__,
"Skipping extension %d [%s]", extension,
178 cpl_propertylist_delete(hdata);
180 hdata = cpl_propertylist_load(aFilename, extension);
182 cpl_msg_debug(__func__,
"Taking extension %d [%s]", extension,
187 image->
data = cpl_image_load(aFilename, CPL_TYPE_FLOAT, 0, extension);
189 cpl_error_set_message(__func__, MUSE_ERROR_READ_DATA,
"Could not load " 190 "extension %s from \"%s\"", extname, aFilename);
196 if (cpl_propertylist_has(hdata,
"BUNIT")) {
197 cpl_propertylist_append_string(image->
header,
"BUNIT",
199 cpl_propertylist_set_comment(image->
header,
"BUNIT",
200 cpl_propertylist_get_comment(hdata,
"BUNIT"));
202 cpl_msg_warning(__func__,
"No BUNIT given in extension %d [%s] of \"%s\"!",
203 extension, extname, aFilename);
206 if (!cpl_propertylist_has(hdata,
"CUNIT1") ||
207 !cpl_propertylist_has(hdata,
"CUNIT2")) {
208 cpl_msg_warning(__func__,
"No WCS found in extension %d [%s] of \"%s\"!",
209 extension, extname, aFilename);
214 cpl_propertylist_erase_regexp(hdata,
"(^ESO |" MUSE_WCS_KEYS ")", 1);
215 cpl_propertylist_append(image->
header, hdata);
216 cpl_propertylist_delete(hdata);
220 if (extname && !strncmp(extname, EXTNAME_DATA, strlen(EXTNAME_DATA)+1)) {
222 extension = cpl_fits_find_extension(aFilename, EXTNAME_STAT);
225 char *statname = cpl_sprintf(
"%s_STAT", extname);
226 extension = cpl_fits_find_extension(aFilename, statname);
230 cpl_errorstate status = cpl_errorstate_get();
231 image->
stat = cpl_image_load(aFilename, CPL_TYPE_INT, 0, extension);
232 if (!cpl_errorstate_is_equal(status)) {
233 if (cpl_error_get_code() == CPL_ERROR_DATA_NOT_FOUND) {
234 cpl_errorstate_set(status);
235 cpl_msg_debug(__func__,
"Ignoring empty extension %s in \"%s\"",
236 EXTNAME_STAT, aFilename);
238 cpl_error_set_message(__func__, MUSE_ERROR_READ_STAT,
"Could not load " 239 "extension %s from \"%s\"", EXTNAME_STAT, aFilename);
249 if (extname && !strncmp(extname, EXTNAME_DATA, strlen(EXTNAME_DATA)+1)) {
251 extension = cpl_fits_find_extension(aFilename, EXTNAME_DQ);
254 char *dqname = cpl_sprintf(
"%s_DQ", extname);
255 extension = cpl_fits_find_extension(aFilename, dqname);
259 cpl_errorstate status = cpl_errorstate_get();
260 image->
dq = cpl_image_load(aFilename, CPL_TYPE_INT, 0, extension);
261 if (!cpl_errorstate_is_equal(status)) {
262 cpl_errorstate_set(status);
263 cpl_error_set_message(__func__, MUSE_ERROR_READ_DQ,
"Could not load " 264 "extension %s from \"%s\"", EXTNAME_DQ, aFilename);
288 cpl_ensure(aProcessing, CPL_ERROR_NULL_INPUT, NULL);
289 cpl_size nframes = cpl_frameset_get_size(aProcessing->
inframes);
290 cpl_ensure(nframes, CPL_ERROR_DATA_NOT_FOUND, NULL);
294 cpl_size iexposure = 0;
296 for (iframe = 0; iframe < nframes; ++iframe) {
297 const cpl_frame *frame = cpl_frameset_get_position(aProcessing->
inframes,
299 const char *tag = cpl_frame_get_tag(frame);
302 const char *filename = cpl_frame_get_filename(frame);
304 cpl_msg_debug(__func__,
"Loading FOV image '%s' as exposure %" 305 CPL_SIZE_FORMAT, filename, iexposure + 1);
309 cpl_msg_error(__func__,
"Could not load FOV image '%s'", filename);
316 CPL_FRAME_GROUP_RAW, 1);
335 cpl_error_set_message(__func__, CPL_ERROR_ILLEGAL_INPUT,
336 "Fraction of image pixel to ignore for the " 337 "background estimation must be greater than" 338 "or equal to zero!");
342 cpl_error_set_message(__func__, CPL_ERROR_ILLEGAL_INPUT,
343 "Fraction of image pixels to use for the " 344 "background estimation must be greater " 348 if (aParams->
fwhm < 0.5) {
349 cpl_error_set_message(__func__, CPL_ERROR_ILLEGAL_INPUT,
350 "FWHM must be greater than 0.5 pixels!");
354 cpl_error_set_message(__func__, CPL_ERROR_ILLEGAL_INPUT,
355 "Maximum number of sources must be grater than the " 356 "minimum number of sources!");
360 cpl_error_set_message(__func__, CPL_ERROR_ILLEGAL_INPUT,
361 "Upper limit of the point-source roundness must be " 362 "greater than the lower limit!");
366 cpl_error_set_message(__func__, CPL_ERROR_ILLEGAL_INPUT,
367 "Low limit of the point-source sharpness must be " 368 "greater than zero!");
372 cpl_error_set_message(__func__, CPL_ERROR_ILLEGAL_INPUT,
373 "Upper limit of the point-source sharpness must be " 374 "greater than the lower limit!");
390 muse_align_parse_valuelist(
const char *aValuelist)
392 cpl_ensure(aValuelist, CPL_ERROR_NULL_INPUT, NULL);
393 if (strlen(aValuelist) == 0) {
394 cpl_error_set_message(__func__, CPL_ERROR_DATA_NOT_FOUND,
395 "List of values is empty!");
400 cpl_size nvalues = cpl_array_get_size(strings);
402 cpl_array *values = cpl_array_new(nvalues, CPL_TYPE_DOUBLE);
404 for (ivalue = 0; ivalue < nvalues; ++ivalue) {
405 const char *svalue = cpl_array_get_string(strings, ivalue);
406 if ((strncasecmp(svalue,
"inf", 3) == 0) ||
407 (strncasecmp(svalue,
"nan", 3) == 0)) {
408 cpl_error_set_message(__func__, CPL_ERROR_ILLEGAL_INPUT,
409 "Illegal value \"%s\" encountered!", svalue);
410 cpl_array_delete(values);
411 cpl_array_delete(strings);
416 double value = strtod(svalue, &last);
417 if( (errno == ERANGE) || ((value == 0.) && (last == svalue))) {
418 cpl_error_set_message(__func__, CPL_ERROR_TYPE_MISMATCH,
419 "Could not convert \"%s\" to a decimal number!", svalue);
420 cpl_array_delete(values);
421 cpl_array_delete(strings);
424 cpl_array_set_double(values, ivalue, value);
427 cpl_array_delete(strings);
442 muse_align_celestial_from_pixel(cpl_table *aTable,
443 cpl_propertylist *aWCSHeader)
450 if (unit1 && unit2) {
451 if ((strncmp(unit1, unit2, 4) == 0) && (strncmp(unit1,
"deg", 3) == 0)) {
458 cpl_errorstate status = cpl_errorstate_get();
460 cpl_table_new_column(aTable,
"RA", CPL_TYPE_DOUBLE);
461 cpl_table_new_column(aTable,
"DEC", CPL_TYPE_DOUBLE);
464 for (irow = 0; irow < cpl_table_get_nrow(aTable); ++irow) {
465 double x = cpl_table_get_double(aTable,
"X", irow, NULL);
466 double y = cpl_table_get_double(aTable,
"Y", irow, NULL);
471 cpl_table_set_double(aTable,
"RA", irow, ra);
472 cpl_table_set_double(aTable,
"DEC", irow, dec);
476 if (!cpl_errorstate_is_equal(status)) {
485 #define USE_SVD_SOLVER 486 #ifdef USE_SVD_SOLVER 488 #if CPL_VERSION_CODE < 459008 489 #define cpl_matrix_solve_svd muse_cplmatrix_solve_svd 532 static cpl_error_code
533 muse_cplmatrix_decomp_sv_jacobi(cpl_matrix *U, cpl_vector *S, cpl_matrix *V)
535 cpl_ensure_code((U != NULL) && (V != NULL) && (S != NULL),
536 CPL_ERROR_NULL_INPUT);
537 cpl_ensure_code(cpl_matrix_get_nrow(U) >= cpl_matrix_get_ncol(U),
538 CPL_ERROR_ILLEGAL_INPUT);
539 cpl_ensure_code(cpl_matrix_get_nrow(V) == cpl_matrix_get_ncol(V),
540 CPL_ERROR_ILLEGAL_INPUT);
541 cpl_ensure_code(cpl_matrix_get_nrow(V) == cpl_matrix_get_ncol(U),
542 CPL_ERROR_INCOMPATIBLE_INPUT);
543 cpl_ensure_code(cpl_vector_get_size(S) == cpl_matrix_get_ncol(U),
544 CPL_ERROR_INCOMPATIBLE_INPUT);
546 const cpl_size m = cpl_matrix_get_nrow(U);
547 const cpl_size n = cpl_matrix_get_ncol(U);
553 cpl_size sweepmax = CPL_MAX(5 * n, 12);
555 double tolerance = 10. * m * DBL_EPSILON;
558 cpl_matrix_fill(V, 0.);
559 cpl_matrix_fill_diagonal(V, 1., 0);
566 cpl_vector *cv1 = cpl_vector_new(m);
567 double *
const _cv1 = cpl_vector_get_data(cv1);
569 for (j = 0; j < n; ++j) {
570 for (i = 0; i < m; ++i) {
571 _cv1[i] = cpl_matrix_get(U, i, j);
574 double s = sqrt(cpl_vector_product(cv1, cv1));
575 cpl_vector_set(S, j, DBL_EPSILON * s);
579 cpl_vector *cv2 = cpl_vector_new(m);
580 double *
const _cv2 = cpl_vector_get_data(cv2);
582 while ((count > 0) && (sweep <= sweepmax)) {
585 count = n * (n - 1) / 2;
587 for (j = 0; j < n - 1; ++j) {
589 for (k = j + 1; k < n; ++k) {
590 for (i = 0; i < m; ++i) {
591 _cv1[i] = cpl_matrix_get(U, i, j);
593 for (i = 0; i < m; ++i) {
594 _cv2[i] = cpl_matrix_get(U, i, k);
599 double a = sqrt(cpl_vector_product(cv1, cv1));
600 double b = sqrt(cpl_vector_product(cv2, cv2));
601 double c = 2. * cpl_vector_product(cv1, cv2);
604 double abserr_a = cpl_vector_get(S, j);
605 double abserr_b = cpl_vector_get(S, k);
606 cpl_boolean orthogonal = (fabs(c) <= tolerance * a * b);
607 cpl_boolean sorted = (a >= b);
608 cpl_boolean noisy_a = (a < abserr_a);
609 cpl_boolean noisy_b = (b < abserr_b);
611 if (sorted && (orthogonal || noisy_a || noisy_b)) {
618 double q = a * a - b * b;
619 double v = sqrt(c * c + q * q);
623 if (v == 0. || !sorted) {
627 cs = sqrt((v + q) / (2. * v));
628 sn = c / (2. * v * cs);
632 for (i = 0; i < m; ++i) {
633 const double u_ij = cpl_matrix_get(U, i, j);
634 const double u_ik = cpl_matrix_get(U, i, k);
635 cpl_matrix_set(U, i, j, u_ij * cs + u_ik * sn);
636 cpl_matrix_set(U, i, k, -u_ij * sn + u_ik * cs);
639 for (i = 0; i < n; ++i) {
640 const double v_ij = cpl_matrix_get(V, i, j);
641 const double v_ik = cpl_matrix_get(V, i, k);
642 cpl_matrix_set(V, i, j, v_ij * cs + v_ik * sn);
643 cpl_matrix_set(V, i, k, -v_ij * sn + v_ik * cs);
646 cpl_vector_set(S, j, fabs(cs) * abserr_a +
647 fabs(sn) * abserr_b);
648 cpl_vector_set(S, k, fabs(sn) * abserr_a +
649 fabs(cs) * abserr_b);
655 cpl_vector_delete(cv2);
658 double prev_norm = -1.;
659 for (j = 0; j < n; ++j) {
660 for (i = 0; i < m; ++i) {
661 _cv1[i] = cpl_matrix_get(U, i, j);
664 double norm = sqrt(cpl_vector_product(cv1, cv1));
669 if ((norm == 0.) || (prev_norm == 0.) ||
670 ((j > 0) && (norm <= tolerance * prev_norm))) {
671 cpl_vector_set(S, j, 0.);
672 cpl_matrix_fill_column(U, 0., j);
675 cpl_vector_set(S, j, norm);
678 for (i = 0; i < m; ++i) {
679 cpl_matrix_set(U, i, j, _cv1[i] / norm);
684 cpl_vector_delete(cv1);
687 return CPL_ERROR_CONTINUE;
689 return CPL_ERROR_NONE;
703 muse_cplmatrix_solve_svd(
const cpl_matrix *coeff,
const cpl_matrix *rhs)
705 cpl_ensure((coeff != NULL) && (rhs != NULL), CPL_ERROR_NULL_INPUT, NULL);
706 cpl_ensure(cpl_matrix_get_nrow(coeff) == cpl_matrix_get_nrow(rhs),
707 CPL_ERROR_INCOMPATIBLE_INPUT, NULL);
708 cpl_ensure(cpl_matrix_get_ncol(rhs) == 1, CPL_ERROR_ILLEGAL_INPUT, NULL);
711 cpl_matrix *U = cpl_matrix_duplicate(coeff);
712 cpl_size nc = cpl_matrix_get_ncol(U);
714 cpl_matrix *V = cpl_matrix_new(nc, nc);
715 cpl_vector *S = cpl_vector_new(nc);
717 cpl_error_code status = muse_cplmatrix_decomp_sv_jacobi(U, S, V);
718 if (status != CPL_ERROR_NONE) {
719 cpl_vector_delete(S);
720 cpl_matrix_delete(V);
721 cpl_matrix_delete(U);
722 cpl_error_set_where(cpl_func);
727 cpl_matrix *Ut = cpl_matrix_transpose_create(U);
728 cpl_matrix *w = cpl_matrix_product_create(Ut, rhs);
729 cpl_matrix_delete(Ut);
731 const double *_s = cpl_vector_get_data_const(S);
732 double *_w = cpl_matrix_get_data(w);
735 for (ic = 0; ic < nc; ++ic) {
743 cpl_matrix *solution = cpl_matrix_product_create(V, w);
744 cpl_matrix_delete(w);
745 cpl_vector_delete(S);
746 cpl_matrix_delete(V);
747 cpl_matrix_delete(U);
766 muse_cplmatrix_solve_least_square(
const cpl_matrix *aMatrix1,
767 const cpl_matrix *aMatrix2)
769 cpl_ensure(aMatrix1 && aMatrix2, CPL_ERROR_NULL_INPUT, NULL);
771 cpl_size nc = cpl_matrix_get_ncol(aMatrix1);
772 cpl_size nr = cpl_matrix_get_nrow(aMatrix1);
773 cpl_ensure(cpl_matrix_get_nrow(aMatrix2) == nr,
774 CPL_ERROR_INCOMPATIBLE_INPUT, NULL);
776 cpl_matrix *result = NULL;
777 cpl_errorstate status = cpl_errorstate_get();
779 #ifndef USE_SVD_SOLVER 781 result = cpl_matrix_solve(aMatrix1, aMatrix2);
785 result = cpl_matrix_solve_normal(aMatrix1, aMatrix2);
787 cpl_matrix *mt = cpl_matrix_transpose_create(aMatrix1);
788 cpl_matrix *mgram = cpl_matrix_product_create(aMatrix1, mt);
789 cpl_matrix *mw = cpl_matrix_solve(mgram, aMatrix2);
791 result = cpl_matrix_product_create(mt, mw);
792 cpl_matrix_delete(mw);
793 cpl_matrix_delete(mgram);
794 cpl_matrix_delete(mt);
799 result = cpl_matrix_solve_svd(aMatrix1, aMatrix2);
801 cpl_matrix *mt = cpl_matrix_transpose_create(aMatrix1);
802 cpl_matrix *mgram = cpl_matrix_product_create(aMatrix1, mt);
803 cpl_matrix *mw = cpl_matrix_solve(mgram, aMatrix2);
805 result = cpl_matrix_product_create(mt, mw);
806 cpl_matrix_delete(mw);
807 cpl_matrix_delete(mgram);
808 cpl_matrix_delete(mt);
812 if (status != cpl_errorstate_get()) {
813 cpl_matrix_delete(result);
830 static cpl_error_code
831 muse_cplmatrix_cosine(cpl_matrix *aMatrix)
833 cpl_ensure(aMatrix, CPL_ERROR_NULL_INPUT, CPL_ERROR_NULL_INPUT);
835 cpl_size size = cpl_matrix_get_nrow(aMatrix) * cpl_matrix_get_ncol(aMatrix);
836 double *mdata = cpl_matrix_get_data(aMatrix);
839 for (i = 0; i < size; ++i) {
840 mdata[i] = cos(mdata[i]);
842 return CPL_ERROR_NONE;
862 static cpl_error_code
863 muse_align_estimate_background(
muse_image *aImage,
864 double aMinFraction,
double aFraction,
865 double *bkgValue,
double *bkgError)
868 cpl_ensure(aImage, CPL_ERROR_NULL_INPUT, CPL_ERROR_NULL_INPUT);
870 CPL_ERROR_ILLEGAL_INPUT, CPL_ERROR_ILLEGAL_INPUT);
874 image->
data = cpl_image_duplicate(aImage->
data);
875 image->
header = cpl_propertylist_duplicate(aImage->
header);
881 cpl_image_reject_value(image->
data, CPL_VALUE_NOTFINITE);
882 image->
dq = cpl_image_new(cpl_image_get_size_x(image->
data),
883 cpl_image_get_size_y(image->
data),
886 "ESO QC EXPALIGN BKG");
888 cpl_error_set_message(__func__, CPL_ERROR_ILLEGAL_OUTPUT,
889 "Creating background mask failed!");
890 return CPL_ERROR_ILLEGAL_OUTPUT;
893 cpl_mask_not(mask->
mask);
894 cpl_image_reject_from_mask(image->
data, mask->
mask);
897 double bkg_sigma = 0.;
898 double bkg = cpl_image_get_mad(image->
data, &bkg_sigma);
903 *bkgError = bkg_sigma;
905 return CPL_ERROR_NONE;
926 muse_align_compute_distances(
const cpl_matrix *aDeltaRA,
const cpl_matrix *aDeltaDEC,
927 const cpl_matrix *aMeanDEC,
const double *aOffsetRA,
928 const double *aOffsetDEC)
931 cpl_errorstate status = cpl_errorstate_get();
938 cpl_matrix *tmp = cpl_matrix_duplicate(aDeltaDEC);
939 cpl_matrix_subtract_scalar(tmp, *aOffsetDEC);
942 cpl_matrix_delete(tmp);
947 cpl_matrix *dec = cpl_matrix_duplicate(aMeanDEC);
948 muse_cplmatrix_cosine(dec);
952 dRA = cpl_matrix_duplicate(aDeltaRA);
953 cpl_matrix_subtract_scalar(dRA, *aOffsetRA);
954 cpl_matrix_multiply(dRA, dec);
958 cpl_matrix_delete(dec);
961 cpl_matrix_delete(dRA);
963 cpl_matrix_add(distance, dDEC);
964 cpl_matrix_power(distance, 0.5);
965 cpl_matrix_delete(dDEC);
967 if (status != cpl_errorstate_get()) {
968 cpl_matrix_delete(distance);
997 muse_align_estimate_offsets(
double *aOffsetRA,
double *aOffsetDEC,
double *aWeight,
998 const cpl_matrix *aDeltaRA,
const cpl_matrix *aDeltaDEC,
999 const cpl_matrix *aMeanDEC,
double aRadius,
int aNbins)
1001 cpl_ensure((aOffsetRA && aOffsetDEC) && aWeight, CPL_ERROR_NULL_INPUT, 0);
1002 cpl_ensure((aDeltaRA && aDeltaDEC) && aMeanDEC, CPL_ERROR_NULL_INPUT, 0);
1003 cpl_ensure(cpl_matrix_get_nrow(aDeltaRA) == cpl_matrix_get_nrow(aDeltaDEC),
1004 CPL_ERROR_INCOMPATIBLE_INPUT, 0);
1005 cpl_ensure(cpl_matrix_get_nrow(aDeltaRA) == cpl_matrix_get_nrow(aMeanDEC),
1006 CPL_ERROR_INCOMPATIBLE_INPUT, 0);
1007 cpl_ensure(aRadius > 0., CPL_ERROR_ILLEGAL_INPUT, 0);
1008 cpl_ensure(aNbins > 0, CPL_ERROR_ILLEGAL_INPUT, 0);
1010 cpl_matrix *distance = muse_align_compute_distances(aDeltaRA, aDeltaDEC,
1011 aMeanDEC, NULL, NULL);
1012 if (cpl_matrix_get_min(distance) >= aRadius) {
1013 cpl_matrix_delete(distance);
1021 _muse_condition_less_than);
1022 cpl_matrix_delete(distance);
1025 cpl_array_delete(selection);
1028 double bstep = 2. * aRadius / (double)aNbins;
1041 cpl_matrix *bins = cpl_matrix_new(1, aNbins);
1043 for (ibin = 0; ibin < aNbins; ++ibin) {
1044 cpl_matrix_set(bins, 0, ibin, -aRadius + ibin * bstep);
1046 cpl_matrix *histogram = cpl_matrix_new(aNbins, aNbins);
1047 double *hdata = cpl_matrix_get_data(histogram);
1048 cpl_size npos = cpl_matrix_get_ncol(dRA);
1049 cpl_size nmatch = 0;
1051 for (ipos = 0; ipos < npos; ++ipos) {
1052 cpl_size ix = (cpl_size)((cpl_matrix_get(dRA, 0, ipos) + aRadius) / bstep);
1053 cpl_size iy = (cpl_size)((cpl_matrix_get(dDEC, 0, ipos) + aRadius) / bstep);
1054 if (((ix >= 0) && (ix < aNbins)) && ((iy >= 0) && (iy < aNbins))) {
1055 hdata[iy * aNbins + ix] += 1.;
1059 cpl_matrix_delete(dRA);
1060 cpl_matrix_delete(dDEC);
1063 cpl_matrix_delete(histogram);
1064 cpl_matrix_delete(bins);
1070 cpl_matrix_get_maxpos(histogram, &row, &column);
1072 *aOffsetRA = cpl_matrix_get(bins, 0, column) + 0.5 * bstep;
1073 *aOffsetDEC = cpl_matrix_get(bins, 0, row) + 0.5 * bstep;
1074 *aWeight = cpl_matrix_get_max(histogram);
1075 cpl_matrix_delete(histogram);
1076 cpl_matrix_delete(bins);
1103 muse_align_update_offsets(
double *aOffsetRA,
double *aOffsetDEC,
double *aWeight,
1104 const cpl_matrix *aDeltaRA,
const cpl_matrix *aDeltaDEC,
1105 const cpl_matrix *aMeanDEC,
double aRadius)
1107 cpl_ensure((aOffsetRA && aOffsetDEC) && aWeight, CPL_ERROR_NULL_INPUT,
1109 cpl_ensure((aDeltaRA && aDeltaDEC) && aMeanDEC, CPL_ERROR_NULL_INPUT,
1111 cpl_ensure(cpl_matrix_get_nrow(aDeltaRA) == cpl_matrix_get_nrow(aDeltaDEC),
1112 CPL_ERROR_INCOMPATIBLE_INPUT, CPL_FALSE);
1113 cpl_ensure(cpl_matrix_get_nrow(aDeltaRA) == cpl_matrix_get_nrow(aMeanDEC),
1114 CPL_ERROR_INCOMPATIBLE_INPUT, CPL_FALSE);
1115 cpl_ensure(aRadius > 0., CPL_ERROR_ILLEGAL_INPUT, CPL_FALSE);
1117 cpl_matrix *distance = muse_align_compute_distances(aDeltaRA, aDeltaDEC,
1118 aMeanDEC, aOffsetRA, aOffsetDEC);
1119 if (cpl_matrix_get_min(distance) >= aRadius) {
1120 cpl_matrix_delete(distance);
1128 _muse_condition_less_than);
1129 cpl_matrix_delete(distance);
1132 cpl_array_delete(selection);
1134 cpl_size nselected = cpl_matrix_get_ncol(dRA);
1135 double variance = 2. * aRadius * aRadius;
1136 if (nselected > 1) {
1137 double sdevRA = cpl_matrix_get_stdev(dRA);
1138 double sdevDEC = cpl_matrix_get_stdev(dDEC);
1139 double _variance = sdevRA * sdevRA + sdevDEC * sdevDEC;
1140 if (_variance > nselected / DBL_MAX) {
1141 variance = _variance;
1145 *aOffsetRA = cpl_matrix_get_median(dRA);
1146 *aOffsetDEC = cpl_matrix_get_median(dDEC);
1147 *aWeight = nselected / variance;
1149 cpl_matrix_delete(dRA);
1150 cpl_matrix_delete(dDEC);
1175 const cpl_array *aFieldId,
1176 const cpl_array *aSearchradius,
1177 int aNbins, cpl_boolean aWeight,
1178 cpl_propertylist *aHeader)
1180 cpl_ensure(aImagelist && aCataloglist && aFieldId,
1181 CPL_ERROR_NULL_INPUT, NULL);
1182 cpl_ensure(aSearchradius, CPL_ERROR_NULL_INPUT, NULL);
1184 CPL_ERROR_ILLEGAL_INPUT, NULL);
1185 cpl_ensure(aNbins > 1, CPL_ERROR_ILLEGAL_INPUT, NULL);
1188 cpl_ensure(nfields >= 2, CPL_ERROR_DATA_NOT_FOUND, NULL);
1192 cpl_size npairs = nfields * (nfields - 1) / 2;
1194 muse_indexpair *combinations = cpl_calloc(npairs,
sizeof *combinations);
1197 for (ifield = 0; ifield < nfields; ++ifield) {
1199 for (jfield = ifield + 1; jfield < nfields; ++jfield) {
1200 combinations[ipair].first = ifield;
1201 combinations[ipair].second = jfield;
1207 for (ifield = 0; ifield < nfields; ++ifield) {
1208 cpl_size kfield = cpl_array_get_cplsize(aFieldId, ifield, NULL);
1209 char *kw = cpl_sprintf(QC_ALIGN_NDETi, (
int)kfield + 1);
1210 cpl_propertylist_append_int(aHeader, kw,
1211 cpl_table_get_nrow(aCataloglist[ifield]->table));
1219 cpl_array **aoverlaps = cpl_calloc(nfields,
sizeof(cpl_array *));
1220 for (ifield = 0; ifield < nfields; ++ifield) {
1221 aoverlaps[ifield] = cpl_array_new(nfields, CPL_TYPE_INT);
1225 cpl_matrix *ra_offsets = NULL;
1226 cpl_matrix *dec_offsets = NULL;
1227 cpl_size *has_neighbors = cpl_malloc(nfields *
sizeof *has_neighbors);
1229 for (isearch = 0; isearch < cpl_array_get_size(aSearchradius); ++isearch) {
1230 double radius = cpl_array_get_double(aSearchradius, isearch, NULL);
1238 for (ifield = 0; ifield < nfields; ++ifield) {
1239 cpl_array_fill_window_int(aoverlaps[ifield], 0, nfields, 0);
1243 cpl_array_set_invalid(aoverlaps[ifield], ifield);
1245 cpl_array_fill_window_invalid(aoverlaps[ifield], 0, nfields);
1251 cpl_matrix *weights = cpl_matrix_new(npairs + 1, nfields);
1252 cpl_matrix *offsets_ra = cpl_matrix_new(npairs + 1, 1);
1253 cpl_matrix *offsets_dec = cpl_matrix_new(npairs + 1, 1);
1255 cpl_matrix_fill_row(weights, 1., 0);
1256 cpl_size kpairs = 0;
1258 memset(has_neighbors, 0, nfields *
sizeof *has_neighbors);
1260 for (ipair = 0; ipair < npairs; ++ipair) {
1262 const cpl_table *catalog1 = aCataloglist[combinations[ipair].first]->
table;
1263 const cpl_table *catalog2 = aCataloglist[combinations[ipair].second]->
table;
1265 cpl_size nstars1 = cpl_table_get_nrow(catalog1);
1266 cpl_size nstars2 = cpl_table_get_nrow(catalog2);
1268 const double *ra1 = cpl_table_get_data_double_const(catalog1,
"RA");
1269 const double *ra2 = cpl_table_get_data_double_const(catalog2,
"RA");
1270 const double *dec1 = cpl_table_get_data_double_const(catalog1,
"DEC");
1271 const double *dec2 = cpl_table_get_data_double_const(catalog2,
"DEC");
1273 cpl_matrix *delta_ra = cpl_matrix_new(nstars1, nstars2);
1274 cpl_matrix *delta_dec = cpl_matrix_new(nstars1, nstars2);
1275 cpl_matrix *dec_mean = cpl_matrix_new(nstars1, nstars2);
1278 for (irow = 0; irow < nstars1; ++irow) {
1280 for (icol = 0; icol < nstars2; ++icol) {
1281 cpl_matrix_set(delta_ra, irow, icol, (ra1[irow] - ra2[icol]) * deg2as);
1282 cpl_matrix_set(delta_dec, irow, icol, (dec1[irow] - dec2[icol]) * deg2as);
1283 cpl_matrix_set(dec_mean, irow, icol,
1284 0.5 *(dec1[irow] + dec2[icol]) * CPL_MATH_RAD_DEG);
1288 double offset_ra = 0.;
1289 double offset_dec = 0.;
1291 cpl_size noverlap = 0;
1293 noverlap = muse_align_estimate_offsets(&offset_ra, &offset_dec, &weight,
1294 delta_ra, delta_dec, dec_mean,
1297 offset_ra = cpl_matrix_get(ra_offsets, combinations[ipair].first, 0) -
1298 cpl_matrix_get(ra_offsets, combinations[ipair].second, 0);
1299 offset_dec = cpl_matrix_get(dec_offsets, combinations[ipair].first, 0) -
1300 cpl_matrix_get(dec_offsets, combinations[ipair].second, 0);
1302 noverlap = muse_align_update_offsets(&offset_ra, &offset_dec, &weight,
1303 delta_ra, delta_dec, dec_mean,
1306 cpl_matrix_delete(dec_mean);
1307 cpl_matrix_delete(delta_ra);
1308 cpl_matrix_delete(delta_dec);
1316 cpl_matrix_set(offsets_ra, kpairs, 0, weight * offset_ra);
1317 cpl_matrix_set(offsets_dec, kpairs, 0, weight * offset_dec);
1318 cpl_matrix_set(weights, kpairs, combinations[ipair].first, weight);
1319 cpl_matrix_set(weights, kpairs, combinations[ipair].second, -weight);
1320 has_neighbors[combinations[ipair].first] += 1;
1321 has_neighbors[combinations[ipair].second] += 1;
1324 cpl_array_set_int(aoverlaps[combinations[ipair].first],
1325 combinations[ipair].second, noverlap);
1326 cpl_array_set_int(aoverlaps[combinations[ipair].second],
1327 combinations[ipair].first, noverlap);
1332 cpl_matrix_delete(ra_offsets);
1333 cpl_matrix_delete(dec_offsets);
1338 cpl_matrix_delete(offsets_dec);
1339 cpl_matrix_delete(offsets_ra);
1340 cpl_matrix_delete(weights);
1342 cpl_msg_warning(__func__,
"No overlapping fields of view were found " 1343 "for search radius %.4f!", radius);
1345 ra_offsets = cpl_matrix_new(nfields, 1);
1346 dec_offsets = cpl_matrix_new(nfields, 1);
1350 cpl_matrix *_weights = cpl_matrix_wrap(kpairs + 1, nfields,
1351 cpl_matrix_get_data(weights));
1352 cpl_matrix *_offsets_ra = cpl_matrix_wrap(kpairs + 1, 1,
1353 cpl_matrix_get_data(offsets_ra));
1354 cpl_matrix *_offsets_dec = cpl_matrix_wrap(kpairs + 1, 1,
1355 cpl_matrix_get_data(offsets_dec));
1357 ra_offsets = muse_cplmatrix_solve_least_square(_weights, _offsets_ra);
1358 dec_offsets = muse_cplmatrix_solve_least_square(_weights, _offsets_dec);
1360 cpl_matrix_unwrap(_offsets_dec);
1361 cpl_matrix_unwrap(_offsets_ra);
1362 cpl_matrix_unwrap(_weights);
1364 cpl_matrix_delete(offsets_dec);
1365 cpl_matrix_delete(offsets_ra);
1366 cpl_matrix_delete(weights);
1369 cpl_free(combinations);
1378 cpl_size minmatch = LLONG_MAX,
1380 for (ifield = 0; ifield < nfields; ++ifield) {
1382 cpl_size field_id = cpl_array_get_cplsize(aFieldId, ifield, NULL);
1385 cpl_table_set_string(offsets, MUSE_OFFSETS_DATEOBS, ifield, timestamp);
1386 cpl_table_set_double(offsets, MUSE_OFFSETS_MJDOBS, ifield, mjd);
1388 if (has_neighbors[ifield]) {
1389 double ra_offset = cpl_matrix_get(ra_offsets, ifield, 0);
1390 double dec_offset = cpl_matrix_get(dec_offsets, ifield, 0);
1391 cpl_table_set_double(offsets, MUSE_OFFSETS_DRA, ifield, ra_offset / deg2as);
1392 cpl_table_set_double(offsets, MUSE_OFFSETS_DDEC, ifield, dec_offset / deg2as);
1394 int nmedian = cpl_array_get_median(aoverlaps[ifield]);
1395 char *kw = cpl_sprintf(QC_ALIGN_NMATCHi, (
int)field_id + 1);
1396 cpl_propertylist_append_int(aHeader, kw, nmedian);
1398 if (nmedian < minmatch) {
1402 cpl_table_set_double(offsets, MUSE_OFFSETS_DRA, ifield, 0.);
1403 cpl_table_set_double(offsets, MUSE_OFFSETS_DDEC, ifield, 0.);
1404 char *kw = cpl_sprintf(QC_ALIGN_NMATCHi, (
int)field_id + 1);
1405 cpl_propertylist_append_int(aHeader, kw, 0);
1410 cpl_propertylist_append_int(aHeader, QC_ALIGN_NMATCH_MIN, minmatch);
1411 cpl_propertylist_append_int(aHeader, QC_ALIGN_NOMATCH, nomatch);
1412 muse_vfree((
void **)aoverlaps, nfields, (muse_free_func)cpl_array_delete);
1413 cpl_free(has_neighbors);
1414 cpl_matrix_delete(dec_offsets);
1415 cpl_matrix_delete(ra_offsets);
1420 double ra_shift = cpl_table_get(offsets, MUSE_OFFSETS_DRA, 0, NULL);
1421 double dec_shift = cpl_table_get(offsets, MUSE_OFFSETS_DDEC, 0, NULL);
1422 cpl_table_subtract_scalar(offsets, MUSE_OFFSETS_DRA, ra_shift);
1423 cpl_table_subtract_scalar(offsets, MUSE_OFFSETS_DDEC, dec_shift);
1424 cpl_table_set_column_invalid(offsets, MUSE_OFFSETS_FSCALE, 0, nfields);
1427 double qcmin = cpl_table_get_column_min(offsets, MUSE_OFFSETS_DRA) * 3600.;
1428 double qcmax = cpl_table_get_column_max(offsets, MUSE_OFFSETS_DRA) * 3600.;
1429 double qcmean = cpl_table_get_column_mean(offsets, MUSE_OFFSETS_DRA) * 3600.;
1430 double qcsdev = cpl_table_get_column_stdev(offsets, MUSE_OFFSETS_DRA) * 3600.;
1432 cpl_propertylist_append_float(aHeader, QC_ALIGN_DRA_MIN, qcmin);
1433 cpl_propertylist_append_float(aHeader, QC_ALIGN_DRA_MAX, qcmax);
1434 cpl_propertylist_append_float(aHeader, QC_ALIGN_DRA_MEAN, qcmean);
1435 cpl_propertylist_append_float(aHeader, QC_ALIGN_DRA_STDEV, qcsdev);
1437 qcmin = cpl_table_get_column_min(offsets, MUSE_OFFSETS_DDEC) * 3600.;
1438 qcmax = cpl_table_get_column_max(offsets, MUSE_OFFSETS_DDEC) * 3600.;
1439 qcmean = cpl_table_get_column_mean(offsets, MUSE_OFFSETS_DDEC) * 3600.;
1440 qcsdev = cpl_table_get_column_stdev(offsets, MUSE_OFFSETS_DDEC) * 3600.;
1442 cpl_propertylist_append_float(aHeader, QC_ALIGN_DDEC_MIN, qcmin);
1443 cpl_propertylist_append_float(aHeader, QC_ALIGN_DDEC_MAX, qcmax);
1444 cpl_propertylist_append_float(aHeader, QC_ALIGN_DDEC_MEAN, qcmean);
1445 cpl_propertylist_append_float(aHeader, QC_ALIGN_DDEC_STDEV, qcsdev);
1465 cpl_size nexposures = cpl_frameset_count_tags(aProcessing->
inframes,
1466 MUSE_TAG_IMAGE_FOV);
1467 if (nexposures < 2) {
1468 cpl_msg_error(__func__,
"This recipe requires at least 2 input FOV " 1474 cpl_msg_debug(__func__,
"Validating source detection parameters");
1475 if (!muse_align_check_detection_params(aParams)) {
1476 cpl_error_set_where(__func__);
1481 cpl_msg_info(__func__,
"Loading FOV images");
1483 muse_imagelist *fovimages = muse_processing_fov_load_all(aProcessing);
1485 cpl_msg_error(__func__,
"Could not load FOV input images!");
1491 for (iexposure = 0; iexposure < nexposures; ++iexposure) {
1493 cpl_errorstate es = cpl_errorstate_get();
1496 if (!cpl_errorstate_is_equal(es) &&
1497 cpl_error_get_code() == CPL_ERROR_DATA_NOT_FOUND) {
1498 cpl_msg_error(__func__,
"Missing \"MJD-OBS\" in FOV image %" 1499 CPL_SIZE_FORMAT, iexposure + 1);
1504 for (jexposure = iexposure + 1; jexposure < nexposures; ++jexposure) {
1507 if (_mjdobs == mjdobs) {
1508 cpl_msg_warning(__func__,
"Found FOV images with non-unique " 1509 "\"MJD-OBS\" value (image %" CPL_SIZE_FORMAT
1510 " and %" CPL_SIZE_FORMAT
")!", iexposure + 1,
1517 cpl_msg_info(__func__,
"Computing pointing corrections for %" CPL_SIZE_FORMAT
1518 " FOV images", nexposures);
1522 for (iexposure = 0; iexposure < nexposures; ++iexposure) {
1524 cpl_image_reject_value(image->
data, CPL_VALUE_NOTFINITE);
1525 muse_utils_replace_nan(image, 0.);
1535 muse_table **srclists = cpl_calloc(nexposures,
sizeof *srclists);
1537 for (iexposure = 0; iexposure < nexposures; ++iexposure) {
1538 cpl_msg_debug(__func__,
"Detecting sources on image %" CPL_SIZE_FORMAT
1539 " of %" CPL_SIZE_FORMAT, iexposure + 1, nexposures);
1546 double bkg_sigma = 0.;
1550 muse_align_estimate_background(fov, aParams->
bkgignore,
1553 cpl_msg_info(__func__,
"Median background level computed from sky mask: " 1554 "%.6f +/- %.6f (MAD)", bkg, bkg_sigma);
1557 threshold = bkg + fabs(threshold) * bkg_sigma;
1560 cpl_boolean iterate = CPL_TRUE;
1562 cpl_size ndetections = 0;
1563 cpl_table *detections;
1564 cpl_image *image = cpl_image_cast(fov->
data, CPL_TYPE_DOUBLE);
1572 cpl_size nx = cpl_image_get_size_x(image);
1573 cpl_size ny = cpl_image_get_size_y(image);
1575 if ((nx < 6) || (ny < 6)) {
1576 cpl_image_delete(image);
1579 cpl_msg_error(__func__,
"Image %" CPL_SIZE_FORMAT
" of %" 1580 CPL_SIZE_FORMAT
"is too small!", iexposure + 1, nexposures);
1584 if ((nx % 2 != 0) || (ny % 2 != 0)) {
1585 cpl_msg_debug(__func__,
"Trimming image %" CPL_SIZE_FORMAT
" to an " 1586 "even number of pixels along the axes.", iexposure + 1);
1593 cpl_image *_image = cpl_image_extract(image, 1, 1, nx, ny);
1595 cpl_image_delete(image);
1598 cpl_msg_error(__func__,
"Trimming image %" CPL_SIZE_FORMAT
" failed",
1602 cpl_image_delete(image);
1607 cpl_errorstate status = cpl_errorstate_get();
1609 cpl_msg_info(__func__,
"Searching for sources using pixel value %.6f " 1610 "as detection threshold", threshold);
1613 roundness, sharpness);
1616 if (cpl_error_get_code() == CPL_ERROR_DATA_NOT_FOUND) {
1617 cpl_errorstate_set(status);
1620 cpl_image_delete(image);
1623 cpl_msg_error(__func__,
"Source detection failed on image %" 1624 CPL_SIZE_FORMAT
" of %" CPL_SIZE_FORMAT, iexposure + 1,
1629 ndetections = cpl_table_get_nrow(detections);
1632 if ((ndetections > 0) &&
1633 ((ndetections >= nobjects[0]) && (ndetections <= nobjects[1]))) {
1634 iterate = CPL_FALSE;
1636 cpl_table_delete(detections);
1638 if (++iteration < aParams->iterations) {
1639 if (ndetections > nobjects[1]) {
1640 threshold += aParams->
step;
1642 threshold -= aParams->
step;
1643 if (threshold <= 0.) {
1644 iterate = CPL_FALSE;
1648 iterate = CPL_FALSE;
1652 cpl_image_delete(image);
1655 if (ndetections == 0) {
1656 cpl_msg_warning(__func__,
"No point-sources were detected in image %" 1657 CPL_SIZE_FORMAT, iexposure + 1);
1658 }
else if (ndetections < nobjects[0]) {
1659 cpl_msg_warning(__func__,
"Too few (%" CPL_SIZE_FORMAT
") point-sources " 1660 "were detected in image %" CPL_SIZE_FORMAT, ndetections,
1662 }
else if (ndetections > nobjects[1]) {
1663 cpl_msg_warning(__func__,
"Too many (%" CPL_SIZE_FORMAT
") point-sources " 1664 "were detected in image %" CPL_SIZE_FORMAT, ndetections,
1667 cpl_msg_warning(__func__,
"Invalid number (%" CPL_SIZE_FORMAT
") of " 1668 "point-sources were detected in image %" CPL_SIZE_FORMAT,
1669 ndetections, iexposure + 1);
1673 cpl_msg_info(__func__,
"%" CPL_SIZE_FORMAT
" point-sources detected in " 1674 "image %" CPL_SIZE_FORMAT, ndetections, iexposure + 1);
1678 if (muse_align_celestial_from_pixel(detections, fov->
header)) {
1681 cpl_msg_error(__func__,
"Computing WCS coordinates from pixel positions " 1682 "failed for image %" CPL_SIZE_FORMAT
" of %" 1683 CPL_SIZE_FORMAT, iexposure + 1, nexposures);
1687 cpl_size nsources = cpl_table_get_nrow(detections);
1691 for (isource = 0; isource < nsources; ++isource) {
1695 cpl_table_set_int(srclist, MUSE_SRCLIST_ID, isource, isource + 1);
1697 value = cpl_table_get_double(detections, MUSE_SRCLIST_X,
1699 cpl_table_set_double(srclist, MUSE_SRCLIST_X, isource, value);
1700 value = cpl_table_get_double(detections, MUSE_SRCLIST_Y,
1702 cpl_table_set_double(srclist, MUSE_SRCLIST_Y, isource, value);
1703 value = cpl_table_get_double(detections, MUSE_SRCLIST_FLUX,
1705 cpl_table_set_double(srclist, MUSE_SRCLIST_FLUX, isource, value);
1706 value = cpl_table_get_double(detections, MUSE_SRCLIST_SHARPNESS,
1708 cpl_table_set_double(srclist, MUSE_SRCLIST_SHARPNESS, isource, value);
1709 value = cpl_table_get_double(detections, MUSE_SRCLIST_ROUNDNESS,
1711 cpl_table_set_double(srclist, MUSE_SRCLIST_ROUNDNESS, isource, value);
1712 value = cpl_table_get_double(detections, MUSE_SRCLIST_RA,
1714 cpl_table_set_double(srclist, MUSE_SRCLIST_RA, isource, value);
1715 cpl_table_set_double(srclist, MUSE_SRCLIST_RACORR, isource, value);
1716 value = cpl_table_get_double(detections, MUSE_SRCLIST_DEC,
1718 cpl_table_set_double(srclist, MUSE_SRCLIST_DEC, isource, value);
1719 cpl_table_set_double(srclist, MUSE_SRCLIST_DECCORR, isource, value);
1723 srclists[iexposure]->
table = srclist;
1724 cpl_table_delete(detections);
1727 srclists[iexposure]->
header = cpl_propertylist_duplicate(fov->
header);
1729 cpl_propertylist_erase_regexp(srclists[iexposure]->header,
"^ESO QC.*",
1732 cpl_propertylist_update_int(srclists[iexposure]->header,
1733 QC_ALIGN_NDET, ndetections);
1734 cpl_propertylist_update_float(srclists[iexposure]->header,
1735 QC_ALIGN_BKG_MEDIAN, bkg);
1736 cpl_propertylist_update_float(srclists[iexposure]->header,
1737 QC_ALIGN_BKG_MAD, bkg_sigma);
1738 cpl_propertylist_update_float(srclists[iexposure]->header,
1739 QC_ALIGN_THRESHOLD, threshold);
1745 cpl_array *radii = muse_align_parse_valuelist(aParams->
rsearch);
1749 cpl_msg_error(__func__,
"Cannot compute field offsets! No valid search " 1750 "radius was given!");
1764 muse_table **_srclists = cpl_calloc(nexposures,
sizeof *_srclists);
1765 cpl_array *field_id = cpl_array_new(nexposures, CPL_TYPE_SIZE);
1766 cpl_propertylist *header = cpl_propertylist_new();
1768 cpl_size kexposures = 0;
1769 for (iexposure = 0; iexposure < nexposures; ++iexposure) {
1770 if (cpl_table_get_nrow(srclists[iexposure]->table) > 0) {
1773 _srclists[kexposures] = srclists[iexposure];
1774 cpl_array_set(field_id, kexposures, iexposure);
1777 char *kw = cpl_sprintf(QC_ALIGN_NDETi, (
int)iexposure + 1);
1778 cpl_propertylist_append_int(header, kw, 0);
1781 kw = cpl_sprintf(QC_ALIGN_NMATCHi, (
int)iexposure + 1);
1782 cpl_propertylist_append_int(header, kw, 0);
1785 cpl_msg_warning(__func__,
"No sources are available for image % " 1786 CPL_SIZE_FORMAT
". This field will be ignored and no " 1787 "coordinate offsets will be calculated!", iexposure + 1);
1790 _srclists = cpl_realloc(_srclists, kexposures *
sizeof *_srclists);
1793 if (kexposures < 2) {
1794 cpl_propertylist_delete(header);
1795 cpl_array_delete(field_id);
1800 cpl_msg_error(__func__,
"Cannot compute field offsets! Source positions " 1801 "are only available for %" CPL_SIZE_FORMAT
" out of %" 1802 CPL_SIZE_FORMAT
" fields!", kexposures, nexposures);
1806 cpl_msg_info(__func__,
"Calculating field offsets...");
1807 cpl_table *offsets = muse_align_compute_field_offsets(_fovimages, _srclists,
1817 int nomatch = cpl_propertylist_get_int(header, QC_ALIGN_NOMATCH) +
1818 nexposures - kexposures;
1819 cpl_propertylist_update_int(header, QC_ALIGN_NOMATCH, nomatch);
1827 cpl_free(_srclists);
1835 cpl_array_delete(radii);
1837 cpl_propertylist_delete(header);
1838 cpl_array_delete(field_id);
1841 cpl_msg_error(__func__,
"Could not compute FOV offsets for %" 1842 CPL_SIZE_FORMAT
" images", nexposures);
1848 for (iexposure = 0; iexposure < cpl_array_get_size(field_id); ++iexposure) {
1849 int invalid[2] = {0, 0};
1850 double shift_ra = cpl_table_get_double(offsets, MUSE_OFFSETS_DRA,
1851 iexposure, &invalid[0]);
1852 double shift_dec = cpl_table_get_double(offsets, MUSE_OFFSETS_DDEC,
1853 iexposure, &invalid[1]);
1855 cpl_size ifield = cpl_array_get(field_id, iexposure, NULL);
1857 if (invalid[0] || invalid[1]) {
1858 cpl_msg_warning(__func__,
"Invalid field offsets found for field %" 1859 CPL_SIZE_FORMAT
". No Correction will not be applied to " 1860 "the position of the detected sources.", ifield);
1863 cpl_table_subtract_scalar(srclists[ifield]->table, MUSE_SRCLIST_RACORR,
1865 cpl_table_subtract_scalar(srclists[ifield]->table, MUSE_SRCLIST_DECCORR,
1870 cpl_array_delete(field_id);
1874 header, MUSE_TAG_OFFSET_LIST,
1876 cpl_propertylist_delete(header);
1877 cpl_table_delete(offsets);
1882 for (iexposure = 0; iexposure < nexposures; ++iexposure) {
1884 srclists[iexposure]->header,
1891 if (rc != CPL_ERROR_NONE) {
cpl_error_code muse_cplarray_erase_invalid(cpl_array *aArray)
Erase all invalid values from an array.
int srcmin
Minimum number of sources which must be found.
Structure definition for a collection of muse_images.
void muse_image_delete(muse_image *aImage)
Deallocate memory associated to a muse_image object.
const char * muse_pfits_get_extname(const cpl_propertylist *aHeaders)
find out the extension name
double fwhm
FWHM in pixels of the convolution filter.
A structure containing a spatial two-axis WCS.
cpl_size muse_pfits_get_naxis(const cpl_propertylist *aHeaders, unsigned int aAxis)
find out the size of a given axis
cpl_image * data
the data extension
void muse_mask_delete(muse_mask *aMask)
Deallocate memory associated to a muse_mask object.
double roundmax
Upper limit of the allowed point-source roundness.
cpl_image * stat
the statistics extension
const char * muse_pfits_get_dateobs(const cpl_propertylist *aHeaders)
find out the date of observations
void muse_imagelist_delete(muse_imagelist *aList)
Free the memory of the MUSE image list.
muse_image * muse_fov_load(const char *aFilename)
Load a FOV image into a MUSE image.
const char * muse_pfits_get_bunit(const cpl_propertylist *aHeaders)
find out the unit string
muse_table * muse_table_new(void)
Allocate memory for a new muse_table object.
Structure definition of MUSE three extension FITS file.
const char * muse_pfits_get_cunit(const cpl_propertylist *aHeaders, unsigned int aAxis)
find out the WCS axis unit string
cpl_propertylist * header
the FITS header
cpl_boolean muse_processing_check_intags(muse_processing *aProcessing, const char *aTag, int aNChars)
Check that a tag is part of the input tags of a processing structure.
double step
Increment/decrement of the threshold value in subsequent iterations.
unsigned int muse_imagelist_get_size(muse_imagelist *aList)
Return the number of stored images.
cpl_matrix * muse_cplmatrix_extract_selected(const cpl_matrix *aMatrix, const cpl_array *aIndices)
Extract the elements given by the index array.
cpl_image * dq
the data quality extension
cpl_table * muse_cpltable_new(const muse_cpltable_def *aDef, cpl_size aLength)
Create an empty table according to the specified definition.
double bkgfraction
Fraction of the image (without the ignored part) to be considered as background.
muse_wcs * muse_wcs_new(cpl_propertylist *aHeader)
Create a new WCS structure from a given FITS header.
cpl_array * muse_cplarray_new_from_delimited_string(const char *aString, const char *aDelim)
Convert a delimited string into an array of strings.
double sharpmax
Upper limit of the allowed point-source sharpness.
Structure to hold the parameters of the muse_exp_align recipe.
int srcmax
Maximum number of sources which may be found.
#define MUSE_WCS_KEYS
regular expression for WCS properties
muse_image * muse_imagelist_get(muse_imagelist *aList, unsigned int aIdx)
Get the muse_image of given list index.
double threshold
Initial intensity threshold for detecting point sources. If the value is negative or zero the thresho...
static void muse_wcs_celestial_from_pixel_fast(muse_wcs *aWCS, double aX, double aY, double *aRA, double *aDEC)
Convert from pixel coordinates to celestial spherical coordinates.
int nbins
Number of bins to use for 2D histogram on the first iteration of the offset computation.
cpl_table * table
The table.
double bkgignore
Fraction of the image to be ignored.
void muse_processing_append_used(muse_processing *aProcessing, cpl_frame *aFrame, cpl_frame_group aGroup, int aDuplicate)
Add a frame to the set of used frames.
Structure to store a table together with a property list.
void muse_table_delete(muse_table *aTable)
Deallocate memory associated to a muse_table object.
cpl_propertylist * header
the header
const char * rsearch
Search radius (in arcsec) for each iteration of the offset computation.
double muse_pfits_get_mjdobs(const cpl_propertylist *aHeaders)
find out the Julian Date of the observation
Handling of "mask" files.
double roundmin
Lower limit of the allowed point-source roundness.
muse_imagelist * muse_imagelist_new(void)
Create a new (empty) MUSE image list.
cpl_matrix * muse_cplmatrix_multiply_create(const cpl_matrix *aMatrix1, const cpl_matrix *aMatrix2)
Compute the element-wise product of two matrices.
cpl_array * muse_cplmatrix_where(const cpl_matrix *aMatrix, double aValue, muse_cplmatrix_element_compare_func aCondition)
Select matrix elements according to a condition.
cpl_table * muse_find_stars(const cpl_image *aImage, double aHmin, double aFwhm, const double *aRoundLimits, const double *aSharpLimits)
Find positive brightness perturbations (i.e stars) in an image.
muse_mask * muse_sky_create_skymask(muse_image *, double, double, const char *)
Select spaxels to be considered as sky.
cpl_error_code muse_processing_save_table(muse_processing *aProcessing, int aIFU, void *aTable, cpl_propertylist *aHeader, const char *aTag, muse_table_type aType)
Save a computed table to disk.
muse_image * muse_image_new(void)
Allocate memory for a new muse_image object.
cpl_mask * mask
The mask data.
cpl_error_code muse_imagelist_set(muse_imagelist *aList, muse_image *aImage, unsigned int aIdx)
Set the muse_image of given list index.
muse_image * muse_imagelist_unset(muse_imagelist *aList, unsigned int aIdx)
Unset the muse_image at given list index from the list.
cpl_error_code muse_image_dq_to_nan(muse_image *aImage)
Convert pixels flagged in the DQ extension to NANs in DATA (and STAT, if present).
double sharpmin
Lower limit of the allowed point-source sharpness.