sinfo_dfs.c

00001 /* $Id: sinfo_dfs.c,v 1.43 2013/01/24 15:56:40 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: 2013/01/24 15:56:40 $
00024  * $Revision: 1.43 $
00025  * $Name: HEAD $
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 <cpl.h>
00046 #include <math.h>
00047 #include "sinfo_error.h"
00048 #include "sinfo_utilities.h"
00049 #include "sinfo_utils_wrappers.h"
00050 #include "sinfo_msg.h"
00051 #include "sinfo_globals.h"
00052 #include "sinfo_pro_save.h"
00053 #include "sinfo_skycor.h"
00054 #include "sinfo_file_handling.h"
00055 #include <unistd.h>
00056 #include <stdio.h>
00058 /*---------------------------------------------------------------------------*/
00064 /*---------------------------------------------------------------------------*/
00065 
00066 /* defines */
00067 
00068 #define FITS_MAGIC_SZ      6
00069 #define SINFO_FIT_AMOEBA_NMAX 5000
00070 
00071 /* function prototypes */
00072 /*
00073   static int
00074   sinfo_stat_rectangle(cpl_image* img,
00075   const int kappa,
00076   const int nclip,
00077   double *mean,
00078   double *stdev);
00079 */
00080 static void
00081 sinfo_fit_amoeba_get_psum(int ndim, int mpts, double** p, double* psum);
00082 
00083 
00084 static  double
00085 sinfo_fit_amotry(double** p,
00086                  double y[],
00087                  double psum[],
00088                  int ndim,
00089                  double (*funk)(double[]),
00090                  int ihi,
00091                  double fac);
00092 
00093 
00094 static double
00095 get_chisq(int N, int D,
00096           int (*f)(const double x[], const double a[], double *result),
00097           const double *a,
00098           const double *x,
00099           const double *y,
00100           const double *sigma);
00101 
00102 static int get_candidate(const double *a, const int ia[],
00103                          int M, int N, int D,
00104                          double lambda,
00105                          int    (*f)(const double x[], const double a[],
00106                                      double *result),
00107                          int (*dfda)(const double x[], const double a[],
00108                                      double result[]),
00109                          const double *x,
00110                          const double *y,
00111                          const double *sigma,
00112                          double *partials,
00113                          cpl_matrix *alpha,
00114                          cpl_matrix *beta,
00115                          double *a_da);
00116 
00117 
00118 
00119 int
00120 sinfo_frame_is_raw_dark(char * tag);
00121 
00127 /*---------------------------------------------------------------------------*/
00134 /*---------------------------------------------------------------------------*/
00135 cpl_frameset *
00136 sinfo_frameset_extract(const cpl_frameset *frames,
00137                        const char *tag)
00138 {
00139    cpl_frameset *subset = NULL;
00140    const cpl_frame *f;
00141 
00142 
00143 
00144    assure( frames != NULL, CPL_ERROR_ILLEGAL_INPUT, "Null frameset" );
00145    assure( tag    != NULL, CPL_ERROR_ILLEGAL_INPUT, "Null tag" );
00146 
00147    subset = cpl_frameset_new();
00148 
00149    for (f = cpl_frameset_find_const(frames, tag);
00150         f != NULL;
00151         f = cpl_frameset_find_const(frames, NULL)) {
00152 
00153       cpl_frameset_insert(subset, cpl_frame_duplicate(f));
00154    }
00155   cleanup:
00156    return subset;
00157 }
00158 
00159 
00160 
00166 int sinfo_print_rec_status(const int val) {
00167    if(cpl_error_get_code() != CPL_ERROR_NONE) {
00168       sinfo_msg_error("Recipe status at %d",val);
00169       sinfo_msg_error("%s",(const char*) cpl_error_get_message());
00170       sinfo_msg_error("%s",(const char*) cpl_error_get_where());
00171       return -1;
00172    }
00173    return 0;
00174 }
00175 
00176 
00193 cpl_vector*
00194 sinfo_vector_clip(const cpl_vector* vinp,
00195                   const double kappa,
00196                   const int n,
00197                   const int method)
00198 {
00199    cpl_vector* vout=NULL;
00200    cpl_vector* vtmp=NULL;
00201    int size=0;
00202    int j=0;
00203    register int i=0;
00204 
00205    double mean=0;
00206    double median=0;
00207    double stdev=0;
00208    double* pt=NULL;
00209    double* po=NULL;
00210 
00211    cknull(vinp,"Null input vector");
00212    check_nomsg(vout=cpl_vector_duplicate(vinp));
00213    check_nomsg(mean=cpl_vector_get_mean(vout));
00214    check_nomsg(median=cpl_vector_get_median_const(vout));
00215    check_nomsg(stdev=cpl_vector_get_stdev(vout));
00216    check_nomsg(pt=cpl_vector_get_data(vtmp));
00217 
00218    if(method == 0) {
00219       /*
00220          are rejected
00221          values ||val-mean|| > kappa*sigma
00222       */
00223       for(j=0;j<n;j++) {
00224 
00225          check_nomsg(cpl_vector_sort(vout,1));  /* sort by increasing data */
00226          check_nomsg(po=cpl_vector_get_data(vout));
00227 
00228          if( (size > 1) && (fabs(po[size-1]-mean) > kappa*stdev)) {
00229 
00230             size--;
00231             check_nomsg(vtmp=cpl_vector_new(size));
00232             check_nomsg(pt=cpl_vector_get_data(vtmp));
00233             for(i=0;i<size;i++) {
00234                pt[i]=po[i];
00235             }
00236             check_nomsg(cpl_vector_delete(vout));
00237             check_nomsg(vout=cpl_vector_duplicate(vtmp));
00238             check_nomsg(cpl_vector_delete(vtmp));
00239 
00240             check_nomsg(mean=cpl_vector_get_mean(vout));
00241             check_nomsg(stdev=cpl_vector_get_stdev(vout));
00242 
00243          }
00244 
00245       }
00246 
00247    } else {
00248       /*
00249          are rejected
00250          values ||val-median|| > kappa*sigma
00251       */
00252 
00253 
00254       for(j=0;j<n;j++) {
00255 
00256          check_nomsg(cpl_vector_sort(vout,1));  /* sort by increasing data */
00257          check_nomsg(po=cpl_vector_get_data(vout));
00258 
00259          if( (size > 1) && (fabs(po[size-1]-median) > kappa*stdev)) {
00260 
00261             size--;
00262             check_nomsg(vtmp=cpl_vector_new(size));
00263             check_nomsg(pt=cpl_vector_get_data(vtmp));
00264             for(i=0;i<size;i++) {
00265                pt[i]=po[i];
00266             }
00267             check_nomsg(cpl_vector_delete(vout));
00268             check_nomsg(vout=cpl_vector_duplicate(vtmp));
00269             check_nomsg(cpl_vector_delete(vtmp));
00270 
00271             check_nomsg(median=cpl_vector_get_median_const(vout));
00272             check_nomsg(stdev=cpl_vector_get_stdev(vout));
00273 
00274          }
00275 
00276       }
00277 
00278 
00279 
00280 
00281    }
00282    return vout;
00283   cleanup:
00284    return NULL;
00285 
00286 
00287 }
00288 
00289 
00290 
00291 /*----------------------------------------------------------------------------*/
00325 /*----------------------------------------------------------------------------*/
00326 static int
00327 get_candidate(const double *a, const int ia[],
00328               int M, int N, int D,
00329               double lambda,
00330               int    (*f)(const double x[], const double a[], double *result),
00331               int (*dfda)(const double x[], const double a[], double result[]),
00332               const double *x,
00333               const double *y,
00334               const double *sigma,
00335               double *partials,
00336               cpl_matrix *alpha,
00337               cpl_matrix *beta,
00338               double *a_da)
00339 {
00340    int Mfit = 0;         /* Number of non-constant fit parameters */
00341    cpl_matrix *da;       /* Solution of   alpha * da = beta */
00342    double *alpha_data;
00343    double *beta_data;
00344    double *da_data;
00345    int i, imfit = 0;
00346    int j, jmfit = 0;
00347    int k = 0;
00348 
00349    /* For efficiency, don't check input in this static function */
00350 
00351    Mfit = cpl_matrix_get_nrow(alpha);
00352 
00353    alpha_data    = cpl_matrix_get_data(alpha);
00354    beta_data     = cpl_matrix_get_data(beta);
00355 
00356    /* Build alpha, beta:
00357     *
00358     *  alpha[i,j] = sum_{k=1,N} (sigma_k)^-2 * df/da_i * df/da_j  *
00359     *                           (1 + delta_ij lambda) ,
00360     *
00361     *   beta[i]   = sum_{k=1,N} (sigma_k)^-2 * ( y_k - f(x_k) ) * df/da_i
00362     *
00363     * where (i,j) loop over the non-constant parameters (0 to Mfit-1),
00364     * delta is Kronecker's delta, and all df/da are evaluated in x_k
00365     */
00366 
00367    cpl_matrix_fill(alpha, 0.0);
00368    cpl_matrix_fill(beta , 0.0);
00369 
00370    for (k = 0; k < N; k++)
00371    {
00372       double sm2 = 0.0;                /* (sigma_k)^-2 */
00373       double fx_k = 0.0;               /* f(x_k)       */
00374       const double *x_k = &(x[0+k*D]); /* x_k          */
00375 
00376       if (sigma == NULL)
00377       {
00378          sm2 = 1.0;
00379       }
00380       else
00381       {
00382          sm2 = 1.0 / (sigma[k] * sigma[k]);
00383       }
00384 
00385       /* Evaluate f(x_k) */
00386       cpl_ensure( f(x_k, a, &fx_k) == 0, CPL_ERROR_ILLEGAL_INPUT, -1);
00387 
00388       /* Evaluate (all) df/da (x_k) */
00389       cpl_ensure( dfda(x_k, a, partials) == 0,
00390                   CPL_ERROR_ILLEGAL_INPUT, -1);
00391 
00392       for (i = 0, imfit = 0; i < M; i++)
00393       {
00394          if (ia[i] != 0)
00395          {
00396             /* Beta */
00397             beta_data[imfit] +=
00398                sm2 * (y[k] - fx_k) * partials[i];
00399 
00400             /* Alpha is symmetrical, so compute
00401                only lower-left part */
00402             for (j = 0, jmfit = 0; j < i; j++)
00403             {
00404                if (ia[j] != 0)
00405                {
00406                   alpha_data[jmfit + imfit*Mfit] +=
00407                      sm2 * partials[i] *
00408                      partials[j];
00409 
00410                   jmfit += 1;
00411                }
00412             }
00413 
00414             /* Alpha, diagonal terms */
00415             j = i;
00416             jmfit = imfit;
00417 
00418             alpha_data[jmfit + imfit*Mfit] +=
00419                sm2 * partials[i] *
00420                partials[j] * (1 + lambda);
00421 
00422             imfit += 1;
00423          }
00424       }
00425 
00426       cpl_ensure( imfit == Mfit, CPL_ERROR_ILLEGAL_INPUT,-1);
00427    }
00428 
00429    /* Create upper-right part of alpha */
00430    for (i = 0, imfit = 0; i < M; i++) {
00431       if (ia[i] != 0) {
00432          for (j = i+1, jmfit = imfit+1; j < M; j++) {
00433             if (ia[j] != 0) {
00434                alpha_data[jmfit+imfit*Mfit] = alpha_data[imfit+jmfit*Mfit];
00435                jmfit += 1;
00436             }
00437          }
00438          cpl_ensure( jmfit == Mfit,CPL_ERROR_ILLEGAL_INPUT,-1 );
00439          imfit += 1;
00440       }
00441    }
00442    cpl_ensure( imfit == Mfit, CPL_ERROR_ILLEGAL_INPUT,-1);
00443 
00444    da = cpl_matrix_solve(alpha, beta);
00445 
00446    cpl_ensure(da != NULL, cpl_error_get_code(), -1);
00447 
00448    /* Create a+da vector by adding a and da */
00449    da_data   = cpl_matrix_get_data(da);
00450 
00451    for (i = 0, imfit = 0; i < M; i++)
00452    {
00453       if (ia[i] != 0)
00454       {
00455          a_da[i] = a[i] + da_data[0 + imfit*1];
00456 
00457          imfit += 1;
00458       }
00459       else
00460       {
00461          a_da[i] = a[i];
00462       }
00463    }
00464 
00465    cpl_ensure( imfit == Mfit ,CPL_ERROR_ILLEGAL_INPUT,-1);
00466 
00467    cpl_matrix_delete(da);
00468 
00469    return 0;
00470 }
00471 
00472 /*----------------------------------------------------------------------------*/
00473 
00474 
00475 /*----------------------------------------------------------------------------*/
00496 /*----------------------------------------------------------------------------*/
00497 
00498 static double
00499 get_chisq(int N, int D,
00500           int (*f)(const double x[], const double a[], double *result),
00501           const double *a,
00502           const double *x,
00503           const double *y,
00504           const double *sigma)
00505 {
00506    double chi_sq;     /* Result */
00507    int i = 0;
00508 
00509    /* For efficiency, don't check input in this static function */
00510    chi_sq = 0.0;
00511    for (i = 0; i < N; i++)
00512    {
00513       double fx_i;
00514       double residual;                 /* Residual in units of uncertainty */
00515       const double *x_i = &(x[0+i*D]);
00516 
00517       /* Evaluate */
00518       cpl_ensure( f(x_i,
00519                     a,
00520                     &fx_i) == 0, CPL_ERROR_ILLEGAL_INPUT, -1.0);
00521 
00522       /* Accumulate */
00523       if (sigma == NULL)
00524       {
00525          residual = (fx_i - y[i]);
00526       }
00527       else
00528       {
00529          residual = (fx_i - y[i]) / sigma[i];
00530       }
00531 
00532       chi_sq += residual*residual;
00533 
00534    }
00535 
00536    return chi_sq;
00537 }
00538 
00539 
00540 
00541 #ifndef CPL_VECTOR_FIT_MAXITER
00542 #define CPL_VECTOR_FIT_MAXITER 1000
00543 #endif
00544 /*----------------------------------------------------------------------------*/
00611 /*----------------------------------------------------------------------------*/
00612 cpl_error_code
00613 sinfo_fit_lm(const cpl_matrix *x,
00614              const cpl_matrix *sigma_x,
00615              const cpl_vector *y,
00616              const cpl_vector *sigma_y,
00617              cpl_vector *a,
00618              const int ia[],
00619              int    (*f)(const double x[],
00620                          const double a[],
00621                          double *result),
00622              int (*dfda)(const double x[],
00623                          const double a[],
00624                          double result[]),
00625              double *mse,
00626              double *red_chisq,
00627              cpl_matrix **covariance)
00628 {
00629    const double *x_data     = NULL; /* Pointer to input data                  */
00630    const double *y_data     = NULL; /* Pointer to input data                  */
00631    const double *sigma_data = NULL; /* Pointer to input data                  */
00632    int N    = 0;                    /* Number of data points                  */
00633    int D    = 0;                    /* Dimension of x-points                  */
00634    int M    = 0;                    /* Number of fit parameters               */
00635    int Mfit = 0;                    /* Number of non-constant fit
00636                                        parameters                             */
00637 
00638    double lambda    = 0.0;          /* Lambda in L-M algorithm                */
00639    double MAXLAMBDA = 10e40;        /* Parameter to control the graceful exit
00640                                        if steepest descent unexpectedly fails */
00641    double chi_sq    = 0.0;          /* Current  chi^2                         */
00642    int count        = 0;            /* Number of successive small improvements
00643                                        in chi^2 */
00644    int iterations   = 0;
00645 
00646    cpl_matrix *alpha  = NULL;       /* The MxM ~curvature matrix used in L-M  */
00647    cpl_matrix *beta   = NULL;       /* Mx1 matrix = -.5 grad(chi^2)           */
00648    double *a_data     = NULL;       /* Parameters, a                          */
00649    double *a_da       = NULL;       /* Candidate position a+da                */
00650    double *part       = NULL;       /* The partial derivatives df/da          */
00651    int *ia_local      = NULL;       /* non-NULL version of ia                 */
00652 
00653    /* If covariance computation is requested, then either
00654     * return the covariance matrix or return NULL.
00655     */
00656    if (covariance != NULL) *covariance = NULL;
00657 
00658    /* Validate input */
00659    cpl_ensure_code(x       != NULL, CPL_ERROR_NULL_INPUT);
00660    cpl_ensure_code(sigma_x == NULL, CPL_ERROR_UNSUPPORTED_MODE);
00661    cpl_ensure_code(y       != NULL, CPL_ERROR_NULL_INPUT);
00662    cpl_ensure_code(a       != NULL, CPL_ERROR_NULL_INPUT);
00663    /* ia may be NULL */
00664    cpl_ensure_code(f       != NULL, CPL_ERROR_NULL_INPUT);
00665    cpl_ensure_code(dfda    != NULL, CPL_ERROR_NULL_INPUT);
00666 
00667    /* Chi^2 and covariance computations require sigmas to be known */
00668    cpl_ensure_code( sigma_y != NULL ||
00669                     (red_chisq == NULL && covariance == NULL),
00670                     CPL_ERROR_INCOMPATIBLE_INPUT);
00671 
00672    D = cpl_matrix_get_ncol(x);
00673    N = cpl_matrix_get_nrow(x);
00674    M = cpl_vector_get_size(a);
00675    cpl_ensure_code(N > 0 && D > 0 && M > 0, CPL_ERROR_ILLEGAL_INPUT);
00676 
00677    cpl_ensure_code( cpl_vector_get_size(y) == N,
00678                     CPL_ERROR_INCOMPATIBLE_INPUT);
00679 
00680    x_data = cpl_matrix_get_data_const(x);
00681    y_data = cpl_vector_get_data_const(y);
00682    a_data = cpl_vector_get_data(a);
00683 
00684    if (sigma_y != NULL)
00685    {
00686       cpl_ensure_code( cpl_vector_get_size(sigma_y) == N,
00687                        CPL_ERROR_INCOMPATIBLE_INPUT);
00688       /* Sigmas must be positive */
00689       cpl_ensure_code( cpl_vector_get_min (sigma_y) > 0,
00690                        CPL_ERROR_ILLEGAL_INPUT);
00691       sigma_data = cpl_vector_get_data_const(sigma_y);
00692    }
00693 
00694    ia_local = cpl_malloc(M * sizeof(int));
00695    cpl_ensure_code(ia_local != NULL, CPL_ERROR_ILLEGAL_OUTPUT);
00696 
00697    /* Count non-constant fit parameters, copy ia */
00698    if (ia != NULL)
00699    {
00700       int i;
00701 
00702       Mfit = 0;
00703       for (i = 0; i < M; i++)
00704       {
00705          ia_local[i] = ia[i];
00706 
00707          if (ia[i] != 0)
00708          {
00709             Mfit += 1;
00710          }
00711       }
00712 
00713       if (! (Mfit > 0))
00714       {
00715          cpl_free(ia_local);
00716          cpl_ensure_code( CPL_FALSE,
00717                           CPL_ERROR_ILLEGAL_INPUT);
00718       }
00719    }
00720    else
00721    {
00722       /* All parameters participate */
00723       int i;
00724 
00725       Mfit = M;
00726 
00727       for (i = 0; i < M; i++)
00728       {
00729          ia_local[i] = 1;
00730       }
00731    }
00732 
00733    /* To compute reduced chi^2, we need N > Mfit */
00734    if (! ( red_chisq == NULL || N > Mfit ) )
00735    {
00736       cpl_free(ia_local);
00737       cpl_ensure_code(
00738          CPL_FALSE,
00739          CPL_ERROR_ILLEGAL_INPUT);
00740    }
00741 
00742    /* Create alpha, beta, a_da, part  work space */
00743    alpha = cpl_matrix_new(Mfit, Mfit);
00744    if (alpha == NULL)
00745    {
00746       cpl_free(ia_local);
00747       cpl_ensure_code(
00748          CPL_FALSE,
00749          CPL_ERROR_ILLEGAL_OUTPUT);
00750    }
00751 
00752    beta = cpl_matrix_new(Mfit, 1);
00753    if (beta == NULL)
00754    {
00755       cpl_free(ia_local);
00756       cpl_matrix_delete(alpha);
00757       cpl_ensure_code(
00758          CPL_FALSE,
00759          CPL_ERROR_ILLEGAL_OUTPUT);
00760    }
00761 
00762    a_da = cpl_malloc(M * sizeof(double));
00763    if (a_da == NULL)
00764    {
00765       cpl_free(ia_local);
00766       cpl_matrix_delete(alpha);
00767       cpl_matrix_delete(beta);
00768       cpl_ensure_code(
00769          CPL_FALSE,
00770          CPL_ERROR_ILLEGAL_OUTPUT);
00771    }
00772 
00773    part = cpl_malloc(M * sizeof(double));
00774    if (part == NULL)
00775    {
00776       cpl_free(ia_local);
00777       cpl_matrix_delete(alpha);
00778       cpl_matrix_delete(beta);
00779       cpl_free(a_da);
00780       cpl_ensure_code(
00781          CPL_FALSE,
00782          CPL_ERROR_ILLEGAL_OUTPUT);
00783    }
00784 
00785    /* Initialize loop variables */
00786    lambda = 0.001;
00787    count = 0;
00788    iterations = 0;
00789    if( (chi_sq = get_chisq(N, D, f, a_data, x_data, y_data, sigma_data)) < 0)
00790    {
00791       cpl_free(ia_local);
00792       cpl_matrix_delete(alpha);
00793       cpl_matrix_delete(beta);
00794       cpl_free(a_da);
00795       cpl_free(part);
00796       cpl_ensure_code(
00797          CPL_FALSE,
00798          cpl_error_get_code());
00799    }
00800 
00801    /* uves_msg_debug("Initial chi^2 = %f", chi_sq); */
00802 
00803    /* Iterate until chi^2 didn't improve substantially many (say, 5)
00804       times in a row */
00805    while (count < 5 &&
00806           lambda < MAXLAMBDA &&
00807           iterations < CPL_VECTOR_FIT_MAXITER)
00808    {
00809       /* In each iteration lambda increases, or chi^2 decreases or
00810          count increases. Because chi^2 is bounded from below
00811          (and lambda and count from above), the loop will terminate */
00812 
00813       double chi_sq_candidate = 0.0;
00814       int returncode = 0;
00815 
00816       /* Get candidate position in parameter space = a+da,
00817        * where  alpha * da = beta .
00818        * Increase lambda until alpha is non-singular
00819        */
00820 
00821       while( (returncode = get_candidate(a_data, ia_local,
00822                                          M, N, D,
00823                                          lambda, f, dfda,
00824                                          x_data, y_data, sigma_data,
00825                                          part, alpha, beta, a_da)
00826                 ) != 0
00827              && cpl_error_get_code() == CPL_ERROR_SINGULAR_MATRIX
00828              && lambda < MAXLAMBDA)
00829       {
00830          /* uves_msg_debug("Singular matrix. lambda = %e", lambda); */
00831          cpl_error_reset();
00832          lambda *= 9.0;
00833       }
00834 
00835       /* Set error if lambda diverged */
00836       if ( !( lambda < MAXLAMBDA ) )
00837       {
00838          cpl_free(ia_local);
00839          cpl_matrix_delete(alpha);
00840          cpl_matrix_delete(beta);
00841          cpl_free(a_da);
00842          cpl_free(part);
00843          cpl_ensure_code(
00844             CPL_FALSE,
00845             CPL_ERROR_CONTINUE);
00846       }
00847 
00848       if (returncode != 0)
00849       {
00850          cpl_free(ia_local);
00851          cpl_matrix_delete(alpha);
00852          cpl_matrix_delete(beta);
00853          cpl_free(a_da);
00854          cpl_free(part);
00855          cpl_ensure_code(
00856             CPL_FALSE,
00857             cpl_error_get_code());
00858       }
00859 
00860       /* Get chi^2(a+da) */
00861       if ((chi_sq_candidate = get_chisq(N, D, f, a_da,
00862                                         x_data, y_data, sigma_data)) < 0)
00863       {
00864          cpl_free(ia_local);
00865          cpl_matrix_delete(alpha);
00866          cpl_matrix_delete(beta);
00867          cpl_free(a_da);
00868          cpl_free(part);
00869          cpl_ensure_code(
00870             CPL_FALSE,
00871             cpl_error_get_code());
00872       }
00873 
00874       /* uves_msg_debug("Chi^2 = %f  Candidate = %f  Lambda = %e",
00875          chi_sq, chi_sq_candidate, lambda);  */
00876 
00877       if (chi_sq_candidate > chi_sq)
00878       {
00879          /* Move towards steepest descent */
00880          lambda *= 9.0;
00881       }
00882       else
00883       {
00884          /* Move towards Newton's algorithm */
00885          lambda /= 10.0;
00886 
00887          /* Count the number of successive improvements in chi^2 of
00888             less than 0.01 relative */
00889          if ( chi_sq == 0 ||
00890               (chi_sq - chi_sq_candidate)/chi_sq < .01)
00891          {
00892             count += 1;
00893          }
00894          else
00895          {
00896             /* Chi^2 improved by a significant amount,
00897                reset counter */
00898             count = 0;
00899          }
00900 
00901          /* chi^2 improved, update a and chi^2 */
00902          {
00903             int i;
00904             for (i = 0; i < M; i++) a_data[i] = a_da[i];
00905          }
00906          chi_sq = chi_sq_candidate;
00907       }
00908       iterations++;
00909    }
00910 
00911    /* Set error if we didn't converge */
00912    if ( !( lambda < MAXLAMBDA && iterations < CPL_VECTOR_FIT_MAXITER ) )
00913    {
00914       cpl_free(ia_local);
00915       cpl_matrix_delete(alpha);
00916       cpl_matrix_delete(beta);
00917       cpl_free(a_da);
00918       cpl_free(part);
00919       cpl_ensure_code(
00920          CPL_FALSE,
00921          CPL_ERROR_CONTINUE);
00922    }
00923 
00924    /* Compute mse if requested */
00925    if (mse != NULL)
00926    {
00927       int i;
00928 
00929       *mse = 0.0;
00930 
00931       for(i = 0; i < N; i++)
00932       {
00933          double fx_i = 0.0;
00934          double residual = 0.0;
00935 
00936          /* Evaluate f(x_i) at the best fit parameters */
00937          if( f(&(x_data[i*D]),
00938                a_data,
00939                &fx_i) != 0)
00940          {
00941             cpl_free(ia_local);
00942             cpl_matrix_delete(alpha);
00943             cpl_matrix_delete(beta);
00944             cpl_free(a_da);
00945             cpl_free(part);
00946             cpl_ensure_code(
00947                CPL_FALSE,
00948                CPL_ERROR_ILLEGAL_INPUT);
00949          }
00950 
00951          residual = y_data[i] - fx_i;
00952          *mse += residual * residual;
00953       }
00954       *mse /= N;
00955    }
00956 
00957    /* Compute reduced chi^2 if requested */
00958    if (red_chisq != NULL)
00959    {
00960       /* We already know the optimal chi^2 (and that N > Mfit)*/
00961       *red_chisq = chi_sq / (N-Mfit);
00962    }
00963 
00964    /* Compute covariance matrix if requested
00965     * cov = alpha(lambda=0)^-1
00966     */
00967    if (covariance != NULL)
00968    {
00969       cpl_matrix *cov;
00970 
00971       if( get_candidate(a_data, ia_local,
00972                         M, N, D, 0.0, f, dfda,
00973                         x_data, y_data, sigma_data,
00974                         part, alpha, beta, a_da)
00975           != 0)
00976       {
00977          cpl_free(ia_local);
00978          cpl_matrix_delete(alpha);
00979          cpl_matrix_delete(beta);
00980          cpl_free(a_da);
00981          cpl_free(part);
00982          cpl_ensure_code(
00983             CPL_FALSE,
00984             cpl_error_get_code());
00985       }
00986 
00987       cov = cpl_matrix_invert_create(alpha);
00988       if (cov == NULL)
00989       {
00990          cpl_free(ia_local);
00991          cpl_matrix_delete(alpha);
00992          cpl_matrix_delete(beta);
00993          cpl_free(a_da);
00994          cpl_free(part);
00995          cpl_ensure_code(
00996             CPL_FALSE,
00997             cpl_error_get_code());
00998       }
00999 
01000       /* Make sure that variances are positive */
01001       {
01002          int i;
01003          for (i = 0; i < Mfit; i++)
01004          {
01005             if ( !(cpl_matrix_get(cov, i, i) > 0) )
01006             {
01007                cpl_free(ia_local);
01008                cpl_matrix_delete(alpha);
01009                cpl_matrix_delete(beta);
01010                cpl_free(a_da);
01011                cpl_free(part);
01012                cpl_matrix_delete(cov);
01013                *covariance = NULL;
01014                cpl_ensure_code(
01015                   CPL_FALSE,
01016                   CPL_ERROR_SINGULAR_MATRIX);
01017             }
01018          }
01019       }
01020 
01021       /* Expand covariance matrix from Mfit x Mfit
01022          to M x M. Set rows/columns corresponding to fixed
01023          parameters to zero */
01024 
01025       *covariance = cpl_matrix_new(M, M);
01026       if (*covariance == NULL)
01027       {
01028          cpl_free(ia_local);
01029          cpl_matrix_delete(alpha);
01030          cpl_matrix_delete(beta);
01031          cpl_free(a_da);
01032          cpl_free(part);
01033          cpl_matrix_delete(cov);
01034          cpl_ensure_code(
01035             CPL_FALSE,
01036             CPL_ERROR_ILLEGAL_OUTPUT);
01037       }
01038 
01039       {
01040          int j, jmfit;
01041 
01042          for (j = 0, jmfit = 0; j < M; j++)
01043             if (ia_local[j] != 0)
01044             {
01045                int i, imfit;
01046 
01047                for (i = 0, imfit = 0; i < M; i++)
01048                   if (ia_local[i] != 0)
01049                   {
01050                      cpl_matrix_set(*covariance, i, j,
01051                                     cpl_matrix_get(
01052                                        cov, imfit, jmfit));
01053                      imfit += 1;
01054                   }
01055 
01056                cpl_ensure( imfit == Mfit, CPL_ERROR_ILLEGAL_INPUT,-1);
01057 
01058                jmfit += 1;
01059             }
01060 
01061          cpl_ensure( jmfit == Mfit, CPL_ERROR_ILLEGAL_INPUT,-1 );
01062       }
01063 
01064       cpl_matrix_delete(cov);
01065    }
01066 
01067    cpl_free(ia_local);
01068    cpl_matrix_delete(alpha);
01069    cpl_matrix_delete(beta);
01070    cpl_free(a_da);
01071    cpl_free(part);
01072 
01073    return CPL_ERROR_NONE;
01074 }
01075 
01076 
01077 
01078 
01079 
01080 
01081 
01082 
01083 
01084 
01085 
01086 
01087 
01088 
01089 
01090 /*-------------------------------------------------------------------------*/
01091 
01103 static void
01104 sinfo_fit_amoeba_get_psum(int ndim, int mpts, double** p, double* psum)
01105 {
01106    int i=0;
01107    int j=0;
01108    double sum=0;
01109    for (j=0;j<ndim;j++) {
01110       for (sum=0.0,i=0;i<mpts;i++) {
01111          sum += p[i][j];
01112       }
01113       psum[j]=sum;
01114    }
01115 
01116 }
01117 
01118 
01119 #define SINFO_FIT_AMOEBA_SWAP(a,b) {swap=(a);(a)=(b);(b)=swap;}
01120 
01121 
01122 
01149 void
01150 sinfo_fit_amoeba(
01151    double**p,
01152    double y[],
01153    int ndim,
01154    double ftol,
01155    double (*funk)(double[]),
01156    int* nfunk)
01157 {
01158 
01159 
01160    int i=0;
01161    int ihi=0;
01162    int ilo=0;
01163    int inhi=0;
01164    int j=0;
01165    int mpts=ndim+1;
01166    double rtol=0;
01167    double swap=0;
01168    double ysave=0;
01169    double ytry=0;
01170    cpl_vector* sum=NULL;
01171    double* psum=NULL;
01172 
01173    sum=cpl_vector_new(ndim);
01174    psum=cpl_vector_get_data(sum);
01175    *nfunk=0;
01176 
01177    sinfo_fit_amoeba_get_psum(ndim,mpts,p,psum);
01178 
01179    for(;;) {
01180       ilo=0;
01181       /*
01182         First we must determine which point is the highest (worst),
01183         next-highest, and lowest (best), by looping over the points
01184         in the simplex
01185       */
01186       ihi=y[0]>y[1] ? (inhi=1,0) : (inhi=0,1);
01187 
01188       for (i=0;i< mpts;i++) {
01189          if (y[i] <= y[ilo]) ilo=i;
01190          if (y[i] > y[ihi]) {
01191             inhi=ihi;
01192             ihi=i;
01193          } else if (y[i] > y[inhi] && i != ihi) inhi=i;
01194       }
01195       rtol=2.0*fabs(y[ihi]-y[ilo])/(fabs(y[ihi])+fabs(y[ilo]));
01196 
01197       /*
01198         compute the fractional range from highest to lowest and return if
01199         satisfactory
01200       */
01201       if(rtol < ftol) { // if returning, but best point and value is in slot 1
01202          SINFO_FIT_AMOEBA_SWAP(y[0],y[ilo]);
01203          for (i=0;i<ndim;i++) SINFO_FIT_AMOEBA_SWAP(p[1][i],p[ilo][i]);
01204          break;
01205       }
01206       if (*nfunk >= SINFO_FIT_AMOEBA_NMAX) {
01207          sinfo_msg_error("NMAX exceeded");
01208          SINFO_FIT_AMOEBA_SWAP(y[0],y[ilo]);
01209          for (i=0;i<ndim;i++) SINFO_FIT_AMOEBA_SWAP(p[1][i],p[ilo][i]);
01210          for (i=0;i<ndim;i++) {
01211             sinfo_msg("p[1][i]=%g p[ilo][i]=%g ilo=%d",p[1][i],p[ilo][i],ilo);
01212          }
01213          assure(*nfunk >= SINFO_FIT_AMOEBA_NMAX,CPL_ERROR_UNSPECIFIED,
01214                 "NMAX exceeded");
01215          break;
01216 
01217       }
01218       *nfunk +=2;
01219       /*
01220         Begin a new iteration. First extrapolate by a Factor -1 through the face
01221         of the simplex across the high point, i.e. reflect the simplex from the
01222         high point
01223       */
01224       ytry=sinfo_fit_amotry(p,y,psum,ndim,funk,ihi,-1.0);
01225       if(ytry <= y[ilo]) {
01226          /*
01227             Gives a result better than the best point, so try an additional
01228             extrapolation by a factor 2
01229          */
01230          ytry=sinfo_fit_amotry(p,y,psum,ndim,funk,ihi,2.0);
01231       } else if (ytry >= y[inhi]) {
01232 
01233          /*
01234            The reflected point is worse than the second highest, so look for an
01235            intermediate lower point, i.e. do a one-dimensional contraction
01236          */
01237          ysave=y[ihi];
01238          ytry=sinfo_fit_amotry(p,y,psum,ndim,funk,ihi,0.5);
01239          if(ytry >= ysave) {
01240             /*
01241                Can't seem to get rid of that high point.
01242                Better contract around the lowest (best) point
01243             */
01244             for(i=0;i<mpts;i++) {
01245                if(i != ilo) {
01246                   for( j=0;j<ndim;j++) {
01247                      p[i][j]=psum[j]=0.5*(p[i][j]+p[ilo][j]);
01248                   }
01249                   y[i]=(*funk)(psum);
01250                }
01251             }
01252             *nfunk += ndim; /* Keep track of function evaluations */
01253             sinfo_fit_amoeba_get_psum(ndim,mpts,p,psum);/* Recomputes psum */
01254          }
01255       } else {
01256          --(*nfunk);
01257          /* Go back for the test of doneness and the next iteration */
01258       }
01259    }
01260   cleanup:
01261    cpl_vector_delete(sum);
01262 }
01263 
01264 
01265 static  double
01266 sinfo_fit_amotry(double** p, double y[], double psum[], int ndim,
01267                  double (*funk)(double[]),int ihi, double fac)
01268 {
01269    int j;
01270    double fac1=0;
01271    double fac2=0;
01272    double ytry=0;
01273    cpl_vector * vtry=NULL;
01274    double *ptry=NULL;
01275 
01276    vtry=cpl_vector_new(ndim);
01277    ptry=cpl_vector_get_data(vtry);
01278 
01279    fac1=(1.0-fac)/ndim;
01280    fac2=fac1-fac;
01281 
01282    for (j=0;j<ndim;j++) {
01283       ptry[j]=psum[j]*fac1-p[ihi][j]*fac2;
01284    }
01285    ytry=(*funk)(ptry);
01286    if (ytry < y[ihi]) {
01287       y[ihi]=ytry;
01288       for (j=0;j<ndim;j++) {
01289          psum[j] += ptry[j]-p[ihi][j];
01290          p[ihi][j]=ptry[j];
01291       }
01292    }
01293    sinfo_free_my_vector(&vtry);
01294    return ytry;
01295 }
01296 
01297 /*-------------------------------------------------------------------------*/
01298 /*-------------------------------------------------------------------------*/
01308 /*--------------------------------------------------------------------------*/
01309 
01310 int sinfo_vector_dindgen(cpl_vector** v)
01311 {
01312 
01313    int sz=0;
01314    int i=0;
01315 
01316    cknull(*v,"Null input vector");
01317    check(sz=cpl_vector_get_size(*v),"Getting size of a vector");
01318 
01319    for(i=0;i<sz;i++) {
01320       cpl_vector_set(*v,i,(double)i);
01321    }
01322 
01323    return 0;
01324   cleanup:
01325    return -1;
01326 
01327 }
01328 
01329 /*-------------------------------------------------------------------------*/
01340 /*--------------------------------------------------------------------------*/
01341 
01342 int sinfo_is_fits_file(const char *filename)
01343 {
01344    FILE    *fp ;
01345    char    *magic ;
01346    int        isfits ;
01347 
01348    if ((fp = fopen(filename, "r"))==NULL) {
01349       sinfo_msg_error("cannot open file [%s]", filename) ;
01350       return -1 ;
01351    }
01352 
01353    magic = cpl_calloc(FITS_MAGIC_SZ+1, sizeof(char)) ;
01354    (void)fread(magic, 1, FITS_MAGIC_SZ, fp) ;
01355    (void)fclose(fp) ;
01356    magic[FITS_MAGIC_SZ] = (char)0 ;
01357    if (strstr(magic, "SIMPLE")!=NULL)
01358       isfits = 1 ;
01359    else
01360       isfits = 0 ;
01361    cpl_free(magic) ;
01362    return isfits ;
01363 }
01364 
01365 /*---------------------------------------------------------------------------*/
01371 /*---------------------------------------------------------------------------*/
01372 cpl_error_code
01373 sinfo_table_correl(cpl_table * t1, cpl_table* t2, cpl_table* range,double* xcor)
01374 {
01375 
01376    double wsr=0;
01377    double wer=0;
01378    int nr=0;
01379    int i=0;
01380    int status=0;
01381    int nrows=0;
01382    double mean=0;
01383    double prod=0;
01384 
01385    cpl_table* tmp_t1=NULL;
01386    cpl_table* tmp_t2=NULL;
01387 
01388    check_nomsg(nr=cpl_table_get_nrow(range));
01389    for(i=0;i<nr;i++) {
01390       wsr=cpl_table_get_double(range,"WSTART",i,&status);
01391       wer=cpl_table_get_double(range,"WEND",i,&status);
01392       cpl_table_and_selected_double(t1,"WAVE",CPL_NOT_LESS_THAN,wsr);
01393       cpl_table_and_selected_double(t1,"WAVE",CPL_NOT_GREATER_THAN,wer);
01394       tmp_t1=cpl_table_extract_selected(t1);
01395       cpl_table_and_selected_double(t2,"WAVE",CPL_NOT_LESS_THAN,wsr);
01396       cpl_table_and_selected_double(t2,"WAVE",CPL_NOT_GREATER_THAN,wer);
01397       tmp_t2=cpl_table_extract_selected(t2);
01398       cpl_table_duplicate_column(tmp_t1,"INT1",tmp_t1,"INT");
01399       cpl_table_duplicate_column(tmp_t1,"INT2",tmp_t2,"INT");
01400       cpl_table_multiply_columns(tmp_t1,"INT1","INT2");
01401       mean=cpl_table_get_column_mean(tmp_t1,"INT1");
01402       nrows=cpl_table_get_nrow(tmp_t1);
01403       prod=mean*nrows;
01404       *xcor+=prod;
01405    }
01406 
01407   cleanup:
01408    return cpl_error_get_code();
01409 }
01415 /*----------------------------------------------------------------------------*/
01416 cpl_error_code
01417 sinfo_frameset_merge(cpl_frameset * set1, cpl_frameset* set2)
01418 {
01419 
01420    cpl_frame* frm_tmp=NULL;
01421    cpl_frame* frm_dup=NULL;
01422 
01423    passure(set1 != NULL, "Wrong input set");
01424 
01425    check_nomsg(frm_tmp = cpl_frameset_get_first(set2));
01426    while (frm_tmp != NULL)
01427    {
01428       frm_dup=cpl_frame_duplicate(frm_tmp);
01429       cpl_frameset_insert(set1,frm_dup);
01430       frm_tmp = cpl_frameset_get_next(set2);
01431    }
01432 
01433   cleanup:
01434    return cpl_error_get_code();
01435 }
01436 
01437 /*----------------------------------------------------------------------------*/
01445 /*----------------------------------------------------------------------------*/
01446 
01447 cpl_error_code
01448 sinfo_extract_frames_group_type(const cpl_frameset * set,
01449                                 cpl_frameset** ext,
01450                                 cpl_frame_group type)
01451 {
01452    const cpl_frame* frm_tmp=NULL;
01453    cpl_frame* frm_dup=NULL;
01454    cpl_frame_group g;
01455 
01456    check_nomsg(*ext = cpl_frameset_new());
01457    check_nomsg(frm_tmp = cpl_frameset_get_first_const(set));
01458    while (frm_tmp != NULL)
01459    {
01460       g=cpl_frame_get_group(frm_tmp);
01461       if(g == type) {
01462          frm_dup=cpl_frame_duplicate(frm_tmp);
01463          cpl_frameset_insert(*ext,frm_dup);
01464          sinfo_msg_debug("group %d insert file %s ",
01465                          type,cpl_frame_get_filename(frm_dup));
01466       }
01467       frm_tmp = cpl_frameset_get_next_const(set);
01468    }
01469 
01470   cleanup:
01471    return cpl_error_get_code();
01472 }
01473 
01474 
01475 
01476 
01487 int
01488 sinfo_get_pupil_shift(cpl_imagelist* iml,const int n,cpl_table** qclog_tbl)
01489 {
01490    cpl_size max_ima_x=0;
01491    cpl_size max_ima_y=0;
01492    int nx=0;
01493    int ny=0;
01494 
01495    double xshift=0;
01496    double yshift=0;
01497 
01498    double max_ima_cx=0;
01499    double max_ima_cy=0;
01500 
01501    cpl_image* img=NULL;
01502    cpl_image* img_dup=NULL;
01503 
01504    char key_name[FILE_NAME_SZ];
01505 
01506    img=cpl_imagelist_collapse_median_create(iml);
01507    nx=cpl_image_get_size_x(img);
01508    ny=cpl_image_get_size_y(img);
01509 
01510    img_dup=cpl_image_duplicate(img);
01511    sinfo_clean_nan(&img_dup);
01512    cpl_image_get_maxpos(img_dup,&max_ima_x,&max_ima_y);
01513    max_ima_cx=cpl_image_get_centroid_x_window(img_dup,1,1,nx,ny);
01514    max_ima_cy=cpl_image_get_centroid_y_window(img_dup,1,1,nx,ny);
01515 
01516    cpl_image_delete(img_dup);
01517 
01518 
01519    xshift=max_ima_cx-(double)nx/2;
01520    yshift=max_ima_cy-(double)ny/2;
01521 
01522    snprintf(key_name,sizeof(key_name),"%s%d%s","QC PUPIL",n," SHIFTX");
01523    sinfo_qclog_add_double(*qclog_tbl,key_name,xshift,
01524                           "X shift centroid - center image","%f");
01525 
01526    snprintf(key_name,sizeof(key_name),"%s%d%s","QC PUPIL",n," SHIFTY");
01527    sinfo_qclog_add_double(*qclog_tbl,key_name,yshift,
01528                           "Y shift centroid - center image","%f");
01529    cpl_image_delete(img);
01530 
01531    return 0;
01532 }
01533 
01534 
01535 
01542 int sinfo_get_strehl_type(cpl_frameset* sof)
01543 {
01544    int strehl_sw=0;
01545    int nobs=0;
01546    int i=0;
01547    cpl_frameset* obs=NULL;
01548 
01549    cpl_frame* frame=NULL;
01550    float* pix_scale=NULL;
01551    cpl_propertylist* plist=NULL;
01552 
01553    obs = cpl_frameset_new();
01554 
01555    sinfo_contains_frames_kind(sof,obs,PRO_OBS_PSF);
01556 
01557    nobs=cpl_frameset_get_size(obs);
01558    if (nobs < 1) {
01559       sinfo_contains_frames_kind(sof,obs,PRO_OBS_STD);
01560       nobs=cpl_frameset_get_size(obs);
01561    }
01562 
01563    nobs=cpl_frameset_get_size(obs);
01564 
01565    if (nobs < 1) {
01566       return 0;
01567    } else {
01568       pix_scale=cpl_calloc(nobs,sizeof(float));
01569       for(i=0;i<nobs;i++) {
01570          frame=cpl_frameset_get_frame(obs,i);
01571          plist=cpl_propertylist_load(cpl_frame_get_filename(frame),0);
01572          pix_scale[i]=sinfo_pfits_get_pixscale(plist);
01573          cpl_propertylist_delete(plist);
01574       }
01575       if(sinfo_pix_scale_isnot_const(pix_scale,nobs)) {
01576          strehl_sw=1;
01577       }
01578       cpl_free(pix_scale);
01579    }
01580    cpl_frameset_delete(obs);
01581 
01582    return strehl_sw;
01583 
01584 }
01585 
01586 
01587 
01594 double sinfo_get_wave_cent(const char* band)
01595 {
01596    double lam=0.;
01597    if (strcmp(band,"H+K") == 0) {
01598       lam=1.950;
01599    } else if (strcmp(band,"K") == 0) {
01600       lam=2.175;
01601    } else if (strcmp(band,"J") == 0) {
01602       lam=1.225;
01603    } else if (strcmp(band,"H") == 0) {
01604       lam=1.675;
01605    }
01606    return lam;
01607 
01608 }
01609 
01610 
01611 
01620 int sinfo_pix_scale_isnot_const(float* pix_scale, const int n) {
01621    int i=0;
01622    float eps=0.0001;
01623    float ref=pix_scale[0];
01624 
01625    for(i=1;i<n;i++) {
01626       if(fabs(pix_scale[i]-ref) > eps) return 1;
01627    }
01628    return 0;
01629 }
01630 
01631 
01639 const char* sinfo_get_pix_scale(float ps) {
01640 
01641    const char* key_value;
01642    float eps=0.0001;
01643 
01644    if(fabs(ps - 0.025) < eps) {
01645       key_value="0.025";
01646    }
01647    else if (fabs(ps - 0.1) < eps) {
01648       key_value="0.1";
01649    }
01650    else if (fabs(ps - 0.25) < eps) {
01651       key_value="0.25";
01652    }
01653    else if (fabs(ps - 1.0) < eps) {
01654       key_value="pupil";
01655    } else {
01656       sinfo_msg_error("ps=%f. Failed to set pixel scale",ps);
01657       return NULL;
01658    }
01659 
01660    return key_value;
01661 }
01662 
01663 
01679 int  sinfo_get_clean_mean_window(cpl_image* img,
01680                                  int llx, int lly, int urx, int ury,
01681                                  const int kappa, const int nclip,
01682                                  double* local_clean_mean,
01683                                  double* clean_stdev)
01684 {
01685 
01686    double mean=0;
01687    double stdev=0;
01688    double threshold=0;
01689    double lo_cut=0;
01690    double hi_cut=0;
01691    cpl_mask* mask=NULL;
01692    cpl_image* tmp=NULL;
01693    cpl_stats* stats=NULL;
01694    int i=0;
01695 
01696    tmp=cpl_image_extract(img,llx,lly,urx,ury);
01697    cpl_image_accept_all(tmp);
01698    for(i=0;i<nclip;i++) {
01699 
01700 
01701       cpl_stats_delete(stats);
01702       stats = cpl_stats_new_from_image(tmp, CPL_STATS_MEAN | CPL_STATS_STDEV);
01703       mean = cpl_stats_get_mean(stats);
01704       stdev = cpl_stats_get_stdev(stats);
01705 
01706       threshold=kappa*stdev;
01707       lo_cut=mean-threshold;
01708       hi_cut=mean+threshold;
01709       cpl_image_accept_all(tmp);
01710       mask=cpl_mask_threshold_image_create(tmp,lo_cut,hi_cut);
01711 
01712       cpl_mask_not(mask);
01713       cpl_image_reject_from_mask(tmp,mask);
01714       cpl_mask_delete(mask);
01715 
01716 
01717    }
01718    *local_clean_mean=mean;
01719    *clean_stdev=stdev;
01720    cpl_image_delete(tmp);
01721    cpl_stats_delete(stats);
01722 
01723 
01724    return 0;
01725 
01726 
01727 }
01733 int sinfo_check_rec_status(const int val) {
01734    if(cpl_error_get_code() != CPL_ERROR_NONE) {
01735       sinfo_msg_error("error before %d",val);
01736       sinfo_msg_error((char* ) cpl_error_get_message());
01737       sinfo_msg_error((char* ) cpl_error_get_where());
01738       return -1;
01739    }
01740    return 0;
01741 }
01742 
01749 int
01750 sinfo_clean_nan(cpl_image** im)
01751 {
01752    int i=0;
01753    int j=0;
01754    int nx=0;
01755    int ny=0;
01756    float* data=NULL;
01757 
01758    nx=cpl_image_get_size_x(*im);
01759    ny=cpl_image_get_size_y(*im);
01760    data=cpl_image_get_data_float(*im);
01761 
01762    for(j=0;j<ny;j++) {
01763       for(i=0;i<nx;i++) {
01764          if(isnan(data[j*nx+i]) != 0) {
01765             data[j*nx+i] = 0;
01766          }
01767       }
01768    }
01769    return 0;
01770 }
01771 
01781 void
01782 sinfo_add_pro_fits_key(cpl_propertylist * plist,
01783                        char* pro_catg,
01784                        char* file_name,
01785                        char* out_name)
01786 {
01787 
01788    char* date=NULL;
01789    date     = sinfo_get_datetime_iso8601() ;
01790 
01791    cpl_propertylist_insert_after_string(plist, "EXPTIME",
01792                                         KEY_NAME_PIPEFILE, out_name) ;
01793    cpl_propertylist_set_comment(plist, KEY_NAME_PIPEFILE,KEY_HELP_PIPEFILE) ;
01794 
01795    cpl_propertylist_insert_after_string(plist, "EXPTIME",
01796                                         KEY_NAME_HPRO_DID, KEY_VALUE_HPRO_DID) ;
01797    cpl_propertylist_set_comment(plist, KEY_NAME_HPRO_DID,KEY_HELP_HPRO_DID) ;
01798 
01799    cpl_propertylist_insert_after_string(plist, "EXPTIME",
01800                                         KEY_NAME_HPRO_TYPE, "REDUCED") ;
01801    cpl_propertylist_set_comment(plist, KEY_NAME_HPRO_TYPE, KEY_HELP_HPRO_TYPE) ;
01802 
01803    cpl_propertylist_insert_after_string(plist, "EXPTIME",
01804                                         KEY_NAME_HPRO_CATG, pro_catg) ;
01805    cpl_propertylist_set_comment(plist, KEY_NAME_HPRO_CATG,  KEY_HELP_HPRO_CATG);
01806 
01807    cpl_propertylist_insert_after_string(plist, "EXPTIME",
01808                                         KEY_NAME_HPRO_STATUS, "OK") ;
01809    cpl_propertylist_set_comment(plist, KEY_NAME_HPRO_STATUS,KEY_HELP_HPRO_CATG);
01810 
01811    cpl_propertylist_insert_after_string(plist, "EXPTIME",
01812                                         KEY_NAME_HPRO_DATE, date) ;
01813    cpl_propertylist_set_comment(plist, KEY_NAME_HPRO_DATE, KEY_HELP_HPRO_DATE);
01814 
01815    cpl_propertylist_insert_after_string(plist, "EXPTIME",
01816                                         KEY_NAME_HPRO_RECID, file_name) ;
01817    cpl_propertylist_set_comment(plist, KEY_NAME_HPRO_RECID,KEY_HELP_HPRO_RECID);
01818 
01819    cpl_propertylist_insert_after_string(plist, "EXPTIME",
01820                                         KEY_NAME_HPRO_DRSID, PACKAGE_VERSION);
01821    cpl_propertylist_set_comment(plist, KEY_NAME_HPRO_DRSID,KEY_HELP_HPRO_DRSID);
01822 
01823 
01824 }
01825 /*---------------------------------------------------------------------------*/
01832 /*---------------------------------------------------------------------------*/
01833 int  sinfo_compare_tags(
01834    const cpl_frame   *   frame1,
01835    const cpl_frame   *   frame2)
01836 {
01837    char            *   v1 ;
01838    char            *   v2 ;
01839 
01840    /* Test entries */
01841    if (frame1==NULL || frame2==NULL) return -1 ;
01842 
01843    /* Get the tags */
01844    if ((v1 = (char*)cpl_frame_get_tag(frame1)) == NULL) return -1 ;
01845    if ((v2 = (char*)cpl_frame_get_tag(frame2)) == NULL) return -1 ;
01846 
01847    /* Compare the tags */
01848    if (strcmp(v1, v2)) return 0 ;
01849    else return 1 ;
01850 }
01851 
01860 int sinfo_extract_raw_pinhole_frames(cpl_frameset * sof, cpl_frameset** raw)
01861 {
01862    char* tag=NULL;
01863    char* name=NULL;
01864    cpl_frame* frame   = NULL;
01865    int nsof=0;
01866    int i=0;
01867    nsof = cpl_frameset_get_size(sof);
01868    for (i=0 ; i<nsof ; i++) {
01869       frame = cpl_frameset_get_frame(sof,i);
01870       name= (char*) cpl_frame_get_filename(frame);
01871       if(sinfo_is_fits_file(name) == 1) {
01872          /* to go on the file must exist */
01873          if(cpl_frame_get_tag(frame) != NULL) {
01874             /* If the frame has a tag we process it. Else it is an object */
01875             tag= (char*) cpl_frame_get_tag(frame);
01876             if(sinfo_frame_is_pinhole_lamp(tag) == 1) {
01877                cpl_frameset_insert(*raw,frame);
01878             }
01879          }
01880       }
01881    }
01882    return 0;
01883 }
01884 
01885 
01886 int sinfo_get_ins_set(char* band,int* ins_set){
01887 
01888    if (strcmp(band,"H") == 0) {
01889       *ins_set = 0;
01890    }
01891    else if (strcmp(band,"H+K") == 0) {
01892       *ins_set = 1;
01893    }
01894    else if (strcmp(band,"K") == 0) {
01895       *ins_set = 2;
01896    }
01897    else if (strcmp(band,"J") == 0) {
01898       *ins_set = 3;
01899    }
01900    return 0;
01901 
01902 
01903 }
01904 int sinfo_extract_raw_frames(cpl_frameset * sof, cpl_frameset** raw)
01905 {
01906    char* tag=NULL;
01907    char* name=NULL;
01908    cpl_frame* frame   = NULL;
01909    int nsof=0;
01910    int i=0;
01911    nsof = cpl_frameset_get_size(sof);
01912    for (i=0 ; i<nsof ; i++) {
01913       frame = cpl_frameset_get_frame(sof,i);
01914       name= (char*) cpl_frame_get_filename(frame);
01915       if(sinfo_is_fits_file(name) == 1) {
01916          /* to go on the file must exist */
01917          if(cpl_frame_get_tag(frame) != NULL) {
01918             /* If the frame has a tag we process it. Else it is an object */
01919             tag= (char*) cpl_frame_get_tag(frame);
01920             if(sinfo_frame_is_raw(tag) == 1) {
01921                cpl_frameset_insert(*raw,frame);
01922             }
01923          }
01924       }
01925    }
01926    return 0;
01927 }
01928 
01929 int sinfo_remove_qc_frames(cpl_frameset* sof,cpl_frameset** raw)
01930 {
01931    char* tag=NULL;
01932    char* name=NULL;
01933    cpl_frame* frame   = NULL;
01934    int nsof=0;
01935    int i=0;
01936 
01937    nsof = cpl_frameset_get_size(sof);
01938    for (i=0 ; i<nsof ; i++) {
01939       frame = cpl_frameset_get_frame(sof,i);
01940       name= (char*) cpl_frame_get_filename(frame);
01941       /* sinfo_msg("name=%s",name); */
01942       if(sinfo_is_fits_file(name) == 1) {
01943          /* sinfo_msg("\t exist "); */
01944          /* to go on the file must exist */
01945          if(cpl_frame_get_tag(frame) != NULL) {
01946             /* If the frame has a tag we process it. Else it is an object */
01947             tag= (char*) cpl_frame_get_tag(frame);
01948             /* sinfo_msg("\t tag %s\n ",tag); */
01949             if(strstr(tag,"QC") != NULL) {
01950                /* sinfo_msg("remove frame %s\n",name); */
01951                cpl_frameset_erase(*raw,tag);
01952             }
01953          }
01954       } else {
01955          /* sinfo_msg("remove frame\n"); */
01956          cpl_frameset_erase_frame(*raw,frame);
01957       }
01958    }
01959    return 0;
01960 
01961 }
01962 
01963 
01964 int sinfo_contains_frames_kind(cpl_frameset * sof,
01965                                cpl_frameset* raw,
01966                                const char*         type)
01967 {
01968    char* tag=NULL;
01969    char* name=NULL;
01970    cpl_frame* frame   = NULL;
01971    cpl_frame* frame_dup   = NULL;
01972 
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             /* sinfo_msg("name=%s tag=%s type=%s\n",name,tag,type); */
01985             if(strstr(tag,type) != NULL) {
01986                /* sinfo_msg("Match name=%s tag=%s type=%s\n",name,tag,type); */
01987                frame_dup = cpl_frame_duplicate(frame);
01988                cpl_frameset_insert(raw,frame_dup);
01989                /* sinfo_msg("inserted\n"); */
01990             }
01991          }
01992       }
01993    }
01994    return 0;
01995 }
01996 
01997 
01998 
01999 
02000 int sinfo_is_fibres_on_off(cpl_frameset * sof,
02001                            cpl_frameset* raw)
02002 {
02003    char* tag=NULL;
02004    char* name=NULL;
02005    cpl_frame* frame   = NULL;
02006    cpl_frame* frame_dup   = NULL;
02007 
02008    int nsof=0;
02009    int i=0;
02010    nsof = cpl_frameset_get_size(sof);
02011    for (i=0 ; i<nsof ; i++) {
02012       frame = cpl_frameset_get_frame(sof,i);
02013       name= (char*) cpl_frame_get_filename(frame);
02014       if(sinfo_is_fits_file(name) == 1) {
02015          /* to go on the file must exist */
02016          if(cpl_frame_get_tag(frame) != NULL) {
02017             /* If the frame has a tag we process it. Else it is an object */
02018             tag= (char*) cpl_frame_get_tag(frame);
02019             /* sinfo_msg("name=%s tag=%s type=%s\n",name,tag,type); */
02020             if( strcmp(tag,PRO_FIBRE_NS_STACKED ) == 0) {
02021                /* sinfo_msg("Match name=%s tag=%s type=%s\n",name,tag); */
02022                frame_dup = cpl_frame_duplicate(frame);
02023                cpl_frameset_insert(raw,frame_dup);
02024                /* sinfo_msg("inserted\n"); */
02025             }
02026          }
02027       }
02028    }
02029    return 0;
02030 }
02031 
02032 
02033 int sinfo_contains_frames_type(cpl_frameset * sof,
02034                                cpl_frameset** raw,
02035                                const char*          type)
02036 {
02037    char* tag=NULL;
02038    char* name=NULL;
02039    cpl_frame* frame   = NULL;
02040    cpl_frame* frame_dup   = NULL;
02041    int nsof=0;
02042    int i=0;
02043    nsof = cpl_frameset_get_size(sof);
02044    for (i=0 ; i<nsof ; i++) {
02045       frame = cpl_frameset_get_frame(sof,i);
02046       name= (char*) cpl_frame_get_filename(frame);
02047       if(sinfo_is_fits_file(name) == 1) {
02048          /* to go on the file must exist */
02049          if(cpl_frame_get_tag(frame) != NULL) {
02050             /* If the frame has a tag we process it. Else it is an object */
02051             tag= (char*) cpl_frame_get_tag(frame);
02052             if(strstr(tag,type) != NULL) {
02053                /* sinfo_msg("name=%s tag=%s type=%s\n",name,tag,type); */
02054                frame_dup=cpl_frame_duplicate(frame);
02055                cpl_frameset_insert(*raw,frame_dup);
02056             }
02057          }
02058       }
02059    }
02060    return 0;
02061 }
02062 
02063 int sinfo_extract_raw_frames_type2(cpl_frameset * sof,
02064                                    cpl_frameset** raw,
02065                                    const char*          type)
02066 {
02067 
02068    cpl_frame* frame=NULL;
02069    cpl_frame* frame_dup   = NULL;
02070    frame = cpl_frameset_find(sof,type);
02071    while(frame) {
02072       frame_dup=cpl_frame_duplicate(frame);
02073       cpl_frameset_insert(*raw,frame_dup);
02074       frame = cpl_frameset_find(sof,NULL);
02075    }
02076    return 0;
02077 
02078 }
02079 
02080 
02081 int sinfo_extract_raw_frames_type1(cpl_frameset * sof,
02082                                    cpl_frameset* raw,
02083                                    const char*          type)
02084 {
02085 
02086    cpl_frame* frame=NULL;
02087    cpl_frame* frame_dup   = NULL;
02088    frame = cpl_frameset_find(sof,type);
02089    while(frame) {
02090       frame_dup=cpl_frame_duplicate(frame);
02091       cpl_frameset_insert(raw,frame_dup);
02092       frame = cpl_frameset_find(sof,NULL);
02093    }
02094    return 0;
02095 
02096 }
02097 
02098 int sinfo_extract_raw_frames_type(cpl_frameset * sof,
02099                                   cpl_frameset** raw,
02100                                   const char*          type)
02101 {
02102    char tag[FILE_NAME_SZ];
02103    char name[FILE_NAME_SZ];
02104    cpl_frame* frame   = NULL;
02105    cpl_frame* frame_dup   = NULL;
02106    int nsof=0;
02107    int i=0;
02108    nsof = cpl_frameset_get_size(sof);
02109    for (i=0 ; i<nsof ; i++) {
02110       frame = cpl_frameset_get_frame(sof,i);
02111       strcpy(name, cpl_frame_get_filename(frame));
02112       if(sinfo_is_fits_file(name) == 1) {
02113          /* to go on the file must exist */
02114          if(cpl_frame_get_tag(frame) != NULL) {
02115             /* If the frame has a tag we process it. Else it is an object */
02116             strcpy(tag,cpl_frame_get_tag(frame));
02117             if(strcmp(tag,type) == 0) {
02118                /* sinfo_msg("name=%s tag=%s type=%s\n",name,tag,type); */
02119                frame_dup=cpl_frame_duplicate(frame);
02120 
02121                cpl_frameset_insert(*raw,frame_dup);
02122             }
02123          }
02124       }
02125    }
02126    return 0;
02127 }
02128 
02129 int sinfo_extract_frames_type(cpl_frameset * sof,
02130                               cpl_frameset * raw,
02131                               const char*          type)
02132 {
02133    char* tag=NULL;
02134    char* name=NULL;
02135    cpl_frame* frame   = NULL;
02136    cpl_frame* frame_dup   = NULL;
02137    int nsof=0;
02138    int i=0;
02139    nsof = cpl_frameset_get_size(sof);
02140    for (i=0 ; i<nsof ; i++) {
02141       frame = cpl_frameset_get_frame(sof,i);
02142       name= (char*) cpl_frame_get_filename(frame);
02143       if(sinfo_is_fits_file(name) == 1) {
02144          /* to go on the file must exist */
02145          if(cpl_frame_get_tag(frame) != NULL) {
02146             /* If the frame has a tag we process it. Else it is an object */
02147             tag= (char*) cpl_frame_get_tag(frame);
02148             if(strcmp(tag,type) == 0) {
02149                /* sinfo_msg("name=%s tag=%s type=%s\n",name,tag,type); */
02150                frame_dup=cpl_frame_duplicate(frame);
02151                cpl_frameset_insert(raw,frame_dup);
02152             }
02153          }
02154       }
02155    }
02156    return 0;
02157 }
02158 
02159 
02160 int sinfo_extract_obj_frames(cpl_frameset * sof, cpl_frameset* obj)
02161 {
02162    char* tag=NULL;
02163    char* name=NULL;
02164    cpl_frame* frame   = NULL;
02165    cpl_frame* frame_dup   = NULL;
02166 
02167    int nsof=0;
02168    int i=0;
02169    nsof = cpl_frameset_get_size(sof);
02170    for (i=0 ; i<nsof ; i++) {
02171       frame = cpl_frameset_get_frame(sof,i);
02172       name= (char*) cpl_frame_get_filename(frame);
02173       if(sinfo_is_fits_file(name) ==1) {
02174          /* to go on the file must exist */
02175          if(cpl_frame_get_tag(frame) != NULL) {
02176             /* If the frame has a tag we process it. Else it is an object */
02177             tag= (char*) cpl_frame_get_tag(frame);
02178             if(sinfo_tag_is_obj(tag) == 1) {
02179                frame_dup=cpl_frame_duplicate(frame);
02180                cpl_frameset_insert(obj,frame_dup);
02181             }
02182          }
02183       }
02184    }
02185 
02186    return 0;
02187 }
02188 
02189 
02190 int sinfo_extract_obj_products(cpl_frameset * sof, cpl_frameset* obj)
02191 {
02192    char* tag=NULL;
02193    char* name=NULL;
02194    cpl_frame* frame   = NULL;
02195    cpl_frame* frame_dup   = NULL;
02196 
02197    int nsof=0;
02198    int i=0;
02199    nsof = cpl_frameset_get_size(sof);
02200    for (i=0 ; i<nsof ; i++) {
02201       frame = cpl_frameset_get_frame(sof,i);
02202       name= (char*) cpl_frame_get_filename(frame);
02203       if(sinfo_is_fits_file(name) ==1) {
02204          /* to go on the file must exist */
02205          if(cpl_frame_get_tag(frame) != NULL) {
02206             /* If the frame has a tag we process it. Else it is an object */
02207             tag= (char*) cpl_frame_get_tag(frame);
02208             if(sinfo_tag_is_objpro(tag) == 1) {
02209                frame_dup=cpl_frame_duplicate(frame);
02210                cpl_frameset_insert(obj,frame_dup);
02211             }
02212          }
02213       }
02214    }
02215 
02216    return 0;
02217 }
02218 
02219 int sinfo_extract_on_frames(cpl_frameset * sof, cpl_frameset* on)
02220 {
02221    cpl_frame* frame   = NULL;
02222    cpl_frame* frame_dup   = NULL;
02223 
02224    int nsof=0;
02225    int i=0;
02226    nsof = cpl_frameset_get_size(sof);
02227    for (i=0 ; i<nsof ; i++) {
02228       frame = cpl_frameset_get_frame(sof,i);
02229       if(sinfo_frame_is_on(frame) ==1) {
02230          frame_dup=cpl_frame_duplicate(frame);
02231          cpl_frameset_insert(on,frame_dup);
02232       }
02233    }
02234 
02235    return 0;
02236 }
02237 
02238 int sinfo_extract_sky_frames(cpl_frameset * sof, cpl_frameset* sky)
02239 {
02240    char* tag=NULL;
02241    char* name=NULL;
02242    cpl_frame* frame   = NULL;
02243    cpl_frame* frame_dup   = NULL;
02244    int nsof=0;
02245    int i=0;
02246    nsof = cpl_frameset_get_size(sof);
02247    for (i=0 ; i<nsof ; i++) {
02248       frame = cpl_frameset_get_frame(sof,i);
02249       name= (char*) cpl_frame_get_filename(frame);
02250       if(sinfo_is_fits_file(name) ==1) {
02251          /* to go on the file must exist */
02252          if(cpl_frame_get_tag(frame) != NULL) {
02253             /* If the frame has a tag we process it. Else it is an object */
02254             tag= (char*) cpl_frame_get_tag(frame);
02255             if(sinfo_tag_is_sky(tag) == 1) {
02256                frame_dup=cpl_frame_duplicate(frame);
02257                cpl_frameset_insert(sky,frame_dup);
02258             }
02259          }
02260       }
02261    }
02262 
02263    return 0;
02264 }
02265 
02266 
02267 
02268 int sinfo_extract_off_frames(cpl_frameset * sof, cpl_frameset* off)
02269 {
02270    cpl_frame* frame   = NULL;
02271    cpl_frame* frame_dup   = NULL;
02272    int nsof=0;
02273    int i=0;
02274    nsof = cpl_frameset_get_size(sof);
02275    for (i=0 ; i<nsof ; i++) {
02276       frame = cpl_frameset_get_frame(sof,i);
02277       if(sinfo_frame_is_on(frame)) {
02278          frame_dup=cpl_frame_duplicate(frame);
02279          cpl_frameset_insert(off,frame_dup);
02280       }
02281    }
02282 
02283    return 0;
02284 }
02285 
02286 int sinfo_extract_mst_frames(cpl_frameset * sof, cpl_frameset* cdb)
02287 {
02288    char* tag=NULL;
02289    char* name=NULL;
02290    cpl_frame* frame   = NULL;
02291    cpl_frame* frame_dup   = NULL;
02292    int nsof=0;
02293    int i=0;
02294 
02295    nsof = cpl_frameset_get_size(sof);
02296    for (i=0 ; i<nsof ; i++) {
02297       frame = cpl_frameset_get_frame(sof,i);
02298       name= (char*) cpl_frame_get_filename(frame);
02299       if(sinfo_is_fits_file(name) ==1) {
02300          /* to go on the file must exist */
02301          if(cpl_frame_get_tag(frame) != NULL) {
02302             /* If the frame has a tag we process it. Else it is an object */
02303             tag= (char*) cpl_frame_get_tag(frame);
02304             if(sinfo_frame_is_cdb(tag) == 1) {
02305                frame_dup=cpl_frame_duplicate(frame);
02306                cpl_frameset_insert(cdb,frame_dup);
02307             }
02308          }
02309       }
02310    }
02311 
02312    return 0;
02313 }
02314 
02315 cpl_frameset* sinfo_frameset_join(cpl_frameset* fs1,cpl_frameset* fs2) {
02316 
02317    cpl_frameset* join=NULL;
02318    cpl_frame* frm=NULL;
02319    cpl_frame* frm_dup=NULL;
02320    int i=0;
02321    int n=0;
02322 
02323    join=cpl_frameset_new();
02324 
02325    n=cpl_frameset_get_size(fs1);
02326    for(i=0;i<n; i++) {
02327       frm=cpl_frameset_get_frame(fs1,i);
02328       frm_dup= cpl_frame_duplicate(frm);
02329       cpl_frameset_insert(join,frm_dup);
02330    }
02331 
02332    n=cpl_frameset_get_size(fs2);
02333    for(i=0;i<n; i++) {
02334       frm=cpl_frameset_get_frame(fs2,i);
02335       frm_dup= cpl_frame_duplicate(frm);
02336       cpl_frameset_insert(join,frm_dup);
02337    }
02338 
02339 
02340    return join;
02341 
02342 }
02343 
02344 
02345 int sinfo_extract_stk_frames(cpl_frameset * sof,
02346                              cpl_frameset* res)
02347 {
02348    char* tag=NULL;
02349    char* name=NULL;
02350    cpl_frame* frame   = NULL;
02351    cpl_frame* frame_dup   = NULL;
02352    int nsof=0;
02353    int i=0;
02354 
02355    nsof = cpl_frameset_get_size(sof);
02356    for (i=0 ; i<nsof ; i++) {
02357       frame = cpl_frameset_get_frame(sof,i);
02358       name= (char*) cpl_frame_get_filename(frame);
02359       if(sinfo_is_fits_file(name) ==1) {
02360          /* to go on the file must exist */
02361          if(cpl_frame_get_tag(frame) != NULL) {
02362             /* If the frame has a tag we process it. Else it is an object */
02363             tag= (char*) cpl_frame_get_tag(frame);
02364             if(sinfo_frame_is_stk(tag) == 1) {
02365                frame_dup=cpl_frame_duplicate(frame);
02366                cpl_frameset_insert(res,frame_dup);
02367             }
02368          }
02369       }
02370    }
02371 
02372    return 0;
02373 }
02374 
02375 
02376 int
02377 sinfo_extract_preoptic_frames(cpl_frameset * sof,
02378                               cpl_frameset** res,
02379                               const char* val)
02380 {
02381    char* name=NULL;
02382    cpl_frame* frame   = NULL;
02383    cpl_frame* frame_dup   = NULL;
02384    int nsof=0;
02385    int i=0;
02386 
02387    nsof = cpl_frameset_get_size(sof);
02388    for (i=0 ; i<nsof ; i++) {
02389       frame = cpl_frameset_get_frame(sof,i);
02390       name= (char*) cpl_frame_get_filename(frame);
02391       if(sinfo_is_fits_file(name) ==1) {
02392          if(sinfo_frame_is_preoptic(frame,val) == 1) {
02393             frame_dup=cpl_frame_duplicate(frame);
02394             cpl_frameset_insert(*res,frame_dup);
02395          }
02396       }
02397    }
02398 
02399    return 0;
02400 }
02401 
02402 int sinfo_extract_raw_stack_frames(cpl_frameset * sof, cpl_frameset** pro)
02403 {
02404    char* tag=NULL;
02405    char* name=NULL;
02406    cpl_frame* frame   = NULL;
02407    cpl_frame* frame_dup   = NULL;
02408 
02409    int nsof=0;
02410    int i=0;
02411    nsof = cpl_frameset_get_size(sof);
02412 
02413    for (i=0 ; i<nsof ; i++) {
02414       frame = cpl_frameset_get_frame(sof,i);
02415       name= (char*) cpl_frame_get_filename(frame);
02416       if(sinfo_is_fits_file(name) ==1) {
02417          /* to go on the file must exist */
02418          if(cpl_frame_get_tag(frame) != NULL) {
02419             /* If the frame has a tag we process it. Else it is an object */
02420             tag= (char*) cpl_frame_get_tag(frame);
02421             /* sinfo_msg("tag=%s\n",tag); */
02422             if(sinfo_frame_is_raw_stack(tag) == 1) {
02423                frame_dup   = cpl_frame_duplicate(frame);
02424                cpl_frameset_insert(*pro,frame_dup);
02425             }
02426          }
02427       }
02428    }
02429 
02430    return 0;
02431 }
02432 
02433 
02434 int sinfo_extract_raw_slit_frames(cpl_frameset * sof, cpl_frameset** pro)
02435 {
02436    char* tag=NULL;
02437    char* name=NULL;
02438    cpl_frame* frame   = NULL;
02439    int nsof=0;
02440    int i=0;
02441    nsof = cpl_frameset_get_size(sof);
02442    for (i=0 ; i<nsof ; i++) {
02443       frame = cpl_frameset_get_frame(sof,i);
02444       name= (char*) cpl_frame_get_filename(frame);
02445       if(sinfo_is_fits_file(name) ==1) {
02446          /* to go on the file must exist */
02447          if(cpl_frame_get_tag(frame) != NULL) {
02448             /* If the frame has a tag we process it. Else it is an object */
02449             tag= (char*) cpl_frame_get_tag(frame);
02450             if(sinfo_frame_is_slit_lamp(tag) == 1) {
02451                cpl_frameset_insert(*pro,frame);
02452             }
02453          }
02454       }
02455    }
02456 
02457    return 0;
02458 }
02459 
02460 /*---------------------------------------------------------------------------*/
02466 /*---------------------------------------------------------------------------*/
02467 int sinfo_frame_is_raw(char * tag)
02468 {
02469    /* Test entries */
02470    if (tag == NULL) return -1 ;
02471 
02472    if (!strcmp(tag, RAW_LINEARITY_LAMP)) return 1 ;
02473    if (!strcmp(tag, RAW_DARK)) return 1 ;
02474    if (!strcmp(tag, RAW_PINHOLE_LAMP)) return 1 ;
02475    if (!strcmp(tag, RAW_SLIT_LAMP)) return 1 ;
02476    if (!strcmp(tag, RAW_WAVE_LAMP)) return 1 ;
02477    if (!strcmp(tag, RAW_FLAT_LAMP)) return 1 ;
02478    if (!strcmp(tag, RAW_WAVE_NS)) return 1 ;
02479    if (!strcmp(tag, RAW_FLAT_NS)) return 1 ;
02480    if (!strcmp(tag, RAW_FIBRE_LAMP)) return 1 ;
02481    if (!strcmp(tag, RAW_FIBRE_EW)) return 1 ;
02482    if (!strcmp(tag, RAW_FIBRE_NS)) return 1 ;
02483    if (!strcmp(tag, RAW_FLAT_SKY)) return 1 ;
02484    if (!strcmp(tag, RAW_FLUX_LAMP)) return 1 ;
02485    if (!strcmp(tag, RAW_PSF_CALIBRATOR)) return 1 ;
02486    if (!strcmp(tag, RAW_FOCUS)) return 1 ;
02487 
02488    if (!strcmp(tag, RAW_STD)) return 1 ;
02489    if (!strcmp(tag, RAW_STD_STAR)) return 1 ;
02490    if (!strcmp(tag, RAW_STD_STAR_DITHER)) return 1 ;
02491    if (!strcmp(tag, RAW_SKY_STD)) return 1 ;
02492    if (!strcmp(tag, RAW_SKY_OH)) return 1 ;
02493    if (!strcmp(tag, RAW_SKY_PSF_CALIBRATOR)) return 1 ;
02494 
02495    if (!strcmp(tag, RAW_PUPIL_LAMP)) return 1 ;
02496    if (!strcmp(tag, RAW_OBJECT_JITTER)) return 1 ;
02497    if (!strcmp(tag, RAW_SKY_JITTER)) return 1 ;
02498    if (!strcmp(tag, RAW_OBJECT_NODDING)) return 1 ;
02499    if (!strcmp(tag, RAW_OBJECT_SKYSPIDER)) return 1 ;
02500    if (!strcmp(tag, RAW_SKY_NODDING)) return 1 ;
02501 
02502    if (!strcmp(tag, RAW_FLAT_LAMP_DITHER)) return 1 ;
02503    if (!strcmp(tag, RAW_WAVE_LAMP_DITHER)) return 1 ;
02504    if (!strcmp(tag, RAW_STD_STAR_DITHER)) return 1 ;
02505    if (!strcmp(tag, RAW_OBJECT_NODDING_DITHER)) return 1 ;
02506    if (!strcmp(tag, RAW_OBJECT_SKYSPIDER_DITHER)) return 1 ;
02507    if (!strcmp(tag, RAW_SKY_NODDING_DITHER)) return 1 ;
02508 
02509 
02510    return 0 ;
02511 }
02512 
02513 /*---------------------------------------------------------------------------*/
02519 /*---------------------------------------------------------------------------*/
02520 int sinfo_frame_is_raw_stack(char * tag)
02521 {
02522    /* Test entries */
02523    if (tag == NULL) return -1 ;
02524 
02525 
02526    if (!strcmp(tag, PRO_SKY_DUMMY)) return 1 ;
02527    if (!strcmp(tag, RAW_WAVE_LAMP)) return 1 ;
02528    if (!strcmp(tag, RAW_WAVE_LAMP_DITHER)) return 1 ;
02529    if (!strcmp(tag, RAW_WAVE_NS)) return 1 ;
02530    if (!strcmp(tag, RAW_WAVE_NS_DITHER)) return 1 ;
02531 
02532    if (!strcmp(tag, RAW_FLUX_LAMP)) return 1 ;
02533    if (!strcmp(tag, RAW_FIBRE_NS)) return 1 ;
02534    if (!strcmp(tag, RAW_FIBRE_EW)) return 1 ;
02535 
02536    if (!strcmp(tag, RAW_PSF_CALIBRATOR)) return 1 ;
02537    if (!strcmp(tag, RAW_FIBRE_PSF)) return 1 ;
02538    if (!strcmp(tag, RAW_FIBRE_DARK)) return 1 ;
02539 
02540    if (!strcmp(tag, RAW_FOCUS)) return 1 ;
02541 
02542    if (!strcmp(tag, RAW_PUPIL_LAMP)) return 1 ;
02543    if (!strcmp(tag, RAW_OBJECT_JITTER)) return 1 ;
02544    if (!strcmp(tag, RAW_SKY_JITTER)) return 1 ;
02545    if (!strcmp(tag, RAW_OBJECT_NODDING)) return 1 ;
02546    if (!strcmp(tag, RAW_OBJECT_SKYSPIDER)) return 1 ;
02547    if (!strcmp(tag, RAW_SKY_NODDING)) return 1 ;
02548 
02549    if (!strcmp(tag, RAW_OBJECT_NODDING_DITHER)) return 1 ;
02550    if (!strcmp(tag, RAW_OBJECT_SKYSPIDER_DITHER)) return 1 ;
02551    if (!strcmp(tag, RAW_SKY_NODDING_DITHER)) return 1 ;
02552 
02553 
02554    if (!strcmp(tag, RAW_IMAGE_PRE_OBJECT)) return 1 ;
02555    if (!strcmp(tag, RAW_IMAGE_PRE_SKY)) return 1 ;
02556    if (!strcmp(tag, RAW_STD)) return 1 ;
02557    if (!strcmp(tag, RAW_SKY_STD)) return 1 ;
02558    if (!strcmp(tag, RAW_SKY_OH)) return 1 ;
02559    if (!strcmp(tag, RAW_SKY_PSF_CALIBRATOR)) return 1 ;
02560    if (!strcmp(tag, RAW_STD_STAR)) return 1 ;
02561    if (!strcmp(tag, RAW_SKY)) return 1 ;
02562 
02563    return 0 ;
02564 }
02565 
02566 
02567 /*---------------------------------------------------------------------------*/
02573 /*---------------------------------------------------------------------------*/
02574 int sinfo_frame_is_raw_dark(char * tag)
02575 {
02576    /* Test entries */
02577    if (tag == NULL) return -1 ;
02578 
02579    if (!strcmp(tag, RAW_DARK)) return 1 ;
02580 
02581    return 0 ;
02582 }
02583 
02584 /*---------------------------------------------------------------------------*/
02590 /*---------------------------------------------------------------------------*/
02591 int sinfo_frame_is_slit_lamp(char * tag)
02592 {
02593    /* Test entries */
02594    if (tag == NULL) return -1 ;
02595 
02596    if (!strcmp(tag, RAW_SLIT_LAMP)) return 1 ;
02597 
02598    return 0 ;
02599 }
02600 
02601 
02602 /*---------------------------------------------------------------------------*/
02608 /*---------------------------------------------------------------------------*/
02609 int sinfo_frame_is_pinhole_lamp(char * tag)
02610 {
02611    /* Test entries */
02612    if (tag == NULL) return -1 ;
02613 
02614    if (!strcmp(tag, RAW_PINHOLE_LAMP)) return 1 ;
02615 
02616    return 0 ;
02617 }
02618 
02619 
02620 int sinfo_frame_is_cdb(char * tag)
02621 {
02622    /* Test entries */
02623    if (tag == NULL) return -1 ;
02624    /* For the moment not checked the following:
02625 
02626    PRO_STACKED
02627    PRO_SLIT_ON
02628    PRO_FLUX_LAMP_STACKED
02629    PRO_WAVE_LAMP_STACKED
02630    PRO_PSF_CALIBRATOR_STACKED
02631    PRO_FOCUS_STACKED
02632    PRO_OBJECT_NODDING_STACKED
02633    PRO_OBJECT_SKYSPIDER_STACKED
02634    PRO_SKY_NODDING_STACKED
02635    PRO_STD_NODDING_STACKED
02636    PRO_MASK_CUBE
02637    PRO_PSF
02638    TMP_FOCUS
02639    TMP_FOCUS_ON
02640    TMP_FOCUS_OFF
02641    PRO_FOCUS
02642    PRO_FOCUS_GAUSS
02643    PRO_SPECTRA
02644    PRO_CUBE
02645    PRO_CUBE_COLL
02646    PRO_SLOPEX
02647    PRO_SLOPEY
02648    PRO_MASK_CUBE
02649    PRO_OBJ_CUBE
02650    PRO_BP_COEFF
02651    */
02652 
02653    if (!strcmp(tag, REF_LINE_ARC)) return 1 ;
02654    if (!strcmp(tag, REF_LINE_OH)) return 1 ;
02655    if (!strcmp(tag, PRO_BP_MAP)) return 1 ;
02656    if (!strcmp(tag, PRO_BP_MAP_HP)) return 1 ;
02657    if (!strcmp(tag, PRO_BP_MAP_DI)) return 1 ;
02658    if (!strcmp(tag, PRO_BP_MAP_NO)) return 1 ;
02659    if (!strcmp(tag, PRO_BP_MAP_NL)) return 1 ;
02660    if (!strcmp(tag, PRO_MASTER_BP_MAP)) return 1 ;
02661    if (!strcmp(tag, PRO_MASTER_DARK)) return 1 ;
02662    if (!strcmp(tag, PRO_SLOPE)) return 1 ;
02663    if (!strcmp(tag, PRO_DISTORTION)) return 1 ;
02664    if (!strcmp(tag, PRO_SLITLETS_DISTANCE)) return 1 ;
02665    if (!strcmp(tag, PRO_MASTER_FLAT_LAMP)) return 1 ;
02666    if (!strcmp(tag, PRO_MASTER_FLAT_LAMP1)) return 1 ;
02667    if (!strcmp(tag, PRO_MASTER_FLAT_LAMP2)) return 1 ;
02668    if (!strcmp(tag, PRO_SLIT_POS)) return 1 ;
02669    if (!strcmp(tag, PRO_SLIT_POS_GUESS)) return 1 ;
02670    if (!strcmp(tag, PRO_WAVE_PAR_LIST)) return 1 ;
02671    if (!strcmp(tag, PRO_WAVE_COEF_SLIT)) return 1 ;
02672    if (!strcmp(tag, PRO_MASTER_LAMP_SPEC)) return 1 ;
02673    if (!strcmp(tag, PRO_MASTER_TWIFLAT)) return 1 ;
02674    if (!strcmp(tag, PRO_COEFF_LIST)) return 1 ;
02675    if (!strcmp(tag, PRO_INDEX_LIST)) return 1 ;
02676    if (!strcmp(tag, PRO_HALO_SPECT)) return 1 ;
02677    if (!strcmp(tag, PRO_FIRST_COL)) return 1 ;
02678    if (!strcmp(tag, PRO_FOCUS)) return 1 ;
02679    if (!strcmp(tag, PRO_WAVE_MAP)) return 1 ;
02680    if (!strcmp(tag, PRO_REF_ATM_REF_CORR)) return 1 ;
02681 
02682    return 0;
02683 
02684 }
02685 
02686 
02687 
02688 
02689 int sinfo_frame_is_stk(char * tag)
02690 {
02691    /* Test entries */
02692    if (tag == NULL) return -1 ;
02693    /* For the moment not checked the following: */
02694 
02695 
02696    if (!strcmp(tag, PRO_SKY_STACKED_DUMMY)) return 1 ;
02697    if (!strcmp(tag, PRO_STACK_SKY_DIST)) return 1 ;
02698    if (!strcmp(tag, PRO_STACK_MFLAT_DIST)) return 1 ;
02699    if (!strcmp(tag, PRO_PSF_CALIBRATOR_STACKED)) return 1 ;
02700 
02701 
02702    return 0;
02703 
02704 }
02705 
02706 int
02707 sinfo_propertylist_has(cpl_propertylist* plist,
02708                        const char* key) {
02709 
02710    return cpl_propertylist_has(plist,key);
02711 
02712 }
02713 
02714 int sinfo_frame_is_preoptic(cpl_frame* frame,const char* val)
02715 {
02716 
02717    char* file=NULL;
02718    char popt[FILE_NAME_SZ];
02719    cpl_propertylist* plist=NULL;
02720 
02721 
02722    file = cpl_strdup(cpl_frame_get_filename(frame)) ;
02723    if ((cpl_error_code)((plist = cpl_propertylist_load(file, 0)) == NULL)) {
02724       sinfo_msg_error( "getting header from reference frame %s",file);
02725       cpl_propertylist_delete(plist) ;
02726       cpl_free(file);
02727       return -1 ;
02728    }
02729 
02730    if (sinfo_propertylist_has(plist, KEY_NAME_PREOPTICS)) {
02731       strcpy(popt,cpl_propertylist_get_string(plist, KEY_NAME_PREOPTICS));
02732    } else {
02733       sinfo_msg_error("keyword %s does not exist",KEY_NAME_PREOPTICS);
02734       cpl_free(file);
02735       return -1;
02736    }
02737    cpl_propertylist_delete(plist) ;
02738    cpl_free(file);
02739 
02740    if (strstr(val,popt) != NULL) return 1 ;
02741 
02742 
02743    return 0;
02744 
02745 }
02746 
02747 
02748 int sinfo_get_preoptic(const char* file, const char* val)
02749 {
02750 
02751    cpl_propertylist* plist=NULL;
02752 
02753 
02754    if ((cpl_error_code)((plist = cpl_propertylist_load(file, 0)) == NULL)) {
02755       sinfo_msg_error( "getting header from reference frame %s",file);
02756       cpl_propertylist_delete(plist) ;
02757       return -1 ;
02758    }
02759 
02760    if (sinfo_propertylist_has(plist, KEY_NAME_PREOPTICS)) {
02761       strcpy((char*)val,cpl_propertylist_get_string(plist, KEY_NAME_PREOPTICS));
02762    } else {
02763       sinfo_msg_error("keyword %s does not exist",KEY_NAME_PREOPTICS);
02764       return -1;
02765    }
02766    cpl_propertylist_delete(plist) ;
02767 
02768    return 0;
02769 
02770 }
02771 
02772 /*
02773   static int
02774   sinfo_stat_rectangle(cpl_image* img,
02775   const int kappa,
02776   const int nclip,
02777   double *mean,
02778   double *stdev)
02779   {
02780 
02781   double sum=0;
02782   double sum2=0;
02783   double noise=0;
02784 
02785   double* pim=NULL;
02786   int i=0;
02787   int j=0;
02788   int kk=0;
02789   int sx=0;
02790   int sy=0;
02791 
02792   *mean=0;
02793   pim=cpl_image_get_data(img);
02794   kk=0;
02795   for(i=0;i<sx*sy;i++) {
02796   *mean+=pim[i];
02797   }
02798   *mean/=(sx*sy);
02799 
02800   for(i=0;i<sx*sy;i++) {
02801   sum+=(pim[i]-*mean)*(pim[i]-*mean);
02802   }
02803   noise=sqrt(sum/(sx*sy));
02804 
02805 
02806   //clean a bit the bad pixels
02807   for(j=0;j<nclip;j++) {
02808   sum=0;
02809   sum2=0;
02810   kk=0;
02811   for(i=0;i<sx*sy;i++) {
02812   if(fabs(pim[i]-*mean)<kappa*noise) {
02813 
02814   sum  +=(pim[i]-*mean)*(pim[i]-*mean);
02815   sum2 += pim[i];
02816   kk+=1;
02817   }
02818   noise=sqrt(sum/kk);
02819   *mean=sum2/kk;
02820 
02821   }
02822 
02823   }
02824   *stdev=noise;
02825 
02826   return 0;
02827 
02828   }
02829 */
02830 
02831 cpl_table* sinfo_compute_gain(cpl_frameset* son, cpl_frameset* sof)
02832 {
02833 
02834 
02835    cpl_frame*    frm=NULL;
02836 
02837    cpl_image* img_on1=NULL;
02838    cpl_image* img_on2=NULL;
02839    cpl_image* img_on_dif=NULL;
02840    cpl_image* img_on_sub=NULL;
02841 
02842 
02843    cpl_image* img_of1=NULL;
02844    cpl_image* img_of2=NULL;
02845    cpl_image* img_of_dif=NULL;
02846    cpl_image* img_of_sub=NULL;
02847 
02848    cpl_table* res_tbl=NULL;
02849    cpl_vector* dit_on=NULL;
02850    cpl_vector* dit_of=NULL;
02851    cpl_vector* exptime_on=NULL;
02852    cpl_vector* exptime_of=NULL;
02853    cpl_propertylist* plist=NULL;
02854 
02855    int non=0;
02856    int nof=0;
02857    int nfr=0;
02858    double avg_on1=0;
02859    double avg_on2=0;
02860    double avg_of1=0;
02861    double avg_of2=0;
02862    double std=0;
02863 
02864    double sig_on_dif=0;
02865    double sig_of_dif=0;
02866    char* name=NULL;
02867    int i=0;
02868    int m=0;
02869 
02870    int llx=270;
02871    int lly=1000;
02872    int urx=320;
02873    int ury=1050;
02874    int zone[4];
02875    double gain=0;
02876    double dit_ref=0;
02877    double dit_tmp=0;
02878    double exptime_ref=0;
02879    double exptime_tmp=0;
02880    int kappa=5;
02881    int nclip=25;
02882    double centre=0;
02883 
02884    non = cpl_frameset_get_size(son);
02885    nof = cpl_frameset_get_size(sof);
02886    nfr = (non <= nof) ? non : nof;
02887 
02888    dit_on=cpl_vector_new(nfr);
02889    dit_of=cpl_vector_new(nfr);
02890    exptime_on=cpl_vector_new(nfr);
02891    exptime_of=cpl_vector_new(nfr);
02892 
02893    for(i=0;i<nfr;i++) {
02894 
02895       frm=cpl_frameset_get_frame(son,i);
02896       name=(char*)cpl_frame_get_filename(frm);
02897       plist=cpl_propertylist_load(name,0);
02898       dit_ref=sinfo_pfits_get_dit(plist);
02899       exptime_ref=(double)sinfo_pfits_get_exp_time(plist);
02900       cpl_propertylist_delete(plist);
02901       cpl_vector_set(dit_on,i,dit_ref);
02902       cpl_vector_set(exptime_on,i,exptime_ref);
02903 
02904       frm=cpl_frameset_get_frame(sof,i);
02905       name=(char*)cpl_frame_get_filename(frm);
02906       plist=cpl_propertylist_load(name,0);
02907       dit_ref=sinfo_pfits_get_dit(plist);
02908       exptime_ref=(double)sinfo_pfits_get_exp_time(plist);
02909       cpl_propertylist_delete(plist);
02910       cpl_vector_set(dit_of,i,dit_ref);
02911       cpl_vector_set(exptime_of,i,exptime_ref);
02912 
02913    }
02914 
02915 
02916 
02917    zone[0]=270;
02918    zone[1]=1030;
02919    zone[2]=310;
02920    zone[3]=1060;
02921 
02922 
02923 
02924    zone[0]=20;
02925    zone[1]=2028;
02926    zone[2]=20;
02927    zone[3]=2028;
02928 
02929 
02930 
02931    check_nomsg(res_tbl=cpl_table_new(nfr));
02932    cpl_table_new_column(res_tbl,"adu", CPL_TYPE_DOUBLE);
02933    cpl_table_new_column(res_tbl,"gain", CPL_TYPE_DOUBLE);
02934 
02935    for(i=0;i<nfr;i++) {
02936       frm=cpl_frameset_get_frame(son,i);
02937       name=(char*)cpl_frame_get_filename(frm);
02938       img_on1=cpl_image_load(name,CPL_TYPE_DOUBLE,0,0);
02939 
02940       frm=cpl_frameset_get_frame(sof,i);
02941       name=(char*)cpl_frame_get_filename(frm);
02942       img_of1=cpl_image_load(name,CPL_TYPE_DOUBLE,0,0);
02943 
02944 
02945       dit_ref=cpl_vector_get(dit_on,i);
02946       exptime_ref=cpl_vector_get(exptime_on,i);
02947 
02948 
02949       for(m=0;m<nfr; m++) {
02950          if(m != i) {
02951             frm=cpl_frameset_get_frame(son,m);
02952             name=(char*)cpl_frame_get_filename(frm);
02953             dit_tmp=cpl_vector_get(dit_on,m);
02954             exptime_tmp=cpl_vector_get(exptime_on,m);
02955             if(dit_tmp == dit_ref && exptime_tmp == exptime_ref) {
02956                /* sinfo_msg("m=%d i=%d\n",m,i); */
02957                img_on2=cpl_image_load(name,CPL_TYPE_DOUBLE,0,0);
02958                frm=cpl_frameset_get_frame(sof,m);
02959                name=(char*)cpl_frame_get_filename(frm);
02960                img_of2=cpl_image_load(name,CPL_TYPE_DOUBLE,0,0);
02961 
02962                img_on_dif=cpl_image_subtract_create(img_on1,img_on2);
02963                img_of_dif=cpl_image_subtract_create(img_of1,img_of2);
02964 
02965                img_on_sub=cpl_image_extract(img_on_dif,llx,lly,urx,ury);
02966                img_of_sub=cpl_image_extract(img_of_dif,llx,lly,urx,ury);
02967 
02968                sinfo_get_clean_mean_window(img_on1,llx,lly,urx,ury,kappa,
02969                                            nclip,&avg_on1,&std);
02970                sinfo_get_clean_mean_window(img_on2,llx,lly,urx,ury,kappa,
02971                                            nclip,&avg_on2,&std);
02972                sinfo_get_clean_mean_window(img_of1,llx,lly,urx,ury,kappa,
02973                                            nclip,&avg_of1,&std);
02974                sinfo_get_clean_mean_window(img_of2,llx,lly,urx,ury,kappa,
02975                                            nclip,&avg_of2,&std);
02976                /*
02977                  cpl_image_save(img_on_sub,"ima_on_sub.fits",
02978                                 CPL_BPP_IEEE_FLOAT,NULL,CPL_IO_DEFAULT);
02979                  cpl_image_save(img_of_sub,"ima_of_sub.fits",
02980                                 CPL_BPP_IEEE_FLOAT,NULL,CPL_IO_DEFAULT);
02981                */
02982                /*
02983                //worse accuracy
02984                sinfo_stat_rectangle(img_on_dif,kappa,nclip,
02985                                     &centre,&sig_on_dif);
02986                sinfo_stat_rectangle(img_of_dif,kappa,nclip,
02987                                     &centre,&sig_of_dif);
02988                */
02989 
02990 
02991                //better accuracy
02992                sinfo_get_clean_mean_window(img_on_dif,llx,lly,urx,ury,kappa,
02993                                            nclip,&centre,&sig_on_dif);
02994                sinfo_get_clean_mean_window(img_of_dif,llx,lly,urx,ury,kappa,
02995                                            nclip,&centre,&sig_of_dif);
03012                cpl_image_delete(img_on2);
03013                cpl_image_delete(img_of2);
03014                cpl_image_delete(img_on_dif);
03015                cpl_image_delete(img_of_dif);
03016                cpl_image_delete(img_on_sub);
03017                cpl_image_delete(img_of_sub);
03018 
03019                gain=((avg_on1+avg_on2)-(avg_of1+avg_of2))/
03020                   ((sig_on_dif*sig_on_dif)-(sig_of_dif*sig_of_dif));
03021 
03022                cpl_table_set_double(res_tbl,"gain",m,gain);
03023                cpl_table_set_double(res_tbl,"adu",m,
03024                                     ((avg_on1+avg_on2)/2-(avg_of1+avg_of2)/2));
03025                /* sinfo_msg("gain=%f ADU=%f\n",gain,
03026                   (avg_on1+avg_on2)/2-(avg_of1+avg_of2)/2);
03027                   sinfo_msg("g=%f avg_on1=%f avg_on2=%f",gain,avg_on1,avg_on2);
03028                  sinfo_msg("avg_of1=%f avg_of2=%f sig_on_dif=%f sig_of_dif=%f",
03029                   avg_of1,avg_of2,sig_on_dif,sig_of_dif);
03030                */
03031 
03032             }
03033          }
03034       }
03035       cpl_image_delete(img_on1);
03036       cpl_image_delete(img_of1);
03037    }
03038 
03039 
03040    /*
03041      sinfo_get_clean_mean_window(img_on_dif,llx,lly,urx,ury,kappa,
03042      nclip,&avg,&sig_on_dif);
03043      sinfo_get_clean_mean_window(img_of_dif,llx,lly,urx,ury,kappa,
03044      nclip,&avg,&sig_of_dif);
03045    */
03046 
03047    cpl_vector_delete(dit_on);
03048    cpl_vector_delete(dit_of);
03049    cpl_vector_delete(exptime_on);
03050    cpl_vector_delete(exptime_of);
03051 
03052    return res_tbl;
03053 
03054   cleanup:
03055    return NULL;
03056 
03057 }
03058 
03059 
03060 /*-------------------------------------------------------------------------*/
03072 /*--------------------------------------------------------------------------*/
03073 
03074 int
03075 sinfo_image_estimate_noise(cpl_image* img,
03076                            const int noise_fit,
03077                            double* centre,
03078                            double* noise)
03079 {
03080 
03081    int nbins=0;
03082 
03083    int xsz=0;
03084    int ysz=0;
03085    int n=0;
03086    int i=0;
03087    int r=0;
03088 
03089    int ndist=0;
03090    double min_fct=HISTO_DIST_TEMPC_MIN_FCT;
03091    double max_fct=HISTO_DIST_TEMPC_MAX_FCT;
03092    double avg_d=0;
03093    double std_d=0;
03094    double hmin=0;
03095    double hmax=0;
03096    double kappa=3;
03097 
03098    double* pdata=NULL;
03099    double* disth=NULL;
03100    double* distx=NULL;
03101 
03102    double peak=0;
03103    double tempc=0;
03104    double value=0;
03105    double x0=0;
03106    double sigma=0;
03107    double area=0;
03108    double offset=0;
03109    //double mse=0;
03110    //double chired=0;
03111 
03112    cpl_table* data_tbl=NULL;
03113    cpl_table* histo=NULL;
03114    cpl_table* dist=NULL;
03115    cpl_table* min_xi=NULL;
03116    cpl_table* tmp_tbl1=NULL;
03117    cpl_table* tmp_tbl2=NULL;
03118    cpl_vector* vx=NULL;
03119    cpl_vector* vy=NULL;
03120    cpl_vector* sx=NULL;
03121    cpl_vector* sy=NULL;
03122 
03123    // Get Object relevant information
03124    check_nomsg(xsz=cpl_image_get_size_x(img));
03125    check_nomsg(ysz=cpl_image_get_size_y(img));
03126    n=xsz*ysz;
03127    nbins=sqrt(n);
03128    check_nomsg(data_tbl=cpl_table_new(n));
03129    check_nomsg(cpl_table_new_column(data_tbl,"DATA",CPL_TYPE_DOUBLE));
03130 
03131    check_nomsg(pdata=cpl_image_get_data(img));
03132    for(i=0;i<n;i++) {
03133       if(!isnan(pdata[i])) {
03134          cpl_table_set_double(data_tbl,"DATA",r,pdata[i]);
03135          r++;
03136       }
03137    }
03138 
03139    check_nomsg(cpl_table_erase_invalid(data_tbl));
03140    check_nomsg(avg_d=cpl_table_get_column_mean(data_tbl,"DATA"));
03141    check_nomsg(std_d=cpl_table_get_column_stdev(data_tbl,"DATA"));
03142 
03143    cpl_table_save(data_tbl, NULL, NULL, "out_data.fits", CPL_IO_DEFAULT);
03144 
03145    hmin=avg_d-kappa*std_d;
03146    hmax=avg_d+kappa*std_d;
03147    //sinfo_msg("mean=%g stdv=%g",avg_d,std_d);
03148    //sinfo_msg("hmin=%g hmax=%g",hmin,hmax);
03149    //sinfo_msg("Computes histogram");
03150    ck0(sinfo_histogram(data_tbl,nbins,hmin,hmax,&histo),"building histogram");
03151 
03152    value=(double)(hmax-hmin)/nbins/2.;
03153    //sinfo_msg("value=%10.8f",value);
03154    //cpl_table_save(histo, NULL, NULL, "out_pippo.fits", CPL_IO_DEFAULT0);
03155 
03156    check_nomsg(peak=cpl_table_get_column_max(histo,"HY"));
03157    //sinfo_msg("peak=%f",peak);
03158    sinfo_free_table(&tmp_tbl1);
03159 
03160    check_nomsg(tmp_tbl1=sinfo_extract_table_rows(histo,"HY",CPL_EQUAL_TO,peak));
03161 
03162    //cpl_table_save(tmp_tbl1, NULL, NULL, "out_tmp_tbl1.fits", CPL_IO_DEFAULT);
03163 
03164 
03165    check_nomsg(*centre=cpl_table_get_column_mean(tmp_tbl1,"HL"));
03166    //sinfo_msg("Background level=%f",*centre);
03167 
03168    sinfo_free_table(&tmp_tbl1);
03169    check_nomsg(tmp_tbl1=sinfo_extract_table_rows(histo,"HY",
03170                                                  CPL_GREATER_THAN,
03171                                                  peak/HISTO_Y_CUT));
03172    sinfo_free_table(&tmp_tbl2);
03173    check_nomsg(tmp_tbl2=sinfo_extract_table_rows(tmp_tbl1,"HY",
03174                                                  CPL_LESS_THAN,peak));
03175    sinfo_free_table(&tmp_tbl1);
03176 
03177    check_nomsg(tempc=*centre-cpl_table_get_column_min(tmp_tbl2,"HL"));
03178    //sinfo_msg("min HX %f",cpl_table_get_column_min(tmp_tbl2,"HL"));
03179    sinfo_free_table(&tmp_tbl2);
03180    //sinfo_msg("Tempc=%f",tempc);
03181    check_nomsg(dist=sinfo_where_tab_min_max(histo,"HL",
03182                                             CPL_GREATER_THAN,
03183                                             *centre-min_fct*tempc,
03184                                             CPL_NOT_GREATER_THAN,
03185                                             *centre+max_fct*tempc));
03186 
03187    offset=cpl_table_get_column_min(histo,"HY");
03188    sinfo_free_table(&histo);
03189 
03190 
03191    check_nomsg(ndist=cpl_table_get_nrow(dist));
03192    check_nomsg(cpl_table_cast_column(dist,"HY","HYdouble",CPL_TYPE_DOUBLE));
03193    check_nomsg(disth=cpl_table_get_data_double(dist,"HYdouble"));
03194    check_nomsg(distx=cpl_table_get_data_double(dist,"HL"));
03195    cpl_table_save(dist, NULL, NULL, "out_dist.fits", CPL_IO_DEFAULT);
03196 
03197    //TODO
03198    //gaussfit(distx,disty,dista,nterms=3);
03199    //*noise=dista[2];
03200    *noise=tempc/2;
03201    /* THIS DOES NOT WORK */
03202    //sinfo_msg("FWHM/2=%f",*noise);
03203 
03204    if(noise_fit == 1) {
03205       check_nomsg(vy=cpl_vector_wrap(ndist,disth));
03206       check_nomsg(vx=cpl_vector_wrap(ndist,distx));
03207       check_nomsg(sx=cpl_vector_new(ndist));
03208       check_nomsg(cpl_vector_fill(sx,1.));
03209       check_nomsg(sy=cpl_vector_duplicate(sx));
03210       x0=*centre;
03211       sigma=tempc/2;
03212 
03213       if(CPL_ERROR_NONE != cpl_vector_fit_gaussian(vx,NULL,
03214                                                    vy,NULL,
03215                                                    CPL_FIT_ALL,
03216                                                    &x0,&sigma,&area,&offset,
03217                                                    NULL,NULL,NULL)) {
03218          cpl_error_reset();
03219       }
03220       //sinfo_msg("Gauss fit parameters:"
03221       //          "x0=%f sigma=%f area=%f offset=%f mse=%f chired=%f",
03222       //           x0,sigma,area,offset,mse,chired);
03223       //sinfo_msg("Background level=%f",*centre);
03224       //sinfo_msg("Noise=%f",sigma);
03225       *noise=sigma;
03226       sinfo_unwrap_vector(&vx);
03227       sinfo_unwrap_vector(&vy);
03228       sinfo_free_my_vector(&sx);
03229       sinfo_free_my_vector(&sy);
03230    }
03231    sinfo_free_table(&dist);
03232 
03233    return 0;
03234 
03235   cleanup:
03236    sinfo_free_table(&min_xi);
03237    sinfo_free_table(&tmp_tbl1);
03238    sinfo_free_table(&tmp_tbl2);
03239    sinfo_free_table(&histo);
03240    sinfo_free_table(&dist);
03241    sinfo_free_table(&data_tbl);
03242    sinfo_free_my_vector(&sx);
03243    sinfo_free_my_vector(&sy);
03244    sinfo_unwrap_vector(&vx);
03245    sinfo_unwrap_vector(&vy);
03246 
03247    return -1;
03248 
03249 }
03250 
03251 
03252 
03253 
03254 
03255 cpl_table* sinfo_compute_linearity(cpl_frameset* son, cpl_frameset* sof)
03256 {
03257 
03258    cpl_frame*    frm=NULL;
03259 
03260    int* status=0;
03261    int non=0;
03262    int nof=0;
03263    int nfr=0;
03264    int i=0;
03265    double med_on=0;
03266    double avg_on=0;
03267    double med_of=0;
03268    double avg_of=0;
03269    double med_dit=0;
03270    double avg_dit=0;
03271 
03272    double med=0;
03273    double avg=0;
03274 
03275    char* name=NULL;
03276    cpl_image* img=NULL;
03277    cpl_vector* vec_adl=NULL;
03278    cpl_vector* vec_dit=NULL;
03279    cpl_vector* vec_avg=NULL;
03280    cpl_vector* vec_med=NULL;
03281    cpl_vector* vec_avg_dit=NULL;
03282    cpl_vector* vec_med_dit=NULL;
03283    cpl_propertylist* plist=NULL;
03284 
03285    double dit=0;
03286    cpl_table* lin_tbl=NULL;
03287 
03288 
03289    non = cpl_frameset_get_size(son);
03290    nof = cpl_frameset_get_size(sof);
03291    nfr = (non <= nof) ? non : nof;
03292 
03293    lin_tbl=cpl_table_new(nfr);
03294    cpl_table_new_column(lin_tbl,"med", CPL_TYPE_DOUBLE);
03295    cpl_table_new_column(lin_tbl,"avg", CPL_TYPE_DOUBLE);
03296    cpl_table_new_column(lin_tbl,"med_dit", CPL_TYPE_DOUBLE);
03297    cpl_table_new_column(lin_tbl,"avg_dit", CPL_TYPE_DOUBLE);
03298    cpl_table_new_column(lin_tbl,"dit", CPL_TYPE_DOUBLE);
03299    vec_med=cpl_vector_new(nfr);
03300    vec_avg=cpl_vector_new(nfr);
03301    vec_med_dit=cpl_vector_new(nfr);
03302    vec_avg_dit=cpl_vector_new(nfr);
03303    vec_dit=cpl_vector_new(nfr);
03304    vec_adl=cpl_vector_new(nfr);
03305 
03306    for(i=0;i<nfr;i++) {
03307       frm=cpl_frameset_get_frame(son,i);
03308       name=(char*)cpl_frame_get_filename(frm);
03309       img=cpl_image_load(name,CPL_TYPE_FLOAT,0,0);
03310       med_on=cpl_image_get_median(img);
03311       avg_on=cpl_image_get_mean(img);
03312       cpl_image_delete(img);
03313 
03314       frm=cpl_frameset_get_frame(sof,i);
03315       name=(char*)cpl_frame_get_filename(frm);
03316       img=cpl_image_load(name,CPL_TYPE_FLOAT,0,0);
03317       med_of=cpl_image_get_median(img);
03318       avg_of=cpl_image_get_mean(img);
03319       cpl_image_delete(img);
03320 
03321       med=med_on-med_of;
03322       avg=avg_on-avg_of;
03323       plist=cpl_propertylist_load(name,0);
03324       dit=(double)sinfo_pfits_get_dit(plist);
03325       cpl_propertylist_delete(plist);
03326       avg_dit=avg/dit;
03327       med_dit=med/dit;
03328 
03329       cpl_vector_set(vec_dit,i,dit);
03330       cpl_vector_set(vec_avg,i,avg);
03331       cpl_vector_set(vec_med,i,med);
03332       cpl_vector_set(vec_avg_dit,i,avg_dit);
03333       cpl_vector_set(vec_med_dit,i,med_dit);
03334 
03335       cpl_table_set_double(lin_tbl,"dit",i,dit);
03336       cpl_table_set_double(lin_tbl,"med",i,med);
03337       cpl_table_set_double(lin_tbl,"avg",i,avg);
03338       cpl_table_set_double(lin_tbl,"med_dit",i,med_dit);
03339       cpl_table_set_double(lin_tbl,"avg_dit",i,avg_dit);
03340 
03341    }
03342    cpl_table_new_column(lin_tbl,"adl", CPL_TYPE_DOUBLE);
03343    med_dit=cpl_vector_get_mean(vec_med_dit);
03344    avg_dit=cpl_vector_get_mean(vec_avg_dit);
03345 
03346    for(i=0;i<nfr;i++) {
03347       dit = cpl_table_get_double(lin_tbl,"dit",i,status);
03348       cpl_vector_set(vec_adl,i,dit*med_dit);
03349       cpl_table_set_double(lin_tbl,"adl",i,dit*med_dit);
03350    }
03351 
03352    cpl_vector_delete(vec_dit);
03353    cpl_vector_delete(vec_adl);
03354    cpl_vector_delete(vec_avg);
03355    cpl_vector_delete(vec_med);
03356    cpl_vector_delete(vec_avg_dit);
03357    cpl_vector_delete(vec_med_dit);
03358 
03359 
03360    return lin_tbl;
03361 
03362 }
03363 
03364 /*--------------------------------------------------------------------*/
03371 /*--------------------------------------------------------------------*/
03372 int
03373 sinfo_get_ron(cpl_frameset    *   framelist,
03374               const int ron_xmin,
03375               const int ron_xmax,
03376               const int ron_ymin,
03377               const int ron_ymax,
03378               const int ron_hsize,
03379               const int ron_nsamp,
03380               double** ron)
03381 {
03382    cpl_imagelist   *   iset =NULL;
03383    cpl_image       *   tmp_im =NULL;
03384    cpl_size                 zone[4] ;
03385    double              rms  =0;
03386    double              ndit =0;
03387    cpl_frame       *   frame =NULL;
03388    int                 i;
03389    cpl_propertylist* plist=NULL;
03390 
03391    /* Test entries */
03392 
03393    if (framelist == NULL) return -1 ;
03394 
03395    /* Load the current set */
03396    if ((iset = sinfo_new_frameset_to_iset(framelist)) == NULL) {
03397       sinfo_msg_error( "Cannot load the data") ;
03398       return -1 ;
03399    }
03400 
03401    /* Initialise */
03402    zone[0]=ron_xmin;
03403    zone[1]=ron_xmax;
03404    zone[2]=ron_ymin;
03405    zone[3]=ron_ymax;
03406 
03407    /* Loop on all pairs */
03408    for (i=0 ; i<cpl_imagelist_get_size(iset)-1 ; i++) {
03409 
03410       /* Compute the current subtracted image */
03411       if ((tmp_im=cpl_image_subtract_create(cpl_imagelist_get(iset,i),
03412                                             cpl_imagelist_get(iset, i+1)))
03413           == NULL) {
03414          sinfo_msg_error( "Cannot subtract the images") ;
03415          sinfo_free_imagelist(&iset) ;
03416          return -1 ;
03417       }
03418 
03419       /* Compute the read-out noise */
03420       if (cpl_flux_get_noise_window(tmp_im, zone, ron_hsize,
03421                                     ron_nsamp, &rms, NULL) != CPL_ERROR_NONE) {
03422          sinfo_msg_error( "Cannot compute the RON") ;
03423          sinfo_free_image(&tmp_im) ;
03424          sinfo_free_imagelist(&iset) ;
03425          return -1 ;
03426       }
03427       sinfo_free_image(&tmp_im) ;
03428       /* Normalise the RON with NDIT */
03429       frame = cpl_frameset_get_frame(framelist, i) ;
03430       cknull_nomsg(plist=cpl_propertylist_load(cpl_frame_get_filename(frame),
03431                                                0));
03432       ndit=sinfo_pfits_get_ndit(plist);
03433       sinfo_free_propertylist(&plist);
03434 
03435       (*ron)[i] = rms * sqrt(ndit/2.0) ;
03436 
03437    }
03438 
03439    /* Free and return */
03440    sinfo_free_imagelist(&iset) ;
03441    return 0 ;
03442 
03443   cleanup:
03444    sinfo_free_image(&tmp_im);
03445    sinfo_free_imagelist(&iset);
03446    sinfo_free_propertylist(&plist);
03447    return -1;
03448 
03449 }
03450 
03451 
03452 
03453 /*---------------------------------------------------------------------------*/
03459 /*---------------------------------------------------------------------------*/
03460 int sinfo_stack_get_pro_tag(char * tag_in, char* tag_out)
03461 {
03462    /* Test entries */
03463    if (tag_in == NULL) return -1 ;
03464    /* here for the moment we set the same PRO ID as a non stacked frame */
03465    if (strcmp(tag_in,RAW_WAVE_LAMP_DITHER) == 0 ) {
03466       strcpy(tag_out,PRO_WAVE_LAMP_STACKED);
03467       return 0 ;
03468    }
03469 
03470 
03471    if (strcmp(tag_in,RAW_WAVE_LAMP) == 0 ) {
03472       strcpy(tag_out,PRO_WAVE_LAMP_STACKED);
03473       return 0 ;
03474    }
03475 
03476    if (strcmp(tag_in,RAW_WAVE_NS_DITHER) == 0 ) {
03477       strcpy(tag_out,PRO_WAVE_NS_STACKED);
03478       return 0 ;
03479    }
03480 
03481 
03482    if (strcmp(tag_in,RAW_WAVE_NS) == 0 ) {
03483       strcpy(tag_out,PRO_WAVE_NS_STACKED);
03484       return 0 ;
03485    }
03486 
03487 
03488    if (strcmp(tag_in,RAW_FIBRE_LAMP) == 0 ) {
03489       strcpy(tag_out,PRO_FIBRE_LAMP_STACKED);
03490       return 0 ;
03491    }
03492 
03493    if (strcmp(tag_in,RAW_FIBRE_EW) == 0 ) {
03494       strcpy(tag_out,PRO_FIBRE_EW_STACKED);
03495       return 0 ;
03496    }
03497 
03498    if (strcmp(tag_in,RAW_FIBRE_NS) == 0 ) {
03499       strcpy(tag_out,PRO_FIBRE_NS_STACKED);
03500       return 0 ;
03501    }
03502 
03503 
03504    if (strcmp(tag_in,PRO_FIBRE_NS_STACKED_ON) == 0 ) {
03505       strcpy(tag_out,PRO_FIBRE_NS_STACKED);
03506       return 0 ;
03507    }
03508 
03509    if (strcmp(tag_in,PRO_FIBRE_NS_STACKED) == 0 ) {
03510       strcpy(tag_out,PRO_FIBRE_NS_STACKED_DIST);
03511       return 0 ;
03512    }
03513 
03514 
03515    if (strcmp(tag_in,RAW_SLIT_LAMP) == 0 ) {
03516       strcpy(tag_out,PRO_SLIT_LAMP_STACKED);
03517       return 0 ;
03518    }
03519 
03520 
03521    if (strstr(tag_in, "FLUX") != NULL ) {
03522       strcpy(tag_out,PRO_FLUX_LAMP_STACKED);
03523       return 0 ;
03524    }
03525 
03526    if (strstr(tag_in, "PSF") != NULL ) {
03527       strcpy(tag_out,PRO_PSF_CALIBRATOR_STACKED);
03528       return 0 ;
03529    }
03530 
03531 
03532    if (strstr(tag_in, "FOCUS") != NULL ) {
03533       strcpy(tag_out,PRO_FOCUS_STACKED);
03534       return 0 ;
03535    }
03536 
03537    if (strstr(tag_in, "OBJECT_NODDING") != NULL ) {
03538       strcpy(tag_out,PRO_OBJECT_NODDING_STACKED);
03539       return 0 ;
03540    }
03541 
03542    if (strstr(tag_in, "SKY_NODDING") != NULL ) {
03543       strcpy(tag_out,PRO_SKY_NODDING_STACKED);
03544       return 0 ;
03545    }
03546 
03547    if (strstr(tag_in, "STD_NODDING") != NULL ) {
03548       strcpy(tag_out,PRO_STD_NODDING_STACKED);
03549       return 0 ;
03550    }
03551 
03552    if (strstr(tag_in, "OBJECT_SKYSPIDER") != NULL ) {
03553       strcpy(tag_out,PRO_OBJECT_SKYSPIDER_STACKED);
03554       return 0 ;
03555    }
03556 
03557 
03558    if (strstr(tag_in, RAW_STD) != NULL ) {
03559       strcpy(tag_out,PRO_STD_STACKED);
03560       return 0 ;
03561    }
03562 
03563 
03564    if (strstr(tag_in, RAW_SKY_STD) != NULL ) {
03565       strcpy(tag_out,PRO_SKY_STD_STACKED);
03566       return 0 ;
03567    }
03568 
03569    if (strstr(tag_in, RAW_SKY_OH) != NULL ) {
03570       strcpy(tag_out,PRO_SKY_OH_STACKED);
03571       return 0 ;
03572    }
03573 
03574    if (strstr(tag_in, RAW_SKY_PSF_CALIBRATOR) != NULL ) {
03575       strcpy(tag_out,PRO_SKY_PSF_CALIBRATOR_STACKED);
03576       return 0 ;
03577    }
03578 
03579    if (strstr(tag_in, RAW_STD_STAR) != NULL ) {
03580       strcpy(tag_out,PRO_STD_STAR_STACKED);
03581       return 0 ;
03582    }
03583 
03584    if (strstr(tag_in, RAW_STD_STAR) != NULL ) {
03585       strcpy(tag_out,PRO_STD_STAR_DITHER_STACKED);
03586       return 0 ;
03587    }
03588 
03589    if (strstr(tag_in, RAW_SKY) != NULL ) {
03590       strcpy(tag_out,PRO_SKY_STACKED);
03591       return 0 ;
03592    }
03593 
03594 
03595    return 1 ;
03596 }
03597 
03598 
03599 int sinfo_is_dark(char * tag)
03600 {
03601    /* Test entries */
03602    if (tag == NULL) return -1 ;
03603 
03604    if (!strcmp(tag, RAW_DARK)) return 1 ;
03605    if (!strcmp(tag, PRO_MASTER_DARK)) return 1 ;
03606    return 0 ;
03607 }
03608 
03609 int sinfo_is_flat_bp(char * tag)
03610 {
03611    /* Test entries */
03612    if (tag == NULL) return -1 ;
03613 
03614    if (!strcmp(tag, RAW_LINEARITY_LAMP)) return 1 ;
03615    return 0 ;
03616 }
03617 
03618 int sinfo_is_flat_lindet(char * tag)
03619 {
03620    /* Test entries */
03621    if (tag == NULL) return -1 ;
03622 
03623    if (!strcmp(tag, RAW_LINEARITY_LAMP)) return 1 ;
03624    return 0 ;
03625 }
03626 
03627 
03628 int sinfo_blank2dot(const char * in, char* ou)
03629 {
03630    int len=0;
03631    int i=0;
03632 
03633    strcpy(ou,in);
03634    len = strlen(in);
03635    for (i=0;i<len;i++)
03636    {
03637       if (in[i] == ' ') {
03638          ou[i] =  '.';
03639       }
03640    }
03641    return 0;
03642 }
03643 
03644 
03645 int sinfo_is_sky_flat(char * tag)
03646 {
03647    /* Test entries */
03648    if (tag == NULL) return -1 ;
03649    if (!strcmp(tag, RAW_FLAT_SKY)) return 1 ;
03650    return 0 ;
03651 }
03652 
03653 
03654 
03655 int sinfo_is_master_flat(char * tag)
03656 {
03657    /* Test entries */
03658    if (tag == NULL) return -1 ;
03659 
03660    if (!strcmp(tag, PRO_MASTER_FLAT_LAMP)) return 1 ;
03661    if (!strcmp(tag, PRO_MASTER_FLAT_LAMP1)) return 1 ;
03662    return 0 ;
03663 }
03664 
03665 int sinfo_is_master_flat_dither(char * tag)
03666 {
03667    /* Test entries */
03668    if (tag == NULL) return -1 ;
03669 
03670    if (!strcmp(tag, PRO_MASTER_FLAT_LAMP2)) return 1 ;
03671    return 0 ;
03672 }
03673 
03674 /*---------------------------------------------------------------------------*/
03680 /*---------------------------------------------------------------------------*/
03681 int sinfo_is_stack(char * tag)
03682 {
03683    /* Test entries */
03684    if (tag == NULL) return -1 ;
03685 
03686    if (strstr(tag, PRO_STACKED) != NULL) return 1 ;
03687    return 0 ;
03688 }
03689 
03690 int sinfo_is_mflat(char * tag)
03691 {
03692    /* Test entries */
03693    if (tag == NULL) return -1 ;
03694 
03695    if (!strcmp(tag, PRO_MASTER_FLAT_LAMP)) return 1 ;
03696    if (!strcmp(tag, PRO_MASTER_FLAT_LAMP1)) return 1 ;
03697    if (!strcmp(tag, PRO_MASTER_FLAT_LAMP2)) return 1 ;
03698    return 0 ;
03699 }
03700 
03701 
03702 /*---------------------------------------------------------------------------*/
03708 /*---------------------------------------------------------------------------*/
03709 int sinfo_is_psf_calibrator_stacked(char * tag)
03710 {
03711    /* Test entries */
03712    if (tag == NULL) return -1 ;
03713 
03714    if (!strcmp(tag, PRO_PSF_CALIBRATOR_STACKED)) return 1 ;
03715    return 0 ;
03716 }
03717 /*---------------------------------------------------------------------------*/
03723 /*---------------------------------------------------------------------------*/
03724 int sinfo_is_focus_stacked(char * tag)
03725 {
03726    /* Test entries */
03727    if (tag == NULL) return -1 ;
03728 
03729    if (!strcmp(tag, PRO_FOCUS_STACKED)) return 1 ;
03730    return 0 ;
03731 }
03732 
03733 /*---------------------------------------------------------------------------*/
03739 /*---------------------------------------------------------------------------*/
03740 int sinfo_is_lamp_wave_stacked(char * tag)
03741 {
03742    /* Test entries */
03743    if (tag == NULL) return -1 ;
03744 
03745    if (!strcmp(tag, PRO_WAVE_LAMP_STACKED)) return 1 ;
03746    return 0 ;
03747 }
03748 
03749 /*---------------------------------------------------------------------------*/
03755 /*---------------------------------------------------------------------------*/
03756 int sinfo_is_lamp_flux_stacked(char * tag)
03757 {
03758    /* Test entries */
03759    if (tag == NULL) return -1 ;
03760 
03761    if (!strcmp(tag, PRO_FLUX_LAMP_STACKED)) return 1 ;
03762    return 0 ;
03763 }
03764 
03765 /*---------------------------------------------------------------------------*/
03771 /*---------------------------------------------------------------------------*/
03772 int sinfo_is_object_nodding_stacked(char * tag)
03773 {
03774    /* Test entries */
03775    if (tag == NULL) return -1 ;
03776 
03777    if (!strcmp(tag, PRO_OBJECT_NODDING_STACKED)) return 1 ;
03778    return 0 ;
03779 }
03780 
03781 /*---------------------------------------------------------------------------*/
03787 /*---------------------------------------------------------------------------*/
03788 int sinfo_is_object_skyspider_stacked(char * tag)
03789 {
03790    /* Test entries */
03791    if (tag == NULL) return -1 ;
03792 
03793    if (!strcmp(tag, PRO_OBJECT_SKYSPIDER_STACKED)) return 1 ;
03794    return 0 ;
03795 }
03796 
03797 
03798 /*---------------------------------------------------------------------------*/
03804 /*---------------------------------------------------------------------------*/
03805 int sinfo_is_sky_nodding_stacked(char * tag)
03806 {
03807    /* Test entries */
03808    if (tag == NULL) return -1 ;
03809 
03810    if (!strcmp(tag, PRO_SKY_NODDING_STACKED)) return 1 ;
03811    return 0 ;
03812 }
03813 
03814 /*---------------------------------------------------------------------------*/
03820 /*---------------------------------------------------------------------------*/
03821 int sinfo_is_wavemap(char * tag)
03822 {
03823    /* Test entries */
03824    if (tag == NULL) return -1 ;
03825 
03826    if (!strcmp(tag, PRO_WAVE_MAP)) return 1 ;
03827    return 0 ;
03828 }
03829 
03830 /*---------------------------------------------------------------------------*/
03836 /*---------------------------------------------------------------------------*/
03837 int sinfo_is_halosp(char * tag)
03838 {
03839    /* Test entries */
03840    if (tag == NULL) return -1 ;
03841 
03842    if (!strcmp(tag, PRO_HALO_SPECT)) return 1 ;
03843    return 0 ;
03844 }
03845 
03846 /*---------------------------------------------------------------------------*/
03852 /*---------------------------------------------------------------------------*/
03853 int sinfo_is_distlist(char * tag)
03854 {
03855    /* Test entries */
03856    if (tag == NULL) return -1 ;
03857 
03858    if (!strcmp(tag, PRO_SLITLETS_DISTANCE)) return 1 ;
03859    return 0 ;
03860 }
03861 
03862 /*---------------------------------------------------------------------------*/
03868 /*---------------------------------------------------------------------------*/
03869 int sinfo_is_slitpos(char * tag)
03870 {
03871    /* Test entries */
03872    if (tag == NULL) return -1 ;
03873 
03874    if (!strcmp(tag, PRO_SLIT_POS)) return 1 ;
03875    return 0 ;
03876 }
03877 
03878 /*---------------------------------------------------------------------------*/
03884 /*---------------------------------------------------------------------------*/
03885 int sinfo_is_firstcol(char * tag)
03886 {
03887    /* Test entries */
03888    if (tag == NULL) return -1 ;
03889 
03890    if (!strcmp(tag, PRO_FIRST_COL)) return 1 ;
03891    return 0 ;
03892 }
03893 
03894 /*---------------------------------------------------------------------------*/
03900 /*---------------------------------------------------------------------------*/
03901 int sinfo_is_bpmap(char * tag)
03902 {
03903    /* Test entries */
03904    if (tag == NULL) return -1 ;
03905 
03906    if (!strcmp(tag, PRO_BP_MAP)) return 1 ;
03907    return 0 ;
03908 }
03909 
03910 
03911 /*---------------------------------------------------------------------------*/
03920 /*---------------------------------------------------------------------------*/
03921 
03922 int sinfo_get_band(cpl_frame * ref_frame,char * band)
03923 {
03924 
03925    char* ref_file=NULL;
03926    cpl_propertylist* plist=NULL;
03927 
03928    ref_file = cpl_strdup(cpl_frame_get_filename(ref_frame)) ;
03929    if ((cpl_error_code)((plist = cpl_propertylist_load(ref_file, 0)) == NULL)) {
03930       sinfo_msg_error( "getting header from reference frame %s",ref_file);
03931       cpl_propertylist_delete(plist) ;
03932       return -1 ;
03933    }
03934 
03935    if (sinfo_propertylist_has(plist, KEY_NAME_FILT_NAME)) {
03936       strcpy(band, cpl_propertylist_get_string(plist, KEY_NAME_FILT_NAME));
03937       /* sinfo_msg("%s value is %s", KEY_NAME_FILT_NAME, band); */
03938 
03939    } else {
03940       sinfo_msg_warning("keyword %s does not exist",KEY_NAME_FILT_NAME);
03941       return -1;
03942    }
03943 
03944    cpl_free(ref_file);
03945    cpl_propertylist_delete(plist);
03946    return 0;
03947 }
03948 
03949 
03950 
03951 
03952 /*---------------------------------------------------------------------------*/
03960 /*---------------------------------------------------------------------------*/
03961 
03962 int sinfo_get_obsname(cpl_frame * ref_frame, const char* obs_name)
03963 {
03964 
03965    char* ref_file=NULL;
03966    cpl_propertylist* plist=NULL;
03967 
03968    ref_file = cpl_strdup(cpl_frame_get_filename(ref_frame)) ;
03969    if ((cpl_error_code)((plist = cpl_propertylist_load(ref_file, 0)) == NULL)) {
03970       sinfo_msg_error( "getting header from reference frame %s",ref_file);
03971       cpl_propertylist_delete(plist) ;
03972       return -1 ;
03973    }
03974 
03975    if (sinfo_propertylist_has(plist, KEY_NAME_OBS_NAME)) {
03976       strcpy((char*)obs_name, cpl_propertylist_get_string(plist,
03977                                                           KEY_NAME_OBS_NAME));
03978       /* sinfo_msg("%s value is %s", KEY_NAME_OBS_NAME, obs_name); */
03979 
03980    } else {
03981       sinfo_msg_warning("keyword %s does not exist",KEY_NAME_OBS_NAME);
03982       return -1;
03983    }
03984 
03985    cpl_free(ref_file);
03986    cpl_propertylist_delete(plist);
03987    return 0;
03988 }
03989 
03990 
03991 
03992 
03993 
03994 /*---------------------------------------------------------------------------*/
04002 /*---------------------------------------------------------------------------*/
04003 
04004 int sinfo_get_keyvalue_int(cpl_frame * ref_frame, const char* key_name)
04005 {
04006 
04007    char* ref_file=NULL;
04008    cpl_propertylist* plist=NULL;
04009    int result=0;
04010 
04011    ref_file = cpl_strdup(cpl_frame_get_filename(ref_frame)) ;
04012 
04013    if ((cpl_error_code)((plist = cpl_propertylist_load(ref_file, 0)) == NULL)) {
04014       sinfo_msg_error( "getting header from reference frame %s",ref_file);
04015       cpl_propertylist_delete(plist) ;
04016       return -1;
04017    }
04018 
04019 
04020    if (sinfo_propertylist_has(plist, key_name)) {
04021       result=cpl_propertylist_get_int(plist,key_name);
04022    } else {
04023       sinfo_msg_warning("keyword %s does not exist",key_name);
04024       return -1;
04025    }
04026 
04027    cpl_free(ref_file);
04028    cpl_propertylist_delete(plist);
04029 
04030    return result;
04031 }
04032 
04033 
04034 
04035 /*---------------------------------------------------------------------------*/
04043 /*---------------------------------------------------------------------------*/
04044 
04045 float sinfo_get_keyvalue_float(cpl_frame * ref_frame, const char* key_name)
04046 {
04047 
04048    char* ref_file=NULL;
04049    cpl_propertylist* plist=NULL;
04050    float result=0;
04051 
04052    ref_file = cpl_strdup(cpl_frame_get_filename(ref_frame)) ;
04053 
04054    if ((cpl_error_code)((plist = cpl_propertylist_load(ref_file, 0)) == NULL)) {
04055       sinfo_msg_error( "getting header from reference frame %s",ref_file);
04056       cpl_propertylist_delete(plist) ;
04057       return -1;
04058    }
04059 
04060 
04061    if (sinfo_propertylist_has(plist, key_name)) {
04062       result=cpl_propertylist_get_float(plist,key_name);
04063    } else {
04064       sinfo_msg_warning("keyword %s does not exist",key_name);
04065       return -1;
04066    }
04067 
04068    cpl_free(ref_file);
04069    cpl_propertylist_delete(plist);
04070 
04071    return result;
04072 }
04073 
04074 
04075 /*---------------------------------------------------------------------------*/
04083 /*---------------------------------------------------------------------------*/
04084 
04085 char sinfo_get_keyvalue_bool(cpl_frame * ref_frame, const char* key_name)
04086 {
04087 
04088    char* ref_file=NULL;
04089    cpl_propertylist* plist=NULL;
04090    int res_val=0;
04091 
04092    ref_file = cpl_strdup(cpl_frame_get_filename(ref_frame)) ;
04093 
04094    if ((cpl_error_code)((plist = cpl_propertylist_load(ref_file, 0)) == NULL)) {
04095       sinfo_msg_error( "getting header from reference frame %s",ref_file);
04096       cpl_propertylist_delete(plist) ;
04097       return '0';
04098    }
04099 
04100 
04101    if (sinfo_propertylist_has(plist, key_name)) {
04102       res_val=cpl_propertylist_get_bool(plist,key_name);
04103    } else {
04104       sinfo_msg_warning("keyword %s does not exist",key_name);
04105       return '0';
04106    }
04107 
04108    cpl_free(ref_file);
04109    cpl_propertylist_delete(plist);
04110    if(res_val == 1) {
04111       return 'T';
04112    } else {
04113       return 'F';
04114    }
04115 }
04116 
04117 
04118 
04119 
04120 /*---------------------------------------------------------------------------*/
04128 /*---------------------------------------------------------------------------*/
04129 
04130 const char*
04131 sinfo_get_keyvalue_string(cpl_frame * ref_frame, const char* key_name)
04132 {
04133 
04134    char* ref_file=NULL;
04135    cpl_propertylist* plist=NULL;
04136    const char* result=NULL;
04137 
04138    ref_file = cpl_strdup(cpl_frame_get_filename(ref_frame)) ;
04139 
04140    if ((cpl_error_code)((plist = cpl_propertylist_load(ref_file, 0)) == NULL)) {
04141       sinfo_msg_error( "getting header from reference frame %s",ref_file);
04142       cpl_propertylist_delete(plist) ;
04143       return FALSE;
04144    }
04145 
04146 
04147    if (sinfo_propertylist_has(plist, key_name)) {
04148       result=cpl_propertylist_get_string(plist,key_name);
04149    } else {
04150       sinfo_msg_warning("keyword %s does not exist",key_name);
04151       return NULL;
04152    }
04153 
04154    cpl_free(ref_file);
04155    cpl_propertylist_delete(plist);
04156 
04157    return result;
04158 }
04159 
04160 
04161 
04162 double sinfo_get_mjd_obs(cpl_frame * frame)
04163 {
04164    cpl_propertylist* plist=NULL;
04165    const char* file=NULL;
04166 
04167    double mjd_obs=0.;
04168    file = cpl_frame_get_filename(frame) ;
04169 
04170    if ((cpl_error_code)((plist = cpl_propertylist_load(file, 0)) == NULL)) {
04171       sinfo_msg_error( "getting header from reference frame %s",file);
04172       sinfo_free_propertylist(&plist) ;
04173       return -1 ;
04174    }
04175 
04176    if (sinfo_propertylist_has(plist, KEY_NAME_MJD_OBS)) {
04177       mjd_obs=cpl_propertylist_get_double(plist, KEY_NAME_MJD_OBS);
04178    } else {
04179       sinfo_msg_error("keyword %s does not exist",KEY_NAME_MJD_OBS);
04180       sinfo_free_propertylist(&plist) ;
04181       return -1;
04182    }
04183    sinfo_free_propertylist(&plist) ;
04184 
04185    return mjd_obs;
04186 
04187 }
04188 
04189 
04190 
04191 
04192 double sinfo_get_cumoffsetx(cpl_frame * frame)
04193 {
04194    cpl_propertylist* plist=NULL;
04195    char* file=NULL;
04196 
04197    double result=0.;
04198    file = cpl_strdup( cpl_frame_get_filename(frame)) ;
04199 
04200    if ((cpl_error_code)((plist = cpl_propertylist_load(file, 0)) == NULL)) {
04201       sinfo_msg_error( "getting header from reference frame %s",file);
04202       cpl_propertylist_delete(plist) ;
04203       cpl_free(file);
04204       return -1 ;
04205    }
04206 
04207    if (sinfo_propertylist_has(plist, KEY_NAME_CUMOFFX)) {
04208       result=cpl_propertylist_get_double(plist, KEY_NAME_CUMOFFX);
04209    } else {
04210       sinfo_msg_error("keyword %s does not exist",KEY_NAME_CUMOFFX);
04211       cpl_propertylist_delete(plist) ;
04212       return -1;
04213    }
04214    cpl_propertylist_delete(plist) ;
04215    cpl_free(file);
04216 
04217    return result;
04218 
04219 }
04220 
04221 
04222 
04223 
04224 double sinfo_get_cumoffsety(cpl_frame * frame)
04225 {
04226    cpl_propertylist* plist=NULL;
04227    char* file=NULL;
04228 
04229    double result=0.;
04230    file = cpl_strdup( cpl_frame_get_filename(frame)) ;
04231 
04232    if ((cpl_error_code)((plist = cpl_propertylist_load(file, 0)) == NULL)) {
04233       sinfo_msg_error( "getting header from reference frame %s",file);
04234       cpl_propertylist_delete(plist) ;
04235       cpl_free(file);
04236       return -1 ;
04237    }
04238 
04239    if (sinfo_propertylist_has(plist, KEY_NAME_CUMOFFY)) {
04240       result=cpl_propertylist_get_double(plist, KEY_NAME_CUMOFFY);
04241    } else {
04242       sinfo_msg_error("keyword %s does not exist",KEY_NAME_CUMOFFY);
04243       cpl_propertylist_delete(plist) ;
04244       return -1;
04245    }
04246    cpl_propertylist_delete(plist) ;
04247    cpl_free(file);
04248 
04249    return result;
04250 
04251 }
04252 
04253 int sinfo_frame_is_dither(cpl_frame * frame)
04254 {
04255 
04256    char file[256];
04257    char band[FILE_NAME_SZ];
04258 
04259 
04260    cpl_propertylist* plist=NULL;
04261    int grat_encoder=0;
04262    int dith_status=1;
04263    int len=0;
04264 
04265 
04266    cknull(frame,"Null input frame. Exit!");
04267 
04268    cknull_nomsg(strcpy(file,cpl_frame_get_filename(frame)));
04269    len= strlen(file);
04270 
04271    if(len<1) goto cleanup;
04272    if(sinfo_file_exists(file)==0) goto cleanup;
04273    //file = cpl_strdup(cpl_frame_get_filename(frame)) ;
04274    cknull(plist = cpl_propertylist_load(file, 0),
04275       "getting header from reference frame %s",file);
04276 
04277    if (sinfo_propertylist_has(plist, KEY_NAME_FILT_NAME)) {
04278       strcpy(band,cpl_propertylist_get_string(plist, KEY_NAME_FILT_NAME));
04279    } else {
04280       sinfo_msg_error("keyword %s does not exist",KEY_NAME_FILT_NAME);
04281       sinfo_free_propertylist(&plist) ;
04282       return -1;
04283    }
04284 
04285    if (sinfo_propertylist_has(plist, KEY_NAME_GRAT_ENC)) {
04286       grat_encoder = cpl_propertylist_get_int(plist, KEY_NAME_GRAT_ENC);
04287    } else {
04288       sinfo_msg_error("keyword %s does not exist",KEY_NAME_GRAT_ENC);
04289       sinfo_free_propertylist(&plist) ;
04290       return -1;
04291    }
04292 
04293    sinfo_free_propertylist(&plist) ;
04294 
04295    if (strcmp(band,"H") == 0) {
04296       if( abs(grat_encoder - GRAT_VAL2_H) <= GRAT_VAL_TOL ) {
04297          dith_status = 0;
04298       } else {
04299          dith_status = 0;
04300       }
04301    }
04302    else if (strcmp(band,"H+K") == 0) {
04303       if( abs(grat_encoder - GRAT_VAL2_HK) <= GRAT_VAL_TOL ) {
04304          dith_status = 0;
04305       } else {
04306          dith_status = 0;
04307       }
04308    }
04309    else if (strcmp(band,"K") == 0) {
04310       if( abs(grat_encoder - GRAT_VAL2_K) <= GRAT_VAL_TOL ) {
04311          dith_status = 0;
04312       } else {
04313          dith_status = 0;
04314       }
04315    }
04316    else if (strcmp(band,"J") == 0) {
04317       if( abs(grat_encoder - GRAT_VAL2_J) <= GRAT_VAL_TOL ) {
04318          dith_status = 0;
04319       } else {
04320          dith_status = 0;
04321       }
04322    }
04323  cleanup:
04324 
04325    sinfo_free_propertylist(&plist) ;
04326    if(cpl_error_get_code() != CPL_ERROR_NONE) {
04327      return -1;
04328    } else {
04329      return dith_status;
04330    }
04331 }
04332 
04333 /*---------------------------------------------------------------------------*/
04342 /*---------------------------------------------------------------------------*/
04343 
04344 int sinfo_get_spatial_res(cpl_frame * ref_frame, char * spat_res)
04345 {
04346 
04347    const char* ref_file;
04348    cpl_propertylist* plist=NULL;
04349 
04350    ref_file=cpl_frame_get_filename(ref_frame) ;
04351    if ((cpl_error_code)((plist = cpl_propertylist_load(ref_file, 0)) == NULL)) {
04352       sinfo_msg_error( "getting header from reference frame %s",ref_file);
04353       sinfo_free_propertylist(&plist) ;
04354       return -1 ;
04355 
04356    }
04357 
04358    if (sinfo_propertylist_has(plist, KEY_NAME_PREOPTICS)) {
04359       strcpy(spat_res,cpl_propertylist_get_string(plist, KEY_NAME_PREOPTICS));
04360       /* sinfo_msg("%s value is %s", KEY_NAME_PREOPTICS, spat_res); */
04361    } else {
04362       sinfo_msg_warning("keyword %s does not exist",KEY_NAME_PREOPTICS);
04363       sinfo_free_propertylist(&plist);
04364       return -1;
04365    }
04366    sinfo_free_propertylist(&plist);
04367    return 0;
04368 
04369 }
04370 
04371 /*---------------------------------------------------------------------------*/
04379 /*---------------------------------------------------------------------------*/
04380 
04381 int sinfo_frame_is_sky(cpl_frame * ref_frame)
04382 {
04383 
04384    char  dpr_type[FILE_NAME_SZ];
04385    char* ref_file=NULL;
04386    const char* sval=NULL;
04387 
04388    int result=0;
04389    cpl_propertylist* plist=NULL;
04390 
04391    sval = cpl_frame_get_filename(ref_frame) ;
04392    ref_file = cpl_strdup(sval) ;
04393 
04394    if ((cpl_error_code)((plist = cpl_propertylist_load(ref_file, 0)) == NULL)) {
04395       sinfo_msg_error( "getting header from reference frame %s",ref_file);
04396       cpl_propertylist_delete(plist) ;
04397       cpl_free(ref_file);
04398       return -1 ;
04399    }
04400 
04401    if (sinfo_propertylist_has(plist, KEY_NAME_DPR_TYPE)) {
04402       strcpy(dpr_type,cpl_propertylist_get_string(plist, KEY_NAME_DPR_TYPE));
04403       /* sinfo_msg("%s value is %d", KEY_NAME_DPR_TYPE, dpr_type); */
04404    } else {
04405       sinfo_msg_warning("keyword %s does not exist",KEY_NAME_DPR_TYPE);
04406       cpl_propertylist_delete(plist) ;
04407       cpl_free(ref_file);
04408       return -1;
04409    }
04410    cpl_propertylist_delete(plist);
04411    if(strstr(dpr_type,RAW_SKY) != NULL) {
04412       result=1;
04413    }
04414    cpl_free(ref_file);
04415 
04416    return result;
04417 
04418 }
04419 
04420 
04421 /*---------------------------------------------------------------------------*/
04429 /*---------------------------------------------------------------------------*/
04430 
04431 int sinfo_tag_is_sky(char * tag)
04432 {
04433 
04434    int result=0;
04435 
04436    if(
04437       (strcmp(tag,RAW_SKY) == 0)         ||
04438       (strcmp(tag,RAW_IMAGE_PRE_SKY) == 0)         ||
04439       (strcmp(tag,RAW_SKY_NODDING) == 0) ||
04440       (strcmp(tag,RAW_SKY_JITTER) == 0) ||
04441       (strcmp(tag,RAW_SKY_STD) == 0)     ||
04442       (strcmp(tag,RAW_FIBRE_DARK) == 0) ||
04443       (strcmp(tag,RAW_SKY_OH) == 0)      ||
04444       (strcmp(tag,RAW_SKY_PSF_CALIBRATOR) == 0)
04445       ) {
04446       result=1;
04447    }
04448 
04449    return result;
04450 
04451 }
04452 
04453 
04454 /*---------------------------------------------------------------------------*/
04462 /*---------------------------------------------------------------------------*/
04463 
04464 int sinfo_tag_is_obj(char * tag)
04465 {
04466 
04467    int result=0;
04468 
04469    if(
04470       (strcmp(tag,RAW_PUPIL_LAMP) == 0) ||
04471       (strcmp(tag,RAW_OBJECT) == 0)         ||
04472       (strcmp(tag,RAW_IMAGE_PRE_OBJECT) == 0)         ||
04473       (strcmp(tag,RAW_OBJECT_NODDING) == 0) ||
04474       (strcmp(tag,RAW_OBJECT_JITTER) == 0) ||
04475       (strcmp(tag,RAW_PSF_CALIBRATOR) == 0) ||
04476       (strcmp(tag,RAW_FIBRE_PSF) == 0) ||
04477       (strcmp(tag,RAW_STD) == 0)            ||
04478       (strcmp(tag,RAW_STD_STAR) == 0)
04479 
04480       ) {
04481       result=1;
04482    }
04483 
04484    return result;
04485 
04486 }
04487 
04488 /*---------------------------------------------------------------------------*/
04496 /*---------------------------------------------------------------------------*/
04497 
04498 int sinfo_tag_is_objpro(char * tag)
04499 {
04500 
04501    int result=0;
04502 
04503    if(
04504       (strcmp(tag,PRO_COADD_OBJ) == 0) ||
04505       (strcmp(tag,PRO_COADD_PSF) == 0) ||
04506       (strcmp(tag,PRO_COADD_STD) == 0) ||
04507       (strcmp(tag,PRO_OBS_OBJ) == 0) ||
04508       (strcmp(tag,PRO_OBS_PSF) == 0) ||
04509       (strcmp(tag,PRO_OBS_STD) == 0) ||
04510       (strcmp(tag,PRO_PSF_CALIBRATOR_STACKED) == 0) ||
04511       (strcmp(tag,PRO_SKY_PSF_CALIBRATOR_STACKED) == 0) ||
04512       (strcmp(tag,PRO_STD_STACKED) == 0) ||
04513       (strcmp(tag,PRO_SKY_STD_STACKED) == 0) ||
04514       (strcmp(tag,PRO_OBJECT_NODDING_STACKED) == 0) ||
04515       (strcmp(tag,PRO_SKY_NODDING_STACKED) == 0)
04516       ) {
04517       result=1;
04518    }
04519 
04520    return result;
04521 
04522 }
04523 
04524 
04525 /*---------------------------------------------------------------------------*/
04533 /*---------------------------------------------------------------------------*/
04534 
04535 int sinfo_frame_is_on(cpl_frame * ref_frame)
04536 {
04537 
04538    char ref_file[FILE_NAME_SZ];
04539    char dpr_type[FILE_NAME_SZ];
04540    int lamp_Xe=0;
04541    int lamp_Kr=0;
04542    int lamp_Ne=0;
04543    int lamp_Ar=0;
04544    int lamp_Halo=0;
04545    int len=0;
04546    int result=0;
04547    cpl_propertylist* plist=NULL;
04548    const char* filename=NULL;
04549    cknull(ref_frame,"Null input frame. Exit!");
04550 
04551    cknull_nomsg(filename=cpl_frame_get_filename(ref_frame));
04552    len= strlen(filename);
04553    if(len<1) goto cleanup;
04554 
04555    check_nomsg(strcpy(ref_file, filename)) ;
04556    if ((cpl_error_code)((plist = cpl_propertylist_load(ref_file,0)) == NULL)) {
04557       sinfo_msg_error( "getting header from reference frame %s",ref_file);
04558       sinfo_free_propertylist(&plist) ;
04559       return -1 ;
04560    }
04561 
04562 /*-----------------------------------------------------------------------
04563   in J  Argon (4)
04564   in H Xenon and Argon (1+4)
04565   in K Neon (3)
04566   in H+K Xenon (1)
04567   -------------------------------------------------------------------------*/
04568    if (sinfo_propertylist_has(plist, KEY_NAME_DPR_TYPE)) {
04569       strcpy(dpr_type,cpl_propertylist_get_string(plist, KEY_NAME_DPR_TYPE));
04570       /* sinfo_msg("%s value is %s", KEY_NAME_DPR_TYPE, dpr_type); */
04571    } else {
04572       sinfo_msg_warning("keyword %s does not exist",KEY_NAME_DPR_TYPE);
04573       sinfo_free_propertylist(&plist);
04574       return -1;
04575    }
04576 
04577    /*
04578      In order to use the frame tag to identify frames we have to add this line
04579      strcpy(dpr_type,cpl_frame_get_tag(ref_frame));
04580 
04581    */
04582 
04583    if(strstr(dpr_type,"STD") != NULL) {
04584       result = 1;
04585       sinfo_free_propertylist(&plist);
04586       return result;
04587    }
04588 
04589    if(strstr(dpr_type,"PSF") != NULL) {
04590       result = 1;
04591       sinfo_free_propertylist(&plist);
04592       return result;
04593    }
04594 
04595    if(strstr(dpr_type,"SKY") != NULL) {
04596       result = 0;
04597       sinfo_free_propertylist(&plist);
04598       return result;
04599    }
04600 
04601 
04602    if(strstr(dpr_type,"OBJECT") != NULL) {
04603       result = 1;
04604       sinfo_free_propertylist(&plist);
04605       return result;
04606    }
04607    /*
04608      if(strstr(dpr_type,"PUPIL") != NULL) {
04609      result = 1;
04610      cpl_propertylist_delete(plist);
04611      return result;
04612      }
04613    */
04614 
04615    if (sinfo_propertylist_has(plist, KEY_NAME_LAMP_XE)) {
04616       lamp_Xe=cpl_propertylist_get_bool(plist, KEY_NAME_LAMP_XE);
04617       /* sinfo_msg("%s value is %d", KEY_NAME_LAMP_XE, lamp_Xe); */
04618    } else {
04619       sinfo_msg_warning("keyword %s does not exist",KEY_NAME_LAMP_XE);
04620       sinfo_free_propertylist(&plist);
04621       return -1;
04622    }
04623 
04624    if (sinfo_propertylist_has(plist, KEY_NAME_LAMP_KR)) {
04625       lamp_Kr=cpl_propertylist_get_bool(plist, KEY_NAME_LAMP_KR);
04626       /* sinfo_msg("%s value is %d", KEY_NAME_LAMP_KR, lamp_Kr); */
04627    } else {
04628       sinfo_msg_warning("keyword %s does not exist",KEY_NAME_LAMP_KR);
04629       sinfo_free_propertylist(&plist);
04630       return -1;
04631    }
04632 
04633    if (sinfo_propertylist_has(plist, KEY_NAME_LAMP_NE)) {
04634       lamp_Ne=cpl_propertylist_get_bool(plist, KEY_NAME_LAMP_NE);
04635       /* sinfo_msg("%s value is %d", KEY_NAME_LAMP_NE, lamp_Ne); */
04636    } else {
04637       sinfo_msg_warning("keyword %s does not exist",KEY_NAME_LAMP_NE);
04638       sinfo_free_propertylist(&plist);
04639       return -1;
04640    }
04641 
04642    if (sinfo_propertylist_has(plist, KEY_NAME_LAMP_AR)) {
04643       lamp_Ar=cpl_propertylist_get_bool(plist, KEY_NAME_LAMP_AR);
04644       /* sinfo_msg("%s value is %d", KEY_NAME_LAMP_AR, lamp_Ar); */
04645    } else {
04646       sinfo_msg_warning("keyword %s does not exist",KEY_NAME_LAMP_AR);
04647       sinfo_free_propertylist(&plist);
04648       return -1;
04649    }
04650 
04651    if (sinfo_propertylist_has(plist, KEY_NAME_LAMP_HALO)) {
04652       lamp_Halo=cpl_propertylist_get_bool(plist, KEY_NAME_LAMP_HALO);
04653       /* sinfo_msg("%s value is %d", KEY_NAME_LAMP_HALO, lamp_Halo); */
04654    } else {
04655       sinfo_msg_warning("keyword %s does not exist",KEY_NAME_LAMP_HALO);
04656       sinfo_free_propertylist(&plist);
04657       return -1;
04658    }
04659 
04660 
04661 
04662 
04663    if(lamp_Xe) {
04664       result=1;
04665    }
04666 
04667    if(lamp_Kr) {
04668       result=1;
04669    }
04670 
04671    if(lamp_Ne) {
04672       result=1;
04673    }
04674 
04675    if(lamp_Ar) {
04676       result=1;
04677    }
04678 
04679 
04680    if(lamp_Halo) {
04681       result=1;
04682    }
04683 
04684   cleanup:
04685    sinfo_free_propertylist(&plist);
04686    return result;
04687 
04688 
04689 }
04690 
04691 
04692 
04693 int
04694 sinfo_pfits_add_qc(cpl_propertylist       *   plist,
04695                    qc_log          *   qclog)
04696 {
04697    char            key_name[80] ;
04698    char            key_value[80] ;
04699 
04700    int             i =0;
04701 
04702    /* Test entries */
04703    if (plist == NULL) return -1 ;
04704 
04705    /* Parameter Name:    PIPEFILE */
04706    /* we add ESO prefix to FITS keywords" */
04707    for(i=0;i<qclog[0].n;i++) {
04708       strcpy(key_name,"ESO ");
04709       strcat(key_name,qclog[i].name);
04710       if(strcmp(qclog[i].type,"string") == 0) {
04711          snprintf(key_value,sizeof(key_value)-1,"%s",qclog[i].s_val);
04712          cpl_propertylist_append_string(plist, key_name,key_value) ;
04713          cpl_propertylist_set_comment(plist, key_name,qclog[i].comm) ;
04714 
04715       } else if(strcmp(qclog[i].type,"bool") == 0) {
04716          snprintf(key_value,sizeof(key_value),"%i",(int)qclog[i].n_val);
04717          cpl_propertylist_append_bool(plist, key_name,(int)qclog[i].n_val) ;
04718          cpl_propertylist_set_comment(plist, key_name,qclog[i].comm) ;
04719       } else if(strcmp(qclog[i].type,"int") == 0) {
04720          snprintf(key_value,sizeof(key_value),"%i",(int)qclog[i].n_val);
04721          cpl_propertylist_append_int(plist, key_name,(int)qclog[i].n_val) ;
04722          cpl_propertylist_set_comment(plist, key_name,qclog[i].comm) ;
04723       } else if(strcmp(qclog[i].type,"float") == 0) {
04724          snprintf(key_value,sizeof(key_value),"%f",(float)qclog[i].n_val);
04725          cpl_propertylist_append_float(plist, key_name,(float)qclog[i].n_val) ;
04726          cpl_propertylist_set_comment(plist, key_name,qclog[i].comm) ;
04727       } else if(strcmp(qclog[i].type,"double") == 0) {
04728          snprintf(key_value,sizeof(key_value),"%f",qclog[i].n_val);
04729          cpl_propertylist_append_double(plist, key_name,qclog[i].n_val) ;
04730          cpl_propertylist_set_comment(plist, key_name,qclog[i].comm) ;
04731       }
04732 
04733    }
04734 
04735    return 0 ;
04736 }
04737 
04738 
04739 

Generated on 3 Mar 2013 for SINFONI Pipeline Reference Manual by  doxygen 1.6.1