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