sinfo_wcal_functions.c

00001 /* $Id: sinfo_wcal_functions.c,v 1.8 2008/02/12 09:44:05 amodigli Exp $
00002  *
00003  * This file is part of the SINFONI Pipeline
00004  * Copyright (C) 2002,2003 European Southern Observatory
00005  *
00006  * This program is free software; you can redistribute it and/or modify
00007  * it under the terms of the GNU General Public License as published by
00008  * the Free Software Foundation; either version 2 of the License, or
00009  * (at your option) any later version.
00010  *
00011  * This program is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU General Public License
00017  * along with this program; if not, write to the Free Software
00018  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00019  */
00020 
00021 /*
00022  * $Author: amodigli $
00023  * $Date: 2008/02/12 09:44:05 $
00024  * $Revision: 1.8 $
00025  * $Name: sinfo-2_2_5 $
00026  */
00027 
00028 
00029 #ifdef HAVE_CONFIG_H
00030 #  include <config.h>
00031 #endif
00032 /*-----------------------------------------------------------------------------
00033                                    Includes
00034  ----------------------------------------------------------------------------*/
00035 
00036 #include "sinfo_wcal_functions.h"
00037 #include "sinfo_globals.h"
00038 #include "sinfo_error.h"
00039 #include "sinfo_utils_wrappers.h"
00040 
00041 /* struct qc_wcal qc_wcal_par; */
00049 qc_wcal* sinfo_qc_wcal_new(void)
00050 {  
00051 
00052   qc_wcal * qc;
00053   qc= cpl_malloc(sizeof(qc_wcal));
00054 
00055   qc->avg_on=0; 
00056   qc->std_on=0;
00057   qc->avg_of=0; 
00058   qc->std_of=0;
00059   qc->avg_di=0; 
00060   qc->std_di=0;
00061   qc->max_on=0;
00062   qc->max_of=0;
00063   qc->max_di=0;
00064   qc->nsat_on=0;
00065   qc->noise_on=0;
00066   qc->noise_of=0;
00067   qc->flux_on=0;
00068   qc->nsat=0;
00069   return qc;
00070 }
00071 
00072 void sinfo_qc_wcal_delete(qc_wcal** qc)
00073 {
00074   cpl_free(*qc);
00075   *qc = NULL;
00076 }
00077 
00078 int sinfo_dumpTblToFitParams ( FitParams ** params, char * filename )
00079 {
00080     cpl_table * fp =NULL;
00081     char* col=NULL;
00082     int i =0;
00083     int j =0;
00084     int status=0;
00085 
00086 
00087     if ( NULL == params )
00088     {
00089         sinfo_msg_error ("no fit parameters available!") ;
00090         return -1;
00091     }
00092 
00093     if ( NULL == filename )
00094     {
00095         sinfo_msg_error ("no filename available!") ;
00096         return -1;
00097     }
00098    
00099 
00100     fp=cpl_table_load(filename,1,0);
00101     if(cpl_error_get_code() != CPL_ERROR_NONE) {
00102       sinfo_msg("cannot load table %s",filename);
00103       sinfo_msg_error((char* ) cpl_error_get_message());
00104       return -1;
00105     }
00106 
00107     col = (char*) cpl_calloc(MAX_NAME_SIZE,sizeof(char*));
00108     for ( i = 0 ; i < params[0]->n_params ; i++ )
00109     {
00110        params[i]->n_params=cpl_table_get_int(fp,"n_params",i,&status);
00111        params[i]->column  =cpl_table_get_int(fp,"column",i,&status);
00112        params[i]->line    =cpl_table_get_int(fp,"line",i,&status);
00113        for (j=0 ; j < 4; j++) {
00114      snprintf(col,MAX_NAME_SIZE-1,"%s%d","fpar",j);
00115          params[i]->fit_par[j]=cpl_table_get_double(fp,col,i,&status);
00116      snprintf(col,MAX_NAME_SIZE-1,"%s%d","dpar",j);
00117          params[i]->derv_par[j]=cpl_table_get_double(fp,col,i,&status);
00118        }
00119     }
00120     cpl_free(col);
00121     cpl_table_delete(fp) ;
00122     return 0;
00123 }
00124 
00125 
00126 
00127 
00128 int sinfo_det_ncounts(cpl_frameset* raw, int thresh_max, qc_wcal* qc)
00129 {
00130  int i=0;
00131  int j=0;
00132 
00133  long nraw=0;
00134  int non=0;
00135  int noff=0;
00136  int nsat=0;
00137 
00138  double mean=0;
00139  double max=0;
00140 
00141  double mjd_on=0;
00142  double mjd_of=0;
00143  double mjd_of_frm=0;
00144 
00145  char name[FILE_NAME_SZ];
00146  char filename[FILE_NAME_SZ];
00147 
00148 
00149  cpl_vector* avg_on=NULL;
00150  cpl_vector* avg_of=NULL;
00151  cpl_vector* avg_di=NULL;
00152 
00153 
00154  cpl_vector* max_on=NULL;
00155  cpl_vector* max_of=NULL;
00156  cpl_vector* max_di=NULL;
00157 
00158  cpl_vector* vec_nsat=NULL;
00159 
00160  cpl_frame* frm=NULL;
00161  cpl_frame* frm_dup=NULL;
00162  cpl_frame* on_frm=NULL;
00163  cpl_frame* of_frm=NULL;
00164  cpl_frame* tmp_of_frm=NULL;
00165 
00166  cpl_image* dif_ima=NULL;
00167  cpl_image* on_ima=NULL;
00168  cpl_image* of_ima=NULL;
00169  cpl_image* tmp_ima=NULL;
00170 
00171  cpl_frameset* on_set=NULL;
00172  cpl_frameset* of_set=NULL;
00173  
00174  on_set=cpl_frameset_new();
00175  of_set=cpl_frameset_new();
00176  nraw = cpl_frameset_get_size(raw);
00177 
00178 
00179  for (i=0; i< nraw; i++) {
00180    check_nomsg(frm = cpl_frameset_get_frame(raw,i));
00181    check_nomsg(frm_dup = cpl_frame_duplicate(frm));
00182    if(sinfo_frame_is_on(frm) == 1) {
00183      cpl_frameset_insert(on_set,frm_dup);
00184      non++;
00185    } else {
00186      cpl_frameset_insert(of_set,frm_dup);
00187      noff++;
00188    }
00189  }
00190 
00191 
00192  if (non == noff) {   
00193      sinfo_msg("Monitor counts: case Non==Noff");
00194 
00195 
00196     avg_on = cpl_vector_new(non);
00197     avg_of = cpl_vector_new(non);
00198     avg_di = cpl_vector_new(non);
00199 
00200     max_on = cpl_vector_new(non);
00201     max_of = cpl_vector_new(non);
00202     max_di = cpl_vector_new(non);
00203 
00204 
00205 
00206     vec_nsat = cpl_vector_new(non);
00207     for (i=0; i< non; i++) {
00208       check_nomsg(on_frm = cpl_frameset_get_frame(on_set,i));
00209       strcpy(name,cpl_frame_get_filename(on_frm));
00210       check_nomsg(on_ima = cpl_image_load(name,CPL_TYPE_FLOAT,0,0));
00211 
00212       check_nomsg(mean= cpl_image_get_mean(on_ima));
00213       check_nomsg(cpl_vector_set(avg_on,i,mean));
00214       check_nomsg(max= cpl_image_get_max(on_ima));
00215       check_nomsg(cpl_vector_set(max_on,i,max));
00216       check_nomsg(tmp_ima = cpl_image_duplicate(on_ima));
00217       check_nomsg(cpl_image_threshold(tmp_ima,thresh_max,thresh_max,0,1));
00218       check_nomsg(nsat=cpl_image_get_flux(tmp_ima));
00219       check_nomsg(cpl_vector_set(vec_nsat,i,nsat));
00220 
00221 
00222       check_nomsg(of_frm = cpl_frameset_get_frame(of_set,i));
00223       strcpy(name,cpl_frame_get_filename(of_frm));
00224       check_nomsg(of_ima = cpl_image_load(name,CPL_TYPE_FLOAT,0,0));
00225       check_nomsg(mean= cpl_image_get_mean(of_ima));
00226       check_nomsg(cpl_vector_set(avg_of,i,mean));
00227       check_nomsg(max= cpl_image_get_max(of_ima));
00228       check_nomsg(cpl_vector_set(max_of,i,max));
00229      
00230       check_nomsg(dif_ima = cpl_image_subtract_create(on_ima,of_ima));
00231       check_nomsg(mean= cpl_image_get_mean(dif_ima));
00232       check_nomsg(cpl_vector_set(avg_di,i,mean));
00233       check_nomsg(max= cpl_image_get_max(dif_ima));
00234       check_nomsg(cpl_vector_set(max_di,i,max));
00235 
00236       sinfo_free_image(&on_ima);
00237       sinfo_free_image(&of_ima);
00238       sinfo_free_image(&dif_ima);
00239       sinfo_free_image(&tmp_ima);
00240 
00241     }
00242 
00243     check_nomsg(qc->avg_on=cpl_vector_get_mean(avg_on));
00244     check_nomsg(qc->avg_of=cpl_vector_get_mean(avg_of));
00245     check_nomsg(qc->avg_di=cpl_vector_get_mean(avg_di));
00246 
00247     check_nomsg(qc->max_on=cpl_vector_get_max(max_on));
00248     check_nomsg(qc->max_of=cpl_vector_get_max(max_of));
00249     check_nomsg(qc->max_di=cpl_vector_get_max(max_di));
00250 
00251     check_nomsg(qc->nsat=cpl_vector_get_mean(vec_nsat));
00252 
00253     if (non > 1) {
00254       check_nomsg(qc->std_on=cpl_vector_get_stdev(avg_on));
00255       check_nomsg(qc->std_of=cpl_vector_get_stdev(avg_of));
00256       check_nomsg(qc->std_di=cpl_vector_get_stdev(avg_di));
00257     }
00258 
00259     sinfo_free_my_vector(&avg_on);
00260     sinfo_free_my_vector(&avg_of);
00261     sinfo_free_my_vector(&avg_di);    
00262 
00263     sinfo_free_my_vector(&max_on);
00264     sinfo_free_my_vector(&max_of);
00265     sinfo_free_my_vector(&max_di);
00266 
00267     sinfo_free_my_vector(&vec_nsat);
00268 
00269 
00270  } else if ( ((non ==0) | (noff == 0)) && (nraw > 0 ) ) {
00271     sinfo_msg("Monitor counts: case Nraw == 1");
00272     check_nomsg(avg_di = cpl_vector_new(nraw));
00273     check_nomsg(max_di = cpl_vector_new(nraw));
00274     check_nomsg(vec_nsat = cpl_vector_new(nraw));
00275 
00276     for (i=0; i< nraw; i++) {
00277       check_nomsg(frm = cpl_frameset_get_frame(raw,i));
00278       strcpy(name,cpl_frame_get_filename(frm));
00279       check_nomsg(dif_ima = cpl_image_load(name,CPL_TYPE_FLOAT,0,0));
00280 
00281       check_nomsg(mean= cpl_image_get_mean(dif_ima));
00282       check_nomsg(tmp_ima=cpl_image_duplicate(dif_ima));
00283       sinfo_clean_nan(&tmp_ima);
00284       check_nomsg(mean= cpl_image_get_mean(tmp_ima));
00285       check_nomsg(cpl_vector_set(avg_di,i,mean));
00286       check_nomsg(max= cpl_image_get_max(tmp_ima));
00287       check_nomsg(cpl_vector_set(max_di,i,max));
00288       check_nomsg(cpl_image_threshold(tmp_ima,thresh_max,thresh_max,0,1));
00289       check_nomsg(nsat=cpl_image_get_flux(tmp_ima));
00290       check_nomsg(cpl_vector_set(vec_nsat,i,nsat));
00291 
00292       sinfo_free_image(&tmp_ima);
00293       sinfo_free_image(&dif_ima);
00294 
00295     }
00296 
00297     qc->avg_di=cpl_vector_get_mean(avg_di);
00298     if (nraw > 1) {
00299       check_nomsg(qc->std_di=cpl_vector_get_stdev(avg_di));
00300     }
00301     check_nomsg(qc->max_di=cpl_vector_get_max(max_di));
00302     check_nomsg(qc->nsat=cpl_vector_get_mean(vec_nsat));
00303 
00304     sinfo_free_my_vector(&avg_di);
00305     sinfo_free_my_vector(&max_di);
00306     sinfo_free_my_vector(&vec_nsat);
00307 
00308  } else {
00309    sinfo_msg("Monitor counts: else case");
00310 
00311     avg_on = cpl_vector_new(non);
00312     avg_of = cpl_vector_new(non);
00313     avg_di = cpl_vector_new(non);
00314 
00315     max_on = cpl_vector_new(non);
00316     max_of = cpl_vector_new(non);
00317     max_di = cpl_vector_new(non);
00318 
00319     vec_nsat = cpl_vector_new(non);
00320 
00321     for (i=0;i<non;i++) {
00322       check_nomsg(on_frm=cpl_frameset_get_frame(on_set,i));
00323       check_nomsg(mjd_on=sinfo_get_mjd_obs(on_frm));
00324       check_nomsg(of_frm=cpl_frameset_get_frame(of_set,0));
00325       check_nomsg(mjd_of=sinfo_get_mjd_obs(of_frm));
00326       strcpy(filename,cpl_frame_get_filename(of_frm));
00327 
00328       for (j=1;j<noff;j++) {
00329     check_nomsg(tmp_of_frm = cpl_frameset_get_frame(of_set,j));
00330         check_nomsg(mjd_of_frm = sinfo_get_mjd_obs(tmp_of_frm));
00331 
00332           if(1000.*(mjd_of_frm-mjd_on)*(mjd_of_frm-mjd_on) <
00333              1000.*(mjd_of-    mjd_on)*(mjd_of-    mjd_on) ) {
00334             mjd_of=mjd_of_frm;
00335              of_frm=tmp_of_frm;
00336       }
00337       }
00338 
00339        strcpy(filename,cpl_frame_get_filename(of_frm));
00340        check_nomsg(of_ima = cpl_image_load(filename,CPL_TYPE_FLOAT,0,0));
00341        check_nomsg(mean= cpl_image_get_mean(of_ima));
00342        check_nomsg(cpl_vector_set(avg_of,i,mean));
00343        check_nomsg(max= cpl_image_get_max(of_ima));
00344        check_nomsg(cpl_vector_set(max_of,i,max));
00345 
00346        strcpy(filename,cpl_frame_get_filename(on_frm));
00347        check_nomsg(on_ima = cpl_image_load(filename,CPL_TYPE_FLOAT,0,0));
00348        check_nomsg(mean= cpl_image_get_mean(on_ima)); 
00349        check_nomsg(cpl_vector_set(avg_on,i,mean));
00350        check_nomsg(max= cpl_image_get_mean(on_ima)); 
00351        check_nomsg(cpl_vector_set(max_on,i,max));
00352 
00353        check_nomsg(tmp_ima = cpl_image_duplicate(on_ima));     
00354        check_nomsg(cpl_image_threshold(tmp_ima,thresh_max,thresh_max,0,1));
00355        check_nomsg(nsat=cpl_image_get_flux(tmp_ima));
00356        check_nomsg(cpl_vector_set(vec_nsat,i,nsat));
00357 
00358        check_nomsg(dif_ima = cpl_image_subtract_create(on_ima,of_ima));
00359        check_nomsg(mean= cpl_image_get_mean(dif_ima));
00360        check_nomsg(cpl_vector_set(avg_di,i,mean));
00361        check_nomsg(max= cpl_image_get_max(dif_ima));
00362        check_nomsg(cpl_vector_set(max_di,i,max));
00363 
00364        sinfo_free_image(&on_ima);
00365        sinfo_free_image(&of_ima);
00366        sinfo_free_image(&dif_ima);
00367        sinfo_free_image(&tmp_ima);
00368 
00369    }
00370 
00371     sinfo_free_my_vector(&avg_on);
00372     sinfo_free_my_vector(&avg_of);
00373     sinfo_free_my_vector(&avg_di);
00374 
00375     sinfo_free_my_vector(&max_on);
00376     sinfo_free_my_vector(&max_of);
00377     sinfo_free_my_vector(&max_di);
00378 
00379     sinfo_free_my_vector(&vec_nsat);
00380 
00381  }
00382 
00383  /*
00384  sinfo_msg("avg_on=%g std_on=%g ",qc->avg_on,qc_wcal.std_on);
00385  sinfo_msg("avg_of=%g std_of=%g ",qc->avg_of,qc_wcal.std_of);
00386  sinfo_msg("avg_di=%g std_di=%g ",qc->avg_di,qc_wcal.std_di);
00387  sinfo_msg("max_fl=%g nsat_on=%g ",qc->max_di,qc_wcal.nsat);
00388  */
00389  cleanup:
00390  
00391  sinfo_free_image(&on_ima);
00392  sinfo_free_image(&of_ima);
00393  sinfo_free_image(&dif_ima);
00394  sinfo_free_image(&tmp_ima);
00395 
00396  sinfo_free_my_vector(&avg_on);
00397  sinfo_free_my_vector(&avg_of);
00398  sinfo_free_my_vector(&avg_di);
00399 
00400  sinfo_free_my_vector(&max_on);
00401  sinfo_free_my_vector(&max_of);
00402  sinfo_free_my_vector(&max_di);
00403 
00404  sinfo_free_my_vector(&vec_nsat);
00405 
00406  sinfo_free_frameset(&on_set);
00407  sinfo_free_frameset(&of_set);
00408 
00409  return 0;
00410 
00411 }
00412 
00413 

Generated on 8 Mar 2011 for SINFONI Pipeline Reference Manual by  doxygen 1.6.1