vircam_platesol.c

00001 /* $Id: vircam_platesol.c,v 1.15 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.15 $
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 #include "vircam_paf.h"
00042 
00043 /* Function prototypes */
00044 
00045 static int vircam_platesol_create(cpl_plugin *) ;
00046 static int vircam_platesol_exec(cpl_plugin *) ;
00047 static int vircam_platesol_destroy(cpl_plugin *) ;
00048 static int vircam_platesol_test(cpl_parameterlist *, cpl_frameset *) ;
00049 static int vircam_platesol_save(void);
00050 static void vircam_platesol_init(void);
00051 static void vircam_platesol_tidy(void);
00052 
00053 static struct {
00054 
00055     /* Input */
00056 
00057     int         nconst;
00058     int         shiftan;
00059     int         extenum;
00060 
00061 } vircam_platesol_config;
00062 
00063 static struct {
00064     cpl_size    *labels;
00065     cpl_frame   *img;
00066     cpl_frame   *mstds;
00067     vir_fits    *imgf;
00068     vir_tfits   *mstdsf;
00069     FILE        *paf;
00070 } ps;
00071 
00072 
00073 static char vircam_platesol_description[] =
00074 "vircam_platesol -- VIRCAM plate solution fitting test recipe.\n\n"
00075 "Fit a plate solution to a matched standards table and write the resulting\n"
00076 "WCS to an input image header.\n\n"
00077 "The program accepts the following files in the SOF:\n\n"
00078 "    Tag                   Description\n"
00079 "    -----------------------------------------------------------------------\n"
00080 "    %-21s A input uncorrected image\n"
00081 "    %-21s A matched standards table\n"
00082 "The WCS values from the solution are written to a paf file. This is the\n"
00083 "only product of this recipe\n"
00084 "\n";
00085 
00137 /* Function code */
00138 
00139 
00140 /*---------------------------------------------------------------------------*/
00148 /*---------------------------------------------------------------------------*/
00149 
00150 int cpl_plugin_get_info(cpl_pluginlist *list) {
00151     cpl_recipe  *recipe = cpl_calloc(1,sizeof(*recipe));
00152     cpl_plugin  *plugin = &recipe->interface;
00153     char alldesc[SZ_ALLDESC];
00154     (void)snprintf(alldesc,SZ_ALLDESC,vircam_platesol_description,
00155                    VIRCAM_TEST_SCIENCE_RAW,VIRCAM_CAL_MSTDTAB);
00156 
00157     cpl_plugin_init(plugin,
00158                     CPL_PLUGIN_API,
00159                     VIRCAM_BINARY_VERSION,
00160                     CPL_PLUGIN_TYPE_RECIPE,
00161                     "vircam_platesol",
00162                     "VIRCAM plate solution test recipe [test]",
00163                     alldesc,
00164                     "Jim Lewis",
00165                     "jrl@ast.cam.ac.uk",
00166                     vircam_get_license(),
00167                     vircam_platesol_create,
00168                     vircam_platesol_exec,
00169                     vircam_platesol_destroy);
00170 
00171     cpl_pluginlist_append(list,plugin);
00172 
00173     return(0);
00174 }
00175 
00176 /*---------------------------------------------------------------------------*/
00185 /*---------------------------------------------------------------------------*/
00186 
00187 static int vircam_platesol_create(cpl_plugin *plugin) {
00188     cpl_recipe      *recipe;
00189     cpl_parameter   *p;
00190 
00191     /* Get the recipe out of the plugin */
00192 
00193     if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
00194         recipe = (cpl_recipe *)plugin;
00195     else
00196         return(-1);
00197 
00198     /* Create the parameters list in the cpl_recipe object */
00199 
00200     recipe->parameters = cpl_parameterlist_new();
00201 
00202     /* Fill in the parameters. First the number of constants */
00203 
00204     p = cpl_parameter_new_enum("vircam.vircam_platesol.nconst",
00205                                CPL_TYPE_INT,
00206                                "Number of plate constants",
00207                                "vircam.vircam_platesol",6,2,4,6);
00208     cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI,"nconst");
00209     cpl_parameterlist_append(recipe->parameters,p);
00210 
00211     /* Now whether or not to shift the tangent point */
00212 
00213     p = cpl_parameter_new_value("vircam.vircam_platesol.shiftan",
00214                                 CPL_TYPE_BOOL,
00215                                 "Shift position of tangent point",
00216                                 "vircam.vircam_platesol",FALSE);
00217     cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI,"shiftan");
00218     cpl_parameterlist_append(recipe->parameters,p);
00219 
00220     /* Extension number of input frames to use */
00221 
00222     p = cpl_parameter_new_range("vircam.vircam_platesol.extenum",
00223                                 CPL_TYPE_INT,
00224                                 "Extension number to be done, 0 == all",
00225                                 "vircam.vircam_platesol",1,0,16);
00226     cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI,"ext");
00227     cpl_parameterlist_append(recipe->parameters,p);
00228 
00229     /* Get out of here */
00230 
00231     return(0);
00232 }
00233 
00234 /*---------------------------------------------------------------------------*/
00240 /*---------------------------------------------------------------------------*/
00241 
00242 static int vircam_platesol_exec(cpl_plugin *plugin) {
00243     cpl_recipe  *recipe;
00244 
00245     /* Get the recipe out of the plugin */
00246 
00247     if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
00248         recipe = (cpl_recipe *)plugin;
00249     else
00250         return(-1);
00251 
00252     return(vircam_platesol_test(recipe->parameters,recipe->frames));
00253 }
00254 
00255 /*---------------------------------------------------------------------------*/
00261 /*---------------------------------------------------------------------------*/
00262 
00263 static int vircam_platesol_destroy(cpl_plugin *plugin) {
00264     cpl_recipe *recipe ;
00265 
00266     /* Get the recipe out of the plugin */
00267 
00268     if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
00269         recipe = (cpl_recipe *)plugin;
00270     else
00271         return(-1);
00272 
00273     cpl_parameterlist_delete(recipe->parameters);
00274     return(0);
00275 }
00276 
00277 /*---------------------------------------------------------------------------*/
00284 /*---------------------------------------------------------------------------*/
00285 
00286 static int vircam_platesol_test(cpl_parameterlist *parlist, 
00287                                 cpl_frameset *framelist) {
00288     const char *fctid="vircam_platesol";
00289     cpl_parameter *p;
00290     int jst,jfn,status,j;
00291     cpl_size nlab;
00292 
00293     /* Check validity of input frameset */
00294 
00295     if (framelist == NULL || cpl_frameset_get_size(framelist) <= 0) {
00296         cpl_msg_error(fctid,"Input framelist NULL or has no input data");
00297         return(-1);
00298     }
00299 
00300     /* Initialise some things */
00301 
00302     vircam_platesol_init();
00303 
00304     /* Get the parameters */
00305 
00306     p = cpl_parameterlist_find(parlist,"vircam.vircam_platesol.nconst");
00307     vircam_platesol_config.nconst = cpl_parameter_get_int(p);
00308     p = cpl_parameterlist_find(parlist,"vircam.vircam_platesol.shiftan");
00309     vircam_platesol_config.shiftan = cpl_parameter_get_bool(p);
00310     p = cpl_parameterlist_find(parlist,"vircam.vircam_platesol.extenum");
00311     vircam_platesol_config.extenum = cpl_parameter_get_int(p);
00312 
00313     /* Sort out raw from calib frames */
00314 
00315     if (vircam_dfs_set_groups(framelist) != VIR_OK) {
00316         cpl_msg_error(fctid,"Cannot identify RAW and CALIB frames");
00317         vircam_platesol_tidy();
00318         return(-1);
00319     }
00320 
00321     /* Get the frames */
00322 
00323     if ((ps.labels = cpl_frameset_labelise(framelist,vircam_compare_tags,
00324                                            &nlab)) == NULL) {
00325         cpl_msg_error(fctid,"Cannot labelise the input frames");
00326         vircam_platesol_tidy();
00327         return(-1);
00328     }
00329     if ((ps.mstds = vircam_frameset_subgroup_1(framelist,ps.labels,nlab,
00330                                                VIRCAM_CAL_MSTDTAB)) == NULL) {
00331         cpl_msg_info(fctid,"No matched standards table found -- cannot continue");
00332         vircam_platesol_tidy();
00333         return(-1);
00334     }
00335     if ((ps.img = vircam_frameset_subgroup_1(framelist,ps.labels,nlab,
00336                                              VIRCAM_TEST_SCIENCE_RAW)) == NULL) {
00337         cpl_msg_info(fctid,"No raw image found -- cannot continue");
00338         vircam_platesol_tidy();
00339         return(-1);
00340     }
00341 
00342     /* Now, how many image extensions do we want to do? If the extension
00343        number is zero, then we loop for all possible extensions. If it
00344        isn't then we just do the extension specified */
00345 
00346     vircam_exten_range(vircam_platesol_config.extenum,(const cpl_frame *)ps.img,
00347                        &jst,&jfn);
00348     if (jst == -1 || jfn == -1) {
00349         cpl_msg_error(fctid,"Unable to continue");
00350         vircam_platesol_tidy();
00351         return(-1);
00352     }
00353 
00354     /* Now loop for all the extension... */
00355 
00356     status = VIR_OK;
00357     for (j = jst; j <= jfn; j++) {
00358 
00359         /* Load up the images */
00360 
00361         ps.imgf = vircam_fits_load(ps.img,CPL_TYPE_FLOAT,j);
00362         ps.mstdsf = vircam_tfits_load(ps.mstds,j);
00363         if (ps.img == NULL || ps.mstdsf == NULL) {
00364             freefits(ps.imgf);
00365             freetfits(ps.mstdsf);
00366             cpl_msg_warning(fctid,"Unable to load one of the inputs");
00367             continue;
00368         }
00369 
00370         /* Now do the correction */
00371 
00372         cpl_msg_info(fctid,"Doing the plate solution for extension %" CPL_SIZE_FORMAT,
00373                      (cpl_size)j);
00374         (void)vircam_platesol(vircam_fits_get_ehu(ps.imgf),NULL,
00375                               vircam_tfits_get_table(ps.mstdsf),
00376                               vircam_platesol_config.nconst,
00377                               vircam_platesol_config.shiftan,&status);
00378         if (status != VIR_OK) {
00379             cpl_msg_warning(fctid,"Plate solution failed");
00380             status = VIR_OK;
00381         }
00382 
00383         /* Now save the result */
00384 
00385         cpl_msg_info(fctid,"Saving results for extension %" CPL_SIZE_FORMAT,
00386                      (cpl_size)j);
00387         if (vircam_platesol_save() != 0)
00388             cpl_msg_warning(fctid,"Save routine failed");
00389 
00390         /* Tidy a few things before the next image */
00391 
00392         freefits(ps.imgf);
00393         freetfits(ps.mstdsf);
00394     }
00395     vircam_platesol_tidy();
00396     return(0);
00397 }
00398 
00399 /*---------------------------------------------------------------------------*/
00404 /*---------------------------------------------------------------------------*/
00405 
00406 static int vircam_platesol_save(void) {
00407     const char *fctid = "vircam_platesol_save";
00408     const char *outfile = "platesol";
00409     const char *keys[] = {"CRVAL1","CRVAL2","CRPIX1","CRPIX2","CD1_1","CD1_2",
00410                           "CD2_1","CD2_2","PV2_3","ESO DRS NUMBRMS",
00411                           "ESO DRS STDCRMS","ESO DRS WCSRAOFF",
00412                           "ESO DRS WCSDECOFF"};
00413     int i,nkeys=13;
00414     cpl_propertylist *plist,*p2,*p3;
00415 
00416     /* Get the propertylist we need */
00417 
00418     plist = vircam_fits_get_ehu(ps.imgf);
00419 
00420     /* Extract the standard things */
00421 
00422     if ((p2 = vircam_paf_req_items(plist)) == NULL) {
00423         cpl_msg_error(fctid,"Unable to find required items in header");
00424         return(-1);
00425     }
00426     p3 = vircam_paf_phu_items(vircam_fits_get_phu(ps.imgf));
00427     vircam_merge_propertylists(p2,p3);
00428     freepropertylist(p3);
00429 
00430     /* Add in the extra stuff */
00431 
00432     for (i = 0; i < nkeys; i++) {
00433         cpl_propertylist_copy_property(p2,plist,keys[i]);
00434         if (cpl_error_get_code() != CPL_ERROR_NONE) {
00435             cpl_msg_error(fctid,"A header parameter %s is missing",keys[i]);
00436             cpl_propertylist_delete(p2);
00437             return(-1);
00438         }
00439     }
00440 
00441     /* Now write it all out */
00442     
00443     if (vircam_paf_print((char *)outfile,"VIRCAM/vircam_platesol",
00444                          "Test QC file",p2) != VIR_OK) {
00445         cpl_msg_error(fctid,"Error writing PAF");
00446         cpl_propertylist_delete(p2);
00447         return(-1);
00448     }
00449 
00450     /* Tidy and exit */
00451 
00452     cpl_propertylist_delete(p2);
00453     return(0);
00454 }
00455 
00456 
00457 /*---------------------------------------------------------------------------*/
00461 /*---------------------------------------------------------------------------*/
00462 
00463 static void vircam_platesol_init(void) {
00464     ps.labels = NULL;
00465     ps.img = NULL;
00466     ps.imgf = NULL;
00467     ps.mstds = NULL;
00468     ps.mstdsf = NULL;
00469 }
00470 
00471 
00472 /*---------------------------------------------------------------------------*/
00476 /*---------------------------------------------------------------------------*/
00477 
00478 static void vircam_platesol_tidy(void) {
00479     freespace(ps.labels);
00480     freefits(ps.imgf);
00481     freetfits(ps.mstdsf);
00482     freeframe(ps.mstds);
00483     freeframe(ps.img);
00484 }
00485 
00486 
00487 /*
00488 
00489 $Log: vircam_platesol.c,v $
00490 Revision 1.15  2012/01/16 14:44:02  jim
00491 Fixed test recipes for cpl6 compliance
00492 
00493 Revision 1.14  2012/01/15 17:40:09  jim
00494 Minor modifications to take into accout the changes in cpl API for v6
00495 
00496 Revision 1.13  2007/10/25 19:38:22  jim
00497 modified to keep lint happy
00498 
00499 Revision 1.12  2007/10/15 12:53:55  jim
00500 Modified for compatibility with cpl_4.0
00501 
00502 Revision 1.11  2007/07/09 13:22:09  jim
00503 Modified to use new version of vircam_exten_range
00504 
00505 Revision 1.10  2007/05/02 09:17:04  jim
00506 uses new vircam_platesol api
00507 
00508 Revision 1.9  2007/04/23 12:49:43  jim
00509 Modified error condition behaviour
00510 
00511 Revision 1.8  2007/04/13 12:27:39  jim
00512 Added some extra docs
00513 
00514 Revision 1.7  2007/04/04 10:36:29  jim
00515 Modified to use new dfs tags
00516 
00517 Revision 1.6  2007/03/01 12:42:59  jim
00518 Modified slightly after code checking
00519 
00520 Revision 1.5  2007/02/15 12:17:45  jim
00521 Modified to use new version of PAF files
00522 
00523 Revision 1.4  2006/06/15 09:59:00  jim
00524 Minor changes to docs
00525 
00526 Revision 1.3  2006/05/04 11:53:45  jim
00527 Fixed _save routine so that it's more consistent with the standard CPL
00528 way of doing things
00529 
00530 Revision 1.2  2006/04/27 14:22:06  jim
00531 Fixed docs
00532 
00533 Revision 1.1  2006/04/24 10:42:45  jim
00534 New routine
00535 
00536 
00537 */
00538 
00539 
00540 
00541 

Generated on 5 Mar 2013 for VIRCAM Pipeline by  doxygen 1.6.1