VISIR Pipeline Reference Manual  4.1.0
visir_util_destripe.c
1 /* $Id: visir_util_destripe.c,v 1.9 2013-05-14 12:59:23 jtaylor Exp $
2  *
3  * This file is part of the VISIR Pipeline
4  * Copyright (C) 2012 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-05-14 12:59:23 $
24  * $Revision: 1.9 $
25  * $Name: not supported by cvs2svn $
26  */
27 
28 #ifdef HAVE_CONFIG_H
29 #include <config.h>
30 #endif
31 
32 /*-----------------------------------------------------------------------------
33  Includes
34  -----------------------------------------------------------------------------*/
35 
36 #include "visir_recipe.h"
37 
38 #include "visir_spc_distortion.h"
39 #include "visir_destripe.h"
40 #ifdef _OPENMP
41 #include <omp.h>
42 #endif
43 
44 /*-----------------------------------------------------------------------------
45  Defines
46  -----------------------------------------------------------------------------*/
47 
48 #define RECIPE_STRING "visir_old_util_destripe"
49 // FIXME:
50 #define VISIR_UTIL_DESTRIPE_PROCATG "DESTRIPED"
51 
52 /*-----------------------------------------------------------------------------
53  Private Functions prototypes
54  -----------------------------------------------------------------------------*/
55 static
56 cpl_error_code visir_util_destripe_one(cpl_frameset *,
57  irplib_framelist *, int,
58  const cpl_parameterlist *,
59  const int,
60  const cpl_boolean);
61 
62 #ifdef VISIR_CHAIN
63 #define cpl_plugin_get_info visir_old_util_destripe_get_info
64 #endif
65 cpl_recipe_define(visir_old_util_destripe, VISIR_BINARY_VERSION,
66  "Julian Taylor", PACKAGE_BUGREPORT, "2012",
67  "Old DRS detector: Attempt to remove stripes in spectral data",
68  "The files listed in the Set Of Frames (sof-file) "
69  "must be tagged:\n"
70  "VISIR-chopnod-corrected-file.fits " VISIR_UTIL_CORRECTED
71  "\nThe product(s) will have a FITS card\n"
72  "'HIERARCH ESO PRO CATG' with a value of:\n"
73  VISIR_UTIL_DESTRIPE_PROCATG);
74 
75 /*----------------------------------------------------------------------------*/
79 /*----------------------------------------------------------------------------*/
80 
81 /*-----------------------------------------------------------------------------
82  Functions code
83  -----------------------------------------------------------------------------*/
84 
85 
86 /*----------------------------------------------------------------------------*/
94 /*----------------------------------------------------------------------------*/
95 static cpl_error_code
96 visir_old_util_destripe_fill_parameterlist(cpl_parameterlist * self)
97 {
98 
99  return visir_parameter_set(self, RECIPE_STRING, VISIR_PARAM_STRIPITE |
100  VISIR_PARAM_STRIPMOR)
101  ? cpl_error_set_where(cpl_func) : CPL_ERROR_NONE;
102 }
103 
104 
105 /*----------------------------------------------------------------------------*/
112 /*----------------------------------------------------------------------------*/
113 static int visir_old_util_destripe(cpl_frameset * framelist,
114  const cpl_parameterlist * parlist)
115 {
116 #ifdef _OPENMP
117  cpl_errorstate cleanstate = cpl_errorstate_get();
118 #endif
119  cpl_error_code didfail = CPL_ERROR_NONE;
120  irplib_framelist * allframes = NULL;
121  irplib_framelist * rawframes = NULL;
122  int n;
123 
124 #ifdef _OPENMP
125  omp_set_num_threads(visir_get_num_threads(CPL_FALSE));
126 #endif
127 
128  skip_if (0);
129 
130  int ndestripe = visir_parameterlist_get_int(parlist, RECIPE_STRING,
131  VISIR_PARAM_STRIPITE);
132  bug_if (0);
133 
134  cpl_boolean morpho_destripe = ndestripe <= 0 ? CPL_FALSE :
135  visir_parameterlist_get_bool(parlist, RECIPE_STRING,
136  VISIR_PARAM_STRIPMOR);
137  bug_if (0);
138 
139  /* Objects observation */
140  allframes = irplib_framelist_cast(framelist);
141  skip_if(allframes == NULL);
142  rawframes = irplib_framelist_extract_regexp(allframes, "^("
143  VISIR_UTIL_CORRECTED"|"
144  VISIR_UTIL_UNDISTORT_PROCATG
145  ")$",
146  CPL_FALSE);
147  skip_if (rawframes == NULL);
148  n = irplib_framelist_get_size(rawframes);
149 
150 #ifdef _OPENMP
151 #pragma omp parallel for
152 #endif
153  for (cpl_size i = 0; i < n; i++) {
154  if (!didfail) {
155 
156  /* The total number of iterations must be pre-determined for the
157  parallelism to work. In case of an error we can therefore not
158  break, so instead we skip immediately to the next iteration.
159  FIXME: This check on didfail does not guarantee that only one
160  iteration can cause an error to be dumped, but it is not
161  worse than checking on a thread-local state, e.g. errori. */
162 
163  if (visir_util_destripe_one(framelist, rawframes, i, parlist,
164  ndestripe, morpho_destripe)) {
165  const cpl_error_code errori = cpl_error_set_where(cpl_func);
166 #ifdef _OPENMP
167  /* Cannot access these errors after the join,
168  so dump them now. :-(((((((((((((((((((( */
169  cpl_errorstate_dump(cleanstate, CPL_FALSE, NULL);
170  cpl_errorstate_set(cleanstate);
171 #pragma omp critical(visir_util_destripe)
172 #endif
173  didfail = errori;
174  }
175  }
176  }
177 
178  error_if(didfail, didfail, "Failed to destripe images in %d frame(s)", n);
179 
180  end_skip;
181 
182  irplib_framelist_delete(allframes);
183  irplib_framelist_delete(rawframes);
184 
185  return cpl_error_get_code();
186 }
187 
188 
189 /*----------------------------------------------------------------------------*/
201 /*----------------------------------------------------------------------------*/
202 static
203 cpl_error_code visir_util_destripe_one(cpl_frameset * framelist,
204  irplib_framelist * rawframes, int iframe,
205  const cpl_parameterlist * parlist,
206  const int ndestripe,
207  const cpl_boolean morpho_destripe)
208 {
209 
210  cpl_frameset * products = cpl_frameset_new();
211  cpl_frameset * usedframes = cpl_frameset_new();
212  const cpl_frame * rawframe = irplib_framelist_get_const(rawframes, iframe);
213  const char * filename = cpl_frame_get_filename(rawframe);
214  cpl_image * img = NULL;
215  char * proname = cpl_sprintf(RECIPE_STRING "_%d" CPL_DFS_FITS,
216  1+iframe);
217  cpl_propertylist * plist = NULL;
218  visir_data_type dtype;
219  cpl_size next;
220 
221  cpl_frame_set_group(irplib_framelist_get(rawframes, iframe),
222  CPL_FRAME_GROUP_RAW);
223  bug_if(cpl_frameset_insert(usedframes, cpl_frame_duplicate
224  (irplib_framelist_get_const(rawframes,
225  iframe))));
226 
227  irplib_framelist_load_propertylist(rawframes, iframe, 0, ".?", CPL_FALSE);
228  plist = irplib_framelist_get_propertylist(rawframes, iframe);
229 
230  skip_if(visir_get_data_type(rawframe, plist, &dtype, &next));
231  skip_if(irplib_dfs_save_propertylist(products, parlist, usedframes,
232  RECIPE_STRING,
233  VISIR_UTIL_DESTRIPE_PROCATG, plist,
234  NULL,
235  visir_pipe_id, proname));
236 
237  for (cpl_size iext = 0; iext < 1 + next; iext++) {
238  cpl_errorstate prestate = cpl_errorstate_get();
239 
240  img = cpl_image_load(filename, CPL_TYPE_FLOAT, 0, iext);
241  if (img == NULL) {
242  cpl_msg_info(cpl_func, "No image-data in extension %d", (int)iext);
243  cpl_errorstate_set(prestate);
244  continue;
245  }
246 
247  /* FIXME: handle aqu data? */
248  if (visir_data_is_drs(dtype)) {
249  if(visir_destripe_image(img, ndestripe,
250  VISIR_DESTRIPE_DETECT,
251  VISIR_DESTRIPE_DETECT_THRESHOLD,
252  morpho_destripe)) {
253  cpl_error_set_message(cpl_func,
254  cpl_error_get_code(),
255  "Failure for extension %d/%d",
256  (int)iext, (int)next);
257  break;
258  }
259  }
260 
261  cpl_image_save(img, proname, CPL_BPP_IEEE_FLOAT,
262  NULL, CPL_IO_EXTEND);
263  }
264 
265  FOR_EACH_FRAMESET_C(frame, products) {
266  cpl_frame * copy = cpl_frame_duplicate(frame);
267  cpl_error_code error;
268 
269 #ifdef _OPENMP
270 #pragma omp critical(visir_util_destripe_one)
271 #endif
272  error = cpl_frameset_insert(framelist, copy);
273 
274  skip_if(error);
275  }
276 
277 
278  end_skip;
279 
280  cpl_image_delete(img);
281  cpl_frameset_delete(usedframes);
282  cpl_frameset_delete(products);
283  cpl_free(proname);
284 
285  return cpl_error_get_code();
286 }
cpl_error_code visir_parameter_set(cpl_parameterlist *self, const char *recipe, visir_parameter bitmask)
Define the specified parameters.
cpl_error_code visir_destripe_image(cpl_image *self, int niter, double threshold, double thres_detect, cpl_boolean morpho)
Remove the stripes frome an image using iterations.
cpl_error_code irplib_dfs_save_propertylist(cpl_frameset *allframes, const cpl_parameterlist *parlist, const cpl_frameset *usedframes, const char *recipe, const char *procat, const cpl_propertylist *applist, const char *remregexp, const char *pipe_id, const char *filename)
Save a propertylist as a DFS-compliant pipeline product.
Definition: irplib_utils.c:240
int visir_parameterlist_get_int(const cpl_parameterlist *self, const char *recipe, visir_parameter bitmask)
Retrieve the value of a VISIR integer parameter.
irplib_framelist * irplib_framelist_extract_regexp(const irplib_framelist *self, const char *regexp, cpl_boolean invert)
Extract the frames with the given tag from a framelist.
cpl_boolean visir_parameterlist_get_bool(const cpl_parameterlist *self, const char *recipe, visir_parameter bitmask)
Retrieve the value of a VISIR boolean parameter.
cpl_frame * irplib_framelist_get(irplib_framelist *self, int pos)
Get the specified frame from the framelist.
const cpl_frame * irplib_framelist_get_const(const irplib_framelist *self, int pos)
Get the specified frame from the framelist.
void irplib_framelist_delete(irplib_framelist *self)
Deallocate an irplib_framelist with its frames and properties.
cpl_propertylist * irplib_framelist_get_propertylist(irplib_framelist *self, int pos)
Get the propertylist of the specified frame in the framelist.
cpl_error_code irplib_framelist_load_propertylist(irplib_framelist *self, int pos, int ind, const char *regexp, cpl_boolean invert)
Load the propertylist of the specified frame in the framelist.
irplib_framelist * irplib_framelist_cast(const cpl_frameset *frameset)
Create an irplib_framelist from a cpl_framelist.
int irplib_framelist_get_size(const irplib_framelist *self)
Get the size of a framelist.