vircam_flatcor.c

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

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