VIRCAM Pipeline  1.3.3
vircam_standard_process.c
1 /* $Id: vircam_standard_process.c,v 1.43 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.43 $
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 #include "vircam_paf.h"
48 
49 /* Function prototypes */
50 
51 static int vircam_standard_process_create(cpl_plugin *);
52 static int vircam_standard_process_exec(cpl_plugin *);
53 static int vircam_standard_process_destroy(cpl_plugin *);
54 static int vircam_standard_process(cpl_parameterlist *,cpl_frameset *);
55 static cpl_propertylist *vircam_standard_process_dummyqc(int type);
56 
57 
58 static char vircam_standard_process_description[] =
59 "vircam_standard_process -- VIRCAM standard field processing recipe.\n\n"
60 "Process a complete pawprint for standard fields in VIRCAM data.\n"
61 "Remove instrumental signature, interleave microstep sequences (if done),\n"
62 "combine jitters, photometrically and astrometrically calibrate the pawprint\n"
63 "image. Photometric calibration can be done with 2mass and 1 other source\n\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 readnoise/gain file\n"
73 " %-21s A master confidence map or\n"
74 " %-21s A master bad pixel mask\n"
75 " %-21s A master 2mass catalogue index\n"
76 " %-21s A second reference catalogue index\n"
77 "All of the above are required\n"
78 "\n";
79 
223 /* Function code */
224 
225 /*---------------------------------------------------------------------------*/
233 /*---------------------------------------------------------------------------*/
234 
235 int cpl_plugin_get_info(cpl_pluginlist *list) {
236  cpl_recipe *recipe = cpl_calloc(1,sizeof(*recipe));
237  cpl_plugin *plugin = &recipe->interface;
238  char alldesc[SZ_ALLDESC];
239  (void)snprintf(alldesc,SZ_ALLDESC,vircam_standard_process_description,
240  VIRCAM_STD_OBJECT_RAW,VIRCAM_CAL_DARK,
241  VIRCAM_CAL_TWILIGHT_FLAT,VIRCAM_CAL_CHANTAB,
242  VIRCAM_CAL_PHOTTAB,VIRCAM_CAL_READGAINFILE,VIRCAM_CAL_CONF,
243  VIRCAM_CAL_BPM,VIRCAM_CAL_2MASS,VIRCAM_CAL_REFCAT);
244 
245  cpl_plugin_init(plugin,
246  CPL_PLUGIN_API,
247  VIRCAM_BINARY_VERSION,
248  CPL_PLUGIN_TYPE_RECIPE,
249  "vircam_standard_process",
250  "VIRCAM standard field processing recipe",
251  alldesc,
252  "Jim Lewis",
253  "jrl@ast.cam.ac.uk",
255  vircam_standard_process_create,
256  vircam_standard_process_exec,
257  vircam_standard_process_destroy);
258 
259  cpl_pluginlist_append(list,plugin);
260 
261  return(0);
262 }
263 
264 /*---------------------------------------------------------------------------*/
273 /*---------------------------------------------------------------------------*/
274 
275 static int vircam_standard_process_create(cpl_plugin *plugin) {
276  cpl_recipe *recipe;
277  cpl_parameter *p;
278 
279  /* Get the recipe out of the plugin */
280 
281  if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
282  recipe = (cpl_recipe *)plugin;
283  else
284  return(-1);
285 
286  /* Create the parameters list in the cpl_recipe object */
287 
288  recipe->parameters = cpl_parameterlist_new();
289 
290  /* Fill in the minimum object size */
291 
292  p = cpl_parameter_new_value("vircam.vircam_standard_process.ipix",
293  CPL_TYPE_INT,
294  "Minimum pixel area for each detected object",
295  "vircam.vircam_standard_process",5);
296  cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI,"ipix");
297  cpl_parameterlist_append(recipe->parameters,p);
298 
299  /* Fill in the detection threshold parameter */
300 
301  p = cpl_parameter_new_value("vircam.vircam_standard_process.thresh",
302  CPL_TYPE_DOUBLE,
303  "Detection threshold in sigma above sky",
304  "vircam.vircam_standard_process",2.0);
305  cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI,"thr");
306  cpl_parameterlist_append(recipe->parameters,p);
307 
308  /* Fill in flag to use deblending software or not */
309 
310  p = cpl_parameter_new_value("vircam.vircam_standard_process.icrowd",
311  CPL_TYPE_BOOL,"Use deblending?",
312  "vircam.vircam_standard_process",1);
313  cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI,"icrowd");
314  cpl_parameterlist_append(recipe->parameters,p);
315 
316  /* Fill in core radius */
317 
318  p = cpl_parameter_new_value("vircam.vircam_standard_process.rcore",
319  CPL_TYPE_DOUBLE,"Value of Rcore in pixels",
320  "vircam.vircam_standard_process",3.0);
321  cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI,"rcore");
322  cpl_parameterlist_append(recipe->parameters,p);
323 
324  /* Fill in background smoothing box size */
325 
326  p = cpl_parameter_new_value("vircam.vircam_standard_process.nbsize",
327  CPL_TYPE_INT,"Background smoothing box size",
328  "vircam.vircam_standard_process",64);
329  cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI,"nb");
330  cpl_parameterlist_append(recipe->parameters,p);
331 
332  /* Fill in flag to use save the output catalogue */
333 
334  p = cpl_parameter_new_value("vircam.vircam_standard_process.savecat",
335  CPL_TYPE_BOOL,"Save catalogue?",
336  "vircam.vircam_standard_process",1);
337  cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI,"savecat");
338  cpl_parameterlist_append(recipe->parameters,p);
339 
340 
341  /* Fill in flag to destripe the images */
342 
343  p = cpl_parameter_new_value("vircam.vircam_standard_process.destripe",
344  CPL_TYPE_BOOL,"Destripe images?",
345  "vircam.vircam_standard_process",1);
346  cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI,"destripe");
347  cpl_parameterlist_append(recipe->parameters,p);
348 
349  /* Fill in flag to correct sky background */
350 
351  p = cpl_parameter_new_value("vircam.vircam_standard_process.skycor",
352  CPL_TYPE_BOOL,"Do sky correction?",
353  "vircam.vircam_standard_process",1);
354  cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI,"skycor");
355  cpl_parameterlist_append(recipe->parameters,p);
356 
357  /* Fill in flag to save simple images */
358 
359  p = cpl_parameter_new_value("vircam.vircam_standard_process.savesimple",
360  CPL_TYPE_BOOL,"Save simple images?",
361  "vircam.vircam_standard_process",0);
362  cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI,"savesimple");
363  cpl_parameterlist_append(recipe->parameters,p);
364 
365  /* Extension number of input frames to use */
366 
367  p = cpl_parameter_new_range("vircam.vircam_standard_process.extenum",
368  CPL_TYPE_INT,
369  "Extension number to be done, 0 == all",
370  "vircam.vircam_standard_process",
371  1,0,16);
372  cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI,"ext");
373  cpl_parameterlist_append(recipe->parameters,p);
374 
375  /* Get out of here */
376 
377  return(0);
378 }
379 
380 
381 /*---------------------------------------------------------------------------*/
387 /*---------------------------------------------------------------------------*/
388 
389 static int vircam_standard_process_exec(cpl_plugin *plugin) {
390  cpl_recipe *recipe;
391 
392  /* Get the recipe out of the plugin */
393 
394  if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
395  recipe = (cpl_recipe *)plugin;
396  else
397  return(-1);
398 
399  return(vircam_standard_process(recipe->parameters,recipe->frames));
400 }
401 
402 /*---------------------------------------------------------------------------*/
408 /*---------------------------------------------------------------------------*/
409 
410 static int vircam_standard_process_destroy(cpl_plugin *plugin) {
411  cpl_recipe *recipe ;
412 
413  /* Get the recipe out of the plugin */
414 
415  if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
416  recipe = (cpl_recipe *)plugin;
417  else
418  return(-1);
419 
420  cpl_parameterlist_delete(recipe->parameters);
421  return(0);
422 }
423 
424 /*---------------------------------------------------------------------------*/
431 /*---------------------------------------------------------------------------*/
432 
433 static int vircam_standard_process(cpl_parameterlist *parlist,
434  cpl_frameset *framelist) {
435  const char *fctid="vircam_standard_process";
436  cpl_parameter *p;
437  int jst,jfn,status,j,i,retval,nusteps,isconf,live,ndit;
438  cpl_size nlab;
439  float readnoise,gain,gaincor_fac;
440  vir_fits *ff;
441  cpl_propertylist *ehu,*pp;
442  cpl_frame *catindex;
443 
444  /* Check validity of input frameset */
445 
446  if (framelist == NULL || cpl_frameset_get_size(framelist) <= 0) {
447  cpl_msg_error(fctid,"Input framelist NULL or has no input data");
448  return(-1);
449  }
450 
451  /* Check the files in the frameset */
452 
453  if (vircam_frameset_fexists(framelist) != VIR_OK) {
454  cpl_msg_error(fctid,"Input frameset is missing files. Check SOF");
455  return(-1);
456  }
457 
458  /* Initialise some things */
459 
460  vircam_jmp_init();
461  (void)strncpy(vircam_recipename,fctid,VIRCAM_PATHSZ);
462  (void)snprintf(vircam_recipepaf,VIRCAM_PATHSZ,"VIRCAM/%s",fctid);
463  recflag = RECSTD;
464 
465  /* Get the parameters */
466 
467  p = cpl_parameterlist_find(parlist,
468  "vircam.vircam_standard_process.ipix");
469  vircam_jmp_config.ipix = cpl_parameter_get_int(p);
470  p = cpl_parameterlist_find(parlist,
471  "vircam.vircam_standard_process.thresh");
472  vircam_jmp_config.threshold = (float)cpl_parameter_get_double(p);
473  p = cpl_parameterlist_find(parlist,
474  "vircam.vircam_standard_process.icrowd");
475  vircam_jmp_config.icrowd = cpl_parameter_get_bool(p);
476  p = cpl_parameterlist_find(parlist,
477  "vircam.vircam_standard_process.rcore");
478  vircam_jmp_config.rcore = (float)cpl_parameter_get_double(p);
479  p = cpl_parameterlist_find(parlist,
480  "vircam.vircam_standard_process.nbsize");
481  vircam_jmp_config.nbsize = cpl_parameter_get_int(p);
482  p = cpl_parameterlist_find(parlist,
483  "vircam.vircam_standard_process.savecat");
484  vircam_jmp_config.savecat = cpl_parameter_get_bool(p);
485  p = cpl_parameterlist_find(parlist,
486  "vircam.vircam_standard_process.destripe");
487  vircam_jmp_config.destripe = cpl_parameter_get_bool(p);
488  p = cpl_parameterlist_find(parlist,
489  "vircam.vircam_standard_process.skycor");
490  vircam_jmp_config.skycor = cpl_parameter_get_bool(p);
491  p = cpl_parameterlist_find(parlist,
492  "vircam.vircam_standard_process.savesimple");
493  vircam_jmp_config.savesimple = cpl_parameter_get_bool(p);
494  p = cpl_parameterlist_find(parlist,
495  "vircam.vircam_standard_process.extenum");
496  vircam_jmp_config.extenum = cpl_parameter_get_int(p);
497 
498  /* Sort out raw from calib frames */
499 
500  if (vircam_dfs_set_groups(framelist) != VIR_OK) {
501  cpl_msg_error(fctid,"Cannot identify RAW and CALIB frames");
502  vircam_jmp_tidy(0);
503  return(-1);
504  }
505 
506  /* Label the input frames */
507 
508  if ((ps.labels = cpl_frameset_labelise(framelist,vircam_compare_tags,
509  &nlab)) == NULL) {
510  cpl_msg_error(fctid,"Cannot labelise the input frames");
511  vircam_jmp_tidy(0);
512  return(-1);
513  }
514 
515  /* Get the input science frames */
516 
517  if ((ps.science_frames =
518  vircam_frameset_subgroup(framelist,ps.labels,nlab,
519  VIRCAM_STD_OBJECT_RAW)) == NULL) {
520  cpl_msg_error(fctid,"No science images to process!");
521  vircam_jmp_tidy(0);
522  return(-1);
523  }
524 
525  /* Check to see if there is a master dark frame */
526 
527  if ((ps.master_dark =
528  vircam_frameset_subgroup_1(framelist,ps.labels,nlab,
529  VIRCAM_CAL_DARK)) == NULL) {
530  cpl_msg_error(fctid,"No master dark found");
531  vircam_jmp_tidy(0);
532  return(-1);
533  }
534 
535  /* Check to see if there is a master twilight flat frame */
536 
537  if ((ps.master_twilight_flat =
538  vircam_frameset_subgroup_1(framelist,ps.labels,nlab,
539  VIRCAM_CAL_TWILIGHT_FLAT)) == NULL) {
540  cpl_msg_error(fctid,"No master twilight flat found");
541  vircam_jmp_tidy(0);
542  return(-1);
543  }
544 
545  /* Get the gain corrections */
546 
547  status = VIR_OK;
548  if (vircam_gaincor_calc(ps.master_twilight_flat,&i,&(ps.gaincors),
549  &status) != VIR_OK) {
550  cpl_msg_error(fctid,"Error calculating gain corrections");
551  vircam_jmp_tidy(0);
552  return(-1);
553  }
554 
555  /* Check to see if there is a readgain file */
556 
557  if ((ps.readgain_file =
558  vircam_frameset_subgroup_1(framelist,ps.labels,nlab,
559  VIRCAM_CAL_READGAINFILE)) == NULL) {
560  cpl_msg_error(fctid,"No master readnoise/gain file found");
561  vircam_jmp_tidy(0);
562  return(-1);
563  }
564 
565  /* Check to see if there is a master confidence map. If there isn't
566  then look for a bad pixel mask that can be converted into a
567  confidence map (in an emergency) */
568 
569  isconf = 1;
570  if ((ps.master_conf =
571  vircam_frameset_subgroup_1(framelist,ps.labels,nlab,
572  VIRCAM_CAL_CONF)) == NULL) {
573  isconf = 0;
574  if ((ps.master_conf =
575  vircam_frameset_subgroup_1(framelist,ps.labels,nlab,
576  VIRCAM_CAL_BPM)) == NULL) {
577  cpl_msg_error(fctid,"No master confidence map found");
578  vircam_jmp_tidy(0);
579  return(-1);
580  }
581  }
582  ps.mask = vircam_mask_define(framelist,ps.labels,nlab);
583 
584  /* Check to see if there is a channel table */
585 
586  if ((ps.chantab = vircam_frameset_subgroup_1(framelist,ps.labels,nlab,
587  VIRCAM_CAL_CHANTAB)) == NULL) {
588  cpl_msg_error(fctid,"No channel table found");
589  vircam_jmp_tidy(0);
590  return(-1);
591  }
592 
593  /* Check to see if there is a photometric table */
594 
595  if ((ps.phottab = vircam_frameset_subgroup_1(framelist,ps.labels,nlab,
596  VIRCAM_CAL_PHOTTAB)) == NULL) {
597  cpl_msg_error(fctid,"No photometric table found");
598  vircam_jmp_tidy(0);
599  return(-1);
600  }
601  if ((ps.tphottab = cpl_table_load(cpl_frame_get_filename(ps.phottab),1,0)) == NULL) {
602  cpl_msg_error(fctid,"Unable to load photometric table");
603  vircam_jmp_tidy(0);
604  return(-1);
605  }
606 
607  /* Is the 2mass index file specified? */
608 
609  if ((catindex = vircam_frameset_subgroup_1(framelist,ps.labels,nlab,
610  VIRCAM_CAL_2MASS)) == NULL) {
611  cpl_msg_info(fctid,"No 2MASS index found -- cannot continue");
612  vircam_jmp_tidy(0);
613  return(-1);
614  }
615 
616  /* Get catalogue parameters */
617 
618  if (vircam_catpars(catindex,&(ps.catpath),&(ps.catname)) == VIR_FATAL) {
619  vircam_jmp_tidy(0);
620  cpl_frame_delete(catindex);
621  return(-1);
622  }
623  cpl_frame_delete(catindex);
624 
625  /* Is there a second photometric catalogue index file specified? */
626 
627  if ((catindex = vircam_frameset_subgroup_1(framelist,ps.labels,nlab,
628  VIRCAM_CAL_REFCAT)) != NULL) {
629 
630  /* Get catalogue parameters */
631 
632  if (vircam_catpars(catindex,&(ps.catpath2),&(ps.catname2)) == VIR_FATAL) {
633  vircam_jmp_tidy(0);
634  cpl_frame_delete(catindex);
635  return(-1);
636  }
637  cpl_frame_delete(catindex);
638  }
639 
640  /* Get the number of DITs */
641 
642  pp = cpl_propertylist_load(cpl_frame_get_filename(cpl_frameset_get_frame(ps.science_frames,0)),0);
643  if (vircam_pfits_get_ndit(pp,&ndit) != VIR_OK) {
644  cpl_msg_error(fctid,"No value for NDIT available");
645  freepropertylist(pp);
646  vircam_jmp_tidy(0);
647  return(-1);
648  }
649  cpl_propertylist_delete(pp);
650 
651  /* Now, how many image extensions do we want to do? If the extension
652  number is zero, then we loop for all possible extensions. If it
653  isn't then we just do the extension specified */
654 
655  vircam_exten_range(vircam_jmp_config.extenum,
656  (const cpl_frame *)cpl_frameset_get_frame(ps.science_frames,0),
657  &jst,&jfn);
658  if (jst == -1 || jfn == -1) {
659  cpl_msg_error(fctid,"Unable to continue");
660  vircam_jmp_tidy(0);
661  return(-1);
662  }
663 
664  /* Now loop for all the extensions... */
665 
666  status = VIR_OK;
667  for (j = jst; j <= jfn; j++) {
668  isfirst = (j == jst);
669  gaincor_fac = (ps.gaincors)[j-1];
670 
671  /* Load up the calibration frames into vir_fits/vir_tfits structures
672  It is a fatal error if any one of them can't load properly */
673 
674  ps.fdark = vircam_fits_load(ps.master_dark,CPL_TYPE_FLOAT,j);
675  if (ps.fdark == NULL) {
676  cpl_msg_error(fctid,
677  "Error loading master dark %s[%" CPL_SIZE_FORMAT "]\n%s",
678  cpl_frame_get_filename(ps.master_dark),(cpl_size)j,
679  cpl_error_get_message());
680  vircam_jmp_tidy(0);
681  return(-1);
682  }
683  ps.fflat = vircam_fits_load(ps.master_twilight_flat,CPL_TYPE_FLOAT,j);
684  if (ps.fflat == NULL) {
685  cpl_msg_error(fctid,
686  "Error loading master flat %s[%" CPL_SIZE_FORMAT "]\n%s",
687  cpl_frame_get_filename(ps.master_twilight_flat),
688  (cpl_size)j,cpl_error_get_message());
689  vircam_jmp_tidy(0);
690  return(-1);
691  }
692  ps.fconf = vircam_fits_load(ps.master_conf,CPL_TYPE_INT,j);
693  if (ps.fconf == NULL) {
694  cpl_msg_error(fctid,
695  "Error loading master conf %s[%" CPL_SIZE_FORMAT "]\n%s",
696  cpl_frame_get_filename(ps.master_conf),(cpl_size)j,
697  cpl_error_get_message());
698  vircam_jmp_tidy(0);
699  return(-1);
700  }
701  if (! isconf)
703  if (vircam_mask_load(ps.mask,j,
704  (int)cpl_image_get_size_x(vircam_fits_get_image(ps.fconf)),
705  (int)cpl_image_get_size_y(vircam_fits_get_image(ps.fconf))) != VIR_OK) {
706  cpl_msg_error(fctid,
707  "Error loading mask from master conf %s[%" CPL_SIZE_FORMAT "]\n%s",
708  cpl_frame_get_filename(ps.master_conf),(cpl_size)j,
709  cpl_error_get_message());
710  vircam_jmp_tidy(0);
711  return(-1);
712  }
713  ps.fchantab = vircam_tfits_load(ps.chantab,j);
714  if (ps.fchantab == NULL) {
715  cpl_msg_error(fctid,
716  "Error loading channel table %s[%" CPL_SIZE_FORMAT "]\n%s",
717  cpl_frame_get_filename(ps.chantab),(cpl_size)j,
718  cpl_error_get_message());
719  vircam_jmp_tidy(0);
720  return(-1);
721  }
722 
723  /* Load up the vir_fits structures for the science images */
724 
725  ps.nscience = cpl_frameset_get_size(ps.science_frames);
726  ps.sci_fits = vircam_fits_load_list(ps.science_frames,CPL_TYPE_FLOAT,j);
727  if (ps.sci_fits == NULL) {
728  cpl_msg_error(fctid,
729  "Error loading science frames extension %" CPL_SIZE_FORMAT ": %s",
730  (cpl_size)j,cpl_error_get_message());
731  vircam_jmp_tidy(0);
732  return(-1);
733  }
734 
735  /* Set up something for the PAFs */
736 
737  ps.phupaf = vircam_paf_phu_items(vircam_fits_get_phu(ps.sci_fits[0]));
738 
739  /* Loop through and mark the frames where the header says the detector
740  wasn't live */
741 
742  for (i = 0; i < ps.nscience; i++) {
743  ff = ps.sci_fits[i];
745  if (! live)
746  vircam_fits_set_error(ff,VIR_FATAL);
748  vircam_fits_get_ehu(ff)) != VIR_OK) {
749  cpl_msg_error(fctid,"Unable to correct CRVAL in %s",
751  vircam_jmp_tidy(0);
752  return(-1);
753  }
754  }
755 
756  /* Get the readnoise and gain estimate for this extension */
757 
758  vircam_jmp_get_readnoise_gain(j,&readnoise,&gain);
759 
760  /* Loop for all the science frames and do the 2d corrections */
761 
762  cpl_msg_info(fctid,"Doing stage1 corrections on %s",
763  vircam_fits_get_extname(ps.sci_fits[0]));
764  for (i = 0; i < ps.nscience; i++) {
765  ff = ps.sci_fits[i];
766  cpl_propertylist_update_float(vircam_fits_get_ehu(ff),"READNOIS",
767  readnoise);
768  cpl_propertylist_set_comment(vircam_fits_get_ehu(ff),"READNOIS",
769  "[e-] Readnoise used in processing");
770  cpl_propertylist_update_float(vircam_fits_get_ehu(ff),"GAIN",gain);
771  cpl_propertylist_set_comment(vircam_fits_get_ehu(ff),"GAIN",
772  "[e-/adu] Gain used in processing");
773  if (vircam_fits_get_status(ff) == VIR_FATAL) {
774  cpl_msg_info(fctid,"Detector is flagged dead in %s",
776  continue;
777  }
778  status = VIR_OK;
779  (void)vircam_darkcor(ff,ps.fdark,1.0,&status);
780  (void)vircam_lincor(ff,ps.fchantab,1,ndit,&status);
781  (void)vircam_nditcor(ff,ndit,&status);
782  (void)vircam_flatcor(ff,ps.fflat,&status);
783  (void)vircam_gaincor(ff,gaincor_fac,&status);
784  vircam_fits_set_error(ff,status);
785  }
786 
787  /* Do a simple sky correction if requested */
788 
789  if (vircam_jmp_config.skycor) {
790  cpl_msg_info(fctid,"Doing sky correction");
792  } else {
793  for (i = 0; i < ps.nscience; i++) {
794  pp = vircam_fits_get_ehu(ps.sci_fits[i]);
795  cpl_propertylist_update_string(pp,"ESO DRS SKYCOR","none");
796  cpl_propertylist_set_comment(pp,"ESO DRS SKYCOR",
797  "Sky correction method");
798  }
799  }
800 
801  /* Do destripe now */
802 
803  if (vircam_jmp_config.destripe) {
804  cpl_msg_info(fctid,"Doing destripe");
805  for (i = 0; i < ps.nscience; i++) {
806  ff = ps.sci_fits[i];
807  (void)vircam_destripe(ff,ps.mask,&status);
808  vircam_fits_set_error(ff,status);
809  }
810  }
811 
812  /* Calculate the illumination correction */
813 
814  cpl_msg_info(fctid,"Doing illumination correction");
815  (void)strcpy(current_cat,ps.catname);
816  (void)strcpy(current_catpath,ps.catpath);
817  vircam_jmp_illum();
818 
819  /* Save the simple images */
820 
821  if (vircam_jmp_config.savesimple) {
822  cpl_msg_info(fctid,"Saving simple images");
823  if (vircam_jmp_save_simple(framelist,parlist) != 0) {
824  vircam_jmp_tidy(0);
825  return(-1);
826  }
827  }
828  vircam_mask_clear(ps.mask);
829 
830  /* Save the illumination correction table */
831 
832  cpl_msg_info(fctid,"Saving illumination correction table");
833  dummyqc = vircam_standard_process_dummyqc(3);
834  if (vircam_jmp_save_illum(framelist,parlist) != 0) {
835  vircam_jmp_tidy(0);
836  freepropertylist(dummyqc);
837  return(-1);
838  }
839  freepropertylist(dummyqc);
840 
841  /* Look at the first frame in the list and see if the number of
842  microsteps is greater than 1. If so, then we'll have to go
843  through interleaving. */
844 
845  retval = vircam_pfits_get_nusteps(vircam_fits_get_phu(ps.sci_fits[0]),
846  &nusteps);
847  if (retval != VIR_OK) {
848  cpl_msg_warning(fctid,"Unable to get nusteps from header.\nAssuming no microstepping");
849  nusteps = 1;
850  }
851 
852  /* If the number of microsteps is 1 then copy over the good frames
853  into a fits list for dithering. */
854 
855  ps.ndith = 0;
856  if (nusteps < 4) {
857  if (nusteps == 1)
858  cpl_msg_info(fctid,"No interleaving will be done");
859  else
860  cpl_msg_warning(fctid,
861  "Illegal number of microsteps: %" CPL_SIZE_FORMAT "\nNo interleaving will be done",
862  (cpl_size)nusteps);
863  ps.dith_input = cpl_malloc(ps.nscience*sizeof(vir_fits *));
864  ps.dithc_input = cpl_malloc(sizeof(vir_fits *));
865  for (i = 0; i < ps.nscience; i++) {
866  if (vircam_fits_get_status(ps.sci_fits[i]) == VIR_OK)
867  ps.dith_input[ps.ndith++] = ps.sci_fits[i];
868  }
869  ps.dithc_input[0] = ps.fconf;
870  ps.ndithc = 1;
871  interlv = 0;
872 
873  /* If the number of microsteps is more than 1, then we need
874  to do interleaving. The interleaving routine define
875  ps.dith_input. */
876 
877  } else {
878  cpl_msg_info(fctid,"Interleaving");
880  cpl_msg_info(fctid,"Saving superframe images");
881  if (vircam_jmp_save_super(framelist,parlist) != 0) {
882  vircam_jmp_tidy(0);
883  return(-1);
884  }
885  interlv = 1;
886  }
887 
888  /* Work out the jitter offsets and the stack the jitter frame */
889 
890  cpl_msg_info(fctid,"Working out jitter offsets");
892  cpl_msg_info(fctid,"Stacking jittered frame");
894 
895  /* Do a catalogue generation */
896 
897  cpl_msg_info(fctid,"Doing object extraction");
899 
900  /* Create a matched standards table */
901 
902  cpl_msg_info(fctid,"Matching objects with 2mass standards");
904 
905  /* Do a WCS fit for the dithered image */
906 
907  cpl_msg_info(fctid,"Fitting a WCS");
909 
910  /* Finally do the photometric zeropoint fit */
911 
912  cpl_msg_info(fctid,"Doing 2mass photometric zeropoint calculation");
914  if (vircam_fits_get_status(ps.stack_frame) == VIR_OK) {
915  ehu = vircam_fits_get_ehu(ps.stack_frame);
916  cpl_propertylist_update_float(ehu,"ESO QC ZPT_2MASS",
917  cpl_propertylist_get_float(ehu,"ESO QC MAGZPT"));
918  cpl_propertylist_set_comment(ehu,"ESO QC ZPT_2MASS",
919  cpl_propertylist_get_comment(ehu,"ESO QC MAGZPT"));
920  }
921 
922  /* Redo the photometric zeropoint with another catalogue if we
923  have one */
924 
925  if (ps.catpath2 != NULL) {
926  freetable(ps.matchstds);
927  (void)strcpy(current_cat,ps.catname2);
928  (void)strcpy(current_catpath,ps.catpath2);
929  cpl_msg_info(fctid,"Matching objects with %s standards",
930  ps.catname2);
932  cpl_msg_info(fctid,"Doing %s photometric zeropoint calculation",
933  ps.catname2);
935  if (vircam_fits_get_status(ps.stack_frame) == VIR_OK) {
936  ehu = vircam_fits_get_ehu(ps.stack_frame);
937  cpl_propertylist_update_float(ehu,"ESO QC ZPT_STDS",
938  cpl_propertylist_get_float(ehu,"ESO QC MAGZPT"));
939  cpl_propertylist_set_comment(ehu,"ESO QC ZPT_STDS",
940  cpl_propertylist_get_comment(ehu,"ESO QC MAGZPT"));
941  cpl_propertylist_update_string(ehu,"ESO QC ZPT_STDS_CAT",
942  ps.catname2);
943  cpl_propertylist_set_comment(ehu,"ESO QC ZPT_STDS_CAT",
944  "Catalogue used in zeropoint calc");
945  }
946  } else {
947  if (vircam_fits_get_status(ps.stack_frame) == VIR_OK) {
948  ehu = vircam_fits_get_ehu(ps.stack_frame);
949  cpl_propertylist_update_float(ehu,"ESO QC ZPT_STDS",0.0);
950  cpl_propertylist_set_comment(ehu,"ESO QC ZPT_STDS",
951  "No secondary standard calib");
952  cpl_propertylist_update_string(ehu,"ESO QC ZPT_STDS_CAT","");
953  cpl_propertylist_set_comment(ehu,"ESO QC ZPT_STDS_CAT",
954  "No secondary standard catalogue");
955  }
956  }
957 
958  /* Save the dithered images */
959 
960  cpl_msg_info(fctid,"Saving stacked images");
961  dummyqc = vircam_standard_process_dummyqc(1);
962  if (vircam_jmp_save_stack(framelist,parlist) != 0) {
963  vircam_jmp_tidy(0);
964  freepropertylist(dummyqc);
965  return(-1);
966  }
967  freepropertylist(dummyqc);
968  if (vircam_jmp_config.savecat) {
969  cpl_msg_info(fctid,"Saving stacked image catalogues");
970  dummyqc = vircam_standard_process_dummyqc(2);
971  if (vircam_jmp_save_catalogue(framelist,parlist) != 0) {
972  vircam_jmp_tidy(0);
973  freepropertylist(dummyqc);
974  return(-1);
975  }
976  freepropertylist(dummyqc);
977  }
978 
979  /* Clean up on aisle 12! */
980 
981  vircam_jmp_tidy(1);
982  }
983 
984  /* Final cleanup */
985 
986  vircam_jmp_tidy(0);
987  return(0);
988 }
989 
990 static cpl_propertylist *vircam_standard_process_dummyqc(int type) {
991  cpl_propertylist *p;
992 
993  /* Get an empty property list */
994 
995  p = cpl_propertylist_new();
996 
997  /* Now switch for the various products */
998 
999  switch (type) {
1000 
1001  /* Stack images */
1002 
1003  case 1:
1004  cpl_propertylist_update_double(p,"ESO QC WCS_DCRVAL1",0.0);
1005  cpl_propertylist_set_comment(p,"ESO QC WCS_DCRVAL1",
1006  "[deg] change in crval1");
1007  cpl_propertylist_update_double(p,"ESO QC WCS_DCRVAL2",0.0);
1008  cpl_propertylist_set_comment(p,"ESO QC WCS_DCRVAL2",
1009  "[deg] change in crval2");
1010  cpl_propertylist_update_double(p,"ESO QC WCS_DTHETA",0.0);
1011  cpl_propertylist_set_comment(p,"ESO QC WCS_DTHETA",
1012  "[deg] change in rotation");
1013  cpl_propertylist_update_double(p,"ESO QC WCS_SCALE",0.0);
1014  cpl_propertylist_set_comment(p,"ESO QC WCS_SCALE",
1015  "[arcsec] mean plate scale");
1016  cpl_propertylist_update_double(p,"ESO QC WCS_SHEAR",0.0);
1017  cpl_propertylist_set_comment(p,"ESO QC WCS_SHEAR",
1018  "[deg] abs(xrot) - abs(yrot)");
1019  cpl_propertylist_update_double(p,"ESO QC WCS_RMS",0.0);
1020  cpl_propertylist_set_comment(p,"ESO QC WCS_RMS",
1021  "[arcsec] Average error in WCS fit");
1022  cpl_propertylist_update_float(p,"ESO QC MAGZPT",0.0);
1023  cpl_propertylist_set_comment(p,"ESO QC MAGZPT",
1024  "[mag] photometric zeropoint");
1025  cpl_propertylist_update_float(p,"ESO QC MAGZERR",0.0);
1026  cpl_propertylist_set_comment(p,"ESO QC MAGZERR",
1027  "[mag] photometric zeropoint error");
1028  cpl_propertylist_update_int(p,"ESO QC MAGNZPT",0);
1029  cpl_propertylist_set_comment(p,"ESO QC MAGNZPT",
1030  "number of stars in magzpt calc");
1031  cpl_propertylist_update_int(p,"ESO QC MAGNCUT",0);
1032  cpl_propertylist_set_comment(p,"ESO QC MAGNCUT",
1033  "number of cut from magzpt calc");
1034  cpl_propertylist_update_float(p,"ESO QC LIMITING_MAG",0.0);
1035  cpl_propertylist_set_comment(p,"ESO QC LIMITING_MAG",
1036  "[mag] 5 sigma limiting mag");
1037  cpl_propertylist_update_float(p,"ESO QC ZPT_2MASS",0.0);
1038  cpl_propertylist_set_comment(p,"ESO QC ZPT_2MASS",
1039  "[mag] photometric zeropoint");
1040  cpl_propertylist_update_float(p,"ESO QC ZPT_STDS",0.0);
1041  cpl_propertylist_set_comment(p,"ESO QC ZPT_STDS",
1042  "[mag] photometric zeropoint");
1043  cpl_propertylist_update_string(p,"ESO QC ZPT_STDS_CAT","");
1044  cpl_propertylist_set_comment(p,"ESO QC ZPT_STDS_CAT",
1045  "Catalogue used in zeropoint calc");
1046  break;
1047 
1048  /* Catalogues */
1049 
1050  case 2:
1051  cpl_propertylist_update_float(p,"ESO QC SATURATION",0.0);
1052  cpl_propertylist_set_comment(p,"ESO QC SATURATION",
1053  "[adu] Saturation level");
1054  cpl_propertylist_update_float(p,"ESO QC MEAN_SKY",0.0);
1055  cpl_propertylist_set_comment(p,"ESO QC MEAN_SKY",
1056  "[adu] Median sky brightness");
1057  cpl_propertylist_update_float(p,"ESO QC SKY_NOISE",0.0);
1058  cpl_propertylist_set_comment(p,"ESO QC SKY_NOISE",
1059  "[adu] Pixel noise at sky level");
1060  cpl_propertylist_update_float(p,"ESO QC IMAGE_SIZE",0.0);
1061  cpl_propertylist_set_comment(p,"ESO QC IMAGE_SIZE",
1062  "[pixels] Average FWHM of stellar objects");
1063  cpl_propertylist_update_float(p,"ESO QC ELLIPTICITY",0.0);
1064  cpl_propertylist_set_comment(p,"ESO QC ELLIPTICITY",
1065  "Average stellar ellipticity (1-b/a)");
1066  cpl_propertylist_update_float(p,"ESO QC APERTURE_CORR",0.0);
1067  cpl_propertylist_set_comment(p,"ESO QC APERTURE_CORR",
1068  "Stellar ap-corr 1x core flux");
1069  cpl_propertylist_update_int(p,"ESO QC NOISE_OBJ",0);
1070  cpl_propertylist_set_comment(p,"ESO QC NOISE_OBJ",
1071  "Number of noise objects");
1072  break;
1073 
1074  /* Illumination tables */
1075 
1076  case 3:
1077  cpl_propertylist_update_float(p,"ESO QC ILLUMCOR_RMS",0.0);
1078  cpl_propertylist_set_comment(p,"ESO QC ILLUMCOR_RMS",
1079  "RMS of illumination correction map");
1080  break;
1081  default:
1082  break;
1083  }
1084 
1085  /* Get out of here */
1086 
1087  return(p);
1088 }
1089 
1092 /*
1093 
1094 $Log: not supported by cvs2svn $
1095 Revision 1.42 2012/01/15 17:40:09 jim
1096 Minor modifications to take into accout the changes in cpl API for v6
1097 
1098 Revision 1.41 2011/05/09 09:58:10 jim
1099 Cosmetic changes to stop compiler warnings
1100 
1101 Revision 1.40 2010/12/09 13:20:49 jim
1102 Modified some comments
1103 
1104 Revision 1.39 2010/09/13 11:43:13 jim
1105 Added flag DRS SKYCOR if background correction isn't being done
1106 
1107 Revision 1.38 2010/09/09 12:16:00 jim
1108 Added new QC parameter MAGNCUT
1109 
1110 Revision 1.37 2010/06/03 11:31:06 jim
1111 Modified so that deblending is done by default
1112 
1113 Revision 1.36 2010/03/21 06:47:42 jim
1114 Added code to trap for problems in saving products
1115 
1116 Revision 1.35 2010/03/12 10:44:07 lbilbao
1117 Added missing header inclusion.
1118 
1119 Revision 1.34 2010/02/08 16:35:26 jim
1120 Moved the definition of propertylist ps.phupaf to the recipes so that
1121 this doesn't fail when we don't save the simple images
1122 
1123 Revision 1.33 2010/01/31 18:56:35 jim
1124 Now optionally saves the simple images. Also included missing ndit correction
1125 
1126 Revision 1.32 2009/12/11 06:52:56 jim
1127 Minor mods to documentation
1128 
1129 Revision 1.31 2009/09/09 09:50:21 jim
1130 Modified to try and get headers right
1131 
1132 Revision 1.30 2009/07/03 12:30:04 jim
1133 Default value of rcore is now 3
1134 
1135 Revision 1.29 2009/02/04 09:23:29 jim
1136 Moved destriping to after sky subtraction
1137 
1138 Revision 1.28 2008/12/08 06:38:41 jim
1139 Added trap for crval==0 error. Also added a trap for illegal 2x1 microstep
1140 sequences
1141 
1142 Revision 1.27 2008/11/27 09:14:47 jim
1143 Fixed PRO CATG keyword values for interleaved confidence maps in docs
1144 
1145 Revision 1.26 2008/11/25 18:55:59 jim
1146 took out extra sky stuff
1147 
1148 Revision 1.25 2008/11/25 12:03:03 jim
1149 Now allows for masked object sky estimation
1150 
1151 Revision 1.24 2008/11/25 06:18:30 jim
1152 Changed a reference to VIRCAM_CAL_2MASS to VIRCAM_CAL_REFCAT.
1153 
1154 Revision 1.23 2008/10/01 04:59:13 jim
1155 Added call to vircam_frameset_fexists to check input frameset
1156 
1157 Revision 1.22 2008/05/06 12:15:21 jim
1158 Changed to use new version of vircam_catpars
1159 
1160 Revision 1.21 2007/11/26 09:59:06 jim
1161 Recipe now takes ndit into account when doing linearity correction
1162 
1163 Revision 1.20 2007/10/25 18:39:22 jim
1164 Altered to remove some lint messages
1165 
1166 Revision 1.19 2007/10/19 06:55:06 jim
1167 Modifications made to use new method for directing the recipes to the
1168 standard catalogues using the sof
1169 
1170 Revision 1.18 2007/07/09 13:21:56 jim
1171 Modified to use new version of vircam_exten_range
1172 
1173 Revision 1.17 2007/06/13 08:11:27 jim
1174 Modified docs to reflect changes in DFS tags
1175 
1176 Revision 1.16 2007/05/08 21:31:16 jim
1177 fixed typo
1178 
1179 Revision 1.15 2007/05/08 10:42:44 jim
1180 Added gain correction
1181 
1182 Revision 1.14 2007/05/02 12:53:11 jim
1183 typo fixes in docs
1184 
1185 Revision 1.13 2007/04/26 13:09:40 jim
1186 fixed typos
1187 
1188 Revision 1.12 2007/04/04 16:05:59 jim
1189 Modified to make paf information a bit more correct
1190 
1191 Revision 1.11 2007/04/04 10:36:18 jim
1192 Modified to use new dfs tags
1193 
1194 Revision 1.10 2007/03/29 12:19:39 jim
1195 Little changes to improve documentation
1196 
1197 Revision 1.9 2007/03/14 14:49:13 jim
1198 Fixed problem with missing paf files in jmp recipes if detlive = F. Also
1199 fixed problem where extra dummy products were being created
1200 
1201 Revision 1.8 2007/03/06 12:00:48 jim
1202 Fixed stupid typo in header
1203 
1204 Revision 1.7 2007/03/02 12:37:51 jim
1205 Fixed small memory leak
1206 
1207 Revision 1.6 2007/03/01 12:41:49 jim
1208 Modified slightly after code checking
1209 
1210 Revision 1.5 2007/02/07 10:12:40 jim
1211 Removed calls to vircam_ndit_correct as this is now no longer necessary
1212 
1213 Revision 1.4 2006/12/19 13:32:03 jim
1214 Images that are flagged as dead detectors now generate an INFO rather than
1215 an ERROR message
1216 
1217 Revision 1.3 2006/11/29 12:28:45 jim
1218 Modified so that the correct recipe names would appear in the headers of
1219 data products
1220 
1221 Revision 1.2 2006/11/28 20:57:43 jim
1222 Added illumination correction section
1223 
1224 Revision 1.1 2006/11/27 12:15:43 jim
1225 Initial entry
1226 
1227 
1228 */