VIRCAM Pipeline  1.3.3
vircam_mesostep_analyse.c
1 /* $Id: vircam_mesostep_analyse.c,v 1.24 2012-01-16 12:32:18 jim Exp $
2  *
3  * This file is part of the VIRCAM Pipeline
4  * Copyright (C) 2005 Cambridge Astronomy Survey Unit
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: jim $
23  * $Date: 2012-01-16 12:32:18 $
24  * $Revision: 1.24 $
25  * $Name: not supported by cvs2svn $
26  */
27 
28 /* Includes */
29 
30 #ifdef HAVE_CONFIG_H
31 #include <config.h>
32 #endif
33 
34 #include <stdio.h>
35 #include <math.h>
36 #include <string.h>
37 #include <cpl.h>
38 
39 #include "vircam_utils.h"
40 #include "vircam_mask.h"
41 #include "vircam_pfits.h"
42 #include "vircam_dfs.h"
43 #include "vircam_mods.h"
44 #include "vircam_fits.h"
45 #include "vircam_tfits.h"
46 #include "vircam_jmp_utils.h"
47 
48 /* Function prototypes */
49 
50 static int vircam_mesostep_analyse_create(cpl_plugin *);
51 static int vircam_mesostep_analyse_exec(cpl_plugin *);
52 static int vircam_mesostep_analyse_destroy(cpl_plugin *);
53 static int vircam_mesostep_analyse(cpl_parameterlist *, cpl_frameset *);
54 static cpl_propertylist *vircam_mesostep_analyse_dummyqc(int type);
55 
56 
57 static char vircam_mesostep_analyse_description[] =
58 "vircam_mesostep_analyse -- VIRCAM mesostep processing recipe.\n\n"
59 "Process a complete mesostep sequence of vircam data. Remove instrumental\n"
60 "signature and sky subtract if desired. Work out the illumination correction\n"
61 "for all of the input frames and then smooth the result by fitting a 2d\n"
62 "polynomial. Evaluate the polynomial at the grid points to form the final\n"
63 "illumination correction data product\n"
64 "The program accepts the following files in the SOF:\n\n"
65 " Tag Description\n"
66 " -----------------------------------------------------------------------\n"
67 " %-21s A list of raw science images\n"
68 " %-21s A master dark frame\n"
69 " %-21s A master twilight flat frame\n"
70 " %-21s A channel table\n"
71 " %-21s A photometric calibration table\n"
72 " %-21s A master confidence map or\n"
73 " %-21s A master bad pixel mask\n"
74 " %-21s A master standard star index\n"
75 "All of the above are required\n"
76 "\n";
77 
156 /* Function code */
157 
158 /*---------------------------------------------------------------------------*/
166 /*---------------------------------------------------------------------------*/
167 
168 int cpl_plugin_get_info(cpl_pluginlist *list) {
169  cpl_recipe *recipe = cpl_calloc(1,sizeof(*recipe));
170  cpl_plugin *plugin = &recipe->interface;
171  char alldesc[SZ_ALLDESC];
172  (void)snprintf(alldesc,SZ_ALLDESC,vircam_mesostep_analyse_description,
173  VIRCAM_ILLUM_RAW,VIRCAM_CAL_DARK,VIRCAM_CAL_TWILIGHT_FLAT,
174  VIRCAM_CAL_CHANTAB,VIRCAM_CAL_PHOTTAB,VIRCAM_CAL_CONF,
175  VIRCAM_CAL_BPM,VIRCAM_CAL_2MASS);
176 
177  cpl_plugin_init(plugin,
178  CPL_PLUGIN_API,
179  VIRCAM_BINARY_VERSION,
180  CPL_PLUGIN_TYPE_RECIPE,
181  "vircam_mesostep_analyse",
182  "VIRCAM mesostep analysis recipe",
183  alldesc,
184  "Jim Lewis",
185  "jrl@ast.cam.ac.uk",
187  vircam_mesostep_analyse_create,
188  vircam_mesostep_analyse_exec,
189  vircam_mesostep_analyse_destroy);
190 
191  cpl_pluginlist_append(list,plugin);
192 
193  return(0);
194 }
195 
196 /*---------------------------------------------------------------------------*/
205 /*---------------------------------------------------------------------------*/
206 
207 static int vircam_mesostep_analyse_create(cpl_plugin *plugin) {
208  cpl_recipe *recipe;
209  cpl_parameter *p;
210 
211  /* Get the recipe out of the plugin */
212 
213  if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
214  recipe = (cpl_recipe *)plugin;
215  else
216  return(-1);
217 
218  /* Create the parameters list in the cpl_recipe object */
219 
220  recipe->parameters = cpl_parameterlist_new();
221 
222  /* Fill in the minimum object size */
223 
224  p = cpl_parameter_new_value("vircam.vircam_mesostep_analyse.ipix",
225  CPL_TYPE_INT,
226  "Minimum pixel area for each detected object",
227  "vircam.vircam_mesostep_analyse",5);
228  cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI,"ipix");
229  cpl_parameterlist_append(recipe->parameters,p);
230 
231  /* Fill in the detection threshold parameter */
232 
233  p = cpl_parameter_new_value("vircam.vircam_mesostep_analyse.thresh",
234  CPL_TYPE_DOUBLE,
235  "Detection threshold in sigma above sky",
236  "vircam.vircam_mesostep_analyse",2.0);
237  cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI,"thr");
238  cpl_parameterlist_append(recipe->parameters,p);
239 
240  /* Fill in flag to use deblending software or not */
241 
242  p = cpl_parameter_new_value("vircam.vircam_mesostep_analyse.icrowd",
243  CPL_TYPE_BOOL,"Use deblending?",
244  "vircam.vircam_mesostep_analyse",0);
245  cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI,"icrowd");
246  cpl_parameterlist_append(recipe->parameters,p);
247 
248  /* Fill in core radius */
249 
250  p = cpl_parameter_new_value("vircam.vircam_mesostep_analyse.rcore",
251  CPL_TYPE_DOUBLE,"Value of Rcore in pixels",
252  "vircam.vircam_mesostep_analyse",3.0);
253  cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI,"rcore");
254  cpl_parameterlist_append(recipe->parameters,p);
255 
256  /* Fill in background smoothing box size */
257 
258  p = cpl_parameter_new_value("vircam.vircam_mesostep_analyse.nbsize",
259  CPL_TYPE_INT,"Background smoothing box size",
260  "vircam.vircam_mesostep_analyse",64);
261  cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI,"nb");
262  cpl_parameterlist_append(recipe->parameters,p);
263 
264  /* Fill in flag to destripe the images */
265 
266  p = cpl_parameter_new_value("vircam.vircam_mesostep_analyse.destripe",
267  CPL_TYPE_BOOL,"Destripe images?",
268  "vircam.vircam_mesostep_analyse",1);
269  cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI,"destripe");
270  cpl_parameterlist_append(recipe->parameters,p);
271 
272  /* Fill in flag to correct sky background */
273 
274  p = cpl_parameter_new_value("vircam.vircam_mesostep_analyse.skycor",
275  CPL_TYPE_BOOL,"Sky correct images?",
276  "vircam.vircam_mesostep_analyse",1);
277  cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI,"skycor");
278  cpl_parameterlist_append(recipe->parameters,p);
279 
280  /* Order for surface fit */
281 
282  p = cpl_parameter_new_value("vircam.vircam_mesostep_analyse.nord",
283  CPL_TYPE_INT,
284  "Polynomial order for surface fit",
285  "vircam.vircam_mesostep_analyse",3);
286  cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI,"nord");
287  cpl_parameterlist_append(recipe->parameters,p);
288 
289  /* Extension number of input frames to use */
290 
291  p = cpl_parameter_new_range("vircam.vircam_mesostep_analyse.extenum",
292  CPL_TYPE_INT,
293  "Extension number to be done, 0 == all",
294  "vircam.vircam_mesostep_analyse",
295  1,0,16);
296  cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI,"ext");
297  cpl_parameterlist_append(recipe->parameters,p);
298 
299  /* Get out of here */
300 
301  return(0);
302 }
303 
304 /*---------------------------------------------------------------------------*/
310 /*---------------------------------------------------------------------------*/
311 
312 static int vircam_mesostep_analyse_exec(cpl_plugin *plugin) {
313  cpl_recipe *recipe;
314 
315  /* Get the recipe out of the plugin */
316 
317  if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
318  recipe = (cpl_recipe *)plugin;
319  else
320  return(-1);
321 
322  return(vircam_mesostep_analyse(recipe->parameters,recipe->frames));
323 }
324 
325 /*---------------------------------------------------------------------------*/
331 /*---------------------------------------------------------------------------*/
332 
333 static int vircam_mesostep_analyse_destroy(cpl_plugin *plugin) {
334  cpl_recipe *recipe ;
335 
336  /* Get the recipe out of the plugin */
337 
338  if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
339  recipe = (cpl_recipe *)plugin;
340  else
341  return(-1);
342 
343  cpl_parameterlist_delete(recipe->parameters);
344  return(0);
345 }
346 
347 /*---------------------------------------------------------------------------*/
354 /*---------------------------------------------------------------------------*/
355 
356 static int vircam_mesostep_analyse(cpl_parameterlist *parlist,
357  cpl_frameset *framelist) {
358  const char *fctid="vircam_mesostep_analyse";
359  cpl_parameter *p;
360  cpl_polynomial *poly;
361  int jst,jfn,status,isconf,j,i,live,nrows,n,ndit;
362  cpl_size nlab;
363  float *xx1,*xx2,*yy1,*yy2,*ill,gaincor_fac;
364  double *bv_x,*bv_y,*vdata,val;
365  cpl_bivector *bv;
366  cpl_vector *v;
367  vir_fits *ff;
368  cpl_table *ic;
369  cpl_frame *catindex;
370  cpl_propertylist *pp;
371 
372  /* Check validity of input frameset */
373 
374  if (framelist == NULL || cpl_frameset_get_size(framelist) <= 0) {
375  cpl_msg_error(fctid,"Input framelist NULL or has no input data");
376  return(-1);
377  }
378 
379  /* Check the files in the frameset */
380 
381  if (vircam_frameset_fexists(framelist) != VIR_OK) {
382  cpl_msg_error(fctid,"Input frameset is missing files. Check SOF");
383  return(-1);
384  }
385 
386  /* Initialise some things */
387 
388  vircam_jmp_init();
389  (void)strncpy(vircam_recipename,fctid,VIRCAM_PATHSZ);
390  (void)snprintf(vircam_recipepaf,VIRCAM_PATHSZ,"VIRCAM/%s",fctid);
391  recflag = RECMES;
392 
393  /* Get the parameters */
394 
395  p = cpl_parameterlist_find(parlist,
396  "vircam.vircam_mesostep_analyse.ipix");
397  vircam_jmp_config.ipix = cpl_parameter_get_int(p);
398  p = cpl_parameterlist_find(parlist,
399  "vircam.vircam_mesostep_analyse.thresh");
400  vircam_jmp_config.threshold = (float)cpl_parameter_get_double(p);
401  p = cpl_parameterlist_find(parlist,
402  "vircam.vircam_mesostep_analyse.icrowd");
403  vircam_jmp_config.icrowd = cpl_parameter_get_bool(p);
404  p = cpl_parameterlist_find(parlist,
405  "vircam.vircam_mesostep_analyse.rcore");
406  vircam_jmp_config.rcore = (float)cpl_parameter_get_double(p);
407  p = cpl_parameterlist_find(parlist,
408  "vircam.vircam_mesostep_analyse.nbsize");
409  vircam_jmp_config.nbsize = cpl_parameter_get_int(p);
410  p = cpl_parameterlist_find(parlist,
411  "vircam.vircam_mesostep_analyse.destripe");
412  vircam_jmp_config.destripe = cpl_parameter_get_bool(p);
413  p = cpl_parameterlist_find(parlist,
414  "vircam.vircam_mesostep_analyse.skycor");
415  vircam_jmp_config.skycor = cpl_parameter_get_bool(p);
416  p = cpl_parameterlist_find(parlist,
417  "vircam.vircam_mesostep_analyse.nord");
418  vircam_jmp_config.nord = cpl_parameter_get_int(p);
419  p = cpl_parameterlist_find(parlist,
420  "vircam.vircam_mesostep_analyse.extenum");
421  vircam_jmp_config.extenum = cpl_parameter_get_int(p);
422 
423  /* Sort out raw from calib frames */
424 
425  if (vircam_dfs_set_groups(framelist) != VIR_OK) {
426  cpl_msg_error(fctid,"Cannot identify RAW and CALIB frames");
427  vircam_jmp_tidy(0);
428  return(-1);
429  }
430 
431  /* Label the input frames */
432 
433  if ((ps.labels = cpl_frameset_labelise(framelist,vircam_compare_tags,
434  &nlab)) == NULL) {
435  cpl_msg_error(fctid,"Cannot labelise the input frames");
436  vircam_jmp_tidy(0);
437  return(-1);
438  }
439 
440  /* Get the input science frames */
441 
442  if ((ps.science_frames =
443  vircam_frameset_subgroup(framelist,ps.labels,nlab,
444  VIRCAM_ILLUM_RAW)) == NULL) {
445  cpl_msg_error(fctid,"No science images to process!");
446  vircam_jmp_tidy(0);
447  return(-1);
448  }
449 
450  /* Check to see if there is a master dark frame */
451 
452  if ((ps.master_dark =
453  vircam_frameset_subgroup_1(framelist,ps.labels,nlab,
454  VIRCAM_CAL_DARK)) == NULL) {
455  cpl_msg_error(fctid,"No master dark found");
456  vircam_jmp_tidy(0);
457  return(-1);
458  }
459 
460  /* Check to see if there is a master twilight flat frame */
461 
462  if ((ps.master_twilight_flat =
463  vircam_frameset_subgroup_1(framelist,ps.labels,nlab,
464  VIRCAM_CAL_TWILIGHT_FLAT)) == NULL) {
465  cpl_msg_error(fctid,"No master twilight flat found");
466  vircam_jmp_tidy(0);
467  return(-1);
468  }
469 
470  /* Get the gain corrections */
471 
472  status = VIR_OK;
473  if (vircam_gaincor_calc(ps.master_twilight_flat,&i,&(ps.gaincors),
474  &status) != VIR_OK) {
475  cpl_msg_error(fctid,"Error calculating gain corrections");
476  vircam_jmp_tidy(0);
477  return(-1);
478  }
479 
480  /* Check to see if there is a master confidence map. If there isn't
481  then look for a bad pixel mask that can be converted into a
482  confidence map (in an emergency) */
483 
484  isconf = 1;
485  if ((ps.master_conf =
486  vircam_frameset_subgroup_1(framelist,ps.labels,nlab,
487  VIRCAM_CAL_CONF)) == NULL) {
488  isconf = 0;
489  if ((ps.master_conf =
490  vircam_frameset_subgroup_1(framelist,ps.labels,nlab,
491  VIRCAM_CAL_BPM)) == NULL) {
492  cpl_msg_error(fctid,"No master confidence map found");
493  vircam_jmp_tidy(0);
494  return(-1);
495  }
496  }
497  ps.mask = vircam_mask_define(framelist,ps.labels,nlab);
498 
499  /* Check to see if there is a channel table */
500 
501  if ((ps.chantab = vircam_frameset_subgroup_1(framelist,ps.labels,nlab,
502  VIRCAM_CAL_CHANTAB)) == NULL) {
503  cpl_msg_error(fctid,"No channel table found");
504  vircam_jmp_tidy(0);
505  return(-1);
506  }
507 
508  /* Check to see if there is a photometric table */
509 
510  if ((ps.phottab = vircam_frameset_subgroup_1(framelist,ps.labels,nlab,
511  VIRCAM_CAL_PHOTTAB)) == NULL) {
512  cpl_msg_error(fctid,"No photometric table found");
513  vircam_jmp_tidy(0);
514  return(-1);
515  }
516  if ((ps.tphottab = cpl_table_load(cpl_frame_get_filename(ps.phottab),1,0)) == NULL) {
517  cpl_msg_error(fctid,"Unable to load photometric table");
518  vircam_jmp_tidy(0);
519  return(-1);
520  }
521 
522  /* Is the 2mass index file specified? */
523 
524  if ((catindex = vircam_frameset_subgroup_1(framelist,ps.labels,nlab,
525  VIRCAM_CAL_2MASS)) == NULL) {
526  cpl_msg_info(fctid,"No 2MASS index found -- cannot continue");
527  vircam_jmp_tidy(0);
528  return(-1);
529  }
530 
531  /* Get catalogue parameters */
532 
533  if (vircam_catpars(catindex,&(ps.catpath),&(ps.catname)) == VIR_FATAL) {
534  vircam_jmp_tidy(0);
535  cpl_frame_delete(catindex);
536  return(-1);
537  }
538  cpl_frame_delete(catindex);
539 
540  /* Get the number of DITs */
541 
542  pp = cpl_propertylist_load(cpl_frame_get_filename(cpl_frameset_get_frame(ps.science_frames,0)),0);
543  if (vircam_pfits_get_ndit(pp,&ndit) != VIR_OK) {
544  cpl_msg_error(fctid,"No value for NDIT available");
545  freepropertylist(pp);
546  vircam_jmp_tidy(0);
547  return(-1);
548  }
549  cpl_propertylist_delete(pp);
550 
551  /* Now, how many image extensions do we want to do? If the extension
552  number is zero, then we loop for all possible extensions. If it
553  isn't then we just do the extension specified */
554 
555  vircam_exten_range(vircam_jmp_config.extenum,
556  (const cpl_frame *)cpl_frameset_get_frame(ps.science_frames,0),
557  &jst,&jfn);
558  if (jst == -1 || jfn == -1) {
559  cpl_msg_error(fctid,"Unable to continue");
560  vircam_jmp_tidy(0);
561  return(-1);
562  }
563 
564  /* Now loop for all the extensions... */
565 
566  status = VIR_OK;
567  for (j = jst; j <= jfn; j++) {
568  isfirst = (j == jst);
569  gaincor_fac = (ps.gaincors)[j-1];
570 
571  /* Load up the calibration frames into vir_fits/vir_tfits structures
572  It is a fatal error if any one of them can't load properly */
573 
574  ps.fdark = vircam_fits_load(ps.master_dark,CPL_TYPE_FLOAT,j);
575  if (ps.fdark == NULL) {
576  cpl_msg_error(fctid,
577  "Error loading master dark %s[%" CPL_SIZE_FORMAT "]\n%s",
578  cpl_frame_get_filename(ps.master_dark),(cpl_size)j,
579  cpl_error_get_message());
580  vircam_jmp_tidy(0);
581  return(-1);
582  }
583  ps.fflat = vircam_fits_load(ps.master_twilight_flat,CPL_TYPE_FLOAT,j);
584  if (ps.fflat == NULL) {
585  cpl_msg_error(fctid,
586  "Error loading master flat %s[%" CPL_SIZE_FORMAT "]\n%s",
587  cpl_frame_get_filename(ps.master_twilight_flat),
588  (cpl_size)j,cpl_error_get_message());
589  vircam_jmp_tidy(0);
590  return(-1);
591  }
592  ps.fconf = vircam_fits_load(ps.master_conf,CPL_TYPE_INT,j);
593  if (ps.fconf == NULL) {
594  cpl_msg_error(fctid,
595  "Error loading master conf %s[%" CPL_SIZE_FORMAT "]\n%s",
596  cpl_frame_get_filename(ps.master_conf),(cpl_size)j,
597  cpl_error_get_message());
598  vircam_jmp_tidy(0);
599  return(-1);
600  }
601  if (! isconf)
603  if (vircam_mask_load(ps.mask,j,
604  (int)cpl_image_get_size_x(vircam_fits_get_image(ps.fconf)),
605  (int)cpl_image_get_size_y(vircam_fits_get_image(ps.fconf))) != VIR_OK) {
606  cpl_msg_error(fctid,
607  "Error loading mask from master conf %s[%" CPL_SIZE_FORMAT "]\n%s",
608  cpl_frame_get_filename(ps.master_conf),(cpl_size)j,
609  cpl_error_get_message());
610  vircam_jmp_tidy(0);
611  return(-1);
612  }
613  ps.fchantab = vircam_tfits_load(ps.chantab,j);
614  if (ps.fchantab == NULL) {
615  cpl_msg_error(fctid,"Error loading channel table %s[%" CPL_SIZE_FORMAT "]\n%s",
616  cpl_frame_get_filename(ps.chantab),(cpl_size)j,
617  cpl_error_get_message());
618  vircam_jmp_tidy(0);
619  return(-1);
620  }
621 
622  /* Load up the vir_fits structures for the science images */
623 
624  ps.nscience = cpl_frameset_get_size(ps.science_frames);
625  ps.sci_fits = vircam_fits_load_list(ps.science_frames,CPL_TYPE_FLOAT,j);
626  if (ps.sci_fits == NULL) {
627  cpl_msg_error(fctid,
628  "Error loading science frames extension %" CPL_SIZE_FORMAT ": %s",
629  (cpl_size)j,cpl_error_get_message());
630  vircam_jmp_tidy(0);
631  return(-1);
632  }
633 
634  /* Loop through and mark the frames where the header says the detector
635  wasn't live */
636 
637  for (i = 0; i < ps.nscience; i++) {
638  ff = ps.sci_fits[i];
640  if (! live)
641  vircam_fits_set_error(ff,VIR_FATAL);
642  }
643 
644  /* Loop for all the science frames and do the 2d corrections */
645 
646  cpl_msg_info(fctid,"Doing stage1 corrections on %s",
647  vircam_fits_get_extname(ps.sci_fits[0]));
648  for (i = 0; i < ps.nscience; i++) {
649  ff = ps.sci_fits[i];
650  if (vircam_fits_get_status(ff) == VIR_FATAL) {
651  cpl_msg_info(fctid,"Detector is flagged dead in %s",
653  continue;
654  }
655  status = VIR_OK;
656  (void)vircam_darkcor(ff,ps.fdark,1.0,&status);
657  (void)vircam_lincor(ff,ps.fchantab,1,ndit,&status);
658  (void)vircam_flatcor(ff,ps.fflat,&status);
659  (void)vircam_gaincor(ff,gaincor_fac,&status);
660  if (vircam_jmp_config.destripe)
661  (void)vircam_destripe(ff,ps.mask,&status);
662  vircam_fits_set_error(ff,status);
663  }
664 
665  /* Do a simple sky correction if requested */
666 
667  if (vircam_jmp_config.skycor) {
668  cpl_msg_info(fctid,"Doing sky correction");
670  }
671 
672  /* Calculate the illumination correction */
673 
674  cpl_msg_info(fctid,"Doing illumination correction");
675  (void)strcpy(current_cat,ps.catname);
676  (void)strcpy(current_catpath,ps.catpath);
677  vircam_jmp_illum();
678 
679  /* Get the data from the illumination correction table */
680 
681  if (ps.illcor != NULL) {
682  ic = vircam_tfits_get_table(ps.illcor);
683  nrows = (int)cpl_table_get_nrow(ic);
684  xx1 = cpl_table_get_data_float(ic,"xmin");
685  xx2 = cpl_table_get_data_float(ic,"xmax");
686  yy1 = cpl_table_get_data_float(ic,"ymin");
687  yy2 = cpl_table_get_data_float(ic,"ymax");
688  ill = cpl_table_get_data_float(ic,"illcor");
689  n = 0;
690  for (i = 0; i < nrows; i++)
691  if (ill[i] != -99.0)
692  n++;
693 
694  /* If there is nothing to fit, then don't bother with the rest
695  of this */
696 
697  if (n == 0) {
698  cpl_msg_warning(fctid,"Illum correction table is all NULLs");
699 
700  } else {
701 
702  /* Create bivector and vector objects with the xy positions
703  and the illumination correction */
704 
705  bv = cpl_bivector_new((cpl_size)n);
706  bv_x = cpl_bivector_get_x_data(bv);
707  bv_y = cpl_bivector_get_y_data(bv);
708  v = cpl_vector_new((cpl_size)n);
709  vdata = cpl_vector_get_data(v);
710  n = 0;
711  for (i = 0; i < nrows; i++) {
712  if (ill[i] == -99.0)
713  continue;
714  bv_x[n] = 0.5*(double)(xx2[i] + xx1[i]);
715  bv_y[n] = 0.5*(double)(yy2[i] + yy1[i]);
716  vdata[n++] = (double)ill[i];
717  }
718 
719  /* Now fit a surface to these results */
720 
721  poly = cpl_polynomial_fit_2d_create(bv,v,
722  (cpl_size)(vircam_jmp_config.nord),
723  NULL);
724  cpl_vector_delete(v);
725  if (cpl_error_get_code() != 0) {
726  cpl_error_reset();
727  } else {
728 
729  /* Evaluate the polynomial at the input points and replace
730  the values in the illumination correction with the new
731  value. */
732 
733  v = cpl_vector_new(2);
734  vdata = cpl_vector_get_data(v);
735  for (i = 0; i < nrows; i++) {
736  vdata[0] = 0.5*(double)(xx2[i] + xx1[i]);
737  vdata[1] = 0.5*(double)(yy2[i] + yy1[i]);
738  val = cpl_polynomial_eval(poly,v);
739  ill[i] = val;
740  }
741  cpl_vector_delete(v);
742  }
743  cpl_bivector_delete(bv);
744  cpl_polynomial_delete(poly);
745  }
746  }
747 
748  /* Save the table */
749 
750  cpl_msg_info(fctid,"Saving illumination correction table");
751  dummyqc = vircam_mesostep_analyse_dummyqc(3);
752  vircam_jmp_save_illum(framelist,parlist);
753  freepropertylist(dummyqc);
754 
755  /* Clean up on aisle 12! */
756 
757  vircam_jmp_tidy(1);
758  }
759 
760  /* Final cleanup */
761 
762  vircam_jmp_tidy(0);
763  return(0);
764 }
765 
766 static cpl_propertylist *vircam_mesostep_analyse_dummyqc(int type) {
767  cpl_propertylist *p;
768 
769  /* Get an empty property list */
770 
771  p = cpl_propertylist_new();
772 
773  /* Now switch for the various products */
774 
775  switch (type) {
776 
777  /* Illumination tables */
778 
779  case 3:
780  cpl_propertylist_update_float(p,"ESO QC ILLUMCOR_RMS",0.0);
781  cpl_propertylist_set_comment(p,"ESO QC ILLUMCOR_RMS",
782  "RMS of illumination correction map");
783  break;
784  default:
785  break;
786  }
787 
788  /* Get out of here */
789 
790  return(p);
791 }
792 
795 /*
796 
797 $Log: not supported by cvs2svn $
798 Revision 1.23 2012/01/15 17:40:09 jim
799 Minor modifications to take into accout the changes in cpl API for v6
800 
801 Revision 1.22 2010/06/30 12:42:00 jim
802 A few fixes to stop compiler compaints
803 
804 Revision 1.21 2009/07/03 12:30:04 jim
805 Default value of rcore is now 3
806 
807 Revision 1.20 2008/12/09 11:35:51 jim
808 Fixed bug in illumination correction routine which didn't deal properly with
809 the situation where the fit failed
810 
811 Revision 1.19 2008/10/01 04:59:13 jim
812 Added call to vircam_frameset_fexists to check input frameset
813 
814 Revision 1.18 2008/05/06 12:15:20 jim
815 Changed to use new version of vircam_catpars
816 
817 Revision 1.17 2007/11/26 09:59:06 jim
818 Recipe now takes ndit into account when doing linearity correction
819 
820 Revision 1.16 2007/10/25 18:39:22 jim
821 Altered to remove some lint messages
822 
823 Revision 1.15 2007/10/19 06:55:06 jim
824 Modifications made to use new method for directing the recipes to the
825 standard catalogues using the sof
826 
827 Revision 1.14 2007/07/09 13:21:56 jim
828 Modified to use new version of vircam_exten_range
829 
830 Revision 1.13 2007/06/13 08:11:27 jim
831 Modified docs to reflect changes in DFS tags
832 
833 Revision 1.12 2007/05/08 21:31:16 jim
834 fixed typo
835 
836 Revision 1.11 2007/05/08 10:42:44 jim
837 Added gain correction
838 
839 Revision 1.10 2007/05/02 12:53:11 jim
840 typo fixes in docs
841 
842 Revision 1.9 2007/04/04 16:05:59 jim
843 Modified to make paf information a bit more correct
844 
845 Revision 1.8 2007/04/04 10:36:18 jim
846 Modified to use new dfs tags
847 
848 Revision 1.7 2007/03/29 12:19:38 jim
849 Little changes to improve documentation
850 
851 Revision 1.6 2007/03/14 14:49:13 jim
852 Fixed problem with missing paf files in jmp recipes if detlive = F. Also
853 fixed problem where extra dummy products were being created
854 
855 Revision 1.5 2007/02/07 10:12:40 jim
856 Removed calls to vircam_ndit_correct as this is now no longer necessary
857 
858 Revision 1.4 2006/12/19 13:33:02 jim
859 Blocked off bivariate fit so that in the event that the original illumnation
860 correction fails, it doesn't then try to fit the surface.
861 
862 Revision 1.3 2006/12/18 16:43:15 jim
863 fixed bug with nord parameter doc
864 
865 Revision 1.2 2006/12/18 16:42:27 jim
866 Blocked off the fitting bit so that it doesn't get done if the initial
867 illumination correction fails.
868 
869 Revision 1.1 2006/12/04 21:10:14 jim
870 Initial entry
871 
872 
873 */