HAWKI Pipeline Reference Manual  1.8.12
hawki_step_stitch.c
1 /* $Id: hawki_step_stitch.c,v 1.8 2013/03/11 11:01:58 cgarcia Exp $
2  *
3  * This file is part of the HAWKI 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  */
20 
21 /*
22  * $Author: cgarcia $
23  * $Date: 2013/03/11 11:01:58 $
24  * $Revision: 1.8 $
25  * $Name: hawki-1_8_12 $
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 "hawki_utils.h"
42 #include "hawki_pfits.h"
43 #include "hawki_dfs.h"
44 #include "hawki_load.h"
45 
46 /*-----------------------------------------------------------------------------
47  Functions prototypes
48  -----------------------------------------------------------------------------*/
49 
50 #ifdef __cplusplus
51 extern "C"
52 #endif
53 int cpl_plugin_get_info(cpl_pluginlist * list);
54 
55 static int hawki_step_stitch_create(cpl_plugin *) ;
56 static int hawki_step_stitch_exec(cpl_plugin *) ;
57 static int hawki_step_stitch_destroy(cpl_plugin *) ;
58 static int hawki_step_stitch(cpl_parameterlist *, cpl_frameset *) ;
59 static int hawki_step_stitch_save
60 (cpl_image * in,
61  cpl_frame * combined,
62  cpl_parameterlist * parlist,
63  cpl_frameset * set);
64 
65 /*-----------------------------------------------------------------------------
66  Static variables
67  -----------------------------------------------------------------------------*/
68 
69 static char hawki_step_stitch_description[] =
70 "hawki_step_stitch -- Stitching utility\n"
71 "This recipe accepts 1 parameter:\n"
72 "First parameter: the HAWKI image to stitch "
73 " (PRO CATG = "HAWKI_CALPRO_COMBINED")\n"
74 "\n"
75 "This recipe produces 1 file:\n"
76 "First product: the stitch image.\n"
77 " (PRO CATG = "HAWKI_CALPRO_STITCHED")\n" ;
78 
79 /*-----------------------------------------------------------------------------
80  Functions code
81  -----------------------------------------------------------------------------*/
82 
83 /*----------------------------------------------------------------------------*/
92 /*----------------------------------------------------------------------------*/
93 int cpl_plugin_get_info(cpl_pluginlist * list)
94 {
95  cpl_recipe * recipe = cpl_calloc(1, sizeof *recipe ) ;
96  cpl_plugin * plugin = &recipe->interface ;
97 
98  cpl_plugin_init(plugin,
99  CPL_PLUGIN_API,
100  HAWKI_BINARY_VERSION,
101  CPL_PLUGIN_TYPE_RECIPE,
102  "hawki_step_stitch",
103  "Stitching utility",
104  hawki_step_stitch_description,
105  "Cesar Enrique Garcia",
106  PACKAGE_BUGREPORT,
108  hawki_step_stitch_create,
109  hawki_step_stitch_exec,
110  hawki_step_stitch_destroy) ;
111 
112  cpl_pluginlist_append(list, plugin) ;
113 
114  return 0;
115 }
116 
117 /*----------------------------------------------------------------------------*/
125 /*----------------------------------------------------------------------------*/
126 static int hawki_step_stitch_create(cpl_plugin * plugin)
127 {
128  cpl_recipe * recipe ;
129 
130  /* Check that the plugin is part of a valid recipe */
131  if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
132  recipe = (cpl_recipe *)plugin ;
133  else return -1 ;
134 
135  /* Create the parameters list in the cpl_recipe object */
136  recipe->parameters = cpl_parameterlist_new() ;
137  if (recipe->parameters == NULL)
138  return 1;
139 
140  /* Return */
141  return 0;
142 }
143 
144 /*----------------------------------------------------------------------------*/
150 /*----------------------------------------------------------------------------*/
151 static int hawki_step_stitch_exec(cpl_plugin * plugin)
152 {
153  cpl_recipe * recipe ;
154 
155  /* Get the recipe out of the plugin */
156  if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
157  recipe = (cpl_recipe *)plugin ;
158  else return -1 ;
159 
160  /* Issue a banner */
162 
163  return hawki_step_stitch(recipe->parameters, recipe->frames) ;
164 }
165 
166 /*----------------------------------------------------------------------------*/
172 /*----------------------------------------------------------------------------*/
173 static int hawki_step_stitch_destroy(cpl_plugin * plugin)
174 {
175  cpl_recipe * recipe ;
176 
177  /* Get the recipe out of the plugin */
178  if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
179  recipe = (cpl_recipe *)plugin ;
180  else return -1 ;
181 
182  cpl_parameterlist_delete(recipe->parameters) ;
183  return 0 ;
184 }
185 
186 /*----------------------------------------------------------------------------*/
193 /*----------------------------------------------------------------------------*/
194 static int hawki_step_stitch(
195  cpl_parameterlist * parlist,
196  cpl_frameset * frameset)
197 {
198  const char * comb_filename ;
199  cpl_frameset * combframes;
200  cpl_frame * combframe;
201  cpl_propertylist * plist ;
202  cpl_image * stitched ;
203  cpl_image * in[HAWKI_NB_DETECTORS] ;
204  double posx[HAWKI_NB_DETECTORS] ;
205  double posy[HAWKI_NB_DETECTORS] ;
206  int i, j ;
207  cpl_errorstate error_prevstate;
208 
209 
210  /* Retrieve input parameters */
211 
212  /* Identify the RAW and CALIB frames in the input frameset */
213  if (hawki_dfs_set_groups(frameset)) {
214  cpl_msg_error(__func__, "Cannot identify RAW and CALIB frames") ;
215  return -1 ;
216  }
217 
218  /* Identifying the combined frame */
219  cpl_msg_info(__func__, "Identifying the combined frame");
220  combframes = hawki_extract_frameset
221  (frameset, HAWKI_CALPRO_COMBINED);
222  if (combframes == NULL)
223  {
224  cpl_msg_error(__func__, "No combined images found (%s)",
225  HAWKI_CALPRO_COMBINED);
226  cpl_frameset_delete(combframes);
227  return -1 ;
228  }
229 
230  /* Check that we have 1 files in input */
231  if (cpl_frameset_get_size(combframes) != 1) {
232  cpl_msg_error(__func__, "Expects one single combined images") ;
233  cpl_frameset_delete(combframes);
234  return -1 ;
235  }
236 
237  /* Load the HAWKI images */
238  cpl_msg_info(__func__,"Loading combined frame");
239  for (i=0 ; i<HAWKI_NB_DETECTORS ; i++) {
240  if ((in[i] = hawki_load_image(combframes, 0, i+1,
241  CPL_TYPE_FLOAT)) == NULL) {
242  cpl_msg_error(__func__, "Cannot load chip nb %d", i+1) ;
243  for (j=0 ; j<i ; i++) cpl_image_delete(in[j]) ;
244  cpl_frameset_delete(combframes);
245  return -1 ;
246  }
247  }
248 
249  /* Get the first input frame */
250  combframe = cpl_frameset_get_first(combframes);
251  comb_filename = cpl_frame_get_filename(combframe);
252 
253  /* Get the POSX / POSY informations */
254  error_prevstate = cpl_errorstate_get();
255  for (i=0 ; i<HAWKI_NB_DETECTORS ; i++) {
256  plist = cpl_propertylist_load_regexp(comb_filename, i+1, "QC", 0) ;
257  posx[i] = hawki_pfits_get_comb_posx(plist);
258  posy[i] = hawki_pfits_get_comb_posy(plist);
259  cpl_propertylist_delete(plist) ;
260  if(!cpl_errorstate_is_equal(error_prevstate))
261  {
262  cpl_msg_error(__func__, "Cannot get POS infos for chip %d", i+1) ;
263  return -1 ;
264  }
265  }
266 
267  /* Compute the stitched image */
268  cpl_msg_info(__func__, "Computing the stiched image") ;
269  if ((stitched = hawki_images_stitch(in, posx, posy)) == NULL) {
270  cpl_msg_error(__func__, "Cannot stitch the images") ;
271  for (i=0 ; i<HAWKI_NB_DETECTORS ; i++) cpl_image_delete(in[i]) ;
272  return -1 ;
273  }
274  for (i=0 ; i<HAWKI_NB_DETECTORS ; i++) cpl_image_delete(in[i]) ;
275 
276  /* Save the corrected image */
277  if (hawki_step_stitch_save(stitched, combframe, parlist, frameset) == -1)
278  cpl_msg_warning(__func__,"Some data could not be saved. "
279  "Check permisions or disk space");
280 
281  /* Free and Return */
282  cpl_frameset_delete(combframes);
283  cpl_image_delete(stitched);
284 
285  /* Return */
286  if (cpl_error_get_code())
287  {
288  cpl_msg_error(__func__,
289  "HAWK-I pipeline could not recover from previous errors");
290  return -1 ;
291  }
292  else return 0 ;
293 }
294 
295 /*----------------------------------------------------------------------------*/
303 /*----------------------------------------------------------------------------*/
304 static int hawki_step_stitch_save
305 (cpl_image * in,
306  cpl_frame * combined,
307  cpl_parameterlist * parlist,
308  cpl_frameset * set)
309 {
310  cpl_propertylist * plist;
311  cpl_propertylist * wcslist;
312  const char * recipe_name = "hawki_step_stitch" ;
313  int ext_chip_1;
314  cpl_errorstate error_prevstate = cpl_errorstate_get();
315 
316  cpl_msg_indent_more();
317 
318  /* Create a propertylist for PRO.x */
319  plist = cpl_propertylist_new();
320  cpl_propertylist_append_string(plist, CPL_DFS_PRO_TYPE,
321  HAWKI_PROTYPE_STITCHED) ;
322  cpl_propertylist_append_string(plist, CPL_DFS_PRO_CATG,
323  HAWKI_CALPRO_STITCHED) ;
324 
325  /* Handle WCS keys */
326  ext_chip_1 = 1;
327  wcslist = cpl_propertylist_load_regexp(
328  cpl_frame_get_filename(combined), ext_chip_1, HAWKI_HEADER_WCS, 0);
329  cpl_propertylist_append(plist, wcslist);
330 
331  /* Save the image */
332  if(cpl_dfs_save_image(set,
333  NULL,
334  parlist,
335  set,
336  NULL,
337  in,
338  CPL_BPP_IEEE_FLOAT,
339  recipe_name,
340  plist,
341  NULL,
342  PACKAGE "/" PACKAGE_VERSION,
343  "hawki_step_stitch.fits") != CPL_ERROR_NONE)
344  cpl_msg_error(__func__,"Could not save stitched image");
345 
346  cpl_propertylist_delete(plist) ;
347  cpl_propertylist_delete(wcslist) ;
348  cpl_msg_indent_less();
349  if(!cpl_errorstate_is_equal(error_prevstate))
350  {
351  cpl_errorstate_set(CPL_ERROR_NONE);
352  return -1;
353  }
354  return 0;
355 }
356