vircam_getstds.c

00001 /* $Id: vircam_getstds.c,v 1.17 2012/01/16 14:44:02 jim Exp $
00002  *
00003  * This file is part of the VIRCAM Pipeline
00004  * Copyright (C) 2006 Cambridge Astronomy Survey Unit
00005  *
00006  * This program is free software; you can redistribute it and/or modify
00007  * it under the terms of the GNU General Public License as published by
00008  * the Free Software Foundation; either version 2 of the License, or
00009  * (at your option) any later version.
00010  *
00011  * This program is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU General Public License
00017  * along with this program; if not, write to the Free Software
00018  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00019  */
00020 
00021 /*
00022  * $Author: jim $
00023  * $Date: 2012/01/16 14:44:02 $
00024  * $Revision: 1.17 $
00025  * $Name: vcam-1_3_0 $
00026  */
00027 
00028 /* Includes */
00029 
00030 #ifdef HAVE_CONFIG_H
00031 #include <config.h>
00032 #endif
00033 
00034 #include <stdio.h>
00035 #include <cpl.h>
00036 
00037 #include "vircam_utils.h"
00038 #include "vircam_dfs.h"
00039 #include "vircam_fits.h"
00040 #include "vircam_mods.h"
00041 
00042 /* Function prototypes */
00043 
00044 static int vircam_getstds_create(cpl_plugin *) ;
00045 static int vircam_getstds_exec(cpl_plugin *) ;
00046 static int vircam_getstds_destroy(cpl_plugin *) ;
00047 static int vircam_getstds_test(cpl_parameterlist *, cpl_frameset *) ;
00048 static int vircam_getstds_save(cpl_frameset *framelist, 
00049                                cpl_parameterlist *parlist);
00050 static void vircam_getstds_init(void);
00051 static void vircam_getstds_tidy(void);
00052 
00053 static struct {
00054 
00055     /* Input */
00056     
00057     int         extenum;
00058 
00059 } vircam_getstds_config;
00060 
00061 static struct {
00062     cpl_size         *labels;
00063     cpl_frame        *img;
00064     vir_fits         *imgf;
00065     cpl_propertylist *plist;
00066     cpl_table        *stds;
00067     char             *catpath;
00068     char             *catname;
00069 } ps;
00070 
00071 static int isfirst;
00072 static cpl_frame *product_frame = NULL;
00073 
00074 static char vircam_getstds_description[] =
00075 "vircam_getstds -- VIRCAM test recipe to get standard stars for a frame\n\n"
00076 "The program accepts the following files in the SOF:\n\n"
00077 "    Tag                   Description\n"
00078 "    -----------------------------------------------------------------------\n"
00079 "    %-21s A input image\n"
00080 "    %-21s Standard catalogue index file\n"
00081 "\n";
00082 
00128 /* Function code */
00129 
00130 /*---------------------------------------------------------------------------*/
00138 /*---------------------------------------------------------------------------*/
00139 
00140 int cpl_plugin_get_info(cpl_pluginlist *list) {
00141     cpl_recipe  *recipe = cpl_calloc(1,sizeof(*recipe));
00142     cpl_plugin  *plugin = &recipe->interface;
00143     char alldesc[SZ_ALLDESC];
00144     (void)snprintf(alldesc,SZ_ALLDESC,vircam_getstds_description,
00145                    VIRCAM_TEST_SCIENCE_RAW,VIRCAM_CAL_2MASS);
00146 
00147     cpl_plugin_init(plugin,
00148                     CPL_PLUGIN_API,
00149                     VIRCAM_BINARY_VERSION,
00150                     CPL_PLUGIN_TYPE_RECIPE,
00151                     "vircam_getstds",
00152                     "VIRCAM standard star extraction test recipe [test]",
00153                     alldesc,
00154                     "Jim Lewis",
00155                     "jrl@ast.cam.ac.uk",
00156                     vircam_get_license(),
00157                     vircam_getstds_create,
00158                     vircam_getstds_exec,
00159                     vircam_getstds_destroy);
00160 
00161     cpl_pluginlist_append(list,plugin);
00162 
00163     return(0);
00164 }
00165 
00166 /*---------------------------------------------------------------------------*/
00175 /*---------------------------------------------------------------------------*/
00176 
00177 static int vircam_getstds_create(cpl_plugin *plugin) {
00178     cpl_recipe      *recipe;
00179     cpl_parameter   *p;
00180 
00181     /* Get the recipe out of the plugin */
00182 
00183     if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
00184         recipe = (cpl_recipe *)plugin;
00185     else
00186         return(-1);
00187 
00188     /* Create the parameters list in the cpl_recipe object */
00189 
00190     recipe->parameters = cpl_parameterlist_new();
00191 
00192     /* Get the extension number of input frames to use */
00193 
00194     p = cpl_parameter_new_range("vircam.vircam_getstds.extenum",
00195                                 CPL_TYPE_INT,
00196                                 "Extension number to be done, 0 == all",
00197                                 "vircam.vircam_getstds",1,0,16);
00198     cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI,"ext");
00199     cpl_parameterlist_append(recipe->parameters,p);
00200 
00201     /* Get out of here */
00202 
00203     return(0);
00204 }
00205 
00206 /*---------------------------------------------------------------------------*/
00212 /*---------------------------------------------------------------------------*/
00213 
00214 static int vircam_getstds_exec(cpl_plugin *plugin) {
00215     cpl_recipe  *recipe;
00216 
00217     /* Get the recipe out of the plugin */
00218 
00219     if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
00220         recipe = (cpl_recipe *)plugin;
00221     else
00222         return(-1);
00223 
00224     return(vircam_getstds_test(recipe->parameters,recipe->frames));
00225 }
00226 
00227 
00228 /*---------------------------------------------------------------------------*/
00234 /*---------------------------------------------------------------------------*/
00235 
00236 static int vircam_getstds_destroy(cpl_plugin *plugin) {
00237     cpl_recipe *recipe ;
00238 
00239     /* Get the recipe out of the plugin */
00240 
00241     if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
00242         recipe = (cpl_recipe *)plugin;
00243     else
00244         return(-1);
00245 
00246     cpl_parameterlist_delete(recipe->parameters);
00247     return(0);
00248 }
00249 
00250 
00251 /*---------------------------------------------------------------------------*/
00258 /*---------------------------------------------------------------------------*/
00259 
00260 static int vircam_getstds_test(cpl_parameterlist *parlist, 
00261                                cpl_frameset *framelist) {
00262     const char *fctid="vircam_getstds";
00263     cpl_parameter *p;
00264     int jst,jfn,status,j;
00265     cpl_size nlab;
00266     cpl_frame *catindex;
00267 
00268     /* Check validity of input frameset */
00269 
00270     if (framelist == NULL || cpl_frameset_get_size(framelist) <= 0) {
00271         cpl_msg_error(fctid,"Input framelist NULL or has no input data");
00272         return(-1);
00273     }
00274 
00275     /* Initialise some things */
00276 
00277     vircam_getstds_init();
00278 
00279     /* Get parameters */
00280 
00281     p = cpl_parameterlist_find(parlist,"vircam.vircam_getstds.extenum");
00282     vircam_getstds_config.extenum = cpl_parameter_get_int(p);
00283 
00284     /* Sort out raw from calib frames */
00285 
00286     if (vircam_dfs_set_groups(framelist) != VIR_OK) {
00287         cpl_msg_error(fctid,"Cannot identify RAW and CALIB frames");
00288         vircam_getstds_tidy();
00289         return(-1);
00290     }
00291 
00292     /* Get the frames */
00293 
00294     if ((ps.labels = cpl_frameset_labelise(framelist,vircam_compare_tags,
00295                                            &nlab)) == NULL) {
00296         cpl_msg_error(fctid,"Cannot labelise the input frames");
00297         vircam_getstds_tidy();
00298         return(-1);
00299     }
00300     if ((ps.img = vircam_frameset_subgroup_1(framelist,ps.labels,nlab,
00301                                              VIRCAM_TEST_SCIENCE_RAW)) == NULL) {
00302         cpl_msg_info(fctid,"No raw image found -- cannot continue");
00303         vircam_getstds_tidy();
00304         return(-1);
00305     }
00306     if ((catindex = vircam_frameset_subgroup_1(framelist,ps.labels,nlab,
00307                                                VIRCAM_CAL_2MASS)) == NULL) {
00308         cpl_msg_info(fctid,"No 2MASS index found -- cannot continue");
00309         vircam_getstds_tidy();
00310         return(-1);
00311     }
00312     
00313     /* Get catalogue parameters */
00314 
00315     vircam_catpars(catindex,&(ps.catpath),&(ps.catname));
00316     cpl_frame_delete(catindex);
00317 
00318     /* Now, how many image extensions do we want to do? If the extension
00319        number is zero, then we loop for all possible extensions. If it
00320        isn't then we just do the extension specified */
00321 
00322     vircam_exten_range(vircam_getstds_config.extenum,(const cpl_frame *)ps.img,
00323                        &jst,&jfn);
00324     if (jst == -1 || jfn == -1) {
00325         cpl_msg_error(fctid,"Unable to continue");
00326         vircam_getstds_tidy();
00327         return(-1);
00328     }
00329 
00330     /* Now loop for all the extension... */
00331 
00332     status = VIR_OK;
00333     for (j = jst; j <= jfn; j++) {
00334         isfirst = (j == jst);
00335 
00336         /* Load up the images */
00337 
00338         ps.imgf = vircam_fits_load(ps.img,CPL_TYPE_FLOAT,j);
00339         if (ps.img == NULL) {
00340             vircam_getstds_tidy();
00341             return(-1);
00342         }
00343 
00344         /* Now do the correction */
00345 
00346         cpl_msg_info(fctid,"Extracting the standards");
00347         (void)vircam_getstds(vircam_fits_get_ehu(ps.imgf),1,ps.catpath,
00348                              ps.catname,&(ps.stds),&status);
00349         if (status != VIR_OK) {
00350             vircam_getstds_tidy();
00351             return(-1);
00352         }
00353 
00354         /* Now save the result */
00355 
00356         cpl_msg_info(fctid,"Saving results for extension %" CPL_SIZE_FORMAT,
00357                      (cpl_size)j);
00358         if (vircam_getstds_save(framelist,parlist) != 0) {
00359             vircam_getstds_tidy();
00360             return(-1);
00361         }
00362 
00363         /* Tidy a few things before the next image */
00364 
00365         freetable(ps.stds);
00366         freefits(ps.imgf);
00367     }
00368     vircam_getstds_tidy();
00369     return(0);
00370 }
00371 
00372 
00373 /*---------------------------------------------------------------------------*/
00380 /*---------------------------------------------------------------------------*/
00381 
00382 static int vircam_getstds_save(cpl_frameset *framelist,
00383                                cpl_parameterlist *parlist) {
00384     const char *fctid = "vircam_getstds_save";
00385     const char *outfile = "getstds.fits";
00386     const char *recipeid = "vircam_getstds";
00387     cpl_propertylist *plist,*elist;
00388 
00389     /* If we need to make a PHU then do that now based on the first frame
00390        in the input frame list */
00391 
00392     if (isfirst) {
00393 
00394         /* Create a new product frame object and define some tags */
00395 
00396         product_frame = cpl_frame_new();
00397         cpl_frame_set_filename(product_frame,outfile);
00398         cpl_frame_set_tag(product_frame,VIRCAM_PRO_STDTAB);
00399         cpl_frame_set_type(product_frame,CPL_FRAME_TYPE_TABLE);
00400         cpl_frame_set_group(product_frame,CPL_FRAME_GROUP_PRODUCT);
00401         cpl_frame_set_level(product_frame,CPL_FRAME_LEVEL_FINAL);
00402 
00403         /* Set up product frame phu */
00404 
00405         plist = vircam_fits_get_phu(ps.imgf);
00406         vircam_dfs_set_product_primary_header(plist,product_frame,framelist,
00407                                               parlist,(char *)recipeid,
00408                                               "?Dictionary?",NULL,0);
00409 
00410         /* Now fiddle with the extension header */
00411 
00412         elist = vircam_fits_get_ehu(ps.imgf);
00413         vircam_dfs_set_product_exten_header(elist,product_frame,framelist,
00414                                             parlist,(char *)recipeid,
00415                                             "?Dictionary?",NULL);
00416 
00417         /* 'Save' the table */
00418 
00419         if (cpl_table_save(ps.stds,plist,elist,outfile,CPL_IO_DEFAULT) 
00420             != CPL_ERROR_NONE) {
00421             cpl_msg_error(fctid,"Cannot save product");
00422             cpl_frame_delete(product_frame);
00423             return(-1);
00424         }
00425         cpl_frameset_insert(framelist,product_frame);
00426 
00427     /* Subsequent extensions...*/
00428 
00429     } else {
00430 
00431         /* Now fiddle with the extension header */
00432 
00433         elist = vircam_fits_get_ehu(ps.imgf);
00434         vircam_dfs_set_product_exten_header(elist,product_frame,framelist,
00435                                             parlist,(char *)recipeid,
00436                                             "?Dictionary?",NULL);
00437 
00438         if (cpl_table_save(ps.stds,NULL,elist,outfile,CPL_IO_EXTEND)
00439                            != CPL_ERROR_NONE) {
00440             cpl_msg_error(fctid,"Cannot save product table extension");
00441             return(-1);
00442         }       
00443     }
00444 
00445     return(0);
00446 }
00447 
00448 
00449 /*---------------------------------------------------------------------------*/
00453 /*---------------------------------------------------------------------------*/
00454 
00455 static void vircam_getstds_init(void) {
00456     ps.img = NULL;
00457     ps.imgf = NULL;
00458     ps.labels = NULL;
00459     ps.plist = NULL;
00460     ps.stds = NULL;
00461     ps.catname = NULL;
00462     ps.catpath = NULL;
00463 }
00464 
00465 
00466 /*---------------------------------------------------------------------------*/
00470 /*---------------------------------------------------------------------------*/
00471 
00472 static void vircam_getstds_tidy(void) {
00473     freeframe(ps.img);
00474     freefits(ps.imgf);
00475     freespace(ps.labels);
00476     freepropertylist(ps.plist);
00477     freetable(ps.stds);
00478     freespace(ps.catname);
00479     freespace(ps.catpath);
00480 }
00481 
00484 /*
00485 
00486 $Log: vircam_getstds.c,v $
00487 Revision 1.17  2012/01/16 14:44:02  jim
00488 Fixed test recipes for cpl6 compliance
00489 
00490 Revision 1.16  2012/01/15 17:40:09  jim
00491 Minor modifications to take into accout the changes in cpl API for v6
00492 
00493 Revision 1.15  2009/09/09 09:51:13  jim
00494 modified to use new saving routines so that headers are right
00495 
00496 Revision 1.14  2007/10/25 18:39:22  jim
00497 Altered to remove some lint messages
00498 
00499 Revision 1.13  2007/10/19 06:55:06  jim
00500 Modifications made to use new method for directing the recipes to the
00501 standard catalogues using the sof
00502 
00503 Revision 1.12  2007/10/15 12:53:55  jim
00504 Modified for compatibility with cpl_4.0
00505 
00506 Revision 1.11  2007/07/09 13:22:09  jim
00507 Modified to use new version of vircam_exten_range
00508 
00509 Revision 1.10  2007/04/23 12:47:54  jim
00510 Changed default location for 2mass catalogue
00511 
00512 Revision 1.9  2007/04/13 12:27:38  jim
00513 Added some extra docs
00514 
00515 Revision 1.8  2007/04/04 10:36:29  jim
00516 Modified to use new dfs tags
00517 
00518 Revision 1.7  2007/03/01 12:42:59  jim
00519 Modified slightly after code checking
00520 
00521 Revision 1.6  2006/11/27 12:11:10  jim
00522 Modified to add the catname parameter
00523 
00524 Revision 1.5  2006/06/15 09:58:59  jim
00525 Minor changes to docs
00526 
00527 Revision 1.4  2006/05/04 11:53:40  jim
00528 Fixed _save routine so that it's more consistent with the standard CPL
00529 way of doing things
00530 
00531 Revision 1.3  2006/05/02 11:29:13  jim
00532 Fixed problem where propertylist was being deleted incorrectly
00533 
00534 Revision 1.2  2006/04/27 14:22:04  jim
00535 Fixed docs
00536 
00537 Revision 1.1  2006/04/24 10:42:44  jim
00538 New routine
00539 
00540 
00541 */
00542 
00543 
00544 
00545 
00546 

Generated on 15 Mar 2012 for VIRCAM Pipeline by  doxygen 1.6.1