OMEGA Pipeline Reference Manual  1.0.5
omega_background.c
1 /* $Id: omega_background.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_dfs.h"
41 #include "omega_utils.h"
42 
58 /*----------------------------------------------------------------------------*/
70 int omega_create_background(cpl_parameterlist *parlist, const char *in_name, const char *out_name)
71 {
72 
73  int i = 0;
74  int npars = 0;
75  double dthre = 0.0;
76  int backsize=64;
77  char *cmd = NULL;
78  const char *path = NULL;
79  const char *sex_conf = NULL;
80  const char *sex_conv = NULL;
81  const char *sex_nnw = NULL;
82  const char *sex_par = NULL;
83  const char *text = NULL;
84 
85  cpl_parameter *par;
86 
87 
88  /*
89  * Start the recipe
90  */
91 
92  if (!parlist) {
93  cpl_msg_error (cpl_func,"Sextractor parameters list not found");
94  return -1;
95  }
96  else {
97  npars = cpl_parameterlist_get_size(parlist);
98  if(npars < 1) {
99  cpl_msg_error (cpl_func,"Sextractor parameters list is NULL");
100  return -1;
101  }
102  }
103 
104 
105  /*
106  * Retrieve Sextractor configuration parameters
107  */
108 
109  par = cpl_parameterlist_get_first(parlist);
110 
111  for(i=0; i<npars; i++) {
112  text = cpl_parameter_get_alias(par, CPL_PARAMETER_MODE_CLI);
113  if(strcmp("bin-path", text) == 0) {
114  path = cpl_parameter_get_string(par) ;
115  }
116  else if(strcmp("sex-config", text) == 0) {
117  sex_conf = cpl_parameter_get_string(par) ;
118  }
119  else if(strcmp("sex-conv", text) == 0) {
120  sex_conv = cpl_parameter_get_string(par) ;
121  }
122  else if(strcmp("sex-param", text) == 0) {
123  sex_par = cpl_parameter_get_string(par) ;
124  }
125  else if(strcmp("sex-nnw", text) == 0) {
126  sex_nnw = cpl_parameter_get_string(par) ;
127  }
128  else if(strcmp("backthre", text) == 0) {
129  dthre = cpl_parameter_get_double(par) ;
130  }
131  else if(strcmp("backsize", text) == 0){
132  backsize = cpl_parameter_get_int(par);
133  }
134  par = cpl_parameterlist_get_next(parlist);
135  }
136 
137  /*
138  * Create command line for Sextractor
139  */
140 
141  cmd = cpl_sprintf("%s/sex %s -c %s -PARAMETERS_NAME %s -FILTER_NAME %s -STARNNW_NAME %s "
142  "-FILTER N "
143  "-DETECT_THRESH %g "
144  "-BACK_SIZE %d "
145  "-CHECKIMAGE_TYPE BACKGROUND "
146  "-CHECKIMAGE_NAME %s",
147  path,
148  in_name,
149  sex_conf,
150  sex_par,
151  sex_conv,
152  sex_nnw,
153  dthre,
154  backsize,
155  out_name);
156 
157  if (system(cmd) != 0) {
158  cpl_free(cmd);
159  cpl_msg_error(cpl_func,"Failed to run Sextractor for background creation");
160  return -1;
161  }
162 
163  cpl_free(cmd);
164 
165  return 0;
166 }
167 
168 
178 cpl_image *omega_background(const cpl_image *sci, const cpl_parameterlist *parlist)
179 {
180 
181  int i = 0;
182  int npars = 0;
183  double dthre = 0.0;
184  char *cmd = NULL;
185  const char *sciname = "omega_sci4back.fits";
186  const char *weightname = "omega_sci4back_weight.fits";
187  const char *out_name = "omega_back.fits";
188  const char *path = NULL;
189  const char *sex_conf = NULL;
190  const char *sex_conv = NULL;
191  const char *sex_nnw = NULL;
192  const char *sex_par = NULL;
193  const char *text = NULL;
194 
195  cpl_parameter *par;
196  cpl_image *back;
197 
198 
199  if((sci == NULL) || (parlist == NULL))
200  return NULL;
201 
202 
203  /*
204  * Retrieve Sextractor configuration parameters
205  */
206 
207  npars = cpl_parameterlist_get_size(parlist);
208  par = cpl_parameterlist_get_first(parlist);
209 
210  for(i=0; i<npars; i++) {
211  text = cpl_parameter_get_alias(par, CPL_PARAMETER_MODE_CLI);
212  if(strcmp("bin-path", text) == 0) {
213  path = cpl_parameter_get_string(par) ;
214  }
215  else if(strcmp("sex-config", text) == 0) {
216  sex_conf = cpl_parameter_get_string(par) ;
217  }
218  else if(strcmp("sex-conv", text) == 0) {
219  sex_conv = cpl_parameter_get_string(par) ;
220  }
221  else if(strcmp("sex-param", text) == 0) {
222  sex_par = cpl_parameter_get_string(par) ;
223  }
224  else if(strcmp("sex-nnw", text) == 0) {
225  sex_nnw = cpl_parameter_get_string(par) ;
226  }
227  else if(strcmp("backthre", text) == 0) {
228  dthre = cpl_parameter_get_double(par) ;
229  }
230  par = cpl_parameterlist_get_next(parlist);
231  }
232 
233  /* Save image in disk to use in Sextractor */
234  cpl_image_save(sci, sciname, BITPIX, NULL, CPL_IO_CREATE);
235 
236  /*Use the bad pixel mask as a zero order weiht map for sextractor*/
237  cpl_mask * sci_bpm=cpl_mask_duplicate(cpl_image_get_bpm_const(sci));
238  cpl_mask_not(sci_bpm);
239  cpl_mask_save(sci_bpm, weightname, NULL, CPL_IO_CREATE);
240  cpl_mask_delete(sci_bpm);
241  /*
242  * Create command line for Sextractor
243  */
244  cmd = cpl_sprintf("%s/sex %s "
245  " -WEIGHT_TYPE MAP_WEIGHT -WEIGHT_IMAGE %s "
246  "-c %s -PARAMETERS_NAME %s -FILTER_NAME %s -STARNNW_NAME %s "
247  "-FILTER N "
248  "-DETECT_THRESH %g "
249  "-CHECKIMAGE_TYPE BACKGROUND "
250  "-CHECKIMAGE_NAME %s 2>/dev/null",
251  path,
252  sciname,
253  weightname,
254  sex_conf,
255  sex_par,
256  sex_conv,
257  sex_nnw,
258  dthre,
259  out_name);
260  cpl_msg_debug(cpl_func,"Sextractor call for Background subtraction:\n "
261  "%s",cmd);
262  if (system(cmd) != 0) {
263  cpl_free(cmd);
264  cpl_msg_error(cpl_func,"Failed to run Sextractor for background detection");
265  return NULL;
266  }
267 
268  cpl_free(cmd);
269 
270  back = cpl_image_load(out_name,CPL_TYPE_FLOAT,0,0);
271 
272  return back;
273 }
274