vircam_genlincur.c

00001 /* $Id: vircam_genlincur.c,v 1.33 2010/06/03 12:15:31 jim Exp $
00002  *
00003  * This file is part of the VIRCAM Pipeline
00004  * Copyright (C) 2005 Cambridge Astronomy Survey Unit
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: jim $
00023  * $Date: 2010/06/03 12:15:31 $
00024  * $Revision: 1.33 $
00025  * $Name: v1-1-0 $
00026  */
00027 
00028 /* Includes */
00029 
00030 #ifdef HAVE_CONFIG_H
00031 #include <config.h>
00032 #endif
00033 
00034 #include <cpl.h>
00035 
00036 #include <math.h>
00037 
00038 #include "vircam_mods.h"
00039 #include "vircam_utils.h"
00040 #include "vircam_stats.h"
00041 #include "vircam_pfits.h"
00042 #include "vircam_channel.h"
00043 
00044 
00045 #define SZCOLNAME 16
00046 
00047 static double nom_val = 10000;
00048 
00049 
00050 static double linval(double inval, double *kfacs, double tolerance,
00051                      int niter, double *b, int norder);
00052 static double getkfac(long index, long ncpts, float reset_time, 
00053                       float read_time, float delay_time, float exptime);
00054 static void getco(double *a, int nord, int m);
00055 
00058 /*---------------------------------------------------------------------------*/
00115 /*---------------------------------------------------------------------------*/
00116 
00117 extern int vircam_genlincur(double **fdata, int nimages, double *exps, 
00118                             double mindit, vir_tfits *chantab, 
00119                             int norder, cpl_table **lchantab, 
00120                             double **lindata, int *status) {
00121 
00122     const char *fctid = "vircam_genlincur";
00123     int retval,i,j,nbad,oldnorder,nullval,k,ii;
00124     long np;
00125     double *meds,sigfit,**aco,c0,lin_nom,*temp,*polyco,pt,*work,kfac;
00126     double sum,t10000,*kfacs;
00127     parquet *p,*pp;
00128     cpl_table *ctab,*lc;
00129     cpl_array *exparray,*medsarray,*polyfitco,*workarray;
00130     char colname[SZCOLNAME];
00131 
00132     /* Inherited status */
00133 
00134     *lchantab = NULL;
00135     if (*status != VIR_OK)
00136         return(*status);
00137 
00138     /* Check that you have enough images in the list */
00139 
00140     if (nimages < norder+1) {
00141         cpl_msg_error(fctid,"Not enought images (%d) for fit order (%d)",
00142                       nimages,norder);
00143         FATAL_ERROR
00144     }
00145 
00146     /* Open the parquet structure for the channel table */
00147 
00148     ctab = vircam_tfits_get_table(chantab);
00149     retval = vircam_chan_fill(ctab,&pp,&np);
00150     if (retval != VIR_OK) {
00151         *status = retval;
00152         return(retval);
00153     }
00154 
00155     /* Create an output channel table. Copy the input channel table and then
00156        massage the linearity part */
00157 
00158     lc = cpl_table_duplicate(ctab);
00159     oldnorder = cpl_table_get_int(lc,"norder",0,&nullval);
00160     if (oldnorder > norder) {
00161         for (i = norder+1; i <= oldnorder; i++) {
00162             snprintf(colname,SZCOLNAME,"coeff_%d",i);
00163             cpl_table_erase_column(lc,colname);
00164         }
00165     } else if (oldnorder < norder) {
00166         for (i = oldnorder+1; i <= norder; i++) {
00167             snprintf(colname,SZCOLNAME,"coeff_%d",i);
00168             if (cpl_table_has_column(lc,colname)) 
00169                 continue;
00170             cpl_table_new_column(lc,colname,CPL_TYPE_DOUBLE);
00171         }
00172     }
00173 
00174     /* Get some memory for the fitting arrays */
00175 
00176     exparray = cpl_array_wrap_double(exps,nimages);
00177     medsarray = cpl_array_new(nimages,CPL_TYPE_DOUBLE);
00178     meds = cpl_array_get_data_double(medsarray);
00179     aco = cpl_malloc(norder*sizeof(double *));
00180     for (i = 0; i < norder; i++) 
00181         aco[i] = cpl_malloc(norder*sizeof(double));
00182     temp = cpl_malloc(norder*sizeof(double));
00183     kfacs = cpl_malloc(norder*sizeof(double));
00184 
00185     /* Get memory for output array of linearised stats */
00186 
00187     *lindata = cpl_malloc(nimages*np*sizeof(double));
00188 
00189     /* Loop for each channel */
00190 
00191     nbad = 0;
00192     for (i = 0; i < np; i++) {
00193         p = pp + i;
00194 
00195         /* Load the data up for this channel */
00196 
00197         for (j = 0; j < nimages; j++) 
00198             meds[j] = fdata[j][i];
00199 
00200         /* Do the initial fit */
00201 
00202         if (vircam_polyfit(exparray,medsarray,norder,1,2,2.0,2.0,&polyfitco,
00203                            &sigfit) != VIR_OK) {
00204             nbad++;
00205             cpl_table_set_int(lc,"norder",i,norder);
00206             cpl_table_set_double(lc,"coeff_1",i,1.0);
00207             for (k = 1; k < norder; k++) {
00208                 snprintf(colname,SZCOLNAME,"coeff_%d",k+1);
00209                 cpl_table_set_double(lc,colname,i,0.0);
00210             }
00211             freearray(polyfitco);
00212             continue;
00213         }
00214         polyco = cpl_array_get_data_double(polyfitco);
00215 
00216         /* Work out linearity */
00217 
00218         for (j = 0; j < nimages; j++) 
00219             if (meds[j] > nom_val)
00220                 break;
00221         t10000 = exps[j-1] + (nom_val - meds[j-1])/(meds[j] - meds[j-1]);
00222         sum = 0.0;
00223         for (j = 0; j < norder; j++)
00224             sum += (double)(j+1)*polyco[j]*pow(t10000,(double)j);
00225         lin_nom = 100.0*fabs(sum - polyco[0])/polyco[0];
00226 
00227         /* Get intermediate coefficients for matrix */
00228 
00229         for (j = 0; j < norder; j++) {
00230             getco(temp,norder,j+1);
00231             for (k = 0; k < norder; k++) {
00232                 pt = pow(mindit,(double)(j-k));
00233                 aco[j][k] = pt*temp[k];
00234             }
00235         }
00236 
00237         /* Solve matrix equation to do the back substitution */
00238 
00239         if (vircam_solve_gauss(aco,polyco,norder) != VIR_OK) {
00240             nbad++;
00241             cpl_table_set_int(lc,"norder",i,norder);
00242             cpl_table_set_double(lc,"coeff_1",i,1.0);
00243             for (k = 1; k < norder; k++) {
00244                 snprintf(colname,SZCOLNAME,"coeff_%d",k+1);
00245                 cpl_table_set_double(lc,colname,i,0.0);
00246             }
00247             freearray(polyfitco);
00248             continue;
00249         }
00250 
00251         /* Now normalise to unit slope and write the result to the table*/
00252 
00253         c0 = polyco[0];
00254         for (j = 0; j < norder; j++) {
00255             polyco[j] /= pow(c0,(double)(j+1));
00256             snprintf(colname,SZCOLNAME,"coeff_%d",j+1);
00257             cpl_table_set_double(lc,colname,i,polyco[j]);
00258         }
00259         cpl_table_set_int(lc,"norder",i,norder);
00260 
00261         /* Work out how well the solution creates a linear system. Loop 
00262            for each input image and work out a 'linearised' median. Then
00263            do a linear fit to the linearsed median vs exposure time. */
00264 
00265         workarray = cpl_array_new(nimages,CPL_TYPE_DOUBLE);
00266         work = cpl_array_get_data_double(workarray);
00267         for (j = 0; j < nimages; j++) {
00268             kfac = mindit/exps[j];
00269             kfacs[0] = 1.0;
00270             for (ii = 1; ii < norder; ii++) 
00271                 kfacs[ii] = pow((kfac+1.0),(double)(ii+1)) - 
00272                     pow(kfac,(double)(ii+1));
00273             work[j] = linval(meds[j],kfacs,0.5,10,polyco,norder);
00274             (*lindata)[j*np+i] = work[j];
00275         }       
00276         freearray(polyfitco);
00277         (void)vircam_polyfit(exparray,workarray,2,0,2,2.0,2.0,&polyfitco,
00278                              &sigfit);
00279         polyco = cpl_array_get_data_double(polyfitco);
00280         sigfit *= 100.0/nom_val;
00281         freearray(workarray);
00282         freearray(polyfitco);
00283                        
00284         /* Put the nominal linearity and the fit quality into the header */
00285 
00286         cpl_table_set_double(lc,"lin_10000_err",i,sigfit);
00287         cpl_table_set_double(lc,"lin_10000",i,lin_nom);
00288     }
00289 
00290     /* Tidy and get out of here */
00291 
00292     *lchantab = cpl_table_duplicate(lc);
00293     cpl_array_unwrap(exparray);
00294     freearray(medsarray);
00295     freespace2(aco,norder);
00296     freespace(temp);
00297     freetable(lc);
00298     vircam_chan_free(np,&pp);
00299     freespace(kfacs);
00300     if (nbad != 0) {
00301         cpl_msg_warning(fctid,"%d channels have a failed solution",nbad);
00302         WARN_RETURN
00303     }
00304     GOOD_STATUS
00305 }
00306 
00307 
00308 /*---------------------------------------------------------------------------*/
00349 /*--------------------------------------------------------------------------*/
00350 
00351 extern int vircam_lincor(vir_fits *infile, vir_tfits *lchantab, int kconst, 
00352                          int ndit, int *status) {
00353     int retval,i,norder,ii;
00354     long naxis[2],j,rind,aind,ncpts,np;
00355     float *data,texp,reset_time,read_time,delay_time;
00356     double kfac_nom,lkfac,inval,outval,*lbb,dnd,*kfacs;
00357     const char *fctid = "vircam_lincor";
00358     parquet *pp;
00359     cpl_propertylist *plist;
00360     cpl_table *lctab;
00361     parquet *p;
00362 
00363     /* Inherited status */
00364 
00365     if (*status != VIR_OK)
00366         return(*status);
00367 
00368     /* Do we even need to be here? */
00369 
00370     if (cpl_propertylist_has(vircam_fits_get_ehu(infile),"ESO DRS LINCOR"))
00371         return(*status);
00372 
00373     /* Open the parquet structure for the channel table */
00374 
00375     lctab = vircam_tfits_get_table(lchantab);
00376     retval = vircam_chan_fill(lctab,&p,&np);
00377     if (retval != VIR_OK)
00378         return(retval);
00379 
00380     /* Get the data array for the image */
00381 
00382     data = cpl_image_get_data(vircam_fits_get_image(infile));
00383     if (data == NULL) {
00384         vircam_chan_free(np,&p);
00385         cpl_msg_error(fctid,"Error mapping data in input image");
00386         FATAL_ERROR
00387     }
00388     naxis[0] = (long)cpl_image_get_size_x(vircam_fits_get_image(infile));
00389     naxis[1] = (long)cpl_image_get_size_y(vircam_fits_get_image(infile));
00390 
00391     /* Get the required parameters from the header */
00392 
00393     plist = vircam_fits_get_ehu(infile);
00394     if (vircam_pfits_get_exptime(plist,&texp) != VIR_OK) {
00395         vircam_chan_free(np,&p);
00396         cpl_msg_error(fctid,"No exposure time in %s",
00397                       vircam_fits_get_fullname(infile));
00398         FATAL_ERROR
00399     }
00400     if (vircam_pfits_get_mindit(plist,&reset_time) != VIR_OK) {
00401         vircam_chan_free(np,&p);
00402         cpl_msg_error(fctid,"No mindit time in %s",
00403                       vircam_fits_get_fullname(infile));
00404         FATAL_ERROR
00405     }
00406     read_time = reset_time;
00407     if (vircam_pfits_get_ditdelay(plist,&delay_time) != VIR_OK) {
00408         vircam_chan_free(np,&p);
00409         cpl_msg_error(fctid,"No dit delay time in %s",
00410                       vircam_fits_get_fullname(infile));
00411         FATAL_ERROR
00412     }
00413       
00414     /* If there is a constant k factor, then calculate it now */
00415 
00416     kfac_nom = (double)(read_time/texp);
00417 
00418     /* Factor to take the number of DITs into account */
00419 
00420     dnd = (double)ndit;
00421         
00422     /* Loop for each channel now */
00423 
00424     for (i = 0; i < np; i++) {
00425         pp = p + i;
00426         ncpts = (pp->delta_i)*(pp->delta_j);
00427 
00428         /* Load up the fit coefficients. If there is only one coefficient
00429            this is by definition 1 and therefore we can skip this channel */
00430 
00431         norder = pp->norder;
00432         if (norder == 1)
00433             continue;
00434         lbb = pp->bb;
00435 
00436         /* Get workspace for K array and fill it in for situation of constant
00437            k factor */
00438 
00439         kfacs = cpl_malloc(norder*sizeof(double));
00440         if (kconst) {
00441             kfacs[0] = 1.0;
00442             for (ii = 1; ii < norder; ii++) 
00443                 kfacs[ii] = pow((kfac_nom+1.0),(double)(ii+1)) - 
00444                     pow(kfac_nom,(double)(ii+1));
00445         }
00446 
00447         /* Now for each pixel */
00448 
00449         for (j = 0; j < ncpts; j++) {
00450 
00451             /* Get the 'non-constant' k-factor and fill in the K array. */
00452 
00453             rind = vircam_chan_d2r(pp,j);
00454             aind = vircam_chan_r2a(pp,naxis,rind);
00455             if (! kconst) {
00456                 lkfac = getkfac(j,ncpts,reset_time,read_time,delay_time,texp);
00457                 kfacs[0] = 1.0;
00458                 for (ii = 1; ii < norder; ii++) 
00459                      kfacs[ii] = pow((lkfac+1.0),(double)(ii+1)) - 
00460                          pow(lkfac,(double)(ii+1));
00461             }
00462 
00463             /* Calculate the linearised value now */
00464 
00465             inval = ((double)data[aind])/dnd;
00466             outval = linval(inval,kfacs,0.5,10,lbb,norder);
00467             data[aind] = (float)(dnd*outval);
00468         }
00469         freespace(kfacs);
00470     }
00471 
00472     /* Add the linearity table to the DRS header */
00473 
00474     cpl_propertylist_update_string(vircam_fits_get_ehu(infile),
00475                                    "ESO DRS LINCOR",
00476                                    vircam_tfits_get_filename(lchantab));
00477 
00478     /* Right, get out of here */
00479 
00480     vircam_chan_free(np,&p);
00481     GOOD_STATUS
00482 }
00483 
00484 /*---------------------------------------------------------------------------*/
00513 /*---------------------------------------------------------------------------*/
00514 
00515 static double linval(double inval, double *kfacs, double tolerance,
00516                      int niter, double *b, int norder) {
00517     int jj,iter;
00518     double val_old,val,tol,sum;
00519 
00520     val = inval;
00521     iter = 0;
00522     tol = tolerance + 1.0;
00523     while (iter < niter && tol > tolerance) {
00524         val_old = val;
00525         iter++;
00526         sum = 0.0;
00527         for (jj = norder - 1; jj >= 1; jj--) 
00528             sum = (sum + b[jj]*kfacs[jj])*val;
00529         sum *= val;
00530         val = inval - sum;
00531         tol = fabs(val - val_old);
00532         if (val > 65535.0) {
00533             val = 65535.0;
00534             break;
00535         } else if (val < -1000.0) {
00536             val = -1000.0;
00537             break;
00538         }
00539     }
00540     return(val);
00541 }
00542 
00543     
00544 /*---------------------------------------------------------------------------*/
00572 /*---------------------------------------------------------------------------*/
00573 
00574 static double getkfac(long index, long npts, float reset_time, 
00575                       float read_time, float delay_time, float exptime) {
00576     double tkfac,dt1,dt2,dt3,dt4,df;
00577 
00578     df = ((double)index/(double)npts);
00579     dt1 = (double)exptime;
00580     dt2 = (double)read_time;
00581     dt3 = (double)reset_time;
00582     dt4 = (double)delay_time;
00583     tkfac = (dt3 + dt4 + (dt2 - dt3)*df)/dt1;
00584     return(tkfac);
00585 }
00586 
00587 /*---------------------------------------------------------------------------*/
00609 /*---------------------------------------------------------------------------*/
00610 
00611 static void getco(double *a, int nord, int m) {
00612     int i,j,start;
00613 
00614     for (i = 0; i < nord; i++)
00615         a[i] = 0.0;
00616     start = m-1;
00617     a[start] = 1.0;
00618     j = 1;
00619     for (i = start-1; i >= 0; i--) {
00620         j++;
00621         a[i] = a[i+1]*(double)(m - j + 2)/(double)(j-1);
00622     }
00623 }
00624 
00625 
00629 /*
00630 
00631 $Log: vircam_genlincur.c,v $
00632 Revision 1.33  2010/06/03 12:15:31  jim
00633 A few mods to get rid of compiler warnings
00634 
00635 Revision 1.32  2009/12/11 06:53:35  jim
00636 Minor changes to documentation
00637 
00638 Revision 1.31  2009/09/09 09:45:36  jim
00639 Modified to speed things up
00640 
00641 Revision 1.30  2009/06/08 08:08:15  jim
00642 Fixed memory leak and changed clipping parameters to vircam_polyfit
00643 
00644 Revision 1.29  2009/05/20 12:18:42  jim
00645 Modified so that if the operation is already done, then it just returns
00646 
00647 Revision 1.28  2009/02/20 10:50:55  jim
00648 Removed superfluous declarations
00649 
00650 Revision 1.27  2008/10/21 08:38:48  jim
00651 Final estiamte on linearity error done with vircam_polyfit now
00652 
00653 Revision 1.26  2008/09/29 11:26:18  jim
00654 Modified linval to provide a lower limit in case the fit goes crazy
00655 
00656 Revision 1.25  2008/08/28 09:05:37  jim
00657 Fixed bug where QC was being duplicated from master BPM on rare occasions.
00658 Fixed bug where ARCFILE wasn't being written to the paf file for illum_cor
00659 tables. Sky combine is done with medians
00660 
00661 Revision 1.24  2008/01/22 19:45:24  jim
00662 New version of genlincur to take into account the equality of readout and
00663 reset time
00664 
00665 Revision 1.23  2007/11/26 09:57:14  jim
00666 Linearity correction routines now take account of ndit
00667 
00668 Revision 1.22  2007/11/22 12:36:15  jim
00669 Modified to return linearised values in an array
00670 
00671 Revision 1.21  2007/11/20 09:38:57  jim
00672 changed definition of fit quality to percentage error at 10000 counts
00673 
00674 Revision 1.20  2007/11/14 14:47:32  jim
00675 Modified the qualfit definition to be back in line with DRLD
00676 
00677 Revision 1.19  2007/11/14 12:34:21  jim
00678 Fixed header comments
00679 
00680 Revision 1.18  2007/11/14 10:48:29  jim
00681 Major rewrite to incorporate simpler and more robust algorithm
00682 
00683 Revision 1.17  2007/03/29 12:19:39  jim
00684 Little changes to improve documentation
00685 
00686 Revision 1.16  2007/03/01 12:42:41  jim
00687 Modified slightly after code checking
00688 
00689 Revision 1.15  2006/11/27 12:08:18  jim
00690 Modified lincor to get a better answer. Also modified the way the fit quality
00691 is calculated
00692 
00693 Revision 1.14  2006/09/29 11:19:31  jim
00694 changed aliases on parameter names
00695 
00696 Revision 1.13  2006/07/04 09:19:05  jim
00697 replaced all sprintf statements with snprintf
00698 
00699 Revision 1.12  2006/06/09 11:26:26  jim
00700 Small changes to keep lint happy
00701 
00702 Revision 1.11  2006/04/20 11:23:15  jim
00703 Now medians the data before accumulation in the event that k is constant.
00704 
00705 Revision 1.10  2006/03/23 21:18:47  jim
00706 Minor changes mainly to comment headers
00707 
00708 Revision 1.9  2006/03/22 13:36:50  jim
00709 cosmetic changes to keep lint happy
00710 
00711 Revision 1.8  2006/03/15 10:43:41  jim
00712 Fixed a few things
00713 
00714 Revision 1.7  2006/03/08 14:32:21  jim
00715 Lots of little modifications
00716 
00717 Revision 1.6  2006/03/03 14:29:46  jim
00718 Modified definition of vir_fits and channel table
00719 
00720 Revision 1.5  2006/03/01 10:31:28  jim
00721 Now uses new vir_fits objects
00722 
00723 Revision 1.4  2006/02/18 11:45:59  jim
00724 Fixed a couple of memory bugs
00725 
00726 Revision 1.3  2006/01/23 22:58:14  jim
00727 Added vircam_lincor module
00728 
00729 Revision 1.2  2006/01/23 16:06:03  jim
00730 Added in header comments and section to evaluate the goodness of fit
00731 
00732 Revision 1.1  2006/01/23 10:31:56  jim
00733 New file
00734 
00735 
00736 */

Generated on 7 Feb 2011 for VIRCAM Pipeline by  doxygen 1.6.1