VIRCAM Pipeline  1.3.3
tests/vircam_getstds.c
1 /* $Id: vircam_getstds.c,v 1.17 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.17 $
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_getstds_create(cpl_plugin *) ;
45 static int vircam_getstds_exec(cpl_plugin *) ;
46 static int vircam_getstds_destroy(cpl_plugin *) ;
47 static int vircam_getstds_test(cpl_parameterlist *, cpl_frameset *) ;
48 static int vircam_getstds_save(cpl_frameset *framelist,
49  cpl_parameterlist *parlist);
50 static void vircam_getstds_init(void);
51 static void vircam_getstds_tidy(void);
52 
53 static struct {
54 
55  /* Input */
56 
57  int extenum;
58 
59 } vircam_getstds_config;
60 
61 static struct {
62  cpl_size *labels;
63  cpl_frame *img;
64  vir_fits *imgf;
65  cpl_propertylist *plist;
66  cpl_table *stds;
67  char *catpath;
68  char *catname;
69 } ps;
70 
71 static int isfirst;
72 static cpl_frame *product_frame = NULL;
73 
74 static char vircam_getstds_description[] =
75 "vircam_getstds -- VIRCAM test recipe to get standard stars for a frame\n\n"
76 "The program accepts the following files in the SOF:\n\n"
77 " Tag Description\n"
78 " -----------------------------------------------------------------------\n"
79 " %-21s A input image\n"
80 " %-21s Standard catalogue index file\n"
81 "\n";
82 
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_getstds_description,
145  VIRCAM_TEST_SCIENCE_RAW,VIRCAM_CAL_2MASS);
146 
147  cpl_plugin_init(plugin,
148  CPL_PLUGIN_API,
149  VIRCAM_BINARY_VERSION,
150  CPL_PLUGIN_TYPE_RECIPE,
151  "vircam_getstds",
152  "VIRCAM standard star extraction test recipe [test]",
153  alldesc,
154  "Jim Lewis",
155  "jrl@ast.cam.ac.uk",
157  vircam_getstds_create,
158  vircam_getstds_exec,
159  vircam_getstds_destroy);
160 
161  cpl_pluginlist_append(list,plugin);
162 
163  return(0);
164 }
165 
166 /*---------------------------------------------------------------------------*/
175 /*---------------------------------------------------------------------------*/
176 
177 static int vircam_getstds_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  /* Get the extension number of input frames to use */
193 
194  p = cpl_parameter_new_range("vircam.vircam_getstds.extenum",
195  CPL_TYPE_INT,
196  "Extension number to be done, 0 == all",
197  "vircam.vircam_getstds",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_getstds_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_getstds_test(recipe->parameters,recipe->frames));
225 }
226 
227 
228 /*---------------------------------------------------------------------------*/
234 /*---------------------------------------------------------------------------*/
235 
236 static int vircam_getstds_destroy(cpl_plugin *plugin) {
237  cpl_recipe *recipe ;
238 
239  /* Get the recipe out of the plugin */
240 
241  if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
242  recipe = (cpl_recipe *)plugin;
243  else
244  return(-1);
245 
246  cpl_parameterlist_delete(recipe->parameters);
247  return(0);
248 }
249 
250 
251 /*---------------------------------------------------------------------------*/
258 /*---------------------------------------------------------------------------*/
259 
260 static int vircam_getstds_test(cpl_parameterlist *parlist,
261  cpl_frameset *framelist) {
262  const char *fctid="vircam_getstds";
263  cpl_parameter *p;
264  int jst,jfn,status,j;
265  cpl_size nlab;
266  cpl_frame *catindex;
267 
268  /* Check validity of input frameset */
269 
270  if (framelist == NULL || cpl_frameset_get_size(framelist) <= 0) {
271  cpl_msg_error(fctid,"Input framelist NULL or has no input data");
272  return(-1);
273  }
274 
275  /* Initialise some things */
276 
277  vircam_getstds_init();
278 
279  /* Get parameters */
280 
281  p = cpl_parameterlist_find(parlist,"vircam.vircam_getstds.extenum");
282  vircam_getstds_config.extenum = cpl_parameter_get_int(p);
283 
284  /* Sort out raw from calib frames */
285 
286  if (vircam_dfs_set_groups(framelist) != VIR_OK) {
287  cpl_msg_error(fctid,"Cannot identify RAW and CALIB frames");
288  vircam_getstds_tidy();
289  return(-1);
290  }
291 
292  /* Get the frames */
293 
294  if ((ps.labels = cpl_frameset_labelise(framelist,vircam_compare_tags,
295  &nlab)) == NULL) {
296  cpl_msg_error(fctid,"Cannot labelise the input frames");
297  vircam_getstds_tidy();
298  return(-1);
299  }
300  if ((ps.img = vircam_frameset_subgroup_1(framelist,ps.labels,nlab,
301  VIRCAM_TEST_SCIENCE_RAW)) == NULL) {
302  cpl_msg_info(fctid,"No raw image found -- cannot continue");
303  vircam_getstds_tidy();
304  return(-1);
305  }
306  if ((catindex = vircam_frameset_subgroup_1(framelist,ps.labels,nlab,
307  VIRCAM_CAL_2MASS)) == NULL) {
308  cpl_msg_info(fctid,"No 2MASS index found -- cannot continue");
309  vircam_getstds_tidy();
310  return(-1);
311  }
312 
313  /* Get catalogue parameters */
314 
315  vircam_catpars(catindex,&(ps.catpath),&(ps.catname));
316  cpl_frame_delete(catindex);
317 
318  /* Now, how many image extensions do we want to do? If the extension
319  number is zero, then we loop for all possible extensions. If it
320  isn't then we just do the extension specified */
321 
322  vircam_exten_range(vircam_getstds_config.extenum,(const cpl_frame *)ps.img,
323  &jst,&jfn);
324  if (jst == -1 || jfn == -1) {
325  cpl_msg_error(fctid,"Unable to continue");
326  vircam_getstds_tidy();
327  return(-1);
328  }
329 
330  /* Now loop for all the extension... */
331 
332  status = VIR_OK;
333  for (j = jst; j <= jfn; j++) {
334  isfirst = (j == jst);
335 
336  /* Load up the images */
337 
338  ps.imgf = vircam_fits_load(ps.img,CPL_TYPE_FLOAT,j);
339  if (ps.img == NULL) {
340  vircam_getstds_tidy();
341  return(-1);
342  }
343 
344  /* Now do the correction */
345 
346  cpl_msg_info(fctid,"Extracting the standards");
347  (void)vircam_getstds(vircam_fits_get_ehu(ps.imgf),1,ps.catpath,
348  ps.catname,&(ps.stds),&status);
349  if (status != VIR_OK) {
350  vircam_getstds_tidy();
351  return(-1);
352  }
353 
354  /* Now save the result */
355 
356  cpl_msg_info(fctid,"Saving results for extension %" CPL_SIZE_FORMAT,
357  (cpl_size)j);
358  if (vircam_getstds_save(framelist,parlist) != 0) {
359  vircam_getstds_tidy();
360  return(-1);
361  }
362 
363  /* Tidy a few things before the next image */
364 
365  freetable(ps.stds);
366  freefits(ps.imgf);
367  }
368  vircam_getstds_tidy();
369  return(0);
370 }
371 
372 
373 /*---------------------------------------------------------------------------*/
380 /*---------------------------------------------------------------------------*/
381 
382 static int vircam_getstds_save(cpl_frameset *framelist,
383  cpl_parameterlist *parlist) {
384  const char *fctid = "vircam_getstds_save";
385  const char *outfile = "getstds.fits";
386  const char *recipeid = "vircam_getstds";
387  cpl_propertylist *plist,*elist;
388 
389  /* If we need to make a PHU then do that now based on the first frame
390  in the input frame list */
391 
392  if (isfirst) {
393 
394  /* Create a new product frame object and define some tags */
395 
396  product_frame = cpl_frame_new();
397  cpl_frame_set_filename(product_frame,outfile);
398  cpl_frame_set_tag(product_frame,VIRCAM_PRO_STDTAB);
399  cpl_frame_set_type(product_frame,CPL_FRAME_TYPE_TABLE);
400  cpl_frame_set_group(product_frame,CPL_FRAME_GROUP_PRODUCT);
401  cpl_frame_set_level(product_frame,CPL_FRAME_LEVEL_FINAL);
402 
403  /* Set up product frame phu */
404 
405  plist = vircam_fits_get_phu(ps.imgf);
406  vircam_dfs_set_product_primary_header(plist,product_frame,framelist,
407  parlist,(char *)recipeid,
408  "?Dictionary?",NULL,0);
409 
410  /* Now fiddle with the extension header */
411 
412  elist = vircam_fits_get_ehu(ps.imgf);
413  vircam_dfs_set_product_exten_header(elist,product_frame,framelist,
414  parlist,(char *)recipeid,
415  "?Dictionary?",NULL);
416 
417  /* 'Save' the table */
418 
419  if (cpl_table_save(ps.stds,plist,elist,outfile,CPL_IO_DEFAULT)
420  != CPL_ERROR_NONE) {
421  cpl_msg_error(fctid,"Cannot save product");
422  cpl_frame_delete(product_frame);
423  return(-1);
424  }
425  cpl_frameset_insert(framelist,product_frame);
426 
427  /* Subsequent extensions...*/
428 
429  } else {
430 
431  /* Now fiddle with the extension header */
432 
433  elist = vircam_fits_get_ehu(ps.imgf);
434  vircam_dfs_set_product_exten_header(elist,product_frame,framelist,
435  parlist,(char *)recipeid,
436  "?Dictionary?",NULL);
437 
438  if (cpl_table_save(ps.stds,NULL,elist,outfile,CPL_IO_EXTEND)
439  != CPL_ERROR_NONE) {
440  cpl_msg_error(fctid,"Cannot save product table extension");
441  return(-1);
442  }
443  }
444 
445  return(0);
446 }
447 
448 
449 /*---------------------------------------------------------------------------*/
453 /*---------------------------------------------------------------------------*/
454 
455 static void vircam_getstds_init(void) {
456  ps.img = NULL;
457  ps.imgf = NULL;
458  ps.labels = NULL;
459  ps.plist = NULL;
460  ps.stds = NULL;
461  ps.catname = NULL;
462  ps.catpath = NULL;
463 }
464 
465 
466 /*---------------------------------------------------------------------------*/
470 /*---------------------------------------------------------------------------*/
471 
472 static void vircam_getstds_tidy(void) {
473  freeframe(ps.img);
474  freefits(ps.imgf);
475  freespace(ps.labels);
476  freepropertylist(ps.plist);
477  freetable(ps.stds);
478  freespace(ps.catname);
479  freespace(ps.catpath);
480 }
481 
484 /*
485 
486 $Log: not supported by cvs2svn $
487 Revision 1.16 2012/01/15 17:40:09 jim
488 Minor modifications to take into accout the changes in cpl API for v6
489 
490 Revision 1.15 2009/09/09 09:51:13 jim
491 modified to use new saving routines so that headers are right
492 
493 Revision 1.14 2007/10/25 18:39:22 jim
494 Altered to remove some lint messages
495 
496 Revision 1.13 2007/10/19 06:55:06 jim
497 Modifications made to use new method for directing the recipes to the
498 standard catalogues using the sof
499 
500 Revision 1.12 2007/10/15 12:53:55 jim
501 Modified for compatibility with cpl_4.0
502 
503 Revision 1.11 2007/07/09 13:22:09 jim
504 Modified to use new version of vircam_exten_range
505 
506 Revision 1.10 2007/04/23 12:47:54 jim
507 Changed default location for 2mass catalogue
508 
509 Revision 1.9 2007/04/13 12:27:38 jim
510 Added some extra docs
511 
512 Revision 1.8 2007/04/04 10:36:29 jim
513 Modified to use new dfs tags
514 
515 Revision 1.7 2007/03/01 12:42:59 jim
516 Modified slightly after code checking
517 
518 Revision 1.6 2006/11/27 12:11:10 jim
519 Modified to add the catname parameter
520 
521 Revision 1.5 2006/06/15 09:58:59 jim
522 Minor changes to docs
523 
524 Revision 1.4 2006/05/04 11:53:40 jim
525 Fixed _save routine so that it's more consistent with the standard CPL
526 way of doing things
527 
528 Revision 1.3 2006/05/02 11:29:13 jim
529 Fixed problem where propertylist was being deleted incorrectly
530 
531 Revision 1.2 2006/04/27 14:22:04 jim
532 Fixed docs
533 
534 Revision 1.1 2006/04/24 10:42:44 jim
535 New routine
536 
537 
538 */
539 
540 
541 
542 
543