CR2RE Pipeline Reference Manual 1.6.10
hdrl_maglim.c
1/* $Id: hdrl_maglim.c,v 1.15 2013-09-24 14:58:54 jtaylor Exp $
2 *
3 * This file is part of the HDRL
4 * Copyright (C) 2013 European Southern Observatory
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21#ifdef HAVE_CONFIG_H
22#include <config.h>
23#endif
24
25/*-----------------------------------------------------------------------------
26 Includes
27-----------------------------------------------------------------------------*/
28
29#include "hdrl_maglim.h"
30#include <cpl.h>
31#include <math.h>
32
33/*-----------------------------------------------------------------------------
34 Static
35 -----------------------------------------------------------------------------*/
36
37/*----------------------------------------------------------------------------*/
57/*----------------------------------------------------------------------------*/
58
66/*----------------------------------------------------------------------------*/
111/*----------------------------------------------------------------------------*/
112
113cpl_error_code
114hdrl_maglim_compute(const cpl_image * image, const double zeropoint,
115 const double fwhm, const cpl_size kernel_size_x,
116 const cpl_size kernel_size_y,
117 const hdrl_image_extend_method image_extend_method,
118 const hdrl_parameter * mode_parameter,
119 double* limiting_magnitude)
120{
121 cpl_error_ensure(fwhm > 0, CPL_ERROR_ILLEGAL_INPUT, return
122 CPL_ERROR_ILLEGAL_INPUT, "fwhm must be > 0");
123
124 cpl_error_ensure(kernel_size_x > 0, CPL_ERROR_ILLEGAL_INPUT, return
125 CPL_ERROR_ILLEGAL_INPUT, "kernel_size_x must be > 0");
126
127 cpl_error_ensure(kernel_size_y > 0, CPL_ERROR_ILLEGAL_INPUT, return
128 CPL_ERROR_ILLEGAL_INPUT, "kernel_size_y must be > 0");
129
130 cpl_error_ensure(image_extend_method == HDRL_IMAGE_EXTEND_MIRROR ||
131 image_extend_method == HDRL_IMAGE_EXTEND_NEAREST,
132 CPL_ERROR_ILLEGAL_INPUT, return CPL_ERROR_ILLEGAL_INPUT,
133 "image extension method can be 'HDRL_IMAGE_EXTEND_MIRROR'"
134 "or 'HDRL_IMAGE_EXTEND_NEAREST' only");
135
136 cpl_error_ensure(hdrl_collapse_parameter_is_mode(mode_parameter),
137 CPL_ERROR_INCOMPATIBLE_INPUT, return
138 CPL_ERROR_INCOMPATIBLE_INPUT,
139 "Not a mode parameter");
140
141 cpl_error_ensure((kernel_size_x % 2 != 0) && (kernel_size_y % 2 != 0),
142 CPL_ERROR_INCOMPATIBLE_INPUT,
143 return CPL_ERROR_INCOMPATIBLE_INPUT,
144 "The size of the convolution kernel must be odd in x and y");
145
146 cpl_msg_debug(cpl_func, "Convolution kernel: X size: %lld Y size: %lld, "
147 "FWHM: %16.14g", kernel_size_x, kernel_size_y, fwhm);
148
149 /* build convolution kernel */
150 cpl_matrix * kernel = hdrl_maglim_kernel_create(kernel_size_x, kernel_size_y,
151 fwhm);
152 /* Convolve the image */
153 cpl_image * ima_convolved = hdrl_image_convolve(image, kernel,
154 image_extend_method);
155
156 cpl_matrix_delete(kernel);
157
158 // compute the mode of the image
159 hdrl_image* hima = hdrl_image_create(ima_convolved, NULL);
160 cpl_image_delete(ima_convolved);
161 double histo_min = hdrl_collapse_mode_parameter_get_histo_min(mode_parameter);
162 double histo_max = hdrl_collapse_mode_parameter_get_histo_max(mode_parameter);
163 double bin_size = hdrl_collapse_mode_parameter_get_bin_size(mode_parameter);
164 hdrl_mode_type mode_method =
166
167 /*
168 * We do not need the error here as the limiting magnitude algorithm does not
169 * deliver any error
170 * cpl_size error_niter = hdrl_collapse_mode_parameter_get_error_niter(mode_parameter);
171 * */
172
173 cpl_size error_niter = 0;
174
175 hdrl_value hmode = hdrl_image_get_mode(hima, histo_min, histo_max, bin_size,
176 mode_method, error_niter);
177
178 double mode = hmode.data;
179
180 double norm = 1;
181 cpl_msg_debug(cpl_func,"Computing noise and limiting magnitude ...");
182
183
184 /*
185 * The value CPL_BINARY_1 is assigned where the pixel value is not marked
186 * as rejected and is strictly inside the provided interval. The other
187 * positions are assigned CPL_BINARY_0
188*/
189 cpl_mask* bpm = cpl_mask_threshold_image_create(hdrl_image_get_image(hima),
190 mode, DBL_MAX);
191
192 /* Adding the original bad pixels if present */
193 cpl_mask * hbpm = hdrl_image_get_mask(hima);
194 cpl_mask_or(bpm, hbpm);
196 cpl_mask_delete(bpm);
197
198 double arg = (1. -2. / CPL_MATH_PI);
199 double correction_factor = 1. / sqrt(arg);
200 double mad = 0.;
201 cpl_image_get_mad(hdrl_image_get_image_const(hima), &mad);
202 if (mad <= 0) {
203 mad = nextafter(0,1.0);
204 }
205 double noise = mad * CPL_MATH_STD_MAD * correction_factor;
206
207 double factor = (fwhm / CPL_MATH_FWHM_SIG );
208 factor *= factor;
209 norm = 4. * CPL_MATH_PI * factor;
210
211 *limiting_magnitude = -2.5 * log10(5. * noise * norm) + zeropoint;
212
213 cpl_msg_debug(cpl_func,"Computed values: M.A.D. %g std (from M.A.D.) %g "
214 "correction_factor %g norm %g", mad, mad * CPL_MATH_STD_MAD,
215 correction_factor, norm);
216 cpl_msg_debug(cpl_func,"Computed values: mode %16.14g stdev %16.14g "
217 "correction_factor %16.14g noise %16.14g Limiting Magnitude %10.7g", mode,
218 hdrl_image_get_stdev(hima), correction_factor, noise,
219 *limiting_magnitude);
220
221 hdrl_image_delete(hima);
222
223 return cpl_error_get_code();
224}
225
226
double hdrl_collapse_mode_parameter_get_bin_size(const hdrl_parameter *p)
get size of the histogram bins
double hdrl_collapse_mode_parameter_get_histo_min(const hdrl_parameter *p)
get min value
double hdrl_collapse_mode_parameter_get_histo_max(const hdrl_parameter *p)
get high value
cpl_boolean hdrl_collapse_parameter_is_mode(const hdrl_parameter *self)
check if parameter is a mode parameter
hdrl_mode_type hdrl_collapse_mode_parameter_get_method(const hdrl_parameter *p)
get the mode determination method
cpl_error_code hdrl_image_reject_from_mask(hdrl_image *self, const cpl_mask *map)
set bpm of hdrl_image
Definition: hdrl_image.c:407
hdrl_value hdrl_image_get_mode(const hdrl_image *self, double histo_min, double histo_max, double bin_size, hdrl_mode_type method, cpl_size error_niter)
computes the mode and the associated error of an image.
double hdrl_image_get_stdev(const hdrl_image *self)
computes the standard deviation of the data of an image
cpl_mask * hdrl_image_get_mask(hdrl_image *himg)
get cpl bad pixel mask from image
Definition: hdrl_image.c:157
hdrl_image * hdrl_image_create(const cpl_image *image, const cpl_image *error)
create a new hdrl_image from to existing images by copying them
Definition: hdrl_image.c:295
cpl_image * hdrl_image_get_image(hdrl_image *himg)
get data as cpl image
Definition: hdrl_image.c:105
const cpl_image * hdrl_image_get_image_const(const hdrl_image *himg)
get data as cpl image
Definition: hdrl_image.c:118
void hdrl_image_delete(hdrl_image *himg)
delete hdrl_image
Definition: hdrl_image.c:379
cpl_error_code hdrl_maglim_compute(const cpl_image *image, const double zeropoint, const double fwhm, const cpl_size kernel_size_x, const cpl_size kernel_size_y, const hdrl_image_extend_method image_extend_method, const hdrl_parameter *mode_parameter, double *limiting_magnitude)
Computes the limiting magnitude of an image.
Definition: hdrl_maglim.c:114