VISIR Pipeline Reference Manual  4.1.0
visir_util_join.c
1 /* $Id: visir_util_join.c,v 1.13 2013-05-13 16:03:43 jtaylor Exp $
2  *
3  * This file is part of the VISIR Pipeline
4  * Copyright (C) 2011 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-13 16:03:43 $
24  * $Revision: 1.13 $
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 #include <string.h>
38 
39 /*-----------------------------------------------------------------------------
40  Defines
41  -----------------------------------------------------------------------------*/
42 
43 #define RECIPE_STRING "visir_util_join"
44 
45 /*-----------------------------------------------------------------------------
46  Private Functions prototypes
47  -----------------------------------------------------------------------------*/
48 
49 static
50 cpl_error_code visir_util_join_one(cpl_frameset *,
51  const irplib_framelist *,
52  const irplib_framelist *,
53  const irplib_framelist *,
54  const irplib_framelist *,
55  const irplib_framelist *,
56  const irplib_framelist *,
57  int, cpl_boolean, cpl_boolean,
58  cpl_boolean, cpl_boolean,
59  cpl_boolean,
60  const cpl_parameterlist *);
61 
62 #ifdef VISIR_CHAIN
63 #define cpl_plugin_get_info visir_util_join_get_info
64 #endif
65 VISIR_RECIPE_DEFINE(visir_util_join, 0,
66  "Extend the final product with extensions containing its "
67  "error map, bad pixel map and weight or contribution map",
68  "The files listed in the Set Of Frames (SOF-file) "
69  "must be tagged:\n"
70  "VISIR-data.fits " VISIR_UTIL_DATA "\n"
71  "\nOptionally, the SOF may also contain one or more of "
72  "the following:\n"
73  "VISIR-bad-pixel-map.fits " VISIR_CALIB_BPM "\n"
74  "VISIR-error.fits " VISIR_UTIL_WEIGHT2ERROR_PROCATG
75  "\n"
76  "VISIR-contribution-map.fits " VISIR_IMG_CLIPPED_MAP_PROCATG
77  "\n"
78  "VISIR-weight-map.fits " VISIR_UTIL_WEIGHT2ERROR
79  "\n"
80  "\nThe product(s) will have a FITS card\n"
81  "'HIERARCH " CPL_DFS_PRO_CATG "' with a value of:\n"
82  VISIR_UTIL_JOIN_PROCATG);
83 
84 /*----------------------------------------------------------------------------*/
88 /*----------------------------------------------------------------------------*/
89 
90 /*-----------------------------------------------------------------------------
91  Functions code
92  -----------------------------------------------------------------------------*/
93 
94 /*----------------------------------------------------------------------------*/
101 /*----------------------------------------------------------------------------*/
102 static int visir_util_join(cpl_frameset * framelist,
103  const cpl_parameterlist * parlist)
104 {
105  irplib_framelist * allframes = NULL;
106  irplib_framelist * rawframes = NULL;
107  irplib_framelist * bpmframes = NULL;
108  irplib_framelist * errframes = NULL;
109  irplib_framelist * conframes = NULL;
110  irplib_framelist * wgtframes = NULL;
111  irplib_framelist * qcframes = NULL;
112  int n;
113  int nbad = 0, nerr = 0, ncon = 0, nwgt = 0, nqc = 0;
114 
115 
116  /* Identify the RAW and TAB frames in the input frameset */
117  skip_if (visir_dfs_set_groups(framelist));
118  FOR_EACH_FRAMESET(frm, framelist)
119  cpl_frame_set_group(frm, CPL_FRAME_GROUP_RAW);
120 
121  /* Objects observation */
122  allframes = irplib_framelist_cast(framelist);
123  skip_if(allframes == NULL);
124 
125  rawframes =
126  irplib_framelist_extract_regexp(allframes, "^("
127  VISIR_UTIL_DATA
128  "|" VISIR_IMG_PHOT_ONEBEAM_PROCATG
129  "|" VISIR_IMG_PHOT_COMBINED_PROCATG
130  "|COADDED_IMAGE"
131  "|COADDED_IMAGE_COMBINED"
132  ")$",
133  CPL_FALSE);
134 
135  skip_if(rawframes == NULL);
136 
137  n = irplib_framelist_get_size(rawframes);
138 
139  if (cpl_frameset_find_const(framelist, VISIR_CALIB_BPM)) {
140  bpmframes = irplib_framelist_extract(allframes, VISIR_CALIB_BPM);
141  skip_if (bpmframes == NULL);
142 
143  nbad = irplib_framelist_get_size(bpmframes);
144  error_if(nbad != n && nbad != 1, CPL_ERROR_INCOMPATIBLE_INPUT,
145  "%d raw-frames <=> %d bpm frames", n, nbad);
146  }
147 
148  if (cpl_frameset_find_const(framelist, VISIR_UTIL_ERROR_MAP_PROCATG)) {
149  errframes = irplib_framelist_extract(allframes,
150  VISIR_UTIL_ERROR_MAP_PROCATG);
151  skip_if (errframes == NULL);
152 
153  nerr = irplib_framelist_get_size(errframes);
154  error_if(nerr != n && nerr != 1, CPL_ERROR_INCOMPATIBLE_INPUT,
155  "%d raw-frames <=> %d error frames", n, nerr);
156 
157  }
158 
159  if (cpl_frameset_find_const(framelist, VISIR_IMG_CLIPPED_MAP_PROCATG)) {
160  conframes = irplib_framelist_extract(allframes,
161  VISIR_IMG_CLIPPED_MAP_PROCATG);
162  skip_if (conframes == NULL);
163 
164  ncon = irplib_framelist_get_size(conframes);
165  error_if(ncon % n != 0 && ncon != 1, CPL_ERROR_INCOMPATIBLE_INPUT,
166  "%d raw-frames <=> %d contribution frames", n, ncon);
167  }
168 
169  if (cpl_frameset_find_const(framelist, VISIR_UTIL_WEIGHT_MAP_PROCATG)) {
170  wgtframes = irplib_framelist_extract(allframes,
171  VISIR_UTIL_WEIGHT_MAP_PROCATG);
172  skip_if (wgtframes == NULL);
173 
174  nwgt = irplib_framelist_get_size(wgtframes);
175  error_if(nwgt != n && nwgt != 1, CPL_ERROR_INCOMPATIBLE_INPUT,
176  "%d raw-frames <=> %d weight frames", n, nwgt);
177  }
178 
179  if (cpl_frameset_find_const(framelist, VISIR_UTIL_QC_PROCATG)) {
180  qcframes = irplib_framelist_extract(allframes, VISIR_UTIL_QC_PROCATG);
181  skip_if (qcframes == NULL);
182 
183  nqc = irplib_framelist_get_size(qcframes);
184  error_if(nqc != n && nqc != 1, CPL_ERROR_INCOMPATIBLE_INPUT,
185  "%d raw-frames <=> %d qc frames", n, nqc);
186  }
187 
188  for (int i = 0; i < n; i++) {
189  cpl_msg_info(cpl_func, "Joining frame %d/%d", 1+i, n);
190 
191  skip_if (visir_util_join_one(framelist, rawframes, bpmframes, errframes,
192  conframes, wgtframes, qcframes, i, nbad == 1,
193  nerr == 1, ncon == 1, nwgt == 1, nqc == 1,
194  parlist));
195  }
196 
197  end_skip;
198 
199  irplib_framelist_delete(allframes);
200  irplib_framelist_delete(rawframes);
201  irplib_framelist_delete(bpmframes);
202  irplib_framelist_delete(errframes);
203  irplib_framelist_delete(conframes);
204  irplib_framelist_delete(wgtframes);
205  irplib_framelist_delete(qcframes);
206 
207  return cpl_error_get_code();
208 }
209 
210 
211 /*----------------------------------------------------------------------------*/
230 /*----------------------------------------------------------------------------*/
231 static
232 cpl_error_code visir_util_join_one(cpl_frameset * framelist,
233  const irplib_framelist * rawframes,
234  const irplib_framelist * bpmframes,
235  const irplib_framelist * errframes,
236  const irplib_framelist * conframes,
237  const irplib_framelist * wgtframes,
238  const irplib_framelist * qcframes,
239  int i,
240  cpl_boolean bshared, cpl_boolean eshared,
241  cpl_boolean cshared, cpl_boolean wshared,
242  cpl_boolean qcshared,
243  const cpl_parameterlist * parlist)
244 {
245 
246  const int n = irplib_framelist_get_size(rawframes);
247  const cpl_frame * frame;
248  cpl_frameset * products = cpl_frameset_new();
249  cpl_frameset * usedframes = cpl_frameset_new();
250  const char * filename;
251  cpl_propertylist * img_plist = NULL;
252  char * proname = NULL;
253  cpl_image * img = NULL;
254  cpl_image * csum = NULL;
255  const char * bunit = NULL;
256 
257  cpl_image * bpm = NULL;
258  cpl_image * err = NULL;
259  cpl_image * wgt = NULL;
260  cpl_image * con = NULL;
261 
262  cpl_propertylist * bpm_plist = NULL;
263  cpl_propertylist * err_plist = NULL;
264  cpl_propertylist * wgt_plist = NULL;
265  cpl_propertylist * con_plist = NULL;
266  cpl_propertylist * qc_plist = NULL;
267 
268  const char * procatg = VISIR_UTIL_JOIN_PROCATG;
269 
270  bug_if(cpl_frameset_insert(usedframes, cpl_frame_duplicate
271  (irplib_framelist_get_const(rawframes, i))));
272 
273  /* 1) The data frame */
274  frame = irplib_framelist_get_const(rawframes, i);
275  filename = cpl_frame_get_filename(frame);
276 
277  if (!strcmp(cpl_frame_get_tag(frame), VISIR_IMG_PHOT_ONEBEAM_PROCATG))
278  procatg = VISIR_IMG_PHOT_ONEBEAM_PROCATG;
279  else if (!strcmp(cpl_frame_get_tag(frame), VISIR_IMG_PHOT_COMBINED_PROCATG))
280  procatg = VISIR_IMG_PHOT_COMBINED_PROCATG;
281  else if (!strcmp(cpl_frame_get_tag(frame), "COADDED_IMAGE_COMBINED"))
282  procatg = "IMG_OBJ_COMBINED";
283  else if (!strcmp(cpl_frame_get_tag(frame), "COADDED_IMAGE"))
284  procatg = "IMG_OBJ_ONEBEAM";
285 
286  img_plist = cpl_propertylist_load(filename, 0);
287  skip_if(img_plist == NULL);
288 
289  img = cpl_image_load(filename, CPL_TYPE_UNSPECIFIED, 0, 0);
290  skip_if(img == NULL);
291 
292  if (qcframes) {
293  frame = irplib_framelist_get_const(qcframes, qcshared ? 0 : i);
294  filename = cpl_frame_get_filename(frame);
295  qc_plist = cpl_propertylist_load_regexp(filename, 0, "ESO QC |BUNIT", CPL_FALSE);
296  skip_if (qc_plist == NULL);
297  if (cpl_propertylist_has(qc_plist, "BUNIT")) {
298  bunit = cpl_propertylist_get_string(qc_plist, "BUNIT");
299  }
300  }
301 
302  if (bunit == NULL && cpl_propertylist_has(img_plist, "BUNIT")) {
303  bunit = cpl_propertylist_get_string(img_plist, "BUNIT");
304  }
305  cpl_propertylist_append(img_plist, qc_plist);
306 
307 
308  if (cpl_propertylist_has(img_plist, "ESO QC BEAMID")) {
309  const char * b = cpl_propertylist_get_string(img_plist,
310  "ESO QC BEAMID");
311  proname = cpl_sprintf(RECIPE_STRING "_b%s_%03d"
312  CPL_DFS_FITS, b, 1+i);
313  }
314  else
315  proname = cpl_sprintf(RECIPE_STRING "_%03d"
316  CPL_DFS_FITS, 1+i);
317 
318  if (bpmframes != NULL) {
319  /* 3) The bpm frame */
320  frame = irplib_framelist_get_const(bpmframes, bshared ? 0 : i);
321 
322  filename = cpl_frame_get_filename(frame);
323 
324  bpm = cpl_image_load(filename, CPL_TYPE_INT, 0, 0);
325  skip_if(bpm == NULL);
326 
327  bpm_plist = cpl_propertylist_load_regexp(filename, 0, " ESO QC | ESO PRO ",
328  0);
329  skip_if(bpm_plist == NULL);
330  cpl_propertylist_append_string(bpm_plist, "EXTNAME", VISIR_EXTN_BPM);
331  }
332 
333  if (errframes != NULL) {
334  /* 3) The error frame */
335 
336  frame = irplib_framelist_get_const(errframes, eshared ? 0 : i);
337 
338  filename = cpl_frame_get_filename(frame);
339 
340  err = cpl_image_load(filename, CPL_TYPE_UNSPECIFIED, 0, 0);
341  skip_if(err == NULL);
342 
343  err_plist = cpl_propertylist_load_regexp(filename, 0, " ESO QC | ESO PRO ",
344  0);
345  skip_if(err_plist == NULL);
346  cpl_propertylist_append_string(err_plist, "EXTNAME", VISIR_EXTN_ERROR);
347  if (bunit) {
348  cpl_propertylist_append_string(err_plist, "BUNIT", bunit);
349  }
350 
351  if (wgtframes == NULL) {
352  wgt = cpl_image_new(cpl_image_get_size_x(err),
353  cpl_image_get_size_y(err),
354  CPL_TYPE_FLOAT);
355  cpl_image_add_scalar(wgt, 1.0);
356  cpl_image_divide(wgt, err);
357  cpl_image_power(wgt, 2);
358  wgt_plist = cpl_propertylist_new();
359  cpl_propertylist_append_string(wgt_plist, "EXTNAME", VISIR_EXTN_WEIGHT);
360  skip_if(0);
361  }
362 
363  }
364 
365  if (conframes != NULL) {
366  /* 4) The contribution frame(s) */
367 
368  if (cshared || irplib_framelist_get_size(conframes) == n) {
369 
370  frame = irplib_framelist_get_const(conframes, cshared ? 0 : i);
371 
372  filename = cpl_frame_get_filename(frame);
373 
374  con = cpl_image_load(filename, CPL_TYPE_INT, 0, 0);
375  skip_if(con == NULL);
376 
377  con_plist = cpl_propertylist_load_regexp(filename, 0, " ESO QC | ESO PRO ",
378  0);
379  skip_if(con_plist == NULL);
380 
381  } else {
382 
383  const int ncon = irplib_framelist_get_size(conframes);
384  const int nz = ncon / n;
385  int j;
386 
387  for (j = i * nz; j < i * nz + nz; j ++) {
388  frame = irplib_framelist_get_const(conframes, j);
389  bug_if(cpl_frameset_insert(usedframes,
390  cpl_frame_duplicate(frame)));
391 
392  filename = cpl_frame_get_filename(frame);
393 
394  if (j == i * nz) {
395 
396  con = cpl_image_load(filename, CPL_TYPE_INT, 0, 0);
397  skip_if(con == NULL);
398 
399  } else {
400  csum = cpl_image_load(filename, CPL_TYPE_INT, 0, 0);
401  skip_if(csum == NULL);
402  skip_if(cpl_image_add(con, csum));
403  }
404  }
405 
406  }
407 
408  if (bpm == NULL) {
409  bpm = cpl_image_duplicate(con);
410 
411  /* FIXME: Find a better way to create a image BPM */
412  bug_if(cpl_image_threshold(bpm, -0.5, 1.0, 0.5, 1.0));
413  bug_if(cpl_image_multiply_scalar(bpm, -1.0));
414  bug_if(cpl_image_add_scalar(bpm, 1.0));
415 
416  bpm_plist = cpl_propertylist_new();
417  cpl_propertylist_append_string(bpm_plist, "EXTNAME", VISIR_EXTN_BPM);
418  }
419  }
420 
421 
422  if (wgtframes != NULL) {
423  /* 5) The weight frame */
424 
425  frame = irplib_framelist_get_const(wgtframes, wshared ? 0 : i);
426 
427  filename = cpl_frame_get_filename(frame);
428 
429  wgt_plist = cpl_propertylist_load_regexp(filename, 0, " ESO QC | ESO PRO ",
430  0);
431  skip_if(wgt_plist == NULL);
432 
433  bug_if(cpl_propertylist_append_string(wgt_plist, "EXTNAME", VISIR_EXTN_WEIGHT));
434 
435  wgt = cpl_image_load(filename, CPL_TYPE_FLOAT, 0, 0);
436  skip_if(wgt == NULL);
437 
438  if (err == NULL) {
439  err = cpl_image_new(cpl_image_get_size_x(wgt),
440  cpl_image_get_size_y(wgt), CPL_TYPE_FLOAT);
441  cpl_image_add_scalar(err, 1.0);
442  cpl_image_divide(err, wgt);
443  cpl_image_power(err, 0.5);
444  cpl_image_fill_rejected(err, INFINITY);
445  err_plist = cpl_propertylist_new();
446  cpl_propertylist_append_string(err_plist, "EXTNAME", VISIR_EXTN_ERROR);
447  if (bunit) {
448  cpl_propertylist_append_string(err_plist, "BUNIT", bunit);
449  }
450  skip_if(0);
451  }
452 
453  }
454 
455  if (err != NULL && con == NULL && bpm == NULL) {
456 
457  /* FIXME: Find a better way to create a image BPM */
458  cpl_mask * mbpm = cpl_mask_threshold_image_create(err, -DBL_EPSILON, 1e8);
459  cpl_mask_not(mbpm);
460  bpm = cpl_image_new_from_mask(mbpm);
461  cpl_mask_delete(mbpm);
462 
463  bpm_plist = cpl_propertylist_new();
464  cpl_propertylist_append_string(bpm_plist, "EXTNAME", VISIR_EXTN_BPM);
465  }
466  skip_if(0);
467 
468  skip_if(irplib_dfs_save_image(products, parlist, usedframes,
469  img, CPL_TYPE_UNSPECIFIED,
470  RECIPE_STRING,
471  procatg, img_plist,
472  NULL, visir_pipe_id, proname));
473 
474  if (bpm) {
475  if (bpmframes) {
476  frame = irplib_framelist_get_const(bpmframes, bshared ? 0 : i);
477  bug_if(cpl_frameset_insert(usedframes, cpl_frame_duplicate(frame)));
478  }
479  skip_if(cpl_image_save(bpm, proname, CPL_BPP_8_UNSIGNED,
480  bpm_plist, CPL_IO_EXTEND));
481  }
482  if (err) {
483  if (errframes) {
484  frame = irplib_framelist_get_const(errframes, eshared ? 0 : i);
485  bug_if(cpl_frameset_insert(usedframes, cpl_frame_duplicate(frame)));
486  }
487  skip_if(cpl_image_save(err, proname, CPL_TYPE_UNSPECIFIED,
488  err_plist, CPL_IO_EXTEND));
489  }
490  if (con) {
491  if (conframes) {
492  frame = irplib_framelist_get_const(conframes, cshared ? 0 : i);
493  bug_if(cpl_frameset_insert(usedframes, cpl_frame_duplicate(frame)));
494  }
495  skip_if(cpl_image_save(con, proname, CPL_TYPE_UNSPECIFIED,
496  con_plist, CPL_IO_EXTEND));
497  }
498  if (wgt) {
499  if (wgtframes) {
500  frame = irplib_framelist_get_const(wgtframes, wshared ? 0 : i);
501  bug_if(cpl_frameset_insert(usedframes, cpl_frame_duplicate(frame)));
502  }
503  skip_if(cpl_image_save(wgt, proname, CPL_TYPE_UNSPECIFIED,
504  wgt_plist, CPL_IO_EXTEND));
505  }
506 
507  FOR_EACH_FRAMESET_C(frm, products) {
508  cpl_frame * copy = cpl_frame_duplicate(frm);
509  cpl_error_code error = cpl_frameset_insert(framelist, copy);
510 
511  if (error) break;
512  }
513 
514  end_skip;
515 
516  cpl_image_delete(img);
517  cpl_image_delete(err);
518  cpl_image_delete(wgt);
519  cpl_image_delete(con);
520  cpl_image_delete(bpm);
521  cpl_image_delete(csum);
522  cpl_free(proname);
523  cpl_propertylist_delete(img_plist);
524  cpl_propertylist_delete(err_plist);
525  cpl_propertylist_delete(wgt_plist);
526  cpl_propertylist_delete(con_plist);
527  cpl_propertylist_delete(bpm_plist);
528  cpl_propertylist_delete(qc_plist);
529  cpl_frameset_delete(usedframes);
530  cpl_frameset_delete(products);
531 
532  return cpl_error_get_code();
533 }
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_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
irplib_framelist * irplib_framelist_extract(const irplib_framelist *self, const char *tag)
Extract the frames with the given tag from a framelist.
int visir_dfs_set_groups(cpl_frameset *set)
Set the group as RAW or CALIB in a frameset.
Definition: visir_dfs.c:72
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.
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.