VISIR Pipeline Reference Manual  4.1.0
visir_util_insert_pos.c
1 /* $Id: visir_util_insert_pos.c,v 1.1 2011-05-31 13:21:28 llundin Exp $
2  *
3  * This file is part of the VISIR Pipeline
4  * Copyright (C) 2002,2003 European Southern Observatory
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02111-1307 USA
19  */
20 
21 /*
22  * $Author: llundin $
23  * $Date: 2011-05-31 13:21:28 $
24  * $Revision: 1.1 $
25  * $Name: not supported by cvs2svn $
26  */
27 
28 #ifdef HAVE_CONFIG_H
29 #include <config.h>
30 #endif
31 
32 /*-----------------------------------------------------------------------------
33  Includes
34  -----------------------------------------------------------------------------*/
35 
36 #include "visir_recipe.h"
37 
38 /*-----------------------------------------------------------------------------
39  Defines
40  -----------------------------------------------------------------------------*/
41 
42 #define RECIPE_STRING "visir_util_insert_pos"
43 
44 #define FLUX_COL "FLUX_AUTO"
45 #define FLUXERR_COL "FLUXERR_AUTO"
46 #define X_COL "XWIN_IMAGE"
47 #define Y_COL "YWIN_IMAGE"
48 
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"
54 
55 
56 /*-----------------------------------------------------------------------------
57  Private Functions prototypes
58  -----------------------------------------------------------------------------*/
59 
60 static cpl_error_code visir_util_insert_pos_one(cpl_frameset *, double *,
61  irplib_framelist *,
62  irplib_framelist *, int,
63  const cpl_parameterlist *);
64 
65 
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) "
70  "must be tagged:\n"
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);
79 
80 /*----------------------------------------------------------------------------*/
84 /*----------------------------------------------------------------------------*/
85 
86 /*-----------------------------------------------------------------------------
87  Functions code
88  -----------------------------------------------------------------------------*/
89 
90 /*----------------------------------------------------------------------------*/
97 /*----------------------------------------------------------------------------*/
98 static int visir_util_insert_pos(cpl_frameset * framelist,
99  const cpl_parameterlist * parlist)
100 {
101 #ifdef _OPENMP
102  cpl_errorstate cleanstate = cpl_errorstate_get();
103 #endif
104  cpl_error_code didfail = CPL_ERROR_NONE;
105  irplib_framelist * allframes = NULL;
106  irplib_framelist * rawframes = NULL;
107  irplib_framelist * tabframes = NULL;
108  double flux1 = 0.0;
109  int i, n;
110 
111 
112  /* Identify the RAW and TAB frames in the input frameset */
113  skip_if (visir_dfs_set_groups(framelist));
114 
115  /* Objects observation */
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);
122 
123  n = irplib_framelist_get_size(rawframes);
124  skip_if(n != irplib_framelist_get_size(tabframes));
125 #ifdef _OPENMP
126 #pragma omp parallel for private(i)
127 #endif
128  for (i = 0; i < n; i++) {
129  if (!didfail) {
130 
131  /* The total number of iterations must be pre-determined for the
132  parallelism to work. In case of an error we can therefore not
133  break, so instead we skip immediately to the next iteration.
134  FIXME: This check on didfail does not guarantee that only one
135  iteration can cause an error to be dumped, but it is not
136  worse than checking on a thread-local state, e.g. errori. */
137 
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);
141 #ifdef _OPENMP
142  /* Cannot access these errors after the join,
143  so dump them now. :-(((((((((((((((((((( */
144  cpl_errorstate_dump(cleanstate, CPL_FALSE, NULL);
145  cpl_errorstate_set(cleanstate);
146 #pragma omp critical(visir_util_insert_pos)
147 #endif
148  didfail = errori;
149  }
150  }
151  }
152 
153  error_if(didfail, didfail, "Failed to process %d frame(s)", n);
154 
155  end_skip;
156 
157  irplib_framelist_delete(allframes);
158  irplib_framelist_delete(rawframes);
159  irplib_framelist_delete(tabframes);
160 
161  return cpl_error_get_code();
162 }
163 
164 
165 /*----------------------------------------------------------------------------*/
176 /*----------------------------------------------------------------------------*/
177 static
178 cpl_error_code visir_util_insert_pos_one(cpl_frameset * framelist,
179  double * pflux1,
180  irplib_framelist * rawframes,
181  irplib_framelist * tabframes, int i,
182  const cpl_parameterlist * parlist)
183 {
184 
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 ")$",
189  CPL_FALSE);
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();
196  const char * imgname = cpl_frame_get_filename(irplib_framelist_get_const
197  (rawframes, i));
198  const char * tabname = cpl_frame_get_filename(irplib_framelist_get_const
199  (tabframes, i));
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);
204  int iobj;
205 
206  skip_if(code);
207  bug_if(pflux1 == NULL);
208 
209  skip_if_lt(nobjs, 1, "row(s) in table of extracted sources with "
210  "column " FLUX_COL ": %s", tabname);
211 
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))));
216 
217  for (iobj = 0; iobj < nobjs; iobj++) {
218  int maxpos;
219  double xpos, ypos, relflux, flux, fluxerr;
220 
221  cpl_free(proname);
222  proname = cpl_sprintf(RECIPE_STRING "_%d_%d" CPL_DFS_FITS, 1+i, 1+iobj);
223 
224  skip_if(cpl_table_get_column_maxpos(table, FLUX_COL, &maxpos));
225 
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);
229 
230  /* Flag as processed */
231  bug_if(cpl_table_set_invalid(table, FLUX_COL, maxpos));
232 
233  if (*pflux1 != 0.0) {
234  relflux = flux / *pflux1;
235  } else {
236  *pflux1 = flux;
237  relflux = 1.0;
238  }
239 
240  cpl_msg_info(cpl_func, "Converting frame %d/%d: (%g,%g) using row %d",
241  1+i, n, xpos, ypos, maxpos);
242 
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));
247 
248  if (iobj == 0) {
249  /* Comments copied from actual header */
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,
255  VISIR_FLUX_COMM));
256  bug_if(cpl_propertylist_set_comment(updlist, VISIR_FLUX,
257  VISIR_FLUX_COMM));
258  }
259 
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));
264 
265  }
266 
267  /* Failure here is a bug */
268  skip_if_lt(cpl_table_count_invalid(table, FLUX_COL), nobjs,
269  "processed objects from table %s", tabname);
270 
271  for (frame = cpl_frameset_get_first_const(products);
272  frame != NULL;
273  frame = cpl_frameset_get_next_const(products)) {
274  cpl_frame * copy = cpl_frame_duplicate(frame);
275  cpl_error_code error;
276 
277 #ifdef _OPENMP
278 #pragma omp critical(visir_util_insert_pos_one)
279 #endif
280  error = cpl_frameset_insert(framelist, copy);
281 
282  if (error) break;
283  }
284  bug_if(frame != NULL);
285 
286  end_skip;
287 
288  cpl_image_delete(image);
289  cpl_table_delete(table);
290  cpl_free(proname);
291  cpl_propertylist_delete(updlist);
292  cpl_propertylist_delete(plsort);
293  cpl_frameset_delete(usedframes);
294  cpl_frameset_delete(products);
295 
296  return cpl_error_get_code();
297 }
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.
Definition: irplib_utils.c:192
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.
Definition: visir_dfs.c:72
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.