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_cosmic.h"
00041 #include "omega_dfs.h"
00042 #include "omega_utils.h"
00043
00059
00063 static cpl_mask *detcosmic(cpl_image *nscience, cpl_parameterlist *spars);
00064
00065
00074
00075 static cpl_mask *detcosmic(cpl_image *nscience, cpl_parameterlist *spars)
00076 {
00077
00078 int count = 0;
00079 int i = 0;
00080 int npars = 0;
00081
00082 double thre = 0.0;
00083
00084 char *cmd = NULL;
00085 const char *cosmiccat = "omega_cosmic.cat";
00086 const char *cosmicname = "omega_cosmic.fits";
00087 const char *path = NULL;
00088 const char *sex_conf = NULL;
00089 const char *sex_conv = NULL;
00090 const char *sex_nnw = NULL;
00091 const char *sex_par = NULL;
00092 const char *text = NULL;
00093 const char *tmpname23 = "temp0023.fits";
00094
00095 cpl_image *img;
00096 cpl_mask *cosmic_map;
00097 cpl_parameter *par;
00098 cpl_propertylist *plist;
00099
00100
00101 if(nscience == NULL)
00102 return NULL;
00103
00104
00105
00106 par = cpl_parameterlist_get_first(spars);
00107 npars = cpl_parameterlist_get_size(spars);
00108
00109 for(i=0; i<npars; i++) {
00110 text = cpl_parameter_get_alias(par, CPL_PARAMETER_MODE_CLI);
00111 if(strcmp("bin-path", text) == 0) {
00112 path = cpl_parameter_get_string(par) ;
00113 }
00114 else if(strcmp("sex-cosmic", text) == 0) {
00115 sex_conf = cpl_parameter_get_string(par);
00116 }
00117 else if(strcmp("sex-cosfilt", text) == 0) {
00118 sex_conv = cpl_parameter_get_string(par);
00119 }
00120 else if(strcmp("sex-cosmic-param", text) == 0) {
00121 sex_par = cpl_parameter_get_string(par);
00122 }
00123 else if(strcmp("sex-nnw", text) == 0) {
00124 sex_nnw = cpl_parameter_get_string(par);
00125 }
00126 else if(strcmp("sex-cosmic-det", text) == 0) {
00127 thre = cpl_parameter_get_double(par);
00128 }
00129 par = cpl_parameterlist_get_next(spars);
00130
00131 }
00132
00133
00134
00135 cpl_image_save(nscience, tmpname23,BITPIX, NULL, CPL_IO_CREATE);
00136
00137
00138
00139
00140
00141
00142 cmd = cpl_sprintf("%s/sex %s -c %s -PARAMETERS_NAME %s -FILTER_NAME %s -STARNNW_NAME %s "
00143 "-FILTER Y "
00144 "-DETECT_THRESH %g "
00145 "-CATALOG_NAME %s "
00146 "-CATALOG_TYPE FITS_1.0 "
00147 "-CHECKIMAGE_NAME %s 2>/dev/null",
00148 path,
00149 tmpname23,
00150 sex_conf,
00151 sex_par,
00152 sex_conv,
00153 sex_nnw,
00154 thre,
00155 cosmiccat,
00156 cosmicname);
00157
00158
00159 if (system(cmd) != 0) {
00160 cpl_free(cmd);
00161 cpl_msg_debug(cpl_func,"Failed to run Sextractor");
00162 return NULL;
00163 }
00164 cpl_free(cmd);
00165
00166 img = cpl_image_load(cosmicname, CPL_TYPE_INT, 0, 0);
00167 if(img == NULL){
00168 cpl_msg_debug(cpl_func,"Cannot load cosmic rays image");
00169 return NULL;
00170 }
00171
00172 cosmic_map = cpl_mask_threshold_image_create(img, -0.5, 0.5);
00173
00174
00175
00176 cpl_mask_not(cosmic_map);
00177 count = cpl_mask_count(cosmic_map);
00178
00179
00180
00181
00182
00183 plist = cpl_propertylist_load(cosmiccat, 1);
00184 if(plist == NULL) {
00185 cpl_msg_debug(cpl_func,"Cannot load header of %s",cosmiccat);
00186 freeimage(img);
00187 freemask(cosmic_map);
00188 return NULL;
00189 }
00190
00191
00192 count = cpl_propertylist_get_int(plist,"NAXIS2");
00193 cpl_msg_debug(cpl_func,"Detected %d cosmic ray events",count);
00194
00195 freeimage(img);
00196 freeplist(plist);
00197
00198 return cosmic_map;
00199
00200 }
00201
00213 cpl_mask *omega_cosmic_rays(const cpl_image *sci, const cpl_image *flat, const cpl_image *illum,
00214 const cpl_image *bpm, cpl_parameterlist *pars)
00215 {
00216
00217 cpl_mask *cosmic_map = NULL;
00218 cpl_mask *bpm_map = NULL;
00219 cpl_image *weight, *fbpm, *image;
00220
00221 if((sci == NULL) || (flat == NULL) || (bpm == NULL) || (pars == NULL))
00222 return NULL;
00223
00224
00225
00226 bpm_map=cpl_mask_threshold_image_create(bpm,-0.5,0.5);
00227 fbpm=cpl_image_new_from_mask(bpm_map);
00228 freemask(bpm_map);
00229
00230 weight = cpl_image_multiply_create(flat,fbpm);
00231 freeimage(fbpm);
00232
00233 if(illum != NULL)
00234 cpl_image_divide(weight,illum);
00235
00236 cpl_image_power(weight, 0.5);
00237
00238 image = cpl_image_multiply_create(sci, weight);
00239
00240 freeimage(weight);
00241
00242 cosmic_map = detcosmic(image, pars);
00243
00244 freeimage(image);
00245 if(cosmic_map == NULL){
00246 cpl_msg_debug(cpl_func,"NULL cosmic rays map");
00247 return NULL;
00248 }
00249
00250 return cosmic_map;
00251 }
00252