File: uves_extract_iterate.c

package info (click to toggle)
cpl-plugin-uves 6.1.3+dfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 23,128 kB
  • sloc: ansic: 171,056; sh: 4,359; python: 3,002; makefile: 1,322
file content (354 lines) | stat: -rw-r--r-- 11,898 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
/*                                                                              *
 *   This file is part of the ESO UVES Pipeline                                 *
 *   Copyright (C) 2004,2005 European Southern Observatory                      *
 *                                                                              *
 *   This library is free software; you can redistribute it and/or modify       *
 *   it under the terms of the GNU General Public License as published by       *
 *   the Free Software Foundation; either version 2 of the License, or          *
 *   (at your option) any later version.                                        *
 *                                                                              *
 *   This program is distributed in the hope that it will be useful,            *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of             *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the              *
 *   GNU General Public License for more details.                               *
 *                                                                              *
 *   You should have received a copy of the GNU General Public License          *
 *   along with this program; if not, write to the Free Software                *
 *   Foundation, 51 Franklin St, Fifth Floor, Boston, MA  02111-1307  USA       *
 *                                                                              */

/*
 * $Author: amodigli $
 * $Date: 2010-09-24 09:32:03 $
 * $Revision: 1.10 $
 * $Name: not supported by cvs2svn $
 * $Log: not supported by cvs2svn $
 * Revision 1.8  2010/02/13 12:22:31  amodigli
 * removed inlines (let's do work to compiler)
 *
 * Revision 1.7  2007/06/06 08:17:33  amodigli
 * replace tab with 4 spaces
 *
 * Revision 1.6  2007/05/02 13:17:23  jmlarsen
 * Allow specifying offset in optimal extraction
 *
 * Revision 1.5  2007/02/21 12:44:58  jmlarsen
 * uves_iterate_increment: Avoid recursion, fixed bug at beginning of new order (y limits were not recalculated)
 *
 * Revision 1.4  2006/11/20 08:01:46  jmlarsen
 * Changed format of pointer in uves_iterate_dump
 *
 * Revision 1.3  2006/11/16 09:48:30  jmlarsen
 * Renamed data type position -> uves_iterate_position, for namespace reasons
 *
 * Revision 1.2  2006/09/11 14:19:28  jmlarsen
 * Updated documentation
 *
 * Revision 1.1  2006/09/08 14:04:00  jmlarsen
 * Simplified code by using iterators, sky subtraction much optimized
 *
 *
 */

#ifdef HAVE_CONFIG_H
#  include <config.h>
#endif

/*----------------------------------------------------------------------------*/
/**
 * @addtogroup uves_extract_iterate   Iterate over an image
 *
 * This class exists in order to be able to write
 *
 *  @code
 *      for (uves_iterate_set_first(pos, ...);
 *           !uves_iterate_finished(pos);
 *           uves_iterate_increment(pos))
 *       {
 *             do something
 *       }
 *  @endcode
 *
 *  instead of repeating the same structure of nested loops + selections
 *  again and again in the extraction module. I.e. to avoid repetitions of
 *  (in pseudocode)
 *   
 *  @code
 *
 *  for each order ...
 *     for each bin ...
 *        calculate limits for this bin
 *        for each position on the slit ...
 *           if pixel is good ...
 *
 *              do something
 *
 *  @endcode
 *
 *  where only the "do something" part is different
 *
 */
/*----------------------------------------------------------------------------*/
/**@{*/


/*-----------------------------------------------------------------------------
                                Includes
 -----------------------------------------------------------------------------*/

#include <uves_extract_iterate.h>

#include <uves_utils.h>

#include <cpl.h>

/*-----------------------------------------------------------------------------
                            Functions prototypes
 -----------------------------------------------------------------------------*/
static
bool illegal_position(const uves_iterate_position *p);

/*-----------------------------------------------------------------------------
                            Implementation
 -----------------------------------------------------------------------------*/


/*----------------------------------------------------------------------------*/
/**
   @brief    Allocate iterator
   @param    image           the image to iterate over
   @param    order_locations order polynomial
   @param    minorder        first order (inclusive) in image
   @param    maxorder        last order (inclusive) in image
   @param    slit_geometry   extraction slit length and offset
   @return   newly allocated iterator which must be deallocated using
             uves_iterate_delete

   The iterator, pos, should be used in constructs like
   @code
    for (uves_iterate_set_first(pos,
                    xmin,
                    xmax,
                    ordermin, ordermax,
                    current_image_bpm, true);
         !uves_iterate_finished(pos);
         uves_iterate_increment(pos))
         {
                flux += image[p->x, p->y];
         }
   @endcode

   The struct members

    int order;
    int x, y;
    double ycenter;
    int ylow;
    int yhigh;

   can be accessed inside the loop.

*/
/*----------------------------------------------------------------------------*/
uves_iterate_position *
uves_iterate_new(int nx, int ny,
         const polynomial *order_locations,
         int minorder, int maxorder,
         slit_geometry sg)
{
    uves_iterate_position *p = cpl_calloc(1, sizeof(uves_iterate_position));

    p->nx = nx;
    p->ny = ny;
    p->order_locations = order_locations;
    p->minorder = minorder;
    p->maxorder = maxorder;
    p->sg = sg;

    return p;
}

/*----------------------------------------------------------------------------*/
/**
   @brief    Deallocate iterator and set pointer to NULL
   @param    p             iterator to delete
*/
/*----------------------------------------------------------------------------*/
void
uves_iterate_delete(uves_iterate_position **p)
{
    if (p != NULL)
    {
        cpl_free(*p);
        *p = NULL;
    }
}

