OMEGA Pipeline Reference Manual  1.0.5
omega_cosmic.c
1 /* $Id: omega_cosmic.c,v 1.4 2012-01-31 13:38:50 agabasch Exp $
2  *
3  * This file is part of the OMEGA Pipeline
4  * Copyright (C) 2002,2003 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: agabasch $
23  * $Date: 2012-01-31 13:38:50 $
24  * $Revision: 1.4 $
25  * $Name: not supported by cvs2svn $
26  */
27 
28 #ifdef HAVE_CONFIG_H
29 #include <config.h>
30 #endif
31 
32 /*-----------------------------------------------------------------------------
33  Includes
34  -----------------------------------------------------------------------------*/
35 
36 #include <string.h>
37 #include <stdlib.h>
38 
39 #include "omega_background.h"
40 #include "omega_cosmic.h"
41 #include "omega_dfs.h"
42 #include "omega_utils.h"
43 
59 /*----------------------------------------------------------------------------*/
63 static cpl_mask *detcosmic(cpl_image *nscience, cpl_parameterlist *spars);
64 
65 /*----------------------------------------------------------------------------*/
74 /*----------------------------------------------------------------------------*/
75 static cpl_mask *detcosmic(cpl_image *nscience, cpl_parameterlist *spars)
76 {
77 
78  int count = 0;
79  int i = 0;
80  int npars = 0;
81 
82  double thre = 0.0;
83 
84  char *cmd = NULL;
85  const char *cosmiccat = "omega_cosmic.cat";
86  const char *cosmicname = "omega_cosmic.fits";
87  const char *path = NULL;
88  const char *sex_conf = NULL;
89  const char *sex_conv = NULL;
90  const char *sex_nnw = NULL;
91  const char *sex_par = NULL;
92  const char *text = NULL;
93  const char *tmpname23 = "temp0023.fits";
94 
95  cpl_image *img;
96  cpl_mask *cosmic_map;
97  cpl_parameter *par;
98  cpl_propertylist *plist;
99 
100 
101  if(nscience == NULL)
102  return NULL;
103 
104 
105  /* Retrieve Sextractor configuration parameters */
106  par = cpl_parameterlist_get_first(spars);
107  npars = cpl_parameterlist_get_size(spars);
108 
109  for(i=0; i<npars; i++) {
110  text = cpl_parameter_get_alias(par, CPL_PARAMETER_MODE_CLI);
111  if(strcmp("bin-path", text) == 0) {
112  path = cpl_parameter_get_string(par) ;
113  }
114  else if(strcmp("sex-cosmic", text) == 0) {
115  sex_conf = cpl_parameter_get_string(par);
116  }
117  else if(strcmp("sex-cosfilt", text) == 0) {
118  sex_conv = cpl_parameter_get_string(par);
119  }
120  else if(strcmp("sex-cosmic-param", text) == 0) {
121  sex_par = cpl_parameter_get_string(par);
122  }
123  else if(strcmp("sex-nnw", text) == 0) {
124  sex_nnw = cpl_parameter_get_string(par);
125  }
126  else if(strcmp("sex-cosmic-det", text) == 0) {
127  thre = cpl_parameter_get_double(par);
128  }
129  par = cpl_parameterlist_get_next(spars);
130 
131  }
132 
133 
134  /*Save temporary reduced science image to disk with no extension header*/
135  cpl_image_save(nscience, tmpname23,BITPIX, NULL, CPL_IO_CREATE);
136 
137 
138  /*
139  * Create command line for Sextractor
140  */
141 
142  cmd = cpl_sprintf("%s/sex %s -c %s -PARAMETERS_NAME %s -FILTER_NAME %s -STARNNW_NAME %s "
143  "-FILTER Y "
144  "-DETECT_THRESH %g "
145  "-CATALOG_NAME %s "
146  "-CATALOG_TYPE FITS_1.0 "
147  "-CHECKIMAGE_NAME %s 2>/dev/null",
148  path,
149  tmpname23,
150  sex_conf,
151  sex_par,
152  sex_conv,
153  sex_nnw,
154  thre,
155  cosmiccat,
156  cosmicname);
157 
158 
159  if (system(cmd) != 0) {
160  cpl_free(cmd);
161  cpl_msg_debug(cpl_func,"Failed to run Sextractor");
162  return NULL;
163  }
164  cpl_free(cmd);
165 
166  img = cpl_image_load(cosmicname, CPL_TYPE_INT, 0, 0);
167  if(img == NULL){
168  cpl_msg_debug(cpl_func,"Cannot load cosmic rays image");
169  return NULL;
170  }
171 
172  cosmic_map = cpl_mask_threshold_image_create(img, -0.5, 0.5);
173 
174  /* Count the number of CPL_BINARY_1 which in this image
175  * have pixels with high values (> 0.5)*/
176  cpl_mask_not(cosmic_map);
177  count = cpl_mask_count(cosmic_map);
178  /* cpl_msg_info("","Detected %d pixels with cosmic rays",count);*/
179 
180  /*Invert back*/
181 // cpl_mask_not(cosmic_map);
182 
183  plist = cpl_propertylist_load(cosmiccat, 1);
184  if(plist == NULL) {
185  cpl_msg_debug(cpl_func,"Cannot load header of %s",cosmiccat);
186  freeimage(img);
187  freemask(cosmic_map);
188  return NULL;
189  }
190 
191  /*The length of the catalog tells how many cosmic rays were detected*/
192  count = cpl_propertylist_get_int(plist,"NAXIS2");
193  cpl_msg_debug(cpl_func,"Detected %d cosmic ray events",count);
194 
195  freeimage(img);
196  freeplist(plist);
197 
198  return cosmic_map;
199 
200 }
201 
213 cpl_mask *omega_cosmic_rays(const cpl_image *sci, const cpl_image *flat, const cpl_image *illum,
214  const cpl_image *bpm, cpl_parameterlist *pars)
215 {
216 
217  cpl_mask *cosmic_map = NULL;
218  cpl_mask *bpm_map = NULL;
219  cpl_image *weight, *fbpm, *image;
220 
221  if((sci == NULL) || (flat == NULL) || (bpm == NULL) || (pars == NULL))
222  return NULL;
223 
224  /* Change pixel values in BPM image:
225  * 0->1 and 1-> 0 in order to use it more easily */
226  bpm_map=cpl_mask_threshold_image_create(bpm,-0.5,0.5);
227  fbpm=cpl_image_new_from_mask(bpm_map);
228  freemask(bpm_map);
229 
230  weight = cpl_image_multiply_create(flat,fbpm);
231  freeimage(fbpm);
232 
233  if(illum != NULL)
234  cpl_image_divide(weight,illum);
235 
236  cpl_image_power(weight, 0.5);
237 
238  image = cpl_image_multiply_create(sci, weight);
239 
240  freeimage(weight);
241 
242  cosmic_map = detcosmic(image, pars);
243 
244  freeimage(image);
245  if(cosmic_map == NULL){
246  cpl_msg_debug(cpl_func,"NULL cosmic rays map");
247  return NULL;
248  }
249 
250  return cosmic_map;
251 }
252