VISIR Pipeline Reference Manual  4.1.0
visir_img_combine.c
1 /* $Id: visir_img_combine.c,v 1.107 2010-10-21 11:51:32 llundin Exp $
2  *
3  * This file is part of the VISIR Pipeline
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: llundin $
23  * $Date: 2010-10-21 11:51:32 $
24  * $Revision: 1.107 $
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 /*-----------------------------------------------------------------------------
39  Defines
40  -----------------------------------------------------------------------------*/
41 
42 #define RECIPE_STRING "visir_old_img_combine"
43 
44 /* FITS keys to be loaded for all raw files */
45 #define RECIPE_KEYS_REGEXP_ALL \
46  VISIR_PFITS_REGEXP_IMG_RECOMBINE
47 
48 /* FITS keys to be loaded for first raw file */
49 #define RECIPE_KEYS_REGEXP \
50  RECIPE_KEYS_REGEXP_ALL \
51  "|" VISIR_PFITS_REGEXP_COMBINE_PAF \
52  "|" VISIR_PFITS_INT_NDIT \
53  "|" VISIR_PFITS_INT_CHOP_NCYCLES \
54  "|" VISIR_PFITS_STRING_FILTER1 \
55  "|" VISIR_PFITS_STRING_FILTER2
56 
57 /* FITS keys to be loaded for first raw file, in case WCS is used */
58 #define RECIPE_KEYS_REGEXP_WCS \
59  RECIPE_KEYS_REGEXP \
60  "|" IRPLIB_PFITS_WCS_REGEXP
61 
62 /*-----------------------------------------------------------------------------
63  Private Functions prototypes
64  -----------------------------------------------------------------------------*/
65 
66 static cpl_error_code visir_img_combine_qc(cpl_propertylist *,
67  cpl_boolean,
68  const irplib_framelist *,
69  const cpl_image *);
70 
71 static cpl_error_code visir_img_combine_save(cpl_frameset *,
72  const cpl_parameterlist *,
73  const cpl_propertylist *,
74  const cpl_image *,
75  const cpl_image *,
76  const cpl_image *,
77  const cpl_image *,
78  const char *,
79  const char *);
80 
81 VISIR_RECIPE_DEFINE(visir_old_img_combine,
82  VISIR_PARAM_REFINE | VISIR_PARAM_XCORR |
83  VISIR_PARAM_OFFSETS | VISIR_PARAM_OBJECTS |
84  VISIR_PARAM_NODPOS | VISIR_PARAM_AUTOBPM |
85  VISIR_PARAM_GLITCH | VISIR_PARAM_PURGE |
86  VISIR_PARAM_UNION | VISIR_PARAM_REJECT |
87  VISIR_PARAM_COMBINE | VISIR_PARAM_ECCMAX |
88  VISIR_PARAM_STRIPITE | VISIR_PARAM_STRIPMOR |
89  VISIR_PARAM_PLOT,
90  "Old DRS detector: Images combination recipe",
91  "This recipe recombines the data observed in "
92  "chopping/nodding or\n"
93  "chopping or nodding modes into one combined image using "
94  "optionally\n"
95  "cross-correlation methods.\n"
96  "The files listed in the Set Of Frames (sof-file) "
97  "must be tagged:\n"
98  "VISIR-observation-file.fits " VISIR_IMG_COMBINE_CNJ " or\n"
99  "VISIR-observation-file.fits " VISIR_IMG_COMBINE_CJ " or\n"
100  "VISIR-observation-file.fits " VISIR_IMG_COMBINE_NJ " or\n"
101  "VISIR-observation-file.fits " VISIR_IMG_COMBINE_DJ "\n"
102  "\n"
103  "The corresponding primary product will have a FITS card\n"
104  "'HIERARCH ESO PRO CATG' with a value of\n"
105  VISIR_IMG_COMBINE_COMBINED_PROCATG_CNJ " or\n"
106  VISIR_IMG_COMBINE_COMBINED_PROCATG_CJ " or\n"
107  VISIR_IMG_COMBINE_COMBINED_PROCATG_NJ " or\n"
108  VISIR_IMG_COMBINE_COMBINED_PROCATG_DJ
109  " and the corresponding beam-collapsed product will "
110  "have a FITS card\n"
111  "'HIERARCH ESO PRO CATG' with a value of\n"
112  VISIR_IMG_COMBINE_ONEBEAM_PROCATG_CNJ " or\n"
113  VISIR_IMG_COMBINE_ONEBEAM_PROCATG_CJ " or\n"
114  VISIR_IMG_COMBINE_ONEBEAM_PROCATG_NJ " or\n"
115  VISIR_IMG_COMBINE_ONEBEAM_PROCATG_DJ
116  "\n"
117  MAN_VISIR_CALIB_BPM_IMG);
118 
119 /*----------------------------------------------------------------------------*/
123 /*----------------------------------------------------------------------------*/
124 
125 /*-----------------------------------------------------------------------------
126  Functions code
127  -----------------------------------------------------------------------------*/
128 
129 /*----------------------------------------------------------------------------*/
136 /*----------------------------------------------------------------------------*/
137 static int visir_old_img_combine(cpl_frameset * framelist,
138  const cpl_parameterlist * parlist)
139 {
140  cpl_errorstate cleanstate = cpl_errorstate_get();
141  irplib_framelist * allframes = NULL;
142  irplib_framelist * rawframes = NULL;
143  cpl_propertylist * qclist = cpl_propertylist_new();
144  const char * badpix;
145  const char * flat;
146  cpl_image ** combined = NULL;
147  cpl_image ** beam1 = NULL;
148  const char * procatg_combine = NULL;
149  const char * procatg_onebeam = NULL;
150  cpl_boolean drop_wcs;
151  const char * keys_regexp = "^(" RECIPE_KEYS_REGEXP_WCS ")$";
152 
153  const char * combine_string;
154  cpl_geom_combine combine_mode;
155 
156  /* Retrieve input parameters */
157  combine_string = visir_parameterlist_get_string(parlist, RECIPE_STRING,
158  VISIR_PARAM_COMBINE);
159 
160  bug_if (combine_string == NULL);
161 
162  if (combine_string[0] == 'u')
163  combine_mode = CPL_GEOM_UNION;
164  else if (combine_string[0] == 'f')
165  combine_mode = CPL_GEOM_FIRST;
166  else if (combine_string[0] == 'i')
167  combine_mode = CPL_GEOM_INTERSECT;
168  else
169  skip_if(1);
170 
171  /* Identify the RAW and CALIB frames in the input frameset */
172  skip_if (visir_dfs_set_groups(framelist));
173 
174  /* Objects observation */
175  allframes = irplib_framelist_cast(framelist);
176  skip_if(allframes == NULL);
177  rawframes = irplib_framelist_extract_regexp(allframes, "^("
178  VISIR_IMG_COMBINE_CNJ "|"
179  VISIR_IMG_COMBINE_CJ "|"
180  VISIR_IMG_COMBINE_NJ "|"
181  VISIR_IMG_COMBINE_DJ ")$",
182  CPL_FALSE);
183  skip_if (rawframes == NULL);
184  irplib_framelist_empty(allframes);
185 
186  skip_if( irplib_framelist_load_propertylist(rawframes, 0, 0, keys_regexp,
187  CPL_FALSE));
188 
189  skip_if(irplib_framelist_load_propertylist_all(rawframes, 0, "^("
190  RECIPE_KEYS_REGEXP_ALL
191  ")$", CPL_FALSE));
192 
193  skip_if(visir_dfs_check_framelist_tag(rawframes));
194 
195  /* Set PRO.CATG - and verify uniqueness of frame type */
196  if (cpl_frameset_find(framelist, VISIR_IMG_COMBINE_DJ)) {
197  procatg_combine = VISIR_IMG_COMBINE_COMBINED_PROCATG_DJ;
198  procatg_onebeam = VISIR_IMG_COMBINE_ONEBEAM_PROCATG_DJ;
199  }
200  if (cpl_frameset_find(framelist, VISIR_IMG_COMBINE_NJ)) {
201  skip_if (procatg_combine != NULL);
202  procatg_combine = VISIR_IMG_COMBINE_COMBINED_PROCATG_NJ;
203  procatg_onebeam = VISIR_IMG_COMBINE_ONEBEAM_PROCATG_NJ;
204  }
205  if (cpl_frameset_find(framelist, VISIR_IMG_COMBINE_CJ)) {
206  skip_if (procatg_combine != NULL);
207  procatg_combine = VISIR_IMG_COMBINE_COMBINED_PROCATG_CJ;
208  procatg_onebeam = VISIR_IMG_COMBINE_ONEBEAM_PROCATG_CJ;
209  }
210  if (cpl_frameset_find(framelist, VISIR_IMG_COMBINE_CNJ)) {
211  skip_if (procatg_combine != NULL);
212  procatg_combine = VISIR_IMG_COMBINE_COMBINED_PROCATG_CNJ;
213  procatg_onebeam = VISIR_IMG_COMBINE_ONEBEAM_PROCATG_CNJ;
214  }
215  bug_if (procatg_combine == NULL);
216 
217  /* Bad pixels calibration file */
218  badpix = irplib_frameset_find_file(framelist, VISIR_CALIB_BPM);
219 
220  /* Flatfield calibration file */
221  flat = irplib_frameset_find_file(framelist, VISIR_CALIB_FLAT);
222 
223  /* Combine the frames */
224  combined = visir_img_recombine(RECIPE_STRING, parlist, rawframes, badpix,
225  flat, combine_mode, &drop_wcs,
226  CPL_FALSE, 0.0, 0);
227 
228  if (combined == NULL) {
229  cpl_msg_error(cpl_func, "Could not combine the input frames");
230  skip_if(1);
231  }
232 
233  beam1 = visir_img_collapse_beam(qclist, combined[0], parlist, RECIPE_STRING,
234  VISIR_CHOPNOD_AUTO,
236  (rawframes, 0));
237 
238  if (beam1 == NULL) {
239  irplib_error_recover(cleanstate, "Could not collapse the beams of "
240  "the combined image");
241  }
242 
243  /* Add QC parameters */
244  skip_if(visir_img_combine_qc(qclist, drop_wcs, rawframes, combined[0]));
245 
246  /* Reduce maximum number of pointers used */
247  irplib_framelist_empty(rawframes);
248 
249  /* Save the combined image and contribution map */
250  cpl_msg_info(cpl_func, "Save the combined image and contribution map");
251 
252  if (beam1 == NULL) {
253  skip_if(visir_img_combine_save(framelist, parlist, qclist,
254  combined[0], combined[1], NULL,
255  NULL, procatg_combine, procatg_onebeam));
256  } else {
257  skip_if(visir_img_combine_save(framelist, parlist, qclist,
258  combined[0], combined[1], beam1[0],
259  beam1[1], procatg_combine,
260  procatg_onebeam));
261  }
262 
263  end_skip;
264 
265  if (combined) {
266  cpl_image_delete(combined[0]);
267  cpl_image_delete(combined[1]);
268  cpl_free(combined);
269  }
270 
271  if (beam1) {
272  cpl_image_delete(beam1[0]);
273  cpl_image_delete(beam1[1]);
274  cpl_free(beam1);
275  }
276 
277  irplib_framelist_delete(allframes);
278  irplib_framelist_delete(rawframes);
279  cpl_propertylist_delete(qclist);
280 
281  return cpl_error_get_code();
282 }
283 
284 /*----------------------------------------------------------------------------*/
293 /*----------------------------------------------------------------------------*/
294 static cpl_error_code visir_img_combine_qc(cpl_propertylist * qclist,
295  cpl_boolean drop_wcs,
296  const irplib_framelist * rawframes,
297  const cpl_image * combined)
298 {
299 
300  const cpl_propertylist * reflist
302  double bg_sigma;
303 
304 
305  bug_if (0);
306 
307  /* QC.EXPTIME */
308  skip_if (visir_qc_append_exptime(qclist, rawframes));
309 
310  /* Copy the filter name */
311  skip_if (visir_qc_append_filter(qclist, rawframes));
312 
313 
314  /* QC.BACKGD.MEAN */
315  /* Compute the background values of the HCYCLE frames */
316  skip_if(visir_qc_append_background(qclist, rawframes, 0, 0));
317 
318  /* Compute the background sigma */
319  bg_sigma = visir_img_phot_sigma_clip(combined);
320 
321  skip_if (0);
322 
323  /* QC.BACKGD.SIGMA */
324  bug_if (cpl_propertylist_append_double(qclist, "ESO QC BACKGD SIGMA",
325  bg_sigma));
326 
327  /* QC.CAPA */
328  skip_if (visir_qc_append_capa(qclist, rawframes));
329 
330  if (drop_wcs) {
331  cpl_propertylist * pcopy = cpl_propertylist_new();
332  const cpl_error_code error
333  = cpl_propertylist_copy_property_regexp(pcopy, reflist, "^("
334  IRPLIB_PFITS_WCS_REGEXP
335  ")$", 0);
336  if (!error && cpl_propertylist_get_size(pcopy) > 0) {
337  cpl_msg_warning(cpl_func, "Combined image will have no WCS "
338  "coordinates");
339  }
340  cpl_propertylist_delete(pcopy);
341  bug_if(0);
342  } else {
343  bug_if(cpl_propertylist_copy_property_regexp(qclist, reflist, "^("
344  IRPLIB_PFITS_WCS_REGEXP
345  ")$", 0));
346  }
347 
348  end_skip;
349 
350  return cpl_error_get_code();
351 }
352 
353 /*----------------------------------------------------------------------------*/
367 /*----------------------------------------------------------------------------*/
368 static
369 cpl_error_code visir_img_combine_save(cpl_frameset * self,
370  const cpl_parameterlist * parlist,
371  const cpl_propertylist * qclist,
372  const cpl_image * combined,
373  const cpl_image * contrib,
374  const cpl_image * beam1,
375  const cpl_image * beam1i,
376  const char * procat_c,
377  const char * procat_b)
378 {
379  cpl_propertylist * xtlist = cpl_propertylist_new();
380 
381 
382  bug_if(cpl_propertylist_append_string(xtlist, "EXTNAME",
383  "Contribution Map"));
384 
385  /* THE COMBINED IMAGE */
386  skip_if (irplib_dfs_save_image(self, parlist, self, combined,
387  CPL_BPP_IEEE_FLOAT, RECIPE_STRING, procat_c,
388  qclist, NULL, visir_pipe_id,
389  RECIPE_STRING CPL_DFS_FITS));
390 
391  /* THE CONTRIBUTION MAP */
392  skip_if (cpl_image_save(contrib, RECIPE_STRING CPL_DFS_FITS,
393  CPL_BPP_16_UNSIGNED, xtlist, CPL_IO_EXTEND));
394 
395  if (beam1 != NULL) {
396  /* THE BEAM COLLAPSED IMAGE */
397  skip_if (irplib_dfs_save_image(self, parlist, self, beam1,
398  CPL_BPP_IEEE_FLOAT, RECIPE_STRING,
399  procat_b, qclist, NULL, visir_pipe_id,
400  RECIPE_STRING "_onebeam" CPL_DFS_FITS));
401 
402  /* THE BEAM COLLAPSED CONTRIBUTION MAP */
403  skip_if (cpl_image_save(beam1i, RECIPE_STRING "_onebeam" CPL_DFS_FITS,
404  CPL_BPP_8_UNSIGNED, xtlist, CPL_IO_EXTEND));
405  }
406 
407  end_skip;
408 
409  cpl_propertylist_delete(xtlist);
410 
411  return cpl_error_get_code();
412 
413 }
cpl_error_code visir_dfs_check_framelist_tag(const irplib_framelist *self)
Check the tags in a frameset (group raw only)
Definition: visir_dfs.c:173
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.
const cpl_propertylist * irplib_framelist_get_propertylist_const(const irplib_framelist *self, int pos)
Get the propertylist of the specified frame in the framelist.
void irplib_framelist_empty(irplib_framelist *self)
Erase all frames from a framelist.
cpl_image ** visir_img_collapse_beam(cpl_propertylist *qclist, const cpl_image *self, const cpl_parameterlist *parlist, const char *recipename, visir_chopnod_mode mode, const cpl_propertylist *plist)
Collapse the 3/4 beams of a combined image.
Definition: visir_inputs.c:504
cpl_error_code irplib_dfs_save_image(cpl_frameset *allframes, const cpl_parameterlist *parlist, const cpl_frameset *usedframes, const cpl_image *image, cpl_type_bpp bpp, const char *recipe, const char *procat, const cpl_propertylist *applist, const char *remregexp, const char *pipe_id, const char *filename)
Save an image as a DFS-compliant pipeline product.
Definition: irplib_utils.c:192
cpl_error_code irplib_framelist_load_propertylist_all(irplib_framelist *self, int ind, const char *regexp, cpl_boolean invert)
Load the propertylists of all frames in the framelist.
const char * visir_parameterlist_get_string(const cpl_parameterlist *self, const char *recipe, visir_parameter bitmask)
Retrieve the value of a VISIR string parameter.
int visir_dfs_set_groups(cpl_frameset *set)
Set the group as RAW or CALIB in a frameset.
Definition: visir_dfs.c:72
void irplib_framelist_delete(irplib_framelist *self)
Deallocate an irplib_framelist with its frames and properties.
const char * irplib_frameset_find_file(const cpl_frameset *self, const char *tag)
Find the filename with the given tag in a frame set.
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.
cpl_image ** visir_img_recombine(const char *recipename, const cpl_parameterlist *parlist, const irplib_framelist *rawframes, const char *badpix, const char *flat, cpl_geom_combine combine_mode, cpl_boolean *pdid_resize, cpl_boolean do_spc_fix, double wlen, visir_spc_resol resol)
The VISIR imaging combination using cross correlation.