VIRCAM Pipeline  2.3.12
vircam_matchstds.c
1 /* $Id: vircam_matchstds.c,v 1.13 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.13 $
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 
46 static int vircam_matchstds_create(cpl_plugin *);
47 static int vircam_matchstds_exec(cpl_plugin *);
48 static int vircam_matchstds_destroy(cpl_plugin *);
49 static int vircam_matchstds_test(cpl_parameterlist *, cpl_frameset *);
50 static int vircam_matchstds_save(cpl_frameset *framelist,
51  cpl_parameterlist *parlist);
52 static void vircam_matchstds_init(void);
53 static void vircam_matchstds_tidy(void);
54 
55 static struct {
56 
57  /* Input */
58 
59  int extenum;
60 
61 } vircam_matchstds_config;
62 
63 
64 static struct {
65  cpl_size *labels;
66  cpl_frame *cat;
67  cpl_frame *stds;
68  casu_tfits *catf;
69  casu_tfits *stdsf;
70  cpl_table *outtab;
71 } ps;
72 
73 static int isfirst;
74 static cpl_frame *product_frame = NULL;
75 
76 static char vircam_matchstds_description[] =
77 "vircam_matchstds -- VIRCAM recipe to test vircam_matchstds.\n\n"
78 "Match a catalogue with a table of standards\n\n"
79 "The program accepts the following files in the SOF:\n\n"
80 " Tag Description\n"
81 " -----------------------------------------------------------------------\n"
82 " %-21s An input catalogue of objects extracted from an image\n"
83 " %-21s An input catalogue of standard stars\n"
84 "\n";
85 
128 /* Function code */
129 
130 /*---------------------------------------------------------------------------*/
138 /*---------------------------------------------------------------------------*/
139 
140 int cpl_plugin_get_info(cpl_pluginlist *list) {
141  cpl_recipe *recipe = cpl_calloc(1,sizeof(*recipe));
142  cpl_plugin *plugin = &recipe->interface;
143  char alldesc[SZ_ALLDESC];
144  (void)snprintf(alldesc,SZ_ALLDESC,vircam_matchstds_description,
145  VIRCAM_CAL_OBJCAT,VIRCAM_CAL_STDTAB);
146 
147  cpl_plugin_init(plugin,
148  CPL_PLUGIN_API,
149  VIRCAM_BINARY_VERSION,
150  CPL_PLUGIN_TYPE_RECIPE,
151  "vircam_matchstds",
152  "VIRCAM catalogue and standards matching test recipe [test]",
153  alldesc,
154  "Jim Lewis",
155  "jrl@ast.cam.ac.uk",
157  vircam_matchstds_create,
158  vircam_matchstds_exec,
159  vircam_matchstds_destroy);
160 
161  cpl_pluginlist_append(list,plugin);
162 
163  return(0);
164 }
165 
166 /*---------------------------------------------------------------------------*/
175 /*---------------------------------------------------------------------------*/
176 
177 static int vircam_matchstds_create(cpl_plugin *plugin) {
178  cpl_recipe *recipe;
179  cpl_parameter *p;
180 
181  /* Get the recipe out of the plugin */
182 
183  if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
184  recipe = (cpl_recipe *)plugin;
185  else
186  return(-1);
187 
188  /* Create the parameters list in the cpl_recipe object */
189 
190  recipe->parameters = cpl_parameterlist_new();
191 
192  /* Extension number of input frames to use */
193 
194  p = cpl_parameter_new_range("vircam.vircam_matchstds.extenum",
195  CPL_TYPE_INT,
196  "Extension number to be done, 0 == all",
197  "vircam.vircam_matchstds",1,0,16);
198  cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI,"ext");
199  cpl_parameterlist_append(recipe->parameters,p);
200 
201  /* Get out of here */
202 
203  return(0);
204 }
205 
206 /*---------------------------------------------------------------------------*/
212 /*---------------------------------------------------------------------------*/
213 
214 static int vircam_matchstds_exec(cpl_plugin *plugin) {
215  cpl_recipe *recipe;
216 
217  /* Get the recipe out of the plugin */
218 
219  if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
220  recipe = (cpl_recipe *)plugin;
221  else
222  return(-1);
223 
224  return(vircam_matchstds_test(recipe->parameters,recipe->frames));
225 }
226 
227 /*---------------------------------------------------------------------------*/
233 /*---------------------------------------------------------------------------*/
234 
235 static int vircam_matchstds_destroy(cpl_plugin *plugin) {
236  cpl_recipe *recipe ;
237 
238  /* Get the recipe out of the plugin */
239 
240  if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
241  recipe = (cpl_recipe *)plugin;
242  else
243  return(-1);
244 
245  cpl_parameterlist_delete(recipe->parameters);
246  return(0);
247 }
248 
249 /*---------------------------------------------------------------------------*/
256 /*---------------------------------------------------------------------------*/
257 
258 static int vircam_matchstds_test(cpl_parameterlist *parlist,
259  cpl_frameset *framelist) {
260  const char *fctid="vircam_matchstds";
261  cpl_parameter *p;
262  int jst,jfn,status,j;
263  cpl_size nlab;
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_matchstds_init();
275 
276  /* Get the parameters */
277 
278  p = cpl_parameterlist_find(parlist,"vircam.vircam_matchstds.extenum");
279  vircam_matchstds_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_matchstds_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_matchstds_tidy();
295  return(-1);
296  }
297  if ((ps.cat = casu_frameset_subgroup_1(framelist,ps.labels,nlab,
298  VIRCAM_CAL_OBJCAT)) == NULL) {
299  cpl_msg_info(fctid,"No object catalogue found -- cannot continue");
300  vircam_matchstds_tidy();
301  return(-1);
302  }
303  if ((ps.stds = casu_frameset_subgroup_1(framelist,ps.labels,nlab,
304  VIRCAM_CAL_STDTAB)) == NULL) {
305  cpl_msg_info(fctid,"No standards catalogue found -- cannot continue");
306  vircam_matchstds_tidy();
307  return(-1);
308  }
309 
310  /* Now, how many image extensions do we want to do? If the extension
311  number is zero, then we loop for all possible extensions. If it
312  isn't then we just do the extension specified */
313 
314  vircam_exten_range(vircam_matchstds_config.extenum,
315  (const cpl_frame *)ps.cat,&jst,&jfn);
316  if (jst == -1 || jfn == -1) {
317  cpl_msg_error(fctid,"Unable to continue");
318  vircam_matchstds_tidy();
319  return(-1);
320  }
321 
322  /* Now loop for all the extension... */
323 
324  status = CASU_OK;
325  for (j = jst; j <= jfn; j++) {
326  isfirst = (j == jst);
327 
328  /* Load up the tables */
329 
330  ps.catf = casu_tfits_load(ps.cat,j);
331  ps.stdsf = casu_tfits_load(ps.stds,j);
332  if (ps.stdsf == NULL || ps.catf == NULL) {
333  freetfits(ps.catf);
334  freetfits(ps.stdsf);
335  cpl_msg_info(fctid,"No matching possible");
336  continue;
337  }
338 
339  /* Now do the correction */
340 
341  cpl_msg_info(fctid,"Doing the matching for extension %" CPL_SIZE_FORMAT,
342  (cpl_size)j);
343  (void)casu_matchstds(casu_tfits_get_table(ps.catf),
344  casu_tfits_get_table(ps.stdsf),300.0,
345  &(ps.outtab),&status);
346  if (status != CASU_OK) {
347  cpl_msg_info(fctid,"No matching done");
348  status = CASU_OK;
349  }
350 
351  /* Now save the result */
352 
353  cpl_msg_info(fctid,"Saving results for extension %" CPL_SIZE_FORMAT,
354  (cpl_size)j);
355  if (vircam_matchstds_save(framelist,parlist) != 0)
356  cpl_msg_info(fctid,"No matching saved");
357 
358 
359  /* Tidy a few things before the next image */
360 
361  freetfits(ps.catf);
362  freetfits(ps.stdsf);
363  freetable(ps.outtab);
364  }
365  vircam_matchstds_tidy();
366  return(0);
367 }
368 
369 /*---------------------------------------------------------------------------*/
376 /*---------------------------------------------------------------------------*/
377 
378 static int vircam_matchstds_save(cpl_frameset *framelist,
379  cpl_parameterlist *parlist) {
380  const char *recipeid = "vircam_matchstds";
381  const char *fctid = "vircam_matchstds_save";
382  const char *outfile = "matchstds.fits";
383  cpl_propertylist *plist,*elist;
384 
385  /* Create the output table. First see if you need a primary */
386 
387  if (isfirst) {
388 
389  /* Create a new product frame object and define some tags */
390 
391  product_frame = cpl_frame_new();
392  cpl_frame_set_filename(product_frame,outfile);
393  cpl_frame_set_tag(product_frame,VIRCAM_PRO_MSTDTAB);
394  cpl_frame_set_type(product_frame,CPL_FRAME_TYPE_TABLE);
395  cpl_frame_set_group(product_frame,CPL_FRAME_GROUP_PRODUCT);
396  cpl_frame_set_level(product_frame,CPL_FRAME_LEVEL_FINAL);
397 
398  /* Fiddle with the header new */
399 
400  plist = casu_tfits_get_phu(ps.catf);
401  vircam_dfs_set_product_primary_header(plist,product_frame,framelist,
402  parlist,(char *)recipeid,
403  "?Dictionary?",NULL,0);
404 
405  /* Get the extension header and tack the extra header items onto it. */
406 
407  elist = casu_tfits_get_ehu(ps.catf);
408  vircam_dfs_set_product_exten_header(elist,product_frame,framelist,
409  parlist,(char *)recipeid,
410  "?Dictionary?",NULL);
411 
412  /* 'Save' the PHU and create a table extension */
413 
414  if (cpl_table_save(ps.outtab,plist,elist,outfile,CPL_IO_DEFAULT)
415  != CPL_ERROR_NONE) {
416  cpl_msg_error(fctid,"Cannot save product table");
417  cpl_frame_delete(product_frame);
418  return(-1);
419  }
420  cpl_frameset_insert(framelist,product_frame);
421 
422  /* Otherwise save the next extension */
423 
424  } else {
425 
426  /* Get the extension header and tack the extra header items onto it. */
427 
428  elist = casu_tfits_get_ehu(ps.catf);
429  vircam_dfs_set_product_exten_header(elist,product_frame,framelist,
430  parlist,(char *)recipeid,
431  "?Dictionary?",NULL);
432 
433  /* Sae the table */
434 
435  if (cpl_table_save(ps.outtab,NULL,elist,outfile,CPL_IO_EXTEND)
436  != CPL_ERROR_NONE) {
437  cpl_msg_error(fctid,"Cannot save product table");
438  return(-1);
439  }
440  }
441 
442  return(0);
443 }
444 
445 
446 /*---------------------------------------------------------------------------*/
450 /*---------------------------------------------------------------------------*/
451 
452 static void vircam_matchstds_init(void) {
453  ps.labels = NULL;
454  ps.cat = NULL;
455  ps.catf = NULL;
456  ps.stds = NULL;
457  ps.stdsf = NULL;
458  ps.outtab = NULL;
459 }
460 
461 
462 /*---------------------------------------------------------------------------*/
466 /*---------------------------------------------------------------------------*/
467 
468 static void vircam_matchstds_tidy(void) {
469  freespace(ps.labels);
470  freetfits(ps.catf);
471  freetfits(ps.stdsf);
472  freeframe(ps.stds);
473  freeframe(ps.cat);
474  freetable(ps.outtab);
475 }
476 
480 /*
481 
482 $Log: not supported by cvs2svn $
483 Revision 1.12 2012/01/15 17:40:09 jim
484 Minor modifications to take into accout the changes in cpl API for v6
485 
486 Revision 1.11 2009/09/09 09:51:13 jim
487 modified to use new saving routines so that headers are right
488 
489 Revision 1.10 2009/07/03 12:30:39 jim
490 Modified the search radius
491 
492 Revision 1.9 2007/07/09 13:22:09 jim
493 Modified to use new version of vircam_exten_range
494 
495 Revision 1.8 2007/04/23 12:49:07 jim
496 Changed behaviour for error condition
497 
498 Revision 1.7 2007/04/13 12:27:39 jim
499 Added some extra docs
500 
501 Revision 1.6 2007/04/04 10:36:29 jim
502 Modified to use new dfs tags
503 
504 Revision 1.5 2007/03/01 12:42:59 jim
505 Modified slightly after code checking
506 
507 Revision 1.4 2006/06/15 09:58:59 jim
508 Minor changes to docs
509 
510 Revision 1.3 2006/05/04 11:53:43 jim
511 Fixed _save routine so that it's more consistent with the standard CPL
512 way of doing things
513 
514 Revision 1.2 2006/04/27 14:22:05 jim
515 Fixed docs
516 
517 Revision 1.1 2006/04/24 10:42:45 jim
518 New routine
519 
520 
521 */
522 
523 
524 
525 
int casu_matchstds(cpl_table *objtab, cpl_table *stdstab, float srad, cpl_table **outtab, int *status)
Match object and standard star tables by their xy coordinates.
Definition: casu_match.c:300
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
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 casu_compare_tags(const cpl_frame *frame1, const cpl_frame *frame2)
Compare input tags.
Definition: casu_utils.c:96
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