vircam_mask.c

00001 /* $Id: vircam_mask.c,v 1.10 2007/10/19 09: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: 2007/10/19 09:25:10 $
00024  * $Revision: 1.10 $
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 <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, int *labels, 
00087                                     int nlab) {
00088     cpl_frame *master_mask;
00089     int masktype;
00090     vir_mask *m;
00091     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 = cpl_image_get_size_x(vircam_fits_get_image(m->mask_image));
00190         m->ny = 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 /*---------------------------------------------------------------------------*/
00250 /*---------------------------------------------------------------------------*/
00251 
00252 extern void vircam_mask_clear(vir_mask *m) {
00253     if (m == NULL)
00254         return;
00255     freespace(m->mdata);
00256     freefits(m->mask_image);
00257     m->nx = 0;
00258     m->ny = 0;
00259 }
00260 
00261 /*---------------------------------------------------------------------------*/
00286 /*---------------------------------------------------------------------------*/
00287 
00288 extern void vircam_mask_force(vir_mask *m, int nx, int ny) {
00289     if (m == NULL) 
00290         return;
00291     freespace(m->mdata);
00292     freefits(m->mask_image);
00293     freeframe(m->master_mask);
00294     m->masktype = MASK_NONE;
00295     m->nx = nx;
00296     m->ny = ny;
00297 }
00298     
00299 /*---------------------------------------------------------------------------*/
00316 /*---------------------------------------------------------------------------*/
00317 
00318 extern vir_fits *vircam_mask_get_fits(vir_mask *m) {
00319     return(m->mask_image);
00320 }
00321 
00322 /*---------------------------------------------------------------------------*/
00339 /*---------------------------------------------------------------------------*/
00340 
00341 extern const char *vircam_mask_get_filename(vir_mask *m) {
00342     if (m->master_mask != NULL) {
00343         return(cpl_frame_get_filename(m->master_mask));
00344     } else {
00345         return(NULL);
00346     }
00347 }
00348 
00349 
00350 /*---------------------------------------------------------------------------*/
00367 /*---------------------------------------------------------------------------*/
00368 
00369 extern int vircam_mask_get_size_x(vir_mask *m) {
00370     return(m->nx);
00371 }
00372 
00373 /*---------------------------------------------------------------------------*/
00390 /*---------------------------------------------------------------------------*/
00391 
00392 extern int vircam_mask_get_size_y(vir_mask *m) {
00393     return(m->ny);
00394 }
00395 
00396 /*---------------------------------------------------------------------------*/
00413 /*---------------------------------------------------------------------------*/
00414 
00415 extern int vircam_mask_get_type(vir_mask *m) {
00416     return(m->masktype);
00417 }
00418 
00419 /*---------------------------------------------------------------------------*/
00436 /*---------------------------------------------------------------------------*/
00437 
00438 extern unsigned char *vircam_mask_get_data(vir_mask *m) {
00439     unsigned char *bpm;
00440     long npix;
00441 
00442     /* Has this already been done? */
00443 
00444     if (m->mdata != NULL)
00445         return(m->mdata);
00446 
00447     /* Get the bpm depending on what type of input you have */
00448 
00449     switch (m->masktype) {
00450     case MASK_NONE:
00451         npix = (m->nx)*(m->ny);
00452         bpm = cpl_calloc(npix,sizeof(*bpm));
00453         break;
00454     case MASK_BPM:
00455         bpm = vircam_mask_getbpm(vircam_mask_get_fits(m));
00456         break;
00457     case MASK_CPM:
00458         bpm = vircam_mask_conf2bpm(vircam_mask_get_fits(m));
00459         break;
00460     default:
00461         npix = (m->nx)*(m->ny);
00462         bpm = cpl_calloc(npix,sizeof(*bpm));
00463         break;
00464     }
00465     m->mdata = bpm;
00466     return(bpm);
00467 }
00468 
00469 /*---------------------------------------------------------------------------*/
00490 /*---------------------------------------------------------------------------*/
00491 
00492 static unsigned char *vircam_mask_getbpm(vir_fits *bpmimage) {
00493     long npts,i;
00494     int *bpmdata;
00495     cpl_image *b;
00496     unsigned char *bpm;
00497 
00498     /* Load the bad pixel map data */
00499 
00500     b = vircam_fits_get_image(bpmimage);
00501     npts = cpl_image_get_size_x(b)*cpl_image_get_size_y(b);
00502     bpmdata = cpl_image_get_data(b);
00503 
00504     /* Get some space for the bad pixel mask and define it */
00505 
00506     bpm = cpl_malloc(npts*sizeof(*bpm));
00507     for (i = 0; i < npts; i++)
00508         bpm[i] = (unsigned char)bpmdata[i];
00509 
00510     /* Tidy and exit */
00511 
00512     return(bpm);
00513 }
00514         
00515 /*---------------------------------------------------------------------------*/
00535 /*---------------------------------------------------------------------------*/
00536 
00537 static unsigned char *vircam_mask_conf2bpm(vir_fits *cpmimage) {
00538     long npts,i;
00539     int *cpmdata;
00540     cpl_image *c;
00541     unsigned char *bpm;
00542 
00543     /* Load the confidence map image and get its data */
00544 
00545     c = vircam_fits_get_image(cpmimage);
00546     npts = cpl_image_get_size_x(c)*cpl_image_get_size_y(c);
00547     cpmdata = cpl_image_get_data(c);
00548 
00549     /* Get some space for the bad pixel mask and define it where the
00550        confidence map is zero */
00551 
00552     bpm = cpl_malloc(npts*sizeof(*bpm));
00553     for (i = 0; i < npts; i++)
00554         bpm[i] = (cpmdata[i] == 0);
00555 
00556     /* Tidy and exit */
00557 
00558     return(bpm);
00559 }
00560 
00563 /*
00564 
00565 $Log: vircam_mask.c,v $
00566 Revision 1.10  2007/10/19 09:25:10  jim
00567 Fixed problems with missing includes
00568 
00569 Revision 1.9  2007/07/18 15:34:56  jim
00570 Added vircam_mask_force and vircam_mask_get_filename. Also made changes to
00571 the way that vircam_mask_load and vircam_mask_define deal with corrupt or
00572 missing mask extensions
00573 
00574 Revision 1.8  2007/04/04 10:34:55  jim
00575 Modified to use new dfs tags
00576 
00577 Revision 1.7  2007/03/23 10:53:22  jim
00578 Fixed little documentation errors
00579 
00580 Revision 1.6  2007/03/01 12:42:42  jim
00581 Modified slightly after code checking
00582 
00583 Revision 1.5  2006/06/09 11:26:26  jim
00584 Small changes to keep lint happy
00585 
00586 Revision 1.4  2006/05/04 10:05:07  jim
00587 Fixed bug where the cpl_frame was being deleted by vircam_mask_clear. This
00588 needs to remain until the whole structure is deleted.
00589 
00590 Revision 1.3  2006/05/02 13:25:47  jim
00591 removed include to xmemory
00592 
00593 Revision 1.2  2006/03/22 12:13:08  jim
00594 Fixed comments
00595 
00596 Revision 1.1  2006/03/22 11:37:58  jim
00597 new file
00598 
00599 
00600 */

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