/*----------------------------------------------------------------------------*/
/**
   @brief    Initialize iteration
   @param    p             iterator to initialize
   @param    xmin          minimum x bin
   @param    xmax          maximum x bin
   @param    ordermin      first order to iterate over
   @param    ordermax      last order to iterate over
   @param    bpm           image bad pixel map to use for this iteration. If
                           NULL all pixels are considered good
   @param    loop_y        if true, the iteration is over (order, x, y).
                           If false, the iteration is over (order, x) only,
               and the bpm is not used
*/
/*----------------------------------------------------------------------------*/
void
uves_iterate_set_first(uves_iterate_position *p,
               int xmin, int xmax, 
               int ordermin, int ordermax,
               const cpl_binary *bpm,
               bool loop_y)
{
    /* Limits for this iteration */
    p->xmin = xmin;
    p->xmax = xmax;
    p->ordermax = ordermax;
    p->bpm = bpm;
    p->loop_y = loop_y;
    p->end = false;

    /* Set first postion */
    p->x = xmin;
    p->order = ordermin;

    p->ycenter = uves_polynomial_evaluate_2d(p->order_locations, p->x, p->order)
        + p->sg.offset;
    p->yhigh = uves_round_double(p->ycenter + p->sg.length/2);
    p->ylow  = uves_round_double(p->ycenter - p->sg.length/2);
    if (loop_y) 
    {
        p->y = p->ylow;
    }

    /* Go to first good pixel */
    while (illegal_position(p) && !uves_iterate_finished(p))
    {
        uves_iterate_increment(p);
    }
}

/*----------------------------------------------------------------------------*/
/**
   @brief    Get next position
   @param    p             iterator to increment

   p is changed to contain the next position
*/
/*----------------------------------------------------------------------------*/
void
uves_iterate_increment(uves_iterate_position *p)
{
    do {
    if (p->loop_y && p->y < p->yhigh)
        {
        (p->y)++;
        }
    else if (p->x < p->xmax)
        {
        (p->x)++;
        
        p->ycenter = 
                    uves_polynomial_evaluate_2d(p->order_locations, 
                                                p->x, p->order)
                    + p->sg.offset;

        p->yhigh = uves_round_double(p->ycenter + p->sg.length/2);
        p->ylow  = uves_round_double(p->ycenter - p->sg.length/2);
        if (p->loop_y) p->y = p->ylow;
        }
    else if (p->order < p->ordermax)
        {
        (p->order)++;
        p->x = p->xmin;
        
        p->ycenter = 
                    uves_polynomial_evaluate_2d(p->order_locations,
                                                p->x, p->order)
                    + p->sg.offset;

        p->yhigh = uves_round_double(p->ycenter + p->sg.length/2);
        p->ylow  = uves_round_double(p->ycenter - p->sg.length/2);
        if (p->loop_y) p->y = p->ylow;
        }
    else
        {
        p->end = true;
        }
    } while (illegal_position(p) && !uves_iterate_finished(p));

    return;
}

/*----------------------------------------------------------------------------*/
/**
   @brief    Finished iterating?
   @param    p             iterator to check
   @return false if p contains a valid position, false if the iteration
   is over

   This function should always be checked after calling uves_iterate_increment()
   and uves_iterate_set_first()
*/
/*----------------------------------------------------------------------------*/
bool
uves_iterate_finished(const uves_iterate_position *p)
{
    return p->end;
}

/*----------------------------------------------------------------------------*/
/**
   @brief    dump iterator (for debugging)
   @param    p             iterator to check
   @param    stream        stream to write to
*/
/*----------------------------------------------------------------------------*/
void
uves_iterate_dump(const uves_iterate_position *p, FILE *stream)
{
    fprintf(stream, "Position:\n");
    fprintf(stream, "order       = %d\n", p->order);
    fprintf(stream, "x           = %d\n", p->x);
    fprintf(stream, "y           = %d\n", p->y);
    fprintf(stream, "ycenter     = %f\n", p->ycenter);
    fprintf(stream, "ylow, yhigh = %d, %d\n", p->ylow, p->yhigh);
    fprintf(stream, "Limits:\n");
    fprintf(stream, "xmin, xmax = %d, %d\n", p->xmin, p->xmax);
    fprintf(stream, "ordermax   = %d\n", p->ordermax);
    fprintf(stream, "bpm        = %d\n", p->bpm != NULL ? 1 : 0);
    fprintf(stream, "loop_y     = %s\n", p->loop_y ? "true" : "false");
    fprintf(stream, "end        = %s\n", p->end    ? "true" : "false");
    fprintf(stream, "Geometry:\n");
    fprintf(stream, "nx, ny             = %d, %d\n", p->nx, p->ny);
    fprintf(stream, "minorder, maxorder = %d, %d\n", p->minorder, p->maxorder);
    fprintf(stream, "order_locations    = %d\n", p->order_locations != NULL ? 1 : 0);
    fprintf(stream, "slit length        = %f\n", p->sg.length);
    fprintf(stream, "slit offset        = %f\n", p->sg.offset);

    return;
}

/*----------------------------------------------------------------------------*/
/**
   @brief    Find out if the current position is valid
   @param    p             iterator to check
   @return true iff the current position is a good pixel and the entire
   slit is inside the image
*/
/*----------------------------------------------------------------------------*/
static
bool illegal_position(const uves_iterate_position *p)
{
    return p->ylow < 1 || p->yhigh > p->ny ||
    (p->loop_y && p->bpm != NULL &&
     p->bpm[(p->x-1) + (p->y-1)*p->nx] != CPL_BINARY_0);
}

/**@}*/