36#include "visir_recipe.h"
42#define RECIPE_STRING "visir_util_insert_pos"
44#define FLUX_COL "FLUX_AUTO"
45#define FLUXERR_COL "FLUXERR_AUTO"
46#define X_COL "XWIN_IMAGE"
47#define Y_COL "YWIN_IMAGE"
49#define VISIR_FLUX "FLXSCALE"
50#define VISIR_DRS_FLUX "ESO DRS FLXSCALE"
51#define VISIR_FLUX_COMM "The relative flux (to that of the 1st source)"
52#define VISIR_DRS_SNR "ESO DRS SNR"
53#define VISIR_DRS_SNR_COMM "The signal-to-noise ratio for the extracted source"
60static cpl_error_code visir_util_insert_pos_one(cpl_frameset *,
double *,
62 irplib_framelist *,
int,
63 const cpl_parameterlist *);
66VISIR_RECIPE_DEFINE(visir_util_insert_pos, 0,
67 "Conversion of a single object position from matching "
68 "pairs of sextractor table + object files",
69 "The files listed in the Set Of Frames (sof-file) "
71 "Sextractor-object-file.fits " VISIR_UTIL_INSERT_RAW
72 "Sextractor-table-file.fits " VISIR_UTIL_INSERT_TAB
73 "\nFor each input pair the produced object file will "
74 "have its WCS-ccordinates (CRPIX[12]) set to the position "
75 "from the brightest object in the corresponding table."
76 "\nThe product(s) will have a FITS card\n"
77 "'HIERARCH " CPL_DFS_PRO_CATG
"' with a value of:\n"
78 VISIR_UTIL_INSERT_PROCATG);
98static int visir_util_insert_pos(cpl_frameset * framelist,
99 const cpl_parameterlist * parlist)
102 cpl_errorstate cleanstate = cpl_errorstate_get();
104 cpl_error_code didfail = CPL_ERROR_NONE;
105 irplib_framelist * allframes = NULL;
106 irplib_framelist * rawframes = NULL;
107 irplib_framelist * tabframes = NULL;
116 allframes = irplib_framelist_cast(framelist);
117 skip_if(allframes == NULL);
118 rawframes = irplib_framelist_extract(allframes, VISIR_UTIL_INSERT_RAW);
119 skip_if (rawframes == NULL);
120 tabframes = irplib_framelist_extract(allframes, VISIR_UTIL_INSERT_TAB);
121 skip_if (tabframes == NULL);
123 n = irplib_framelist_get_size(rawframes);
124 skip_if(n != irplib_framelist_get_size(tabframes));
126#pragma omp parallel for private(i)
128 for (i = 0; i < n; i++) {
138 if (visir_util_insert_pos_one(framelist, &flux1, rawframes,
139 tabframes, i, parlist)) {
140 const cpl_error_code errori = cpl_error_set_where(cpl_func);
144 cpl_errorstate_dump(cleanstate, CPL_FALSE, NULL);
145 cpl_errorstate_set(cleanstate);
146#pragma omp critical(visir_util_insert_pos)
153 error_if(didfail, didfail,
"Failed to process %d frame(s)", n);
157 irplib_framelist_delete(allframes);
158 irplib_framelist_delete(rawframes);
159 irplib_framelist_delete(tabframes);
161 return cpl_error_get_code();
178cpl_error_code visir_util_insert_pos_one(cpl_frameset * framelist,
180 irplib_framelist * rawframes,
181 irplib_framelist * tabframes,
int i,
182 const cpl_parameterlist * parlist)
185 const int n = irplib_framelist_get_size(rawframes);
186 const cpl_error_code code =
187 irplib_framelist_load_propertylist(rawframes, i, 0,
"^("
188 IRPLIB_PFITS_WCS_REGEXP
")$",
190 cpl_propertylist * updlist = cpl_propertylist_duplicate
191 (irplib_framelist_get_propertylist_const(rawframes, i));
192 const cpl_frame * frame;
193 cpl_frameset * products = cpl_frameset_new();
194 cpl_frameset * usedframes = cpl_frameset_new();
195 cpl_propertylist * plsort = cpl_propertylist_new();
196 const char * imgname = cpl_frame_get_filename(irplib_framelist_get_const
198 const char * tabname = cpl_frame_get_filename(irplib_framelist_get_const
200 cpl_table * table = cpl_table_load(tabname, 1, 0);
201 cpl_image * image = cpl_image_load(imgname, CPL_TYPE_UNSPECIFIED, 0, 0);
202 char * proname = NULL;
203 const int nobjs = cpl_table_get_nrow(table);
207 bug_if(pflux1 == NULL);
209 skip_if_lt(nobjs, 1,
"row(s) in table of extracted sources with "
210 "column " FLUX_COL
": %s", tabname);
212 bug_if(cpl_frameset_insert(usedframes, cpl_frame_duplicate
213 (irplib_framelist_get_const(rawframes, i))));
214 bug_if(cpl_frameset_insert(usedframes, cpl_frame_duplicate
215 (irplib_framelist_get_const(tabframes, i))));
217 for (iobj = 0; iobj < nobjs; iobj++) {
219 double xpos, ypos, relflux, flux, fluxerr;
222 proname = cpl_sprintf(RECIPE_STRING
"_%d_%d" CPL_DFS_FITS, 1+i, 1+iobj);
224 skip_if(cpl_table_get_column_maxpos(table, FLUX_COL, &maxpos));
226 xpos = cpl_table_get(table, X_COL, maxpos, NULL);
227 ypos = cpl_table_get(table, Y_COL, maxpos, NULL);
228 flux = cpl_table_get(table, FLUX_COL, maxpos, NULL);
231 bug_if(cpl_table_set_invalid(table, FLUX_COL, maxpos));
233 if (*pflux1 != 0.0) {
234 relflux = flux / *pflux1;
240 cpl_msg_info(cpl_func,
"Converting frame %d/%d: (%g,%g) using row %d",
241 1+i, n, xpos, ypos, maxpos);
243 bug_if(cpl_propertylist_append_double(updlist,
"CRPIX1", xpos));
244 bug_if(cpl_propertylist_append_double(updlist,
"CRPIX2", ypos));
245 bug_if(cpl_propertylist_append_double(updlist, VISIR_DRS_FLUX,relflux));
246 bug_if(cpl_propertylist_append_double(updlist, VISIR_FLUX, relflux));
250 bug_if(cpl_propertylist_set_comment(updlist,
"CRPIX1",
"Windowed "
251 "position estimate along x"));
252 bug_if(cpl_propertylist_set_comment(updlist,
"CRPIX2",
"Windowed "
253 "position estimate along y"));
254 bug_if(cpl_propertylist_set_comment(updlist, VISIR_DRS_FLUX,
256 bug_if(cpl_propertylist_set_comment(updlist, VISIR_FLUX,
260 skip_if(irplib_dfs_save_image(products, parlist, usedframes, image,
261 CPL_BPP_IEEE_FLOAT, RECIPE_STRING,
262 VISIR_UTIL_INSERT_PROCATG, updlist,
263 NULL, visir_pipe_id, proname));
268 skip_if_lt(cpl_table_count_invalid(table, FLUX_COL), nobjs,
269 "processed objects from table %s", tabname);
271 for (frame = cpl_frameset_get_first_const(products);
273 frame = cpl_frameset_get_next_const(products)) {
274 cpl_frame * copy = cpl_frame_duplicate(frame);
275 cpl_error_code error;
278#pragma omp critical(visir_util_insert_pos_one)
280 error = cpl_frameset_insert(framelist, copy);
284 bug_if(frame != NULL);
288 cpl_image_delete(image);
289 cpl_table_delete(table);
291 cpl_propertylist_delete(updlist);
292 cpl_propertylist_delete(plsort);
293 cpl_frameset_delete(usedframes);
294 cpl_frameset_delete(products);
296 return cpl_error_get_code();
int visir_dfs_set_groups(cpl_frameset *set)
Set the group as RAW or CALIB in a frameset.