vircam_mask.c

00001 /* $Id: vircam_mask.c,v 1.17 2012/01/27 12:25:10 jim Exp $
00002  *
00003  * This file is part of the VIRCAM Pipeline
00004  * Copyright (C) 2005 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/27 12:25:10 $
00024  * $Revision: 1.17 $
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 <stdlib.h>
00036 #include <unistd.h>
00037 
00038 #include <cpl.h>
00039 #include "vircam_utils.h"
00040 #include "vircam_fits.h"
00041 #include "vircam_mask.h"
00042 #include "vircam_dfs.h"
00043 
00044 static unsigned char *vircam_mask_getbpm(vir_fits *bpmimage);
00045 static unsigned char *vircam_mask_conf2bpm(vir_fits *cpmimage);
00046 
00060 /*---------------------------------------------------------------------------*/
00084 /*---------------------------------------------------------------------------*/
00085 
00086 extern vir_mask *vircam_mask_define(cpl_frameset *framelist, cpl_size *labels, 
00087                                     cpl_size nlab) {
00088     cpl_frame *master_mask;
00089     int masktype;
00090     vir_mask *m;
00091     const char *fctid = "vircam_define_mask";
00092 
00093     /* What kind of mask are we defining here? */
00094 
00095     if ((master_mask = vircam_frameset_subgroup_1(framelist,labels,nlab,
00096                                                   VIRCAM_CAL_BPM)) == NULL) {
00097         if ((master_mask = vircam_frameset_subgroup_1(framelist,labels,
00098                                                       nlab,VIRCAM_CAL_CONF)) == NULL) {
00099             cpl_msg_info(fctid,
00100                          "No master pixel mask found -- all pixels considered good");
00101             masktype = MASK_NONE;
00102         } else {
00103             masktype = MASK_CPM;
00104         }
00105     } else {
00106         masktype = MASK_BPM;
00107     }
00108 
00109     /* If a master mask is defined, then check to see if the file is 
00110        accessible. If it isn't then continue on as though you don't have one */
00111 
00112     if (master_mask != NULL) {
00113         if (access(cpl_frame_get_filename(master_mask),R_OK) != 0) {
00114             cpl_msg_warning(fctid,"File %s is not read accessible",
00115                             cpl_frame_get_filename(master_mask));
00116             masktype = MASK_NONE;
00117             freeframe(master_mask);
00118         }
00119     }
00120 
00121     /* Get the vir_mask structure... */
00122 
00123     m = cpl_malloc(sizeof(*m));
00124 
00125     /* Initialise a few things */
00126 
00127     m->master_mask = master_mask;
00128     m->mask_image = NULL;
00129     m->masktype = masktype;
00130     m->nx = 0;
00131     m->ny = 0;
00132     m->mdata = NULL;
00133 
00134     /* Now return it */
00135 
00136     return(m);    
00137 }
00138 
00139 /*---------------------------------------------------------------------------*/
00162 /*---------------------------------------------------------------------------*/
00163 
00164 extern int vircam_mask_load(vir_mask *m, int nexten, int nx, int ny) {
00165 
00166     /* Check for nonsense input */
00167 
00168     if (m == NULL)
00169         return(VIR_FATAL);
00170 
00171     /* Look to see if the sizes make sense */
00172 
00173     if (nx <= 0 && ny <= 0 && m->masktype == MASK_NONE)
00174         return(VIR_FATAL);
00175 
00176     /* If the mask image has already been loaded then free it up. */
00177 
00178     if (m->mask_image != NULL) {
00179         vircam_fits_delete(m->mask_image);
00180         freespace(m->mdata);
00181     }
00182 
00183     /* Load up the image if there is one. */
00184 
00185     if (m->masktype != MASK_NONE) {
00186         m->mask_image = vircam_fits_load(m->master_mask,CPL_TYPE_INT,nexten);
00187         if (m->mask_image == NULL)
00188             return(VIR_FATAL);
00189         m->nx = (int)cpl_image_get_size_x(vircam_fits_get_image(m->mask_image));
00190         m->ny = (int)cpl_image_get_size_y(vircam_fits_get_image(m->mask_image));
00191     } else {
00192         m->nx = nx;
00193         m->ny = ny;
00194     }
00195 
00196     /* Return the status */
00197 
00198     return(VIR_OK);
00199 }
00200 
00201 /*---------------------------------------------------------------------------*/
00220 /*---------------------------------------------------------------------------*/
00221 
00222 extern void vircam_mask_delete(vir_mask *m) {
00223 
00224     if (m == NULL)
00225         return;
00226     vircam_mask_clear(m);
00227     freeframe(m->master_mask); 
00228     freespace(m);
00229 }
00230 
00231 /*---------------------------------------------------------------------------*/
00253 /*---------------------------------------------------------------------------*/
00254 
00255 extern vir_mask *vircam_mask_wrap_bpm(unsigned char *inbpm, int nx, int ny) {
00256     vir_mask *m;
00257     cpl_image *im;
00258     int *mdata,i;
00259 
00260     /* Get the vir_mask structure... */
00261 
00262     m = cpl_malloc(sizeof(m));
00263 
00264     /* Create the image and copy the input bpm to it */
00265 
00266     im = cpl_image_new((cpl_size)nx,(cpl_size)ny,CPL_TYPE_INT);
00267     mdata = cpl_image_get_data_int(im);
00268     for (i = 0; i < nx*ny; i++)
00269         mdata[i] = (int)(inbpm[i]);
00270 
00271     /* Initialise a few things */
00272 
00273     m->master_mask = NULL;
00274     m->mask_image = vircam_fits_wrap(im,NULL,NULL,NULL);
00275     m->masktype = MASK_BPM;
00276     m->nx = nx;
00277     m->ny = ny;
00278     m->mdata = inbpm;
00279     return(m);
00280 }
00281 
00282 /*---------------------------------------------------------------------------*/
00301 /*---------------------------------------------------------------------------*/
00302 
00303 extern void vircam_mask_clear(vir_mask *m) {
00304     if (m == NULL)
00305         return;
00306     freespace(m->mdata);
00307     freefits(m->mask_image);
00308     m->nx = 0;
00309     m->ny = 0;
00310 }
00311 
00312 /*---------------------------------------------------------------------------*/
00337 /*---------------------------------------------------------------------------*/
00338 
00339 extern void vircam_mask_force(vir_mask *m, int nx, int ny) {
00340     if (m == NULL) 
00341         return;
00342     freespace(m->mdata);
00343     freefits(m->mask_image);
00344     freeframe(m->master_mask);
00345     m->masktype = MASK_NONE;
00346     m->nx = nx;
00347     m->ny = ny;
00348 }
00349     
00350 /*---------------------------------------------------------------------------*/
00367 /*---------------------------------------------------------------------------*/
00368 
00369 extern vir_fits *vircam_mask_get_fits(vir_mask *m) {
00370     return(m->mask_image);
00371 }
00372 
00373 /*---------------------------------------------------------------------------*/
00390 /*---------------------------------------------------------------------------*/
00391 
00392 extern const char *vircam_mask_get_filename(vir_mask *m) {
00393     if (m->master_mask != NULL) {
00394         return(cpl_frame_get_filename(m->master_mask));
00395     } else {
00396         return(NULL);
00397     }
00398 }
00399 
00400 
00401 /*---------------------------------------------------------------------------*/
00418 /*---------------------------------------------------------------------------*/
00419 
00420 extern int vircam_mask_get_size_x(vir_mask *m) {
00421     return(m->nx);
00422 }
00423 
00424 /*---------------------------------------------------------------------------*/
00441 /*---------------------------------------------------------------------------*/
00442 
00443 extern int vircam_mask_get_size_y(vir_mask *m) {
00444     return(m->ny);
00445 }
00446 
00447 /*---------------------------------------------------------------------------*/
00464 /*---------------------------------------------------------------------------*/
00465 
00466 extern int vircam_mask_get_type(vir_mask *m) {
00467     return(m->masktype);
00468 }
00469 
00470 /*---------------------------------------------------------------------------*/
00487 /*---------------------------------------------------------------------------*/
00488 
00489 extern unsigned char *vircam_mask_get_data(vir_mask *m) {
00490     unsigned char *bpm;
00491     long npix;
00492 
00493     /* Has this already been done? */
00494 
00495     if (m->mdata != NULL)
00496         return(m->mdata);
00497 
00498     /* Get the bpm depending on what type of input you have */
00499 
00500     switch (m->masktype) {
00501     case MASK_NONE:
00502         npix = (m->nx)*(m->ny);
00503         bpm = cpl_calloc(npix,sizeof(*bpm));
00504         break;
00505     case MASK_BPM:
00506         bpm = vircam_mask_getbpm(vircam_mask_get_fits(m));
00507         break;
00508     case MASK_CPM:
00509         bpm = vircam_mask_conf2bpm(vircam_mask_get_fits(m));
00510         break;
00511     default:
00512         npix = (m->nx)*(m->ny);
00513         bpm = cpl_calloc(npix,sizeof(*bpm));
00514         break;
00515     }
00516     m->mdata = bpm;
00517     return(bpm);
00518 }
00519 
00520 /*---------------------------------------------------------------------------*/
00541 /*---------------------------------------------------------------------------*/
00542 
00543 static unsigned char *vircam_mask_getbpm(vir_fits *bpmimage) {
00544     long npts,i;
00545     int *bpmdata;
00546     cpl_image *b;
00547     unsigned char *bpm;
00548 
00549     /* Load the bad pixel map data */
00550 
00551     b = vircam_fits_get_image(bpmimage);
00552     npts = cpl_image_get_size_x(b)*cpl_image_get_size_y(b);
00553     bpmdata = cpl_image_get_data(b);
00554 
00555     /* Get some space for the bad pixel mask and define it */
00556 
00557     bpm = cpl_malloc(npts*sizeof(*bpm));
00558     for (i = 0; i < npts; i++)
00559         bpm[i] = (unsigned char)bpmdata[i];
00560 
00561     /* Tidy and exit */
00562 
00563     return(bpm);
00564 }
00565         
00566 /*---------------------------------------------------------------------------*/
00586 /*---------------------------------------------------------------------------*/
00587 
00588 static unsigned char *vircam_mask_conf2bpm(vir_fits *cpmimage) {
00589     long npts,i;
00590     int *cpmdata;
00591     cpl_image *c;
00592     unsigned char *bpm;
00593 
00594     /* Load the confidence map image and get its data */
00595 
00596     c = vircam_fits_get_image(cpmimage);
00597     npts = cpl_image_get_size_x(c)*cpl_image_get_size_y(c);
00598     cpmdata = cpl_image_get_data(c);
00599 
00600     /* Get some space for the bad pixel mask and define it where the
00601        confidence map is zero */
00602 
00603     bpm = cpl_malloc(npts*sizeof(*bpm));
00604     for (i = 0; i < npts; i++)
00605         bpm[i] = (cpmdata[i] == 0);
00606 
00607     /* Tidy and exit */
00608 
00609     return(bpm);
00610 }
00611 
00614 /*
00615 
00616 $Log: vircam_mask.c,v $
00617 Revision 1.17  2012/01/27 12:25:10  jim
00618 Fixed some casts
00619 
00620 Revision 1.16  2012/01/16 12:32:18  jim
00621 A few more changes to fit in with cpl6
00622 
00623 Revision 1.15  2012/01/15 17:40:09  jim
00624 Minor modifications to take into accout the changes in cpl API for v6
00625 
00626 Revision 1.14  2010/09/09 12:11:09  jim
00627 Fixed problems with docs that make doxygen barf
00628 
00629 Revision 1.13  2010/06/07 12:42:40  jim
00630 Modifications to get rid of compiler gripes
00631 
00632 Revision 1.12  2008/10/21 08:40:06  jim
00633 fixed bug in vircam_mask_wrap_bpm
00634 
00635 Revision 1.11  2008/10/13 08:14:50  jim
00636 Added vircam_mask_wrap_bpm
00637 
00638 Revision 1.10  2007/10/19 09:25:10  jim
00639 Fixed problems with missing includes
00640 
00641 Revision 1.9  2007/07/18 15:34:56  jim
00642 Added vircam_mask_force and vircam_mask_get_filename. Also made changes to
00643 the way that vircam_mask_load and vircam_mask_define deal with corrupt or
00644 missing mask extensions
00645 
00646 Revision 1.8  2007/04/04 10:34:55  jim
00647 Modified to use new dfs tags
00648 
00649 Revision 1.7  2007/03/23 10:53:22  jim
00650 Fixed little documentation errors
00651 
00652 Revision 1.6  2007/03/01 12:42:42  jim
00653 Modified slightly after code checking
00654 
00655 Revision 1.5  2006/06/09 11:26:26  jim
00656 Small changes to keep lint happy
00657 
00658 Revision 1.4  2006/05/04 10:05:07  jim
00659 Fixed bug where the cpl_frame was being deleted by vircam_mask_clear. This
00660 needs to remain until the whole structure is deleted.
00661 
00662 Revision 1.3  2006/05/02 13:25:47  jim
00663 removed include to xmemory
00664 
00665 Revision 1.2  2006/03/22 12:13:08  jim
00666 Fixed comments
00667 
00668 Revision 1.1  2006/03/22 11:37:58  jim
00669 new file
00670 
00671 
00672 */

Generated on 5 Mar 2013 for VIRCAM Pipeline by  doxygen 1.6.1