sinfo_dfs.c

00001 /* $Id: sinfo_dfs.c,v 1.38 2010/02/13 12:24:00 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: 2010/02/13 12:24:00 $
00024  * $Revision: 1.38 $
00025  * $Name: sinfo-2_2_5 $
00026  */
00027 
00028 #ifdef HAVE_CONFIG_H
00029 #  include <config.h>
00030 #endif
00031 /*----------------------------------------------------------------------------
00032   Macros
00033   ----------------------------------------------------------------------------*/
00034 
00035 
00036 /*----------------------------------------------------------------------------
00037   Private to this module
00038   ----------------------------------------------------------------------------*/
00039 
00040 
00041 /*-----------------------------------------------------------------------------
00042   Includes
00043   ----------------------------------------------------------------------------*/
00044 #include "sinfo_dfs.h"
00045 #include <assert.h>
00046 #include <cpl.h>
00047 #include <math.h>
00048 #include "sinfo_error.h"
00049 #include "sinfo_utilities.h"
00050 #include "sinfo_utils_wrappers.h"
00051 #include "sinfo_msg.h"
00052 #include "sinfo_globals.h"
00053 #include "sinfo_pro_save.h"
00054 #include "sinfo_skycor.h"
00055 #include "sinfo_file_handling.h"
00056 #include <unistd.h>
00057 #include <stdio.h>
00058 
00059 /*---------------------------------------------------------------------------*/
00065 /*---------------------------------------------------------------------------*/
00066 
00067 /* defines */
00068 
00069 #define FITS_MAGIC_SZ      6
00070 #define SINFO_FIT_AMOEBA_NMAX 5000
00071 
00072 /* function prototypes */
00073 /*
00074   static int
00075   sinfo_stat_rectangle(cpl_image* img,
00076   const int kappa,
00077   const int nclip,
00078   double *mean,
00079   double *stdev);
00080 */
00081 static void
00082 sinfo_fit_amoeba_get_psum(int ndim, int mpts, double** p, double* psum);
00083 
00084 
00085 static  double
00086 sinfo_fit_amotry(double** p,
00087                  double y[],
00088                  double psum[],
00089                  int ndim,
00090                  double (*funk)(double[]),
00091                  int ihi,
00092                  double fac);
00093 
00094 
00095 static double
00096 get_chisq(int N, int D,
00097           int (*f)(const double x[], const double a[], double *result),
00098           const double *a,
00099           const double *x,
00100           const double *y,
00101           const double *sigma);
00102 
00103 static int get_candidate(const double *a, const int ia[],
00104                          int M, int N, int D,
00105                          double lambda,
00106                          int    (*f)(const double x[], const double a[],
00107                                      double *result),
00108                          int (*dfda)(const double x[], const double a[],
00109                                      double result[]),
00110                          const double *x,
00111                          const double *y,
00112                          const double *sigma,
00113                          double *partials,
00114                          cpl_matrix *alpha,
00115                          cpl_matrix *beta,
00116                          double *a_da);
00117 
00118 
00119 
00120 int
00121 sinfo_frame_is_raw_dark(char * tag);
00122 
00128 /*---------------------------------------------------------------------------*/
00135 /*---------------------------------------------------------------------------*/
00136 cpl_frameset *
00137 sinfo_frameset_extract(const cpl_frameset *frames,
00138                        const char *tag)
00139 {
00140    cpl_frameset *subset = NULL;
00141    const cpl_frame *f;
00142 
00143 
00144 
00145    assure( frames != NULL, CPL_ERROR_ILLEGAL_INPUT, "Null frameset" );
00146    assure( tag    != NULL, CPL_ERROR_ILLEGAL_INPUT, "Null tag" );
00147 
00148    subset = cpl_frameset_new();
00149 
00150    for (f = cpl_frameset_find_const(frames, tag);
00151         f != NULL;
00152         f = cpl_frameset_find_const(frames, NULL)) {
00153 
00154       cpl_frameset_insert(subset, cpl_frame_duplicate(f));
00155    }
00156   cleanup:
00157    return subset;
00158 }
00159 
00160 
00161 
00167 int sinfo_print_rec_status(const int val) {
00168    if(cpl_error_get_code() != CPL_ERROR_NONE) {
00169       sinfo_msg_error("Recipe status at %d",val);
00170       sinfo_msg_error("%s",(const char*) cpl_error_get_message());
00171       sinfo_msg_error("%s",(const char*) cpl_error_get_where());
00172       return -1;
00173    }
00174    return 0;
00175 }
00176 
00177 
00194 cpl_vector*
00195 sinfo_vector_clip(const cpl_vector* vinp,
00196                   const double kappa,
00197                   const int n,
00198                   const int method)
00199 {
00200    cpl_vector* vout=NULL;
00201    cpl_vector* vtmp=NULL;
00202    int size=0;
00203    int j=0;
00204    register int i=0;
00205 
00206    double mean=0;
00207    double median=0;
00208    double stdev=0;
00209    double* pt=NULL;
00210    double* po=NULL;
00211 
00212    cknull(vinp,"Null input vector");
00213    check_nomsg(vout=cpl_vector_duplicate(vinp));
00214    check_nomsg(mean=cpl_vector_get_mean(vout));
00215    check_nomsg(median=cpl_vector_get_median_const(vout));
00216    check_nomsg(stdev=cpl_vector_get_stdev(vout));
00217    check_nomsg(pt=cpl_vector_get_data(vtmp));
00218 
00219    if(method == 0) {
00220       /*
00221          are rejected
00222          values ||val-mean|| > kappa*sigma
00223       */
00224       for(j=0;j<n;j++) {
00225 
00226          check_nomsg(cpl_vector_sort(vout,1));  /* sort by increasing data */
00227          check_nomsg(po=cpl_vector_get_data(vout));
00228 
00229          if( (size > 1) && (fabs(po[size-1]-mean) > kappa*stdev)) {
00230 
00231             size--;
00232             check_nomsg(vtmp=cpl_vector_new(size));
00233             check_nomsg(pt=cpl_vector_get_data(vtmp));
00234             for(i=0;i<size;i++) {
00235                pt[i]=po[i];
00236             }
00237             check_nomsg(cpl_vector_delete(vout));
00238             check_nomsg(vout=cpl_vector_duplicate(vtmp));
00239             check_nomsg(cpl_vector_delete(vtmp));
00240 
00241             check_nomsg(mean=cpl_vector_get_mean(vout));
00242             check_nomsg(stdev=cpl_vector_get_stdev(vout));
00243 
00244          }
00245 
00246       }
00247 
00248    } else {
00249       /*
00250          are rejected
00251          values ||val-median|| > kappa*sigma
00252       */
00253 
00254 
00255       for(j=0;j<n;j++) {
00256 
00257          check_nomsg(cpl_vector_sort(vout,1));  /* sort by increasing data */
00258          check_nomsg(po=cpl_vector_get_data(vout));
00259 
00260          if( (size > 1) && (fabs(po[size-1]-median) > kappa*stdev)) {
00261 
00262             size--;
00263             check_nomsg(vtmp=cpl_vector_new(size));
00264             check_nomsg(pt=cpl_vector_get_data(vtmp));
00265             for(i=0;i<size;i++) {
00266                pt[i]=po[i];
00267             }
00268             check_nomsg(cpl_vector_delete(vout));
00269             check_nomsg(vout=cpl_vector_duplicate(vtmp));
00270             check_nomsg(cpl_vector_delete(vtmp));
00271 
00272             check_nomsg(median=cpl_vector_get_median_const(vout));
00273             check_nomsg(stdev=cpl_vector_get_stdev(vout));
00274 
00275          }
00276 
00277       }
00278 
00279 
00280 
00281 
00282    }
00283    return vout;
00284   cleanup:
00285    return NULL;
00286 
00287 
00288 }
00289 
00290 
00291 
00292 /*----------------------------------------------------------------------------*/
00326 /*----------------------------------------------------------------------------*/
00327 static int
00328 get_candidate(const double *a, const int ia[],
00329               int M, int N, int D,
00330               double lambda,
00331               int    (*f)(const double x[], const double a[], double *result),
00332               int (*dfda)(const double x[], const double a[], double result[]),
00333               const double *x,
00334               const double *y,
00335               const double *sigma,
00336               double *partials,
00337               cpl_matrix *alpha,
00338               cpl_matrix *beta,
00339               double *a_da)
00340 {
00341    int Mfit = 0;         /* Number of non-constant fit parameters */
00342    cpl_matrix *da;       /* Solution of   alpha * da = beta */
00343    double *alpha_data;
00344    double *beta_data;
00345    double *da_data;
00346    int i, imfit = 0;
00347    int j, jmfit = 0;
00348    int k = 0;
00349 
00350    /* For efficiency, don't check input in this static function */
00351 
00352    Mfit = cpl_matrix_get_nrow(alpha);
00353 
00354    alpha_data    = cpl_matrix_get_data(alpha);
00355    beta_data     = cpl_matrix_get_data(beta);
00356 
00357    /* Build alpha, beta:
00358     *
00359     *  alpha[i,j] = sum_{k=1,N} (sigma_k)^-2 * df/da_i * df/da_j  *
00360     *                           (1 + delta_ij lambda) ,
00361     *
00362     *   beta[i]   = sum_{k=1,N} (sigma_k)^-2 * ( y_k - f(x_k) ) * df/da_i
00363     *
00364     * where (i,j) loop over the non-constant parameters (0 to Mfit-1),
00365     * delta is Kronecker's delta, and all df/da are evaluated in x_k
00366     */
00367 
00368    cpl_matrix_fill(alpha, 0.0);
00369    cpl_matrix_fill(beta , 0.0);
00370 
00371    for (k = 0; k < N; k++)
00372    {
00373       double sm2 = 0.0;                /* (sigma_k)^-2 */
00374       double fx_k = 0.0;               /* f(x_k)       */
00375       const double *x_k = &(x[0+k*D]); /* x_k          */
00376 
00377       if (sigma == NULL)
00378       {
00379          sm2 = 1.0;
00380       }
00381       else
00382       {
00383          sm2 = 1.0 / (sigma[k] * sigma[k]);
00384       }
00385 
00386       /* Evaluate f(x_k) */
00387       cpl_ensure( f(x_k, a, &fx_k) == 0, CPL_ERROR_ILLEGAL_INPUT, -1);
00388 
00389       /* Evaluate (all) df/da (x_k) */
00390       cpl_ensure( dfda(x_k, a, partials) == 0,
00391                   CPL_ERROR_ILLEGAL_INPUT, -1);
00392 
00393       for (i = 0, imfit = 0; i < M; i++)
00394       {
00395          if (ia[i] != 0)
00396          {
00397             /* Beta */
00398             beta_data[imfit] +=
00399                sm2 * (y[k] - fx_k) * partials[i];
00400 
00401             /* Alpha is symmetrical, so compute
00402                only lower-left part */
00403             for (j = 0, jmfit = 0; j < i; j++)
00404             {
00405                if (ia[j] != 0)
00406                {
00407                   alpha_data[jmfit + imfit*Mfit] +=
00408                      sm2 * partials[i] *
00409                      partials[j];
00410 
00411                   jmfit += 1;
00412                }
00413             }
00414 
00415             /* Alpha, diagonal terms */
00416             j = i;
00417             jmfit = imfit;
00418 
00419             alpha_data[jmfit + imfit*Mfit] +=
00420                sm2 * partials[i] *
00421                partials[j] * (1 + lambda);
00422 
00423             imfit += 1;
00424          }
00425       }
00426 
00427       assert( imfit == Mfit );
00428    }
00429 
00430    /* Create upper-right part of alpha */
00431    for (i = 0, imfit = 0; i < M; i++) {
00432       if (ia[i] != 0) {
00433          for (j = i+1, jmfit = imfit+1; j < M; j++) {
00434             if (ia[j] != 0) {
00435                alpha_data[jmfit+imfit*Mfit] = alpha_data[imfit+jmfit*Mfit];
00436                jmfit += 1;
00437             }
00438          }
00439          assert( jmfit == Mfit );
00440          imfit += 1;
00441       }
00442    }
00443    assert( imfit == Mfit );
00444 
00445    da = cpl_matrix_solve(alpha, beta);
00446 
00447    cpl_ensure(da != NULL, cpl_error_get_code(), -1);
00448 
00449    /* Create a+da vector by adding a and da */
00450    da_data   = cpl_matrix_get_data(da);
00451 
00452    for (i = 0, imfit = 0; i < M; i++)
00453    {
00454       if (ia[i] != 0)
00455       {
00456          a_da[i] = a[i] + da_data[0 + imfit*1];
00457 
00458          imfit += 1;
00459       }
00460       else
00461       {
00462          a_da[i] = a[i];
00463       }
00464    }
00465 
00466    assert( imfit == Mfit );
00467 
00468    cpl_matrix_delete(da);
00469 
00470    return 0;
00471 }
00472 
00473 /*----------------------------------------------------------------------------*/
00474 
00475 
00476 /*----------------------------------------------------------------------------*/
00497 /*----------------------------------------------------------------------------*/
00498 
00499 static double
00500 get_chisq(int N, int D,
00501           int (*f)(const double x[], const double a[], double *result),
00502           const double *a,
00503           const double *x,
00504           const double *y,
00505           const double *sigma)
00506 {
00507    double chi_sq;     /* Result */
00508    int i = 0;
00509 
00510    /* For efficiency, don't check input in this static function */
00511    chi_sq = 0.0;
00512    for (i = 0; i < N; i++)
00513    {
00514       double fx_i;
00515       double residual;                 /* Residual in units of uncertainty */
00516       const double *x_i = &(x[0+i*D]);
00517 
00518       /* Evaluate */
00519       cpl_ensure( f(x_i,
00520                     a,
00521                     &fx_i) == 0, CPL_ERROR_ILLEGAL_INPUT, -1.0);
00522 
00523       /* Accumulate */
00524       if (sigma == NULL)
00525       {
00526          residual = (fx_i - y[i]);
00527       }
00528       else
00529       {
00530          residual = (fx_i - y[i]) / sigma[i];
00531       }
00532 
00533       chi_sq += residual*residual;
00534 
00535    }
00536 
00537    return chi_sq;
00538 }
00539 
00540 
00541 
00542 #ifndef CPL_VECTOR_FIT_MAXITER
00543 #define CPL_VECTOR_FIT_MAXITER 1000
00544 #endif
00545 /*----------------------------------------------------------------------------*/
00612 /*----------------------------------------------------------------------------*/
00613 cpl_error_code
00614 sinfo_fit_lm(const cpl_matrix *x,
00615              const cpl_matrix *sigma_x,
00616              const cpl_vector *y,
00617              const cpl_vector *sigma_y,
00618              cpl_vector *a,
00619              const int ia[],
00620              int    (*f)(const double x[],
00621                          const double a[],
00622                          double *result),
00623              int (*dfda)(const double x[],
00624                          const double a[],
00625                          double result[]),
00626              double *mse,
00627              double *red_chisq,
00628              cpl_matrix **covariance)
00629 {
00630    const double *x_data     = NULL; /* Pointer to input data                  */
00631    const double *y_data     = NULL; /* Pointer to input data                  */
00632    const double *sigma_data = NULL; /* Pointer to input data                  */
00633    int N    = 0;                    /* Number of data points                  */
00634    int D    = 0;                    /* Dimension of x-points                  */
00635    int M    = 0;                    /* Number of fit parameters               */
00636    int Mfit = 0;                    /* Number of non-constant fit
00637                                        parameters                             */
00638 
00639    double lambda    = 0.0;          /* Lambda in L-M algorithm                */
00640    double MAXLAMBDA = 10e40;        /* Parameter to control the graceful exit
00641                                        if steepest descent unexpectedly fails */
00642    double chi_sq    = 0.0;          /* Current  chi^2                         */
00643    int count        = 0;            /* Number of successive small improvements
00644                                        in chi^2 */
00645    int iterations   = 0;
00646 
00647    cpl_matrix *alpha  = NULL;       /* The MxM ~curvature matrix used in L-M  */
00648    cpl_matrix *beta   = NULL;       /* Mx1 matrix = -.5 grad(chi^2)           */
00649    double *a_data     = NULL;       /* Parameters, a                          */
00650    double *a_da       = NULL;       /* Candidate position a+da                */
00651    double *part       = NULL;       /* The partial derivatives df/da          */
00652    int *ia_local      = NULL;       /* non-NULL version of ia                 */
00653 
00654    /* If covariance computation is requested, then either
00655     * return the covariance matrix or return NULL.
00656     */
00657    if (covariance != NULL) *covariance = NULL;
00658 
00659    /* Validate input */
00660    cpl_ensure_code(x       != NULL, CPL_ERROR_NULL_INPUT);
00661    cpl_ensure_code(sigma_x == NULL, CPL_ERROR_UNSUPPORTED_MODE);
00662    cpl_ensure_code(y       != NULL, CPL_ERROR_NULL_INPUT);
00663    cpl_ensure_code(a       != NULL, CPL_ERROR_NULL_INPUT);
00664    /* ia may be NULL */
00665    cpl_ensure_code(f       != NULL, CPL_ERROR_NULL_INPUT);
00666    cpl_ensure_code(dfda    != NULL, CPL_ERROR_NULL_INPUT);
00667 
00668    /* Chi^2 and covariance computations require sigmas to be known */
00669    cpl_ensure_code( sigma_y != NULL ||
00670                     (red_chisq == NULL && covariance == NULL),
00671                     CPL_ERROR_INCOMPATIBLE_INPUT);
00672 
00673    D = cpl_matrix_get_ncol(x);
00674    N = cpl_matrix_get_nrow(x);
00675    M = cpl_vector_get_size(a);
00676    cpl_ensure_code(N > 0 && D > 0 && M > 0, CPL_ERROR_ILLEGAL_INPUT);
00677 
00678    cpl_ensure_code( cpl_vector_get_size(y) == N,
00679                     CPL_ERROR_INCOMPATIBLE_INPUT);
00680 
00681    x_data = cpl_matrix_get_data_const(x);
00682    y_data = cpl_vector_get_data_const(y);
00683    a_data = cpl_vector_get_data(a);
00684 
00685    if (sigma_y != NULL)
00686    {
00687       cpl_ensure_code( cpl_vector_get_size(sigma_y) == N,
00688                        CPL_ERROR_INCOMPATIBLE_INPUT);
00689       /* Sigmas must be positive */
00690       cpl_ensure_code( cpl_vector_get_min (sigma_y) > 0,
00691                        CPL_ERROR_ILLEGAL_INPUT);
00692       sigma_data = cpl_vector_get_data_const(sigma_y);
00693    }
00694 
00695    ia_local = cpl_malloc(M * sizeof(int));
00696    cpl_ensure_code(ia_local != NULL, CPL_ERROR_ILLEGAL_OUTPUT);
00697 
00698    /* Count non-constant fit parameters, copy ia */
00699    if (ia != NULL)
00700    {
00701       int i;
00702 
00703       Mfit = 0;
00704       for (i = 0; i < M; i++)
00705       {
00706          ia_local[i] = ia[i];
00707 
00708          if (ia[i] != 0)
00709          {
00710             Mfit += 1;
00711          }
00712       }
00713 
00714       if (! (Mfit > 0))
00715       {
00716          cpl_free(ia_local);
00717          cpl_ensure_code( CPL_FALSE,
00718                           CPL_ERROR_ILLEGAL_INPUT);
00719       }
00720    }
00721    else
00722    {
00723       /* All parameters participate */
00724       int i;
00725 
00726       Mfit = M;
00727 
00728       for (i = 0; i < M; i++)
00729       {
00730          ia_local[i] = 1;
00731       }
00732    }
00733 
00734    /* To compute reduced chi^2, we need N > Mfit */
00735    if (! ( red_chisq == NULL || N > Mfit ) )
00736    {
00737       cpl_free(ia_local);
00738       cpl_ensure_code(
00739          CPL_FALSE,
00740          CPL_ERROR_ILLEGAL_INPUT);
00741    }
00742 
00743    /* Create alpha, beta, a_da, part  work space */
00744    alpha = cpl_matrix_new(Mfit, Mfit);
00745    if (alpha == NULL)
00746    {
00747       cpl_free(ia_local);
00748       cpl_ensure_code(
00749          CPL_FALSE,
00750          CPL_ERROR_ILLEGAL_OUTPUT);
00751    }
00752 
00753    beta = cpl_matrix_new(Mfit, 1);
00754    if (beta == NULL)
00755    {
00756       cpl_free(ia_local);
00757       cpl_matrix_delete(alpha);
00758       cpl_ensure_code(
00759          CPL_FALSE,
00760          CPL_ERROR_ILLEGAL_OUTPUT);
00761    }
00762 
00763    a_da = cpl_malloc(M * sizeof(double));
00764    if (a_da == NULL)
00765    {
00766       cpl_free(ia_local);
00767       cpl_matrix_delete(alpha);
00768       cpl_matrix_delete(beta);
00769       cpl_ensure_code(
00770          CPL_FALSE,
00771          CPL_ERROR_ILLEGAL_OUTPUT);
00772    }
00773 
00774    part = cpl_malloc(M * sizeof(double));
00775    if (part == NULL)
00776    {
00777       cpl_free(ia_local);
00778       cpl_matrix_delete(alpha);
00779       cpl_matrix_delete(beta);
00780       cpl_free(a_da);
00781       cpl_ensure_code(
00782          CPL_FALSE,
00783          CPL_ERROR_ILLEGAL_OUTPUT);
00784    }
00785 
00786    /* Initialize loop variables */
00787    lambda = 0.001;
00788    count = 0;
00789    iterations = 0;
00790    if( (chi_sq = get_chisq(N, D, f, a_data, x_data, y_data, sigma_data)) < 0)
00791    {
00792       cpl_free(ia_local);
00793       cpl_matrix_delete(alpha);
00794       cpl_matrix_delete(beta);
00795       cpl_free(a_da);
00796       cpl_free(part);
00797       cpl_ensure_code(
00798          CPL_FALSE,
00799          cpl_error_get_code());
00800    }
00801 
00802    /* uves_msg_debug("Initial chi^2 = %f", chi_sq); */
00803 
00804    /* Iterate until chi^2 didn't improve substantially many (say, 5)
00805       times in a row */
00806    while (count < 5 &&
00807           lambda < MAXLAMBDA &&
00808           iterations < CPL_VECTOR_FIT_MAXITER)
00809    {
00810       /* In each iteration lambda increases, or chi^2 decreases or
00811          count increases. Because chi^2 is bounded from below
00812          (and lambda and count from above), the loop will terminate */
00813 
00814       double chi_sq_candidate = 0.0;
00815       int returncode = 0;
00816 
00817       /* Get candidate position in parameter space = a+da,
00818        * where  alpha * da = beta .
00819        * Increase lambda until alpha is non-singular
00820        */
00821 
00822       while( (returncode = get_candidate(a_data, ia_local,
00823                                          M, N, D,
00824                                          lambda, f, dfda,
00825                                          x_data, y_data, sigma_data,
00826                                          part, alpha, beta, a_da)
00827                 ) != 0
00828              && cpl_error_get_code() == CPL_ERROR_SINGULAR_MATRIX
00829              && lambda < MAXLAMBDA)
00830       {
00831          /* uves_msg_debug("Singular matrix. lambda = %e", lambda); */
00832          cpl_error_reset();
00833          lambda *= 9.0;
00834       }
00835 
00836       /* Set error if lambda diverged */
00837       if ( !( lambda < MAXLAMBDA ) )
00838       {
00839          cpl_free(ia_local);
00840          cpl_matrix_delete(alpha);
00841          cpl_matrix_delete(beta);
00842          cpl_free(a_da);
00843          cpl_free(part);
00844          cpl_ensure_code(
00845             CPL_FALSE,
00846             CPL_ERROR_CONTINUE);
00847       }
00848 
00849       if (returncode != 0)
00850       {
00851          cpl_free(ia_local);
00852          cpl_matrix_delete(alpha);
00853          cpl_matrix_delete(beta);
00854          cpl_free(a_da);
00855          cpl_free(part);
00856          cpl_ensure_code(
00857             CPL_FALSE,
00858             cpl_error_get_code());
00859       }
00860 
00861       /* Get chi^2(a+da) */
00862       if ((chi_sq_candidate = get_chisq(N, D, f, a_da,
00863                                         x_data, y_data, sigma_data)) < 0)
00864       {
00865          cpl_free(ia_local);
00866          cpl_matrix_delete(alpha);
00867          cpl_matrix_delete(beta);
00868          cpl_free(a_da);
00869          cpl_free(part);
00870          cpl_ensure_code(
00871             CPL_FALSE,
00872             cpl_error_get_code());
00873       }
00874 
00875       /* uves_msg_debug("Chi^2 = %f  Candidate = %f  Lambda = %e",
00876          chi_sq, chi_sq_candidate, lambda);  */
00877 
00878       if (chi_sq_candidate > chi_sq)
00879       {
00880          /* Move towards steepest descent */
00881          lambda *= 9.0;
00882       }
00883       else
00884       {
00885          /* Move towards Newton's algorithm */
00886          lambda /= 10.0;
00887 
00888          /* Count the number of successive improvements in chi^2 of
00889             less than 0.01 relative */
00890          if ( chi_sq == 0 ||
00891               (chi_sq - chi_sq_candidate)/chi_sq < .01)
00892          {
00893             count += 1;
00894          }
00895          else
00896          {
00897             /* Chi^2 improved by a significant amount,
00898                reset counter */
00899             count = 0;
00900          }
00901 
00902          /* chi^2 improved, update a and chi^2 */
00903          {
00904             int i;
00905             for (i = 0; i < M; i++) a_data[i] = a_da[i];
00906          }
00907          chi_sq = chi_sq_candidate;
00908       }
00909       iterations++;
00910    }
00911 
00912    /* Set error if we didn't converge */
00913    if ( !( lambda < MAXLAMBDA && iterations < CPL_VECTOR_FIT_MAXITER ) )
00914    {
00915       cpl_free(ia_local);
00916       cpl_matrix_delete(alpha);
00917       cpl_matrix_delete(beta);
00918       cpl_free(a_da);
00919       cpl_free(part);
00920       cpl_ensure_code(
00921          CPL_FALSE,
00922          CPL_ERROR_CONTINUE);
00923    }
00924 
00925    /* Compute mse if requested */
00926    if (mse != NULL)
00927    {
00928       int i;
00929 
00930       *mse = 0.0;
00931 
00932       for(i = 0; i < N; i++)
00933       {
00934          double fx_i = 0.0;
00935          double residual = 0.0;
00936 
00937          /* Evaluate f(x_i) at the best fit parameters */
00938          if( f(&(x_data[i*D]),
00939                a_data,
00940                &fx_i) != 0)
00941          {
00942             cpl_free(ia_local);
00943             cpl_matrix_delete(alpha);
00944             cpl_matrix_delete(beta);
00945             cpl_free(a_da);
00946             cpl_free(part);
00947             cpl_ensure_code(
00948                CPL_FALSE,
00949                CPL_ERROR_ILLEGAL_INPUT);
00950          }
00951 
00952          residual = y_data[i] - fx_i;
00953          *mse += residual * residual;
00954       }
00955       *mse /= N;
00956    }
00957 
00958    /* Compute reduced chi^2 if requested */
00959    if (red_chisq != NULL)
00960    {
00961       /* We already know the optimal chi^2 (and that N > Mfit)*/
00962       *red_chisq = chi_sq / (N-Mfit);
00963    }
00964 
00965    /* Compute covariance matrix if requested
00966     * cov = alpha(lambda=0)^-1
00967     */
00968    if (covariance != NULL)
00969    {
00970       cpl_matrix *cov;
00971 
00972       if( get_candidate(a_data, ia_local,
00973                         M, N, D, 0.0, f, dfda,
00974                         x_data, y_data, sigma_data,
00975                         part, alpha, beta, a_da)
00976           != 0)
00977       {
00978          cpl_free(ia_local);
00979          cpl_matrix_delete(alpha);
00980          cpl_matrix_delete(beta);
00981          cpl_free(a_da);
00982          cpl_free(part);
00983          cpl_ensure_code(
00984             CPL_FALSE,
00985             cpl_error_get_code());
00986       }
00987 
00988       cov = cpl_matrix_invert_create(alpha);
00989       if (cov == NULL)
00990       {
00991          cpl_free(ia_local);
00992          cpl_matrix_delete(alpha);
00993          cpl_matrix_delete(beta);
00994          cpl_free(a_da);
00995          cpl_free(part);
00996          cpl_ensure_code(
00997             CPL_FALSE,
00998             cpl_error_get_code());
00999       }
01000 
01001       /* Make sure that variances are positive */
01002       {
01003          int i;
01004          for (i = 0; i < Mfit; i++)
01005          {
01006             if ( !(cpl_matrix_get(cov, i, i) > 0) )
01007             {
01008                cpl_free(ia_local);
01009                cpl_matrix_delete(alpha);
01010                cpl_matrix_delete(beta);
01011                cpl_free(a_da);
01012                cpl_free(part);
01013                cpl_matrix_delete(cov);
01014                *covariance = NULL;
01015                cpl_ensure_code(
01016                   CPL_FALSE,
01017                   CPL_ERROR_SINGULAR_MATRIX);
01018             }
01019          }
01020       }
01021 
01022       /* Expand covariance matrix from Mfit x Mfit
01023          to M x M. Set rows/columns corresponding to fixed
01024          parameters to zero */
01025 
01026       *covariance = cpl_matrix_new(M, M);
01027       if (*covariance == NULL)
01028       {
01029          cpl_free(ia_local);
01030          cpl_matrix_delete(alpha);
01031          cpl_matrix_delete(beta);
01032          cpl_free(a_da);
01033          cpl_free(part);
01034          cpl_matrix_delete(cov);
01035          cpl_ensure_code(
01036             CPL_FALSE,
01037             CPL_ERROR_ILLEGAL_OUTPUT);
01038       }
01039 
01040       {
01041          int j, jmfit;
01042 
01043          for (j = 0, jmfit = 0; j < M; j++)
01044             if (ia_local[j] != 0)
01045             {
01046                int i, imfit;
01047 
01048                for (i = 0, imfit = 0; i < M; i++)
01049                   if (ia_local[i] != 0)
01050                   {
01051                      cpl_matrix_set(*covariance, i, j,
01052                                     cpl_matrix_get(
01053                                        cov, imfit, jmfit));
01054                      imfit += 1;
01055                   }
01056 
01057                assert( imfit == Mfit );
01058 
01059                jmfit += 1;
01060             }
01061 
01062          assert( jmfit == Mfit );
01063       }
01064 
01065       cpl_matrix_delete(cov);
01066    }
01067 
01068    cpl_free(ia_local);
01069    cpl_matrix_delete(alpha);
01070    cpl_matrix_delete(beta);
01071    cpl_free(a_da);
01072    cpl_free(part);
01073 
01074    return CPL_ERROR_NONE;
01075 }
01076 
01077 
01078 
01079 
01080 
01081 
01082 
01083 
01084 
01085 
01086 
01087 
01088 
01089 
01090 
01091 /*-------------------------------------------------------------------------*/
01092 
01104 static void
01105 sinfo_fit_amoeba_get_psum(int ndim, int mpts, double** p, double* psum)
01106 {
01107    int i=0;
01108    int j=0;
01109    double sum=0;
01110    for (j=0;j<ndim;j++) {
01111       for (sum=0.0,i=0;i<mpts;i++) {
01112          sum += p[i][j];
01113       }
01114       psum[j]=sum;
01115    }
01116 
01117 }
01118 
01119 
01120 #define SINFO_FIT_AMOEBA_SWAP(a,b) {swap=(a);(a)=(b);(b)=swap;}
01121 
01122 
01123 
01150 void
01151 sinfo_fit_amoeba(
01152    double**p,
01153    double y[],
01154    int ndim,
01155    double ftol,
01156    double (*funk)(double[]),
01157    int* nfunk)
01158 {
01159 
01160 
01161    int i=0;
01162    int ihi=0;
01163    int ilo=0;
01164    int inhi=0;
01165    int j=0;
01166    int mpts=ndim+1;
01167    double rtol=0;
01168    double swap=0;
01169    double ysave=0;
01170    double ytry=0;
01171    cpl_vector* sum=NULL;
01172    double* psum=NULL;
01173 
01174    sum=cpl_vector_new(ndim);
01175    psum=cpl_vector_get_data(sum);
01176    *nfunk=0;
01177 
01178    sinfo_fit_amoeba_get_psum(ndim,mpts,p,psum);
01179 
01180    for(;;) {
01181       ilo=0;
01182       /*
01183         First we must determine which point is the highest (worst),
01184         next-highest, and lowest (best), by looping over the points
01185         in the simplex
01186       */
01187       ihi=y[0]>y[1] ? (inhi=1,0) : (inhi=0,1);
01188 
01189       for (i=0;i< mpts;i++) {
01190          if (y[i] <= y[ilo]) ilo=i;
01191          if (y[i] > y[ihi]) {
01192             inhi=ihi;
01193             ihi=i;
01194          } else if (y[i] > y[inhi] && i != ihi) inhi=i;
01195       }
01196       rtol=2.0*fabs(y[ihi]-y[ilo])/(fabs(y[ihi])+fabs(y[ilo]));
01197 
01198       /*
01199         compute the fractional range from highest to lowest and return if
01200         satisfactory
01201       */
01202       if(rtol < ftol) { // if returning, but best point and value is in slot 1
01203          SINFO_FIT_AMOEBA_SWAP(y[0],y[ilo]);
01204          for (i=0;i<ndim;i++) SINFO_FIT_AMOEBA_SWAP(p[1][i],p[ilo][i]);
01205          break;
01206       }
01207       if (*nfunk >= SINFO_FIT_AMOEBA_NMAX) {
01208          sinfo_msg_error("NMAX exceeded");
01209          SINFO_FIT_AMOEBA_SWAP(y[0],y[ilo]);
01210          for (i=0;i<ndim;i++) SINFO_FIT_AMOEBA_SWAP(p[1][i],p[ilo][i]);
01211          for (i=0;i<ndim;i++) {
01212             sinfo_msg("p[1][i]=%g p[ilo][i]=%g ilo=%d",p[1][i],p[ilo][i],ilo);
01213          }
01214          assure(*nfunk >= SINFO_FIT_AMOEBA_NMAX,CPL_ERROR_UNSPECIFIED,
01215                 "NMAX exceeded");
01216          break;
01217 
01218       }
01219       *nfunk +=2;
01220       /*
01221         Begin a new iteration. First extrapolate by a Factor -1 through the face
01222         of the simplex across the high point, i.e. reflect the simplex from the
01223         high point
01224       */
01225       ytry=sinfo_fit_amotry(p,y,psum,ndim,funk,ihi,-1.0);
01226       if(ytry <= y[ilo]) {
01227          /*
01228             Gives a result better than the best point, so try an additional
01229             extrapolation by a factor 2
01230          */
01231          ytry=sinfo_fit_amotry(p,y,psum,ndim,funk,ihi,2.0);
01232       } else if (ytry >= y[inhi]) {
01233 
01234          /*
01235            The reflected point is worse than the second highest, so look for an
01236            intermediate lower point, i.e. do a one-dimensional contraction
01237          */
01238          ysave=y[ihi];
01239          ytry=sinfo_fit_amotry(p,y,psum,ndim,funk,ihi,0.5);
01240          if(ytry >= ysave) {
01241             /*
01242                Can't seem to get rid of that high point.
01243                Better contract around the lowest (best) point
01244             */
01245             for(i=0;i<mpts;i++) {
01246                if(i != ilo) {
01247                   for( j=0;j<ndim;j++) {
01248                      p[i][j]=psum[j]=0.5*(p[i][j]+p[ilo][j]);
01249                   }
01250                   y[i]=(*funk)(psum);
01251                }
01252             }
01253             *nfunk += ndim; /* Keep track of function evaluations */
01254             sinfo_fit_amoeba_get_psum(ndim,mpts,p,psum);/* Recomputes psum */
01255          }
01256       } else {
01257          --(*nfunk);
01258          /* Go back for the test of doneness and the next iteration */
01259       }
01260    }
01261   cleanup:
01262    cpl_vector_delete(sum);
01263 }
01264 
01265 
01266 static  double
01267 sinfo_fit_amotry(double** p, double y[], double psum[], int ndim,
01268                  double (*funk)(double[]),int ihi, double fac)
01269 {
01270    int j;
01271    double fac1=0;
01272    double fac2=0;
01273    double ytry=0;
01274    cpl_vector * vtry=NULL;
01275    double *ptry=NULL;
01276 
01277    vtry=cpl_vector_new(ndim);
01278    ptry=cpl_vector_get_data(vtry);
01279 
01280    fac1=(1.0-fac)/ndim;
01281    fac2=fac1-fac;
01282 
01283    for (j=0;j<ndim;j++) {
01284       ptry[j]=psum[j]*fac1-p[ihi][j]*fac2;
01285    }
01286    ytry=(*funk)(ptry);
01287    if (ytry < y[ihi]) {
01288       y[ihi]=ytry;
01289       for (j=0;j<ndim;j++) {
01290          psum[j] += ptry[j]-p[ihi][j];
01291          p[ihi][j]=ptry[j];
01292       }
01293    }
01294    sinfo_free_my_vector(&vtry);
01295    return ytry;
01296 }
01297 
01298 /*-------------------------------------------------------------------------*/
01299 /*-------------------------------------------------------------------------*/
01309 /*--------------------------------------------------------------------------*/
01310 
01311 int sinfo_vector_dindgen(cpl_vector** v)
01312 {
01313 
01314    int sz=0;
01315    int i=0;
01316 
01317    cknull(*v,"Null input vector");
01318    check(sz=cpl_vector_get_size(*v),"Getting size of a vector");
01319 
01320    for(i=0;i<sz;i++) {
01321       cpl_vector_set(*v,i,(double)i);
01322    }
01323 
01324    return 0;
01325   cleanup:
01326    return -1;
01327 
01328 }
01329 
01330 /*-------------------------------------------------------------------------*/
01341 /*--------------------------------------------------------------------------*/
01342 
01343 int sinfo_is_fits_file(const char *filename)
01344 {
01345    FILE    *fp ;
01346    char    *magic ;
01347    int        isfits ;
01348 
01349    if ((fp = fopen(filename, "r"))==NULL) {
01350       sinfo_msg_error("cannot open file [%s]", filename) ;
01351       return -1 ;
01352    }
01353 
01354    magic = cpl_calloc(FITS_MAGIC_SZ+1, sizeof(char)) ;
01355    (void)fread(magic, 1, FITS_MAGIC_SZ, fp) ;
01356    (void)fclose(fp) ;
01357    magic[FITS_MAGIC_SZ] = (char)0 ;
01358    if (strstr(magic, "SIMPLE")!=NULL)
01359       isfits = 1 ;
01360    else
01361       isfits = 0 ;
01362    cpl_free(magic) ;
01363    return isfits ;
01364 }
01365 
01366 /*---------------------------------------------------------------------------*/
01372 /*---------------------------------------------------------------------------*/
01373 cpl_error_code
01374 sinfo_table_correl(cpl_table * t1, cpl_table* t2, cpl_table* range,double* xcor)
01375 {
01376 
01377    double wsr=0;
01378    double wer=0;
01379    int nr=0;
01380    int i=0;
01381    int status=0;
01382    int nrows=0;
01383    double mean=0;
01384    double prod=0;
01385 
01386    cpl_table* tmp_t1=NULL;
01387    cpl_table* tmp_t2=NULL;
01388 
01389    check_nomsg(nr=cpl_table_get_nrow(range));
01390    for(i=0;i<nr;i++) {
01391       wsr=cpl_table_get_double(range,"WSTART",i,&status);
01392       wer=cpl_table_get_double(range,"WEND",i,&status);
01393       cpl_table_and_selected_double(t1,"WAVE",CPL_NOT_LESS_THAN,wsr);
01394       cpl_table_and_selected_double(t1,"WAVE",CPL_NOT_GREATER_THAN,wer);
01395       tmp_t1=cpl_table_extract_selected(t1);
01396       cpl_table_and_selected_double(t2,"WAVE",CPL_NOT_LESS_THAN,wsr);
01397       cpl_table_and_selected_double(t2,"WAVE",CPL_NOT_GREATER_THAN,wer);
01398       tmp_t2=cpl_table_extract_selected(t2);
01399       cpl_table_duplicate_column(tmp_t1,"INT1",tmp_t1,"INT");
01400       cpl_table_duplicate_column(tmp_t1,"INT2",tmp_t2,"INT");
01401       cpl_table_multiply_columns(tmp_t1,"INT1","INT2");
01402       mean=cpl_table_get_column_mean(tmp_t1,"INT1");
01403       nrows=cpl_table_get_nrow(tmp_t1);
01404       prod=mean*nrows;
01405       *xcor+=prod;
01406    }
01407 
01408   cleanup:
01409    return cpl_error_get_code();
01410 }
01416 /*----------------------------------------------------------------------------*/
01417 cpl_error_code
01418 sinfo_frameset_merge(cpl_frameset * set1, cpl_frameset* set2)
01419 {
01420 
01421    cpl_frame* frm_tmp=NULL;
01422    cpl_frame* frm_dup=NULL;
01423 
01424    passure(set1 != NULL, "Wrong input set");
01425 
01426    check_nomsg(frm_tmp = cpl_frameset_get_first(set2));
01427    while (frm_tmp != NULL)
01428    {
01429       frm_dup=cpl_frame_duplicate(frm_tmp);
01430       cpl_frameset_insert(set1,frm_dup);
01431       frm_tmp = cpl_frameset_get_next(set2);
01432    }
01433 
01434   cleanup:
01435    return cpl_error_get_code();
01436 }
01437 
01438 /*----------------------------------------------------------------------------*/
01446 /*----------------------------------------------------------------------------*/
01447 
01448 cpl_error_code
01449 sinfo_extract_frames_group_type(const cpl_frameset * set,
01450                                 cpl_frameset** ext,
01451                                 cpl_frame_group type)
01452 {
01453    const cpl_frame* frm_tmp=NULL;
01454    cpl_frame* frm_dup=NULL;
01455    cpl_frame_group g;
01456 
01457    check_nomsg(*ext = cpl_frameset_new());
01458    check_nomsg(frm_tmp = cpl_frameset_get_first_const(set));
01459    while (frm_tmp != NULL)
01460    {
01461       g=cpl_frame_get_group(frm_tmp);
01462       if(g == type) {
01463          frm_dup=cpl_frame_duplicate(frm_tmp);
01464          cpl_frameset_insert(*ext,frm_dup);
01465          sinfo_msg_debug("group %d insert file %s ",
01466                          type,cpl_frame_get_filename(frm_dup));
01467       }
01468       frm_tmp = cpl_frameset_get_next_const(set);
01469    }
01470 
01471   cleanup:
01472    return cpl_error_get_code();
01473 }
01474 
01475 
01476 
01477 
01488 int
01489 sinfo_get_pupil_shift(cpl_imagelist* iml,const int n,cpl_table** qclog_tbl)
01490 {
01491    int max_ima_x=0;
01492    int max_ima_y=0;
01493    int nx=0;
01494    int ny=0;
01495 
01496    double xshift=0;
01497    double yshift=0;
01498 
01499    double max_ima_cx=0;
01500    double max_ima_cy=0;
01501 
01502    cpl_image* img=NULL;
01503    cpl_image* img_dup=NULL;
01504 
01505    char key_name[FILE_NAME_SZ];
01506 
01507    img=cpl_imagelist_collapse_median_create(iml);
01508    nx=cpl_image_get_size_x(img);
01509    ny=cpl_image_get_size_y(img);
01510 
01511    img_dup=cpl_image_duplicate(img);
01512    sinfo_clean_nan(&img_dup);
01513    cpl_image_get_maxpos(img_dup,&max_ima_x,&max_ima_y);
01514    max_ima_cx=cpl_image_get_centroid_x_window(img_dup,1,1,nx,ny);
01515    max_ima_cy=cpl_image_get_centroid_y_window(img_dup,1,1,nx,ny);
01516 
01517    cpl_image_delete(img_dup);
01518 
01519 
01520    xshift=max_ima_cx-(double)nx/2;
01521    yshift=max_ima_cy-(double)ny/2;
01522 
01523    snprintf(key_name,MAX_NAME_SIZE-1,"%s%d%s","QC PUPIL",n," SHIFTX");
01524    sinfo_qclog_add_double(*qclog_tbl,key_name,xshift,
01525                           "X shift centroid - center image","%f");
01526 
01527    snprintf(key_name,MAX_NAME_SIZE-1,"%s%d%s","QC PUPIL",n," SHIFTY");
01528    sinfo_qclog_add_double(*qclog_tbl,key_name,yshift,
01529                           "Y shift centroid - center image","%f");
01530    cpl_image_delete(img);
01531 
01532    return 0;
01533 }
01534 
01535 
01536 
01543 int sinfo_get_strehl_type(cpl_frameset* sof)
01544 {
01545    int strehl_sw=0;
01546    int nobs=0;
01547    int i=0;
01548    cpl_frameset* obs=NULL;
01549 
01550    cpl_frame* frame=NULL;
01551    float* pix_scale=NULL;
01552    cpl_propertylist* plist=NULL;
01553 
01554    obs = cpl_frameset_new();
01555 
01556    sinfo_contains_frames_kind(sof,obs,PRO_OBS_PSF);
01557 
01558    nobs=cpl_frameset_get_size(obs);
01559    if (nobs < 1) {
01560       sinfo_contains_frames_kind(sof,obs,PRO_OBS_STD);
01561       nobs=cpl_frameset_get_size(obs);
01562    }
01563 
01564    nobs=cpl_frameset_get_size(obs);
01565 
01566    if (nobs < 1) {
01567       return 0;
01568    } else {
01569       pix_scale=cpl_calloc(nobs,sizeof(float));
01570       for(i=0;i<nobs;i++) {
01571          frame=cpl_frameset_get_frame(obs,i);
01572          plist=cpl_propertylist_load(cpl_frame_get_filename(frame),0);
01573          pix_scale[i]=sinfo_pfits_get_pixscale(plist);
01574          cpl_propertylist_delete(plist);
01575       }
01576       if(sinfo_pix_scale_isnot_const(pix_scale,nobs)) {
01577          strehl_sw=1;
01578       }
01579       cpl_free(pix_scale);
01580    }
01581    cpl_frameset_delete(obs);
01582 
01583    return strehl_sw;
01584 
01585 }
01586 
01587 
01588 
01595 double sinfo_get_wave_cent(const char* band)
01596 {
01597    double lam=0.;
01598    if (strcmp(band,"H+K") == 0) {
01599       lam=1.950;
01600    } else if (strcmp(band,"K") == 0) {
01601       lam=2.175;
01602    } else if (strcmp(band,"J") == 0) {
01603       lam=1.225;
01604    } else if (strcmp(band,"H") == 0) {
01605       lam=1.675;
01606    }
01607    return lam;
01608 
01609 }
01610 
01611 
01612 
01621 int sinfo_pix_scale_isnot_const(float* pix_scale, const int n) {
01622    int i=0;
01623    float eps=0.0001;
01624    float ref=pix_scale[0];
01625 
01626    for(i=1;i<n;i++) {
01627       if(fabs(pix_scale[i]-ref) > eps) return 1;
01628    }
01629    return 0;
01630 }
01631 
01632 
01640 const char* sinfo_get_pix_scale(float ps) {
01641 
01642    const char* key_value;
01643    float eps=0.0001;
01644 
01645    if(fabs(ps - 0.025) < eps) {
01646       key_value="0.025";
01647    }
01648    else if (fabs(ps - 0.1) < eps) {
01649       key_value="0.1";
01650    }
01651    else if (fabs(ps - 0.25) < eps) {
01652       key_value="0.25";
01653    }
01654    else if (fabs(ps - 1.0) < eps) {
01655       key_value="pupil";
01656    } else {
01657       sinfo_msg_error("ps=%f. Failed to set pixel scale",ps);
01658       return NULL;
01659    }
01660 
01661    return key_value;
01662 }
01663 
01664 
01680 int  sinfo_get_clean_mean_window(cpl_image* img,
01681                                  int llx, int lly, int urx, int ury,
01682                                  const int kappa, const int nclip,
01683                                  double* local_clean_mean,
01684                                  double* clean_stdev)
01685 {
01686 
01687    double mean=0;
01688    double stdev=0;
01689    double threshold=0;
01690    double lo_cut=0;
01691    double hi_cut=0;
01692    cpl_mask* mask=NULL;
01693    cpl_image* tmp=NULL;
01694    cpl_stats* stats=NULL;
01695    int i=0;
01696 
01697    tmp=cpl_image_extract(img,llx,lly,urx,ury);
01698    cpl_image_accept_all(tmp);
01699    for(i=0;i<nclip;i++) {
01700 
01701 
01702       cpl_stats_delete(stats);
01703       stats = cpl_stats_new_from_image(tmp, CPL_STATS_MEAN | CPL_STATS_STDEV);
01704       mean = cpl_stats_get_mean(stats);
01705       stdev = cpl_stats_get_stdev(stats);
01706 
01707       threshold=kappa*stdev;
01708       lo_cut=mean-threshold;
01709       hi_cut=mean+threshold;
01710       cpl_image_accept_all(tmp);
01711       mask=cpl_mask_threshold_image_create(tmp,lo_cut,hi_cut);
01712 
01713       cpl_mask_not(mask);
01714       cpl_image_reject_from_mask(tmp,mask);
01715       cpl_mask_delete(mask);
01716 
01717 
01718    }
01719    *local_clean_mean=mean;
01720    *clean_stdev=stdev;
01721    cpl_image_delete(tmp);
01722    cpl_stats_delete(stats);
01723 
01724 
01725    return 0;
01726 
01727 
01728 }
01734 int sinfo_check_rec_status(const int val) {
01735    if(cpl_error_get_code() != CPL_ERROR_NONE) {
01736       sinfo_msg_error("error before %d",val);
01737       sinfo_msg_error((char* ) cpl_error_get_message());
01738       sinfo_msg_error((char* ) cpl_error_get_where());
01739       return -1;
01740    }
01741    return 0;
01742 }
01743 
01750 int
01751 sinfo_clean_nan(cpl_image** im)
01752 {
01753    int i=0;
01754    int j=0;
01755    int nx=0;
01756    int ny=0;
01757    float* data=NULL;
01758 
01759    nx=cpl_image_get_size_x(*im);
01760    ny=cpl_image_get_size_y(*im);
01761    data=cpl_image_get_data_float(*im);
01762 
01763    for(j=0;j<ny;j++) {
01764       for(i=0;i<nx;i++) {
01765          if(isnan(data[j*nx+i]) != 0) {
01766             data[j*nx+i] = 0;
01767          }
01768       }
01769    }
01770    return 0;
01771 }
01772 
01782 void
01783 sinfo_add_pro_fits_key(cpl_propertylist * plist,
01784                        char* pro_catg,
01785                        char* file_name,
01786                        char* out_name)
01787 {
01788 
01789    char* date=NULL;
01790    date     = sinfo_get_datetime_iso8601() ;
01791 
01792    cpl_propertylist_insert_after_string(plist, "EXPTIME",
01793                                         KEY_NAME_PIPEFILE, out_name) ;
01794    cpl_propertylist_set_comment(plist, KEY_NAME_PIPEFILE,KEY_HELP_PIPEFILE) ;
01795 
01796    cpl_propertylist_insert_after_string(plist, "EXPTIME",
01797                                         KEY_NAME_HPRO_DID, KEY_VALUE_HPRO_DID) ;
01798    cpl_propertylist_set_comment(plist, KEY_NAME_HPRO_DID,KEY_HELP_HPRO_DID) ;
01799 
01800    cpl_propertylist_insert_after_string(plist, "EXPTIME",
01801                                         KEY_NAME_HPRO_TYPE, "REDUCED") ;
01802    cpl_propertylist_set_comment(plist, KEY_NAME_HPRO_TYPE, KEY_HELP_HPRO_TYPE) ;
01803 
01804    cpl_propertylist_insert_after_string(plist, "EXPTIME",
01805                                         KEY_NAME_HPRO_CATG, pro_catg) ;
01806    cpl_propertylist_set_comment(plist, KEY_NAME_HPRO_CATG,  KEY_HELP_HPRO_CATG);
01807 
01808    cpl_propertylist_insert_after_string(plist, "EXPTIME",
01809                                         KEY_NAME_HPRO_STATUS, "OK") ;
01810    cpl_propertylist_set_comment(plist, KEY_NAME_HPRO_STATUS,KEY_HELP_HPRO_CATG);
01811 
01812    cpl_propertylist_insert_after_string(plist, "EXPTIME",
01813                                         KEY_NAME_HPRO_DATE, date) ;
01814    cpl_propertylist_set_comment(plist, KEY_NAME_HPRO_DATE, KEY_HELP_HPRO_DATE);
01815 
01816    cpl_propertylist_insert_after_string(plist, "EXPTIME",
01817                                         KEY_NAME_HPRO_RECID, file_name) ;
01818    cpl_propertylist_set_comment(plist, KEY_NAME_HPRO_RECID,KEY_HELP_HPRO_RECID);
01819 
01820    cpl_propertylist_insert_after_string(plist, "EXPTIME",
01821                                         KEY_NAME_HPRO_DRSID, PACKAGE_VERSION);
01822    cpl_propertylist_set_comment(plist, KEY_NAME_HPRO_DRSID,KEY_HELP_HPRO_DRSID);
01823 
01824 
01825 }
01826 /*---------------------------------------------------------------------------*/
01833 /*---------------------------------------------------------------------------*/
01834 int  sinfo_compare_tags(
01835    const cpl_frame   *   frame1,
01836    const cpl_frame   *   frame2)
01837 {
01838    char            *   v1 ;
01839    char            *   v2 ;
01840 
01841    /* Test entries */
01842    if (frame1==NULL || frame2==NULL) return -1 ;
01843 
01844    /* Get the tags */
01845    if ((v1 = (char*)cpl_frame_get_tag(frame1)) == NULL) return -1 ;
01846    if ((v2 = (char*)cpl_frame_get_tag(frame2)) == NULL) return -1 ;
01847 
01848    /* Compare the tags */
01849    if (strcmp(v1, v2)) return 0 ;
01850    else return 1 ;
01851 }
01852 
01861 int sinfo_extract_raw_pinhole_frames(cpl_frameset * sof, cpl_frameset** raw)
01862 {
01863    char* tag=NULL;
01864    char* name=NULL;
01865    cpl_frame* frame   = NULL;
01866    int nsof=0;
01867    int i=0;
01868    nsof = cpl_frameset_get_size(sof);
01869    for (i=0 ; i<nsof ; i++) {
01870       frame = cpl_frameset_get_frame(sof,i);
01871       name= (char*) cpl_frame_get_filename(frame);
01872       if(sinfo_is_fits_file(name) == 1) {
01873          /* to go on the file must exist */
01874          if(cpl_frame_get_tag(frame) != NULL) {
01875             /* If the frame has a tag we process it. Else it is an object */
01876             tag= (char*) cpl_frame_get_tag(frame);
01877             if(sinfo_frame_is_pinhole_lamp(tag) == 1) {
01878                cpl_frameset_insert(*raw,frame);
01879             }
01880          }
01881       }
01882    }
01883    return 0;
01884 }
01885 
01886 
01887 int sinfo_get_ins_set(char* band,int* ins_set){
01888 
01889    if (strcmp(band,"H") == 0) {
01890       *ins_set = 0;
01891    }
01892    else if (strcmp(band,"H+K") == 0) {
01893       *ins_set = 1;
01894    }
01895    else if (strcmp(band,"K") == 0) {
01896       *ins_set = 2;
01897    }
01898    else if (strcmp(band,"J") == 0) {
01899       *ins_set = 3;
01900    }
01901    return 0;
01902 
01903 
01904 }
01905 int sinfo_extract_raw_frames(cpl_frameset * sof, cpl_frameset** raw)
01906 {
01907    char* tag=NULL;
01908    char* name=NULL;
01909    cpl_frame* frame   = NULL;
01910    int nsof=0;
01911    int i=0;
01912    nsof = cpl_frameset_get_size(sof);
01913    for (i=0 ; i<nsof ; i++) {
01914       frame = cpl_frameset_get_frame(sof,i);
01915       name= (char*) cpl_frame_get_filename(frame);
01916       if(sinfo_is_fits_file(name) == 1) {
01917          /* to go on the file must exist */
01918          if(cpl_frame_get_tag(frame) != NULL) {
01919             /* If the frame has a tag we process it. Else it is an object */
01920             tag= (char*) cpl_frame_get_tag(frame);
01921             if(sinfo_frame_is_raw(tag) == 1) {
01922                cpl_frameset_insert(*raw,frame);
01923             }
01924          }
01925       }
01926    }
01927    return 0;
01928 }
01929 
01930 int sinfo_remove_qc_frames(cpl_frameset* sof,cpl_frameset** raw)
01931 {
01932    char* tag=NULL;
01933    char* name=NULL;
01934    cpl_frame* frame   = NULL;
01935    int nsof=0;
01936    int i=0;
01937 
01938    nsof = cpl_frameset_get_size(sof);
01939    for (i=0 ; i<nsof ; i++) {
01940       frame = cpl_frameset_get_frame(sof,i);
01941       name= (char*) cpl_frame_get_filename(frame);
01942       /* sinfo_msg("name=%s",name); */
01943       if(sinfo_is_fits_file(name) == 1) {
01944          /* sinfo_msg("\t exist "); */
01945          /* to go on the file must exist */
01946          if(cpl_frame_get_tag(frame) != NULL) {
01947             /* If the frame has a tag we process it. Else it is an object */
01948             tag= (char*) cpl_frame_get_tag(frame);
01949             /* sinfo_msg("\t tag %s\n ",tag); */
01950             if(strstr(tag,"QC") != NULL) {
01951                /* sinfo_msg("remove frame %s\n",name); */
01952                cpl_frameset_erase(*raw,tag);
01953             }
01954          }
01955       } else {
01956          /* sinfo_msg("remove frame\n"); */
01957          cpl_frameset_erase_frame(*raw,frame);
01958       }
01959    }
01960    return 0;
01961 
01962 }
01963 
01964 
01965 int sinfo_contains_frames_kind(cpl_frameset * sof,
01966                                cpl_frameset* raw,
01967                                const char*         type)
01968 {
01969    char* tag=NULL;
01970    char* name=NULL;
01971    cpl_frame* frame   = NULL;
01972    cpl_frame* frame_dup   = NULL;
01973 
01974    int nsof=0;
01975    int i=0;
01976    nsof = cpl_frameset_get_size(sof);
01977    for (i=0 ; i<nsof ; i++) {
01978      frame = cpl_frameset_get_frame(sof,i);
01979       name= (char*) cpl_frame_get_filename(frame);
01980       if(sinfo_is_fits_file(name) == 1) {
01981          /* to go on the file must exist */
01982          if(cpl_frame_get_tag(frame) != NULL) {
01983             /* If the frame has a tag we process it. Else it is an object */
01984             tag= (char*) cpl_frame_get_tag(frame);
01985             /* sinfo_msg("name=%s tag=%s type=%s\n",name,tag,type); */
01986             if(strstr(tag,type) != NULL) {
01987                /* sinfo_msg("Match name=%s tag=%s type=%s\n",name,tag,type); */
01988                frame_dup = cpl_frame_duplicate(frame);
01989                cpl_frameset_insert(raw,frame_dup);
01990                /* sinfo_msg("inserted\n"); */
01991             }
01992          }
01993       }
01994    }
01995    return 0;
01996 }
01997 
01998 
01999 
02000 
02001 int sinfo_is_fibres_on_off(cpl_frameset * sof,
02002                            cpl_frameset* raw)
02003 {
02004    char* tag=NULL;
02005    char* name=NULL;
02006    cpl_frame* frame   = NULL;
02007    cpl_frame* frame_dup   = NULL;
02008 
02009    int nsof=0;
02010    int i=0;
02011    nsof = cpl_frameset_get_size(sof);
02012    for (i=0 ; i<nsof ; i++) {
02013       frame = cpl_frameset_get_frame(sof,i);
02014       name= (char*) cpl_frame_get_filename(frame);
02015       if(sinfo_is_fits_file(name) == 1) {
02016          /* to go on the file must exist */
02017          if(cpl_frame_get_tag(frame) != NULL) {
02018             /* If the frame has a tag we process it. Else it is an object */
02019             tag= (char*) cpl_frame_get_tag(frame);
02020             /* sinfo_msg("name=%s tag=%s type=%s\n",name,tag,type); */
02021             if( strcmp(tag,PRO_FIBRE_NS_STACKED ) == 0) {
02022                /* sinfo_msg("Match name=%s tag=%s type=%s\n",name,tag); */
02023                frame_dup = cpl_frame_duplicate(frame);
02024                cpl_frameset_insert(raw,frame_dup);
02025                /* sinfo_msg("inserted\n"); */
02026             }
02027          }
02028       }
02029    }
02030    return 0;
02031 }
02032 
02033 
02034 int sinfo_contains_frames_type(cpl_frameset * sof,
02035                                cpl_frameset** raw,
02036                                const char*          type)
02037 {
02038    char* tag=NULL;
02039    char* name=NULL;
02040    cpl_frame* frame   = NULL;
02041    cpl_frame* frame_dup   = NULL;
02042    int nsof=0;
02043    int i=0;
02044    nsof = cpl_frameset_get_size(sof);
02045    for (i=0 ; i<nsof ; i++) {
02046       frame = cpl_frameset_get_frame(sof,i);
02047       name= (char*) cpl_frame_get_filename(frame);
02048       if(sinfo_is_fits_file(name) == 1) {
02049          /* to go on the file must exist */
02050          if(cpl_frame_get_tag(frame) != NULL) {
02051             /* If the frame has a tag we process it. Else it is an object */
02052             tag= (char*) cpl_frame_get_tag(frame);
02053             if(strstr(tag,type) != NULL) {
02054                /* sinfo_msg("name=%s tag=%s type=%s\n",name,tag,type); */
02055                frame_dup=cpl_frame_duplicate(frame);
02056                cpl_frameset_insert(*raw,frame_dup);
02057             }
02058          }
02059       }
02060    }
02061    return 0;
02062 }
02063 
02064 int sinfo_extract_raw_frames_type2(cpl_frameset * sof,
02065                                    cpl_frameset** raw,
02066                                    const char*          type)
02067 {
02068 
02069    cpl_frame* frame=NULL;
02070    cpl_frame* frame_dup   = NULL;
02071    frame = cpl_frameset_find(sof,type);
02072    while(frame) {
02073       frame_dup=cpl_frame_duplicate(frame);
02074       cpl_frameset_insert(*raw,frame_dup);
02075       frame = cpl_frameset_find(sof,NULL);
02076    }
02077    return 0;
02078 
02079 }
02080 
02081 
02082 int sinfo_extract_raw_frames_type1(cpl_frameset * sof,
02083                                    cpl_frameset* raw,
02084                                    const char*          type)
02085 {
02086 
02087    cpl_frame* frame=NULL;
02088    cpl_frame* frame_dup   = NULL;
02089    frame = cpl_frameset_find(sof,type);
02090    while(frame) {
02091       frame_dup=cpl_frame_duplicate(frame);
02092       cpl_frameset_insert(raw,frame_dup);
02093       frame = cpl_frameset_find(sof,NULL);
02094    }
02095    return 0;
02096 
02097 }
02098 
02099 int sinfo_extract_raw_frames_type(cpl_frameset * sof,
02100                                   cpl_frameset** raw,
02101                                   const char*          type)
02102 {
02103    char tag[FILE_NAME_SZ];
02104    char name[FILE_NAME_SZ];
02105    cpl_frame* frame   = NULL;
02106    cpl_frame* frame_dup   = NULL;
02107    int nsof=0;
02108    int i=0;
02109    nsof = cpl_frameset_get_size(sof);
02110    for (i=0 ; i<nsof ; i++) {
02111       frame = cpl_frameset_get_frame(sof,i);
02112       strcpy(name, cpl_frame_get_filename(frame));
02113       if(sinfo_is_fits_file(name) == 1) {
02114          /* to go on the file must exist */
02115          if(cpl_frame_get_tag(frame) != NULL) {
02116             /* If the frame has a tag we process it. Else it is an object */
02117             strcpy(tag,cpl_frame_get_tag(frame));
02118             if(strcmp(tag,type) == 0) {
02119                /* sinfo_msg("name=%s tag=%s type=%s\n",name,tag,type); */
02120                frame_dup=cpl_frame_duplicate(frame);
02121 
02122                cpl_frameset_insert(*raw,frame_dup);
02123             }
02124          }
02125       }
02126    }
02127    return 0;
02128 }
02129 
02130 int sinfo_extract_frames_type(cpl_frameset * sof,
02131                               cpl_frameset * raw,
02132                               const char*          type)
02133 {
02134    char* tag=NULL;
02135    char* name=NULL;
02136    cpl_frame* frame   = NULL;
02137    cpl_frame* frame_dup   = NULL;
02138    int nsof=0;
02139    int i=0;
02140    nsof = cpl_frameset_get_size(sof);
02141    for (i=0 ; i<nsof ; i++) {
02142       frame = cpl_frameset_get_frame(sof,i);
02143       name= (char*) cpl_frame_get_filename(frame);
02144       if(sinfo_is_fits_file(name) == 1) {
02145          /* to go on the file must exist */
02146          if(cpl_frame_get_tag(frame) != NULL) {
02147             /* If the frame has a tag we process it. Else it is an object */
02148             tag= (char*) cpl_frame_get_tag(frame);
02149             if(strcmp(tag,type) == 0) {
02150                /* sinfo_msg("name=%s tag=%s type=%s\n",name,tag,type); */
02151                frame_dup=cpl_frame_duplicate(frame);
02152                cpl_frameset_insert(raw,frame_dup);
02153             }
02154          }
02155       }
02156    }
02157    return 0;
02158 }
02159 
02160 
02161 int sinfo_extract_obj_frames(cpl_frameset * sof, cpl_frameset* obj)
02162 {
02163    char* tag=NULL;
02164    char* name=NULL;
02165    cpl_frame* frame   = NULL;
02166    cpl_frame* frame_dup   = NULL;
02167 
02168    int nsof=0;
02169    int i=0;
02170    nsof = cpl_frameset_get_size(sof);
02171    for (i=0 ; i<nsof ; i++) {
02172       frame = cpl_frameset_get_frame(sof,i);
02173       name= (char*) cpl_frame_get_filename(frame);
02174       if(sinfo_is_fits_file(name) ==1) {
02175          /* to go on the file must exist */
02176          if(cpl_frame_get_tag(frame) != NULL) {
02177             /* If the frame has a tag we process it. Else it is an object */
02178             tag= (char*) cpl_frame_get_tag(frame);
02179             if(sinfo_tag_is_obj(tag) == 1) {
02180                frame_dup=cpl_frame_duplicate(frame);
02181                cpl_frameset_insert(obj,frame_dup);
02182             }
02183          }
02184       }
02185    }
02186 
02187    return 0;
02188 }
02189 
02190 
02191 int sinfo_extract_obj_products(cpl_frameset * sof, cpl_frameset* obj)
02192 {
02193    char* tag=NULL;
02194    char* name=NULL;
02195    cpl_frame* frame   = NULL;
02196    cpl_frame* frame_dup   = NULL;
02197 
02198    int nsof=0;
02199    int i=0;
02200    nsof = cpl_frameset_get_size(sof);
02201    for (i=0 ; i<nsof ; i++) {
02202       frame = cpl_frameset_get_frame(sof,i);
02203       name= (char*) cpl_frame_get_filename(frame);
02204       if(sinfo_is_fits_file(name) ==1) {
02205          /* to go on the file must exist */
02206          if(cpl_frame_get_tag(frame) != NULL) {
02207             /* If the frame has a tag we process it. Else it is an object */
02208             tag= (char*) cpl_frame_get_tag(frame);
02209             if(sinfo_tag_is_objpro(tag) == 1) {
02210                frame_dup=cpl_frame_duplicate(frame);
02211                cpl_frameset_insert(obj,frame_dup);
02212             }
02213          }
02214       }
02215    }
02216 
02217    return 0;
02218 }
02219 
02220 int sinfo_extract_on_frames(cpl_frameset * sof, cpl_frameset* on)
02221 {
02222    cpl_frame* frame   = NULL;
02223    cpl_frame* frame_dup   = NULL;
02224 
02225    int nsof=0;
02226    int i=0;
02227    nsof = cpl_frameset_get_size(sof);
02228    for (i=0 ; i<nsof ; i++) {
02229       frame = cpl_frameset_get_frame(sof,i);
02230       if(sinfo_frame_is_on(frame) ==1) {
02231          frame_dup=cpl_frame_duplicate(frame);
02232          cpl_frameset_insert(on,frame_dup);
02233       }
02234    }
02235 
02236    return 0;
02237 }
02238 
02239 int sinfo_extract_sky_frames(cpl_frameset * sof, cpl_frameset* sky)
02240 {
02241    char* tag=NULL;
02242    char* name=NULL;
02243    cpl_frame* frame   = NULL;
02244    cpl_frame* frame_dup   = NULL;
02245    int nsof=0;
02246    int i=0;
02247    nsof = cpl_frameset_get_size(sof);
02248    for (i=0 ; i<nsof ; i++) {
02249       frame = cpl_frameset_get_frame(sof,i);
02250       name= (char*) cpl_frame_get_filename(frame);
02251       if(sinfo_is_fits_file(name) ==1) {
02252          /* to go on the file must exist */
02253          if(cpl_frame_get_tag(frame) != NULL) {
02254             /* If the frame has a tag we process it. Else it is an object */
02255             tag= (char*) cpl_frame_get_tag(frame);
02256             if(sinfo_tag_is_sky(tag) == 1) {
02257                frame_dup=cpl_frame_duplicate(frame);
02258                cpl_frameset_insert(sky,frame_dup);
02259             }
02260          }
02261       }
02262    }
02263 
02264    return 0;
02265 }
02266 
02267 
02268 
02269 int sinfo_extract_off_frames(cpl_frameset * sof, cpl_frameset* off)
02270 {
02271    cpl_frame* frame   = NULL;
02272    cpl_frame* frame_dup   = NULL;
02273    int nsof=0;
02274    int i=0;
02275    nsof = cpl_frameset_get_size(sof);
02276    for (i=0 ; i<nsof ; i++) {
02277       frame = cpl_frameset_get_frame(sof,i);
02278       if(sinfo_frame_is_on(frame)) {
02279          frame_dup=cpl_frame_duplicate(frame);
02280          cpl_frameset_insert(off,frame_dup);
02281       }
02282    }
02283 
02284    return 0;
02285 }
02286 
02287 int sinfo_extract_mst_frames(cpl_frameset * sof, cpl_frameset* cdb)
02288 {
02289    char* tag=NULL;
02290    char* name=NULL;
02291    cpl_frame* frame   = NULL;
02292    cpl_frame* frame_dup   = NULL;
02293    int nsof=0;
02294    int i=0;
02295 
02296    nsof = cpl_frameset_get_size(sof);
02297    for (i=0 ; i<nsof ; i++) {
02298       frame = cpl_frameset_get_frame(sof,i);
02299       name= (char*) cpl_frame_get_filename(frame);
02300       if(sinfo_is_fits_file(name) ==1) {
02301          /* to go on the file must exist */
02302          if(cpl_frame_get_tag(frame) != NULL) {
02303             /* If the frame has a tag we process it. Else it is an object */
02304             tag= (char*) cpl_frame_get_tag(frame);
02305             if(sinfo_frame_is_cdb(tag) == 1) {
02306                frame_dup=cpl_frame_duplicate(frame);
02307                cpl_frameset_insert(cdb,frame_dup);
02308             }
02309          }
02310       }
02311    }
02312 
02313    return 0;
02314 }
02315 
02316 cpl_frameset* sinfo_frameset_join(cpl_frameset* fs1,cpl_frameset* fs2) {
02317 
02318    cpl_frameset* join=NULL;
02319    cpl_frame* frm=NULL;
02320    cpl_frame* frm_dup=NULL;
02321    int i=0;
02322    int n=0;
02323 
02324    join=cpl_frameset_new();
02325 
02326    n=cpl_frameset_get_size(fs1);
02327    for(i=0;i<n; i++) {
02328       frm=cpl_frameset_get_frame(fs1,i);
02329       frm_dup= cpl_frame_duplicate(frm);
02330       cpl_frameset_insert(join,frm_dup);
02331    }
02332 
02333    n=cpl_frameset_get_size(fs2);
02334    for(i=0;i<n; i++) {
02335       frm=cpl_frameset_get_frame(fs2,i);
02336       frm_dup= cpl_frame_duplicate(frm);
02337       cpl_frameset_insert(join,frm_dup);
02338    }
02339 
02340 
02341    return join;
02342 
02343 }
02344 
02345 
02346 int sinfo_extract_stk_frames(cpl_frameset * sof,
02347                              cpl_frameset* res)
02348 {
02349    char* tag=NULL;
02350    char* name=NULL;
02351    cpl_frame* frame   = NULL;
02352    cpl_frame* frame_dup   = NULL;
02353    int nsof=0;
02354    int i=0;
02355 
02356    nsof = cpl_frameset_get_size(sof);
02357    for (i=0 ; i<nsof ; i++) {
02358       frame = cpl_frameset_get_frame(sof,i);
02359       name= (char*) cpl_frame_get_filename(frame);
02360       if(sinfo_is_fits_file(name) ==1) {
02361          /* to go on the file must exist */
02362          if(cpl_frame_get_tag(frame) != NULL) {
02363             /* If the frame has a tag we process it. Else it is an object */
02364             tag= (char*) cpl_frame_get_tag(frame);
02365             if(sinfo_frame_is_stk(tag) == 1) {
02366                frame_dup=cpl_frame_duplicate(frame);
02367                cpl_frameset_insert(res,frame_dup);
02368             }
02369          }
02370       }
02371    }
02372 
02373    return 0;
02374 }
02375 
02376 
02377 int
02378 sinfo_extract_preoptic_frames(cpl_frameset * sof,
02379                               cpl_frameset** res,
02380                               const char* val)
02381 {
02382    char* name=NULL;
02383    cpl_frame* frame   = NULL;
02384    cpl_frame* frame_dup   = NULL;
02385    int nsof=0;
02386    int i=0;
02387 
02388    nsof = cpl_frameset_get_size(sof);
02389    for (i=0 ; i<nsof ; i++) {
02390       frame = cpl_frameset_get_frame(sof,i);
02391       name= (char*) cpl_frame_get_filename(frame);
02392       if(sinfo_is_fits_file(name) ==1) {
02393          if(sinfo_frame_is_preoptic(frame,val) == 1) {
02394             frame_dup=cpl_frame_duplicate(frame);
02395             cpl_frameset_insert(*res,frame_dup);
02396          }
02397       }
02398    }
02399 
02400    return 0;
02401 }
02402 
02403 int sinfo_extract_raw_stack_frames(cpl_frameset * sof, cpl_frameset** pro)
02404 {
02405    char* tag=NULL;
02406    char* name=NULL;
02407    cpl_frame* frame   = NULL;
02408    cpl_frame* frame_dup   = NULL;
02409 
02410    int nsof=0;
02411    int i=0;
02412    nsof = cpl_frameset_get_size(sof);
02413 
02414    for (i=0 ; i<nsof ; i++) {
02415       frame = cpl_frameset_get_frame(sof,i);
02416       name= (char*) cpl_frame_get_filename(frame);
02417       if(sinfo_is_fits_file(name) ==1) {
02418          /* to go on the file must exist */
02419          if(cpl_frame_get_tag(frame) != NULL) {
02420             /* If the frame has a tag we process it. Else it is an object */
02421             tag= (char*) cpl_frame_get_tag(frame);
02422             /* sinfo_msg("tag=%s\n",tag); */
02423             if(sinfo_frame_is_raw_stack(tag) == 1) {
02424                frame_dup   = cpl_frame_duplicate(frame);
02425                cpl_frameset_insert(*pro,frame_dup);
02426             }
02427          }
02428       }
02429    }
02430 
02431    return 0;
02432 }
02433 
02434 
02435 int sinfo_extract_raw_slit_frames(cpl_frameset * sof, cpl_frameset** pro)
02436 {
02437    char* tag=NULL;
02438    char* name=NULL;
02439    cpl_frame* frame   = NULL;
02440    int nsof=0;
02441    int i=0;
02442    nsof = cpl_frameset_get_size(sof);
02443    for (i=0 ; i<nsof ; i++) {
02444       frame = cpl_frameset_get_frame(sof,i);
02445       name= (char*) cpl_frame_get_filename(frame);
02446       if(sinfo_is_fits_file(name) ==1) {
02447          /* to go on the file must exist */
02448          if(cpl_frame_get_tag(frame) != NULL) {
02449             /* If the frame has a tag we process it. Else it is an object */
02450             tag= (char*) cpl_frame_get_tag(frame);
02451             if(sinfo_frame_is_slit_lamp(tag) == 1) {
02452                cpl_frameset_insert(*pro,frame);
02453             }
02454          }
02455       }
02456    }
02457 
02458    return 0;
02459 }
02460 
02461 /*---------------------------------------------------------------------------*/
02467 /*---------------------------------------------------------------------------*/
02468 int sinfo_frame_is_raw(char * tag)
02469 {
02470    /* Test entries */
02471    if (tag == NULL) return -1 ;
02472 
02473    if (!strcmp(tag, RAW_LINEARITY_LAMP)) return 1 ;
02474    if (!strcmp(tag, RAW_DARK)) return 1 ;
02475    if (!strcmp(tag, RAW_PINHOLE_LAMP)) return 1 ;
02476    if (!strcmp(tag, RAW_SLIT_LAMP)) return 1 ;
02477    if (!strcmp(tag, RAW_WAVE_LAMP)) return 1 ;
02478    if (!strcmp(tag, RAW_FLAT_LAMP)) return 1 ;
02479    if (!strcmp(tag, RAW_WAVE_NS)) return 1 ;
02480    if (!strcmp(tag, RAW_FLAT_NS)) return 1 ;
02481    if (!strcmp(tag, RAW_FIBRE_LAMP)) return 1 ;
02482    if (!strcmp(tag, RAW_FIBRE_EW)) return 1 ;
02483    if (!strcmp(tag, RAW_FIBRE_NS)) return 1 ;
02484    if (!strcmp(tag, RAW_FLAT_SKY)) return 1 ;
02485    if (!strcmp(tag, RAW_FLUX_LAMP)) return 1 ;
02486    if (!strcmp(tag, RAW_PSF_CALIBRATOR)) return 1 ;
02487    if (!strcmp(tag, RAW_FOCUS)) return 1 ;
02488 
02489    if (!strcmp(tag, RAW_STD)) return 1 ;
02490    if (!strcmp(tag, RAW_STD_STAR)) return 1 ;
02491    if (!strcmp(tag, RAW_STD_STAR_DITHER)) return 1 ;
02492    if (!strcmp(tag, RAW_SKY_STD)) return 1 ;
02493    if (!strcmp(tag, RAW_SKY_OH)) return 1 ;
02494    if (!strcmp(tag, RAW_SKY_PSF_CALIBRATOR)) return 1 ;
02495 
02496    if (!strcmp(tag, RAW_PUPIL_LAMP)) return 1 ;
02497    if (!strcmp(tag, RAW_OBJECT_JITTER)) return 1 ;
02498    if (!strcmp(tag, RAW_SKY_JITTER)) return 1 ;
02499    if (!strcmp(tag, RAW_OBJECT_NODDING)) return 1 ;
02500    if (!strcmp(tag, RAW_OBJECT_SKYSPIDER)) return 1 ;
02501    if (!strcmp(tag, RAW_SKY_NODDING)) return 1 ;
02502 
02503    if (!strcmp(tag, RAW_FLAT_LAMP_DITHER)) return 1 ;
02504    if (!strcmp(tag, RAW_WAVE_LAMP_DITHER)) return 1 ;
02505    if (!strcmp(tag, RAW_STD_STAR_DITHER)) return 1 ;
02506    if (!strcmp(tag, RAW_OBJECT_NODDING_DITHER)) return 1 ;
02507    if (!strcmp(tag, RAW_OBJECT_SKYSPIDER_DITHER)) return 1 ;
02508    if (!strcmp(tag, RAW_SKY_NODDING_DITHER)) return 1 ;
02509 
02510 
02511    return 0 ;
02512 }
02513 
02514 /*---------------------------------------------------------------------------*/
02520 /*---------------------------------------------------------------------------*/
02521 int sinfo_frame_is_raw_stack(char * tag)
02522 {
02523    /* Test entries */
02524    if (tag == NULL) return -1 ;
02525 
02526 
02527    if (!strcmp(tag, PRO_SKY_DUMMY)) return 1 ;
02528    if (!strcmp(tag, RAW_WAVE_LAMP)) return 1 ;
02529    if (!strcmp(tag, RAW_WAVE_LAMP_DITHER)) return 1 ;
02530    if (!strcmp(tag, RAW_WAVE_NS)) return 1 ;
02531    if (!strcmp(tag, RAW_WAVE_NS_DITHER)) return 1 ;
02532 
02533    if (!strcmp(tag, RAW_FLUX_LAMP)) return 1 ;
02534    if (!strcmp(tag, RAW_FIBRE_NS)) return 1 ;
02535    if (!strcmp(tag, RAW_FIBRE_EW)) return 1 ;
02536 
02537    if (!strcmp(tag, RAW_PSF_CALIBRATOR)) return 1 ;
02538    if (!strcmp(tag, RAW_FIBRE_PSF)) return 1 ;
02539    if (!strcmp(tag, RAW_FIBRE_DARK)) return 1 ;
02540 
02541    if (!strcmp(tag, RAW_FOCUS)) return 1 ;
02542 
02543    if (!strcmp(tag, RAW_PUPIL_LAMP)) return 1 ;
02544    if (!strcmp(tag, RAW_OBJECT_JITTER)) return 1 ;
02545    if (!strcmp(tag, RAW_SKY_JITTER)) return 1 ;
02546    if (!strcmp(tag, RAW_OBJECT_NODDING)) return 1 ;
02547    if (!strcmp(tag, RAW_OBJECT_SKYSPIDER)) return 1 ;
02548    if (!strcmp(tag, RAW_SKY_NODDING)) return 1 ;
02549 
02550    if (!strcmp(tag, RAW_OBJECT_NODDING_DITHER)) return 1 ;
02551    if (!strcmp(tag, RAW_OBJECT_SKYSPIDER_DITHER)) return 1 ;
02552    if (!strcmp(tag, RAW_SKY_NODDING_DITHER)) return 1 ;
02553 
02554 
02555    if (!strcmp(tag, RAW_IMAGE_PRE_OBJECT)) return 1 ;
02556    if (!strcmp(tag, RAW_IMAGE_PRE_SKY)) return 1 ;
02557    if (!strcmp(tag, RAW_STD)) return 1 ;
02558    if (!strcmp(tag, RAW_SKY_STD)) return 1 ;
02559    if (!strcmp(tag, RAW_SKY_OH)) return 1 ;
02560    if (!strcmp(tag, RAW_SKY_PSF_CALIBRATOR)) return 1 ;
02561    if (!strcmp(tag, RAW_STD_STAR)) return 1 ;
02562    if (!strcmp(tag, RAW_SKY)) return 1 ;
02563 
02564    return 0 ;
02565 }
02566 
02567 
02568 /*---------------------------------------------------------------------------*/
02574 /*---------------------------------------------------------------------------*/
02575 int sinfo_frame_is_raw_dark(char * tag)
02576 {
02577    /* Test entries */
02578    if (tag == NULL) return -1 ;
02579 
02580    if (!strcmp(tag, RAW_DARK)) return 1 ;
02581 
02582    return 0 ;
02583 }
02584 
02585 /*---------------------------------------------------------------------------*/
02591 /*---------------------------------------------------------------------------*/
02592 int sinfo_frame_is_slit_lamp(char * tag)
02593 {
02594    /* Test entries */
02595    if (tag == NULL) return -1 ;
02596 
02597    if (!strcmp(tag, RAW_SLIT_LAMP)) return 1 ;
02598 
02599    return 0 ;
02600 }
02601 
02602 
02603 /*---------------------------------------------------------------------------*/
02609 /*---------------------------------------------------------------------------*/
02610 int sinfo_frame_is_pinhole_lamp(char * tag)
02611 {
02612    /* Test entries */
02613    if (tag == NULL) return -1 ;
02614 
02615    if (!strcmp(tag, RAW_PINHOLE_LAMP)) return 1 ;
02616 
02617    return 0 ;
02618 }
02619 
02620 
02621 int sinfo_frame_is_cdb(char * tag)
02622 {
02623    /* Test entries */
02624    if (tag == NULL) return -1 ;
02625    /* For the moment not checked the following:
02626 
02627    PRO_STACKED
02628    PRO_SLIT_ON
02629    PRO_FLUX_LAMP_STACKED
02630    PRO_WAVE_LAMP_STACKED
02631    PRO_PSF_CALIBRATOR_STACKED
02632    PRO_FOCUS_STACKED
02633    PRO_OBJECT_NODDING_STACKED
02634    PRO_OBJECT_SKYSPIDER_STACKED
02635    PRO_SKY_NODDING_STACKED
02636    PRO_STD_NODDING_STACKED
02637    PRO_MASK_CUBE
02638    PRO_PSF
02639    TMP_FOCUS
02640    TMP_FOCUS_ON
02641    TMP_FOCUS_OFF
02642    PRO_FOCUS
02643    PRO_FOCUS_GAUSS
02644    PRO_SPECTRA
02645    PRO_CUBE
02646    PRO_CUBE_COLL
02647    PRO_SLOPEX
02648    PRO_SLOPEY
02649    PRO_MASK_CUBE
02650    PRO_OBJ_CUBE
02651    PRO_BP_COEFF
02652    */
02653 
02654    if (!strcmp(tag, REF_LINE_ARC)) 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    int                 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,MAX_NAME_SIZE-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,MAX_NAME_SIZE-1,"%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,MAX_NAME_SIZE-1,"%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,MAX_NAME_SIZE-1,"%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,MAX_NAME_SIZE-1,"%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 8 Mar 2011 for SINFONI Pipeline Reference Manual by  doxygen 1.6.1