vircam_matchxy.c

00001 /* $Id: vircam_matchxy.c,v 1.11 2007/10/25 19:38:22 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/10/25 19:38:22 $
00024  * $Revision: 1.11 $
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 #include "vircam_paf.h"
00042 
00043 /* Function prototypes */
00044 
00045 static int vircam_matchxy_create(cpl_plugin *);
00046 static int vircam_matchxy_exec(cpl_plugin *);
00047 static int vircam_matchxy_destroy(cpl_plugin *);
00048 static int vircam_matchxy_test(cpl_parameterlist *, cpl_frameset *);
00049 static int vircam_matchxy_save(void);
00050 static void vircam_matchxy_init(void);
00051 static void vircam_matchxy_tidy(void);
00052 
00053 static struct {
00054 
00055     /* Input */
00056 
00057     int         extenum;
00058 
00059     /* Output */
00060 
00061     float       xoff;
00062     float       yoff;
00063     int         nm;
00064 
00065 } vircam_matchxy_config;
00066 
00067 
00068 static struct {
00069     int          *labels;
00070     cpl_frameset *catlist;
00071     cpl_frame    *cat1;
00072     cpl_frame    *cat2;
00073     vir_tfits    *cat1f;
00074     vir_tfits    *cat2f;
00075 } ps;
00076 
00077 
00078 static char vircam_matchxy_description[] =
00079 "vircam_matchxy -- VIRCAM recipe to test vircam_matchxy.\n\n"
00080 "Match a catalogue with another to get x,y offsets\n\n"
00081 "The program accepts the following files in the SOF:\n\n"
00082 "    Tag                   Description\n"
00083 "    -----------------------------------------------------------------------\n"
00084 "    %-21s Input catalogues of objects extracted from an image\n"
00085 "\n";
00086 
00125 /* Function code */
00126 
00127 /*---------------------------------------------------------------------------*/
00135 /*---------------------------------------------------------------------------*/
00136 
00137 int cpl_plugin_get_info(cpl_pluginlist *list) {
00138     cpl_recipe  *recipe = cpl_calloc(1,sizeof(*recipe));
00139     cpl_plugin  *plugin = &recipe->interface;
00140     char alldesc[SZ_ALLDESC];
00141     (void)snprintf(alldesc,SZ_ALLDESC,vircam_matchxy_description,
00142                    VIRCAM_CAL_OBJCAT);
00143 
00144     cpl_plugin_init(plugin,
00145                     CPL_PLUGIN_API,
00146                     VIRCAM_BINARY_VERSION,
00147                     CPL_PLUGIN_TYPE_RECIPE,
00148                     "vircam_matchxy",
00149                     "VIRCAM catalogue matching test recipe [test]",
00150                     alldesc,
00151                     "Jim Lewis",
00152                     "jrl@ast.cam.ac.uk",
00153                     vircam_get_license(),
00154                     vircam_matchxy_create,
00155                     vircam_matchxy_exec,
00156                     vircam_matchxy_destroy);
00157 
00158     cpl_pluginlist_append(list,plugin);
00159 
00160     return(0);
00161 }
00162 
00163 /*---------------------------------------------------------------------------*/
00172 /*---------------------------------------------------------------------------*/
00173 
00174 static int vircam_matchxy_create(cpl_plugin *plugin) {
00175     cpl_recipe      *recipe;
00176     cpl_parameter   *p;
00177 
00178     /* Get the recipe out of the plugin */
00179 
00180     if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
00181         recipe = (cpl_recipe *)plugin;
00182     else
00183         return(-1);
00184 
00185     /* Create the parameters list in the cpl_recipe object */
00186 
00187     recipe->parameters = cpl_parameterlist_new();
00188 
00189     /* Extension number of input frames to use */
00190 
00191     p = cpl_parameter_new_range("vircam.vircam_matchxy.extenum",
00192                                 CPL_TYPE_INT,
00193                                 "Extension number to be done, 0 == all",
00194                                 "vircam.vircam_matchxy",1,0,16);
00195     cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI,"ext");
00196     cpl_parameterlist_append(recipe->parameters,p);
00197 
00198     /* Get out of here */
00199 
00200     return(0);
00201 }
00202 
00203 /*---------------------------------------------------------------------------*/
00209 /*---------------------------------------------------------------------------*/
00210 
00211 static int vircam_matchxy_exec(cpl_plugin *plugin) {
00212     cpl_recipe  *recipe;
00213 
00214     /* Get the recipe out of the plugin */
00215 
00216     if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
00217         recipe = (cpl_recipe *)plugin;
00218     else
00219         return(-1);
00220 
00221     return(vircam_matchxy_test(recipe->parameters,recipe->frames));
00222 }
00223 
00224 /*---------------------------------------------------------------------------*/
00230 /*---------------------------------------------------------------------------*/
00231 
00232 static int vircam_matchxy_destroy(cpl_plugin *plugin) {
00233     cpl_recipe *recipe ;
00234 
00235     /* Get the recipe out of the plugin */
00236 
00237     if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
00238         recipe = (cpl_recipe *)plugin;
00239     else
00240         return(-1);
00241 
00242     cpl_parameterlist_delete(recipe->parameters);
00243     return(0);
00244 }
00245 
00246 /*---------------------------------------------------------------------------*/
00253 /*---------------------------------------------------------------------------*/
00254 
00255 static int vircam_matchxy_test(cpl_parameterlist *parlist, 
00256                                cpl_frameset *framelist) {
00257     const char *fctid="vircam_matchxy";
00258     cpl_parameter *p;
00259     int nlab,jst,jfn,status,j;
00260 
00261     /* Check validity of input frameset */
00262 
00263     if (framelist == NULL || cpl_frameset_get_size(framelist) <= 0) {
00264         cpl_msg_error(fctid,"Input framelist NULL or has no input data\n");
00265         return(-1);
00266     }
00267 
00268     /* Initialise some things */
00269 
00270     vircam_matchxy_init();
00271 
00272     /* Get the parameters */
00273 
00274     p = cpl_parameterlist_find(parlist,"vircam.vircam_matchxy.extenum");
00275     vircam_matchxy_config.extenum = cpl_parameter_get_int(p);
00276 
00277     /* Sort out raw from calib frames */
00278 
00279     if (vircam_dfs_set_groups(framelist) != VIR_OK) {
00280         cpl_msg_error(fctid,"Cannot identify RAW and CALIB frames");
00281         vircam_matchxy_tidy();
00282         return(-1);
00283     }
00284 
00285     /* Get the frames */
00286 
00287     if ((ps.labels = cpl_frameset_labelise(framelist,vircam_compare_tags,
00288                                            &nlab)) == NULL) {
00289         cpl_msg_error(fctid,"Cannot labelise the input frames");
00290         vircam_matchxy_tidy();
00291         return(-1);
00292     }
00293     if ((ps.catlist = vircam_frameset_subgroup(framelist,ps.labels,nlab,
00294                                                VIRCAM_CAL_OBJCAT)) == NULL) {
00295         cpl_msg_error(fctid,"No object catalogues found -- cannot continue");
00296         vircam_matchxy_tidy();
00297         return(-1);
00298     }
00299     ps.cat1 = cpl_frameset_get_frame(ps.catlist,0);
00300     ps.cat2 = cpl_frameset_get_frame(ps.catlist,1);
00301     if (ps.cat1 == NULL || ps.cat2 == NULL) {
00302         cpl_msg_error(fctid,"List does not contain two object catalogues");
00303         vircam_matchxy_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_matchxy_config.extenum,
00312                        (const cpl_frame *)ps.cat1,&jst,&jfn);
00313     if (jst == -1 || jfn == -1) {
00314         cpl_msg_error(fctid,"Unable to continue");
00315         vircam_matchxy_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 
00324         /* Load up the tables */
00325 
00326         ps.cat1f = vircam_tfits_load(ps.cat1,j);
00327         ps.cat2f = vircam_tfits_load(ps.cat2,j);
00328         if (ps.cat1f == NULL || ps.cat2f == NULL) {
00329             cpl_msg_warning(fctid,"No matching done\n");
00330             continue;
00331         }
00332 
00333         /* Now do the correction */
00334 
00335         cpl_msg_info(fctid,"Doing the matching for extension %d",j);
00336         (void)vircam_matchxy(vircam_tfits_get_table(ps.cat1f),
00337                              vircam_tfits_get_table(ps.cat2f),200.0,
00338                              &(vircam_matchxy_config.xoff),
00339                              &(vircam_matchxy_config.yoff),
00340                              &(vircam_matchxy_config.nm),&status);
00341         if (status != VIR_OK) {
00342             cpl_msg_warning(fctid,"No matching results done\n");
00343             status = VIR_OK;
00344         }
00345 
00346         /* Now save the result */
00347 
00348         cpl_msg_info(fctid,"Saving results for extension %d",j);
00349         if (vircam_matchxy_save() != 0) 
00350             cpl_msg_warning(fctid,"No matching results saved\n");
00351 
00352         /* Tidy a few things before the next image */
00353 
00354         freetfits(ps.cat1f);
00355         freetfits(ps.cat2f);
00356     }
00357     vircam_matchxy_tidy();
00358     return(0);
00359 }
00360 
00361 /*---------------------------------------------------------------------------*/
00366 /*---------------------------------------------------------------------------*/
00367 
00368 static int vircam_matchxy_save(void) {
00369     const char *fctid = "vircam_matchxy_save";
00370     const char *outfile = "matchxy";
00371     cpl_propertylist *p,*p2,*p3;
00372 
00373     /* Get a propertylist from the first file extension */
00374 
00375     p = vircam_tfits_get_ehu(ps.cat1f);
00376 
00377     /* Remove this line when they fix cpl so that the date-obs isn't removed
00378        from the table extensions */
00379 
00380     cpl_propertylist_update_string(p,"DATE-OBS","ABC");
00381 
00382     /* Extract the required things */
00383 
00384     p2 = vircam_paf_req_items(p);
00385     p3 = vircam_paf_phu_items(vircam_tfits_get_phu(ps.cat1f));
00386     vircam_merge_propertylists(p2,p3);
00387     freepropertylist(p3);
00388 
00389     /* Add some new stuff in */
00390 
00391     cpl_propertylist_update_float(p2,"ESO QC XOFF",vircam_matchxy_config.xoff);
00392     cpl_propertylist_set_comment(p2,"ESO QC XOFF",
00393                                  "Calculated X offset (pixels)");
00394     cpl_propertylist_update_float(p2,"ESO QC YOFF",vircam_matchxy_config.yoff);
00395     cpl_propertylist_set_comment(p2,"ESO QC YOFF",
00396                                  "Calculated Y offset (pixels)");
00397     cpl_propertylist_update_int(p2,"ESO QC NUMMATCH",
00398                                   vircam_matchxy_config.nm);
00399     cpl_propertylist_set_comment(p2,"ESO QC NUMMATCH",
00400                                  "Number of matching objects");
00401 
00402     /* Now write the PAF */
00403 
00404     if (vircam_paf_print((char *)outfile,"VIRCAM/vircam_matchxy",
00405                          "Test output file",p2) != VIR_OK) {
00406         cpl_msg_error(fctid,"Unable to write PAF");
00407         cpl_propertylist_delete(p2);
00408         return(-1);
00409     }
00410     cpl_propertylist_delete(p2);
00411     return(0);
00412 }
00413 
00414 
00415 
00416 /*---------------------------------------------------------------------------*/
00420 /*---------------------------------------------------------------------------*/
00421 
00422 static void vircam_matchxy_init(void) {
00423     ps.labels = NULL;
00424     ps.cat1 = NULL;
00425     ps.cat2 = NULL;
00426     ps.cat1f = NULL;
00427     ps.cat2f = NULL;
00428     ps.catlist = NULL;
00429 }
00430 
00431 
00432 /*---------------------------------------------------------------------------*/
00436 /*---------------------------------------------------------------------------*/
00437 
00438 static void vircam_matchxy_tidy(void) {
00439     freespace(ps.labels);
00440     freetfits(ps.cat1f);
00441     freetfits(ps.cat2f);
00442     freeframeset(ps.catlist);
00443 }
00444 
00447 /*
00448 
00449 $Log: vircam_matchxy.c,v $
00450 Revision 1.11  2007/10/25 19:38:22  jim
00451 modified to keep lint happy
00452 
00453 Revision 1.10  2007/07/09 13:22:09  jim
00454 Modified to use new version of vircam_exten_range
00455 
00456 Revision 1.9  2007/04/23 12:49:07  jim
00457 Changed behaviour for error condition
00458 
00459 Revision 1.8  2007/04/13 12:27:39  jim
00460 Added some extra docs
00461 
00462 Revision 1.7  2007/04/04 10:36:29  jim
00463 Modified to use new dfs tags
00464 
00465 Revision 1.6  2007/03/01 12:42:59  jim
00466 Modified slightly after code checking
00467 
00468 Revision 1.5  2007/02/15 12:17:45  jim
00469 Modified to use new version of PAF files
00470 
00471 Revision 1.4  2006/06/15 09:59:00  jim
00472 Minor changes to docs
00473 
00474 Revision 1.3  2006/05/04 11:53:44  jim
00475 Fixed _save routine so that it's more consistent with the standard CPL
00476 way of doing things
00477 
00478 Revision 1.2  2006/04/27 14:22:05  jim
00479 Fixed docs
00480 
00481 Revision 1.1  2006/04/24 10:42:45  jim
00482 New routine
00483 
00484 
00485 */
00486 
00487 
00488 
00489 

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