IIINSTRUMENT Pipeline Reference Manual 6.2.5
isaac_img_slitpos.c
1/* $Id: isaac_img_slitpos.c,v 1.36 2013-03-12 08:06:48 llundin Exp $
2 *
3 * This file is part of the ISAAC 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:06:48 $
24 * $Revision: 1.36 $
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 <string.h>
37#include <math.h>
38#include <cpl.h>
39
40#include "irplib_utils.h"
41#include "irplib_slitpos.h"
42
43#include "isaac_utils.h"
44#include "isaac_pfits.h"
45#include "isaac_dfs.h"
46
47/*-----------------------------------------------------------------------------
48 Functions prototypes
49 -----------------------------------------------------------------------------*/
50
51static int isaac_img_slitpos_create(cpl_plugin *);
52static int isaac_img_slitpos_exec(cpl_plugin *);
53static int isaac_img_slitpos_destroy(cpl_plugin *);
54static int isaac_img_slitpos(cpl_parameterlist *, cpl_frameset *);
55static int isaac_img_slitpos_save(cpl_table *, int, cpl_parameterlist *,
56 cpl_frameset *);
57
58/*-----------------------------------------------------------------------------
59 Static variables
60 -----------------------------------------------------------------------------*/
61
62static struct {
63 /* Inputs */
64 double slit_width;
65 int products_flag;
66 /* Outputs */
67 double angle;
68 int slit_length;
69 double xpos;
70 double ypos;
71 double slit_flux;
72} isaac_img_slitpos_config;
73
74static char isaac_img_slitpos_description[] =
75"isaac_img_slitpos -- ISAAC imaging slit position recipe.\n"
76"The files listed in the Set Of Frames (sof-file) must be tagged:\n"
77"raw-file.fits " ISAAC_IMG_SLITPOS_RAW " or"
78"raw-file.fits " ISAAC_IMG_SLITPOS_CAL_RAW "\n";
79
80/*-----------------------------------------------------------------------------
81 Functions code
82 -----------------------------------------------------------------------------*/
83
84/*----------------------------------------------------------------------------*/
92/*----------------------------------------------------------------------------*/
93int 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 ISAAC_BINARY_VERSION,
101 CPL_PLUGIN_TYPE_RECIPE,
102 "isaac_img_slitpos",
103 "Slit position recipe",
104 isaac_img_slitpos_description,
105 "Lars Lundin",
106 PACKAGE_BUGREPORT,
108 isaac_img_slitpos_create,
109 isaac_img_slitpos_exec,
110 isaac_img_slitpos_destroy);
111
112 cpl_pluginlist_append(list, plugin);
113
114 return 0;
115}
116
117/*----------------------------------------------------------------------------*/
126/*----------------------------------------------------------------------------*/
127static int isaac_img_slitpos_create(cpl_plugin * plugin)
128{
129 cpl_recipe * recipe;
130 cpl_parameter * p;
131
132 /* Get the recipe out of the plugin */
133 if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
134 recipe = (cpl_recipe *)plugin;
135 else return CPL_ERROR_UNSPECIFIED;
136
137 /* Create the parameters list in the cpl_recipe object */
138 recipe->parameters = cpl_parameterlist_new();
139
140 /* Fill the parameters list */
141 /* --slit_w */
142 p = cpl_parameter_new_value("isaac.isaac_img_slitpos.slit_width",
143 CPL_TYPE_DOUBLE, "Slit width", "isaac.isaac_img_slitpos",
144 20.0);
145 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "slit_w");
146 cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV);
147 cpl_parameterlist_append(recipe->parameters, p);
148 /* --prod */
149 p = cpl_parameter_new_value("isaac.isaac_img_slitpos.products",
150 CPL_TYPE_BOOL, "flag to create the products",
151 "isaac.isaac_img_slitpos", TRUE);
152 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "prod");
153 cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV);
154 cpl_parameterlist_append(recipe->parameters, p);
155
156 /* Return */
157 return 0;
158}
159
160/*----------------------------------------------------------------------------*/
166/*----------------------------------------------------------------------------*/
167static int isaac_img_slitpos_exec(cpl_plugin * plugin)
168{
169 cpl_recipe * recipe;
170
171 /* Get the recipe out of the plugin */
172 if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
173 recipe = (cpl_recipe *)plugin;
174 else return CPL_ERROR_UNSPECIFIED;
175
176 return isaac_img_slitpos(recipe->parameters, recipe->frames);
177}
178
179/*----------------------------------------------------------------------------*/
185/*----------------------------------------------------------------------------*/
186static int isaac_img_slitpos_destroy(cpl_plugin * plugin)
187{
188 cpl_recipe * recipe;
189
190 /* Get the recipe out of the plugin */
191 if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
192 recipe = (cpl_recipe *)plugin;
193 else return CPL_ERROR_UNSPECIFIED;
194
195 cpl_parameterlist_delete(recipe->parameters);
196 return 0;
197}
198
199/*----------------------------------------------------------------------------*/
206/*----------------------------------------------------------------------------*/
207static int isaac_img_slitpos(
208 cpl_parameterlist * parlist,
209 cpl_frameset * framelist)
210{
211 cpl_parameter * par;
212 cpl_imagelist * imlist;
213 cpl_propertylist * plist;
214 cpl_frame * cur_frame;
215 cpl_frameset * slitframes;
216 const char * sval;
217 cpl_table * out_table;
218 int slit_length;
219 int with_dark;
220 int slit_ind;
221 cpl_image * dark;
222 double loc_pi;
223 cpl_boolean did_reduce = CPL_FALSE;
224 int i;
225
226 /* Initialise */
227 isaac_img_slitpos_config.angle = -1.0;
228 isaac_img_slitpos_config.slit_flux = 0.0;
229 isaac_img_slitpos_config.xpos = -1.0;
230 isaac_img_slitpos_config.ypos = -1.0;
231 loc_pi = 3.1415926535897932384626433;
232
233 /* Retrieve input parameters */
234 par = cpl_parameterlist_find(parlist, "isaac.isaac_img_slitpos.slit_width");
235 isaac_img_slitpos_config.slit_width = cpl_parameter_get_double(par);
236 par = cpl_parameterlist_find(parlist, "isaac.isaac_img_slitpos.products");
237 isaac_img_slitpos_config.products_flag = cpl_parameter_get_bool(par);
238
239 /* Identify the RAW and CALIB frames in the input frameset */
240 if (isaac_dfs_set_groups(framelist)) {
241 cpl_msg_error(cpl_func, "Cannot identify RAW and CALIB frames");
242 return CPL_ERROR_UNSPECIFIED;
243 }
244
245 slitframes = isaac_extract_frameset(framelist, ISAAC_IMG_SLITPOS_RAW);
246 if (slitframes == NULL)
247 slitframes = isaac_extract_frameset(framelist,
248 ISAAC_IMG_SLITPOS_CAL_RAW);
249 cpl_ensure_code(slitframes != NULL, CPL_ERROR_DATA_NOT_FOUND);
250
251 /* Load the data */
252 imlist = cpl_imagelist_load_frameset(slitframes, CPL_TYPE_FLOAT, 1, 0);
253 cpl_frameset_delete(slitframes);
254
255 /* Check if the dark frame is there */
256 /* Get FITS header from reference file */
257 cur_frame = cpl_frameset_get_position(framelist, 0);
258 if ((plist=cpl_propertylist_load(cpl_frame_get_filename(cur_frame),
259 0)) == NULL) {
260 cpl_msg_error(cpl_func, "getting header from first frame");
261 cpl_imagelist_delete(imlist);
262 return CPL_ERROR_UNSPECIFIED;
263 }
264 if ((sval = isaac_pfits_get_mode(plist)) == NULL) {
265 cpl_msg_error(cpl_func, "getting the MODE");
266 cpl_propertylist_delete(plist);
267 cpl_imagelist_delete(imlist);
268 return CPL_ERROR_UNSPECIFIED;
269 }
270 with_dark = 0;
271 if (!strcmp(sval, "SW_DARK") || !strcmp(sval, "LW_DARK")) {
272 dark = cpl_image_duplicate(cpl_imagelist_get(imlist, 0));
273 cpl_imagelist_subtract_image(imlist, dark);
274 cpl_image_delete(dark);
275 with_dark = 1;
276 }
277 cpl_propertylist_delete(plist);
278
279 /* Reduce each frame separately */
280 for (i=0; i<cpl_imagelist_get_size(imlist); i++) {
281 cpl_errorstate prestate = cpl_errorstate_get();
282 if(i==0 && with_dark) continue;
283 if (with_dark) slit_ind = i-1;
284 else slit_ind = i;
285 /* Slit analysis */
286 cpl_msg_info(cpl_func, "Analyse slit nb %d", slit_ind+1);
287 cpl_msg_indent_more();
288 if ((out_table = irplib_slitpos_analysis(
289 cpl_imagelist_get(imlist, i),
290 isaac_img_slitpos_config.slit_width,
291 &(isaac_img_slitpos_config.slit_flux))) == NULL) {
292 cpl_msg_error(cpl_func, "Could not analyse the slit in image "
293 "%d/%d", i+1, (int)cpl_imagelist_get_size(imlist));
294 cpl_errorstate_dump(prestate, CPL_FALSE, NULL);
295 cpl_errorstate_set(prestate);
296 cpl_msg_indent_less();
297 continue;
298 }
299
300 /* Find the slit angle in degrees with the horizontal axis */
301 slit_length = cpl_table_get_nrow(out_table);
302 /* THE ANGLE */
303 isaac_img_slitpos_config.angle = (double)atan(
304 (double)(cpl_table_get_int(out_table,"SLIT_Y",slit_length-1, NULL) -
305 cpl_table_get_int(out_table, "SLIT_Y", 0, NULL)) /
306 (cpl_table_get_double(out_table,"SLIT_CENTER",slit_length-1, NULL) -
307 cpl_table_get_double(out_table, "SLIT_CENTER", 0, NULL)));
308 isaac_img_slitpos_config.angle *= 180.0/loc_pi;
309 if (isaac_img_slitpos_config.angle >= 0.00 &&
310 isaac_img_slitpos_config.angle < 90.0)
311 isaac_img_slitpos_config.angle += 180.0;
312 /* THE SLIT LENGTH */
313 isaac_img_slitpos_config.slit_length = slit_length;
314 /* THE SLIT CENTER POSITION */
315 isaac_img_slitpos_config.xpos =
316 (cpl_table_get_double(out_table,"SLIT_CENTER",0,NULL) +
317 cpl_table_get_double(out_table,"SLIT_CENTER",slit_length-1,NULL))
318 / 2.0;
319 isaac_img_slitpos_config.ypos =
320 (double)(cpl_table_get_int(out_table,"SLIT_Y",0,NULL) +
321 cpl_table_get_int(out_table,"SLIT_Y",slit_length-1,NULL))
322 / 2.0;
323
324 /* Print the results */
325 cpl_msg_info(cpl_func, "Slit flux : %g",
326 isaac_img_slitpos_config.slit_flux);
327 cpl_msg_info(cpl_func, "Slit angle : %g",
328 isaac_img_slitpos_config.angle);
329 cpl_msg_info(cpl_func, "Slit length : %d",
330 isaac_img_slitpos_config.slit_length);
331 cpl_msg_info(cpl_func, "Slit center : %g %g",
332 isaac_img_slitpos_config.xpos,
333 isaac_img_slitpos_config.ypos);
334 cpl_msg_indent_less();
335
336 /* Save the product */
337 if (isaac_img_slitpos_config.products_flag) {
338 cpl_msg_info(cpl_func, "Save the product");
339 cpl_msg_indent_more();
340 if (isaac_img_slitpos_save(out_table, slit_ind+1, parlist,
341 framelist) == -1) {
342 cpl_msg_error(cpl_func, "Cannot save the products");
343 cpl_imagelist_delete(imlist);
344 cpl_table_delete(out_table);
345 cpl_msg_indent_less();
346 return CPL_ERROR_UNSPECIFIED;
347 }
348 cpl_msg_indent_less();
349 }
350 cpl_table_delete(out_table);
351 if (!cpl_error_get_code()) did_reduce = CPL_TRUE;
352 }
353
354 /* Free and return */
355 cpl_imagelist_delete(imlist);
356
357 cpl_ensure_code(did_reduce, CPL_ERROR_ILLEGAL_INPUT);
358
359 return cpl_error_set_where(cpl_func); /* Propagate error, if any */
360}
361
362/*----------------------------------------------------------------------------*/
371/*----------------------------------------------------------------------------*/
372static int isaac_img_slitpos_save(
373 cpl_table * out_table,
374 int file_nb,
375 cpl_parameterlist * parlist,
376 cpl_frameset * set)
377{
378 cpl_propertylist * plist;
379 cpl_propertylist * paflist;
380 cpl_propertylist * qclist;
381 /* Get the reference frame */
382 const cpl_frame * ref_frame = cpl_frameset_get_position_const(set, file_nb-1);
383 char * filename;
384
385 /* Get the QC params in qclist */
386 qclist = cpl_propertylist_new();
387 cpl_propertylist_append_double(qclist, "ESO QC SLIT FLUX",
388 isaac_img_slitpos_config.slit_flux);
389 cpl_propertylist_append_double(qclist, "ESO QC SLIT POSANG",
390 isaac_img_slitpos_config.angle);
391 cpl_propertylist_append_double(qclist, "ESO QC SLIT XPOS",
392 isaac_img_slitpos_config.xpos);
393 cpl_propertylist_append_double(qclist, "ESO QC SLIT YPOS",
394 isaac_img_slitpos_config.ypos);
395
396 cpl_propertylist_append_string(qclist, CPL_DFS_PRO_CATG,
397 ISAAC_IMG_SLITPOS_RES);
398
399 /* Write the table */
400 filename = cpl_sprintf("isaac_img_slitpos_%02d.fits", file_nb);
401
402 cpl_dfs_save_table(set, NULL, parlist, set, ref_frame, out_table,
403 NULL, "isaac_img_slitpos", qclist, NULL,
404 PACKAGE "/" PACKAGE_VERSION, filename);
405 cpl_free(filename);
406
407 /* Get FITS header from reference file */
408 if ((plist=cpl_propertylist_load(cpl_frame_get_filename(ref_frame),
409 0)) == NULL) {
410 cpl_msg_error(cpl_func, "getting header from reference frame");
411 cpl_propertylist_delete(qclist);
412 return CPL_ERROR_UNSPECIFIED;
413 }
414
415 /* Get the keywords for the paf file */
416 paflist = cpl_propertylist_new();
417 cpl_propertylist_copy_property_regexp(paflist, plist,
418 "^(ARCFILE|ORIGFILE|MJD-OBS|INSTRUME|ESO TPL ID|ESO TPL NEXP|"
419 "ESO TPL EXPNO|ESO DPR CATG|ESO DPR TECH|ESO DPR TYPE|DATE-OBS|"
420 "ESO INS OPTI1 ID)$",0);
421 cpl_propertylist_delete(plist);
422
423 /* Copy the QC in paflist */
424 cpl_propertylist_copy_property_regexp(paflist, qclist, ".", 0);
425 cpl_propertylist_delete(qclist);
426
427 /* PRO.CATG */
428 cpl_propertylist_update_string(paflist, CPL_DFS_PRO_CATG,
429 ISAAC_IMG_SLITPOS_RES);
430
431 /* Save the PAF file */
432 filename = cpl_sprintf("isaac_img_slitpos_%02d.paf", file_nb);
433 cpl_dfs_save_paf("ISAAC",
434 "isaac_img_slitpos",
435 paflist,
436 filename);
437 cpl_free(filename);
438 cpl_propertylist_delete(paflist);
439 return 0;
440}
441
int isaac_dfs_set_groups(cpl_frameset *set)
Set the group as RAW or CALIB in a frameset.
Definition: isaac_dfs.c:60
const char * isaac_pfits_get_mode(const cpl_propertylist *plist)
find out the instrument mode
Definition: isaac_pfits.c:537
cpl_frameset * isaac_extract_frameset(const cpl_frameset *self, const char *tag)
Extract the frames with the given tag from a frameset.
Definition: isaac_utils.c:356
const char * isaac_get_license(void)
Get the pipeline copyright and license.
Definition: isaac_utils.c:62