vircam_lincor.c

00001 /* $Id: vircam_lincor.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_pfits.h"
00042 
00043 /* Function prototypes */
00044 
00045 static int vircam_lincor_create(cpl_plugin *) ;
00046 static int vircam_lincor_exec(cpl_plugin *) ;
00047 static int vircam_lincor_destroy(cpl_plugin *) ;
00048 static int vircam_lincor_test(cpl_parameterlist *, cpl_frameset *) ;
00049 static int vircam_lincor_save(cpl_frameset *framelist, 
00050                               cpl_parameterlist *parlist);
00051 static void vircam_lincor_init(void);
00052 static void vircam_lincor_tidy(void);
00053 
00054 static struct {
00055 
00056     /* Input */
00057 
00058     int         extenum;
00059 
00060 } vircam_lincor_config;
00061 
00062 static struct {
00063     cpl_size    *labels;
00064     cpl_frame   *chan;
00065     cpl_frame   *img;
00066     vir_tfits   *chanf;
00067     vir_fits    *imgf;
00068 } ps;
00069 
00070 static int isfirst;
00071 static cpl_frame *product_frame = NULL;
00072 
00073 static char vircam_lincor_description[] =
00074 "vircam_lincor -- VIRCAM linearity correction test recipe.\n\n"
00075 "Linearity correct an input frame using a pre-existing channel table\n\n"
00076 "The program accepts the following files in the SOF:\n\n"
00077 "    Tag                   Description\n"
00078 "    -----------------------------------------------------------------------\n"
00079 "    %-21s A input uncorrected image\n"
00080 "    %-21s A channel table\n"
00081 "\n";
00082 
00129 /* Function code */
00130 
00131 /*---------------------------------------------------------------------------*/
00139 /*---------------------------------------------------------------------------*/
00140 
00141 int cpl_plugin_get_info(cpl_pluginlist *list) {
00142     cpl_recipe  *recipe = cpl_calloc(1,sizeof(*recipe));
00143     cpl_plugin  *plugin = &recipe->interface;
00144     char alldesc[SZ_ALLDESC];
00145     (void)snprintf(alldesc,SZ_ALLDESC,vircam_lincor_description,
00146                    VIRCAM_TEST_SCIENCE_RAW,VIRCAM_CAL_CHANTAB);
00147 
00148     cpl_plugin_init(plugin,
00149                     CPL_PLUGIN_API,
00150                     VIRCAM_BINARY_VERSION,
00151                     CPL_PLUGIN_TYPE_RECIPE,
00152                     "vircam_lincor",
00153                     "VIRCAM linearisation test recipe [test]",
00154                     alldesc,
00155                     "Jim Lewis",
00156                     "jrl@ast.cam.ac.uk",
00157                     vircam_get_license(),
00158                     vircam_lincor_create,
00159                     vircam_lincor_exec,
00160                     vircam_lincor_destroy);
00161 
00162     cpl_pluginlist_append(list,plugin);
00163 
00164     return(0);
00165 }
00166 
00167 /*---------------------------------------------------------------------------*/
00176 /*---------------------------------------------------------------------------*/
00177 
00178 static int vircam_lincor_create(cpl_plugin *plugin) {
00179     cpl_recipe      *recipe;
00180     cpl_parameter   *p;
00181 
00182     /* Get the recipe out of the plugin */
00183 
00184     if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
00185         recipe = (cpl_recipe *)plugin;
00186     else
00187         return(-1);
00188 
00189     /* Create the parameters list in the cpl_recipe object */
00190 
00191     recipe->parameters = cpl_parameterlist_new();
00192 
00193     /* Get the extension number of input frames to use */
00194 
00195     p = cpl_parameter_new_range("vircam.vircam_lincor.extenum",
00196                                 CPL_TYPE_INT,
00197                                 "Extension number to be done, 0 == all",
00198                                 "vircam.vircam_lincor",1,0,16);
00199     cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI,"ext");
00200     cpl_parameterlist_append(recipe->parameters,p);
00201 
00202     /* Get out of here */
00203 
00204     return(0);
00205 }
00206 
00207 /*---------------------------------------------------------------------------*/
00213 /*---------------------------------------------------------------------------*/
00214 
00215 static int vircam_lincor_exec(cpl_plugin *plugin) {
00216     cpl_recipe  *recipe;
00217 
00218     /* Get the recipe out of the plugin */
00219 
00220     if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
00221         recipe = (cpl_recipe *)plugin;
00222     else
00223         return(-1);
00224 
00225     return(vircam_lincor_test(recipe->parameters,recipe->frames));
00226 }
00227 
00228 
00229 /*---------------------------------------------------------------------------*/
00235 /*---------------------------------------------------------------------------*/
00236 
00237 static int vircam_lincor_destroy(cpl_plugin *plugin) {
00238     cpl_recipe *recipe ;
00239 
00240     /* Get the recipe out of the plugin */
00241 
00242     if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
00243         recipe = (cpl_recipe *)plugin;
00244     else
00245         return(-1);
00246 
00247     cpl_parameterlist_delete(recipe->parameters);
00248     return(0);
00249 }
00250 
00251 /*---------------------------------------------------------------------------*/
00258 /*---------------------------------------------------------------------------*/
00259 
00260 static int vircam_lincor_test(cpl_parameterlist *parlist, 
00261                               cpl_frameset *framelist) {
00262     const char *fctid="vircam_lincor";
00263     cpl_parameter *p;
00264     int jst,jfn,status,j,ndit;
00265     cpl_size nlab;
00266     cpl_propertylist *pp;
00267 
00268     /* Check validity of input frameset */
00269 
00270     if (framelist == NULL || cpl_frameset_get_size(framelist) <= 0) {
00271         cpl_msg_error(fctid,"Input framelist NULL or has no input data");
00272         return(-1);
00273     }
00274 
00275     /* Initialise some things */
00276 
00277     vircam_lincor_init();
00278 
00279     /* Get parameters */
00280 
00281     p = cpl_parameterlist_find(parlist,"vircam.vircam_lincor.extenum");
00282     vircam_lincor_config.extenum = cpl_parameter_get_int(p);
00283 
00284     /* Sort out raw from calib frames */
00285 
00286     if (vircam_dfs_set_groups(framelist) != VIR_OK) {
00287         cpl_msg_error(fctid,"Cannot identify RAW and CALIB frames");
00288         vircam_lincor_tidy();
00289         return(-1);
00290     }
00291 
00292     /* Get the frames */
00293 
00294     if ((ps.labels = cpl_frameset_labelise(framelist,vircam_compare_tags,
00295                                            &nlab)) == NULL) {
00296         cpl_msg_error(fctid,"Cannot labelise the input frames");
00297         vircam_lincor_tidy();
00298         return(-1);
00299     }
00300     if ((ps.chan = vircam_frameset_subgroup_1(framelist,ps.labels,nlab,
00301                                               VIRCAM_CAL_CHANTAB)) == NULL) {
00302         cpl_msg_info(fctid,"No channel table found -- cannot continue");
00303         vircam_lincor_tidy();
00304         return(-1);
00305     }
00306     if ((ps.img = vircam_frameset_subgroup_1(framelist,ps.labels,nlab,
00307                                              VIRCAM_TEST_SCIENCE_RAW)) == NULL) {
00308         cpl_msg_info(fctid,"No raw image found -- cannot continue");
00309         vircam_lincor_tidy();
00310         return(-1);
00311     }
00312 
00313     /* Get the number of DITs */
00314 
00315     pp = cpl_propertylist_load(cpl_frame_get_filename(ps.img),0);
00316     if (vircam_pfits_get_ndit(pp,&ndit) != VIR_OK) {
00317         cpl_msg_error(fctid,"No value for NDIT available");
00318         freepropertylist(pp);
00319         vircam_lincor_tidy();
00320         return(-1);
00321     }
00322     cpl_propertylist_delete(pp);
00323 
00324     /* Now, how many image extensions do we want to do? If the extension
00325        number is zero, then we loop for all possible extensions. If it
00326        isn't then we just do the extension specified */
00327 
00328     vircam_exten_range(vircam_lincor_config.extenum,(const cpl_frame *)ps.img,
00329                        &jst,&jfn);
00330     if (jst == -1 || jfn == -1) {
00331         cpl_msg_error(fctid,"Unable to continue");
00332         vircam_lincor_tidy();
00333         return(-1);
00334     }
00335 
00336     /* Now loop for all the extension... */
00337 
00338     status = VIR_OK;
00339     for (j = jst; j <= jfn; j++) {
00340         isfirst = (j == jst);
00341 
00342         /* Load up the images */
00343 
00344         ps.imgf = vircam_fits_load(ps.img,CPL_TYPE_FLOAT,j);
00345         ps.chanf = vircam_tfits_load(ps.chan,j);
00346         if (ps.img == NULL || ps.chanf == NULL) {
00347             vircam_lincor_tidy();
00348             return(-1);
00349         }
00350 
00351         /* Now do the correction */
00352 
00353         cpl_msg_info(fctid,"Doing the linearisation for extension %" CPL_SIZE_FORMAT,
00354                      (cpl_size)j);
00355         (void)vircam_lincor(ps.imgf,ps.chanf,1,ndit,&status);
00356         if (status != VIR_OK) {
00357             vircam_lincor_tidy();
00358             return(-1);
00359         }
00360 
00361         /* Now save the result */
00362 
00363         cpl_msg_info(fctid,"Saving results for extension %" CPL_SIZE_FORMAT,
00364                      (cpl_size)j);
00365         if (vircam_lincor_save(framelist,parlist) != 0) {
00366             vircam_lincor_tidy();
00367             return(-1);
00368         }
00369 
00370         /* Tidy a few things before the next image */
00371 
00372         freefits(ps.imgf);
00373         freetfits(ps.chanf);
00374     }
00375     vircam_lincor_tidy();
00376     return(0);
00377 }
00378 
00379 
00380 /*---------------------------------------------------------------------------*/
00387 /*---------------------------------------------------------------------------*/
00388 
00389 static int vircam_lincor_save(cpl_frameset *framelist,
00390                               cpl_parameterlist *parlist) {
00391     const char *recipeid = "vircam_lincor";
00392     const char *fctid = "vircam_lincor_save";
00393     const char *outfile = "lincor.fits";
00394     cpl_propertylist *plist;
00395 
00396     /* If we need to make a PHU then do that now based on the first frame
00397        in the input frame list */
00398 
00399     if (isfirst) {
00400 
00401         /* Create a new product frame object and define some tags */
00402 
00403         product_frame = cpl_frame_new();
00404         cpl_frame_set_filename(product_frame,outfile);
00405         cpl_frame_set_tag(product_frame,VIRCAM_PRO_SIMPLE_TEST);
00406         cpl_frame_set_type(product_frame,CPL_FRAME_TYPE_IMAGE);
00407         cpl_frame_set_group(product_frame,CPL_FRAME_GROUP_PRODUCT);
00408         cpl_frame_set_level(product_frame,CPL_FRAME_LEVEL_FINAL);
00409 
00410         /* Set up phu header */
00411 
00412         plist = vircam_fits_get_phu(ps.imgf);
00413         vircam_dfs_set_product_primary_header(plist,product_frame,framelist,
00414                                               parlist,(char *)recipeid,
00415                                               "?Dictionary?",NULL,0);
00416 
00417         /* 'Save' the PHU image */
00418 
00419         if (cpl_image_save(NULL,outfile,CPL_TYPE_UCHAR,plist,
00420                            CPL_IO_DEFAULT) != CPL_ERROR_NONE) {
00421             cpl_msg_error(fctid,"Cannot save product PHU");
00422             cpl_frame_delete(product_frame);
00423             return(-1);
00424         }
00425         cpl_frameset_insert(framelist,product_frame);
00426     }
00427 
00428     /* Get the extension property list */
00429 
00430     plist = vircam_fits_get_ehu(ps.imgf);
00431 
00432     /* Fiddle with the header now */
00433 
00434     vircam_dfs_set_product_exten_header(plist,product_frame,framelist,parlist,
00435                                         (char *)recipeid,"?Dictionary?",NULL);
00436 
00437     /* Save the image */
00438 
00439     if (cpl_image_save(vircam_fits_get_image(ps.imgf),outfile,CPL_TYPE_FLOAT,
00440                        plist,CPL_IO_EXTEND) != CPL_ERROR_NONE) {
00441         cpl_msg_error(fctid,"Cannot save product image extension");
00442         return(-1);
00443     }
00444 
00445     return(0);
00446 }
00447 
00448 
00449 /*---------------------------------------------------------------------------*/
00453 /*---------------------------------------------------------------------------*/
00454 
00455 static void vircam_lincor_init(void) {
00456     ps.labels = NULL;
00457     ps.chan = NULL;
00458     ps.chanf = NULL;
00459     ps.img = NULL;
00460     ps.imgf = NULL;
00461 }
00462 
00463 
00464 /*---------------------------------------------------------------------------*/
00468 /*---------------------------------------------------------------------------*/
00469 
00470 static void vircam_lincor_tidy(void) {
00471     freespace(ps.labels);
00472     freefits(ps.imgf);
00473     freetfits(ps.chanf);
00474     freeframe(ps.chan);
00475     freeframe(ps.img);
00476 }
00477 
00480 /*
00481 
00482 $Log: vircam_lincor.c,v $
00483 Revision 1.15  2012/01/16 14:44:02  jim
00484 Fixed test recipes for cpl6 compliance
00485 
00486 Revision 1.14  2012/01/15 17:40:09  jim
00487 Minor modifications to take into accout the changes in cpl API for v6
00488 
00489 Revision 1.13  2010/03/12 10:44:07  lbilbao
00490 Added missing header inclusion.
00491 
00492 Revision 1.12  2009/09/09 09:51:13  jim
00493 modified to use new saving routines so that headers are right
00494 
00495 Revision 1.11  2007/11/26 09:56:04  jim
00496 Fixed call to vircam_lincor to include ndit
00497 
00498 Revision 1.10  2007/10/15 12:53:55  jim
00499 Modified for compatibility with cpl_4.0
00500 
00501 Revision 1.9  2007/07/09 13:22:09  jim
00502 Modified to use new version of vircam_exten_range
00503 
00504 Revision 1.8  2007/04/13 12:27:39  jim
00505 Added some extra docs
00506 
00507 Revision 1.7  2007/04/04 10:36:29  jim
00508 Modified to use new dfs tags
00509 
00510 Revision 1.6  2007/03/01 12:42:59  jim
00511 Modified slightly after code checking
00512 
00513 Revision 1.5  2006/06/15 09:58:59  jim
00514 Minor changes to docs
00515 
00516 Revision 1.4  2006/05/04 11:53:43  jim
00517 Fixed _save routine so that it's more consistent with the standard CPL
00518 way of doing things
00519 
00520 Revision 1.3  2006/05/02 11:29:15  jim
00521 Fixed problem where propertylist was being deleted incorrectly
00522 
00523 Revision 1.2  2006/04/27 14:22:05  jim
00524 Fixed docs
00525 
00526 Revision 1.1  2006/04/24 10:42:45  jim
00527 New routine
00528 
00529 
00530 */
00531 
00532 
00533 
00534 
00535 

Generated on 5 Mar 2013 for VIRCAM Pipeline by  doxygen 1.6.1