sinfo_dfs.c

00001 /* $Id: sinfo_dfs.c,v 1.14 2007/01/04 12:43:33 amodigli Exp $
00002  *
00003  * This file is part of the SINFONI Pipeline
00004  * Copyright (C) 2002,2003 European Southern Observatory
00005  *
00006  * This program is free software; you can redistribute it and/or modify
00007  * it under the terms of the GNU General Public License as published by
00008  * the Free Software Foundation; either version 2 of the License, or
00009  * (at your option) any later version.
00010  *
00011  * This program is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU General Public License
00017  * along with this program; if not, write to the Free Software
00018  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00019  */
00020 
00021 /*
00022  * $Author: amodigli $
00023  * $Date: 2007/01/04 12:43:33 $
00024  * $Revision: 1.14 $
00025  * $Name:  $
00026  */
00027 
00028 #ifdef HAVE_CONFIG_H
00029 #  include <config.h>
00030 #endif
00031 /*----------------------------------------------------------------------------
00032                                    Macros
00033  ----------------------------------------------------------------------------*/
00034 
00035 
00036 /*----------------------------------------------------------------------------
00037                             Private to this module
00038  ----------------------------------------------------------------------------*/
00039 
00040 
00041 /*-----------------------------------------------------------------------------
00042                                 Includes
00043  ----------------------------------------------------------------------------*/
00044 #include "sinfo_dfs.h"
00045 #include <assert.h>
00046 #include <cpl.h>
00047 #include <math.h>
00048 #include "sinfo_error.h"
00049 #include "sinfo_utilities.h"
00050 #include "sinfo_utils_wrappers.h"
00051 #include "sinfo_msg.h"
00052 #include "sinfo_globals.h"
00053 #include <unistd.h>
00054 #include <stdio.h>
00055 
00056 /*---------------------------------------------------------------------------*/
00062 /*---------------------------------------------------------------------------*/
00063 
00064 /* defines */
00065 
00066 #define FITS_MAGIC_SZ      6
00067 #define SINFO_FIT_AMOEBA_NMAX 5000
00068 
00069 /* function prototypes */
00070 
00071 static inline void 
00072 sinfo_fit_amoeba_get_psum(int ndim, int mpts, double** p, double* psum);
00073 
00074 static  double 
00075 sinfo_fit_amotry(double** p, 
00076              double y[], 
00077              double psum[], 
00078              int ndim, 
00079              double (*funk)(double[]),
00080              int ihi, 
00081              double fac);
00082 
00083 
00084 static double
00085 get_chisq(int N, int D,
00086       int (*f)(const double x[], const double a[], double *result),
00087       const double *a,
00088       const double *x,
00089       const double *y,
00090       const double *sigma);
00091 
00092 static int get_candidate(const double *a, const int ia[],
00093              int M, int N, int D,
00094              double lambda,
00095              int    (*f)(const double x[], const double a[], 
00096                      double *result),
00097              int (*dfda)(const double x[], const double a[], 
00098                      double result[]),
00099              const double *x,
00100              const double *y,
00101              const double *sigma,
00102              double *partials,
00103              cpl_matrix *alpha,
00104              cpl_matrix *beta,
00105              double *a_da);
00106 
00107 
00108 
00109 int 
00110 sinfo_frame_is_raw_dark(char * tag);
00111 
00129 cpl_vector* 
00130 sinfo_vector_clip(const cpl_vector* vinp, 
00131                   const double kappa, 
00132                   const int n, 
00133                   const int method)
00134 {
00135   cpl_vector* vout=NULL;
00136   cpl_vector* vtmp=NULL;
00137   int size=0;
00138   int j=0;
00139   register int i=0;
00140 
00141   double mean=0;
00142   double median=0;
00143   double stdev=0;
00144   double* pt=NULL;
00145   double* po=NULL;
00146 
00147   cknull(vinp,"Null input vector");
00148   check_nomsg(vout=cpl_vector_duplicate(vinp));
00149   check_nomsg(mean=cpl_vector_get_mean(vout));
00150   check_nomsg(median=cpl_vector_get_median(vout));
00151   check_nomsg(stdev=cpl_vector_get_stdev(vout));
00152   check_nomsg(pt=cpl_vector_get_data(vtmp));
00153 
00154   if(method == 0) {  
00155                     /* 
00156                        are rejected
00157                        values ||val-mean|| > kappa*sigma
00158             */
00159     for(j=0;j<n;j++) {
00160 
00161        check_nomsg(cpl_vector_sort(vout,1));  /* sort by increasing data */
00162        check_nomsg(po=cpl_vector_get_data(vout));
00163 
00164        if( (size > 1) && (fabs(po[size-1]-mean) > kappa*stdev)) {
00165 
00166      size--;
00167      check_nomsg(vtmp=cpl_vector_new(size));
00168      check_nomsg(pt=cpl_vector_get_data(vtmp));
00169      for(i=0;i<size;i++) {
00170        pt[i]=po[i];
00171      }
00172          check_nomsg(cpl_vector_delete(vout));
00173          check_nomsg(vout=cpl_vector_duplicate(vtmp));
00174          check_nomsg(cpl_vector_delete(vtmp));
00175 
00176          check_nomsg(mean=cpl_vector_get_mean(vout));
00177          check_nomsg(stdev=cpl_vector_get_stdev(vout));
00178 
00179        }
00180 
00181     }
00182   
00183   } else {
00184                     /* 
00185                        are rejected
00186                        values ||val-median|| > kappa*sigma
00187             */
00188 
00189 
00190     for(j=0;j<n;j++) {
00191 
00192        check_nomsg(cpl_vector_sort(vout,1));  /* sort by increasing data */
00193        check_nomsg(po=cpl_vector_get_data(vout));
00194 
00195        if( (size > 1) && (fabs(po[size-1]-median) > kappa*stdev)) {
00196 
00197      size--;
00198      check_nomsg(vtmp=cpl_vector_new(size));
00199      check_nomsg(pt=cpl_vector_get_data(vtmp));
00200      for(i=0;i<size;i++) {
00201        pt[i]=po[i];
00202      }
00203          check_nomsg(cpl_vector_delete(vout));
00204          check_nomsg(vout=cpl_vector_duplicate(vtmp));
00205          check_nomsg(cpl_vector_delete(vtmp));
00206 
00207          check_nomsg(median=cpl_vector_get_median(vout));
00208          check_nomsg(stdev=cpl_vector_get_stdev(vout));
00209 
00210        }
00211 
00212     }
00213   
00214 
00215 
00216 
00217   }
00218   return vout;
00219  cleanup:
00220   return NULL;
00221 
00222 
00223 }
00224 
00225 
00226 
00227 /*----------------------------------------------------------------------------*/
00261 /*----------------------------------------------------------------------------*/
00262 static int
00263 get_candidate(const double *a, const int ia[],
00264           int M, int N, int D,
00265           double lambda,
00266           int    (*f)(const double x[], const double a[], double *result),
00267           int (*dfda)(const double x[], const double a[], double result[]),
00268           const double *x,
00269           const double *y,
00270           const double *sigma,
00271           double *partials,
00272           cpl_matrix *alpha,
00273           cpl_matrix *beta,
00274           double *a_da)
00275 {
00276     int Mfit = 0;         /* Number of non-constant fit parameters */
00277     cpl_matrix *da;       /* Solution of   alpha * da = beta */
00278     double *alpha_data;
00279     double *beta_data;
00280     double *da_data;
00281     int i, imfit = 0;
00282     int j, jmfit = 0;
00283     int k = 0;
00284 
00285     /* For efficiency, don't check input in this static function */
00286 
00287     Mfit = cpl_matrix_get_nrow(alpha);
00288 
00289     alpha_data    = cpl_matrix_get_data(alpha);
00290     beta_data     = cpl_matrix_get_data(beta);
00291    
00292     /* Build alpha, beta:
00293      *
00294      *  alpha[i,j] = sum_{k=1,N} (sigma_k)^-2 * df/da_i * df/da_j  *
00295      *                           (1 + delta_ij lambda) ,
00296      *
00297      *   beta[i]   = sum_{k=1,N} (sigma_k)^-2 * ( y_k - f(x_k) ) * df/da_i
00298      *
00299      * where (i,j) loop over the non-constant parameters (0 to Mfit-1),
00300      * delta is Kronecker's delta, and all df/da are evaluated in x_k
00301      */
00302 
00303     cpl_matrix_fill(alpha, 0.0);
00304     cpl_matrix_fill(beta , 0.0);
00305 
00306     for (k = 0; k < N; k++)
00307     {
00308         double sm2 = 0.0;                /* (sigma_k)^-2 */
00309         double fx_k = 0.0;               /* f(x_k)       */
00310         const double *x_k = &(x[0+k*D]); /* x_k          */
00311 
00312         if (sigma == NULL)
00313         {
00314             sm2 = 1.0;
00315         }
00316         else
00317         {
00318             sm2 = 1.0 / (sigma[k] * sigma[k]);
00319         }
00320         
00321         /* Evaluate f(x_k) */
00322         cpl_ensure( f(x_k, a, &fx_k) == 0, CPL_ERROR_ILLEGAL_INPUT, -1);
00323 
00324         /* Evaluate (all) df/da (x_k) */
00325         cpl_ensure( dfda(x_k, a, partials) == 0, 
00326             CPL_ERROR_ILLEGAL_INPUT, -1);
00327 
00328         for (i = 0, imfit = 0; i < M; i++)
00329         {
00330             if (ia[i] != 0)
00331             {
00332                 /* Beta */
00333                 beta_data[imfit] +=
00334                 sm2 * (y[k] - fx_k) * partials[i];
00335                 
00336                 /* Alpha is symmetrical, so compute
00337                    only lower-left part */
00338                 for (j = 0, jmfit = 0; j < i; j++)
00339                 {
00340                     if (ia[j] != 0)
00341                     {
00342                         alpha_data[jmfit + imfit*Mfit] +=
00343                         sm2 * partials[i] * 
00344                         partials[j];
00345                         
00346                         jmfit += 1;
00347                     }
00348                 }
00349                 
00350                 /* Alpha, diagonal terms */
00351                 j = i;
00352                 jmfit = imfit;
00353                 
00354                 alpha_data[jmfit + imfit*Mfit] += 
00355                 sm2 * partials[i] *
00356                 partials[j] * (1 + lambda);
00357                 
00358                 imfit += 1;
00359             }
00360         }
00361 
00362         assert( imfit == Mfit );
00363     }
00364 
00365     /* Create upper-right part of alpha */
00366     for (i = 0, imfit = 0; i < M; i++) {
00367        if (ia[i] != 0) {
00368       for (j = i+1, jmfit = imfit+1; j < M; j++) {
00369              if (ia[j] != 0) {
00370             alpha_data[jmfit+imfit*Mfit] = alpha_data[imfit+jmfit*Mfit];
00371         jmfit += 1;
00372          }
00373       }
00374        assert( jmfit == Mfit );
00375        imfit += 1;
00376        }
00377     }
00378     assert( imfit == Mfit );
00379     
00380     da = cpl_matrix_solve(alpha, beta);
00381 
00382     cpl_ensure(da != NULL, cpl_error_get_code(), -1);
00383 
00384     /* Create a+da vector by adding a and da */
00385     da_data   = cpl_matrix_get_data(da);
00386 
00387     for (i = 0, imfit = 0; i < M; i++)
00388     {
00389         if (ia[i] != 0)
00390         {
00391             a_da[i] = a[i] + da_data[0 + imfit*1];
00392             
00393             imfit += 1;
00394         }
00395         else
00396         {
00397             a_da[i] = a[i];
00398         }
00399     }
00400     
00401     assert( imfit == Mfit );
00402 
00403     cpl_matrix_delete(da);
00404 
00405     return 0;
00406 }
00407 
00408 /*----------------------------------------------------------------------------*/
00409 
00410 
00411 /*----------------------------------------------------------------------------*/
00432 /*----------------------------------------------------------------------------*/
00433 
00434 static double
00435 get_chisq(int N, int D,
00436       int (*f)(const double x[], const double a[], double *result),
00437       const double *a,
00438       const double *x,
00439       const double *y,
00440       const double *sigma)
00441 {
00442     double chi_sq;     /* Result */
00443     int i = 0;
00444 
00445     /* For efficiency, don't check input in this static function */
00446     chi_sq = 0.0;
00447     for (i = 0; i < N; i++)
00448     {
00449         double fx_i;
00450         double residual;                 /* Residual in units of uncertainty */
00451         const double *x_i = &(x[0+i*D]);
00452 
00453         /* Evaluate */
00454         cpl_ensure( f(x_i,
00455               a,
00456               &fx_i) == 0, CPL_ERROR_ILLEGAL_INPUT, -1.0);
00457 
00458         /* Accumulate */
00459         if (sigma == NULL)
00460         {
00461             residual = (fx_i - y[i]);
00462         }
00463         else
00464         {
00465             residual = (fx_i - y[i]) / sigma[i];
00466         }
00467 
00468         chi_sq += residual*residual;
00469        
00470     }
00471 
00472     return chi_sq;
00473 }
00474 
00475 
00476 
00477 #ifndef CPL_VECTOR_FIT_MAXITER
00478 #define CPL_VECTOR_FIT_MAXITER 1000
00479 #endif
00480 /*----------------------------------------------------------------------------*/
00547 /*----------------------------------------------------------------------------*/
00548 cpl_error_code
00549 sinfo_fit_lm(const cpl_matrix *x, const cpl_matrix *sigma_x,
00550        const cpl_vector *y, const cpl_vector *sigma_y,
00551        cpl_vector *a, const int ia[],
00552        int    (*f)(const double x[], 
00553                        const double a[], 
00554                        double *result),
00555        int (*dfda)(const double x[], 
00556                        const double a[], 
00557                        double result[]),
00558        double *mse,
00559        double *red_chisq,
00560        cpl_matrix **covariance)
00561 {
00562     const double *x_data     = NULL; /* Pointer to input data                  */
00563     const double *y_data     = NULL; /* Pointer to input data                  */
00564     const double *sigma_data = NULL; /* Pointer to input data                  */
00565     int N    = 0;                    /* Number of data points                  */
00566     int D    = 0;                    /* Dimension of x-points                  */
00567     int M    = 0;                    /* Number of fit parameters               */
00568     int Mfit = 0;                    /* Number of non-constant fit
00569                         parameters                             */
00570 
00571     double lambda    = 0.0;          /* Lambda in L-M algorithm                */
00572     double MAXLAMBDA = 10e40;        /* Parameter to control the graceful exit
00573                     if steepest descent unexpectedly fails */
00574     double chi_sq    = 0.0;          /* Current  chi^2                         */
00575     int count        = 0;            /* Number of successive small improvements
00576                     in chi^2 */
00577     int iterations   = 0;
00578    
00579     cpl_matrix *alpha  = NULL;       /* The MxM ~curvature matrix used in L-M  */
00580     cpl_matrix *beta   = NULL;       /* Mx1 matrix = -.5 grad(chi^2)           */
00581     double *a_data     = NULL;       /* Parameters, a                          */
00582     double *a_da       = NULL;       /* Candidate position a+da                */
00583     double *part       = NULL;       /* The partial derivatives df/da          */
00584     int *ia_local      = NULL;       /* non-NULL version of ia                 */
00585    
00586     /* If covariance computation is requested, then either
00587      * return the covariance matrix or return NULL.
00588      */
00589     if (covariance != NULL) *covariance = NULL;
00590 
00591     /* Validate input */
00592     cpl_ensure_code(x       != NULL, CPL_ERROR_NULL_INPUT);
00593     cpl_ensure_code(sigma_x == NULL, CPL_ERROR_UNSUPPORTED_MODE);
00594     cpl_ensure_code(y       != NULL, CPL_ERROR_NULL_INPUT);
00595     cpl_ensure_code(a       != NULL, CPL_ERROR_NULL_INPUT);
00596     /* ia may be NULL */
00597     cpl_ensure_code(f       != NULL, CPL_ERROR_NULL_INPUT);
00598     cpl_ensure_code(dfda    != NULL, CPL_ERROR_NULL_INPUT);
00599 
00600     /* Chi^2 and covariance computations require sigmas to be known */
00601     cpl_ensure_code( sigma_y != NULL || 
00602                      (red_chisq == NULL && covariance == NULL),
00603              CPL_ERROR_INCOMPATIBLE_INPUT);
00604 
00605     D = cpl_matrix_get_ncol(x);
00606     N = cpl_matrix_get_nrow(x);
00607     M = cpl_vector_get_size(a);
00608     cpl_ensure_code(N > 0 && D > 0 && M > 0, CPL_ERROR_ILLEGAL_INPUT);
00609 
00610     cpl_ensure_code( cpl_vector_get_size(y) == N,
00611              CPL_ERROR_INCOMPATIBLE_INPUT);
00612 
00613     x_data = cpl_matrix_get_data(x);
00614     y_data = cpl_vector_get_data(y);
00615     a_data = cpl_vector_get_data(a);
00616 
00617     if (sigma_y != NULL)
00618     {
00619         cpl_ensure_code( cpl_vector_get_size(sigma_y) == N,
00620                  CPL_ERROR_INCOMPATIBLE_INPUT);
00621         /* Sigmas must be positive */
00622         cpl_ensure_code( cpl_vector_get_min (sigma_y) > 0,
00623                  CPL_ERROR_ILLEGAL_INPUT);
00624         sigma_data = cpl_vector_get_data(sigma_y);
00625     }
00626 
00627     ia_local = cpl_malloc(M * sizeof(int));
00628     cpl_ensure_code(ia_local != NULL, CPL_ERROR_ILLEGAL_OUTPUT);
00629 
00630     /* Count non-constant fit parameters, copy ia */
00631     if (ia != NULL)
00632     {
00633         int i;
00634 
00635         Mfit = 0;
00636         for (i = 0; i < M; i++)
00637         {
00638             ia_local[i] = ia[i];
00639 
00640             if (ia[i] != 0) 
00641             {
00642                 Mfit += 1;
00643             }
00644         }
00645         
00646         if (! (Mfit > 0))
00647         {
00648             cpl_free(ia_local);
00649             cpl_ensure_code( CPL_FALSE,
00650                      CPL_ERROR_ILLEGAL_INPUT);
00651         }
00652     }
00653     else
00654     {
00655         /* All parameters participate */
00656         int i;
00657         
00658         Mfit = M;
00659         
00660         for (i = 0; i < M; i++)
00661         {
00662             ia_local[i] = 1;
00663         }
00664     }
00665 
00666     /* To compute reduced chi^2, we need N > Mfit */
00667     if (! ( red_chisq == NULL || N > Mfit ) )
00668     {
00669         cpl_free(ia_local);
00670         cpl_ensure_code(
00671         CPL_FALSE,
00672         CPL_ERROR_ILLEGAL_INPUT);
00673     }
00674 
00675     /* Create alpha, beta, a_da, part  work space */
00676     alpha = cpl_matrix_new(Mfit, Mfit);
00677     if (alpha == NULL)
00678     {
00679         cpl_free(ia_local);
00680         cpl_ensure_code(
00681         CPL_FALSE,
00682         CPL_ERROR_ILLEGAL_OUTPUT);
00683     }
00684 
00685     beta = cpl_matrix_new(Mfit, 1);
00686     if (beta == NULL)
00687     {
00688         cpl_free(ia_local);
00689         cpl_matrix_delete(alpha);
00690         cpl_ensure_code(
00691         CPL_FALSE,
00692         CPL_ERROR_ILLEGAL_OUTPUT);
00693     }
00694 
00695     a_da = cpl_malloc(M * sizeof(double));
00696     if (a_da == NULL)
00697     {
00698         cpl_free(ia_local);
00699         cpl_matrix_delete(alpha);
00700         cpl_matrix_delete(beta);
00701         cpl_ensure_code(
00702         CPL_FALSE,
00703         CPL_ERROR_ILLEGAL_OUTPUT);
00704     }
00705 
00706     part = cpl_malloc(M * sizeof(double));
00707     if (part == NULL)
00708     {
00709         cpl_free(ia_local);
00710         cpl_matrix_delete(alpha);
00711         cpl_matrix_delete(beta);
00712         cpl_free(a_da);
00713         cpl_ensure_code(
00714         CPL_FALSE,
00715         CPL_ERROR_ILLEGAL_OUTPUT);
00716     }
00717  
00718     /* Initialize loop variables */
00719     lambda = 0.001;
00720     count = 0;
00721     iterations = 0;
00722     if( (chi_sq = get_chisq(N, D, f, a_data, x_data, y_data, sigma_data)) < 0)
00723     {
00724         cpl_free(ia_local);
00725         cpl_matrix_delete(alpha);
00726         cpl_matrix_delete(beta);
00727         cpl_free(a_da);
00728         cpl_free(part);
00729         cpl_ensure_code(
00730         CPL_FALSE,
00731         cpl_error_get_code());
00732     }
00733 
00734     /* uves_msg_debug("Initial chi^2 = %f", chi_sq); */
00735 
00736     /* Iterate until chi^2 didn't improve substantially many (say, 5)
00737        times in a row */
00738     while (count < 5 && 
00739            lambda < MAXLAMBDA && 
00740            iterations < CPL_VECTOR_FIT_MAXITER)
00741     {
00742         /* In each iteration lambda increases, or chi^2 decreases or
00743            count increases. Because chi^2 is bounded from below
00744            (and lambda and count from above), the loop will terminate */
00745 
00746         double chi_sq_candidate = 0.0;
00747         int returncode = 0;
00748 
00749         /* Get candidate position in parameter space = a+da,
00750          * where  alpha * da = beta .
00751          * Increase lambda until alpha is non-singular
00752          */
00753        
00754         while( (returncode = get_candidate(a_data, ia_local,
00755                            M, N, D,
00756                            lambda, f, dfda,
00757                            x_data, y_data, sigma_data,
00758                            part, alpha, beta, a_da)
00759                ) != 0
00760            && cpl_error_get_code() == CPL_ERROR_SINGULAR_MATRIX
00761            && lambda < MAXLAMBDA)
00762         {
00763             /* uves_msg_debug("Singular matrix. lambda = %e", lambda); */
00764             cpl_error_reset();
00765             lambda *= 9.0;
00766         }
00767        
00768         /* Set error if lambda diverged */
00769         if ( !( lambda < MAXLAMBDA ) )
00770         {
00771             cpl_free(ia_local);
00772             cpl_matrix_delete(alpha);
00773             cpl_matrix_delete(beta);
00774             cpl_free(a_da);
00775             cpl_free(part);
00776             cpl_ensure_code(
00777             CPL_FALSE,
00778             CPL_ERROR_CONTINUE);
00779         }
00780        
00781         if (returncode != 0)
00782         {
00783             cpl_free(ia_local);
00784             cpl_matrix_delete(alpha);
00785             cpl_matrix_delete(beta);
00786             cpl_free(a_da);
00787             cpl_free(part);
00788             cpl_ensure_code(
00789             CPL_FALSE,
00790             cpl_error_get_code());
00791         }
00792 
00793         /* Get chi^2(a+da) */
00794         if ((chi_sq_candidate = get_chisq(N, D, f, a_da,
00795                           x_data, y_data, sigma_data)) < 0)
00796         {
00797             cpl_free(ia_local);
00798             cpl_matrix_delete(alpha);
00799             cpl_matrix_delete(beta);
00800             cpl_free(a_da);
00801             cpl_free(part);
00802             cpl_ensure_code(
00803             CPL_FALSE,
00804             cpl_error_get_code());
00805         }
00806 
00807         /* uves_msg_debug("Chi^2 = %f  Candidate = %f  Lambda = %e",
00808            chi_sq, chi_sq_candidate, lambda);  */
00809 
00810         if (chi_sq_candidate > chi_sq)
00811         {
00812             /* Move towards steepest descent */
00813             lambda *= 9.0;
00814         }
00815         else
00816         {
00817             /* Move towards Newton's algorithm */
00818             lambda /= 10.0;
00819            
00820             /* Count the number of successive improvements in chi^2 of
00821                less than 0.01 relative */
00822             if ( chi_sq == 0 ||
00823              (chi_sq - chi_sq_candidate)/chi_sq < .01)
00824             {
00825                 count += 1;
00826             }
00827             else
00828             {
00829                 /* Chi^2 improved by a significant amount,
00830                    reset counter */
00831                 count = 0;
00832             }
00833 
00834             /* chi^2 improved, update a and chi^2 */
00835             {
00836             int i;
00837             for (i = 0; i < M; i++) a_data[i] = a_da[i];
00838             }
00839             chi_sq = chi_sq_candidate;
00840         }
00841         iterations++;
00842     }
00843 
00844     /* Set error if we didn't converge */
00845     if ( !( lambda < MAXLAMBDA && iterations < CPL_VECTOR_FIT_MAXITER ) )
00846     {
00847         cpl_free(ia_local);
00848         cpl_matrix_delete(alpha);
00849         cpl_matrix_delete(beta);
00850         cpl_free(a_da);
00851         cpl_free(part);
00852         cpl_ensure_code(
00853         CPL_FALSE,
00854         CPL_ERROR_CONTINUE);
00855     }
00856 
00857     /* Compute mse if requested */
00858     if (mse != NULL)
00859     {
00860         int i;
00861 
00862         *mse = 0.0;
00863        
00864         for(i = 0; i < N; i++)
00865         {
00866             double fx_i = 0.0;
00867             double residual = 0.0;
00868            
00869             /* Evaluate f(x_i) at the best fit parameters */
00870             if( f(&(x_data[i*D]),
00871               a_data,
00872               &fx_i) != 0)
00873             {
00874                 cpl_free(ia_local);
00875                 cpl_matrix_delete(alpha);
00876                 cpl_matrix_delete(beta);
00877                 cpl_free(a_da);
00878                 cpl_free(part);
00879                 cpl_ensure_code(
00880                 CPL_FALSE,
00881                 CPL_ERROR_ILLEGAL_INPUT);
00882             }
00883 
00884             residual = y_data[i] - fx_i;
00885             *mse += residual * residual;
00886         }
00887         *mse /= N;
00888     }
00889 
00890     /* Compute reduced chi^2 if requested */
00891     if (red_chisq != NULL)
00892     {
00893         /* We already know the optimal chi^2 (and that N > Mfit)*/
00894         *red_chisq = chi_sq / (N-Mfit);
00895     }
00896 
00897     /* Compute covariance matrix if requested
00898      * cov = alpha(lambda=0)^-1              
00899      */
00900     if (covariance != NULL)
00901     {
00902         cpl_matrix *cov;
00903 
00904         if( get_candidate(a_data, ia_local, 
00905                   M, N, D, 0.0, f, dfda, 
00906                   x_data, y_data, sigma_data,
00907                   part, alpha, beta, a_da)
00908         != 0)
00909         {
00910             cpl_free(ia_local);
00911             cpl_matrix_delete(alpha);
00912             cpl_matrix_delete(beta);
00913             cpl_free(a_da);
00914             cpl_free(part);
00915             cpl_ensure_code(
00916             CPL_FALSE,
00917             cpl_error_get_code());
00918         }
00919        
00920         cov = cpl_matrix_invert_create(alpha);
00921         if (cov == NULL)
00922         {
00923             cpl_free(ia_local);
00924             cpl_matrix_delete(alpha);
00925             cpl_matrix_delete(beta);
00926             cpl_free(a_da);
00927             cpl_free(part);
00928             cpl_ensure_code(
00929             CPL_FALSE,
00930             cpl_error_get_code());
00931         }
00932        
00933         /* Make sure that variances are positive */
00934         {
00935         int i;
00936         for (i = 0; i < Mfit; i++)
00937             {
00938             if ( !(cpl_matrix_get(cov, i, i) > 0) )
00939                 {
00940                 cpl_free(ia_local);
00941                 cpl_matrix_delete(alpha);
00942                 cpl_matrix_delete(beta);
00943                 cpl_free(a_da);
00944                 cpl_free(part);
00945                 cpl_matrix_delete(cov);
00946                 *covariance = NULL;
00947                 cpl_ensure_code(
00948                     CPL_FALSE,
00949                     CPL_ERROR_SINGULAR_MATRIX);
00950                 }
00951             }
00952         }
00953 
00954         /* Expand covariance matrix from Mfit x Mfit
00955            to M x M. Set rows/columns corresponding to fixed
00956            parameters to zero */
00957 
00958         *covariance = cpl_matrix_new(M, M);
00959         if (*covariance == NULL)
00960         {
00961             cpl_free(ia_local);
00962             cpl_matrix_delete(alpha);
00963             cpl_matrix_delete(beta);
00964             cpl_free(a_da);
00965             cpl_free(part);
00966             cpl_matrix_delete(cov);
00967             cpl_ensure_code(
00968             CPL_FALSE,
00969             CPL_ERROR_ILLEGAL_OUTPUT);
00970         }
00971 
00972         {
00973         int j, jmfit;
00974 
00975         for (j = 0, jmfit = 0; j < M; j++)
00976             if (ia_local[j] != 0)
00977             {
00978                 int i, imfit;
00979 
00980                 for (i = 0, imfit = 0; i < M; i++)
00981                 if (ia_local[i] != 0)
00982                     {
00983                     cpl_matrix_set(*covariance, i, j,
00984                                cpl_matrix_get(
00985                                cov, imfit, jmfit));
00986                     imfit += 1;
00987                     }
00988                 
00989                 assert( imfit == Mfit );
00990 
00991                 jmfit += 1;
00992             }
00993         
00994         assert( jmfit == Mfit );
00995         }
00996 
00997         cpl_matrix_delete(cov);
00998     }
00999 
01000     cpl_free(ia_local);
01001     cpl_matrix_delete(alpha);
01002     cpl_matrix_delete(beta);
01003     cpl_free(a_da);
01004     cpl_free(part);
01005    
01006     return CPL_ERROR_NONE;
01007 }
01008 
01009 
01010 
01011 
01012 
01013 
01014 
01015 
01016 
01017 
01018 
01019 
01020 
01021 
01022 
01023 /*-------------------------------------------------------------------------*/
01024 
01036 static inline void 
01037 sinfo_fit_amoeba_get_psum(int ndim, int mpts, double** p, double* psum)
01038 {
01039   int i=0;
01040   int j=0;
01041   double sum=0;
01042   for (j=0;j<ndim;j++) {
01043     for (sum=0.0,i=0;i<mpts;i++) {
01044       sum += p[i][j];
01045     }
01046     psum[j]=sum;
01047   }
01048 
01049 } 
01050 
01051 
01052 #define SINFO_FIT_AMOEBA_SWAP(a,b) {swap=(a);(a)=(b);(b)=swap;}
01053 
01054 
01055 
01082 void 
01083 sinfo_fit_amoeba(
01084          double**p, 
01085          double y[], 
01086          int ndim,
01087          double ftol,
01088          double (*funk)(double[]),
01089          int* nfunk)
01090 {
01091 
01092 
01093   int i=0;
01094   int ihi=0;
01095   int ilo=0;
01096   int inhi=0;
01097   int j=0;
01098   int mpts=ndim+1;
01099   double rtol=0;
01100   double swap=0;
01101   double ysave=0;
01102   double ytry=0;
01103   cpl_vector* sum=NULL;
01104   double* psum=NULL;
01105 
01106   sum=cpl_vector_new(ndim);
01107   psum=cpl_vector_get_data(sum);
01108   *nfunk=0;
01109 
01110   sinfo_fit_amoeba_get_psum(ndim,mpts,p,psum);
01111 
01112   for(;;) {
01113     ilo=0;
01114     /*
01115     First we must determine which point is the highest (worst), 
01116     next-highest, and lowest (best), by looping over the points in the simplex
01117     */
01118     ihi=y[0]>y[1] ? (inhi=1,0) : (inhi=0,1);
01119 
01120     for (i=0;i< mpts;i++) {
01121       if (y[i] <= y[ilo]) ilo=i;
01122       if (y[i] > y[ihi]) {
01123     inhi=ihi;
01124     ihi=i;
01125       } else if (y[i] > y[inhi] && i != ihi) inhi=i;
01126     }
01127     rtol=2.0*fabs(y[ihi]-y[ilo])/(fabs(y[ihi])+fabs(y[ilo]));
01128 
01129     /*
01130     compute the fractional range from highest to lowest and return if 
01131     satisfactory
01132     */
01133     if(rtol < ftol) { // if returning, but best point and value is in slot 1
01134       SINFO_FIT_AMOEBA_SWAP(y[0],y[ilo]);
01135       for (i=0;i<ndim;i++) SINFO_FIT_AMOEBA_SWAP(p[1][i],p[ilo][i]);
01136       break;
01137     }
01138     if (*nfunk >= SINFO_FIT_AMOEBA_NMAX) {
01139          sinfo_msg_error("NMAX exceeded");
01140          SINFO_FIT_AMOEBA_SWAP(y[0],y[ilo]);
01141          for (i=0;i<ndim;i++) SINFO_FIT_AMOEBA_SWAP(p[1][i],p[ilo][i]);
01142      for (i=0;i<ndim;i++) {
01143            sinfo_msg("p[1][i]=%g p[ilo][i]=%g ilo=%d",p[1][i],p[ilo][i],ilo);
01144      }
01145          assure(*nfunk >= SINFO_FIT_AMOEBA_NMAX,CPL_ERROR_UNSPECIFIED,
01146                 "NMAX exceeded");
01147          break;
01148 
01149     }
01150     *nfunk +=2;
01151     /*
01152     Begin a new iteration. First extrapolate by a Factor -1 through the face
01153     of the simplex across the high point, i.e. reflect the simplex from the 
01154     high point
01155     */
01156     ytry=sinfo_fit_amotry(p,y,psum,ndim,funk,ihi,-1.0);
01157     if(ytry <= y[ilo]) {
01158       /* 
01159       Gives a result better than the best point, so try an additional 
01160       extrapolation by a factor 2 
01161       */
01162       ytry=sinfo_fit_amotry(p,y,psum,ndim,funk,ihi,2.0);
01163     } else if (ytry >= y[inhi]) {
01164 
01165       /*
01166       The reflected point is worse than the second highest, so look for an 
01167       intermediate lower point, i.e. do a one-dimensional contraction
01168       */
01169       ysave=y[ihi];
01170       ytry=sinfo_fit_amotry(p,y,psum,ndim,funk,ihi,0.5);
01171       if(ytry >= ysave) { 
01172        /* 
01173           Can't seem to get rid of that high point.
01174           Better contract around the lowest (best) point
01175        */
01176     for(i=0;i<mpts;i++) {
01177       if(i != ilo) {
01178         for( j=0;j<ndim;j++) {
01179           p[i][j]=psum[j]=0.5*(p[i][j]+p[ilo][j]);
01180         }
01181         y[i]=(*funk)(psum);
01182       }
01183     }
01184     *nfunk += ndim; /* Keep track of function evaluations */
01185         sinfo_fit_amoeba_get_psum(ndim,mpts,p,psum);/* Recomputes psum */
01186       }
01187     } else {
01188       --(*nfunk); /* Go back for the test of doneness and the next iteration */
01189     }
01190   }
01191  cleanup:
01192   cpl_vector_delete(sum);
01193 }
01194 
01195 
01196 static  double 
01197 sinfo_fit_amotry(double** p, double y[], double psum[], int ndim, 
01198              double (*funk)(double[]),int ihi, double fac)
01199 {
01200   int j;
01201   double fac1=0;
01202   double fac2=0;
01203   double ytry=0;
01204   cpl_vector * vtry=NULL;
01205   double *ptry=NULL;
01206 
01207   vtry=cpl_vector_new(ndim);
01208   ptry=cpl_vector_get_data(vtry);
01209 
01210   fac1=(1.0-fac)/ndim;
01211   fac2=fac1-fac;
01212         
01213   for (j=0;j<ndim;j++) {
01214     ptry[j]=psum[j]*fac1-p[ihi][j]*fac2;
01215   }
01216   ytry=(*funk)(ptry);
01217   if (ytry < y[ihi]) {
01218     y[ihi]=ytry;
01219     for (j=0;j<ndim;j++) {
01220       psum[j] += ptry[j]-p[ihi][j];
01221       p[ihi][j]=ptry[j];
01222     }
01223   }
01224   sinfoni_free_vector(&vtry);
01225   return ytry;
01226 }
01227 
01228 /*-------------------------------------------------------------------------*/
01229 /*-------------------------------------------------------------------------*/
01239 /*--------------------------------------------------------------------------*/
01240 
01241 int sinfo_vector_dindgen(cpl_vector** v)
01242 {
01243 
01244   int sz=0;
01245   int i=0;
01246 
01247   cknull(*v,"Null input vector");
01248   check(sz=cpl_vector_get_size(*v),"Getting size of a vector");
01249 
01250   for(i=0;i<sz;i++) {
01251     cpl_vector_set(*v,i,(double)i);
01252   }
01253 
01254   return 0;
01255  cleanup:
01256   return -1;
01257 
01258 }
01259 
01260 /*-------------------------------------------------------------------------*/
01271 /*--------------------------------------------------------------------------*/
01272 
01273 int sinfo_is_fits_file(const char *filename)
01274 {
01275     FILE    *fp ;
01276     char    *magic ;
01277     int     isfits ;
01278 
01279     if ((fp = fopen(filename, "r"))==NULL) {
01280         sinfo_msg_error("cannot open file [%s]", filename) ;
01281         return -1 ;
01282     }
01283 
01284     magic = cpl_calloc(FITS_MAGIC_SZ+1, sizeof(char)) ;
01285     (void)fread(magic, 1, FITS_MAGIC_SZ, fp) ;
01286     (void)fclose(fp) ;
01287     magic[FITS_MAGIC_SZ] = (char)0 ;
01288     if (strstr(magic, "SIMPLE")!=NULL)
01289         isfits = 1 ;
01290     else
01291         isfits = 0 ;
01292     cpl_free(magic) ;
01293     return isfits ;
01294 }
01295 
01296 /*---------------------------------------------------------------------------*/
01302 /*---------------------------------------------------------------------------*/
01303 cpl_error_code
01304 sinfo_table_correl(cpl_table * t1, cpl_table* t2, cpl_table* range,double* xcor)
01305 {
01306 
01307   double wsr=0;
01308   double wer=0;
01309   int nr=0;
01310   int i=0;
01311   int status=0;
01312   int nrows=0;
01313   double mean=0;
01314   double prod=0;
01315 
01316   cpl_table* tmp_t1=NULL;
01317   cpl_table* tmp_t2=NULL;
01318 
01319   check_nomsg(nr=cpl_table_get_nrow(range));
01320   for(i=0;i<nr;i++) {
01321     wsr=cpl_table_get_double(range,"WSTART",i,&status);
01322     wer=cpl_table_get_double(range,"WEND",i,&status);
01323     cpl_table_and_selected_double(t1,"WAVE",CPL_NOT_LESS_THAN,wsr);
01324     cpl_table_and_selected_double(t1,"WAVE",CPL_NOT_GREATER_THAN,wer);
01325     tmp_t1=cpl_table_extract_selected(t1);
01326     cpl_table_and_selected_double(t2,"WAVE",CPL_NOT_LESS_THAN,wsr);
01327     cpl_table_and_selected_double(t2,"WAVE",CPL_NOT_GREATER_THAN,wer);
01328     tmp_t2=cpl_table_extract_selected(t2);
01329     cpl_table_duplicate_column(tmp_t1,"INT1",tmp_t1,"INT");
01330     cpl_table_duplicate_column(tmp_t1,"INT2",tmp_t2,"INT");
01331     cpl_table_multiply_columns(tmp_t1,"INT1","INT2");
01332     mean=cpl_table_get_column_mean(tmp_t1,"INT1");
01333     nrows=cpl_table_get_nrow(tmp_t1);
01334     prod=mean*nrows;
01335     *xcor+=prod;
01336   }
01337 
01338  cleanup:
01339     return cpl_error_get_code();
01340 }
01346 /*----------------------------------------------------------------------------*/
01347 cpl_error_code
01348 sinfo_frameset_merge(cpl_frameset * set1, cpl_frameset* set2)
01349 {
01350 
01351   cpl_frame* frm_tmp=NULL;
01352   cpl_frame* frm_dup=NULL;
01353 
01354   passure(set1 != NULL, "Wrong input set");
01355   
01356   check_nomsg(frm_tmp = cpl_frameset_get_first(set2));
01357   while (frm_tmp != NULL)
01358     {
01359       frm_dup=cpl_frame_duplicate(frm_tmp);
01360       cpl_frameset_insert(set1,frm_dup);
01361       frm_tmp = cpl_frameset_get_next(set2);
01362     }
01363 
01364   cleanup:
01365     return cpl_error_get_code();
01366 }
01367 
01368 /*----------------------------------------------------------------------------*/
01376 /*----------------------------------------------------------------------------*/
01377 
01378 cpl_error_code
01379 sinfo_extract_frames_group_type(const cpl_frameset * set, 
01380                                 cpl_frameset** ext, 
01381                                 cpl_frame_group type)
01382 {
01383   cpl_frame* frm_tmp=NULL;
01384   cpl_frame* frm_dup=NULL;
01385   cpl_frame_group g;
01386 
01387   check_nomsg(*ext = cpl_frameset_new());
01388   check_nomsg(frm_tmp = cpl_frameset_get_first(set));
01389   while (frm_tmp != NULL)
01390     {
01391       g=cpl_frame_get_group(frm_tmp);
01392       if(g == type) {
01393     frm_dup=cpl_frame_duplicate(frm_tmp);
01394         cpl_frameset_insert(*ext,frm_dup);
01395         sinfo_msg_debug("group %d insert file %s ",
01396                          type,cpl_frame_get_filename(frm_dup));
01397       }
01398       frm_tmp = cpl_frameset_get_next(set);
01399     }
01400 
01401   cleanup:
01402     return cpl_error_get_code();
01403 }
01404 
01405 
01406 
01407 
01418 int 
01419 sinfo_get_pupil_shift(cpl_imagelist* iml,const int n,cpl_table** qclog_tbl)
01420 {
01421   int max_ima_x=0;
01422   int max_ima_y=0;
01423   int nx=0;
01424   int ny=0;
01425 
01426   double xshift=0;
01427   double yshift=0;
01428 
01429   double max_ima_cx=0;
01430   double max_ima_cy=0;
01431  
01432   cpl_image* img=NULL;
01433   cpl_image* img_dup=NULL;
01434 
01435   char key_name[FILE_NAME_SZ];
01436 
01437   img=cpl_imagelist_collapse_median_create(iml);
01438   nx=cpl_image_get_size_x(img);
01439   ny=cpl_image_get_size_y(img);
01440 
01441   img_dup=cpl_image_duplicate(img);
01442   sinfo_clean_nan(&img_dup);
01443   cpl_image_get_maxpos(img_dup,&max_ima_x,&max_ima_y);
01444   max_ima_cx=cpl_image_get_centroid_x_window(img_dup,1,1,nx,ny);
01445   max_ima_cy=cpl_image_get_centroid_y_window(img_dup,1,1,nx,ny);
01446 
01447   cpl_image_delete(img_dup);
01448 
01449 
01450   xshift=max_ima_cx-(double)nx/2;
01451   yshift=max_ima_cy-(double)ny/2;
01452 
01453   snprintf(key_name,MAX_NAME_SIZE-1,"%s%d%s","QC PUPIL",n," SHIFTX");
01454   sinfo_qclog_add_double(*qclog_tbl,key_name,xshift,
01455                          "X shift centroid - center image","%f");
01456 
01457   snprintf(key_name,MAX_NAME_SIZE-1,"%s%d%s","QC PUPIL",n," SHIFTY");
01458   sinfo_qclog_add_double(*qclog_tbl,key_name,yshift,
01459                           "Y shift centroid - center image","%f");
01460   cpl_image_delete(img);
01461  
01462   return 0;
01463 }
01464 
01465 
01466 
01473 int sinfo_get_strehl_type(cpl_frameset* sof)
01474 {
01475   int strehl_sw=0;
01476   int nobs=0;
01477   int i=0;
01478   cpl_frameset* obs=NULL;
01479 
01480   cpl_frame* frame=NULL;
01481   float* pix_scale=NULL;
01482   cpl_propertylist* plist=NULL;
01483 
01484   obs = cpl_frameset_new();
01485 
01486   sinfo_contains_frames_kind(sof,obs,PRO_OBS_PSF);
01487  
01488   nobs=cpl_frameset_get_size(obs);
01489   if (nobs < 1) {
01490      sinfo_contains_frames_kind(sof,obs,PRO_OBS_STD);
01491      nobs=cpl_frameset_get_size(obs);
01492   }
01493 
01494   nobs=cpl_frameset_get_size(obs);
01495 
01496   if (nobs < 1) {
01497     return 0;
01498   } else {
01499     pix_scale=cpl_calloc(nobs,sizeof(float));
01500     for(i=0;i<nobs;i++) {
01501       frame=cpl_frameset_get_frame(obs,i);
01502       plist=cpl_propertylist_load(cpl_frame_get_filename(frame),0);
01503       pix_scale[i]=sinfo_pfits_get_pixscale(plist);
01504       cpl_propertylist_delete(plist);
01505     }
01506     if(sinfo_pix_scale_isnot_const(pix_scale,nobs)) {
01507       strehl_sw=1;
01508     }
01509     cpl_free(pix_scale);
01510   }
01511   cpl_frameset_delete(obs);
01512 
01513   return strehl_sw;
01514 
01515 }
01516 
01517 
01518 
01525 double sinfo_get_wave_cent(const char* band)
01526 {
01527   double lam=0.;
01528  if (strcmp(band,"H+K") == 0) {
01529    lam=1.950;
01530  } else if (strcmp(band,"K") == 0) {
01531    lam=2.175;
01532  } else if (strcmp(band,"J") == 0) {
01533    lam=1.225;
01534  } else if (strcmp(band,"H") == 0) { 
01535    lam=1.675;
01536  }
01537 return lam;
01538 
01539 }
01540 
01541 
01542 
01551 int sinfo_pix_scale_isnot_const(float* pix_scale, const int n) {
01552   int i=0;
01553   float eps=0.0001;
01554   float ref=pix_scale[0];
01555 
01556   for(i=1;i<n;i++) {
01557     if(fabs(pix_scale[i]-ref) > eps) return 1;
01558   }
01559   return 0;
01560 }
01561 
01562 
01570 const char* sinfo_get_pix_scale(float ps) {
01571 
01572   const char* key_value;
01573   float eps=0.0001;
01574 
01575   if(fabs(ps - 0.025) < eps) {
01576     key_value="0.025";
01577   }
01578   else if (fabs(ps - 0.1) < eps) {
01579     key_value="0.1";
01580   }
01581   else if (fabs(ps - 0.25) < eps) {
01582     key_value="0.25";
01583   }
01584   else if (fabs(ps - 1.0) < eps) {
01585     key_value="pupil";
01586   } else {
01587     sinfo_msg_error("ps=%f. Failed to set pixel scale",ps);
01588     return NULL;
01589   }
01590 
01591   return key_value;
01592 }
01593 
01594 
01610 int  sinfo_get_clean_mean_window(cpl_image* img, 
01611                                    int llx, int lly, int urx, int ury, 
01612                                    const int kappa, const int nclip, 
01613                                    double* local_clean_mean, 
01614                                    double* clean_stdev)
01615 {
01616 
01617   double mean=0;
01618   double stdev=0;
01619   double threshold=0;
01620   double lo_cut=0;
01621   double hi_cut=0;
01622   cpl_mask* mask=NULL;
01623   cpl_image* tmp=NULL;
01624   cpl_stats* stats=NULL;
01625   int i=0;
01626   
01627   tmp=cpl_image_extract(img,llx,lly,urx,ury);
01628   cpl_image_accept_all(tmp);
01629   for(i=0;i<nclip;i++) {
01630    
01631 
01632     cpl_stats_delete(stats);
01633     stats = cpl_stats_new_from_image(tmp, CPL_STATS_MEAN | CPL_STATS_STDEV);
01634     mean = cpl_stats_get_mean(stats);
01635     stdev = cpl_stats_get_stdev(stats);
01636 
01637     threshold=kappa*stdev;
01638     lo_cut=mean-threshold;
01639     hi_cut=mean+threshold;
01640 
01641     cpl_image_accept_all(tmp);
01642     mask=cpl_mask_threshold_image_create(tmp,lo_cut,hi_cut);
01643 
01644     cpl_mask_not(mask);
01645     cpl_image_reject_from_mask(tmp,mask);
01646     cpl_mask_delete(mask);
01647 
01648 
01649   }
01650   *local_clean_mean=mean;
01651   *clean_stdev=stdev;
01652   cpl_image_delete(tmp);
01653   cpl_stats_delete(stats);
01654 
01655  
01656   return 0;
01657 
01658 
01659 }
01665 int sinfo_check_rec_status(const int val) {
01666    if(cpl_error_get_code() != CPL_ERROR_NONE) {
01667       sinfo_msg_error("error before %d",val);
01668       sinfo_msg_error((char* ) cpl_error_get_message());
01669       sinfo_msg_error((char* ) cpl_error_get_where());
01670       return -1;
01671     }
01672     return 0;
01673 }
01674 
01681 int
01682 sinfo_clean_nan(cpl_image** im)
01683 {
01684   int i=0;
01685   int j=0;
01686   int nx=0;
01687   int ny=0;
01688   float* data=NULL;
01689 
01690   nx=cpl_image_get_size_x(*im);
01691   ny=cpl_image_get_size_y(*im);
01692   data=cpl_image_get_data_float(*im);
01693 
01694   for(j=0;j<ny;j++) {
01695   for(i=0;i<nx;i++) {
01696     if(isnan(data[j*nx+i]) != 0) {
01697       data[j*nx+i] = 0;
01698     } 
01699   }
01700   }
01701   return 0;
01702 }
01703 
01713 void
01714 sinfo_add_pro_fits_key(cpl_propertylist * plist,  
01715                        char* pro_catg, 
01716                        char* file_name, 
01717                        char* out_name)
01718 {
01719 
01720   char* date=NULL;
01721   date     = sinfo_get_datetime_iso8601() ;
01722 
01723   cpl_propertylist_insert_after_string(plist, "EXPTIME", 
01724                                        KEY_NAME_PIPEFILE, out_name) ;
01725   cpl_propertylist_set_comment(plist, KEY_NAME_PIPEFILE,KEY_HELP_PIPEFILE) ;
01726 
01727   cpl_propertylist_insert_after_string(plist, "EXPTIME", 
01728                                        KEY_NAME_HPRO_DID, KEY_VALUE_HPRO_DID) ;
01729   cpl_propertylist_set_comment(plist, KEY_NAME_HPRO_DID,KEY_HELP_HPRO_DID) ;
01730 
01731   cpl_propertylist_insert_after_string(plist, "EXPTIME", 
01732                                        KEY_NAME_HPRO_TYPE, "REDUCED") ;
01733   cpl_propertylist_set_comment(plist, KEY_NAME_HPRO_TYPE, KEY_HELP_HPRO_TYPE) ;
01734 
01735   cpl_propertylist_insert_after_string(plist, "EXPTIME", 
01736                                        KEY_NAME_HPRO_CATG, pro_catg) ;
01737   cpl_propertylist_set_comment(plist, KEY_NAME_HPRO_CATG,  KEY_HELP_HPRO_CATG);
01738 
01739   cpl_propertylist_insert_after_string(plist, "EXPTIME", 
01740                                        KEY_NAME_HPRO_STATUS, "OK") ;
01741   cpl_propertylist_set_comment(plist, KEY_NAME_HPRO_STATUS,KEY_HELP_HPRO_CATG);
01742 
01743   cpl_propertylist_insert_after_string(plist, "EXPTIME", 
01744                                        KEY_NAME_HPRO_DATE, date) ;
01745   cpl_propertylist_set_comment(plist, KEY_NAME_HPRO_DATE, KEY_HELP_HPRO_DATE);
01746 
01747   cpl_propertylist_insert_after_string(plist, "EXPTIME", 
01748                                        KEY_NAME_HPRO_RECID, file_name) ;
01749   cpl_propertylist_set_comment(plist, KEY_NAME_HPRO_RECID,KEY_HELP_HPRO_RECID);
01750 
01751   cpl_propertylist_insert_after_string(plist, "EXPTIME", 
01752                                        KEY_NAME_HPRO_DRSID, PACKAGE_VERSION);
01753   cpl_propertylist_set_comment(plist, KEY_NAME_HPRO_DRSID,KEY_HELP_HPRO_DRSID);
01754 
01755 
01756 }
01757 /*---------------------------------------------------------------------------*/
01764 /*---------------------------------------------------------------------------*/
01765 int sinfo_compare_tags(
01766         cpl_frame   *   frame1,
01767         cpl_frame   *   frame2)
01768 {
01769     char            *   v1 ;
01770     char            *   v2 ;
01771 
01772     /* Test entries */
01773     if (frame1==NULL || frame2==NULL) return -1 ;
01774 
01775     /* Get the tags */
01776     if ((v1 = (char*)cpl_frame_get_tag(frame1)) == NULL) return -1 ;
01777     if ((v2 = (char*)cpl_frame_get_tag(frame2)) == NULL) return -1 ;
01778 
01779     /* Compare the tags */
01780     if (strcmp(v1, v2)) return 0 ;
01781     else return 1 ;
01782 }
01783 
01792 int sinfo_extract_raw_pinhole_frames(cpl_frameset * sof, cpl_frameset** raw)
01793 {
01794    char* tag=NULL;
01795    char* name=NULL;
01796    cpl_frame* frame   = NULL;
01797    int nsof=0;
01798    int i=0;
01799    nsof = cpl_frameset_get_size(sof);
01800    for (i=0 ; i<nsof ; i++) {
01801       frame = cpl_frameset_get_frame(sof,i);
01802       name= (char*) cpl_frame_get_filename(frame);
01803       if(sinfo_is_fits_file(name) == 1) {
01804     /* to go on the file must exist */
01805     if(cpl_frame_get_tag(frame) != NULL) {
01806       /* If the frame has a tag we process it. Else it is an object */ 
01807       tag= (char*) cpl_frame_get_tag(frame);
01808           if(sinfo_frame_is_pinhole_lamp(tag) == 1) {
01809         cpl_frameset_insert(*raw,frame);
01810       }
01811     }
01812       }
01813    }
01814    return 0;
01815 }
01816 
01817 
01818 int sinfo_get_ins_set(char* band,int* ins_set){
01819 
01820       if (strcmp(band,"H") == 0) {
01821     *ins_set = 0;
01822       }
01823  else if (strcmp(band,"H+K") == 0) {
01824     *ins_set = 1;
01825  }
01826  else if (strcmp(band,"K") == 0) {
01827     *ins_set = 2;
01828  }
01829  else if (strcmp(band,"J") == 0) {
01830     *ins_set = 3;
01831  }
01832       return 0;
01833 
01834 
01835 }
01836 int sinfo_extract_raw_frames(cpl_frameset * sof, cpl_frameset** raw)
01837 {
01838    char* tag=NULL;
01839    char* name=NULL;
01840    cpl_frame* frame   = NULL;
01841    int nsof=0;
01842    int i=0;
01843    nsof = cpl_frameset_get_size(sof);
01844    for (i=0 ; i<nsof ; i++) {
01845       frame = cpl_frameset_get_frame(sof,i);
01846       name= (char*) cpl_frame_get_filename(frame);
01847       if(sinfo_is_fits_file(name) == 1) {
01848     /* to go on the file must exist */
01849     if(cpl_frame_get_tag(frame) != NULL) {
01850       /* If the frame has a tag we process it. Else it is an object */ 
01851       tag= (char*) cpl_frame_get_tag(frame);
01852           if(sinfo_frame_is_raw(tag) == 1) {
01853         cpl_frameset_insert(*raw,frame);
01854       }
01855     }
01856       }
01857    }
01858    return 0;
01859 }
01860 
01861 int sinfo_remove_qc_frames(cpl_frameset* sof,cpl_frameset** raw)
01862 {
01863    char* tag=NULL;
01864    char* name=NULL;
01865    cpl_frame* frame   = NULL;
01866    int nsof=0;
01867    int i=0;
01868 
01869    nsof = cpl_frameset_get_size(sof);
01870    for (i=0 ; i<nsof ; i++) {
01871       frame = cpl_frameset_get_frame(sof,i);
01872       name= (char*) cpl_frame_get_filename(frame);
01873       /* sinfo_msg("name=%s",name); */
01874       if(sinfo_is_fits_file(name) == 1) {
01875         /* sinfo_msg("\t exist "); */
01876     /* to go on the file must exist */
01877     if(cpl_frame_get_tag(frame) != NULL) {
01878       /* If the frame has a tag we process it. Else it is an object */ 
01879       tag= (char*) cpl_frame_get_tag(frame);
01880       /* sinfo_msg("\t tag %s\n ",tag); */
01881           if(strstr(tag,"QC") != NULL) {
01882             /* sinfo_msg("remove frame %s\n",name); */
01883         cpl_frameset_erase(*raw,tag);
01884       }
01885     }
01886       } else {
01887         /* sinfo_msg("remove frame\n"); */
01888         cpl_frameset_erase_frame(*raw,frame);
01889       }
01890    }
01891    return 0;
01892 
01893 }
01894 
01895 
01896 int sinfo_contains_frames_kind(cpl_frameset * sof, 
01897                                  cpl_frameset* raw,
01898                                  const char*         type)
01899 {
01900    char* tag=NULL;
01901    char* name=NULL;
01902    cpl_frame* frame   = NULL;
01903    cpl_frame* frame_dup   = NULL;
01904 
01905    int nsof=0;
01906    int i=0;
01907    nsof = cpl_frameset_get_size(sof);
01908    for (i=0 ; i<nsof ; i++) {
01909       frame = cpl_frameset_get_frame(sof,i);
01910       name= (char*) cpl_frame_get_filename(frame);
01911       if(sinfo_is_fits_file(name) == 1) {
01912     /* to go on the file must exist */
01913     if(cpl_frame_get_tag(frame) != NULL) {
01914       /* If the frame has a tag we process it. Else it is an object */ 
01915       tag= (char*) cpl_frame_get_tag(frame);
01916       /* sinfo_msg("name=%s tag=%s type=%s\n",name,tag,type); */
01917           if(strstr(tag,type) != NULL) {
01918         /* sinfo_msg("Match name=%s tag=%s type=%s\n",name,tag,type); */
01919             frame_dup = cpl_frame_duplicate(frame);
01920         cpl_frameset_insert(raw,frame_dup);
01921         /* sinfo_msg("inserted\n"); */ 
01922       }
01923     }
01924       }
01925    }
01926    return 0;
01927 }
01928 
01929 
01930 
01931 
01932 int sinfo_is_fibres_on_off(cpl_frameset * sof, 
01933                  cpl_frameset* raw)
01934 {
01935    char* tag=NULL;
01936    char* name=NULL;
01937    cpl_frame* frame   = NULL;
01938    cpl_frame* frame_dup   = NULL;
01939 
01940    int nsof=0;
01941    int i=0;
01942    nsof = cpl_frameset_get_size(sof);
01943    for (i=0 ; i<nsof ; i++) {
01944       frame = cpl_frameset_get_frame(sof,i);
01945       name= (char*) cpl_frame_get_filename(frame);
01946       if(sinfo_is_fits_file(name) == 1) {
01947     /* to go on the file must exist */
01948     if(cpl_frame_get_tag(frame) != NULL) {
01949       /* If the frame has a tag we process it. Else it is an object */ 
01950       tag= (char*) cpl_frame_get_tag(frame);
01951       /* sinfo_msg("name=%s tag=%s type=%s\n",name,tag,type); */
01952           if( strcmp(tag,PRO_FIBRE_NS_STACKED ) == 0) {
01953         /* sinfo_msg("Match name=%s tag=%s type=%s\n",name,tag); */ 
01954             frame_dup = cpl_frame_duplicate(frame);
01955         cpl_frameset_insert(raw,frame_dup);
01956         /* sinfo_msg("inserted\n"); */ 
01957       }
01958     }
01959       }
01960    }
01961    return 0;
01962 }
01963 
01964 
01965 int sinfo_contains_frames_type(cpl_frameset * sof, 
01966                                     cpl_frameset** raw,
01967                                     const char*          type)
01968 {
01969    char* tag=NULL;
01970    char* name=NULL;
01971    cpl_frame* frame   = NULL;
01972    cpl_frame* frame_dup   = NULL;
01973    int nsof=0;
01974    int i=0;
01975    nsof = cpl_frameset_get_size(sof);
01976    for (i=0 ; i<nsof ; i++) {
01977       frame = cpl_frameset_get_frame(sof,i);
01978       name= (char*) cpl_frame_get_filename(frame);
01979       if(sinfo_is_fits_file(name) == 1) {
01980     /* to go on the file must exist */
01981     if(cpl_frame_get_tag(frame) != NULL) {
01982       /* If the frame has a tag we process it. Else it is an object */ 
01983       tag= (char*) cpl_frame_get_tag(frame);
01984           if(strstr(tag,type) != NULL) {
01985         /* sinfo_msg("name=%s tag=%s type=%s\n",name,tag,type); */
01986             frame_dup=cpl_frame_duplicate(frame);
01987         cpl_frameset_insert(*raw,frame_dup);
01988       }
01989     }
01990       }
01991    }
01992    return 0;
01993 }
01994 
01995 int sinfo_extract_raw_frames_type2(cpl_frameset * sof, 
01996                                     cpl_frameset** raw,
01997                                     const char*          type)
01998 {
01999 
02000   cpl_frame* frame=NULL;
02001   cpl_frame* frame_dup   = NULL;
02002   frame = cpl_frameset_find(sof,type);
02003   while(frame) {    
02004     frame_dup=cpl_frame_duplicate(frame);
02005     cpl_frameset_insert(*raw,frame_dup);
02006     frame = cpl_frameset_find(sof,NULL);
02007   }
02008   return 0;
02009 
02010 }
02011 
02012 
02013 int sinfo_extract_raw_frames_type1(cpl_frameset * sof, 
02014                                     cpl_frameset* raw,
02015                                     const char*          type)
02016 {
02017 
02018   cpl_frame* frame=NULL;
02019   cpl_frame* frame_dup   = NULL;
02020   frame = cpl_frameset_find(sof,type);
02021   while(frame) {    
02022     frame_dup=cpl_frame_duplicate(frame);
02023     cpl_frameset_insert(raw,frame_dup);
02024     frame = cpl_frameset_find(sof,NULL);
02025   }
02026   return 0;
02027 
02028 }
02029 
02030 int sinfo_extract_raw_frames_type(cpl_frameset * sof, 
02031                                     cpl_frameset** raw,
02032                                     const char*          type)
02033 {
02034    char tag[FILE_NAME_SZ];
02035    char name[FILE_NAME_SZ];
02036    cpl_frame* frame   = NULL;
02037    cpl_frame* frame_dup   = NULL;
02038    int nsof=0;
02039    int i=0;
02040    nsof = cpl_frameset_get_size(sof);
02041    for (i=0 ; i<nsof ; i++) {
02042       frame = cpl_frameset_get_frame(sof,i);
02043       strcpy(name, cpl_frame_get_filename(frame));
02044       if(sinfo_is_fits_file(name) == 1) {
02045     /* to go on the file must exist */
02046     if(cpl_frame_get_tag(frame) != NULL) {
02047       /* If the frame has a tag we process it. Else it is an object */ 
02048       strcpy(tag,cpl_frame_get_tag(frame));
02049           if(strcmp(tag,type) == 0) {
02050         /* sinfo_msg("name=%s tag=%s type=%s\n",name,tag,type); */
02051             frame_dup=cpl_frame_duplicate(frame);
02052 
02053         cpl_frameset_insert(*raw,frame_dup);
02054       }
02055     }
02056       }
02057    }
02058    return 0;
02059 }
02060 
02061 int sinfo_extract_frames_type(cpl_frameset * sof, 
02062                                     cpl_frameset * raw,
02063                                     const char*          type)
02064 {
02065    char* tag=NULL;
02066    char* name=NULL;
02067    cpl_frame* frame   = NULL;
02068    cpl_frame* frame_dup   = NULL;
02069    int nsof=0;
02070    int i=0;
02071    nsof = cpl_frameset_get_size(sof);
02072    for (i=0 ; i<nsof ; i++) {
02073       frame = cpl_frameset_get_frame(sof,i);
02074       name= (char*) cpl_frame_get_filename(frame);
02075       if(sinfo_is_fits_file(name) == 1) {
02076     /* to go on the file must exist */
02077     if(cpl_frame_get_tag(frame) != NULL) {
02078       /* If the frame has a tag we process it. Else it is an object */ 
02079       tag= (char*) cpl_frame_get_tag(frame);
02080           if(strcmp(tag,type) == 0) {
02081         /* sinfo_msg("name=%s tag=%s type=%s\n",name,tag,type); */
02082             frame_dup=cpl_frame_duplicate(frame);
02083         cpl_frameset_insert(raw,frame_dup);
02084       }
02085     }
02086       }
02087    }
02088    return 0;
02089 }
02090 
02091 
02092 int sinfo_extract_obj_frames(cpl_frameset * sof, cpl_frameset* obj)
02093 {
02094    char* tag=NULL;
02095    char* name=NULL;
02096    cpl_frame* frame   = NULL;
02097    cpl_frame* frame_dup   = NULL;
02098 
02099    int nsof=0;
02100    int i=0;
02101    nsof = cpl_frameset_get_size(sof);
02102     for (i=0 ; i<nsof ; i++) {
02103       frame = cpl_frameset_get_frame(sof,i);
02104       name= (char*) cpl_frame_get_filename(frame);
02105       if(sinfo_is_fits_file(name) ==1) {
02106     /* to go on the file must exist */
02107     if(cpl_frame_get_tag(frame) != NULL) {
02108       /* If the frame has a tag we process it. Else it is an object */ 
02109       tag= (char*) cpl_frame_get_tag(frame);
02110           if(sinfo_tag_is_obj(tag) == 1) {
02111             frame_dup=cpl_frame_duplicate(frame);
02112         cpl_frameset_insert(obj,frame_dup);
02113       }
02114     }
02115       }
02116    }
02117   
02118    return 0;
02119 }
02120 
02121 
02122 int sinfo_extract_obj_products(cpl_frameset * sof, cpl_frameset* obj)
02123 {
02124    char* tag=NULL;
02125    char* name=NULL;
02126    cpl_frame* frame   = NULL;
02127    cpl_frame* frame_dup   = NULL;
02128 
02129    int nsof=0;
02130    int i=0;
02131    nsof = cpl_frameset_get_size(sof);
02132     for (i=0 ; i<nsof ; i++) {
02133       frame = cpl_frameset_get_frame(sof,i);
02134       name= (char*) cpl_frame_get_filename(frame);
02135       if(sinfo_is_fits_file(name) ==1) {
02136     /* to go on the file must exist */
02137     if(cpl_frame_get_tag(frame) != NULL) {
02138       /* If the frame has a tag we process it. Else it is an object */ 
02139       tag= (char*) cpl_frame_get_tag(frame);
02140           if(sinfo_tag_is_objpro(tag) == 1) {
02141             frame_dup=cpl_frame_duplicate(frame);
02142         cpl_frameset_insert(obj,frame_dup);
02143       }
02144     }
02145       }
02146    }
02147   
02148    return 0;
02149 }
02150 
02151 int sinfo_extract_on_frames(cpl_frameset * sof, cpl_frameset* on)
02152 {
02153    cpl_frame* frame   = NULL;
02154    cpl_frame* frame_dup   = NULL;
02155 
02156    int nsof=0;
02157    int i=0;
02158    nsof = cpl_frameset_get_size(sof);
02159    for (i=0 ; i<nsof ; i++) {
02160       frame = cpl_frameset_get_frame(sof,i);
02161       if(sinfo_frame_is_on(frame) ==1) {
02162         frame_dup=cpl_frame_duplicate(frame);
02163     cpl_frameset_insert(on,frame_dup);
02164       }
02165    }
02166   
02167    return 0;
02168 }
02169 
02170 int sinfo_extract_sky_frames(cpl_frameset * sof, cpl_frameset* sky)
02171 {
02172    char* tag=NULL;
02173    char* name=NULL;
02174    cpl_frame* frame   = NULL;
02175    cpl_frame* frame_dup   = NULL;
02176    int nsof=0;
02177    int i=0;
02178    nsof = cpl_frameset_get_size(sof);
02179     for (i=0 ; i<nsof ; i++) {
02180       frame = cpl_frameset_get_frame(sof,i);
02181       name= (char*) cpl_frame_get_filename(frame);
02182       if(sinfo_is_fits_file(name) ==1) {
02183     /* to go on the file must exist */
02184     if(cpl_frame_get_tag(frame) != NULL) {
02185       /* If the frame has a tag we process it. Else it is an object */ 
02186       tag= (char*) cpl_frame_get_tag(frame);
02187           if(sinfo_tag_is_sky(tag) == 1) {
02188             frame_dup=cpl_frame_duplicate(frame);
02189         cpl_frameset_insert(sky,frame_dup);
02190       }
02191     }
02192       }
02193    }
02194   
02195    return 0;
02196 }
02197 
02198 
02199 
02200 int sinfo_extract_off_frames(cpl_frameset * sof, cpl_frameset* off)
02201 {
02202    cpl_frame* frame   = NULL;
02203    cpl_frame* frame_dup   = NULL;
02204    int nsof=0;
02205    int i=0;
02206    nsof = cpl_frameset_get_size(sof);
02207    for (i=0 ; i<nsof ; i++) {
02208       frame = cpl_frameset_get_frame(sof,i);
02209       if(sinfo_frame_is_on(frame)) {
02210     frame_dup=cpl_frame_duplicate(frame);
02211     cpl_frameset_insert(off,frame_dup);
02212       }
02213    }
02214   
02215    return 0;
02216 }
02217 
02218 int sinfo_extract_mst_frames(cpl_frameset * sof, cpl_frameset* cdb)
02219 {
02220    char* tag=NULL;
02221    char* name=NULL;
02222    cpl_frame* frame   = NULL;
02223    cpl_frame* frame_dup   = NULL;
02224    int nsof=0;
02225    int i=0;
02226   
02227    nsof = cpl_frameset_get_size(sof);
02228    for (i=0 ; i<nsof ; i++) {
02229       frame = cpl_frameset_get_frame(sof,i);
02230       name= (char*) cpl_frame_get_filename(frame);
02231       if(sinfo_is_fits_file(name) ==1) {
02232     /* to go on the file must exist */
02233     if(cpl_frame_get_tag(frame) != NULL) {
02234       /* If the frame has a tag we process it. Else it is an object */ 
02235       tag= (char*) cpl_frame_get_tag(frame);
02236           if(sinfo_frame_is_cdb(tag) == 1) {
02237              frame_dup=cpl_frame_duplicate(frame);
02238         cpl_frameset_insert(cdb,frame_dup);
02239       }
02240     }
02241       }
02242    }
02243   
02244    return 0;
02245 }
02246 
02247 cpl_frameset* sinfo_frameset_join(cpl_frameset* fs1,cpl_frameset* fs2) {
02248 
02249   cpl_frameset* join=NULL;
02250   cpl_frame* frm=NULL;
02251   cpl_frame* frm_dup=NULL;
02252   int i=0;
02253   int n=0;
02254 
02255   join=cpl_frameset_new();
02256 
02257   n=cpl_frameset_get_size(fs1);
02258   for(i=0;i<n; i++) {
02259     frm=cpl_frameset_get_frame(fs1,i);
02260     frm_dup= cpl_frame_duplicate(frm);
02261     cpl_frameset_insert(join,frm_dup);
02262   }
02263 
02264   n=cpl_frameset_get_size(fs2);
02265   for(i=0;i<n; i++) {
02266     frm=cpl_frameset_get_frame(fs2,i);
02267     frm_dup= cpl_frame_duplicate(frm);
02268     cpl_frameset_insert(join,frm_dup);
02269   }
02270 
02271 
02272   return join;
02273 
02274 }
02275 
02276 
02277 int sinfo_extract_stk_frames(cpl_frameset * sof, 
02278                              cpl_frameset* res)
02279 {
02280    char* tag=NULL;
02281    char* name=NULL;
02282    cpl_frame* frame   = NULL;
02283    cpl_frame* frame_dup   = NULL;
02284    int nsof=0;
02285    int i=0;
02286   
02287    nsof = cpl_frameset_get_size(sof);
02288    for (i=0 ; i<nsof ; i++) {
02289       frame = cpl_frameset_get_frame(sof,i);
02290       name= (char*) cpl_frame_get_filename(frame);
02291       if(sinfo_is_fits_file(name) ==1) {
02292     /* to go on the file must exist */
02293     if(cpl_frame_get_tag(frame) != NULL) {
02294       /* If the frame has a tag we process it. Else it is an object */ 
02295       tag= (char*) cpl_frame_get_tag(frame);
02296           if(sinfo_frame_is_stk(tag) == 1) {
02297              frame_dup=cpl_frame_duplicate(frame);
02298         cpl_frameset_insert(res,frame_dup);
02299       }
02300     }
02301       }
02302    }
02303   
02304    return 0;
02305 }
02306 
02307 
02308 int 
02309 sinfo_extract_preoptic_frames(cpl_frameset * sof, 
02310                               cpl_frameset** res, 
02311                               const char* val)
02312 {
02313    char* name=NULL;
02314    cpl_frame* frame   = NULL;
02315    cpl_frame* frame_dup   = NULL;
02316    int nsof=0;
02317    int i=0;
02318 
02319    nsof = cpl_frameset_get_size(sof);
02320    for (i=0 ; i<nsof ; i++) {
02321      frame = cpl_frameset_get_frame(sof,i);
02322      name= (char*) cpl_frame_get_filename(frame);
02323      if(sinfo_is_fits_file(name) ==1) {
02324        if(sinfo_frame_is_preoptic(frame,val) == 1) {
02325      frame_dup=cpl_frame_duplicate(frame);
02326      cpl_frameset_insert(*res,frame_dup);
02327        }
02328      }
02329    }
02330   
02331    return 0;
02332 }
02333 
02334 int sinfo_extract_raw_stack_frames(cpl_frameset * sof, cpl_frameset** pro)
02335 {
02336    char* tag=NULL;
02337    char* name=NULL;
02338    cpl_frame* frame   = NULL;
02339    cpl_frame* frame_dup   = NULL;
02340 
02341    int nsof=0;
02342    int i=0;
02343    nsof = cpl_frameset_get_size(sof);
02344 
02345    for (i=0 ; i<nsof ; i++) {
02346       frame = cpl_frameset_get_frame(sof,i);
02347       name= (char*) cpl_frame_get_filename(frame);
02348       if(sinfo_is_fits_file(name) ==1) {
02349     /* to go on the file must exist */
02350     if(cpl_frame_get_tag(frame) != NULL) {
02351       /* If the frame has a tag we process it. Else it is an object */ 
02352       tag= (char*) cpl_frame_get_tag(frame);
02353           /* sinfo_msg("tag=%s\n",tag); */
02354           if(sinfo_frame_is_raw_stack(tag) == 1) {
02355             frame_dup   = cpl_frame_duplicate(frame);
02356         cpl_frameset_insert(*pro,frame_dup);
02357       }
02358     }
02359       }
02360    }
02361   
02362    return 0;
02363 }
02364 
02365 
02366 int sinfo_extract_raw_slit_frames(cpl_frameset * sof, cpl_frameset** pro)
02367 {
02368    char* tag=NULL;
02369    char* name=NULL;
02370    cpl_frame* frame   = NULL;
02371    int nsof=0;
02372    int i=0;
02373    nsof = cpl_frameset_get_size(sof);
02374    for (i=0 ; i<nsof ; i++) {
02375       frame = cpl_frameset_get_frame(sof,i);
02376       name= (char*) cpl_frame_get_filename(frame);
02377       if(sinfo_is_fits_file(name) ==1) {
02378     /* to go on the file must exist */
02379     if(cpl_frame_get_tag(frame) != NULL) {
02380       /* If the frame has a tag we process it. Else it is an object */ 
02381       tag= (char*) cpl_frame_get_tag(frame);
02382           if(sinfo_frame_is_slit_lamp(tag) == 1) {
02383         cpl_frameset_insert(*pro,frame);
02384       }
02385     }
02386       }
02387    }
02388   
02389    return 0;
02390 }
02391 
02392 /*---------------------------------------------------------------------------*/
02398 /*---------------------------------------------------------------------------*/
02399 int sinfo_frame_is_raw(char * tag) 
02400 {
02401     /* Test entries */
02402     if (tag == NULL) return -1 ;
02403 
02404     if (!strcmp(tag, RAW_LINEARITY_LAMP)) return 1 ;
02405     if (!strcmp(tag, RAW_DARK)) return 1 ;
02406     if (!strcmp(tag, RAW_PINHOLE_LAMP)) return 1 ;
02407     if (!strcmp(tag, RAW_SLIT_LAMP)) return 1 ;
02408     if (!strcmp(tag, RAW_WAVE_LAMP)) return 1 ;
02409     if (!strcmp(tag, RAW_FLAT_LAMP)) return 1 ;
02410     if (!strcmp(tag, RAW_WAVE_NS)) return 1 ;
02411     if (!strcmp(tag, RAW_FLAT_NS)) return 1 ;
02412     if (!strcmp(tag, RAW_FIBRE_LAMP)) return 1 ;
02413     if (!strcmp(tag, RAW_FIBRE_EW)) return 1 ;
02414     if (!strcmp(tag, RAW_FIBRE_NS)) return 1 ;
02415     if (!strcmp(tag, RAW_FLAT_SKY)) return 1 ;
02416     if (!strcmp(tag, RAW_FLUX_LAMP)) return 1 ;
02417     if (!strcmp(tag, RAW_PSF_CALIBRATOR)) return 1 ;
02418     if (!strcmp(tag, RAW_FOCUS)) return 1 ;
02419 
02420     if (!strcmp(tag, RAW_STD)) return 1 ;
02421     if (!strcmp(tag, RAW_STD_STAR)) return 1 ;
02422     if (!strcmp(tag, RAW_STD_STAR_DITHER)) return 1 ;
02423     if (!strcmp(tag, RAW_SKY_STD)) return 1 ;
02424     if (!strcmp(tag, RAW_SKY_OH)) return 1 ;
02425     if (!strcmp(tag, RAW_SKY_PSF_CALIBRATOR)) return 1 ;
02426 
02427     if (!strcmp(tag, RAW_PUPIL_LAMP)) return 1 ;
02428     if (!strcmp(tag, RAW_OBJECT_JITTER)) return 1 ;
02429     if (!strcmp(tag, RAW_SKY_JITTER)) return 1 ;
02430     if (!strcmp(tag, RAW_OBJECT_NODDING)) return 1 ;
02431     if (!strcmp(tag, RAW_OBJECT_SKYSPIDER)) return 1 ;
02432     if (!strcmp(tag, RAW_SKY_NODDING)) return 1 ;
02433 
02434     if (!strcmp(tag, RAW_FLAT_LAMP_DITHER)) return 1 ;
02435     if (!strcmp(tag, RAW_WAVE_LAMP_DITHER)) return 1 ;
02436     if (!strcmp(tag, RAW_STD_STAR_DITHER)) return 1 ;
02437     if (!strcmp(tag, RAW_OBJECT_NODDING_DITHER)) return 1 ;
02438     if (!strcmp(tag, RAW_OBJECT_SKYSPIDER_DITHER)) return 1 ;
02439     if (!strcmp(tag, RAW_SKY_NODDING_DITHER)) return 1 ;
02440 
02441 
02442     return 0 ;
02443 }
02444 
02445 /*---------------------------------------------------------------------------*/
02451 /*---------------------------------------------------------------------------*/
02452 int sinfo_frame_is_raw_stack(char * tag) 
02453 {
02454     /* Test entries */
02455     if (tag == NULL) return -1 ;
02456 
02457 
02458     if (!strcmp(tag, PRO_SKY_DUMMY)) return 1 ;
02459     if (!strcmp(tag, RAW_WAVE_LAMP)) return 1 ;
02460     if (!strcmp(tag, RAW_WAVE_LAMP_DITHER)) return 1 ;
02461     if (!strcmp(tag, RAW_WAVE_NS)) return 1 ;
02462     if (!strcmp(tag, RAW_WAVE_NS_DITHER)) return 1 ;
02463 
02464     if (!strcmp(tag, RAW_FLUX_LAMP)) return 1 ;
02465     if (!strcmp(tag, RAW_FIBRE_NS)) return 1 ;
02466     if (!strcmp(tag, RAW_FIBRE_EW)) return 1 ;
02467 
02468     if (!strcmp(tag, RAW_PSF_CALIBRATOR)) return 1 ;
02469     if (!strcmp(tag, RAW_FIBRE_PSF)) return 1 ;
02470     if (!strcmp(tag, RAW_FIBRE_DARK)) return 1 ;
02471 
02472     if (!strcmp(tag, RAW_FOCUS)) return 1 ;
02473 
02474     if (!strcmp(tag, RAW_PUPIL_LAMP)) return 1 ;
02475     if (!strcmp(tag, RAW_OBJECT_JITTER)) return 1 ;
02476     if (!strcmp(tag, RAW_SKY_JITTER)) return 1 ;
02477     if (!strcmp(tag, RAW_OBJECT_NODDING)) return 1 ;
02478     if (!strcmp(tag, RAW_OBJECT_SKYSPIDER)) return 1 ;
02479     if (!strcmp(tag, RAW_SKY_NODDING)) return 1 ;
02480 
02481     if (!strcmp(tag, RAW_OBJECT_NODDING_DITHER)) return 1 ;
02482     if (!strcmp(tag, RAW_OBJECT_SKYSPIDER_DITHER)) return 1 ;
02483     if (!strcmp(tag, RAW_SKY_NODDING_DITHER)) return 1 ;
02484 
02485 
02486     if (!strcmp(tag, RAW_IMAGE_PRE_OBJECT)) return 1 ;
02487     if (!strcmp(tag, RAW_IMAGE_PRE_SKY)) return 1 ;
02488     if (!strcmp(tag, RAW_STD)) return 1 ;
02489     if (!strcmp(tag, RAW_SKY_STD)) return 1 ;
02490     if (!strcmp(tag, RAW_SKY_OH)) return 1 ;
02491     if (!strcmp(tag, RAW_SKY_PSF_CALIBRATOR)) return 1 ;
02492     if (!strcmp(tag, RAW_STD_STAR)) return 1 ;
02493     if (!strcmp(tag, RAW_SKY)) return 1 ;
02494 
02495     return 0 ;
02496 }
02497 
02498 
02499 /*---------------------------------------------------------------------------*/
02505 /*---------------------------------------------------------------------------*/
02506 int sinfo_frame_is_raw_dark(char * tag) 
02507 {
02508     /* Test entries */
02509     if (tag == NULL) return -1 ;
02510 
02511     if (!strcmp(tag, RAW_DARK)) return 1 ;
02512 
02513     return 0 ;
02514 }
02515 
02516 /*---------------------------------------------------------------------------*/
02522 /*---------------------------------------------------------------------------*/
02523 int sinfo_frame_is_slit_lamp(char * tag) 
02524 {
02525     /* Test entries */
02526     if (tag == NULL) return -1 ;
02527 
02528     if (!strcmp(tag, RAW_SLIT_LAMP)) return 1 ;
02529 
02530     return 0 ;
02531 }
02532 
02533 
02534 /*---------------------------------------------------------------------------*/
02540 /*---------------------------------------------------------------------------*/
02541 int sinfo_frame_is_pinhole_lamp(char * tag) 
02542 {
02543     /* Test entries */
02544     if (tag == NULL) return -1 ;
02545 
02546     if (!strcmp(tag, RAW_PINHOLE_LAMP)) return 1 ;
02547 
02548     return 0 ;
02549 }
02550 
02551 
02552 int sinfo_frame_is_cdb(char * tag) 
02553 {
02554     /* Test entries */
02555     if (tag == NULL) return -1 ;
02556     /* For the moment not checked the following:
02557 
02558 PRO_STACKED                      
02559 PRO_SLIT_ON                      
02560 PRO_FLUX_LAMP_STACKED            
02561 PRO_WAVE_LAMP_STACKED            
02562 PRO_PSF_CALIBRATOR_STACKED       
02563 PRO_FOCUS_STACKED                
02564 PRO_OBJECT_NODDING_STACKED       
02565 PRO_OBJECT_SKYSPIDER_STACKED     
02566 PRO_SKY_NODDING_STACKED          
02567 PRO_STD_NODDING_STACKED          
02568 PRO_MASK_CUBE                      
02569 PRO_PSF                          
02570 TMP_FOCUS                        
02571 TMP_FOCUS_ON                     
02572 TMP_FOCUS_OFF                    
02573 PRO_FOCUS                        
02574 PRO_FOCUS_GAUSS                  
02575 PRO_SPECTRA                      
02576 PRO_CUBE                         
02577 PRO_CUBE_COLL                    
02578 PRO_SLOPEX                       
02579 PRO_SLOPEY                       
02580 PRO_MASK_CUBE                    
02581 PRO_OBJ_CUBE                     
02582 PRO_BP_COEFF                     
02583 */
02584 
02585     if (!strcmp(tag, REF_LINE_ARC)) return 1 ;
02586     if (!strcmp(tag, PRO_BP_MAP)) return 1 ;
02587     if (!strcmp(tag, PRO_BP_MAP_HP)) return 1 ;
02588     if (!strcmp(tag, PRO_BP_MAP_DI)) return 1 ;
02589     if (!strcmp(tag, PRO_BP_MAP_NO)) return 1 ;
02590     if (!strcmp(tag, PRO_BP_MAP_NL)) return 1 ;
02591     if (!strcmp(tag, PRO_MASTER_BP_MAP)) return 1 ;
02592     if (!strcmp(tag, PRO_MASTER_DARK)) return 1 ;
02593     if (!strcmp(tag, PRO_SLOPE)) return 1 ;
02594     if (!strcmp(tag, PRO_DISTORTION)) return 1 ;
02595     if (!strcmp(tag, PRO_SLITLETS_DISTANCE)) return 1 ;
02596     if (!strcmp(tag, PRO_MASTER_FLAT_LAMP)) return 1 ;
02597     if (!strcmp(tag, PRO_MASTER_FLAT_LAMP1)) return 1 ;
02598     if (!strcmp(tag, PRO_MASTER_FLAT_LAMP2)) return 1 ;
02599     if (!strcmp(tag, PRO_SLIT_POS)) return 1 ;
02600     if (!strcmp(tag, PRO_SLIT_POS_GUESS)) return 1 ;
02601     if (!strcmp(tag, PRO_WAVE_PAR_LIST)) return 1 ;
02602     if (!strcmp(tag, PRO_WAVE_COEF_SLIT)) return 1 ;
02603     if (!strcmp(tag, PRO_MASTER_LAMP_SPEC)) return 1 ;
02604     if (!strcmp(tag, PRO_MASTER_TWIFLAT)) return 1 ;
02605     if (!strcmp(tag, PRO_COEFF_LIST)) return 1 ;
02606     if (!strcmp(tag, PRO_INDEX_LIST)) return 1 ;
02607     if (!strcmp(tag, PRO_HALO_SPECT)) return 1 ;
02608     if (!strcmp(tag, PRO_FIRST_COL)) return 1 ;
02609     if (!strcmp(tag, PRO_FOCUS)) return 1 ;
02610     if (!strcmp(tag, PRO_WAVE_MAP)) return 1 ;
02611 
02612   return 0;
02613 
02614 }
02615 
02616 
02617 
02618 
02619 int sinfo_frame_is_stk(char * tag) 
02620 {
02621     /* Test entries */
02622     if (tag == NULL) return -1 ;
02623     /* For the moment not checked the following: */
02624 
02625 
02626     if (!strcmp(tag, PRO_SKY_STACKED_DUMMY)) return 1 ;
02627     if (!strcmp(tag, PRO_STACK_SKY_DIST)) return 1 ;
02628     if (!strcmp(tag, PRO_STACK_MFLAT_DIST)) return 1 ;
02629     if (!strcmp(tag, PRO_PSF_CALIBRATOR_STACKED)) return 1 ;
02630 
02631 
02632   return 0;
02633 
02634 }
02635 
02636 
02637 
02638 int sinfo_frame_is_preoptic(cpl_frame* frame,const char* val) 
02639 {
02640 
02641   char* file=NULL;
02642   char popt[FILE_NAME_SZ];
02643   cpl_propertylist* plist=NULL;
02644 
02645 
02646   file = cpl_strdup(cpl_frame_get_filename(frame)) ;
02647   if ((cpl_error_code)((plist = cpl_propertylist_load(file, 0)) == NULL)) {
02648       sinfo_msg_error( "getting header from reference frame %s",file);
02649       cpl_propertylist_delete(plist) ;
02650       cpl_free(file);
02651       return -1 ;
02652   }
02653 
02654   if (cpl_propertylist_contains(plist, KEY_NAME_PREOPTICS)) {
02655     strcpy(popt,cpl_propertylist_get_string(plist, KEY_NAME_PREOPTICS));
02656   } else {
02657       sinfo_msg_error("keyword %s does not exist",KEY_NAME_PREOPTICS);
02658       cpl_free(file);
02659       return -1;
02660   }
02661   cpl_propertylist_delete(plist) ;
02662   cpl_free(file);
02663  
02664   if (strstr(val,popt) != NULL) return 1 ;
02665 
02666 
02667   return 0;
02668 
02669 }
02670 
02671 
02672 int sinfo_get_preoptic(const char* file, const char* val) 
02673 {
02674 
02675    cpl_propertylist* plist=NULL;
02676 
02677 
02678   if ((cpl_error_code)((plist = cpl_propertylist_load(file, 0)) == NULL)) {
02679       sinfo_msg_error( "getting header from reference frame %s",file);
02680       cpl_propertylist_delete(plist) ;
02681       return -1 ;
02682   }
02683 
02684   if (cpl_propertylist_contains(plist, KEY_NAME_PREOPTICS)) {
02685     strcpy((char*)val,cpl_propertylist_get_string(plist, KEY_NAME_PREOPTICS));
02686   } else {
02687       sinfo_msg_error("keyword %s does not exist",KEY_NAME_PREOPTICS);
02688       return -1;
02689   }
02690   cpl_propertylist_delete(plist) ;
02691  
02692   return 0;
02693 
02694 }
02695 
02696 
02697 cpl_table* sinfo_compute_gain(cpl_frameset* son, cpl_frameset* sof)
02698 {
02699 
02700 
02701   cpl_frame*    frm=NULL;
02702 
02703   cpl_image* img_on1=NULL;
02704   cpl_image* img_on2=NULL;
02705   cpl_image* img_on_dif=NULL;
02706 
02707   cpl_image* img_of1=NULL;
02708   cpl_image* img_of2=NULL;
02709   cpl_image* img_of_dif=NULL;
02710 
02711   cpl_table* res_tbl=NULL;
02712   cpl_vector* dit_on=NULL;
02713   cpl_vector* dit_of=NULL;
02714   cpl_vector* exptime_on=NULL;
02715   cpl_vector* exptime_of=NULL;
02716   cpl_propertylist* plist=NULL;
02717 
02718   int non=0;
02719   int nof=0;
02720   int nfr=0;
02721   double avg_on1=0;
02722   double avg_on2=0;
02723   double avg_of1=0;
02724   double avg_of2=0;
02725   double std=0;
02726 
02727   double sig_on_dif=0;
02728   double sig_of_dif=0;
02729   char* name=NULL;
02730   int i=0;
02731   int m=0;
02732 
02733   int llx=270;
02734   int lly=1000;
02735   int urx=320;
02736   int ury=1050;
02737   int zone[4];
02738   double gain=0;
02739   double dit_ref=0;
02740   double dit_tmp=0;
02741   double exptime_ref=0;
02742   double exptime_tmp=0;
02743   int kappa=3;
02744   int nclip=5;
02745 
02746   non = cpl_frameset_get_size(son);
02747   nof = cpl_frameset_get_size(sof);
02748   nfr = (non <= nof) ? non : nof;
02749 
02750   dit_on=cpl_vector_new(nfr);
02751   dit_of=cpl_vector_new(nfr);  
02752   exptime_on=cpl_vector_new(nfr);
02753   exptime_of=cpl_vector_new(nfr);
02754 
02755   for(i=0;i<nfr;i++) {
02756 
02757     frm=cpl_frameset_get_frame(son,i);
02758     name=(char*)cpl_frame_get_filename(frm);
02759     plist=cpl_propertylist_load(name,0);
02760     dit_ref=sinfo_pfits_get_dit(plist);
02761     exptime_ref=(double)sinfo_pfits_get_exp_time(plist);
02762     cpl_propertylist_delete(plist);
02763     cpl_vector_set(dit_on,i,dit_ref);
02764     cpl_vector_set(exptime_on,i,exptime_ref);
02765 
02766     frm=cpl_frameset_get_frame(sof,i);
02767     name=(char*)cpl_frame_get_filename(frm);
02768     plist=cpl_propertylist_load(name,0);
02769     dit_ref=sinfo_pfits_get_dit(plist);
02770     exptime_ref=(double)sinfo_pfits_get_exp_time(plist);
02771     cpl_propertylist_delete(plist);
02772     cpl_vector_set(dit_of,i,dit_ref);
02773     cpl_vector_set(exptime_of,i,exptime_ref);
02774 
02775   }
02776 
02777   zone[0]=270;
02778   zone[1]=1030;
02779   zone[2]=310;
02780   zone[3]=1060;
02781 
02782 
02783   res_tbl=cpl_table_new(nfr);
02784   cpl_table_new_column(res_tbl,"adu", CPL_TYPE_DOUBLE);
02785   cpl_table_new_column(res_tbl,"gain", CPL_TYPE_DOUBLE);
02786  
02787   for(i=0;i<nfr;i++) {
02788     frm=cpl_frameset_get_frame(son,i);
02789     name=(char*)cpl_frame_get_filename(frm);
02790     img_on1=cpl_image_load(name,CPL_TYPE_FLOAT,0,0);
02791 
02792     frm=cpl_frameset_get_frame(sof,i);
02793     name=(char*)cpl_frame_get_filename(frm);
02794     img_of1=cpl_image_load(name,CPL_TYPE_FLOAT,0,0);
02795 
02796 
02797     dit_ref=cpl_vector_get(dit_on,i);
02798     exptime_ref=cpl_vector_get(exptime_on,i);
02799 
02800    
02801     for(m=0;m<nfr; m++) {
02802       if(m != i) {
02803     frm=cpl_frameset_get_frame(son,m);
02804     name=(char*)cpl_frame_get_filename(frm);
02805     dit_tmp=cpl_vector_get(dit_on,m);
02806     exptime_tmp=cpl_vector_get(exptime_on,m);
02807     if(dit_tmp == dit_ref && exptime_tmp == exptime_ref) {
02808       /* sinfo_msg("m=%d i=%d\n",m,i); */
02809       img_on2=cpl_image_load(name,CPL_TYPE_FLOAT,0,0);
02810       frm=cpl_frameset_get_frame(sof,m);
02811       name=(char*)cpl_frame_get_filename(frm);
02812       img_of2=cpl_image_load(name,CPL_TYPE_FLOAT,0,0);
02813 
02814       img_on_dif=cpl_image_subtract_create(img_on1,img_on2);
02815       img_of_dif=cpl_image_subtract_create(img_of1,img_of2);
02816       
02817       sinfo_get_clean_mean_window(img_on1,llx,lly,urx,ury,kappa,
02818                                       nclip,&avg_on1,&std);
02819       sinfo_get_clean_mean_window(img_on2,llx,lly,urx,ury,kappa,
02820                                       nclip,&avg_on2,&std);
02821       sinfo_get_clean_mean_window(img_of1,llx,lly,urx,ury,kappa,
02822                                       nclip,&avg_of1,&std);
02823       sinfo_get_clean_mean_window(img_of2,llx,lly,urx,ury,kappa,
02824                                       nclip,&avg_of2,&std);
02825           cpl_flux_get_noise_window(img_on_dif, zone,2,100, &sig_on_dif, NULL);
02826           cpl_flux_get_noise_window(img_of_dif, zone,2,100, &sig_of_dif, NULL);
02827       
02828       cpl_image_delete(img_on2);
02829       cpl_image_delete(img_of2);
02830       cpl_image_delete(img_on_dif);
02831       cpl_image_delete(img_of_dif);
02832 
02833           gain=((avg_on1+avg_on2)-(avg_of1+avg_of2))/
02834                ((sig_on_dif*sig_on_dif)-(sig_of_dif*sig_of_dif));
02835 
02836           cpl_table_set_double(res_tbl,"gain",m,gain);
02837           cpl_table_set_double(res_tbl,"adu",m,
02838                                ((avg_on1+avg_on2)/2-(avg_of1+avg_of2)/2));
02839       /* sinfo_msg("gain=%f ADU=%f\n",gain,
02840                                (avg_on1+avg_on2)/2-(avg_of1+avg_of2)/2); */
02841 
02842     }
02843       }
02844     }
02845     cpl_image_delete(img_on1);
02846     cpl_image_delete(img_of1);
02847   }
02848   
02849 
02850       /*
02851       sinfo_get_clean_mean_window(img_on_dif,llx,lly,urx,ury,kappa,
02852                                       nclip,&avg,&sig_on_dif);
02853       sinfo_get_clean_mean_window(img_of_dif,llx,lly,urx,ury,kappa,
02854                                       nclip,&avg,&sig_of_dif);
02855       */
02856 
02857   cpl_vector_delete(dit_on);
02858   cpl_vector_delete(dit_of);
02859   cpl_vector_delete(exptime_on);
02860   cpl_vector_delete(exptime_of);
02861 
02862   return res_tbl;
02863 
02864 }
02865 
02866 cpl_table* sinfo_compute_linearity(cpl_frameset* son, cpl_frameset* sof)
02867 {
02868 
02869   cpl_frame*    frm=NULL;
02870 
02871   int* status=0;
02872   int non=0;
02873   int nof=0;
02874   int nfr=0;
02875   int i=0;
02876   double med_on=0;
02877   double avg_on=0;
02878   double med_of=0;
02879   double avg_of=0;
02880   double med_dit=0;
02881   double avg_dit=0;
02882 
02883   double med=0;
02884   double avg=0;
02885 
02886   char* name=NULL;
02887   cpl_image* img=NULL;
02888   cpl_vector* vec_adl=NULL;
02889   cpl_vector* vec_dit=NULL;
02890   cpl_vector* vec_avg=NULL;
02891   cpl_vector* vec_med=NULL;
02892   cpl_vector* vec_avg_dit=NULL;
02893   cpl_vector* vec_med_dit=NULL;
02894   cpl_propertylist* plist=NULL;
02895 
02896   double dit=0;
02897   cpl_table* lin_tbl=NULL;
02898 
02899 
02900   non = cpl_frameset_get_size(son);
02901   nof = cpl_frameset_get_size(sof);
02902   nfr = (non <= nof) ? non : nof;
02903 
02904   lin_tbl=cpl_table_new(nfr);
02905   cpl_table_new_column(lin_tbl,"med", CPL_TYPE_DOUBLE);
02906   cpl_table_new_column(lin_tbl,"avg", CPL_TYPE_DOUBLE);
02907   cpl_table_new_column(lin_tbl,"med_dit", CPL_TYPE_DOUBLE);
02908   cpl_table_new_column(lin_tbl,"avg_dit", CPL_TYPE_DOUBLE);
02909   cpl_table_new_column(lin_tbl,"dit", CPL_TYPE_DOUBLE);
02910   vec_med=cpl_vector_new(nfr);
02911   vec_avg=cpl_vector_new(nfr);
02912   vec_med_dit=cpl_vector_new(nfr);
02913   vec_avg_dit=cpl_vector_new(nfr);
02914   vec_dit=cpl_vector_new(nfr);
02915   vec_adl=cpl_vector_new(nfr);
02916 
02917   for(i=0;i<nfr;i++) {
02918     frm=cpl_frameset_get_frame(son,i);
02919     name=(char*)cpl_frame_get_filename(frm);
02920     img=cpl_image_load(name,CPL_TYPE_FLOAT,0,0);
02921     med_on=cpl_image_get_median(img);
02922     avg_on=cpl_image_get_mean(img);
02923     cpl_image_delete(img);
02924 
02925     frm=cpl_frameset_get_frame(sof,i);
02926     name=(char*)cpl_frame_get_filename(frm);
02927     img=cpl_image_load(name,CPL_TYPE_FLOAT,0,0);
02928     med_of=cpl_image_get_median(img);
02929     avg_of=cpl_image_get_mean(img);
02930     cpl_image_delete(img);
02931 
02932     med=med_on-med_of;
02933     avg=avg_on-avg_of;
02934     plist=cpl_propertylist_load(name,0);
02935     dit=(double)sinfo_pfits_get_dit(plist);
02936     cpl_propertylist_delete(plist);
02937     avg_dit=avg/dit;
02938     med_dit=med/dit;
02939 
02940     cpl_vector_set(vec_dit,i,dit);
02941     cpl_vector_set(vec_avg,i,avg);
02942     cpl_vector_set(vec_med,i,med);
02943     cpl_vector_set(vec_avg_dit,i,avg_dit);
02944     cpl_vector_set(vec_med_dit,i,med_dit);
02945 
02946     cpl_table_set_double(lin_tbl,"dit",i,dit);
02947     cpl_table_set_double(lin_tbl,"med",i,med);
02948     cpl_table_set_double(lin_tbl,"avg",i,avg);
02949     cpl_table_set_double(lin_tbl,"med_dit",i,med_dit);
02950     cpl_table_set_double(lin_tbl,"avg_dit",i,avg_dit);
02951 
02952   }
02953   cpl_table_new_column(lin_tbl,"adl", CPL_TYPE_DOUBLE);
02954   med_dit=cpl_vector_get_mean(vec_med_dit);
02955   avg_dit=cpl_vector_get_mean(vec_avg_dit);
02956 
02957   for(i=0;i<nfr;i++) {
02958     dit = cpl_table_get_double(lin_tbl,"dit",i,status);
02959     cpl_vector_set(vec_adl,i,dit*med_dit);
02960     cpl_table_set_double(lin_tbl,"adl",i,dit*med_dit);
02961   }
02962 
02963   cpl_vector_delete(vec_dit);
02964   cpl_vector_delete(vec_adl);
02965   cpl_vector_delete(vec_avg);
02966   cpl_vector_delete(vec_med);
02967   cpl_vector_delete(vec_avg_dit);
02968   cpl_vector_delete(vec_med_dit);
02969 
02970 
02971   return lin_tbl;
02972 
02973 }
02974 
02975 /*--------------------------------------------------------------------*/
02982 /*--------------------------------------------------------------------*/
02983 int 
02984 sinfo_get_ron(cpl_frameset    *   framelist,
02985           const int ron_xmin,
02986           const int ron_xmax,
02987           const int ron_ymin,
02988           const int ron_ymax,
02989           const int ron_hsize,
02990           const int ron_nsamp,
02991           double** ron)
02992 {
02993   cpl_imagelist   *   iset =NULL;
02994   cpl_image       *   tmp_im =NULL;
02995   int                 zone[4] ;
02996   double              rms  =0;
02997   double              ndit =0;
02998   cpl_frame       *   cur_frame =NULL;
02999   int                 i;
03000   cpl_propertylist* plist=NULL;     
03001 
03002   /* Test entries */
03003 
03004   if (framelist == NULL) return -1 ;
03005 
03006   /* Load the current set */
03007   if ((iset = sinfo_new_frameset_to_iset(framelist)) == NULL) {
03008     sinfo_msg_error( "Cannot load the data") ;
03009     return -1 ;
03010   }
03011 
03012   /* Initialise */
03013   zone[0]=ron_xmin;
03014   zone[1]=ron_xmax;
03015   zone[2]=ron_ymin;
03016   zone[3]=ron_ymax;
03017 
03018   /* Loop on all pairs */
03019   for (i=0 ; i<cpl_imagelist_get_size(iset)-1 ; i++) {
03020 
03021     /* Compute the current subtracted image */
03022     if ((tmp_im=cpl_image_subtract_create(cpl_imagelist_get(iset,i),
03023                       cpl_imagelist_get(iset, i+1))) 
03024     == NULL) {
03025       sinfo_msg_error( "Cannot subtract the images") ;
03026       sinfo_free_imagelist(&iset) ;
03027       return -1 ;
03028     }
03029 
03030     /* Compute the read-out noise */
03031     if (cpl_flux_get_noise_window(tmp_im, zone, ron_hsize,
03032                   ron_nsamp, &rms, NULL) != CPL_ERROR_NONE) {
03033       sinfo_msg_error( "Cannot compute the RON") ;
03034       sinfo_free_image(&tmp_im) ;
03035       sinfo_free_imagelist(&iset) ;
03036       return -1 ;
03037     }
03038     sinfo_free_image(&tmp_im) ;
03039     /* Normalise the RON with NDIT */
03040     cur_frame = cpl_frameset_get_frame(framelist, i) ;
03041     cknull_nomsg(plist=cpl_propertylist_load(cpl_frame_get_filename(cur_frame),0));
03042     ndit=sinfo_pfits_get_ndit(plist);
03043     sinfo_free_propertylist(&plist);
03044 
03045     (*ron)[i] = rms * sqrt(ndit/2.0) ;
03046 
03047   }
03048 
03049   /* Free and return */
03050   sinfo_free_imagelist(&iset) ;
03051   return 0 ;
03052 
03053  cleanup:
03054   sinfo_free_image(&tmp_im);
03055   sinfo_free_imagelist(&iset);
03056   sinfo_free_propertylist(&plist);
03057   return -1;
03058 
03059 }
03060 
03061 
03062 
03063 /*---------------------------------------------------------------------------*/
03069 /*---------------------------------------------------------------------------*/
03070 int sinfo_stack_get_pro_tag(char * tag_in, char* tag_out) 
03071 {
03072     /* Test entries */
03073     if (tag_in == NULL) return -1 ;
03074     /* here for the moment we set the same PRO ID as a non stacked frame */
03075     if (strcmp(tag_in,RAW_WAVE_LAMP_DITHER) == 0 ) {
03076          strcpy(tag_out,PRO_WAVE_LAMP_STACKED); 
03077          return 0 ;
03078     }
03079 
03080 
03081     if (strcmp(tag_in,RAW_WAVE_LAMP) == 0 ) {
03082          strcpy(tag_out,PRO_WAVE_LAMP_STACKED); 
03083          return 0 ;
03084     }
03085 
03086     if (strcmp(tag_in,RAW_WAVE_NS_DITHER) == 0 ) {
03087          strcpy(tag_out,PRO_WAVE_NS_STACKED); 
03088          return 0 ;
03089     }
03090 
03091 
03092     if (strcmp(tag_in,RAW_WAVE_NS) == 0 ) {
03093          strcpy(tag_out,PRO_WAVE_NS_STACKED); 
03094          return 0 ;
03095     }
03096 
03097 
03098     if (strcmp(tag_in,RAW_FIBRE_LAMP) == 0 ) {
03099          strcpy(tag_out,PRO_FIBRE_LAMP_STACKED); 
03100          return 0 ;
03101     }
03102 
03103     if (strcmp(tag_in,RAW_FIBRE_EW) == 0 ) {
03104          strcpy(tag_out,PRO_FIBRE_EW_STACKED); 
03105          return 0 ;
03106     }
03107 
03108     if (strcmp(tag_in,RAW_FIBRE_NS) == 0 ) {
03109          strcpy(tag_out,PRO_FIBRE_NS_STACKED); 
03110          return 0 ;
03111     }
03112 
03113 
03114     if (strcmp(tag_in,PRO_FIBRE_NS_STACKED_ON) == 0 ) {
03115          strcpy(tag_out,PRO_FIBRE_NS_STACKED); 
03116          return 0 ;
03117     }
03118 
03119     if (strcmp(tag_in,PRO_FIBRE_NS_STACKED) == 0 ) {
03120          strcpy(tag_out,PRO_FIBRE_NS_STACKED_DIST); 
03121          return 0 ;
03122     }
03123 
03124 
03125     if (strcmp(tag_in,RAW_SLIT_LAMP) == 0 ) {
03126          strcpy(tag_out,PRO_SLIT_LAMP_STACKED); 
03127          return 0 ;
03128     }
03129 
03130 
03131     if (strstr(tag_in, "FLUX") != NULL ) {
03132          strcpy(tag_out,PRO_FLUX_LAMP_STACKED); 
03133          return 0 ;
03134     }
03135 
03136     if (strstr(tag_in, "PSF") != NULL ) {
03137          strcpy(tag_out,PRO_PSF_CALIBRATOR_STACKED); 
03138          return 0 ;
03139     }
03140 
03141 
03142     if (strstr(tag_in, "FOCUS") != NULL ) {
03143          strcpy(tag_out,PRO_FOCUS_STACKED); 
03144          return 0 ;
03145     }
03146 
03147     if (strstr(tag_in, "OBJECT_NODDING") != NULL ) {
03148          strcpy(tag_out,PRO_OBJECT_NODDING_STACKED); 
03149          return 0 ;
03150     }
03151 
03152     if (strstr(tag_in, "SKY_NODDING") != NULL ) {
03153          strcpy(tag_out,PRO_SKY_NODDING_STACKED); 
03154          return 0 ;
03155     }
03156 
03157     if (strstr(tag_in, "STD_NODDING") != NULL ) {
03158          strcpy(tag_out,PRO_STD_NODDING_STACKED); 
03159          return 0 ;
03160     }
03161 
03162     if (strstr(tag_in, "OBJECT_SKYSPIDER") != NULL ) {
03163          strcpy(tag_out,PRO_OBJECT_SKYSPIDER_STACKED); 
03164          return 0 ;
03165     }
03166 
03167 
03168     if (strstr(tag_in, RAW_STD) != NULL ) {
03169          strcpy(tag_out,PRO_STD_STACKED); 
03170          return 0 ;
03171     }
03172 
03173 
03174     if (strstr(tag_in, RAW_SKY_STD) != NULL ) {
03175          strcpy(tag_out,PRO_SKY_STD_STACKED); 
03176          return 0 ;
03177     }
03178 
03179     if (strstr(tag_in, RAW_SKY_OH) != NULL ) {
03180          strcpy(tag_out,PRO_SKY_OH_STACKED); 
03181          return 0 ;
03182     }
03183 
03184     if (strstr(tag_in, RAW_SKY_PSF_CALIBRATOR) != NULL ) {
03185          strcpy(tag_out,PRO_SKY_PSF_CALIBRATOR_STACKED); 
03186          return 0 ;
03187     }
03188 
03189     if (strstr(tag_in, RAW_STD_STAR) != NULL ) {
03190          strcpy(tag_out,PRO_STD_STAR_STACKED); 
03191          return 0 ;
03192     }
03193 
03194     if (strstr(tag_in, RAW_STD_STAR) != NULL ) {
03195          strcpy(tag_out,PRO_STD_STAR_DITHER_STACKED); 
03196          return 0 ;
03197     }
03198 
03199     if (strstr(tag_in, RAW_SKY) != NULL ) {
03200          strcpy(tag_out,PRO_SKY_STACKED); 
03201          return 0 ;
03202     }
03203 
03204 
03205     return 1 ;
03206 }
03207 
03208 
03209 int sinfo_is_dark(char * tag) 
03210 {
03211     /* Test entries */
03212     if (tag == NULL) return -1 ;
03213     
03214     if (!strcmp(tag, RAW_DARK)) return 1 ;
03215     if (!strcmp(tag, PRO_MASTER_DARK)) return 1 ;
03216     return 0 ;
03217 }
03218 
03219 int sinfo_is_flat_bp(char * tag) 
03220 {
03221     /* Test entries */
03222     if (tag == NULL) return -1 ;
03223     
03224     if (!strcmp(tag, RAW_LINEARITY_LAMP)) return 1 ;
03225     return 0 ;
03226 }
03227 
03228 int sinfo_is_flat_lindet(char * tag) 
03229 {
03230     /* Test entries */
03231     if (tag == NULL) return -1 ;
03232     
03233     if (!strcmp(tag, RAW_LINEARITY_LAMP)) return 1 ;
03234     return 0 ;
03235 }
03236 
03237 
03238 int sinfo_blank2dot(const char * in, char* ou) 
03239 {
03240   int len=0;
03241   int i=0;
03242 
03243   strcpy(ou,in);
03244   len = strlen(in);
03245   for (i=0;i<len;i++)
03246     {
03247       if (in[i] == ' ') {
03248           ou[i] =  '.';
03249       }
03250     }
03251   return 0;
03252 }
03253 
03254 
03255 int sinfo_is_sky_flat(char * tag) 
03256 {
03257     /* Test entries */
03258     if (tag == NULL) return -1 ;
03259     if (!strcmp(tag, RAW_FLAT_SKY)) return 1 ;
03260     return 0 ;
03261 }
03262 
03263 
03264 
03265 int sinfo_is_master_flat(char * tag) 
03266 {
03267     /* Test entries */
03268     if (tag == NULL) return -1 ;
03269     
03270     if (!strcmp(tag, PRO_MASTER_FLAT_LAMP)) return 1 ;
03271     if (!strcmp(tag, PRO_MASTER_FLAT_LAMP1)) return 1 ;
03272     return 0 ;
03273 }
03274 
03275 int sinfo_is_master_flat_dither(char * tag) 
03276 {
03277     /* Test entries */
03278     if (tag == NULL) return -1 ;
03279     
03280     if (!strcmp(tag, PRO_MASTER_FLAT_LAMP2)) return 1 ;
03281     return 0 ;
03282 }
03283 
03284 /*---------------------------------------------------------------------------*/
03290 /*---------------------------------------------------------------------------*/
03291 int sinfo_is_stack(char * tag) 
03292 {
03293     /* Test entries */
03294     if (tag == NULL) return -1 ;
03295     
03296     if (strstr(tag, PRO_STACKED) != NULL) return 1 ;
03297     return 0 ;
03298 }
03299 
03300 int sinfo_is_mflat(char * tag) 
03301 {
03302     /* Test entries */
03303     if (tag == NULL) return -1 ;
03304     
03305     if (!strcmp(tag, PRO_MASTER_FLAT_LAMP)) return 1 ;
03306     if (!strcmp(tag, PRO_MASTER_FLAT_LAMP1)) return 1 ;
03307     if (!strcmp(tag, PRO_MASTER_FLAT_LAMP2)) return 1 ;
03308     return 0 ;
03309 }
03310 
03311 
03312 /*---------------------------------------------------------------------------*/
03318 /*---------------------------------------------------------------------------*/
03319 int sinfo_is_psf_calibrator_stacked(char * tag) 
03320 {
03321     /* Test entries */
03322     if (tag == NULL) return -1 ;
03323     
03324     if (!strcmp(tag, PRO_PSF_CALIBRATOR_STACKED)) return 1 ;
03325     return 0 ;
03326 }
03327 /*---------------------------------------------------------------------------*/
03333 /*---------------------------------------------------------------------------*/
03334 int sinfo_is_focus_stacked(char * tag) 
03335 {
03336     /* Test entries */
03337     if (tag == NULL) return -1 ;
03338     
03339     if (!strcmp(tag, PRO_FOCUS_STACKED)) return 1 ;
03340     return 0 ;
03341 }
03342 
03343 /*---------------------------------------------------------------------------*/
03349 /*---------------------------------------------------------------------------*/
03350 int sinfo_is_lamp_wave_stacked(char * tag) 
03351 {
03352     /* Test entries */
03353     if (tag == NULL) return -1 ;
03354     
03355     if (!strcmp(tag, PRO_WAVE_LAMP_STACKED)) return 1 ;
03356     return 0 ;
03357 }
03358 
03359 /*---------------------------------------------------------------------------*/
03365 /*---------------------------------------------------------------------------*/
03366 int sinfo_is_lamp_flux_stacked(char * tag) 
03367 {
03368     /* Test entries */
03369     if (tag == NULL) return -1 ;
03370     
03371     if (!strcmp(tag, PRO_FLUX_LAMP_STACKED)) return 1 ;
03372     return 0 ;
03373 }
03374 
03375 /*---------------------------------------------------------------------------*/
03381 /*---------------------------------------------------------------------------*/
03382 int sinfo_is_object_nodding_stacked(char * tag) 
03383 {
03384     /* Test entries */
03385     if (tag == NULL) return -1 ;
03386     
03387     if (!strcmp(tag, PRO_OBJECT_NODDING_STACKED)) return 1 ;
03388     return 0 ;
03389 }
03390 
03391 /*---------------------------------------------------------------------------*/
03397 /*---------------------------------------------------------------------------*/
03398 int sinfo_is_object_skyspider_stacked(char * tag) 
03399 {
03400     /* Test entries */
03401     if (tag == NULL) return -1 ;
03402     
03403     if (!strcmp(tag, PRO_OBJECT_SKYSPIDER_STACKED)) return 1 ;
03404     return 0 ;
03405 }
03406 
03407 
03408 /*---------------------------------------------------------------------------*/
03414 /*---------------------------------------------------------------------------*/
03415 int sinfo_is_sky_nodding_stacked(char * tag) 
03416 {
03417     /* Test entries */
03418     if (tag == NULL) return -1 ;
03419     
03420     if (!strcmp(tag, PRO_SKY_NODDING_STACKED)) return 1 ;
03421     return 0 ;
03422 }
03423 
03424 /*---------------------------------------------------------------------------*/
03430 /*---------------------------------------------------------------------------*/
03431 int sinfo_is_wavemap(char * tag) 
03432 {
03433     /* Test entries */
03434     if (tag == NULL) return -1 ;
03435 
03436     if (!strcmp(tag, PRO_WAVE_MAP)) return 1 ;
03437     return 0 ;
03438 }
03439 
03440 /*---------------------------------------------------------------------------*/
03446 /*---------------------------------------------------------------------------*/
03447 int sinfo_is_halosp(char * tag) 
03448 {
03449     /* Test entries */
03450     if (tag == NULL) return -1 ;
03451     
03452     if (!strcmp(tag, PRO_HALO_SPECT)) return 1 ;
03453     return 0 ;
03454 }
03455 
03456 /*---------------------------------------------------------------------------*/
03462 /*---------------------------------------------------------------------------*/
03463 int sinfo_is_distlist(char * tag) 
03464 {
03465     /* Test entries */
03466     if (tag == NULL) return -1 ;
03467     
03468     if (!strcmp(tag, PRO_SLITLETS_DISTANCE)) return 1 ;
03469     return 0 ;
03470 }
03471 
03472 /*---------------------------------------------------------------------------*/
03478 /*---------------------------------------------------------------------------*/
03479 int sinfo_is_slitpos(char * tag) 
03480 {
03481     /* Test entries */
03482     if (tag == NULL) return -1 ;
03483     
03484     if (!strcmp(tag, PRO_SLIT_POS)) return 1 ;
03485     return 0 ;
03486 }
03487 
03488 /*---------------------------------------------------------------------------*/
03494 /*---------------------------------------------------------------------------*/
03495 int sinfo_is_firstcol(char * tag) 
03496 {
03497     /* Test entries */
03498     if (tag == NULL) return -1 ;
03499     
03500     if (!strcmp(tag, PRO_FIRST_COL)) return 1 ;
03501     return 0 ;
03502 }
03503 
03504 /*---------------------------------------------------------------------------*/
03510 /*---------------------------------------------------------------------------*/
03511 int sinfo_is_bpmap(char * tag) 
03512 {
03513     /* Test entries */
03514     if (tag == NULL) return -1 ;
03515     
03516     if (!strcmp(tag, PRO_BP_MAP)) return 1 ;
03517     return 0 ;
03518 }
03519 
03520 
03521 /*---------------------------------------------------------------------------*/
03530 /*---------------------------------------------------------------------------*/
03531 
03532 int sinfo_get_band(cpl_frame * ref_frame,char * band)
03533 {
03534 
03535   char* ref_file=NULL;
03536   cpl_propertylist* plist=NULL;
03537 
03538   ref_file = cpl_strdup(cpl_frame_get_filename(ref_frame)) ;
03539   if ((cpl_error_code)((plist = cpl_propertylist_load(ref_file, 0)) == NULL)) {
03540       sinfo_msg_error( "getting header from reference frame %s",ref_file);
03541       cpl_propertylist_delete(plist) ;
03542       return -1 ;
03543   }
03544 
03545   if (cpl_propertylist_contains(plist, KEY_NAME_FILT_NAME)) {
03546       strcpy(band, cpl_propertylist_get_string(plist, KEY_NAME_FILT_NAME));
03547       /* sinfo_msg("%s value is %s", KEY_NAME_FILT_NAME, band); */
03548 
03549   } else {
03550       sinfo_msg_warning("keyword %s does not exist",KEY_NAME_FILT_NAME);
03551       return -1;
03552   }
03553 
03554   cpl_free(ref_file);
03555   cpl_propertylist_delete(plist);
03556   return 0;
03557 }
03558 
03559 
03560 
03561 
03562 /*---------------------------------------------------------------------------*/
03570 /*---------------------------------------------------------------------------*/
03571 
03572 int sinfo_get_obsname(cpl_frame * ref_frame, const char* obs_name)
03573 {
03574 
03575   char* ref_file=NULL;
03576   cpl_propertylist* plist=NULL;
03577 
03578   ref_file = cpl_strdup(cpl_frame_get_filename(ref_frame)) ;
03579   if ((cpl_error_code)((plist = cpl_propertylist_load(ref_file, 0)) == NULL)) {
03580       sinfo_msg_error( "getting header from reference frame %s",ref_file);
03581       cpl_propertylist_delete(plist) ;
03582       return -1 ;
03583   }
03584 
03585   if (cpl_propertylist_contains(plist, KEY_NAME_OBS_NAME)) {
03586       strcpy((char*)obs_name, cpl_propertylist_get_string(plist, 
03587                               KEY_NAME_OBS_NAME));
03588       /* sinfo_msg("%s value is %s", KEY_NAME_OBS_NAME, obs_name); */
03589 
03590   } else {
03591       sinfo_msg_warning("keyword %s does not exist",KEY_NAME_OBS_NAME);
03592       return -1;
03593   }
03594 
03595   cpl_free(ref_file);
03596   cpl_propertylist_delete(plist);
03597   return 0;
03598 }
03599 
03600 
03601 
03602 
03603 
03604 /*---------------------------------------------------------------------------*/
03612 /*---------------------------------------------------------------------------*/
03613 
03614 int sinfo_get_keyvalue_int(cpl_frame * ref_frame, const char* key_name)
03615 {
03616 
03617   char* ref_file=NULL;
03618   cpl_propertylist* plist=NULL;
03619   int result=0;
03620 
03621   ref_file = cpl_strdup(cpl_frame_get_filename(ref_frame)) ;
03622 
03623   if ((cpl_error_code)((plist = cpl_propertylist_load(ref_file, 0)) == NULL)) {
03624       sinfo_msg_error( "getting header from reference frame %s",ref_file);
03625       cpl_propertylist_delete(plist) ;
03626       return -1;
03627   }
03628 
03629 
03630   if (cpl_propertylist_contains(plist, key_name)) {
03631       result=cpl_propertylist_get_int(plist,key_name);
03632   } else {
03633       sinfo_msg_warning("keyword %s does not exist",key_name);
03634       return -1;
03635   }
03636 
03637   cpl_free(ref_file);
03638   cpl_propertylist_delete(plist);
03639 
03640   return result;
03641 }
03642 
03643 
03644 
03645 /*---------------------------------------------------------------------------*/
03653 /*---------------------------------------------------------------------------*/
03654 
03655 float sinfo_get_keyvalue_float(cpl_frame * ref_frame, const char* key_name)
03656 {
03657 
03658   char* ref_file=NULL;
03659   cpl_propertylist* plist=NULL;
03660   float result=0;
03661 
03662   ref_file = cpl_strdup(cpl_frame_get_filename(ref_frame)) ;
03663 
03664   if ((cpl_error_code)((plist = cpl_propertylist_load(ref_file, 0)) == NULL)) {
03665       sinfo_msg_error( "getting header from reference frame %s",ref_file);
03666       cpl_propertylist_delete(plist) ;
03667       return -1;
03668   }
03669 
03670 
03671   if (cpl_propertylist_contains(plist, key_name)) {
03672       result=cpl_propertylist_get_float(plist,key_name);
03673   } else {
03674       sinfo_msg_warning("keyword %s does not exist",key_name);
03675       return -1;
03676   }
03677 
03678   cpl_free(ref_file);
03679   cpl_propertylist_delete(plist);
03680 
03681   return result;
03682 }
03683 
03684 
03685 /*---------------------------------------------------------------------------*/
03693 /*---------------------------------------------------------------------------*/
03694 
03695 char sinfo_get_keyvalue_bool(cpl_frame * ref_frame, const char* key_name)
03696 {
03697 
03698   char* ref_file=NULL;
03699   cpl_propertylist* plist=NULL;
03700   int res_val=0;
03701   
03702   ref_file = cpl_strdup(cpl_frame_get_filename(ref_frame)) ;
03703 
03704   if ((cpl_error_code)((plist = cpl_propertylist_load(ref_file, 0)) == NULL)) {
03705       sinfo_msg_error( "getting header from reference frame %s",ref_file);
03706       cpl_propertylist_delete(plist) ;
03707       return '0';
03708   }
03709 
03710 
03711   if (cpl_propertylist_contains(plist, key_name)) {
03712       res_val=cpl_propertylist_get_bool(plist,key_name);
03713   } else {
03714       sinfo_msg_warning("keyword %s does not exist",key_name);
03715       return '0';
03716   }
03717 
03718   cpl_free(ref_file);
03719   cpl_propertylist_delete(plist);
03720   if(res_val == 1) {
03721     return 'T';
03722   } else {
03723     return 'F';
03724   }
03725 }
03726 
03727 
03728 
03729 
03730 /*---------------------------------------------------------------------------*/
03738 /*---------------------------------------------------------------------------*/
03739 
03740 const char* 
03741 sinfo_get_keyvalue_string(cpl_frame * ref_frame, const char* key_name)
03742 {
03743 
03744   char* ref_file=NULL;
03745   cpl_propertylist* plist=NULL;
03746   const char* result=NULL;
03747 
03748   ref_file = cpl_strdup(cpl_frame_get_filename(ref_frame)) ;
03749 
03750   if ((cpl_error_code)((plist = cpl_propertylist_load(ref_file, 0)) == NULL)) {
03751       sinfo_msg_error( "getting header from reference frame %s",ref_file);
03752       cpl_propertylist_delete(plist) ;
03753       return FALSE;
03754   }
03755 
03756 
03757   if (cpl_propertylist_contains(plist, key_name)) {
03758       result=cpl_propertylist_get_string(plist,key_name);
03759   } else {
03760       sinfo_msg_warning("keyword %s does not exist",key_name);
03761       return NULL;
03762   }
03763 
03764   cpl_free(ref_file);
03765   cpl_propertylist_delete(plist);
03766 
03767   return result;
03768 }
03769 
03770 
03771 
03772 double sinfo_get_mjd_obs(cpl_frame * frame)
03773 {
03774   cpl_propertylist* plist=NULL;
03775   char* file=NULL;
03776 
03777   double mjd_obs=0.;
03778   file = cpl_strdup( cpl_frame_get_filename(frame)) ;
03779 
03780   if ((cpl_error_code)((plist = cpl_propertylist_load(file, 0)) == NULL)) {
03781       sinfo_msg_error( "getting header from reference frame %s",file);
03782       cpl_propertylist_delete(plist) ;
03783       cpl_free(file);
03784       return -1 ;
03785   }
03786 
03787   if (cpl_propertylist_contains(plist, KEY_NAME_MJD_OBS)) {
03788       mjd_obs=cpl_propertylist_get_double(plist, KEY_NAME_MJD_OBS);
03789   } else {
03790       sinfo_msg_error("keyword %s does not exist",KEY_NAME_MJD_OBS);
03791       cpl_propertylist_delete(plist) ;
03792       return -1;
03793   }
03794   cpl_propertylist_delete(plist) ;
03795   cpl_free(file);
03796 
03797   return mjd_obs;
03798 
03799 }
03800 
03801 
03802 
03803 
03804 double sinfo_get_cumoffsetx(cpl_frame * frame)
03805 {
03806   cpl_propertylist* plist=NULL;
03807   char* file=NULL;
03808 
03809   double result=0.;
03810   file = cpl_strdup( cpl_frame_get_filename(frame)) ;
03811 
03812   if ((cpl_error_code)((plist = cpl_propertylist_load(file, 0)) == NULL)) {
03813       sinfo_msg_error( "getting header from reference frame %s",file);
03814       cpl_propertylist_delete(plist) ;
03815       cpl_free(file);
03816       return -1 ;
03817   }
03818 
03819   if (cpl_propertylist_contains(plist, KEY_NAME_CUMOFFX)) {
03820       result=cpl_propertylist_get_double(plist, KEY_NAME_CUMOFFX);
03821   } else {
03822       sinfo_msg_error("keyword %s does not exist",KEY_NAME_CUMOFFX);
03823       cpl_propertylist_delete(plist) ;
03824       return -1;
03825   }
03826   cpl_propertylist_delete(plist) ;
03827   cpl_free(file);
03828 
03829   return result;
03830 
03831 }
03832 
03833 
03834 
03835 
03836 double sinfo_get_cumoffsety(cpl_frame * frame)
03837 {
03838   cpl_propertylist* plist=NULL;
03839   char* file=NULL;
03840 
03841   double result=0.;
03842   file = cpl_strdup( cpl_frame_get_filename(frame)) ;
03843 
03844   if ((cpl_error_code)((plist = cpl_propertylist_load(file, 0)) == NULL)) {
03845       sinfo_msg_error( "getting header from reference frame %s",file);
03846       cpl_propertylist_delete(plist) ;
03847       cpl_free(file);
03848       return -1 ;
03849   }
03850 
03851   if (cpl_propertylist_contains(plist, KEY_NAME_CUMOFFY)) {
03852       result=cpl_propertylist_get_double(plist, KEY_NAME_CUMOFFY);
03853   } else {
03854       sinfo_msg_error("keyword %s does not exist",KEY_NAME_CUMOFFY);
03855       cpl_propertylist_delete(plist) ;
03856       return -1;
03857   }
03858   cpl_propertylist_delete(plist) ;
03859   cpl_free(file);
03860 
03861   return result;
03862 
03863 }
03864 
03865 int sinfo_frame_is_dither(cpl_frame * frame)
03866 {
03867 
03868   char* file=NULL;
03869   char band[FILE_NAME_SZ];
03870 
03871 
03872   cpl_propertylist* plist=NULL;
03873   int grat_encoder=0;
03874   int dith_status=1;
03875 
03876 
03877   file = cpl_strdup(cpl_frame_get_filename(frame)) ;
03878   if ((cpl_error_code)((plist = cpl_propertylist_load(file, 0)) == NULL)) {
03879       sinfo_msg_error( "getting header from reference frame %s",file);
03880       cpl_propertylist_delete(plist) ;
03881       cpl_free(file);
03882       return -1 ;
03883   }
03884 
03885   if (cpl_propertylist_contains(plist, KEY_NAME_FILT_NAME)) {
03886       strcpy(band,cpl_propertylist_get_string(plist, KEY_NAME_FILT_NAME));
03887   } else {
03888       sinfo_msg_error("keyword %s does not exist",KEY_NAME_FILT_NAME);
03889       cpl_free(file);
03890       return -1;
03891   }
03892 
03893   if (cpl_propertylist_contains(plist, KEY_NAME_GRAT_ENC)) {
03894       grat_encoder = cpl_propertylist_get_int(plist, KEY_NAME_GRAT_ENC);
03895   } else {
03896       sinfo_msg_error("keyword %s does not exist",KEY_NAME_GRAT_ENC);
03897       cpl_free(file);
03898       return -1;
03899   }
03900 
03901   cpl_propertylist_delete(plist);
03902 
03903       if (strcmp(band,"H") == 0) {
03904     if( abs(grat_encoder - GRAT_VAL2_H) <= GRAT_VAL_TOL ) {
03905       dith_status = 0;
03906     } else {
03907       dith_status = 0;
03908     } 
03909       }
03910  else if (strcmp(band,"H+K") == 0) {
03911     if( abs(grat_encoder - GRAT_VAL2_HK) <= GRAT_VAL_TOL ) {
03912       dith_status = 0;
03913     } else {
03914       dith_status = 0;
03915     } 
03916  }
03917  else if (strcmp(band,"K") == 0) {
03918     if( abs(grat_encoder - GRAT_VAL2_K) <= GRAT_VAL_TOL ) {
03919       dith_status = 0;
03920     } else {
03921       dith_status = 0;
03922     } 
03923  }
03924  else if (strcmp(band,"J") == 0) {
03925     if( abs(grat_encoder - GRAT_VAL2_J) <= GRAT_VAL_TOL ) {
03926       dith_status = 0;
03927     } else {
03928       dith_status = 0;
03929     } 
03930  }
03931       cpl_free(file);
03932  return dith_status;
03933 }
03934 
03935 /*---------------------------------------------------------------------------*/
03944 /*---------------------------------------------------------------------------*/
03945 
03946 int sinfo_get_spatial_res(cpl_frame * ref_frame, char * spat_res)
03947 {
03948 
03949   char* ref_file;
03950   cpl_propertylist* plist=NULL;
03951 
03952   ref_file=(char*)cpl_frame_get_filename(ref_frame) ;
03953   if ((cpl_error_code)((plist = cpl_propertylist_load(ref_file, 0)) == NULL)) {
03954       sinfo_msg_error( "getting header from reference frame %s",ref_file);
03955       cpl_propertylist_delete(plist) ;
03956       return -1 ;
03957 
03958   }  
03959 
03960   if (cpl_propertylist_contains(plist, KEY_NAME_PREOPTICS)) {
03961       strcpy(spat_res,cpl_propertylist_get_string(plist, KEY_NAME_PREOPTICS));
03962       /* sinfo_msg("%s value is %s", KEY_NAME_PREOPTICS, spat_res); */
03963   } else {
03964       sinfo_msg_warning("keyword %s does not exist",KEY_NAME_PREOPTICS);
03965       cpl_free(ref_file);
03966       return -1;
03967   }
03968   cpl_propertylist_delete(plist);
03969   return 0;
03970 
03971 }
03972 
03973 /*---------------------------------------------------------------------------*/
03981 /*---------------------------------------------------------------------------*/
03982 
03983 int sinfo_frame_is_sky(cpl_frame * ref_frame)
03984 {
03985 
03986   char  dpr_type[FILE_NAME_SZ];
03987   char* ref_file=NULL;
03988   const char* sval=NULL;
03989 
03990   int result=0;
03991   cpl_propertylist* plist=NULL;
03992 
03993   sval = cpl_frame_get_filename(ref_frame) ;
03994   ref_file = cpl_strdup(sval) ;
03995 
03996   if ((cpl_error_code)((plist = cpl_propertylist_load(ref_file, 0)) == NULL)) {
03997       sinfo_msg_error( "getting header from reference frame %s",ref_file);
03998       cpl_propertylist_delete(plist) ;
03999       cpl_free(ref_file);
04000       return -1 ;
04001   }
04002 
04003   if (cpl_propertylist_contains(plist, KEY_NAME_DPR_TYPE)) {
04004       strcpy(dpr_type,cpl_propertylist_get_string(plist, KEY_NAME_DPR_TYPE));
04005       /* sinfo_msg("%s value is %d", KEY_NAME_DPR_TYPE, dpr_type); */
04006   } else {
04007       sinfo_msg_warning("keyword %s does not exist",KEY_NAME_DPR_TYPE);
04008       cpl_propertylist_delete(plist) ;
04009       cpl_free(ref_file);
04010       return -1;
04011   }
04012   cpl_propertylist_delete(plist);
04013   if(strstr(dpr_type,RAW_SKY) != NULL) {
04014     result=1;
04015   }
04016   cpl_free(ref_file);
04017 
04018   return result;
04019 
04020 }
04021 
04022 
04023 /*---------------------------------------------------------------------------*/
04031 /*---------------------------------------------------------------------------*/
04032 
04033 int sinfo_tag_is_sky(char * tag)
04034 {
04035 
04036   int result=0;
04037   
04038   if(
04039      (strcmp(tag,RAW_SKY) == 0)         ||
04040      (strcmp(tag,RAW_IMAGE_PRE_SKY) == 0)         ||
04041      (strcmp(tag,RAW_SKY_NODDING) == 0) ||
04042      (strcmp(tag,RAW_SKY_JITTER) == 0) ||
04043      (strcmp(tag,RAW_SKY_STD) == 0)     || 
04044      (strcmp(tag,RAW_FIBRE_DARK) == 0) || 
04045      (strcmp(tag,RAW_SKY_OH) == 0)      || 
04046      (strcmp(tag,RAW_SKY_PSF_CALIBRATOR) == 0) 
04047     ) {
04048     result=1;
04049   }
04050 
04051   return result;
04052 
04053 }
04054 
04055 
04056 /*---------------------------------------------------------------------------*/
04064 /*---------------------------------------------------------------------------*/
04065 
04066 int sinfo_tag_is_obj(char * tag)
04067 {
04068 
04069   int result=0;
04070   
04071   if(
04072      (strcmp(tag,RAW_PUPIL_LAMP) == 0) ||
04073      (strcmp(tag,RAW_OBJECT) == 0)         ||
04074      (strcmp(tag,RAW_IMAGE_PRE_OBJECT) == 0)         ||
04075      (strcmp(tag,RAW_OBJECT_NODDING) == 0) ||
04076      (strcmp(tag,RAW_OBJECT_JITTER) == 0) ||
04077      (strcmp(tag,RAW_PSF_CALIBRATOR) == 0) || 
04078      (strcmp(tag,RAW_FIBRE_PSF) == 0) || 
04079      (strcmp(tag,RAW_STD) == 0)            ||
04080      (strcmp(tag,RAW_STD_STAR) == 0)  
04081 
04082     ) {
04083     result=1;
04084   }
04085 
04086   return result;
04087 
04088 }
04089 
04090 /*---------------------------------------------------------------------------*/
04098 /*---------------------------------------------------------------------------*/
04099 
04100 int sinfo_tag_is_objpro(char * tag)
04101 {
04102 
04103   int result=0;
04104   
04105   if(
04106      (strcmp(tag,PRO_COADD_OBJ) == 0) ||
04107      (strcmp(tag,PRO_COADD_PSF) == 0) ||
04108      (strcmp(tag,PRO_COADD_STD) == 0) ||
04109      (strcmp(tag,PRO_OBS_OBJ) == 0) ||
04110      (strcmp(tag,PRO_OBS_PSF) == 0) ||
04111      (strcmp(tag,PRO_OBS_STD) == 0) ||
04112      (strcmp(tag,PRO_PSF_CALIBRATOR_STACKED) == 0) ||
04113      (strcmp(tag,PRO_SKY_PSF_CALIBRATOR_STACKED) == 0) ||
04114      (strcmp(tag,PRO_STD_STACKED) == 0) ||
04115      (strcmp(tag,PRO_SKY_STD_STACKED) == 0) ||
04116      (strcmp(tag,PRO_OBJECT_NODDING_STACKED) == 0) ||
04117      (strcmp(tag,PRO_SKY_NODDING_STACKED) == 0) 
04118     ) {
04119     result=1;
04120   }
04121 
04122   return result;
04123 
04124 }
04125 
04126 
04127 /*---------------------------------------------------------------------------*/
04135 /*---------------------------------------------------------------------------*/
04136 
04137 int sinfo_frame_is_on(cpl_frame * ref_frame)
04138 {
04139 
04140   char* ref_file=NULL;
04141   char  dpr_type[FILE_NAME_SZ];
04142   int lamp_Xe=0;
04143   int lamp_Kr=0;
04144   int lamp_Ne=0;
04145   int lamp_Ar=0;
04146   int lamp_Halo=0;
04147 
04148   int result=0;
04149   cpl_propertylist* plist=NULL;
04150 
04151   ref_file = cpl_strdup(cpl_frame_get_filename(ref_frame)) ; 
04152   if ((cpl_error_code)((plist = cpl_propertylist_load(ref_file,0)) == NULL)) {
04153       sinfo_msg_error( "getting header from reference frame %s",ref_file);
04154       cpl_propertylist_delete(plist) ;
04155       cpl_free(ref_file);
04156       return -1 ;
04157   }
04158 
04159 /*-----------------------------------------------------------------------
04160 in J  Argon (4)
04161 in H Xenon and Argon (1+4)
04162 in K Neon (3)
04163 in H+K Xenon (1) 
04164 -------------------------------------------------------------------------*/
04165  if (cpl_propertylist_contains(plist, KEY_NAME_DPR_TYPE)) {
04166       strcpy(dpr_type,cpl_propertylist_get_string(plist, KEY_NAME_DPR_TYPE));
04167       /* sinfo_msg("%s value is %s", KEY_NAME_DPR_TYPE, dpr_type); */
04168   } else {
04169       sinfo_msg_warning("keyword %s does not exist",KEY_NAME_DPR_TYPE);
04170       cpl_propertylist_delete(plist); 
04171       cpl_free(ref_file);
04172       return -1;
04173   }
04174 
04175  /*
04176    In order to use the frame tag to identify frames we have to add this line
04177    strcpy(dpr_type,cpl_frame_get_tag(ref_frame));
04178 
04179  */
04180 
04181   if(strstr(dpr_type,"STD") != NULL) {
04182     result = 1;
04183     cpl_propertylist_delete(plist); 
04184     cpl_free(ref_file);
04185     return result;
04186   }
04187 
04188   if(strstr(dpr_type,"PSF") != NULL) {
04189     result = 1;
04190     cpl_propertylist_delete(plist); 
04191     cpl_free(ref_file);
04192     return result;
04193   }
04194 
04195   if(strstr(dpr_type,"SKY") != NULL) {
04196     result = 0;
04197     cpl_propertylist_delete(plist);
04198     cpl_free(ref_file);
04199     return result;
04200   }
04201 
04202 
04203   if(strstr(dpr_type,"OBJECT") != NULL) {
04204     result = 1;
04205     cpl_propertylist_delete(plist);
04206     cpl_free(ref_file);
04207     return result;
04208   }
04209   /*
04210   if(strstr(dpr_type,"PUPIL") != NULL) {
04211     result = 1;
04212     cpl_propertylist_delete(plist);
04213     cpl_free(ref_file);
04214     return result;
04215   }
04216   */
04217 
04218   if (cpl_propertylist_contains(plist, KEY_NAME_LAMP_XE)) {
04219       lamp_Xe=cpl_propertylist_get_bool(plist, KEY_NAME_LAMP_XE);
04220       /* sinfo_msg("%s value is %d", KEY_NAME_LAMP_XE, lamp_Xe); */
04221   } else {
04222       sinfo_msg_warning("keyword %s does not exist",KEY_NAME_LAMP_XE);
04223       cpl_propertylist_delete(plist);
04224       cpl_free(ref_file);
04225       return -1;
04226   }
04227 
04228   if (cpl_propertylist_contains(plist, KEY_NAME_LAMP_KR)) {
04229       lamp_Kr=cpl_propertylist_get_bool(plist, KEY_NAME_LAMP_KR);
04230       /* sinfo_msg("%s value is %d", KEY_NAME_LAMP_KR, lamp_Kr); */
04231   } else {
04232       sinfo_msg_warning("keyword %s does not exist",KEY_NAME_LAMP_KR);
04233       cpl_propertylist_delete(plist);
04234       cpl_free(ref_file);
04235       return -1;
04236   }
04237 
04238   if (cpl_propertylist_contains(plist, KEY_NAME_LAMP_NE)) {
04239       lamp_Ne=cpl_propertylist_get_bool(plist, KEY_NAME_LAMP_NE);
04240       /* sinfo_msg("%s value is %d", KEY_NAME_LAMP_NE, lamp_Ne); */
04241   } else {
04242       sinfo_msg_warning("keyword %s does not exist",KEY_NAME_LAMP_NE); 
04243       cpl_propertylist_delete(plist);
04244       cpl_free(ref_file);
04245       return -1;
04246   }
04247 
04248   if (cpl_propertylist_contains(plist, KEY_NAME_LAMP_AR)) {
04249       lamp_Ar=cpl_propertylist_get_bool(plist, KEY_NAME_LAMP_AR);
04250       /* sinfo_msg("%s value is %d", KEY_NAME_LAMP_AR, lamp_Ar); */
04251   } else {
04252       sinfo_msg_warning("keyword %s does not exist",KEY_NAME_LAMP_AR);
04253       cpl_propertylist_delete(plist);
04254       cpl_free(ref_file);
04255       return -1;
04256   }
04257 
04258   if (cpl_propertylist_contains(plist, KEY_NAME_LAMP_HALO)) {
04259       lamp_Halo=cpl_propertylist_get_bool(plist, KEY_NAME_LAMP_HALO);
04260       /* sinfo_msg("%s value is %d", KEY_NAME_LAMP_HALO, lamp_Halo); */
04261   } else {
04262       sinfo_msg_warning("keyword %s does not exist",KEY_NAME_LAMP_HALO); 
04263       cpl_propertylist_delete(plist);
04264       cpl_free(ref_file);
04265       return -1;
04266   }
04267 
04268 
04269 
04270 
04271   if(lamp_Xe) {
04272     result=1;
04273   }
04274 
04275   if(lamp_Kr) {
04276     result=1;
04277   }
04278 
04279   if(lamp_Ne) {
04280     result=1;
04281   }
04282 
04283   if(lamp_Ar) {
04284     result=1;
04285   }
04286 
04287 
04288   if(lamp_Halo) {
04289     result=1;
04290   }
04291 
04292    cpl_propertylist_delete(plist);
04293    cpl_free(ref_file);
04294   return result;
04295 
04296 
04297 }
04298 
04299 
04300 
04301 int 
04302 sinfo_pfits_add_qc(cpl_propertylist       *   plist,
04303                    qc_log          *   qclog)
04304 {
04305     char            key_name[80] ;
04306     char            key_value[80] ;
04307 
04308     int             i =0;
04309 
04310     /* Test entries */
04311     if (plist == NULL) return -1 ;
04312 
04313     /* Parameter Name:    PIPEFILE */
04314     /* we add ESO prefix to FITS keywords" */
04315     for(i=0;i<qclog[0].n;i++) {
04316        strcpy(key_name,"ESO ");
04317        strcat(key_name,qclog[i].name);
04318        if(strcmp(qclog[i].type,"string") == 0) {
04319          snprintf(key_value,MAX_NAME_SIZE-1,"%s",qclog[i].s_val);
04320          cpl_propertylist_append_string(plist, key_name,key_value) ;
04321          cpl_propertylist_set_comment(plist, key_name,qclog[i].comm) ;
04322 
04323        } else if(strcmp(qclog[i].type,"bool") == 0) {
04324          snprintf(key_value,MAX_NAME_SIZE-1,"%i",(int)qclog[i].n_val);
04325          cpl_propertylist_append_bool(plist, key_name,(int)qclog[i].n_val) ;
04326          cpl_propertylist_set_comment(plist, key_name,qclog[i].comm) ;
04327        } else if(strcmp(qclog[i].type,"int") == 0) {
04328          snprintf(key_value,MAX_NAME_SIZE-1,"%i",(int)qclog[i].n_val);
04329          cpl_propertylist_append_int(plist, key_name,(int)qclog[i].n_val) ;
04330          cpl_propertylist_set_comment(plist, key_name,qclog[i].comm) ;
04331        } else if(strcmp(qclog[i].type,"float") == 0) {
04332          snprintf(key_value,MAX_NAME_SIZE-1,"%f",(float)qclog[i].n_val);
04333          cpl_propertylist_append_float(plist, key_name,(float)qclog[i].n_val) ;
04334          cpl_propertylist_set_comment(plist, key_name,qclog[i].comm) ;
04335        } else if(strcmp(qclog[i].type,"double") == 0) {
04336          snprintf(key_value,MAX_NAME_SIZE-1,"%f",qclog[i].n_val);
04337          cpl_propertylist_append_double(plist, key_name,qclog[i].n_val) ;
04338          cpl_propertylist_set_comment(plist, key_name,qclog[i].comm) ;
04339        }
04340 
04341     }
04342 
04343     return 0 ;
04344 }
04345 
04346 int sinfo_pfits_put_qc(
04347         cpl_propertylist       *   plist,
04348         cpl_table          *   qclog)
04349 {
04350   char            key_name[FILE_NAME_SZ];
04351   char            key_value[FILE_NAME_SZ];
04352   char            key_type[FILE_NAME_SZ];
04353   char            key_help[FILE_NAME_SZ] ;
04354 
04355   int             i =0;
04356   int n =0;
04357   /* Test entries */
04358   if (plist == NULL) {
04359     sinfo_msg_error("plist=NULL, something strange");
04360     return -1 ;
04361   }
04362   /* Parameter Name:    PIPEFILE */
04363 
04364   n=cpl_table_get_nrow(qclog);
04365   for(i=0;i<n;i++) {
04366     strcpy(key_name,"ESO ");
04367     strcat(key_name,cpl_table_get_string(qclog,"key_name",i));
04368     strcpy(key_type,cpl_table_get_string(qclog,"key_type",i));
04369     strcpy(key_value,cpl_table_get_string(qclog,"key_value",i));
04370     strcpy(key_help,cpl_table_get_string(qclog,"key_help",i));
04371 
04372     /* sinfo_msg("name=%s type=%s value=%s\n",key_name,key_type,key_value); */
04373     if(!cpl_propertylist_contains(plist,key_name)) {
04374       if(strcmp(key_type,"CPL_TYPE_STRING") == 0) {
04375     cpl_propertylist_append_string(plist, key_name,key_value) ;
04376     cpl_propertylist_set_comment(plist, key_name,key_help) ;
04377       } else if(strcmp(key_type,"CPL_TYPE_BOOL") == 0) {
04378         printf("key_value=%s %d\n",key_value);
04379     cpl_propertylist_append_bool(plist, key_name,atoi(key_value)) ;
04380     cpl_propertylist_set_comment(plist, key_name,key_help) ;
04381       } else if(strcmp(key_type,"CPL_TYPE_INT") == 0) {
04382     cpl_propertylist_append_int(plist,key_name,atoi(key_value)) ;
04383     cpl_propertylist_set_comment(plist, key_name,key_help) ;
04384       } else if(strcmp(key_type,"CPL_TYPE_FLOAT") == 0) {
04385         cpl_propertylist_append_float(plist, key_name,(float)atof(key_value)) ;
04386         cpl_propertylist_set_comment(plist, key_name,key_help) ;
04387       } else if(strcmp(key_type,"CPL_TYPE_DOUBLE") == 0) {
04388         cpl_propertylist_append_double(plist, key_name,atof(key_value)) ;
04389         cpl_propertylist_set_comment(plist, key_name,key_help) ;
04390       }
04391     }
04392 
04393   }
04394 
04395   return 0 ;
04396 }
04397 
04398 
04399 cpl_table *
04400 sinfo_qclog_init(void)
04401 {
04402 
04403   cpl_table *table;
04404 
04405   table = cpl_table_new(0);
04406   cpl_table_new_column(table,"key_name", CPL_TYPE_STRING);
04407   cpl_table_new_column(table,"key_type", CPL_TYPE_STRING);
04408   cpl_table_new_column(table,"key_value", CPL_TYPE_STRING);
04409   cpl_table_new_column(table,"key_help", CPL_TYPE_STRING);
04410 
04411   return table;
04412 }
04413 
04414 
04415 
04416 
04417 
04418 int
04419 sinfo_qclog_add_int(cpl_table* table,
04420                   const char*  key_name,  
04421                   const int    value,
04422                   const char*  key_help,
04423                   const char*  format)
04424 {
04425   int sz = cpl_table_get_nrow(table);
04426   int raw = sz;
04427   char key_value[FILE_NAME_SZ];
04428   char key_type[FILE_NAME_SZ];
04429 
04430   snprintf(key_value,MAX_NAME_SIZE-1,format,value);
04431   strcpy(key_type,"CPL_TYPE_INT"); 
04432  
04433   cpl_table_set_size(table,sz+1);
04434 
04435   cpl_table_set_string(table,"key_name" ,raw,key_name);
04436   cpl_table_set_string(table,"key_type" ,raw,key_type);
04437   cpl_table_set_string(table,"key_value",raw,key_value);
04438   cpl_table_set_string(table,"key_help" ,raw,key_help);
04439 
04440   return 0;
04441 
04442 }
04443 
04444 
04445 
04446 int
04447 sinfo_qclog_add_bool(cpl_table* table,
04448                   const char*  key_name,  
04449                   const char   value,
04450                   const char*  key_help,
04451                   const char*  format)
04452 {
04453   int sz = cpl_table_get_nrow(table);
04454   int raw = sz;
04455   char key_value[FILE_NAME_SZ];
04456   char key_type[FILE_NAME_SZ];
04457 
04458   snprintf(key_value,MAX_NAME_SIZE-1,format,value);
04459   strcpy(key_type,"CPL_TYPE_BOOL"); 
04460 
04461   cpl_table_set_size(table,sz+1);
04462 
04463   cpl_table_set_string(table,"key_name" ,raw,key_name);
04464   cpl_table_set_string(table,"key_type" ,raw,key_type);
04465   cpl_table_set_string(table,"key_value",raw,key_value);
04466   cpl_table_set_string(table,"key_help" ,raw,key_help);
04467 
04468   return 0;
04469 
04470 }
04471 
04472 
04473 
04474 int
04475 sinfo_qclog_add_float(cpl_table* table,
04476                   const char*  key_name,  
04477                   const float  value,
04478                   const char*  key_help,
04479                   const char*  format)
04480 {
04481   int sz = cpl_table_get_nrow(table);
04482   int raw = sz;
04483   char key_value[FILE_NAME_SZ];
04484   char key_type[FILE_NAME_SZ];
04485 
04486   snprintf(key_value,MAX_NAME_SIZE-1,format,value);
04487   strcpy(key_type,"CPL_TYPE_FLOAT"); 
04488  
04489   cpl_table_set_size(table,sz+1);
04490 
04491   cpl_table_set_string(table,"key_name" ,raw,key_name);
04492   cpl_table_set_string(table,"key_type" ,raw,key_type);
04493   cpl_table_set_string(table,"key_value",raw,key_value);
04494   cpl_table_set_string(table,"key_help" ,raw,key_help);
04495 
04496   return 0;
04497 
04498 }
04499 
04500 
04501 
04502 int
04503 sinfo_qclog_add_double(cpl_table* table,
04504                   const char*  key_name,  
04505                   const double value,
04506                   const char*  key_help,
04507                   const char*  format)
04508 {
04509   int sz = cpl_table_get_nrow(table);
04510   int raw = sz;
04511   char key_value[FILE_NAME_SZ];
04512   char key_type[FILE_NAME_SZ];
04513 
04514   snprintf(key_value,MAX_NAME_SIZE-1,format,value);
04515   strcpy(key_type,"CPL_TYPE_DOUBLE"); 
04516  
04517   cpl_table_set_size(table,sz+1);
04518 
04519   cpl_table_set_string(table,"key_name" ,raw,key_name);
04520   cpl_table_set_string(table,"key_type" ,raw,key_type);
04521   cpl_table_set_string(table,"key_value",raw,key_value);
04522   cpl_table_set_string(table,"key_help" ,raw,key_help);
04523 
04524   return 0;
04525 
04526 }
04527 
04528 int
04529 sinfo_qclog_add_string(cpl_table* table,
04530                   const char*  key_name,  
04531                   const char*  value,
04532                   const char*  key_help,
04533                   const char*  format)
04534 {
04535   int sz = cpl_table_get_nrow(table);
04536   int raw = sz;
04537   char key_value[FILE_NAME_SZ];
04538   char key_type[FILE_NAME_SZ];
04539 
04540   snprintf(key_value,MAX_NAME_SIZE-1,format,value);
04541   strcpy(key_type,"CPL_TYPE_STRING"); 
04542  
04543   cpl_table_set_size(table,sz+1);
04544 
04545   cpl_table_set_string(table,"key_name" ,raw,key_name);
04546   cpl_table_set_string(table,"key_type" ,raw,key_type);
04547   cpl_table_set_string(table,"key_value",raw,key_value);
04548   cpl_table_set_string(table,"key_help" ,raw,key_help);
04549 
04550   return 0;
04551 
04552 }
04553 

Generated on Wed Jan 17 08:33:42 2007 for SINFONI Pipeline Reference Manual by  doxygen 1.4.4