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"
60 static cpl_error_code visir_util_insert_pos_one(cpl_frameset *,
double *,
62 irplib_framelist *,
int,
63 const cpl_parameterlist *);
66 VISIR_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);
98 static 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;
117 skip_if(allframes == NULL);
119 skip_if (rawframes == NULL);
121 skip_if (tabframes == NULL);
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);
161 return cpl_error_get_code();
178 cpl_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)
186 const cpl_error_code code =
188 IRPLIB_PFITS_WCS_REGEXP
")$",
190 cpl_propertylist * updlist = cpl_propertylist_duplicate
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();
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
214 bug_if(cpl_frameset_insert(usedframes, cpl_frame_duplicate
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,
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();
const cpl_propertylist * irplib_framelist_get_propertylist_const(const irplib_framelist *self, int pos)
Get the propertylist of the specified frame in the framelist.
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.
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.
const cpl_frame * irplib_framelist_get_const(const irplib_framelist *self, int pos)
Get the specified frame from the framelist.
void irplib_framelist_delete(irplib_framelist *self)
Deallocate an irplib_framelist with its frames and properties.
cpl_error_code irplib_framelist_load_propertylist(irplib_framelist *self, int pos, int ind, const char *regexp, cpl_boolean invert)
Load the propertylist of the specified frame in the framelist.
irplib_framelist * irplib_framelist_cast(const cpl_frameset *frameset)
Create an irplib_framelist from a cpl_framelist.
int irplib_framelist_get_size(const irplib_framelist *self)
Get the size of a framelist.