vircam_matchstds.c

00001 /* $Id: vircam_matchstds.c,v 1.13 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.13 $
00025  * $Name: vcam-1_3_2 $
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_matchstds_create(cpl_plugin *);
00045 static int vircam_matchstds_exec(cpl_plugin *);
00046 static int vircam_matchstds_destroy(cpl_plugin *);
00047 static int vircam_matchstds_test(cpl_parameterlist *, cpl_frameset *);
00048 static int vircam_matchstds_save(cpl_frameset *framelist,
00049                                  cpl_parameterlist *parlist);
00050 static void vircam_matchstds_init(void);
00051 static void vircam_matchstds_tidy(void);
00052 
00053 static struct {
00054 
00055     /* Input */
00056 
00057     int         extenum;
00058 
00059 } vircam_matchstds_config;
00060 
00061 
00062 static struct {
00063     cpl_size    *labels;
00064     cpl_frame   *cat;
00065     cpl_frame   *stds;
00066     vir_tfits   *catf;
00067     vir_tfits   *stdsf;
00068     cpl_table   *outtab;
00069 } ps;
00070 
00071 static int isfirst;
00072 static cpl_frame *product_frame = NULL;
00073 
00074 static char vircam_matchstds_description[] =
00075 "vircam_matchstds -- VIRCAM recipe to test vircam_matchstds.\n\n"
00076 "Match a catalogue with a table of standards\n\n"
00077 "The program accepts the following files in the SOF:\n\n"
00078 "    Tag                   Description\n"
00079 "    -----------------------------------------------------------------------\n"
00080 "    %-21s An input catalogue of objects extracted from an image\n"
00081 "    %-21s An input catalogue of standard stars\n"
00082 "\n";
00083 
00126 /* Function code */
00127 
00128 /*---------------------------------------------------------------------------*/
00136 /*---------------------------------------------------------------------------*/
00137 
00138 int cpl_plugin_get_info(cpl_pluginlist *list) {
00139     cpl_recipe  *recipe = cpl_calloc(1,sizeof(*recipe));
00140     cpl_plugin  *plugin = &recipe->interface;
00141     char alldesc[SZ_ALLDESC];
00142     (void)snprintf(alldesc,SZ_ALLDESC,vircam_matchstds_description,
00143                    VIRCAM_CAL_OBJCAT,VIRCAM_CAL_STDTAB);
00144 
00145     cpl_plugin_init(plugin,
00146                     CPL_PLUGIN_API,
00147                     VIRCAM_BINARY_VERSION,
00148                     CPL_PLUGIN_TYPE_RECIPE,
00149                     "vircam_matchstds",
00150                     "VIRCAM catalogue and standards matching test recipe [test]",
00151                     alldesc,
00152                     "Jim Lewis",
00153                     "jrl@ast.cam.ac.uk",
00154                     vircam_get_license(),
00155                     vircam_matchstds_create,
00156                     vircam_matchstds_exec,
00157                     vircam_matchstds_destroy);
00158 
00159     cpl_pluginlist_append(list,plugin);
00160 
00161     return(0);
00162 }
00163 
00164 /*---------------------------------------------------------------------------*/
00173 /*---------------------------------------------------------------------------*/
00174 
00175 static int vircam_matchstds_create(cpl_plugin *plugin) {
00176     cpl_recipe      *recipe;
00177     cpl_parameter   *p;
00178 
00179     /* Get the recipe out of the plugin */
00180 
00181     if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
00182         recipe = (cpl_recipe *)plugin;
00183     else
00184         return(-1);
00185 
00186     /* Create the parameters list in the cpl_recipe object */
00187 
00188     recipe->parameters = cpl_parameterlist_new();
00189 
00190     /* Extension number of input frames to use */
00191 
00192     p = cpl_parameter_new_range("vircam.vircam_matchstds.extenum",
00193                                 CPL_TYPE_INT,
00194                                 "Extension number to be done, 0 == all",
00195                                 "vircam.vircam_matchstds",1,0,16);
00196     cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI,"ext");
00197     cpl_parameterlist_append(recipe->parameters,p);
00198 
00199     /* Get out of here */
00200 
00201     return(0);
00202 }
00203 
00204 /*---------------------------------------------------------------------------*/
00210 /*---------------------------------------------------------------------------*/
00211 
00212 static int vircam_matchstds_exec(cpl_plugin *plugin) {
00213     cpl_recipe  *recipe;
00214 
00215     /* Get the recipe out of the plugin */
00216 
00217     if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
00218         recipe = (cpl_recipe *)plugin;
00219     else
00220         return(-1);
00221 
00222     return(vircam_matchstds_test(recipe->parameters,recipe->frames));
00223 }
00224 
00225 /*---------------------------------------------------------------------------*/
00231 /*---------------------------------------------------------------------------*/
00232 
00233 static int vircam_matchstds_destroy(cpl_plugin *plugin) {
00234     cpl_recipe *recipe ;
00235 
00236     /* Get the recipe out of the plugin */
00237 
00238     if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
00239         recipe = (cpl_recipe *)plugin;
00240     else
00241         return(-1);
00242 
00243     cpl_parameterlist_delete(recipe->parameters);
00244     return(0);
00245 }
00246 
00247 /*---------------------------------------------------------------------------*/
00254 /*---------------------------------------------------------------------------*/
00255 
00256 static int vircam_matchstds_test(cpl_parameterlist *parlist, 
00257                                  cpl_frameset *framelist) {
00258     const char *fctid="vircam_matchstds";
00259     cpl_parameter *p;
00260     int jst,jfn,status,j;
00261     cpl_size nlab;
00262 
00263     /* Check validity of input frameset */
00264 
00265     if (framelist == NULL || cpl_frameset_get_size(framelist) <= 0) {
00266         cpl_msg_error(fctid,"Input framelist NULL or has no input data");
00267         return(-1);
00268     }
00269 
00270     /* Initialise some things */
00271 
00272     vircam_matchstds_init();
00273 
00274     /* Get the parameters */
00275 
00276     p = cpl_parameterlist_find(parlist,"vircam.vircam_matchstds.extenum");
00277     vircam_matchstds_config.extenum = cpl_parameter_get_int(p);
00278 
00279     /* Sort out raw from calib frames */
00280 
00281     if (vircam_dfs_set_groups(framelist) != VIR_OK) {
00282         cpl_msg_error(fctid,"Cannot identify RAW and CALIB frames");
00283         vircam_matchstds_tidy();
00284         return(-1);
00285     }
00286 
00287     /* Get the frames */
00288 
00289     if ((ps.labels = cpl_frameset_labelise(framelist,vircam_compare_tags,
00290                                            &nlab)) == NULL) {
00291         cpl_msg_error(fctid,"Cannot labelise the input frames");
00292         vircam_matchstds_tidy();
00293         return(-1);
00294     }
00295     if ((ps.cat = vircam_frameset_subgroup_1(framelist,ps.labels,nlab,
00296                                              VIRCAM_CAL_OBJCAT)) == NULL) {
00297         cpl_msg_info(fctid,"No object catalogue found -- cannot continue");
00298         vircam_matchstds_tidy();
00299         return(-1);
00300     }
00301     if ((ps.stds = vircam_frameset_subgroup_1(framelist,ps.labels,nlab,
00302                                               VIRCAM_CAL_STDTAB)) == NULL) {
00303         cpl_msg_info(fctid,"No standards catalogue found -- cannot continue");
00304         vircam_matchstds_tidy();
00305         return(-1);
00306     }
00307 
00308     /* Now, how many image extensions do we want to do? If the extension
00309        number is zero, then we loop for all possible extensions. If it
00310        isn't then we just do the extension specified */
00311 
00312     vircam_exten_range(vircam_matchstds_config.extenum,
00313                        (const cpl_frame *)ps.cat,&jst,&jfn);
00314     if (jst == -1 || jfn == -1) {
00315         cpl_msg_error(fctid,"Unable to continue");
00316         vircam_matchstds_tidy();
00317         return(-1);
00318     }
00319 
00320     /* Now loop for all the extension... */
00321 
00322     status = VIR_OK;
00323     for (j = jst; j <= jfn; j++) {
00324         isfirst = (j == jst);
00325 
00326         /* Load up the tables */
00327 
00328         ps.catf = vircam_tfits_load(ps.cat,j);
00329         ps.stdsf = vircam_tfits_load(ps.stds,j);
00330         if (ps.stdsf == NULL || ps.catf == NULL) {
00331             freetfits(ps.catf);
00332             freetfits(ps.stdsf);
00333             cpl_msg_info(fctid,"No matching possible");
00334             continue;
00335         }
00336 
00337         /* Now do the correction */
00338 
00339         cpl_msg_info(fctid,"Doing the matching for extension %" CPL_SIZE_FORMAT,
00340                      (cpl_size)j);
00341         (void)vircam_matchstds(vircam_tfits_get_table(ps.catf),
00342                                vircam_tfits_get_table(ps.stdsf),300.0,
00343                                &(ps.outtab),&status);
00344         if (status != VIR_OK) {
00345             cpl_msg_info(fctid,"No matching done");
00346             status = VIR_OK;
00347         }
00348 
00349         /* Now save the result */
00350 
00351         cpl_msg_info(fctid,"Saving results for extension %" CPL_SIZE_FORMAT,
00352                      (cpl_size)j);
00353         if (vircam_matchstds_save(framelist,parlist) != 0) 
00354             cpl_msg_info(fctid,"No matching saved");
00355 
00356 
00357         /* Tidy a few things before the next image */
00358 
00359         freetfits(ps.catf);
00360         freetfits(ps.stdsf);
00361         freetable(ps.outtab);
00362     }
00363     vircam_matchstds_tidy();
00364     return(0);
00365 }
00366 
00367 /*---------------------------------------------------------------------------*/
00374 /*---------------------------------------------------------------------------*/
00375 
00376 static int vircam_matchstds_save(cpl_frameset *framelist, 
00377                                  cpl_parameterlist *parlist) {
00378     const char *recipeid = "vircam_matchstds";
00379     const char *fctid = "vircam_matchstds_save";
00380     const char *outfile = "matchstds.fits";
00381     cpl_propertylist *plist,*elist;
00382 
00383     /* Create the output table. First see if you need a primary */
00384 
00385     if (isfirst) {
00386 
00387         /* Create a new product frame object and define some tags */
00388 
00389         product_frame = cpl_frame_new();
00390         cpl_frame_set_filename(product_frame,outfile);
00391         cpl_frame_set_tag(product_frame,VIRCAM_PRO_MSTDTAB);
00392         cpl_frame_set_type(product_frame,CPL_FRAME_TYPE_TABLE);
00393         cpl_frame_set_group(product_frame,CPL_FRAME_GROUP_PRODUCT);
00394         cpl_frame_set_level(product_frame,CPL_FRAME_LEVEL_FINAL);
00395 
00396         /* Fiddle with the header new */
00397 
00398         plist = vircam_tfits_get_phu(ps.catf);
00399         vircam_dfs_set_product_primary_header(plist,product_frame,framelist,
00400                                               parlist,(char *)recipeid,
00401                                               "?Dictionary?",NULL,0);
00402 
00403         /* Get the extension header and tack the extra header items onto it. */
00404 
00405         elist = vircam_tfits_get_ehu(ps.catf);
00406         vircam_dfs_set_product_exten_header(elist,product_frame,framelist,
00407                                             parlist,(char *)recipeid,
00408                                             "?Dictionary?",NULL);
00409 
00410         /* 'Save' the PHU and create a table extension */
00411 
00412         if (cpl_table_save(ps.outtab,plist,elist,outfile,CPL_IO_DEFAULT)
00413             != CPL_ERROR_NONE) {
00414             cpl_msg_error(fctid,"Cannot save product table");
00415             cpl_frame_delete(product_frame);
00416             return(-1);
00417         }
00418         cpl_frameset_insert(framelist,product_frame);
00419 
00420     /* Otherwise save the next extension */
00421 
00422     } else {
00423 
00424         /* Get the extension header and tack the extra header items onto it. */
00425 
00426         elist = vircam_tfits_get_ehu(ps.catf);
00427         vircam_dfs_set_product_exten_header(elist,product_frame,framelist,
00428                                             parlist,(char *)recipeid,
00429                                             "?Dictionary?",NULL);
00430 
00431         /* Sae the table */
00432 
00433         if (cpl_table_save(ps.outtab,NULL,elist,outfile,CPL_IO_EXTEND)
00434             != CPL_ERROR_NONE) {
00435             cpl_msg_error(fctid,"Cannot save product table");
00436             return(-1);
00437         }
00438     }
00439 
00440     return(0);
00441 }
00442 
00443 
00444 /*---------------------------------------------------------------------------*/
00448 /*---------------------------------------------------------------------------*/
00449 
00450 static void vircam_matchstds_init(void) {
00451     ps.labels = NULL;
00452     ps.cat = NULL;
00453     ps.catf = NULL;
00454     ps.stds = NULL;
00455     ps.stdsf = NULL;
00456     ps.outtab = NULL;
00457 }
00458 
00459 
00460 /*---------------------------------------------------------------------------*/
00464 /*---------------------------------------------------------------------------*/
00465 
00466 static void vircam_matchstds_tidy(void) {
00467     freespace(ps.labels);
00468     freetfits(ps.catf);
00469     freetfits(ps.stdsf);
00470     freeframe(ps.stds);
00471     freeframe(ps.cat);
00472     freetable(ps.outtab);
00473 }
00474 
00478 /*
00479 
00480 $Log: vircam_matchstds.c,v $
00481 Revision 1.13  2012/01/16 14:44:02  jim
00482 Fixed test recipes for cpl6 compliance
00483 
00484 Revision 1.12  2012/01/15 17:40:09  jim
00485 Minor modifications to take into accout the changes in cpl API for v6
00486 
00487 Revision 1.11  2009/09/09 09:51:13  jim
00488 modified to use new saving routines so that headers are right
00489 
00490 Revision 1.10  2009/07/03 12:30:39  jim
00491 Modified the search radius
00492 
00493 Revision 1.9  2007/07/09 13:22:09  jim
00494 Modified to use new version of vircam_exten_range
00495 
00496 Revision 1.8  2007/04/23 12:49:07  jim
00497 Changed behaviour for error condition
00498 
00499 Revision 1.7  2007/04/13 12:27:39  jim
00500 Added some extra docs
00501 
00502 Revision 1.6  2007/04/04 10:36:29  jim
00503 Modified to use new dfs tags
00504 
00505 Revision 1.5  2007/03/01 12:42:59  jim
00506 Modified slightly after code checking
00507 
00508 Revision 1.4  2006/06/15 09:58:59  jim
00509 Minor changes to docs
00510 
00511 Revision 1.3  2006/05/04 11:53:43  jim
00512 Fixed _save routine so that it's more consistent with the standard CPL
00513 way of doing things
00514 
00515 Revision 1.2  2006/04/27 14:22:05  jim
00516 Fixed docs
00517 
00518 Revision 1.1  2006/04/24 10:42:45  jim
00519 New routine
00520 
00521 
00522 */
00523 
00524 
00525 
00526 

Generated on 5 Mar 2013 for VIRCAM Pipeline by  doxygen 1.6.1