VIRCAM Pipeline 2.3.12
casu_mask.c
1/* $Id: casu_mask.c,v 1.4 2015/09/14 18:47:00 jim Exp $
2 *
3 * This file is part of the CASU Pipeline utilities
4 * Copyright (C) 2015 European Southern Observatory
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: 2015/09/14 18:47:00 $
24 * $Revision: 1.4 $
25 * $Name: $
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 "catalogue/casu_utils.h"
40#include "catalogue/casu_fits.h"
41#include "casu_mask.h"
42
43static unsigned char *casu_mask_getbpm(casu_fits *bpmimage);
44static unsigned char *casu_mask_conf2bpm(casu_fits *cpmimage);
45
59/*---------------------------------------------------------------------------*/
87/*---------------------------------------------------------------------------*/
88
89extern casu_mask *casu_mask_define(cpl_frameset *framelist, cpl_size *labels,
90 cpl_size nlab, const char *conftag,
91 const char *bpmtag) {
92 cpl_frame *master_mask;
93 int masktype;
94 casu_mask *m;
95 const char *fctid = "casu_mask_define";
96
97 /* What kind of mask are we defining here? */
98
99 if ((master_mask = casu_frameset_subgroup_1(framelist,labels,nlab,
100 bpmtag)) == NULL) {
101 if ((master_mask = casu_frameset_subgroup_1(framelist,labels,nlab,
102 conftag)) == NULL) {
103 cpl_msg_info(fctid,
104 "No master pixel mask found -- all pixels considered good");
105 masktype = MASK_NONE;
106 } else {
107 masktype = MASK_CPM;
108 }
109 } else {
110 masktype = MASK_BPM;
111 }
112
113 /* If a master mask is defined, then check to see if the file is
114 accessible. If it isn't then continue on as though you don't have one */
115
116 if (master_mask != NULL) {
117 if (access(cpl_frame_get_filename(master_mask),R_OK) != 0) {
118 cpl_msg_warning(fctid,"File %s is not read accessible",
119 cpl_frame_get_filename(master_mask));
120 masktype = MASK_NONE;
121 freeframe(master_mask);
122 }
123 }
124
125 /* Get the casu_mask structure... */
126
127 m = cpl_malloc(sizeof(*m));
128
129 /* Initialise a few things */
130
131 m->master_mask = master_mask;
132 m->mask_image = NULL;
133 m->masktype = masktype;
134 m->nx = 0;
135 m->ny = 0;
136 m->mdata = NULL;
137
138 /* Now return it */
139
140 return(m);
141}
142
143/*---------------------------------------------------------------------------*/
161/*---------------------------------------------------------------------------*/
162
163extern casu_mask *casu_objmask_define(cpl_frame *frame) {
164 casu_mask *m;
165
166 /* If there isn't one then send back a NULL */
167
168 if (frame == NULL)
169 return(NULL);
170
171 /* Get the casu_mask structure... */
172
173 m = cpl_malloc(sizeof(*m));
174
175 /* Initialise a few things */
176
177 m->master_mask = cpl_frame_duplicate(frame);
178 m->mask_image = NULL;
179 m->masktype = MASK_OPM;
180 m->nx = 0;
181 m->ny = 0;
182 m->mdata = NULL;
183
184 /* Now return it */
185
186 return(m);
187}
188
189/*---------------------------------------------------------------------------*/
212/*---------------------------------------------------------------------------*/
213
214extern int casu_mask_load(casu_mask *m, int nexten, int nx, int ny) {
215
216 /* Check for nonsense input */
217
218 if (m == NULL)
219 return(CASU_FATAL);
220
221 /* Look to see if the sizes make sense */
222
223 if (nx <= 0 && ny <= 0 && m->masktype == MASK_NONE)
224 return(CASU_FATAL);
225
226 /* If the mask image has already been loaded then free it up. */
227
228 if (m->mask_image != NULL) {
229 casu_fits_delete(m->mask_image);
230 freespace(m->mdata);
231 }
232
233 /* Load up the image if there is one. */
234
235 if (m->masktype != MASK_NONE) {
236 if (m->masktype == MASK_CPM)
237 m->mask_image = casu_fits_load(m->master_mask,CPL_TYPE_UNSPECIFIED,
238 nexten);
239 else
240 m->mask_image = casu_fits_load(m->master_mask,CPL_TYPE_INT,nexten);
241 if (m->mask_image == NULL)
242 return(CASU_FATAL);
243 m->nx = (int)cpl_image_get_size_x(casu_fits_get_image(m->mask_image));
244 m->ny = (int)cpl_image_get_size_y(casu_fits_get_image(m->mask_image));
245 } else {
246 m->nx = nx;
247 m->ny = ny;
248 }
249
250 /* Return the status */
251
252 return(CASU_OK);
253}
254
255/*---------------------------------------------------------------------------*/
274/*---------------------------------------------------------------------------*/
275
276extern void casu_mask_delete(casu_mask *m) {
277
278 if (m == NULL)
279 return;
281 freeframe(m->master_mask);
282 cpl_free(m);
283}
284
285/*---------------------------------------------------------------------------*/
307/*---------------------------------------------------------------------------*/
308
309extern casu_mask *casu_mask_wrap_bpm(unsigned char *inbpm, int nx, int ny) {
310 casu_mask *m;
311 cpl_image *im;
312 int *mdata,i;
313
314 /* Get the casu_mask structure... */
315
316 m = cpl_malloc(sizeof(*m));
317
318 /* Create the image and copy the input bpm to it */
319
320 im = cpl_image_new((cpl_size)nx,(cpl_size)ny,CPL_TYPE_INT);
321 mdata = cpl_image_get_data_int(im);
322 for (i = 0; i < nx*ny; i++)
323 mdata[i] = (int)(inbpm[i]);
324
325 /* Initialise a few things */
326
327 m->master_mask = NULL;
328 m->mask_image = casu_fits_wrap(im,NULL,NULL,NULL);
329 m->masktype = MASK_BPM;
330 m->nx = nx;
331 m->ny = ny;
332 m->mdata = inbpm;
333 return(m);
334}
335
336/*---------------------------------------------------------------------------*/
355/*---------------------------------------------------------------------------*/
356
357extern void casu_mask_clear(casu_mask *m) {
358 if (m == NULL)
359 return;
360
361 freespace(m->mdata);
362 freefits(m->mask_image);
363 m->nx = 0;
364 m->ny = 0;
365}
366
367/*---------------------------------------------------------------------------*/
392/*---------------------------------------------------------------------------*/
393
394extern void casu_mask_force(casu_mask *m, int nx, int ny) {
395 if (m == NULL)
396 return;
397 freespace(m->mdata);
398 freefits(m->mask_image);
399 freeframe(m->master_mask);
400 m->masktype = MASK_NONE;
401 m->nx = nx;
402 m->ny = ny;
403}
404
405/*---------------------------------------------------------------------------*/
422/*---------------------------------------------------------------------------*/
423
424extern casu_fits *casu_mask_get_fits(casu_mask *m) {
425 return(m->mask_image);
426}
427
428/*---------------------------------------------------------------------------*/
445/*---------------------------------------------------------------------------*/
446
447extern const char *casu_mask_get_filename(casu_mask *m) {
448 if (m->master_mask != NULL) {
449 return(cpl_frame_get_filename(m->master_mask));
450 } else {
451 return(NULL);
452 }
453}
454
455
456/*---------------------------------------------------------------------------*/
473/*---------------------------------------------------------------------------*/
474
475extern int casu_mask_get_size_x(casu_mask *m) {
476 return(m->nx);
477}
478
479/*---------------------------------------------------------------------------*/
496/*---------------------------------------------------------------------------*/
497
498extern int casu_mask_get_size_y(casu_mask *m) {
499 return(m->ny);
500}
501
502/*---------------------------------------------------------------------------*/
519/*---------------------------------------------------------------------------*/
520
521extern int casu_mask_get_type(casu_mask *m) {
522 return(m->masktype);
523}
524
525/*---------------------------------------------------------------------------*/
542/*---------------------------------------------------------------------------*/
543
544extern unsigned char *casu_mask_get_data(casu_mask *m) {
545 unsigned char *bpm;
546 long npix;
547
548 /* Has this already been done? */
549
550 if (m->mdata != NULL)
551 return(m->mdata);
552
553 /* Get the bpm depending on what type of input you have */
554
555 switch (m->masktype) {
556 case MASK_NONE:
557 npix = (m->nx)*(m->ny);
558 bpm = cpl_calloc(npix,sizeof(*bpm));
559 break;
560 case MASK_BPM:
561 bpm = casu_mask_getbpm(casu_mask_get_fits(m));
562 break;
563 case MASK_OPM:
564 bpm = casu_mask_getbpm(casu_mask_get_fits(m));
565 break;
566 case MASK_CPM:
567 bpm = casu_mask_conf2bpm(casu_mask_get_fits(m));
568 break;
569 default:
570 npix = (m->nx)*(m->ny);
571 bpm = cpl_calloc(npix,sizeof(*bpm));
572 break;
573 }
574 m->mdata = bpm;
575 return(bpm);
576}
577
578/*---------------------------------------------------------------------------*/
599/*---------------------------------------------------------------------------*/
600
601static unsigned char *casu_mask_getbpm(casu_fits *bpmimage) {
602 long npts,i;
603 int *bpmdata;
604 cpl_image *b;
605 unsigned char *bpm;
606
607 /* Load the bad pixel map data */
608
609 b = casu_fits_get_image(bpmimage);
610 npts = cpl_image_get_size_x(b)*cpl_image_get_size_y(b);
611 bpmdata = cpl_image_get_data(b);
612
613 /* Get some space for the bad pixel mask and define it */
614
615 bpm = cpl_malloc(npts*sizeof(*bpm));
616 for (i = 0; i < npts; i++)
617 bpm[i] = (unsigned char)bpmdata[i];
618
619 /* Tidy and exit */
620
621 return(bpm);
622}
623
624/*---------------------------------------------------------------------------*/
644/*---------------------------------------------------------------------------*/
645
646static unsigned char *casu_mask_conf2bpm(casu_fits *cpmimage) {
647 long npts,i;
648 int *cpmdata;
649 cpl_image *c;
650 unsigned char *bpm;
651
652 /* Load the confidence map image and get its data */
653
654 c = casu_fits_get_image(cpmimage);
655 npts = cpl_image_get_size_x(c)*cpl_image_get_size_y(c);
656 cpmdata = cpl_image_get_data(c);
657
658 /* Get some space for the bad pixel mask and define it where the
659 confidence map is zero */
660
661 bpm = cpl_malloc(npts*sizeof(*bpm));
662 for (i = 0; i < npts; i++)
663 bpm[i] = (cpmdata[i] == 0);
664
665 /* Tidy and exit */
666
667 return(bpm);
668}
669
672/*
673
674$Log: casu_mask.c,v $
675Revision 1.4 2015/09/14 18:47:00 jim
676replaced a call to free with cpl_free
677
678Revision 1.3 2015/08/07 13:06:54 jim
679Fixed copyright to ESO
680
681Revision 1.2 2015/08/06 05:34:02 jim
682Fixes to get rid of compiler moans
683
684Revision 1.1.1.1 2015/06/12 10:44:32 jim
685Initial import
686
687Revision 1.7 2015/01/29 11:51:56 jim
688modified comments
689
690Revision 1.6 2015/01/09 12:13:15 jim
691*** empty log message ***
692
693Revision 1.5 2014/12/11 12:23:33 jim
694new version
695
696Revision 1.4 2014/04/09 09:09:51 jim
697Detabbed
698
699Revision 1.3 2014/03/26 15:50:37 jim
700Modified for floating poing confidence maps
701
702Revision 1.2 2013/11/21 09:38:14 jim
703detabbed
704
705Revision 1.1.1.1 2013-08-27 12:07:48 jim
706Imported
707
708
709*/
casu_fits * casu_fits_wrap(cpl_image *im, casu_fits *model, cpl_propertylist *phu, cpl_propertylist *ehu)
Definition: casu_fits.c:883
cpl_image * casu_fits_get_image(casu_fits *p)
Definition: casu_fits.c:436
void casu_fits_delete(casu_fits *p)
Definition: casu_fits.c:364
casu_fits * casu_fits_load(cpl_frame *frame, cpl_type type, int nexten)
Definition: casu_fits.c:80
void casu_mask_force(casu_mask *m, int nx, int ny)
Definition: casu_mask.c:394
void casu_mask_delete(casu_mask *m)
Definition: casu_mask.c:276
unsigned char * casu_mask_get_data(casu_mask *m)
Definition: casu_mask.c:544
int casu_mask_get_size_y(casu_mask *m)
Definition: casu_mask.c:498
int casu_mask_load(casu_mask *m, int nexten, int nx, int ny)
Definition: casu_mask.c:214
void casu_mask_clear(casu_mask *m)
Definition: casu_mask.c:357
casu_fits * casu_mask_get_fits(casu_mask *m)
Definition: casu_mask.c:424
int casu_mask_get_type(casu_mask *m)
Definition: casu_mask.c:521
casu_mask * casu_mask_define(cpl_frameset *framelist, cpl_size *labels, cpl_size nlab, const char *conftag, const char *bpmtag)
Definition: casu_mask.c:89
const char * casu_mask_get_filename(casu_mask *m)
Definition: casu_mask.c:447
casu_mask * casu_mask_wrap_bpm(unsigned char *inbpm, int nx, int ny)
Definition: casu_mask.c:309
casu_mask * casu_objmask_define(cpl_frame *frame)
Definition: casu_mask.c:163
int casu_mask_get_size_x(casu_mask *m)
Definition: casu_mask.c:475
cpl_frame * casu_frameset_subgroup_1(cpl_frameset *frameset, cpl_size *labels, cpl_size nlab, const char *tag)
Extract a frame of a given label from a frameset.
Definition: casu_utils.c:206