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 "visir_recipe.h"
00037
00038
00039
00040
00041
00042 #define RECIPE_STRING "visir_img_illu"
00043
00044
00045
00046
00047 static cpl_image * visir_img_illu_fit(const cpl_table *, int, int);
00048 static cpl_error_code visir_img_illu_save(cpl_frameset *,
00049 const cpl_parameterlist *,
00050 const cpl_table *,
00051 const cpl_image *);
00052
00053 VISIR_RECIPE_DEFINE(visir_img_illu,
00054 VISIR_PARAM_NODPOS | VISIR_PARAM_AUTOBPM |
00055 VISIR_PARAM_STRIPITE | VISIR_PARAM_STRIPMOR |
00056 VISIR_PARAM_STRIPNON |
00057 VISIR_PARAM_GLITCH | VISIR_PARAM_PURGE,
00058 "Illumination recipe",
00059 "This recipe compares the illumination of a bright star "
00060 "at different\n"
00061 "positions on the detector. It produces a normalised "
00062 "illumination map.\n"
00063 "The files listed in the Set Of Frames (sof-file) "
00064 "must be tagged:\n"
00065 "VISIR-illumination-file.fits " VISIR_IMG_ILLU_RAW "\n"
00066 MAN_VISIR_CALIB_BPM_IMG);
00067
00068
00072
00073
00074
00075
00076
00077
00078
00085
00086 static int visir_img_illu(cpl_frameset * framelist,
00087 const cpl_parameterlist * parlist)
00088 {
00089 irplib_framelist * allframes = NULL;
00090 irplib_framelist * rawframes = NULL;
00091 const char * badpix;
00092 const char * flat;
00093 cpl_imagelist * nodded = NULL;
00094 cpl_table * tab = NULL;
00095 cpl_image * fitted = NULL;
00096 int nx, ny;
00097 int nfiles;
00098
00099
00100
00101 skip_if (visir_dfs_set_groups(framelist));
00102
00103
00104 allframes = irplib_framelist_cast(framelist);
00105 skip_if(allframes == NULL);
00106 rawframes = irplib_framelist_extract(allframes, VISIR_IMG_ILLU_RAW);
00107 skip_if (rawframes == NULL);
00108
00109 skip_if(irplib_framelist_load_propertylist_all(rawframes, 0,
00110 visir_property_regexp,
00111 CPL_FALSE));
00112
00113 skip_if(visir_dfs_check_framelist_tag(rawframes));
00114
00115
00116 badpix = irplib_frameset_find_file(framelist, VISIR_CALIB_BPM);
00117
00118
00119 flat = irplib_frameset_find_file(framelist, VISIR_CALIB_FLAT);
00120
00121
00122 cpl_msg_info(cpl_func, "Construct the nodded images");
00123 nodded = visir_inputs_combine(RECIPE_STRING, parlist, rawframes, badpix, flat,
00124 NULL, CPL_FALSE, 0.0,0);
00125 if (nodded == NULL) {
00126 cpl_msg_error(cpl_func, "Could not combine the input frames");
00127 skip_if(1);
00128 }
00129
00130 nfiles = cpl_imagelist_get_size(nodded);
00131 nx = cpl_image_get_size_x(cpl_imagelist_get(nodded, 0));
00132 ny = cpl_image_get_size_y(cpl_imagelist_get(nodded, 0));
00133 skip_if (0);
00134
00135
00136 tab = visir_table_new_xypos(nodded, "FLUX");
00137 skip_if (tab == NULL);
00138
00139
00140 cpl_msg_info(cpl_func, "Detect the objects and compute the flux");
00141
00142
00143 cpl_msg_info(cpl_func, "Fit a 2d polynomial on the flux");
00144 if ((fitted = visir_img_illu_fit(tab, nx, ny)) == NULL) {
00145 cpl_msg_error(cpl_func, "Could not compute the fit: '%s' in %s",
00146 cpl_error_get_message(), cpl_error_get_where());
00147 skip_if(1);
00148 }
00149
00150
00151 cpl_msg_info(cpl_func, "Saving products");
00152 skip_if (visir_img_illu_save(framelist, parlist, tab, fitted));
00153
00154 end_skip;
00155
00156 irplib_framelist_delete(allframes);
00157 irplib_framelist_delete(rawframes);
00158 cpl_imagelist_delete(nodded);
00159 cpl_image_delete(fitted);
00160 cpl_table_delete(tab);
00161
00162 return cpl_error_get_code();
00163 }
00164
00165
00173
00174 static cpl_image * visir_img_illu_fit(
00175 const cpl_table * tab,
00176 int nx,
00177 int ny)
00178 {
00179 const int nrow = cpl_table_get_nrow(tab);
00180 cpl_polynomial * pol;
00181 cpl_bivector * surface;
00182 cpl_vector * x_pos;
00183 cpl_vector * y_pos;
00184 cpl_vector * values;
00185 double norm;
00186 cpl_image * fitted;
00187 double xpos, ypos, flux;
00188 double mse = -1;
00189 int npoints = 0;
00190 int i;
00191
00192
00193
00194 cpl_ensure(!cpl_error_get_code(), cpl_error_get_code(), NULL);
00195
00196
00197
00198 for (i=0 ; i < nrow ; i++)
00199 if (cpl_table_get_double(tab, "X_POS", i, NULL) > 0 &&
00200 cpl_table_get_double(tab, "Y_POS", i, NULL) > 0)
00201 npoints++;
00202
00203 assert(!cpl_error_get_code());
00204
00205
00206 cpl_ensure(npoints >= 6, CPL_ERROR_DATA_NOT_FOUND, NULL);
00207
00208
00209 surface = cpl_bivector_new(npoints);
00210 x_pos = cpl_bivector_get_x(surface);
00211 y_pos = cpl_bivector_get_y(surface);
00212 values = cpl_vector_new(npoints);
00213 npoints = 0;
00214 for (i=0 ; i < nrow ; i++) {
00215 xpos = cpl_table_get_double(tab, "X_POS", i, NULL);
00216 if (xpos <= 0) continue;
00217 ypos = cpl_table_get_double(tab, "Y_POS", i, NULL);
00218 if (ypos <= 0) continue;
00219
00220 flux = cpl_table_get_double(tab, "FLUX", i, NULL);
00221
00222
00223 cpl_vector_set(x_pos, npoints, xpos);
00224 cpl_vector_set(y_pos, npoints, ypos);
00225 cpl_vector_set(values, npoints, flux);
00226 npoints++;
00227 }
00228
00229
00230 norm = cpl_vector_get(values, npoints/2 );
00231 cpl_vector_divide_scalar(values, norm);
00232
00233 if (cpl_error_get_code()) {
00234 cpl_bivector_delete(surface);
00235 cpl_vector_delete(values);
00236 return NULL;
00237 }
00238
00239
00240 pol = cpl_polynomial_fit_2d_create(surface, values, 2, &mse);
00241 cpl_msg_info(cpl_func, "Mean Squared Error(%d): %g", npoints, mse);
00242
00243 cpl_bivector_delete(surface);
00244 cpl_vector_delete(values);
00245
00246 if (pol == NULL) return NULL;
00247
00248
00249 fitted = cpl_image_new(nx, ny, CPL_TYPE_DOUBLE);
00250 if (cpl_image_fill_polynomial(fitted, pol, 1.0, 1.0, 1.0, 1.0)) {
00251 cpl_image_delete(fitted);
00252 cpl_polynomial_delete(pol);
00253 return NULL;
00254 }
00255 cpl_polynomial_delete(pol);
00256
00257 return fitted;
00258 }
00259
00260
00269
00270 static cpl_error_code visir_img_illu_save(cpl_frameset * set,
00271 const cpl_parameterlist * parlist,
00272 const cpl_table * tab,
00273 const cpl_image * fitted)
00274 {
00275
00276
00277 bug_if(0);
00278
00279
00280 skip_if (irplib_dfs_save_table(set, parlist, set, tab, NULL, RECIPE_STRING,
00281 VISIR_IMG_ILLU_TAB_PROCATG, NULL, NULL,
00282 visir_pipe_id,
00283 RECIPE_STRING "_tab" CPL_DFS_FITS));
00284
00285 if (fitted)
00286 skip_if(irplib_dfs_save_image(set, parlist, set, fitted, CPL_BPP_IEEE_FLOAT,
00287 RECIPE_STRING, VISIR_IMG_ILLU_FITTED_PROCATG,
00288 NULL, NULL, visir_pipe_id,
00289 RECIPE_STRING CPL_DFS_FITS));
00290
00291 end_skip;
00292
00293 return cpl_error_get_code();
00294 }