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: 2010/09/24 09:32:03 $
00023  * $Revision: 1.10 $
00024  * $Name: uves-5_0_0 $
00025  * $Log: uves_extract_iterate.c,v $
00026  * Revision 1.10  2010/09/24 09:32:03  amodigli
00027  * put back QFITS dependency to fix problem spot by NRI on FIBER mode (with MIDAS calibs) data
00028  *
00029  * Revision 1.8  2010/02/13 12:22:31  amodigli
00030  * removed inlines (let's do work to compiler)
00031  *
00032  * Revision 1.7  2007/06/06 08:17:33  amodigli
00033  * replace tab with 4 spaces
00034  *
00035  * Revision 1.6  2007/05/02 13:17:23  jmlarsen
00036  * Allow specifying offset in optimal extraction
00037  *
00038  * Revision 1.5  2007/02/21 12:44:58  jmlarsen
00039  * uves_iterate_increment: Avoid recursion, fixed bug at beginning of new order (y limits were not recalculated)
00040  *
00041  * Revision 1.4  2006/11/20 08:01:46  jmlarsen
00042  * Changed format of pointer in uves_iterate_dump
00043  *
00044  * Revision 1.3  2006/11/16 09:48:30  jmlarsen
00045  * Renamed data type position -> uves_iterate_position, for namespace reasons
00046  *
00047  * Revision 1.2  2006/09/11 14:19:28  jmlarsen
00048  * Updated documentation
00049  *
00050  * Revision 1.1  2006/09/08 14:04:00  jmlarsen
00051  * Simplified code by using iterators, sky subtraction much optimized
00052  *
00053  *
00054  */
00055 
00056 #ifdef HAVE_CONFIG_H
00057 #  include <config.h>
00058 #endif
00059 
00060 /*----------------------------------------------------------------------------*/
00094 /*----------------------------------------------------------------------------*/
00098 /*-----------------------------------------------------------------------------
00099                                 Includes
00100  -----------------------------------------------------------------------------*/
00101 
00102 #include <uves_extract_iterate.h>
00103 
00104 #include <uves_utils.h>
00105 
00106 #include <cpl.h>
00107 
00108 /*-----------------------------------------------------------------------------
00109                             Functions prototypes
00110  -----------------------------------------------------------------------------*/
00111 static
00112 bool illegal_position(const uves_iterate_position *p);
00113 
00114 /*-----------------------------------------------------------------------------
00115                             Implementation
00116  -----------------------------------------------------------------------------*/
00117 
00118 
00119 /*----------------------------------------------------------------------------*/
00155 /*----------------------------------------------------------------------------*/
00156 uves_iterate_position *
00157 uves_iterate_new(int nx, int ny,
00158          const polynomial *order_locations,
00159          int minorder, int maxorder,
00160          slit_geometry sg)
00161 {
00162     uves_iterate_position *p = cpl_calloc(1, sizeof(uves_iterate_position));
00163 
00164     p->nx = nx;
00165     p->ny = ny;
00166     p->order_locations = order_locations;
00167     p->minorder = minorder;
00168     p->maxorder = maxorder;
00169     p->sg = sg;
00170 
00171     return p;
00172 }
00173 
00174 /*----------------------------------------------------------------------------*/
00179 /*----------------------------------------------------------------------------*/
00180 void
00181 uves_iterate_delete(uves_iterate_position **p)
00182 {
00183     if (p != NULL)
00184     {
00185         cpl_free(*p);
00186         *p = NULL;
00187     }
00188 }
00189 
00190 /*----------------------------------------------------------------------------*/
00204 /*----------------------------------------------------------------------------*/
00205 void
00206 uves_iterate_set_first(uves_iterate_position *p,
00207                int xmin, int xmax, 
00208                int ordermin, int ordermax,
00209                const cpl_binary *bpm,
00210                bool loop_y)
00211 {
00212     /* Limits for this iteration */
00213     p->xmin = xmin;
00214     p->xmax = xmax;
00215     p->ordermax = ordermax;
00216     p->bpm = bpm;
00217     p->loop_y = loop_y;
00218     p->end = false;
00219 
00220     /* Set first postion */
00221     p->x = xmin;
00222     p->order = ordermin;
00223 
00224     p->ycenter = uves_polynomial_evaluate_2d(p->order_locations, p->x, p->order)
00225         + p->sg.offset;
00226     p->yhigh = uves_round_double(p->ycenter + p->sg.length/2);
00227     p->ylow  = uves_round_double(p->ycenter - p->sg.length/2);
00228     if (loop_y) 
00229     {
00230         p->y = p->ylow;
00231     }
00232 
00233     /* Go to first good pixel */
00234     while (illegal_position(p) && !uves_iterate_finished(p))
00235     {
00236         uves_iterate_increment(p);
00237     }
00238 }
00239 
00240 /*----------------------------------------------------------------------------*/
00247 /*----------------------------------------------------------------------------*/
00248 void
00249 uves_iterate_increment(uves_iterate_position *p)
00250 {
00251     do {
00252     if (p->loop_y && p->y < p->yhigh)
00253         {
00254         (p->y)++;
00255         }
00256     else if (p->x < p->xmax)
00257         {
00258         (p->x)++;
00259         
00260         p->ycenter = 
00261                     uves_polynomial_evaluate_2d(p->order_locations, 
00262                                                 p->x, p->order)
00263                     + p->sg.offset;
00264 
00265         p->yhigh = uves_round_double(p->ycenter + p->sg.length/2);
00266         p->ylow  = uves_round_double(p->ycenter - p->sg.length/2);
00267         if (p->loop_y) p->y = p->ylow;
00268         }
00269     else if (p->order < p->ordermax)
00270         {
00271         (p->order)++;
00272         p->x = p->xmin;
00273         
00274         p->ycenter = 
00275                     uves_polynomial_evaluate_2d(p->order_locations,
00276                                                 p->x, p->order)
00277                     + p->sg.offset;
00278 
00279         p->yhigh = uves_round_double(p->ycenter + p->sg.length/2);
00280         p->ylow  = uves_round_double(p->ycenter - p->sg.length/2);
00281         if (p->loop_y) p->y = p->ylow;
00282         }
00283     else
00284         {
00285         p->end = true;
00286         }
00287     } while (illegal_position(p) && !uves_iterate_finished(p));
00288 
00289     return;
00290 }
00291 
00292 /*----------------------------------------------------------------------------*/
00302 /*----------------------------------------------------------------------------*/
00303 bool
00304 uves_iterate_finished(const uves_iterate_position *p)
00305 {
00306     return p->end;
00307 }
00308 
00309 /*----------------------------------------------------------------------------*/
00315 /*----------------------------------------------------------------------------*/
00316 void
00317 uves_iterate_dump(const uves_iterate_position *p, FILE *stream)
00318 {
00319     fprintf(stream, "Position:\n");
00320     fprintf(stream, "order       = %d\n", p->order);
00321     fprintf(stream, "x           = %d\n", p->x);
00322     fprintf(stream, "y           = %d\n", p->y);
00323     fprintf(stream, "ycenter     = %f\n", p->ycenter);
00324     fprintf(stream, "ylow, yhigh = %d, %d\n", p->ylow, p->yhigh);
00325     fprintf(stream, "Limits:\n");
00326     fprintf(stream, "xmin, xmax = %d, %d\n", p->xmin, p->xmax);
00327     fprintf(stream, "ordermax   = %d\n", p->ordermax);
00328     fprintf(stream, "bpm        = %d\n", p->bpm != NULL ? 1 : 0);
00329     fprintf(stream, "loop_y     = %s\n", p->loop_y ? "true" : "false");
00330     fprintf(stream, "end        = %s\n", p->end    ? "true" : "false");
00331     fprintf(stream, "Geometry:\n");
00332     fprintf(stream, "nx, ny             = %d, %d\n", p->nx, p->ny);
00333     fprintf(stream, "minorder, maxorder = %d, %d\n", p->minorder, p->maxorder);
00334     fprintf(stream, "order_locations    = %d\n", p->order_locations != NULL ? 1 : 0);
00335     fprintf(stream, "slit length        = %f\n", p->sg.length);
00336     fprintf(stream, "slit offset        = %f\n", p->sg.offset);
00337 
00338     return;
00339 }
00340 
00341 /*----------------------------------------------------------------------------*/
00348 /*----------------------------------------------------------------------------*/
00349 static
00350 bool illegal_position(const uves_iterate_position *p)
00351 {
00352     return p->ylow < 1 || p->yhigh > p->ny ||
00353     (p->loop_y && p->bpm != NULL &&
00354      p->bpm[(p->x-1) + (p->y-1)*p->nx] != CPL_BINARY_0);
00355 }
00356 

Generated on 9 Mar 2012 for UVES Pipeline Reference Manual by  doxygen 1.6.1