IIINSTRUMENT Pipeline Reference Manual  1.5.13
sofi_img_domeflat.c
1 /* $Id: sofi_img_domeflat.c,v 1.17 2013-03-12 08:04:52 llundin Exp $
2  *
3  * This file is part of the SOFI 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: 2013-03-12 08:04:52 $
24  * $Revision: 1.17 $
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 <math.h>
37 #include <cpl.h>
38 
39 #include "irplib_utils.h"
40 
41 #include "sofi_utils.h"
42 #include "sofi_pfits.h"
43 #include "sofi_dfs.h"
44 
45 /*-----------------------------------------------------------------------------
46  Functions prototypes
47  -----------------------------------------------------------------------------*/
48 
49 static int sofi_img_domeflat_create(cpl_plugin *);
50 static int sofi_img_domeflat_exec(cpl_plugin *);
51 static int sofi_img_domeflat_destroy(cpl_plugin *);
52 static int sofi_img_domeflat(cpl_parameterlist *, cpl_frameset *);
53 static cpl_image * sofi_img_domeflat_compute(cpl_frameset *);
54 static cpl_image * sofi_img_domeflat_bias(cpl_image *, cpl_image *,
55  int, int, int, int);
56 static int sofi_img_domeflat_save(cpl_image *, cpl_parameterlist *,
57  cpl_frameset *);
58 
59 /*-----------------------------------------------------------------------------
60  Static variables
61  -----------------------------------------------------------------------------*/
62 
63 static char sofi_img_domeflat_description[] =
64 "sofi_img_domeflat -- SOFI imaging flat-field creation.\n"
65 "The files listed in the Set Of Frames (sof-file) must be tagged:\n"
66 "raw-file.fits "SOFI_IMG_DOMEFLAT_RAW"\n";
67 
68 /*-----------------------------------------------------------------------------
69  Functions code
70  -----------------------------------------------------------------------------*/
71 
72 /*----------------------------------------------------------------------------*/
80 /*----------------------------------------------------------------------------*/
81 int cpl_plugin_get_info(cpl_pluginlist * list)
82 {
83  cpl_recipe * recipe = cpl_calloc(1, sizeof(*recipe));
84  cpl_plugin * plugin = &recipe->interface;
85 
86  cpl_plugin_init(plugin,
87  CPL_PLUGIN_API,
88  SOFI_BINARY_VERSION,
89  CPL_PLUGIN_TYPE_RECIPE,
90  "sofi_img_domeflat",
91  "Dome flat recipe",
92  sofi_img_domeflat_description,
93  "Yves Jung",
94  "yjung@eso.org",
96  sofi_img_domeflat_create,
97  sofi_img_domeflat_exec,
98  sofi_img_domeflat_destroy);
99 
100  cpl_pluginlist_append(list, plugin);
101 
102  return 0;
103 }
104 
105 /*----------------------------------------------------------------------------*/
114 /*----------------------------------------------------------------------------*/
115 static int sofi_img_domeflat_create(cpl_plugin * plugin)
116 {
117  cpl_recipe * recipe;
118 
119  /* Get the recipe out of the plugin */
120  if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
121  recipe = (cpl_recipe *)plugin;
122  else return CPL_ERROR_UNSPECIFIED;
123 
124  /* Create the parameters list in the cpl_recipe object */
125  recipe->parameters = cpl_parameterlist_new();
126 
127  /* Fill the parameters list */
128 
129  /* Return */
130  return 0;
131 }
132 
133 /*----------------------------------------------------------------------------*/
139 /*----------------------------------------------------------------------------*/
140 static int sofi_img_domeflat_exec(cpl_plugin * plugin)
141 {
142  cpl_recipe * recipe;
143 
144  /* Get the recipe out of the plugin */
145  if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
146  recipe = (cpl_recipe *)plugin;
147  else return CPL_ERROR_UNSPECIFIED;
148 
149  return sofi_img_domeflat(recipe->parameters, recipe->frames);
150 }
151 
152 /*----------------------------------------------------------------------------*/
158 /*----------------------------------------------------------------------------*/
159 static int sofi_img_domeflat_destroy(cpl_plugin * plugin)
160 {
161  cpl_recipe * recipe;
162 
163  /* Get the recipe out of the plugin */
164  if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
165  recipe = (cpl_recipe *)plugin;
166  else return CPL_ERROR_UNSPECIFIED;
167 
168  cpl_parameterlist_delete(recipe->parameters);
169  return 0;
170 }
171 
172 /*----------------------------------------------------------------------------*/
179 /*----------------------------------------------------------------------------*/
180 static int sofi_img_domeflat(
181  cpl_parameterlist * parlist,
182  cpl_frameset * framelist)
183 {
184  cpl_frameset * flatframes;
185  cpl_image * flat;
186 
187  /* Identify the RAW and CALIB frames in the input frameset */
188  if (sofi_dfs_set_groups(framelist)) {
189  cpl_msg_error(cpl_func, "Cannot identify RAW and CALIB frames");
190  return CPL_ERROR_UNSPECIFIED;
191  }
192 
193  /* Retrieve raw frames */
194  if ((flatframes = sofi_extract_frameset(framelist,
195  SOFI_IMG_DOMEFLAT_RAW)) == NULL) {
196  cpl_msg_error(cpl_func, "Cannot find flat frames in the input list");
197  return CPL_ERROR_UNSPECIFIED;
198  }
199 
200  /* Compute the flat field */
201  cpl_msg_info(cpl_func, "Compute dome flat");
202  if ((flat = sofi_img_domeflat_compute(flatframes)) == NULL) {
203  cpl_msg_error(cpl_func, "Cannot compute dome flat");
204  cpl_frameset_delete(flatframes);
205  return CPL_ERROR_UNSPECIFIED;
206  }
207  cpl_frameset_delete(flatframes);
208 
209  /* Save the flat field */
210  cpl_msg_info(cpl_func, "Save the product");
211  if (sofi_img_domeflat_save(flat, parlist, framelist) == -1) {
212  cpl_msg_error(cpl_func, "Cannot save the product");
213  cpl_image_delete(flat);
214  return CPL_ERROR_UNSPECIFIED;
215  }
216  cpl_image_delete(flat);
217 
218  /* Free and return */
219  return 0;
220 }
221 
222 /*----------------------------------------------------------------------------*/
228 /*----------------------------------------------------------------------------*/
229 static cpl_image * sofi_img_domeflat_compute(cpl_frameset * fset)
230 {
231  cpl_imagelist * ilist;
232  int x1, x2, x3, x4;
233  cpl_image * on;
234  cpl_image * on_ma;
235  cpl_image * off;
236  cpl_image * off_ma;
237  cpl_image * bias;
238 
239  /* Test entries */
240  if (fset == NULL) return NULL;
241  if (cpl_frameset_get_size(fset) != 8) return NULL;
242 
243  /* Initialise */
244  x1 = 500;
245  x2 = 600;
246  x3 = 50;
247  x4 = 150;
248 
249  /* Load the input frames */
250  cpl_msg_info(cpl_func, "Load the frames");
251  if ((ilist = cpl_imagelist_load_frameset(fset, CPL_TYPE_FLOAT, 0,
252  0)) == NULL) {
253  cpl_msg_error(cpl_func, "Cannot load the frames");
254  return NULL;
255  }
256 
257  /* Get the on images */
258  on = cpl_image_add_create( cpl_imagelist_get(ilist, 3),
259  cpl_imagelist_get(ilist, 4));
260  cpl_image_divide_scalar(on, 2);
261  on_ma = cpl_image_add_create( cpl_imagelist_get(ilist, 2),
262  cpl_imagelist_get(ilist, 5));
263  cpl_image_divide_scalar(on_ma, 2);
264 
265  /* Get the on bias */
266  if ((bias = sofi_img_domeflat_bias(on, on_ma, x1, x2, x3, x4)) == NULL) {
267  cpl_msg_error(cpl_func, "Cannot compute the bias of the on frames");
268  cpl_image_delete(on);
269  cpl_image_delete(on_ma);
270  cpl_imagelist_delete(ilist);
271  return NULL;
272  }
273  cpl_image_delete(on_ma);
274 
275  /* Correct the on bias */
276  cpl_image_subtract(on, bias);
277  cpl_image_delete(bias);
278 
279  /* Get the off images */
280  off = cpl_image_add_create( cpl_imagelist_get(ilist, 0),
281  cpl_imagelist_get(ilist, 7));
282  cpl_image_divide_scalar(off, 2);
283  off_ma =cpl_image_add_create( cpl_imagelist_get(ilist, 1),
284  cpl_imagelist_get(ilist, 6));
285  cpl_image_divide_scalar(off_ma, 2);
286  cpl_imagelist_delete(ilist);
287 
288  /* Get the off bias */
289  if ((bias = sofi_img_domeflat_bias(off, off_ma, x1, x2, x3,
290  x4)) == NULL) {
291  cpl_msg_error(cpl_func, "Cannot compute the bias of the off frames");
292  cpl_image_delete(on);
293  cpl_image_delete(off);
294  cpl_image_delete(off_ma);
295  return NULL;
296  }
297  cpl_image_delete(off_ma);
298 
299  /* Correct the on bias */
300  cpl_image_subtract(off, bias);
301  cpl_image_delete(bias);
302 
303  /* Compute the flat */
304  cpl_image_subtract(on, off);
305  cpl_image_delete(off);
306 
307  /* Normalise the flat */
308  cpl_image_normalise(on, CPL_NORM_MEAN);
309 
310  /* Return */
311  return on;
312 }
313 
314 /*----------------------------------------------------------------------------*/
325 /*----------------------------------------------------------------------------*/
326 static cpl_image * sofi_img_domeflat_bias(
327  cpl_image * in,
328  cpl_image * in_mask,
329  int x1,
330  int x2,
331  int x3,
332  int x4)
333 {
334  int nx, ny;
335  cpl_image * a_1d;
336  cpl_image * b_1d;
337  cpl_image * c_1d;
338  cpl_image * a_2d;
339  float * pa_1d;
340  float * pa_2d;
341  int i, j;
342 
343  /* Test entries */
344  if (in == NULL) return NULL;
345  if (in_mask == NULL) return NULL;
346 
347  /* Initialise */
348  nx = cpl_image_get_size_x(in);
349  ny = cpl_image_get_size_y(in);
350 
351  /* Compute the bias */
352  a_1d = cpl_image_collapse_window_create(in, x1, 1, x2, ny, 1);
353  cpl_image_divide_scalar(a_1d, x2-x1+1);
354  c_1d = cpl_image_collapse_window_create(in_mask, x1, 1, x2, ny, 1);
355  cpl_image_divide_scalar(c_1d, x2-x1+1);
356  b_1d = cpl_image_collapse_window_create(in_mask, x3, 1, x4, ny, 1);
357  cpl_image_divide_scalar(b_1d, x4-x3+1);
358  cpl_image_subtract(a_1d, c_1d);
359  cpl_image_delete(c_1d);
360  cpl_image_add(a_1d, b_1d);
361  cpl_image_delete(b_1d);
362 
363  /* Expand a_1d to a_2d */
364  a_2d = cpl_image_new(nx, ny, CPL_TYPE_FLOAT);
365  pa_2d = cpl_image_get_data_float(a_2d);
366  pa_1d = cpl_image_get_data_float(a_1d);
367  for (j=0 ; j<ny ; j++) {
368  for (i=0 ; i<nx ; i++) {
369  pa_2d[i+j*nx] = pa_1d[j];
370  }
371  }
372  cpl_image_delete(a_1d);
373 
374  return a_2d;
375 }
376 
377 /*----------------------------------------------------------------------------*/
385 /*----------------------------------------------------------------------------*/
386 static int sofi_img_domeflat_save(
387  cpl_image * flat,
388  cpl_parameterlist * parlist,
389  cpl_frameset * set)
390 {
391  cpl_propertylist * plist;
392  cpl_propertylist * paflist;
393  cpl_propertylist * qclist;
394  const cpl_frame * ref_frame;
395 
396 
397  /* Get the QC params in qclist */
398  qclist = cpl_propertylist_new();
399 
400  /* Write the average image */
401  irplib_dfs_save_image(set,
402  parlist,
403  set,
404  flat,
405  CPL_BPP_IEEE_FLOAT,
406  "sofi_img_domeflat",
407  SOFI_IMG_DOMEFLAT_RES,
408  qclist,
409  NULL,
410  PACKAGE "/" PACKAGE_VERSION,
411  "sofi_img_domeflat.fits");
412 
413  /* Get the reference frame */
414  ref_frame = irplib_frameset_get_first_from_group(set, CPL_FRAME_GROUP_RAW);
415 
416  /* Get FITS header from reference file */
417  if ((plist=cpl_propertylist_load(cpl_frame_get_filename(ref_frame),
418  0)) == NULL) {
419  cpl_msg_error(cpl_func, "getting header from reference frame");
420  cpl_propertylist_delete(qclist);
421  return CPL_ERROR_UNSPECIFIED;
422  }
423 
424  /* Get the keywords for the paf file */
425  paflist = cpl_propertylist_new();
426  cpl_propertylist_copy_property_regexp(paflist, plist,
427  "^(DATE-OBS|ESO DET CHIP NAME|ARCFILE|ESO TPL ID|ESO DET MODE NAME|"
428  "ESO DET NCORRS NAME|ESO DET RSPEED|ESO DET DIT)$", 0);
429  cpl_propertylist_delete(plist);
430 
431  /* Copy the QC in paflist */
432  /* cpl_propertylist_copy_property_regexp(paflist, qclist, ".", 0) ; */
433  cpl_propertylist_delete(qclist);
434 
435  /* Save the PAF file */
436  cpl_dfs_save_paf("SOFI",
437  "sofi_img_domeflat",
438  paflist,
439  "sofi_img_domeflat.paf");
440  cpl_propertylist_delete(paflist);
441  return 0;
442 }
443 
sofi_extract_frameset
cpl_frameset * sofi_extract_frameset(const cpl_frameset *in, const char *tag)
Extract the frames with the given tag from a frameset.
Definition: sofi_utils.c:298
sofi_dfs_set_groups
int sofi_dfs_set_groups(cpl_frameset *set)
Set the group as RAW or CALIB in a frameset.
Definition: sofi_dfs.c:60
sofi_get_license
const char * sofi_get_license(void)
Get the pipeline copyright and license.
Definition: sofi_utils.c:60