vircam_jmp_utils.c

00001 /* $Id: vircam_jmp_utils.c,v 1.57 2012/01/15 17:40:09 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: 2012/01/15 17:40:09 $
00024  * $Revision: 1.57 $
00025  * $Name: vcam-1_3_2 $
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_TYPE_UCHAR,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_TYPE_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_TYPE_UCHAR,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_TYPE_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_TYPE_UCHAR,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_TYPE_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_TYPE_UCHAR,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_TYPE_UCHAR,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_TYPE_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_TYPE_SHORT,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_TYPE_UCHAR,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_TYPE_UCHAR,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_TYPE_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_TYPE_SHORT,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,
01350                             "Microstep sequence %" CPL_SIZE_FORMAT " has no input",
01351                             (cpl_size)(ps.ustep_sets[i].ustep_number));
01352             ps.ustep_sets[i].status = VIR_FATAL;
01353         } else if (ps.ustep_sets[i].ngood != ps.ustep_sets[i].nustep) {
01354             cpl_msg_warning(fctid,"Microstep sequence %" CPL_SIZE_FORMAT " incomplete",
01355                             (cpl_size)(ps.ustep_sets[i].ustep_number));
01356             ps.ustep_sets[i].status = VIR_WARN;
01357         }
01358     }
01359 }
01360 
01361 
01362 /*---------------------------------------------------------------------------*/
01378 /*---------------------------------------------------------------------------*/
01379 
01380 extern void vircam_jmp_interleave(void) {
01381     int i,refset,k,nstep,status,nk;
01382     const int *d;
01383     long npts;
01384     float val;
01385     double refx,refy,refra,refdec,x,y;
01386     cpl_image *fi,*outimage,*outconf;
01387     cpl_propertylist *plist;
01388     vir_fits *ff,**tmp;
01389     cpl_wcs *wcs;
01390     const char *fctid = "vircam_jmp_interleave";
01391 
01392     /* Work out the microstep sequences */
01393 
01394     vircam_jmp_ustep_seq(); 
01395    
01396     /* Get workspace to contain the output superframes */
01397 
01398     ps.dith_input = cpl_malloc(ps.nustep_sets*sizeof(vir_fits*));
01399     ps.dithc_input = cpl_malloc(ps.nustep_sets*sizeof(vir_fits*));
01400             
01401     /* Loop for each of the ustep sets */
01402 
01403     ps.ndith = 0;
01404     for (i = 0; i < ps.nustep_sets; i++) {
01405         if (ps.ustep_sets[i].status == VIR_FATAL) {
01406             outimage = vircam_dummy_image(ps.ustep_sets[i].f[0]);
01407             outconf = vircam_dummy_image(ps.fconf);
01408             ff = vircam_fits_wrap(outimage,ps.ustep_sets[i].f[0],NULL,NULL);
01409             ps.dith_input[ps.ndith] = ff;
01410             vircam_fits_set_error(ff,VIR_FATAL);
01411             ps.ustep_sets[i].super = ff;
01412             ff = vircam_fits_wrap(outconf,ps.ustep_sets[i].f[0],NULL,NULL);
01413             ps.dithc_input[ps.ndith++] = ff;
01414             vircam_fits_set_error(ff,VIR_FATAL);
01415             ps.ustep_sets[i].superc = ff;
01416             continue;
01417         }
01418 
01419         /* Work out the offsets in the sequence from the WCS in the header.
01420            Fail the ustep sequence if any of it's components have an
01421            unreadable WCS */
01422 
01423         refset = 0;
01424         refx = 0.0;
01425         refy = 0.0;
01426         for (k = 0; k < ps.ustep_sets[i].nframes; k++) {
01427             ff = ps.ustep_sets[i].f[k];
01428             if (vircam_fits_get_status(ff) == VIR_FATAL)
01429                 continue;
01430             wcs = cpl_wcs_new_from_propertylist(vircam_fits_get_ehu(ff));
01431             if (wcs == NULL) {
01432                 cpl_msg_error(fctid,"Unable to open WCS structure %s",
01433                               vircam_fits_get_fullname(ff));
01434                 vircam_fits_set_error(ff,VIR_FATAL);
01435                 continue;
01436             }
01437     
01438             /* Get the background value for this image */
01439 
01440             fi = vircam_fits_get_image(ff);
01441             npts = vircam_getnpts(fi);
01442             val = vircam_med(cpl_image_get_data_float(fi),NULL,npts);
01443             cpl_propertylist_update_float(vircam_fits_get_ehu(ff),
01444                                           "ESO DRS BACKMED",val);
01445 
01446             /* If this is the first frame, then set up the reference coords. */
01447     
01448             if (refset == 0) {
01449                 refset = 1;
01450                 d = cpl_array_get_data_int_const(cpl_wcs_get_image_dims(wcs));
01451                 refx = 0.5*(double)d[0];
01452                 refy = 0.5*(double)d[1];
01453                 vircam_xytoradec(wcs,refx,refy,&refra,&refdec);
01454                 cpl_propertylist_update_double(vircam_fits_get_ehu(ff),
01455                                                "ESO DRS XOFFMICRO",0.0);
01456                 cpl_propertylist_update_double(vircam_fits_get_ehu(ff),
01457                                                "ESO DRS YOFFMICRO",0.0);
01458                 cpl_wcs_delete(wcs);
01459                 continue;
01460             }
01461     
01462             /* Take the reference ra and dec and see where that occurs on
01463                the program image in x,y space */
01464     
01465             vircam_radectoxy(wcs,refra,refdec,&x,&y);
01466             x = refx - x;
01467             y = refy - y;
01468             cpl_propertylist_update_double(vircam_fits_get_ehu(ff),
01469                                            "ESO DRS XOFFMICRO",x);
01470             cpl_propertylist_update_double(vircam_fits_get_ehu(ff),
01471                                            "ESO DRS YOFFMICRO",y);
01472             cpl_wcs_delete(wcs);
01473         }
01474 
01475         /* Get a temporary workspace to hold all the good files in the list */
01476 
01477         tmp = cpl_malloc(ps.ustep_sets[i].nframes*sizeof(vir_fits *));
01478         nk = 0;
01479         for (k = 0; k < ps.ustep_sets[i].nframes; k++) {
01480             ff = ps.ustep_sets[i].f[k];
01481             if (vircam_fits_get_status(ff) != VIR_FATAL)
01482                 tmp[nk++] = ff;
01483         }
01484         if (nk < ps.ustep_sets[i].nframes) {
01485             cpl_msg_error(fctid,"A frame in this ustep sequence failed");
01486             ps.ustep_sets[i].status = VIR_WARN;
01487         }
01488 
01489         /* Otherwise interleave them */
01490 
01491         status = VIR_OK;
01492         nstep = (int)sqrt((double)(ps.ustep_sets[i].nustep));
01493         (void)vircam_interleave(tmp,nk,&(ps.fconf),1,nstep,&plist,&outimage,
01494                                 &outconf,&status);
01495         freespace(tmp);
01496         if (status != VIR_OK) {
01497             cpl_msg_error(fctid,
01498                           "Interleaving failed for ugroup %" CPL_SIZE_FORMAT " extn %" CPL_SIZE_FORMAT,
01499                           (cpl_size)(ps.ustep_sets[i].ustep_number),
01500                           (cpl_size)(vircam_fits_get_nexten(ps.ustep_sets[i].f[0])));
01501             freepropertylist(plist);
01502             freeimage(outimage);
01503             freeimage(outconf);
01504             outimage = vircam_dummy_image(ps.ustep_sets[i].f[0]);
01505             outconf = vircam_dummy_image(ps.fconf);
01506             ff = vircam_fits_wrap(outimage,ps.ustep_sets[i].f[0],NULL,NULL);
01507             vircam_fits_set_error(ff,VIR_FATAL);
01508             ps.ustep_sets[i].super = ff;
01509             ff = vircam_fits_wrap(outconf,ps.ustep_sets[i].f[0],NULL,NULL);
01510             vircam_fits_set_error(ff,VIR_FATAL);
01511             ps.ustep_sets[i].superc = ff;
01512         }
01513 
01514         /* Wrap the output results and store them away into the list of
01515            frames that will be dithered later on. */
01516 
01517         ps.dith_input[ps.ndith] = vircam_fits_wrap(outimage,
01518                                                    ps.ustep_sets[i].f[0],NULL,
01519                                                    plist);
01520         ps.dithc_input[ps.ndith++] = vircam_fits_wrap(outconf,
01521                                                       ps.ustep_sets[i].f[0],
01522                                                       NULL,plist);
01523         ps.ndithc = ps.ndith;
01524         ps.ustep_sets[i].super = ps.dith_input[ps.ndith - 1];
01525         ps.ustep_sets[i].superc = ps.dithc_input[ps.ndithc - 1];
01526         freepropertylist(plist);
01527     }
01528 }
01529 
01530 /*---------------------------------------------------------------------------*/
01550 /*---------------------------------------------------------------------------*/
01551 
01552 extern void vircam_jmp_dither_offsets(void) {
01553     int status,i,ustepnum,nmatch,refset;
01554     float *xoffs,*yoffs,xoff,yoff,filtfwhm;
01555     cpl_wcs *wcsref,*wcs;
01556     vir_fits *ff,*ffc;
01557     vir_tfits *catref,*outcat;
01558     cpl_table *cr,*oc,*outxy;
01559     const char *fctid = "vircam_jmp_dither_offsets";
01560     const double maxoffset = 2048;
01561 
01562     /* Is there anything to dither? If not, then get out of here. NB: We
01563        don't have to check the status of the input dither files as the
01564        calling routine won't have included any files with bad status in
01565        the input list for this routine. */
01566 
01567     if (ps.ndith == 0)
01568         return;
01569 
01570     /* If there is only 1 then set the offsets to zero and get out of here */
01571 
01572     if (ps.ndith == 1) {
01573         cpl_propertylist_update_double(vircam_fits_get_ehu(ps.dith_input[0]),
01574                                        "ESO DRS XOFFDITHER",(double)0.0);
01575         cpl_propertylist_update_double(vircam_fits_get_ehu(ps.dith_input[0]),
01576                                        "ESO DRS YOFFDITHER",(double)0.0);
01577         return;
01578     }
01579 
01580     /* Initialise the status variable and get some workspace for the offsets */
01581 
01582     status = VIR_OK;
01583     xoffs = cpl_malloc(ps.ndith*sizeof(float));
01584     yoffs = cpl_malloc(ps.ndith*sizeof(float));
01585 
01586     /* Loop for all the input files and get the FITS WCS information. */
01587 
01588     refset = 0;
01589     wcsref = NULL;
01590     for (i = 0; i < ps.ndith; i++) {
01591         ff = ps.dith_input[i];
01592         if (vircam_fits_get_status(ff) == VIR_FATAL)
01593             continue;   
01594         wcs = cpl_wcs_new_from_propertylist(vircam_fits_get_ehu(ff));
01595         (void)vircam_pfits_get_ustepnum(vircam_fits_get_phu(ff),&ustepnum);
01596 
01597         /* If we can't get a WCS for this image, then signal that with
01598            a warning */
01599 
01600         if (wcs == NULL) {
01601             cpl_msg_warning(fctid,
01602                             "Unable to get WCS for ustep %" CPL_SIZE_FORMAT,
01603                             (cpl_size)ustepnum);
01604             xoffs[i] = 0.0;
01605             yoffs[i] = 0.0;
01606             vircam_fits_set_error(ff,VIR_WARN);
01607             continue;
01608         }
01609 
01610         /* If we don't have a reference WCS yet, then make the current frame
01611            WCS the reference and move on */
01612 
01613         if (! refset) {
01614             xoffs[i] = 0.0;
01615             yoffs[i] = 0.0;
01616             refset = 1;
01617             wcsref = wcs;
01618             continue;
01619         }
01620 
01621         /* Right, assuming we're here, then we need to work out the xy 
01622            differences */
01623 
01624         (void)vircam_diffxywcs(wcs,wcsref,&xoff,&yoff,&status);
01625 
01626         /* Did it work? If not the set a warning status for this file */
01627 
01628         if (status != VIR_OK) {
01629             xoffs[i] = 0.0;
01630             yoffs[i] = 0.0;
01631             cpl_msg_warning(fctid,
01632                             "Unable to WCS difference for %" CPL_SIZE_FORMAT,
01633                             (cpl_size)ustepnum);
01634         } else if (fabs((double)xoff) > maxoffset || 
01635                    fabs((double)yoff) > maxoffset) {
01636             cpl_msg_warning(fctid,
01637                             "WCS offsets for %" CPL_SIZE_FORMAT " are > %g: %g %g -- ignoring",
01638                             (cpl_size)ustepnum,maxoffset,xoff,yoff);
01639             xoffs[i] = 0.0;
01640             yoffs[i] = 0.0;
01641         } else {
01642             xoffs[i] = xoff;
01643             yoffs[i] = yoff;
01644         }
01645         cpl_wcs_delete(wcs);
01646     }
01647     if (wcsref != NULL)
01648         cpl_wcs_delete(wcsref);
01649 
01650     /* Now generate a catalogue for each of the input images */
01651 
01652     catref = NULL;
01653     cr = NULL;
01654     filtfwhm = (interlv ? 3.5 : 2.0);
01655     for (i = 0; i < ps.ndith; i++) {
01656         ff = ps.dith_input[i];
01657         if (vircam_fits_get_status(ff) == VIR_FATAL)
01658             continue;
01659         status = VIR_OK;
01660         if (ps.ndithc != 1)
01661             ffc = ps.dithc_input[i];
01662         else
01663             ffc = ps.dithc_input[0];
01664         (void)vircam_pfits_get_ustepnum(vircam_fits_get_phu(ff),&ustepnum);
01665         outcat = NULL;
01666         (void)vircam_imcore(ff,ffc,25,5.0,0,3.5,64,1,filtfwhm,&outcat,
01667                             &status);
01668 
01669         /* If we get a bad status from imcore and this frame has already
01670            failed in the WCS stage, then mark it with VIR_FATAL status
01671            and move to the next one */
01672 
01673         if (status != VIR_OK && vircam_fits_get_status(ff) != VIR_OK) {
01674             vircam_fits_set_error(ff,VIR_FATAL);
01675             freetfits(outcat);
01676             cpl_msg_error(fctid,
01677                           "Unable to get offsets for %" CPL_SIZE_FORMAT,
01678                           (cpl_size)ustepnum);
01679             continue;
01680 
01681         /* If we get bad status, but this file has a perfectly good offset
01682            from the WCS then just go with it that and issue a warning */
01683 
01684         } else if (status != VIR_OK && vircam_fits_get_status(ff) == VIR_OK) {
01685             vircam_fits_set_error(ff,VIR_WARN);
01686             freetfits(outcat);
01687             cpl_msg_error(fctid,
01688                           "Unable to get object offset for %" CPL_SIZE_FORMAT ". Going with WCS offset",
01689                           (cpl_size)ustepnum);
01690             continue;
01691 
01692         /*  OK, we got a good status from imcore. See if the reference 
01693             catalogue has already been defined. If it hasn't then this 
01694             catalogue becomes the reference */
01695 
01696         } else {
01697             oc = vircam_tfits_get_table(outcat);
01698             cpl_table_add_scalar(oc,"X_coordinate",(double)xoffs[i]);
01699             cpl_table_add_scalar(oc,"Y_coordinate",(double)yoffs[i]);
01700             if (catref == NULL) {
01701                 catref = outcat;
01702                 cr = oc;
01703                 continue;
01704             }
01705 
01706             /* If this isn't the reference file, then do the cross match */
01707 
01708             (void)vircam_matchxy(oc,cr,100.0,&xoff,&yoff,&nmatch,&outxy,&status);
01709             freetable(outxy);
01710             freetfits(outcat);
01711     
01712             /* If we got a bad result with the cross match and this current
01713                file is already got a warning status, then flag it as fatal
01714                status */
01715     
01716             if ((nmatch == 0 || status == VIR_FATAL) && 
01717                 vircam_fits_get_status(ff) != VIR_OK) {
01718                 xoff = 0.0;
01719                 yoff = 0.0;
01720                 vircam_fits_set_error(ff,VIR_FATAL);
01721                 cpl_msg_error(fctid,
01722                               "Unable to match stars for %" CPL_SIZE_FORMAT,
01723                               (cpl_size)ustepnum);
01724     
01725             /* If we got a bad result with the cross match and this current
01726                file has a good status, then just go with the WCS offset
01727                we already have */
01728     
01729             } else if ((nmatch == 0 || status == VIR_FATAL) && 
01730                        vircam_fits_get_status(ff) == VIR_OK) {
01731                 vircam_fits_set_error(ff,VIR_WARN);
01732                 xoff = 0.0;
01733                 yoff = 0.0;
01734                 cpl_msg_warning(fctid,
01735                                 "Unable to match stars for %" CPL_SIZE_FORMAT ". Going with WCS offsets",
01736                                 (cpl_size)ustepnum);
01737             }
01738     
01739             /* Add the current offsets to what we already have */
01740     
01741             xoffs[i] += xoff;
01742             yoffs[i] += yoff;
01743         }
01744     }
01745 
01746     /* Write the results to the headers */
01747 
01748     for (i = 0; i < ps.ndith; i++) {
01749         if (vircam_fits_get_status(ps.dith_input[i]) == VIR_FATAL)
01750             continue;
01751         ff = ps.dith_input[i];
01752         cpl_propertylist_update_double(vircam_fits_get_ehu(ff),
01753                                        "ESO DRS XOFFDITHER",(double)xoffs[i]);
01754         cpl_propertylist_update_double(vircam_fits_get_ehu(ff),
01755                                        "ESO DRS YOFFDITHER",(double)yoffs[i]);
01756     }
01757 
01758     /* Get rid of some workspace */
01759 
01760     freespace(xoffs);
01761     freespace(yoffs);
01762     freetfits(catref);
01763 }               
01764 
01765 /*---------------------------------------------------------------------------*/
01781 /*---------------------------------------------------------------------------*/
01782 
01783 extern void vircam_jmp_dither_images(void) {
01784     int status,ngood,i,n;
01785     vir_fits **d,**dc,*ff;
01786     cpl_propertylist *dither_ehu,*dither_phu;
01787     cpl_image *outdither,*outditherc;
01788     const char *fctid = "vircam_jmp_dither_images";
01789 
01790     /* Count how many good input images there are. */
01791 
01792     ngood = 0;
01793     for (i = 0; i < ps.ndith; i++)
01794         if (vircam_fits_get_status(ps.dith_input[i]) != VIR_FATAL)
01795             ngood++;
01796     
01797     /* If there are none, then get out of here. The output jittered image
01798        and it's confidence map should still be set to NULL */
01799 
01800     if (ngood == 0) {
01801         cpl_msg_error(fctid,"No good input images for jittering");
01802 
01803 
01804         outdither = vircam_dummy_image(ps.sci_fits[0]);
01805         outditherc = vircam_dummy_image(ps.dithc_input[0]);
01806         ff = vircam_fits_wrap(outdither,ps.sci_fits[0],NULL,NULL);
01807         ps.stack_frame = ff;
01808         vircam_fits_set_error(ff,VIR_FATAL);
01809         ff = vircam_fits_wrap(outditherc,ps.dithc_input[0],NULL,NULL);
01810         vircam_fits_set_error(ff,VIR_FATAL);
01811         ps.stackc_frame = ff;
01812         return;
01813     }
01814      
01815     /* Now transfer the input images over to new arrays... */
01816 
01817     d = cpl_malloc(ngood*sizeof(vir_fits *));
01818     dc = cpl_malloc(ngood*sizeof(vir_fits *));
01819     n = 0;
01820     for (i = 0; i < ps.ndith; i++) {
01821         if (vircam_fits_get_status(ps.dith_input[i]) != VIR_FATAL) {
01822             d[n] = ps.dith_input[i];
01823             if (ps.ndithc != 1) 
01824                 dc[n++] = ps.dithc_input[i];
01825             else 
01826                 dc[n++] = ps.dithc_input[0];
01827         }
01828     }
01829 
01830     /* Dither the remaining images */
01831 
01832     status = VIR_OK;
01833     (void)vircam_imdither(d,dc,ngood,ngood,5.0,5.0,&dither_ehu,&outdither,
01834                           &outditherc,&status);
01835     dither_phu = cpl_propertylist_duplicate(vircam_fits_get_phu(d[0]));
01836     if (status != VIR_OK) {
01837         freeimage(outdither);
01838         freeimage(outditherc);
01839         cpl_msg_error(fctid,"Error jittering to output");
01840     } else {
01841         ps.stack_frame = vircam_fits_wrap(outdither,d[0],dither_phu,
01842                                           dither_ehu);
01843         ps.stackc_frame = vircam_fits_wrap(outditherc,dc[0],dither_phu,
01844                                            dither_ehu);
01845     }
01846 
01847     /* Tidy and exit */
01848 
01849     freepropertylist(dither_phu);
01850     freepropertylist(dither_ehu);
01851     freespace(d);
01852     freespace(dc);
01853 }
01854 
01855 /*---------------------------------------------------------------------------*/
01870 /*---------------------------------------------------------------------------*/
01871 
01872 extern void vircam_jmp_catalogue(void) {
01873     const char *fctid = "vircam_jmp_catalogue";
01874     int status;
01875     vir_tfits *outtab;
01876     float filtfwhm;
01877 
01878     /* Check to see if there has been a jitter frame defined */
01879 
01880     if (ps.stack_frame == NULL || vircam_fits_get_status(ps.stack_frame) == VIR_FATAL) {
01881         cpl_msg_error(fctid,"No stack image available. No catalogue generated");
01882         return;
01883     }
01884 
01885     /* Generate the catalogue */
01886 
01887     status = VIR_OK;
01888     filtfwhm = (interlv ? 3.5 : 2);
01889     (void)vircam_imcore(ps.stack_frame,ps.stackc_frame,
01890                         vircam_jmp_config.ipix,
01891                         vircam_jmp_config.threshold,
01892                         vircam_jmp_config.icrowd,
01893                         vircam_jmp_config.rcore,
01894                         vircam_jmp_config.nbsize,2,filtfwhm,
01895                         &outtab,&status);
01896 
01897     /* If it failed, then get rid of workspaces and get out of here with
01898        an error message. If it went well, then wrap the result in a
01899        vir_tfits object */
01900 
01901     if (status != VIR_OK) {
01902         cpl_msg_error(fctid,"Error generating catalogue");
01903         freetfits(outtab);
01904         vircam_fits_set_error(ps.stack_frame,VIR_FATAL);
01905     } else {
01906         ps.outcat = outtab;
01907     }
01908                           
01909     return;
01910 }
01911 
01912 /*---------------------------------------------------------------------------*/
01928 /*---------------------------------------------------------------------------*/
01929     
01930 extern void vircam_jmp_matched_stds(void) {
01931     int status,ncat,nstd,level;
01932     const char *fctid = "vircam_jmp_matched_stds";
01933     cpl_table *stdscat,*newstds,*tmp,*tmp2,*cat;
01934     cpl_propertylist *p;
01935 
01936     /* Initialise status */
01937 
01938     status = VIR_OK;
01939 
01940     /* Check that we have a catalogue */
01941     
01942     if (ps.outcat == NULL) {
01943         cpl_msg_error(fctid,"No input catalogue found");
01944         return;
01945     }
01946 
01947     /* Get some standard stars */
01948 
01949     (void)vircam_getstds(vircam_fits_get_ehu(ps.stack_frame),1,
01950                          current_catpath,current_cat,&stdscat,&status);
01951     if (status != VIR_OK) {
01952         freetable(stdscat);
01953         cpl_msg_error(fctid,"Failed to find any standards");
01954         return;
01955     }
01956 
01957     /* Restrict to stars with good photometry */
01958 
01959     (void)cpl_table_or_selected_float(stdscat,"j_msig",CPL_LESS_THAN,0.2);
01960     (void)cpl_table_and_selected_float(stdscat,"k_msig",CPL_LESS_THAN,0.2);
01961     newstds = cpl_table_extract_selected(stdscat);
01962     nstd = (int)cpl_table_get_nrow(newstds);
01963 
01964     /* If there are too many objects in the catalogue then first restrict
01965        ourselves by ellipticity. Cut so that there are similar numbers of
01966        objects in the standards and the object catalogues by retaining the
01967        brighter objects */
01968 
01969     cat = vircam_tfits_get_table(ps.outcat);
01970     ncat = (int)cpl_table_get_nrow(cat);
01971     if (ncat > 500 && ncat > 2.0*nstd) {
01972         tmp = cpl_table_duplicate(cat);
01973         (void)cpl_table_or_selected_float(tmp,"Ellipticity",CPL_LESS_THAN,0.5);
01974         tmp2 = cpl_table_extract_selected(tmp);
01975         ncat = (int)cpl_table_get_nrow(tmp2);
01976         freetable(tmp);
01977         p = cpl_propertylist_new();
01978         cpl_propertylist_append_bool(p,"Aper_flux_3",TRUE);         
01979         cpl_table_sort(tmp2,(const cpl_propertylist *)p);
01980         cpl_propertylist_delete(p);
01981         level = min(ncat,max(1,min(5000,max(500,2*nstd))));
01982         tmp = cpl_table_extract(tmp2,1,(cpl_size)level);
01983         freetable(tmp2);
01984         ncat = (int)cpl_table_get_nrow(tmp);
01985         cat = tmp;
01986     } else {
01987         tmp = NULL;
01988     }
01989 
01990     /* Now match this against the catalogue */
01991 
01992     (void)vircam_matchstds(cat,newstds,300.0,&(ps.matchstds),&status);
01993     freetable(stdscat);
01994     freetable(newstds);
01995     freetable(tmp);
01996     if (status != VIR_OK) {
01997         freetable(ps.matchstds);
01998         cpl_msg_error(fctid,"Failed to match standards to catalogue");
01999         return;
02000     }
02001 }
02002 
02003 /*---------------------------------------------------------------------------*/
02020 /*---------------------------------------------------------------------------*/
02021     
02022 extern void vircam_jmp_wcsfit(void) {
02023     int status,n,i;
02024     const char *fctid = "vircam_jmp_wcsfit";
02025     float *ra,*dec,*x,*y;
02026     double r,d;
02027     cpl_table *cat;
02028     cpl_wcs *wcs;
02029 
02030     /* Initialise status */
02031 
02032     status = VIR_OK;
02033 
02034     /* Check that we have a catalogue */
02035     
02036     if (ps.matchstds == NULL) {
02037         cpl_msg_error(fctid,"No input matched standards catalogue found");
02038         return;
02039     }
02040 
02041     /* Fit the plate solution */
02042 
02043     (void)vircam_platesol(vircam_fits_get_ehu(ps.stack_frame),
02044                           vircam_tfits_get_ehu(ps.outcat),ps.matchstds,
02045                           6,1,&status);
02046     if (status != VIR_OK) {
02047         cpl_msg_error(fctid,"Failed to fit WCS");
02048         return;
02049     }
02050 
02051     /* Update the RA and DEC of the objects in the object catalogue */
02052 
02053     cat = vircam_tfits_get_table(ps.outcat);
02054     n = (int)cpl_table_get_nrow(cat);
02055     wcs = cpl_wcs_new_from_propertylist(vircam_fits_get_ehu(ps.stack_frame));
02056     if (wcs == NULL) {
02057         cpl_msg_error(fctid,"Failed to fill RA and Dec in catalogue");
02058         return;
02059     }
02060     x = cpl_table_get_data_float(cat,"X_coordinate"); 
02061     y = cpl_table_get_data_float(cat,"Y_coordinate");
02062     ra = cpl_table_get_data_float(cat,"RA");
02063     dec = cpl_table_get_data_float(cat,"DEC");
02064     for (i = 0; i < n; i++) {
02065         vircam_xytoradec(wcs,x[i],y[i],&r,&d);
02066         ra[i] = (float)r;
02067         dec[i] = (float)d;
02068     }
02069     cpl_wcs_delete(wcs);
02070 }
02071 
02072 /*---------------------------------------------------------------------------*/
02088 /*---------------------------------------------------------------------------*/
02089 
02090 extern void vircam_jmp_photcal(void) {
02091     int status;
02092     const char *fctid = "vircam_jmp_photcal";
02093     char filt[32];
02094     cpl_propertylist *pl;
02095 
02096     /* Initialise status */
02097 
02098     status = VIR_OK;
02099 
02100     /* Check that we have a catalogue */
02101     
02102     if (ps.matchstds == NULL || (int)cpl_table_get_nrow(ps.matchstds) == 0) {
02103         cpl_msg_error(fctid,"No input matched standards catalogue found");
02104         return;
02105     }
02106 
02107     /* What filter is this? */
02108 
02109     if (vircam_pfits_get_filter(vircam_fits_get_phu(ps.stack_frame),filt) != VIR_OK) {
02110         cpl_msg_error(fctid,"No filter name in stack header");
02111         return;
02112     }
02113 
02114     /* Fit the photometric calibration */
02115     
02116     pl = vircam_tfits_get_ehu(ps.outcat);
02117     (void)vircam_photcal(&(ps.stack_frame),&(ps.matchstds),&pl,1,filt,
02118                          ps.tphottab,&status);
02119     if (status != VIR_OK) {
02120         cpl_msg_error(fctid,"Failed to fit photometric zeropoint");
02121         return;
02122     }
02123 }
02124 
02125 /*---------------------------------------------------------------------------*/
02142 /*---------------------------------------------------------------------------*/
02143 
02144 extern void vircam_jmp_bpm2conf(void) {
02145     cpl_image *im;
02146     int i,n,*data;
02147 
02148     /* Get the image */
02149 
02150     im = vircam_fits_get_image(ps.fconf);
02151     n = (int)cpl_image_get_size_x(im)*(int)cpl_image_get_size_y(im);
02152     data = cpl_image_get_data_int(im);
02153 
02154     /* Convert it now */ 
02155 
02156     for (i = 0; i < n; i++)
02157         data[i] = (data[i] == 1 ? 0 : 100);
02158         
02159 }
02160 
02161 /*---------------------------------------------------------------------------*/
02178 /*---------------------------------------------------------------------------*/
02179 
02180 extern void vircam_jmp_skycor(void) {
02181     int i,ngood,status,nfrms;
02182     long npts;
02183     vir_fits **ftmp,*ff,**frms;
02184     const char *fctid = "vircam_jmp_skycor";
02185     unsigned char *rejmask,*rejplus;
02186     cpl_propertylist *drs;
02187     cpl_image *skyimg,*fim;
02188     float *data,med,sig;
02189 
02190     /* If we need to do a sky combination... */
02191 
02192     if (offsky == 0 || offsky == 1) {
02193 
02194         /* Which array are we combining? */
02195 
02196         if (offsky == 1) {
02197             nfrms = ps.noffsets;
02198             frms = ps.offsky_fits;
02199         } else {
02200             nfrms = ps.nscience;
02201             frms = ps.sci_fits;
02202         }
02203 
02204         /* Sort out all images with good status */
02205 
02206         ftmp = cpl_malloc(nfrms*sizeof(vir_fits *));
02207         ngood = 0;
02208         for (i = 0; i < nfrms; i++) {
02209             ff = frms[i];
02210             if (vircam_fits_get_status(ff) != VIR_FATAL)
02211                 ftmp[ngood++] = ff;
02212         }
02213 
02214         /* If there aren't any good images, then get out of here now */
02215 
02216         if (ngood == 0) {
02217             freespace(ftmp);
02218             cpl_msg_error(fctid,"Sky correction impossible. No good sky frames available");
02219             return;
02220         }
02221 
02222         /* Combine all the good science images */
02223 
02224         status = VIR_OK;
02225         (void)vircam_imcombine(ftmp,ngood,1,1,1,2.0,&skyimg,&rejmask,&rejplus,
02226                                &drs,&status);
02227         freespace(rejmask);
02228         freespace(rejplus);
02229         if (offsky == 0)
02230             freepropertylist(drs);
02231         freespace(ftmp);
02232     } else {
02233         skyimg = vircam_fits_get_image(ps.fsky);
02234     }
02235 
02236     /* Normalise the sky frame to zero median */
02237 
02238     data = cpl_image_get_data_float(skyimg);
02239     npts = (long)cpl_image_get_size_x(skyimg)*(long)cpl_image_get_size_y(skyimg);
02240     vircam_qmedsig(data,NULL,npts,5.0,3,-1000.0,65535.0,&med,&sig);
02241     for (i = 0; i < npts; i++)
02242         data[i] -= med;
02243 
02244     /* Save the sky frame if it's an offset sky */
02245 
02246     if (offsky == 1) {
02247         ps.outsky = vircam_fits_wrap(skyimg,frms[0],NULL,NULL);
02248         vircam_merge_propertylists(vircam_fits_get_ehu(ps.outsky),drs);
02249         freepropertylist(drs);
02250         drs = vircam_fits_get_ehu(ps.outsky);
02251         cpl_propertylist_update_float(drs,"ESO QC SKYMED",med);
02252         cpl_propertylist_set_comment(drs,"ESO QC SKYMED",
02253                                      "Median sky background");
02254         cpl_propertylist_update_float(drs,"ESO QC SKYSIG",sig);
02255         cpl_propertylist_set_comment(drs,"ESO QC SKYSIG",
02256                                      "Sky background sigma");
02257     }
02258 
02259     /* Subtract the normalised sky frame from the science images and do the
02260        same for the sky images */
02261 
02262     for (i = 0; i < ps.nscience; i++) {
02263         ff = ps.sci_fits[i];
02264         fim = vircam_fits_get_image(ff);
02265         if (vircam_fits_get_status(ff) != VIR_FATAL) {
02266             drs = vircam_fits_get_ehu(ff);
02267             cpl_image_subtract(fim,skyimg);
02268             if (offsky == 0) {
02269                 cpl_propertylist_update_string(drs,"ESO DRS SKYCOR","internal");
02270             } else if (offsky == 1) {
02271                 cpl_propertylist_update_string(drs,"ESO DRS SKYCOR","offsky");
02272             } else if (offsky == -1) {
02273                 cpl_propertylist_update_string(drs,"ESO DRS SKYCOR",
02274                                                vircam_fits_get_fullname(ps.fsky));
02275             }           
02276             cpl_propertylist_set_comment(drs,"ESO DRS SKYCOR",
02277                                          "Sky correction method");
02278         }
02279     }
02280     for (i = 0; i < ps.noffsets; i++) {
02281         ff = ps.offsky_fits[i];
02282         fim = vircam_fits_get_image(ff);
02283         if (vircam_fits_get_status(ff) != VIR_FATAL) {
02284             drs = vircam_fits_get_ehu(ff);
02285             cpl_image_subtract(fim,skyimg);
02286             if (offsky == 0) {
02287                 cpl_propertylist_update_string(drs,"ESO DRS SKYCOR","internal");
02288             } else if (offsky == 1) {
02289                 cpl_propertylist_update_string(drs,"ESO DRS SKYCOR","offsky");
02290             } else if (offsky == -1) {
02291                 cpl_propertylist_update_string(drs,"ESO DRS SKYCOR",
02292                                                vircam_fits_get_fullname(ps.fsky));
02293             }           
02294             cpl_propertylist_set_comment(drs,"ESO DRS SKYCOR",
02295                                          "Image has been sky corrected");
02296         }
02297     }
02298 
02299     /* Clean up and get out of here */
02300 
02301     if (offsky == 0) 
02302         cpl_image_delete(skyimg);
02303     else if (offsky == 1)
02304         cpl_image_add_scalar(skyimg,med);
02305     return;
02306 }
02307 
02308 /*---------------------------------------------------------------------------*/
02324 /*---------------------------------------------------------------------------*/
02325 
02326 extern void vircam_jmp_get_readnoise_gain(int jext, float *readnoise, 
02327                                           float *gain) {
02328     cpl_propertylist *p_rg;
02329     const char *fctid = "vircam_jmp_get_readnoise_gain";
02330 
02331     /* Load the propertylist */
02332 
02333     p_rg = cpl_propertylist_load(cpl_frame_get_filename(ps.readgain_file),
02334                                  (cpl_size)jext);
02335 
02336     /* Check the readnoise property type and read it */
02337 
02338     switch (cpl_propertylist_get_type(p_rg,"ESO QC READNOISE")) {
02339     case CPL_TYPE_FLOAT:
02340         *readnoise = cpl_propertylist_get_float(p_rg,"ESO QC READNOISE");
02341         break;
02342     case CPL_TYPE_DOUBLE:
02343         *readnoise = (float)cpl_propertylist_get_double(p_rg,
02344                                                         "ESO QC READNOISE");
02345         break;
02346     default:
02347         cpl_error_reset();
02348         *readnoise = 25.0;
02349         cpl_msg_error(fctid,"Unable to get READNOISE estimate, guessing %g\n",
02350                       *readnoise);
02351     }
02352 
02353     /* Now the gain */
02354 
02355     switch (cpl_propertylist_get_type(p_rg,"ESO QC CONAD")) {
02356     case CPL_TYPE_FLOAT:
02357         *gain = cpl_propertylist_get_float(p_rg,"ESO QC CONAD");
02358         break;
02359     case CPL_TYPE_DOUBLE:
02360         *gain = (float)cpl_propertylist_get_double(p_rg,"ESO QC CONAD");
02361         break;
02362     default:
02363         cpl_error_reset();
02364         *gain = 1.0;
02365         cpl_msg_error(fctid,"Unable to get GAIN estimate, guessing %g\n",
02366                       *gain);
02367     }
02368     cpl_propertylist_delete(p_rg);
02369 }
02370 
02371 /*---------------------------------------------------------------------------*/
02386 /*---------------------------------------------------------------------------*/
02387 
02388 extern void vircam_jmp_illum(void) {
02389     int ngood,i,status,ii;
02390     float illcor_rms;
02391     vir_fits **ftmp,*ff,*ffc;
02392     char filt[32];
02393     cpl_table **mstds,*stdscat,*ms,*illcor,*ot;
02394     vir_tfits *outtab;
02395     cpl_propertylist **pl,*phu,*ehu;
02396     const char *fctid = "vircam_jmp_illum";
02397 
02398     /* Set some default values */
02399 
02400     ps.illcor = NULL;
02401     
02402     /* Sort out all images with good status */
02403 
02404     ftmp = cpl_malloc(ps.nscience*sizeof(vir_fits *));
02405     ngood = 0;
02406     for (i = 0; i < ps.nscience; i++) {
02407         ff = ps.sci_fits[i];
02408         if (vircam_fits_get_status(ff) != VIR_FATAL)
02409             ftmp[ngood++] = ff;
02410     }
02411 
02412     /* If there aren't any good images, then get out of here now */
02413 
02414     if (ngood == 0) {
02415         freespace(ftmp);
02416         cpl_msg_error(fctid,"Illumination correction impossible. No good science frames available");
02417         return;
02418     }
02419 
02420     /* What filter is this? */
02421 
02422     if (vircam_pfits_get_filter(vircam_fits_get_phu(ftmp[0]),filt) != VIR_OK) {
02423         cpl_msg_error(fctid,"No filter name in stack header");
02424         freespace(ftmp);
02425         return;
02426     }
02427 
02428     /* Get some workspace for the various arrays you need for the 
02429        illumination correction routine */
02430 
02431     mstds = cpl_malloc(ngood*sizeof(cpl_table *));
02432     for (i = 0; i < ngood; i++)
02433         mstds[i] = NULL;
02434     pl = cpl_malloc(ngood*sizeof(cpl_propertylist *));
02435     for (i = 0; i < ngood; i++)
02436         pl[i] = NULL;
02437 
02438     /* For each of the good input frames, do a catalogue generation and
02439        get some matched standards */
02440 
02441     ffc = ps.fconf;
02442     for (i = 0; i < ngood; i++) {
02443         status = VIR_OK;
02444         ff = ftmp[i];
02445         (void)vircam_imcore(ff,ffc,vircam_jmp_config.ipix,
02446                             1.5*vircam_jmp_config.threshold,0,
02447                             vircam_jmp_config.rcore,vircam_jmp_config.nbsize,2,
02448                             3.5,&outtab,&status);
02449         if (status != VIR_OK)
02450             cpl_error_reset();
02451         pl[i] = cpl_propertylist_duplicate(vircam_tfits_get_ehu(outtab));
02452 
02453         /* Get some standard stars */
02454 
02455         (void)vircam_getstds(vircam_fits_get_ehu(ff),1,current_catpath,
02456                              current_cat,&stdscat,&status);
02457         if (status == VIR_FATAL) {
02458             freetfits(outtab);
02459             freespace(ftmp);
02460             for (ii = 0; ii < ngood; ii++) {
02461                 freetable(mstds[ii]);
02462                 freepropertylist(pl[ii]);
02463             }
02464             freespace(mstds);
02465             freespace(pl);
02466             cpl_msg_error(fctid,"Illumination correction fails");
02467             return;
02468         } else if (status == VIR_WARN) {
02469             cpl_error_reset();
02470             freetfits(outtab);
02471             continue;
02472         }
02473 
02474         /* Now match this against the catalogue */
02475 
02476         ot = vircam_tfits_get_table(outtab);
02477         (void)vircam_matchstds(ot,stdscat,300.0,&ms,&status);
02478         if (status == VIR_FATAL) {
02479             freetable(stdscat);
02480             freetfits(outtab);
02481             freespace(ftmp);
02482             for (ii = 0; ii < ngood; ii++) {
02483                 freetable(mstds[ii]);
02484                 freepropertylist(pl[ii]);
02485             }
02486             freespace(mstds);
02487             freespace(pl);
02488             cpl_msg_error(fctid,"%s",cpl_error_get_message());
02489             return;
02490         }
02491         mstds[i] = ms;
02492         freetfits(outtab);
02493         freetable(stdscat);
02494     }
02495     
02496     /* Call the illumination routine */
02497 
02498     status = VIR_OK;
02499     (void)vircam_illum(ftmp,mstds,pl,ngood,filt,ps.tphottab,128,&illcor,
02500                        &illcor_rms,&status);
02501     
02502     /* Wrap the result */
02503     
02504     phu = cpl_propertylist_duplicate(vircam_fits_get_phu(ftmp[0]));
02505     ehu = cpl_propertylist_duplicate(vircam_fits_get_ehu(ftmp[0]));
02506     ps.illcor = vircam_tfits_wrap(illcor,NULL,phu,ehu);
02507     cpl_propertylist_update_float(ehu,"ESO QC ILLUMCOR_RMS",illcor_rms);
02508     cpl_propertylist_set_comment(ehu,"ESO QC ILLUMCOR_RMS",
02509                                  "RMS of illumination correction map");
02510     
02511     /* Tidy up */
02512 
02513     for (i = 0; i < ngood; i++) {
02514         freetable(mstds[i]);
02515         freepropertylist(pl[i]);
02516     }
02517     freespace(mstds);
02518     freespace(pl);
02519     freespace(ftmp);
02520 }
02521 
02522 /*---------------------------------------------------------------------------*/
02544 /*---------------------------------------------------------------------------*/
02545 
02546 static char *vircam_jmp_outfile(const char *bname, int ind, int isfits) {
02547     int nf;
02548     char *fname;
02549 
02550     /* Count up how much space you need for the output string. The 'isfits'
02551        block accounts for ".fits", an underscore and an EOS*/
02552 
02553     nf = strlen(bname);
02554     if (ind == 0) 
02555         nf++;
02556     else 
02557         nf += ((int)log10((double)ind)+1);
02558     if (isfits) 
02559         nf += 7;
02560     else
02561         nf += 2;
02562 
02563     /* Get the space for the filename */
02564 
02565     fname = cpl_malloc(nf);
02566 
02567     /* Now write the name */
02568 
02569     if (isfits) 
02570         (void)snprintf(fname,nf,"%s_%d.fits",bname,ind);
02571     else
02572         (void)snprintf(fname,nf,"%s_%d",bname,ind);
02573     return(fname);
02574 }
02575 
02576 /*---------------------------------------------------------------------------*/
02591 /*---------------------------------------------------------------------------*/
02592 
02593 extern void vircam_jmp_init(void) {
02594 
02595     /* Level 0 stuff */
02596 
02597     ps.labels = NULL;
02598     ps.master_dark = NULL;
02599     ps.master_twilight_flat = NULL;
02600     ps.master_conf = NULL;
02601     ps.master_sky = NULL;
02602     ps.mask = NULL;
02603     ps.chantab = NULL;
02604     ps.phottab = NULL;
02605     ps.tphottab = NULL;
02606     ps.readgain_file = NULL;
02607     ps.science_frames = NULL;
02608     ps.offset_skies = NULL;
02609     ps.product_frames_simple = NULL;
02610     ps.product_frames_simple_off = NULL;
02611     ps.product_frames_super = NULL;
02612     ps.product_frames_superc = NULL;
02613     ps.product_frame_stack = NULL;
02614     ps.product_frame_stackc = NULL;
02615     ps.product_frame_cat = NULL;
02616     ps.product_frame_illcor = NULL;
02617     ps.product_frame_sky = NULL;
02618     ps.phupaf = NULL;
02619     ps.gaincors = NULL;
02620     ps.catpath = NULL;
02621     ps.catname = NULL;
02622     ps.catpath2 = NULL;
02623     ps.catname2 = NULL;
02624 
02625     /* Level 1 stuff */
02626 
02627     ps.fdark = NULL;
02628     ps.fflat = NULL;
02629     ps.fconf = NULL;
02630     ps.fsky = NULL;
02631     ps.fchantab = NULL;
02632     ps.nscience = 0;
02633     ps.sci_fits = NULL;
02634     ps.noffsets = 0;
02635     ps.offsky_fits = NULL;
02636     ps.nustep_sets = 0;
02637     ps.ustep_sets = NULL;
02638     ps.ndith = 0;
02639     ps.ndithc = 0;
02640     ps.dith_input = NULL;
02641     ps.dithc_input = NULL;
02642     ps.stack_frame = NULL;
02643     ps.stackc_frame = NULL;
02644     ps.outcat = NULL;
02645     ps.outsky = NULL;
02646     ps.illcor = NULL;
02647 }
02648 
02649 /*---------------------------------------------------------------------------*/
02670 /*---------------------------------------------------------------------------*/
02671 
02672 extern void vircam_jmp_tidy(int level) {
02673     int i;
02674 
02675     /* Level 1 stuff */
02676 
02677     freefits(ps.fdark); 
02678     freefits(ps.fflat); 
02679     freefits(ps.fconf);
02680     freefits(ps.fsky);
02681     freetfits(ps.fchantab);
02682     freefitslist(ps.sci_fits,ps.nscience);
02683     freefitslist(ps.offsky_fits,ps.noffsets);
02684     ps.nscience = 0;
02685     for (i = 0; i < ps.nustep_sets; i++) {
02686         freespace(ps.ustep_sets[i].f);
02687         freefits(ps.ustep_sets[i].super);
02688         freefits(ps.ustep_sets[i].superc);
02689     }
02690     freespace(ps.ustep_sets);
02691     ps.nustep_sets = 0;
02692     freespace(ps.dith_input);
02693     ps.ndith = 0;
02694     freespace(ps.dithc_input);
02695     ps.ndithc = 0;
02696 
02697     freefits(ps.stack_frame);
02698     freefits(ps.stackc_frame);
02699     freetfits(ps.outcat);
02700     freetable(ps.matchstds);
02701     freetfits(ps.illcor);
02702     freefits(ps.outsky);
02703 
02704     if (level == 1)
02705         return;
02706     
02707     /* Level 0 stuff */
02708 
02709     freespace(ps.labels);
02710     freeframe(ps.master_dark);
02711     freeframe(ps.master_twilight_flat);
02712     freeframe(ps.master_conf);
02713     freeframe(ps.master_sky);
02714     freemask(ps.mask);
02715     freeframe(ps.chantab);
02716     freeframe(ps.phottab);
02717     freeframe(ps.readgain_file);
02718     freetable(ps.tphottab);
02719     freeframeset(ps.science_frames);
02720     freeframeset(ps.offset_skies);
02721     freepropertylist(ps.phupaf);
02722     freespace(ps.product_frames_simple);     /* NB: We only have to delete */
02723     freespace(ps.product_frames_super);      /* the arrays and not the frames */
02724     freespace(ps.product_frames_superc);     /* as these get passed back */
02725     freespace(ps.product_frames_simple_off); /* to esorex */
02726     freespace(ps.gaincors);
02727     freespace(ps.catpath);
02728     freespace(ps.catname);
02729     freespace(ps.catpath2);
02730     freespace(ps.catname2);
02731 }
02732 
02735 /*
02736 
02737 $Log: vircam_jmp_utils.c,v $
02738 Revision 1.57  2012/01/15 17:40:09  jim
02739 Minor modifications to take into accout the changes in cpl API for v6
02740 
02741 Revision 1.56  2010/09/13 11:50:18  jim
02742 removed unnecessary declaration
02743 
02744 Revision 1.55  2010/09/13 11:40:33  jim
02745 Fixed the way DRS SKYCOR is written to header
02746 
02747 Revision 1.54  2010/09/10 11:24:35  jim
02748 Modified skycor so that if a master sky exists, then it uses it rather than
02749 create a new one.
02750 
02751 Revision 1.53  2010/06/07 12:42:40  jim
02752 Modifications to get rid of compiler gripes
02753 
02754 Revision 1.52  2010/06/03 11:33:29  jim
02755 Opened up matching radius in call to vircam_matchxy to deal with the case
02756 where the input WCS is poorly defined.
02757 
02758 Revision 1.51  2010/02/08 16:35:26  jim
02759 Moved the definition of propertylist ps.phupaf to the recipes so that
02760 this doesn't fail when we don't save the simple images
02761 
02762 Revision 1.50  2010/02/08 10:50:09  jim
02763 Fixed GAIN -> CONAD bug
02764 
02765 Revision 1.49  2010/01/31 19:28:46  jim
02766 SKYMED and SKYSIG are moved from DRS to QC in offset skies
02767 
02768 Revision 1.48  2009/11/18 21:08:46  jim
02769 Mods made to wcsfit to restrict standards to those with good photometry and to
02770 limit the number of entries in catalogue to use in matching up with the
02771 standards
02772 
02773 Revision 1.47  2009/09/22 12:29:12  jim
02774 Modified to do offset sky exposures
02775 
02776 Revision 1.46  2009/09/21 14:45:54  jim
02777 Fixed bug in _save_simple where template frame wasn't being defined for
02778 each image extension
02779 
02780 Revision 1.45  2009/09/09 09:46:33  jim
02781 modified in attempt to get headers right
02782 
02783 Revision 1.44  2008/12/09 11:36:45  jim
02784 Fixed _illcor routine so that if the imcore fails the cpl error is reset
02785 
02786 Revision 1.43  2008/12/08 13:04:24  jim
02787 Fixed PRO CATG for catalogue and illumination correction pafs
02788 
02789 Revision 1.42  2008/12/08 06:41:18  jim
02790 Fixed problem where wrong PRO CATG was being written to pafs
02791 
02792 Revision 1.41  2008/11/27 09:13:47  jim
02793 Changed PRO CATG values for interleaved confidence maps so they are different
02794 for science and standard recipes
02795 
02796 Revision 1.40  2008/11/25 18:54:45  jim
02797 removed extra sky stuff
02798 
02799 Revision 1.39  2008/11/25 11:56:38  jim
02800 Routine skycor now allows for either standard or masked object sky correction.
02801 Modification was done to routine which saves superframes to define the
02802 output confidence map as officially a different type from the confidence
02803 maps generated from jittering.
02804 
02805 Revision 1.38  2008/11/21 10:10:06  jim
02806 Patched in newer version of vircam_matchxy
02807 
02808 Revision 1.37  2008/09/30 11:35:01  jim
02809 Added PRO CATG to the pafs
02810 
02811 Revision 1.36  2008/09/29 11:29:46  jim
02812 Some cosmetic fixes. Also define a maximum shift between dither frames
02813 
02814 Revision 1.35  2008/08/28 09:05:37  jim
02815 Fixed bug where QC was being duplicated from master BPM on rare occasions.
02816 Fixed bug where ARCFILE wasn't being written to the paf file for illum_cor
02817 tables. Sky combine is done with medians
02818 
02819 Revision 1.34  2008/08/05 14:07:01  jim
02820 Relaxed matching criteria
02821 
02822 Revision 1.33  2008/07/10 13:05:53  jim
02823 Modified to use v4.2 version of cpl_wcs
02824 
02825 Revision 1.32  2008/06/20 11:13:35  jim
02826 Fixed dodgy call to cpl_wcs_get_image_dims
02827 
02828 Revision 1.31  2008/05/06 08:40:10  jim
02829 Modified to use cpl_wcs interface
02830 
02831 Revision 1.30  2007/11/22 12:34:08  jim
02832 Added line to vircam_jmp_save_illum to make sure that phupaf is defined
02833 when _save_simple isn't called
02834 
02835 Revision 1.29  2007/10/25 17:34:00  jim
02836 Modified to remove lint warnings
02837 
02838 Revision 1.28  2007/10/19 09:25:10  jim
02839 Fixed problems with missing includes
02840 
02841 Revision 1.27  2007/10/19 06:55:06  jim
02842 Modifications made to use new method for directing the recipes to the
02843 standard catalogues using the sof
02844 
02845 Revision 1.26  2007/10/15 12:50:28  jim
02846 Modified for compatibility with cpl_4.0
02847 
02848 Revision 1.25  2007/06/13 08:10:49  jim
02849 Modified to allow for different output file names depending upon the calling
02850 recipe
02851 
02852 Revision 1.24  2007/05/15 08:54:07  jim
02853 Fixed small bug in stack_save and dither_offsets. Also modified dither_offsets
02854 to just send back zeros if there is only one dither frame
02855 
02856 Revision 1.23  2007/05/08 10:41:49  jim
02857 Added gaincor variables
02858 
02859 Revision 1.22  2007/05/02 09:15:37  jim
02860 Modified to use new api for vircam_imcore and vircam_platesol
02861 
02862 Revision 1.21  2007/04/30 09:40:01  jim
02863 Added vircam_paf_append
02864 
02865 Revision 1.20  2007/04/13 12:29:11  jim
02866 Fixed bug in save_simple which mean that ps.pafphu was allocated for every
02867 file rather than just the first one
02868 
02869 Revision 1.19  2007/04/04 16:05:59  jim
02870 Modified to make paf information a bit more correct
02871 
02872 Revision 1.18  2007/04/04 10:34:55  jim
02873 Modified to use new dfs tags
02874 
02875 Revision 1.17  2007/03/14 22:08:54  jim
02876 Fixed typo
02877 
02878 Revision 1.16  2007/03/14 14:49:13  jim
02879 Fixed problem with missing paf files in jmp recipes if detlive = F. Also
02880 fixed problem where extra dummy products were being created
02881 
02882 Revision 1.15  2007/03/13 09:50:53  jim
02883 Fixed bug in vircam_jmp_save_illum where PAF wasn't being saved for the
02884 first extension
02885 
02886 Revision 1.14  2007/03/06 14:06:24  jim
02887 Fixed missing paf write
02888 
02889 Revision 1.13  2007/03/06 13:49:48  jim
02890 Created static routine vircam_jmp_outfile to create a 'predictable'
02891 output filename for each product
02892 
02893 Revision 1.12  2007/03/01 12:42:42  jim
02894 Modified slightly after code checking
02895 
02896 Revision 1.11  2007/02/25 06:34:20  jim
02897 Plugged memory leak
02898 
02899 Revision 1.10  2007/02/15 06:59:38  jim
02900 Added ability to write QC paf files
02901 
02902 Revision 1.9  2007/02/06 13:11:12  jim
02903 Fixed entry for PRO dictionary in cpl_dfs_set_product_header
02904 
02905 Revision 1.8  2007/01/17 23:54:00  jim
02906 Plugged some memory leaks
02907 
02908 Revision 1.7  2006/12/19 13:30:01  jim
02909 Fixed vircam_jmp_illum to initialise pointers
02910 
02911 Revision 1.6  2006/12/18 12:51:20  jim
02912 Tightened up some of the error reporting
02913 
02914 Revision 1.5  2006/12/15 09:58:28  jim
02915 read noise and gain keywords were wrong...
02916 
02917 Revision 1.4  2006/11/29 12:28:45  jim
02918 Modified so that the correct recipe names would appear in the headers of
02919 data products
02920 
02921 Revision 1.3  2006/11/28 22:10:22  jim
02922 Added illcor_rms to calling sequence of vircam_illum
02923 
02924 Revision 1.2  2006/11/28 20:56:31  jim
02925 Added vircam_jmp_illum and vircam_jmp_save_illum
02926 
02927 Revision 1.1  2006/11/27 11:54:37  jim
02928 Initial entry
02929 
02930 
02931 */
02932 

Generated on 5 Mar 2013 for VIRCAM Pipeline by  doxygen 1.6.1