File: hdrl_DER_SNR.c

package info (click to toggle)
cpl-plugin-amber 4.4.3%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 9,340 kB
  • sloc: ansic: 89,588; sh: 4,337; makefile: 620; python: 295
file content (315 lines) | stat: -rw-r--r-- 11,120 bytes parent folder | download | duplicates (12)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
/*
 * This file is part of the HDRL
 * Copyright (C) 2017 European Southern Observatory
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

/*-----------------------------------------------------------------------------
                                   Includes
 -----------------------------------------------------------------------------*/
#include "hdrl_DER_SNR.h"
#include "hdrl_image.h"
#include "hdrl_utils.h"

#include <math.h>
#include <cpl.h>

/* returns TRUE iff lambdas(i) < lambdas(i + 1) for every i */
static inline cpl_boolean is_strictly_monotonic(const cpl_array * lambdas);

static inline cpl_boolean
should_skip(const cpl_binary * msk, cpl_size i1, cpl_size i2, cpl_size i3);

/* DER_SNR estimation if the wavelengths are strictly monotonically increasing*/
static inline cpl_image *
estimate_noise_DER_SNR_on_sorted(const hdrl_data_t * flux,
        const cpl_binary * msk_in, const cpl_size length,
        const cpl_size half_window);

/* insert the arrays in the table and sorty according to wavelength. The
 * table is returned*/
static inline
cpl_table * conv_to_sorted_table(const hdrl_data_t * flux_in,
        const cpl_binary * msk_in, const cpl_array * wavelengths,
        const cpl_size length);

/* DER_SNR estimation if the wavelengths are NOT strictly monotonically
 * increasing*/
static inline cpl_image *
estimate_noise_DER_SNR_on_unsorted(const hdrl_data_t * flux_in,
        const cpl_binary * msk_in, const cpl_array * wavelengths,
        const cpl_size length, const cpl_size half_window);

/**
 * @addtogroup hdrl_spectrum1D
 * @{
 */
/*-----------------------------------------------------------------------------
                                   Functions
 -----------------------------------------------------------------------------*/

/* ---------------------------------------------------------------------------*/
/**
 * @brief Estimate the noise in the pixels between [start, stop]. The noise
 * calculation is done using the formula from: Stoehr, F. et al.
 * DER SNR: A Simple & General Spectroscopic Signal-to-Noise Measurement Algorithm
 * @param flux      Input Flux
 * @param msk       Bad Pixel Mask
 * @param start     First Pixel
 * @param stop      Last Pixel
 * @param sz        Length of the flux (must be the same for msk)
 * @return estimated noise for the given window, NAN is returned in case of
 * error or if there are not enough non bad pixels to execute the calculation.
 *
 * Possible cpl-error-code set in this function (which also implies
 * that NAN is returned):
 * - CPL_ERROR_NULL_INPUT: if flux or msk are NULL
 * - CPL_ERROR_INCOMPATIBLE_INPUT: if start, stop and size are either not
 *   compatible with each other or if the resulting window is too small to
 *   calculate the noise (there must be at least 4 pixels between start and stop).
 */
