UVES Pipeline Reference Manual  5.4.6
uves_extract_iterate.c
1 /* *
2  * This file is part of the ESO UVES Pipeline *
3  * Copyright (C) 2004,2005 European Southern Observatory *
4  * *
5  * This library is free software; you can redistribute it and/or modify *
6  * it under the terms of the GNU General Public License as published by *
7  * the Free Software Foundation; either version 2 of the License, or *
8  * (at your option) any later version. *
9  * *
10  * This program is distributed in the hope that it will be useful, *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13  * GNU General Public License for more details. *
14  * *
15  * You should have received a copy of the GNU General Public License *
16  * along with this program; if not, write to the Free Software *
17  * Foundation, 51 Franklin St, Fifth Floor, Boston, MA 02111-1307 USA *
18  * */
19 
20 /*
21  * $Author: amodigli $
22  * $Date: 2010-09-24 09:32:03 $
23  * $Revision: 1.10 $
24  * $Name: not supported by cvs2svn $
25  * $Log: not supported by cvs2svn $
26  * Revision 1.8 2010/02/13 12:22:31 amodigli
27  * removed inlines (let's do work to compiler)
28  *
29  * Revision 1.7 2007/06/06 08:17:33 amodigli
30  * replace tab with 4 spaces
31  *
32  * Revision 1.6 2007/05/02 13:17:23 jmlarsen
33  * Allow specifying offset in optimal extraction
34  *
35  * Revision 1.5 2007/02/21 12:44:58 jmlarsen
36  * uves_iterate_increment: Avoid recursion, fixed bug at beginning of new order (y limits were not recalculated)
37  *
38  * Revision 1.4 2006/11/20 08:01:46 jmlarsen
39  * Changed format of pointer in uves_iterate_dump
40  *
41  * Revision 1.3 2006/11/16 09:48:30 jmlarsen
42  * Renamed data type position -> uves_iterate_position, for namespace reasons
43  *
44  * Revision 1.2 2006/09/11 14:19:28 jmlarsen
45  * Updated documentation
46  *
47  * Revision 1.1 2006/09/08 14:04:00 jmlarsen
48  * Simplified code by using iterators, sky subtraction much optimized
49  *
50  *
51  */
52 
53 #ifdef HAVE_CONFIG_H
54 # include <config.h>
55 #endif
56 
57 /*----------------------------------------------------------------------------*/
91 /*----------------------------------------------------------------------------*/
95 /*-----------------------------------------------------------------------------
96  Includes
97  -----------------------------------------------------------------------------*/
98 
99 #include <uves_extract_iterate.h>
100 
101 #include <uves_utils.h>
102 
103 #include <cpl.h>
104 
105 /*-----------------------------------------------------------------------------
106  Functions prototypes
107  -----------------------------------------------------------------------------*/
108 static
109 bool illegal_position(const uves_iterate_position *p);
110 
111 /*-----------------------------------------------------------------------------
112  Implementation
113  -----------------------------------------------------------------------------*/
114 
115 
116 /*----------------------------------------------------------------------------*/
152 /*----------------------------------------------------------------------------*/
153 uves_iterate_position *
154 uves_iterate_new(int nx, int ny,
155  const polynomial *order_locations,
156  int minorder, int maxorder,
157  slit_geometry sg)
158 {
159  uves_iterate_position *p = cpl_calloc(1, sizeof(uves_iterate_position));
160 
161  p->nx = nx;
162  p->ny = ny;
163  p->order_locations = order_locations;
164  p->minorder = minorder;
165  p->maxorder = maxorder;
166  p->sg = sg;
167 
168  return p;
169 }
170 
171 /*----------------------------------------------------------------------------*/
176 /*----------------------------------------------------------------------------*/
177 void
178 uves_iterate_delete(uves_iterate_position **p)
179 {
180  if (p != NULL)
181  {
182  cpl_free(*p);
183  *p = NULL;
184  }
185 }
186 
187 /*----------------------------------------------------------------------------*/
201 /*----------------------------------------------------------------------------*/
202 void
203 uves_iterate_set_first(uves_iterate_position *p,
204  int xmin, int xmax,
205  int ordermin, int ordermax,
206  const cpl_binary *bpm,
207  bool loop_y)
208 {
209  /* Limits for this iteration */
210  p->xmin = xmin;
211  p->xmax = xmax;
212  p->ordermax = ordermax;
213  p->bpm = bpm;
214  p->loop_y = loop_y;
215  p->end = false;
216 
217  /* Set first postion */
218  p->x = xmin;
219  p->order = ordermin;
220 
221  p->ycenter = uves_polynomial_evaluate_2d(p->order_locations, p->x, p->order)
222  + p->sg.offset;
223  p->yhigh = uves_round_double(p->ycenter + p->sg.length/2);
224  p->ylow = uves_round_double(p->ycenter - p->sg.length/2);
225  if (loop_y)
226  {
227  p->y = p->ylow;
228  }
229 
230  /* Go to first good pixel */
231  while (illegal_position(p) && !uves_iterate_finished(p))
232  {
234  }
235 }
236 
237 /*----------------------------------------------------------------------------*/
244 /*----------------------------------------------------------------------------*/
245 void
246 uves_iterate_increment(uves_iterate_position *p)
247 {
248  do {
249  if (p->loop_y && p->y < p->yhigh)
250  {
251  (p->y)++;
252  }
253  else if (p->x < p->xmax)
254  {
255  (p->x)++;
256 
257  p->ycenter =
258  uves_polynomial_evaluate_2d(p->order_locations,
259  p->x, p->order)
260  + p->sg.offset;
261 
262  p->yhigh = uves_round_double(p->ycenter + p->sg.length/2);
263  p->ylow = uves_round_double(p->ycenter - p->sg.length/2);
264  if (p->loop_y) p->y = p->ylow;
265  }
266  else if (p->order < p->ordermax)
267  {
268  (p->order)++;
269  p->x = p->xmin;
270 
271  p->ycenter =
272  uves_polynomial_evaluate_2d(p->order_locations,
273  p->x, p->order)
274  + p->sg.offset;
275 
276  p->yhigh = uves_round_double(p->ycenter + p->sg.length/2);
277  p->ylow = uves_round_double(p->ycenter - p->sg.length/2);
278  if (p->loop_y) p->y = p->ylow;
279  }
280  else
281  {
282  p->end = true;
283  }
284  } while (illegal_position(p) && !uves_iterate_finished(p));
285 
286  return;
287 }
288 
289 /*----------------------------------------------------------------------------*/
299 /*----------------------------------------------------------------------------*/
300 bool
301 uves_iterate_finished(const uves_iterate_position *p)
302 {
303  return p->end;
304 }
305 
306 /*----------------------------------------------------------------------------*/
312 /*----------------------------------------------------------------------------*/
313 void
314 uves_iterate_dump(const uves_iterate_position *p, FILE *stream)
315 {
316  fprintf(stream, "Position:\n");
317  fprintf(stream, "order = %d\n", p->order);
318  fprintf(stream, "x = %d\n", p->x);
319  fprintf(stream, "y = %d\n", p->y);
320  fprintf(stream, "ycenter = %f\n", p->ycenter);
321  fprintf(stream, "ylow, yhigh = %d, %d\n", p->ylow, p->yhigh);
322  fprintf(stream, "Limits:\n");
323  fprintf(stream, "xmin, xmax = %d, %d\n", p->xmin, p->xmax);
324  fprintf(stream, "ordermax = %d\n", p->ordermax);
325  fprintf(stream, "bpm = %d\n", p->bpm != NULL ? 1 : 0);
326  fprintf(stream, "loop_y = %s\n", p->loop_y ? "true" : "false");
327  fprintf(stream, "end = %s\n", p->end ? "true" : "false");
328  fprintf(stream, "Geometry:\n");
329  fprintf(stream, "nx, ny = %d, %d\n", p->nx, p->ny);
330  fprintf(stream, "minorder, maxorder = %d, %d\n", p->minorder, p->maxorder);
331  fprintf(stream, "order_locations = %d\n", p->order_locations != NULL ? 1 : 0);
332  fprintf(stream, "slit length = %f\n", p->sg.length);
333  fprintf(stream, "slit offset = %f\n", p->sg.offset);
334 
335  return;
336 }
337 
338 /*----------------------------------------------------------------------------*/
345 /*----------------------------------------------------------------------------*/
346 static
347 bool illegal_position(const uves_iterate_position *p)
348 {
349  return p->ylow < 1 || p->yhigh > p->ny ||
350  (p->loop_y && p->bpm != NULL &&
351  p->bpm[(p->x-1) + (p->y-1)*p->nx] != CPL_BINARY_0);
352 }
353 
bool uves_iterate_finished(const uves_iterate_position *p)
Finished iterating?
void uves_iterate_delete(uves_iterate_position **p)
Deallocate iterator and set pointer to NULL.
static bool illegal_position(const uves_iterate_position *p)
Find out if the current position is valid.
uves_iterate_position * uves_iterate_new(int nx, int ny, const polynomial *order_locations, int minorder, int maxorder, slit_geometry sg)
Allocate iterator.
void uves_iterate_set_first(uves_iterate_position *p, int xmin, int xmax, int ordermin, int ordermax, const cpl_binary *bpm, bool loop_y)
Initialize iteration.
double uves_polynomial_evaluate_2d(const polynomial *p, double x1, double x2)
Evaluate a 2d polynomial.
void uves_iterate_increment(uves_iterate_position *p)
Get next position.
void uves_iterate_dump(const uves_iterate_position *p, FILE *stream)
dump iterator (for debugging)