SINFONI Pipeline Reference Manual  2.5.2
sinfo_wcal_functions.c
1 /* $Id: sinfo_wcal_functions.c,v 1.10 2013-09-17 08:12:53 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:12:53 $
24  * $Revision: 1.10 $
25  * $Name: not supported by cvs2svn $
26  */
27 
28 
29 #ifdef HAVE_CONFIG_H
30 # include <config.h>
31 #endif
32 /*-----------------------------------------------------------------------------
33  Includes
34  ----------------------------------------------------------------------------*/
35 
36 #include "sinfo_wcal_functions.h"
37 #include "sinfo_globals.h"
38 #include "sinfo_error.h"
39 #include "sinfo_utils_wrappers.h"
40 
41 /* struct qc_wcal qc_wcal_par; */
49 qc_wcal* sinfo_qc_wcal_new(void)
50 {
51 
52  qc_wcal * qc;
53  qc= cpl_malloc(sizeof(qc_wcal));
54 
55  qc->avg_on=0;
56  qc->std_on=0;
57  qc->avg_of=0;
58  qc->std_of=0;
59  qc->avg_di=0;
60  qc->std_di=0;
61  qc->max_on=0;
62  qc->max_of=0;
63  qc->max_di=0;
64  qc->nsat_on=0;
65  qc->noise_on=0;
66  qc->noise_of=0;
67  qc->flux_on=0;
68  qc->nsat=0;
69  return qc;
70 }
71 
72 void sinfo_qc_wcal_delete(qc_wcal** qc)
73 {
74  cpl_free(*qc);
75  *qc = NULL;
76 }
77 
78 int sinfo_dumpTblToFitParams ( FitParams ** params, char * filename )
79 {
80  cpl_table * fp =NULL;
81  char* col=NULL;
82  int i =0;
83  int j =0;
84  int status=0;
85 
86 
87  if ( NULL == params )
88  {
89  sinfo_msg_error ("no fit parameters available!") ;
90  return -1;
91  }
92 
93  if ( NULL == filename )
94  {
95  sinfo_msg_error ("no filename available!") ;
96  return -1;
97  }
98 
99 
100  fp=cpl_table_load(filename,1,0);
101  if(cpl_error_get_code() != CPL_ERROR_NONE) {
102  sinfo_msg("cannot load table %s",filename);
103  sinfo_msg_error("%s", (char* ) cpl_error_get_message());
104  return -1;
105  }
106 
107  col = (char*) cpl_calloc(MAX_NAME_SIZE,sizeof(char*));
108  for ( i = 0 ; i < params[0]->n_params ; i++ )
109  {
110  params[i]->n_params=cpl_table_get_int(fp,"n_params",i,&status);
111  params[i]->column =cpl_table_get_int(fp,"column",i,&status);
112  params[i]->line =cpl_table_get_int(fp,"line",i,&status);
113  for (j=0 ; j < 4; j++) {
114  snprintf(col,MAX_NAME_SIZE-1,"%s%d","fpar",j);
115  params[i]->fit_par[j]=cpl_table_get_double(fp,col,i,&status);
116  snprintf(col,MAX_NAME_SIZE-1,"%s%d","dpar",j);
117  params[i]->derv_par[j]=cpl_table_get_double(fp,col,i,&status);
118  }
119  }
120  cpl_free(col);
121  cpl_table_delete(fp) ;
122  return 0;
123 }
124 
125 
126 
127 
128 int sinfo_det_ncounts(cpl_frameset* raw, int thresh_max, qc_wcal* qc)
129 {
130  int i=0;
131  int j=0;
132 
133  long nraw=0;
134  int non=0;
135  int noff=0;
136  int nsat=0;
137 
138  double mean=0;
139  double max=0;
140 
141  double mjd_on=0;
142  double mjd_of=0;
143  double mjd_of_frm=0;
144 
145  char name[FILE_NAME_SZ];
146  char filename[FILE_NAME_SZ];
147 
148 
149  cpl_vector* avg_on=NULL;
150  cpl_vector* avg_of=NULL;
151  cpl_vector* avg_di=NULL;
152 
153 
154  cpl_vector* max_on=NULL;
155  cpl_vector* max_of=NULL;
156  cpl_vector* max_di=NULL;
157 
158  cpl_vector* vec_nsat=NULL;
159 
160  cpl_frame* frm=NULL;
161  cpl_frame* frm_dup=NULL;
162  cpl_frame* on_frm=NULL;
163  cpl_frame* of_frm=NULL;
164  cpl_frame* tmp_of_frm=NULL;
165 
166  cpl_image* dif_ima=NULL;
167  cpl_image* on_ima=NULL;
168  cpl_image* of_ima=NULL;
169  cpl_image* tmp_ima=NULL;
170 
171  cpl_frameset* on_set=NULL;
172  cpl_frameset* of_set=NULL;
173 
174  on_set=cpl_frameset_new();
175  of_set=cpl_frameset_new();
176  nraw = cpl_frameset_get_size(raw);
177 
178 
179  for (i=0; i< nraw; i++) {
180  check_nomsg(frm = cpl_frameset_get_frame(raw,i));
181  check_nomsg(frm_dup = cpl_frame_duplicate(frm));
182  if(sinfo_frame_is_on(frm) == 1) {
183  cpl_frameset_insert(on_set,frm_dup);
184  non++;
185  } else {
186  cpl_frameset_insert(of_set,frm_dup);
187  noff++;
188  }
189  }
190 
191 
192  if (non == noff) {
193  sinfo_msg("Monitor counts: case Non==Noff");
194 
195 
196  avg_on = cpl_vector_new(non);
197  avg_of = cpl_vector_new(non);
198  avg_di = cpl_vector_new(non);
199 
200  max_on = cpl_vector_new(non);
201  max_of = cpl_vector_new(non);
202  max_di = cpl_vector_new(non);
203 
204 
205 
206  vec_nsat = cpl_vector_new(non);
207  for (i=0; i< non; i++) {
208  check_nomsg(on_frm = cpl_frameset_get_frame(on_set,i));
209  strcpy(name,cpl_frame_get_filename(on_frm));
210  check_nomsg(on_ima = cpl_image_load(name,CPL_TYPE_FLOAT,0,0));
211 
212  check_nomsg(mean= cpl_image_get_mean(on_ima));
213  check_nomsg(cpl_vector_set(avg_on,i,mean));
214  check_nomsg(max= cpl_image_get_max(on_ima));
215  check_nomsg(cpl_vector_set(max_on,i,max));
216  check_nomsg(tmp_ima = cpl_image_duplicate(on_ima));
217  check_nomsg(cpl_image_threshold(tmp_ima,thresh_max,thresh_max,0,1));
218  check_nomsg(nsat=cpl_image_get_flux(tmp_ima));
219  check_nomsg(cpl_vector_set(vec_nsat,i,nsat));
220 
221 
222  check_nomsg(of_frm = cpl_frameset_get_frame(of_set,i));
223  strcpy(name,cpl_frame_get_filename(of_frm));
224  check_nomsg(of_ima = cpl_image_load(name,CPL_TYPE_FLOAT,0,0));
225  check_nomsg(mean= cpl_image_get_mean(of_ima));
226  check_nomsg(cpl_vector_set(avg_of,i,mean));
227  check_nomsg(max= cpl_image_get_max(of_ima));
228  check_nomsg(cpl_vector_set(max_of,i,max));
229 
230  check_nomsg(dif_ima = cpl_image_subtract_create(on_ima,of_ima));
231  check_nomsg(mean= cpl_image_get_mean(dif_ima));
232  check_nomsg(cpl_vector_set(avg_di,i,mean));
233  check_nomsg(max= cpl_image_get_max(dif_ima));
234  check_nomsg(cpl_vector_set(max_di,i,max));
235 
236  sinfo_free_image(&on_ima);
237  sinfo_free_image(&of_ima);
238  sinfo_free_image(&dif_ima);
239  sinfo_free_image(&tmp_ima);
240 
241  }
242 
243  check_nomsg(qc->avg_on=cpl_vector_get_mean(avg_on));
244  check_nomsg(qc->avg_of=cpl_vector_get_mean(avg_of));
245  check_nomsg(qc->avg_di=cpl_vector_get_mean(avg_di));
246 
247  check_nomsg(qc->max_on=cpl_vector_get_max(max_on));
248  check_nomsg(qc->max_of=cpl_vector_get_max(max_of));
249  check_nomsg(qc->max_di=cpl_vector_get_max(max_di));
250 
251  check_nomsg(qc->nsat=cpl_vector_get_mean(vec_nsat));
252 
253  if (non > 1) {
254  check_nomsg(qc->std_on=cpl_vector_get_stdev(avg_on));
255  check_nomsg(qc->std_of=cpl_vector_get_stdev(avg_of));
256  check_nomsg(qc->std_di=cpl_vector_get_stdev(avg_di));
257  }
258 
259  sinfo_free_my_vector(&avg_on);
260  sinfo_free_my_vector(&avg_of);
261  sinfo_free_my_vector(&avg_di);
262 
263  sinfo_free_my_vector(&max_on);
264  sinfo_free_my_vector(&max_of);
265  sinfo_free_my_vector(&max_di);
266 
267  sinfo_free_my_vector(&vec_nsat);
268 
269 
270  } else if ( ((non ==0) | (noff == 0)) && (nraw > 0 ) ) {
271  sinfo_msg("Monitor counts: case Nraw == 1");
272  check_nomsg(avg_di = cpl_vector_new(nraw));
273  check_nomsg(max_di = cpl_vector_new(nraw));
274  check_nomsg(vec_nsat = cpl_vector_new(nraw));
275 
276  for (i=0; i< nraw; i++) {
277  check_nomsg(frm = cpl_frameset_get_frame(raw,i));
278  strcpy(name,cpl_frame_get_filename(frm));
279  check_nomsg(dif_ima = cpl_image_load(name,CPL_TYPE_FLOAT,0,0));
280 
281  check_nomsg(mean= cpl_image_get_mean(dif_ima));
282  check_nomsg(tmp_ima=cpl_image_duplicate(dif_ima));
283  sinfo_clean_nan(&tmp_ima);
284  check_nomsg(mean= cpl_image_get_mean(tmp_ima));
285  check_nomsg(cpl_vector_set(avg_di,i,mean));
286  check_nomsg(max= cpl_image_get_max(tmp_ima));
287  check_nomsg(cpl_vector_set(max_di,i,max));
288  check_nomsg(cpl_image_threshold(tmp_ima,thresh_max,thresh_max,0,1));
289  check_nomsg(nsat=cpl_image_get_flux(tmp_ima));
290  check_nomsg(cpl_vector_set(vec_nsat,i,nsat));
291 
292  sinfo_free_image(&tmp_ima);
293  sinfo_free_image(&dif_ima);
294 
295  }
296 
297  qc->avg_di=cpl_vector_get_mean(avg_di);
298  if (nraw > 1) {
299  check_nomsg(qc->std_di=cpl_vector_get_stdev(avg_di));
300  }
301  check_nomsg(qc->max_di=cpl_vector_get_max(max_di));
302  check_nomsg(qc->nsat=cpl_vector_get_mean(vec_nsat));
303 
304  sinfo_free_my_vector(&avg_di);
305  sinfo_free_my_vector(&max_di);
306  sinfo_free_my_vector(&vec_nsat);
307 
308  } else {
309  sinfo_msg("Monitor counts: else case");
310 
311  avg_on = cpl_vector_new(non);
312  avg_of = cpl_vector_new(non);
313  avg_di = cpl_vector_new(non);
314 
315  max_on = cpl_vector_new(non);
316  max_of = cpl_vector_new(non);
317  max_di = cpl_vector_new(non);
318 
319  vec_nsat = cpl_vector_new(non);
320 
321  for (i=0;i<non;i++) {
322  check_nomsg(on_frm=cpl_frameset_get_frame(on_set,i));
323  check_nomsg(mjd_on=sinfo_get_mjd_obs(on_frm));
324  check_nomsg(of_frm=cpl_frameset_get_frame(of_set,0));
325  check_nomsg(mjd_of=sinfo_get_mjd_obs(of_frm));
326  strcpy(filename,cpl_frame_get_filename(of_frm));
327 
328  for (j=1;j<noff;j++) {
329  check_nomsg(tmp_of_frm = cpl_frameset_get_frame(of_set,j));
330  check_nomsg(mjd_of_frm = sinfo_get_mjd_obs(tmp_of_frm));
331 
332  if(1000.*(mjd_of_frm-mjd_on)*(mjd_of_frm-mjd_on) <
333  1000.*(mjd_of- mjd_on)*(mjd_of- mjd_on) ) {
334  mjd_of=mjd_of_frm;
335  of_frm=tmp_of_frm;
336  }
337  }
338 
339  strcpy(filename,cpl_frame_get_filename(of_frm));
340  check_nomsg(of_ima = cpl_image_load(filename,CPL_TYPE_FLOAT,0,0));
341  check_nomsg(mean= cpl_image_get_mean(of_ima));
342  check_nomsg(cpl_vector_set(avg_of,i,mean));
343  check_nomsg(max= cpl_image_get_max(of_ima));
344  check_nomsg(cpl_vector_set(max_of,i,max));
345 
346  strcpy(filename,cpl_frame_get_filename(on_frm));
347  check_nomsg(on_ima = cpl_image_load(filename,CPL_TYPE_FLOAT,0,0));
348  check_nomsg(mean= cpl_image_get_mean(on_ima));
349  check_nomsg(cpl_vector_set(avg_on,i,mean));
350  check_nomsg(max= cpl_image_get_mean(on_ima));
351  check_nomsg(cpl_vector_set(max_on,i,max));
352 
353  check_nomsg(tmp_ima = cpl_image_duplicate(on_ima));
354  check_nomsg(cpl_image_threshold(tmp_ima,thresh_max,thresh_max,0,1));
355  check_nomsg(nsat=cpl_image_get_flux(tmp_ima));
356  check_nomsg(cpl_vector_set(vec_nsat,i,nsat));
357 
358  check_nomsg(dif_ima = cpl_image_subtract_create(on_ima,of_ima));
359  check_nomsg(mean= cpl_image_get_mean(dif_ima));
360  check_nomsg(cpl_vector_set(avg_di,i,mean));
361  check_nomsg(max= cpl_image_get_max(dif_ima));
362  check_nomsg(cpl_vector_set(max_di,i,max));
363 
364  sinfo_free_image(&on_ima);
365  sinfo_free_image(&of_ima);
366  sinfo_free_image(&dif_ima);
367  sinfo_free_image(&tmp_ima);
368 
369  }
370 
371  sinfo_free_my_vector(&avg_on);
372  sinfo_free_my_vector(&avg_of);
373  sinfo_free_my_vector(&avg_di);
374 
375  sinfo_free_my_vector(&max_on);
376  sinfo_free_my_vector(&max_of);
377  sinfo_free_my_vector(&max_di);
378 
379  sinfo_free_my_vector(&vec_nsat);
380 
381  }
382 
383  /*
384  sinfo_msg("avg_on=%g std_on=%g ",qc->avg_on,qc_wcal.std_on);
385  sinfo_msg("avg_of=%g std_of=%g ",qc->avg_of,qc_wcal.std_of);
386  sinfo_msg("avg_di=%g std_di=%g ",qc->avg_di,qc_wcal.std_di);
387  sinfo_msg("max_fl=%g nsat_on=%g ",qc->max_di,qc_wcal.nsat);
388  */
389  cleanup:
390 
391  sinfo_free_image(&on_ima);
392  sinfo_free_image(&of_ima);
393  sinfo_free_image(&dif_ima);
394  sinfo_free_image(&tmp_ima);
395 
396  sinfo_free_my_vector(&avg_on);
397  sinfo_free_my_vector(&avg_of);
398  sinfo_free_my_vector(&avg_di);
399 
400  sinfo_free_my_vector(&max_on);
401  sinfo_free_my_vector(&max_of);
402  sinfo_free_my_vector(&max_di);
403 
404  sinfo_free_my_vector(&vec_nsat);
405 
406  sinfo_free_frameset(&on_set);
407  sinfo_free_frameset(&of_set);
408 
409  return 0;
410 
411 }
412 
413