vircam_matchstds.c

00001 /* $Id: vircam_matchstds.c,v 1.9 2007/07/09 13:22:09 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: 2007/07/09 13:22:09 $
00024  * $Revision: 1.9 $
00025  * $Name:  $
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     int         *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 nlab,jst,jfn,status,j;
00261 
00262     /* Check validity of input frameset */
00263 
00264     if (framelist == NULL || cpl_frameset_get_size(framelist) <= 0) {
00265         cpl_msg_error(fctid,"Input framelist NULL or has no input data\n");
00266         return(-1);
00267     }
00268 
00269     /* Initialise some things */
00270 
00271     vircam_matchstds_init();
00272 
00273     /* Get the parameters */
00274 
00275     p = cpl_parameterlist_find(parlist,"vircam.vircam_matchstds.extenum");
00276     vircam_matchstds_config.extenum = cpl_parameter_get_int(p);
00277 
00278     /* Sort out raw from calib frames */
00279 
00280     if (vircam_dfs_set_groups(framelist) != VIR_OK) {
00281         cpl_msg_error(fctid,"Cannot identify RAW and CALIB frames");
00282         vircam_matchstds_tidy();
00283         return(-1);
00284     }
00285 
00286     /* Get the frames */
00287 
00288     if ((ps.labels = cpl_frameset_labelise(framelist,vircam_compare_tags,
00289                                            &nlab)) == NULL) {
00290         cpl_msg_error(fctid,"Cannot labelise the input frames");
00291         vircam_matchstds_tidy();
00292         return(-1);
00293     }
00294     if ((ps.cat = vircam_frameset_subgroup_1(framelist,ps.labels,nlab,
00295                                              VIRCAM_CAL_OBJCAT)) == NULL) {
00296         cpl_msg_info(fctid,"No object catalogue found -- cannot continue");
00297         vircam_matchstds_tidy();
00298         return(-1);
00299     }
00300     if ((ps.stds = vircam_frameset_subgroup_1(framelist,ps.labels,nlab,
00301                                               VIRCAM_CAL_STDTAB)) == NULL) {
00302         cpl_msg_info(fctid,"No standards catalogue found -- cannot continue");
00303         vircam_matchstds_tidy();
00304         return(-1);
00305     }
00306 
00307     /* Now, how many image extensions do we want to do? If the extension
00308        number is zero, then we loop for all possible extensions. If it
00309        isn't then we just do the extension specified */
00310 
00311     vircam_exten_range(vircam_matchstds_config.extenum,
00312                        (const cpl_frame *)ps.cat,&jst,&jfn);
00313     if (jst == -1 || jfn == -1) {
00314         cpl_msg_error(fctid,"Unable to continue");
00315         vircam_matchstds_tidy();
00316         return(-1);
00317     }
00318 
00319     /* Now loop for all the extension... */
00320 
00321     status = VIR_OK;
00322     for (j = jst; j <= jfn; j++) {
00323         isfirst = (j == jst);
00324 
00325         /* Load up the tables */
00326 
00327         ps.catf = vircam_tfits_load(ps.cat,j);
00328         ps.stdsf = vircam_tfits_load(ps.stds,j);
00329         if (ps.stdsf == NULL || ps.catf == NULL) {
00330             freetfits(ps.catf);
00331             freetfits(ps.stdsf);
00332             cpl_msg_info(fctid,"No matching possible");
00333             continue;
00334         }
00335 
00336         /* Now do the correction */
00337 
00338         cpl_msg_info(fctid,"Doing the matching for extension %d",j);
00339         (void)vircam_matchstds(vircam_tfits_get_table(ps.catf),
00340                                vircam_tfits_get_table(ps.stdsf),10.0,
00341                                &(ps.outtab),&status);
00342         if (status != VIR_OK) {
00343             cpl_msg_info(fctid,"No matching done");
00344             status = VIR_OK;
00345         }
00346 
00347         /* Now save the result */
00348 
00349         cpl_msg_info(fctid,"Saving results for extension %d",j);
00350         if (vircam_matchstds_save(framelist,parlist) != 0) 
00351             cpl_msg_info(fctid,"No matching saved");
00352 
00353 
00354         /* Tidy a few things before the next image */
00355 
00356         freetfits(ps.catf);
00357         freetfits(ps.stdsf);
00358         freetable(ps.outtab);
00359     }
00360     vircam_matchstds_tidy();
00361     return(0);
00362 }
00363 
00364 /*---------------------------------------------------------------------------*/
00371 /*---------------------------------------------------------------------------*/
00372 
00373 static int vircam_matchstds_save(cpl_frameset *framelist, 
00374                                  cpl_parameterlist *parlist) {
00375     const char *recipeid = "vircam_matchstds";
00376     const char *fctid = "vircam_matchstds_save";
00377     const char *outfile = "matchstds.fits";
00378     cpl_propertylist *plist,*elist;
00379 
00380     /* Create the output table. First see if you need a primary */
00381 
00382     if (isfirst) {
00383 
00384         /* Create a new product frame object and define some tags */
00385 
00386         product_frame = cpl_frame_new();
00387         cpl_frame_set_filename(product_frame,outfile);
00388         cpl_frame_set_tag(product_frame,VIRCAM_PRO_MSTDTAB);
00389         cpl_frame_set_type(product_frame,CPL_FRAME_TYPE_TABLE);
00390         cpl_frame_set_group(product_frame,CPL_FRAME_GROUP_PRODUCT);
00391         cpl_frame_set_level(product_frame,CPL_FRAME_LEVEL_FINAL);
00392 
00393         /* Fiddle with the header new */
00394 
00395         plist = vircam_tfits_get_phu(ps.catf);
00396         vircam_dfs_set_product_primary_header(plist,product_frame,framelist,
00397                                               parlist,(char *)recipeid,
00398                                               "?Dictionary?");
00399 
00400         /* Get the extension header and tack the extra header items onto it. */
00401 
00402         elist = vircam_tfits_get_ehu(ps.catf);
00403         vircam_dfs_set_product_exten_header(elist,product_frame,framelist,
00404                                             parlist,(char *)recipeid,
00405                                             "?Dictionary?");
00406 
00407         /* 'Save' the PHU and create a table extension */
00408 
00409         if (cpl_table_save(ps.outtab,plist,elist,outfile,CPL_IO_DEFAULT)
00410             != CPL_ERROR_NONE) {
00411             cpl_msg_error(fctid,"Cannot save product table");
00412             cpl_frame_delete(product_frame);
00413             return(-1);
00414         }
00415         cpl_frameset_insert(framelist,product_frame);
00416 
00417     /* Otherwise save the next extension */
00418 
00419     } else {
00420 
00421         /* Get the extension header and tack the extra header items onto it. */
00422 
00423         elist = vircam_tfits_get_ehu(ps.catf);
00424         vircam_dfs_set_product_exten_header(elist,product_frame,framelist,
00425                                             parlist,(char *)recipeid,
00426                                             "?Dictionary?");
00427 
00428         /* Sae the table */
00429 
00430         if (cpl_table_save(ps.outtab,NULL,elist,outfile,CPL_IO_EXTEND)
00431             != CPL_ERROR_NONE) {
00432             cpl_msg_error(fctid,"Cannot save product table");
00433             return(-1);
00434         }
00435     }
00436 
00437     return(0);
00438 }
00439 
00440 
00441 /*---------------------------------------------------------------------------*/
00445 /*---------------------------------------------------------------------------*/
00446 
00447 static void vircam_matchstds_init(void) {
00448     ps.labels = NULL;
00449     ps.cat = NULL;
00450     ps.catf = NULL;
00451     ps.stds = NULL;
00452     ps.stdsf = NULL;
00453     ps.outtab = NULL;
00454 }
00455 
00456 
00457 /*---------------------------------------------------------------------------*/
00461 /*---------------------------------------------------------------------------*/
00462 
00463 static void vircam_matchstds_tidy(void) {
00464     freespace(ps.labels);
00465     freetfits(ps.catf);
00466     freetfits(ps.stdsf);
00467     freeframe(ps.stds);
00468     freeframe(ps.cat);
00469     freetable(ps.outtab);
00470 }
00471 
00475 /*
00476 
00477 $Log: vircam_matchstds.c,v $
00478 Revision 1.9  2007/07/09 13:22:09  jim
00479 Modified to use new version of vircam_exten_range
00480 
00481 Revision 1.8  2007/04/23 12:49:07  jim
00482 Changed behaviour for error condition
00483 
00484 Revision 1.7  2007/04/13 12:27:39  jim
00485 Added some extra docs
00486 
00487 Revision 1.6  2007/04/04 10:36:29  jim
00488 Modified to use new dfs tags
00489 
00490 Revision 1.5  2007/03/01 12:42:59  jim
00491 Modified slightly after code checking
00492 
00493 Revision 1.4  2006/06/15 09:58:59  jim
00494 Minor changes to docs
00495 
00496 Revision 1.3  2006/05/04 11:53:43  jim
00497 Fixed _save routine so that it's more consistent with the standard CPL
00498 way of doing things
00499 
00500 Revision 1.2  2006/04/27 14:22:05  jim
00501 Fixed docs
00502 
00503 Revision 1.1  2006/04/24 10:42:45  jim
00504 New routine
00505 
00506 
00507 */
00508 
00509 
00510 
00511 

Generated on Wed Apr 10 04:01:56 2013 for VIRCAM Pipeline by  doxygen 1.5.1