36 #include "visir_recipe.h"
42 #define RECIPE_STRING "visir_img_illu"
47 static cpl_image * visir_img_illu_fit(
const cpl_table *,
int,
int);
48 static cpl_error_code visir_img_illu_save(cpl_frameset *,
49 const cpl_parameterlist *,
53 VISIR_RECIPE_DEFINE(visir_img_illu,
54 VISIR_PARAM_NODPOS | VISIR_PARAM_AUTOBPM |
55 VISIR_PARAM_STRIPITE | VISIR_PARAM_STRIPMOR |
56 VISIR_PARAM_STRIPNON |
57 VISIR_PARAM_GLITCH | VISIR_PARAM_PURGE,
58 "Illumination recipe",
59 "This recipe compares the illumination of a bright star "
61 "positions on the detector. It produces a normalised "
63 "The files listed in the Set Of Frames (sof-file) "
65 "VISIR-illumination-file.fits " VISIR_IMG_ILLU_RAW
"\n"
66 MAN_VISIR_CALIB_BPM_IMG);
86 static int visir_img_illu(cpl_frameset * framelist,
87 const cpl_parameterlist * parlist)
89 irplib_framelist * allframes = NULL;
90 irplib_framelist * rawframes = NULL;
93 cpl_imagelist * nodded = NULL;
94 cpl_table * tab = NULL;
95 cpl_image * fitted = NULL;
105 skip_if(allframes == NULL);
107 skip_if (rawframes == NULL);
110 visir_property_regexp,
122 cpl_msg_info(cpl_func,
"Construct the nodded images");
124 NULL, CPL_FALSE, 0.0,0);
125 if (nodded == NULL) {
126 cpl_msg_error(cpl_func,
"Could not combine the input frames");
130 nfiles = cpl_imagelist_get_size(nodded);
131 nx = cpl_image_get_size_x(cpl_imagelist_get(nodded, 0));
132 ny = cpl_image_get_size_y(cpl_imagelist_get(nodded, 0));
136 tab = visir_table_new_xypos(nodded,
"FLUX");
137 skip_if (tab == NULL);
140 cpl_msg_info(cpl_func,
"Detect the objects and compute the flux");
143 cpl_msg_info(cpl_func,
"Fit a 2d polynomial on the flux");
144 if ((fitted = visir_img_illu_fit(tab, nx, ny)) == NULL) {
145 cpl_msg_error(cpl_func,
"Could not compute the fit: '%s' in %s",
146 cpl_error_get_message(), cpl_error_get_where());
151 cpl_msg_info(cpl_func,
"Saving products");
152 skip_if (visir_img_illu_save(framelist, parlist, tab, fitted));
158 cpl_imagelist_delete(nodded);
159 cpl_image_delete(fitted);
160 cpl_table_delete(tab);
162 return cpl_error_get_code();
174 static cpl_image * visir_img_illu_fit(
175 const cpl_table * tab,
179 const int nrow = cpl_table_get_nrow(tab);
180 cpl_polynomial * pol;
181 cpl_bivector * surface;
187 double xpos, ypos, flux;
194 cpl_ensure(!cpl_error_get_code(), cpl_error_get_code(), NULL);
198 for (i=0 ; i < nrow ; i++)
199 if (cpl_table_get_double(tab,
"X_POS", i, NULL) > 0 &&
200 cpl_table_get_double(tab,
"Y_POS", i, NULL) > 0)
203 assert(!cpl_error_get_code());
206 cpl_ensure(npoints >= 6, CPL_ERROR_DATA_NOT_FOUND, NULL);
209 surface = cpl_bivector_new(npoints);
210 x_pos = cpl_bivector_get_x(surface);
211 y_pos = cpl_bivector_get_y(surface);
212 values = cpl_vector_new(npoints);
214 for (i=0 ; i < nrow ; i++) {
215 xpos = cpl_table_get_double(tab,
"X_POS", i, NULL);
216 if (xpos <= 0)
continue;
217 ypos = cpl_table_get_double(tab,
"Y_POS", i, NULL);
218 if (ypos <= 0)
continue;
220 flux = cpl_table_get_double(tab,
"FLUX", i, NULL);
223 cpl_vector_set(x_pos, npoints, xpos);
224 cpl_vector_set(y_pos, npoints, ypos);
225 cpl_vector_set(values, npoints, flux);
230 norm = cpl_vector_get(values, npoints/2 );
231 cpl_vector_divide_scalar(values, norm);
233 if (cpl_error_get_code()) {
234 cpl_bivector_delete(surface);
235 cpl_vector_delete(values);
240 pol = cpl_polynomial_fit_2d_create(surface, values, 2, &mse);
241 cpl_msg_info(cpl_func,
"Mean Squared Error(%d): %g", npoints, mse);
243 cpl_bivector_delete(surface);
244 cpl_vector_delete(values);
246 if (pol == NULL)
return NULL;
249 fitted = cpl_image_new(nx, ny, CPL_TYPE_DOUBLE);
250 if (cpl_image_fill_polynomial(fitted, pol, 1.0, 1.0, 1.0, 1.0)) {
251 cpl_image_delete(fitted);
252 cpl_polynomial_delete(pol);
255 cpl_polynomial_delete(pol);
270 static cpl_error_code visir_img_illu_save(cpl_frameset * set,
271 const cpl_parameterlist * parlist,
272 const cpl_table * tab,
273 const cpl_image * fitted)
281 VISIR_IMG_ILLU_TAB_PROCATG, NULL, NULL,
283 RECIPE_STRING
"_tab" CPL_DFS_FITS));
287 RECIPE_STRING, VISIR_IMG_ILLU_FITTED_PROCATG,
288 NULL, NULL, visir_pipe_id,
289 RECIPE_STRING CPL_DFS_FITS));
293 return cpl_error_get_code();
cpl_error_code visir_dfs_check_framelist_tag(const irplib_framelist *self)
Check the tags in a frameset (group raw only)
cpl_error_code irplib_dfs_save_table(cpl_frameset *allframes, const cpl_parameterlist *parlist, const cpl_frameset *usedframes, const cpl_table *table, const cpl_propertylist *tablelist, const char *recipe, const char *procat, const cpl_propertylist *applist, const char *remregexp, const char *pipe_id, const char *filename)
Save a table as a DFS-compliant pipeline product.
cpl_error_code irplib_dfs_save_image(cpl_frameset *allframes, const cpl_parameterlist *parlist, const cpl_frameset *usedframes, const cpl_image *image, cpl_type_bpp bpp, const char *recipe, const char *procat, const cpl_propertylist *applist, const char *remregexp, const char *pipe_id, const char *filename)
Save an image as a DFS-compliant pipeline product.
cpl_error_code irplib_framelist_load_propertylist_all(irplib_framelist *self, int ind, const char *regexp, cpl_boolean invert)
Load the propertylists of all frames in the framelist.
irplib_framelist * irplib_framelist_extract(const irplib_framelist *self, const char *tag)
Extract the frames with the given tag from a framelist.
int visir_dfs_set_groups(cpl_frameset *set)
Set the group as RAW or CALIB in a frameset.
void irplib_framelist_delete(irplib_framelist *self)
Deallocate an irplib_framelist with its frames and properties.
const char * irplib_frameset_find_file(const cpl_frameset *self, const char *tag)
Find the filename with the given tag in a frame set.
irplib_framelist * irplib_framelist_cast(const cpl_frameset *frameset)
Create an irplib_framelist from a cpl_framelist.