uves_extract_iterate.c

00001 /*                                                                              *
00002  *   This file is part of the ESO UVES Pipeline                                 *
00003  *   Copyright (C) 2004,2005 European Southern Observatory                      *
00004  *                                                                              *
00005  *   This library is free software; you can redistribute it and/or modify       *
00006  *   it under the terms of the GNU General Public License as published by       *
00007  *   the Free Software Foundation; either version 2 of the License, or          *
00008  *   (at your option) any later version.                                        *
00009  *                                                                              *
00010  *   This program is distributed in the hope that it will be useful,            *
00011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of             *
00012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the              *
00013  *   GNU General Public License for more details.                               *
00014  *                                                                              *
00015  *   You should have received a copy of the GNU General Public License          *
00016  *   along with this program; if not, write to the Free Software                *
00017  *   Foundation, 51 Franklin St, Fifth Floor, Boston, MA  02111-1307  USA       *
00018  *                                                                              */
00019 
00020 /*
00021  * $Author: amodigli $
00022  * $Date: 2007/06/06 08:17:33 $
00023  * $Revision: 1.7 $
00024  * $Name: uves-3_9_0 $
00025  * $Log: uves_extract_iterate.c,v $
00026  * Revision 1.7  2007/06/06 08:17:33  amodigli
00027  * replace tab with 4 spaces
00028  *
00029  * Revision 1.6  2007/05/02 13:17:23  jmlarsen
00030  * Allow specifying offset in optimal extraction
00031  *
00032  * Revision 1.5  2007/02/21 12:44:58  jmlarsen
00033  * uves_iterate_increment: Avoid recursion, fixed bug at beginning of new order (y limits were not recalculated)
00034  *
00035  * Revision 1.4  2006/11/20 08:01:46  jmlarsen
00036  * Changed format of pointer in uves_iterate_dump
00037  *
00038  * Revision 1.3  2006/11/16 09:48:30  jmlarsen
00039  * Renamed data type position -> uves_iterate_position, for namespace reasons
00040  *
00041  * Revision 1.2  2006/09/11 14:19:28  jmlarsen
00042  * Updated documentation
00043  *
00044  * Revision 1.1  2006/09/08 14:04:00  jmlarsen
00045  * Simplified code by using iterators, sky subtraction much optimized
00046  *
00047  *
00048  */
00049 
00050 #ifdef HAVE_CONFIG_H
00051 #  include <config.h>
00052 #endif
00053 
00054 /*----------------------------------------------------------------------------*/
00088 /*----------------------------------------------------------------------------*/
00092 /*-----------------------------------------------------------------------------
00093                                 Includes
00094  -----------------------------------------------------------------------------*/
00095 
00096 #include <uves_extract_iterate.h>
00097 
00098 #include <uves_utils.h>
00099 
00100 #include <cpl.h>
00101 
00102 /*-----------------------------------------------------------------------------
00103                             Functions prototypes
00104  -----------------------------------------------------------------------------*/
00105 static
00106 bool illegal_position(const uves_iterate_position *p);
00107 
00108 /*-----------------------------------------------------------------------------
00109                             Implementation
00110  -----------------------------------------------------------------------------*/
00111 
00112 
00113 /*----------------------------------------------------------------------------*/
00149 /*----------------------------------------------------------------------------*/
00150 uves_iterate_position *
00151 uves_iterate_new(int nx, int ny,
00152          const polynomial *order_locations,
00153          int minorder, int maxorder,
00154          slit_geometry sg)
00155 {
00156     uves_iterate_position *p = cpl_calloc(1, sizeof(uves_iterate_position));
00157 
00158     p->nx = nx;
00159     p->ny = ny;
00160     p->order_locations = order_locations;
00161     p->minorder = minorder;
00162     p->maxorder = maxorder;
00163     p->sg = sg;
00164 
00165     return p;
00166 }
00167 
00168 /*----------------------------------------------------------------------------*/
00173 /*----------------------------------------------------------------------------*/
00174 void
00175 uves_iterate_delete(uves_iterate_position **p)
00176 {
00177     if (p != NULL)
00178     {
00179         cpl_free(*p);
00180         *p = NULL;
00181     }
00182 }
00183 
00184 /*----------------------------------------------------------------------------*/
00198 /*----------------------------------------------------------------------------*/
00199 void
00200 uves_iterate_set_first(uves_iterate_position *p,
00201                int xmin, int xmax, 
00202                int ordermin, int ordermax,
00203                const cpl_binary *bpm,
00204                bool loop_y)
00205 {
00206     /* Limits for this iteration */
00207     p->xmin = xmin;
00208     p->xmax = xmax;
00209     p->ordermax = ordermax;
00210     p->bpm = bpm;
00211     p->loop_y = loop_y;
00212     p->end = false;
00213 
00214     /* Set first postion */
00215     p->x = xmin;
00216     p->order = ordermin;
00217 
00218     p->ycenter = uves_polynomial_evaluate_2d(p->order_locations, p->x, p->order)
00219         + p->sg.offset;
00220     p->yhigh = uves_round_double(p->ycenter + p->sg.length/2);
00221     p->ylow  = uves_round_double(p->ycenter - p->sg.length/2);
00222     if (loop_y) 
00223     {
00224         p->y = p->ylow;
00225     }
00226 
00227     /* Go to first good pixel */
00228     while (illegal_position(p) && !uves_iterate_finished(p))
00229     {
00230         uves_iterate_increment(p);
00231     }
00232 }
00233 
00234 /*----------------------------------------------------------------------------*/
00241 /*----------------------------------------------------------------------------*/
00242 inline void
00243 uves_iterate_increment(uves_iterate_position *p)
00244 {
00245     do {
00246     if (p->loop_y && p->y < p->yhigh)
00247         {
00248         (p->y)++;
00249         }
00250     else if (p->x < p->xmax)
00251         {
00252         (p->x)++;
00253         
00254         p->ycenter = 
00255                     uves_polynomial_evaluate_2d(p->order_locations, 
00256                                                 p->x, p->order)
00257                     + p->sg.offset;
00258 
00259         p->yhigh = uves_round_double(p->ycenter + p->sg.length/2);
00260         p->ylow  = uves_round_double(p->ycenter - p->sg.length/2);
00261         if (p->loop_y) p->y = p->ylow;
00262         }
00263     else if (p->order < p->ordermax)
00264         {
00265         (p->order)++;
00266         p->x = p->xmin;
00267         
00268         p->ycenter = 
00269                     uves_polynomial_evaluate_2d(p->order_locations,
00270                                                 p->x, p->order)
00271                     + p->sg.offset;
00272 
00273         p->yhigh = uves_round_double(p->ycenter + p->sg.length/2);
00274         p->ylow  = uves_round_double(p->ycenter - p->sg.length/2);
00275         if (p->loop_y) p->y = p->ylow;
00276         }
00277     else
00278         {
00279         p->end = true;
00280         }
00281     } while (illegal_position(p) && !uves_iterate_finished(p));
00282 
00283     return;
00284 }
00285 
00286 /*----------------------------------------------------------------------------*/
00296 /*----------------------------------------------------------------------------*/
00297 inline bool
00298 uves_iterate_finished(const uves_iterate_position *p)
00299 {
00300     return p->end;
00301 }
00302 
00303 /*----------------------------------------------------------------------------*/
00309 /*----------------------------------------------------------------------------*/
00310 void
00311 uves_iterate_dump(const uves_iterate_position *p, FILE *stream)
00312 {
00313     fprintf(stream, "Position:\n");
00314     fprintf(stream, "order       = %d\n", p->order);
00315     fprintf(stream, "x           = %d\n", p->x);
00316     fprintf(stream, "y           = %d\n", p->y);
00317     fprintf(stream, "ycenter     = %f\n", p->ycenter);
00318     fprintf(stream, "ylow, yhigh = %d, %d\n", p->ylow, p->yhigh);
00319     fprintf(stream, "Limits:\n");
00320     fprintf(stream, "xmin, xmax = %d, %d\n", p->xmin, p->xmax);
00321     fprintf(stream, "ordermax   = %d\n", p->ordermax);
00322     fprintf(stream, "bpm        = %d\n", p->bpm != NULL ? 1 : 0);
00323     fprintf(stream, "loop_y     = %s\n", p->loop_y ? "true" : "false");
00324     fprintf(stream, "end        = %s\n", p->end    ? "true" : "false");
00325     fprintf(stream, "Geometry:\n");
00326     fprintf(stream, "nx, ny             = %d, %d\n", p->nx, p->ny);
00327     fprintf(stream, "minorder, maxorder = %d, %d\n", p->minorder, p->maxorder);
00328     fprintf(stream, "order_locations    = %d\n", p->order_locations != NULL ? 1 : 0);
00329     fprintf(stream, "slit length        = %f\n", p->sg.length);
00330     fprintf(stream, "slit offset        = %f\n", p->sg.offset);
00331 
00332     return;
00333 }
00334 
00335 /*----------------------------------------------------------------------------*/
00342 /*----------------------------------------------------------------------------*/
00343 static
00344 bool illegal_position(const uves_iterate_position *p)
00345 {
00346     return p->ylow < 1 || p->yhigh > p->ny ||
00347     (p->loop_y && p->bpm != NULL &&
00348      p->bpm[(p->x-1) + (p->y-1)*p->nx] != CPL_BINARY_0);
00349 }
00350 

Generated on Fri Apr 18 14:11:42 2008 for UVES Pipeline Reference Manual by  doxygen 1.5.1