vircam_platesol.c

00001 /* $Id: vircam_platesol.c,v 1.13 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.13 $
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_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     int         *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 nlab,jst,jfn,status,j;
00291 
00292     /* Check validity of input frameset */
00293 
00294     if (framelist == NULL || cpl_frameset_get_size(framelist) <= 0) {
00295         cpl_msg_error(fctid,"Input framelist NULL or has no input data\n");
00296         return(-1);
00297     }
00298 
00299     /* Initialise some things */
00300 
00301     vircam_platesol_init();
00302 
00303     /* Get the parameters */
00304 
00305     p = cpl_parameterlist_find(parlist,"vircam.vircam_platesol.nconst");
00306     vircam_platesol_config.nconst = cpl_parameter_get_int(p);
00307     p = cpl_parameterlist_find(parlist,"vircam.vircam_platesol.shiftan");
00308     vircam_platesol_config.shiftan = cpl_parameter_get_bool(p);
00309     p = cpl_parameterlist_find(parlist,"vircam.vircam_platesol.extenum");
00310     vircam_platesol_config.extenum = cpl_parameter_get_int(p);
00311 
00312     /* Sort out raw from calib frames */
00313 
00314     if (vircam_dfs_set_groups(framelist) != VIR_OK) {
00315         cpl_msg_error(fctid,"Cannot identify RAW and CALIB frames");
00316         vircam_platesol_tidy();
00317         return(-1);
00318     }
00319 
00320     /* Get the frames */
00321 
00322     if ((ps.labels = cpl_frameset_labelise(framelist,vircam_compare_tags,
00323                                            &nlab)) == NULL) {
00324         cpl_msg_error(fctid,"Cannot labelise the input frames");
00325         vircam_platesol_tidy();
00326         return(-1);
00327     }
00328     if ((ps.mstds = vircam_frameset_subgroup_1(framelist,ps.labels,nlab,
00329                                                VIRCAM_CAL_MSTDTAB)) == NULL) {
00330         cpl_msg_info(fctid,"No matched standards table found -- cannot continue");
00331         vircam_platesol_tidy();
00332         return(-1);
00333     }
00334     if ((ps.img = vircam_frameset_subgroup_1(framelist,ps.labels,nlab,
00335                                              VIRCAM_TEST_SCIENCE_RAW)) == NULL) {
00336         cpl_msg_info(fctid,"No raw image found -- cannot continue");
00337         vircam_platesol_tidy();
00338         return(-1);
00339     }
00340 
00341     /* Now, how many image extensions do we want to do? If the extension
00342        number is zero, then we loop for all possible extensions. If it
00343        isn't then we just do the extension specified */
00344 
00345     vircam_exten_range(vircam_platesol_config.extenum,(const cpl_frame *)ps.img,
00346                        &jst,&jfn);
00347     if (jst == -1 || jfn == -1) {
00348         cpl_msg_error(fctid,"Unable to continue");
00349         vircam_platesol_tidy();
00350         return(-1);
00351     }
00352 
00353     /* Now loop for all the extension... */
00354 
00355     status = VIR_OK;
00356     for (j = jst; j <= jfn; j++) {
00357 
00358         /* Load up the images */
00359 
00360         ps.imgf = vircam_fits_load(ps.img,CPL_TYPE_FLOAT,j);
00361         ps.mstdsf = vircam_tfits_load(ps.mstds,j);
00362         if (ps.img == NULL || ps.mstdsf == NULL) {
00363             freefits(ps.imgf);
00364             freetfits(ps.mstdsf);
00365             cpl_msg_warning(fctid,"Unable to load one of the inputs");
00366             continue;
00367         }
00368 
00369         /* Now do the correction */
00370 
00371         cpl_msg_info(fctid,"Doing the plate solution for extension %d",j);
00372         (void)vircam_platesol(vircam_fits_get_ehu(ps.imgf),NULL,
00373                               vircam_tfits_get_table(ps.mstdsf),
00374                               vircam_platesol_config.nconst,
00375                               vircam_platesol_config.shiftan,&status);
00376         if (status != VIR_OK) {
00377             cpl_msg_warning(fctid,"Plate solution failed");
00378             status = VIR_OK;
00379         }
00380 
00381         /* Now save the result */
00382 
00383         cpl_msg_info(fctid,"Saving results for extension %d",j);
00384         if (vircam_platesol_save() != 0)
00385             cpl_msg_warning(fctid,"Save routine failed");
00386 
00387         /* Tidy a few things before the next image */
00388 
00389         freefits(ps.imgf);
00390         freetfits(ps.mstdsf);
00391     }
00392     vircam_platesol_tidy();
00393     return(0);
00394 }
00395 
00396 /*---------------------------------------------------------------------------*/
00401 /*---------------------------------------------------------------------------*/
00402 
00403 static int vircam_platesol_save(void) {
00404     const char *fctid = "vircam_platesol_save";
00405     const char *outfile = "platesol";
00406     const char *keys[] = {"CRVAL1","CRVAL2","CRPIX1","CRPIX2","CD1_1","CD1_2",
00407                           "CD2_1","CD2_2","PV2_3","ESO DRS NUMBRMS",
00408                           "ESO DRS STDCRMS","ESO DRS WCSRAOFF",
00409                           "ESO DRS WCSDECOFF"};
00410     int i,nkeys=13;
00411     cpl_propertylist *plist,*p2,*p3;
00412 
00413     /* Get the propertylist we need */
00414 
00415     plist = vircam_fits_get_ehu(ps.imgf);
00416 
00417     /* Extract the standard things */
00418 
00419     if ((p2 = vircam_paf_req_items(plist)) == NULL) {
00420         cpl_msg_error(fctid,"Unable to find required items in header");
00421         return(-1);
00422     }
00423     p3 = vircam_paf_phu_items(vircam_fits_get_phu(ps.imgf));
00424     vircam_merge_propertylists(p2,p3);
00425     freepropertylist(p3);
00426 
00427     /* Add in the extra stuff */
00428 
00429     for (i = 0; i < nkeys; i++) {
00430         cpl_propertylist_copy_property(p2,plist,keys[i]);
00431         if (cpl_error_get_code() != CPL_ERROR_NONE) {
00432             cpl_msg_error(fctid,"A header parameter %s is missing",keys[i]);
00433             cpl_propertylist_delete(p2);
00434             return(-1);
00435         }
00436     }
00437 
00438     /* Now write it all out */
00439     
00440     if (vircam_paf_print((char *)outfile,"VIRCAM/vircam_platesol",
00441                          "Test QC file",p2) != VIR_OK) {
00442         cpl_msg_error(fctid,"Error writing PAF\n");
00443         cpl_propertylist_delete(p2);
00444         return(-1);
00445     }
00446 
00447     /* Tidy and exit */
00448 
00449     cpl_propertylist_delete(p2);
00450     return(0);
00451 }
00452 
00453 
00454 /*---------------------------------------------------------------------------*/
00458 /*---------------------------------------------------------------------------*/
00459 
00460 static void vircam_platesol_init(void) {
00461     ps.labels = NULL;
00462     ps.img = NULL;
00463     ps.imgf = NULL;
00464     ps.mstds = NULL;
00465     ps.mstdsf = NULL;
00466 }
00467 
00468 
00469 /*---------------------------------------------------------------------------*/
00473 /*---------------------------------------------------------------------------*/
00474 
00475 static void vircam_platesol_tidy(void) {
00476     freespace(ps.labels);
00477     freefits(ps.imgf);
00478     freetfits(ps.mstdsf);
00479     freeframe(ps.mstds);
00480     freeframe(ps.img);
00481 }
00482 
00483 
00484 /*
00485 
00486 $Log: vircam_platesol.c,v $
00487 Revision 1.13  2007/10/25 19:38:22  jim
00488 modified to keep lint happy
00489 
00490 Revision 1.12  2007/10/15 12:53:55  jim
00491 Modified for compatibility with cpl_4.0
00492 
00493 Revision 1.11  2007/07/09 13:22:09  jim
00494 Modified to use new version of vircam_exten_range
00495 
00496 Revision 1.10  2007/05/02 09:17:04  jim
00497 uses new vircam_platesol api
00498 
00499 Revision 1.9  2007/04/23 12:49:43  jim
00500 Modified error condition behaviour
00501 
00502 Revision 1.8  2007/04/13 12:27:39  jim
00503 Added some extra docs
00504 
00505 Revision 1.7  2007/04/04 10:36:29  jim
00506 Modified to use new dfs tags
00507 
00508 Revision 1.6  2007/03/01 12:42:59  jim
00509 Modified slightly after code checking
00510 
00511 Revision 1.5  2007/02/15 12:17:45  jim
00512 Modified to use new version of PAF files
00513 
00514 Revision 1.4  2006/06/15 09:59:00  jim
00515 Minor changes to docs
00516 
00517 Revision 1.3  2006/05/04 11:53:45  jim
00518 Fixed _save routine so that it's more consistent with the standard CPL
00519 way of doing things
00520 
00521 Revision 1.2  2006/04/27 14:22:06  jim
00522 Fixed docs
00523 
00524 Revision 1.1  2006/04/24 10:42:45  jim
00525 New routine
00526 
00527 
00528 */
00529 
00530 
00531 
00532 

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