VIRCAM Pipeline 2.3.12
tests/vircam_interleave.c
1/* $Id: vircam_interleave.c,v 1.23 2012-01-16 15:47:24 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 15:47:24 $
24 * $Revision: 1.23 $
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_stats.h>
41#include <casu_wcsutils.h>
42
43#include "vircam_utils.h"
44#include "vircam_dfs.h"
45#include "vircam_mods.h"
46
47/* Function prototypes */
48
49static int vircam_interleave_create(cpl_plugin *) ;
50static int vircam_interleave_exec(cpl_plugin *) ;
51static int vircam_interleave_destroy(cpl_plugin *) ;
52static int vircam_interleave_test(cpl_parameterlist *, cpl_frameset *) ;
53static int vircam_interleave_save(cpl_frameset *framelist,
54 cpl_parameterlist *parlist);
55static void vircam_interleave_init(void);
56static void vircam_interleave_tidy(void);
57
58/* Static global variables */
59
60static struct {
61
62 /* Input */
63
64 int extenum;
65
66} vircam_interleave_config;
67
68
69static struct {
70 cpl_size *labels;
71 cpl_frameset *imagelist;
72 casu_fits **images;
73 cpl_frameset *conflist;
74 casu_fits **confs;
75 int nimages;
76 int nconfs;
77 cpl_image *outimage;
78 cpl_image *outconf;
79 cpl_propertylist *plist;
80} ps;
81
82static int isfirst;
83static cpl_frame *product_frame = NULL;
84static cpl_frame *product_conf = NULL;
85
86static char vircam_interleave_description[] =
87"vircam_interleave -- VIRCAM test interleave recipe.\n\n"
88"Interleave 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
138/* Function code */
139
140/*---------------------------------------------------------------------------*/
148/*---------------------------------------------------------------------------*/
149
150int cpl_plugin_get_info(cpl_pluginlist *list) {
151 cpl_recipe *recipe = cpl_calloc(1,sizeof(*recipe));
152 cpl_plugin *plugin = &recipe->interface;
153 char alldesc[SZ_ALLDESC];
154 (void)snprintf(alldesc,SZ_ALLDESC,vircam_interleave_description,
155 VIRCAM_TEST_SCIENCE_RAW,VIRCAM_CAL_CONF);
156
157 cpl_plugin_init(plugin,
158 CPL_PLUGIN_API,
159 VIRCAM_BINARY_VERSION,
160 CPL_PLUGIN_TYPE_RECIPE,
161 "vircam_interleave",
162 "VIRCAM interleaf test recipe [test]",
163 alldesc,
164 "Jim Lewis",
165 "jrl@ast.cam.ac.uk",
167 vircam_interleave_create,
168 vircam_interleave_exec,
169 vircam_interleave_destroy);
170
171 cpl_pluginlist_append(list,plugin);
172
173 return(0);
174}
175
176/*---------------------------------------------------------------------------*/
185/*---------------------------------------------------------------------------*/
186
187static int vircam_interleave_create(cpl_plugin *plugin) {
188 cpl_recipe *recipe;
189 cpl_parameter *p;
190
191 /* Get the recipe out of the plugin */
192
193 if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
194 recipe = (cpl_recipe *)plugin;
195 else
196 return(-1);
197
198 /* Create the parameters list in the cpl_recipe object */
199
200 recipe->parameters = cpl_parameterlist_new();
201
202 /* Extension number of input frames to use */
203
204 p = cpl_parameter_new_range("vircam.vircam_interleave.extenum",
205 CPL_TYPE_INT,
206 "Extension number to be done, 0 == all",
207 "vircam.vircam_interleave",
208 1,0,16);
209 cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI,"ext");
210 cpl_parameterlist_append(recipe->parameters,p);
211
212 /* Get out of here */
213
214 return(0);
215}
216
217
218/*---------------------------------------------------------------------------*/
224/*---------------------------------------------------------------------------*/
225
226static int vircam_interleave_exec(cpl_plugin *plugin) {
227 cpl_recipe *recipe;
228
229 /* Get the recipe out of the plugin */
230
231 if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
232 recipe = (cpl_recipe *)plugin;
233 else
234 return(-1);
235
236 return(vircam_interleave_test(recipe->parameters,recipe->frames));
237}
238
239/*---------------------------------------------------------------------------*/
245/*---------------------------------------------------------------------------*/
246
247static int vircam_interleave_destroy(cpl_plugin *plugin) {
248 cpl_recipe *recipe ;
249
250 /* Get the recipe out of the plugin */
251
252 if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
253 recipe = (cpl_recipe *)plugin;
254 else
255 return(-1);
256
257 cpl_parameterlist_delete(recipe->parameters);
258 return(0);
259}
260
261/*---------------------------------------------------------------------------*/
268/*---------------------------------------------------------------------------*/
269
270static int vircam_interleave_test(cpl_parameterlist *parlist,
271 cpl_frameset *framelist) {
272 const char *fctid="vircam_interleave";
273 int j,jst,jfn,retval,status,i,nstep;
274 cpl_size nlab;
275 const int *dims;
276 long npts;
277 float val;
278 double refx,refy,refra,refdec,x,y;
279 cpl_parameter *p;
280 cpl_image *image;
281 const cpl_array *a;
282 cpl_wcs *wcs;
283
284
285 /* Check validity of input frameset */
286
287 if (framelist == NULL || cpl_frameset_get_size(framelist) <= 0) {
288 cpl_msg_error(fctid,"Input framelist NULL or has no input data");
289 return(-1);
290 }
291
292 /* Initialise some things */
293
294 vircam_interleave_init();
295
296 /* Get the parameters */
297
298 p = cpl_parameterlist_find(parlist,"vircam.vircam_interleave.extenum");
299 vircam_interleave_config.extenum = cpl_parameter_get_int(p);
300
301 /* Sort out raw from calib frames */
302
303 if (vircam_dfs_set_groups(framelist) != CASU_OK) {
304 cpl_msg_error(fctid,"Cannot identify RAW and CALIB frames");
305 vircam_interleave_tidy();
306 return(-1);
307 }
308
309 /* Get the frames frames */
310
311 if ((ps.labels = cpl_frameset_labelise(framelist,casu_compare_tags,
312 &nlab)) == NULL) {
313 cpl_msg_error(fctid,"Cannot labelise the input frames");
314 vircam_interleave_tidy();
315 return(-1);
316 }
317 if ((ps.imagelist = casu_frameset_subgroup(framelist,ps.labels,nlab,
318 VIRCAM_TEST_SCIENCE_RAW)) == NULL) {
319 cpl_msg_error(fctid,"Cannot get images in input frameset");
320 vircam_interleave_tidy();
321 return(-1);
322 }
323 ps.nimages = cpl_frameset_get_size(ps.imagelist);
324 nstep = (int)sqrt((float)ps.nimages);
325 if ((ps.conflist = casu_frameset_subgroup(framelist,ps.labels,nlab,
326 VIRCAM_CAL_CONF)) == NULL) {
327 cpl_msg_error(fctid,"Cannot get confidence maps in input frameset");
328 vircam_interleave_tidy();
329 return(-1);
330 }
331 ps.nconfs = cpl_frameset_get_size(ps.conflist);
332
333 /* Now, how many image extensions do we want to do? If the extension
334 number is zero, then we loop for all possible extensions. If it
335 isn't then we just do the extension specified */
336
337 vircam_exten_range(vircam_interleave_config.extenum,
338 (const cpl_frame *)cpl_frameset_get_position(ps.imagelist,0),
339 &jst,&jfn);
340 if (jst == -1 || jfn == -1) {
341 cpl_msg_error(fctid,"Unable to continue");
342 vircam_interleave_tidy();
343 return(-1);
344 }
345
346 /* Now loop for all the extension... */
347
348 status = CASU_OK;
349 for (j = jst; j <= jfn; j++) {
350 isfirst = (j == jst);
351
352 /* Load the images */
353
354 ps.images = casu_fits_load_list(ps.imagelist,CPL_TYPE_FLOAT,j);
355 ps.confs = casu_fits_load_list(ps.conflist,CPL_TYPE_INT,j);
356
357 /* Get some information that you need for the interleaving. Start
358 by getting the background level add it to the extension property
359 list */
360
361 refx = 0.0;
362 refy = 0.0;
363 for (i = 0; i < ps.nimages; i++) {
364 image = casu_fits_get_image(ps.images[i]);
365 npts = casu_getnpts(image);
366 val = casu_med(cpl_image_get_data_float(image),NULL,npts);
367 cpl_propertylist_update_float(casu_fits_get_ehu(ps.images[i]),
368 "ESO DRS BACKMED",val);
369 wcs = cpl_wcs_new_from_propertylist(casu_fits_get_ehu(ps.images[i]));
370 if (i == 0) {
371 a = cpl_wcs_get_image_dims(wcs);
372 dims = cpl_array_get_data_int_const(a);
373 refx = 0.5*(double)dims[0];
374 refy = 0.5*(double)dims[1];
375 casu_xytoradec(wcs,refx,refy,&refra,&refdec);
376 }
377 casu_radectoxy(wcs,refra,refdec,&x,&y);
378 x = refx - x;
379 y = refy - y;
380 cpl_propertylist_update_double(casu_fits_get_ehu(ps.images[i]),
381 "ESO DRS XOFFMICRO",x);
382 cpl_propertylist_update_double(casu_fits_get_ehu(ps.images[i]),
383 "ESO DRS YOFFMICRO",y);
384 cpl_wcs_delete(wcs);
385 }
386
387 /* Call the interleaf module */
388
389 cpl_msg_info(fctid,"Doing interleaving for extension %" CPL_SIZE_FORMAT,
390 (cpl_size)j);
391 (void)vircam_interleave(ps.images,ps.nimages,ps.confs,ps.nconfs,
392 nstep,&(ps.plist),&(ps.outimage),&(ps.outconf),
393 &status);
394 if (status != CASU_OK) {
395 vircam_interleave_tidy();
396 return(-1);
397 }
398
399 /* Save everything */
400
401 cpl_msg_info(fctid,"Saving combined image extension %" CPL_SIZE_FORMAT,
402 (cpl_size)j);
403 retval = vircam_interleave_save(framelist,parlist);
404 if (retval != 0) {
405 vircam_interleave_tidy();
406 return(-1);
407 }
408 freefitslist(ps.images,ps.nimages);
409 freefitslist(ps.confs,ps.nconfs);
410 freeimage(ps.outimage);
411 freeimage(ps.outconf);
412 freepropertylist(ps.plist);
413 }
414 vircam_interleave_tidy();
415 return(0);
416}
417
418
419/*---------------------------------------------------------------------------*/
427/*---------------------------------------------------------------------------*/
428
429static int vircam_interleave_save(cpl_frameset *framelist,
430 cpl_parameterlist *parlist) {
431 cpl_propertylist *plist;
432 const char *recipeid = "vircam_interleave";
433 const char *fctid = "vircam_interleave_save";
434 const char *outfile = "comb.fits";
435 const char *outconf = "combconf.fits";
436
437 /* If we need to make a PHU then do that now based on the first frame
438 in the input frame list */
439
440 if (isfirst) {
441
442 /* Create a new product frame object and define some tags */
443
444 product_frame = cpl_frame_new();
445 cpl_frame_set_filename(product_frame,outfile);
446 cpl_frame_set_tag(product_frame,VIRCAM_PRO_INTER_TEST);
447 cpl_frame_set_type(product_frame,CPL_FRAME_TYPE_IMAGE);
448 cpl_frame_set_group(product_frame,CPL_FRAME_GROUP_PRODUCT);
449 cpl_frame_set_level(product_frame,CPL_FRAME_LEVEL_FINAL);
450
451 /* Set up header for phu */
452
453 plist = casu_fits_get_phu(ps.images[0]);
454 vircam_dfs_set_product_primary_header(plist,product_frame,framelist,
455 parlist,(char *)recipeid,
456 "?Dictionary?",NULL,0);
457
458 /* 'Save' the PHU interleaved image */
459
460 if (cpl_image_save(NULL,outfile,CPL_TYPE_UCHAR,plist,
461 CPL_IO_DEFAULT) != CPL_ERROR_NONE) {
462 cpl_msg_error(fctid,"Cannot save product PHU");
463 cpl_frame_delete(product_frame);
464 return(-1);
465 }
466 cpl_frameset_insert(framelist,product_frame);
467
468 /* Create a new product frame object and define some tags */
469
470 product_conf = cpl_frame_new();
471 cpl_frame_set_filename(product_conf,outconf);
472 cpl_frame_set_tag(product_conf,VIRCAM_PRO_CONF_TEST);
473 cpl_frame_set_type(product_conf,CPL_FRAME_TYPE_IMAGE);
474 cpl_frame_set_group(product_conf,CPL_FRAME_GROUP_PRODUCT);
475 cpl_frame_set_level(product_conf,CPL_FRAME_LEVEL_FINAL);
476
477 /* 'Save' the PHU confidence map image */
478
479 if (cpl_image_save(NULL,outconf,CPL_TYPE_UCHAR,plist,
480 CPL_IO_DEFAULT) != CPL_ERROR_NONE) {
481 cpl_msg_error(fctid,"Cannot save product PHU");
482 cpl_frame_delete(product_conf);
483 return(-1);
484 }
485 cpl_frameset_insert(framelist,product_conf);
486 }
487
488 /* Get the extension property list */
489
490 plist = ps.plist;
491
492 /* Fiddle with the header now */
493
494 vircam_dfs_set_product_exten_header(plist,product_frame,framelist,
495 parlist,(char *)recipeid,
496 "?Dictionary?",NULL);
497
498 /* Now save the interleaved image extension */
499
500 if (cpl_image_save(ps.outimage,outfile,CPL_TYPE_FLOAT,plist,
501 CPL_IO_EXTEND) != CPL_ERROR_NONE) {
502 cpl_msg_error(fctid,"Cannot save interleaved image extension");
503 return(-1);
504 }
505
506 /* And the confidence map */
507
508 if (cpl_image_save(ps.outconf,outconf,CPL_TYPE_SHORT,plist,
509 CPL_IO_EXTEND) != CPL_ERROR_NONE) {
510 cpl_msg_error(fctid,"Cannot save confidence map image extension");
511 return(-1);
512 }
513
514 /* Get out of here */
515
516 return(0);
517}
518
519/*---------------------------------------------------------------------------*/
523/*---------------------------------------------------------------------------*/
524
525static void vircam_interleave_init(void) {
526 ps.labels = NULL;
527 ps.imagelist = NULL;
528 ps.images = NULL;
529 ps.conflist = NULL;
530 ps.confs = NULL;
531 ps.outimage = NULL;
532 ps.outconf = NULL;
533 ps.plist = NULL;
534}
535
536/*---------------------------------------------------------------------------*/
540/*---------------------------------------------------------------------------*/
541
542static void vircam_interleave_tidy(void) {
543 freespace(ps.labels);
544 freeframeset(ps.imagelist);
545 freefitslist(ps.images,ps.nimages);
546 freeframeset(ps.conflist);
547 freefitslist(ps.confs,ps.nconfs);
548 freeimage(ps.outimage);
549 freeimage(ps.outconf);
550 freepropertylist(ps.plist);
551}
552
556/*
557
558$Log: not supported by cvs2svn $
559Revision 1.22 2012/01/16 14:44:02 jim
560Fixed test recipes for cpl6 compliance
561
562Revision 1.21 2012/01/15 17:40:09 jim
563Minor modifications to take into accout the changes in cpl API for v6
564
565Revision 1.20 2010/07/13 11:16:50 jim
566A few changes to deal with compiler whinges
567
568Revision 1.19 2010/06/30 12:42:00 jim
569A few fixes to stop compiler compaints
570
571Revision 1.18 2009/09/09 09:51:13 jim
572modified to use new saving routines so that headers are right
573
574Revision 1.17 2008/07/10 13:01:35 jim
575Modified to use v4.2 version of cpl_wcs
576
577Revision 1.16 2008/06/20 11:13:02 jim
578Fixed dodgy call to cpl_wcs_get_image_dims
579
580Revision 1.15 2008/05/06 08:40:43 jim
581Modified to use cpl_wcs interface
582
583Revision 1.14 2007/10/25 19:38:22 jim
584modified to keep lint happy
585
586Revision 1.13 2007/10/15 12:53:55 jim
587Modified for compatibility with cpl_4.0
588
589Revision 1.12 2007/07/09 13:22:09 jim
590Modified to use new version of vircam_exten_range
591
592Revision 1.11 2007/05/02 12:53:11 jim
593typo fixes in docs
594
595Revision 1.10 2007/04/13 12:27:39 jim
596Added some extra docs
597
598Revision 1.9 2007/04/04 10:36:29 jim
599Modified to use new dfs tags
600
601Revision 1.8 2007/03/02 12:38:33 jim
602Fixed small memory leak
603
604Revision 1.7 2007/03/01 12:42:59 jim
605Modified slightly after code checking
606
607Revision 1.6 2006/07/11 14:59:09 jim
608Fixed offsets
609
610Revision 1.5 2006/06/15 09:58:59 jim
611Minor changes to docs
612
613Revision 1.4 2006/05/15 12:55:42 jim
614Fixed a few typos
615
616Revision 1.3 2006/05/04 11:53:42 jim
617Fixed _save routine so that it's more consistent with the standard CPL
618way of doing things
619
620Revision 1.2 2006/04/27 14:22:05 jim
621Fixed docs
622
623Revision 1.1 2006/04/24 10:42:45 jim
624New routine
625
626
627*/
cpl_image * casu_fits_get_image(casu_fits *p)
Definition: casu_fits.c:436
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
cpl_propertylist * casu_fits_get_ehu(casu_fits *p)
Definition: casu_fits.c:576
float casu_med(float *data, unsigned char *bpm, long npts)
Definition: casu_stats.c:89
int casu_compare_tags(const cpl_frame *frame1, const cpl_frame *frame2)
Compare input tags.
Definition: casu_utils.c:96
long casu_getnpts(cpl_image *in)
Get the number of pixels in a 2d image.
Definition: casu_utils.c:243
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
void casu_radectoxy(cpl_wcs *wcs, double ra, double dec, double *x, double *y)
void casu_xytoradec(cpl_wcs *wcs, double x, double y, double *ra, double *dec)
Definition: casu_wcsutils.c:93
int vircam_interleave(casu_fits **infiles, int ninputs, casu_fits **inconfs, int nconfs, int nsteps, cpl_propertylist **p, cpl_image **outimage, cpl_image **outconf, int *status)
Interleave a set of microstepped observations.
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