VIRCAM Pipeline 2.3.15
vircam_imdither.c
1/* $Id: vircam_imdither.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#include <math.h>
37
38#include <casu_utils.h>
39#include <casu_mask.h>
40#include <casu_mods.h>
41
42#include "vircam_utils.h"
43#include "vircam_dfs.h"
44#include "vircam_mods.h"
45
46/* Function prototypes */
47
48static int vircam_imdither_create(cpl_plugin *) ;
49static int vircam_imdither_exec(cpl_plugin *) ;
50static int vircam_imdither_destroy(cpl_plugin *) ;
51static int vircam_imdither_test(cpl_parameterlist *, cpl_frameset *) ;
52static int vircam_imdither_save(cpl_frameset *framelist,
53 cpl_parameterlist *parlist);
54static void vircam_imdither_init(void);
55static void vircam_imdither_tidy(void);
56
57/* Static global variables */
58
59static struct {
60
61 /* Input */
62
63 int extenum;
64
65} vircam_imdither_config;
66
67
68static struct {
69 cpl_size *labels;
70 cpl_frameset *imagelist;
71 casu_fits **images;
72 cpl_frameset *conflist;
73 casu_fits **confs;
74 int nimages;
75 int nconfs;
76 cpl_image *outimage;
77 cpl_image *outconf;
78 cpl_propertylist *plist;
79} ps;
80
81static int isfirst;
82static cpl_frame *product_frame = NULL;
83static cpl_frame *product_conf = NULL;
84
85
86static char vircam_imdither_description[] =
87"vircam_imdither -- VIRCAM test jitter recipe.\n\n"
88"Dither a list of frames into an output frame.\n\n"
89"The program accepts the following files in the SOF:\n\n"
90" Tag Description\n"
91" -----------------------------------------------------------------------\n"
92" %-21s A list of images\n"
93" %-21s A list of confidence maps\n"
94"\n";
95
140/* Function code */
141
142/*---------------------------------------------------------------------------*/
150/*---------------------------------------------------------------------------*/
151
152int cpl_plugin_get_info(cpl_pluginlist *list) {
153 cpl_recipe *recipe = cpl_calloc(1,sizeof(*recipe));
154 cpl_plugin *plugin = &recipe->interface;
155 char alldesc[SZ_ALLDESC];
156 (void)snprintf(alldesc,SZ_ALLDESC,vircam_imdither_description,
157 VIRCAM_TEST_SCIENCE_RAW,VIRCAM_CAL_CONF);
158
159 cpl_plugin_init(plugin,
160 CPL_PLUGIN_API,
161 VIRCAM_BINARY_VERSION,
162 CPL_PLUGIN_TYPE_RECIPE,
163 "vircam_imdither",
164 "VIRCAM jitter test recipe [test]",
165 alldesc,
166 "Jim Lewis",
167 "jrl@ast.cam.ac.uk",
169 vircam_imdither_create,
170 vircam_imdither_exec,
171 vircam_imdither_destroy);
172
173 cpl_pluginlist_append(list,plugin);
174
175 return(0);
176}
177
178/*---------------------------------------------------------------------------*/
187/*---------------------------------------------------------------------------*/
188
189static int vircam_imdither_create(cpl_plugin *plugin) {
190 cpl_recipe *recipe;
191 cpl_parameter *p;
192
193 /* Get the recipe out of the plugin */
194
195 if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
196 recipe = (cpl_recipe *)plugin;
197 else
198 return(-1);
199
200 /* Create the parameters list in the cpl_recipe object */
201
202 recipe->parameters = cpl_parameterlist_new();
203
204 /* Extension number of input frames to use */
205
206 p = cpl_parameter_new_range("vircam.vircam_imdither.extenum",
207 CPL_TYPE_INT,
208 "Extension number to be done, 0 == all",
209 "vircam.vircam_imdither",
210 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
220/*---------------------------------------------------------------------------*/
226/*---------------------------------------------------------------------------*/
227
228static int vircam_imdither_exec(cpl_plugin *plugin) {
229 cpl_recipe *recipe;
230
231 /* Get the recipe out of the plugin */
232
233 if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
234 recipe = (cpl_recipe *)plugin;
235 else
236 return(-1);
237
238 return(vircam_imdither_test(recipe->parameters,recipe->frames));
239}
240
241/*---------------------------------------------------------------------------*/
247/*---------------------------------------------------------------------------*/
248
249static int vircam_imdither_destroy(cpl_plugin *plugin) {
250 cpl_recipe *recipe ;
251
252 /* Get the recipe out of the plugin */
253
254 if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
255 recipe = (cpl_recipe *)plugin;
256 else
257 return(-1);
258
259 cpl_parameterlist_delete(recipe->parameters);
260 return(0);
261}
262
263/*---------------------------------------------------------------------------*/
270/*---------------------------------------------------------------------------*/
271
272static int vircam_imdither_test(cpl_parameterlist *parlist,
273 cpl_frameset *framelist) {
274 const char *fctid="vircam_imdither";
275 int j,jst,jfn,retval,status;
276 cpl_size nlab;
277 cpl_parameter *p;
278
279
280 /* Check validity of input frameset */
281
282 if (framelist == NULL || cpl_frameset_get_size(framelist) <= 0) {
283 cpl_msg_error(fctid,"Input framelist NULL or has no input data");
284 return(-1);
285 }
286
287 /* Initialise some things */
288
289 vircam_imdither_init();
290
291 /* Get the parameters */
292
293 p = cpl_parameterlist_find(parlist,"vircam.vircam_imdither.extenum");
294 vircam_imdither_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_imdither_tidy();
301 return(-1);
302 }
303
304 /* Get the frames 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_imdither_tidy();
310 return(-1);
311 }
312 if ((ps.imagelist = casu_frameset_subgroup(framelist,ps.labels,nlab,
313 VIRCAM_TEST_SCIENCE_RAW)) == NULL) {
314 cpl_msg_error(fctid,"Cannot get images in input frameset");
315 vircam_imdither_tidy();
316 return(-1);
317 }
318 ps.nimages = cpl_frameset_get_size(ps.imagelist);
319 if ((ps.conflist = casu_frameset_subgroup(framelist,ps.labels,nlab,
320 VIRCAM_CAL_CONF)) == NULL) {
321 cpl_msg_error(fctid,"Cannot get confidence maps in input frameset");
322 vircam_imdither_tidy();
323 return(-1);
324 }
325 ps.nconfs = cpl_frameset_get_size(ps.conflist);
326
327 /* Now, how many image extensions do we want to do? If the extension
328 number is zero, then we loop for all possible extensions. If it
329 isn't then we just do the extension specified */
330
331 vircam_exten_range(vircam_imdither_config.extenum,
332 (const cpl_frame *)cpl_frameset_get_position(ps.imagelist,0),
333 &jst,&jfn);
334 if (jst == -1 || jfn == -1) {
335 cpl_msg_error(fctid,"Unable to continue");
336 vircam_imdither_tidy();
337 return(-1);
338 }
339
340 /* Now loop for all the extension... */
341
342 status = CASU_OK;
343 for (j = jst; j <= jfn; j++) {
344 isfirst = (j == jst);
345
346 /* Load the images */
347
348 ps.images = casu_fits_load_list(ps.imagelist,CPL_TYPE_FLOAT,j);
349 ps.confs = casu_fits_load_list(ps.conflist,CPL_TYPE_INT,j);
350
351 /* Call the dithering module */
352
353 cpl_msg_info(fctid,"Doing jittering for extension %" CPL_SIZE_FORMAT,
354 (cpl_size)j);
355 (void)casu_imdither(ps.images,ps.confs,ps.nimages,ps.nconfs,
356 5.0,5.0,&(ps.plist),"EXPTIME",&(ps.outimage),
357 &(ps.outconf),&status);
358 if (status != CASU_OK) {
359 vircam_imdither_tidy();
360 return(-1);
361 }
362
363 /* Save everything */
364
365 cpl_msg_info(fctid,"Saving combined image extension %" CPL_SIZE_FORMAT,
366 (cpl_size)j);
367 retval = vircam_imdither_save(framelist,parlist);
368 if (retval != 0) {
369 vircam_imdither_tidy();
370 return(-1);
371 }
372 freefitslist(ps.images,ps.nimages);
373 freefitslist(ps.confs,ps.nconfs);
374 freeimage(ps.outimage);
375 freeimage(ps.outconf);
376 freepropertylist(ps.plist);
377 }
378 vircam_imdither_tidy();
379 return(0);
380}
381
382
383/*---------------------------------------------------------------------------*/
390/*---------------------------------------------------------------------------*/
391
392static int vircam_imdither_save(cpl_frameset *framelist,
393 cpl_parameterlist *parlist) {
394 cpl_propertylist *plist;
395 const char *recipeid = "vircam_imdither";
396 const char *fctid = "vircam_imdither_save";
397 const char *outfile = "comb.fits";
398 const char *outconf = "combconf.fits";
399
400 /* If we need to make a PHU then do that now based on the first frame
401 in the input frame list */
402
403 if (isfirst) {
404
405 /* Create a new product frame object and define some tags */
406
407 product_frame = cpl_frame_new();
408 cpl_frame_set_filename(product_frame,outfile);
409 cpl_frame_set_tag(product_frame,VIRCAM_PRO_JITTERED_TEST);
410 cpl_frame_set_type(product_frame,CPL_FRAME_TYPE_IMAGE);
411 cpl_frame_set_group(product_frame,CPL_FRAME_GROUP_PRODUCT);
412 cpl_frame_set_level(product_frame,CPL_FRAME_LEVEL_FINAL);
413
414 /* Set up header for phu */
415
416 plist = casu_fits_get_phu(ps.images[0]);
417 vircam_dfs_set_product_primary_header(plist,product_frame,framelist,
418 parlist,(char *)recipeid,
419 "?Dictionary?",NULL,0);
420
421 /* 'Save' the PHU dithered image */
422
423 if (cpl_image_save(NULL,outfile,CPL_TYPE_UCHAR,plist,
424 CPL_IO_DEFAULT) != CPL_ERROR_NONE) {
425 cpl_msg_error(fctid,"Cannot save product PHU");
426 cpl_frame_delete(product_frame);
427 return(-1);
428 }
429 cpl_frameset_insert(framelist,product_frame);
430
431 /* Create a new product frame object and define some tags */
432
433 product_conf = cpl_frame_new();
434 cpl_frame_set_filename(product_conf,outconf);
435 cpl_frame_set_tag(product_conf,VIRCAM_PRO_CONF_TEST);
436 cpl_frame_set_type(product_conf,CPL_FRAME_TYPE_IMAGE);
437 cpl_frame_set_group(product_conf,CPL_FRAME_GROUP_PRODUCT);
438 cpl_frame_set_level(product_conf,CPL_FRAME_LEVEL_FINAL);
439
440 /* 'Save' the PHU confidence map */
441
442 if (cpl_image_save(NULL,outconf,CPL_TYPE_UCHAR,plist,
443 CPL_IO_DEFAULT) != CPL_ERROR_NONE) {
444 cpl_msg_error(fctid,"Cannot save product PHU");
445 cpl_frame_delete(product_conf);
446 return(-1);
447 }
448 cpl_frameset_insert(framelist,product_conf);
449 }
450
451 /* Get the extension property list */
452
453 plist = ps.plist;
454
455 /* Fiddle with the header now */
456
457 vircam_dfs_set_product_exten_header(plist,product_frame,framelist,
458 parlist,(char *)recipeid,
459 "?Dictionary?",NULL);
460
461 /* Now save the dithered image extension */
462
463 if (cpl_image_save(ps.outimage,outfile,CPL_TYPE_FLOAT,plist,
464 CPL_IO_EXTEND) != CPL_ERROR_NONE) {
465 cpl_msg_error(fctid,"Cannot save dithered image extension");
466 return(-1);
467 }
468
469 /* And the confidence map */
470
471 if (cpl_image_save(ps.outconf,outconf,CPL_TYPE_SHORT,plist,
472 CPL_IO_EXTEND) != CPL_ERROR_NONE) {
473 cpl_msg_error(fctid,"Cannot save confidence map image extension");
474 return(-1);
475 }
476
477 /* Get out of here */
478
479 return(0);
480}
481
482/*---------------------------------------------------------------------------*/
486/*---------------------------------------------------------------------------*/
487
488static void vircam_imdither_init(void) {
489 ps.labels = NULL;
490 ps.imagelist = NULL;
491 ps.images = NULL;
492 ps.conflist = NULL;
493 ps.confs = NULL;
494 ps.outimage = NULL;
495 ps.outconf = NULL;
496 ps.plist = NULL;
497}
498
499/*---------------------------------------------------------------------------*/
503/*---------------------------------------------------------------------------*/
504
505static void vircam_imdither_tidy(void) {
506 freespace(ps.labels);
507 freeframeset(ps.imagelist);
508 freefitslist(ps.images,ps.nimages);
509 freeframeset(ps.conflist);
510 freefitslist(ps.confs,ps.nconfs);
511 freeimage(ps.outimage);
512 freeimage(ps.outconf);
513 freepropertylist(ps.plist);
514}
515
518/*
519
520$Log: not supported by cvs2svn $
521Revision 1.13 2012/01/15 17:40:09 jim
522Minor modifications to take into accout the changes in cpl API for v6
523
524Revision 1.12 2009/09/09 09:51:13 jim
525modified to use new saving routines so that headers are right
526
527Revision 1.11 2007/10/25 19:38:22 jim
528modified to keep lint happy
529
530Revision 1.10 2007/10/15 12:53:55 jim
531Modified for compatibility with cpl_4.0
532
533Revision 1.9 2007/07/09 13:22:09 jim
534Modified to use new version of vircam_exten_range
535
536Revision 1.8 2007/05/02 12:53:11 jim
537typo fixes in docs
538
539Revision 1.7 2007/04/13 12:27:38 jim
540Added some extra docs
541
542Revision 1.6 2007/04/04 10:36:29 jim
543Modified to use new dfs tags
544
545Revision 1.5 2007/03/02 12:38:32 jim
546Fixed small memory leak
547
548Revision 1.4 2007/03/01 12:42:59 jim
549Modified slightly after code checking
550
551Revision 1.3 2006/06/15 09:58:59 jim
552Minor changes to docs
553
554Revision 1.2 2006/06/06 13:08:25 jim
555Fixed minor doc problem
556
557Revision 1.1 2006/06/01 13:57:29 jim
558First entry
559
560
561*/
casu_fits ** casu_fits_load_list(cpl_frameset *f, cpl_type type, int exten)
Definition: casu_fits.c:318
cpl_propertylist * casu_fits_get_phu(casu_fits *p)
Definition: casu_fits.c:531
int casu_imdither(casu_fits **inf, casu_fits **inconf, int nimages, int nconfs, float lthr, float hthr, cpl_propertylist **p, const char *expkey, cpl_image **out, cpl_image **outc, int *status)
Dither a set of jittered observations.
int casu_compare_tags(const cpl_frame *frame1, const cpl_frame *frame2)
Compare input tags.
Definition: casu_utils.c:96
cpl_frameset * casu_frameset_subgroup(cpl_frameset *frameset, cpl_size *labels, cpl_size nlab, const char *tag)
Extract a frameset from another frameset.
Definition: casu_utils.c:149
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