SINFONI Pipeline Reference Manual  2.5.2
sinfo_dfs.c
1 /* $Id: sinfo_dfs.c,v 1.44 2013-09-17 08:11:22 amodigli Exp $
2  *
3  * This file is part of the SINFONI Pipeline
4  * Copyright (C) 2002,2003 European Southern Observatory
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  */
20 
21 /*
22  * $Author: amodigli $
23  * $Date: 2013-09-17 08:11:22 $
24  * $Revision: 1.44 $
25  * $Name: not supported by cvs2svn $
26  */
27 
28 #ifdef HAVE_CONFIG_H
29 # include <config.h>
30 #endif
31 /*----------------------------------------------------------------------------
32  Macros
33  ----------------------------------------------------------------------------*/
34 
35 
36 /*----------------------------------------------------------------------------
37  Private to this module
38  ----------------------------------------------------------------------------*/
39 
40 
41 /*-----------------------------------------------------------------------------
42  Includes
43  ----------------------------------------------------------------------------*/
44 #include "sinfo_dfs.h"
45 #include <cpl.h>
46 #include <math.h>
47 #include "sinfo_error.h"
48 #include "sinfo_utilities.h"
49 #include "sinfo_utils_wrappers.h"
50 #include "sinfo_msg.h"
51 #include "sinfo_globals.h"
52 #include "sinfo_pro_save.h"
53 #include "sinfo_skycor.h"
54 #include "sinfo_file_handling.h"
55 #include <unistd.h>
56 #include <stdio.h>
58 /*---------------------------------------------------------------------------*/
64 /*---------------------------------------------------------------------------*/
65 
66 /* defines */
67 
68 #define FITS_MAGIC_SZ 6
69 #define SINFO_FIT_AMOEBA_NMAX 5000
70 
71 /* function prototypes */
72 /*
73  static int
74  sinfo_stat_rectangle(cpl_image* img,
75  const int kappa,
76  const int nclip,
77  double *mean,
78  double *stdev);
79  */
80 static void
81 sinfo_fit_amoeba_get_psum(int ndim, int mpts, double** p, double* psum);
82 
83 
84 static double
85 sinfo_fit_amotry(double** p,
86  double y[],
87  double psum[],
88  int ndim,
89  double (*funk)(double[]),
90  int ihi,
91  double fac);
92 
93 
94 static double
95 get_chisq(int N, int D,
96  int (*f)(const double x[], const double a[], double *result),
97  const double *a,
98  const double *x,
99  const double *y,
100  const double *sigma);
101 
102 static int get_candidate(const double *a, const int ia[],
103  int M, int N, int D,
104  double lambda,
105  int (*f)(const double x[], const double a[],
106  double *result),
107  int (*dfda)(const double x[], const double a[],
108  double result[]),
109  const double *x,
110  const double *y,
111  const double *sigma,
112  double *partials,
113  cpl_matrix *alpha,
114  cpl_matrix *beta,
115  double *a_da);
116 
117 
118 
119 int
120 sinfo_frame_is_raw_dark(char * tag);
121 
127 /*---------------------------------------------------------------------------*/
134 /*---------------------------------------------------------------------------*/
135 cpl_frameset *
136 sinfo_frameset_extract(const cpl_frameset *frames,
137  const char *tag)
138 {
139  cpl_frameset *subset = NULL;
140  const cpl_frame *f;
141 
142 
143 
144  assure( frames != NULL, CPL_ERROR_ILLEGAL_INPUT, "Null frameset" );
145  assure( tag != NULL, CPL_ERROR_ILLEGAL_INPUT, "Null tag" );
146 
147  subset = cpl_frameset_new();
148 
149  for (f = cpl_frameset_find_const(frames, tag);
150  f != NULL;
151  f = cpl_frameset_find_const(frames, NULL)) {
152 
153  cpl_frameset_insert(subset, cpl_frame_duplicate(f));
154  }
155  cleanup:
156  return subset;
157 }
158 
159 
160 
166 int sinfo_print_rec_status(const int val) {
167  if(cpl_error_get_code() != CPL_ERROR_NONE) {
168  sinfo_msg_error("Recipe status at %d",val);
169  sinfo_msg_error("%s",(const char*) cpl_error_get_message());
170  sinfo_msg_error("%s",(const char*) cpl_error_get_where());
171  return -1;
172  }
173  return 0;
174 }
175 
176 
193 cpl_vector*
194 sinfo_vector_clip(const cpl_vector* vinp,
195  const double kappa,
196  const int n,
197  const int method)
198 {
199  cpl_vector* vout=NULL;
200  cpl_vector* vtmp=NULL;
201  int size=0;
202  int j=0;
203  register int i=0;
204 
205  double mean=0;
206  double median=0;
207  double stdev=0;
208  double* pt=NULL;
209  double* po=NULL;
210 
211  cknull(vinp,"Null input vector");
212  check_nomsg(vout=cpl_vector_duplicate(vinp));
213  check_nomsg(mean=cpl_vector_get_mean(vout));
214  check_nomsg(median=cpl_vector_get_median_const(vout));
215  check_nomsg(stdev=cpl_vector_get_stdev(vout));
216  check_nomsg(pt=cpl_vector_get_data(vtmp));
217 
218  if(method == 0) {
219  /*
220  are rejected
221  values ||val-mean|| > kappa*sigma
222  */
223  for(j=0;j<n;j++) {
224 
225  check_nomsg(cpl_vector_sort(vout,1)); /* sort by increasing data */
226  check_nomsg(po=cpl_vector_get_data(vout));
227 
228  if( (size > 1) && (fabs(po[size-1]-mean) > kappa*stdev)) {
229 
230  size--;
231  check_nomsg(vtmp=cpl_vector_new(size));
232  check_nomsg(pt=cpl_vector_get_data(vtmp));
233  for(i=0;i<size;i++) {
234  pt[i]=po[i];
235  }
236  check_nomsg(cpl_vector_delete(vout));
237  check_nomsg(vout=cpl_vector_duplicate(vtmp));
238  check_nomsg(cpl_vector_delete(vtmp));
239 
240  check_nomsg(mean=cpl_vector_get_mean(vout));
241  check_nomsg(stdev=cpl_vector_get_stdev(vout));
242 
243  }
244 
245  }
246 
247  } else {
248  /*
249  are rejected
250  values ||val-median|| > kappa*sigma
251  */
252 
253 
254  for(j=0;j<n;j++) {
255 
256  check_nomsg(cpl_vector_sort(vout,1)); /* sort by increasing data */
257  check_nomsg(po=cpl_vector_get_data(vout));
258 
259  if( (size > 1) && (fabs(po[size-1]-median) > kappa*stdev)) {
260 
261  size--;
262  check_nomsg(vtmp=cpl_vector_new(size));
263  check_nomsg(pt=cpl_vector_get_data(vtmp));
264  for(i=0;i<size;i++) {
265  pt[i]=po[i];
266  }
267  check_nomsg(cpl_vector_delete(vout));
268  check_nomsg(vout=cpl_vector_duplicate(vtmp));
269  check_nomsg(cpl_vector_delete(vtmp));
270 
271  check_nomsg(median=cpl_vector_get_median_const(vout));
272  check_nomsg(stdev=cpl_vector_get_stdev(vout));
273 
274  }
275 
276  }
277 
278 
279 
280 
281  }
282  return vout;
283  cleanup:
284  return NULL;
285 
286 
287 }
288 
289 
290 
291 /*----------------------------------------------------------------------------*/
325 /*----------------------------------------------------------------------------*/
326 static int
327 get_candidate(const double *a, const int ia[],
328  int M, int N, int D,
329  double lambda,
330  int (*f)(const double x[], const double a[], double *result),
331  int (*dfda)(const double x[], const double a[], double result[]),
332  const double *x,
333  const double *y,
334  const double *sigma,
335  double *partials,
336  cpl_matrix *alpha,
337  cpl_matrix *beta,
338  double *a_da)
339 {
340  int Mfit = 0; /* Number of non-constant fit parameters */
341  cpl_matrix *da; /* Solution of alpha * da = beta */
342  double *alpha_data;
343  double *beta_data;
344  double *da_data;
345  int i, imfit = 0;
346  int j, jmfit = 0;
347  int k = 0;
348 
349  /* For efficiency, don't check input in this static function */
350 
351  Mfit = cpl_matrix_get_nrow(alpha);
352 
353  alpha_data = cpl_matrix_get_data(alpha);
354  beta_data = cpl_matrix_get_data(beta);
355 
356  /* Build alpha, beta:
357  *
358  * alpha[i,j] = sum_{k=1,N} (sigma_k)^-2 * df/da_i * df/da_j *
359  * (1 + delta_ij lambda) ,
360  *
361  * beta[i] = sum_{k=1,N} (sigma_k)^-2 * ( y_k - f(x_k) ) * df/da_i
362  *
363  * where (i,j) loop over the non-constant parameters (0 to Mfit-1),
364  * delta is Kronecker's delta, and all df/da are evaluated in x_k
365  */
366 
367  cpl_matrix_fill(alpha, 0.0);
368  cpl_matrix_fill(beta , 0.0);
369 
370  for (k = 0; k < N; k++)
371  {
372  double sm2 = 0.0; /* (sigma_k)^-2 */
373  double fx_k = 0.0; /* f(x_k) */
374  const double *x_k = &(x[0+k*D]); /* x_k */
375 
376  if (sigma == NULL)
377  {
378  sm2 = 1.0;
379  }
380  else
381  {
382  sm2 = 1.0 / (sigma[k] * sigma[k]);
383  }
384 
385  /* Evaluate f(x_k) */
386  cpl_ensure( f(x_k, a, &fx_k) == 0, CPL_ERROR_ILLEGAL_INPUT, -1);
387 
388  /* Evaluate (all) df/da (x_k) */
389  cpl_ensure( dfda(x_k, a, partials) == 0,
390  CPL_ERROR_ILLEGAL_INPUT, -1);
391 
392  for (i = 0, imfit = 0; i < M; i++)
393  {
394  if (ia[i] != 0)
395  {
396  /* Beta */
397  beta_data[imfit] +=
398  sm2 * (y[k] - fx_k) * partials[i];
399 
400  /* Alpha is symmetrical, so compute
401  only lower-left part */
402  for (j = 0, jmfit = 0; j < i; j++)
403  {
404  if (ia[j] != 0)
405  {
406  alpha_data[jmfit + imfit*Mfit] +=
407  sm2 * partials[i] *
408  partials[j];
409 
410  jmfit += 1;
411  }
412  }
413 
414  /* Alpha, diagonal terms */
415  j = i;
416  jmfit = imfit;
417 
418  alpha_data[jmfit + imfit*Mfit] +=
419  sm2 * partials[i] *
420  partials[j] * (1 + lambda);
421 
422  imfit += 1;
423  }
424  }
425 
426  cpl_ensure( imfit == Mfit, CPL_ERROR_ILLEGAL_INPUT,-1);
427  }
428 
429  /* Create upper-right part of alpha */
430  for (i = 0, imfit = 0; i < M; i++) {
431  if (ia[i] != 0) {
432  for (j = i+1, jmfit = imfit+1; j < M; j++) {
433  if (ia[j] != 0) {
434  alpha_data[jmfit+imfit*Mfit] = alpha_data[imfit+jmfit*Mfit];
435  jmfit += 1;
436  }
437  }
438  cpl_ensure( jmfit == Mfit,CPL_ERROR_ILLEGAL_INPUT,-1 );
439  imfit += 1;
440  }
441  }
442  cpl_ensure( imfit == Mfit, CPL_ERROR_ILLEGAL_INPUT,-1);
443 
444  da = cpl_matrix_solve(alpha, beta);
445 
446  cpl_ensure(da != NULL, cpl_error_get_code(), -1);
447 
448  /* Create a+da vector by adding a and da */
449  da_data = cpl_matrix_get_data(da);
450 
451  for (i = 0, imfit = 0; i < M; i++)
452  {
453  if (ia[i] != 0)
454  {
455  a_da[i] = a[i] + da_data[0 + imfit*1];
456 
457  imfit += 1;
458  }
459  else
460  {
461  a_da[i] = a[i];
462  }
463  }
464 
465  cpl_ensure( imfit == Mfit ,CPL_ERROR_ILLEGAL_INPUT,-1);
466 
467  cpl_matrix_delete(da);
468 
469  return 0;
470 }
471 
472 /*----------------------------------------------------------------------------*/
473 
474 
475 /*----------------------------------------------------------------------------*/
496 /*----------------------------------------------------------------------------*/
497 
498 static double
499 get_chisq(int N, int D,
500  int (*f)(const double x[], const double a[], double *result),
501  const double *a,
502  const double *x,
503  const double *y,
504  const double *sigma)
505 {
506  double chi_sq; /* Result */
507  int i = 0;
508 
509  /* For efficiency, don't check input in this static function */
510  chi_sq = 0.0;
511  for (i = 0; i < N; i++)
512  {
513  double fx_i;
514  double residual; /* Residual in units of uncertainty */
515  const double *x_i = &(x[0+i*D]);
516 
517  /* Evaluate */
518  cpl_ensure( f(x_i,
519  a,
520  &fx_i) == 0, CPL_ERROR_ILLEGAL_INPUT, -1.0);
521 
522  /* Accumulate */
523  if (sigma == NULL)
524  {
525  residual = (fx_i - y[i]);
526  }
527  else
528  {
529  residual = (fx_i - y[i]) / sigma[i];
530  }
531 
532  chi_sq += residual*residual;
533 
534  }
535 
536  return chi_sq;
537 }
538 
539 
540 
541 #ifndef CPL_VECTOR_FIT_MAXITER
542 #define CPL_VECTOR_FIT_MAXITER 1000
543 #endif
544 /*----------------------------------------------------------------------------*/
611 /*----------------------------------------------------------------------------*/
612 cpl_error_code
613 sinfo_fit_lm(const cpl_matrix *x,
614  const cpl_matrix *sigma_x,
615  const cpl_vector *y,
616  const cpl_vector *sigma_y,
617  cpl_vector *a,
618  const int ia[],
619  int (*f)(const double x[],
620  const double a[],
621  double *result),
622  int (*dfda)(const double x[],
623  const double a[],
624  double result[]),
625  double *mse,
626  double *red_chisq,
627  cpl_matrix **covariance)
628 {
629  const double *x_data = NULL; /* Pointer to input data */
630  const double *y_data = NULL; /* Pointer to input data */
631  const double *sigma_data = NULL; /* Pointer to input data */
632  int N = 0; /* Number of data points */
633  int D = 0; /* Dimension of x-points */
634  int M = 0; /* Number of fit parameters */
635  int Mfit = 0; /* Number of non-constant fit
636  parameters */
637 
638  double lambda = 0.0; /* Lambda in L-M algorithm */
639  double MAXLAMBDA = 10e40; /* Parameter to control the graceful exit
640  if steepest descent unexpectedly fails */
641  double chi_sq = 0.0; /* Current chi^2 */
642  int count = 0; /* Number of successive small improvements
643  in chi^2 */
644  int iterations = 0;
645 
646  cpl_matrix *alpha = NULL; /* The MxM ~curvature matrix used in L-M */
647  cpl_matrix *beta = NULL; /* Mx1 matrix = -.5 grad(chi^2) */
648  double *a_data = NULL; /* Parameters, a */
649  double *a_da = NULL; /* Candidate position a+da */
650  double *part = NULL; /* The partial derivatives df/da */
651  int *ia_local = NULL; /* non-NULL version of ia */
652 
653  /* If covariance computation is requested, then either
654  * return the covariance matrix or return NULL.
655  */
656  if (covariance != NULL) *covariance = NULL;
657 
658  /* Validate input */
659  cpl_ensure_code(x != NULL, CPL_ERROR_NULL_INPUT);
660  cpl_ensure_code(sigma_x == NULL, CPL_ERROR_UNSUPPORTED_MODE);
661  cpl_ensure_code(y != NULL, CPL_ERROR_NULL_INPUT);
662  cpl_ensure_code(a != NULL, CPL_ERROR_NULL_INPUT);
663  /* ia may be NULL */
664  cpl_ensure_code(f != NULL, CPL_ERROR_NULL_INPUT);
665  cpl_ensure_code(dfda != NULL, CPL_ERROR_NULL_INPUT);
666 
667  /* Chi^2 and covariance computations require sigmas to be known */
668  cpl_ensure_code( sigma_y != NULL ||
669  (red_chisq == NULL && covariance == NULL),
670  CPL_ERROR_INCOMPATIBLE_INPUT);
671 
672  D = cpl_matrix_get_ncol(x);
673  N = cpl_matrix_get_nrow(x);
674  M = cpl_vector_get_size(a);
675  cpl_ensure_code(N > 0 && D > 0 && M > 0, CPL_ERROR_ILLEGAL_INPUT);
676 
677  cpl_ensure_code( cpl_vector_get_size(y) == N,
678  CPL_ERROR_INCOMPATIBLE_INPUT);
679 
680  x_data = cpl_matrix_get_data_const(x);
681  y_data = cpl_vector_get_data_const(y);
682  a_data = cpl_vector_get_data(a);
683 
684  if (sigma_y != NULL)
685  {
686  cpl_ensure_code( cpl_vector_get_size(sigma_y) == N,
687  CPL_ERROR_INCOMPATIBLE_INPUT);
688  /* Sigmas must be positive */
689  cpl_ensure_code( cpl_vector_get_min (sigma_y) > 0,
690  CPL_ERROR_ILLEGAL_INPUT);
691  sigma_data = cpl_vector_get_data_const(sigma_y);
692  }
693 
694  ia_local = cpl_malloc(M * sizeof(int));
695  cpl_ensure_code(ia_local != NULL, CPL_ERROR_ILLEGAL_OUTPUT);
696 
697  /* Count non-constant fit parameters, copy ia */
698  if (ia != NULL)
699  {
700  int i;
701 
702  Mfit = 0;
703  for (i = 0; i < M; i++)
704  {
705  ia_local[i] = ia[i];
706 
707  if (ia[i] != 0)
708  {
709  Mfit += 1;
710  }
711  }
712 
713  if (! (Mfit > 0))
714  {
715  cpl_free(ia_local);
716  cpl_ensure_code( CPL_FALSE,
717  CPL_ERROR_ILLEGAL_INPUT);
718  }
719  }
720  else
721  {
722  /* All parameters participate */
723  int i;
724 
725  Mfit = M;
726 
727  for (i = 0; i < M; i++)
728  {
729  ia_local[i] = 1;
730  }
731  }
732 
733  /* To compute reduced chi^2, we need N > Mfit */
734  if (! ( red_chisq == NULL || N > Mfit ) )
735  {
736  cpl_free(ia_local);
737  cpl_ensure_code(
738  CPL_FALSE,
739  CPL_ERROR_ILLEGAL_INPUT);
740  }
741 
742  /* Create alpha, beta, a_da, part work space */
743  alpha = cpl_matrix_new(Mfit, Mfit);
744  if (alpha == NULL)
745  {
746  cpl_free(ia_local);
747  cpl_ensure_code(
748  CPL_FALSE,
749  CPL_ERROR_ILLEGAL_OUTPUT);
750  }
751 
752  beta = cpl_matrix_new(Mfit, 1);
753  if (beta == NULL)
754  {
755  cpl_free(ia_local);
756  cpl_matrix_delete(alpha);
757  cpl_ensure_code(
758  CPL_FALSE,
759  CPL_ERROR_ILLEGAL_OUTPUT);
760  }
761 
762  a_da = cpl_malloc(M * sizeof(double));
763  if (a_da == NULL)
764  {
765  cpl_free(ia_local);
766  cpl_matrix_delete(alpha);
767  cpl_matrix_delete(beta);
768  cpl_ensure_code(
769  CPL_FALSE,
770  CPL_ERROR_ILLEGAL_OUTPUT);
771  }
772 
773  part = cpl_malloc(M * sizeof(double));
774  if (part == NULL)
775  {
776  cpl_free(ia_local);
777  cpl_matrix_delete(alpha);
778  cpl_matrix_delete(beta);
779  cpl_free(a_da);
780  cpl_ensure_code(
781  CPL_FALSE,
782  CPL_ERROR_ILLEGAL_OUTPUT);
783  }
784 
785  /* Initialize loop variables */
786  lambda = 0.001;
787  count = 0;
788  iterations = 0;
789  if( (chi_sq = get_chisq(N, D, f, a_data, x_data, y_data, sigma_data)) < 0)
790  {
791  cpl_free(ia_local);
792  cpl_matrix_delete(alpha);
793  cpl_matrix_delete(beta);
794  cpl_free(a_da);
795  cpl_free(part);
796  cpl_ensure_code(
797  CPL_FALSE,
798  cpl_error_get_code());
799  }
800 
801  /* uves_msg_debug("Initial chi^2 = %f", chi_sq); */
802 
803  /* Iterate until chi^2 didn't improve substantially many (say, 5)
804  times in a row */
805  while (count < 5 &&
806  lambda < MAXLAMBDA &&
807  iterations < CPL_VECTOR_FIT_MAXITER)
808  {
809  /* In each iteration lambda increases, or chi^2 decreases or
810  count increases. Because chi^2 is bounded from below
811  (and lambda and count from above), the loop will terminate */
812 
813  double chi_sq_candidate = 0.0;
814  int returncode = 0;
815 
816  /* Get candidate position in parameter space = a+da,
817  * where alpha * da = beta .
818  * Increase lambda until alpha is non-singular
819  */
820 
821  while( (returncode = get_candidate(a_data, ia_local,
822  M, N, D,
823  lambda, f, dfda,
824  x_data, y_data, sigma_data,
825  part, alpha, beta, a_da)
826  ) != 0
827  && cpl_error_get_code() == CPL_ERROR_SINGULAR_MATRIX
828  && lambda < MAXLAMBDA)
829  {
830  /* uves_msg_debug("Singular matrix. lambda = %e", lambda); */
831  cpl_error_reset();
832  lambda *= 9.0;
833  }
834 
835  /* Set error if lambda diverged */
836  if ( !( lambda < MAXLAMBDA ) )
837  {
838  cpl_free(ia_local);
839  cpl_matrix_delete(alpha);
840  cpl_matrix_delete(beta);
841  cpl_free(a_da);
842  cpl_free(part);
843  cpl_ensure_code(
844  CPL_FALSE,
845  CPL_ERROR_CONTINUE);
846  }
847 
848  if (returncode != 0)
849  {
850  cpl_free(ia_local);
851  cpl_matrix_delete(alpha);
852  cpl_matrix_delete(beta);
853  cpl_free(a_da);
854  cpl_free(part);
855  cpl_ensure_code(
856  CPL_FALSE,
857  cpl_error_get_code());
858  }
859 
860  /* Get chi^2(a+da) */
861  if ((chi_sq_candidate = get_chisq(N, D, f, a_da,
862  x_data, y_data, sigma_data)) < 0)
863  {
864  cpl_free(ia_local);
865  cpl_matrix_delete(alpha);
866  cpl_matrix_delete(beta);
867  cpl_free(a_da);
868  cpl_free(part);
869  cpl_ensure_code(
870  CPL_FALSE,
871  cpl_error_get_code());
872  }
873 
874  /* uves_msg_debug("Chi^2 = %f Candidate = %f Lambda = %e",
875  chi_sq, chi_sq_candidate, lambda); */
876 
877  if (chi_sq_candidate > chi_sq)
878  {
879  /* Move towards steepest descent */
880  lambda *= 9.0;
881  }
882  else
883  {
884  /* Move towards Newton's algorithm */
885  lambda /= 10.0;
886 
887  /* Count the number of successive improvements in chi^2 of
888  less than 0.01 relative */
889  if ( chi_sq == 0 ||
890  (chi_sq - chi_sq_candidate)/chi_sq < .01)
891  {
892  count += 1;
893  }
894  else
895  {
896  /* Chi^2 improved by a significant amount,
897  reset counter */
898  count = 0;
899  }
900 
901  /* chi^2 improved, update a and chi^2 */
902  {
903  int i;
904  for (i = 0; i < M; i++) a_data[i] = a_da[i];
905  }
906  chi_sq = chi_sq_candidate;
907  }
908  iterations++;
909  }
910 
911  /* Set error if we didn't converge */
912  if ( !( lambda < MAXLAMBDA && iterations < CPL_VECTOR_FIT_MAXITER ) )
913  {
914  cpl_free(ia_local);
915  cpl_matrix_delete(alpha);
916  cpl_matrix_delete(beta);
917  cpl_free(a_da);
918  cpl_free(part);
919  cpl_ensure_code(
920  CPL_FALSE,
921  CPL_ERROR_CONTINUE);
922  }
923 
924  /* Compute mse if requested */
925  if (mse != NULL)
926  {
927  int i;
928 
929  *mse = 0.0;
930 
931  for(i = 0; i < N; i++)
932  {
933  double fx_i = 0.0;
934  double residual = 0.0;
935 
936  /* Evaluate f(x_i) at the best fit parameters */
937  if( f(&(x_data[i*D]),
938  a_data,
939  &fx_i) != 0)
940  {
941  cpl_free(ia_local);
942  cpl_matrix_delete(alpha);
943  cpl_matrix_delete(beta);
944  cpl_free(a_da);
945  cpl_free(part);
946  cpl_ensure_code(
947  CPL_FALSE,
948  CPL_ERROR_ILLEGAL_INPUT);
949  }
950 
951  residual = y_data[i] - fx_i;
952  *mse += residual * residual;
953  }
954  *mse /= N;
955  }
956 
957  /* Compute reduced chi^2 if requested */
958  if (red_chisq != NULL)
959  {
960  /* We already know the optimal chi^2 (and that N > Mfit)*/
961  *red_chisq = chi_sq / (N-Mfit);
962  }
963 
964  /* Compute covariance matrix if requested
965  * cov = alpha(lambda=0)^-1
966  */
967  if (covariance != NULL)
968  {
969  cpl_matrix *cov;
970 
971  if( get_candidate(a_data, ia_local,
972  M, N, D, 0.0, f, dfda,
973  x_data, y_data, sigma_data,
974  part, alpha, beta, a_da)
975  != 0)
976  {
977  cpl_free(ia_local);
978  cpl_matrix_delete(alpha);
979  cpl_matrix_delete(beta);
980  cpl_free(a_da);
981  cpl_free(part);
982  cpl_ensure_code(
983  CPL_FALSE,
984  cpl_error_get_code());
985  }
986 
987  cov = cpl_matrix_invert_create(alpha);
988  if (cov == NULL)
989  {
990  cpl_free(ia_local);
991  cpl_matrix_delete(alpha);
992  cpl_matrix_delete(beta);
993  cpl_free(a_da);
994  cpl_free(part);
995  cpl_ensure_code(
996  CPL_FALSE,
997  cpl_error_get_code());
998  }
999 
1000  /* Make sure that variances are positive */
1001  {
1002  int i;
1003  for (i = 0; i < Mfit; i++)
1004  {
1005  if ( !(cpl_matrix_get(cov, i, i) > 0) )
1006  {
1007  cpl_free(ia_local);
1008  cpl_matrix_delete(alpha);
1009  cpl_matrix_delete(beta);
1010  cpl_free(a_da);
1011  cpl_free(part);
1012  cpl_matrix_delete(cov);
1013  *covariance = NULL;
1014  cpl_ensure_code(
1015  CPL_FALSE,
1016  CPL_ERROR_SINGULAR_MATRIX);
1017  }
1018  }
1019  }
1020 
1021  /* Expand covariance matrix from Mfit x Mfit
1022  to M x M. Set rows/columns corresponding to fixed
1023  parameters to zero */
1024 
1025  *covariance = cpl_matrix_new(M, M);
1026  if (*covariance == NULL)
1027  {
1028  cpl_free(ia_local);
1029  cpl_matrix_delete(alpha);
1030  cpl_matrix_delete(beta);
1031  cpl_free(a_da);
1032  cpl_free(part);
1033  cpl_matrix_delete(cov);
1034  cpl_ensure_code(
1035  CPL_FALSE,
1036  CPL_ERROR_ILLEGAL_OUTPUT);
1037  }
1038 
1039  {
1040  int j, jmfit;
1041 
1042  for (j = 0, jmfit = 0; j < M; j++)
1043  if (ia_local[j] != 0)
1044  {
1045  int i, imfit;
1046 
1047  for (i = 0, imfit = 0; i < M; i++)
1048  if (ia_local[i] != 0)
1049  {
1050  cpl_matrix_set(*covariance, i, j,
1051  cpl_matrix_get(
1052  cov, imfit, jmfit));
1053  imfit += 1;
1054  }
1055 
1056  cpl_ensure( imfit == Mfit, CPL_ERROR_ILLEGAL_INPUT,-1);
1057 
1058  jmfit += 1;
1059  }
1060 
1061  cpl_ensure( jmfit == Mfit, CPL_ERROR_ILLEGAL_INPUT,-1 );
1062  }
1063 
1064  cpl_matrix_delete(cov);
1065  }
1066 
1067  cpl_free(ia_local);
1068  cpl_matrix_delete(alpha);
1069  cpl_matrix_delete(beta);
1070  cpl_free(a_da);
1071  cpl_free(part);
1072 
1073  return CPL_ERROR_NONE;
1074 }
1075 
1076 
1077 
1078 
1079 
1080 
1081 
1082 
1083 
1084 
1085 
1086 
1087 
1088 
1089 
1090 /*-------------------------------------------------------------------------*/
1091 
1103 static void
1104 sinfo_fit_amoeba_get_psum(int ndim, int mpts, double** p, double* psum)
1105 {
1106  int i=0;
1107  int j=0;
1108  double sum=0;
1109  for (j=0;j<ndim;j++) {
1110  for (sum=0.0,i=0;i<mpts;i++) {
1111  sum += p[i][j];
1112  }
1113  psum[j]=sum;
1114  }
1115 
1116 }
1117 
1118 
1119 #define SINFO_FIT_AMOEBA_SWAP(a,b) {swap=(a);(a)=(b);(b)=swap;}
1120 
1121 
1122 
1149 void
1150 sinfo_fit_amoeba(
1151  double**p,
1152  double y[],
1153  int ndim,
1154  double ftol,
1155  double (*funk)(double[]),
1156  int* nfunk)
1157 {
1158 
1159 
1160  int i=0;
1161  int ihi=0;
1162  int ilo=0;
1163  int inhi=0;
1164  int j=0;
1165  int mpts=ndim+1;
1166  double rtol=0;
1167  double swap=0;
1168  double ysave=0;
1169  double ytry=0;
1170  cpl_vector* sum=NULL;
1171  double* psum=NULL;
1172 
1173  sum=cpl_vector_new(ndim);
1174  psum=cpl_vector_get_data(sum);
1175  *nfunk=0;
1176 
1177  sinfo_fit_amoeba_get_psum(ndim,mpts,p,psum);
1178 
1179  for(;;) {
1180  ilo=0;
1181  /*
1182  First we must determine which point is the highest (worst),
1183  next-highest, and lowest (best), by looping over the points
1184  in the simplex
1185  */
1186  ihi=y[0]>y[1] ? (inhi=1,0) : (inhi=0,1);
1187 
1188  for (i=0;i< mpts;i++) {
1189  if (y[i] <= y[ilo]) ilo=i;
1190  if (y[i] > y[ihi]) {
1191  inhi=ihi;
1192  ihi=i;
1193  } else if (y[i] > y[inhi] && i != ihi) inhi=i;
1194  }
1195  rtol=2.0*fabs(y[ihi]-y[ilo])/(fabs(y[ihi])+fabs(y[ilo]));
1196 
1197  /*
1198  compute the fractional range from highest to lowest and return if
1199  satisfactory
1200  */
1201  if(rtol < ftol) { // if returning, but best point and value is in slot 1
1202  SINFO_FIT_AMOEBA_SWAP(y[0],y[ilo]);
1203  for (i=0;i<ndim;i++) SINFO_FIT_AMOEBA_SWAP(p[1][i],p[ilo][i]);
1204  break;
1205  }
1206  if (*nfunk >= SINFO_FIT_AMOEBA_NMAX) {
1207  sinfo_msg_error("NMAX exceeded");
1208  SINFO_FIT_AMOEBA_SWAP(y[0],y[ilo]);
1209  for (i=0;i<ndim;i++) SINFO_FIT_AMOEBA_SWAP(p[1][i],p[ilo][i]);
1210  for (i=0;i<ndim;i++) {
1211  sinfo_msg("p[1][i]=%g p[ilo][i]=%g ilo=%d",p[1][i],p[ilo][i],ilo);
1212  }
1213  assure(*nfunk >= SINFO_FIT_AMOEBA_NMAX,CPL_ERROR_UNSPECIFIED,
1214  "NMAX exceeded");
1215  break;
1216 
1217  }
1218  *nfunk +=2;
1219  /*
1220  Begin a new iteration. First extrapolate by a Factor -1 through the face
1221  of the simplex across the high point, i.e. reflect the simplex from the
1222  high point
1223  */
1224  ytry=sinfo_fit_amotry(p,y,psum,ndim,funk,ihi,-1.0);
1225  if(ytry <= y[ilo]) {
1226  /*
1227  Gives a result better than the best point, so try an additional
1228  extrapolation by a factor 2
1229  */
1230  ytry=sinfo_fit_amotry(p,y,psum,ndim,funk,ihi,2.0);
1231  } else if (ytry >= y[inhi]) {
1232 
1233  /*
1234  The reflected point is worse than the second highest, so look for an
1235  intermediate lower point, i.e. do a one-dimensional contraction
1236  */
1237  ysave=y[ihi];
1238  ytry=sinfo_fit_amotry(p,y,psum,ndim,funk,ihi,0.5);
1239  if(ytry >= ysave) {
1240  /*
1241  Can't seem to get rid of that high point.
1242  Better contract around the lowest (best) point
1243  */
1244  for(i=0;i<mpts;i++) {
1245  if(i != ilo) {
1246  for( j=0;j<ndim;j++) {
1247  p[i][j]=psum[j]=0.5*(p[i][j]+p[ilo][j]);
1248  }
1249  y[i]=(*funk)(psum);
1250  }
1251  }
1252  *nfunk += ndim; /* Keep track of function evaluations */
1253  sinfo_fit_amoeba_get_psum(ndim,mpts,p,psum);/* Recomputes psum */
1254  }
1255  } else {
1256  --(*nfunk);
1257  /* Go back for the test of doneness and the next iteration */
1258  }
1259  }
1260  cleanup:
1261  cpl_vector_delete(sum);
1262 }
1263 
1264 
1265 static double
1266 sinfo_fit_amotry(double** p, double y[], double psum[], int ndim,
1267  double (*funk)(double[]),int ihi, double fac)
1268 {
1269  int j;
1270  double fac1=0;
1271  double fac2=0;
1272  double ytry=0;
1273  cpl_vector * vtry=NULL;
1274  double *ptry=NULL;
1275 
1276  vtry=cpl_vector_new(ndim);
1277  ptry=cpl_vector_get_data(vtry);
1278 
1279  fac1=(1.0-fac)/ndim;
1280  fac2=fac1-fac;
1281 
1282  for (j=0;j<ndim;j++) {
1283  ptry[j]=psum[j]*fac1-p[ihi][j]*fac2;
1284  }
1285  ytry=(*funk)(ptry);
1286  if (ytry < y[ihi]) {
1287  y[ihi]=ytry;
1288  for (j=0;j<ndim;j++) {
1289  psum[j] += ptry[j]-p[ihi][j];
1290  p[ihi][j]=ptry[j];
1291  }
1292  }
1293  sinfo_free_my_vector(&vtry);
1294  return ytry;
1295 }
1296 
1297 /*-------------------------------------------------------------------------*/
1298 /*-------------------------------------------------------------------------*/
1308 /*--------------------------------------------------------------------------*/
1309 
1310 int sinfo_vector_dindgen(cpl_vector** v)
1311 {
1312 
1313  int sz=0;
1314  int i=0;
1315 
1316  cknull(*v,"Null input vector");
1317  check(sz=cpl_vector_get_size(*v),"Getting size of a vector");
1318 
1319  for(i=0;i<sz;i++) {
1320  cpl_vector_set(*v,i,(double)i);
1321  }
1322 
1323  return 0;
1324  cleanup:
1325  return -1;
1326 
1327 }
1328 
1329 /*-------------------------------------------------------------------------*/
1340 /*--------------------------------------------------------------------------*/
1341 
1342 int sinfo_is_fits_file(const char *filename)
1343 {
1344  FILE *fp ;
1345  char *magic ;
1346  int isfits ;
1347 
1348  if ((fp = fopen(filename, "r"))==NULL) {
1349  sinfo_msg_error("cannot open file [%s]", filename) ;
1350  return -1 ;
1351  }
1352 
1353  magic = cpl_calloc(FITS_MAGIC_SZ+1, sizeof(char)) ;
1354  (void)fread(magic, 1, FITS_MAGIC_SZ, fp) ;
1355  (void)fclose(fp) ;
1356  magic[FITS_MAGIC_SZ] = (char)0 ;
1357  if (strstr(magic, "SIMPLE")!=NULL)
1358  isfits = 1 ;
1359  else
1360  isfits = 0 ;
1361  cpl_free(magic) ;
1362  return isfits ;
1363 }
1364 
1365 /*---------------------------------------------------------------------------*/
1371 /*---------------------------------------------------------------------------*/
1372 cpl_error_code
1373 sinfo_table_correl(cpl_table * t1, cpl_table* t2, cpl_table* range,double* xcor)
1374 {
1375 
1376  double wsr=0;
1377  double wer=0;
1378  int nr=0;
1379  int i=0;
1380  int status=0;
1381  int nrows=0;
1382  double mean=0;
1383  double prod=0;
1384 
1385  cpl_table* tmp_t1=NULL;
1386  cpl_table* tmp_t2=NULL;
1387 
1388  check_nomsg(nr=cpl_table_get_nrow(range));
1389  for(i=0;i<nr;i++) {
1390  wsr=cpl_table_get_double(range,"WSTART",i,&status);
1391  wer=cpl_table_get_double(range,"WEND",i,&status);
1392  cpl_table_and_selected_double(t1,"WAVE",CPL_NOT_LESS_THAN,wsr);
1393  cpl_table_and_selected_double(t1,"WAVE",CPL_NOT_GREATER_THAN,wer);
1394  tmp_t1=cpl_table_extract_selected(t1);
1395  cpl_table_and_selected_double(t2,"WAVE",CPL_NOT_LESS_THAN,wsr);
1396  cpl_table_and_selected_double(t2,"WAVE",CPL_NOT_GREATER_THAN,wer);
1397  tmp_t2=cpl_table_extract_selected(t2);
1398  cpl_table_duplicate_column(tmp_t1,"INT1",tmp_t1,"INT");
1399  cpl_table_duplicate_column(tmp_t1,"INT2",tmp_t2,"INT");
1400  cpl_table_multiply_columns(tmp_t1,"INT1","INT2");
1401  mean=cpl_table_get_column_mean(tmp_t1,"INT1");
1402  nrows=cpl_table_get_nrow(tmp_t1);
1403  prod=mean*nrows;
1404  *xcor+=prod;
1405  }
1406 
1407  cleanup:
1408  return cpl_error_get_code();
1409 }
1415 /*----------------------------------------------------------------------------*/
1416 cpl_error_code
1417 sinfo_frameset_merge(cpl_frameset * set1, cpl_frameset* set2)
1418 {
1419 
1420  cpl_frame* frm_tmp=NULL;
1421  cpl_frame* frm_dup=NULL;
1422 
1423  passure(set1 != NULL, "Wrong input set");
1424 
1425  check_nomsg(frm_tmp = cpl_frameset_get_first(set2));
1426  while (frm_tmp != NULL)
1427  {
1428  frm_dup=cpl_frame_duplicate(frm_tmp);
1429  cpl_frameset_insert(set1,frm_dup);
1430  frm_tmp = cpl_frameset_get_next(set2);
1431  }
1432 
1433  cleanup:
1434  return cpl_error_get_code();
1435 }
1436 
1437 /*----------------------------------------------------------------------------*/
1445 /*----------------------------------------------------------------------------*/
1446 
1447 cpl_error_code
1448 sinfo_extract_frames_group_type(const cpl_frameset * set,
1449  cpl_frameset** ext,
1450  cpl_frame_group type)
1451 {
1452  const cpl_frame* frm_tmp=NULL;
1453  cpl_frame* frm_dup=NULL;
1454  cpl_frame_group g;
1455 
1456  check_nomsg(*ext = cpl_frameset_new());
1457  check_nomsg(frm_tmp = cpl_frameset_get_first_const(set));
1458  while (frm_tmp != NULL)
1459  {
1460  g=cpl_frame_get_group(frm_tmp);
1461  if(g == type) {
1462  frm_dup=cpl_frame_duplicate(frm_tmp);
1463  cpl_frameset_insert(*ext,frm_dup);
1464  sinfo_msg_debug("group %d insert file %s ",
1465  type,cpl_frame_get_filename(frm_dup));
1466  }
1467  frm_tmp = cpl_frameset_get_next_const(set);
1468  }
1469 
1470  cleanup:
1471  return cpl_error_get_code();
1472 }
1473 
1474 
1475 
1476 
1487 int
1488 sinfo_get_pupil_shift(cpl_imagelist* iml,const int n,cpl_table** qclog_tbl)
1489 {
1490  cpl_size max_ima_x=0;
1491  cpl_size max_ima_y=0;
1492  int nx=0;
1493  int ny=0;
1494 
1495  double xshift=0;
1496  double yshift=0;
1497 
1498  double max_ima_cx=0;
1499  double max_ima_cy=0;
1500 
1501  cpl_image* img=NULL;
1502  cpl_image* img_dup=NULL;
1503 
1504  char key_name[FILE_NAME_SZ];
1505 
1506  img=cpl_imagelist_collapse_median_create(iml);
1507  nx=cpl_image_get_size_x(img);
1508  ny=cpl_image_get_size_y(img);
1509 
1510  img_dup=cpl_image_duplicate(img);
1511  sinfo_clean_nan(&img_dup);
1512  cpl_image_get_maxpos(img_dup,&max_ima_x,&max_ima_y);
1513  max_ima_cx=cpl_image_get_centroid_x_window(img_dup,1,1,nx,ny);
1514  max_ima_cy=cpl_image_get_centroid_y_window(img_dup,1,1,nx,ny);
1515 
1516  cpl_image_delete(img_dup);
1517 
1518 
1519  xshift=max_ima_cx-(double)nx/2;
1520  yshift=max_ima_cy-(double)ny/2;
1521 
1522  snprintf(key_name,sizeof(key_name),"%s%d%s","QC PUPIL",n," SHIFTX");
1523  sinfo_qclog_add_double(*qclog_tbl,key_name,xshift,
1524  "X shift centroid - center image","%f");
1525 
1526  snprintf(key_name,sizeof(key_name),"%s%d%s","QC PUPIL",n," SHIFTY");
1527  sinfo_qclog_add_double(*qclog_tbl,key_name,yshift,
1528  "Y shift centroid - center image","%f");
1529  cpl_image_delete(img);
1530 
1531  return 0;
1532 }
1533 
1534 
1535 
1542 int sinfo_get_strehl_type(cpl_frameset* sof)
1543 {
1544  int strehl_sw=0;
1545  int nobs=0;
1546  int i=0;
1547  cpl_frameset* obs=NULL;
1548 
1549  cpl_frame* frame=NULL;
1550  float* pix_scale=NULL;
1551  cpl_propertylist* plist=NULL;
1552 
1553  obs = cpl_frameset_new();
1554 
1555  sinfo_contains_frames_kind(sof,obs,PRO_OBS_PSF);
1556 
1557  nobs=cpl_frameset_get_size(obs);
1558  if (nobs < 1) {
1559  sinfo_contains_frames_kind(sof,obs,PRO_OBS_STD);
1560  nobs=cpl_frameset_get_size(obs);
1561  }
1562 
1563  nobs=cpl_frameset_get_size(obs);
1564 
1565  if (nobs < 1) {
1566  return 0;
1567  } else {
1568  pix_scale=cpl_calloc(nobs,sizeof(float));
1569  for(i=0;i<nobs;i++) {
1570  frame=cpl_frameset_get_frame(obs,i);
1571  plist=cpl_propertylist_load(cpl_frame_get_filename(frame),0);
1572  pix_scale[i]=sinfo_pfits_get_pixscale(plist);
1573  cpl_propertylist_delete(plist);
1574  }
1575  if(sinfo_pix_scale_isnot_const(pix_scale,nobs)) {
1576  strehl_sw=1;
1577  }
1578  cpl_free(pix_scale);
1579  }
1580  cpl_frameset_delete(obs);
1581 
1582  return strehl_sw;
1583 
1584 }
1585 
1586 
1587 
1594 double sinfo_get_wave_cent(const char* band)
1595 {
1596  double lam=0.;
1597  if (strcmp(band,"H+K") == 0) {
1598  lam=1.950;
1599  } else if (strcmp(band,"K") == 0) {
1600  lam=2.175;
1601  } else if (strcmp(band,"J") == 0) {
1602  lam=1.225;
1603  } else if (strcmp(band,"H") == 0) {
1604  lam=1.675;
1605  }
1606  return lam;
1607 
1608 }
1609 
1610 
1611 
1620 int sinfo_pix_scale_isnot_const(float* pix_scale, const int n) {
1621  int i=0;
1622  float eps=0.0001;
1623  float ref=pix_scale[0];
1624 
1625  for(i=1;i<n;i++) {
1626  if(fabs(pix_scale[i]-ref) > eps) return 1;
1627  }
1628  return 0;
1629 }
1630 
1631 
1639 const char* sinfo_get_pix_scale(float ps) {
1640 
1641  const char* key_value;
1642  float eps=0.0001;
1643 
1644  if(fabs(ps - 0.025) < eps) {
1645  key_value="0.025";
1646  }
1647  else if (fabs(ps - 0.1) < eps) {
1648  key_value="0.1";
1649  }
1650  else if (fabs(ps - 0.25) < eps) {
1651  key_value="0.25";
1652  }
1653  else if (fabs(ps - 1.0) < eps) {
1654  key_value="pupil";
1655  } else {
1656  sinfo_msg_error("ps=%f. Failed to set pixel scale",ps);
1657  return NULL;
1658  }
1659 
1660  return key_value;
1661 }
1662 
1663 
1679 int sinfo_get_clean_mean_window(cpl_image* img,
1680  int llx, int lly, int urx, int ury,
1681  const int kappa, const int nclip,
1682  double* local_clean_mean,
1683  double* clean_stdev)
1684 {
1685 
1686  double mean=0;
1687  double stdev=0;
1688  double threshold=0;
1689  double lo_cut=0;
1690  double hi_cut=0;
1691  cpl_mask* mask=NULL;
1692  cpl_image* tmp=NULL;
1693  cpl_stats* stats=NULL;
1694  int i=0;
1695 
1696  tmp=cpl_image_extract(img,llx,lly,urx,ury);
1697  cpl_image_accept_all(tmp);
1698  for(i=0;i<nclip;i++) {
1699 
1700 
1701  cpl_stats_delete(stats);
1702  stats = cpl_stats_new_from_image(tmp, CPL_STATS_MEAN | CPL_STATS_STDEV);
1703  mean = cpl_stats_get_mean(stats);
1704  stdev = cpl_stats_get_stdev(stats);
1705 
1706  threshold=kappa*stdev;
1707  lo_cut=mean-threshold;
1708  hi_cut=mean+threshold;
1709  cpl_image_accept_all(tmp);
1710  mask=cpl_mask_threshold_image_create(tmp,lo_cut,hi_cut);
1711 
1712  cpl_mask_not(mask);
1713  cpl_image_reject_from_mask(tmp,mask);
1714  cpl_mask_delete(mask);
1715 
1716 
1717  }
1718  *local_clean_mean=mean;
1719  *clean_stdev=stdev;
1720  cpl_image_delete(tmp);
1721  cpl_stats_delete(stats);
1722 
1723 
1724  return 0;
1725 
1726 
1727 }
1733 int sinfo_check_rec_status(const int val) {
1734  if(cpl_error_get_code() != CPL_ERROR_NONE) {
1735  sinfo_msg_error("error before %d",val);
1736  sinfo_msg_error("%s", (char* ) cpl_error_get_message());
1737  sinfo_msg_error("%s", (char* ) cpl_error_get_where());
1738  return -1;
1739  }
1740  return 0;
1741 }
1742 
1749 int
1750 sinfo_clean_nan(cpl_image** im)
1751 {
1752  int i=0;
1753  int j=0;
1754  int nx=0;
1755  int ny=0;
1756  float* data=NULL;
1757 
1758  nx=cpl_image_get_size_x(*im);
1759  ny=cpl_image_get_size_y(*im);
1760  data=cpl_image_get_data_float(*im);
1761 
1762  for(j=0;j<ny;j++) {
1763  for(i=0;i<nx;i++) {
1764  if(isnan(data[j*nx+i]) != 0) {
1765  data[j*nx+i] = 0;
1766  }
1767  }
1768  }
1769  return 0;
1770 }
1771 
1781 void
1782 sinfo_add_pro_fits_key(cpl_propertylist * plist,
1783  char* pro_catg,
1784  char* file_name,
1785  char* out_name)
1786 {
1787 
1788  char* date=NULL;
1789  date = sinfo_get_datetime_iso8601() ;
1790 
1791  cpl_propertylist_insert_after_string(plist, "EXPTIME",
1792  KEY_NAME_PIPEFILE, out_name) ;
1793  cpl_propertylist_set_comment(plist, KEY_NAME_PIPEFILE,KEY_HELP_PIPEFILE) ;
1794 
1795  cpl_propertylist_insert_after_string(plist, "EXPTIME",
1796  KEY_NAME_HPRO_DID, KEY_VALUE_HPRO_DID) ;
1797  cpl_propertylist_set_comment(plist, KEY_NAME_HPRO_DID,KEY_HELP_HPRO_DID) ;
1798 
1799  cpl_propertylist_insert_after_string(plist, "EXPTIME",
1800  KEY_NAME_HPRO_TYPE, "REDUCED") ;
1801  cpl_propertylist_set_comment(plist, KEY_NAME_HPRO_TYPE, KEY_HELP_HPRO_TYPE) ;
1802 
1803  cpl_propertylist_insert_after_string(plist, "EXPTIME",
1804  KEY_NAME_HPRO_CATG, pro_catg) ;
1805  cpl_propertylist_set_comment(plist, KEY_NAME_HPRO_CATG, KEY_HELP_HPRO_CATG);
1806 
1807  cpl_propertylist_insert_after_string(plist, "EXPTIME",
1808  KEY_NAME_HPRO_STATUS, "OK") ;
1809  cpl_propertylist_set_comment(plist, KEY_NAME_HPRO_STATUS,KEY_HELP_HPRO_CATG);
1810 
1811  cpl_propertylist_insert_after_string(plist, "EXPTIME",
1812  KEY_NAME_HPRO_DATE, date) ;
1813  cpl_propertylist_set_comment(plist, KEY_NAME_HPRO_DATE, KEY_HELP_HPRO_DATE);
1814 
1815  cpl_propertylist_insert_after_string(plist, "EXPTIME",
1816  KEY_NAME_HPRO_RECID, file_name) ;
1817  cpl_propertylist_set_comment(plist, KEY_NAME_HPRO_RECID,KEY_HELP_HPRO_RECID);
1818 
1819  cpl_propertylist_insert_after_string(plist, "EXPTIME",
1820  KEY_NAME_HPRO_DRSID, PACKAGE_VERSION);
1821  cpl_propertylist_set_comment(plist, KEY_NAME_HPRO_DRSID,KEY_HELP_HPRO_DRSID);
1822 
1823 
1824 }
1825 /*---------------------------------------------------------------------------*/
1832 /*---------------------------------------------------------------------------*/
1833 int sinfo_compare_tags(
1834  const cpl_frame * frame1,
1835  const cpl_frame * frame2)
1836 {
1837  char * v1 ;
1838  char * v2 ;
1839 
1840  /* Test entries */
1841  if (frame1==NULL || frame2==NULL) return -1 ;
1842 
1843  /* Get the tags */
1844  if ((v1 = (char*)cpl_frame_get_tag(frame1)) == NULL) return -1 ;
1845  if ((v2 = (char*)cpl_frame_get_tag(frame2)) == NULL) return -1 ;
1846 
1847  /* Compare the tags */
1848  if (strcmp(v1, v2)) return 0 ;
1849  else return 1 ;
1850 }
1851 
1860 int sinfo_extract_raw_pinhole_frames(cpl_frameset * sof, cpl_frameset** raw)
1861 {
1862  char* tag=NULL;
1863  char* name=NULL;
1864  cpl_frame* frame = NULL;
1865  int nsof=0;
1866  int i=0;
1867  nsof = cpl_frameset_get_size(sof);
1868  for (i=0 ; i<nsof ; i++) {
1869  frame = cpl_frameset_get_frame(sof,i);
1870  name= (char*) cpl_frame_get_filename(frame);
1871  if(sinfo_is_fits_file(name) == 1) {
1872  /* to go on the file must exist */
1873  if(cpl_frame_get_tag(frame) != NULL) {
1874  /* If the frame has a tag we process it. Else it is an object */
1875  tag= (char*) cpl_frame_get_tag(frame);
1876  if(sinfo_frame_is_pinhole_lamp(tag) == 1) {
1877  cpl_frameset_insert(*raw,frame);
1878  }
1879  }
1880  }
1881  }
1882  return 0;
1883 }
1884 
1885 
1886 int sinfo_get_ins_set(char* band,int* ins_set){
1887 
1888  if (strcmp(band,"H") == 0) {
1889  *ins_set = 0;
1890  }
1891  else if (strcmp(band,"H+K") == 0) {
1892  *ins_set = 1;
1893  }
1894  else if (strcmp(band,"K") == 0) {
1895  *ins_set = 2;
1896  }
1897  else if (strcmp(band,"J") == 0) {
1898  *ins_set = 3;
1899  }
1900  return 0;
1901 
1902 
1903 }
1904 int sinfo_extract_raw_frames(cpl_frameset * sof, cpl_frameset** raw)
1905 {
1906  char* tag=NULL;
1907  char* name=NULL;
1908  cpl_frame* frame = NULL;
1909  int nsof=0;
1910  int i=0;
1911  nsof = cpl_frameset_get_size(sof);
1912  for (i=0 ; i<nsof ; i++) {
1913  frame = cpl_frameset_get_frame(sof,i);
1914  name= (char*) cpl_frame_get_filename(frame);
1915  if(sinfo_is_fits_file(name) == 1) {
1916  /* to go on the file must exist */
1917  if(cpl_frame_get_tag(frame) != NULL) {
1918  /* If the frame has a tag we process it. Else it is an object */
1919  tag= (char*) cpl_frame_get_tag(frame);
1920  if(sinfo_frame_is_raw(tag) == 1) {
1921  cpl_frameset_insert(*raw,frame);
1922  }
1923  }
1924  }
1925  }
1926  return 0;
1927 }
1928 
1929 int sinfo_remove_qc_frames(cpl_frameset* sof,cpl_frameset** raw)
1930 {
1931  char* tag=NULL;
1932  char* name=NULL;
1933  cpl_frame* frame = NULL;
1934  int nsof=0;
1935  int i=0;
1936 
1937  nsof = cpl_frameset_get_size(sof);
1938  for (i=0 ; i<nsof ; i++) {
1939  frame = cpl_frameset_get_frame(sof,i);
1940  name= (char*) cpl_frame_get_filename(frame);
1941  /* sinfo_msg("name=%s",name); */
1942  if(sinfo_is_fits_file(name) == 1) {
1943  /* sinfo_msg("\t exist "); */
1944  /* to go on the file must exist */
1945  if(cpl_frame_get_tag(frame) != NULL) {
1946  /* If the frame has a tag we process it. Else it is an object */
1947  tag= (char*) cpl_frame_get_tag(frame);
1948  /* sinfo_msg("\t tag %s\n ",tag); */
1949  if(strstr(tag,"QC") != NULL) {
1950  /* sinfo_msg("remove frame %s\n",name); */
1951  cpl_frameset_erase(*raw,tag);
1952  }
1953  }
1954  } else {
1955  /* sinfo_msg("remove frame\n"); */
1956  cpl_frameset_erase_frame(*raw,frame);
1957  }
1958  }
1959  return 0;
1960 
1961 }
1962 
1963 
1964 int sinfo_contains_frames_kind(cpl_frameset * sof,
1965  cpl_frameset* raw,
1966  const char* type)
1967 {
1968  char* tag=NULL;
1969  char* name=NULL;
1970  cpl_frame* frame = NULL;
1971  cpl_frame* frame_dup = NULL;
1972 
1973  int nsof=0;
1974  int i=0;
1975  nsof = cpl_frameset_get_size(sof);
1976  for (i=0 ; i<nsof ; i++) {
1977  frame = cpl_frameset_get_frame(sof,i);
1978  name= (char*) cpl_frame_get_filename(frame);
1979  if(sinfo_is_fits_file(name) == 1) {
1980  /* to go on the file must exist */
1981  if(cpl_frame_get_tag(frame) != NULL) {
1982  /* If the frame has a tag we process it. Else it is an object */
1983  tag= (char*) cpl_frame_get_tag(frame);
1984  /* sinfo_msg("name=%s tag=%s type=%s\n",name,tag,type); */
1985  if(strstr(tag,type) != NULL) {
1986  /* sinfo_msg("Match name=%s tag=%s type=%s\n",name,tag,type); */
1987  frame_dup = cpl_frame_duplicate(frame);
1988  cpl_frameset_insert(raw,frame_dup);
1989  /* sinfo_msg("inserted\n"); */
1990  }
1991  }
1992  }
1993  }
1994  return 0;
1995 }
1996 
1997 
1998 
1999 
2000 int sinfo_is_fibres_on_off(cpl_frameset * sof,
2001  cpl_frameset* raw)
2002 {
2003  char* tag=NULL;
2004  char* name=NULL;
2005  cpl_frame* frame = NULL;
2006  cpl_frame* frame_dup = NULL;
2007 
2008  int nsof=0;
2009  int i=0;
2010  nsof = cpl_frameset_get_size(sof);
2011  for (i=0 ; i<nsof ; i++) {
2012  frame = cpl_frameset_get_frame(sof,i);
2013  name= (char*) cpl_frame_get_filename(frame);
2014  if(sinfo_is_fits_file(name) == 1) {
2015  /* to go on the file must exist */
2016  if(cpl_frame_get_tag(frame) != NULL) {
2017  /* If the frame has a tag we process it. Else it is an object */
2018  tag= (char*) cpl_frame_get_tag(frame);
2019  /* sinfo_msg("name=%s tag=%s type=%s\n",name,tag,type); */
2020  if( strcmp(tag,PRO_FIBRE_NS_STACKED ) == 0) {
2021  /* sinfo_msg("Match name=%s tag=%s type=%s\n",name,tag); */
2022  frame_dup = cpl_frame_duplicate(frame);
2023  cpl_frameset_insert(raw,frame_dup);
2024  /* sinfo_msg("inserted\n"); */
2025  }
2026  }
2027  }
2028  }
2029  return 0;
2030 }
2031 
2032 
2033 int sinfo_contains_frames_type(cpl_frameset * sof,
2034  cpl_frameset** raw,
2035  const char* type)
2036 {
2037  char* tag=NULL;
2038  char* name=NULL;
2039  cpl_frame* frame = NULL;
2040  cpl_frame* frame_dup = NULL;
2041  int nsof=0;
2042  int i=0;
2043  nsof = cpl_frameset_get_size(sof);
2044  for (i=0 ; i<nsof ; i++) {
2045  frame = cpl_frameset_get_frame(sof,i);
2046  name= (char*) cpl_frame_get_filename(frame);
2047  if(sinfo_is_fits_file(name) == 1) {
2048  /* to go on the file must exist */
2049  if(cpl_frame_get_tag(frame) != NULL) {
2050  /* If the frame has a tag we process it. Else it is an object */
2051  tag= (char*) cpl_frame_get_tag(frame);
2052  if(strstr(tag,type) != NULL) {
2053  /* sinfo_msg("name=%s tag=%s type=%s\n",name,tag,type); */
2054  frame_dup=cpl_frame_duplicate(frame);
2055  cpl_frameset_insert(*raw,frame_dup);
2056  }
2057  }
2058  }
2059  }
2060  return 0;
2061 }
2062 
2063 int sinfo_extract_raw_frames_type2(cpl_frameset * sof,
2064  cpl_frameset** raw,
2065  const char* type)
2066 {
2067 
2068  cpl_frame* frame=NULL;
2069  cpl_frame* frame_dup = NULL;
2070  frame = cpl_frameset_find(sof,type);
2071  while(frame) {
2072  frame_dup=cpl_frame_duplicate(frame);
2073  cpl_frameset_insert(*raw,frame_dup);
2074  frame = cpl_frameset_find(sof,NULL);
2075  }
2076  return 0;
2077 
2078 }
2079 
2080 
2081 int sinfo_extract_raw_frames_type1(cpl_frameset * sof,
2082  cpl_frameset* raw,
2083  const char* type)
2084 {
2085 
2086  cpl_frame* frame=NULL;
2087  cpl_frame* frame_dup = NULL;
2088  frame = cpl_frameset_find(sof,type);
2089  while(frame) {
2090  frame_dup=cpl_frame_duplicate(frame);
2091  cpl_frameset_insert(raw,frame_dup);
2092  frame = cpl_frameset_find(sof,NULL);
2093  }
2094  return 0;
2095 
2096 }
2097 
2098 int sinfo_extract_raw_frames_type(cpl_frameset * sof,
2099  cpl_frameset** raw,
2100  const char* type)
2101 {
2102  char tag[FILE_NAME_SZ];
2103  char name[FILE_NAME_SZ];
2104  cpl_frame* frame = NULL;
2105  cpl_frame* frame_dup = NULL;
2106  int nsof=0;
2107  int i=0;
2108  nsof = cpl_frameset_get_size(sof);
2109  for (i=0 ; i<nsof ; i++) {
2110  frame = cpl_frameset_get_frame(sof,i);
2111  strcpy(name, cpl_frame_get_filename(frame));
2112  if(sinfo_is_fits_file(name) == 1) {
2113  /* to go on the file must exist */
2114  if(cpl_frame_get_tag(frame) != NULL) {
2115  /* If the frame has a tag we process it. Else it is an object */
2116  strcpy(tag,cpl_frame_get_tag(frame));
2117  if(strcmp(tag,type) == 0) {
2118  /* sinfo_msg("name=%s tag=%s type=%s\n",name,tag,type); */
2119  frame_dup=cpl_frame_duplicate(frame);
2120 
2121  cpl_frameset_insert(*raw,frame_dup);
2122  }
2123  }
2124  }
2125  }
2126  return 0;
2127 }
2128 
2129 int sinfo_extract_frames_type(cpl_frameset * sof,
2130  cpl_frameset * raw,
2131  const char* type)
2132 {
2133  char* tag=NULL;
2134  char* name=NULL;
2135  cpl_frame* frame = NULL;
2136  cpl_frame* frame_dup = NULL;
2137  int nsof=0;
2138  int i=0;
2139  nsof = cpl_frameset_get_size(sof);
2140  for (i=0 ; i<nsof ; i++) {
2141  frame = cpl_frameset_get_frame(sof,i);
2142  name= (char*) cpl_frame_get_filename(frame);
2143  if(sinfo_is_fits_file(name) == 1) {
2144  /* to go on the file must exist */
2145  if(cpl_frame_get_tag(frame) != NULL) {
2146  /* If the frame has a tag we process it. Else it is an object */
2147  tag= (char*) cpl_frame_get_tag(frame);
2148  if(strcmp(tag,type) == 0) {
2149  /* sinfo_msg("name=%s tag=%s type=%s\n",name,tag,type); */
2150  frame_dup=cpl_frame_duplicate(frame);
2151  cpl_frameset_insert(raw,frame_dup);
2152  }
2153  }
2154  }
2155  }
2156  return 0;
2157 }
2158 
2159 
2160 int sinfo_extract_obj_frames(cpl_frameset * sof, cpl_frameset* obj)
2161 {
2162  char* tag=NULL;
2163  char* name=NULL;
2164  cpl_frame* frame = NULL;
2165  cpl_frame* frame_dup = NULL;
2166 
2167  int nsof=0;
2168  int i=0;
2169  nsof = cpl_frameset_get_size(sof);
2170  for (i=0 ; i<nsof ; i++) {
2171  frame = cpl_frameset_get_frame(sof,i);
2172  name= (char*) cpl_frame_get_filename(frame);
2173  if(sinfo_is_fits_file(name) ==1) {
2174  /* to go on the file must exist */
2175  if(cpl_frame_get_tag(frame) != NULL) {
2176  /* If the frame has a tag we process it. Else it is an object */
2177  tag= (char*) cpl_frame_get_tag(frame);
2178  if(sinfo_tag_is_obj(tag) == 1) {
2179  frame_dup=cpl_frame_duplicate(frame);
2180  cpl_frameset_insert(obj,frame_dup);
2181  }
2182  }
2183  }
2184  }
2185 
2186  return 0;
2187 }
2188 
2189 
2190 int sinfo_extract_obj_products(cpl_frameset * sof, cpl_frameset* obj)
2191 {
2192  char* tag=NULL;
2193  char* name=NULL;
2194  cpl_frame* frame = NULL;
2195  cpl_frame* frame_dup = NULL;
2196 
2197  int nsof=0;
2198  int i=0;
2199  nsof = cpl_frameset_get_size(sof);
2200  for (i=0 ; i<nsof ; i++) {
2201  frame = cpl_frameset_get_frame(sof,i);
2202  name= (char*) cpl_frame_get_filename(frame);
2203  if(sinfo_is_fits_file(name) ==1) {
2204  /* to go on the file must exist */
2205  if(cpl_frame_get_tag(frame) != NULL) {
2206  /* If the frame has a tag we process it. Else it is an object */
2207  tag= (char*) cpl_frame_get_tag(frame);
2208  if(sinfo_tag_is_objpro(tag) == 1) {
2209  frame_dup=cpl_frame_duplicate(frame);
2210  cpl_frameset_insert(obj,frame_dup);
2211  }
2212  }
2213  }
2214  }
2215 
2216  return 0;
2217 }
2218 
2219 int sinfo_extract_on_frames(cpl_frameset * sof, cpl_frameset* on)
2220 {
2221  cpl_frame* frame = NULL;
2222  cpl_frame* frame_dup = NULL;
2223 
2224  int nsof=0;
2225  int i=0;
2226  nsof = cpl_frameset_get_size(sof);
2227  for (i=0 ; i<nsof ; i++) {
2228  frame = cpl_frameset_get_frame(sof,i);
2229  if(sinfo_frame_is_on(frame) ==1) {
2230  frame_dup=cpl_frame_duplicate(frame);
2231  cpl_frameset_insert(on,frame_dup);
2232  }
2233  }
2234 
2235  return 0;
2236 }
2237 
2238 int sinfo_extract_sky_frames(cpl_frameset * sof, cpl_frameset* sky)
2239 {
2240  char* tag=NULL;
2241  char* name=NULL;
2242  cpl_frame* frame = NULL;
2243  cpl_frame* frame_dup = NULL;
2244  int nsof=0;
2245  int i=0;
2246  nsof = cpl_frameset_get_size(sof);
2247  for (i=0 ; i<nsof ; i++) {
2248  frame = cpl_frameset_get_frame(sof,i);
2249  name= (char*) cpl_frame_get_filename(frame);
2250  if(sinfo_is_fits_file(name) ==1) {
2251  /* to go on the file must exist */
2252  if(cpl_frame_get_tag(frame) != NULL) {
2253  /* If the frame has a tag we process it. Else it is an object */
2254  tag= (char*) cpl_frame_get_tag(frame);
2255  if(sinfo_tag_is_sky(tag) == 1) {
2256  frame_dup=cpl_frame_duplicate(frame);
2257  cpl_frameset_insert(sky,frame_dup);
2258  }
2259  }
2260  }
2261  }
2262 
2263  return 0;
2264 }
2265 
2266 
2267 
2268 int sinfo_extract_off_frames(cpl_frameset * sof, cpl_frameset* off)
2269 {
2270  cpl_frame* frame = NULL;
2271  cpl_frame* frame_dup = NULL;
2272  int nsof=0;
2273  int i=0;
2274  nsof = cpl_frameset_get_size(sof);
2275  for (i=0 ; i<nsof ; i++) {
2276  frame = cpl_frameset_get_frame(sof,i);
2277  if(sinfo_frame_is_on(frame)) {
2278  frame_dup=cpl_frame_duplicate(frame);
2279  cpl_frameset_insert(off,frame_dup);
2280  }
2281  }
2282 
2283  return 0;
2284 }
2285 
2286 int sinfo_extract_mst_frames(cpl_frameset * sof, cpl_frameset* cdb)
2287 {
2288  char* tag=NULL;
2289  char* name=NULL;
2290  cpl_frame* frame = NULL;
2291  cpl_frame* frame_dup = NULL;
2292  int nsof=0;
2293  int i=0;
2294 
2295  nsof = cpl_frameset_get_size(sof);
2296  for (i=0 ; i<nsof ; i++) {
2297  frame = cpl_frameset_get_frame(sof,i);
2298  name= (char*) cpl_frame_get_filename(frame);
2299  if(sinfo_is_fits_file(name) ==1) {
2300  /* to go on the file must exist */
2301  if(cpl_frame_get_tag(frame) != NULL) {
2302  /* If the frame has a tag we process it. Else it is an object */
2303  tag= (char*) cpl_frame_get_tag(frame);
2304  if(sinfo_frame_is_cdb(tag) == 1) {
2305  frame_dup=cpl_frame_duplicate(frame);
2306  cpl_frameset_insert(cdb,frame_dup);
2307  }
2308  }
2309  }
2310  }
2311 
2312  return 0;
2313 }
2314 
2315 cpl_frameset* sinfo_frameset_join(cpl_frameset* fs1,cpl_frameset* fs2) {
2316 
2317  cpl_frameset* join=NULL;
2318  cpl_frame* frm=NULL;
2319  cpl_frame* frm_dup=NULL;
2320  int i=0;
2321  int n=0;
2322 
2323  join=cpl_frameset_new();
2324 
2325  n=cpl_frameset_get_size(fs1);
2326  for(i=0;i<n; i++) {
2327  frm=cpl_frameset_get_frame(fs1,i);
2328  frm_dup= cpl_frame_duplicate(frm);
2329  cpl_frameset_insert(join,frm_dup);
2330  }
2331 
2332  n=cpl_frameset_get_size(fs2);
2333  for(i=0;i<n; i++) {
2334  frm=cpl_frameset_get_frame(fs2,i);
2335  frm_dup= cpl_frame_duplicate(frm);
2336  cpl_frameset_insert(join,frm_dup);
2337  }
2338 
2339 
2340  return join;
2341 
2342 }
2343 
2344 
2345 int sinfo_extract_stk_frames(cpl_frameset * sof,
2346  cpl_frameset* res)
2347 {
2348  char* tag=NULL;
2349  char* name=NULL;
2350  cpl_frame* frame = NULL;
2351  cpl_frame* frame_dup = NULL;
2352  int nsof=0;
2353  int i=0;
2354 
2355  nsof = cpl_frameset_get_size(sof);
2356  for (i=0 ; i<nsof ; i++) {
2357  frame = cpl_frameset_get_frame(sof,i);
2358  name= (char*) cpl_frame_get_filename(frame);
2359  if(sinfo_is_fits_file(name) ==1) {
2360  /* to go on the file must exist */
2361  if(cpl_frame_get_tag(frame) != NULL) {
2362  /* If the frame has a tag we process it. Else it is an object */
2363  tag= (char*) cpl_frame_get_tag(frame);
2364  if(sinfo_frame_is_stk(tag) == 1) {
2365  frame_dup=cpl_frame_duplicate(frame);
2366  cpl_frameset_insert(res,frame_dup);
2367  }
2368  }
2369  }
2370  }
2371 
2372  return 0;
2373 }
2374 
2375 
2376 int
2377 sinfo_extract_preoptic_frames(cpl_frameset * sof,
2378  cpl_frameset** res,
2379  const char* val)
2380 {
2381  char* name=NULL;
2382  cpl_frame* frame = NULL;
2383  cpl_frame* frame_dup = NULL;
2384  int nsof=0;
2385  int i=0;
2386 
2387  nsof = cpl_frameset_get_size(sof);
2388  for (i=0 ; i<nsof ; i++) {
2389  frame = cpl_frameset_get_frame(sof,i);
2390  name= (char*) cpl_frame_get_filename(frame);
2391  if(sinfo_is_fits_file(name) ==1) {
2392  if(sinfo_frame_is_preoptic(frame,val) == 1) {
2393  frame_dup=cpl_frame_duplicate(frame);
2394  cpl_frameset_insert(*res,frame_dup);
2395  }
2396  }
2397  }
2398 
2399  return 0;
2400 }
2401 
2402 int sinfo_extract_raw_stack_frames(cpl_frameset * sof, cpl_frameset** pro)
2403 {
2404  char* tag=NULL;
2405  char* name=NULL;
2406  cpl_frame* frame = NULL;
2407  cpl_frame* frame_dup = NULL;
2408 
2409  int nsof=0;
2410  int i=0;
2411  nsof = cpl_frameset_get_size(sof);
2412 
2413  for (i=0 ; i<nsof ; i++) {
2414  frame = cpl_frameset_get_frame(sof,i);
2415  name= (char*) cpl_frame_get_filename(frame);
2416  if(sinfo_is_fits_file(name) ==1) {
2417  /* to go on the file must exist */
2418  if(cpl_frame_get_tag(frame) != NULL) {
2419  /* If the frame has a tag we process it. Else it is an object */
2420  tag= (char*) cpl_frame_get_tag(frame);
2421  /* sinfo_msg("tag=%s\n",tag); */
2422  if(sinfo_frame_is_raw_stack(tag) == 1) {
2423  frame_dup = cpl_frame_duplicate(frame);
2424  cpl_frameset_insert(*pro,frame_dup);
2425  }
2426  }
2427  }
2428  }
2429 
2430  return 0;
2431 }
2432 
2433 
2434 int sinfo_extract_raw_slit_frames(cpl_frameset * sof, cpl_frameset** pro)
2435 {
2436  char* tag=NULL;
2437  char* name=NULL;
2438  cpl_frame* frame = NULL;
2439  int nsof=0;
2440  int i=0;
2441  nsof = cpl_frameset_get_size(sof);
2442  for (i=0 ; i<nsof ; i++) {
2443  frame = cpl_frameset_get_frame(sof,i);
2444  name= (char*) cpl_frame_get_filename(frame);
2445  if(sinfo_is_fits_file(name) ==1) {
2446  /* to go on the file must exist */
2447  if(cpl_frame_get_tag(frame) != NULL) {
2448  /* If the frame has a tag we process it. Else it is an object */
2449  tag= (char*) cpl_frame_get_tag(frame);
2450  if(sinfo_frame_is_slit_lamp(tag) == 1) {
2451  cpl_frameset_insert(*pro,frame);
2452  }
2453  }
2454  }
2455  }
2456 
2457  return 0;
2458 }
2459 
2460 /*---------------------------------------------------------------------------*/
2466 /*---------------------------------------------------------------------------*/
2467 int sinfo_frame_is_raw(char * tag)
2468 {
2469  /* Test entries */
2470  if (tag == NULL) return -1 ;
2471 
2472  if (!strcmp(tag, RAW_LINEARITY_LAMP)) return 1 ;
2473  if (!strcmp(tag, RAW_DARK)) return 1 ;
2474  if (!strcmp(tag, RAW_PINHOLE_LAMP)) return 1 ;
2475  if (!strcmp(tag, RAW_SLIT_LAMP)) return 1 ;
2476  if (!strcmp(tag, RAW_WAVE_LAMP)) return 1 ;
2477  if (!strcmp(tag, RAW_FLAT_LAMP)) return 1 ;
2478  if (!strcmp(tag, RAW_WAVE_NS)) return 1 ;
2479  if (!strcmp(tag, RAW_FLAT_NS)) return 1 ;
2480  if (!strcmp(tag, RAW_FIBRE_LAMP)) return 1 ;
2481  if (!strcmp(tag, RAW_FIBRE_EW)) return 1 ;
2482  if (!strcmp(tag, RAW_FIBRE_NS)) return 1 ;
2483  if (!strcmp(tag, RAW_FLAT_SKY)) return 1 ;
2484  if (!strcmp(tag, RAW_FLUX_LAMP)) return 1 ;
2485  if (!strcmp(tag, RAW_PSF_CALIBRATOR)) return 1 ;
2486  if (!strcmp(tag, RAW_FOCUS)) return 1 ;
2487 
2488  if (!strcmp(tag, RAW_STD)) return 1 ;
2489  if (!strcmp(tag, RAW_STD_STAR)) return 1 ;
2490  if (!strcmp(tag, RAW_STD_STAR_DITHER)) return 1 ;
2491  if (!strcmp(tag, RAW_SKY_STD)) return 1 ;
2492  if (!strcmp(tag, RAW_SKY_OH)) return 1 ;
2493  if (!strcmp(tag, RAW_SKY_PSF_CALIBRATOR)) return 1 ;
2494 
2495  if (!strcmp(tag, RAW_PUPIL_LAMP)) return 1 ;
2496  if (!strcmp(tag, RAW_OBJECT_JITTER)) return 1 ;
2497  if (!strcmp(tag, RAW_SKY_JITTER)) return 1 ;
2498  if (!strcmp(tag, RAW_OBJECT_NODDING)) return 1 ;
2499  if (!strcmp(tag, RAW_OBJECT_SKYSPIDER)) return 1 ;
2500  if (!strcmp(tag, RAW_SKY_NODDING)) return 1 ;
2501 
2502  if (!strcmp(tag, RAW_FLAT_LAMP_DITHER)) return 1 ;
2503  if (!strcmp(tag, RAW_WAVE_LAMP_DITHER)) return 1 ;
2504  if (!strcmp(tag, RAW_STD_STAR_DITHER)) return 1 ;
2505  if (!strcmp(tag, RAW_OBJECT_NODDING_DITHER)) return 1 ;
2506  if (!strcmp(tag, RAW_OBJECT_SKYSPIDER_DITHER)) return 1 ;
2507  if (!strcmp(tag, RAW_SKY_NODDING_DITHER)) return 1 ;
2508 
2509 
2510  return 0 ;
2511 }
2512 
2513 /*---------------------------------------------------------------------------*/
2519 /*---------------------------------------------------------------------------*/
2520 int sinfo_frame_is_raw_stack(char * tag)
2521 {
2522  /* Test entries */
2523  if (tag == NULL) return -1 ;
2524 
2525 
2526  if (!strcmp(tag, PRO_SKY_DUMMY)) return 1 ;
2527  if (!strcmp(tag, RAW_WAVE_LAMP)) return 1 ;
2528  if (!strcmp(tag, RAW_WAVE_LAMP_DITHER)) return 1 ;
2529  if (!strcmp(tag, RAW_WAVE_NS)) return 1 ;
2530  if (!strcmp(tag, RAW_WAVE_NS_DITHER)) return 1 ;
2531 
2532  if (!strcmp(tag, RAW_FLUX_LAMP)) return 1 ;
2533  if (!strcmp(tag, RAW_FIBRE_NS)) return 1 ;
2534  if (!strcmp(tag, RAW_FIBRE_EW)) return 1 ;
2535 
2536  if (!strcmp(tag, RAW_PSF_CALIBRATOR)) return 1 ;
2537  if (!strcmp(tag, RAW_FIBRE_PSF)) return 1 ;
2538  if (!strcmp(tag, RAW_FIBRE_DARK)) return 1 ;
2539 
2540  if (!strcmp(tag, RAW_FOCUS)) return 1 ;
2541 
2542  if (!strcmp(tag, RAW_PUPIL_LAMP)) return 1 ;
2543  if (!strcmp(tag, RAW_OBJECT_JITTER)) return 1 ;
2544  if (!strcmp(tag, RAW_SKY_JITTER)) return 1 ;
2545  if (!strcmp(tag, RAW_OBJECT_NODDING)) return 1 ;
2546  if (!strcmp(tag, RAW_OBJECT_SKYSPIDER)) return 1 ;
2547  if (!strcmp(tag, RAW_SKY_NODDING)) return 1 ;
2548 
2549  if (!strcmp(tag, RAW_OBJECT_NODDING_DITHER)) return 1 ;
2550  if (!strcmp(tag, RAW_OBJECT_SKYSPIDER_DITHER)) return 1 ;
2551  if (!strcmp(tag, RAW_SKY_NODDING_DITHER)) return 1 ;
2552 
2553 
2554  if (!strcmp(tag, RAW_IMAGE_PRE_OBJECT)) return 1 ;
2555  if (!strcmp(tag, RAW_IMAGE_PRE_SKY)) return 1 ;
2556  if (!strcmp(tag, RAW_STD)) return 1 ;
2557  if (!strcmp(tag, RAW_SKY_STD)) return 1 ;
2558  if (!strcmp(tag, RAW_SKY_OH)) return 1 ;
2559  if (!strcmp(tag, RAW_SKY_PSF_CALIBRATOR)) return 1 ;
2560  if (!strcmp(tag, RAW_STD_STAR)) return 1 ;
2561  if (!strcmp(tag, RAW_SKY)) return 1 ;
2562 
2563  return 0 ;
2564 }
2565 
2566 
2567 /*---------------------------------------------------------------------------*/
2573 /*---------------------------------------------------------------------------*/
2574 int sinfo_frame_is_raw_dark(char * tag)
2575 {
2576  /* Test entries */
2577  if (tag == NULL) return -1 ;
2578 
2579  if (!strcmp(tag, RAW_DARK)) return 1 ;
2580 
2581  return 0 ;
2582 }
2583 
2584 /*---------------------------------------------------------------------------*/
2590 /*---------------------------------------------------------------------------*/
2591 int sinfo_frame_is_slit_lamp(char * tag)
2592 {
2593  /* Test entries */
2594  if (tag == NULL) return -1 ;
2595 
2596  if (!strcmp(tag, RAW_SLIT_LAMP)) return 1 ;
2597 
2598  return 0 ;
2599 }
2600 
2601 
2602 /*---------------------------------------------------------------------------*/
2608 /*---------------------------------------------------------------------------*/
2609 int sinfo_frame_is_pinhole_lamp(char * tag)
2610 {
2611  /* Test entries */
2612  if (tag == NULL) return -1 ;
2613 
2614  if (!strcmp(tag, RAW_PINHOLE_LAMP)) return 1 ;
2615 
2616  return 0 ;
2617 }
2618 
2619 
2620 int sinfo_frame_is_cdb(char * tag)
2621 {
2622  /* Test entries */
2623  if (tag == NULL) return -1 ;
2624  /* For the moment not checked the following:
2625 
2626  PRO_STACKED
2627  PRO_SLIT_ON
2628  PRO_FLUX_LAMP_STACKED
2629  PRO_WAVE_LAMP_STACKED
2630  PRO_PSF_CALIBRATOR_STACKED
2631  PRO_FOCUS_STACKED
2632  PRO_OBJECT_NODDING_STACKED
2633  PRO_OBJECT_SKYSPIDER_STACKED
2634  PRO_SKY_NODDING_STACKED
2635  PRO_STD_NODDING_STACKED
2636  PRO_MASK_CUBE
2637  PRO_PSF
2638  TMP_FOCUS
2639  TMP_FOCUS_ON
2640  TMP_FOCUS_OFF
2641  PRO_FOCUS
2642  PRO_FOCUS_GAUSS
2643  PRO_SPECTRA
2644  PRO_CUBE
2645  PRO_CUBE_COLL
2646  PRO_SLOPEX
2647  PRO_SLOPEY
2648  PRO_MASK_CUBE
2649  PRO_OBJ_CUBE
2650  PRO_BP_COEFF
2651  */
2652 
2653  if (!strcmp(tag, REF_LINE_ARC)) return 1 ;
2654  if (!strcmp(tag, REF_LINE_OH)) return 1 ;
2655  if (!strcmp(tag, PRO_BP_MAP)) return 1 ;
2656  if (!strcmp(tag, PRO_BP_MAP_HP)) return 1 ;
2657  if (!strcmp(tag, PRO_BP_MAP_DI)) return 1 ;
2658  if (!strcmp(tag, PRO_BP_MAP_NO)) return 1 ;
2659  if (!strcmp(tag, PRO_BP_MAP_NL)) return 1 ;
2660  if (!strcmp(tag, PRO_MASTER_BP_MAP)) return 1 ;
2661  if (!strcmp(tag, PRO_MASTER_DARK)) return 1 ;
2662  if (!strcmp(tag, PRO_SLOPE)) return 1 ;
2663  if (!strcmp(tag, PRO_DISTORTION)) return 1 ;
2664  if (!strcmp(tag, PRO_SLITLETS_DISTANCE)) return 1 ;
2665  if (!strcmp(tag, PRO_MASTER_FLAT_LAMP)) return 1 ;
2666  if (!strcmp(tag, PRO_MASTER_FLAT_LAMP1)) return 1 ;
2667  if (!strcmp(tag, PRO_MASTER_FLAT_LAMP2)) return 1 ;
2668  if (!strcmp(tag, PRO_SLIT_POS)) return 1 ;
2669  if (!strcmp(tag, PRO_SLIT_POS_GUESS)) return 1 ;
2670  if (!strcmp(tag, PRO_WAVE_PAR_LIST)) return 1 ;
2671  if (!strcmp(tag, PRO_WAVE_COEF_SLIT)) return 1 ;
2672  if (!strcmp(tag, PRO_MASTER_LAMP_SPEC)) return 1 ;
2673  if (!strcmp(tag, PRO_MASTER_TWIFLAT)) return 1 ;
2674  if (!strcmp(tag, PRO_COEFF_LIST)) return 1 ;
2675  if (!strcmp(tag, PRO_INDEX_LIST)) return 1 ;
2676  if (!strcmp(tag, PRO_HALO_SPECT)) return 1 ;
2677  if (!strcmp(tag, PRO_FIRST_COL)) return 1 ;
2678  if (!strcmp(tag, PRO_FOCUS)) return 1 ;
2679  if (!strcmp(tag, PRO_WAVE_MAP)) return 1 ;
2680  if (!strcmp(tag, PRO_REF_ATM_REF_CORR)) return 1 ;
2681 
2682  return 0;
2683 
2684 }
2685 
2686 
2687 
2688 
2689 int sinfo_frame_is_stk(char * tag)
2690 {
2691  /* Test entries */
2692  if (tag == NULL) return -1 ;
2693  /* For the moment not checked the following: */
2694 
2695 
2696  if (!strcmp(tag, PRO_SKY_STACKED_DUMMY)) return 1 ;
2697  if (!strcmp(tag, PRO_STACK_SKY_DIST)) return 1 ;
2698  if (!strcmp(tag, PRO_STACK_MFLAT_DIST)) return 1 ;
2699  if (!strcmp(tag, PRO_PSF_CALIBRATOR_STACKED)) return 1 ;
2700 
2701 
2702  return 0;
2703 
2704 }
2705 
2706 int
2707 sinfo_propertylist_has(cpl_propertylist* plist,
2708  const char* key) {
2709 
2710  return cpl_propertylist_has(plist,key);
2711 
2712 }
2713 
2714 int sinfo_frame_is_preoptic(cpl_frame* frame,const char* val)
2715 {
2716 
2717  char* file=NULL;
2718  char popt[FILE_NAME_SZ];
2719  cpl_propertylist* plist=NULL;
2720 
2721 
2722  file = cpl_strdup(cpl_frame_get_filename(frame)) ;
2723  if ((cpl_error_code)((plist = cpl_propertylist_load(file, 0)) == NULL)) {
2724  sinfo_msg_error( "getting header from reference frame %s",file);
2725  cpl_propertylist_delete(plist) ;
2726  cpl_free(file);
2727  return -1 ;
2728  }
2729 
2730  if (sinfo_propertylist_has(plist, KEY_NAME_PREOPTICS)) {
2731  strcpy(popt,cpl_propertylist_get_string(plist, KEY_NAME_PREOPTICS));
2732  } else {
2733  sinfo_msg_error("keyword %s does not exist",KEY_NAME_PREOPTICS);
2734  cpl_free(file);
2735  return -1;
2736  }
2737  cpl_propertylist_delete(plist) ;
2738  cpl_free(file);
2739 
2740  if (strstr(val,popt) != NULL) return 1 ;
2741 
2742 
2743  return 0;
2744 
2745 }
2746 
2747 
2748 int sinfo_get_preoptic(const char* file, const char* val)
2749 {
2750 
2751  cpl_propertylist* plist=NULL;
2752 
2753 
2754  if ((cpl_error_code)((plist = cpl_propertylist_load(file, 0)) == NULL)) {
2755  sinfo_msg_error( "getting header from reference frame %s",file);
2756  cpl_propertylist_delete(plist) ;
2757  return -1 ;
2758  }
2759 
2760  if (sinfo_propertylist_has(plist, KEY_NAME_PREOPTICS)) {
2761  strcpy((char*)val,cpl_propertylist_get_string(plist, KEY_NAME_PREOPTICS));
2762  } else {
2763  sinfo_msg_error("keyword %s does not exist",KEY_NAME_PREOPTICS);
2764  return -1;
2765  }
2766  cpl_propertylist_delete(plist) ;
2767 
2768  return 0;
2769 
2770 }
2771 
2772 /*
2773  static int
2774  sinfo_stat_rectangle(cpl_image* img,
2775  const int kappa,
2776  const int nclip,
2777  double *mean,
2778  double *stdev)
2779  {
2780 
2781  double sum=0;
2782  double sum2=0;
2783  double noise=0;
2784 
2785  double* pim=NULL;
2786  int i=0;
2787  int j=0;
2788  int kk=0;
2789  int sx=0;
2790  int sy=0;
2791 
2792  *mean=0;
2793  pim=cpl_image_get_data(img);
2794  kk=0;
2795  for(i=0;i<sx*sy;i++) {
2796  *mean+=pim[i];
2797  }
2798  *mean/=(sx*sy);
2799 
2800  for(i=0;i<sx*sy;i++) {
2801  sum+=(pim[i]-*mean)*(pim[i]-*mean);
2802  }
2803  noise=sqrt(sum/(sx*sy));
2804 
2805 
2806  //clean a bit the bad pixels
2807  for(j=0;j<nclip;j++) {
2808  sum=0;
2809  sum2=0;
2810  kk=0;
2811  for(i=0;i<sx*sy;i++) {
2812  if(fabs(pim[i]-*mean)<kappa*noise) {
2813 
2814  sum +=(pim[i]-*mean)*(pim[i]-*mean);
2815  sum2 += pim[i];
2816  kk+=1;
2817  }
2818  noise=sqrt(sum/kk);
2819  *mean=sum2/kk;
2820 
2821  }
2822 
2823  }
2824  *stdev=noise;
2825 
2826  return 0;
2827 
2828  }
2829  */
2830 
2831 cpl_table* sinfo_compute_gain(cpl_frameset* son, cpl_frameset* sof)
2832 {
2833 
2834 
2835  cpl_frame* frm=NULL;
2836 
2837  cpl_image* img_on1=NULL;
2838  cpl_image* img_on2=NULL;
2839  cpl_image* img_on_dif=NULL;
2840  cpl_image* img_on_sub=NULL;
2841 
2842 
2843  cpl_image* img_of1=NULL;
2844  cpl_image* img_of2=NULL;
2845  cpl_image* img_of_dif=NULL;
2846  cpl_image* img_of_sub=NULL;
2847 
2848  cpl_table* res_tbl=NULL;
2849  cpl_vector* dit_on=NULL;
2850  cpl_vector* dit_of=NULL;
2851  cpl_vector* exptime_on=NULL;
2852  cpl_vector* exptime_of=NULL;
2853  cpl_propertylist* plist=NULL;
2854 
2855  int non=0;
2856  int nof=0;
2857  int nfr=0;
2858  double avg_on1=0;
2859  double avg_on2=0;
2860  double avg_of1=0;
2861  double avg_of2=0;
2862  double std=0;
2863 
2864  double sig_on_dif=0;
2865  double sig_of_dif=0;
2866  char* name=NULL;
2867  int i=0;
2868  int m=0;
2869 
2870  int llx=270;
2871  int lly=1000;
2872  int urx=320;
2873  int ury=1050;
2874  //int zone[4];
2875  double gain=0;
2876  double dit_ref=0;
2877  double dit_tmp=0;
2878  double exptime_ref=0;
2879  double exptime_tmp=0;
2880  int kappa=5;
2881  int nclip=25;
2882  double centre=0;
2883 
2884  non = cpl_frameset_get_size(son);
2885  nof = cpl_frameset_get_size(sof);
2886  nfr = (non <= nof) ? non : nof;
2887 
2888  dit_on=cpl_vector_new(nfr);
2889  dit_of=cpl_vector_new(nfr);
2890  exptime_on=cpl_vector_new(nfr);
2891  exptime_of=cpl_vector_new(nfr);
2892 
2893  for(i=0;i<nfr;i++) {
2894 
2895  frm=cpl_frameset_get_frame(son,i);
2896  name=(char*)cpl_frame_get_filename(frm);
2897  plist=cpl_propertylist_load(name,0);
2898  dit_ref=sinfo_pfits_get_dit(plist);
2899  exptime_ref=(double)sinfo_pfits_get_exp_time(plist);
2900  cpl_propertylist_delete(plist);
2901  cpl_vector_set(dit_on,i,dit_ref);
2902  cpl_vector_set(exptime_on,i,exptime_ref);
2903 
2904  frm=cpl_frameset_get_frame(sof,i);
2905  name=(char*)cpl_frame_get_filename(frm);
2906  plist=cpl_propertylist_load(name,0);
2907  dit_ref=sinfo_pfits_get_dit(plist);
2908  exptime_ref=(double)sinfo_pfits_get_exp_time(plist);
2909  cpl_propertylist_delete(plist);
2910  cpl_vector_set(dit_of,i,dit_ref);
2911  cpl_vector_set(exptime_of,i,exptime_ref);
2912 
2913  }
2914 
2915 
2916  /*
2917  zone[0]=270;
2918  zone[1]=1030;
2919  zone[2]=310;
2920  zone[3]=1060;
2921 
2922 
2923 
2924  zone[0]=20;
2925  zone[1]=2028;
2926  zone[2]=20;
2927  zone[3]=2028;
2928  */
2929 
2930 
2931  check_nomsg(res_tbl=cpl_table_new(nfr));
2932  cpl_table_new_column(res_tbl,"adu", CPL_TYPE_DOUBLE);
2933  cpl_table_new_column(res_tbl,"gain", CPL_TYPE_DOUBLE);
2934 
2935  for(i=0;i<nfr;i++) {
2936  frm=cpl_frameset_get_frame(son,i);
2937  name=(char*)cpl_frame_get_filename(frm);
2938  img_on1=cpl_image_load(name,CPL_TYPE_DOUBLE,0,0);
2939 
2940  frm=cpl_frameset_get_frame(sof,i);
2941  name=(char*)cpl_frame_get_filename(frm);
2942  img_of1=cpl_image_load(name,CPL_TYPE_DOUBLE,0,0);
2943 
2944 
2945  dit_ref=cpl_vector_get(dit_on,i);
2946  exptime_ref=cpl_vector_get(exptime_on,i);
2947 
2948 
2949  for(m=0;m<nfr; m++) {
2950  if(m != i) {
2951  frm=cpl_frameset_get_frame(son,m);
2952  name=(char*)cpl_frame_get_filename(frm);
2953  dit_tmp=cpl_vector_get(dit_on,m);
2954  exptime_tmp=cpl_vector_get(exptime_on,m);
2955  if(dit_tmp == dit_ref && exptime_tmp == exptime_ref) {
2956  /* sinfo_msg("m=%d i=%d\n",m,i); */
2957  img_on2=cpl_image_load(name,CPL_TYPE_DOUBLE,0,0);
2958  frm=cpl_frameset_get_frame(sof,m);
2959  name=(char*)cpl_frame_get_filename(frm);
2960  img_of2=cpl_image_load(name,CPL_TYPE_DOUBLE,0,0);
2961 
2962  img_on_dif=cpl_image_subtract_create(img_on1,img_on2);
2963  img_of_dif=cpl_image_subtract_create(img_of1,img_of2);
2964 
2965  img_on_sub=cpl_image_extract(img_on_dif,llx,lly,urx,ury);
2966  img_of_sub=cpl_image_extract(img_of_dif,llx,lly,urx,ury);
2967 
2968  sinfo_get_clean_mean_window(img_on1,llx,lly,urx,ury,kappa,
2969  nclip,&avg_on1,&std);
2970  sinfo_get_clean_mean_window(img_on2,llx,lly,urx,ury,kappa,
2971  nclip,&avg_on2,&std);
2972  sinfo_get_clean_mean_window(img_of1,llx,lly,urx,ury,kappa,
2973  nclip,&avg_of1,&std);
2974  sinfo_get_clean_mean_window(img_of2,llx,lly,urx,ury,kappa,
2975  nclip,&avg_of2,&std);
2976  /*
2977  cpl_image_save(img_on_sub,"ima_on_sub.fits",
2978  CPL_BPP_IEEE_FLOAT,NULL,CPL_IO_DEFAULT);
2979  cpl_image_save(img_of_sub,"ima_of_sub.fits",
2980  CPL_BPP_IEEE_FLOAT,NULL,CPL_IO_DEFAULT);
2981  */
2982  /*
2983  //worse accuracy
2984  sinfo_stat_rectangle(img_on_dif,kappa,nclip,
2985  &centre,&sig_on_dif);
2986  sinfo_stat_rectangle(img_of_dif,kappa,nclip,
2987  &centre,&sig_of_dif);
2988  */
2989 
2990 
2991  //better accuracy
2992  sinfo_get_clean_mean_window(img_on_dif,llx,lly,urx,ury,kappa,
2993  nclip,&centre,&sig_on_dif);
2994  sinfo_get_clean_mean_window(img_of_dif,llx,lly,urx,ury,kappa,
2995  nclip,&centre,&sig_of_dif);
3012  cpl_image_delete(img_on2);
3013  cpl_image_delete(img_of2);
3014  cpl_image_delete(img_on_dif);
3015  cpl_image_delete(img_of_dif);
3016  cpl_image_delete(img_on_sub);
3017  cpl_image_delete(img_of_sub);
3018 
3019  gain=((avg_on1+avg_on2)-(avg_of1+avg_of2))/
3020  ((sig_on_dif*sig_on_dif)-(sig_of_dif*sig_of_dif));
3021 
3022  cpl_table_set_double(res_tbl,"gain",m,gain);
3023  cpl_table_set_double(res_tbl,"adu",m,
3024  ((avg_on1+avg_on2)/2-(avg_of1+avg_of2)/2));
3025  /* sinfo_msg("gain=%f ADU=%f\n",gain,
3026  (avg_on1+avg_on2)/2-(avg_of1+avg_of2)/2);
3027  sinfo_msg("g=%f avg_on1=%f avg_on2=%f",gain,avg_on1,avg_on2);
3028  sinfo_msg("avg_of1=%f avg_of2=%f sig_on_dif=%f sig_of_dif=%f",
3029  avg_of1,avg_of2,sig_on_dif,sig_of_dif);
3030  */
3031 
3032  }
3033  }
3034  }
3035  cpl_image_delete(img_on1);
3036  cpl_image_delete(img_of1);
3037  }
3038 
3039 
3040  /*
3041  sinfo_get_clean_mean_window(img_on_dif,llx,lly,urx,ury,kappa,
3042  nclip,&avg,&sig_on_dif);
3043  sinfo_get_clean_mean_window(img_of_dif,llx,lly,urx,ury,kappa,
3044  nclip,&avg,&sig_of_dif);
3045  */
3046 
3047  cpl_vector_delete(dit_on);
3048  cpl_vector_delete(dit_of);
3049  cpl_vector_delete(exptime_on);
3050  cpl_vector_delete(exptime_of);
3051 
3052  return res_tbl;
3053 
3054  cleanup:
3055  return NULL;
3056 
3057 }
3058 
3059 
3060 /*-------------------------------------------------------------------------*/
3072 /*--------------------------------------------------------------------------*/
3073 
3074 int
3075 sinfo_image_estimate_noise(cpl_image* img,
3076  const int noise_fit,
3077  double* centre,
3078  double* noise)
3079 {
3080 
3081  int nbins=0;
3082 
3083  int xsz=0;
3084  int ysz=0;
3085  int n=0;
3086  int i=0;
3087  int r=0;
3088 
3089  int ndist=0;
3090  double min_fct=HISTO_DIST_TEMPC_MIN_FCT;
3091  double max_fct=HISTO_DIST_TEMPC_MAX_FCT;
3092  double avg_d=0;
3093  double std_d=0;
3094  double hmin=0;
3095  double hmax=0;
3096  double kappa=3;
3097 
3098  double* pdata=NULL;
3099  double* disth=NULL;
3100  double* distx=NULL;
3101 
3102  double peak=0;
3103  double tempc=0;
3104  //double value=0;
3105  double x0=0;
3106  double sigma=0;
3107  double area=0;
3108  double offset=0;
3109  //double mse=0;
3110  //double chired=0;
3111 
3112  cpl_table* data_tbl=NULL;
3113  cpl_table* histo=NULL;
3114  cpl_table* dist=NULL;
3115  cpl_table* min_xi=NULL;
3116  cpl_table* tmp_tbl1=NULL;
3117  cpl_table* tmp_tbl2=NULL;
3118  cpl_vector* vx=NULL;
3119  cpl_vector* vy=NULL;
3120  cpl_vector* sx=NULL;
3121  cpl_vector* sy=NULL;
3122 
3123  // Get Object relevant information
3124  check_nomsg(xsz=cpl_image_get_size_x(img));
3125  check_nomsg(ysz=cpl_image_get_size_y(img));
3126  n=xsz*ysz;
3127  nbins=sqrt(n);
3128  check_nomsg(data_tbl=cpl_table_new(n));
3129  check_nomsg(cpl_table_new_column(data_tbl,"DATA",CPL_TYPE_DOUBLE));
3130 
3131  check_nomsg(pdata=cpl_image_get_data(img));
3132  for(i=0;i<n;i++) {
3133  if(!isnan(pdata[i])) {
3134  cpl_table_set_double(data_tbl,"DATA",r,pdata[i]);
3135  r++;
3136  }
3137  }
3138 
3139  check_nomsg(cpl_table_erase_invalid(data_tbl));
3140  check_nomsg(avg_d=cpl_table_get_column_mean(data_tbl,"DATA"));
3141  check_nomsg(std_d=cpl_table_get_column_stdev(data_tbl,"DATA"));
3142 
3143  cpl_table_save(data_tbl, NULL, NULL, "out_data.fits", CPL_IO_DEFAULT);
3144 
3145  hmin=avg_d-kappa*std_d;
3146  hmax=avg_d+kappa*std_d;
3147  //sinfo_msg("mean=%g stdv=%g",avg_d,std_d);
3148  //sinfo_msg("hmin=%g hmax=%g",hmin,hmax);
3149  //sinfo_msg("Computes histogram");
3150  ck0(sinfo_histogram(data_tbl,nbins,hmin,hmax,&histo),"building histogram");
3151 
3152  //value=(double)(hmax-hmin)/nbins/2.;
3153  //sinfo_msg("value=%10.8f",value);
3154  //cpl_table_save(histo, NULL, NULL, "out_pippo.fits", CPL_IO_DEFAULT0);
3155 
3156  check_nomsg(peak=cpl_table_get_column_max(histo,"HY"));
3157  //sinfo_msg("peak=%f",peak);
3158  sinfo_free_table(&tmp_tbl1);
3159 
3160  check_nomsg(tmp_tbl1=sinfo_extract_table_rows(histo,"HY",CPL_EQUAL_TO,peak));
3161 
3162  //cpl_table_save(tmp_tbl1, NULL, NULL, "out_tmp_tbl1.fits", CPL_IO_DEFAULT);
3163 
3164 
3165  check_nomsg(*centre=cpl_table_get_column_mean(tmp_tbl1,"HL"));
3166  //sinfo_msg("Background level=%f",*centre);
3167 
3168  sinfo_free_table(&tmp_tbl1);
3169  check_nomsg(tmp_tbl1=sinfo_extract_table_rows(histo,"HY",
3170  CPL_GREATER_THAN,
3171  peak/HISTO_Y_CUT));
3172  sinfo_free_table(&tmp_tbl2);
3173  check_nomsg(tmp_tbl2=sinfo_extract_table_rows(tmp_tbl1,"HY",
3174  CPL_LESS_THAN,peak));
3175  sinfo_free_table(&tmp_tbl1);
3176 
3177  check_nomsg(tempc=*centre-cpl_table_get_column_min(tmp_tbl2,"HL"));
3178  //sinfo_msg("min HX %f",cpl_table_get_column_min(tmp_tbl2,"HL"));
3179  sinfo_free_table(&tmp_tbl2);
3180  //sinfo_msg("Tempc=%f",tempc);
3181  check_nomsg(dist=sinfo_where_tab_min_max(histo,"HL",
3182  CPL_GREATER_THAN,
3183  *centre-min_fct*tempc,
3184  CPL_NOT_GREATER_THAN,
3185  *centre+max_fct*tempc));
3186 
3187  offset=cpl_table_get_column_min(histo,"HY");
3188  sinfo_free_table(&histo);
3189 
3190 
3191  check_nomsg(ndist=cpl_table_get_nrow(dist));
3192  check_nomsg(cpl_table_cast_column(dist,"HY","HYdouble",CPL_TYPE_DOUBLE));
3193  check_nomsg(disth=cpl_table_get_data_double(dist,"HYdouble"));
3194  check_nomsg(distx=cpl_table_get_data_double(dist,"HL"));
3195  cpl_table_save(dist, NULL, NULL, "out_dist.fits", CPL_IO_DEFAULT);
3196 
3197  //TODO
3198  //gaussfit(distx,disty,dista,nterms=3);
3199  //*noise=dista[2];
3200  *noise=tempc/2;
3201  /* THIS DOES NOT WORK */
3202  //sinfo_msg("FWHM/2=%f",*noise);
3203 
3204  if(noise_fit == 1) {
3205  check_nomsg(vy=cpl_vector_wrap(ndist,disth));
3206  check_nomsg(vx=cpl_vector_wrap(ndist,distx));
3207  check_nomsg(sx=cpl_vector_new(ndist));
3208  check_nomsg(cpl_vector_fill(sx,1.));
3209  check_nomsg(sy=cpl_vector_duplicate(sx));
3210  x0=*centre;
3211  sigma=tempc/2;
3212 
3213  if(CPL_ERROR_NONE != cpl_vector_fit_gaussian(vx,NULL,
3214  vy,NULL,
3215  CPL_FIT_ALL,
3216  &x0,&sigma,&area,&offset,
3217  NULL,NULL,NULL)) {
3218  cpl_error_reset();
3219  }
3220  //sinfo_msg("Gauss fit parameters:"
3221  // "x0=%f sigma=%f area=%f offset=%f mse=%f chired=%f",
3222  // x0,sigma,area,offset,mse,chired);
3223  //sinfo_msg("Background level=%f",*centre);
3224  //sinfo_msg("Noise=%f",sigma);
3225  *noise=sigma;
3226  sinfo_unwrap_vector(&vx);
3227  sinfo_unwrap_vector(&vy);
3228  sinfo_free_my_vector(&sx);
3229  sinfo_free_my_vector(&sy);
3230  }
3231  sinfo_free_table(&dist);
3232 
3233  return 0;
3234 
3235  cleanup:
3236  sinfo_free_table(&min_xi);
3237  sinfo_free_table(&tmp_tbl1);
3238  sinfo_free_table(&tmp_tbl2);
3239  sinfo_free_table(&histo);
3240  sinfo_free_table(&dist);
3241  sinfo_free_table(&data_tbl);
3242  sinfo_free_my_vector(&sx);
3243  sinfo_free_my_vector(&sy);
3244  sinfo_unwrap_vector(&vx);
3245  sinfo_unwrap_vector(&vy);
3246 
3247  return -1;
3248 
3249 }
3250 
3251 
3252 
3253 
3254 
3255 cpl_table* sinfo_compute_linearity(cpl_frameset* son, cpl_frameset* sof)
3256 {
3257 
3258  cpl_frame* frm=NULL;
3259 
3260  int* status=0;
3261  int non=0;
3262  int nof=0;
3263  int nfr=0;
3264  int i=0;
3265  double med_on=0;
3266  double avg_on=0;
3267  double med_of=0;
3268  double avg_of=0;
3269  double med_dit=0;
3270  double avg_dit=0;
3271 
3272  double med=0;
3273  double avg=0;
3274 
3275  char* name=NULL;
3276  cpl_image* img=NULL;
3277  cpl_vector* vec_adl=NULL;
3278  cpl_vector* vec_dit=NULL;
3279  cpl_vector* vec_avg=NULL;
3280  cpl_vector* vec_med=NULL;
3281  cpl_vector* vec_avg_dit=NULL;
3282  cpl_vector* vec_med_dit=NULL;
3283  cpl_propertylist* plist=NULL;
3284 
3285  double dit=0;
3286  cpl_table* lin_tbl=NULL;
3287 
3288 
3289  non = cpl_frameset_get_size(son);
3290  nof = cpl_frameset_get_size(sof);
3291  nfr = (non <= nof) ? non : nof;
3292 
3293  lin_tbl=cpl_table_new(nfr);
3294  cpl_table_new_column(lin_tbl,"med", CPL_TYPE_DOUBLE);
3295  cpl_table_new_column(lin_tbl,"avg", CPL_TYPE_DOUBLE);
3296  cpl_table_new_column(lin_tbl,"med_dit", CPL_TYPE_DOUBLE);
3297  cpl_table_new_column(lin_tbl,"avg_dit", CPL_TYPE_DOUBLE);
3298  cpl_table_new_column(lin_tbl,"dit", CPL_TYPE_DOUBLE);
3299  vec_med=cpl_vector_new(nfr);
3300  vec_avg=cpl_vector_new(nfr);
3301  vec_med_dit=cpl_vector_new(nfr);
3302  vec_avg_dit=cpl_vector_new(nfr);
3303  vec_dit=cpl_vector_new(nfr);
3304  vec_adl=cpl_vector_new(nfr);
3305 
3306  for(i=0;i<nfr;i++) {
3307  frm=cpl_frameset_get_frame(son,i);
3308  name=(char*)cpl_frame_get_filename(frm);
3309  img=cpl_image_load(name,CPL_TYPE_FLOAT,0,0);
3310  med_on=cpl_image_get_median(img);
3311  avg_on=cpl_image_get_mean(img);
3312  cpl_image_delete(img);
3313 
3314  frm=cpl_frameset_get_frame(sof,i);
3315  name=(char*)cpl_frame_get_filename(frm);
3316  img=cpl_image_load(name,CPL_TYPE_FLOAT,0,0);
3317  med_of=cpl_image_get_median(img);
3318  avg_of=cpl_image_get_mean(img);
3319  cpl_image_delete(img);
3320 
3321  med=med_on-med_of;
3322  avg=avg_on-avg_of;
3323  plist=cpl_propertylist_load(name,0);
3324  dit=(double)sinfo_pfits_get_dit(plist);
3325  cpl_propertylist_delete(plist);
3326  avg_dit=avg/dit;
3327  med_dit=med/dit;
3328 
3329  cpl_vector_set(vec_dit,i,dit);
3330  cpl_vector_set(vec_avg,i,avg);
3331  cpl_vector_set(vec_med,i,med);
3332  cpl_vector_set(vec_avg_dit,i,avg_dit);
3333  cpl_vector_set(vec_med_dit,i,med_dit);
3334 
3335  cpl_table_set_double(lin_tbl,"dit",i,dit);
3336  cpl_table_set_double(lin_tbl,"med",i,med);
3337  cpl_table_set_double(lin_tbl,"avg",i,avg);
3338  cpl_table_set_double(lin_tbl,"med_dit",i,med_dit);
3339  cpl_table_set_double(lin_tbl,"avg_dit",i,avg_dit);
3340 
3341  }
3342  cpl_table_new_column(lin_tbl,"adl", CPL_TYPE_DOUBLE);
3343  med_dit=cpl_vector_get_mean(vec_med_dit);
3344  avg_dit=cpl_vector_get_mean(vec_avg_dit);
3345 
3346  for(i=0;i<nfr;i++) {
3347  dit = cpl_table_get_double(lin_tbl,"dit",i,status);
3348  cpl_vector_set(vec_adl,i,dit*med_dit);
3349  cpl_table_set_double(lin_tbl,"adl",i,dit*med_dit);
3350  }
3351 
3352  cpl_vector_delete(vec_dit);
3353  cpl_vector_delete(vec_adl);
3354  cpl_vector_delete(vec_avg);
3355  cpl_vector_delete(vec_med);
3356  cpl_vector_delete(vec_avg_dit);
3357  cpl_vector_delete(vec_med_dit);
3358 
3359 
3360  return lin_tbl;
3361 
3362 }
3363 
3364 /*--------------------------------------------------------------------*/
3371 /*--------------------------------------------------------------------*/
3372 int
3373 sinfo_get_ron(cpl_frameset * framelist,
3374  const int ron_xmin,
3375  const int ron_xmax,
3376  const int ron_ymin,
3377  const int ron_ymax,
3378  const int ron_hsize,
3379  const int ron_nsamp,
3380  double** ron)
3381 {
3382  cpl_imagelist * iset =NULL;
3383  cpl_image * tmp_im =NULL;
3384  cpl_size zone[4] ;
3385  double rms =0;
3386  double ndit =0;
3387  cpl_frame * frame =NULL;
3388  int i;
3389  cpl_propertylist* plist=NULL;
3390 
3391  /* Test entries */
3392 
3393  if (framelist == NULL) return -1 ;
3394 
3395  /* Load the current set */
3396  if ((iset = sinfo_new_frameset_to_iset(framelist)) == NULL) {
3397  sinfo_msg_error( "Cannot load the data") ;
3398  return -1 ;
3399  }
3400 
3401  /* Initialise */
3402  zone[0]=ron_xmin;
3403  zone[1]=ron_xmax;
3404  zone[2]=ron_ymin;
3405  zone[3]=ron_ymax;
3406 
3407  /* Loop on all pairs */
3408  for (i=0 ; i<cpl_imagelist_get_size(iset)-1 ; i++) {
3409 
3410  /* Compute the current subtracted image */
3411  if ((tmp_im=cpl_image_subtract_create(cpl_imagelist_get(iset,i),
3412  cpl_imagelist_get(iset, i+1)))
3413  == NULL) {
3414  sinfo_msg_error( "Cannot subtract the images") ;
3415  sinfo_free_imagelist(&iset) ;
3416  return -1 ;
3417  }
3418 
3419  /* Compute the read-out noise */
3420  if (cpl_flux_get_noise_window(tmp_im, zone, ron_hsize,
3421  ron_nsamp, &rms, NULL) != CPL_ERROR_NONE) {
3422  sinfo_msg_error( "Cannot compute the RON") ;
3423  sinfo_free_image(&tmp_im) ;
3424  sinfo_free_imagelist(&iset) ;
3425  return -1 ;
3426  }
3427  sinfo_free_image(&tmp_im) ;
3428  /* Normalise the RON with NDIT */
3429  frame = cpl_frameset_get_frame(framelist, i) ;
3430  cknull_nomsg(plist=cpl_propertylist_load(cpl_frame_get_filename(frame),
3431  0));
3432  ndit=sinfo_pfits_get_ndit(plist);
3433  sinfo_free_propertylist(&plist);
3434 
3435  (*ron)[i] = rms * sqrt(ndit/2.0) ;
3436 
3437  }
3438 
3439  /* Free and return */
3440  sinfo_free_imagelist(&iset) ;
3441  return 0 ;
3442 
3443  cleanup:
3444  sinfo_free_image(&tmp_im);
3445  sinfo_free_imagelist(&iset);
3446  sinfo_free_propertylist(&plist);
3447  return -1;
3448 
3449 }
3450 
3451 
3452 
3453 /*---------------------------------------------------------------------------*/
3459 /*---------------------------------------------------------------------------*/
3460 int sinfo_stack_get_pro_tag(char * tag_in, char* tag_out)
3461 {
3462  /* Test entries */
3463  if (tag_in == NULL) return -1 ;
3464  /* here for the moment we set the same PRO ID as a non stacked frame */
3465  if (strcmp(tag_in,RAW_WAVE_LAMP_DITHER) == 0 ) {
3466  strcpy(tag_out,PRO_WAVE_LAMP_STACKED);
3467  return 0 ;
3468  }
3469 
3470 
3471  if (strcmp(tag_in,RAW_WAVE_LAMP) == 0 ) {
3472  strcpy(tag_out,PRO_WAVE_LAMP_STACKED);
3473  return 0 ;
3474  }
3475 
3476  if (strcmp(tag_in,RAW_WAVE_NS_DITHER) == 0 ) {
3477  strcpy(tag_out,PRO_WAVE_NS_STACKED);
3478  return 0 ;
3479  }
3480 
3481 
3482  if (strcmp(tag_in,RAW_WAVE_NS) == 0 ) {
3483  strcpy(tag_out,PRO_WAVE_NS_STACKED);
3484  return 0 ;
3485  }
3486 
3487 
3488  if (strcmp(tag_in,RAW_FIBRE_LAMP) == 0 ) {
3489  strcpy(tag_out,PRO_FIBRE_LAMP_STACKED);
3490  return 0 ;
3491  }
3492 
3493  if (strcmp(tag_in,RAW_FIBRE_EW) == 0 ) {
3494  strcpy(tag_out,PRO_FIBRE_EW_STACKED);
3495  return 0 ;
3496  }
3497 
3498  if (strcmp(tag_in,RAW_FIBRE_NS) == 0 ) {
3499  strcpy(tag_out,PRO_FIBRE_NS_STACKED);
3500  return 0 ;
3501  }
3502 
3503 
3504  if (strcmp(tag_in,PRO_FIBRE_NS_STACKED_ON) == 0 ) {
3505  strcpy(tag_out,PRO_FIBRE_NS_STACKED);
3506  return 0 ;
3507  }
3508 
3509  if (strcmp(tag_in,PRO_FIBRE_NS_STACKED) == 0 ) {
3510  strcpy(tag_out,PRO_FIBRE_NS_STACKED_DIST);
3511  return 0 ;
3512  }
3513 
3514 
3515  if (strcmp(tag_in,RAW_SLIT_LAMP) == 0 ) {
3516  strcpy(tag_out,PRO_SLIT_LAMP_STACKED);
3517  return 0 ;
3518  }
3519 
3520 
3521  if (strstr(tag_in, "FLUX") != NULL ) {
3522  strcpy(tag_out,PRO_FLUX_LAMP_STACKED);
3523  return 0 ;
3524  }
3525 
3526  if (strstr(tag_in, "PSF") != NULL ) {
3527  strcpy(tag_out,PRO_PSF_CALIBRATOR_STACKED);
3528  return 0 ;
3529  }
3530 
3531 
3532  if (strstr(tag_in, "FOCUS") != NULL ) {
3533  strcpy(tag_out,PRO_FOCUS_STACKED);
3534  return 0 ;
3535  }
3536 
3537  if (strstr(tag_in, "OBJECT_NODDING") != NULL ) {
3538  strcpy(tag_out,PRO_OBJECT_NODDING_STACKED);
3539  return 0 ;
3540  }
3541 
3542  if (strstr(tag_in, "SKY_NODDING") != NULL ) {
3543  strcpy(tag_out,PRO_SKY_NODDING_STACKED);
3544  return 0 ;
3545  }
3546 
3547  if (strstr(tag_in, "STD_NODDING") != NULL ) {
3548  strcpy(tag_out,PRO_STD_NODDING_STACKED);
3549  return 0 ;
3550  }
3551 
3552  if (strstr(tag_in, "OBJECT_SKYSPIDER") != NULL ) {
3553  strcpy(tag_out,PRO_OBJECT_SKYSPIDER_STACKED);
3554  return 0 ;
3555  }
3556 
3557 
3558  if (strstr(tag_in, RAW_STD) != NULL ) {
3559  strcpy(tag_out,PRO_STD_STACKED);
3560  return 0 ;
3561  }
3562 
3563 
3564  if (strstr(tag_in, RAW_SKY_STD) != NULL ) {
3565  strcpy(tag_out,PRO_SKY_STD_STACKED);
3566  return 0 ;
3567  }
3568 
3569  if (strstr(tag_in, RAW_SKY_OH) != NULL ) {
3570  strcpy(tag_out,PRO_SKY_OH_STACKED);
3571  return 0 ;
3572  }
3573 
3574  if (strstr(tag_in, RAW_SKY_PSF_CALIBRATOR) != NULL ) {
3575  strcpy(tag_out,PRO_SKY_PSF_CALIBRATOR_STACKED);
3576  return 0 ;
3577  }
3578 
3579  if (strstr(tag_in, RAW_STD_STAR) != NULL ) {
3580  strcpy(tag_out,PRO_STD_STAR_STACKED);
3581  return 0 ;
3582  }
3583 
3584  if (strstr(tag_in, RAW_STD_STAR) != NULL ) {
3585  strcpy(tag_out,PRO_STD_STAR_DITHER_STACKED);
3586  return 0 ;
3587  }
3588 
3589  if (strstr(tag_in, RAW_SKY) != NULL ) {
3590  strcpy(tag_out,PRO_SKY_STACKED);
3591  return 0 ;
3592  }
3593 
3594 
3595  return 1 ;
3596 }
3597 
3598 
3599 int sinfo_is_dark(char * tag)
3600 {
3601  /* Test entries */
3602  if (tag == NULL) return -1 ;
3603 
3604  if (!strcmp(tag, RAW_DARK)) return 1 ;
3605  if (!strcmp(tag, PRO_MASTER_DARK)) return 1 ;
3606  return 0 ;
3607 }
3608 
3609 int sinfo_is_flat_bp(char * tag)
3610 {
3611  /* Test entries */
3612  if (tag == NULL) return -1 ;
3613 
3614  if (!strcmp(tag, RAW_LINEARITY_LAMP)) return 1 ;
3615  return 0 ;
3616 }
3617 
3618 int sinfo_is_flat_lindet(char * tag)
3619 {
3620  /* Test entries */
3621  if (tag == NULL) return -1 ;
3622 
3623  if (!strcmp(tag, RAW_LINEARITY_LAMP)) return 1 ;
3624  return 0 ;
3625 }
3626 
3627 
3628 int sinfo_blank2dot(const char * in, char* ou)
3629 {
3630  int len=0;
3631  int i=0;
3632 
3633  strcpy(ou,in);
3634  len = strlen(in);
3635  for (i=0;i<len;i++)
3636  {
3637  if (in[i] == ' ') {
3638  ou[i] = '.';
3639  }
3640  }
3641  return 0;
3642 }
3643 
3644 
3645 int sinfo_is_sky_flat(char * tag)
3646 {
3647  /* Test entries */
3648  if (tag == NULL) return -1 ;
3649  if (!strcmp(tag, RAW_FLAT_SKY)) return 1 ;
3650  return 0 ;
3651 }
3652 
3653 
3654 
3655 int sinfo_is_master_flat(char * tag)
3656 {
3657  /* Test entries */
3658  if (tag == NULL) return -1 ;
3659 
3660  if (!strcmp(tag, PRO_MASTER_FLAT_LAMP)) return 1 ;
3661  if (!strcmp(tag, PRO_MASTER_FLAT_LAMP1)) return 1 ;
3662  return 0 ;
3663 }
3664 
3665 int sinfo_is_master_flat_dither(char * tag)
3666 {
3667  /* Test entries */
3668  if (tag == NULL) return -1 ;
3669 
3670  if (!strcmp(tag, PRO_MASTER_FLAT_LAMP2)) return 1 ;
3671  return 0 ;
3672 }
3673 
3674 /*---------------------------------------------------------------------------*/
3680 /*---------------------------------------------------------------------------*/
3681 int sinfo_is_stack(char * tag)
3682 {
3683  /* Test entries */
3684  if (tag == NULL) return -1 ;
3685 
3686  if (strstr(tag, PRO_STACKED) != NULL) return 1 ;
3687  return 0 ;
3688 }
3689 
3690 int sinfo_is_mflat(char * tag)
3691 {
3692  /* Test entries */
3693  if (tag == NULL) return -1 ;
3694 
3695  if (!strcmp(tag, PRO_MASTER_FLAT_LAMP)) return 1 ;
3696  if (!strcmp(tag, PRO_MASTER_FLAT_LAMP1)) return 1 ;
3697  if (!strcmp(tag, PRO_MASTER_FLAT_LAMP2)) return 1 ;
3698  return 0 ;
3699 }
3700 
3701 
3702 /*---------------------------------------------------------------------------*/
3708 /*---------------------------------------------------------------------------*/
3709 int sinfo_is_psf_calibrator_stacked(char * tag)
3710 {
3711  /* Test entries */
3712  if (tag == NULL) return -1 ;
3713 
3714  if (!strcmp(tag, PRO_PSF_CALIBRATOR_STACKED)) return 1 ;
3715  return 0 ;
3716 }
3717 /*---------------------------------------------------------------------------*/
3723 /*---------------------------------------------------------------------------*/
3724 int sinfo_is_focus_stacked(char * tag)
3725 {
3726  /* Test entries */
3727  if (tag == NULL) return -1 ;
3728 
3729  if (!strcmp(tag, PRO_FOCUS_STACKED)) return 1 ;
3730  return 0 ;
3731 }
3732 
3733 /*---------------------------------------------------------------------------*/
3739 /*---------------------------------------------------------------------------*/
3740 int sinfo_is_lamp_wave_stacked(char * tag)
3741 {
3742  /* Test entries */
3743  if (tag == NULL) return -1 ;
3744 
3745  if (!strcmp(tag, PRO_WAVE_LAMP_STACKED)) return 1 ;
3746  return 0 ;
3747 }
3748 
3749 /*---------------------------------------------------------------------------*/
3755 /*---------------------------------------------------------------------------*/
3756 int sinfo_is_lamp_flux_stacked(char * tag)
3757 {
3758  /* Test entries */
3759  if (tag == NULL) return -1 ;
3760 
3761  if (!strcmp(tag, PRO_FLUX_LAMP_STACKED)) return 1 ;
3762  return 0 ;
3763 }
3764 
3765 /*---------------------------------------------------------------------------*/
3771 /*---------------------------------------------------------------------------*/
3772 int sinfo_is_object_nodding_stacked(char * tag)
3773 {
3774  /* Test entries */
3775  if (tag == NULL) return -1 ;
3776 
3777  if (!strcmp(tag, PRO_OBJECT_NODDING_STACKED)) return 1 ;
3778  return 0 ;
3779 }
3780 
3781 /*---------------------------------------------------------------------------*/
3787 /*---------------------------------------------------------------------------*/
3788 int sinfo_is_object_skyspider_stacked(char * tag)
3789 {
3790  /* Test entries */
3791  if (tag == NULL) return -1 ;
3792 
3793  if (!strcmp(tag, PRO_OBJECT_SKYSPIDER_STACKED)) return 1 ;
3794  return 0 ;
3795 }
3796 
3797 
3798 /*---------------------------------------------------------------------------*/
3804 /*---------------------------------------------------------------------------*/
3805 int sinfo_is_sky_nodding_stacked(char * tag)
3806 {
3807  /* Test entries */
3808  if (tag == NULL) return -1 ;
3809 
3810  if (!strcmp(tag, PRO_SKY_NODDING_STACKED)) return 1 ;
3811  return 0 ;
3812 }
3813 
3814 /*---------------------------------------------------------------------------*/
3820 /*---------------------------------------------------------------------------*/
3821 int sinfo_is_wavemap(char * tag)
3822 {
3823  /* Test entries */
3824  if (tag == NULL) return -1 ;
3825 
3826  if (!strcmp(tag, PRO_WAVE_MAP)) return 1 ;
3827  return 0 ;
3828 }
3829 
3830 /*---------------------------------------------------------------------------*/
3836 /*---------------------------------------------------------------------------*/
3837 int sinfo_is_halosp(char * tag)
3838 {
3839  /* Test entries */
3840  if (tag == NULL) return -1 ;
3841 
3842  if (!strcmp(tag, PRO_HALO_SPECT)) return 1 ;
3843  return 0 ;
3844 }
3845 
3846 /*---------------------------------------------------------------------------*/
3852 /*---------------------------------------------------------------------------*/
3853 int sinfo_is_distlist(char * tag)
3854 {
3855  /* Test entries */
3856  if (tag == NULL) return -1 ;
3857 
3858  if (!strcmp(tag, PRO_SLITLETS_DISTANCE)) return 1 ;
3859  return 0 ;
3860 }
3861 
3862 /*---------------------------------------------------------------------------*/
3868 /*---------------------------------------------------------------------------*/
3869 int sinfo_is_slitpos(char * tag)
3870 {
3871  /* Test entries */
3872  if (tag == NULL) return -1 ;
3873 
3874  if (!strcmp(tag, PRO_SLIT_POS)) return 1 ;
3875  return 0 ;
3876 }
3877 
3878 /*---------------------------------------------------------------------------*/
3884 /*---------------------------------------------------------------------------*/
3885 int sinfo_is_firstcol(char * tag)
3886 {
3887  /* Test entries */
3888  if (tag == NULL) return -1 ;
3889 
3890  if (!strcmp(tag, PRO_FIRST_COL)) return 1 ;
3891  return 0 ;
3892 }
3893 
3894 /*---------------------------------------------------------------------------*/
3900 /*---------------------------------------------------------------------------*/
3901 int sinfo_is_bpmap(char * tag)
3902 {
3903  /* Test entries */
3904  if (tag == NULL) return -1 ;
3905 
3906  if (!strcmp(tag, PRO_BP_MAP)) return 1 ;
3907  return 0 ;
3908 }
3909 
3910 
3911 /*---------------------------------------------------------------------------*/
3920 /*---------------------------------------------------------------------------*/
3921 
3922 int sinfo_get_band(cpl_frame * ref_frame,char * band)
3923 {
3924 
3925  char* ref_file=NULL;
3926  cpl_propertylist* plist=NULL;
3927 
3928  ref_file = cpl_strdup(cpl_frame_get_filename(ref_frame)) ;
3929  if ((cpl_error_code)((plist = cpl_propertylist_load(ref_file, 0)) == NULL)) {
3930  sinfo_msg_error( "getting header from reference frame %s",ref_file);
3931  cpl_propertylist_delete(plist) ;
3932  return -1 ;
3933  }
3934 
3935  if (sinfo_propertylist_has(plist, KEY_NAME_FILT_NAME)) {
3936  strcpy(band, cpl_propertylist_get_string(plist, KEY_NAME_FILT_NAME));
3937  /* sinfo_msg("%s value is %s", KEY_NAME_FILT_NAME, band); */
3938 
3939  } else {
3940  sinfo_msg_warning("keyword %s does not exist",KEY_NAME_FILT_NAME);
3941  return -1;
3942  }
3943 
3944  cpl_free(ref_file);
3945  cpl_propertylist_delete(plist);
3946  return 0;
3947 }
3948 
3949 
3950 
3951 
3952 /*---------------------------------------------------------------------------*/
3960 /*---------------------------------------------------------------------------*/
3961 
3962 int sinfo_get_obsname(cpl_frame * ref_frame, const char* obs_name)
3963 {
3964 
3965  char* ref_file=NULL;
3966  cpl_propertylist* plist=NULL;
3967 
3968  ref_file = cpl_strdup(cpl_frame_get_filename(ref_frame)) ;
3969  if ((cpl_error_code)((plist = cpl_propertylist_load(ref_file, 0)) == NULL)) {
3970  sinfo_msg_error( "getting header from reference frame %s",ref_file);
3971  cpl_propertylist_delete(plist) ;
3972  return -1 ;
3973  }
3974 
3975  if (sinfo_propertylist_has(plist, KEY_NAME_OBS_NAME)) {
3976  strcpy((char*)obs_name, cpl_propertylist_get_string(plist,
3977  KEY_NAME_OBS_NAME));
3978  /* sinfo_msg("%s value is %s", KEY_NAME_OBS_NAME, obs_name); */
3979 
3980  } else {
3981  sinfo_msg_warning("keyword %s does not exist",KEY_NAME_OBS_NAME);
3982  return -1;
3983  }
3984 
3985  cpl_free(ref_file);
3986  cpl_propertylist_delete(plist);
3987  return 0;
3988 }
3989 
3990 
3991 
3992 
3993 
3994 /*---------------------------------------------------------------------------*/
4002 /*---------------------------------------------------------------------------*/
4003 
4004 int sinfo_get_keyvalue_int(cpl_frame * ref_frame, const char* key_name)
4005 {
4006 
4007  char* ref_file=NULL;
4008  cpl_propertylist* plist=NULL;
4009  int result=0;
4010 
4011  ref_file = cpl_strdup(cpl_frame_get_filename(ref_frame)) ;
4012 
4013  if ((cpl_error_code)((plist = cpl_propertylist_load(ref_file, 0)) == NULL)) {
4014  sinfo_msg_error( "getting header from reference frame %s",ref_file);
4015  cpl_propertylist_delete(plist) ;
4016  return -1;
4017  }
4018 
4019 
4020  if (sinfo_propertylist_has(plist, key_name)) {
4021  result=cpl_propertylist_get_int(plist,key_name);
4022  } else {
4023  sinfo_msg_warning("keyword %s does not exist",key_name);
4024  return -1;
4025  }
4026 
4027  cpl_free(ref_file);
4028  cpl_propertylist_delete(plist);
4029 
4030  return result;
4031 }
4032 
4033 
4034 
4035 /*---------------------------------------------------------------------------*/
4043 /*---------------------------------------------------------------------------*/
4044 
4045 float sinfo_get_keyvalue_float(cpl_frame * ref_frame, const char* key_name)
4046 {
4047 
4048  char* ref_file=NULL;
4049  cpl_propertylist* plist=NULL;
4050  float result=0;
4051 
4052  ref_file = cpl_strdup(cpl_frame_get_filename(ref_frame)) ;
4053 
4054  if ((cpl_error_code)((plist = cpl_propertylist_load(ref_file, 0)) == NULL)) {
4055  sinfo_msg_error( "getting header from reference frame %s",ref_file);
4056  cpl_propertylist_delete(plist) ;
4057  return -1;
4058  }
4059 
4060 
4061  if (sinfo_propertylist_has(plist, key_name)) {
4062  result=cpl_propertylist_get_float(plist,key_name);
4063  } else {
4064  sinfo_msg_warning("keyword %s does not exist",key_name);
4065  return -1;
4066  }
4067 
4068  cpl_free(ref_file);
4069  cpl_propertylist_delete(plist);
4070 
4071  return result;
4072 }
4073 
4074 
4075 /*---------------------------------------------------------------------------*/
4083 /*---------------------------------------------------------------------------*/
4084 
4085 char sinfo_get_keyvalue_bool(cpl_frame * ref_frame, const char* key_name)
4086 {
4087 
4088  char* ref_file=NULL;
4089  cpl_propertylist* plist=NULL;
4090  int res_val=0;
4091 
4092  ref_file = cpl_strdup(cpl_frame_get_filename(ref_frame)) ;
4093 
4094  if ((cpl_error_code)((plist = cpl_propertylist_load(ref_file, 0)) == NULL)) {
4095  sinfo_msg_error( "getting header from reference frame %s",ref_file);
4096  cpl_propertylist_delete(plist) ;
4097  return '0';
4098  }
4099 
4100 
4101  if (sinfo_propertylist_has(plist, key_name)) {
4102  res_val=cpl_propertylist_get_bool(plist,key_name);
4103  } else {
4104  sinfo_msg_warning("keyword %s does not exist",key_name);
4105  return '0';
4106  }
4107 
4108  cpl_free(ref_file);
4109  cpl_propertylist_delete(plist);
4110  if(res_val == 1) {
4111  return 'T';
4112  } else {
4113  return 'F';
4114  }
4115 }
4116 
4117 
4118 
4119 
4120 /*---------------------------------------------------------------------------*/
4128 /*---------------------------------------------------------------------------*/
4129 
4130 const char*
4131 sinfo_get_keyvalue_string(cpl_frame * ref_frame, const char* key_name)
4132 {
4133 
4134  char* ref_file=NULL;
4135  cpl_propertylist* plist=NULL;
4136  const char* result=NULL;
4137 
4138  ref_file = cpl_strdup(cpl_frame_get_filename(ref_frame)) ;
4139 
4140  if ((cpl_error_code)((plist = cpl_propertylist_load(ref_file, 0)) == NULL)) {
4141  sinfo_msg_error( "getting header from reference frame %s",ref_file);
4142  cpl_propertylist_delete(plist) ;
4143  return FALSE;
4144  }
4145 
4146 
4147  if (sinfo_propertylist_has(plist, key_name)) {
4148  result=cpl_propertylist_get_string(plist,key_name);
4149  } else {
4150  sinfo_msg_warning("keyword %s does not exist",key_name);
4151  return NULL;
4152  }
4153 
4154  cpl_free(ref_file);
4155  cpl_propertylist_delete(plist);
4156 
4157  return result;
4158 }
4159 
4160 
4161 
4162 double sinfo_get_mjd_obs(cpl_frame * frame)
4163 {
4164  cpl_propertylist* plist=NULL;
4165  const char* file=NULL;
4166 
4167  double mjd_obs=0.;
4168  file = cpl_frame_get_filename(frame) ;
4169 
4170  if ((cpl_error_code)((plist = cpl_propertylist_load(file, 0)) == NULL)) {
4171  sinfo_msg_error( "getting header from reference frame %s",file);
4172  sinfo_free_propertylist(&plist) ;
4173  return -1 ;
4174  }
4175 
4176  if (sinfo_propertylist_has(plist, KEY_NAME_MJD_OBS)) {
4177  mjd_obs=cpl_propertylist_get_double(plist, KEY_NAME_MJD_OBS);
4178  } else {
4179  sinfo_msg_error("keyword %s does not exist",KEY_NAME_MJD_OBS);
4180  sinfo_free_propertylist(&plist) ;
4181  return -1;
4182  }
4183  sinfo_free_propertylist(&plist) ;
4184 
4185  return mjd_obs;
4186 
4187 }
4188 
4189 
4190 
4191 
4192 double sinfo_get_cumoffsetx(cpl_frame * frame)
4193 {
4194  cpl_propertylist* plist=NULL;
4195  char* file=NULL;
4196 
4197  double result=0.;
4198  file = cpl_strdup( cpl_frame_get_filename(frame)) ;
4199 
4200  if ((cpl_error_code)((plist = cpl_propertylist_load(file, 0)) == NULL)) {
4201  sinfo_msg_error( "getting header from reference frame %s",file);
4202  cpl_propertylist_delete(plist) ;
4203  cpl_free(file);
4204  return -1 ;
4205  }
4206 
4207  if (sinfo_propertylist_has(plist, KEY_NAME_CUMOFFX)) {
4208  result=cpl_propertylist_get_double(plist, KEY_NAME_CUMOFFX);
4209  } else {
4210  sinfo_msg_error("keyword %s does not exist",KEY_NAME_CUMOFFX);
4211  cpl_propertylist_delete(plist) ;
4212  return -1;
4213  }
4214  cpl_propertylist_delete(plist) ;
4215  cpl_free(file);
4216 
4217  return result;
4218 
4219 }
4220 
4221 
4222 
4223 
4224 double sinfo_get_cumoffsety(cpl_frame * frame)
4225 {
4226  cpl_propertylist* plist=NULL;
4227  char* file=NULL;
4228 
4229  double result=0.;
4230  file = cpl_strdup( cpl_frame_get_filename(frame)) ;
4231 
4232  if ((cpl_error_code)((plist = cpl_propertylist_load(file, 0)) == NULL)) {
4233  sinfo_msg_error( "getting header from reference frame %s",file);
4234  cpl_propertylist_delete(plist) ;
4235  cpl_free(file);
4236  return -1 ;
4237  }
4238 
4239  if (sinfo_propertylist_has(plist, KEY_NAME_CUMOFFY)) {
4240  result=cpl_propertylist_get_double(plist, KEY_NAME_CUMOFFY);
4241  } else {
4242  sinfo_msg_error("keyword %s does not exist",KEY_NAME_CUMOFFY);
4243  cpl_propertylist_delete(plist) ;
4244  return -1;
4245  }
4246  cpl_propertylist_delete(plist) ;
4247  cpl_free(file);
4248 
4249  return result;
4250 
4251 }
4252 
4253 int sinfo_frame_is_dither(cpl_frame * frame)
4254 {
4255 
4256  char file[256];
4257  char band[FILE_NAME_SZ];
4258 
4259 
4260  cpl_propertylist* plist=NULL;
4261  int grat_encoder=0;
4262  int dith_status=1;
4263  int len=0;
4264 
4265 
4266  cknull(frame,"Null input frame. Exit!");
4267 
4268  cknull_nomsg(strcpy(file,cpl_frame_get_filename(frame)));
4269  len= strlen(file);
4270 
4271  if(len<1) goto cleanup;
4272  if(sinfo_file_exists(file)==0) goto cleanup;
4273  //file = cpl_strdup(cpl_frame_get_filename(frame)) ;
4274  cknull(plist = cpl_propertylist_load(file, 0),
4275  "getting header from reference frame %s",file);
4276 
4277  if (sinfo_propertylist_has(plist, KEY_NAME_FILT_NAME)) {
4278  strcpy(band,cpl_propertylist_get_string(plist, KEY_NAME_FILT_NAME));
4279  } else {
4280  sinfo_msg_error("keyword %s does not exist",KEY_NAME_FILT_NAME);
4281  sinfo_free_propertylist(&plist) ;
4282  return -1;
4283  }
4284 
4285  if (sinfo_propertylist_has(plist, KEY_NAME_GRAT_ENC)) {
4286  grat_encoder = cpl_propertylist_get_int(plist, KEY_NAME_GRAT_ENC);
4287  } else {
4288  sinfo_msg_error("keyword %s does not exist",KEY_NAME_GRAT_ENC);
4289  sinfo_free_propertylist(&plist) ;
4290  return -1;
4291  }
4292 
4293  sinfo_free_propertylist(&plist) ;
4294 
4295  if (strcmp(band,"H") == 0) {
4296  if( abs(grat_encoder - GRAT_VAL2_H) <= GRAT_VAL_TOL ) {
4297  dith_status = 0;
4298  } else {
4299  dith_status = 0;
4300  }
4301  }
4302  else if (strcmp(band,"H+K") == 0) {
4303  if( abs(grat_encoder - GRAT_VAL2_HK) <= GRAT_VAL_TOL ) {
4304  dith_status = 0;
4305  } else {
4306  dith_status = 0;
4307  }
4308  }
4309  else if (strcmp(band,"K") == 0) {
4310  if( abs(grat_encoder - GRAT_VAL2_K) <= GRAT_VAL_TOL ) {
4311  dith_status = 0;
4312  } else {
4313  dith_status = 0;
4314  }
4315  }
4316  else if (strcmp(band,"J") == 0) {
4317  if( abs(grat_encoder - GRAT_VAL2_J) <= GRAT_VAL_TOL ) {
4318  dith_status = 0;
4319  } else {
4320  dith_status = 0;
4321  }
4322  }
4323  cleanup:
4324 
4325  sinfo_free_propertylist(&plist) ;
4326  if(cpl_error_get_code() != CPL_ERROR_NONE) {
4327  return -1;
4328  } else {
4329  return dith_status;
4330  }
4331 }
4332 
4333 /*---------------------------------------------------------------------------*/
4342 /*---------------------------------------------------------------------------*/
4343 
4344 int sinfo_get_spatial_res(cpl_frame * ref_frame, char * spat_res)
4345 {
4346 
4347  const char* ref_file;
4348  cpl_propertylist* plist=NULL;
4349 
4350  ref_file=cpl_frame_get_filename(ref_frame) ;
4351  if ((cpl_error_code)((plist = cpl_propertylist_load(ref_file, 0)) == NULL)) {
4352  sinfo_msg_error( "getting header from reference frame %s",ref_file);
4353  sinfo_free_propertylist(&plist) ;
4354  return -1 ;
4355 
4356  }
4357 
4358  if (sinfo_propertylist_has(plist, KEY_NAME_PREOPTICS)) {
4359  strcpy(spat_res,cpl_propertylist_get_string(plist, KEY_NAME_PREOPTICS));
4360  /* sinfo_msg("%s value is %s", KEY_NAME_PREOPTICS, spat_res); */
4361  } else {
4362  sinfo_msg_warning("keyword %s does not exist",KEY_NAME_PREOPTICS);
4363  sinfo_free_propertylist(&plist);
4364  return -1;
4365  }
4366  sinfo_free_propertylist(&plist);
4367  return 0;
4368 
4369 }
4370 
4371 /*---------------------------------------------------------------------------*/
4379 /*---------------------------------------------------------------------------*/
4380 
4381 int sinfo_frame_is_sky(cpl_frame * ref_frame)
4382 {
4383 
4384  char dpr_type[FILE_NAME_SZ];
4385  char* ref_file=NULL;
4386  const char* sval=NULL;
4387 
4388  int result=0;
4389  cpl_propertylist* plist=NULL;
4390 
4391  sval = cpl_frame_get_filename(ref_frame) ;
4392  ref_file = cpl_strdup(sval) ;
4393 
4394  if ((cpl_error_code)((plist = cpl_propertylist_load(ref_file, 0)) == NULL)) {
4395  sinfo_msg_error( "getting header from reference frame %s",ref_file);
4396  cpl_propertylist_delete(plist) ;
4397  cpl_free(ref_file);
4398  return -1 ;
4399  }
4400 
4401  if (sinfo_propertylist_has(plist, KEY_NAME_DPR_TYPE)) {
4402  strcpy(dpr_type,cpl_propertylist_get_string(plist, KEY_NAME_DPR_TYPE));
4403  /* sinfo_msg("%s value is %d", KEY_NAME_DPR_TYPE, dpr_type); */
4404  } else {
4405  sinfo_msg_warning("keyword %s does not exist",KEY_NAME_DPR_TYPE);
4406  cpl_propertylist_delete(plist) ;
4407  cpl_free(ref_file);
4408  return -1;
4409  }
4410  cpl_propertylist_delete(plist);
4411  if(strstr(dpr_type,RAW_SKY) != NULL) {
4412  result=1;
4413  }
4414  cpl_free(ref_file);
4415 
4416  return result;
4417 
4418 }
4419 
4420 
4421 /*---------------------------------------------------------------------------*/
4429 /*---------------------------------------------------------------------------*/
4430 
4431 int sinfo_tag_is_sky(char * tag)
4432 {
4433 
4434  int result=0;
4435 
4436  if(
4437  (strcmp(tag,RAW_SKY) == 0) ||
4438  (strcmp(tag,RAW_IMAGE_PRE_SKY) == 0) ||
4439  (strcmp(tag,RAW_SKY_NODDING) == 0) ||
4440  (strcmp(tag,RAW_SKY_JITTER) == 0) ||
4441  (strcmp(tag,RAW_SKY_STD) == 0) ||
4442  (strcmp(tag,RAW_FIBRE_DARK) == 0) ||
4443  (strcmp(tag,RAW_SKY_OH) == 0) ||
4444  (strcmp(tag,RAW_SKY_PSF_CALIBRATOR) == 0)
4445  ) {
4446  result=1;
4447  }
4448 
4449  return result;
4450 
4451 }
4452 
4453 
4454 /*---------------------------------------------------------------------------*/
4462 /*---------------------------------------------------------------------------*/
4463 
4464 int sinfo_tag_is_obj(char * tag)
4465 {
4466 
4467  int result=0;
4468 
4469  if(
4470  (strcmp(tag,RAW_PUPIL_LAMP) == 0) ||
4471  (strcmp(tag,RAW_OBJECT) == 0) ||
4472  (strcmp(tag,RAW_IMAGE_PRE_OBJECT) == 0) ||
4473  (strcmp(tag,RAW_OBJECT_NODDING) == 0) ||
4474  (strcmp(tag,RAW_OBJECT_JITTER) == 0) ||
4475  (strcmp(tag,RAW_PSF_CALIBRATOR) == 0) ||
4476  (strcmp(tag,RAW_FIBRE_PSF) == 0) ||
4477  (strcmp(tag,RAW_STD) == 0) ||
4478  (strcmp(tag,RAW_STD_STAR) == 0)
4479 
4480  ) {
4481  result=1;
4482  }
4483 
4484  return result;
4485 
4486 }
4487 
4488 /*---------------------------------------------------------------------------*/
4496 /*---------------------------------------------------------------------------*/
4497 
4498 int sinfo_tag_is_objpro(char * tag)
4499 {
4500 
4501  int result=0;
4502 
4503  if(
4504  (strcmp(tag,PRO_COADD_OBJ) == 0) ||
4505  (strcmp(tag,PRO_COADD_PSF) == 0) ||
4506  (strcmp(tag,PRO_COADD_STD) == 0) ||
4507  (strcmp(tag,PRO_OBS_OBJ) == 0) ||
4508  (strcmp(tag,PRO_OBS_PSF) == 0) ||
4509  (strcmp(tag,PRO_OBS_STD) == 0) ||
4510  (strcmp(tag,PRO_PSF_CALIBRATOR_STACKED) == 0) ||
4511  (strcmp(tag,PRO_SKY_PSF_CALIBRATOR_STACKED) == 0) ||
4512  (strcmp(tag,PRO_STD_STACKED) == 0) ||
4513  (strcmp(tag,PRO_SKY_STD_STACKED) == 0) ||
4514  (strcmp(tag,PRO_OBJECT_NODDING_STACKED) == 0) ||
4515  (strcmp(tag,PRO_SKY_NODDING_STACKED) == 0)
4516  ) {
4517  result=1;
4518  }
4519 
4520  return result;
4521 
4522 }
4523 
4524 
4525 /*---------------------------------------------------------------------------*/
4533 /*---------------------------------------------------------------------------*/
4534 
4535 int sinfo_frame_is_on(cpl_frame * ref_frame)
4536 {
4537 
4538  char ref_file[FILE_NAME_SZ];
4539  char dpr_type[FILE_NAME_SZ];
4540  int lamp_Xe=0;
4541  int lamp_Kr=0;
4542  int lamp_Ne=0;
4543  int lamp_Ar=0;
4544  int lamp_Halo=0;
4545  int len=0;
4546  int result=0;
4547  cpl_propertylist* plist=NULL;
4548  const char* filename=NULL;
4549  cknull(ref_frame,"Null input frame. Exit!");
4550 
4551  cknull_nomsg(filename=cpl_frame_get_filename(ref_frame));
4552  len= strlen(filename);
4553  if(len<1) goto cleanup;
4554 
4555  check_nomsg(strcpy(ref_file, filename)) ;
4556  if ((cpl_error_code)((plist = cpl_propertylist_load(ref_file,0)) == NULL)) {
4557  sinfo_msg_error( "getting header from reference frame %s",ref_file);
4558  sinfo_free_propertylist(&plist) ;
4559  return -1 ;
4560  }
4561 
4562  /*-----------------------------------------------------------------------
4563  in J Argon (4)
4564  in H Xenon and Argon (1+4)
4565  in K Neon (3)
4566  in H+K Xenon (1)
4567  -------------------------------------------------------------------------*/
4568  if (sinfo_propertylist_has(plist, KEY_NAME_DPR_TYPE)) {
4569  strcpy(dpr_type,cpl_propertylist_get_string(plist, KEY_NAME_DPR_TYPE));
4570  /* sinfo_msg("%s value is %s", KEY_NAME_DPR_TYPE, dpr_type); */
4571  } else {
4572  sinfo_msg_warning("keyword %s does not exist",KEY_NAME_DPR_TYPE);
4573  sinfo_free_propertylist(&plist);
4574  return -1;
4575  }
4576 
4577  /*
4578  In order to use the frame tag to identify frames we have to add this line
4579  strcpy(dpr_type,cpl_frame_get_tag(ref_frame));
4580 
4581  */
4582 
4583  if(strstr(dpr_type,"STD") != NULL) {
4584  result = 1;
4585  sinfo_free_propertylist(&plist);
4586  return result;
4587  }
4588 
4589  if(strstr(dpr_type,"PSF") != NULL) {
4590  result = 1;
4591  sinfo_free_propertylist(&plist);
4592  return result;
4593  }
4594 
4595  if(strstr(dpr_type,"SKY") != NULL) {
4596  result = 0;
4597  sinfo_free_propertylist(&plist);
4598  return result;
4599  }
4600 
4601 
4602  if(strstr(dpr_type,"OBJECT") != NULL) {
4603  result = 1;
4604  sinfo_free_propertylist(&plist);
4605  return result;
4606  }
4607  /*
4608  if(strstr(dpr_type,"PUPIL") != NULL) {
4609  result = 1;
4610  cpl_propertylist_delete(plist);
4611  return result;
4612  }
4613  */
4614 
4615  if (sinfo_propertylist_has(plist, KEY_NAME_LAMP_XE)) {
4616  lamp_Xe=cpl_propertylist_get_bool(plist, KEY_NAME_LAMP_XE);
4617  /* sinfo_msg("%s value is %d", KEY_NAME_LAMP_XE, lamp_Xe); */
4618  } else {
4619  sinfo_msg_warning("keyword %s does not exist",KEY_NAME_LAMP_XE);
4620  sinfo_free_propertylist(&plist);
4621  return -1;
4622  }
4623 
4624  if (sinfo_propertylist_has(plist, KEY_NAME_LAMP_KR)) {
4625  lamp_Kr=cpl_propertylist_get_bool(plist, KEY_NAME_LAMP_KR);
4626  /* sinfo_msg("%s value is %d", KEY_NAME_LAMP_KR, lamp_Kr); */
4627  } else {
4628  sinfo_msg_warning("keyword %s does not exist",KEY_NAME_LAMP_KR);
4629  sinfo_free_propertylist(&plist);
4630  return -1;
4631  }
4632 
4633  if (sinfo_propertylist_has(plist, KEY_NAME_LAMP_NE)) {
4634  lamp_Ne=cpl_propertylist_get_bool(plist, KEY_NAME_LAMP_NE);
4635  /* sinfo_msg("%s value is %d", KEY_NAME_LAMP_NE, lamp_Ne); */
4636  } else {
4637  sinfo_msg_warning("keyword %s does not exist",KEY_NAME_LAMP_NE);
4638  sinfo_free_propertylist(&plist);
4639  return -1;
4640  }
4641 
4642  if (sinfo_propertylist_has(plist, KEY_NAME_LAMP_AR)) {
4643  lamp_Ar=cpl_propertylist_get_bool(plist, KEY_NAME_LAMP_AR);
4644  /* sinfo_msg("%s value is %d", KEY_NAME_LAMP_AR, lamp_Ar); */
4645  } else {
4646  sinfo_msg_warning("keyword %s does not exist",KEY_NAME_LAMP_AR);
4647  sinfo_free_propertylist(&plist);
4648  return -1;
4649  }
4650 
4651  if (sinfo_propertylist_has(plist, KEY_NAME_LAMP_HALO)) {
4652  lamp_Halo=cpl_propertylist_get_bool(plist, KEY_NAME_LAMP_HALO);
4653  /* sinfo_msg("%s value is %d", KEY_NAME_LAMP_HALO, lamp_Halo); */
4654  } else {
4655  sinfo_msg_warning("keyword %s does not exist",KEY_NAME_LAMP_HALO);
4656  sinfo_free_propertylist(&plist);
4657  return -1;
4658  }
4659 
4660 
4661 
4662 
4663  if(lamp_Xe) {
4664  result=1;
4665  }
4666 
4667  if(lamp_Kr) {
4668  result=1;
4669  }
4670 
4671  if(lamp_Ne) {
4672  result=1;
4673  }
4674 
4675  if(lamp_Ar) {
4676  result=1;
4677  }
4678 
4679 
4680  if(lamp_Halo) {
4681  result=1;
4682  }
4683 
4684  cleanup:
4685  sinfo_free_propertylist(&plist);
4686  return result;
4687 
4688 
4689 }
4690 
4691 
4692 
4693 int
4694 sinfo_pfits_add_qc(cpl_propertylist * plist,
4695  qc_log * qclog)
4696 {
4697  char key_name[80] ;
4698  char key_value[80] ;
4699 
4700  int i =0;
4701 
4702  /* Test entries */
4703  if (plist == NULL) return -1 ;
4704 
4705  /* Parameter Name: PIPEFILE */
4706  /* we add ESO prefix to FITS keywords" */
4707  for(i=0;i<qclog[0].n;i++) {
4708  strcpy(key_name,"ESO ");
4709  strcat(key_name,qclog[i].name);
4710  if(strcmp(qclog[i].type,"string") == 0) {
4711  snprintf(key_value,sizeof(key_value)-1,"%s",qclog[i].s_val);
4712  cpl_propertylist_append_string(plist, key_name,key_value) ;
4713  cpl_propertylist_set_comment(plist, key_name,qclog[i].comm) ;
4714 
4715  } else if(strcmp(qclog[i].type,"bool") == 0) {
4716  snprintf(key_value,sizeof(key_value),"%i",(int)qclog[i].n_val);
4717  cpl_propertylist_append_bool(plist, key_name,(int)qclog[i].n_val) ;
4718  cpl_propertylist_set_comment(plist, key_name,qclog[i].comm) ;
4719  } else if(strcmp(qclog[i].type,"int") == 0) {
4720  snprintf(key_value,sizeof(key_value),"%i",(int)qclog[i].n_val);
4721  cpl_propertylist_append_int(plist, key_name,(int)qclog[i].n_val) ;
4722  cpl_propertylist_set_comment(plist, key_name,qclog[i].comm) ;
4723  } else if(strcmp(qclog[i].type,"float") == 0) {
4724  snprintf(key_value,sizeof(key_value),"%f",(float)qclog[i].n_val);
4725  cpl_propertylist_append_float(plist, key_name,(float)qclog[i].n_val) ;
4726  cpl_propertylist_set_comment(plist, key_name,qclog[i].comm) ;
4727  } else if(strcmp(qclog[i].type,"double") == 0) {
4728  snprintf(key_value,sizeof(key_value),"%f",qclog[i].n_val);
4729  cpl_propertylist_append_double(plist, key_name,qclog[i].n_val) ;
4730  cpl_propertylist_set_comment(plist, key_name,qclog[i].comm) ;
4731  }
4732 
4733  }
4734 
4735  return 0 ;
4736 }
4737 
4738 
4739