30#include <gsl/gsl_multifit.h>
31#include <gsl/gsl_statistics.h>
32#include <gsl/gsl_errno.h>
33#include <gsl/gsl_spline.h>
34#include <gsl/gsl_fit.h>
35#include <gsl/gsl_sort.h>
37#include "moo_badpix.h"
39#include "sc_skycorr.h"
40#include "hdrl_types.h"
52static const char *
const _moons_license =
53 "This file is part of the MOONS Instrument Pipeline\n"
54 "Copyright (C) 2015-2016 European Southern Observatory\n"
56 "This program is free software; you can redistribute it and/or modify\n"
57 "it under the terms of the GNU General Public License as published by\n"
58 "the Free Software Foundation; either version 2 of the License, or\n"
59 "(at your option) any later version.\n"
61 "This program is distributed in the hope that it will be useful,\n"
62 "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
63 "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
64 "GNU General Public License for more details.\n"
66 "You should have received a copy of the GNU General Public License\n"
67 "along with this program; if not, write to the Free Software\n"
68 "Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,\n"
83 return _moons_license;
102 cpl_ensure(list != NULL, CPL_ERROR_NULL_INPUT, NULL);
103 cpl_ensure(qlist != NULL, CPL_ERROR_NULL_INPUT, NULL);
104 cpl_ensure(img != NULL, CPL_ERROR_NULL_INPUT, NULL);
106 int size = cpl_imagelist_get_size(list);
107 int qsize = cpl_imagelist_get_size(qlist);
108 cpl_ensure(size > 1, CPL_ERROR_ILLEGAL_INPUT, NULL);
109 cpl_ensure(size == qsize, CPL_ERROR_ILLEGAL_INPUT, NULL);
111 int nx = cpl_image_get_size_x(img);
112 int ny = cpl_image_get_size_y(img);
113 const double *med_data = cpl_image_get_data_double_const(img);
115 cpl_image *res = cpl_image_new(nx, ny, CPL_TYPE_DOUBLE);
116 double *res_data = cpl_image_get_data_double(res);
118 for (i = 0; i < nx * ny; i++) {
119 double med = med_data[i];
123 for (j = 0; j < size; j++) {
124 const cpl_image *im = cpl_imagelist_get_const(list, j);
125 const cpl_image *qim = cpl_imagelist_get_const(qlist, j);
126 const double *im_data = cpl_image_get_data_double_const(im);
127 const int *qim_data = cpl_image_get_data_int_const(qim);
130 double sig = im_data[i] - med;
136 res_data[i] = sqrt(val / (size - 1));
142 cpl_image_set_bpm(res, cpl_mask_duplicate(cpl_image_get_bpm(img)));
169 cpl_mask *res = NULL;
170 cpl_ensure(sigma_img != NULL, CPL_ERROR_NULL_INPUT, NULL);
171 cpl_ensure(niter >= 0, CPL_ERROR_ILLEGAL_INPUT, NULL);
172 cpl_ensure(kappa >= 0, CPL_ERROR_ILLEGAL_INPUT, NULL);
176 int nx = cpl_image_get_size_x(sigma_img);
177 int ny = cpl_image_get_size_y(sigma_img);
178 cpl_mask *mask = cpl_image_get_bpm(sigma_img);
180 res = cpl_mask_new(nx, ny);
182 cpl_binary *orig_mask_data = cpl_mask_get_data(mask);
183 cpl_binary *res_mask_data = cpl_mask_get_data(res);
185 double *sigma_data = cpl_image_get_data_double(sigma_img);
187 int initbad = cpl_mask_count(mask);
188 int initgood = nx * ny - initbad;
189 double maxrejected = maxfrac * initgood;
192 double median = cpl_image_get_median_dev(sigma_img, &sig);
194 for (iter = 0; iter < niter; iter++) {
195 cpl_msg_info(__func__,
196 "Iteration %d: median value = %f stdev value = %f",
197 iter + 1, median, sig);
200 double ksig = kappa * sig;
201 for (i = 0; i < nx * ny; i++) {
202 if (orig_mask_data[i] == CPL_BINARY_0 &&
203 fabs(sigma_data[i] - median) > ksig) {
205 if (nbrej < maxrejected) {
206 res_mask_data[i] = CPL_BINARY_1;
207 orig_mask_data[i] = CPL_BINARY_1;
212 "Number of rejected pixel reached maximum value %d/%d",
221 cpl_msg_info(__func__,
"Number of detected bad pixels %d", nbrej);
223 median = cpl_image_get_median_dev(sigma_img, &sig2);
225 if ((sig - sig2) > cdiff) {
229 cpl_msg_info(__func__,
230 "Difference in sigma = %f reached minimum value = %f",
231 (sig - sig2), cdiff);
256 cpl_ensure_code(points != NULL, CPL_ERROR_NULL_INPUT);
257 cpl_ensure_code(center != NULL, CPL_ERROR_NULL_INPUT);
258 cpl_ensure_code(width != NULL, CPL_ERROR_NULL_INPUT);
260 int size = cpl_bivector_get_size(points);
262 cpl_ensure_code(size > 0, CPL_ERROR_ILLEGAL_INPUT);
264 cpl_vector *x = cpl_bivector_get_x(points);
265 cpl_vector *y = cpl_bivector_get_y(points);
267 double x1 = cpl_vector_get(x, 0);
268 double x2 = cpl_vector_get(x, size - 1);
273 for (i = 0; i < size; i++) {
274 double p = cpl_vector_get(x, i);
275 double f = cpl_vector_get(y, i);
282 return CPL_ERROR_DIVISION_BY_ZERO;
288 return CPL_ERROR_NONE;
310 cpl_fit_mode fit_pars,
316 cpl_ensure_code(points != NULL, CPL_ERROR_NULL_INPUT);
317 cpl_ensure_code(center != NULL, CPL_ERROR_NULL_INPUT);
318 cpl_ensure_code(width != NULL, CPL_ERROR_NULL_INPUT);
319 cpl_ensure_code(background != NULL, CPL_ERROR_NULL_INPUT);
320 cpl_ensure_code(area != NULL, CPL_ERROR_NULL_INPUT);
322 int size = cpl_bivector_get_size(points);
324 cpl_ensure_code(size > 0, CPL_ERROR_ILLEGAL_INPUT);
326 cpl_vector *x = cpl_bivector_get_x(points);
327 cpl_vector *y = cpl_bivector_get_y(points);
329 cpl_error_code status =
330 cpl_vector_fit_gaussian(x, NULL, y, NULL, fit_pars, center, width, area,
331 background, NULL, NULL, NULL);
352 res = area / sqrt(CPL_MATH_2PI * sigma * sigma) *
353 exp(-(x - x0) * (x - x0) / (2 * sigma * sigma)) +
381 double n = log((y - offset) * (sqrt(CPL_MATH_2PI * sigma * sigma) / area)) *
382 (-2 * sigma * sigma);
387 return CPL_ERROR_NONE;
391_moo_interpolate_x(cpl_bivector *points,
int i,
double val)
393 cpl_ensure_code(points != NULL, CPL_ERROR_NULL_INPUT);
394 cpl_ensure_code(i >= 0, CPL_ERROR_NULL_INPUT);
396 int size = cpl_bivector_get_size(points);
399 cpl_ensure_code(size > i + 1, CPL_ERROR_NULL_INPUT);
401 double *y = cpl_bivector_get_y_data(points);
402 double *x = cpl_bivector_get_x_data(points);
406 double x2 = x[i + 1];
407 double y2 = y[i + 1];
409 return x1 + (val - y1) / (y2 - y1) * (x2 - x1);
428 cpl_ensure_code(points != NULL, CPL_ERROR_NULL_INPUT);
429 cpl_ensure_code(xmin != NULL, CPL_ERROR_NULL_INPUT);
430 cpl_ensure_code(xmax != NULL, CPL_ERROR_NULL_INPUT);
432 int size = cpl_bivector_get_size(points);
434 cpl_ensure_code(size > 1, CPL_ERROR_NULL_INPUT);
436 double *y = cpl_bivector_get_y_data(points);
439 for (i = 1; i < size; i++) {
440 if (y[i] > threshold) {
441 *xmin = _moo_interpolate_x(points, i - 1, threshold);
446 for (i = size - 2; i >= 0; i++) {
447 if (y[i] > threshold) {
448 *xmax = _moo_interpolate_x(points, i, threshold);
452 return CPL_ERROR_NONE;
466_moo_tchebychev_transform(cpl_vector *posv,
double min,
double max)
471 cpl_ensure_code(posv != NULL, CPL_ERROR_NULL_INPUT);
472 int size = cpl_vector_get_size(posv);
473 cpl_ensure_code(size > 0, CPL_ERROR_ILLEGAL_INPUT);
474 cpl_ensure_code(min < max, CPL_ERROR_ILLEGAL_INPUT);
476 double *pos = cpl_vector_get_data(posv);
479 b = 1 - 2 * max / (max - min);
481 for (i = 0; i < size; i++) {
482 double res = pos[i] * a + b;
493 return CPL_ERROR_NONE;
497_moo_tchebychev_reverse_transform(cpl_vector *posv,
double min,
double max)
502 cpl_ensure_code(min < max, CPL_ERROR_ILLEGAL_INPUT);
504 int size = cpl_vector_get_size(posv);
507 b = 1 - 2 * max / (max - min);
509 double *pos = cpl_vector_get_data(posv);
511 for (i = 0; i < size; i++) {
512 double res = (pos[i] - b) / a;
516 return CPL_ERROR_NONE;
531_moo_tchebychev_poly_eval(
int n,
double X)
533 cpl_vector *result = NULL;
535 cpl_ensure(n >= 0, CPL_ERROR_ILLEGAL_INPUT, NULL);
537 result = cpl_vector_new(n + 1);
539 cpl_vector_set(result, 0, 1.0);
543 cpl_vector_set(result, 1, X);
544 while (indice <= n) {
545 double T_indice = 0.0;
546 double T_indice_1 = 0.0;
547 double T_indice_2 = 0.0;
549 T_indice_1 = cpl_vector_get(result, indice - 1);
550 T_indice_2 = cpl_vector_get(result, indice - 2);
551 T_indice = 2 * X * T_indice_1 - T_indice_2;
552 cpl_vector_set(result, indice, T_indice);
572moo_tcheby_polynomial *
578 cpl_ensure(data != NULL, CPL_ERROR_NULL_INPUT, NULL);
579 cpl_ensure(degree > 0, CPL_ERROR_ILLEGAL_INPUT, NULL);
583 gsl_matrix *X = NULL, *cov = NULL;
584 gsl_vector *y = NULL, *c = NULL;
585 gsl_multifit_linear_workspace *work = NULL;
588 cpl_ensure(data != NULL, CPL_ERROR_NULL_INPUT, NULL);
590 int size = cpl_bivector_get_size(data);
592 cpl_ensure(size > degree, CPL_ERROR_ILLEGAL_INPUT, NULL);
594 moo_tcheby_polynomial *res = cpl_calloc(1,
sizeof(moo_tcheby_polynomial));
596 res->solution = cpl_polynomial_new(1);
598 cpl_vector *vx = cpl_bivector_get_x(data);
599 cpl_vector *vy = cpl_bivector_get_y(data);
603 res->ymin = cpl_vector_get_min(vy);
604 res->ymax = cpl_vector_get_max(vy);
606 res->degree = degree;
608 _moo_tchebychev_transform(vx, res->xmin, res->xmax);
609 _moo_tchebychev_transform(vy, res->ymin, res->ymax);
610 double *xdata = cpl_bivector_get_x_data(data);
611 double *ydata = cpl_bivector_get_y_data(data);
613 int nbcoeffs = degree + 1;
615 X = gsl_matrix_alloc(size, nbcoeffs);
616 y = gsl_vector_alloc(size);
617 c = gsl_vector_alloc(nbcoeffs);
618 cov = gsl_matrix_alloc(nbcoeffs, nbcoeffs);
620 for (i = 0; i < size; i++) {
621 for (j = 0; j < degree + 1; j++) {
622 double Tj = cos(j * acos(xdata[i]));
623 gsl_matrix_set(X, i, j, Tj);
625 gsl_vector_set(y, i, ydata[i]);
628 work = gsl_multifit_linear_alloc(size, nbcoeffs);
629 gsl_multifit_linear(X, y, c, cov, &chisq, work);
631 for (j = 0; j < degree + 1; j++) {
633 cpl_polynomial_set_coeff(res->solution, pows, gsl_vector_get(c, j));
636 _moo_tchebychev_reverse_transform(vy, res->ymin, res->ymax);
637 _moo_tchebychev_reverse_transform(vx, res->xmin, res->xmax);
638 gsl_multifit_linear_free(work);
642 gsl_matrix_free(cov);
666moo_tcheby2d_polynomial *
679 cpl_ensure(in_x != NULL, CPL_ERROR_NULL_INPUT, NULL);
680 cpl_ensure(xdegree > 0, CPL_ERROR_ILLEGAL_INPUT, NULL);
681 cpl_ensure(in_y != NULL, CPL_ERROR_NULL_INPUT, NULL);
682 cpl_ensure(ydegree > 0, CPL_ERROR_ILLEGAL_INPUT, NULL);
683 cpl_ensure(in_l != NULL, CPL_ERROR_NULL_INPUT, NULL);
687 gsl_matrix *X = NULL, *cov = NULL;
688 gsl_vector *y = NULL, *c = NULL;
689 gsl_multifit_linear_workspace *work = NULL;
692 moo_tcheby2d_polynomial *res =
693 cpl_calloc(1,
sizeof(moo_tcheby2d_polynomial));
695 res->solution = cpl_polynomial_new(2);
697 int size = cpl_vector_get_size(in_x);
705 res->degree_x = xdegree;
706 res->degree_y = ydegree;
708 cpl_vector *vx = cpl_vector_duplicate(in_x);
709 cpl_vector *vy = cpl_vector_duplicate(in_y);
710 cpl_vector *vl = cpl_vector_duplicate(in_l);
712 _moo_tchebychev_transform(vx, res->xmin, res->xmax);
713 _moo_tchebychev_transform(vy, res->ymin, res->ymax);
714 _moo_tchebychev_transform(vl, res->lmin, res->lmax);
716 double *xdata = cpl_vector_get_data(vx);
717 double *ydata = cpl_vector_get_data(vy);
718 double *ldata = cpl_vector_get_data(vl);
720 int nbcoeffs = (xdegree + 1) * (ydegree + 1);
722 X = gsl_matrix_alloc(size, nbcoeffs);
723 y = gsl_vector_alloc(size);
724 c = gsl_vector_alloc(nbcoeffs);
725 cov = gsl_matrix_alloc(nbcoeffs, nbcoeffs);
727 for (i = 0; i < size; i++) {
728 for (j = 0; j < xdegree + 1; j++) {
729 for (k = 0; k < ydegree + 1; k++) {
730 double px = cos(j * acos(xdata[i]));
731 double py = cos(k * acos(ydata[i]));
732 gsl_matrix_set(X, i, k + j * (ydegree + 1), px * py);
735 gsl_vector_set(y, i, ldata[i]);
737 work = gsl_multifit_linear_alloc(size, nbcoeffs);
738 gsl_multifit_linear(X, y, c, cov, &chisq, work);
740 for (j = 0; j < xdegree + 1; j++) {
741 for (k = 0; k < ydegree + 1; k++) {
744 cpl_polynomial_set_coeff(res->solution, pows,
745 gsl_vector_get(c, k + j * (ydegree + 1)));
749 gsl_multifit_linear_free(work);
753 gsl_matrix_free(cov);
755 cpl_vector_delete(vx);
756 cpl_vector_delete(vy);
757 cpl_vector_delete(vl);
763moo_tcheby_polynomial_delete(moo_tcheby_polynomial *self)
766 if (self->solution != NULL) {
767 cpl_polynomial_delete(self->solution);
774moo_tcheby2d_polynomial_delete(moo_tcheby2d_polynomial *self)
777 if (self->solution != NULL) {
778 cpl_polynomial_delete(self->solution);
785moo_tcheby_polynomial_eval(moo_tcheby_polynomial *self,
double x)
790 cpl_vector *vx = cpl_vector_new(1);
791 cpl_vector *vy = cpl_vector_new(1);
793 cpl_vector_set(vx, 0, x);
795 _moo_tchebychev_transform(vx, self->xmin, self->xmax);
796 double tx = cpl_vector_get(vx, 0);
798 cpl_vector *tcheb = _moo_tchebychev_poly_eval(self->degree, tx);
799 for (
int j = 0; j < self->degree + 1; j++) {
801 double tval = cpl_vector_get(tcheb, j);
802 double coef = cpl_polynomial_get_coeff(self->solution, pows);
805 cpl_vector_set(vy, 0, yval);
807 _moo_tchebychev_reverse_transform(vy, self->ymin, self->ymax);
809 yval = cpl_vector_get(vy, 0);
811 cpl_vector_delete(tcheb);
812 cpl_vector_delete(vx);
813 cpl_vector_delete(vy);
819moo_tcheby2d_polynomial_eval(moo_tcheby2d_polynomial *self,
double x,
double y)
824 cpl_vector *vx = cpl_vector_new(1);
825 cpl_vector *vy = cpl_vector_new(1);
826 cpl_vector *vl = cpl_vector_new(1);
828 cpl_vector_set(vx, 0, x);
829 cpl_vector_set(vy, 0, y);
831 _moo_tchebychev_transform(vx, self->xmin, self->xmax);
832 _moo_tchebychev_transform(vy, self->ymin, self->ymax);
833 double tx = cpl_vector_get(vx, 0);
834 double ty = cpl_vector_get(vy, 0);
836 cpl_vector *tchebx = _moo_tchebychev_poly_eval(self->degree_x, tx);
837 cpl_vector *tcheby = _moo_tchebychev_poly_eval(self->degree_y, ty);
839 for (
int j = 0; j < self->degree_x + 1; j++) {
840 for (
int k = 0; k < self->degree_y + 1; k++) {
843 double tvalx = cpl_vector_get(tchebx, j);
844 double tvaly = cpl_vector_get(tcheby, k);
845 double coef = cpl_polynomial_get_coeff(self->solution, pows);
846 yval += coef * tvalx * tvaly;
850 cpl_vector_set(vl, 0, yval);
852 _moo_tchebychev_reverse_transform(vl, self->lmin, self->lmax);
854 yval = cpl_vector_get(vl, 0);
856 cpl_vector_delete(tchebx);
857 cpl_vector_delete(tcheby);
858 cpl_vector_delete(vx);
859 cpl_vector_delete(vy);
860 cpl_vector_delete(vl);
888 cpl_ensure_code(data != NULL, CPL_ERROR_NULL_INPUT);
889 cpl_ensure_code(flag != NULL, CPL_ERROR_NULL_INPUT);
890 cpl_ensure_code(degree > 0, CPL_ERROR_ILLEGAL_INPUT);
893 gsl_matrix *X = NULL, *cov = NULL;
894 gsl_vector *y = NULL, *c = NULL;
895 gsl_multifit_linear_workspace *work = NULL;
897 cpl_polynomial *result = cpl_polynomial_new(1);
899 cpl_vector *vx = cpl_bivector_get_x(data);
900 cpl_vector *vy = cpl_bivector_get_y(data);
902 _moo_tchebychev_transform(vx, xmin, xmax);
903 _moo_tchebychev_transform(vy, ymin, ymax);
906 double *xdata = cpl_bivector_get_x_data(data);
907 double *ydata = cpl_bivector_get_y_data(data);
909 int nbcoeffs = degree + 1;
912 for (i = xmin - 1; i < xmax; i++) {
918 X = gsl_matrix_alloc(nb_valid, nbcoeffs);
919 y = gsl_vector_alloc(nb_valid);
920 c = gsl_vector_alloc(nbcoeffs);
921 cov = gsl_matrix_alloc(nbcoeffs, nbcoeffs);
924 for (i = xmin - 1; i < xmax; i++) {
926 for (j = 0; j < degree + 1; j++) {
927 double Tj = cos(j * acos(xdata[i]));
928 gsl_matrix_set(X,
id, j, Tj);
930 gsl_vector_set(y,
id, ydata[i]);
935 work = gsl_multifit_linear_alloc(nb_valid, nbcoeffs);
936 gsl_multifit_linear(X, y, c, cov, &chisq, work);
938 for (j = 0; j < degree + 1; j++) {
940 cpl_polynomial_set_coeff(result, pows, gsl_vector_get(c, j));
943 for (i = xmin - 1; i < xmax; i++) {
945 double xval = xdata[i];
946 cpl_vector *tcheb = _moo_tchebychev_poly_eval(degree, xval);
947 for (j = 0; j < degree + 1; j++) {
949 double tval = cpl_vector_get(tcheb, j);
950 double coef = cpl_polynomial_get_coeff(result, pows);
954 cpl_vector_delete(tcheb);
957 _moo_tchebychev_reverse_transform(vx, xmin, xmax);
958 _moo_tchebychev_reverse_transform(vy, ymin, ymax);
960 gsl_multifit_linear_free(work);
964 gsl_matrix_free(cov);
965 cpl_polynomial_delete(result);
967 return CPL_ERROR_NONE;
981 int size = cpl_vector_get_size(v);
983 double res = DBL_MAX;
985 for (
int i = 0; i < size; i++) {
987 double val = cpl_vector_get(v, i);
988 res = CPL_MIN(res, val);
1005 int size = cpl_vector_get_size(v);
1007 double res = DBL_MIN;
1010 for (
int i = 0; i < size; i++) {
1011 if (flags[i] == 0) {
1012 double val = cpl_vector_get(v, i);
1013 res = CPL_MAX(res, val);
1020moo_hpfilter(cpl_vector *Yv,
double S)
1022 int N = cpl_vector_get_size(Yv);
1023 double *Y = cpl_vector_get_data(Yv);
1025 cpl_vector *Tv = cpl_vector_new(N);
1026 double *T = cpl_vector_get_data(Tv);
1027 double **V = cpl_calloc(N,
sizeof(
double *));
1029 for (
int i = 0; i < N; i++) {
1030 V[i] = cpl_calloc(3,
sizeof(
double));
1032 double *D = cpl_calloc(N,
sizeof(
double));
1033 double SS = 0.0, M1 = 0.0, M2 = 0.0, V11 = 0.0, V12 = 0.0, V22 = 0.0,
1034 X = 0.0, Z = 0.0, B11 = 0.0, B12 = 0.0, B22 = 0.0, DET = 0.0,
1036 int NN = 0, I1 = 0, IB = 0;
1038 if (NN != N || S != SS) {
1044 for (
int i = 2; i <= N - 1; i++) {
1047 V11 = 1.0 / S + 4.0 * (X - Z) + V22;
1050 DET = V11 * V22 - V12 * V12;
1051 V[i][0] = V22 / DET;
1052 V[i][1] = -V12 / DET;
1053 V[i][2] = V11 / DET;
1056 V11 = V11 - V11 * V11 / X;
1057 V22 = V22 - V12 * V12 / X;
1058 V12 = V12 - Z * V12 / X;
1066 for (
int i = 2; i <= N - 1; i++) {
1070 T[i - 1] = V[i][0] * M1 + V[i][1] * M2;
1071 D[i - 1] = V[i][1] * M1 + V[i][2] * M2;
1072 DET = V[i][0] * V[i][2] - V[i][1] * V[i][1];
1073 V11 = V[i][2] / DET;
1074 V12 = -V[i][1] / DET;
1075 Z = (Y[i] - M1) / (V11 + 1);
1086 for (
int i = N - 3; i >= 0; i--) {
1094 E1 = V[IB][2] * M2 + V[IB][1] * M1 + T[i];
1095 E2 = V[IB][1] * M2 + V[IB][0] * M1 + D[i];
1096 B11 = V[IB][2] + V[I1][0];
1097 B12 = V[IB][1] + V[I1][1];
1098 B22 = V[IB][0] + V[I1][2];
1099 DET = B11 * B22 - B12 * B12;
1100 T[i] = (-B12 * E1 + B11 * E2) / DET;
1103 DET = V[IB][0] * V[IB][2] - V[IB][1] * V[IB][1];
1104 V11 = V[IB][2] / DET;
1105 V12 = -V[IB][1] / DET;
1106 Z = (Y[i] - M1) / (V11 + 1.);
1114 for (
int i = 0; i < N; i++) {
1161 cpl_vector *fout_errs,
1163 const cpl_bivector *fref,
1164 const cpl_vector *fref_errs,
1165 const int *fref_qual)
1167 const cpl_size m = cpl_bivector_get_size(fref);
1168 const cpl_size n = cpl_bivector_get_size(fout);
1169 const double *xref = cpl_bivector_get_x_data_const(fref);
1170 const double *yref = cpl_bivector_get_y_data_const(fref);
1171 const double *ref_errs = cpl_vector_get_data_const(fref_errs);
1172 double *xout = cpl_bivector_get_x_data(fout);
1173 double *yout = cpl_bivector_get_y_data(fout);
1174 double *out_errs = cpl_vector_get_data(fout_errs);
1177 double y0_err = DBL_MAX;
1178 double grad = DBL_MAX;
1179 double grad_err = DBL_MAX;
1180 double y_0 = DBL_MAX;
1181 cpl_size ibelow, iabove;
1185 cpl_ensure_code(xref != NULL, cpl_error_get_code());
1186 cpl_ensure_code(yref != NULL, cpl_error_get_code());
1187 cpl_ensure_code(ref_errs != NULL, cpl_error_get_code());
1188 cpl_ensure_code(xout != NULL, cpl_error_get_code());
1189 cpl_ensure_code(yout != NULL, cpl_error_get_code());
1190 cpl_ensure_code(out_errs != NULL, cpl_error_get_code());
1193 cpl_ensure_code(xout[n - 1] <= xref[m - 1], CPL_ERROR_DATA_NOT_FOUND);
1196 ibelow = cpl_vector_find(cpl_bivector_get_x_const(fref), xout[0]);
1198 if (xout[0] < xref[ibelow]) {
1200 cpl_ensure_code(ibelow > 0, CPL_ERROR_DATA_NOT_FOUND);
1207 for (i = 0; i < n; i++) {
1209 if (xref[iabove] < xout[i]) {
1211 while (xref[++iabove] < xout[i])
1213 ibelow = iabove - 1;
1216 if (xref[iabove] <= xref[ibelow])
1219 qual = fref_qual[ibelow] | fref_qual[iabove];
1222 (yref[iabove] - yref[ibelow]) / (xref[iabove] - xref[ibelow]);
1223 y_0 = yref[ibelow] - grad * xref[ibelow];
1225 grad_err = (ref_errs[iabove] * ref_errs[iabove] -
1226 ref_errs[ibelow] * ref_errs[ibelow]) /
1227 (xref[iabove] - xref[ibelow]);
1229 ref_errs[ibelow] * ref_errs[ibelow] - grad_err * xref[ibelow];
1232 if (xref[iabove] > xout[i]) {
1233 if (xref[ibelow] < xout[i]) {
1234 fout_qual[i] = qual;
1235 out_errs[i] = sqrt(y0_err + grad_err * xout[i]);
1236 yout[i] = y_0 + grad * xout[i];
1240 yout[i] = yref[ibelow];
1241 out_errs[i] = ref_errs[ibelow];
1242 fout_qual[i] = fref_qual[ibelow];
1246 yout[i] = yref[iabove];
1247 out_errs[i] = ref_errs[iabove];
1248 fout_qual[i] = fref_qual[iabove];
1252 return i == n ? CPL_ERROR_NONE : CPL_ERROR_ILLEGAL_INPUT;
1267 cpl_ensure(v != NULL, CPL_ERROR_NULL_INPUT, res);
1268 cpl_ensure(f >= 0 && f <= 1, CPL_ERROR_ILLEGAL_INPUT, res);
1270 int size = cpl_vector_get_size(v);
1273 for (
int i = 0; i < size; i++) {
1274 double val = cpl_vector_get(v, i);
1279 cpl_vector *temp = cpl_vector_new(size - nbnan);
1281 for (
int i = 0; i < size; i++) {
1282 double val = cpl_vector_get(v, i);
1284 cpl_vector_set(temp, idx, val);
1289 cpl_vector_sort(temp, CPL_SORT_ASCENDING);
1291 double *data = cpl_vector_get_data(temp);
1292 int dsize = cpl_vector_get_size(temp);
1294 res = gsl_stats_quantile_from_sorted_data(data, 1, dsize, f);
1296 cpl_vector_delete(temp);
1311 cpl_vector *res = NULL;
1312 cpl_ensure(v != NULL, CPL_ERROR_NULL_INPUT, res);
1313 int size = cpl_vector_get_size(v);
1316 for (
int i = 0; i < size; i++) {
1317 double val = cpl_vector_get(v, i);
1322 int nsize = size - nbnan;
1324 res = cpl_vector_new(nsize);
1326 for (
int i = 0; i < size; i++) {
1327 double val = cpl_vector_get(v, i);
1329 cpl_vector_set(res, j, val);
1345 cpl_bivector *res = NULL;
1346 cpl_ensure(v != NULL, CPL_ERROR_NULL_INPUT, NULL);
1348 int size = cpl_bivector_get_size(v);
1349 cpl_vector *y = cpl_bivector_get_y(v);
1350 cpl_vector *x = cpl_bivector_get_x(v);
1352 for (
int i = 0; i < size; i++) {
1353 double xval = cpl_vector_get(x, i);
1354 double yval = cpl_vector_get(y, i);
1355 if (isnan(xval) || isnan(yval)) {
1359 int nsize = size - nbnan;
1361 res = cpl_bivector_new(nsize);
1362 cpl_vector *ry = cpl_bivector_get_y(res);
1363 cpl_vector *rx = cpl_bivector_get_x(res);
1366 for (
int i = 0; i < size; i++) {
1367 double xval = cpl_vector_get(x, i);
1368 double yval = cpl_vector_get(y, i);
1369 if (!isnan(xval) && !isnan(yval)) {
1370 cpl_vector_set(ry, j, xval);
1371 cpl_vector_set(rx, j, yval);
1388 cpl_ensure(image != NULL, CPL_ERROR_NULL_INPUT, NULL);
1390 hdrl_image *res = NULL;
1392 int nx = hdrl_image_get_size_x(image);
1393 int ny = hdrl_image_get_size_y(image);
1395 res = hdrl_image_new(nx, 1);
1397 for (
int i = 1; i <= nx; i++) {
1398 hdrl_image *col = hdrl_image_extract(image, i, 1, i, ny);
1400 hdrl_value median = hdrl_image_get_median(col);
1401 hdrl_image_set_pixel(res, i, 1, median);
1402 hdrl_image_delete(col);
1421 double alpha1_rad = CPL_MATH_RAD_DEG * alpha1;
1422 double delta1_rad = CPL_MATH_RAD_DEG * delta1;
1423 double alpha2_rad = CPL_MATH_RAD_DEG * alpha2;
1424 double delta2_rad = CPL_MATH_RAD_DEG * delta2;
1427 acos(sin(delta1_rad) * sin(delta2_rad) +
1428 cos(delta1_rad) * cos(delta2_rad) * cos(alpha1_rad - alpha2_rad));
1433moo_spline_create(cpl_bivector *data)
1435 cpl_ensure(data != NULL, CPL_ERROR_NULL_INPUT, NULL);
1437 int size = cpl_bivector_get_size(data);
1438 double *x = cpl_bivector_get_x_data(data);
1439 double *y = cpl_bivector_get_y_data(data);
1441 moo_spline *res = cpl_calloc(1,
sizeof(moo_spline));
1443 res->max = x[size - 1];
1444 res->acc = gsl_interp_accel_alloc();
1445 res->spline = gsl_spline_alloc(gsl_interp_cspline, size);
1446 gsl_spline_init(res->spline, x, y, size);
1451moo_spline_eval(moo_spline *self,
double x)
1454 cpl_ensure(self != NULL, CPL_ERROR_NULL_INPUT, 0.0);
1455 if (x >= self->min && x <= self->max) {
1456 res = gsl_spline_eval(self->spline, x, self->acc);
1462moo_spline_delete(moo_spline *self)
1465 gsl_interp_accel_free(self->acc);
1466 gsl_spline_free(self->spline);
1473_moo_gsl_fit_wmul(
const double *x,
1487 double W = 0, wm_x = 0, wm_y = 0, wm_dx2 = 0, wm_dxdy = 0;
1488 double sig_wm_y2 = 0, sig_wm_dxdy2 = 0;
1494 for (i = 0; i < n; i++) {
1495 const double wi = w[i];
1499 wm_x += (x[i] - wm_x) * (wi / W);
1500 wm_y += (y[i] - wm_y) * (wi / W);
1501 sig_wm_y2 += (wi * wi * ey[i] * ey[i]);
1507 for (i = 0; i < n; i++) {
1508 const double wi = w[i];
1511 const double dx = x[i] - wm_x;
1512 const double dy = y[i] - wm_y;
1515 wm_dx2 += (dx * dx - wm_dx2) * (wi / W);
1516 wm_dxdy += (dx * dy - wm_dxdy) * (wi / W);
1517 sig_wm_dxdy2 += wi * wi * dx * dx *
1518 (ey[i] * ey[i] + sig_wm_y2);
1526 double b = (wm_x * wm_y + wm_dxdy) / (wm_x * wm_x + wm_dx2);
1528 double sig_b = sqrt(wm_x * wm_x * sig_wm_y2 + sig_wm_dxdy2) /
1529 (wm_x * wm_x + wm_dx2);
1533 *cov_11 = 1 / (W * (wm_x * wm_x + wm_dx2));
1537 for (i = 0; i < n; i++) {
1538 const double wi = w[i];
1541 const double dx = x[i] - wm_x;
1542 const double dy = y[i] - wm_y;
1543 const double d = (wm_y - b * wm_x) + (dy - b * dx);
1568 const cpl_vector *vw,
1569 const cpl_vector *vy,
1570 const cpl_vector *vy_err,
1574 cpl_ensure_code(vx != NULL, CPL_ERROR_NULL_INPUT);
1575 cpl_ensure_code(vy != NULL, CPL_ERROR_NULL_INPUT);
1576 cpl_ensure_code(vw != NULL, CPL_ERROR_NULL_INPUT);
1578 int n = cpl_vector_get_size(vx);
1580 const double *x = cpl_vector_get_data_const(vx);
1581 const double *y = cpl_vector_get_data_const(vy);
1582 const double *yerr = cpl_vector_get_data_const(vy_err);
1583 const double *w = cpl_vector_get_data_const(vw);
1585 double cov11, chisq;
1587 _moo_gsl_fit_wmul(x, w, y, yerr, n, c, sig_c, &cov11, &chisq);
1590 return CPL_ERROR_NONE;
1593static cpl_polynomial *
1594_moo_fit(cpl_vector *v,
int polyorder)
1596 gsl_matrix *X = NULL, *cov = NULL;
1597 gsl_vector *y = NULL, *c = NULL;
1598 gsl_multifit_linear_workspace *work = NULL;
1601 cpl_ensure(v != NULL, CPL_ERROR_NULL_INPUT, NULL);
1603 cpl_polynomial *pol = cpl_polynomial_new(1);
1606 double *data = cpl_vector_get_data(v);
1607 int size = cpl_vector_get_size(v);
1609 int nbcoeffs = polyorder + 1;
1610 X = gsl_matrix_alloc(size, nbcoeffs);
1611 y = gsl_vector_alloc(size);
1612 c = gsl_vector_alloc(nbcoeffs);
1613 cov = gsl_matrix_alloc(nbcoeffs, nbcoeffs);
1616 for (i = 0; i < size; i++) {
1619 for (j = 0; j < polyorder + 1; j++) {
1620 gsl_matrix_set(X, i, j, pow(i, j));
1622 gsl_vector_set(y, i, d);
1629 if ((size - nbnan) > (nbcoeffs + 1)) {
1630 work = gsl_multifit_linear_alloc(size, nbcoeffs);
1631 gsl_multifit_linear(X, y, c, cov, &chisq, work);
1633 for (j = 0; j < polyorder + 1; j++) {
1635 cpl_polynomial_set_coeff(pol, pows, gsl_vector_get(c, j));
1637 gsl_multifit_linear_free(work);
1642 gsl_matrix_free(cov);
1658 cpl_ensure(v != NULL, CPL_ERROR_NULL_INPUT, NULL);
1659 cpl_ensure(polyorder >= 0, CPL_ERROR_ILLEGAL_INPUT, NULL);
1660 cpl_ensure(polyorder < window_length, CPL_ERROR_ILLEGAL_INPUT, NULL);
1661 cpl_ensure(window_length >= 1, CPL_ERROR_ILLEGAL_INPUT, NULL);
1663 int size = cpl_vector_get_size(v);
1664 cpl_vector *result = cpl_vector_new(size);
1665 double *resdata = cpl_vector_get_data(result);
1667 cpl_polynomial *fit = NULL;
1668 cpl_vector *data = NULL;
1671 int hsize = (window_length - 1) / 2;
1673 data = cpl_vector_extract(v, 0, window_length - 1, 1);
1675 fit = _moo_fit(data, polyorder);
1677 for (
int i = 0; i < hsize; i++) {
1679 for (
int j = 0; j < polyorder + 1; j++) {
1681 double coef = cpl_polynomial_get_coeff(fit, pows);
1682 yval += coef * pow(i, j);
1686 cpl_vector_delete(data);
1687 cpl_polynomial_delete(fit);
1689 data = cpl_vector_extract(v, size - window_length, size - 1, 1);
1690 fit = _moo_fit(data, polyorder);
1692 for (
int i = hsize; i < window_length; i++) {
1694 for (
int j = 0; j < polyorder + 1; j++) {
1696 double coef = cpl_polynomial_get_coeff(fit, pows);
1697 yval += coef * pow(i, j);
1699 resdata[size - window_length + i] = yval;
1701 cpl_vector_delete(data);
1702 cpl_polynomial_delete(fit);
1705 for (
int i = hsize; i < size - hsize; i++) {
1707 cpl_vector_extract(v, i - hsize, i - hsize - 1 + window_length, 1);
1708 fit = _moo_fit(data, polyorder);
1710 for (
int j = 0; j < polyorder + 1; j++) {
1712 double coef = cpl_polynomial_get_coeff(fit, pows);
1713 yval += coef * pow(hsize, j);
1715 cpl_vector_delete(data);
1716 cpl_polynomial_delete(fit);
1732 cpl_ensure(v != NULL, CPL_ERROR_NULL_INPUT, NULL);
1733 cpl_ensure(winhsize >= 0, CPL_ERROR_ILLEGAL_INPUT, NULL);
1735 cpl_vector *result = NULL;
1736 int size = cpl_vector_get_size(v);
1737 int K = winhsize * 2 + 1;
1739 result = cpl_vector_new(size);
1741 double *windata = cpl_calloc(K,
sizeof(
double));
1743 for (
int i = 0; i < size; ++i) {
1746 int kmin = i - winhsize;
1751 int kmax = i + winhsize;
1755 for (
int k = kmin; k <= kmax; k++) {
1756 double val = cpl_vector_get(v, k);
1763 cpl_vector *winv = cpl_vector_wrap(idx, windata);
1765 double med = cpl_vector_get_median(winv);
1766 cpl_vector_set(result, i, med);
1767 cpl_vector_unwrap(winv);
1770 cpl_vector_set(result, i, NAN);
1793 cpl_image *result = NULL;
1796 cpl_errorstate prestate = cpl_errorstate_get();
1798 cpl_ensure(list != NULL, CPL_ERROR_NULL_INPUT, NULL);
1799 cpl_ensure(datalist != NULL, CPL_ERROR_NULL_INPUT, NULL);
1800 size = cpl_imagelist_get_size(list);
1801 cpl_ensure(size > 0, CPL_ERROR_NULL_INPUT, NULL);
1803 cpl_image *first = cpl_imagelist_get(list, 0);
1804 int nx = cpl_image_get_size_x(first);
1805 int ny = cpl_image_get_size_y(first);
1807 result = cpl_image_new(nx, ny, CPL_TYPE_INT);
1809 for (
int y = 1; y <= ny; y++) {
1810 for (
int x = 1; x <= nx; x++) {
1814 for (
int k = 0; k < size; k++) {
1815 cpl_image *qual = cpl_imagelist_get(list, k);
1816 hdrl_image *data = hdrl_imagelist_get(datalist, k);
1818 int qv = cpl_image_get(qual, x, y, &rej);
1819 hdrl_image_get_pixel(data, x, y, &rej);
1829 cpl_image_set(result, x, y, v);
1834 if (!cpl_errorstate_is_equal(prestate)) {
1835 cpl_image_delete(result);
1837 cpl_errorstate_dump(prestate, CPL_FALSE, cpl_errorstate_dump_one);
1839 cpl_errorstate_set(prestate);
1870 double max_error_frac,
1874 double ronerr = 0.0;
1877 cpl_ensure(diff, CPL_ERROR_NULL_INPUT, 0.0);
1878 cpl_size *window = (cpl_size *)cpl_calloc(
sizeof(cpl_size), 4);
1885 cpl_flux_get_noise_window(diff, window, box_hsize, nb_boxes, &ron,
1888 }
while (ronerr > max_error_frac * ron && niter < max_niter);
1908 cpl_ensure_code(image != NULL, CPL_ERROR_NULL_INPUT);
1909 cpl_ensure_code(qmin != NULL, CPL_ERROR_NULL_INPUT);
1910 cpl_ensure_code(qmax != NULL, CPL_ERROR_NULL_INPUT);
1912 int nx = cpl_image_get_size_x(image);
1913 int ny = cpl_image_get_size_y(image);
1915 cpl_image *stats = cpl_image_cast(image, CPL_TYPE_DOUBLE);
1916 cpl_image_reject_value(stats, CPL_VALUE_NAN);
1917 int nb = cpl_image_count_rejected(stats);
1918 int nsize = size - nb;
1919 cpl_vector *statsv = cpl_vector_new(nsize);
1920 double *data = cpl_image_get_data_double(stats);
1922 for (
int i = 0; i < size; i++) {
1928 cpl_vector_set(statsv, i - nbnan, v);
1931 double *stats_data = cpl_vector_get_data(statsv);
1932 gsl_sort(stats_data, 1, nsize);
1934 gsl_stats_quantile_from_sorted_data(stats_data, 1, nsize, 0.8415);
1936 gsl_stats_quantile_from_sorted_data(stats_data, 1, nsize, 0.1585);
1940 cpl_vector_delete(statsv);
1941 cpl_image_delete(stats);
1942 return CPL_ERROR_NONE;
1946#define MACHEP DBL_EPSILON
1947#define MAXLOG log(FLT_MAX)
1950static double igam(
double,
double);
1951static double igamc(
double,
double);
1953static double big = 4.503599627370496e15;
1954static double biginv = 2.22044604925031308085e-16;
1957igam(
double a,
double x)
1959 double ans, ax, c, r;
1966 if ((x < 0) || (a <= 0)) {
1967 mtherr(
"igam", DOMAIN);
1971 if ((x > 1.) && (x > a)) {
1972 return (1. - igamc(a, x));
1976 ax = a * log(x) - x - lgamma(a);
1978 mtherr(
"igam", UNDERFLOW);
1993 }
while (c / ans > MACHEP);
1995 return (ans * ax / a);
1999igamc(
double a,
double x)
2001 double ans, ax, c, r, t, y, z;
2002 double pkm1, pkm2, qkm1, qkm2;
2004 if ((x < 0) || (a <= 0)) {
2005 mtherr(
"igamc", DOMAIN);
2009 if ((x < 1.0) || (x < a)) {
2010 return (1. - igam(a, x));
2012 ax = a * log(x) - x - lgamma(a);
2015 mtherr(
"igamc", UNDERFLOW);
2036 double pk = pkm1 * z - pkm2 * yc;
2037 double qk = qkm1 * z - qkm2 * yc;
2041 t = fabs((ans - r) / r);
2053 if (fabs(pk) > big) {
2060 }
while (t > MACHEP);
2066bpm_from_rel(cpl_image *img,
double kappa_low,
double kappa_high,
int mad)
2071 m = cpl_image_get_mad(img, &s);
2072 s *= CPL_MATH_STD_MAD;
2073 s = CX_MAX(DBL_EPSILON, s);
2076 m = cpl_image_get_mean(img);
2077 s = cpl_image_get_stdev(img);
2079 cpl_mask *bpm = cpl_mask_threshold_image_create(img, m - s * kappa_low,
2080 m + s * kappa_high);
2082 r = cpl_image_new_from_mask(bpm);
2083 cpl_mask_delete(bpm);
2087moo_hdrl_bpm_fit_compute(
const hdrl_parameter *par,
2088 const hdrl_imagelist *data,
2089 const cpl_vector *sample_pos,
2090 cpl_image **out_mask)
2092 cpl_error_code status = CPL_ERROR_NONE;
2093 cpl_image *out_chi2 = NULL, *out_dof = NULL;
2094 hdrl_imagelist *out_coef = NULL;
2096 status = hdrl_bpm_fit_parameter_verify(par);
2097 if (status != CPL_ERROR_NONE)
2100 int degree = hdrl_bpm_fit_parameter_get_degree(par);
2105 status = hdrl_fit_polynomial_imagelist(data, sample_pos, degree, &out_coef,
2106 &out_chi2, &out_dof);
2109 return cpl_error_set_message(cpl_func, CPL_ERROR_FILE_NOT_FOUND,
2113 if (cpl_image_count_rejected(out_chi2) ==
2114 (cpl_image_get_size_x(out_chi2) * cpl_image_get_size_y(out_chi2))) {
2115 cpl_msg_error(cpl_func,
2116 "Too few good pixels to fit polynomial of "
2117 "degree %d in all pixels",
2124 cpl_image *bpm = NULL;
2125 double pval = hdrl_bpm_fit_parameter_get_pval(par);
2126 double rel_chi_l = hdrl_bpm_fit_parameter_get_rel_chi_low(par);
2127 double rel_chi_h = hdrl_bpm_fit_parameter_get_rel_chi_high(par);
2128 double rel_coef_l = hdrl_bpm_fit_parameter_get_rel_coef_low(par);
2129 double rel_coef_h = hdrl_bpm_fit_parameter_get_rel_coef_high(par);
2131 if (rel_chi_l >= 0) {
2133 cpl_image_power(out_chi2, 0.5);
2134 bpm = bpm_from_rel(out_chi2, rel_chi_l, rel_chi_h, 1);
2136 else if (rel_coef_l >= 0) {
2137 for (cpl_size i = 0; i < hdrl_imagelist_get_size(out_coef); i++) {
2138 hdrl_image *coef = hdrl_imagelist_get(out_coef, i);
2139 cpl_image *b = bpm_from_rel(hdrl_image_get_image(coef),
2140 rel_coef_l, rel_coef_h, 1);
2144 cpl_image_multiply_scalar(b, pow(2, i));
2145 cpl_image_add(bpm, b);
2146 cpl_image_delete(b);
2153 else if (pval >= 0) {
2155 bpm = cpl_image_new(cpl_image_get_size_x(out_chi2),
2156 cpl_image_get_size_y(out_chi2), CPL_TYPE_INT);
2157 int *md = cpl_image_get_data_int(bpm);
2158 hdrl_data_t *cd = cpl_image_get_data(out_chi2);
2159 hdrl_data_t *dd = cpl_image_get_data(out_dof);
2160 for (
size_t i = 0; i < (size_t)cpl_image_get_size_x(out_chi2) *
2161 cpl_image_get_size_y(out_chi2);
2163 double pv = igamc(dd[i] / 2., cd[i] / 2.);
2164 md[i] = (pv < pval);
2171 hdrl_imagelist_delete(out_coef);
2172 cpl_image_delete(out_chi2);
2173 cpl_image_delete(out_dof);
2175 return cpl_error_get_code();
2192 int size = cpl_vector_get_size(ve);
2195 cpl_vector *sve = cpl_vector_duplicate(ve);
2196 double signal = cpl_vector_get_median(sve);
2197 cpl_vector_delete(sve);
2198 cpl_vector *ve1 = cpl_vector_extract(ve, 2, size - 3, 1);
2199 cpl_vector *ve2 = cpl_vector_extract(ve, 0, size - 5, 1);
2200 cpl_vector *ve3 = cpl_vector_extract(ve, 4, size - 1, 1);
2201 int size1 = cpl_vector_get_size(ve1);
2203 for (
int i = 0; i < size1; i++) {
2204 double v1 = cpl_vector_get(ve1, i);
2205 double v2 = cpl_vector_get(ve2, i);
2206 double v3 = cpl_vector_get(ve3, i);
2207 double v = fabs(2 * v1 - v2 - v3);
2208 cpl_vector_set(ve1, i, v);
2210 double noise = 0.6052697 * cpl_vector_get_median(ve1);
2212 snr = signal / noise;
2214 cpl_vector_delete(ve1);
2215 cpl_vector_delete(ve2);
2216 cpl_vector_delete(ve3);
2236 cpl_ensure(a != NULL, CPL_ERROR_NULL_INPUT, 0);
2237 cpl_ensure(b != NULL, CPL_ERROR_NULL_INPUT, 0);
2239 int sizea = strlen(a);
2240 int sizeb = strlen(b);
2242 if (sizea != sizeb) {
2247 for (
int i = 0; i < sizea; i++) {
2258moo_get_date_from_string(
const char *
string)
2261 int year, month, day, hour, min;
2263 int nb = sscanf(
string,
"%d-%d-%dT%d:%d:%f", &year, &month, &day, &hour,
2289 int nrow = cpl_table_get_nrow(table);
2291 const char **names = cpl_table_get_data_string_const(table, name);
2292 for (
int i = 0; i < nrow; i++) {
2293 if (!cpl_table_is_selected(table, i)) {
2295 cpl_table_select_row(table, i);
2321 char *regexp = cpl_sprintf(
"^%s$",
string);
2322 int size = cpl_table_and_selected_string(table, name, CPL_EQUAL_TO, regexp);
2331 cpl_ensure(v!=NULL,CPL_ERROR_NULL_INPUT,NULL);
2332 cpl_ensure(winhsize>=0,CPL_ERROR_ILLEGAL_INPUT,NULL);
2334 cpl_vector* result = NULL;
2335 int size = cpl_vector_get_size(v);
2336 int K = winhsize*2+1;
2339 gsl_filter_median_workspace *median_p = gsl_filter_median_alloc(K);
2340 gsl_vector *x = gsl_vector_alloc(size);
2341 gsl_vector *y_median = gsl_vector_alloc(size);
2344 for (
int i = 0; i < size; ++i)
2346 double val = cpl_vector_get(v,i);
2347 gsl_vector_set(x, i, val);
2350 gsl_filter_median(GSL_FILTER_END_PADZERO, x, y_median, median_p);
2352 result = cpl_vector_new(size);
2354 for (
int i = 0; i < size; ++i)
2356 double val = gsl_vector_get(y_median,i);
2357 cpl_vector_set(result, i, val);
2361 gsl_vector_free(y_median);
2362 gsl_filter_median_free(median_p);
cpl_vector * moo_savgol_filter(cpl_vector *v, int window_length, int polyorder)
Apply a Savitzky-Golay filter to a vector.
double moo_sky_distance(double alpha1, double delta1, double alpha2, double delta2)
Compute sky distance (in rad)
cpl_error_code moo_gaussian_eval_inv(double y, double x0, double sigma, double offset, double area, double *x1, double *x2)
Find the x positions of the gaussian at the given y position.
cpl_size moo_table_or_selected_sequal_string(cpl_table *table, const char *name, const char *string)
Select from unselected table rows, by comparing column values with a constant.
hdrl_image * moo_image_collapse_median_create(hdrl_image *image)
Collapse row of an image using a median and compute associate error.
double moo_vector_get_min(const cpl_vector *v, int *flags)
Find minimum values in a vector using flags.
cpl_bivector * moo_bivector_filter_nan(cpl_bivector *v)
Create new bi vector with nan values filter.
double moo_gaussian_eval(double x, double x0, double sigma, double offset, double area)
Evaluate the gaussian at the given x position.
moo_tcheby_polynomial * moo_tcheby_polynomial_fit(cpl_bivector *data, int degree, double xmin, double xmax)
Computes Tchebitchev transformation of data.
cpl_mask * moo_kappa_sigma_clipping(cpl_image *sigma_img, int niter, double kappa, double cdiff, double maxfrac)
Compute mask of rejected pixels using kappa sigma algorithm.
cpl_error_code moo_gaussian_fit(cpl_bivector *points, cpl_fit_mode fit_pars, double *center, double *width, double *background, double *area)
Fit the data with a gaussian.
cpl_vector * moo_vector_filter_nan(cpl_vector *v)
Create new vector with nan values filter.
cpl_image * moo_imagelist_collapse_bitwiseor(cpl_imagelist *list, hdrl_imagelist *datalist)
get the QUAL resulting in a bitwise OR operation on the QUAL list
cpl_size moo_table_and_selected_sequal_string(cpl_table *table, const char *name, const char *string)
Select from unselected table rows, by comparing column values with a constant.
cpl_error_code moo_find_threshold_limits(cpl_bivector *points, double threshold, double *xmin, double *xmax)
Find threshold limits of a 1D signal.
moo_tcheby2d_polynomial * moo_tcheby2d_polynomial_fit(cpl_vector *in_x, int xdegree, double xmin, double xmax, cpl_vector *in_y, int ydegree, double ymin, double ymax, cpl_vector *in_l, double lmin, double lmax)
Computes Tchebitchev transformation of data.
double moo_image_get_ron(cpl_image *diff, int llx, int lly, int urx, int ury, int nb_boxes, int box_hsize, double max_error_frac, int max_niter)
compute ron in a diff image using boxes
double moo_vector_get_max(const cpl_vector *v, int *flags)
Find maximum values in a vector using flags.
const char * moo_get_license(void)
Get the pipeline copyright and license.
cpl_error_code moo_fit_mul(const cpl_vector *vx, const cpl_vector *vw, const cpl_vector *vy, const cpl_vector *vy_err, double *c, double *sig_c)
This function computes the best-fit linear regression coefficient c1 of the model Y = c_1 X for the w...
cpl_error_code moo_interpolate_linear(cpl_bivector *fout, cpl_vector *fout_errs, int *fout_qual, const cpl_bivector *fref, const cpl_vector *fref_errs, const int *fref_qual)
Linear interpolation of a 1d-function.
cpl_error_code moo_tchebychev_fit(cpl_bivector *data, int *flag, int degree, double xmin, double xmax, double ymin, double ymax)
Computes Tchebitchev transformation of data.
cpl_vector * moo_median_filter(cpl_vector *v, int winhsize)
Apply a median filter to a vector.
double moo_vector_get_dersnr(const cpl_vector *ve)
This function computes the signal to noise ratio DER_SNR following the definition set forth by the Sp...
cpl_error_code moo_image_get_quartile(cpl_image *image, double *qmin, double *qmax)
compute first and last quartile from an image
double moo_vector_get_percentile(cpl_vector *v, double f)
Get percentile of input vector.
cpl_error_code moo_barycenter_fit(cpl_bivector *points, double *center, double *width)
Fit positions using weighted fluxes.
int moo_string_is_strictly_equal(const char *a, const char *b)
This function compares to string to see if the two string are stricly equal.
cpl_image * moo_compute_sigma_map(cpl_imagelist *list, cpl_imagelist *qlist, cpl_image *img)
Compute image of sigma variation.