00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 #ifdef HAVE_CONFIG_H
00029 #include <config.h>
00030 #endif
00031
00032
00033
00034
00035
00036 #include <string.h>
00037 #include <stdlib.h>
00038
00039 #include "omega_background.h"
00040 #include "omega_dfs.h"
00041 #include "omega_utils.h"
00042
00058
00070 int omega_create_background(cpl_parameterlist *parlist, const char *in_name, const char *out_name)
00071 {
00072
00073 int i = 0;
00074 int npars = 0;
00075 double dthre = 0.0;
00076 int backsize=64;
00077 char *cmd = NULL;
00078 const char *path = NULL;
00079 const char *sex_conf = NULL;
00080 const char *sex_conv = NULL;
00081 const char *sex_nnw = NULL;
00082 const char *sex_par = NULL;
00083 const char *text = NULL;
00084
00085 cpl_parameter *par;
00086
00087
00088
00089
00090
00091
00092 if (!parlist) {
00093 cpl_msg_error (cpl_func,"Sextractor parameters list not found");
00094 return -1;
00095 }
00096 else {
00097 npars = cpl_parameterlist_get_size(parlist);
00098 if(npars < 1) {
00099 cpl_msg_error (cpl_func,"Sextractor parameters list is NULL");
00100 return -1;
00101 }
00102 }
00103
00104
00105
00106
00107
00108
00109 par = cpl_parameterlist_get_first(parlist);
00110
00111 for(i=0; i<npars; i++) {
00112 text = cpl_parameter_get_alias(par, CPL_PARAMETER_MODE_CLI);
00113 if(strcmp("bin-path", text) == 0) {
00114 path = cpl_parameter_get_string(par) ;
00115 }
00116 else if(strcmp("sex-config", text) == 0) {
00117 sex_conf = cpl_parameter_get_string(par) ;
00118 }
00119 else if(strcmp("sex-conv", text) == 0) {
00120 sex_conv = cpl_parameter_get_string(par) ;
00121 }
00122 else if(strcmp("sex-param", text) == 0) {
00123 sex_par = cpl_parameter_get_string(par) ;
00124 }
00125 else if(strcmp("sex-nnw", text) == 0) {
00126 sex_nnw = cpl_parameter_get_string(par) ;
00127 }
00128 else if(strcmp("backthre", text) == 0) {
00129 dthre = cpl_parameter_get_double(par) ;
00130 }
00131 else if(strcmp("backsize", text) == 0){
00132 backsize = cpl_parameter_get_int(par);
00133 }
00134 par = cpl_parameterlist_get_next(parlist);
00135 }
00136
00137
00138
00139
00140
00141 cmd = cpl_sprintf("%s/sex %s -c %s -PARAMETERS_NAME %s -FILTER_NAME %s -STARNNW_NAME %s "
00142 "-FILTER N "
00143 "-DETECT_THRESH %g "
00144 "-BACK_SIZE %d "
00145 "-CHECKIMAGE_TYPE BACKGROUND "
00146 "-CHECKIMAGE_NAME %s",
00147 path,
00148 in_name,
00149 sex_conf,
00150 sex_par,
00151 sex_conv,
00152 sex_nnw,
00153 dthre,
00154 backsize,
00155 out_name);
00156
00157 if (system(cmd) != 0) {
00158 cpl_free(cmd);
00159 cpl_msg_error(cpl_func,"Failed to run Sextractor for background creation");
00160 return -1;
00161 }
00162
00163 cpl_free(cmd);
00164
00165 return 0;
00166 }
00167
00168
00178 cpl_image *omega_background(const cpl_image *sci, const cpl_parameterlist *parlist)
00179 {
00180
00181 int i = 0;
00182 int npars = 0;
00183 double dthre = 0.0;
00184 char *cmd = NULL;
00185 const char *sciname = "omega_sci4back.fits";
00186 const char *out_name = "omega_back.fits";
00187 const char *path = NULL;
00188 const char *sex_conf = NULL;
00189 const char *sex_conv = NULL;
00190 const char *sex_nnw = NULL;
00191 const char *sex_par = NULL;
00192 const char *text = NULL;
00193
00194 cpl_parameter *par;
00195 cpl_image *back;
00196
00197
00198 if((sci == NULL) || (parlist == NULL))
00199 return NULL;
00200
00201
00202
00203
00204
00205
00206 npars = cpl_parameterlist_get_size(parlist);
00207 par = cpl_parameterlist_get_first(parlist);
00208
00209 for(i=0; i<npars; i++) {
00210 text = cpl_parameter_get_alias(par, CPL_PARAMETER_MODE_CLI);
00211 if(strcmp("bin-path", text) == 0) {
00212 path = cpl_parameter_get_string(par) ;
00213 }
00214 else if(strcmp("sex-config", text) == 0) {
00215 sex_conf = cpl_parameter_get_string(par) ;
00216 }
00217 else if(strcmp("sex-conv", text) == 0) {
00218 sex_conv = cpl_parameter_get_string(par) ;
00219 }
00220 else if(strcmp("sex-param", text) == 0) {
00221 sex_par = cpl_parameter_get_string(par) ;
00222 }
00223 else if(strcmp("sex-nnw", text) == 0) {
00224 sex_nnw = cpl_parameter_get_string(par) ;
00225 }
00226 else if(strcmp("backthre", text) == 0) {
00227 dthre = cpl_parameter_get_double(par) ;
00228 }
00229 par = cpl_parameterlist_get_next(parlist);
00230 }
00231
00232
00233 cpl_image_save(sci, sciname, BITPIX, NULL, CPL_IO_CREATE);
00234
00235
00236
00237
00238 cmd = cpl_sprintf("%s/sex %s -c %s -PARAMETERS_NAME %s -FILTER_NAME %s -STARNNW_NAME %s "
00239 "-FILTER N "
00240 "-DETECT_THRESH %g "
00241 "-CHECKIMAGE_TYPE BACKGROUND "
00242 "-CHECKIMAGE_NAME %s 2>/dev/null",
00243 path,
00244 sciname,
00245 sex_conf,
00246 sex_par,
00247 sex_conv,
00248 sex_nnw,
00249 dthre,
00250 out_name);
00251
00252 if (system(cmd) != 0) {
00253 cpl_free(cmd);
00254 cpl_msg_error(cpl_func,"Failed to run Sextractor for background detection");
00255 return NULL;
00256 }
00257
00258 cpl_free(cmd);
00259
00260 back = cpl_image_load(out_name,CPL_TYPE_FLOAT,0,0);
00261
00262 return back;
00263 }
00264