X-shooter Pipeline Reference Manual 3.8.15
xsh_detmon.c
Go to the documentation of this file.
1/* $Id: xsh_detmon.c,v 1.11 2013-07-19 12:00:24 jtaylor Exp $
2 *
3 * This file is part of the irplib package
4 * Copyright (C) 2002, 2003 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 02111-1307 USA
19 */
20
21/*
22 * $Author: jtaylor $
23 * $Date: 2013-07-19 12:00:24 $
24 * $Revision: 1.11 $
25 * $Name: not supported by cvs2svn $
26 */
27
28#ifdef HAVE_CONFIG_H
29#include <config.h>
30#endif
31
32#include <complex.h>
33
34/*---------------------------------------------------------------------------
35 Includes
36 ---------------------------------------------------------------------------*/
37
38#include <math.h>
39#include <string.h>
40#include <assert.h>
41#include <float.h>
42
43#include <cpl.h>
44
45#include "xsh_detmon.h"
46
47#include "xsh_ksigma_clip.h"
48//#include "irplib_hist.h"
49#include "irplib_utils.h"
50
51
52/* Computes the square of an euclidean distance bet. 2 points */
53#define pdist(x1,y1,x2,y2) (((x1-x2)*(x1-x2))+((y1-y2)*(y1-y2)))
54
55#define cpl_drand() ((double)rand()/(double)RAND_MAX)
57/*--------------------------------------------------------------------------*/
58
59/*
60 * @defgroup xsh_detmon Detector monitoring functions
61 */
62
63/*--------------------------------------------------------------------------*/
64
65/*---------------------------------------------------------------------------
66 Defines
67 ---------------------------------------------------------------------------*/
68
69
70#define HIST_FACT 2.354820045
71
73{
74 HOT = 0,
75 DEAD = 1,
76 NOISY = 2
77};
78
80{
81 MINMAX = 0,
82 MEAN = 1,
83 MEDIAN = 2,
84 KSIGMA = 3
85};
86
88{
90 VERTICAL = 2
91};
92
93
94static struct
95{
96 const char * ron_method;
97 const char * dsnu_method;
98 int exts;
100 cpl_boolean opt_nir;
102
103#define NIR TRUE
104#define OPT FALSE
105
106/*---------------------------------------------------------------------------
107 Private function prototypes
108 ---------------------------------------------------------------------------*/
109
110/* Functions for RON/Bias recipe, xsh_detmon_ronbias() */
111
112cpl_error_code
113xsh_detmon_rm_bpixs(cpl_image **,
114 const double,
115 int ,
116 int );
117
118/*---------------------------------------------------------------------------*/
119
120/*
121 * @brief Fill input parameters values
122 * @param parlist parameters list
123 * @param recipe_name recipe name
124 * @param pipeline_name pipeline name
125 * @param npars number of parameters
126
127 * @return CPL_ERROR_NONE on success.
128 */
129
130/*---------------------------------------------------------------------------*/
131cpl_error_code
132xsh_detmon_fill_parlist(cpl_parameterlist * parlist,
133 const char *recipe_name,
134 const char *pipeline_name,
135 int npars, ...)
136{
137
138 va_list ap;
139
140 char *group_name;
141
142 int pars_counter = 0;
143
144 group_name = cpl_sprintf("%s.%s", pipeline_name, recipe_name);
145 assert(group_name != NULL);
146
147#define insert_par(PARNAME, PARDESC, PARVALUE, PARTYPE) \
148 do { \
149 char * par_name = cpl_sprintf("%s.%s", group_name, PARNAME); \
150 cpl_parameter * p; \
151 assert(par_name != NULL); \
152 p = cpl_parameter_new_value(par_name, PARTYPE, \
153 PARDESC, group_name, PARVALUE); \
154 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, PARNAME); \
155 cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV); \
156 cpl_parameterlist_append(parlist, p); \
157 cpl_free(par_name); \
158 } while(0);
159
160
161 va_start(ap, npars);
162
163 while(pars_counter < npars) {
164 char *name = va_arg(ap, char *);
165 char *desc = va_arg(ap, char *);
166 char *type = va_arg(ap, char *);
167
168 if(!strcmp(type, "CPL_TYPE_INT")) {
169 int v1 = va_arg(ap, int);
170
171 insert_par(name, desc, v1, CPL_TYPE_INT);
172 } else if(!strcmp(type, "CPL_TYPE_BOOL")) {
173 char *v2 = va_arg(ap, char *);
174
175 if(!strcmp(v2, "CPL_FALSE"))
176 insert_par(name, desc, CPL_FALSE, CPL_TYPE_BOOL);
177 if(!strcmp(v2, "CPL_TRUE"))
178 insert_par(name, desc, CPL_TRUE, CPL_TYPE_BOOL);
179 } else if(!strcmp(type, "CPL_TYPE_STRING")) {
180 char *v2 = va_arg(ap, char *);
181
182 insert_par(name, desc, v2, CPL_TYPE_STRING);
183 } else if(!strcmp(type, "CPL_TYPE_DOUBLE")) {
184 double v3 = va_arg(ap, double);
185 insert_par(name, desc, v3, CPL_TYPE_DOUBLE);
186 }
187
188 pars_counter++;
189 }
190
191 va_end(ap);
192
193 cpl_free(group_name);
194
195#undef insert_par
196 return 0;
197}
198
199/*---------------------------------------------------------------------------*/
200
201/*
202 * @brief Retrieve input int parameter
203 * @param pipeline_name Input image
204 * @param recipe_name Input image
205 * @param parlist Shift to apply on the x-axis
206 * @return CPL_ERROR_NONE on success.
207 */
208
209/*---------------------------------------------------------------------------*/
210int
212 const char *pipeline_name,
213 const char *recipe_name,
214 const cpl_parameterlist * parlist)
215{
216 char *par_name;
217 const cpl_parameter *par;
218 int value;
219
220 par_name = cpl_sprintf("%s.%s.%s", pipeline_name, recipe_name, parn);
221 assert(par_name != NULL);
222 par = cpl_parameterlist_find_const(parlist, par_name);
223 value = cpl_parameter_get_int(par);
224 cpl_free(par_name);
225
226 return value;
227}
228
229/*---------------------------------------------------------------------------*/
230
231/*
232 * @brief Retrieve input double parameter
233 * @param pipeline_name Input image
234 * @param recipe_name Input image
235 * @param parlist Shift to apply on the x-axis
236 * @return CPL_ERROR_NONE on success.
237 */
238
239/*---------------------------------------------------------------------------*/
240double
242 const char *pipeline_name,
243 const char *recipe_name,
244 const cpl_parameterlist * parlist)
245{
246 char *par_name;
247 const cpl_parameter *par;
248 double value;
249
250 par_name = cpl_sprintf("%s.%s.%s", pipeline_name, recipe_name, parn);
251 assert(par_name != NULL);
252 par = cpl_parameterlist_find_const(parlist, par_name);
253 value = cpl_parameter_get_double(par);
254 cpl_free(par_name);
255
256 return value;
257}
258
259
260/*---------------------------------------------------------------------------*/
261
262/*
263 * @brief Retrieve exposure time
264 * @param plist parameter list
265 * @return "EXPTIME" keyword value.
266 */
267
268/*---------------------------------------------------------------------------*/
269
270double
271irplib_pfits_get_exptime(const cpl_propertylist * plist)
272{
273 double exptime;
274
275 exptime = cpl_propertylist_get_double(plist, "EXPTIME");
276
277 return exptime;
278}
279
280/*---------------------------------------------------------------------------*/
281/*
282 * @brief Generate propertylist with product category, type, technique
283 * @param procatg product category
284 * @param protype product type
285 * @param protech product technique
286 * @param proscience switch to identify a science product
287 * @return filled cpl_propertylist (to be deallocated)
288 */
289/*---------------------------------------------------------------------------*/
290cpl_propertylist *
291xsh_detmon_fill_prolist(const char * procatg,
292 const char * protype,
293 const char * protech,
294 cpl_boolean proscience)
295{
296 cpl_propertylist * prolist = cpl_propertylist_new();
297
298 cpl_propertylist_append_string(prolist, CPL_DFS_PRO_CATG, procatg);
299 cpl_propertylist_append_bool(prolist, CPL_DFS_PRO_SCIENCE, proscience);
300 if (protype) {
301 cpl_propertylist_append_string(prolist, CPL_DFS_PRO_TYPE, protype);
302 }
303 if (protech) {
304 cpl_propertylist_append_string(prolist, CPL_DFS_PRO_TECH, protech);
305 }
306
307 return prolist;
308}
309
310/*---------------------------------------------------------------------------*/
311
312/*
313 * @brief Remove bad pixels
314 * @param image input/output image
315 * @param kappa kappa for kappa-sigma clip
316 * @param nffts number of fft
317 * @param nsamples number of sampling areas
318 * @return CPL_ERROR_NONE on success.
319 */
320
321/*---------------------------------------------------------------------------*/
322
323
324cpl_error_code
325xsh_detmon_rm_bpixs(cpl_image ** image,
326 const double kappa, int nffts, int nsamples)
327{
328 int i, j;
329
330 float *data = cpl_image_get_data_float(*image);
331 int k = 0;
332 for(i = 0; i < nffts; i++) {
333 for(j = 0; j < nsamples; j++) {
334 float neighbours = 0;
335 int nneighs = 0;
336 float average = 0;
337
338 /*
339 * Look for the way to optimize this:
340 * Some of the points added to neighbours coincide
341 * in one iteration and the following
342 */
343 if(i > 0) {
344 neighbours += *(data + (i - 1) * nsamples + j);
345 nneighs++;
346 }
347 if(i < nffts - 1) {
348 neighbours += *(data + (i + 1) * nsamples + j);
349 nneighs++;
350 }
351 if(j > 0) {
352 neighbours += *(data + i * nsamples + (j - 1));
353 nneighs++;
354 }
355 if(j < nsamples - 1) {
356 neighbours += *(data + i * nsamples + (j + 1));
357 nneighs++;
358 }
359 average = neighbours / nneighs;
360 if(average > 0) {
361 if(*(data + i * nsamples + j) < average * (-1 * kappa) ||
362 *(data + i * nsamples + j) > average * (kappa)) {
363 k++;
364 *(data + i * nsamples + j) = average;
365 }
366 }
367 if(average < 0) {
368 if(*(data + i * nsamples + j) > average * (-1 * kappa) ||
369 *(data + i * nsamples + j) < average * (kappa)) {
370 k++;
371 *(data + i * nsamples + j) = average;
372 }
373 }
374
375 }
376 }
377
378
379 return cpl_error_get_code();
380
381}
382
383void
385 if(cpl_error_get_code() != CPL_ERROR_NONE) {
386 /* cpl_msg_error(cpl_func,"Function status at %d",val); */
387 cpl_msg_error(cpl_func,"%s",(const char*) cpl_error_get_message());
388 cpl_msg_error(cpl_func,"%s",(const char*) cpl_error_get_where());
389 return;
390 }
391 return;
392}
393
static double exptime
cpl_error_code xsh_detmon_rm_bpixs(cpl_image **, const double, int, int)
Definition: xsh_detmon.c:325
readouts
Definition: xsh_detmon.c:88
@ VERTICAL
Definition: xsh_detmon.c:90
@ HORIZONTAL
Definition: xsh_detmon.c:89
const char * ron_method
Definition: xsh_detmon.c:96
double xsh_detmon_retrieve_par_double(const char *parn, const char *pipeline_name, const char *recipe_name, const cpl_parameterlist *parlist)
Definition: xsh_detmon.c:241
pixeltypes
Definition: xsh_detmon.c:73
@ DEAD
Definition: xsh_detmon.c:75
@ HOT
Definition: xsh_detmon.c:74
@ NOISY
Definition: xsh_detmon.c:76
cpl_boolean opt_nir
Definition: xsh_detmon.c:100
cpl_error_code xsh_detmon_fill_parlist(cpl_parameterlist *parlist, const char *recipe_name, const char *pipeline_name, int npars,...)
Definition: xsh_detmon.c:132
int exts
Definition: xsh_detmon.c:98
#define insert_par(PARNAME, PARDESC, PARVALUE, PARTYPE)
stackingtypes
Definition: xsh_detmon.c:80
@ KSIGMA
Definition: xsh_detmon.c:84
@ MINMAX
Definition: xsh_detmon.c:81
@ MEAN
Definition: xsh_detmon.c:82
@ MEDIAN
Definition: xsh_detmon.c:83
void detmon_print_rec_status(void)
Definition: xsh_detmon.c:384
const char * dsnu_method
Definition: xsh_detmon.c:97
int nb_extensions
Definition: xsh_detmon.c:99
static struct @1 xsh_detmon_dark_config
int xsh_detmon_retrieve_par_int(const char *parn, const char *pipeline_name, const char *recipe_name, const cpl_parameterlist *parlist)
Definition: xsh_detmon.c:211
cpl_propertylist * xsh_detmon_fill_prolist(const char *procatg, const char *protype, const char *protech, cpl_boolean proscience)
Definition: xsh_detmon.c:291
double irplib_pfits_get_exptime(const cpl_propertylist *plist)
Definition: xsh_detmon.c:271
double kappa
Definition: xsh_detmon_lg.c:81