vircam_lincor.c

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

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