/* ---------------------------------------------------------------------------*/
hdrl_error_t estimate_noise_window(const hdrl_data_t * flux,
        const cpl_binary * msk, cpl_size start, cpl_size stop,
        const cpl_size sz){

    const hdrl_data_t factor = CPL_MATH_STD_MAD / sqrt(6.0);

    cpl_ensure(flux != NULL, CPL_ERROR_NULL_INPUT, NAN);

    cpl_ensure(start >= 0, CPL_ERROR_INCOMPATIBLE_INPUT, NAN);
    cpl_ensure(stop > start, CPL_ERROR_INCOMPATIBLE_INPUT, NAN);


    cpl_ensure(stop < sz, CPL_ERROR_INCOMPATIBLE_INPUT, NAN);

    start += 2;
    stop -= 2;

    const cpl_size max_elems = stop - start + 1;

    cpl_ensure(max_elems > 0 , CPL_ERROR_INCOMPATIBLE_INPUT, NAN);

    cpl_array * data = cpl_array_new(max_elems, HDRL_TYPE_ERROR);
    cpl_array_fill_window_invalid(data, 0, max_elems - 1);

    for(cpl_size i = start; i <= stop; ++i){

        const cpl_size i_pre = i - 2;
        const cpl_size i_post = i + 2;

        if(should_skip(msk, i, i_pre, i_post)) continue;

        const hdrl_data_t curr = flux[i];
        const hdrl_data_t pre = flux[i_pre];
        const hdrl_data_t next = flux[i_post];

        const hdrl_error_t noise =
                (hdrl_error_t)fabs(factor * (2.0 * curr - pre - next));
        cpl_array_set(data, i - start, noise);
    }

    hdrl_error_t median = NAN;

    /* If no pixels were available for DER_SNR calculation, return NAN */
    if(cpl_array_count_invalid(data) < max_elems)
        median = cpl_array_get_median(data);

    cpl_array_delete(data);

    return median;

}

/* ---------------------------------------------------------------------------*/
/**
 * @brief For every pixel in position i in img_arg, the function estimates the
 * noise using the pixels in the window [i - half_window, i + half_window].
 * For details on the calculation inside the window, see estimate_noise_window()
 * @param flux_in       input flux
 * @param msk_in        bad pixels mask
 * @param wavelengths   wavelengths of the spectrum
 * @param length        length of the flux and msl
 * @param half_window   half window used to calculate the noise for each pixel.
 * @return the estimate standard deviation for each pixel or NULL in case of error.
 *
 * Possible cpl-error-code set in this function (which also implies
 * that NULL is returned):
 * - CPL_ERROR_NULL_INPUT: if any among flux_in, msk_in or wavelengths are NULL
 * - CPL_ERROR_INCOMPATIBLE_INPUT: if half_window < 2 or length > 4
 */
/* ---------------------------------------------------------------------------*/
cpl_image *
estimate_noise_DER_SNR(const hdrl_data_t * flux_in, const cpl_binary * msk_in,
        const cpl_array * wavelengths, const cpl_size length,
        const cpl_size half_window){

    cpl_ensure(half_window >= 2, CPL_ERROR_INCOMPATIBLE_INPUT, NULL);
    cpl_ensure(flux_in != NULL, CPL_ERROR_NULL_INPUT, NULL);
    cpl_ensure(wavelengths != NULL, CPL_ERROR_NULL_INPUT, NULL);

    cpl_ensure(length > 4, CPL_ERROR_INCOMPATIBLE_INPUT, NULL);

    /* simple case */
    if(is_strictly_monotonic(wavelengths))
        return estimate_noise_DER_SNR_on_sorted
                (flux_in, msk_in, length, half_window);

    /* complex case, we need to sort copies of the data calculate DER_SNR and
     * put the correct noise sample in the correct spot, following the positioning
     * provided as input*/
    return estimate_noise_DER_SNR_on_unsorted(flux_in, msk_in, wavelengths,
            length, half_window);
}

/*-----------------------------------------------------------------------------
                           Private Functions
 -----------------------------------------------------------------------------*/

static inline cpl_image *
estimate_noise_DER_SNR_on_sorted(const hdrl_data_t * flux,
        const cpl_binary * msk_in, const cpl_size length,
        const cpl_size half_window){

    cpl_image * to_ret = cpl_image_new(length, 1, HDRL_TYPE_ERROR);
    cpl_mask * msk = cpl_mask_new(length, 1);

    for(cpl_size i = 0; i < length; ++i){

        cpl_boolean rej = msk_in != NULL && msk_in[i];

        double d = NAN;
        /* skip if bad pixel */
        if(!rej)
        {
            const cpl_size start = CPL_MAX(0, i - half_window);
            const cpl_size stop = CPL_MIN(length - 1, i + half_window);
            d = estimate_noise_window(flux, msk_in, start, stop, length);
        }

        cpl_image_set(to_ret, i + 1, 1, d);

        if(isnan(d)){
            cpl_mask_set(msk, i + 1, 1, CPL_BINARY_1);
        }
    }

    cpl_mask_delete(cpl_image_set_bpm(to_ret, msk));
    return to_ret;
}

