VIRCAM Pipeline  2.3.10
vircam_jmp_utils.c
1 /* $Id: vircam_jmp_utils.c,v 1.57 2012-01-15 17:40:09 jim Exp $
2  *
3  * This file is part of the VIRCAM Pipeline
4  * Copyright (C) 2015 European Southern Observatory
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  */
20 
21 
22 /* $Author: jim $
23  * $Date: 2012-01-15 17:40:09 $
24  * $Revision: 1.57 $
25  * $Name: not supported by cvs2svn $
26  */
27 
28 /* Includes */
29 
30 #ifdef HAVE_CONFIG_H
31 #include <config.h>
32 #endif
33 
34 #include <cpl.h>
35 #include <math.h>
36 #include <string.h>
37 
38 #include <casu_utils.h>
39 #include <casu_mods.h>
40 #include <casu_stats.h>
41 #include <casu_wcsutils.h>
42 #include <casu_sky.h>
43 
44 #include "vircam_utils.h"
45 #include "vircam_pfits.h"
46 #include "vircam_dfs.h"
47 #include "vircam_mods.h"
48 #include "vircam_paf.h"
49 
50 #include "vircam_jmp_utils.h"
51 
52 struct vircam_jmp_configStruct vircam_jmp_config;
53 int offsky=0;
54 
55 static char *vircam_jmp_outfile(const char *bname, int ind, int isfits);
56 
71 void vircam_jmp_set_config_ipix (int ipix) {vircam_jmp_config.ipix = ipix ;}
72 void vircam_jmp_set_config_icrowd (int icrowd) {vircam_jmp_config.icrowd = icrowd ;}
73 void vircam_jmp_set_config_rcore (float rcore) {vircam_jmp_config.rcore = rcore ;}
74 void vircam_jmp_set_config_threshold (float threshold) {vircam_jmp_config.threshold = threshold ;}
75 void vircam_jmp_set_config_nbsize (int nbsize) {vircam_jmp_config.nbsize = nbsize ;}
76 void vircam_jmp_set_config_savecat (int savecat) {vircam_jmp_config.savecat = savecat ;}
77 void vircam_jmp_set_config_destripe (int destripe) {vircam_jmp_config.destripe = destripe ;}
78 void vircam_jmp_set_config_skycor (int skycore) {vircam_jmp_config.skycor = skycore ;}
79 void vircam_jmp_set_config_savesimple (int savesimple) {vircam_jmp_config.savesimple= savesimple;}
80 void vircam_jmp_set_config_extenum (int extenum) {vircam_jmp_config.extenum = extenum ;}
81 void vircam_jmp_set_offsky(int inp_offsky) {offsky=inp_offsky;}
82 void vircam_jmp_set_isfirst(int inp_isfirst) {isfirst=inp_isfirst;}
83 
84 int vircam_jmp_get_config_savecat () {return vircam_jmp_config.savecat ;}
85 int vircam_jmp_get_config_destripe () {return vircam_jmp_config.destripe ;}
86 int vircam_jmp_get_config_skycor () {return vircam_jmp_config.skycor ;}
87 int vircam_jmp_get_config_savesimple () {return vircam_jmp_config.savesimple;}
88 int vircam_jmp_get_config_extenum () {return vircam_jmp_config.extenum ;}
89 
90 
91 void vircam_set_recipename(const char* recipename,int recipe_flag) {
92  (void)strncpy(vircam_recipename,recipename,VIRCAM_PATHSZ);
93  (void)snprintf(vircam_recipepaf,VIRCAM_PATHSZ,"VIRCAM/%s",recipename);
94  recflag = recipe_flag;
95 
96 }
97 
98 /*---------------------------------------------------------------------------*/
117 /*---------------------------------------------------------------------------*/
118 
119 extern int vircam_jmp_save_simple(struct psStruct* ps, cpl_frameset *framelist,
120  cpl_parameterlist *parlist) {
121  cpl_propertylist *plist;
122  int i,isdummy;
123  cpl_frame *product_frame,*template;
124  char *fname;
125  const char *base[] = {"","simple_jmp","simple_std","simple_mes"};
126  const char *fctid = "vircam_jmp_save_simple";
127 
128  /* Initialise the product frameset */
129 
130  if ((*ps).product_frames_simple == NULL)
131  (*ps).product_frames_simple = cpl_malloc((*ps).nscience*sizeof(cpl_frame *));
132 
133  /* Loop for each of the frames in the ustep sequence */
134 
135  for (i = 0; i < (*ps).nscience; i++) {
136  fname = vircam_jmp_outfile(base[recflag],i,1);
137  isdummy = (casu_fits_get_status((*ps).sci_fits[i]) != CASU_OK);
138  template = cpl_frameset_get_position((*ps).science_frames,i);
139 
140  /* If we need to make a PHU then do that now based on the first frame
141  in the input frame list */
142 
143  if (isfirst) {
144 
145  /* Create a new product frame object and define some tags */
146 
147  product_frame = cpl_frame_new();
148  cpl_frame_set_filename(product_frame,fname);
149  switch (recflag) {
150  case RECSCI:
151  cpl_frame_set_tag(product_frame,VIRCAM_PRO_SIMPLE_SCI);
152  break;
153  case RECSTD:
154  cpl_frame_set_tag(product_frame,VIRCAM_PRO_SIMPLE_STD);
155  break;
156  default:
157  cpl_frame_set_tag(product_frame,VIRCAM_PRO_SIMPLE_SCI);
158  break;
159  }
160  cpl_frame_set_type(product_frame,CPL_FRAME_TYPE_IMAGE);
161  cpl_frame_set_group(product_frame,CPL_FRAME_GROUP_PRODUCT);
162  cpl_frame_set_level(product_frame,CPL_FRAME_LEVEL_FINAL);
163 
164  /* Set up the PHU header */
165 
166  plist = casu_fits_get_phu((*ps).sci_fits[i]);
167  vircam_dfs_set_product_primary_header(plist,product_frame,
168  framelist,parlist,
169  vircam_recipename,
170  "PRO-1.15",template,1);
171 
172  /* 'Save' the PHU image */
173 
174  if (cpl_image_save(NULL,fname,CPL_TYPE_UCHAR,plist,
175  CPL_IO_DEFAULT) != CPL_ERROR_NONE) {
176  cpl_msg_error(fctid,"Cannot save product PHU");
177  cpl_frame_delete(product_frame);
178  freespace(fname);
179  return(-1);
180  }
181  cpl_frameset_insert(framelist,product_frame);
182  (*ps).product_frames_simple[i] = product_frame;
183  }
184 
185  /* Get the extension property list */
186 
187  plist = casu_fits_get_ehu((*ps).sci_fits[i]);
188  if (isdummy)
189  casu_dummy_property(plist);
190 
191  /* Fiddle with the header now */
192 
193  product_frame = (*ps).product_frames_simple[i];
194  vircam_dfs_set_product_exten_header(plist,product_frame,framelist,
195  parlist,vircam_recipename,
196  "PRO-1.15",template);
197  if (cpl_image_save(casu_fits_get_image((*ps).sci_fits[i]),fname,
198  CPL_TYPE_FLOAT,plist,
199  CPL_IO_EXTEND) != CPL_ERROR_NONE) {
200  cpl_msg_error(fctid,"Cannot save product image extension");
201  freespace(fname);
202  return(-1);
203  }
204 
205  /* Clear the allocated workspace */
206 
207  freespace(fname);
208  }
209 
210  /* Get out of here */
211 
212  return(0);
213 }
214 
215 /*---------------------------------------------------------------------------*/
234 /*---------------------------------------------------------------------------*/
235 
236 extern int vircam_jmp_save_simple_offsets(struct psStruct* ps, cpl_frameset *framelist,
237  cpl_parameterlist *parlist) {
238  cpl_propertylist *plist;
239  int i,isdummy;
240  cpl_frame *product_frame,*template;
241  char *fname;
242  const char *base = "simple_off";
243  const char *fctid = "vircam_jmp_save_simple_offsets";
244 
245  /* Initialise the product frameset */
246 
247  if ((*ps).product_frames_simple_off == NULL)
248  (*ps).product_frames_simple_off =
249  cpl_malloc((*ps).noffsets*sizeof(cpl_frame *));
250 
251  /* Loop for each of the frames in the ustep sequence */
252 
253  for (i = 0; i < (*ps).noffsets; i++) {
254  fname = vircam_jmp_outfile(base,i,1);
255  isdummy = (casu_fits_get_status((*ps).offsky_fits[i]) != CASU_OK);
256  template = cpl_frameset_get_position((*ps).offset_skies,i);
257 
258  /* If we need to make a PHU then do that now based on the first frame
259  in the input frame list */
260 
261  if (isfirst) {
262 
263  /* Create a new product frame object and define some tags */
264 
265  product_frame = cpl_frame_new();
266  cpl_frame_set_filename(product_frame,fname);
267  cpl_frame_set_tag(product_frame,VIRCAM_PRO_SIMPLE_SKY);
268  cpl_frame_set_type(product_frame,CPL_FRAME_TYPE_IMAGE);
269  cpl_frame_set_group(product_frame,CPL_FRAME_GROUP_PRODUCT);
270  cpl_frame_set_level(product_frame,CPL_FRAME_LEVEL_FINAL);
271 
272  /* Set up the PHU header */
273 
274  plist = casu_fits_get_phu((*ps).offsky_fits[i]);
275  vircam_dfs_set_product_primary_header(plist,product_frame,
276  framelist,parlist,
277  vircam_recipename,
278  "PRO-1.15",template,1);
279 
280  /* 'Save' the PHU image */
281 
282  if (cpl_image_save(NULL,fname,CPL_TYPE_UCHAR,plist,
283  CPL_IO_DEFAULT) != CPL_ERROR_NONE) {
284  cpl_msg_error(fctid,"Cannot save product PHU");
285  cpl_frame_delete(product_frame);
286  freespace(fname);
287  return(-1);
288  }
289  cpl_frameset_insert(framelist,product_frame);
290  (*ps).product_frames_simple_off[i] = product_frame;
291  }
292 
293  /* Get the extension property list */
294 
295  plist = casu_fits_get_ehu((*ps).offsky_fits[i]);
296  if (isdummy)
297  casu_dummy_property(plist);
298 
299  /* Fiddle with the header now */
300 
301  product_frame = (*ps).product_frames_simple_off[i];
302  vircam_dfs_set_product_exten_header(plist,product_frame,framelist,
303  parlist,vircam_recipename,
304  "PRO-1.15",template);
305  if (cpl_image_save(casu_fits_get_image((*ps).offsky_fits[i]),fname,
306  CPL_TYPE_FLOAT,plist,
307  CPL_IO_EXTEND) != CPL_ERROR_NONE) {
308  cpl_msg_error(fctid,"Cannot save product image extension");
309  freespace(fname);
310  return(-1);
311  }
312 
313  /* Clear the allocated workspace */
314 
315  freespace(fname);
316  }
317 
318  /* Get out of here */
319 
320  return(0);
321 }
322 
323 /*---------------------------------------------------------------------------*/
342 /*---------------------------------------------------------------------------*/
343 
344 extern int vircam_jmp_save_offsky(struct psStruct* ps, cpl_frameset *framelist,
345  cpl_parameterlist *parlist) {
346  cpl_propertylist *p;
347  int isdummy;
348  casu_fits *ff;
349  cpl_image *fim;
350  cpl_frame *product_frame,*template;
351  char *fname;
352  const char *base = "offsky";
353  const char *fctid = "vircam_jmp_save_offsky";
354 
355  /* Work out which frame to base the output on. If this particular
356  sequence failed for whatever reason, there will be a dummy sky frame. */
357 
358  ff = (*ps).outsky;
359  fim = casu_fits_get_image(ff);
360  isdummy = (casu_fits_get_status(ff) != CASU_OK);
361  template = cpl_frameset_get_position((*ps).offset_skies,0);
362 
363  /* Create some names */
364 
365  fname = vircam_jmp_outfile(base,0,1);
366 
367  /* If we need to make a PHU then do that now based on the first frame
368  in the input frame list */
369 
370  if (isfirst) {
371 
372  /* Create a new product frame object and define some tags */
373 
374  product_frame = cpl_frame_new();
375  cpl_frame_set_filename(product_frame,fname);
376  cpl_frame_set_tag(product_frame,VIRCAM_PRO_OFFSET_SKY);
377  cpl_frame_set_type(product_frame,CPL_FRAME_TYPE_IMAGE);
378  cpl_frame_set_group(product_frame,CPL_FRAME_GROUP_PRODUCT);
379  cpl_frame_set_level(product_frame,CPL_FRAME_LEVEL_FINAL);
380  (*ps).product_frame_sky = product_frame;
381 
382  /* Set up the PHU header. */
383 
384  p = casu_fits_get_phu(ff);
385  vircam_dfs_set_product_primary_header(p,product_frame,framelist,
386  parlist,vircam_recipename,
387  "PRO-1.15",template,0);
388 
389  /* 'Save' the PHU image */
390 
391  if (cpl_image_save(NULL,fname,CPL_TYPE_UCHAR,p,
392  CPL_IO_DEFAULT) != CPL_ERROR_NONE) {
393  cpl_msg_error(fctid,"Cannot save product PHU");
394  cpl_frame_delete(product_frame);
395  freespace(fname);
396  return(-1);
397  }
398  cpl_frameset_insert(framelist,product_frame);
399  }
400 
401  /* Get the extension property list */
402 
403  p = cpl_propertylist_duplicate(casu_fits_get_ehu(ff));
404  if (isdummy)
406 
407  /* Fiddle with the header now */
408 
409  product_frame = (*ps).product_frame_sky;
410  vircam_dfs_set_product_exten_header(p,product_frame,framelist,parlist,
411  vircam_recipename,"PRO-1.15",template);
412  if (cpl_image_save(fim,fname,CPL_TYPE_FLOAT,p,CPL_IO_EXTEND) !=
413  CPL_ERROR_NONE) {
414  cpl_msg_error(fctid,"Cannot save product image extension");
415  freespace(fname);
416  return(-1);
417  }
418 
419  /* Quick tidy */
420 
421  cpl_propertylist_delete(p);
422  freespace(fname);
423 
424  /* Get out of here */
425 
426  return(0);
427 }
428 
429 
430 
431 /*---------------------------------------------------------------------------*/
450 /*---------------------------------------------------------------------------*/
451 
452 extern int vircam_jmp_save_super(struct psStruct* ps, cpl_frameset *framelist,
453  cpl_parameterlist *parlist) {
454  cpl_propertylist *plist,*p;
455  int i,isdummy,isdummyc,j,jref;
456  cpl_frame *product_frame,*template;
457  char *fname,*fnamec;
458  const char *base[] = {"","super_jmp","super_std","super_mes"};
459  const char *basec[] = {"","superc_jmp","superc_std","superc_mes"};
460  casu_fits *ff,*ffc;
461  cpl_image *fim,*fimc;
462  const char *fctid = "vircam_jmp_save_super";
463 
464  /* Initialise the product frameset */
465 
466  if ((*ps).product_frames_super == NULL)
467  (*ps).product_frames_super = cpl_malloc((*ps).nustep_sets*sizeof(cpl_frame *));
468  if ((*ps).product_frames_superc == NULL)
469  (*ps).product_frames_superc = cpl_malloc((*ps).nustep_sets*sizeof(cpl_frame *));
470 
471  /* Loop for each of the ustep sequences */
472 
473  for (i = 0; i < (*ps).nustep_sets; i++) {
474 
475  /* Work out which frame to base the output on. If this particular
476  microstep sequence failed for whatever reason, there will be
477  a dummy super frame. */
478 
479  ff = (*ps).ustep_sets[i].super;
480  fim = casu_fits_get_image(ff);
481  ffc = (*ps).ustep_sets[i].superc;
482  fimc = casu_fits_get_image(ffc);
483  isdummy = (casu_fits_get_status(ff) != CASU_OK);
484  isdummyc = (casu_fits_get_status(ffc) != CASU_OK);
485 
486  /* Create some names */
487 
488  fname = vircam_jmp_outfile(base[recflag],i,1);
489  fnamec = vircam_jmp_outfile(basec[recflag],i,1);
490 
491  /* Get a reference to the first frame in this set */
492 
493  jref = -1;
494  for (j = 0; j < (*ps).ustep_sets[i].nframes; j++) {
495  if ((*ps).sci_fits[j] == (*ps).ustep_sets[i].f[j]) {
496  jref = j;
497  break;
498  }
499  }
500  if (jref != -1)
501  template = cpl_frameset_get_position((*ps).science_frames,jref);
502  else
503  template = NULL;
504 
505  /* If we need to make a PHU then do that now based on the first frame
506  in the input frame list */
507 
508  if (isfirst) {
509 
510  /* Create a new product frame object and define some tags */
511 
512  product_frame = cpl_frame_new();
513  cpl_frame_set_filename(product_frame,fname);
514  switch (recflag) {
515  case RECSCI:
516  cpl_frame_set_tag(product_frame,VIRCAM_PRO_INTER_SCI);
517  break;
518  case RECSTD:
519  cpl_frame_set_tag(product_frame,VIRCAM_PRO_INTER_STD);
520  break;
521  default:
522  cpl_frame_set_tag(product_frame,VIRCAM_PRO_INTER_SCI);
523  break;
524  }
525  cpl_frame_set_type(product_frame,CPL_FRAME_TYPE_IMAGE);
526  cpl_frame_set_group(product_frame,CPL_FRAME_GROUP_PRODUCT);
527  cpl_frame_set_level(product_frame,CPL_FRAME_LEVEL_FINAL);
528 
529  /* Set up the PHU header. */
530 
531  plist = casu_fits_get_phu(ff);
532  vircam_dfs_set_product_primary_header(plist,product_frame,
533  framelist,parlist,
534  vircam_recipename,
535  "PRO-1.15",template,1);
536 
537  /* 'Save' the PHU image */
538 
539  if (cpl_image_save(NULL,fname,CPL_TYPE_UCHAR,plist,
540  CPL_IO_DEFAULT) != CPL_ERROR_NONE) {
541  cpl_msg_error(fctid,"Cannot save product PHU");
542  cpl_frame_delete(product_frame);
543  freespace(fname);
544  freespace(fnamec);
545  return(-1);
546  }
547  cpl_frameset_insert(framelist,product_frame);
548  (*ps).product_frames_super[i] = product_frame;
549 
550  /* Now do exactly the same thing for the confidence map */
551 
552  product_frame = cpl_frame_new();
553  cpl_frame_set_filename(product_frame,fnamec);
554  switch (recflag) {
555  case RECSCI:
556  cpl_frame_set_tag(product_frame,VIRCAM_PRO_CONF_INTER_SCI);
557  break;
558  case RECSTD:
559  cpl_frame_set_tag(product_frame,VIRCAM_PRO_CONF_INTER_STD);
560  break;
561  default:
562  cpl_frame_set_tag(product_frame,VIRCAM_PRO_CONF_INTER_SCI);
563  break;
564  }
565  cpl_frame_set_type(product_frame,CPL_FRAME_TYPE_IMAGE);
566  cpl_frame_set_group(product_frame,CPL_FRAME_GROUP_PRODUCT);
567  cpl_frame_set_level(product_frame,CPL_FRAME_LEVEL_FINAL);
568 
569  /* Set up the PHU header. */
570 
571  plist = casu_fits_get_phu(ffc);
572  vircam_dfs_set_product_primary_header(plist,product_frame,
573  framelist,parlist,
574  vircam_recipename,
575  "PRO-1.15",template,1);
576 
577  /* 'Save' the PHU image */
578 
579  if (cpl_image_save(NULL,fnamec,CPL_TYPE_UCHAR,plist,
580  CPL_IO_DEFAULT) != CPL_ERROR_NONE) {
581  cpl_msg_error(fctid,"Cannot save product PHU");
582  cpl_frame_delete(product_frame);
583  freespace(fname);
584  freespace(fnamec);
585  return(-1);
586  }
587  cpl_frameset_insert(framelist,product_frame);
588  (*ps).product_frames_superc[i] = product_frame;
589  }
590 
591 
592  /* Get the extension property list */
593 
594  p = cpl_propertylist_duplicate(casu_fits_get_ehu(ff));
595  if (isdummy)
597 
598  /* Fiddle with the header now */
599 
600  product_frame = (*ps).product_frames_super[i];
601  vircam_dfs_set_product_exten_header(p,product_frame,framelist,parlist,
602  vircam_recipename,"PRO-1.15",
603  template);
604  if (cpl_image_save(fim,fname,CPL_TYPE_FLOAT,p,CPL_IO_EXTEND) !=
605  CPL_ERROR_NONE) {
606  cpl_msg_error(fctid,"Cannot save product image extension");
607  freespace(fname);
608  freespace(fnamec);
609  return(-1);
610  }
611  cpl_propertylist_delete(p);
612 
613  /* And now for the confidence map */
614 
615  p = cpl_propertylist_duplicate(casu_fits_get_ehu(ffc));
616  if (isdummyc)
618 
619  /* Fiddle with the header now */
620 
621  product_frame = (*ps).product_frames_superc[i];
622  vircam_dfs_set_product_exten_header(p,product_frame,framelist,parlist,
623  vircam_recipename,"PRO-1.15",
624  template);
625  if (cpl_image_save(fimc,fnamec,CPL_TYPE_SHORT,p,CPL_IO_EXTEND) !=
626  CPL_ERROR_NONE) {
627  cpl_msg_error(fctid,"Cannot save confidence map image extension");
628  freespace(fname);
629  freespace(fnamec);
630  return(-1);
631  }
632  cpl_propertylist_delete(p);
633 
634  /* Clear the allocated workspace */
635 
636  freespace(fname);
637  freespace(fnamec);
638  }
639 
640  /* Get out of here */
641 
642  return(0);
643 }
644 
645 /*---------------------------------------------------------------------------*/
664 /*---------------------------------------------------------------------------*/
665 
666 extern int vircam_jmp_save_stack(struct psStruct* ps, cpl_frameset *framelist,
667  cpl_parameterlist *parlist,
668  cpl_propertylist *dummyqcprops) {
669  cpl_propertylist *plist,*p,*pafprop;
670  int isdummy,isdummyc;
671  casu_fits *ff,*ffc;
672  cpl_image *fim,*fimc;
673  cpl_frame *product_frame;
674  char *fname,*fnamec,*fnamepaf;
675  const char *base[] = {"","stack_jmp","stack_std","stack_mes"};
676  const char *basec[] = {"","stackc_jmp","stackc_std","stackc_mes"};
677  const char *fctid = "vircam_jmp_save_stack";
678 
679  /* Work out which frame to base the output on. If this particular
680  jitter sequence failed for whatever reason, there will be
681  a dummy stack frame. */
682 
683  ff = (*ps).stack_frame;
684  fim = casu_fits_get_image(ff);
685  ffc = (*ps).stackc_frame;
686  fimc = casu_fits_get_image(ffc);
687  isdummy = (casu_fits_get_status(ff) != CASU_OK);
688  isdummyc = (casu_fits_get_status(ffc) != CASU_OK);
689 
690  /* Create some names */
691 
692  fname = vircam_jmp_outfile(base[recflag],0,1);
693  fnamec = vircam_jmp_outfile(basec[recflag],0,1);
694  fnamepaf = vircam_jmp_outfile(base[recflag],0,0);
695 
696  /* If we need to make a PHU then do that now based on the first frame
697  in the input frame list */
698 
699  if (isfirst) {
700 
701  /* Create a new product frame object and define some tags */
702 
703  product_frame = cpl_frame_new();
704  cpl_frame_set_filename(product_frame,fname);
705  switch (recflag) {
706  case RECSCI:
707  cpl_frame_set_tag(product_frame,VIRCAM_PRO_JITTERED_SCI);
708  break;
709  case RECSTD:
710  cpl_frame_set_tag(product_frame,VIRCAM_PRO_JITTERED_STD);
711  break;
712  default:
713  cpl_frame_set_tag(product_frame,VIRCAM_PRO_JITTERED_SCI);
714  break;
715  }
716  cpl_frame_set_type(product_frame,CPL_FRAME_TYPE_IMAGE);
717  cpl_frame_set_group(product_frame,CPL_FRAME_GROUP_PRODUCT);
718  cpl_frame_set_level(product_frame,CPL_FRAME_LEVEL_FINAL);
719  (*ps).product_frame_stack = product_frame;
720 
721  /* Set up the PHU header. */
722 
723  plist = casu_fits_get_phu(ff);
724  vircam_dfs_set_product_primary_header(plist,product_frame,framelist,
725  parlist,vircam_recipename,
726  "PRO-1.15",NULL,0);
727 
728  /* 'Save' the PHU image */
729 
730  if (cpl_image_save(NULL,fname,CPL_TYPE_UCHAR,plist,
731  CPL_IO_DEFAULT) != CPL_ERROR_NONE) {
732  cpl_msg_error(fctid,"Cannot save product PHU");
733  cpl_frame_delete(product_frame);
734  freespace(fname);
735  freespace(fnamec);
736  freespace(fnamepaf);
737  return(-1);
738  }
739  cpl_frameset_insert(framelist,product_frame);
740 
741  /* Now do exactly the same thing for the confidence map */
742 
743  product_frame = cpl_frame_new();
744  cpl_frame_set_filename(product_frame,fnamec);
745  switch (recflag) {
746  case RECSCI:
747  cpl_frame_set_tag(product_frame,VIRCAM_PRO_CONF_SCI);
748  break;
749  case RECSTD:
750  cpl_frame_set_tag(product_frame,VIRCAM_PRO_CONF_STD);
751  break;
752  default:
753  cpl_frame_set_tag(product_frame,VIRCAM_PRO_CONF_SCI);
754  break;
755  }
756  cpl_frame_set_type(product_frame,CPL_FRAME_TYPE_IMAGE);
757  cpl_frame_set_group(product_frame,CPL_FRAME_GROUP_PRODUCT);
758  cpl_frame_set_level(product_frame,CPL_FRAME_LEVEL_FINAL);
759  (*ps).product_frame_stackc = product_frame;
760 
761  /* Set up the PHU header. */
762 
763  plist = casu_fits_get_phu(ffc);
764  vircam_dfs_set_product_primary_header(plist,product_frame,framelist,
765  parlist,vircam_recipename,
766  "PRO-1.15",NULL,0);
767 
768  /* 'Save' the PHU image */
769 
770  if (cpl_image_save(NULL,fnamec,CPL_TYPE_UCHAR,plist,
771  CPL_IO_DEFAULT) != CPL_ERROR_NONE) {
772  cpl_msg_error(fctid,"Cannot save product PHU");
773  cpl_frame_delete(product_frame);
774  freespace(fname);
775  freespace(fnamec);
776  freespace(fnamepaf);
777  return(-1);
778  }
779  cpl_frameset_insert(framelist,product_frame);
780  }
781 
782  /* Get the extension property list */
783 
784  p = cpl_propertylist_duplicate(casu_fits_get_ehu(ff));
785  if (isdummy) {
787  casu_merge_propertylists(p,dummyqcprops);
788  }
789 
790  /* Fiddle with the header now */
791 
792  product_frame = (*ps).product_frame_stack;
793  vircam_dfs_set_product_exten_header(p,product_frame,framelist,parlist,
794  vircam_recipename,"PRO-1.15",NULL);
795  if (cpl_image_save(fim,fname,CPL_TYPE_FLOAT,p,CPL_IO_EXTEND) !=
796  CPL_ERROR_NONE) {
797  cpl_msg_error(fctid,"Cannot save product image extension");
798  freespace(fname);
799  freespace(fnamec);
800  freespace(fnamepaf);
801  freepropertylist(p);
802  return(-1);
803  }
804 
805  /* Write PAF */
806 
807  pafprop = vircam_paf_req_items(p);
808  casu_merge_propertylists(pafprop,(*ps).phupaf);
809  vircam_paf_append(pafprop,casu_fits_get_phu(ff),"ESO INS FILT1 NAME");
810  vircam_paf_append(pafprop,p,"ESO DET NDIT");
811  vircam_paf_append(pafprop,p,"ESO PRO CATG");
812  vircam_paf_append(pafprop,casu_fits_get_phu(ff),"RA");
813  vircam_paf_append(pafprop,casu_fits_get_phu(ff),"DEC");
814  vircam_paf_append(pafprop,casu_fits_get_phu(ff),"ESO TEL AIRM START");
815  vircam_paf_append(pafprop,casu_fits_get_phu(ff),"ESO TEL GUID FWHM");
816  vircam_paf_append(pafprop,casu_fits_get_phu(ff),"ESO TEL AMBI RHUM");
817  vircam_paf_append(pafprop,casu_fits_get_phu(ff),"ESO OBS TARG NAME");
818  if (vircam_paf_print(fnamepaf,vircam_recipepaf,"QC file",
819  pafprop) != CASU_OK)
820  cpl_msg_warning(fctid,"Unable to save PAF for stack");
821  cpl_propertylist_delete(pafprop);
822 
823  /* Quick tidy */
824 
825  cpl_propertylist_delete(p);
826 
827  /* And now for the confidence map. Erase any stray QC headers
828  that might exist if you've used a BPM instead of a confidence
829  map. This only occurs if this is a dummy output */
830 
831  p = cpl_propertylist_duplicate(casu_fits_get_ehu(ffc));
832  if (isdummyc) {
834  cpl_propertylist_erase_regexp(p,"ESO QC",0);
835  }
836 
837  /* Fiddle with the header now */
838 
839  product_frame = (*ps).product_frame_stackc;
840  vircam_dfs_set_product_exten_header(p,product_frame,framelist,parlist,
841  vircam_recipename,"PRO-1.15",NULL);
842  if (cpl_image_save(fimc,fnamec,CPL_TYPE_SHORT,p,CPL_IO_EXTEND) !=
843  CPL_ERROR_NONE) {
844  cpl_msg_error(fctid,"Cannot save product image extension");
845  freespace(fname);
846  freespace(fnamec);
847  freespace(fnamepaf);
848  freepropertylist(p);
849  return(-1);
850  }
851  cpl_propertylist_delete(p);
852 
853  /* Clear the allocated workspace */
854 
855  freespace(fname);
856  freespace(fnamec);
857  freespace(fnamepaf);
858 
859  /* Get out of here */
860 
861  return(0);
862 }
863 
864 /*---------------------------------------------------------------------------*/
883 /*---------------------------------------------------------------------------*/
884 
885 extern int vircam_jmp_save_catalogue(struct psStruct* ps, cpl_frameset *framelist,
886  cpl_parameterlist *parlist,
887  cpl_propertylist *dummyqcprops) {
888  cpl_frame *product_frame;
889  int isdummy,i;
890  cpl_table *ftab;
891  cpl_propertylist *ehu,*phu,*ehu2,*pafprop;
892  char *fname,*fnamepaf;
893  const char *base[] = {"","catalogue_jmp","catalogue_std","catalogue_mes"};
894  const char *fctid = "vircam_jmp_save_catalogue";
895 
896  /* Work out whether you have a table to save or not. If you don't
897  then create a dummy table and save it */
898 
899  isdummy = 0;
900  if ((*ps).outcat != NULL) {
901  ftab = casu_tfits_get_table((*ps).outcat);
902  } else {
903  ftab = casu_dummy_catalogue(2);
904  isdummy = 1;
905  }
906 
907  /* Make an attempt to get some header information */
908 
909  ehu = NULL;
910  phu = NULL;
911  if ((*ps).outcat != NULL) {
912  ehu = casu_tfits_get_ehu((*ps).outcat);
913  } else if ((*ps).stack_frame != NULL) {
914  ehu = casu_fits_get_ehu((*ps).stack_frame);
915  } else {
916  for (i = 0; i < (*ps).nscience; i++) {
917  if ((ehu = casu_fits_get_ehu((*ps).sci_fits[i])) != NULL)
918  break;
919  }
920  }
921  if ((*ps).outcat != NULL) {
922  phu = casu_tfits_get_phu((*ps).outcat);
923  } else if ((*ps).stack_frame != NULL) {
924  phu = casu_fits_get_phu((*ps).stack_frame);
925  } else {
926  for (i = 0; i < (*ps).nscience; i++) {
927  if ((phu = casu_fits_get_phu((*ps).sci_fits[i])) != NULL)
928  break;
929  }
930  }
931 
932  /* Create a name */
933 
934  fname = vircam_jmp_outfile(base[recflag],0,1);
935  fnamepaf = vircam_jmp_outfile(base[recflag],0,0);
936 
937  /* If we need to make a PHU then do that now based on the first frame
938  in the input frame list */
939 
940  if (isfirst) {
941 
942  /* Create a new product frame object and define some tags */
943 
944  product_frame = cpl_frame_new();
945  cpl_frame_set_filename(product_frame,fname);
946  switch (recflag) {
947  case RECSCI:
948  cpl_frame_set_tag(product_frame,VIRCAM_PRO_OBJCAT_SCI);
949  break;
950  case RECSTD:
951  cpl_frame_set_tag(product_frame,VIRCAM_PRO_OBJCAT_STD);
952  break;
953  default:
954  cpl_frame_set_tag(product_frame,VIRCAM_PRO_OBJCAT_SCI);
955  break;
956  }
957  cpl_frame_set_type(product_frame,CPL_FRAME_TYPE_TABLE);
958  cpl_frame_set_group(product_frame,CPL_FRAME_GROUP_PRODUCT);
959  cpl_frame_set_level(product_frame,CPL_FRAME_LEVEL_FINAL);
960  (*ps).product_frame_cat = product_frame;
961 
962  /* Set up the PHU header. */
963 
964  vircam_dfs_set_product_primary_header(phu,product_frame,framelist,
965  parlist,vircam_recipename,
966  "PRO-1.15",NULL,0);
967 
968  /* Copy the header and delete any WCS info */
969 
970  ehu2 = cpl_propertylist_duplicate(ehu);
971  if (isdummy) {
972  casu_dummy_property(ehu2);
973  casu_merge_propertylists(ehu2,dummyqcprops);
974  casu_removewcs(ehu2, &(int){CASU_OK});
975  }
976 
977  /* Setup the extension header */
978 
979  vircam_dfs_set_product_exten_header(ehu2,product_frame,framelist,
980  parlist,vircam_recipename,
981  "PRO-1.15",NULL);
982 
983  /* 'Save' the PHU image */
984 
985  if (cpl_table_save(ftab,phu,ehu2,fname,CPL_IO_DEFAULT) !=
986  CPL_ERROR_NONE) {
987  cpl_msg_error(fctid,"Cannot save product PHU");
988  cpl_frame_delete(product_frame);
989  freepropertylist(ehu2);
990  freespace(fname);
991  return(-1);
992  }
993 
994  /* Write PAF */
995 
996  pafprop = vircam_paf_req_items(ehu2);
997  casu_merge_propertylists(pafprop,(*ps).phupaf);
998  vircam_paf_append(pafprop,ehu2,"ESO PRO CATG");
999  vircam_paf_append(pafprop,phu,"ESO INS FILT1 NAME");
1000  vircam_paf_append(pafprop,ehu,"ESO DET NDIT");
1001  vircam_paf_append(pafprop,phu,"RA");
1002  vircam_paf_append(pafprop,phu,"DEC");
1003  vircam_paf_append(pafprop,phu,"ESO TEL AIRM START");
1004  vircam_paf_append(pafprop,phu,"ESO TEL GUID FWHM");
1005  vircam_paf_append(pafprop,phu,"ESO TEL AMBI RHUM");
1006  vircam_paf_append(pafprop,phu,"ESO OBS TARG NAME");
1007  if (vircam_paf_print(fnamepaf,vircam_recipepaf,"QC file",
1008  pafprop) != CASU_OK)
1009  cpl_msg_warning(fctid,"Unable to save PAF for catalogue");
1010  cpl_propertylist_delete(pafprop);
1011 
1012  /* Quick tidy */
1013 
1014  freepropertylist(ehu2);
1015  if (isdummy)
1016  cpl_table_delete(ftab);
1017  cpl_frameset_insert(framelist,product_frame);
1018 
1019  } else {
1020 
1021  /* Fiddle with the header now */
1022 
1023  product_frame = (*ps).product_frame_cat;
1024  if (isdummy) casu_removewcs(ehu, &(int){CASU_OK});
1025  vircam_dfs_set_product_exten_header(ehu,product_frame,framelist,
1026  parlist,vircam_recipename,
1027  "PRO-1.15",NULL);
1028 
1029  /* Copy the header */
1030 
1031  ehu2 = cpl_propertylist_duplicate(ehu);
1032  if (isdummy) {
1033  casu_dummy_property(ehu2);
1034  casu_merge_propertylists(ehu2,dummyqcprops);
1035  }
1036 
1037  /* Save the table */
1038 
1039  if (cpl_table_save(ftab,NULL,ehu2,fname,CPL_IO_EXTEND) !=
1040  CPL_ERROR_NONE) {
1041  cpl_msg_error(fctid,"Cannot save product table extension");
1042  freepropertylist(ehu2);
1043  freespace(fname);
1044  return(-1);
1045  }
1046 
1047  /* Write PAF */
1048 
1049  pafprop = vircam_paf_req_items(ehu2);
1050  casu_merge_propertylists(pafprop,(*ps).phupaf);
1051  vircam_paf_append(pafprop,ehu2,"ESO PRO CATG");
1052  vircam_paf_append(pafprop,phu,"ESO INS FILT1 NAME");
1053  vircam_paf_append(pafprop,ehu,"ESO DET NDIT");
1054  vircam_paf_append(pafprop,phu,"RA");
1055  vircam_paf_append(pafprop,phu,"DEC");
1056  vircam_paf_append(pafprop,phu,"ESO TEL AIRM START");
1057  vircam_paf_append(pafprop,phu,"ESO TEL GUID FWHM");
1058  vircam_paf_append(pafprop,phu,"ESO TEL AMBI RHUM");
1059  vircam_paf_append(pafprop,phu,"ESO OBS TARG NAME");
1060  if (vircam_paf_print(fnamepaf,vircam_recipepaf,"QC file",
1061  pafprop) != CASU_OK)
1062  cpl_msg_warning(fctid,"Unable to save PAF for catalogue");
1063  cpl_propertylist_delete(pafprop);
1064 
1065  /* Quick tidy */
1066 
1067  freepropertylist(ehu2);
1068  if (isdummy)
1069  cpl_table_delete(ftab);
1070  }
1071  freespace(fname);
1072  freespace(fnamepaf);
1073 
1074  /* Get out of here */
1075 
1076  return(0);
1077 }
1078 
1079 /*---------------------------------------------------------------------------*/
1098 /*---------------------------------------------------------------------------*/
1099 
1100 extern int vircam_jmp_save_illum(struct psStruct* ps, cpl_frameset *framelist,
1101  cpl_parameterlist *parlist,
1102  cpl_propertylist *dummyqcprops) {
1103  cpl_frame *product_frame;
1104  int isdummy,i;
1105  cpl_table *ftab;
1106  cpl_propertylist *ehu,*phu,*ehu2,*pafprop;
1107  const char *fname = "illum.fits";
1108  const char *fnamepaf = "illum";
1109  const char *fctid = "vircam_jmp_save_illum";
1110 
1111  /* Work out whether you have a table to save or not. If you don't
1112  then create a dummy table and save it */
1113 
1114  isdummy = 0;
1115  if ((*ps).illcor != NULL) {
1116  ftab = casu_tfits_get_table((*ps).illcor);
1117  } else {
1118  ftab = vircam_illcor_newtab(1);
1119  isdummy = 1;
1120  }
1121 
1122  /* Make an attempt to get some header information */
1123 
1124  phu = NULL;
1125  ehu = NULL;
1126  if ((*ps).illcor != NULL) {
1127  ehu = casu_tfits_get_ehu((*ps).illcor);
1128  } else {
1129  for (i = 0; i < (*ps).nscience; i++) {
1130  if ((ehu = casu_fits_get_ehu((*ps).sci_fits[i])) != NULL)
1131  break;
1132  }
1133  }
1134  if ((*ps).illcor != NULL) {
1135  phu = casu_tfits_get_phu((*ps).illcor);
1136  } else {
1137  for (i = 0; i < (*ps).nscience; i++) {
1138  if ((phu = casu_fits_get_phu((*ps).sci_fits[i])) != NULL)
1139  break;
1140  }
1141  }
1142 
1143  /* If we need to make a PHU then do that now based on the first frame
1144  in the input frame list */
1145 
1146  if (isfirst) {
1147 
1148  /* Create a new product frame object and define some tags */
1149 
1150  product_frame = cpl_frame_new();
1151  cpl_frame_set_filename(product_frame,fname);
1152  switch (recflag) {
1153  case RECSTD:
1154  cpl_frame_set_tag(product_frame,VIRCAM_PRO_ILLCOR_STD);
1155  break;
1156  case RECMES:
1157  cpl_frame_set_tag(product_frame,VIRCAM_PRO_ILLCOR_MES);
1158  break;
1159  default:
1160  cpl_frame_set_tag(product_frame,VIRCAM_PRO_ILLCOR_STD);
1161  break;
1162  }
1163  cpl_frame_set_type(product_frame,CPL_FRAME_TYPE_TABLE);
1164  cpl_frame_set_group(product_frame,CPL_FRAME_GROUP_PRODUCT);
1165  cpl_frame_set_level(product_frame,CPL_FRAME_LEVEL_FINAL);
1166  (*ps).product_frame_illcor = product_frame;
1167 
1168  /* Set up the PHU header. */
1169 
1170  if ((*ps).phupaf == NULL)
1171  (*ps).phupaf = vircam_paf_phu_items(phu);
1172  vircam_dfs_set_product_primary_header(phu,product_frame,framelist,
1173  parlist,vircam_recipename,
1174  "PRO-1.15",NULL,0);
1175 
1176  /* Copy the header and delete any WCS info */
1177 
1178  ehu2 = cpl_propertylist_duplicate(ehu);
1179  casu_removewcs(ehu2, &(int){CASU_OK});
1180  if (isdummy) {
1181  casu_dummy_property(ehu2);
1182  casu_merge_propertylists(ehu2,dummyqcprops);
1183  }
1184 
1185 
1186  /* Setup the extension header */
1187 
1188  vircam_dfs_set_product_exten_header(ehu2,product_frame,framelist,
1189  parlist,vircam_recipename,
1190  "PRO-1.15",NULL);
1191 
1192  /* 'Save' the PHU image */
1193 
1194  if (cpl_table_save(ftab,phu,ehu2,fname,CPL_IO_DEFAULT) !=
1195  CPL_ERROR_NONE) {
1196  cpl_msg_error(fctid,"Cannot save product PHU");
1197  cpl_frame_delete(product_frame);
1198  freepropertylist(ehu2);
1199  return(-1);
1200  }
1201 
1202  /* Write the PAF */
1203 
1204  pafprop = vircam_paf_req_items(ehu2);
1205  casu_merge_propertylists(pafprop,(*ps).phupaf);
1206  vircam_paf_append(pafprop,ehu2,"ESO PRO CATG");
1207  vircam_paf_append(pafprop,phu,"ESO INS FILT1 NAME");
1208  vircam_paf_append(pafprop,ehu,"ESO DET NDIT");
1209  vircam_paf_append(pafprop,phu,"RA");
1210  vircam_paf_append(pafprop,phu,"DEC");
1211  vircam_paf_append(pafprop,phu,"ESO TEL AIRM START");
1212  vircam_paf_append(pafprop,phu,"ESO TEL GUID FWHM");
1213  vircam_paf_append(pafprop,phu,"ESO TEL AMBI RHUM");
1214  vircam_paf_append(pafprop,phu,"ESO OBS TARG NAME");
1215  if (vircam_paf_print((char *)fnamepaf,vircam_recipepaf,"QC file",
1216  pafprop) != CASU_OK)
1217  cpl_msg_warning(fctid,"Unable to save PAF for illcor table");
1218  cpl_propertylist_delete(pafprop);
1219 
1220  /* Quick tidy */
1221 
1222  if (isdummy)
1223  cpl_table_delete(ftab);
1224  freepropertylist(ehu2);
1225  cpl_frameset_insert(framelist,product_frame);
1226 
1227  } else {
1228 
1229 
1230  /* Fiddle with the header now */
1231 
1232  product_frame = (*ps).product_frame_illcor;
1233  casu_removewcs(ehu, &(int){CASU_OK});
1234  vircam_dfs_set_product_exten_header(ehu,product_frame,framelist,
1235  parlist,vircam_recipename,
1236  "PRO-1.15",NULL);
1237 
1238  /* Copy the header and delete any WCS info */
1239 
1240  ehu2 = cpl_propertylist_duplicate(ehu);
1241  if (isdummy) {
1242  casu_dummy_property(ehu2);
1243  casu_merge_propertylists(ehu2,dummyqcprops);
1244  }
1245 
1246  /* Save the table */
1247 
1248  if (cpl_table_save(ftab,NULL,ehu2,fname,CPL_IO_EXTEND) !=
1249  CPL_ERROR_NONE) {
1250  cpl_msg_error(fctid,"Cannot save product image extension");
1251  freepropertylist(ehu2);
1252  return(-1);
1253  }
1254 
1255  /* Write the PAF */
1256 
1257  pafprop = vircam_paf_req_items(ehu2);
1258  casu_merge_propertylists(pafprop,(*ps).phupaf);
1259  vircam_paf_append(pafprop,ehu2,"ESO PRO CATG");
1260  vircam_paf_append(pafprop,phu,"ESO INS FILT1 NAME");
1261  vircam_paf_append(pafprop,ehu,"ESO DET NDIT");
1262  vircam_paf_append(pafprop,phu,"RA");
1263  vircam_paf_append(pafprop,phu,"DEC");
1264  vircam_paf_append(pafprop,phu,"ESO TEL AIRM START");
1265  vircam_paf_append(pafprop,phu,"ESO TEL GUID FWHM");
1266  vircam_paf_append(pafprop,phu,"ESO TEL AMBI RHUM");
1267  vircam_paf_append(pafprop,phu,"ESO OBS TARG NAME");
1268  if (vircam_paf_print((char *)fnamepaf,vircam_recipepaf,"QC file",
1269  pafprop) != CASU_OK)
1270  cpl_msg_warning(fctid,"Unable to save PAF for illcor table");
1271  cpl_propertylist_delete(pafprop);
1272 
1273  /* Quick tidy */
1274 
1275  freepropertylist(ehu2);
1276  if (isdummy)
1277  cpl_table_delete(ftab);
1278  }
1279 
1280  /* Get out of here */
1281 
1282  return(0);
1283 }
1284 
1285 /*---------------------------------------------------------------------------*/
1301 /*---------------------------------------------------------------------------*/
1302 
1303 extern void vircam_jmp_ustep_seq(struct psStruct* ps) {
1304  int nalloc,i,match,j,ustepnum,nustep;
1305  casu_fits *ff;
1306  cpl_propertylist *plist;
1307  const char *fctid = "vircam_jmp_ustep_seq";
1308 
1309  /* Allocate an initial amount of workspace for the microstep sequence
1310  image sets */
1311 
1312  nalloc = INITALLOC;
1313  (*ps).ustep_sets = cpl_malloc(nalloc*sizeof(ustep_set));
1314  (*ps).nustep_sets = 0;
1315 
1316  /* Loop for each frame and get the microstep sequence number from the
1317  primary header */
1318 
1319  for (i = 0; i < (*ps).nscience; i++) {
1320  ff = (*ps).sci_fits[i];
1321  plist = casu_fits_get_phu(ff);
1322  if (vircam_pfits_get_ustepnum(plist,&ustepnum) != CASU_OK) {
1323  cpl_msg_error(fctid,"No microstep number in %s",
1325  casu_fits_set_error(ff,CASU_FATAL);
1326  continue;
1327  }
1328 
1329  /* See if this sequence number matches any of the others we've
1330  already defined. If it does, then simply add this frame into
1331  that sequences frameset */
1332 
1333  match = 0;
1334  for (j = 0; j < (*ps).nustep_sets; j++) {
1335  if (ustepnum == (*ps).ustep_sets[j].ustep_number) {
1336  match = 1;
1337  (*ps).ustep_sets[j].f[(*ps).ustep_sets[j].nframes] = ff;
1338  (*ps).ustep_sets[j].nframes += 1;
1339  if (casu_fits_get_status(ff) != CASU_FATAL)
1340  (*ps).ustep_sets[j].ngood += 1;
1341  break;
1342  }
1343  }
1344 
1345  /* If it doesn't match increment the number of sets and check to
1346  make sure that we haven't overrun our allocation for ustep sets. */
1347 
1348  if (! match) {
1349  if ((*ps).nustep_sets+1 == nalloc) {
1350  nalloc += INITALLOC;
1351  (*ps).ustep_sets = cpl_realloc((*ps).ustep_sets,nalloc*sizeof(ustep_set));
1352  }
1353 
1354  /* Now define this ustep set */
1355 
1356  (void)vircam_pfits_get_nusteps(plist,&nustep);
1357  (*ps).ustep_sets[(*ps).nustep_sets].f = cpl_malloc(nustep*sizeof(casu_fits *));
1358  (*ps).ustep_sets[(*ps).nustep_sets].ustep_number = ustepnum;
1359  (*ps).ustep_sets[(*ps).nustep_sets].nustep = nustep;
1360  (*ps).ustep_sets[(*ps).nustep_sets].status = CASU_OK;
1361  (*ps).ustep_sets[(*ps).nustep_sets].super = NULL;
1362  (*ps).ustep_sets[(*ps).nustep_sets].superc = NULL;
1363  (*ps).ustep_sets[(*ps).nustep_sets].nframes = 0;
1364  (*ps).ustep_sets[(*ps).nustep_sets].f[0] = ff;
1365  (*ps).ustep_sets[(*ps).nustep_sets].nframes = 1;
1366  (*ps).ustep_sets[(*ps).nustep_sets].ngood = 0;
1367  if (casu_fits_get_status(ff) != CASU_FATAL)
1368  (*ps).ustep_sets[(*ps).nustep_sets].ngood += 1;
1369  (*ps).nustep_sets++;
1370  }
1371  }
1372 
1373  /* Fix the allocation to what we need and throw the rest away */
1374 
1375  (*ps).ustep_sets = cpl_realloc((*ps).ustep_sets,
1376  (*ps).nustep_sets*sizeof(ustep_set));
1377 
1378  /* Loop through each of the defined sets and see if each is complete */
1379 
1380  for (i = 0; i < (*ps).nustep_sets; i++) {
1381  if ((*ps).ustep_sets[i].ngood == 0) {
1382  cpl_msg_warning(fctid,
1383  "Microstep sequence %" CPL_SIZE_FORMAT " has no input",
1384  (cpl_size)((*ps).ustep_sets[i].ustep_number));
1385  (*ps).ustep_sets[i].status = CASU_FATAL;
1386  } else if ((*ps).ustep_sets[i].ngood != (*ps).ustep_sets[i].nustep) {
1387  cpl_msg_warning(fctid,"Microstep sequence %" CPL_SIZE_FORMAT " incomplete",
1388  (cpl_size)((*ps).ustep_sets[i].ustep_number));
1389  (*ps).ustep_sets[i].status = CASU_WARN;
1390  }
1391  }
1392 }
1393 
1394 
1395 /*---------------------------------------------------------------------------*/
1411 /*---------------------------------------------------------------------------*/
1412 
1413 extern void vircam_jmp_interleave(struct psStruct* ps) {
1414  int i,refset,k,nstep,status,nk;
1415  const int *d;
1416  long npts;
1417  float val;
1418  double refx,refy,refra,refdec,x,y;
1419  cpl_image *fi,*outimage,*outconf;
1420  cpl_propertylist *plist;
1421  casu_fits *ff,**tmp;
1422  cpl_wcs *wcs;
1423  const char *fctid = "vircam_jmp_interleave";
1424 
1425  /* Work out the microstep sequences */
1426 
1427  vircam_jmp_ustep_seq(ps);
1428 
1429  /* Get workspace to contain the output superframes */
1430 
1431  (*ps).dith_input = cpl_malloc((*ps).nustep_sets*sizeof(casu_fits*));
1432  (*ps).dithc_input = cpl_malloc((*ps).nustep_sets*sizeof(casu_fits*));
1433 
1434  /* Loop for each of the ustep sets */
1435 
1436  (*ps).ndith = 0;
1437  for (i = 0; i < (*ps).nustep_sets; i++) {
1438  if ((*ps).ustep_sets[i].status == CASU_FATAL) {
1439  outimage = casu_dummy_image((*ps).ustep_sets[i].f[0]);
1440  outconf = casu_dummy_image((*ps).fconf);
1441  ff = casu_fits_wrap(outimage,(*ps).ustep_sets[i].f[0],NULL,NULL);
1442  (*ps).dith_input[(*ps).ndith] = ff;
1443  casu_fits_set_error(ff,CASU_FATAL);
1444  (*ps).ustep_sets[i].super = ff;
1445  ff = casu_fits_wrap(outconf,(*ps).ustep_sets[i].f[0],NULL,NULL);
1446  (*ps).dithc_input[(*ps).ndith++] = ff;
1447  casu_fits_set_error(ff,CASU_FATAL);
1448  (*ps).ustep_sets[i].superc = ff;
1449  continue;
1450  }
1451 
1452  /* Work out the offsets in the sequence from the WCS in the header.
1453  Fail the ustep sequence if any of it's components have an
1454  unreadable WCS */
1455 
1456  refset = 0;
1457  refx = 0.0;
1458  refy = 0.0;
1459  for (k = 0; k < (*ps).ustep_sets[i].nframes; k++) {
1460  ff = (*ps).ustep_sets[i].f[k];
1461  if (casu_fits_get_status(ff) == CASU_FATAL)
1462  continue;
1463  wcs = cpl_wcs_new_from_propertylist(casu_fits_get_ehu(ff));
1464  if (wcs == NULL) {
1465  cpl_msg_error(fctid,"Unable to open WCS structure %s",
1467  casu_fits_set_error(ff,CASU_FATAL);
1468  continue;
1469  }
1470 
1471  /* Get the background value for this image */
1472 
1473  fi = casu_fits_get_image(ff);
1474  npts = casu_getnpts(fi);
1475  val = casu_med(cpl_image_get_data_float(fi),NULL,npts);
1476  cpl_propertylist_update_float(casu_fits_get_ehu(ff),
1477  "ESO DRS BACKMED",val);
1478 
1479  /* If this is the first frame, then set up the reference coords. */
1480 
1481  if (refset == 0) {
1482  refset = 1;
1483  d = cpl_array_get_data_int_const(cpl_wcs_get_image_dims(wcs));
1484  refx = 0.5*(double)d[0];
1485  refy = 0.5*(double)d[1];
1486  casu_xytoradec(wcs,refx,refy,&refra,&refdec);
1487  cpl_propertylist_update_double(casu_fits_get_ehu(ff),
1488  "ESO DRS XOFFMICRO",0.0);
1489  cpl_propertylist_update_double(casu_fits_get_ehu(ff),
1490  "ESO DRS YOFFMICRO",0.0);
1491  cpl_wcs_delete(wcs);
1492  continue;
1493  }
1494 
1495  /* Take the reference ra and dec and see where that occurs on
1496  the program image in x,y space */
1497 
1498  casu_radectoxy(wcs,refra,refdec,&x,&y);
1499  x = refx - x;
1500  y = refy - y;
1501  cpl_propertylist_update_double(casu_fits_get_ehu(ff),
1502  "ESO DRS XOFFMICRO",x);
1503  cpl_propertylist_update_double(casu_fits_get_ehu(ff),
1504  "ESO DRS YOFFMICRO",y);
1505  cpl_wcs_delete(wcs);
1506  }
1507 
1508  /* Get a temporary workspace to hold all the good files in the list */
1509 
1510  tmp = cpl_malloc((*ps).ustep_sets[i].nframes*sizeof(casu_fits *));
1511  nk = 0;
1512  for (k = 0; k < (*ps).ustep_sets[i].nframes; k++) {
1513  ff = (*ps).ustep_sets[i].f[k];
1514  if (casu_fits_get_status(ff) != CASU_FATAL)
1515  tmp[nk++] = ff;
1516  }
1517  if (nk < (*ps).ustep_sets[i].nframes) {
1518  cpl_msg_error(fctid,"A frame in this ustep sequence failed");
1519  (*ps).ustep_sets[i].status = CASU_WARN;
1520  }
1521 
1522  /* Otherwise interleave them */
1523 
1524  status = CASU_OK;
1525  nstep = (int)sqrt((double)((*ps).ustep_sets[i].nustep));
1526  (void)vircam_interleave(tmp,nk,&((*ps).fconf),1,nstep,&plist,&outimage,
1527  &outconf,&status);
1528  freespace(tmp);
1529  if (status != CASU_OK) {
1530  cpl_msg_error(fctid,
1531  "Interleaving failed for ugroup %" CPL_SIZE_FORMAT " extn %" CPL_SIZE_FORMAT,
1532  (cpl_size)((*ps).ustep_sets[i].ustep_number),
1533  (cpl_size)(casu_fits_get_nexten((*ps).ustep_sets[i].f[0])));
1534  freepropertylist(plist);
1535  freeimage(outimage);
1536  freeimage(outconf);
1537  outimage = casu_dummy_image((*ps).ustep_sets[i].f[0]);
1538  outconf = casu_dummy_image((*ps).fconf);
1539  ff = casu_fits_wrap(outimage,(*ps).ustep_sets[i].f[0],NULL,NULL);
1540  casu_fits_set_error(ff,CASU_FATAL);
1541  (*ps).ustep_sets[i].super = ff;
1542  ff = casu_fits_wrap(outconf,(*ps).ustep_sets[i].f[0],NULL,NULL);
1543  casu_fits_set_error(ff,CASU_FATAL);
1544  (*ps).ustep_sets[i].superc = ff;
1545  }
1546 
1547  /* Wrap the output results and store them away into the list of
1548  frames that will be dithered later on. */
1549 
1550  (*ps).dith_input[(*ps).ndith] = casu_fits_wrap(outimage,
1551  (*ps).ustep_sets[i].f[0],NULL,
1552  plist);
1553  (*ps).dithc_input[(*ps).ndith++] = casu_fits_wrap(outconf,
1554  (*ps).ustep_sets[i].f[0],
1555  NULL,plist);
1556  (*ps).ndithc = (*ps).ndith;
1557  (*ps).ustep_sets[i].super = (*ps).dith_input[(*ps).ndith - 1];
1558  (*ps).ustep_sets[i].superc = (*ps).dithc_input[(*ps).ndithc - 1];
1559  freepropertylist(plist);
1560  }
1561 }
1562 
1563 /*---------------------------------------------------------------------------*/
1583 /*---------------------------------------------------------------------------*/
1584 
1585 extern void vircam_jmp_dither_offsets(struct psStruct* ps, int inter_lv) {
1586  int status,i,ustepnum,nmatch,refset;
1587  float *xoffs,*yoffs,xoff,yoff,filtfwhm;
1588  cpl_wcs *wcsref,*wcs;
1589  casu_fits *ff,*ffc;
1590  casu_tfits *catref,*outcat;
1591  cpl_table *cr,*oc,*outxy;
1592  const char *fctid = "vircam_jmp_dither_offsets";
1593  const double maxoffset = 2048;
1594 
1595  /* Is there anything to dither? If not, then get out of here. NB: We
1596  don't have to check the status of the input dither files as the
1597  calling routine won't have included any files with bad status in
1598  the input list for this routine. */
1599 
1600  if ((*ps).ndith == 0)
1601  return;
1602 
1603  /* If there is only 1 then set the offsets to zero and get out of here */
1604 
1605  if ((*ps).ndith == 1) {
1606  cpl_propertylist_update_double(casu_fits_get_ehu((*ps).dith_input[0]),
1607  "ESO DRS XOFFDITHER",(double)0.0);
1608  cpl_propertylist_update_double(casu_fits_get_ehu((*ps).dith_input[0]),
1609  "ESO DRS YOFFDITHER",(double)0.0);
1610  return;
1611  }
1612 
1613  /* Initialise the status variable and get some workspace for the offsets */
1614 
1615  status = CASU_OK;
1616  xoffs = cpl_malloc((*ps).ndith*sizeof(float));
1617  yoffs = cpl_malloc((*ps).ndith*sizeof(float));
1618 
1619  /* Loop for all the input files and get the FITS WCS information. */
1620 
1621  refset = 0;
1622  wcsref = NULL;
1623  for (i = 0; i < (*ps).ndith; i++) {
1624  ff = (*ps).dith_input[i];
1625  if (casu_fits_get_status(ff) == CASU_FATAL)
1626  continue;
1627  wcs = cpl_wcs_new_from_propertylist(casu_fits_get_ehu(ff));
1628  (void)vircam_pfits_get_ustepnum(casu_fits_get_phu(ff),&ustepnum);
1629 
1630  /* If we can't get a WCS for this image, then signal that with
1631  a warning */
1632 
1633  if (wcs == NULL) {
1634  cpl_msg_warning(fctid,
1635  "Unable to get WCS for ustep %" CPL_SIZE_FORMAT,
1636  (cpl_size)ustepnum);
1637  xoffs[i] = 0.0;
1638  yoffs[i] = 0.0;
1639  casu_fits_set_error(ff,CASU_WARN);
1640  continue;
1641  }
1642 
1643  /* If we don't have a reference WCS yet, then make the current frame
1644  WCS the reference and move on */
1645 
1646  if (! refset) {
1647  xoffs[i] = 0.0;
1648  yoffs[i] = 0.0;
1649  refset = 1;
1650  wcsref = wcs;
1651  continue;
1652  }
1653 
1654  /* Right, assuming we're here, then we need to work out the xy
1655  differences */
1656 
1657  (void)casu_diffxywcs(wcs,wcsref,&xoff,&yoff,&status);
1658 
1659  /* Did it work? If not the set a warning status for this file */
1660 
1661  if (status != CASU_OK) {
1662  xoffs[i] = 0.0;
1663  yoffs[i] = 0.0;
1664  cpl_msg_warning(fctid,
1665  "Unable to WCS difference for %" CPL_SIZE_FORMAT,
1666  (cpl_size)ustepnum);
1667  } else if (fabs((double)xoff) > maxoffset ||
1668  fabs((double)yoff) > maxoffset) {
1669  cpl_msg_warning(fctid,
1670  "WCS offsets for %" CPL_SIZE_FORMAT " are > %g: %g %g -- ignoring",
1671  (cpl_size)ustepnum,maxoffset,xoff,yoff);
1672  xoffs[i] = 0.0;
1673  yoffs[i] = 0.0;
1674  } else {
1675  xoffs[i] = xoff;
1676  yoffs[i] = yoff;
1677  }
1678  cpl_wcs_delete(wcs);
1679  }
1680  if (wcsref != NULL)
1681  cpl_wcs_delete(wcsref);
1682 
1683  /* Now generate a catalogue for each of the input images */
1684 
1685  catref = NULL;
1686  cr = NULL;
1687  filtfwhm = (inter_lv ? 3.5 : 2.0);
1688  for (i = 0; i < (*ps).ndith; i++) {
1689  ff = (*ps).dith_input[i];
1690  if (casu_fits_get_status(ff) == CASU_FATAL)
1691  continue;
1692  status = CASU_OK;
1693  if ((*ps).ndithc != 1)
1694  ffc = (*ps).dithc_input[i];
1695  else
1696  ffc = (*ps).dithc_input[0];
1697  (void)vircam_pfits_get_ustepnum(casu_fits_get_phu(ff),&ustepnum);
1698  outcat = NULL;
1699  (void)casu_imcore(ff,ffc,25,5.0,0,3.5,64,1,filtfwhm,&outcat,4.0,
1700  &status);
1701 
1702  /* If we get a bad status from imcore and this frame has already
1703  failed in the WCS stage, then mark it with CASU_FATAL status
1704  and move to the next one */
1705 
1706  if (status != CASU_OK && casu_fits_get_status(ff) != CASU_OK) {
1707  casu_fits_set_error(ff,CASU_FATAL);
1708  freetfits(outcat);
1709  cpl_msg_error(fctid,
1710  "Unable to get offsets for %" CPL_SIZE_FORMAT,
1711  (cpl_size)ustepnum);
1712  continue;
1713 
1714  /* If we get bad status, but this file has a perfectly good offset
1715  from the WCS then just go with it that and issue a warning */
1716 
1717  } else if (status != CASU_OK && casu_fits_get_status(ff) == CASU_OK) {
1718  casu_fits_set_error(ff,CASU_WARN);
1719  freetfits(outcat);
1720  cpl_msg_error(fctid,
1721  "Unable to get object offset for %" CPL_SIZE_FORMAT ". Going with WCS offset",
1722  (cpl_size)ustepnum);
1723  continue;
1724 
1725  /* OK, we got a good status from imcore. See if the reference
1726  catalogue has already been defined. If it hasn't then this
1727  catalogue becomes the reference */
1728 
1729  } else {
1730  oc = casu_tfits_get_table(outcat);
1731  cpl_table_add_scalar(oc,"X_coordinate",(double)xoffs[i]);
1732  cpl_table_add_scalar(oc,"Y_coordinate",(double)yoffs[i]);
1733  if (catref == NULL) {
1734  catref = outcat;
1735  cr = oc;
1736  continue;
1737  }
1738 
1739  /* If this isn't the reference file, then do the cross match */
1740 
1741  (void)casu_matchxy(oc,cr,100.0,&xoff,&yoff,&nmatch,&outxy,&status);
1742  freetable(outxy);
1743  freetfits(outcat);
1744 
1745  /* If we got a bad result with the cross match and this current
1746  file is already got a warning status, then flag it as fatal
1747  status */
1748 
1749  if ((nmatch == 0 || status == CASU_FATAL) &&
1750  casu_fits_get_status(ff) != CASU_OK) {
1751  xoff = 0.0;
1752  yoff = 0.0;
1753  casu_fits_set_error(ff,CASU_FATAL);
1754  cpl_msg_error(fctid,
1755  "Unable to match stars for %" CPL_SIZE_FORMAT,
1756  (cpl_size)ustepnum);
1757 
1758  /* If we got a bad result with the cross match and this current
1759  file has a good status, then just go with the WCS offset
1760  we already have */
1761 
1762  } else if ((nmatch == 0 || status == CASU_FATAL) &&
1763  casu_fits_get_status(ff) == CASU_OK) {
1764  casu_fits_set_error(ff,CASU_WARN);
1765  xoff = 0.0;
1766  yoff = 0.0;
1767  cpl_msg_warning(fctid,
1768  "Unable to match stars for %" CPL_SIZE_FORMAT ". Going with WCS offsets",
1769  (cpl_size)ustepnum);
1770  }
1771 
1772  /* Add the current offsets to what we already have */
1773 
1774  xoffs[i] += xoff;
1775  yoffs[i] += yoff;
1776  }
1777  }
1778 
1779  /* Write the results to the headers */
1780 
1781  for (i = 0; i < (*ps).ndith; i++) {
1782  if (casu_fits_get_status((*ps).dith_input[i]) == CASU_FATAL)
1783  continue;
1784  ff = (*ps).dith_input[i];
1785  cpl_propertylist_update_double(casu_fits_get_ehu(ff),
1786  "ESO DRS XOFFDITHER",(double)xoffs[i]);
1787  cpl_propertylist_update_double(casu_fits_get_ehu(ff),
1788  "ESO DRS YOFFDITHER",(double)yoffs[i]);
1789  }
1790 
1791  /* Get rid of some workspace */
1792 
1793  freespace(xoffs);
1794  freespace(yoffs);
1795  freetfits(catref);
1796 }
1797 
1798 /*---------------------------------------------------------------------------*/
1814 /*---------------------------------------------------------------------------*/
1815 
1816 extern void vircam_jmp_dither_images(struct psStruct* ps) {
1817  int status,ngood,i,n;
1818  casu_fits **d,**dc,*ff;
1819  cpl_propertylist *dither_ehu,*dither_phu;
1820  cpl_image *outdither,*outditherc;
1821  const char *fctid = "vircam_jmp_dither_images";
1822 
1823  /* Count how many good input images there are. */
1824 
1825  ngood = 0;
1826  for (i = 0; i < (*ps).ndith; i++)
1827  if (casu_fits_get_status((*ps).dith_input[i]) != CASU_FATAL)
1828  ngood++;
1829 
1830  /* If there are none, then get out of here. The output jittered image
1831  and it's confidence map should still be set to NULL */
1832 
1833  if (ngood == 0) {
1834  cpl_msg_error(fctid,"No good input images for jittering");
1835 
1836 
1837  outdither = casu_dummy_image((*ps).sci_fits[0]);
1838  outditherc = casu_dummy_image((*ps).dithc_input[0]);
1839  ff = casu_fits_wrap(outdither,(*ps).sci_fits[0],NULL,NULL);
1840  (*ps).stack_frame = ff;
1841  casu_fits_set_error(ff,CASU_FATAL);
1842  ff = casu_fits_wrap(outditherc,(*ps).dithc_input[0],NULL,NULL);
1843  casu_fits_set_error(ff,CASU_FATAL);
1844  (*ps).stackc_frame = ff;
1845  return;
1846  }
1847 
1848  /* Now transfer the input images over to new arrays... */
1849 
1850  d = cpl_malloc(ngood*sizeof(casu_fits *));
1851  dc = cpl_malloc(ngood*sizeof(casu_fits *));
1852  n = 0;
1853  for (i = 0; i < (*ps).ndith; i++) {
1854  if (casu_fits_get_status((*ps).dith_input[i]) != CASU_FATAL) {
1855  d[n] = (*ps).dith_input[i];
1856  if ((*ps).ndithc != 1)
1857  dc[n++] = (*ps).dithc_input[i];
1858  else
1859  dc[n++] = (*ps).dithc_input[0];
1860  }
1861  }
1862 
1863  /* Dither the remaining images */
1864 
1865  status = CASU_OK;
1866  (void)casu_imdither(d,dc,ngood,ngood,5.0,5.0,&dither_ehu,"EXPTIME",
1867  &outdither,&outditherc,&status);
1868  dither_phu = cpl_propertylist_duplicate(casu_fits_get_phu(d[0]));
1869  if (status != CASU_OK) {
1870  freeimage(outdither);
1871  freeimage(outditherc);
1872  cpl_msg_error(fctid,"Error jittering to output");
1873  } else {
1874  (*ps).stack_frame = casu_fits_wrap(outdither,d[0],dither_phu,
1875  dither_ehu);
1876  (*ps).stackc_frame = casu_fits_wrap(outditherc,dc[0],dither_phu,
1877  dither_ehu);
1878  }
1879 
1880  /* Tidy and exit */
1881 
1882  freepropertylist(dither_phu);
1883  freepropertylist(dither_ehu);
1884  freespace(d);
1885  freespace(dc);
1886 }
1887 
1888 /*---------------------------------------------------------------------------*/
1903 /*---------------------------------------------------------------------------*/
1904 
1905 extern void vircam_jmp_catalogue(struct psStruct* ps, int inter_lv) {
1906  const char *fctid = "vircam_jmp_catalogue";
1907  int status;
1908  casu_tfits *outtab;
1909  float filtfwhm,gain;
1910 
1911  /* Check to see if there has been a jitter frame defined */
1912 
1913  if ((*ps).stack_frame == NULL || casu_fits_get_status((*ps).stack_frame) == CASU_FATAL) {
1914  cpl_msg_error(fctid,"No stack image available. No catalogue generated");
1915  return;
1916  }
1917 
1918  /* Generate the catalogue */
1919 
1920  status = CASU_OK;
1921  filtfwhm = (inter_lv ? 3.5 : 2);
1922  gain = cpl_propertylist_get_float(casu_fits_get_ehu((*ps).stack_frame),"GAIN");
1923  (void)casu_imcore((*ps).stack_frame,(*ps).stackc_frame,
1924  vircam_jmp_config.ipix,
1925  vircam_jmp_config.threshold,
1926  vircam_jmp_config.icrowd,
1927  vircam_jmp_config.rcore,
1928  vircam_jmp_config.nbsize,2,filtfwhm,
1929  &outtab,gain,&status);
1930 
1931  /* If it failed, then get rid of workspaces and get out of here with
1932  an error message. If it went well, then wrap the result in a
1933  casu_tfits object */
1934 
1935  if (status != CASU_OK) {
1936  cpl_msg_error(fctid,"Error generating catalogue");
1937  freetfits(outtab);
1938  casu_fits_set_error((*ps).stack_frame,CASU_FATAL);
1939  } else {
1940  (*ps).outcat = outtab;
1941  }
1942 
1943  return;
1944 }
1945 
1946 /*---------------------------------------------------------------------------*/
1962 /*---------------------------------------------------------------------------*/
1963 
1964 extern void vircam_jmp_matched_stds(struct psStruct* ps, char* cata, char* catapath) {
1965  int status,ncat,nstd,level;
1966  const char *fctid = "vircam_jmp_matched_stds";
1967  cpl_table *stdscat,*newstds,*tmp,*tmp2,*cat;
1968  cpl_propertylist *p;
1969 
1970  /* Initialise status */
1971 
1972  status = CASU_OK;
1973 
1974  /* Check that we have a catalogue */
1975 
1976  if ((*ps).outcat == NULL) {
1977  cpl_msg_error(fctid,"No input catalogue found");
1978  return;
1979  }
1980 
1981  /* Get some standard stars */
1982 
1983  (void)casu_getstds(casu_fits_get_ehu((*ps).stack_frame),1,
1984  catapath,cata,0,".",&stdscat,&status);
1985  if (status != CASU_OK) {
1986  freetable(stdscat);
1987  cpl_msg_error(fctid,"Failed to find any standards");
1988  return;
1989  }
1990 
1991  /* Restrict to stars with good photometry */
1992 
1993  (void)cpl_table_or_selected_float(stdscat,"e_Jmag",CPL_LESS_THAN,0.2);
1994  (void)cpl_table_and_selected_float(stdscat,"e_Kmag",CPL_LESS_THAN,0.2);
1995  newstds = cpl_table_extract_selected(stdscat);
1996  nstd = (int)cpl_table_get_nrow(newstds);
1997 
1998  /* If there are too many objects in the catalogue then first restrict
1999  ourselves by ellipticity. Cut so that there are similar numbers of
2000  objects in the standards and the object catalogues by retaining the
2001  brighter objects */
2002 
2003  cat = casu_tfits_get_table((*ps).outcat);
2004  ncat = (int)cpl_table_get_nrow(cat);
2005  if (ncat > 500 && ncat > 2.0*nstd) {
2006  tmp = cpl_table_duplicate(cat);
2007  (void)cpl_table_or_selected_float(tmp,"Ellipticity",CPL_LESS_THAN,0.5);
2008  tmp2 = cpl_table_extract_selected(tmp);
2009  ncat = (int)cpl_table_get_nrow(tmp2);
2010  freetable(tmp);
2011  p = cpl_propertylist_new();
2012  cpl_propertylist_append_bool(p,"Aper_flux_3",TRUE);
2013  cpl_table_sort(tmp2,(const cpl_propertylist *)p);
2014  cpl_propertylist_delete(p);
2015  level = min(ncat,max(1,min(5000,max(500,2*nstd))));
2016  tmp = cpl_table_extract(tmp2,1,(cpl_size)level);
2017  freetable(tmp2);
2018  ncat = (int)cpl_table_get_nrow(tmp);
2019  cat = tmp;
2020  } else {
2021  tmp = NULL;
2022  }
2023 
2024  /* Now match this against the catalogue */
2025 
2026  (void)casu_matchstds(cat,newstds,300.0,&((*ps).matchstds),&status);
2027  freetable(stdscat);
2028  freetable(newstds);
2029  freetable(tmp);
2030  if (status != CASU_OK) {
2031  freetable((*ps).matchstds);
2032  cpl_msg_error(fctid,"Failed to match standards to catalogue");
2033  return;
2034  }
2035 }
2036 
2037 /*---------------------------------------------------------------------------*/
2054 /*---------------------------------------------------------------------------*/
2055 
2056 extern void vircam_jmp_wcsfit(struct psStruct* ps) {
2057  int status,n,i;
2058  const char *fctid = "vircam_jmp_wcsfit";
2059  float *ra,*dec,*x,*y;
2060  double r,d;
2061  cpl_table *cat;
2062  cpl_wcs *wcs;
2063 
2064  /* Initialise status */
2065 
2066  status = CASU_OK;
2067 
2068  /* Check that we have a catalogue */
2069 
2070  if ((*ps).matchstds == NULL) {
2071  cpl_msg_error(fctid,"No input matched standards catalogue found");
2072  return;
2073  }
2074 
2075  /* Fit the plate solution */
2076 
2077  (void)casu_platesol(casu_fits_get_ehu((*ps).stack_frame),
2078  casu_tfits_get_ehu((*ps).outcat),(*ps).matchstds,
2079  6,1,&status);
2080  if (status != CASU_OK) {
2081  cpl_msg_error(fctid,"Failed to fit WCS");
2082  return;
2083  }
2084 
2085  /* Update the RA and DEC of the objects in the object catalogue */
2086 
2087  cat = casu_tfits_get_table((*ps).outcat);
2088  n = (int)cpl_table_get_nrow(cat);
2089  wcs = cpl_wcs_new_from_propertylist(casu_fits_get_ehu((*ps).stack_frame));
2090  if (wcs == NULL) {
2091  cpl_msg_error(fctid,"Failed to fill RA and Dec in catalogue");
2092  return;
2093  }
2094  x = cpl_table_get_data_float(cat,"X_coordinate");
2095  y = cpl_table_get_data_float(cat,"Y_coordinate");
2096  ra = cpl_table_get_data_float(cat,"RA");
2097  dec = cpl_table_get_data_float(cat,"DEC");
2098  for (i = 0; i < n; i++) {
2099  casu_xytoradec(wcs,x[i],y[i],&r,&d);
2100  ra[i] = (float)r;
2101  dec[i] = (float)d;
2102  }
2103  cpl_wcs_delete(wcs);
2104 }
2105 
2106 /*---------------------------------------------------------------------------*/
2122 /*---------------------------------------------------------------------------*/
2123 
2124 extern void vircam_jmp_photcal(struct psStruct* ps) {
2125  int status;
2126  const char *fctid = "vircam_jmp_photcal";
2127  char filt[32];
2128  casu_tfits *pp;
2129 
2130  /* Initialise status */
2131 
2132  status = CASU_OK;
2133 
2134  /* Check that we have a catalogue */
2135 
2136  if ((*ps).matchstds == NULL || (int)cpl_table_get_nrow((*ps).matchstds) == 0) {
2137  cpl_msg_error(fctid,"No input matched standards catalogue found");
2138  return;
2139  }
2140 
2141  /* What filter is this? */
2142 
2143  if (vircam_pfits_get_filter(casu_fits_get_phu((*ps).stack_frame),filt) != CASU_OK) {
2144  cpl_msg_error(fctid,"No filter name in stack header");
2145  return;
2146  }
2147 
2148  /* Fit the photometric calibration */
2149 
2150  pp = casu_tfits_wrap(cpl_table_duplicate((*ps).matchstds),(*ps).outcat,NULL,
2151  NULL);
2152  (void)casu_photcal_extinct(&((*ps).stack_frame),&pp,&((*ps).outcat),
2153  1,filt,(*ps).tphottab,1,NULL,NULL,"EXPTIME",
2154  "ESO TEL AIRM START",100000.0,&status);
2155  casu_tfits_delete(pp);
2156  if (status != CASU_OK) {
2157  cpl_msg_error(fctid,"Failed to fit photometric zeropoint");
2158  return;
2159  }
2160 }
2161 
2162 /*---------------------------------------------------------------------------*/
2179 /*---------------------------------------------------------------------------*/
2180 
2181 extern void vircam_jmp_bpm2conf(struct psStruct* ps) {
2182  cpl_image *im;
2183  int i,n,*data;
2184 
2185  /* Get the image */
2186 
2187  im = casu_fits_get_image((*ps).fconf);
2188  n = (int)cpl_image_get_size_x(im)*(int)cpl_image_get_size_y(im);
2189  data = cpl_image_get_data_int(im);
2190 
2191  /* Convert it now */
2192 
2193  for (i = 0; i < n; i++)
2194  data[i] = (data[i] == 1 ? 0 : 100);
2195 
2196 }
2197 
2198 /*---------------------------------------------------------------------------*/
2215 /*---------------------------------------------------------------------------*/
2216 
2217 extern void vircam_jmp_skycor(struct psStruct* ps) {
2218  int i,ngood,status,nfrms;
2219  long npts;
2220  casu_fits **ftmp,*ff,**frms = NULL;
2221  const char *fctid = "vircam_jmp_skycor";
2222  unsigned char *rejmask,*rejplus;
2223  cpl_propertylist *drs;
2224  cpl_image *skyimg,*fim;
2225  float *data,med,sig;
2226 
2227  /* If we need to do a sky combination... */
2228 
2229  if (offsky == 0 || offsky == 1) {
2230 
2231  /* Which array are we combining? */
2232 
2233  if (offsky == 1) {
2234  nfrms = (*ps).noffsets;
2235  frms = (*ps).offsky_fits;
2236  } else {
2237  nfrms = (*ps).nscience;
2238  frms = (*ps).sci_fits;
2239  }
2240 
2241  /* Sort out all images with good status */
2242 
2243  ftmp = cpl_malloc(nfrms*sizeof(casu_fits *));
2244  ngood = 0;
2245  for (i = 0; i < nfrms; i++) {
2246  ff = frms[i];
2247  if (casu_fits_get_status(ff) != CASU_FATAL)
2248  ftmp[ngood++] = ff;
2249  }
2250 
2251  /* If there aren't any good images, then get out of here now */
2252 
2253  if (ngood == 0) {
2254  freespace(ftmp);
2255  cpl_msg_error(fctid,"Sky correction impossible. No good sky frames available");
2256  return;
2257  }
2258 
2259  /* Combine all the good science images */
2260 
2261  status = CASU_OK;
2262  (void)casu_imcombine(ftmp,NULL,ngood,1,1,1,2.0,"EXPTIME",&skyimg,NULL,
2263  &rejmask,&rejplus,&drs,&status);
2264  freespace(rejmask);
2265  freespace(rejplus);
2266  if (offsky == 0)
2267  freepropertylist(drs);
2268  freespace(ftmp);
2269  } else {
2270  skyimg = casu_fits_get_image((*ps).fsky);
2271  }
2272 
2273  /* Normalise the sky frame to zero median */
2274 
2275  data = cpl_image_get_data_float(skyimg);
2276  npts = (long)cpl_image_get_size_x(skyimg)*(long)cpl_image_get_size_y(skyimg);
2277  casu_qmedsig(data,NULL,npts,5.0,3,-1000.0,65535.0,&med,&sig);
2278  for (i = 0; i < npts; i++)
2279  data[i] -= med;
2280 
2281  /* Save the sky frame if it's an offset sky */
2282 
2283  if (offsky == 1) {
2284  (*ps).outsky = casu_fits_wrap(skyimg,frms[0],NULL,NULL);
2285  casu_merge_propertylists(casu_fits_get_ehu((*ps).outsky),drs);
2286  freepropertylist(drs);
2287  drs = casu_fits_get_ehu((*ps).outsky);
2288  cpl_propertylist_update_float(drs,"ESO QC SKYMED",med);
2289  cpl_propertylist_set_comment(drs,"ESO QC SKYMED",
2290  "Median sky background");
2291  cpl_propertylist_update_float(drs,"ESO QC SKYSIG",sig);
2292  cpl_propertylist_set_comment(drs,"ESO QC SKYSIG",
2293  "Sky background sigma");
2294  }
2295 
2296  /* Subtract the normalised sky frame from the science images and do the
2297  same for the sky images */
2298 
2299  for (i = 0; i < (*ps).nscience; i++) {
2300  ff = (*ps).sci_fits[i];
2301  fim = casu_fits_get_image(ff);
2302  if (casu_fits_get_status(ff) != CASU_FATAL) {
2303  drs = casu_fits_get_ehu(ff);
2304  cpl_image_subtract(fim,skyimg);
2305  if (offsky == 0) {
2306  cpl_propertylist_update_string(drs,"ESO DRS SKYCOR","internal");
2307  } else if (offsky == 1) {
2308  cpl_propertylist_update_string(drs,"ESO DRS SKYCOR","offsky");
2309  } else if (offsky == -1) {
2310  cpl_propertylist_update_string(drs,"ESO DRS SKYCOR",
2311  casu_fits_get_fullname((*ps).fsky));
2312  }
2313  cpl_propertylist_set_comment(drs,"ESO DRS SKYCOR",
2314  "Sky correction method");
2315  }
2316  }
2317  for (i = 0; i < (*ps).noffsets; i++) {
2318  ff = (*ps).offsky_fits[i];
2319  fim = casu_fits_get_image(ff);
2320  if (casu_fits_get_status(ff) != CASU_FATAL) {
2321  drs = casu_fits_get_ehu(ff);
2322  cpl_image_subtract(fim,skyimg);
2323  if (offsky == 0) {
2324  cpl_propertylist_update_string(drs,"ESO DRS SKYCOR","internal");
2325  } else if (offsky == 1) {
2326  cpl_propertylist_update_string(drs,"ESO DRS SKYCOR","offsky");
2327  } else if (offsky == -1) {
2328  cpl_propertylist_update_string(drs,"ESO DRS SKYCOR",
2329  casu_fits_get_fullname((*ps).fsky));
2330  }
2331  cpl_propertylist_set_comment(drs,"ESO DRS SKYCOR",
2332  "Image has been sky corrected");
2333  }
2334  }
2335 
2336  /* Clean up and get out of here */
2337 
2338  if (offsky == 0)
2339  cpl_image_delete(skyimg);
2340  else if (offsky == 1)
2341  cpl_image_add_scalar(skyimg,med);
2342  return;
2343 }
2344 
2345 /*---------------------------------------------------------------------------*/
2361 /*---------------------------------------------------------------------------*/
2362 
2363 extern void vircam_jmp_get_readnoise_gain(struct psStruct* ps, int jext, float *readnoise,
2364  float *gain) {
2365  cpl_propertylist *p_rg;
2366  const char *fctid = "vircam_jmp_get_readnoise_gain";
2367 
2368  /* Load the propertylist */
2369 
2370  p_rg = cpl_propertylist_load(cpl_frame_get_filename((*ps).readgain_file),
2371  (cpl_size)jext);
2372 
2373  /* Check the readnoise property type and read it */
2374 
2375  switch (cpl_propertylist_get_type(p_rg,"ESO QC READNOISE")) {
2376  case CPL_TYPE_FLOAT:
2377  *readnoise = cpl_propertylist_get_float(p_rg,"ESO QC READNOISE");
2378  break;
2379  case CPL_TYPE_DOUBLE:
2380  *readnoise = (float)cpl_propertylist_get_double(p_rg,
2381  "ESO QC READNOISE");
2382  break;
2383  default:
2384  cpl_error_reset();
2385  *readnoise = 25.0;
2386  cpl_msg_error(fctid,"Unable to get READNOISE estimate, guessing %g\n",
2387  *readnoise);
2388  }
2389 
2390  /* Now the gain */
2391 
2392  switch (cpl_propertylist_get_type(p_rg,"ESO QC CONAD")) {
2393  case CPL_TYPE_FLOAT:
2394  *gain = cpl_propertylist_get_float(p_rg,"ESO QC CONAD");
2395  break;
2396  case CPL_TYPE_DOUBLE:
2397  *gain = (float)cpl_propertylist_get_double(p_rg,"ESO QC CONAD");
2398  break;
2399  default:
2400  cpl_error_reset();
2401  *gain = 1.0;
2402  cpl_msg_error(fctid,"Unable to get GAIN estimate, guessing %g\n",
2403  *gain);
2404  }
2405  cpl_propertylist_delete(p_rg);
2406 }
2407 
2408 /*---------------------------------------------------------------------------*/
2423 /*---------------------------------------------------------------------------*/
2424 
2425 extern void vircam_jmp_illum(struct psStruct* ps, char* cat, char* catpath) {
2426  int ngood,i,status,ii;
2427  float illcor_rms,gain;
2428  casu_fits **ftmp,*ff,*ffc;
2429  char filt[32];
2430  cpl_table **mstds,*stdscat,*ms,*illcor,*ot;
2431  casu_tfits *outtab;
2432  cpl_propertylist **pl,*phu,*ehu;
2433  const char *fctid = "vircam_jmp_illum";
2434 
2435  /* Set some default values */
2436 
2437  (*ps).illcor = NULL;
2438 
2439  /* Sort out all images with good status */
2440 
2441  ftmp = cpl_malloc((*ps).nscience*sizeof(casu_fits *));
2442  ngood = 0;
2443  for (i = 0; i < (*ps).nscience; i++) {
2444  ff = (*ps).sci_fits[i];
2445  if (casu_fits_get_status(ff) != CASU_FATAL)
2446  ftmp[ngood++] = ff;
2447  }
2448 
2449  /* If there aren't any good images, then get out of here now */
2450 
2451  if (ngood == 0) {
2452  freespace(ftmp);
2453  cpl_msg_error(fctid,"Illumination correction impossible. No good science frames available");
2454  return;
2455  }
2456 
2457  /* What filter is this? */
2458 
2459  if (vircam_pfits_get_filter(casu_fits_get_phu(ftmp[0]),filt) != CASU_OK) {
2460  cpl_msg_error(fctid,"No filter name in stack header");
2461  freespace(ftmp);
2462  return;
2463  }
2464 
2465  /* Get some workspace for the various arrays you need for the
2466  illumination correction routine */
2467 
2468  mstds = cpl_malloc(ngood*sizeof(cpl_table *));
2469  for (i = 0; i < ngood; i++)
2470  mstds[i] = NULL;
2471  pl = cpl_malloc(ngood*sizeof(cpl_propertylist *));
2472  for (i = 0; i < ngood; i++)
2473  pl[i] = NULL;
2474 
2475  /* For each of the good input frames, do a catalogue generation and
2476  get some matched standards */
2477 
2478  ffc = (*ps).fconf;
2479  for (i = 0; i < ngood; i++) {
2480  status = CASU_OK;
2481  ff = ftmp[i];
2482  gain = cpl_propertylist_get_float(casu_fits_get_ehu(ff),"GAIN");
2483  (void)casu_imcore(ff,ffc,vircam_jmp_config.ipix,
2484  1.5*vircam_jmp_config.threshold,0,
2485  vircam_jmp_config.rcore,vircam_jmp_config.nbsize,2,
2486  3.5,&outtab,gain,&status);
2487  if (status != CASU_OK)
2488  cpl_error_reset();
2489  pl[i] = cpl_propertylist_duplicate(casu_tfits_get_ehu(outtab));
2490 
2491  /* Get some standard stars */
2492 
2493  (void)casu_getstds(casu_fits_get_ehu(ff),1,catpath,
2494  cat,0,".",&stdscat,&status);
2495  if (status == CASU_FATAL) {
2496  freetfits(outtab);
2497  freespace(ftmp);
2498  for (ii = 0; ii < ngood; ii++) {
2499  freetable(mstds[ii]);
2500  freepropertylist(pl[ii]);
2501  }
2502  freespace(mstds);
2503  freespace(pl);
2504  cpl_msg_error(fctid,"Illumination correction fails");
2505  return;
2506  } else if (status == CASU_WARN) {
2507  cpl_error_reset();
2508  freetfits(outtab);
2509  continue;
2510  }
2511 
2512  /* Now match this against the catalogue */
2513 
2514  ot = casu_tfits_get_table(outtab);
2515  (void)casu_matchstds(ot,stdscat,300.0,&ms,&status);
2516  if (status == CASU_FATAL) {
2517  freetable(stdscat);
2518  freetfits(outtab);
2519  freespace(ftmp);
2520  for (ii = 0; ii < ngood; ii++) {
2521  freetable(mstds[ii]);
2522  freepropertylist(pl[ii]);
2523  }
2524  freespace(mstds);
2525  freespace(pl);
2526  cpl_msg_error(fctid,"%s",cpl_error_get_message());
2527  return;
2528  }
2529  mstds[i] = ms;
2530  freetfits(outtab);
2531  freetable(stdscat);
2532  }
2533 
2534  /* Call the illumination routine */
2535 
2536  status = CASU_OK;
2537  (void)vircam_illum(ftmp,mstds,pl,ngood,filt,(*ps).tphottab,128,&illcor,
2538  &illcor_rms,&status);
2539 
2540  /* Wrap the result */
2541 
2542  phu = cpl_propertylist_duplicate(casu_fits_get_phu(ftmp[0]));
2543  ehu = cpl_propertylist_duplicate(casu_fits_get_ehu(ftmp[0]));
2544  (*ps).illcor = casu_tfits_wrap(illcor,NULL,phu,ehu);
2545  cpl_propertylist_update_float(ehu,"ESO QC ILLUMCOR_RMS",illcor_rms);
2546  cpl_propertylist_set_comment(ehu,"ESO QC ILLUMCOR_RMS",
2547  "RMS of illumination correction map");
2548 
2549  /* Tidy up */
2550 
2551  for (i = 0; i < ngood; i++) {
2552  freetable(mstds[i]);
2553  freepropertylist(pl[i]);
2554  }
2555  freespace(mstds);
2556  freespace(pl);
2557  freespace(ftmp);
2558 }
2559 
2560 /*---------------------------------------------------------------------------*/
2582 /*---------------------------------------------------------------------------*/
2583 
2584 static char *vircam_jmp_outfile(const char *bname, int ind, int isfits) {
2585  int nf;
2586  char *fname;
2587 
2588  /* Count up how much space you need for the output string. The 'isfits'
2589  block accounts for ".fits", an underscore and an EOS*/
2590 
2591  nf = strlen(bname);
2592  if (ind == 0)
2593  nf++;
2594  else
2595  nf += ((int)log10((double)ind)+1);
2596  if (isfits)
2597  nf += 7;
2598  else
2599  nf += 2;
2600 
2601  /* Get the space for the filename */
2602 
2603  fname = cpl_malloc(nf);
2604 
2605  /* Now write the name */
2606 
2607  if (isfits)
2608  (void)snprintf(fname,nf,"%s_%d.fits",bname,ind);
2609  else
2610  (void)snprintf(fname,nf,"%s_%d",bname,ind);
2611  return(fname);
2612 }
2613 
2614 /*---------------------------------------------------------------------------*/
2629 /*---------------------------------------------------------------------------*/
2630 
2631 extern void vircam_jmp_init(struct psStruct* ps) {
2632 
2633  /* Level 0 stuff */
2634 
2635  (*ps).labels = NULL;
2636  (*ps).master_dark = NULL;
2637  (*ps).master_twilight_flat = NULL;
2638  (*ps).master_conf = NULL;
2639  (*ps).master_sky = NULL;
2640  (*ps).mask = NULL;
2641  (*ps).chantab = NULL;
2642  (*ps).phottab = NULL;
2643  (*ps).tphottab = NULL;
2644  (*ps).readgain_file = NULL;
2645  (*ps).science_frames = NULL;
2646  (*ps).offset_skies = NULL;
2647  (*ps).product_frames_simple = NULL;
2648  (*ps).product_frames_simple_off = NULL;
2649  (*ps).product_frames_super = NULL;
2650  (*ps).product_frames_superc = NULL;
2651  (*ps).product_frame_stack = NULL;
2652  (*ps).product_frame_stackc = NULL;
2653  (*ps).product_frame_cat = NULL;
2654  (*ps).product_frame_illcor = NULL;
2655  (*ps).product_frame_sky = NULL;
2656  (*ps).phupaf = NULL;
2657  (*ps).gaincors = NULL;
2658  (*ps).catpath = NULL;
2659  (*ps).catname = NULL;
2660  (*ps).catpath2 = NULL;
2661  (*ps).catname2 = NULL;
2662 
2663  /* Level 1 stuff */
2664 
2665  (*ps).fdark = NULL;
2666  (*ps).fflat = NULL;
2667  (*ps).fconf = NULL;
2668  (*ps).fsky = NULL;
2669  (*ps).fchantab = NULL;
2670  (*ps).nscience = 0;
2671  (*ps).sci_fits = NULL;
2672  (*ps).noffsets = 0;
2673  (*ps).offsky_fits = NULL;
2674  (*ps).nustep_sets = 0;
2675  (*ps).ustep_sets = NULL;
2676  (*ps).ndith = 0;
2677  (*ps).ndithc = 0;
2678  (*ps).dith_input = NULL;
2679  (*ps).dithc_input = NULL;
2680  (*ps).stack_frame = NULL;
2681  (*ps).stackc_frame = NULL;
2682  (*ps).outcat = NULL;
2683  (*ps).outsky = NULL;
2684  (*ps).matchstds = NULL;
2685  (*ps).illcor = NULL;
2686 }
2687 
2688 /*---------------------------------------------------------------------------*/
2709 /*---------------------------------------------------------------------------*/
2710 
2711 extern void vircam_jmp_tidy(struct psStruct* ps, int level) {
2712  int i;
2713 
2714  /* Level 1 stuff */
2715 
2716  freefits((*ps).fdark);
2717  freefits((*ps).fflat);
2718  freefits((*ps).fconf);
2719  freefits((*ps).fsky);
2720  freetfits((*ps).fchantab);
2721  freefitslist((*ps).sci_fits,(*ps).nscience);
2722  freefitslist((*ps).offsky_fits,(*ps).noffsets);
2723  (*ps).nscience = 0;
2724  for (i = 0; i < (*ps).nustep_sets; i++) {
2725  freespace((*ps).ustep_sets[i].f);
2726  freefits((*ps).ustep_sets[i].super);
2727  freefits((*ps).ustep_sets[i].superc);
2728  }
2729  freespace((*ps).ustep_sets);
2730  (*ps).nustep_sets = 0;
2731  freespace((*ps).dith_input);
2732  (*ps).ndith = 0;
2733  freespace((*ps).dithc_input);
2734  (*ps).ndithc = 0;
2735 
2736  freefits((*ps).stack_frame);
2737  freefits((*ps).stackc_frame);
2738  if(ps->outcat !=NULL) freetfits((*ps).outcat);
2739  if(ps->matchstds!=NULL) freetable((*ps).matchstds);
2740  freetfits((*ps).illcor);
2741  freefits((*ps).outsky);
2742 
2743  if (level == 1)
2744  return;
2745 
2746  /* Level 0 stuff */
2747 
2748  freespace((*ps).labels);
2749  freeframe((*ps).master_dark);
2750  freeframe((*ps).master_twilight_flat);
2751  freeframe((*ps).master_conf);
2752  freeframe((*ps).master_sky);
2753  freemask((*ps).mask);
2754  freeframe((*ps).chantab);
2755  freeframe((*ps).phottab);
2756  freeframe((*ps).readgain_file);
2757  freetable((*ps).tphottab);
2758  freeframeset((*ps).science_frames);
2759  freeframeset((*ps).offset_skies);
2760  freepropertylist((*ps).phupaf);
2761  freespace((*ps).product_frames_simple); /* NB: We only have to delete */
2762  freespace((*ps).product_frames_super); /* the arrays and not the frames */
2763  freespace((*ps).product_frames_superc); /* as these get passed back */
2764  freespace((*ps).product_frames_simple_off); /* to esorex */
2765  freespace((*ps).gaincors);
2766  freespace((*ps).catpath);
2767  freespace((*ps).catname);
2768  freespace((*ps).catpath2);
2769  freespace((*ps).catname2);
2770 }
2771 
2774 /*
2775 
2776 $Log: not supported by cvs2svn $
2777 Revision 1.56 2010/09/13 11:50:18 jim
2778 removed unnecessary declaration
2779 
2780 Revision 1.55 2010/09/13 11:40:33 jim
2781 Fixed the way DRS SKYCOR is written to header
2782 
2783 Revision 1.54 2010/09/10 11:24:35 jim
2784 Modified skycor so that if a master sky exists, then it uses it rather than
2785 create a new one.
2786 
2787 Revision 1.53 2010/06/07 12:42:40 jim
2788 Modifications to get rid of compiler gripes
2789 
2790 Revision 1.52 2010/06/03 11:33:29 jim
2791 Opened up matching radius in call to vircam_matchxy to deal with the case
2792 where the input WCS is poorly defined.
2793 
2794 Revision 1.51 2010/02/08 16:35:26 jim
2795 Moved the definition of propertylist (*ps).phupaf to the recipes so that
2796 this doesn't fail when we don't save the simple images
2797 
2798 Revision 1.50 2010/02/08 10:50:09 jim
2799 Fixed GAIN -> CONAD bug
2800 
2801 Revision 1.49 2010/01/31 19:28:46 jim
2802 SKYMED and SKYSIG are moved from DRS to QC in offset skies
2803 
2804 Revision 1.48 2009/11/18 21:08:46 jim
2805 Mods made to wcsfit to restrict standards to those with good photometry and to
2806 limit the number of entries in catalogue to use in matching up with the
2807 standards
2808 
2809 Revision 1.47 2009/09/22 12:29:12 jim
2810 Modified to do offset sky exposures
2811 
2812 Revision 1.46 2009/09/21 14:45:54 jim
2813 Fixed bug in _save_simple where template frame wasn't being defined for
2814 each image extension
2815 
2816 Revision 1.45 2009/09/09 09:46:33 jim
2817 modified in attempt to get headers right
2818 
2819 Revision 1.44 2008/12/09 11:36:45 jim
2820 Fixed _illcor routine so that if the imcore fails the cpl error is reset
2821 
2822 Revision 1.43 2008/12/08 13:04:24 jim
2823 Fixed PRO CATG for catalogue and illumination correction pafs
2824 
2825 Revision 1.42 2008/12/08 06:41:18 jim
2826 Fixed problem where wrong PRO CATG was being written to pafs
2827 
2828 Revision 1.41 2008/11/27 09:13:47 jim
2829 Changed PRO CATG values for interleaved confidence maps so they are different
2830 for science and standard recipes
2831 
2832 Revision 1.40 2008/11/25 18:54:45 jim
2833 removed extra sky stuff
2834 
2835 Revision 1.39 2008/11/25 11:56:38 jim
2836 Routine skycor now allows for either standard or masked object sky correction.
2837 Modification was done to routine which saves superframes to define the
2838 output confidence map as officially a different type from the confidence
2839 maps generated from jittering.
2840 
2841 Revision 1.38 2008/11/21 10:10:06 jim
2842 Patched in newer version of vircam_matchxy
2843 
2844 Revision 1.37 2008/09/30 11:35:01 jim
2845 Added PRO CATG to the pafs
2846 
2847 Revision 1.36 2008/09/29 11:29:46 jim
2848 Some cosmetic fixes. Also define a maximum shift between dither frames
2849 
2850 Revision 1.35 2008/08/28 09:05:37 jim
2851 Fixed bug where QC was being duplicated from master BPM on rare occasions.
2852 Fixed bug where ARCFILE wasn't being written to the paf file for illum_cor
2853 tables. Sky combine is done with medians
2854 
2855 Revision 1.34 2008/08/05 14:07:01 jim
2856 Relaxed matching criteria
2857 
2858 Revision 1.33 2008/07/10 13:05:53 jim
2859 Modified to use v4.2 version of cpl_wcs
2860 
2861 Revision 1.32 2008/06/20 11:13:35 jim
2862 Fixed dodgy call to cpl_wcs_get_image_dims
2863 
2864 Revision 1.31 2008/05/06 08:40:10 jim
2865 Modified to use cpl_wcs interface
2866 
2867 Revision 1.30 2007/11/22 12:34:08 jim
2868 Added line to vircam_jmp_save_illum to make sure that phupaf is defined
2869 when _save_simple isn't called
2870 
2871 Revision 1.29 2007/10/25 17:34:00 jim
2872 Modified to remove lint warnings
2873 
2874 Revision 1.28 2007/10/19 09:25:10 jim
2875 Fixed problems with missing includes
2876 
2877 Revision 1.27 2007/10/19 06:55:06 jim
2878 Modifications made to use new method for directing the recipes to the
2879 standard catalogues using the sof
2880 
2881 Revision 1.26 2007/10/15 12:50:28 jim
2882 Modified for compatibility with cpl_4.0
2883 
2884 Revision 1.25 2007/06/13 08:10:49 jim
2885 Modified to allow for different output file names depending upon the calling
2886 recipe
2887 
2888 Revision 1.24 2007/05/15 08:54:07 jim
2889 Fixed small bug in stack_save and dither_offsets. Also modified dither_offsets
2890 to just send back zeros if there is only one dither frame
2891 
2892 Revision 1.23 2007/05/08 10:41:49 jim
2893 Added gaincor variables
2894 
2895 Revision 1.22 2007/05/02 09:15:37 jim
2896 Modified to use new api for vircam_imcore and vircam_platesol
2897 
2898 Revision 1.21 2007/04/30 09:40:01 jim
2899 Added vircam_paf_append
2900 
2901 Revision 1.20 2007/04/13 12:29:11 jim
2902 Fixed bug in save_simple which mean that (*ps).pafphu was allocated for every
2903 file rather than just the first one
2904 
2905 Revision 1.19 2007/04/04 16:05:59 jim
2906 Modified to make paf information a bit more correct
2907 
2908 Revision 1.18 2007/04/04 10:34:55 jim
2909 Modified to use new dfs tags
2910 
2911 Revision 1.17 2007/03/14 22:08:54 jim
2912 Fixed typo
2913 
2914 Revision 1.16 2007/03/14 14:49:13 jim
2915 Fixed problem with missing paf files in jmp recipes if detlive = F. Also
2916 fixed problem where extra dummy products were being created
2917 
2918 Revision 1.15 2007/03/13 09:50:53 jim
2919 Fixed bug in vircam_jmp_save_illum where PAF wasn't being saved for the
2920 first extension
2921 
2922 Revision 1.14 2007/03/06 14:06:24 jim
2923 Fixed missing paf write
2924 
2925 Revision 1.13 2007/03/06 13:49:48 jim
2926 Created static routine vircam_jmp_outfile to create a 'predictable'
2927 output filename for each product
2928 
2929 Revision 1.12 2007/03/01 12:42:42 jim
2930 Modified slightly after code checking
2931 
2932 Revision 1.11 2007/02/25 06:34:20 jim
2933 Plugged memory leak
2934 
2935 Revision 1.10 2007/02/15 06:59:38 jim
2936 Added ability to write QC paf files
2937 
2938 Revision 1.9 2007/02/06 13:11:12 jim
2939 Fixed entry for PRO dictionary in cpl_dfs_set_product_header
2940 
2941 Revision 1.8 2007/01/17 23:54:00 jim
2942 Plugged some memory leaks
2943 
2944 Revision 1.7 2006/12/19 13:30:01 jim
2945 Fixed vircam_jmp_illum to initialise pointers
2946 
2947 Revision 1.6 2006/12/18 12:51:20 jim
2948 Tightened up some of the error reporting
2949 
2950 Revision 1.5 2006/12/15 09:58:28 jim
2951 read noise and gain keywords were wrong...
2952 
2953 Revision 1.4 2006/11/29 12:28:45 jim
2954 Modified so that the correct recipe names would appear in the headers of
2955 data products
2956 
2957 Revision 1.3 2006/11/28 22:10:22 jim
2958 Added illcor_rms to calling sequence of vircam_illum
2959 
2960 Revision 1.2 2006/11/28 20:56:31 jim
2961 Added vircam_jmp_illum and vircam_jmp_save_illum
2962 
2963 Revision 1.1 2006/11/27 11:54:37 jim
2964 Initial entry
2965 
2966 
2967 */
2968 
int casu_fits_get_status(casu_fits *p)
Definition: casu_fits.c:711
cpl_image * casu_fits_get_image(casu_fits *p)
Definition: casu_fits.c:436
char * casu_fits_get_filename(casu_fits *p)
Definition: casu_fits.c:646
char * casu_fits_get_fullname(casu_fits *p)
Definition: casu_fits.c:680
int casu_fits_set_error(casu_fits *p, int status)
Definition: casu_fits.c:747
casu_fits * casu_fits_wrap(cpl_image *im, casu_fits *model, cpl_propertylist *phu, cpl_propertylist *ehu)
Definition: casu_fits.c:883
cpl_propertylist * casu_fits_get_phu(casu_fits *p)
Definition: casu_fits.c:531
int casu_fits_get_nexten(casu_fits *p)
Definition: casu_fits.c:497
cpl_propertylist * casu_fits_get_ehu(casu_fits *p)
Definition: casu_fits.c:576
int casu_imdither(casu_fits **inf, casu_fits **inconf, int nimages, int nconfs, float lthr, float hthr, cpl_propertylist **p, const char *expkey, cpl_image **out, cpl_image **outc, int *status)
Dither a set of jittered observations.
int casu_imcore(casu_fits *infile, casu_fits *conf, int ipix, float threshold, int icrowd, float rcore, int nbsize, int cattype, float filtfwhm, casu_tfits **outtab, float gainloc, int *status)
Generate object catalogues from input images.
Definition: casu_imcore.c:149
int casu_imcombine(casu_fits **fset, casu_fits **fsetv, int nfits, int combtype, int scaletype, int xrej, float thresh, const char *expkey, cpl_image **outimage, cpl_image **outvimage, unsigned char **rejmask, unsigned char **rejplus, cpl_propertylist **drs, int *status)
Stack images into a mean or median image with rejection.
int casu_matchstds(cpl_table *objtab, cpl_table *stdstab, float srad, cpl_table **outtab, int *status)
Match object and standard star tables by their xy coordinates.
Definition: casu_match.c:300
int casu_matchxy(cpl_table *progtab, cpl_table *template, float srad, float *xoffset, float *yoffset, int *nm, cpl_table **outtab, int *status)
Match two lists of x,y coordinates from two tables to find the cartesian offset between them.
Definition: casu_match.c:105
int casu_photcal_extinct(casu_fits **images, casu_tfits **mstds, casu_tfits **cats, int nimages, char *filt, cpl_table *phottab, int minstars, cpl_frame *schlf_n, cpl_frame *schlf_s, const char *expkey, const char *amkey, float magerrcut, int *status)
Do photometric calibration.
int casu_platesol(cpl_propertylist *plist, cpl_propertylist *tlist, cpl_table *matchedstds, int nconst, int shiftan, int *status)
Work out a WCS for an image.
int casu_getstds(cpl_propertylist *plist, int cache, char *path, char *catname, int cdssearch, char *cacheloc, cpl_table **stds, int *status)
Get a table of standard stars that appear on an image from a catalogue.
Definition: casu_getstds.c:159
float casu_med(float *data, unsigned char *bpm, long npts)
Definition: casu_stats.c:89
void casu_qmedsig(float *data, unsigned char *bpm, long npts, float thresh, int niter, float lowv, float highv, float *median, float *sigma)
Definition: casu_stats.c:258
void casu_tfits_delete(casu_tfits *p)
Definition: casu_tfits.c:292
cpl_table * casu_tfits_get_table(casu_tfits *p)
Definition: casu_tfits.c:364
cpl_propertylist * casu_tfits_get_phu(casu_tfits *p)
Definition: casu_tfits.c:432
cpl_propertylist * casu_tfits_get_ehu(casu_tfits *p)
Definition: casu_tfits.c:473
casu_tfits * casu_tfits_wrap(cpl_table *tab, casu_tfits *model, cpl_propertylist *phu, cpl_propertylist *ehu)
Definition: casu_tfits.c:739
cpl_image * casu_dummy_image(casu_fits *model)
Create a dummy image of zeros based on a model.
Definition: casu_utils.c:533
void casu_merge_propertylists(cpl_propertylist *p1, cpl_propertylist *p2)
Merge two propertylists.
Definition: casu_utils.c:399
long casu_getnpts(cpl_image *in)
Get the number of pixels in a 2d image.
Definition: casu_utils.c:243
void casu_dummy_property(cpl_propertylist *p)
Set dummy property keyword.
Definition: casu_utils.c:445
cpl_table * casu_dummy_catalogue(int type)
Create a dummy catalogue of a given type with no rows.
Definition: casu_utils.c:570
int casu_removewcs(cpl_propertylist *p, int *status)
int casu_diffxywcs(cpl_wcs *wcs, cpl_wcs *wcsref, float *xoff, float *yoff, int *status)
void casu_radectoxy(cpl_wcs *wcs, double ra, double dec, double *x, double *y)
void casu_xytoradec(cpl_wcs *wcs, double x, double y, double *ra, double *dec)
Definition: casu_wcsutils.c:93
int vircam_illum(casu_fits **images, cpl_table **mstds, cpl_propertylist **pl, int nimages, char *filt, cpl_table *phottab, int nbsize, cpl_table **illcor, float *illcor_rms, int *status)
Work out the illumination correction.
int vircam_interleave(casu_fits **infiles, int ninputs, casu_fits **inconfs, int nconfs, int nsteps, cpl_propertylist **p, cpl_image **outimage, cpl_image **outconf, int *status)
Interleave a set of microstepped observations.
void vircam_dfs_set_product_primary_header(cpl_propertylist *plist, cpl_frame *frame, cpl_frameset *frameset, cpl_parameterlist *parlist, char *recipeid, const char *dict, cpl_frame *inherit, int synch)
Definition: vircam_dfs.c:227
void vircam_dfs_set_product_exten_header(cpl_propertylist *plist, cpl_frame *frame, cpl_frameset *frameset, cpl_parameterlist *parlist, char *recipeid, const char *dict, cpl_frame *inherit)
Definition: vircam_dfs.c:299
int vircam_jmp_save_offsky(struct psStruct *ps, cpl_frameset *framelist, cpl_parameterlist *parlist)
void vircam_jmp_dither_images(struct psStruct *ps)
void vircam_jmp_init(struct psStruct *ps)
void vircam_jmp_get_readnoise_gain(struct psStruct *ps, int jext, float *readnoise, float *gain)
void vircam_jmp_matched_stds(struct psStruct *ps, char *cata, char *catapath)
void vircam_jmp_ustep_seq(struct psStruct *ps)
int vircam_jmp_save_super(struct psStruct *ps, cpl_frameset *framelist, cpl_parameterlist *parlist)
void vircam_jmp_bpm2conf(struct psStruct *ps)
int vircam_jmp_save_catalogue(struct psStruct *ps, cpl_frameset *framelist, cpl_parameterlist *parlist, cpl_propertylist *dummyqcprops)
void vircam_jmp_wcsfit(struct psStruct *ps)
void vircam_jmp_photcal(struct psStruct *ps)
int vircam_jmp_save_stack(struct psStruct *ps, cpl_frameset *framelist, cpl_parameterlist *parlist, cpl_propertylist *dummyqcprops)
int vircam_jmp_save_simple_offsets(struct psStruct *ps, cpl_frameset *framelist, cpl_parameterlist *parlist)
void vircam_jmp_illum(struct psStruct *ps, char *cat, char *catpath)
void vircam_jmp_dither_offsets(struct psStruct *ps, int inter_lv)
void vircam_jmp_tidy(struct psStruct *ps, int level)
int vircam_jmp_save_illum(struct psStruct *ps, cpl_frameset *framelist, cpl_parameterlist *parlist, cpl_propertylist *dummyqcprops)
int vircam_jmp_save_simple(struct psStruct *ps, cpl_frameset *framelist, cpl_parameterlist *parlist)
void vircam_jmp_skycor(struct psStruct *ps)
void vircam_jmp_interleave(struct psStruct *ps)
void vircam_jmp_catalogue(struct psStruct *ps, int inter_lv)
int vircam_pfits_get_nusteps(const cpl_propertylist *plist, int *nusteps)
Get the value of the number of microsteps in a sequence.
Definition: vircam_pfits.c:422
int vircam_pfits_get_ustepnum(const cpl_propertylist *plist, int *ustepnum)
Get the value of the first run number in the current microstep sequence.
Definition: vircam_pfits.c:449
int vircam_pfits_get_filter(const cpl_propertylist *plist, char *filt)
Get the name of the current filter.
Definition: vircam_pfits.c:649
cpl_table * vircam_illcor_newtab(int nrows)