VISIR Pipeline Reference Manual  4.1.0
visir_img_pfov.c
1 /* $Id: visir_img_pfov.c,v 1.85 2009-02-27 10:44:01 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: 2009-02-27 10:44:01 $
24  * $Revision: 1.85 $
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_img_pfov"
43 
44 /*-----------------------------------------------------------------------------
45  Private Functions prototypes
46  -----------------------------------------------------------------------------*/
47 
48 static cpl_error_code visir_img_pfov_save(cpl_frameset *,
49  const cpl_parameterlist *,
50  const cpl_table *);
51 
52 VISIR_RECIPE_DEFINE(visir_img_pfov,
53  VISIR_PARAM_NODPOS | VISIR_PARAM_AUTOBPM |
54  VISIR_PARAM_STRIPITE | VISIR_PARAM_STRIPMOR |
55  VISIR_PARAM_STRIPNON |
56  VISIR_PARAM_GLITCH | VISIR_PARAM_PURGE,
57  "Pixel field of view recipe",
58  "This recipe determines the pixel field of view by finding "
59  "the precise\n"
60  "position of a bright object and comparing it to the "
61  "header information\n"
62  "giving the telescope pointing.\n"
63  "The files listed in the Set Of Frames (sof-file) "
64  "must be tagged:\n"
65  "VISIR-field-of-view-file.fits " VISIR_IMG_PFOV_BIN " or\n"
66  "VISIR-field-of-view-file.fits " VISIR_IMG_PFOV_TEL "\n"
67  "\n"
68  "The corresponding product will have a FITS card\n"
69  "'HIERARCH ESO PRO CATG' with a value of\n"
70  VISIR_IMG_PFOV_TAB_PROCATG_BIN " or\n"
71  VISIR_IMG_PFOV_TAB_PROCATG_TEL
72  "\n"
73  MAN_VISIR_CALIB_BPM_IMG);
74 
75 /*-----------------------------------------------------------------------------
76  Static variables
77  -----------------------------------------------------------------------------*/
78 
79 enum _visir_img_mode_ {
80  visir_img_none = 0,
81  visir_img_bin,
82  visir_img_tel
83 };
84 
85 typedef enum _visir_img_mode_ visir_img_mode;
86 
87 static struct {
88  /* Inputs */
89  visir_img_mode img_mode;
90 
91  /* Outputs */
92 } visir_img_pfov_config;
93 
94 /*----------------------------------------------------------------------------*/
98 /*----------------------------------------------------------------------------*/
99 
100 /*-----------------------------------------------------------------------------
101  Functions code
102  -----------------------------------------------------------------------------*/
103 
104 /*----------------------------------------------------------------------------*/
111 /*----------------------------------------------------------------------------*/
112 static int visir_img_pfov(cpl_frameset * framelist,
113  const cpl_parameterlist * parlist)
114 {
115  cpl_errorstate cleanstate = cpl_errorstate_get();
116  irplib_framelist * allframes = NULL;
117  irplib_framelist * rawframes = NULL;
118  const char * badpix;
119  const char * flat;
120  cpl_imagelist * nodded = NULL;
121  cpl_vector * ok = NULL;
122  cpl_table * tab = NULL;
123  double xprev = 0.0; /* Avoid (false) uninit warning */
124  double yprev = 0.0; /* Avoid (false) uninit warning */
125  double aprev = 0.0; /* Avoid (false) uninit warning */
126  double dprev = 0.0; /* Avoid (false) uninit warning */
127  int oki, okprev;
128  int nrow;
129  int i;
130 
131 
132  /* Identify the RAW and CALIB frames in the input frameset */
133  skip_if (visir_dfs_set_groups(framelist));
134 
135  /* Objects observation */
136  allframes = irplib_framelist_cast(framelist);
137  skip_if(allframes == NULL);
138  rawframes = irplib_framelist_extract_regexp(allframes, "^(" VISIR_IMG_PFOV_BIN
139  "|" VISIR_IMG_PFOV_TEL ")$",
140  CPL_FALSE);
141  skip_if (rawframes == NULL);
142 
143  skip_if(irplib_framelist_load_propertylist_all(rawframes, 0,
144  visir_property_regexp,
145  CPL_FALSE));
146 
147  skip_if(visir_dfs_check_framelist_tag(rawframes));
148 
149  /* Verify uniqueness of frame type */
150  visir_img_pfov_config.img_mode = visir_img_none;
151  if (cpl_frameset_find(framelist, VISIR_IMG_PFOV_BIN))
152  visir_img_pfov_config.img_mode = visir_img_bin;
153  if (cpl_frameset_find(framelist, VISIR_IMG_PFOV_TEL)) {
154  skip_if (visir_img_pfov_config.img_mode);
155  visir_img_pfov_config.img_mode = visir_img_tel;
156  }
157 
158  bug_if(visir_img_pfov_config.img_mode == visir_img_none);
159 
160  /* Bad pixels calibration file */
161  badpix = irplib_frameset_find_file(framelist, VISIR_CALIB_BPM);
162 
163  /* Flatfield calibration file */
164  flat = irplib_frameset_find_file(framelist, VISIR_CALIB_FLAT);
165 
166  /* Combine the frames */
167  cpl_msg_info(cpl_func, "Construct the nodded images");
168 
169  nodded = visir_inputs_combine(RECIPE_STRING, parlist, rawframes, badpix, flat,
170  NULL, CPL_FALSE, 0.0, 0);
171  if (nodded == NULL) {
172  cpl_msg_error(cpl_func, "Cannot combine the input frames");
173  skip_if(1);
174  }
175 
176  nrow = cpl_imagelist_get_size(nodded);
177 
178  /* Allocate and initialise arrays */
179  tab = visir_table_new_xypos(nodded, "FLUX");
180  skip_if (tab == NULL);
181 
182  skip_if (cpl_table_erase_column(tab, "FLUX"));
183 
184  skip_if (cpl_table_new_column(tab, "A_POS", CPL_TYPE_DOUBLE));
185  skip_if (cpl_table_new_column(tab, "D_POS", CPL_TYPE_DOUBLE));
186  skip_if (cpl_table_new_column(tab, "PFOV", CPL_TYPE_DOUBLE));
187 
188  skip_if (cpl_table_fill_column_window(tab, "A_POS", 0, nrow, -1));
189  skip_if (cpl_table_fill_column_window(tab, "D_POS", 0, nrow, -1));
190  skip_if (cpl_table_fill_column_window(tab, "PFOV", 0, nrow, 0));
191 
192  ok = cpl_vector_new(nrow);
193 
194  skip_if (cpl_vector_fill(ok, 1));
195 
196  oki = 0;
197  for (i=0; i < nrow ; i++) {
198  const cpl_propertylist * plist =
200 
201  /* Angle from the header */
202  const double apos = visir_pfits_get_alpha(plist);
203  const double dpos = visir_pfits_get_delta(plist);
204 
205  if (cpl_error_get_code()) {
206  visir_error_reset("Could not get FITS key");
207  skip_if(cpl_vector_set(ok, i, 0));
208  continue;
209  }
210 
211  if (cpl_table_get_double(tab, "X_POS", i, NULL) <= 0 ||
212  cpl_table_get_double(tab, "Y_POS", i, NULL) <= 0)
213  skip_if(cpl_vector_set(ok, i, 0));
214 
215  skip_if (cpl_table_set_double(tab, "A_POS", i, apos));
216  skip_if (cpl_table_set_double(tab, "D_POS", i, dpos));
217 
218  oki = 1;
219 
220  }
221 
222  if (oki == 0) {
223  cpl_msg_error(cpl_func, "None of the %d files contain a valid combination "
224  "of centroids and offsets", nrow);
225  visir_error_set(CPL_ERROR_DATA_NOT_FOUND);
226  skip_if(1);
227  }
228 
229  /* Compute the pixel field of view */
230  okprev = 0;
231  for (i=0; i < nrow ; i++, okprev = oki) {
232  oki = cpl_vector_get(ok, i) != 0.0 ? 1 : 0;
233  if (oki) {
234  const double x = cpl_table_get_double(tab, "X_POS", i, NULL);
235  const double y = cpl_table_get_double(tab, "Y_POS", i, NULL);
236  const double a = cpl_table_get_double(tab, "A_POS", i, NULL);
237  const double d = cpl_table_get_double(tab, "D_POS", i, NULL);
238 
239  if (okprev) {
240 
241  const double dx = x - xprev;
242  const double dy = y - yprev;
243  const double da = a - aprev;
244  const double dd = d - dprev;
245 
246  const double dividend = sqrt(da * da + dd * dd);
247  const double divisor = sqrt(dx * dx + dy * dy);
248 
249  if (dividend < FLT_MAX * divisor)
250  cpl_table_set_double(tab, "PFOV", i, dividend / divisor);
251 
252  }
253 
254  xprev = x;
255  yprev = y;
256  aprev = a;
257  dprev = d;
258  }
259  }
260 
261  /* Save the results */
262  cpl_msg_info(cpl_func, "Save the results");
263  skip_if (visir_img_pfov_save(framelist, parlist, tab));
264 
265  end_skip;
266 
267  irplib_framelist_delete(allframes);
268  irplib_framelist_delete(rawframes);
269  cpl_vector_delete(ok);
270  cpl_imagelist_delete(nodded);
271  cpl_table_delete(tab);
272 
273  return cpl_error_get_code();
274 }
275 
276 /*----------------------------------------------------------------------------*/
284 /*----------------------------------------------------------------------------*/
285 static cpl_error_code visir_img_pfov_save(cpl_frameset * set,
286  const cpl_parameterlist * parlist,
287  const cpl_table * tab)
288 {
289 
290  skip_if(irplib_dfs_save_table(set, parlist, set, tab, NULL, RECIPE_STRING,
291  visir_img_pfov_config.img_mode == visir_img_bin
292  ? VISIR_IMG_PFOV_TAB_PROCATG_BIN
293  : VISIR_IMG_PFOV_TAB_PROCATG_TEL, NULL, NULL,
294  visir_pipe_id, RECIPE_STRING CPL_DFS_FITS));
295 
296  end_skip;
297 
298  return cpl_error_get_code();
299 }
cpl_error_code visir_dfs_check_framelist_tag(const irplib_framelist *self)
Check the tags in a frameset (group raw only)
Definition: visir_dfs.c:173
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.
Definition: irplib_utils.c:335
irplib_framelist * irplib_framelist_extract_regexp(const irplib_framelist *self, const char *regexp, cpl_boolean invert)
Extract the frames with the given tag from a framelist.
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_framelist_load_propertylist_all(irplib_framelist *self, int ind, const char *regexp, cpl_boolean invert)
Load the propertylists of all frames in the framelist.
cpl_imagelist * visir_inputs_combine(const char *recipename, const cpl_parameterlist *parlist, const irplib_framelist *rawframes, const char *badpix, const char *flat, int *nodding_p, cpl_boolean do_spc_fix, double wlen, visir_spc_resol resol)
The VISIR input data re-combination is performed here.
Definition: visir_inputs.c:686
int visir_dfs_set_groups(cpl_frameset *set)
Set the group as RAW or CALIB in a frameset.
Definition: visir_dfs.c:72
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.
double visir_pfits_get_delta(const cpl_propertylist *self)
The DELTA keyword in a VISIR header.
Definition: visir_pfits.c:277
double visir_pfits_get_alpha(const cpl_propertylist *self)
The alpha angle.
Definition: visir_pfits.c:107
irplib_framelist * irplib_framelist_cast(const cpl_frameset *frameset)
Create an irplib_framelist from a cpl_framelist.