vircam_jmp_utils.c

00001 /* $Id: vircam_jmp_utils.c,v 1.56 2010/09/13 11:50:18 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/09/13 11:50:18 $
00024  * $Revision: 1.56 $
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 #include <math.h>
00036 #include <string.h>
00037 
00038 #include "vircam_utils.h"
00039 #include "vircam_pfits.h"
00040 #include "vircam_dfs.h"
00041 #include "vircam_mods.h"
00042 #include "vircam_stats.h"
00043 #include "vircam_fits.h"
00044 #include "vircam_tfits.h"
00045 #include "vircam_wcsutils.h"
00046 #include "vircam_paf.h"
00047 #include "vircam_sky.h"
00048 
00049 #include "vircam_jmp_utils.h"
00050 
00051 static char *vircam_jmp_outfile(const char *bname, int ind, int isfits);
00052 
00066 /*---------------------------------------------------------------------------*/
00085 /*---------------------------------------------------------------------------*/
00086 
00087 extern int vircam_jmp_save_simple(cpl_frameset *framelist,
00088                                   cpl_parameterlist *parlist) {
00089     cpl_propertylist *plist;
00090     int i,isdummy;
00091     cpl_frame *product_frame,*template;
00092     char *fname;
00093     const char *base[] = {"","simple_jmp","simple_std","simple_mes"};
00094     const char *fctid = "vircam_jmp_save_simple";
00095 
00096     /* Initialise the product frameset */
00097 
00098     if (ps.product_frames_simple == NULL)
00099         ps.product_frames_simple = cpl_malloc(ps.nscience*sizeof(cpl_frame *));
00100 
00101     /* Loop for each of the frames in the ustep sequence */
00102 
00103     for (i = 0; i < ps.nscience; i++) {
00104         fname = vircam_jmp_outfile(base[recflag],i,1);
00105         isdummy = (vircam_fits_get_status(ps.sci_fits[i]) != VIR_OK);
00106         template = cpl_frameset_get_frame(ps.science_frames,i);
00107 
00108         /* If we need to make a PHU then do that now based on the first frame
00109            in the input frame list */
00110 
00111         if (isfirst) {
00112 
00113             /* Create a new product frame object and define some tags */
00114 
00115             product_frame = cpl_frame_new();
00116             cpl_frame_set_filename(product_frame,fname);
00117             switch (recflag) {
00118             case RECSCI:
00119                 cpl_frame_set_tag(product_frame,VIRCAM_PRO_SIMPLE_SCI);
00120                 break;
00121             case RECSTD:
00122                 cpl_frame_set_tag(product_frame,VIRCAM_PRO_SIMPLE_STD);
00123                 break;
00124             default:
00125                 cpl_frame_set_tag(product_frame,VIRCAM_PRO_SIMPLE_SCI);
00126                 break;
00127             }
00128             cpl_frame_set_type(product_frame,CPL_FRAME_TYPE_IMAGE);
00129             cpl_frame_set_group(product_frame,CPL_FRAME_GROUP_PRODUCT);
00130             cpl_frame_set_level(product_frame,CPL_FRAME_LEVEL_FINAL);
00131 
00132             /* Set up the PHU header */
00133 
00134             plist = vircam_fits_get_phu(ps.sci_fits[i]);
00135             vircam_dfs_set_product_primary_header(plist,product_frame,
00136                                                   framelist,parlist,
00137                                                   vircam_recipename,
00138                                                   "PRO-1.15",template,1);
00139 
00140             /* 'Save' the PHU image */                   
00141 
00142             if (cpl_image_save(NULL,fname,CPL_BPP_8_UNSIGNED,plist,
00143                                CPL_IO_DEFAULT) != CPL_ERROR_NONE) {
00144                 cpl_msg_error(fctid,"Cannot save product PHU");
00145                 cpl_frame_delete(product_frame);
00146                 freespace(fname);
00147                 return(-1);
00148             }
00149             cpl_frameset_insert(framelist,product_frame);
00150             ps.product_frames_simple[i] = product_frame;
00151         }
00152 
00153         /* Get the extension property list */
00154 
00155         plist = vircam_fits_get_ehu(ps.sci_fits[i]);
00156         if (isdummy)
00157             vircam_dummy_property(plist);
00158 
00159         /* Fiddle with the header now */
00160 
00161         product_frame = ps.product_frames_simple[i];
00162         vircam_dfs_set_product_exten_header(plist,product_frame,framelist,
00163                                             parlist,vircam_recipename,
00164                                             "PRO-1.15",template);
00165         if (cpl_image_save(vircam_fits_get_image(ps.sci_fits[i]),fname,
00166                            CPL_BPP_IEEE_FLOAT,plist,
00167                            CPL_IO_EXTEND) != CPL_ERROR_NONE) {
00168             cpl_msg_error(fctid,"Cannot save product image extension");
00169             freespace(fname);
00170             return(-1);
00171         }
00172         
00173         /* Clear the allocated workspace */
00174 
00175         freespace(fname);
00176     }
00177 
00178     /* Get out of here */
00179 
00180     return(0);
00181 }
00182 
00183 /*---------------------------------------------------------------------------*/
00202 /*---------------------------------------------------------------------------*/
00203 
00204 extern int vircam_jmp_save_simple_offsets(cpl_frameset *framelist,
00205                                           cpl_parameterlist *parlist) {
00206     cpl_propertylist *plist;
00207     int i,isdummy;
00208     cpl_frame *product_frame,*template;
00209     char *fname;
00210     const char *base = "simple_off";
00211     const char *fctid = "vircam_jmp_save_simple_offsets";
00212 
00213     /* Initialise the product frameset */
00214 
00215     if (ps.product_frames_simple_off == NULL)
00216         ps.product_frames_simple_off = 
00217             cpl_malloc(ps.noffsets*sizeof(cpl_frame *));
00218 
00219     /* Loop for each of the frames in the ustep sequence */
00220 
00221     for (i = 0; i < ps.noffsets; i++) {
00222         fname = vircam_jmp_outfile(base,i,1);
00223         isdummy = (vircam_fits_get_status(ps.offsky_fits[i]) != VIR_OK);
00224         template = cpl_frameset_get_frame(ps.offset_skies,i);
00225 
00226         /* If we need to make a PHU then do that now based on the first frame
00227            in the input frame list */
00228 
00229         if (isfirst) {
00230 
00231             /* Create a new product frame object and define some tags */
00232 
00233             product_frame = cpl_frame_new();
00234             cpl_frame_set_filename(product_frame,fname);
00235             cpl_frame_set_tag(product_frame,VIRCAM_PRO_SIMPLE_SKY);
00236             cpl_frame_set_type(product_frame,CPL_FRAME_TYPE_IMAGE);
00237             cpl_frame_set_group(product_frame,CPL_FRAME_GROUP_PRODUCT);
00238             cpl_frame_set_level(product_frame,CPL_FRAME_LEVEL_FINAL);
00239 
00240             /* Set up the PHU header */
00241 
00242             plist = vircam_fits_get_phu(ps.offsky_fits[i]);
00243             vircam_dfs_set_product_primary_header(plist,product_frame,
00244                                                   framelist,parlist,
00245                                                   vircam_recipename,
00246                                                   "PRO-1.15",template,1);
00247 
00248             /* 'Save' the PHU image */                   
00249 
00250             if (cpl_image_save(NULL,fname,CPL_BPP_8_UNSIGNED,plist,
00251                                CPL_IO_DEFAULT) != CPL_ERROR_NONE) {
00252                 cpl_msg_error(fctid,"Cannot save product PHU");
00253                 cpl_frame_delete(product_frame);
00254                 freespace(fname);
00255                 return(-1);
00256             }
00257             cpl_frameset_insert(framelist,product_frame);
00258             ps.product_frames_simple_off[i] = product_frame;
00259         }
00260 
00261         /* Get the extension property list */
00262 
00263         plist = vircam_fits_get_ehu(ps.offsky_fits[i]);
00264         if (isdummy)
00265             vircam_dummy_property(plist);
00266 
00267         /* Fiddle with the header now */
00268 
00269         product_frame = ps.product_frames_simple_off[i];
00270         vircam_dfs_set_product_exten_header(plist,product_frame,framelist,
00271                                             parlist,vircam_recipename,
00272                                             "PRO-1.15",template);
00273         if (cpl_image_save(vircam_fits_get_image(ps.offsky_fits[i]),fname,
00274                            CPL_BPP_IEEE_FLOAT,plist,
00275                            CPL_IO_EXTEND) != CPL_ERROR_NONE) {
00276             cpl_msg_error(fctid,"Cannot save product image extension");
00277             freespace(fname);
00278             return(-1);
00279         }
00280         
00281         /* Clear the allocated workspace */
00282 
00283         freespace(fname);
00284     }
00285 
00286     /* Get out of here */
00287 
00288     return(0);
00289 }
00290 
00291 /*---------------------------------------------------------------------------*/
00310 /*---------------------------------------------------------------------------*/
00311 
00312 extern int vircam_jmp_save_offsky(cpl_frameset *framelist,
00313                                   cpl_parameterlist *parlist) {
00314     cpl_propertylist *p;
00315     int isdummy;
00316     vir_fits *ff;
00317     cpl_image *fim;
00318     cpl_frame *product_frame,*template;
00319     char *fname;
00320     const char *base = "offsky";
00321     const char *fctid = "vircam_jmp_save_offsky";
00322 
00323     /* Work out which frame to base the output on. If this particular
00324        sequence failed for whatever reason, there will be a dummy sky frame. */
00325 
00326     ff = ps.outsky;
00327     fim = vircam_fits_get_image(ff);
00328     isdummy = (vircam_fits_get_status(ff) != VIR_OK);
00329     template = cpl_frameset_get_frame(ps.offset_skies,0);
00330 
00331     /* Create some names */ 
00332 
00333     fname = vircam_jmp_outfile(base,0,1);
00334 
00335     /* If we need to make a PHU then do that now based on the first frame
00336        in the input frame list */
00337 
00338     if (isfirst) {
00339 
00340         /* Create a new product frame object and define some tags */
00341 
00342         product_frame = cpl_frame_new();
00343         cpl_frame_set_filename(product_frame,fname);
00344         cpl_frame_set_tag(product_frame,VIRCAM_PRO_OFFSET_SKY);
00345         cpl_frame_set_type(product_frame,CPL_FRAME_TYPE_IMAGE);
00346         cpl_frame_set_group(product_frame,CPL_FRAME_GROUP_PRODUCT);
00347         cpl_frame_set_level(product_frame,CPL_FRAME_LEVEL_FINAL);
00348         ps.product_frame_sky = product_frame;
00349 
00350         /* Set up the PHU header. */
00351 
00352         p = vircam_fits_get_phu(ff);
00353         vircam_dfs_set_product_primary_header(p,product_frame,framelist,
00354                                               parlist,vircam_recipename,
00355                                               "PRO-1.15",template,0);
00356 
00357         /* 'Save' the PHU image */                       
00358 
00359         if (cpl_image_save(NULL,fname,CPL_BPP_8_UNSIGNED,p,
00360                            CPL_IO_DEFAULT) != CPL_ERROR_NONE) {
00361             cpl_msg_error(fctid,"Cannot save product PHU");
00362             cpl_frame_delete(product_frame);
00363             freespace(fname);
00364             return(-1);
00365         }
00366         cpl_frameset_insert(framelist,product_frame);
00367     }
00368 
00369     /* Get the extension property list */
00370 
00371     p = cpl_propertylist_duplicate(vircam_fits_get_ehu(ff));
00372     if (isdummy)
00373         vircam_dummy_property(p);
00374 
00375     /* Fiddle with the header now */
00376 
00377     product_frame = ps.product_frame_sky;
00378     vircam_dfs_set_product_exten_header(p,product_frame,framelist,parlist,
00379                                         vircam_recipename,"PRO-1.15",template);
00380     if (cpl_image_save(fim,fname,CPL_BPP_IEEE_FLOAT,p,CPL_IO_EXTEND) != 
00381         CPL_ERROR_NONE) {
00382         cpl_msg_error(fctid,"Cannot save product image extension");
00383         freespace(fname);
00384         return(-1);
00385     }
00386 
00387     /* Quick tidy */
00388 
00389     cpl_propertylist_delete(p);
00390     freespace(fname);
00391 
00392     /* Get out of here */
00393 
00394     return(0);
00395 }
00396 
00397 
00398 
00399 /*---------------------------------------------------------------------------*/
00418 /*---------------------------------------------------------------------------*/
00419 
00420 extern int vircam_jmp_save_super(cpl_frameset *framelist,
00421                                  cpl_parameterlist *parlist) {
00422     cpl_propertylist *plist,*p;
00423     int i,isdummy,isdummyc,j,jref;
00424     cpl_frame *product_frame,*template;
00425     char *fname,*fnamec;
00426     const char *base[] = {"","super_jmp","super_std","super_mes"};
00427     const char *basec[] = {"","superc_jmp","superc_std","superc_mes"};
00428     vir_fits *ff,*ffc;
00429     cpl_image *fim,*fimc;
00430     const char *fctid = "vircam_jmp_save_super";
00431 
00432     /* Initialise the product frameset */
00433 
00434     if (ps.product_frames_super == NULL)
00435         ps.product_frames_super = cpl_malloc(ps.nustep_sets*sizeof(cpl_frame *));
00436     if (ps.product_frames_superc == NULL)
00437         ps.product_frames_superc = cpl_malloc(ps.nustep_sets*sizeof(cpl_frame *));
00438 
00439     /* Loop for each of the ustep sequences */
00440 
00441     for (i = 0; i < ps.nustep_sets; i++) {
00442 
00443         /* Work out which frame to base the output on. If this particular
00444            microstep sequence failed for whatever reason, there will be
00445            a dummy super frame. */
00446         
00447         ff = ps.ustep_sets[i].super;
00448         fim = vircam_fits_get_image(ff);
00449         ffc = ps.ustep_sets[i].superc;
00450         fimc = vircam_fits_get_image(ffc);
00451         isdummy = (vircam_fits_get_status(ff) != VIR_OK);
00452         isdummyc = (vircam_fits_get_status(ffc) != VIR_OK);
00453 
00454         /* Create some names */
00455 
00456         fname = vircam_jmp_outfile(base[recflag],i,1);
00457         fnamec = vircam_jmp_outfile(basec[recflag],i,1);
00458 
00459         /* Get a reference to the first frame in this set */
00460 
00461         jref = -1;
00462         for (j = 0; j < ps.ustep_sets[i].nframes; j++) {
00463             if (ps.sci_fits[j] == ps.ustep_sets[i].f[j]) {
00464                 jref = j;
00465                 break;
00466             }
00467         }
00468         if (jref != -1)
00469             template = cpl_frameset_get_frame(ps.science_frames,jref);
00470         else
00471             template = NULL;
00472 
00473         /* If we need to make a PHU then do that now based on the first frame
00474            in the input frame list */
00475 
00476         if (isfirst) {
00477 
00478             /* Create a new product frame object and define some tags */
00479 
00480             product_frame = cpl_frame_new();
00481             cpl_frame_set_filename(product_frame,fname);
00482             switch (recflag) {
00483             case RECSCI:
00484                 cpl_frame_set_tag(product_frame,VIRCAM_PRO_INTER_SCI);
00485                 break;
00486             case RECSTD:
00487                 cpl_frame_set_tag(product_frame,VIRCAM_PRO_INTER_STD);
00488                 break;
00489             default:
00490                 cpl_frame_set_tag(product_frame,VIRCAM_PRO_INTER_SCI);
00491                 break;
00492             }
00493             cpl_frame_set_type(product_frame,CPL_FRAME_TYPE_IMAGE);
00494             cpl_frame_set_group(product_frame,CPL_FRAME_GROUP_PRODUCT);
00495             cpl_frame_set_level(product_frame,CPL_FRAME_LEVEL_FINAL);
00496 
00497             /* Set up the PHU header. */
00498 
00499             plist = vircam_fits_get_phu(ff);
00500             vircam_dfs_set_product_primary_header(plist,product_frame,
00501                                                   framelist,parlist,
00502                                                   vircam_recipename,
00503                                                   "PRO-1.15",template,1);
00504 
00505             /* 'Save' the PHU image */                   
00506 
00507             if (cpl_image_save(NULL,fname,CPL_BPP_8_UNSIGNED,plist,
00508                                CPL_IO_DEFAULT) != CPL_ERROR_NONE) {
00509                 cpl_msg_error(fctid,"Cannot save product PHU");
00510                 cpl_frame_delete(product_frame);
00511                 freespace(fname);
00512                 freespace(fnamec);
00513                 return(-1);
00514             }
00515             cpl_frameset_insert(framelist,product_frame);
00516             ps.product_frames_super[i] = product_frame;
00517 
00518             /* Now do exactly the same thing for the confidence map */
00519 
00520             product_frame = cpl_frame_new();
00521             cpl_frame_set_filename(product_frame,fnamec);
00522             switch (recflag) {
00523             case RECSCI:
00524                 cpl_frame_set_tag(product_frame,VIRCAM_PRO_CONF_INTER_SCI);
00525                 break;
00526             case RECSTD:
00527                 cpl_frame_set_tag(product_frame,VIRCAM_PRO_CONF_INTER_STD);
00528                 break;
00529             default:
00530                 cpl_frame_set_tag(product_frame,VIRCAM_PRO_CONF_INTER_SCI);
00531                 break;
00532             }
00533             cpl_frame_set_type(product_frame,CPL_FRAME_TYPE_IMAGE);
00534             cpl_frame_set_group(product_frame,CPL_FRAME_GROUP_PRODUCT);
00535             cpl_frame_set_level(product_frame,CPL_FRAME_LEVEL_FINAL);
00536 
00537             /* Set up the PHU header. */
00538 
00539             plist = vircam_fits_get_phu(ffc);
00540             vircam_dfs_set_product_primary_header(plist,product_frame,
00541                                                   framelist,parlist,
00542                                                   vircam_recipename,
00543                                                   "PRO-1.15",template,1);
00544 
00545             /* 'Save' the PHU image */                   
00546 
00547             if (cpl_image_save(NULL,fnamec,CPL_BPP_8_UNSIGNED,plist,
00548                                CPL_IO_DEFAULT) != CPL_ERROR_NONE) {
00549                 cpl_msg_error(fctid,"Cannot save product PHU");
00550                 cpl_frame_delete(product_frame);
00551                 freespace(fname);
00552                 freespace(fnamec);
00553                 return(-1);
00554             }
00555             cpl_frameset_insert(framelist,product_frame);
00556             ps.product_frames_superc[i] = product_frame;
00557         }
00558 
00559 
00560         /* Get the extension property list */
00561 
00562         p = cpl_propertylist_duplicate(vircam_fits_get_ehu(ff));
00563         if (isdummy)
00564             vircam_dummy_property(p);
00565 
00566         /* Fiddle with the header now */
00567 
00568         product_frame = ps.product_frames_super[i];
00569         vircam_dfs_set_product_exten_header(p,product_frame,framelist,parlist,
00570                                             vircam_recipename,"PRO-1.15",
00571                                             template);
00572         if (cpl_image_save(fim,fname,CPL_BPP_IEEE_FLOAT,p,CPL_IO_EXTEND) != 
00573             CPL_ERROR_NONE) {
00574             cpl_msg_error(fctid,"Cannot save product image extension");
00575             freespace(fname);
00576             freespace(fnamec);
00577             return(-1);
00578         }
00579         cpl_propertylist_delete(p);
00580         
00581         /* And now for the confidence map */
00582 
00583         p = cpl_propertylist_duplicate(vircam_fits_get_ehu(ffc));
00584         if (isdummyc)
00585             vircam_dummy_property(p);
00586 
00587         /* Fiddle with the header now */
00588 
00589         product_frame = ps.product_frames_superc[i];
00590         vircam_dfs_set_product_exten_header(p,product_frame,framelist,parlist,
00591                                             vircam_recipename,"PRO-1.15",
00592                                             template);
00593         if (cpl_image_save(fimc,fnamec,CPL_BPP_16_SIGNED,p,CPL_IO_EXTEND) != 
00594             CPL_ERROR_NONE) {
00595             cpl_msg_error(fctid,"Cannot save confidence map image extension");
00596             freespace(fname);
00597             freespace(fnamec);
00598             return(-1);
00599         }
00600         cpl_propertylist_delete(p);
00601         
00602         /* Clear the allocated workspace */
00603 
00604         freespace(fname);
00605         freespace(fnamec);
00606     }
00607 
00608     /* Get out of here */
00609 
00610     return(0);
00611 }
00612 
00613 /*---------------------------------------------------------------------------*/
00632 /*---------------------------------------------------------------------------*/
00633 
00634 extern int vircam_jmp_save_stack(cpl_frameset *framelist,
00635                                  cpl_parameterlist *parlist) {
00636     cpl_propertylist *plist,*p,*pafprop;
00637     int isdummy,isdummyc;
00638     vir_fits *ff,*ffc;
00639     cpl_image *fim,*fimc;
00640     cpl_frame *product_frame;
00641     char *fname,*fnamec,*fnamepaf;
00642     const char *base[] = {"","stack_jmp","stack_std","stack_mes"};
00643     const char *basec[] = {"","stackc_jmp","stackc_std","stackc_mes"};
00644     const char *fctid = "vircam_jmp_save_stack";
00645 
00646     /* Work out which frame to base the output on. If this particular
00647        jitter sequence failed for whatever reason, there will be
00648        a dummy stack frame. */
00649 
00650     ff = ps.stack_frame;
00651     fim = vircam_fits_get_image(ff);
00652     ffc = ps.stackc_frame;
00653     fimc = vircam_fits_get_image(ffc);
00654     isdummy = (vircam_fits_get_status(ff) != VIR_OK);
00655     isdummyc = (vircam_fits_get_status(ffc) != VIR_OK);
00656 
00657     /* Create some names */
00658 
00659     fname = vircam_jmp_outfile(base[recflag],0,1);
00660     fnamec = vircam_jmp_outfile(basec[recflag],0,1);
00661     fnamepaf = vircam_jmp_outfile(base[recflag],0,0);
00662 
00663     /* If we need to make a PHU then do that now based on the first frame
00664        in the input frame list */
00665 
00666     if (isfirst) {
00667 
00668         /* Create a new product frame object and define some tags */
00669 
00670         product_frame = cpl_frame_new();
00671         cpl_frame_set_filename(product_frame,fname);
00672         switch (recflag) {
00673         case RECSCI:
00674             cpl_frame_set_tag(product_frame,VIRCAM_PRO_JITTERED_SCI);
00675             break;
00676         case RECSTD:
00677             cpl_frame_set_tag(product_frame,VIRCAM_PRO_JITTERED_STD);
00678             break;
00679         default:
00680             cpl_frame_set_tag(product_frame,VIRCAM_PRO_JITTERED_SCI);
00681             break;
00682         }
00683         cpl_frame_set_type(product_frame,CPL_FRAME_TYPE_IMAGE);
00684         cpl_frame_set_group(product_frame,CPL_FRAME_GROUP_PRODUCT);
00685         cpl_frame_set_level(product_frame,CPL_FRAME_LEVEL_FINAL);
00686         ps.product_frame_stack = product_frame;
00687 
00688         /* Set up the PHU header. */
00689 
00690         plist = vircam_fits_get_phu(ff);
00691         vircam_dfs_set_product_primary_header(plist,product_frame,framelist,
00692                                               parlist,vircam_recipename,
00693                                               "PRO-1.15",NULL,0);
00694 
00695         /* 'Save' the PHU image */                       
00696 
00697         if (cpl_image_save(NULL,fname,CPL_BPP_8_UNSIGNED,plist,
00698                            CPL_IO_DEFAULT) != CPL_ERROR_NONE) {
00699             cpl_msg_error(fctid,"Cannot save product PHU");
00700             cpl_frame_delete(product_frame);
00701             freespace(fname);
00702             freespace(fnamec);
00703             return(-1);
00704         }
00705         cpl_frameset_insert(framelist,product_frame);
00706 
00707         /* Now do exactly the same thing for the confidence map */
00708 
00709         product_frame = cpl_frame_new();
00710         cpl_frame_set_filename(product_frame,fnamec);
00711         switch (recflag) {
00712         case RECSCI:
00713             cpl_frame_set_tag(product_frame,VIRCAM_PRO_CONF_SCI);
00714             break;
00715         case RECSTD:
00716             cpl_frame_set_tag(product_frame,VIRCAM_PRO_CONF_STD);
00717             break;
00718         default:
00719             cpl_frame_set_tag(product_frame,VIRCAM_PRO_CONF_SCI);
00720             break;
00721         }
00722         cpl_frame_set_type(product_frame,CPL_FRAME_TYPE_IMAGE);
00723         cpl_frame_set_group(product_frame,CPL_FRAME_GROUP_PRODUCT);
00724         cpl_frame_set_level(product_frame,CPL_FRAME_LEVEL_FINAL);
00725         ps.product_frame_stackc = product_frame;
00726 
00727         /* Set up the PHU header. */
00728 
00729         plist = vircam_fits_get_phu(ffc);
00730         vircam_dfs_set_product_primary_header(plist,product_frame,framelist,
00731                                               parlist,vircam_recipename,
00732                                               "PRO-1.15",NULL,0);
00733 
00734         /* 'Save' the PHU image */                       
00735 
00736         if (cpl_image_save(NULL,fnamec,CPL_BPP_8_UNSIGNED,plist,
00737                            CPL_IO_DEFAULT) != CPL_ERROR_NONE) {
00738             cpl_msg_error(fctid,"Cannot save product PHU");
00739             cpl_frame_delete(product_frame);
00740             freespace(fname);
00741             freespace(fnamec);
00742             return(-1);
00743         }
00744         cpl_frameset_insert(framelist,product_frame);
00745     }
00746 
00747     /* Get the extension property list */
00748 
00749     p = cpl_propertylist_duplicate(vircam_fits_get_ehu(ff));
00750     if (isdummy) {
00751         vircam_dummy_property(p);
00752         vircam_merge_propertylists(p,dummyqc);
00753     }
00754 
00755     /* Fiddle with the header now */
00756 
00757     product_frame = ps.product_frame_stack;
00758     vircam_dfs_set_product_exten_header(p,product_frame,framelist,parlist,
00759                                         vircam_recipename,"PRO-1.15",NULL);
00760     if (cpl_image_save(fim,fname,CPL_BPP_IEEE_FLOAT,p,CPL_IO_EXTEND) != 
00761         CPL_ERROR_NONE) {
00762         cpl_msg_error(fctid,"Cannot save product image extension");
00763         freespace(fname);
00764         freespace(fnamec);
00765         return(-1);
00766     }
00767 
00768     /* Write PAF */
00769 
00770     pafprop = vircam_paf_req_items(p);
00771     vircam_merge_propertylists(pafprop,ps.phupaf);
00772     vircam_paf_append(pafprop,vircam_fits_get_phu(ff),"ESO INS FILT1 NAME");
00773     vircam_paf_append(pafprop,p,"ESO DET NDIT");
00774     vircam_paf_append(pafprop,p,"ESO PRO CATG");
00775     vircam_paf_append(pafprop,vircam_fits_get_phu(ff),"RA");
00776     vircam_paf_append(pafprop,vircam_fits_get_phu(ff),"DEC");
00777     vircam_paf_append(pafprop,vircam_fits_get_phu(ff),"ESO TEL AIRM START");
00778     vircam_paf_append(pafprop,vircam_fits_get_phu(ff),"ESO TEL GUID FWHM");
00779     vircam_paf_append(pafprop,vircam_fits_get_phu(ff),"ESO TEL AMBI RHUM");
00780     vircam_paf_append(pafprop,vircam_fits_get_phu(ff),"ESO OBS TARG NAME");
00781     if (vircam_paf_print(fnamepaf,vircam_recipepaf,"QC file",
00782                          pafprop) != VIR_OK)
00783         cpl_msg_warning(fctid,"Unable to save PAF for stack");
00784     cpl_propertylist_delete(pafprop);
00785 
00786     /* Quick tidy */
00787 
00788     cpl_propertylist_delete(p);
00789 
00790     /* And now for the confidence map. Erase any stray QC headers
00791        that might exist if you've used a BPM instead of a confidence
00792        map. This only occurs if this is a dummy output */
00793 
00794     p = cpl_propertylist_duplicate(vircam_fits_get_ehu(ffc));
00795     if (isdummyc) {
00796         vircam_dummy_property(p);
00797         cpl_propertylist_erase_regexp(p,"ESO QC",0);
00798     }
00799 
00800     /* Fiddle with the header now */
00801 
00802     product_frame = ps.product_frame_stackc;
00803     vircam_dfs_set_product_exten_header(p,product_frame,framelist,parlist,
00804                                         vircam_recipename,"PRO-1.15",NULL);
00805     if (cpl_image_save(fimc,fnamec,CPL_BPP_16_SIGNED,p,CPL_IO_EXTEND) != 
00806         CPL_ERROR_NONE) {
00807         cpl_msg_error(fctid,"Cannot save product image extension");
00808         freespace(fname);
00809         freespace(fnamec);
00810         freepropertylist(p);
00811         return(-1);
00812     }
00813     cpl_propertylist_delete(p);
00814 
00815     /* Clear the allocated workspace */
00816 
00817     freespace(fname);
00818     freespace(fnamec);
00819     freespace(fnamepaf);
00820 
00821     /* Get out of here */
00822 
00823     return(0);
00824 }
00825 
00826 /*---------------------------------------------------------------------------*/
00845 /*---------------------------------------------------------------------------*/
00846 
00847 extern int vircam_jmp_save_catalogue(cpl_frameset *framelist,
00848                                      cpl_parameterlist *parlist) {
00849     cpl_frame *product_frame;
00850     int isdummy,i,status;
00851     cpl_table *ftab;
00852     cpl_propertylist *ehu,*phu,*ehu2,*pafprop;
00853     char *fname,*fnamepaf;
00854     const char *base[] = {"","catalogue_jmp","catalogue_std","catalogue_mes"};
00855     const char *fctid = "vircam_jmp_save_catalogue";
00856 
00857     /* Work out whether you have a table to save or not. If you don't
00858        then create a dummy table and save it */
00859 
00860     isdummy = 0;
00861     if (ps.outcat != NULL) {
00862         ftab = vircam_tfits_get_table(ps.outcat);
00863     } else {
00864         ftab = vircam_dummy_catalogue(2);
00865         isdummy = 1;
00866     }
00867 
00868     /* Make an attempt to get some header information */
00869 
00870     ehu = NULL;
00871     phu = NULL;
00872     if (ps.outcat != NULL) {
00873         ehu = vircam_tfits_get_ehu(ps.outcat);
00874     } else if (ps.stack_frame != NULL) {
00875         ehu = vircam_fits_get_ehu(ps.stack_frame);
00876     } else {
00877         for (i = 0; i < ps.nscience; i++) {
00878             if ((ehu = vircam_fits_get_ehu(ps.sci_fits[i])) != NULL)
00879                 break;
00880         }
00881     }
00882     if (ps.outcat != NULL) {
00883         phu = vircam_tfits_get_phu(ps.outcat);
00884     } else if (ps.stack_frame != NULL) {
00885         phu = vircam_fits_get_phu(ps.stack_frame);
00886     } else {
00887         for (i = 0; i < ps.nscience; i++) {
00888             if ((phu = vircam_fits_get_phu(ps.sci_fits[i])) != NULL)
00889                 break;
00890         }
00891     }
00892 
00893     /* Create a name */
00894 
00895     fname = vircam_jmp_outfile(base[recflag],0,1);
00896     fnamepaf = vircam_jmp_outfile(base[recflag],0,0);
00897 
00898     /* If we need to make a PHU then do that now based on the first frame
00899        in the input frame list */
00900 
00901     if (isfirst) {
00902 
00903         /* Create a new product frame object and define some tags */
00904 
00905         product_frame = cpl_frame_new();
00906         cpl_frame_set_filename(product_frame,fname);
00907         switch (recflag) {
00908         case RECSCI:
00909             cpl_frame_set_tag(product_frame,VIRCAM_PRO_OBJCAT_SCI);
00910             break;
00911         case RECSTD:
00912             cpl_frame_set_tag(product_frame,VIRCAM_PRO_OBJCAT_STD);
00913             break;
00914         default:
00915             cpl_frame_set_tag(product_frame,VIRCAM_PRO_OBJCAT_SCI);
00916             break;
00917         }
00918         cpl_frame_set_type(product_frame,CPL_FRAME_TYPE_TABLE);
00919         cpl_frame_set_group(product_frame,CPL_FRAME_GROUP_PRODUCT);
00920         cpl_frame_set_level(product_frame,CPL_FRAME_LEVEL_FINAL);
00921         ps.product_frame_cat = product_frame;
00922 
00923         /* Set up the PHU header. */
00924 
00925         vircam_dfs_set_product_primary_header(phu,product_frame,framelist,
00926                                               parlist,vircam_recipename,
00927                                               "PRO-1.15",NULL,0);
00928 
00929         /* Copy the header and delete any WCS info */
00930 
00931         ehu2 = cpl_propertylist_duplicate(ehu);
00932         status = VIR_OK;
00933         if (isdummy) {
00934             vircam_dummy_property(ehu2);
00935             vircam_merge_propertylists(ehu2,dummyqc);
00936             (void)vircam_removewcs(ehu2,&status);
00937         }
00938 
00939         /* Setup the extension header */
00940 
00941         vircam_dfs_set_product_exten_header(ehu2,product_frame,framelist,
00942                                             parlist,vircam_recipename,
00943                                             "PRO-1.15",NULL);
00944 
00945         /* 'Save' the PHU image */                       
00946 
00947         if (cpl_table_save(ftab,phu,ehu2,fname,CPL_IO_DEFAULT) != 
00948             CPL_ERROR_NONE) {
00949             cpl_msg_error(fctid,"Cannot save product PHU");
00950             cpl_frame_delete(product_frame);
00951             freepropertylist(ehu2);
00952             freespace(fname);
00953             return(-1);
00954         }
00955 
00956         /* Write PAF */
00957 
00958         pafprop = vircam_paf_req_items(ehu2);
00959         vircam_merge_propertylists(pafprop,ps.phupaf);
00960         vircam_paf_append(pafprop,ehu2,"ESO PRO CATG");
00961         vircam_paf_append(pafprop,phu,"ESO INS FILT1 NAME");
00962         vircam_paf_append(pafprop,ehu,"ESO DET NDIT");
00963         vircam_paf_append(pafprop,phu,"RA");
00964         vircam_paf_append(pafprop,phu,"DEC");
00965         vircam_paf_append(pafprop,phu,"ESO TEL AIRM START");
00966         vircam_paf_append(pafprop,phu,"ESO TEL GUID FWHM");
00967         vircam_paf_append(pafprop,phu,"ESO TEL AMBI RHUM");
00968         vircam_paf_append(pafprop,phu,"ESO OBS TARG NAME");
00969         if (vircam_paf_print(fnamepaf,vircam_recipepaf,"QC file",
00970                              pafprop) != VIR_OK)
00971             cpl_msg_warning(fctid,"Unable to save PAF for catalogue");
00972         cpl_propertylist_delete(pafprop);
00973 
00974         /* Quick tidy */
00975 
00976         freepropertylist(ehu2);
00977         if (isdummy)
00978             cpl_table_delete(ftab);
00979         cpl_frameset_insert(framelist,product_frame);
00980 
00981     } else {
00982 
00983         /* Fiddle with the header now */
00984 
00985         product_frame = ps.product_frame_cat;
00986         vircam_dfs_set_product_exten_header(ehu,product_frame,framelist,
00987                                             parlist,vircam_recipename,
00988                                             "PRO-1.15",NULL);
00989         
00990         /* Copy the header and delete any WCS info */
00991 
00992         ehu2 = cpl_propertylist_duplicate(ehu);
00993         status = VIR_OK;
00994         if (isdummy) {
00995             vircam_dummy_property(ehu2);
00996             vircam_merge_propertylists(ehu2,dummyqc);
00997             (void)vircam_removewcs(ehu2,&status);
00998         }
00999         
01000         /* Save the table */
01001 
01002         if (cpl_table_save(ftab,NULL,ehu2,fname,CPL_IO_EXTEND) != 
01003             CPL_ERROR_NONE) {
01004             cpl_msg_error(fctid,"Cannot save product table extension");
01005             freepropertylist(ehu2);
01006             freespace(fname);
01007             return(-1);
01008         }
01009 
01010         /* Write PAF */
01011 
01012         pafprop = vircam_paf_req_items(ehu2);
01013         vircam_merge_propertylists(pafprop,ps.phupaf);
01014         vircam_paf_append(pafprop,ehu2,"ESO PRO CATG");
01015         vircam_paf_append(pafprop,phu,"ESO INS FILT1 NAME");
01016         vircam_paf_append(pafprop,ehu,"ESO DET NDIT");
01017         vircam_paf_append(pafprop,phu,"RA");
01018         vircam_paf_append(pafprop,phu,"DEC");
01019         vircam_paf_append(pafprop,phu,"ESO TEL AIRM START");
01020         vircam_paf_append(pafprop,phu,"ESO TEL GUID FWHM");
01021         vircam_paf_append(pafprop,phu,"ESO TEL AMBI RHUM");
01022         vircam_paf_append(pafprop,phu,"ESO OBS TARG NAME");
01023         if (vircam_paf_print(fnamepaf,vircam_recipepaf,"QC file",
01024                              pafprop) != VIR_OK)
01025             cpl_msg_warning(fctid,"Unable to save PAF for catalogue");
01026         cpl_propertylist_delete(pafprop);
01027 
01028         /* Quick tidy */
01029 
01030         freepropertylist(ehu2);
01031         if (isdummy)
01032             cpl_table_delete(ftab);
01033     }
01034     freespace(fname);
01035     freespace(fnamepaf);
01036 
01037     /* Get out of here */
01038 
01039     return(0);
01040 }
01041 
01042 /*---------------------------------------------------------------------------*/
01061 /*---------------------------------------------------------------------------*/
01062 
01063 extern int vircam_jmp_save_illum(cpl_frameset *framelist,
01064                                  cpl_parameterlist *parlist) {
01065     cpl_frame *product_frame;
01066     int isdummy,i,status,isnew;
01067     cpl_table *ftab;
01068     cpl_propertylist *ehu,*phu,*ehu2,*pafprop;
01069     const char *fname = "illum.fits";
01070     const char *fnamepaf = "illum";
01071     const char *fctid = "vircam_jmp_save_illum";
01072 
01073     /* Work out whether you have a table to save or not. If you don't
01074        then create a dummy table and save it */
01075 
01076     isdummy = 0;
01077     if (ps.illcor != NULL) {
01078         ftab = vircam_tfits_get_table(ps.illcor);
01079     } else {
01080         ftab = vircam_illcor_newtab(1);
01081         isdummy = 1;
01082     }
01083 
01084     /* Make an attempt to get some header information */
01085 
01086     phu = NULL;
01087     ehu = NULL;
01088     if (ps.illcor != NULL) {
01089         ehu = vircam_tfits_get_ehu(ps.illcor);
01090     } else {
01091         for (i = 0; i < ps.nscience; i++) {
01092             if ((ehu = vircam_fits_get_ehu(ps.sci_fits[i])) != NULL)
01093                 break;
01094         }
01095     }
01096     if (ps.illcor != NULL) {
01097         phu = vircam_tfits_get_phu(ps.illcor);
01098     } else {
01099         for (i = 0; i < ps.nscience; i++) {
01100             if ((phu = vircam_fits_get_phu(ps.sci_fits[i])) != NULL)
01101                 break;
01102         }
01103     }
01104 
01105     /* If we need to make a PHU then do that now based on the first frame
01106        in the input frame list */
01107 
01108     if (isfirst) {
01109 
01110         /* Create a new product frame object and define some tags */
01111 
01112         product_frame = cpl_frame_new();
01113         cpl_frame_set_filename(product_frame,fname);
01114         switch (recflag) {
01115         case RECSTD:
01116             cpl_frame_set_tag(product_frame,VIRCAM_PRO_ILLCOR_STD);
01117             break;
01118         case RECMES:
01119             cpl_frame_set_tag(product_frame,VIRCAM_PRO_ILLCOR_MES);
01120             break;
01121         default:
01122             cpl_frame_set_tag(product_frame,VIRCAM_PRO_ILLCOR_STD);
01123             break;
01124         }
01125         cpl_frame_set_type(product_frame,CPL_FRAME_TYPE_TABLE);
01126         cpl_frame_set_group(product_frame,CPL_FRAME_GROUP_PRODUCT);
01127         cpl_frame_set_level(product_frame,CPL_FRAME_LEVEL_FINAL);
01128         ps.product_frame_illcor = product_frame;
01129 
01130         /* Set up the PHU header. */
01131 
01132         isnew = 0;
01133         if (ps.phupaf == NULL) { 
01134             ps.phupaf = vircam_paf_phu_items(phu);
01135             isnew = 1;
01136         }
01137         vircam_dfs_set_product_primary_header(phu,product_frame,framelist,
01138                                               parlist,vircam_recipename,
01139                                               "PRO-1.15",NULL,0);
01140 
01141         /* Copy the header and delete any WCS info */
01142 
01143         ehu2 = cpl_propertylist_duplicate(ehu);
01144         status = VIR_OK;
01145         (void)vircam_removewcs(ehu2,&status);
01146         if (isdummy) {
01147             vircam_dummy_property(ehu2);
01148             vircam_merge_propertylists(ehu2,dummyqc);
01149         }
01150             
01151 
01152         /* Setup the extension header */
01153 
01154         vircam_dfs_set_product_exten_header(ehu2,product_frame,framelist,
01155                                             parlist,vircam_recipename,
01156                                             "PRO-1.15",NULL);
01157 
01158         /* 'Save' the PHU image */                       
01159 
01160         if (cpl_table_save(ftab,phu,ehu2,fname,CPL_IO_DEFAULT) != 
01161             CPL_ERROR_NONE) {
01162             cpl_msg_error(fctid,"Cannot save product PHU");
01163             cpl_frame_delete(product_frame);
01164             freepropertylist(ehu2);
01165             return(-1);
01166         }
01167 
01168         /* Write the PAF */
01169 
01170         pafprop = vircam_paf_req_items(ehu2);
01171         vircam_merge_propertylists(pafprop,ps.phupaf);
01172         vircam_paf_append(pafprop,ehu2,"ESO PRO CATG");
01173         vircam_paf_append(pafprop,phu,"ESO INS FILT1 NAME");
01174         vircam_paf_append(pafprop,ehu,"ESO DET NDIT");
01175         vircam_paf_append(pafprop,phu,"RA");
01176         vircam_paf_append(pafprop,phu,"DEC");
01177         vircam_paf_append(pafprop,phu,"ESO TEL AIRM START");
01178         vircam_paf_append(pafprop,phu,"ESO TEL GUID FWHM");
01179         vircam_paf_append(pafprop,phu,"ESO TEL AMBI RHUM");
01180         vircam_paf_append(pafprop,phu,"ESO OBS TARG NAME");
01181         if (vircam_paf_print((char *)fnamepaf,vircam_recipepaf,"QC file",
01182                              pafprop) != VIR_OK)
01183             cpl_msg_warning(fctid,"Unable to save PAF for illcor table");
01184         cpl_propertylist_delete(pafprop);
01185         
01186         /* Quick tidy */
01187 
01188         if (isdummy)
01189             cpl_table_delete(ftab);
01190         freepropertylist(ehu2);
01191         cpl_frameset_insert(framelist,product_frame);
01192 
01193     } else {
01194 
01195 
01196         /* Fiddle with the header now */
01197 
01198         product_frame = ps.product_frame_illcor;
01199         vircam_dfs_set_product_exten_header(ehu,product_frame,framelist,
01200                                             parlist,vircam_recipename,
01201                                             "PRO-1.15",NULL);
01202         
01203         /* Copy the header and delete any WCS info */
01204 
01205         ehu2 = cpl_propertylist_duplicate(ehu);
01206         status = VIR_OK;
01207         (void)vircam_removewcs(ehu2,&status);
01208         if (isdummy) {
01209             vircam_dummy_property(ehu2);
01210             vircam_merge_propertylists(ehu2,dummyqc);
01211         }
01212         
01213         /* Save the table */
01214 
01215         if (cpl_table_save(ftab,NULL,ehu2,fname,CPL_IO_EXTEND) != 
01216             CPL_ERROR_NONE) {
01217             cpl_msg_error(fctid,"Cannot save product image extension");
01218             freepropertylist(ehu2);
01219             return(-1);
01220         }
01221 
01222         /* Write the PAF */
01223 
01224         pafprop = vircam_paf_req_items(ehu2);
01225         vircam_merge_propertylists(pafprop,ps.phupaf);
01226         vircam_paf_append(pafprop,ehu2,"ESO PRO CATG");
01227         vircam_paf_append(pafprop,phu,"ESO INS FILT1 NAME");
01228         vircam_paf_append(pafprop,ehu,"ESO DET NDIT");
01229         vircam_paf_append(pafprop,phu,"RA");
01230         vircam_paf_append(pafprop,phu,"DEC");
01231         vircam_paf_append(pafprop,phu,"ESO TEL AIRM START");
01232         vircam_paf_append(pafprop,phu,"ESO TEL GUID FWHM");
01233         vircam_paf_append(pafprop,phu,"ESO TEL AMBI RHUM");
01234         vircam_paf_append(pafprop,phu,"ESO OBS TARG NAME");
01235         if (vircam_paf_print((char *)fnamepaf,vircam_recipepaf,"QC file",
01236                              pafprop) != VIR_OK)
01237             cpl_msg_warning(fctid,"Unable to save PAF for illcor table");
01238         cpl_propertylist_delete(pafprop);
01239         
01240         /* Quick tidy */
01241 
01242         freepropertylist(ehu2);
01243         if (isdummy)
01244             cpl_table_delete(ftab);
01245     }
01246 
01247     /* Get out of here */
01248 
01249     return(0);
01250 }
01251 
01252 /*---------------------------------------------------------------------------*/
01268 /*---------------------------------------------------------------------------*/
01269 
01270 extern void vircam_jmp_ustep_seq(void) {
01271     int nalloc,i,match,j,ustepnum,nustep;
01272     vir_fits *ff;
01273     cpl_propertylist *plist;
01274     const char *fctid = "vircam_jmp_ustep_seq";
01275 
01276     /* Allocate an initial amount of workspace for the microstep sequence
01277        image sets */
01278 
01279     nalloc = INITALLOC;
01280     ps.ustep_sets = cpl_malloc(nalloc*sizeof(ustep_set));
01281     ps.nustep_sets = 0;
01282 
01283     /* Loop for each frame and get the microstep sequence number from the
01284        primary header */
01285 
01286     for (i = 0; i < ps.nscience; i++) {
01287         ff = ps.sci_fits[i];
01288         plist = vircam_fits_get_phu(ff);
01289         if (vircam_pfits_get_ustepnum(plist,&ustepnum) != VIR_OK) {
01290             cpl_msg_error(fctid,"No microstep number in %s",
01291                           vircam_fits_get_filename(ff));
01292             vircam_fits_set_error(ff,VIR_FATAL);
01293             continue;
01294         }
01295 
01296         /* See if this sequence number matches any of the others we've
01297            already defined. If it does, then simply add this frame into
01298            that sequences frameset */
01299         
01300         match = 0;
01301         for (j = 0; j < ps.nustep_sets; j++) {
01302             if (ustepnum == ps.ustep_sets[j].ustep_number) {
01303                 match = 1;
01304                 ps.ustep_sets[j].f[ps.ustep_sets[j].nframes] = ff;
01305                 ps.ustep_sets[j].nframes += 1;
01306                 if (vircam_fits_get_status(ff) != VIR_FATAL) 
01307                     ps.ustep_sets[j].ngood += 1;
01308                 break;
01309             }
01310         }
01311 
01312         /* If it doesn't match increment the number of sets and check to 
01313            make sure that we haven't overrun our allocation for ustep sets. */
01314 
01315         if (! match) {
01316             if (ps.nustep_sets+1 == nalloc) {
01317                 nalloc += INITALLOC;
01318                 ps.ustep_sets = cpl_realloc(ps.ustep_sets,nalloc*sizeof(ustep_set));
01319             }
01320 
01321             /* Now define this ustep set */
01322             
01323             (void)vircam_pfits_get_nusteps(plist,&nustep);
01324             ps.ustep_sets[ps.nustep_sets].f = cpl_malloc(nustep*sizeof(vir_fits *));
01325             ps.ustep_sets[ps.nustep_sets].ustep_number = ustepnum;
01326             ps.ustep_sets[ps.nustep_sets].nustep = nustep;
01327             ps.ustep_sets[ps.nustep_sets].status = VIR_OK;
01328             ps.ustep_sets[ps.nustep_sets].super = NULL;
01329             ps.ustep_sets[ps.nustep_sets].superc = NULL;
01330             ps.ustep_sets[ps.nustep_sets].nframes = 0;
01331             ps.ustep_sets[ps.nustep_sets].f[0] = ff;
01332             ps.ustep_sets[ps.nustep_sets].nframes = 1;
01333             ps.ustep_sets[ps.nustep_sets].ngood = 0;
01334             if (vircam_fits_get_status(ff) != VIR_FATAL)
01335                 ps.ustep_sets[ps.nustep_sets].ngood += 1;
01336             ps.nustep_sets++;
01337         }    
01338     }
01339 
01340     /* Fix the allocation to what we need and throw the rest away */
01341 
01342     ps.ustep_sets = cpl_realloc(ps.ustep_sets,
01343                                 ps.nustep_sets*sizeof(ustep_set));
01344 
01345     /* Loop through each of the defined sets and see if each is complete */
01346 
01347     for (i = 0; i < ps.nustep_sets; i++) {
01348         if (ps.ustep_sets[i].ngood == 0) {
01349             cpl_msg_warning(fctid,"Microstep sequence %d has no input",
01350                             ps.ustep_sets[i].ustep_number);
01351             ps.ustep_sets[i].status = VIR_FATAL;
01352         } else if (ps.ustep_sets[i].ngood != ps.ustep_sets[i].nustep) {
01353             cpl_msg_warning(fctid,"Microstep sequence %d incomplete",
01354                             ps.ustep_sets[i].ustep_number);
01355             ps.ustep_sets[i].status = VIR_WARN;
01356         }
01357     }
01358 }
01359 
01360 
01361 /*---------------------------------------------------------------------------*/
01377 /*---------------------------------------------------------------------------*/
01378 
01379 extern void vircam_jmp_interleave(void) {
01380     int i,refset,k,nstep,status,nk;
01381     const int *d;
01382     long npts;
01383     float val;
01384     double refx,refy,refra,refdec,x,y;
01385     cpl_image *fi,*outimage,*outconf;
01386     cpl_propertylist *plist;
01387     vir_fits *ff,**tmp;
01388     cpl_wcs *wcs;
01389     const char *fctid = "vircam_jmp_interleave";
01390 
01391     /* Work out the microstep sequences */
01392 
01393     vircam_jmp_ustep_seq(); 
01394    
01395     /* Get workspace to contain the output superframes */
01396 
01397     ps.dith_input = cpl_malloc(ps.nustep_sets*sizeof(vir_fits*));
01398     ps.dithc_input = cpl_malloc(ps.nustep_sets*sizeof(vir_fits*));
01399             
01400     /* Loop for each of the ustep sets */
01401 
01402     ps.ndith = 0;
01403     for (i = 0; i < ps.nustep_sets; i++) {
01404         if (ps.ustep_sets[i].status == VIR_FATAL) {
01405             outimage = vircam_dummy_image(ps.ustep_sets[i].f[0]);
01406             outconf = vircam_dummy_image(ps.fconf);
01407             ff = vircam_fits_wrap(outimage,ps.ustep_sets[i].f[0],NULL,NULL);
01408             ps.dith_input[ps.ndith] = ff;
01409             vircam_fits_set_error(ff,VIR_FATAL);
01410             ps.ustep_sets[i].super = ff;
01411             ff = vircam_fits_wrap(outconf,ps.ustep_sets[i].f[0],NULL,NULL);
01412             ps.dithc_input[ps.ndith++] = ff;
01413             vircam_fits_set_error(ff,VIR_FATAL);
01414             ps.ustep_sets[i].superc = ff;
01415             continue;
01416         }
01417 
01418         /* Work out the offsets in the sequence from the WCS in the header.
01419            Fail the ustep sequence if any of it's components have an
01420            unreadable WCS */
01421 
01422         refset = 0;
01423         refx = 0.0;
01424         refy = 0.0;
01425         for (k = 0; k < ps.ustep_sets[i].nframes; k++) {
01426             ff = ps.ustep_sets[i].f[k];
01427             if (vircam_fits_get_status(ff) == VIR_FATAL)
01428                 continue;
01429             wcs = cpl_wcs_new_from_propertylist(vircam_fits_get_ehu(ff));
01430             if (wcs == NULL) {
01431                 cpl_msg_error(fctid,"Unable to open WCS structure %s",
01432                               vircam_fits_get_fullname(ff));
01433                 vircam_fits_set_error(ff,VIR_FATAL);
01434                 continue;
01435             }
01436     
01437             /* Get the background value for this image */
01438 
01439             fi = vircam_fits_get_image(ff);
01440             npts = vircam_getnpts(fi);
01441             val = vircam_med(cpl_image_get_data_float(fi),NULL,npts);
01442             cpl_propertylist_update_float(vircam_fits_get_ehu(ff),
01443                                           "ESO DRS BACKMED",val);
01444 
01445             /* If this is the first frame, then set up the reference coords. */
01446     
01447             if (refset == 0) {
01448                 refset = 1;
01449                 d = cpl_array_get_data_int_const(cpl_wcs_get_image_dims(wcs));
01450                 refx = 0.5*(double)d[0];
01451                 refy = 0.5*(double)d[1];
01452                 vircam_xytoradec(wcs,refx,refy,&refra,&refdec);
01453                 cpl_propertylist_update_double(vircam_fits_get_ehu(ff),
01454                                                "ESO DRS XOFFMICRO",0.0);
01455                 cpl_propertylist_update_double(vircam_fits_get_ehu(ff),
01456                                                "ESO DRS YOFFMICRO",0.0);
01457                 cpl_wcs_delete(wcs);
01458                 continue;
01459             }
01460     
01461             /* Take the reference ra and dec and see where that occurs on
01462                the program image in x,y space */
01463     
01464             vircam_radectoxy(wcs,refra,refdec,&x,&y);
01465             x = refx - x;
01466             y = refy - y;
01467             cpl_propertylist_update_double(vircam_fits_get_ehu(ff),
01468                                            "ESO DRS XOFFMICRO",x);
01469             cpl_propertylist_update_double(vircam_fits_get_ehu(ff),
01470                                            "ESO DRS YOFFMICRO",y);
01471             cpl_wcs_delete(wcs);
01472         }
01473 
01474         /* Get a temporary workspace to hold all the good files in the list */
01475 
01476         tmp = cpl_malloc(ps.ustep_sets[i].nframes*sizeof(vir_fits *));
01477         nk = 0;
01478         for (k = 0; k < ps.ustep_sets[i].nframes; k++) {
01479             ff = ps.ustep_sets[i].f[k];
01480             if (vircam_fits_get_status(ff) != VIR_FATAL)
01481                 tmp[nk++] = ff;
01482         }
01483         if (nk < ps.ustep_sets[i].nframes) {
01484             cpl_msg_error(fctid,"A frame in this ustep sequence failed");
01485             ps.ustep_sets[i].status = VIR_WARN;
01486         }
01487 
01488         /* Otherwise interleave them */
01489 
01490         status = VIR_OK;
01491         nstep = (int)sqrt((double)(ps.ustep_sets[i].nustep));
01492         (void)vircam_interleave(tmp,nk,&(ps.fconf),1,nstep,&plist,&outimage,
01493                                 &outconf,&status);
01494         freespace(tmp);
01495         if (status != VIR_OK) {
01496             cpl_msg_error(fctid,"Interleaving failed for ugroup %d extn %d",
01497                           ps.ustep_sets[i].ustep_number,
01498                           vircam_fits_get_nexten(ps.ustep_sets[i].f[0]));
01499             freepropertylist(plist);
01500             freeimage(outimage);
01501             freeimage(outconf);
01502             outimage = vircam_dummy_image(ps.ustep_sets[i].f[0]);
01503             outconf = vircam_dummy_image(ps.fconf);
01504             ff = vircam_fits_wrap(outimage,ps.ustep_sets[i].f[0],NULL,NULL);
01505             vircam_fits_set_error(ff,VIR_FATAL);
01506             ps.ustep_sets[i].super = ff;
01507             ff = vircam_fits_wrap(outconf,ps.ustep_sets[i].f[0],NULL,NULL);
01508             vircam_fits_set_error(ff,VIR_FATAL);
01509             ps.ustep_sets[i].superc = ff;
01510         }
01511 
01512         /* Wrap the output results and store them away into the list of
01513            frames that will be dithered later on. */
01514 
01515         ps.dith_input[ps.ndith] = vircam_fits_wrap(outimage,
01516                                                    ps.ustep_sets[i].f[0],NULL,
01517                                                    plist);
01518         ps.dithc_input[ps.ndith++] = vircam_fits_wrap(outconf,
01519                                                       ps.ustep_sets[i].f[0],
01520                                                       NULL,plist);
01521         ps.ndithc = ps.ndith;
01522         ps.ustep_sets[i].super = ps.dith_input[ps.ndith - 1];
01523         ps.ustep_sets[i].superc = ps.dithc_input[ps.ndithc - 1];
01524         freepropertylist(plist);
01525     }
01526 }
01527 
01528 /*---------------------------------------------------------------------------*/
01548 /*---------------------------------------------------------------------------*/
01549 
01550 extern void vircam_jmp_dither_offsets(void) {
01551     int status,i,ustepnum,nmatch,refset;
01552     float *xoffs,*yoffs,xoff,yoff,filtfwhm;
01553     cpl_wcs *wcsref,*wcs;
01554     vir_fits *ff,*ffc;
01555     vir_tfits *catref,*outcat;
01556     cpl_table *cr,*oc,*outxy;
01557     const char *fctid = "vircam_jmp_dither_offsets";
01558     const double maxoffset = 2048;
01559 
01560     /* Is there anything to dither? If not, then get out of here. NB: We
01561        don't have to check the status of the input dither files as the
01562        calling routine won't have included any files with bad status in
01563        the input list for this routine. */
01564 
01565     if (ps.ndith == 0)
01566         return;
01567 
01568     /* If there is only 1 then set the offsets to zero and get out of here */
01569 
01570     if (ps.ndith == 1) {
01571         cpl_propertylist_update_double(vircam_fits_get_ehu(ps.dith_input[0]),
01572                                        "ESO DRS XOFFDITHER",(double)0.0);
01573         cpl_propertylist_update_double(vircam_fits_get_ehu(ps.dith_input[0]),
01574                                        "ESO DRS YOFFDITHER",(double)0.0);
01575         return;
01576     }
01577 
01578     /* Initialise the status variable and get some workspace for the offsets */
01579 
01580     status = VIR_OK;
01581     xoffs = cpl_malloc(ps.ndith*sizeof(float));
01582     yoffs = cpl_malloc(ps.ndith*sizeof(float));
01583 
01584     /* Loop for all the input files and get the FITS WCS information. */
01585 
01586     refset = 0;
01587     wcsref = NULL;
01588     for (i = 0; i < ps.ndith; i++) {
01589         ff = ps.dith_input[i];
01590         if (vircam_fits_get_status(ff) == VIR_FATAL)
01591             continue;   
01592         wcs = cpl_wcs_new_from_propertylist(vircam_fits_get_ehu(ff));
01593         (void)vircam_pfits_get_ustepnum(vircam_fits_get_phu(ff),&ustepnum);
01594 
01595         /* If we can't get a WCS for this image, then signal that with
01596            a warning */
01597 
01598         if (wcs == NULL) {
01599             cpl_msg_warning(fctid,"Unable to get WCS for ustep %d",ustepnum);
01600             xoffs[i] = 0.0;
01601             yoffs[i] = 0.0;
01602             vircam_fits_set_error(ff,VIR_WARN);
01603             continue;
01604         }
01605 
01606         /* If we don't have a reference WCS yet, then make the current frame
01607            WCS the reference and move on */
01608 
01609         if (! refset) {
01610             xoffs[i] = 0.0;
01611             yoffs[i] = 0.0;
01612             refset = 1;
01613             wcsref = wcs;
01614             continue;
01615         }
01616 
01617         /* Right, assuming we're here, then we need to work out the xy 
01618            differences */
01619 
01620         (void)vircam_diffxywcs(wcs,wcsref,&xoff,&yoff,&status);
01621 
01622         /* Did it work? If not the set a warning status for this file */
01623 
01624         if (status != VIR_OK) {
01625             xoffs[i] = 0.0;
01626             yoffs[i] = 0.0;
01627             cpl_msg_warning(fctid,"Unable to WCS difference for %d",ustepnum);
01628         } else if (fabs((double)xoff) > maxoffset || 
01629                    fabs((double)yoff) > maxoffset) {
01630             cpl_msg_warning(fctid,
01631                             "WCS offsets for %d are > %g: %g %g -- ignoring",
01632                             ustepnum,maxoffset,xoff,yoff);
01633             xoffs[i] = 0.0;
01634             yoffs[i] = 0.0;
01635         } else {
01636             xoffs[i] = xoff;
01637             yoffs[i] = yoff;
01638         }
01639         cpl_wcs_delete(wcs);
01640     }
01641     if (wcsref != NULL)
01642         cpl_wcs_delete(wcsref);
01643 
01644     /* Now generate a catalogue for each of the input images */
01645 
01646     catref = NULL;
01647     cr = NULL;
01648     filtfwhm = (interlv ? 3.5 : 2.0);
01649     for (i = 0; i < ps.ndith; i++) {
01650         ff = ps.dith_input[i];
01651         if (vircam_fits_get_status(ff) == VIR_FATAL)
01652             continue;
01653         status = VIR_OK;
01654         if (ps.ndithc != 1)
01655             ffc = ps.dithc_input[i];
01656         else
01657             ffc = ps.dithc_input[0];
01658         (void)vircam_pfits_get_ustepnum(vircam_fits_get_phu(ff),&ustepnum);
01659         outcat = NULL;
01660         (void)vircam_imcore(ff,ffc,25,5.0,0,3.5,64,1,filtfwhm,&outcat,
01661                             &status);
01662 
01663         /* If we get a bad status from imcore and this frame has already
01664            failed in the WCS stage, then mark it with VIR_FATAL status
01665            and move to the next one */
01666 
01667         if (status != VIR_OK && vircam_fits_get_status(ff) != VIR_OK) {
01668             vircam_fits_set_error(ff,VIR_FATAL);
01669             freetfits(outcat);
01670             cpl_msg_error(fctid,"Unable to get offsets for %d",ustepnum);
01671             continue;
01672 
01673         /* If we get bad status, but this file has a perfectly good offset
01674            from the WCS then just go with it that and issue a warning */
01675 
01676         } else if (status != VIR_OK && vircam_fits_get_status(ff) == VIR_OK) {
01677             vircam_fits_set_error(ff,VIR_WARN);
01678             freetfits(outcat);
01679             cpl_msg_error(fctid,"Unable to get object offset for %d. Going with WCS offset",
01680                           ustepnum);
01681             continue;
01682 
01683         /*  OK, we got a good status from imcore. See if the reference 
01684             catalogue has already been defined. If it hasn't then this 
01685             catalogue becomes the reference */
01686 
01687         } else {
01688             oc = vircam_tfits_get_table(outcat);
01689             cpl_table_add_scalar(oc,"X_coordinate",(double)xoffs[i]);
01690             cpl_table_add_scalar(oc,"Y_coordinate",(double)yoffs[i]);
01691             if (catref == NULL) {
01692                 catref = outcat;
01693                 cr = oc;
01694                 continue;
01695             }
01696 
01697             /* If this isn't the reference file, then do the cross match */
01698 
01699             (void)vircam_matchxy(oc,cr,100.0,&xoff,&yoff,&nmatch,&outxy,&status);
01700             freetable(outxy);
01701             freetfits(outcat);
01702     
01703             /* If we got a bad result with the cross match and this current
01704                file is already got a warning status, then flag it as fatal
01705                status */
01706     
01707             if ((nmatch == 0 || status == VIR_FATAL) && 
01708                 vircam_fits_get_status(ff) != VIR_OK) {
01709                 xoff = 0.0;
01710                 yoff = 0.0;
01711                 vircam_fits_set_error(ff,VIR_FATAL);
01712                 cpl_msg_error(fctid,"Unable to match stars for %d",ustepnum);
01713     
01714             /* If we got a bad result with the cross match and this current
01715                file has a good status, then just go with the WCS offset
01716                we already have */
01717     
01718             } else if ((nmatch == 0 || status == VIR_FATAL) && 
01719                        vircam_fits_get_status(ff) == VIR_OK) {
01720                 vircam_fits_set_error(ff,VIR_WARN);
01721                 xoff = 0.0;
01722                 yoff = 0.0;
01723                 cpl_msg_warning(fctid,"Unable to match stars for %d. Going with WCS offsets",
01724                                 ustepnum);
01725             }
01726     
01727             /* Add the current offsets to what we already have */
01728     
01729             xoffs[i] += xoff;
01730             yoffs[i] += yoff;
01731         }
01732     }
01733 
01734     /* Write the results to the headers */
01735 
01736     for (i = 0; i < ps.ndith; i++) {
01737         if (vircam_fits_get_status(ps.dith_input[i]) == VIR_FATAL)
01738             continue;
01739         ff = ps.dith_input[i];
01740         cpl_propertylist_update_double(vircam_fits_get_ehu(ff),
01741                                        "ESO DRS XOFFDITHER",(double)xoffs[i]);
01742         cpl_propertylist_update_double(vircam_fits_get_ehu(ff),
01743                                        "ESO DRS YOFFDITHER",(double)yoffs[i]);
01744     }
01745 
01746     /* Get rid of some workspace */
01747 
01748     freespace(xoffs);
01749     freespace(yoffs);
01750     freetfits(catref);
01751 }               
01752 
01753 /*---------------------------------------------------------------------------*/
01769 /*---------------------------------------------------------------------------*/
01770 
01771 extern void vircam_jmp_dither_images(void) {
01772     int status,ngood,i,n;
01773     vir_fits **d,**dc,*ff;
01774     cpl_propertylist *dither_ehu,*dither_phu;
01775     cpl_image *outdither,*outditherc;
01776     const char *fctid = "vircam_jmp_dither_images";
01777 
01778     /* Count how many good input images there are. */
01779 
01780     ngood = 0;
01781     for (i = 0; i < ps.ndith; i++)
01782         if (vircam_fits_get_status(ps.dith_input[i]) != VIR_FATAL)
01783             ngood++;
01784     
01785     /* If there are none, then get out of here. The output jittered image
01786        and it's confidence map should still be set to NULL */
01787 
01788     if (ngood == 0) {
01789         cpl_msg_error(fctid,"No good input images for jittering");
01790 
01791 
01792         outdither = vircam_dummy_image(ps.sci_fits[0]);
01793         outditherc = vircam_dummy_image(ps.dithc_input[0]);
01794         ff = vircam_fits_wrap(outdither,ps.sci_fits[0],NULL,NULL);
01795         ps.stack_frame = ff;
01796         vircam_fits_set_error(ff,VIR_FATAL);
01797         ff = vircam_fits_wrap(outditherc,ps.dithc_input[0],NULL,NULL);
01798         vircam_fits_set_error(ff,VIR_FATAL);
01799         ps.stackc_frame = ff;
01800         return;
01801     }
01802      
01803     /* Now transfer the input images over to new arrays... */
01804 
01805     d = cpl_malloc(ngood*sizeof(vir_fits *));
01806     dc = cpl_malloc(ngood*sizeof(vir_fits *));
01807     n = 0;
01808     for (i = 0; i < ps.ndith; i++) {
01809         if (vircam_fits_get_status(ps.dith_input[i]) != VIR_FATAL) {
01810             d[n] = ps.dith_input[i];
01811             if (ps.ndithc != 1) 
01812                 dc[n++] = ps.dithc_input[i];
01813             else 
01814                 dc[n++] = ps.dithc_input[0];
01815         }
01816     }
01817 
01818     /* Dither the remaining images */
01819 
01820     status = VIR_OK;
01821     (void)vircam_imdither(d,dc,ngood,ngood,5.0,5.0,&dither_ehu,&outdither,
01822                           &outditherc,&status);
01823     dither_phu = cpl_propertylist_duplicate(vircam_fits_get_phu(d[0]));
01824     if (status != VIR_OK) {
01825         freeimage(outdither);
01826         freeimage(outditherc);
01827         cpl_msg_error(fctid,"Error jittering to output");
01828     } else {
01829         ps.stack_frame = vircam_fits_wrap(outdither,d[0],dither_phu,
01830                                           dither_ehu);
01831         ps.stackc_frame = vircam_fits_wrap(outditherc,dc[0],dither_phu,
01832                                            dither_ehu);
01833     }
01834 
01835     /* Tidy and exit */
01836 
01837     freepropertylist(dither_phu);
01838     freepropertylist(dither_ehu);
01839     freespace(d);
01840     freespace(dc);
01841 }
01842 
01843 /*---------------------------------------------------------------------------*/
01858 /*---------------------------------------------------------------------------*/
01859 
01860 extern void vircam_jmp_catalogue(void) {
01861     const char *fctid = "vircam_jmp_catalogue";
01862     int status;
01863     vir_tfits *outtab;
01864     float filtfwhm;
01865 
01866     /* Check to see if there has been a jitter frame defined */
01867 
01868     if (ps.stack_frame == NULL || vircam_fits_get_status(ps.stack_frame) == VIR_FATAL) {
01869         cpl_msg_error(fctid,"No stack image available. No catalogue generated");
01870         return;
01871     }
01872 
01873     /* Generate the catalogue */
01874 
01875     status = VIR_OK;
01876     filtfwhm = (interlv ? 3.5 : 2);
01877     (void)vircam_imcore(ps.stack_frame,ps.stackc_frame,
01878                         vircam_jmp_config.ipix,
01879                         vircam_jmp_config.threshold,
01880                         vircam_jmp_config.icrowd,
01881                         vircam_jmp_config.rcore,
01882                         vircam_jmp_config.nbsize,2,filtfwhm,
01883                         &outtab,&status);
01884 
01885     /* If it failed, then get rid of workspaces and get out of here with
01886        an error message. If it went well, then wrap the result in a
01887        vir_tfits object */
01888 
01889     if (status != VIR_OK) {
01890         cpl_msg_error(fctid,"Error generating catalogue");
01891         freetfits(outtab);
01892         vircam_fits_set_error(ps.stack_frame,VIR_FATAL);
01893     } else {
01894         ps.outcat = outtab;
01895     }
01896                           
01897     return;
01898 }
01899 
01900 /*---------------------------------------------------------------------------*/
01916 /*---------------------------------------------------------------------------*/
01917     
01918 extern void vircam_jmp_matched_stds(void) {
01919     int status,ncat,nstd,level;
01920     const char *fctid = "vircam_jmp_matched_stds";
01921     cpl_table *stdscat,*newstds,*tmp,*tmp2,*cat;
01922     cpl_propertylist *p;
01923 
01924     /* Initialise status */
01925 
01926     status = VIR_OK;
01927 
01928     /* Check that we have a catalogue */
01929     
01930     if (ps.outcat == NULL) {
01931         cpl_msg_error(fctid,"No input catalogue found");
01932         return;
01933     }
01934 
01935     /* Get some standard stars */
01936 
01937     (void)vircam_getstds(vircam_fits_get_ehu(ps.stack_frame),1,
01938                          current_catpath,current_cat,&stdscat,&status);
01939     if (status != VIR_OK) {
01940         freetable(stdscat);
01941         cpl_msg_error(fctid,"Failed to find any standards");
01942         return;
01943     }
01944 
01945     /* Restrict to stars with good photometry */
01946 
01947     cpl_table_or_selected_float(stdscat,"j_msig",CPL_LESS_THAN,0.2);
01948     cpl_table_and_selected_float(stdscat,"k_msig",CPL_LESS_THAN,0.2);
01949     newstds = cpl_table_extract_selected(stdscat);
01950     nstd = cpl_table_get_nrow(newstds);
01951 
01952     /* If there are too many objects in the catalogue then first restrict
01953        ourselves by ellipticity. Cut so that there are similar numbers of
01954        objects in the standards and the object catalogues by retaining the
01955        brighter objects */
01956 
01957     cat = vircam_tfits_get_table(ps.outcat);
01958     ncat = cpl_table_get_nrow(cat);
01959     if (ncat > 500 && ncat > 2.0*nstd) {
01960         tmp = cpl_table_duplicate(cat);
01961         cpl_table_or_selected_float(tmp,"Ellipticity",CPL_LESS_THAN,0.5);
01962         tmp2 = cpl_table_extract_selected(tmp);
01963         ncat = cpl_table_get_nrow(tmp2);
01964         freetable(tmp);
01965         p = cpl_propertylist_new();
01966         cpl_propertylist_append_bool(p,"Aper_flux_3",TRUE);         
01967         cpl_table_sort(tmp2,(const cpl_propertylist *)p);
01968         cpl_propertylist_delete(p);
01969         level = min(ncat,max(1,min(5000,max(500,2*nstd))));
01970         tmp = cpl_table_extract(tmp2,1,level);
01971         freetable(tmp2);
01972         ncat = cpl_table_get_nrow(tmp);
01973         cat = tmp;
01974     } else {
01975         tmp = NULL;
01976     }
01977 
01978     /* Now match this against the catalogue */
01979 
01980     (void)vircam_matchstds(cat,newstds,300.0,&(ps.matchstds),&status);
01981     freetable(stdscat);
01982     freetable(newstds);
01983     freetable(tmp);
01984     if (status != VIR_OK) {
01985         freetable(ps.matchstds);
01986         cpl_msg_error(fctid,"Failed to match standards to catalogue");
01987         return;
01988     }
01989 }
01990 
01991 /*---------------------------------------------------------------------------*/
02008 /*---------------------------------------------------------------------------*/
02009     
02010 extern void vircam_jmp_wcsfit(void) {
02011     int status,n,i;
02012     const char *fctid = "vircam_jmp_wcsfit";
02013     float *ra,*dec,*x,*y;
02014     double r,d;
02015     cpl_table *cat;
02016     cpl_wcs *wcs;
02017 
02018     /* Initialise status */
02019 
02020     status = VIR_OK;
02021 
02022     /* Check that we have a catalogue */
02023     
02024     if (ps.matchstds == NULL) {
02025         cpl_msg_error(fctid,"No input matched standards catalogue found");
02026         return;
02027     }
02028 
02029     /* Fit the plate solution */
02030 
02031     (void)vircam_platesol(vircam_fits_get_ehu(ps.stack_frame),
02032                           vircam_tfits_get_ehu(ps.outcat),ps.matchstds,
02033                           6,1,&status);
02034     if (status != VIR_OK) {
02035         cpl_msg_error(fctid,"Failed to fit WCS");
02036         return;
02037     }
02038 
02039     /* Update the RA and DEC of the objects in the object catalogue */
02040 
02041     cat = vircam_tfits_get_table(ps.outcat);
02042     n = cpl_table_get_nrow(cat);
02043     wcs = cpl_wcs_new_from_propertylist(vircam_fits_get_ehu(ps.stack_frame));
02044     if (wcs == NULL) {
02045         cpl_msg_error(fctid,"Failed to fill RA and Dec in catalogue");
02046         return;
02047     }
02048     x = cpl_table_get_data_float(cat,"X_coordinate"); 
02049     y = cpl_table_get_data_float(cat,"Y_coordinate");
02050     ra = cpl_table_get_data_float(cat,"RA");
02051     dec = cpl_table_get_data_float(cat,"DEC");
02052     for (i = 0; i < n; i++) {
02053         vircam_xytoradec(wcs,x[i],y[i],&r,&d);
02054         ra[i] = (float)r;
02055         dec[i] = (float)d;
02056     }
02057     cpl_wcs_delete(wcs);
02058 }
02059 
02060 /*---------------------------------------------------------------------------*/
02076 /*---------------------------------------------------------------------------*/
02077 
02078 extern void vircam_jmp_photcal(void) {
02079     int status;
02080     const char *fctid = "vircam_jmp_photcal";
02081     char filt[32];
02082     cpl_propertylist *pl;
02083 
02084     /* Initialise status */
02085 
02086     status = VIR_OK;
02087 
02088     /* Check that we have a catalogue */
02089     
02090     if (ps.matchstds == NULL || cpl_table_get_nrow(ps.matchstds) == 0) {
02091         cpl_msg_error(fctid,"No input matched standards catalogue found");
02092         return;
02093     }
02094 
02095     /* What filter is this? */
02096 
02097     if (vircam_pfits_get_filter(vircam_fits_get_phu(ps.stack_frame),filt) != VIR_OK) {
02098         cpl_msg_error(fctid,"No filter name in stack header");
02099         return;
02100     }
02101 
02102     /* Fit the photometric calibration */
02103     
02104     pl = vircam_tfits_get_ehu(ps.outcat);
02105     (void)vircam_photcal(&(ps.stack_frame),&(ps.matchstds),&pl,1,filt,
02106                          ps.tphottab,&status);
02107     if (status != VIR_OK) {
02108         cpl_msg_error(fctid,"Failed to fit photometric zeropoint");
02109         return;
02110     }
02111 }
02112 
02113 /*---------------------------------------------------------------------------*/
02130 /*---------------------------------------------------------------------------*/
02131 
02132 extern void vircam_jmp_bpm2conf(void) {
02133     cpl_image *im;
02134     int i,n,*data;
02135 
02136     /* Get the image */
02137 
02138     im = vircam_fits_get_image(ps.fconf);
02139     n = cpl_image_get_size_x(im)*cpl_image_get_size_y(im);
02140     data = cpl_image_get_data_int(im);
02141 
02142     /* Convert it now */ 
02143 
02144     for (i = 0; i < n; i++)
02145         data[i] = (data[i] == 1 ? 0 : 100);
02146         
02147 }
02148 
02149 /*---------------------------------------------------------------------------*/
02166 /*---------------------------------------------------------------------------*/
02167 
02168 extern void vircam_jmp_skycor(void) {
02169     int i,ngood,status,nfrms;
02170     long npts;
02171     vir_fits **ftmp,*ff,**frms;
02172     const char *fctid = "vircam_jmp_skycor";
02173     unsigned char *rejmask,*rejplus;
02174     cpl_propertylist *drs;
02175     cpl_image *skyimg,*fim;
02176     float *data,med,sig;
02177 
02178     /* If we need to do a sky combination... */
02179 
02180     if (offsky == 0 || offsky == 1) {
02181 
02182         /* Which array are we combining? */
02183 
02184         if (offsky == 1) {
02185             nfrms = ps.noffsets;
02186             frms = ps.offsky_fits;
02187         } else {
02188             nfrms = ps.nscience;
02189             frms = ps.sci_fits;
02190         }
02191 
02192         /* Sort out all images with good status */
02193 
02194         ftmp = cpl_malloc(nfrms*sizeof(vir_fits *));
02195         ngood = 0;
02196         for (i = 0; i < nfrms; i++) {
02197             ff = frms[i];
02198             if (vircam_fits_get_status(ff) != VIR_FATAL)
02199                 ftmp[ngood++] = ff;
02200         }
02201 
02202         /* If there aren't any good images, then get out of here now */
02203 
02204         if (ngood == 0) {
02205             freespace(ftmp);
02206             cpl_msg_error(fctid,"Sky correction impossible. No good sky frames available");
02207             return;
02208         }
02209 
02210         /* Combine all the good science images */
02211 
02212         status = VIR_OK;
02213         (void)vircam_imcombine(ftmp,ngood,1,1,1,2.0,&skyimg,&rejmask,&rejplus,
02214                                &drs,&status);
02215         freespace(rejmask);
02216         freespace(rejplus);
02217         if (offsky == 0)
02218             freepropertylist(drs);
02219         freespace(ftmp);
02220     } else {
02221         skyimg = vircam_fits_get_image(ps.fsky);
02222     }
02223 
02224     /* Normalise the sky frame to zero median */
02225 
02226     data = cpl_image_get_data_float(skyimg);
02227     npts = cpl_image_get_size_x(skyimg)*cpl_image_get_size_y(skyimg);
02228     vircam_qmedsig(data,NULL,npts,5.0,3,-1000.0,65535.0,&med,&sig);
02229     for (i = 0; i < npts; i++)
02230         data[i] -= med;
02231 
02232     /* Save the sky frame if it's an offset sky */
02233 
02234     if (offsky == 1) {
02235         ps.outsky = vircam_fits_wrap(skyimg,frms[0],NULL,NULL);
02236         vircam_merge_propertylists(vircam_fits_get_ehu(ps.outsky),drs);
02237         freepropertylist(drs);
02238         drs = vircam_fits_get_ehu(ps.outsky);
02239         cpl_propertylist_update_float(drs,"ESO QC SKYMED",med);
02240         cpl_propertylist_set_comment(drs,"ESO QC SKYMED",
02241                                      "Median sky background");
02242         cpl_propertylist_update_float(drs,"ESO QC SKYSIG",sig);
02243         cpl_propertylist_set_comment(drs,"ESO QC SKYSIG",
02244                                      "Sky background sigma");
02245     }
02246 
02247     /* Subtract the normalised sky frame from the science images and do the
02248        same for the sky images */
02249 
02250     for (i = 0; i < ps.nscience; i++) {
02251         ff = ps.sci_fits[i];
02252         fim = vircam_fits_get_image(ff);
02253         if (vircam_fits_get_status(ff) != VIR_FATAL) {
02254             drs = vircam_fits_get_ehu(ff);
02255             cpl_image_subtract(fim,skyimg);
02256             if (offsky == 0) {
02257                 cpl_propertylist_update_string(drs,"ESO DRS SKYCOR","internal");
02258             } else if (offsky == 1) {
02259                 cpl_propertylist_update_string(drs,"ESO DRS SKYCOR","offsky");
02260             } else if (offsky == -1) {
02261                 cpl_propertylist_update_string(drs,"ESO DRS SKYCOR",
02262                                                vircam_fits_get_fullname(ps.fsky));
02263             }           
02264             cpl_propertylist_set_comment(drs,"ESO DRS SKYCOR",
02265                                          "Sky correction method");
02266         }
02267     }
02268     for (i = 0; i < ps.noffsets; i++) {
02269         ff = ps.offsky_fits[i];
02270         fim = vircam_fits_get_image(ff);
02271         if (vircam_fits_get_status(ff) != VIR_FATAL) {
02272             drs = vircam_fits_get_ehu(ff);
02273             cpl_image_subtract(fim,skyimg);
02274             if (offsky == 0) {
02275                 cpl_propertylist_update_string(drs,"ESO DRS SKYCOR","internal");
02276             } else if (offsky == 1) {
02277                 cpl_propertylist_update_string(drs,"ESO DRS SKYCOR","offsky");
02278             } else if (offsky == -1) {
02279                 cpl_propertylist_update_string(drs,"ESO DRS SKYCOR",
02280                                                vircam_fits_get_fullname(ps.fsky));
02281             }           
02282             cpl_propertylist_set_comment(drs,"ESO DRS SKYCOR",
02283                                          "Image has been sky corrected");
02284         }
02285     }
02286 
02287     /* Clean up and get out of here */
02288 
02289     if (offsky == 0) 
02290         cpl_image_delete(skyimg);
02291     else if (offsky == 1)
02292         cpl_image_add_scalar(skyimg,med);
02293     return;
02294 }
02295 
02296 /*---------------------------------------------------------------------------*/
02312 /*---------------------------------------------------------------------------*/
02313 
02314 extern void vircam_jmp_get_readnoise_gain(int jext, float *readnoise, 
02315                                           float *gain) {
02316     cpl_propertylist *p_rg;
02317     const char *fctid = "vircam_jmp_get_readnoise_gain";
02318 
02319     /* Load the propertylist */
02320 
02321     p_rg = cpl_propertylist_load(cpl_frame_get_filename(ps.readgain_file),
02322                                  jext);
02323 
02324     /* Check the readnoise property type and read it */
02325 
02326     switch (cpl_propertylist_get_type(p_rg,"ESO QC READNOISE")) {
02327     case CPL_TYPE_FLOAT:
02328         *readnoise = cpl_propertylist_get_float(p_rg,"ESO QC READNOISE");
02329         break;
02330     case CPL_TYPE_DOUBLE:
02331         *readnoise = (float)cpl_propertylist_get_double(p_rg,
02332                                                         "ESO QC READNOISE");
02333         break;
02334     default:
02335         cpl_error_reset();
02336         *readnoise = 25.0;
02337         cpl_msg_error(fctid,"Unable to get READNOISE estimate, guessing %g\n",
02338                       *readnoise);
02339     }
02340 
02341     /* Now the gain */
02342 
02343     switch (cpl_propertylist_get_type(p_rg,"ESO QC CONAD")) {
02344     case CPL_TYPE_FLOAT:
02345         *gain = cpl_propertylist_get_float(p_rg,"ESO QC CONAD");
02346         break;
02347     case CPL_TYPE_DOUBLE:
02348         *gain = (float)cpl_propertylist_get_double(p_rg,"ESO QC CONAD");
02349         break;
02350     default:
02351         cpl_error_reset();
02352         *gain = 1.0;
02353         cpl_msg_error(fctid,"Unable to get GAIN estimate, guessing %g\n",
02354                       *gain);
02355     }
02356     cpl_propertylist_delete(p_rg);
02357 }
02358 
02359 /*---------------------------------------------------------------------------*/
02374 /*---------------------------------------------------------------------------*/
02375 
02376 extern void vircam_jmp_illum(void) {
02377     int ngood,i,status,ii;
02378     float illcor_rms;
02379     vir_fits **ftmp,*ff,*ffc;
02380     char filt[32];
02381     cpl_table **mstds,*stdscat,*ms,*illcor,*ot;
02382     vir_tfits *outtab;
02383     cpl_propertylist **pl,*phu,*ehu;
02384     const char *fctid = "vircam_jmp_illum";
02385 
02386     /* Set some default values */
02387 
02388     ps.illcor = NULL;
02389     
02390     /* Sort out all images with good status */
02391 
02392     ftmp = cpl_malloc(ps.nscience*sizeof(vir_fits *));
02393     ngood = 0;
02394     for (i = 0; i < ps.nscience; i++) {
02395         ff = ps.sci_fits[i];
02396         if (vircam_fits_get_status(ff) != VIR_FATAL)
02397             ftmp[ngood++] = ff;
02398     }
02399 
02400     /* If there aren't any good images, then get out of here now */
02401 
02402     if (ngood == 0) {
02403         freespace(ftmp);
02404         cpl_msg_error(fctid,"Illumination correction impossible. No good science frames available");
02405         return;
02406     }
02407 
02408     /* What filter is this? */
02409 
02410     if (vircam_pfits_get_filter(vircam_fits_get_phu(ftmp[0]),filt) != VIR_OK) {
02411         cpl_msg_error(fctid,"No filter name in stack header");
02412         freespace(ftmp);
02413         return;
02414     }
02415 
02416     /* Get some workspace for the various arrays you need for the 
02417        illumination correction routine */
02418 
02419     mstds = cpl_malloc(ngood*sizeof(cpl_table *));
02420     for (i = 0; i < ngood; i++)
02421         mstds[i] = NULL;
02422     pl = cpl_malloc(ngood*sizeof(cpl_propertylist *));
02423     for (i = 0; i < ngood; i++)
02424         pl[i] = NULL;
02425 
02426     /* For each of the good input frames, do a catalogue generation and
02427        get some matched standards */
02428 
02429     ffc = ps.fconf;
02430     for (i = 0; i < ngood; i++) {
02431         status = VIR_OK;
02432         ff = ftmp[i];
02433         (void)vircam_imcore(ff,ffc,vircam_jmp_config.ipix,
02434                             1.5*vircam_jmp_config.threshold,0,
02435                             vircam_jmp_config.rcore,vircam_jmp_config.nbsize,2,
02436                             3.5,&outtab,&status);
02437         if (status != VIR_OK)
02438             cpl_error_reset();
02439         pl[i] = cpl_propertylist_duplicate(vircam_tfits_get_ehu(outtab));
02440 
02441         /* Get some standard stars */
02442 
02443         (void)vircam_getstds(vircam_fits_get_ehu(ff),1,current_catpath,
02444                              current_cat,&stdscat,&status);
02445         if (status == VIR_FATAL) {
02446             freetfits(outtab);
02447             freespace(ftmp);
02448             for (ii = 0; ii < ngood; ii++) {
02449                 freetable(mstds[ii]);
02450                 freepropertylist(pl[ii]);
02451             }
02452             freespace(mstds);
02453             freespace(pl);
02454             cpl_msg_error(fctid,"Illumination correction fails");
02455             return;
02456         } else if (status == VIR_WARN) {
02457             cpl_error_reset();
02458             freetfits(outtab);
02459             continue;
02460         }
02461 
02462         /* Now match this against the catalogue */
02463 
02464         ot = vircam_tfits_get_table(outtab);
02465         (void)vircam_matchstds(ot,stdscat,300.0,&ms,&status);
02466         if (status == VIR_FATAL) {
02467             freetable(stdscat);
02468             freetfits(outtab);
02469             freespace(ftmp);
02470             for (ii = 0; ii < ngood; ii++) {
02471                 freetable(mstds[ii]);
02472                 freepropertylist(pl[ii]);
02473             }
02474             freespace(mstds);
02475             freespace(pl);
02476             cpl_msg_error(fctid,"%s",cpl_error_get_message());
02477             return;
02478         }
02479         mstds[i] = ms;
02480         freetfits(outtab);
02481         freetable(stdscat);
02482     }
02483     
02484     /* Call the illumination routine */
02485 
02486     status = VIR_OK;
02487     (void)vircam_illum(ftmp,mstds,pl,ngood,filt,ps.tphottab,128,&illcor,
02488                        &illcor_rms,&status);
02489     
02490     /* Wrap the result */
02491     
02492     phu = cpl_propertylist_duplicate(vircam_fits_get_phu(ftmp[0]));
02493     ehu = cpl_propertylist_duplicate(vircam_fits_get_ehu(ftmp[0]));
02494     ps.illcor = vircam_tfits_wrap(illcor,NULL,phu,ehu);
02495     cpl_propertylist_update_float(ehu,"ESO QC ILLUMCOR_RMS",illcor_rms);
02496     cpl_propertylist_set_comment(ehu,"ESO QC ILLUMCOR_RMS",
02497                                  "RMS of illumination correction map");
02498     
02499     /* Tidy up */
02500 
02501     for (i = 0; i < ngood; i++) {
02502         freetable(mstds[i]);
02503         freepropertylist(pl[i]);
02504     }
02505     freespace(mstds);
02506     freespace(pl);
02507     freespace(ftmp);
02508 }
02509 
02510 /*---------------------------------------------------------------------------*/
02532 /*---------------------------------------------------------------------------*/
02533 
02534 static char *vircam_jmp_outfile(const char *bname, int ind, int isfits) {
02535     int nf;
02536     char *fname;
02537 
02538     /* Count up how much space you need for the output string. The 'isfits'
02539        block accounts for ".fits", an underscore and an EOS*/
02540 
02541     nf = strlen(bname);
02542     if (ind == 0) 
02543         nf++;
02544     else 
02545         nf += ((int)log10((double)ind)+1);
02546     if (isfits) 
02547         nf += 7;
02548     else
02549         nf += 2;
02550 
02551     /* Get the space for the filename */
02552 
02553     fname = cpl_malloc(nf);
02554 
02555     /* Now write the name */
02556 
02557     if (isfits) 
02558         (void)snprintf(fname,nf,"%s_%d.fits",bname,ind);
02559     else
02560         (void)snprintf(fname,nf,"%s_%d",bname,ind);
02561     return(fname);
02562 }
02563 
02564 /*---------------------------------------------------------------------------*/
02579 /*---------------------------------------------------------------------------*/
02580 
02581 extern void vircam_jmp_init(void) {
02582 
02583     /* Level 0 stuff */
02584 
02585     ps.labels = NULL;
02586     ps.master_dark = NULL;
02587     ps.master_twilight_flat = NULL;
02588     ps.master_conf = NULL;
02589     ps.master_sky = NULL;
02590     ps.mask = NULL;
02591     ps.chantab = NULL;
02592     ps.phottab = NULL;
02593     ps.tphottab = NULL;
02594     ps.readgain_file = NULL;
02595     ps.science_frames = NULL;
02596     ps.offset_skies = NULL;
02597     ps.product_frames_simple = NULL;
02598     ps.product_frames_simple_off = NULL;
02599     ps.product_frames_super = NULL;
02600     ps.product_frames_superc = NULL;
02601     ps.product_frame_stack = NULL;
02602     ps.product_frame_stackc = NULL;
02603     ps.product_frame_cat = NULL;
02604     ps.product_frame_illcor = NULL;
02605     ps.product_frame_sky = NULL;
02606     ps.phupaf = NULL;
02607     ps.gaincors = NULL;
02608     ps.catpath = NULL;
02609     ps.catname = NULL;
02610     ps.catpath2 = NULL;
02611     ps.catname2 = NULL;
02612 
02613     /* Level 1 stuff */
02614 
02615     ps.fdark = NULL;
02616     ps.fflat = NULL;
02617     ps.fconf = NULL;
02618     ps.fsky = NULL;
02619     ps.fchantab = NULL;
02620     ps.nscience = 0;
02621     ps.sci_fits = NULL;
02622     ps.noffsets = 0;
02623     ps.offsky_fits = NULL;
02624     ps.nustep_sets = 0;
02625     ps.ustep_sets = NULL;
02626     ps.ndith = 0;
02627     ps.ndithc = 0;
02628     ps.dith_input = NULL;
02629     ps.dithc_input = NULL;
02630     ps.stack_frame = NULL;
02631     ps.stackc_frame = NULL;
02632     ps.outcat = NULL;
02633     ps.outsky = NULL;
02634     ps.illcor = NULL;
02635 }
02636 
02637 /*---------------------------------------------------------------------------*/
02658 /*---------------------------------------------------------------------------*/
02659 
02660 extern void vircam_jmp_tidy(int level) {
02661     int i;
02662 
02663     /* Level 1 stuff */
02664 
02665     freefits(ps.fdark); 
02666     freefits(ps.fflat); 
02667     freefits(ps.fconf);
02668     freefits(ps.fsky);
02669     freetfits(ps.fchantab);
02670     freefitslist(ps.sci_fits,ps.nscience);
02671     freefitslist(ps.offsky_fits,ps.noffsets);
02672     ps.nscience = 0;
02673     for (i = 0; i < ps.nustep_sets; i++) {
02674         freespace(ps.ustep_sets[i].f);
02675         freefits(ps.ustep_sets[i].super);
02676         freefits(ps.ustep_sets[i].superc);
02677     }
02678     freespace(ps.ustep_sets);
02679     ps.nustep_sets = 0;
02680     freespace(ps.dith_input);
02681     ps.ndith = 0;
02682     freespace(ps.dithc_input);
02683     ps.ndithc = 0;
02684 
02685     freefits(ps.stack_frame);
02686     freefits(ps.stackc_frame);
02687     freetfits(ps.outcat);
02688     freetable(ps.matchstds);
02689     freetfits(ps.illcor);
02690     freefits(ps.outsky);
02691 
02692     if (level == 1)
02693         return;
02694     
02695     /* Level 0 stuff */
02696 
02697     freespace(ps.labels);
02698     freeframe(ps.master_dark);
02699     freeframe(ps.master_twilight_flat);
02700     freeframe(ps.master_conf);
02701     freeframe(ps.master_sky);
02702     freemask(ps.mask);
02703     freeframe(ps.chantab);
02704     freeframe(ps.phottab);
02705     freeframe(ps.readgain_file);
02706     freetable(ps.tphottab);
02707     freeframeset(ps.science_frames);
02708     freeframeset(ps.offset_skies);
02709     freepropertylist(ps.phupaf);
02710     freespace(ps.product_frames_simple);     /* NB: We only have to delete */
02711     freespace(ps.product_frames_super);      /* the arrays and not the frames */
02712     freespace(ps.product_frames_superc);     /* as these get passed back */
02713     freespace(ps.product_frames_simple_off); /* to esorex */
02714     freespace(ps.gaincors);
02715     freespace(ps.catpath);
02716     freespace(ps.catname);
02717     freespace(ps.catpath2);
02718     freespace(ps.catname2);
02719 }
02720 
02723 /*
02724 
02725 $Log: vircam_jmp_utils.c,v $
02726 Revision 1.56  2010/09/13 11:50:18  jim
02727 removed unnecessary declaration
02728 
02729 Revision 1.55  2010/09/13 11:40:33  jim
02730 Fixed the way DRS SKYCOR is written to header
02731 
02732 Revision 1.54  2010/09/10 11:24:35  jim
02733 Modified skycor so that if a master sky exists, then it uses it rather than
02734 create a new one.
02735 
02736 Revision 1.53  2010/06/07 12:42:40  jim
02737 Modifications to get rid of compiler gripes
02738 
02739 Revision 1.52  2010/06/03 11:33:29  jim
02740 Opened up matching radius in call to vircam_matchxy to deal with the case
02741 where the input WCS is poorly defined.
02742 
02743 Revision 1.51  2010/02/08 16:35:26  jim
02744 Moved the definition of propertylist ps.phupaf to the recipes so that
02745 this doesn't fail when we don't save the simple images
02746 
02747 Revision 1.50  2010/02/08 10:50:09  jim
02748 Fixed GAIN -> CONAD bug
02749 
02750 Revision 1.49  2010/01/31 19:28:46  jim
02751 SKYMED and SKYSIG are moved from DRS to QC in offset skies
02752 
02753 Revision 1.48  2009/11/18 21:08:46  jim
02754 Mods made to wcsfit to restrict standards to those with good photometry and to
02755 limit the number of entries in catalogue to use in matching up with the
02756 standards
02757 
02758 Revision 1.47  2009/09/22 12:29:12  jim
02759 Modified to do offset sky exposures
02760 
02761 Revision 1.46  2009/09/21 14:45:54  jim
02762 Fixed bug in _save_simple where template frame wasn't being defined for
02763 each image extension
02764 
02765 Revision 1.45  2009/09/09 09:46:33  jim
02766 modified in attempt to get headers right
02767 
02768 Revision 1.44  2008/12/09 11:36:45  jim
02769 Fixed _illcor routine so that if the imcore fails the cpl error is reset
02770 
02771 Revision 1.43  2008/12/08 13:04:24  jim
02772 Fixed PRO CATG for catalogue and illumination correction pafs
02773 
02774 Revision 1.42  2008/12/08 06:41:18  jim
02775 Fixed problem where wrong PRO CATG was being written to pafs
02776 
02777 Revision 1.41  2008/11/27 09:13:47  jim
02778 Changed PRO CATG values for interleaved confidence maps so they are different
02779 for science and standard recipes
02780 
02781 Revision 1.40  2008/11/25 18:54:45  jim
02782 removed extra sky stuff
02783 
02784 Revision 1.39  2008/11/25 11:56:38  jim
02785 Routine skycor now allows for either standard or masked object sky correction.
02786 Modification was done to routine which saves superframes to define the
02787 output confidence map as officially a different type from the confidence
02788 maps generated from jittering.
02789 
02790 Revision 1.38  2008/11/21 10:10:06  jim
02791 Patched in newer version of vircam_matchxy
02792 
02793 Revision 1.37  2008/09/30 11:35:01  jim
02794 Added PRO CATG to the pafs
02795 
02796 Revision 1.36  2008/09/29 11:29:46  jim
02797 Some cosmetic fixes. Also define a maximum shift between dither frames
02798 
02799 Revision 1.35  2008/08/28 09:05:37  jim
02800 Fixed bug where QC was being duplicated from master BPM on rare occasions.
02801 Fixed bug where ARCFILE wasn't being written to the paf file for illum_cor
02802 tables. Sky combine is done with medians
02803 
02804 Revision 1.34  2008/08/05 14:07:01  jim
02805 Relaxed matching criteria
02806 
02807 Revision 1.33  2008/07/10 13:05:53  jim
02808 Modified to use v4.2 version of cpl_wcs
02809 
02810 Revision 1.32  2008/06/20 11:13:35  jim
02811 Fixed dodgy call to cpl_wcs_get_image_dims
02812 
02813 Revision 1.31  2008/05/06 08:40:10  jim
02814 Modified to use cpl_wcs interface
02815 
02816 Revision 1.30  2007/11/22 12:34:08  jim
02817 Added line to vircam_jmp_save_illum to make sure that phupaf is defined
02818 when _save_simple isn't called
02819 
02820 Revision 1.29  2007/10/25 17:34:00  jim
02821 Modified to remove lint warnings
02822 
02823 Revision 1.28  2007/10/19 09:25:10  jim
02824 Fixed problems with missing includes
02825 
02826 Revision 1.27  2007/10/19 06:55:06  jim
02827 Modifications made to use new method for directing the recipes to the
02828 standard catalogues using the sof
02829 
02830 Revision 1.26  2007/10/15 12:50:28  jim
02831 Modified for compatibility with cpl_4.0
02832 
02833 Revision 1.25  2007/06/13 08:10:49  jim
02834 Modified to allow for different output file names depending upon the calling
02835 recipe
02836 
02837 Revision 1.24  2007/05/15 08:54:07  jim
02838 Fixed small bug in stack_save and dither_offsets. Also modified dither_offsets
02839 to just send back zeros if there is only one dither frame
02840 
02841 Revision 1.23  2007/05/08 10:41:49  jim
02842 Added gaincor variables
02843 
02844 Revision 1.22  2007/05/02 09:15:37  jim
02845 Modified to use new api for vircam_imcore and vircam_platesol
02846 
02847 Revision 1.21  2007/04/30 09:40:01  jim
02848 Added vircam_paf_append
02849 
02850 Revision 1.20  2007/04/13 12:29:11  jim
02851 Fixed bug in save_simple which mean that ps.pafphu was allocated for every
02852 file rather than just the first one
02853 
02854 Revision 1.19  2007/04/04 16:05:59  jim
02855 Modified to make paf information a bit more correct
02856 
02857 Revision 1.18  2007/04/04 10:34:55  jim
02858 Modified to use new dfs tags
02859 
02860 Revision 1.17  2007/03/14 22:08:54  jim
02861 Fixed typo
02862 
02863 Revision 1.16  2007/03/14 14:49:13  jim
02864 Fixed problem with missing paf files in jmp recipes if detlive = F. Also
02865 fixed problem where extra dummy products were being created
02866 
02867 Revision 1.15  2007/03/13 09:50:53  jim
02868 Fixed bug in vircam_jmp_save_illum where PAF wasn't being saved for the
02869 first extension
02870 
02871 Revision 1.14  2007/03/06 14:06:24  jim
02872 Fixed missing paf write
02873 
02874 Revision 1.13  2007/03/06 13:49:48  jim
02875 Created static routine vircam_jmp_outfile to create a 'predictable'
02876 output filename for each product
02877 
02878 Revision 1.12  2007/03/01 12:42:42  jim
02879 Modified slightly after code checking
02880 
02881 Revision 1.11  2007/02/25 06:34:20  jim
02882 Plugged memory leak
02883 
02884 Revision 1.10  2007/02/15 06:59:38  jim
02885 Added ability to write QC paf files
02886 
02887 Revision 1.9  2007/02/06 13:11:12  jim
02888 Fixed entry for PRO dictionary in cpl_dfs_set_product_header
02889 
02890 Revision 1.8  2007/01/17 23:54:00  jim
02891 Plugged some memory leaks
02892 
02893 Revision 1.7  2006/12/19 13:30:01  jim
02894 Fixed vircam_jmp_illum to initialise pointers
02895 
02896 Revision 1.6  2006/12/18 12:51:20  jim
02897 Tightened up some of the error reporting
02898 
02899 Revision 1.5  2006/12/15 09:58:28  jim
02900 read noise and gain keywords were wrong...
02901 
02902 Revision 1.4  2006/11/29 12:28:45  jim
02903 Modified so that the correct recipe names would appear in the headers of
02904 data products
02905 
02906 Revision 1.3  2006/11/28 22:10:22  jim
02907 Added illcor_rms to calling sequence of vircam_illum
02908 
02909 Revision 1.2  2006/11/28 20:56:31  jim
02910 Added vircam_jmp_illum and vircam_jmp_save_illum
02911 
02912 Revision 1.1  2006/11/27 11:54:37  jim
02913 Initial entry
02914 
02915 
02916 */
02917 

Generated on 7 Feb 2011 for VIRCAM Pipeline by  doxygen 1.6.1