X-shooter Pipeline Reference Manual 3.8.15
xsh_utils_wrappers.c
Go to the documentation of this file.
1/* *
2 * This file is part of the ESO X-shooter Pipeline *
3 * Copyright (C) 2006 European Southern Observatory *
4 * *
5 * This library is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the Free Software *
17 * Foundation, 51 Franklin St, Fifth Floor, Boston, MA 02111-1307 USA *
18 * */
19
20
21#include <xsh_utils_wrappers.h>
22#include <xsh_error.h>
23#include <xsh_msg.h>
24#include <math.h>
25
26
27
28
29static cpl_image *
30xsh_image_filter_wrapper(const cpl_image *b, const cpl_matrix *k, cpl_filter_mode mode)
31{
32 const double EPSILON = 1E-5;
33 int nx = cpl_image_get_size_x(b);
34 int ny = cpl_image_get_size_y(b);
35 int nrow = cpl_matrix_get_nrow(k);
36 int ncol = cpl_matrix_get_ncol(k);
37 int i, j;
38 cpl_type type = cpl_image_get_type(b);
39 cpl_image * a = cpl_image_new(nx, ny, type);
40 // where m is a cpl_mask with a CPL_BINARY_1 whereever k has a 1.0.
41 cpl_mask* m = cpl_mask_new(ncol, nrow);
42 /* commented out to speed up
43 xsh_msg_dbg_medium("nx[%d], ny[%d], ncol[%d], nrow[%d]", nx, ny, ncol, nrow);
44 */
45 for (i = 0; i < ncol ; i++)
46 {
47 for (j = 0; j < nrow ; j++)
48 {
49 double value = cpl_matrix_get(k, j, i);
50 if (fabs(value - 1.0) < EPSILON)
51 {
52 cpl_mask_set(m, i + 1, j + 1, CPL_BINARY_1);
53 }
54 }
55 }
56
57 cpl_image_filter_mask(a, b, m, mode, CPL_BORDER_FILTER);
58 cpl_mask_delete(m);
59 return a;
60 }
61
62cpl_image*
63xsh_image_filter_mode(const cpl_image* b,
64 const cpl_matrix * ker,
65 cpl_filter_mode filter)
66{
67 int nx = cpl_image_get_size_x(b);
68 int ny = cpl_image_get_size_y(b);
69 int type = cpl_image_get_type(b);
70 cpl_image * a = cpl_image_new(nx, ny, type);
71
72 switch(filter) {
73 case CPL_FILTER_MEDIAN:
74 check(cpl_image_filter(a, b, ker, CPL_FILTER_MEDIAN, CPL_BORDER_FILTER));
75 break;
76 case CPL_FILTER_LINEAR:
77 check(cpl_image_filter(a, b, ker, CPL_FILTER_LINEAR, CPL_BORDER_FILTER));
78 break;
79 case CPL_FILTER_STDEV:
80 cpl_image_filter(a, b, ker, CPL_FILTER_STDEV, CPL_BORDER_FILTER);
81 break;
82 case CPL_FILTER_MORPHO:
83 cpl_image_filter(a, b, ker, CPL_FILTER_MORPHO, CPL_BORDER_FILTER);
84 break;
85 default:
86 xsh_msg_error("Filter type not supported");
87 return NULL;
88 }
89 cleanup:
90
91 return a;
92
93}
94
95/*
96cpl_polynomial *
97xsh_polynomial_fit_1d_create(const cpl_vector * x_pos,
98 const cpl_vector * values,
99 int degree,
100 double * mse)
101{
102 cpl_polynomial * fit1d = cpl_polynomial_new(1);
103 cpl_matrix * samppos = cpl_matrix_wrap(1, cpl_vector_get_size(x_pos),
104 cpl_vector_get_data_const(x_pos));
105 cpl_vector * fitresidual = cpl_vector_new(cpl_vector_get_size(x_pos));
106 cpl_polynomial_fit(fit1d, samppos, NULL, values, NULL,
107 CPL_FALSE, NULL, &degree);
108
109 cpl_vector_fill_polynomial_fit_residual(fitresidual, values, NULL, fit1d,
110 samppos, NULL);
111 cpl_matrix_unwrap(samppos);
112 *mse = cpl_vector_product(fitresidual, fitresidual)
113 / cpl_vector_get_size(fitresidual);
114
115
116 return fit1d;
117
118}
119
120*/
121cpl_polynomial * xsh_polynomial_fit_2d_create(cpl_bivector * xy_pos,
122 cpl_vector * values,
123 cpl_size * degree,
124 double * mse)
125{
126 typedef double* (*get_data)(cpl_bivector*);
127 get_data data_extractor[2] = { &cpl_bivector_get_x_data, &cpl_bivector_get_y_data};
128 //samppos matrix must
129 // have two rows with copies of the two vectors in the x_pos bivector.
130
131 double rechisq = 0;
132 int i, j;
133 cpl_vector * fitresidual = 0;
134 cpl_matrix * samppos2d = 0;
135 cpl_polynomial * fit2d = cpl_polynomial_new(2);
136 int xy_size = cpl_bivector_get_size(xy_pos);
137
138 samppos2d = cpl_matrix_new(2, xy_size);
139 for (i = 0; i < 2; i++)
140 {
141 for (j = 0; j < xy_size; j++)
142 {
143 double value = data_extractor[i](xy_pos)[j];
144 cpl_matrix_set(samppos2d, i, j, value);
145 }
146 }
147
148 cpl_polynomial_fit(fit2d, samppos2d, NULL, values, NULL, CPL_FALSE,
149 NULL, degree);
150
151 fitresidual = cpl_vector_new(xy_size);
152 cpl_vector_fill_polynomial_fit_residual(fitresidual, values, NULL, fit2d,
153 samppos2d, &rechisq);
154 if (mse)
155 {
156 *mse = cpl_vector_product(fitresidual, fitresidual)
157 / cpl_vector_get_size(fitresidual);
158 }
159 cpl_matrix_delete(samppos2d);
160 cpl_vector_delete(fitresidual);
161 return fit2d;
162}
163
164cpl_polynomial *
166 const cpl_vector * x_pos,
167 const cpl_vector * values,
168 int degree,
169 double * mse
170 )
171{
172 cpl_polynomial * fit1d = cpl_polynomial_new(1);
173// cpl_vector* x_copy = cpl_vector_duplicate(x_pos);
174// cpl_vector* values_copy = cpl_vector_duplicate(values);
175 int x_size = cpl_vector_get_size(x_pos);
176 double rechisq = 0;
177 cpl_size loc_deg=(cpl_size)degree;
178 cpl_matrix * samppos = cpl_matrix_wrap(1, x_size,
179 (double*)cpl_vector_get_data_const(x_pos));
180 cpl_vector * fitresidual = cpl_vector_new(x_size);
181
182 cpl_polynomial_fit(fit1d, samppos, NULL, values, NULL,
183 CPL_FALSE, NULL, &loc_deg);
184 cpl_ensure(!cpl_error_get_code(), cpl_error_get_code(), NULL);
185
186 if ( x_size > (degree + 1) ) {
187 cpl_vector_fill_polynomial_fit_residual(fitresidual, values, NULL, fit1d,
188 samppos, &rechisq);
189 cpl_ensure(!cpl_error_get_code(), cpl_error_get_code(), NULL);
190 }
191 if (mse)
192 {
193 *mse = cpl_vector_product(fitresidual, fitresidual)
194 / cpl_vector_get_size(fitresidual);
195 }
196 cpl_matrix_unwrap(samppos);
197 cpl_vector_delete(fitresidual);
198 return fit1d;
199}
200
201
202cpl_image *
203xsh_image_filter_median(const cpl_image * img, const cpl_matrix * mx)
204{
205 return xsh_image_filter_wrapper(img, mx, CPL_FILTER_MEDIAN);
206}
207
208cpl_image *
209xsh_image_filter_linear(const cpl_image *img, const cpl_matrix * mx)
210{
211 return xsh_image_filter_mode(img, mx, CPL_FILTER_LINEAR);
212
213}
214
215/*---------------------------------------------------------------------------*/
static int degree
static char mode[32]
#define check(COMMAND)
Definition: xsh_error.h:71
#define xsh_msg_error(...)
Print an error message.
Definition: xsh_msg.h:62
int nx
int m
Definition: xsh_detmon_lg.c:91
int ny
int filter
cpl_polynomial * xsh_polynomial_fit_2d_create(cpl_bivector *xy_pos, cpl_vector *values, cpl_size *degree, double *mse)
cpl_image * xsh_image_filter_median(const cpl_image *img, const cpl_matrix *mx)
cpl_polynomial * xsh_polynomial_fit_1d_create(const cpl_vector *x_pos, const cpl_vector *values, int degree, double *mse)
static cpl_image * xsh_image_filter_wrapper(const cpl_image *b, const cpl_matrix *k, cpl_filter_mode mode)
cpl_image * xsh_image_filter_mode(const cpl_image *b, const cpl_matrix *ker, cpl_filter_mode filter)
cpl_image * xsh_image_filter_linear(const cpl_image *img, const cpl_matrix *mx)