VIRCAM Pipeline 2.3.15
vircam_mkconf.c
1/* $Id: vircam_mkconf.c,v 1.14 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.14 $
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_mkconf_create(cpl_plugin *) ;
47static int vircam_mkconf_exec(cpl_plugin *) ;
48static int vircam_mkconf_destroy(cpl_plugin *) ;
49static int vircam_mkconf_test(cpl_parameterlist *, cpl_frameset *) ;
50static int vircam_mkconf_save(cpl_frameset *framelist,
51 cpl_parameterlist *parlist);
52static void vircam_mkconf_init(void);
53static void vircam_mkconf_tidy(void);
54
55static struct {
56
57 /* Input */
58
59 int extenum;
60
61} vircam_mkconf_config;
62
63static struct {
64 cpl_size *labels;
65 cpl_frame *flat;
66 casu_mask *bpm;
67 casu_fits *flatf;
68 cpl_image *outimg;
69 cpl_propertylist *drs;
70} ps;
71
72static int isfirst;
73static cpl_frame *product_frame = NULL;
74
75static char vircam_mkconf_description[] =
76"vircam_mkconf -- VIRCAM confidence map generation test recipe.\n\n"
77"Create a confidence map using an input flat field and an input mask\n"
78"The program accepts the following files in the SOF:\n\n"
79" Tag Description\n"
80" -----------------------------------------------------------------------\n"
81" %-21s A input master flat field\n"
82" %-21s A master bad pixel map or\n"
83" %-21s A master confidence map\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_mkconf_description,
149 VIRCAM_CAL_TWILIGHT_FLAT,VIRCAM_CAL_BPM,VIRCAM_CAL_CONF);
150
151 cpl_plugin_init(plugin,
152 CPL_PLUGIN_API,
153 VIRCAM_BINARY_VERSION,
154 CPL_PLUGIN_TYPE_RECIPE,
155 "vircam_mkconf",
156 "VIRCAM confidence map test recipe [test]",
157 alldesc,
158 "Jim Lewis",
159 "jrl@ast.cam.ac.uk",
161 vircam_mkconf_create,
162 vircam_mkconf_exec,
163 vircam_mkconf_destroy);
164
165 cpl_pluginlist_append(list,plugin);
166
167 return(0);
168}
169
170/*---------------------------------------------------------------------------*/
179/*---------------------------------------------------------------------------*/
180
181static int vircam_mkconf_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 /* Extension number of input frames to use */
197
198 p = cpl_parameter_new_range("vircam.vircam_mkconf.extenum",
199 CPL_TYPE_INT,
200 "Extension number to be done, 0 == all",
201 "vircam.vircam_mkconf",1,0,16);
202 cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI,"ext");
203 cpl_parameterlist_append(recipe->parameters,p);
204
205 /* Get out of here */
206
207 return(0);
208}
209
210/*---------------------------------------------------------------------------*/
216/*---------------------------------------------------------------------------*/
217
218static int vircam_mkconf_exec(cpl_plugin *plugin) {
219 cpl_recipe *recipe;
220
221 /* Get the recipe out of the plugin */
222
223 if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
224 recipe = (cpl_recipe *)plugin;
225 else
226 return(-1);
227
228 return(vircam_mkconf_test(recipe->parameters,recipe->frames));
229}
230
231/*---------------------------------------------------------------------------*/
237/*---------------------------------------------------------------------------*/
238
239static int vircam_mkconf_destroy(cpl_plugin *plugin) {
240 cpl_recipe *recipe ;
241
242 /* Get the recipe out of the plugin */
243
244 if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
245 recipe = (cpl_recipe *)plugin;
246 else
247 return(-1);
248
249 cpl_parameterlist_delete(recipe->parameters);
250 return(0);
251}
252
253/*---------------------------------------------------------------------------*/
260/*---------------------------------------------------------------------------*/
261
262static int vircam_mkconf_test(cpl_parameterlist *parlist,
263 cpl_frameset *framelist) {
264 const char *fctid="vircam_mkconf";
265 cpl_parameter *p;
266 int jst,jfn,status,j,nx,ny;
267 cpl_size nlab;
268
269 /* Check validity of input frameset */
270
271 if (framelist == NULL || cpl_frameset_get_size(framelist) <= 0) {
272 cpl_msg_error(fctid,"Input framelist NULL or has no input data");
273 return(-1);
274 }
275
276 /* Initialise some things */
277
278 vircam_mkconf_init();
279
280 /* Get the parameters */
281
282 p = cpl_parameterlist_find(parlist,"vircam.vircam_mkconf.extenum");
283 vircam_mkconf_config.extenum = cpl_parameter_get_int(p);
284
285 /* Sort out raw from calib frames */
286
287 if (vircam_dfs_set_groups(framelist) != CASU_OK) {
288 cpl_msg_error(fctid,"Cannot identify RAW and CALIB frames");
289 vircam_mkconf_tidy();
290 return(-1);
291 }
292
293 /* Get the flat */
294
295 if ((ps.labels = cpl_frameset_labelise(framelist,casu_compare_tags,
296 &nlab)) == NULL) {
297 cpl_msg_error(fctid,"Cannot labelise the input frames");
298 vircam_mkconf_tidy();
299 return(-1);
300 }
301 if ((ps.flat = casu_frameset_subgroup_1(framelist,ps.labels,nlab,
302 VIRCAM_CAL_TWILIGHT_FLAT)) == NULL) {
303 cpl_msg_info(fctid,"No master flat found -- cannot continue");
304 vircam_mkconf_tidy();
305 return(-1);
306 }
307
308 /* Get the master mask */
309
310 ps.bpm = casu_mask_define(framelist,ps.labels,nlab,VIRCAM_CAL_CONF,
311 VIRCAM_CAL_BPM);
312
313 /* Now, how many image extensions do we want to do? If the extension
314 number is zero, then we loop for all possible extensions. If it
315 isn't then we just do the extension specified */
316
317 vircam_exten_range(vircam_mkconf_config.extenum,(const cpl_frame *)ps.flat,
318 &jst,&jfn);
319 if (jst == -1 || jfn == -1) {
320 cpl_msg_error(fctid,"Unable to continue");
321 vircam_mkconf_tidy();
322 return(-1);
323 }
324
325 /* Now loop for all the extension... */
326
327 status = CASU_OK;
328 for (j = jst; j <= jfn; j++) {
329 isfirst = (j == jst);
330
331 /* Load up the images */
332
333 ps.flatf = casu_fits_load(ps.flat,CPL_TYPE_FLOAT,j);
334 if (ps.flatf == NULL) {
335 vircam_mkconf_tidy();
336 return(-1);
337 }
338
339 /* Get the size of the flat image */
340
341 nx = (int)cpl_image_get_size_x(casu_fits_get_image(ps.flatf));
342 ny = (int)cpl_image_get_size_y(casu_fits_get_image(ps.flatf));
343
344 /* Load the data for the bpm */
345
346 (void)casu_mask_load(ps.bpm,j,nx,ny);
347
348 /* Now do the correction */
349
350 cpl_msg_info(fctid,"Making confidence map for extension %" CPL_SIZE_FORMAT,
351 (cpl_size)j);
352 (void)casu_mkconf(casu_fits_get_image(ps.flatf),
353 casu_fits_get_fullname(ps.flatf),ps.bpm,
354 &(ps.outimg),&(ps.drs),&status);
355 if (status != CASU_OK) {
356 vircam_mkconf_tidy();
357 return(-1);
358 }
359
360 /* Now save the result */
361
362 cpl_msg_info(fctid,"Saving results for extension %" CPL_SIZE_FORMAT,
363 (cpl_size)j);
364 if (vircam_mkconf_save(framelist,parlist) != 0) {
365 vircam_mkconf_tidy();
366 return(-1);
367 }
368
369 /* Tidy a few things before the next image */
370
371 freefits(ps.flatf);
372 casu_mask_clear(ps.bpm);
373 freeimage(ps.outimg);
374 freepropertylist(ps.drs);
375 }
376 vircam_mkconf_tidy();
377 return(0);
378}
379
380/*---------------------------------------------------------------------------*/
387/*---------------------------------------------------------------------------*/
388
389static int vircam_mkconf_save(cpl_frameset *framelist,
390 cpl_parameterlist *parlist) {
391 const char *recipeid = "vircam_mkconf";
392 const char *fctid = "vircam_mkconf_save";
393 const char *outfile = "mkconf.fits";
394 cpl_propertylist *plist;
395
396 /* If we need to make a PHU then do that now based on the first frame
397 in the input frame list */
398
399 if (isfirst) {
400
401 /* Create a new product frame object and define some tags */
402
403 product_frame = cpl_frame_new();
404 cpl_frame_set_filename(product_frame,outfile);
405 cpl_frame_set_tag(product_frame,VIRCAM_PRO_CONF_TEST);
406 cpl_frame_set_type(product_frame,CPL_FRAME_TYPE_IMAGE);
407 cpl_frame_set_group(product_frame,CPL_FRAME_GROUP_PRODUCT);
408 cpl_frame_set_level(product_frame,CPL_FRAME_LEVEL_FINAL);
409
410 /* Create product frame phu header */
411
412 plist = casu_fits_get_phu(ps.flatf);
413 vircam_dfs_set_product_primary_header(plist,product_frame,framelist,
414 parlist,(char *)recipeid,
415 "?Dictionary?",NULL,0);
416
417 /* 'Save' the PHU image */
418
419 if (cpl_image_save(NULL,outfile,CPL_TYPE_UCHAR,plist,
420 CPL_IO_DEFAULT) != CPL_ERROR_NONE) {
421 cpl_msg_error(fctid,"Cannot save product PHU");
422 cpl_frame_delete(product_frame);
423 return(-1);
424 }
425 cpl_frameset_insert(framelist,product_frame);
426 }
427
428 /* Get the extension property list */
429
430 plist = casu_fits_get_ehu(ps.flatf);
431
432 /* Fiddle with the header now */
433
434 vircam_dfs_set_product_exten_header(plist,product_frame,framelist,parlist,
435 (char *)recipeid,"?Dictionary?",NULL);
436
437 /* Save the image */
438
439 if (cpl_image_save(ps.outimg,outfile,CPL_TYPE_FLOAT,plist,
440 CPL_IO_EXTEND) != CPL_ERROR_NONE) {
441 cpl_msg_error(fctid,"Cannot save product image extension");
442 return(-1);
443 }
444
445 return(0);
446}
447
448
449/*---------------------------------------------------------------------------*/
453/*---------------------------------------------------------------------------*/
454
455static void vircam_mkconf_init(void) {
456 ps.labels = NULL;
457 ps.flat = NULL;
458 ps.bpm = NULL;
459 ps.flatf = NULL;
460 ps.outimg = NULL;
461 ps.drs = NULL;
462}
463
464
465/*---------------------------------------------------------------------------*/
469/*---------------------------------------------------------------------------*/
470
471static void vircam_mkconf_tidy(void) {
472 freespace(ps.labels);
473 freeframe(ps.flat);
474 freemask(ps.bpm);
475 freefits(ps.flatf);
476 freeimage(ps.outimg);
477 freepropertylist(ps.drs);
478}
479
482/*
483
484$Log: not supported by cvs2svn $
485Revision 1.13 2012/01/15 17:40:09 jim
486Minor modifications to take into accout the changes in cpl API for v6
487
488Revision 1.12 2009/09/09 09:51:13 jim
489modified to use new saving routines so that headers are right
490
491Revision 1.11 2007/10/15 12:53:55 jim
492Modified for compatibility with cpl_4.0
493
494Revision 1.10 2007/07/09 13:22:09 jim
495Modified to use new version of vircam_exten_range
496
497Revision 1.9 2007/04/23 12:49:34 jim
498Fixed bug where the wrong image was being saved
499
500Revision 1.8 2007/04/13 12:27:39 jim
501Added some extra docs
502
503Revision 1.7 2007/04/04 10:36:29 jim
504Modified to use new dfs tags
505
506Revision 1.6 2007/03/01 12:42:59 jim
507Modified slightly after code checking
508
509Revision 1.5 2006/06/15 09:59:00 jim
510Minor changes to docs
511
512Revision 1.4 2006/05/04 11:53:44 jim
513Fixed _save routine so that it's more consistent with the standard CPL
514way of doing things
515
516Revision 1.3 2006/05/02 11:29:16 jim
517Fixed problem where propertylist was being deleted incorrectly
518
519Revision 1.2 2006/04/27 14:22:06 jim
520Fixed docs
521
522Revision 1.1 2006/04/24 10:42:45 jim
523New routine
524
525
526*/
527
528
529
530
cpl_image * casu_fits_get_image(casu_fits *p)
Definition: casu_fits.c:436
char * casu_fits_get_fullname(casu_fits *p)
Definition: casu_fits.c:680
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_mask_load(casu_mask *m, int nexten, int nx, int ny)
Definition: casu_mask.c:214
void casu_mask_clear(casu_mask *m)
Definition: casu_mask.c:357
casu_mask * casu_mask_define(cpl_frameset *framelist, cpl_size *labels, cpl_size nlab, const char *conftag, const char *bpmtag)
Definition: casu_mask.c:89
int casu_mkconf(cpl_image *flat, char *flatfile, casu_mask *bpm, cpl_image **outconf, cpl_propertylist **drs, int *status)
Create a confidence map.
Definition: casu_mkconf.c:87
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