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