vircam_mkconf.c

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

Generated on 5 Mar 2013 for VIRCAM Pipeline by  doxygen 1.6.1