VIRCAM Pipeline  1.3.3
vircam_mask.c
1 /* $Id: vircam_mask.c,v 1.18 2013-10-15 16:46:54 jim Exp $
2  *
3  * This file is part of the VIRCAM Pipeline
4  * Copyright (C) 2005 Cambridge Astronomy Survey Unit
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  */
20 
21 /*
22  * $Author: jim $
23  * $Date: 2013-10-15 16:46:54 $
24  * $Revision: 1.18 $
25  * $Name: not supported by cvs2svn $
26  */
27 
28 /* Includes */
29 
30 #ifdef HAVE_CONFIG_H
31 #include <config.h>
32 #endif
33 
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <unistd.h>
37 
38 #include <cpl.h>
39 #include "vircam_utils.h"
40 #include "vircam_fits.h"
41 #include "vircam_mask.h"
42 #include "vircam_dfs.h"
43 
44 static unsigned char *vircam_mask_getbpm(vir_fits *bpmimage);
45 static unsigned char *vircam_mask_conf2bpm(vir_fits *cpmimage);
46 
60 /*---------------------------------------------------------------------------*/
84 /*---------------------------------------------------------------------------*/
85 
86 extern vir_mask *vircam_mask_define(cpl_frameset *framelist, cpl_size *labels,
87  cpl_size nlab) {
88  cpl_frame *master_mask;
89  int masktype;
90  vir_mask *m;
91  const char *fctid = "vircam_mask_define";
92 
93  /* What kind of mask are we defining here? */
94 
95  if ((master_mask = vircam_frameset_subgroup_1(framelist,labels,nlab,
96  VIRCAM_CAL_BPM)) == NULL) {
97  if ((master_mask = vircam_frameset_subgroup_1(framelist,labels,
98  nlab,VIRCAM_CAL_CONF)) == NULL) {
99  cpl_msg_info(fctid,
100  "No master pixel mask found -- all pixels considered good");
101  masktype = MASK_NONE;
102  } else {
103  masktype = MASK_CPM;
104  }
105  } else {
106  masktype = MASK_BPM;
107  }
108 
109  /* If a master mask is defined, then check to see if the file is
110  accessible. If it isn't then continue on as though you don't have one */
111 
112  if (master_mask != NULL) {
113  if (access(cpl_frame_get_filename(master_mask),R_OK) != 0) {
114  cpl_msg_warning(fctid,"File %s is not read accessible",
115  cpl_frame_get_filename(master_mask));
116  masktype = MASK_NONE;
117  freeframe(master_mask);
118  }
119  }
120 
121  /* Get the vir_mask structure... */
122 
123  m = cpl_malloc(sizeof(*m));
124 
125  /* Initialise a few things */
126 
127  m->master_mask = master_mask;
128  m->mask_image = NULL;
129  m->masktype = masktype;
130  m->nx = 0;
131  m->ny = 0;
132  m->mdata = NULL;
133 
134  /* Now return it */
135 
136  return(m);
137 }
138 
139 /*---------------------------------------------------------------------------*/
157 /*---------------------------------------------------------------------------*/
158 
159 extern vir_mask *vircam_objmask_define(cpl_frame *frame) {
160  vir_mask *m;
161 
162  /* If there isn't one then send back a NULL */
163 
164  if (frame == NULL)
165  return(NULL);
166 
167  /* Get the vir_mask structure... */
168 
169  m = cpl_malloc(sizeof(*m));
170 
171  /* Initialise a few things */
172 
173  m->master_mask = cpl_frame_duplicate(frame);
174  m->mask_image = NULL;
175  m->masktype = MASK_OPM;
176  m->nx = 0;
177  m->ny = 0;
178  m->mdata = NULL;
179 
180  /* Now return it */
181 
182  return(m);
183 }
184 
185 /*---------------------------------------------------------------------------*/
208 /*---------------------------------------------------------------------------*/
209 
210 extern int vircam_mask_load(vir_mask *m, int nexten, int nx, int ny) {
211 
212  /* Check for nonsense input */
213 
214  if (m == NULL)
215  return(VIR_FATAL);
216 
217  /* Look to see if the sizes make sense */
218 
219  if (nx <= 0 && ny <= 0 && m->masktype == MASK_NONE)
220  return(VIR_FATAL);
221 
222  /* If the mask image has already been loaded then free it up. */
223 
224  if (m->mask_image != NULL) {
225  vircam_fits_delete(m->mask_image);
226  freespace(m->mdata);
227  }
228 
229  /* Load up the image if there is one. */
230 
231  if (m->masktype != MASK_NONE) {
232  m->mask_image = vircam_fits_load(m->master_mask,CPL_TYPE_INT,nexten);
233  if (m->mask_image == NULL)
234  return(VIR_FATAL);
235  m->nx = (int)cpl_image_get_size_x(vircam_fits_get_image(m->mask_image));
236  m->ny = (int)cpl_image_get_size_y(vircam_fits_get_image(m->mask_image));
237  } else {
238  m->nx = nx;
239  m->ny = ny;
240  }
241 
242  /* Return the status */
243 
244  return(VIR_OK);
245 }
246 
247 /*---------------------------------------------------------------------------*/
266 /*---------------------------------------------------------------------------*/
267 
268 extern void vircam_mask_delete(vir_mask *m) {
269 
270  if (m == NULL)
271  return;
273  freeframe(m->master_mask);
274  freespace(m);
275 }
276 
277 /*---------------------------------------------------------------------------*/
299 /*---------------------------------------------------------------------------*/
300 
301 extern vir_mask *vircam_mask_wrap_bpm(unsigned char *inbpm, int nx, int ny) {
302  vir_mask *m;
303  cpl_image *im;
304  int *mdata,i;
305 
306  /* Get the vir_mask structure... */
307 
308  m = cpl_malloc(sizeof(m));
309 
310  /* Create the image and copy the input bpm to it */
311 
312  im = cpl_image_new((cpl_size)nx,(cpl_size)ny,CPL_TYPE_INT);
313  mdata = cpl_image_get_data_int(im);
314  for (i = 0; i < nx*ny; i++)
315  mdata[i] = (int)(inbpm[i]);
316 
317  /* Initialise a few things */
318 
319  m->master_mask = NULL;
320  m->mask_image = vircam_fits_wrap(im,NULL,NULL,NULL);
321  m->masktype = MASK_BPM;
322  m->nx = nx;
323  m->ny = ny;
324  m->mdata = inbpm;
325  return(m);
326 }
327 
328 /*---------------------------------------------------------------------------*/
347 /*---------------------------------------------------------------------------*/
348 
349 extern void vircam_mask_clear(vir_mask *m) {
350  if (m == NULL)
351  return;
352  freespace(m->mdata);
353  freefits(m->mask_image);
354  m->nx = 0;
355  m->ny = 0;
356 }
357 
358 /*---------------------------------------------------------------------------*/
383 /*---------------------------------------------------------------------------*/
384 
385 extern void vircam_mask_force(vir_mask *m, int nx, int ny) {
386  if (m == NULL)
387  return;
388  freespace(m->mdata);
389  freefits(m->mask_image);
390  freeframe(m->master_mask);
391  m->masktype = MASK_NONE;
392  m->nx = nx;
393  m->ny = ny;
394 }
395 
396 /*---------------------------------------------------------------------------*/
413 /*---------------------------------------------------------------------------*/
414 
415 extern vir_fits *vircam_mask_get_fits(vir_mask *m) {
416  return(m->mask_image);
417 }
418 
419 /*---------------------------------------------------------------------------*/
436 /*---------------------------------------------------------------------------*/
437 
438 extern const char *vircam_mask_get_filename(vir_mask *m) {
439  if (m->master_mask != NULL) {
440  return(cpl_frame_get_filename(m->master_mask));
441  } else {
442  return(NULL);
443  }
444 }
445 
446 
447 /*---------------------------------------------------------------------------*/
464 /*---------------------------------------------------------------------------*/
465 
466 extern int vircam_mask_get_size_x(vir_mask *m) {
467  return(m->nx);
468 }
469 
470 /*---------------------------------------------------------------------------*/
487 /*---------------------------------------------------------------------------*/
488 
489 extern int vircam_mask_get_size_y(vir_mask *m) {
490  return(m->ny);
491 }
492 
493 /*---------------------------------------------------------------------------*/
510 /*---------------------------------------------------------------------------*/
511 
512 extern int vircam_mask_get_type(vir_mask *m) {
513  return(m->masktype);
514 }
515 
516 /*---------------------------------------------------------------------------*/
533 /*---------------------------------------------------------------------------*/
534 
535 extern unsigned char *vircam_mask_get_data(vir_mask *m) {
536  unsigned char *bpm;
537  long npix;
538 
539  /* Has this already been done? */
540 
541  if (m->mdata != NULL)
542  return(m->mdata);
543 
544  /* Get the bpm depending on what type of input you have */
545 
546  switch (m->masktype) {
547  case MASK_NONE:
548  npix = (m->nx)*(m->ny);
549  bpm = cpl_calloc(npix,sizeof(*bpm));
550  break;
551  case MASK_BPM:
552  bpm = vircam_mask_getbpm(vircam_mask_get_fits(m));
553  break;
554  case MASK_OPM:
555  bpm = vircam_mask_getbpm(vircam_mask_get_fits(m));
556  break;
557  case MASK_CPM:
558  bpm = vircam_mask_conf2bpm(vircam_mask_get_fits(m));
559  break;
560  default:
561  npix = (m->nx)*(m->ny);
562  bpm = cpl_calloc(npix,sizeof(*bpm));
563  break;
564  }
565  m->mdata = bpm;
566  return(bpm);
567 }
568 
569 /*---------------------------------------------------------------------------*/
590 /*---------------------------------------------------------------------------*/
591 
592 static unsigned char *vircam_mask_getbpm(vir_fits *bpmimage) {
593  long npts,i;
594  int *bpmdata;
595  cpl_image *b;
596  unsigned char *bpm;
597 
598  /* Load the bad pixel map data */
599 
600  b = vircam_fits_get_image(bpmimage);
601  npts = cpl_image_get_size_x(b)*cpl_image_get_size_y(b);
602  bpmdata = cpl_image_get_data(b);
603 
604  /* Get some space for the bad pixel mask and define it */
605 
606  bpm = cpl_malloc(npts*sizeof(*bpm));
607  for (i = 0; i < npts; i++)
608  bpm[i] = (unsigned char)bpmdata[i];
609 
610  /* Tidy and exit */
611 
612  return(bpm);
613 }
614 
615 /*---------------------------------------------------------------------------*/
635 /*---------------------------------------------------------------------------*/
636 
637 static unsigned char *vircam_mask_conf2bpm(vir_fits *cpmimage) {
638  long npts,i;
639  int *cpmdata;
640  cpl_image *c;
641  unsigned char *bpm;
642 
643  /* Load the confidence map image and get its data */
644 
645  c = vircam_fits_get_image(cpmimage);
646  npts = cpl_image_get_size_x(c)*cpl_image_get_size_y(c);
647  cpmdata = cpl_image_get_data(c);
648 
649  /* Get some space for the bad pixel mask and define it where the
650  confidence map is zero */
651 
652  bpm = cpl_malloc(npts*sizeof(*bpm));
653  for (i = 0; i < npts; i++)
654  bpm[i] = (cpmdata[i] == 0);
655 
656  /* Tidy and exit */
657 
658  return(bpm);
659 }
660 
663 /*
664 
665 $Log: not supported by cvs2svn $
666 Revision 1.17 2012/01/27 12:25:10 jim
667 Fixed some casts
668 
669 Revision 1.16 2012/01/16 12:32:18 jim
670 A few more changes to fit in with cpl6
671 
672 Revision 1.15 2012/01/15 17:40:09 jim
673 Minor modifications to take into accout the changes in cpl API for v6
674 
675 Revision 1.14 2010/09/09 12:11:09 jim
676 Fixed problems with docs that make doxygen barf
677 
678 Revision 1.13 2010/06/07 12:42:40 jim
679 Modifications to get rid of compiler gripes
680 
681 Revision 1.12 2008/10/21 08:40:06 jim
682 fixed bug in vircam_mask_wrap_bpm
683 
684 Revision 1.11 2008/10/13 08:14:50 jim
685 Added vircam_mask_wrap_bpm
686 
687 Revision 1.10 2007/10/19 09:25:10 jim
688 Fixed problems with missing includes
689 
690 Revision 1.9 2007/07/18 15:34:56 jim
691 Added vircam_mask_force and vircam_mask_get_filename. Also made changes to
692 the way that vircam_mask_load and vircam_mask_define deal with corrupt or
693 missing mask extensions
694 
695 Revision 1.8 2007/04/04 10:34:55 jim
696 Modified to use new dfs tags
697 
698 Revision 1.7 2007/03/23 10:53:22 jim
699 Fixed little documentation errors
700 
701 Revision 1.6 2007/03/01 12:42:42 jim
702 Modified slightly after code checking
703 
704 Revision 1.5 2006/06/09 11:26:26 jim
705 Small changes to keep lint happy
706 
707 Revision 1.4 2006/05/04 10:05:07 jim
708 Fixed bug where the cpl_frame was being deleted by vircam_mask_clear. This
709 needs to remain until the whole structure is deleted.
710 
711 Revision 1.3 2006/05/02 13:25:47 jim
712 removed include to xmemory
713 
714 Revision 1.2 2006/03/22 12:13:08 jim
715 Fixed comments
716 
717 Revision 1.1 2006/03/22 11:37:58 jim
718 new file
719 
720 
721 */