VIRCAM Pipeline 2.3.15
vircam_darkcor.c
1/* $Id: vircam_darkcor.c,v 1.13 2012-01-16 14:44:02 jim Exp $
2 *
3 * This file is part of the VIRCAM Pipeline
4 * Copyright (C) 2015 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: jim $
23 * $Date: 2012-01-16 14:44:02 $
24 * $Revision: 1.13 $
25 * $Name: not supported by cvs2svn $
26 */
27
28/* Includes */
29
30#ifdef HAVE_CONFIG_H
31#include <config.h>
32#endif
33
34#include <stdio.h>
35#include <cpl.h>
36
37#include <casu_utils.h>
38#include <casu_mods.h>
39
40#include "vircam_utils.h"
41#include "vircam_dfs.h"
42#include "vircam_mods.h"
43
44/* Function prototypes */
45
46static int vircam_darkcor_create(cpl_plugin *) ;
47static int vircam_darkcor_exec(cpl_plugin *) ;
48static int vircam_darkcor_destroy(cpl_plugin *) ;
49static int vircam_darkcor_test(cpl_parameterlist *, cpl_frameset *) ;
50static int vircam_darkcor_save(cpl_frameset *framelist,
51 cpl_parameterlist *parlist);
52static void vircam_darkcor_init(void);
53static void vircam_darkcor_tidy(void);
54
55static struct {
56
57 /* Input */
58
59 float darkscl;
60 int extenum;
61
62} vircam_darkcor_config;
63
64static struct {
65 cpl_size *labels;
66 cpl_frame *dark;
67 cpl_frame *img;
68 casu_fits *darkf;
69 casu_fits *imgf;
70} ps;
71
72static int isfirst;
73static cpl_frame *product_frame = NULL;
74
75
76static char vircam_darkcor_description[] =
77"vircam_darkcor -- VIRCAM dark correction test recipe.\n\n"
78"Dark correct an input frame using a master dark frame\n\n"
79"The program accepts the following files in the SOF:\n\n"
80" Tag Description\n"
81" -----------------------------------------------------------------------\n"
82" %-21s A input uncorrected image\n"
83" %-21s A master dark frame\n"
84"\n";
85
131/* Function code */
132
133
134/*---------------------------------------------------------------------------*/
142/*---------------------------------------------------------------------------*/
143
144int cpl_plugin_get_info(cpl_pluginlist *list) {
145 cpl_recipe *recipe = cpl_calloc(1,sizeof(*recipe));
146 cpl_plugin *plugin = &recipe->interface;
147 char alldesc[SZ_ALLDESC];
148 (void)snprintf(alldesc,SZ_ALLDESC,vircam_darkcor_description,
149 VIRCAM_TEST_SCIENCE_RAW,VIRCAM_CAL_DARK);
150
151 cpl_plugin_init(plugin,
152 CPL_PLUGIN_API,
153 VIRCAM_BINARY_VERSION,
154 CPL_PLUGIN_TYPE_RECIPE,
155 "vircam_darkcor",
156 "VIRCAM dark correction test recipe [test]",
157 alldesc,
158 "Jim Lewis",
159 "jrl@ast.cam.ac.uk",
161 vircam_darkcor_create,
162 vircam_darkcor_exec,
163 vircam_darkcor_destroy);
164
165 cpl_pluginlist_append(list,plugin);
166
167 return(0);
168}
169
170/*---------------------------------------------------------------------------*/
179/*---------------------------------------------------------------------------*/
180
181static int vircam_darkcor_create(cpl_plugin *plugin) {
182 cpl_recipe *recipe;
183 cpl_parameter *p;
184
185 /* Get the recipe out of the plugin */
186
187 if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
188 recipe = (cpl_recipe *)plugin;
189 else
190 return(-1);
191
192 /* Create the parameters list in the cpl_recipe object */
193
194 recipe->parameters = cpl_parameterlist_new();
195
196 /* Fill in the parameters. First the dark scaling factor */
197
198 p = cpl_parameter_new_value("vircam.vircam_darkcor.darkscl",
199 CPL_TYPE_DOUBLE,
200 "Dark correction scale factor",
201 "vircam.vircam_darkcor",1.0);
202 cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI,"darkscl");
203 cpl_parameterlist_append(recipe->parameters,p);
204
205 /* Extension number of input frames to use */
206
207 p = cpl_parameter_new_range("vircam.vircam_darkcor.extenum",
208 CPL_TYPE_INT,
209 "Extension number to be done, 0 == all",
210 "vircam.vircam_darkcor",1,0,16);
211 cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI,"ext");
212 cpl_parameterlist_append(recipe->parameters,p);
213
214 /* Get out of here */
215
216 return(0);
217}
218
219/*---------------------------------------------------------------------------*/
225/*---------------------------------------------------------------------------*/
226
227static int vircam_darkcor_exec(cpl_plugin *plugin) {
228 cpl_recipe *recipe;
229
230 /* Get the recipe out of the plugin */
231
232 if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
233 recipe = (cpl_recipe *)plugin;
234 else
235 return(-1);
236
237 return(vircam_darkcor_test(recipe->parameters,recipe->frames));
238}
239
240/*---------------------------------------------------------------------------*/
246/*---------------------------------------------------------------------------*/
247
248static int vircam_darkcor_destroy(cpl_plugin *plugin) {
249 cpl_recipe *recipe ;
250
251 /* Get the recipe out of the plugin */
252
253 if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
254 recipe = (cpl_recipe *)plugin;
255 else
256 return(-1);
257
258 cpl_parameterlist_delete(recipe->parameters);
259 return(0);
260}
261
262/*---------------------------------------------------------------------------*/
269/*---------------------------------------------------------------------------*/
270
271static int vircam_darkcor_test(cpl_parameterlist *parlist,
272 cpl_frameset *framelist) {
273 const char *fctid="vircam_darkcor";
274 cpl_parameter *p;
275 int jst,jfn,status,j;
276 cpl_size nlab;
277
278 /* Check validity of input frameset */
279
280 if (framelist == NULL || cpl_frameset_get_size(framelist) <= 0) {
281 cpl_msg_error(fctid,"Input framelist NULL or has no input data");
282 return(-1);
283 }
284
285 /* Initialise some things */
286
287 vircam_darkcor_init();
288
289 /* Get the parameters */
290
291 p = cpl_parameterlist_find(parlist,"vircam.vircam_darkcor.darkscl");
292 vircam_darkcor_config.darkscl = cpl_parameter_get_double(p);
293 p = cpl_parameterlist_find(parlist,"vircam.vircam_darkcor.extenum");
294 vircam_darkcor_config.extenum = cpl_parameter_get_int(p);
295
296 /* Sort out raw from calib frames */
297
298 if (vircam_dfs_set_groups(framelist) != CASU_OK) {
299 cpl_msg_error(fctid,"Cannot identify RAW and CALIB frames");
300 vircam_darkcor_tidy();
301 return(-1);
302 }
303
304 /* Get the frames */
305
306 if ((ps.labels = cpl_frameset_labelise(framelist,casu_compare_tags,
307 &nlab)) == NULL) {
308 cpl_msg_error(fctid,"Cannot labelise the input frames");
309 vircam_darkcor_tidy();
310 return(-1);
311 }
312 if ((ps.dark = casu_frameset_subgroup_1(framelist,ps.labels,nlab,
313 VIRCAM_CAL_DARK)) == NULL) {
314 cpl_msg_info(fctid,"No master dark found -- cannot continue");
315 vircam_darkcor_tidy();
316 return(-1);
317 }
318 if ((ps.img = casu_frameset_subgroup_1(framelist,ps.labels,nlab,
319 VIRCAM_TEST_SCIENCE_RAW)) == NULL) {
320 cpl_msg_info(fctid,"No raw image found -- cannot continue");
321 vircam_darkcor_tidy();
322 return(-1);
323 }
324
325 /* Now, how many image extensions do we want to do? If the extension
326 number is zero, then we loop for all possible extensions. If it
327 isn't then we just do the extension specified */
328
329 vircam_exten_range(vircam_darkcor_config.extenum,(const cpl_frame *)ps.img,
330 &jst,&jfn);
331 if (jst == -1 || jfn == -1) {
332 cpl_msg_error(fctid,"Unable to continue");
333 vircam_darkcor_tidy();
334 return(-1);
335 }
336
337 /* Now loop for all the extension... */
338
339 status = CASU_OK;
340 for (j = jst; j <= jfn; j++) {
341 isfirst = (j == jst);
342
343 /* Load up the images */
344
345 ps.imgf = casu_fits_load(ps.img,CPL_TYPE_FLOAT,j);
346 ps.darkf = casu_fits_load(ps.dark,CPL_TYPE_FLOAT,j);
347 if (ps.img == NULL || ps.darkf == NULL) {
348 vircam_darkcor_tidy();
349 return(-1);
350 }
351
352 /* Now do the correction */
353
354 cpl_msg_info(fctid,"Doing the dark correction for extension %" CPL_SIZE_FORMAT,
355 (cpl_size)j);
356 (void)casu_darkcor(ps.imgf,ps.darkf,vircam_darkcor_config.darkscl,
357 &status);
358 if (status != CASU_OK) {
359 vircam_darkcor_tidy();
360 return(-1);
361 }
362
363 /* Now save the result */
364
365 cpl_msg_info(fctid,"Saving results for extension %" CPL_SIZE_FORMAT,
366 (cpl_size)j);
367 if (vircam_darkcor_save(framelist,parlist) != 0) {
368 vircam_darkcor_tidy();
369 return(-1);
370 }
371
372 /* Tidy a few things before the next image */
373
374 freefits(ps.imgf);
375 freefits(ps.darkf);
376 }
377 vircam_darkcor_tidy();
378 return(0);
379}
380
381/*---------------------------------------------------------------------------*/
388/*---------------------------------------------------------------------------*/
389
390static int vircam_darkcor_save(cpl_frameset *framelist,
391 cpl_parameterlist *parlist) {
392 const char *fctid = "vircam_darkcor_save";
393 const char *outfile = "darkcor.fits";
394 const char *recipeid = "vircam_darkcor";
395 cpl_propertylist *plist;
396
397 /* If we need to make a PHU then do that now based on the first frame
398 in the input frame list */
399
400 if (isfirst) {
401
402 /* Create a new product frame object and define some tags */
403
404 product_frame = cpl_frame_new();
405 cpl_frame_set_filename(product_frame,outfile);
406 cpl_frame_set_tag(product_frame,VIRCAM_PRO_SIMPLE_TEST);
407 cpl_frame_set_type(product_frame,CPL_FRAME_TYPE_IMAGE);
408 cpl_frame_set_group(product_frame,CPL_FRAME_GROUP_PRODUCT);
409 cpl_frame_set_level(product_frame,CPL_FRAME_LEVEL_FINAL);
410
411 /* Set up the header */
412
413 plist = casu_fits_get_phu(ps.imgf);
414 vircam_dfs_set_product_primary_header(plist,product_frame,framelist,
415 parlist,(char *)recipeid,
416 "?Dictionary?",NULL,0);
417
418 /* 'Save' the PHU image */
419
420 if (cpl_image_save(NULL,outfile,CPL_TYPE_UCHAR,plist,
421 CPL_IO_DEFAULT) != CPL_ERROR_NONE) {
422 cpl_msg_error(fctid,"Cannot save product PHU");
423 cpl_frame_delete(product_frame);
424 return(-1);
425 }
426 cpl_frameset_insert(framelist,product_frame);
427 }
428
429 /* Get the extension property list */
430
431 plist = casu_fits_get_ehu(ps.imgf);
432
433 /* Fiddle with the header now */
434
435 vircam_dfs_set_product_exten_header(plist,product_frame,framelist,parlist,
436 (char *)recipeid,"?Dictionary?",NULL);
437
438 /* Save the image */
439
440 if (cpl_image_save(casu_fits_get_image(ps.imgf),outfile,CPL_TYPE_FLOAT,
441 plist,CPL_IO_EXTEND) != CPL_ERROR_NONE) {
442 cpl_msg_error(fctid,"Cannot save product image extension");
443 return(-1);
444 }
445
446 return(0);
447}
448
449
450/*---------------------------------------------------------------------------*/
454/*---------------------------------------------------------------------------*/
455
456static void vircam_darkcor_init(void) {
457 ps.labels = NULL;
458 ps.dark = NULL;
459 ps.darkf = NULL;
460 ps.img = NULL;
461 ps.imgf = NULL;
462}
463
464
465/*---------------------------------------------------------------------------*/
469/*---------------------------------------------------------------------------*/
470
471static void vircam_darkcor_tidy(void) {
472 freespace(ps.labels);
473 freefits(ps.imgf);
474 freefits(ps.darkf);
475 freeframe(ps.dark);
476 freeframe(ps.img);
477}
478
481/*
482
483$Log: not supported by cvs2svn $
484Revision 1.12 2012/01/15 17:40:09 jim
485Minor modifications to take into accout the changes in cpl API for v6
486
487Revision 1.11 2009/09/09 09:51:13 jim
488modified to use new saving routines so that headers are right
489
490Revision 1.10 2007/10/15 12:53:55 jim
491Modified for compatibility with cpl_4.0
492
493Revision 1.9 2007/07/09 13:22:08 jim
494Modified to use new version of vircam_exten_range
495
496Revision 1.8 2007/04/13 12:27:38 jim
497Added some extra docs
498
499Revision 1.7 2007/04/04 10:36:29 jim
500Modified to use new dfs tags
501
502Revision 1.6 2007/03/01 12:42:58 jim
503Modified slightly after code checking
504
505Revision 1.5 2006/06/15 09:58:59 jim
506Minor changes to docs
507
508Revision 1.4 2006/05/04 11:53:40 jim
509Fixed _save routine so that it's more consistent with the standard CPL
510way of doing things
511
512Revision 1.3 2006/05/02 11:29:11 jim
513Fixed problem where propertylist was being deleted incorrectly
514
515Revision 1.2 2006/04/27 14:22:04 jim
516Fixed docs
517
518Revision 1.1 2006/04/24 10:42:44 jim
519New routine
520
521
522*/
523
524
525
526
cpl_image * casu_fits_get_image(casu_fits *p)
Definition: casu_fits.c:436
cpl_propertylist * casu_fits_get_phu(casu_fits *p)
Definition: casu_fits.c:531
cpl_propertylist * casu_fits_get_ehu(casu_fits *p)
Definition: casu_fits.c:576
casu_fits * casu_fits_load(cpl_frame *frame, cpl_type type, int nexten)
Definition: casu_fits.c:80
int casu_darkcor(casu_fits *infile, casu_fits *darksrc, float darkscl, int *status)
Correct input data for dark current.
Definition: casu_darkcor.c:86
int casu_compare_tags(const cpl_frame *frame1, const cpl_frame *frame2)
Compare input tags.
Definition: casu_utils.c:96
cpl_frame * casu_frameset_subgroup_1(cpl_frameset *frameset, cpl_size *labels, cpl_size nlab, const char *tag)
Extract a frame of a given label from a frameset.
Definition: casu_utils.c:206
int vircam_dfs_set_groups(cpl_frameset *set)
Definition: vircam_dfs.c:115
void vircam_dfs_set_product_primary_header(cpl_propertylist *plist, cpl_frame *frame, cpl_frameset *frameset, cpl_parameterlist *parlist, char *recipeid, const char *dict, cpl_frame *inherit, int synch)
Definition: vircam_dfs.c:227
void vircam_dfs_set_product_exten_header(cpl_propertylist *plist, cpl_frame *frame, cpl_frameset *frameset, cpl_parameterlist *parlist, char *recipeid, const char *dict, cpl_frame *inherit)
Definition: vircam_dfs.c:299
const char * vircam_get_license(void)
Definition: vircam_utils.c:116
void vircam_exten_range(int inexten, const cpl_frame *fr, int *out1, int *out2)
Definition: vircam_utils.c:165