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