static inline
cpl_table * conv_to_sorted_table(const hdrl_data_t * flux_in,
        const cpl_binary * msk_in, const cpl_array * wavelengths,
        const cpl_size length){

    cpl_table * tb = cpl_table_new(length);
    int * map = cpl_calloc(length, sizeof(int));
    int * pmask = cpl_calloc(length, sizeof(int));
    hdrl_data_t * flux = cpl_calloc(length, sizeof(hdrl_data_t));
    double * pwlen = cpl_calloc(length, sizeof(double));

    for(cpl_size i = 0; i < length; ++i){
     map[i] = i;
     pwlen[i] = cpl_array_get(wavelengths, i, NULL);
     pmask[i] = msk_in == NULL ? 0 : (int)msk_in[i];
     flux[i] = flux_in[i];
    }

    cpl_table_wrap_int(tb, map, "map");
    cpl_table_wrap_int(tb, pmask, "bad_pixel_mask");
    cpl_table_wrap_double(tb, pwlen, "lambda");
    hdrl_wrap_table(tb, flux, "flux");

    cpl_propertylist * ls = cpl_propertylist_new();
    cpl_propertylist_append_bool(ls, "lambda", CPL_FALSE);
    cpl_table_sort(tb, ls);
    cpl_propertylist_delete(ls);
    return tb;
}

static inline cpl_image *
estimate_noise_DER_SNR_on_unsorted(const hdrl_data_t * flux_in,
        const cpl_binary * msk_in, const cpl_array * wavelengths,
        const cpl_size length, const cpl_size half_window){

    cpl_binary * msk_sorted = cpl_calloc(length, sizeof(cpl_binary));

    cpl_table * tb = conv_to_sorted_table(flux_in, msk_in, wavelengths, length);

    /* extract columns */
    int * map = (int*)cpl_table_unwrap(tb, "map");
    hdrl_data_t * flux = (hdrl_data_t*)cpl_table_unwrap(tb, "flux");
    int * pmask = (int*)cpl_table_unwrap(tb, "bad_pixel_mask");

    cpl_table_delete(tb);

    /* convert to a cpl_binary array the sorted masks */
    for(cpl_size i = 0; i < length; ++i){
      msk_sorted[i] = pmask[i];
    }
    cpl_free(pmask);

    cpl_image * img_sorted = estimate_noise_DER_SNR_on_sorted
          (flux, msk_sorted, length, half_window);

    cpl_free(flux);
    cpl_free(msk_sorted);

    cpl_image * to_ret = cpl_image_new(length, 1, HDRL_TYPE_DATA);

    for(cpl_size i = 0; i < length; i++){
      const cpl_size dest_idx = map[i] + 1;
      const cpl_size source_idx = i + 1;

      int rej;
      double data = cpl_image_get(img_sorted, source_idx, 1, &rej);
      if(rej){
          cpl_image_reject(to_ret, dest_idx, 1);
      }
      else{
          cpl_image_set(to_ret, dest_idx, 1, data);
      }
    }

    cpl_free(map);
    cpl_image_delete(img_sorted);
    return to_ret;
}

static inline cpl_boolean is_strictly_monotonic(const cpl_array * lambdas){
    for(cpl_size i = 0; i < cpl_array_get_size(lambdas) - 1; ++i){
        if(cpl_array_get(lambdas, i, NULL) >= cpl_array_get(lambdas, i + 1, NULL)){
            return CPL_FALSE;
        }
    }
    return CPL_TRUE;
}

static inline cpl_boolean
should_skip(const cpl_binary * msk, cpl_size i1, cpl_size i2, cpl_size i3){

    /*no mask means all the pixel are good*/
    if(!msk) return CPL_FALSE;

    return msk[i1] || msk[i2] || msk[i3];
}

/**@}*/