VIRCAM Pipeline  1.3.4
vircam_matchxy.c
1 /* $Id: vircam_matchxy.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) 2006 Cambridge Astronomy Survey Unit
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 "vircam_utils.h"
38 #include "vircam_dfs.h"
39 #include "vircam_fits.h"
40 #include "vircam_mods.h"
41 #include "vircam_paf.h"
42 
43 /* Function prototypes */
44 
45 static int vircam_matchxy_create(cpl_plugin *);
46 static int vircam_matchxy_exec(cpl_plugin *);
47 static int vircam_matchxy_destroy(cpl_plugin *);
48 static int vircam_matchxy_test(cpl_parameterlist *, cpl_frameset *);
49 static int vircam_matchxy_save(void);
50 static void vircam_matchxy_init(void);
51 static void vircam_matchxy_tidy(void);
52 
53 static struct {
54 
55  /* Input */
56 
57  int extenum;
58 
59  /* Output */
60 
61  float xoff;
62  float yoff;
63  int nm;
64 
65 } vircam_matchxy_config;
66 
67 
68 static struct {
69  cpl_size *labels;
70  cpl_frameset *catlist;
71  cpl_frame *cat1;
72  cpl_frame *cat2;
73  vir_tfits *cat1f;
74  vir_tfits *cat2f;
75 } ps;
76 
77 
78 static char vircam_matchxy_description[] =
79 "vircam_matchxy -- VIRCAM recipe to test vircam_matchxy.\n\n"
80 "Match a catalogue with another to get x,y offsets\n\n"
81 "The program accepts the following files in the SOF:\n\n"
82 " Tag Description\n"
83 " -----------------------------------------------------------------------\n"
84 " %-21s Input catalogues of objects extracted from an image\n"
85 "\n";
86 
125 /* Function code */
126 
127 /*---------------------------------------------------------------------------*/
135 /*---------------------------------------------------------------------------*/
136 
137 int cpl_plugin_get_info(cpl_pluginlist *list) {
138  cpl_recipe *recipe = cpl_calloc(1,sizeof(*recipe));
139  cpl_plugin *plugin = &recipe->interface;
140  char alldesc[SZ_ALLDESC];
141  (void)snprintf(alldesc,SZ_ALLDESC,vircam_matchxy_description,
142  VIRCAM_CAL_OBJCAT);
143 
144  cpl_plugin_init(plugin,
145  CPL_PLUGIN_API,
146  VIRCAM_BINARY_VERSION,
147  CPL_PLUGIN_TYPE_RECIPE,
148  "vircam_matchxy",
149  "VIRCAM catalogue matching test recipe [test]",
150  alldesc,
151  "Jim Lewis",
152  "jrl@ast.cam.ac.uk",
154  vircam_matchxy_create,
155  vircam_matchxy_exec,
156  vircam_matchxy_destroy);
157 
158  cpl_pluginlist_append(list,plugin);
159 
160  return(0);
161 }
162 
163 /*---------------------------------------------------------------------------*/
172 /*---------------------------------------------------------------------------*/
173 
174 static int vircam_matchxy_create(cpl_plugin *plugin) {
175  cpl_recipe *recipe;
176  cpl_parameter *p;
177 
178  /* Get the recipe out of the plugin */
179 
180  if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
181  recipe = (cpl_recipe *)plugin;
182  else
183  return(-1);
184 
185  /* Create the parameters list in the cpl_recipe object */
186 
187  recipe->parameters = cpl_parameterlist_new();
188 
189  /* Extension number of input frames to use */
190 
191  p = cpl_parameter_new_range("vircam.vircam_matchxy.extenum",
192  CPL_TYPE_INT,
193  "Extension number to be done, 0 == all",
194  "vircam.vircam_matchxy",1,0,16);
195  cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI,"ext");
196  cpl_parameterlist_append(recipe->parameters,p);
197 
198  /* Get out of here */
199 
200  return(0);
201 }
202 
203 /*---------------------------------------------------------------------------*/
209 /*---------------------------------------------------------------------------*/
210 
211 static int vircam_matchxy_exec(cpl_plugin *plugin) {
212  cpl_recipe *recipe;
213 
214  /* Get the recipe out of the plugin */
215 
216  if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
217  recipe = (cpl_recipe *)plugin;
218  else
219  return(-1);
220 
221  return(vircam_matchxy_test(recipe->parameters,recipe->frames));
222 }
223 
224 /*---------------------------------------------------------------------------*/
230 /*---------------------------------------------------------------------------*/
231 
232 static int vircam_matchxy_destroy(cpl_plugin *plugin) {
233  cpl_recipe *recipe ;
234 
235  /* Get the recipe out of the plugin */
236 
237  if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
238  recipe = (cpl_recipe *)plugin;
239  else
240  return(-1);
241 
242  cpl_parameterlist_delete(recipe->parameters);
243  return(0);
244 }
245 
246 /*---------------------------------------------------------------------------*/
253 /*---------------------------------------------------------------------------*/
254 
255 static int vircam_matchxy_test(cpl_parameterlist *parlist,
256  cpl_frameset *framelist) {
257  const char *fctid="vircam_matchxy";
258  cpl_parameter *p;
259  int jst,jfn,status,j;
260  cpl_size nlab;
261  cpl_table *out;
262 
263  /* Check validity of input frameset */
264 
265  if (framelist == NULL || cpl_frameset_get_size(framelist) <= 0) {
266  cpl_msg_error(fctid,"Input framelist NULL or has no input data");
267  return(-1);
268  }
269 
270  /* Initialise some things */
271 
272  vircam_matchxy_init();
273 
274  /* Get the parameters */
275 
276  p = cpl_parameterlist_find(parlist,"vircam.vircam_matchxy.extenum");
277  vircam_matchxy_config.extenum = cpl_parameter_get_int(p);
278 
279  /* Sort out raw from calib frames */
280 
281  if (vircam_dfs_set_groups(framelist) != VIR_OK) {
282  cpl_msg_error(fctid,"Cannot identify RAW and CALIB frames");
283  vircam_matchxy_tidy();
284  return(-1);
285  }
286 
287  /* Get the frames */
288 
289  if ((ps.labels = cpl_frameset_labelise(framelist,vircam_compare_tags,
290  &nlab)) == NULL) {
291  cpl_msg_error(fctid,"Cannot labelise the input frames");
292  vircam_matchxy_tidy();
293  return(-1);
294  }
295  if ((ps.catlist = vircam_frameset_subgroup(framelist,ps.labels,nlab,
296  VIRCAM_CAL_OBJCAT)) == NULL) {
297  cpl_msg_error(fctid,"No object catalogues found -- cannot continue");
298  vircam_matchxy_tidy();
299  return(-1);
300  }
301  ps.cat1 = cpl_frameset_get_frame(ps.catlist,0);
302  ps.cat2 = cpl_frameset_get_frame(ps.catlist,1);
303  if (ps.cat1 == NULL || ps.cat2 == NULL) {
304  cpl_msg_error(fctid,"List does not contain two object catalogues");
305  vircam_matchxy_tidy();
306  return(-1);
307  }
308 
309  /* Now, how many image extensions do we want to do? If the extension
310  number is zero, then we loop for all possible extensions. If it
311  isn't then we just do the extension specified */
312 
313  vircam_exten_range(vircam_matchxy_config.extenum,
314  (const cpl_frame *)ps.cat1,&jst,&jfn);
315  if (jst == -1 || jfn == -1) {
316  cpl_msg_error(fctid,"Unable to continue");
317  vircam_matchxy_tidy();
318  return(-1);
319  }
320 
321  /* Now loop for all the extension... */
322 
323  status = VIR_OK;
324  for (j = jst; j <= jfn; j++) {
325 
326  /* Load up the tables */
327 
328  ps.cat1f = vircam_tfits_load(ps.cat1,j);
329  ps.cat2f = vircam_tfits_load(ps.cat2,j);
330  if (ps.cat1f == NULL || ps.cat2f == NULL) {
331  cpl_msg_warning(fctid,"No matching done");
332  continue;
333  }
334 
335  /* Now do the correction */
336 
337  cpl_msg_info(fctid,"Doing the matching for extension %" CPL_SIZE_FORMAT,
338  (cpl_size)j);
339  (void)vircam_matchxy(vircam_tfits_get_table(ps.cat1f),
340  vircam_tfits_get_table(ps.cat2f),200.0,
341  &(vircam_matchxy_config.xoff),
342  &(vircam_matchxy_config.yoff),
343  &(vircam_matchxy_config.nm),&out,&status);
344  freetable(out);
345  if (status != VIR_OK) {
346  cpl_msg_warning(fctid,"No matching results done");
347  status = VIR_OK;
348  }
349 
350  /* Now save the result */
351 
352  cpl_msg_info(fctid,"Saving results for extension %" CPL_SIZE_FORMAT,
353  (cpl_size)j);
354  if (vircam_matchxy_save() != 0)
355  cpl_msg_warning(fctid,"No matching results saved");
356 
357  /* Tidy a few things before the next image */
358 
359  freetfits(ps.cat1f);
360  freetfits(ps.cat2f);
361  }
362  vircam_matchxy_tidy();
363  return(0);
364 }
365 
366 /*---------------------------------------------------------------------------*/
371 /*---------------------------------------------------------------------------*/
372 
373 static int vircam_matchxy_save(void) {
374  const char *fctid = "vircam_matchxy_save";
375  const char *outfile = "matchxy";
376  cpl_propertylist *p,*p2,*p3;
377 
378  /* Get a propertylist from the first file extension */
379 
380  p = vircam_tfits_get_ehu(ps.cat1f);
381 
382  /* Remove this line when they fix cpl so that the date-obs isn't removed
383  from the table extensions */
384 
385  cpl_propertylist_update_string(p,"DATE-OBS","ABC");
386 
387  /* Extract the required things */
388 
389  p2 = vircam_paf_req_items(p);
390  p3 = vircam_paf_phu_items(vircam_tfits_get_phu(ps.cat1f));
392  freepropertylist(p3);
393 
394  /* Add some new stuff in */
395 
396  cpl_propertylist_update_float(p2,"ESO QC XOFF",vircam_matchxy_config.xoff);
397  cpl_propertylist_set_comment(p2,"ESO QC XOFF",
398  "Calculated X offset (pixels)");
399  cpl_propertylist_update_float(p2,"ESO QC YOFF",vircam_matchxy_config.yoff);
400  cpl_propertylist_set_comment(p2,"ESO QC YOFF",
401  "Calculated Y offset (pixels)");
402  cpl_propertylist_update_int(p2,"ESO QC NUMMATCH",
403  vircam_matchxy_config.nm);
404  cpl_propertylist_set_comment(p2,"ESO QC NUMMATCH",
405  "Number of matching objects");
406 
407  /* Now write the PAF */
408 
409  if (vircam_paf_print((char *)outfile,"VIRCAM/vircam_matchxy",
410  "Test output file",p2) != VIR_OK) {
411  cpl_msg_error(fctid,"Unable to write PAF");
412  cpl_propertylist_delete(p2);
413  return(-1);
414  }
415  cpl_propertylist_delete(p2);
416  return(0);
417 }
418 
419 
420 
421 /*---------------------------------------------------------------------------*/
425 /*---------------------------------------------------------------------------*/
426 
427 static void vircam_matchxy_init(void) {
428  ps.labels = NULL;
429  ps.cat1 = NULL;
430  ps.cat2 = NULL;
431  ps.cat1f = NULL;
432  ps.cat2f = NULL;
433  ps.catlist = NULL;
434 }
435 
436 
437 /*---------------------------------------------------------------------------*/
441 /*---------------------------------------------------------------------------*/
442 
443 static void vircam_matchxy_tidy(void) {
444  freespace(ps.labels);
445  freetfits(ps.cat1f);
446  freetfits(ps.cat2f);
447  freeframeset(ps.catlist);
448 }
449 
452 /*
453 
454 $Log: not supported by cvs2svn $
455 Revision 1.13 2012/01/15 17:40:09 jim
456 Minor modifications to take into accout the changes in cpl API for v6
457 
458 Revision 1.12 2008/11/21 10:16:54 jim
459 Spliced in new version of vircam_matchxy utility
460 
461 Revision 1.11 2007/10/25 19:38:22 jim
462 modified to keep lint happy
463 
464 Revision 1.10 2007/07/09 13:22:09 jim
465 Modified to use new version of vircam_exten_range
466 
467 Revision 1.9 2007/04/23 12:49:07 jim
468 Changed behaviour for error condition
469 
470 Revision 1.8 2007/04/13 12:27:39 jim
471 Added some extra docs
472 
473 Revision 1.7 2007/04/04 10:36:29 jim
474 Modified to use new dfs tags
475 
476 Revision 1.6 2007/03/01 12:42:59 jim
477 Modified slightly after code checking
478 
479 Revision 1.5 2007/02/15 12:17:45 jim
480 Modified to use new version of PAF files
481 
482 Revision 1.4 2006/06/15 09:59:00 jim
483 Minor changes to docs
484 
485 Revision 1.3 2006/05/04 11:53:44 jim
486 Fixed _save routine so that it's more consistent with the standard CPL
487 way of doing things
488 
489 Revision 1.2 2006/04/27 14:22:05 jim
490 Fixed docs
491 
492 Revision 1.1 2006/04/24 10:42:45 jim
493 New routine
494 
495 
496 */
497 
498 
499 
500 
const char * vircam_get_license(void)
Definition: vircam_utils.c:92
cpl_table * vircam_tfits_get_table(vir_tfits *p)
Definition: vircam_tfits.c:364
int vircam_compare_tags(const cpl_frame *frame1, const cpl_frame *frame2)
Definition: vircam_utils.c:141
void vircam_merge_propertylists(cpl_propertylist *p1, cpl_propertylist *p2)
cpl_propertylist * vircam_tfits_get_ehu(vir_tfits *p)
Definition: vircam_tfits.c:473
cpl_frameset * vircam_frameset_subgroup(cpl_frameset *frameset, cpl_size *labels, cpl_size nlab, const char *tag)
Definition: vircam_utils.c:193
vir_tfits * vircam_tfits_load(cpl_frame *table, int nexten)
Definition: vircam_tfits.c:78
int vircam_matchxy(cpl_table *progtab, cpl_table *template, float srad, float *xoffset, float *yoffset, int *nm, cpl_table **outtab, int *status)
Match two lists of x,y coordinates from two tables to find the cartesian offset between them...
Definition: vircam_match.c:104
cpl_propertylist * vircam_tfits_get_phu(vir_tfits *p)
Definition: vircam_tfits.c:432
void vircam_exten_range(int inexten, const cpl_frame *fr, int *out1, int *out2)
Definition: vircam_utils.c:353
int vircam_dfs_set_groups(cpl_frameset *set)
Definition: vircam_dfs.c:87