38 #include "vircam_utils.h"
39 #include "vircam_pfits.h"
40 #include "vircam_dfs.h"
41 #include "vircam_mods.h"
42 #include "vircam_stats.h"
43 #include "vircam_fits.h"
44 #include "vircam_tfits.h"
45 #include "vircam_wcsutils.h"
46 #include "vircam_paf.h"
47 #include "vircam_sky.h"
49 #include "vircam_jmp_utils.h"
51 static char *vircam_jmp_outfile(
const char *bname,
int ind,
int isfits);
88 cpl_parameterlist *parlist) {
89 cpl_propertylist *plist;
91 cpl_frame *product_frame,*
template;
93 const char *base[] = {
"",
"simple_jmp",
"simple_std",
"simple_mes"};
94 const char *fctid =
"vircam_jmp_save_simple";
98 if (ps.product_frames_simple == NULL)
99 ps.product_frames_simple = cpl_malloc(ps.nscience*
sizeof(cpl_frame *));
103 for (i = 0; i < ps.nscience; i++) {
104 fname = vircam_jmp_outfile(base[recflag],i,1);
106 template = cpl_frameset_get_frame(ps.science_frames,i);
115 product_frame = cpl_frame_new();
116 cpl_frame_set_filename(product_frame,fname);
119 cpl_frame_set_tag(product_frame,VIRCAM_PRO_SIMPLE_SCI);
122 cpl_frame_set_tag(product_frame,VIRCAM_PRO_SIMPLE_STD);
125 cpl_frame_set_tag(product_frame,VIRCAM_PRO_SIMPLE_SCI);
128 cpl_frame_set_type(product_frame,CPL_FRAME_TYPE_IMAGE);
129 cpl_frame_set_group(product_frame,CPL_FRAME_GROUP_PRODUCT);
130 cpl_frame_set_level(product_frame,CPL_FRAME_LEVEL_FINAL);
138 "PRO-1.15",
template,1);
142 if (cpl_image_save(NULL,fname,CPL_TYPE_UCHAR,plist,
143 CPL_IO_DEFAULT) != CPL_ERROR_NONE) {
144 cpl_msg_error(fctid,
"Cannot save product PHU");
145 cpl_frame_delete(product_frame);
149 cpl_frameset_insert(framelist,product_frame);
150 ps.product_frames_simple[i] = product_frame;
161 product_frame = ps.product_frames_simple[i];
163 parlist,vircam_recipename,
164 "PRO-1.15",
template);
166 CPL_TYPE_FLOAT,plist,
167 CPL_IO_EXTEND) != CPL_ERROR_NONE) {
168 cpl_msg_error(fctid,
"Cannot save product image extension");
205 cpl_parameterlist *parlist) {
206 cpl_propertylist *plist;
208 cpl_frame *product_frame,*
template;
210 const char *base =
"simple_off";
211 const char *fctid =
"vircam_jmp_save_simple_offsets";
215 if (ps.product_frames_simple_off == NULL)
216 ps.product_frames_simple_off =
217 cpl_malloc(ps.noffsets*
sizeof(cpl_frame *));
221 for (i = 0; i < ps.noffsets; i++) {
222 fname = vircam_jmp_outfile(base,i,1);
224 template = cpl_frameset_get_frame(ps.offset_skies,i);
233 product_frame = cpl_frame_new();
234 cpl_frame_set_filename(product_frame,fname);
235 cpl_frame_set_tag(product_frame,VIRCAM_PRO_SIMPLE_SKY);
236 cpl_frame_set_type(product_frame,CPL_FRAME_TYPE_IMAGE);
237 cpl_frame_set_group(product_frame,CPL_FRAME_GROUP_PRODUCT);
238 cpl_frame_set_level(product_frame,CPL_FRAME_LEVEL_FINAL);
246 "PRO-1.15",
template,1);
250 if (cpl_image_save(NULL,fname,CPL_TYPE_UCHAR,plist,
251 CPL_IO_DEFAULT) != CPL_ERROR_NONE) {
252 cpl_msg_error(fctid,
"Cannot save product PHU");
253 cpl_frame_delete(product_frame);
257 cpl_frameset_insert(framelist,product_frame);
258 ps.product_frames_simple_off[i] = product_frame;
269 product_frame = ps.product_frames_simple_off[i];
271 parlist,vircam_recipename,
272 "PRO-1.15",
template);
274 CPL_TYPE_FLOAT,plist,
275 CPL_IO_EXTEND) != CPL_ERROR_NONE) {
276 cpl_msg_error(fctid,
"Cannot save product image extension");
313 cpl_parameterlist *parlist) {
318 cpl_frame *product_frame,*
template;
320 const char *base =
"offsky";
321 const char *fctid =
"vircam_jmp_save_offsky";
329 template = cpl_frameset_get_frame(ps.offset_skies,0);
333 fname = vircam_jmp_outfile(base,0,1);
342 product_frame = cpl_frame_new();
343 cpl_frame_set_filename(product_frame,fname);
344 cpl_frame_set_tag(product_frame,VIRCAM_PRO_OFFSET_SKY);
345 cpl_frame_set_type(product_frame,CPL_FRAME_TYPE_IMAGE);
346 cpl_frame_set_group(product_frame,CPL_FRAME_GROUP_PRODUCT);
347 cpl_frame_set_level(product_frame,CPL_FRAME_LEVEL_FINAL);
348 ps.product_frame_sky = product_frame;
354 parlist,vircam_recipename,
355 "PRO-1.15",
template,0);
359 if (cpl_image_save(NULL,fname,CPL_TYPE_UCHAR,p,
360 CPL_IO_DEFAULT) != CPL_ERROR_NONE) {
361 cpl_msg_error(fctid,
"Cannot save product PHU");
362 cpl_frame_delete(product_frame);
366 cpl_frameset_insert(framelist,product_frame);
377 product_frame = ps.product_frame_sky;
379 vircam_recipename,
"PRO-1.15",
template);
380 if (cpl_image_save(fim,fname,CPL_TYPE_FLOAT,p,CPL_IO_EXTEND) !=
382 cpl_msg_error(fctid,
"Cannot save product image extension");
389 cpl_propertylist_delete(p);
421 cpl_parameterlist *parlist) {
422 cpl_propertylist *plist,*p;
423 int i,isdummy,isdummyc,j,jref;
424 cpl_frame *product_frame,*
template;
426 const char *base[] = {
"",
"super_jmp",
"super_std",
"super_mes"};
427 const char *basec[] = {
"",
"superc_jmp",
"superc_std",
"superc_mes"};
429 cpl_image *fim,*fimc;
430 const char *fctid =
"vircam_jmp_save_super";
434 if (ps.product_frames_super == NULL)
435 ps.product_frames_super = cpl_malloc(ps.nustep_sets*
sizeof(cpl_frame *));
436 if (ps.product_frames_superc == NULL)
437 ps.product_frames_superc = cpl_malloc(ps.nustep_sets*
sizeof(cpl_frame *));
441 for (i = 0; i < ps.nustep_sets; i++) {
447 ff = ps.ustep_sets[i].super;
449 ffc = ps.ustep_sets[i].superc;
456 fname = vircam_jmp_outfile(base[recflag],i,1);
457 fnamec = vircam_jmp_outfile(basec[recflag],i,1);
462 for (j = 0; j < ps.ustep_sets[i].nframes; j++) {
463 if (ps.sci_fits[j] == ps.ustep_sets[i].f[j]) {
469 template = cpl_frameset_get_frame(ps.science_frames,jref);
480 product_frame = cpl_frame_new();
481 cpl_frame_set_filename(product_frame,fname);
484 cpl_frame_set_tag(product_frame,VIRCAM_PRO_INTER_SCI);
487 cpl_frame_set_tag(product_frame,VIRCAM_PRO_INTER_STD);
490 cpl_frame_set_tag(product_frame,VIRCAM_PRO_INTER_SCI);
493 cpl_frame_set_type(product_frame,CPL_FRAME_TYPE_IMAGE);
494 cpl_frame_set_group(product_frame,CPL_FRAME_GROUP_PRODUCT);
495 cpl_frame_set_level(product_frame,CPL_FRAME_LEVEL_FINAL);
503 "PRO-1.15",
template,1);
507 if (cpl_image_save(NULL,fname,CPL_TYPE_UCHAR,plist,
508 CPL_IO_DEFAULT) != CPL_ERROR_NONE) {
509 cpl_msg_error(fctid,
"Cannot save product PHU");
510 cpl_frame_delete(product_frame);
515 cpl_frameset_insert(framelist,product_frame);
516 ps.product_frames_super[i] = product_frame;
520 product_frame = cpl_frame_new();
521 cpl_frame_set_filename(product_frame,fnamec);
524 cpl_frame_set_tag(product_frame,VIRCAM_PRO_CONF_INTER_SCI);
527 cpl_frame_set_tag(product_frame,VIRCAM_PRO_CONF_INTER_STD);
530 cpl_frame_set_tag(product_frame,VIRCAM_PRO_CONF_INTER_SCI);
533 cpl_frame_set_type(product_frame,CPL_FRAME_TYPE_IMAGE);
534 cpl_frame_set_group(product_frame,CPL_FRAME_GROUP_PRODUCT);
535 cpl_frame_set_level(product_frame,CPL_FRAME_LEVEL_FINAL);
543 "PRO-1.15",
template,1);
547 if (cpl_image_save(NULL,fnamec,CPL_TYPE_UCHAR,plist,
548 CPL_IO_DEFAULT) != CPL_ERROR_NONE) {
549 cpl_msg_error(fctid,
"Cannot save product PHU");
550 cpl_frame_delete(product_frame);
555 cpl_frameset_insert(framelist,product_frame);
556 ps.product_frames_superc[i] = product_frame;
568 product_frame = ps.product_frames_super[i];
570 vircam_recipename,
"PRO-1.15",
572 if (cpl_image_save(fim,fname,CPL_TYPE_FLOAT,p,CPL_IO_EXTEND) !=
574 cpl_msg_error(fctid,
"Cannot save product image extension");
579 cpl_propertylist_delete(p);
589 product_frame = ps.product_frames_superc[i];
591 vircam_recipename,
"PRO-1.15",
593 if (cpl_image_save(fimc,fnamec,CPL_TYPE_SHORT,p,CPL_IO_EXTEND) !=
595 cpl_msg_error(fctid,
"Cannot save confidence map image extension");
600 cpl_propertylist_delete(p);
635 cpl_parameterlist *parlist) {
636 cpl_propertylist *plist,*p,*pafprop;
637 int isdummy,isdummyc;
639 cpl_image *fim,*fimc;
640 cpl_frame *product_frame;
641 char *fname,*fnamec,*fnamepaf;
642 const char *base[] = {
"",
"stack_jmp",
"stack_std",
"stack_mes"};
643 const char *basec[] = {
"",
"stackc_jmp",
"stackc_std",
"stackc_mes"};
644 const char *fctid =
"vircam_jmp_save_stack";
652 ffc = ps.stackc_frame;
659 fname = vircam_jmp_outfile(base[recflag],0,1);
660 fnamec = vircam_jmp_outfile(basec[recflag],0,1);
661 fnamepaf = vircam_jmp_outfile(base[recflag],0,0);
670 product_frame = cpl_frame_new();
671 cpl_frame_set_filename(product_frame,fname);
674 cpl_frame_set_tag(product_frame,VIRCAM_PRO_JITTERED_SCI);
677 cpl_frame_set_tag(product_frame,VIRCAM_PRO_JITTERED_STD);
680 cpl_frame_set_tag(product_frame,VIRCAM_PRO_JITTERED_SCI);
683 cpl_frame_set_type(product_frame,CPL_FRAME_TYPE_IMAGE);
684 cpl_frame_set_group(product_frame,CPL_FRAME_GROUP_PRODUCT);
685 cpl_frame_set_level(product_frame,CPL_FRAME_LEVEL_FINAL);
686 ps.product_frame_stack = product_frame;
692 parlist,vircam_recipename,
697 if (cpl_image_save(NULL,fname,CPL_TYPE_UCHAR,plist,
698 CPL_IO_DEFAULT) != CPL_ERROR_NONE) {
699 cpl_msg_error(fctid,
"Cannot save product PHU");
700 cpl_frame_delete(product_frame);
705 cpl_frameset_insert(framelist,product_frame);
709 product_frame = cpl_frame_new();
710 cpl_frame_set_filename(product_frame,fnamec);
713 cpl_frame_set_tag(product_frame,VIRCAM_PRO_CONF_SCI);
716 cpl_frame_set_tag(product_frame,VIRCAM_PRO_CONF_STD);
719 cpl_frame_set_tag(product_frame,VIRCAM_PRO_CONF_SCI);
722 cpl_frame_set_type(product_frame,CPL_FRAME_TYPE_IMAGE);
723 cpl_frame_set_group(product_frame,CPL_FRAME_GROUP_PRODUCT);
724 cpl_frame_set_level(product_frame,CPL_FRAME_LEVEL_FINAL);
725 ps.product_frame_stackc = product_frame;
731 parlist,vircam_recipename,
736 if (cpl_image_save(NULL,fnamec,CPL_TYPE_UCHAR,plist,
737 CPL_IO_DEFAULT) != CPL_ERROR_NONE) {
738 cpl_msg_error(fctid,
"Cannot save product PHU");
739 cpl_frame_delete(product_frame);
744 cpl_frameset_insert(framelist,product_frame);
757 product_frame = ps.product_frame_stack;
759 vircam_recipename,
"PRO-1.15",NULL);
760 if (cpl_image_save(fim,fname,CPL_TYPE_FLOAT,p,CPL_IO_EXTEND) !=
762 cpl_msg_error(fctid,
"Cannot save product image extension");
770 pafprop = vircam_paf_req_items(p);
773 vircam_paf_append(pafprop,p,
"ESO DET NDIT");
774 vircam_paf_append(pafprop,p,
"ESO PRO CATG");
781 if (vircam_paf_print(fnamepaf,vircam_recipepaf,
"QC file",
783 cpl_msg_warning(fctid,
"Unable to save PAF for stack");
784 cpl_propertylist_delete(pafprop);
788 cpl_propertylist_delete(p);
797 cpl_propertylist_erase_regexp(p,
"ESO QC",0);
802 product_frame = ps.product_frame_stackc;
804 vircam_recipename,
"PRO-1.15",NULL);
805 if (cpl_image_save(fimc,fnamec,CPL_TYPE_SHORT,p,CPL_IO_EXTEND) !=
807 cpl_msg_error(fctid,
"Cannot save product image extension");
813 cpl_propertylist_delete(p);
848 cpl_parameterlist *parlist) {
849 cpl_frame *product_frame;
850 int isdummy,i,status;
852 cpl_propertylist *ehu,*phu,*ehu2,*pafprop;
853 char *fname,*fnamepaf;
854 const char *base[] = {
"",
"catalogue_jmp",
"catalogue_std",
"catalogue_mes"};
855 const char *fctid =
"vircam_jmp_save_catalogue";
861 if (ps.outcat != NULL) {
872 if (ps.outcat != NULL) {
874 }
else if (ps.stack_frame != NULL) {
877 for (i = 0; i < ps.nscience; i++) {
882 if (ps.outcat != NULL) {
884 }
else if (ps.stack_frame != NULL) {
887 for (i = 0; i < ps.nscience; i++) {
895 fname = vircam_jmp_outfile(base[recflag],0,1);
896 fnamepaf = vircam_jmp_outfile(base[recflag],0,0);
905 product_frame = cpl_frame_new();
906 cpl_frame_set_filename(product_frame,fname);
909 cpl_frame_set_tag(product_frame,VIRCAM_PRO_OBJCAT_SCI);
912 cpl_frame_set_tag(product_frame,VIRCAM_PRO_OBJCAT_STD);
915 cpl_frame_set_tag(product_frame,VIRCAM_PRO_OBJCAT_SCI);
918 cpl_frame_set_type(product_frame,CPL_FRAME_TYPE_TABLE);
919 cpl_frame_set_group(product_frame,CPL_FRAME_GROUP_PRODUCT);
920 cpl_frame_set_level(product_frame,CPL_FRAME_LEVEL_FINAL);
921 ps.product_frame_cat = product_frame;
926 parlist,vircam_recipename,
931 ehu2 = cpl_propertylist_duplicate(ehu);
942 parlist,vircam_recipename,
947 if (cpl_table_save(ftab,phu,ehu2,fname,CPL_IO_DEFAULT) !=
949 cpl_msg_error(fctid,
"Cannot save product PHU");
950 cpl_frame_delete(product_frame);
951 freepropertylist(ehu2);
958 pafprop = vircam_paf_req_items(ehu2);
960 vircam_paf_append(pafprop,ehu2,
"ESO PRO CATG");
961 vircam_paf_append(pafprop,phu,
"ESO INS FILT1 NAME");
962 vircam_paf_append(pafprop,ehu,
"ESO DET NDIT");
963 vircam_paf_append(pafprop,phu,
"RA");
964 vircam_paf_append(pafprop,phu,
"DEC");
965 vircam_paf_append(pafprop,phu,
"ESO TEL AIRM START");
966 vircam_paf_append(pafprop,phu,
"ESO TEL GUID FWHM");
967 vircam_paf_append(pafprop,phu,
"ESO TEL AMBI RHUM");
968 vircam_paf_append(pafprop,phu,
"ESO OBS TARG NAME");
969 if (vircam_paf_print(fnamepaf,vircam_recipepaf,
"QC file",
971 cpl_msg_warning(fctid,
"Unable to save PAF for catalogue");
972 cpl_propertylist_delete(pafprop);
976 freepropertylist(ehu2);
978 cpl_table_delete(ftab);
979 cpl_frameset_insert(framelist,product_frame);
985 product_frame = ps.product_frame_cat;
987 parlist,vircam_recipename,
992 ehu2 = cpl_propertylist_duplicate(ehu);
1002 if (cpl_table_save(ftab,NULL,ehu2,fname,CPL_IO_EXTEND) !=
1004 cpl_msg_error(fctid,
"Cannot save product table extension");
1005 freepropertylist(ehu2);
1012 pafprop = vircam_paf_req_items(ehu2);
1014 vircam_paf_append(pafprop,ehu2,
"ESO PRO CATG");
1015 vircam_paf_append(pafprop,phu,
"ESO INS FILT1 NAME");
1016 vircam_paf_append(pafprop,ehu,
"ESO DET NDIT");
1017 vircam_paf_append(pafprop,phu,
"RA");
1018 vircam_paf_append(pafprop,phu,
"DEC");
1019 vircam_paf_append(pafprop,phu,
"ESO TEL AIRM START");
1020 vircam_paf_append(pafprop,phu,
"ESO TEL GUID FWHM");
1021 vircam_paf_append(pafprop,phu,
"ESO TEL AMBI RHUM");
1022 vircam_paf_append(pafprop,phu,
"ESO OBS TARG NAME");
1023 if (vircam_paf_print(fnamepaf,vircam_recipepaf,
"QC file",
1025 cpl_msg_warning(fctid,
"Unable to save PAF for catalogue");
1026 cpl_propertylist_delete(pafprop);
1030 freepropertylist(ehu2);
1032 cpl_table_delete(ftab);
1035 freespace(fnamepaf);
1064 cpl_parameterlist *parlist) {
1065 cpl_frame *product_frame;
1066 int isdummy,i,status,isnew;
1068 cpl_propertylist *ehu,*phu,*ehu2,*pafprop;
1069 const char *fname =
"illum.fits";
1070 const char *fnamepaf =
"illum";
1071 const char *fctid =
"vircam_jmp_save_illum";
1077 if (ps.illcor != NULL) {
1088 if (ps.illcor != NULL) {
1091 for (i = 0; i < ps.nscience; i++) {
1096 if (ps.illcor != NULL) {
1099 for (i = 0; i < ps.nscience; i++) {
1112 product_frame = cpl_frame_new();
1113 cpl_frame_set_filename(product_frame,fname);
1116 cpl_frame_set_tag(product_frame,VIRCAM_PRO_ILLCOR_STD);
1119 cpl_frame_set_tag(product_frame,VIRCAM_PRO_ILLCOR_MES);
1122 cpl_frame_set_tag(product_frame,VIRCAM_PRO_ILLCOR_STD);
1125 cpl_frame_set_type(product_frame,CPL_FRAME_TYPE_TABLE);
1126 cpl_frame_set_group(product_frame,CPL_FRAME_GROUP_PRODUCT);
1127 cpl_frame_set_level(product_frame,CPL_FRAME_LEVEL_FINAL);
1128 ps.product_frame_illcor = product_frame;
1133 if (ps.phupaf == NULL) {
1134 ps.phupaf = vircam_paf_phu_items(phu);
1138 parlist,vircam_recipename,
1143 ehu2 = cpl_propertylist_duplicate(ehu);
1155 parlist,vircam_recipename,
1160 if (cpl_table_save(ftab,phu,ehu2,fname,CPL_IO_DEFAULT) !=
1162 cpl_msg_error(fctid,
"Cannot save product PHU");
1163 cpl_frame_delete(product_frame);
1164 freepropertylist(ehu2);
1170 pafprop = vircam_paf_req_items(ehu2);
1172 vircam_paf_append(pafprop,ehu2,
"ESO PRO CATG");
1173 vircam_paf_append(pafprop,phu,
"ESO INS FILT1 NAME");
1174 vircam_paf_append(pafprop,ehu,
"ESO DET NDIT");
1175 vircam_paf_append(pafprop,phu,
"RA");
1176 vircam_paf_append(pafprop,phu,
"DEC");
1177 vircam_paf_append(pafprop,phu,
"ESO TEL AIRM START");
1178 vircam_paf_append(pafprop,phu,
"ESO TEL GUID FWHM");
1179 vircam_paf_append(pafprop,phu,
"ESO TEL AMBI RHUM");
1180 vircam_paf_append(pafprop,phu,
"ESO OBS TARG NAME");
1181 if (vircam_paf_print((
char *)fnamepaf,vircam_recipepaf,
"QC file",
1183 cpl_msg_warning(fctid,
"Unable to save PAF for illcor table");
1184 cpl_propertylist_delete(pafprop);
1189 cpl_table_delete(ftab);
1190 freepropertylist(ehu2);
1191 cpl_frameset_insert(framelist,product_frame);
1198 product_frame = ps.product_frame_illcor;
1200 parlist,vircam_recipename,
1205 ehu2 = cpl_propertylist_duplicate(ehu);
1215 if (cpl_table_save(ftab,NULL,ehu2,fname,CPL_IO_EXTEND) !=
1217 cpl_msg_error(fctid,
"Cannot save product image extension");
1218 freepropertylist(ehu2);
1224 pafprop = vircam_paf_req_items(ehu2);
1226 vircam_paf_append(pafprop,ehu2,
"ESO PRO CATG");
1227 vircam_paf_append(pafprop,phu,
"ESO INS FILT1 NAME");
1228 vircam_paf_append(pafprop,ehu,
"ESO DET NDIT");
1229 vircam_paf_append(pafprop,phu,
"RA");
1230 vircam_paf_append(pafprop,phu,
"DEC");
1231 vircam_paf_append(pafprop,phu,
"ESO TEL AIRM START");
1232 vircam_paf_append(pafprop,phu,
"ESO TEL GUID FWHM");
1233 vircam_paf_append(pafprop,phu,
"ESO TEL AMBI RHUM");
1234 vircam_paf_append(pafprop,phu,
"ESO OBS TARG NAME");
1235 if (vircam_paf_print((
char *)fnamepaf,vircam_recipepaf,
"QC file",
1237 cpl_msg_warning(fctid,
"Unable to save PAF for illcor table");
1238 cpl_propertylist_delete(pafprop);
1242 freepropertylist(ehu2);
1244 cpl_table_delete(ftab);
1271 int nalloc,i,match,j,ustepnum,nustep;
1273 cpl_propertylist *plist;
1274 const char *fctid =
"vircam_jmp_ustep_seq";
1280 ps.ustep_sets = cpl_malloc(nalloc*
sizeof(ustep_set));
1286 for (i = 0; i < ps.nscience; i++) {
1287 ff = ps.sci_fits[i];
1290 cpl_msg_error(fctid,
"No microstep number in %s",
1301 for (j = 0; j < ps.nustep_sets; j++) {
1302 if (ustepnum == ps.ustep_sets[j].ustep_number) {
1304 ps.ustep_sets[j].f[ps.ustep_sets[j].nframes] = ff;
1305 ps.ustep_sets[j].nframes += 1;
1307 ps.ustep_sets[j].ngood += 1;
1316 if (ps.nustep_sets+1 == nalloc) {
1317 nalloc += INITALLOC;
1318 ps.ustep_sets = cpl_realloc(ps.ustep_sets,nalloc*
sizeof(ustep_set));
1324 ps.ustep_sets[ps.nustep_sets].f = cpl_malloc(nustep*
sizeof(vir_fits *));
1325 ps.ustep_sets[ps.nustep_sets].ustep_number = ustepnum;
1326 ps.ustep_sets[ps.nustep_sets].nustep = nustep;
1327 ps.ustep_sets[ps.nustep_sets].status = VIR_OK;
1328 ps.ustep_sets[ps.nustep_sets].super = NULL;
1329 ps.ustep_sets[ps.nustep_sets].superc = NULL;
1330 ps.ustep_sets[ps.nustep_sets].nframes = 0;
1331 ps.ustep_sets[ps.nustep_sets].f[0] = ff;
1332 ps.ustep_sets[ps.nustep_sets].nframes = 1;
1333 ps.ustep_sets[ps.nustep_sets].ngood = 0;
1335 ps.ustep_sets[ps.nustep_sets].ngood += 1;
1342 ps.ustep_sets = cpl_realloc(ps.ustep_sets,
1343 ps.nustep_sets*
sizeof(ustep_set));
1347 for (i = 0; i < ps.nustep_sets; i++) {
1348 if (ps.ustep_sets[i].ngood == 0) {
1349 cpl_msg_warning(fctid,
1350 "Microstep sequence %" CPL_SIZE_FORMAT
" has no input",
1351 (cpl_size)(ps.ustep_sets[i].ustep_number));
1352 ps.ustep_sets[i].status = VIR_FATAL;
1353 }
else if (ps.ustep_sets[i].ngood != ps.ustep_sets[i].nustep) {
1354 cpl_msg_warning(fctid,
"Microstep sequence %" CPL_SIZE_FORMAT
" incomplete",
1355 (cpl_size)(ps.ustep_sets[i].ustep_number));
1356 ps.ustep_sets[i].status = VIR_WARN;
1381 int i,refset,k,nstep,status,nk;
1385 double refx,refy,refra,refdec,x,y;
1386 cpl_image *fi,*outimage,*outconf;
1387 cpl_propertylist *plist;
1390 const char *fctid =
"vircam_jmp_interleave";
1398 ps.dith_input = cpl_malloc(ps.nustep_sets*
sizeof(vir_fits*));
1399 ps.dithc_input = cpl_malloc(ps.nustep_sets*
sizeof(vir_fits*));
1404 for (i = 0; i < ps.nustep_sets; i++) {
1405 if (ps.ustep_sets[i].status == VIR_FATAL) {
1409 ps.dith_input[ps.ndith] = ff;
1411 ps.ustep_sets[i].super = ff;
1413 ps.dithc_input[ps.ndith++] = ff;
1415 ps.ustep_sets[i].superc = ff;
1426 for (k = 0; k < ps.ustep_sets[i].nframes; k++) {
1427 ff = ps.ustep_sets[i].f[k];
1432 cpl_msg_error(fctid,
"Unable to open WCS structure %s",
1442 val =
vircam_med(cpl_image_get_data_float(fi),NULL,npts);
1444 "ESO DRS BACKMED",val);
1450 d = cpl_array_get_data_int_const(cpl_wcs_get_image_dims(wcs));
1451 refx = 0.5*(double)d[0];
1452 refy = 0.5*(double)d[1];
1455 "ESO DRS XOFFMICRO",0.0);
1457 "ESO DRS YOFFMICRO",0.0);
1458 cpl_wcs_delete(wcs);
1469 "ESO DRS XOFFMICRO",x);
1471 "ESO DRS YOFFMICRO",y);
1472 cpl_wcs_delete(wcs);
1477 tmp = cpl_malloc(ps.ustep_sets[i].nframes*
sizeof(vir_fits *));
1479 for (k = 0; k < ps.ustep_sets[i].nframes; k++) {
1480 ff = ps.ustep_sets[i].f[k];
1484 if (nk < ps.ustep_sets[i].nframes) {
1485 cpl_msg_error(fctid,
"A frame in this ustep sequence failed");
1486 ps.ustep_sets[i].status = VIR_WARN;
1492 nstep = (int)sqrt((
double)(ps.ustep_sets[i].nustep));
1496 if (status != VIR_OK) {
1497 cpl_msg_error(fctid,
1498 "Interleaving failed for ugroup %" CPL_SIZE_FORMAT
" extn %" CPL_SIZE_FORMAT,
1499 (cpl_size)(ps.ustep_sets[i].ustep_number),
1501 freepropertylist(plist);
1502 freeimage(outimage);
1508 ps.ustep_sets[i].super = ff;
1511 ps.ustep_sets[i].superc = ff;
1518 ps.ustep_sets[i].f[0],NULL,
1521 ps.ustep_sets[i].f[0],
1523 ps.ndithc = ps.ndith;
1524 ps.ustep_sets[i].super = ps.dith_input[ps.ndith - 1];
1525 ps.ustep_sets[i].superc = ps.dithc_input[ps.ndithc - 1];
1526 freepropertylist(plist);
1553 int status,i,ustepnum,nmatch,refset;
1554 float *xoffs,*yoffs,xoff,yoff,filtfwhm;
1555 cpl_wcs *wcsref,*wcs;
1557 vir_tfits *catref,*outcat;
1558 cpl_table *cr,*oc,*outxy;
1559 const char *fctid =
"vircam_jmp_dither_offsets";
1560 const double maxoffset = 2048;
1572 if (ps.ndith == 1) {
1574 "ESO DRS XOFFDITHER",(
double)0.0);
1576 "ESO DRS YOFFDITHER",(
double)0.0);
1583 xoffs = cpl_malloc(ps.ndith*
sizeof(
float));
1584 yoffs = cpl_malloc(ps.ndith*
sizeof(
float));
1590 for (i = 0; i < ps.ndith; i++) {
1591 ff = ps.dith_input[i];
1601 cpl_msg_warning(fctid,
1602 "Unable to get WCS for ustep %" CPL_SIZE_FORMAT,
1603 (cpl_size)ustepnum);
1628 if (status != VIR_OK) {
1631 cpl_msg_warning(fctid,
1632 "Unable to WCS difference for %" CPL_SIZE_FORMAT,
1633 (cpl_size)ustepnum);
1634 }
else if (fabs((
double)xoff) > maxoffset ||
1635 fabs((
double)yoff) > maxoffset) {
1636 cpl_msg_warning(fctid,
1637 "WCS offsets for %" CPL_SIZE_FORMAT
" are > %g: %g %g -- ignoring",
1638 (cpl_size)ustepnum,maxoffset,xoff,yoff);
1645 cpl_wcs_delete(wcs);
1648 cpl_wcs_delete(wcsref);
1654 filtfwhm = (interlv ? 3.5 : 2.0);
1655 for (i = 0; i < ps.ndith; i++) {
1656 ff = ps.dith_input[i];
1661 ffc = ps.dithc_input[i];
1663 ffc = ps.dithc_input[0];
1666 (void)
vircam_imcore(ff,ffc,25,5.0,0,3.5,64,1,filtfwhm,&outcat,
1676 cpl_msg_error(fctid,
1677 "Unable to get offsets for %" CPL_SIZE_FORMAT,
1678 (cpl_size)ustepnum);
1687 cpl_msg_error(fctid,
1688 "Unable to get object offset for %" CPL_SIZE_FORMAT
". Going with WCS offset",
1689 (cpl_size)ustepnum);
1698 cpl_table_add_scalar(oc,
"X_coordinate",(
double)xoffs[i]);
1699 cpl_table_add_scalar(oc,
"Y_coordinate",(
double)yoffs[i]);
1700 if (catref == NULL) {
1708 (void)
vircam_matchxy(oc,cr,100.0,&xoff,&yoff,&nmatch,&outxy,&status);
1716 if ((nmatch == 0 || status == VIR_FATAL) &&
1721 cpl_msg_error(fctid,
1722 "Unable to match stars for %" CPL_SIZE_FORMAT,
1723 (cpl_size)ustepnum);
1729 }
else if ((nmatch == 0 || status == VIR_FATAL) &&
1734 cpl_msg_warning(fctid,
1735 "Unable to match stars for %" CPL_SIZE_FORMAT
". Going with WCS offsets",
1736 (cpl_size)ustepnum);
1748 for (i = 0; i < ps.ndith; i++) {
1751 ff = ps.dith_input[i];
1753 "ESO DRS XOFFDITHER",(
double)xoffs[i]);
1755 "ESO DRS YOFFDITHER",(
double)yoffs[i]);
1784 int status,ngood,i,n;
1785 vir_fits **d,**dc,*ff;
1786 cpl_propertylist *dither_ehu,*dither_phu;
1787 cpl_image *outdither,*outditherc;
1788 const char *fctid =
"vircam_jmp_dither_images";
1793 for (i = 0; i < ps.ndith; i++)
1801 cpl_msg_error(fctid,
"No good input images for jittering");
1807 ps.stack_frame = ff;
1811 ps.stackc_frame = ff;
1817 d = cpl_malloc(ngood*
sizeof(vir_fits *));
1818 dc = cpl_malloc(ngood*
sizeof(vir_fits *));
1820 for (i = 0; i < ps.ndith; i++) {
1822 d[n] = ps.dith_input[i];
1824 dc[n++] = ps.dithc_input[i];
1826 dc[n++] = ps.dithc_input[0];
1833 (void)
vircam_imdither(d,dc,ngood,ngood,5.0,5.0,&dither_ehu,&outdither,
1834 &outditherc,&status);
1836 if (status != VIR_OK) {
1837 freeimage(outdither);
1838 freeimage(outditherc);
1839 cpl_msg_error(fctid,
"Error jittering to output");
1849 freepropertylist(dither_phu);
1850 freepropertylist(dither_ehu);
1873 const char *fctid =
"vircam_jmp_catalogue";
1881 cpl_msg_error(fctid,
"No stack image available. No catalogue generated");
1888 filtfwhm = (interlv ? 3.5 : 2);
1890 vircam_jmp_config.ipix,
1891 vircam_jmp_config.threshold,
1892 vircam_jmp_config.icrowd,
1893 vircam_jmp_config.rcore,
1894 vircam_jmp_config.nbsize,2,filtfwhm,
1901 if (status != VIR_OK) {
1902 cpl_msg_error(fctid,
"Error generating catalogue");
1931 int status,ncat,nstd,level;
1932 const char *fctid =
"vircam_jmp_matched_stds";
1933 cpl_table *stdscat,*newstds,*tmp,*tmp2,*cat;
1934 cpl_propertylist *p;
1942 if (ps.outcat == NULL) {
1943 cpl_msg_error(fctid,
"No input catalogue found");
1950 current_catpath,current_cat,&stdscat,&status);
1951 if (status != VIR_OK) {
1953 cpl_msg_error(fctid,
"Failed to find any standards");
1959 (void)cpl_table_or_selected_float(stdscat,
"j_msig",CPL_LESS_THAN,0.2);
1960 (void)cpl_table_and_selected_float(stdscat,
"k_msig",CPL_LESS_THAN,0.2);
1961 newstds = cpl_table_extract_selected(stdscat);
1962 nstd = (int)cpl_table_get_nrow(newstds);
1970 ncat = (int)cpl_table_get_nrow(cat);
1971 if (ncat > 500 && ncat > 2.0*nstd) {
1972 tmp = cpl_table_duplicate(cat);
1973 (void)cpl_table_or_selected_float(tmp,
"Ellipticity",CPL_LESS_THAN,0.5);
1974 tmp2 = cpl_table_extract_selected(tmp);
1975 ncat = (int)cpl_table_get_nrow(tmp2);
1977 p = cpl_propertylist_new();
1978 cpl_propertylist_append_bool(p,
"Aper_flux_3",TRUE);
1979 cpl_table_sort(tmp2,(
const cpl_propertylist *)p);
1980 cpl_propertylist_delete(p);
1981 level = min(ncat,max(1,min(5000,max(500,2*nstd))));
1982 tmp = cpl_table_extract(tmp2,1,(cpl_size)level);
1984 ncat = (int)cpl_table_get_nrow(tmp);
1996 if (status != VIR_OK) {
1997 freetable(ps.matchstds);
1998 cpl_msg_error(fctid,
"Failed to match standards to catalogue");
2024 const char *fctid =
"vircam_jmp_wcsfit";
2025 float *ra,*dec,*x,*y;
2036 if (ps.matchstds == NULL) {
2037 cpl_msg_error(fctid,
"No input matched standards catalogue found");
2046 if (status != VIR_OK) {
2047 cpl_msg_error(fctid,
"Failed to fit WCS");
2054 n = (int)cpl_table_get_nrow(cat);
2057 cpl_msg_error(fctid,
"Failed to fill RA and Dec in catalogue");
2060 x = cpl_table_get_data_float(cat,
"X_coordinate");
2061 y = cpl_table_get_data_float(cat,
"Y_coordinate");
2062 ra = cpl_table_get_data_float(cat,
"RA");
2063 dec = cpl_table_get_data_float(cat,
"DEC");
2064 for (i = 0; i < n; i++) {
2069 cpl_wcs_delete(wcs);
2092 const char *fctid =
"vircam_jmp_photcal";
2094 cpl_propertylist *pl;
2102 if (ps.matchstds == NULL || (
int)cpl_table_get_nrow(ps.matchstds) == 0) {
2103 cpl_msg_error(fctid,
"No input matched standards catalogue found");
2110 cpl_msg_error(fctid,
"No filter name in stack header");
2117 (void)
vircam_photcal(&(ps.stack_frame),&(ps.matchstds),&pl,1,filt,
2118 ps.tphottab,&status);
2119 if (status != VIR_OK) {
2120 cpl_msg_error(fctid,
"Failed to fit photometric zeropoint");
2151 n = (int)cpl_image_get_size_x(im)*(int)cpl_image_get_size_y(im);
2152 data = cpl_image_get_data_int(im);
2156 for (i = 0; i < n; i++)
2157 data[i] = (data[i] == 1 ? 0 : 100);
2181 int i,ngood,status,nfrms;
2183 vir_fits **ftmp,*ff,**frms;
2184 const char *fctid =
"vircam_jmp_skycor";
2185 unsigned char *rejmask,*rejplus;
2186 cpl_propertylist *drs;
2187 cpl_image *skyimg,*fim;
2188 float *data,med,sig;
2192 if (offsky == 0 || offsky == 1) {
2197 nfrms = ps.noffsets;
2198 frms = ps.offsky_fits;
2200 nfrms = ps.nscience;
2206 ftmp = cpl_malloc(nfrms*
sizeof(vir_fits *));
2208 for (i = 0; i < nfrms; i++) {
2218 cpl_msg_error(fctid,
"Sky correction impossible. No good sky frames available");
2230 freepropertylist(drs);
2238 data = cpl_image_get_data_float(skyimg);
2239 npts = (long)cpl_image_get_size_x(skyimg)*(long)cpl_image_get_size_y(skyimg);
2241 for (i = 0; i < npts; i++)
2249 freepropertylist(drs);
2251 cpl_propertylist_update_float(drs,
"ESO QC SKYMED",med);
2252 cpl_propertylist_set_comment(drs,
"ESO QC SKYMED",
2253 "Median sky background");
2254 cpl_propertylist_update_float(drs,
"ESO QC SKYSIG",sig);
2255 cpl_propertylist_set_comment(drs,
"ESO QC SKYSIG",
2256 "Sky background sigma");
2262 for (i = 0; i < ps.nscience; i++) {
2263 ff = ps.sci_fits[i];
2267 cpl_image_subtract(fim,skyimg);
2269 cpl_propertylist_update_string(drs,
"ESO DRS SKYCOR",
"internal");
2270 }
else if (offsky == 1) {
2271 cpl_propertylist_update_string(drs,
"ESO DRS SKYCOR",
"offsky");
2272 }
else if (offsky == -1) {
2273 cpl_propertylist_update_string(drs,
"ESO DRS SKYCOR",
2276 cpl_propertylist_set_comment(drs,
"ESO DRS SKYCOR",
2277 "Sky correction method");
2280 for (i = 0; i < ps.noffsets; i++) {
2281 ff = ps.offsky_fits[i];
2285 cpl_image_subtract(fim,skyimg);
2287 cpl_propertylist_update_string(drs,
"ESO DRS SKYCOR",
"internal");
2288 }
else if (offsky == 1) {
2289 cpl_propertylist_update_string(drs,
"ESO DRS SKYCOR",
"offsky");
2290 }
else if (offsky == -1) {
2291 cpl_propertylist_update_string(drs,
"ESO DRS SKYCOR",
2294 cpl_propertylist_set_comment(drs,
"ESO DRS SKYCOR",
2295 "Image has been sky corrected");
2302 cpl_image_delete(skyimg);
2303 else if (offsky == 1)
2304 cpl_image_add_scalar(skyimg,med);
2328 cpl_propertylist *p_rg;
2329 const char *fctid =
"vircam_jmp_get_readnoise_gain";
2333 p_rg = cpl_propertylist_load(cpl_frame_get_filename(ps.readgain_file),
2338 switch (cpl_propertylist_get_type(p_rg,
"ESO QC READNOISE")) {
2339 case CPL_TYPE_FLOAT:
2340 *readnoise = cpl_propertylist_get_float(p_rg,
"ESO QC READNOISE");
2342 case CPL_TYPE_DOUBLE:
2343 *readnoise = (float)cpl_propertylist_get_double(p_rg,
2344 "ESO QC READNOISE");
2349 cpl_msg_error(fctid,
"Unable to get READNOISE estimate, guessing %g\n",
2355 switch (cpl_propertylist_get_type(p_rg,
"ESO QC CONAD")) {
2356 case CPL_TYPE_FLOAT:
2357 *gain = cpl_propertylist_get_float(p_rg,
"ESO QC CONAD");
2359 case CPL_TYPE_DOUBLE:
2360 *gain = (float)cpl_propertylist_get_double(p_rg,
"ESO QC CONAD");
2365 cpl_msg_error(fctid,
"Unable to get GAIN estimate, guessing %g\n",
2368 cpl_propertylist_delete(p_rg);
2389 int ngood,i,status,ii;
2391 vir_fits **ftmp,*ff,*ffc;
2393 cpl_table **mstds,*stdscat,*ms,*illcor,*ot;
2395 cpl_propertylist **pl,*phu,*ehu;
2396 const char *fctid =
"vircam_jmp_illum";
2404 ftmp = cpl_malloc(ps.nscience*
sizeof(vir_fits *));
2406 for (i = 0; i < ps.nscience; i++) {
2407 ff = ps.sci_fits[i];
2416 cpl_msg_error(fctid,
"Illumination correction impossible. No good science frames available");
2423 cpl_msg_error(fctid,
"No filter name in stack header");
2431 mstds = cpl_malloc(ngood*
sizeof(cpl_table *));
2432 for (i = 0; i < ngood; i++)
2434 pl = cpl_malloc(ngood*
sizeof(cpl_propertylist *));
2435 for (i = 0; i < ngood; i++)
2442 for (i = 0; i < ngood; i++) {
2446 1.5*vircam_jmp_config.threshold,0,
2447 vircam_jmp_config.rcore,vircam_jmp_config.nbsize,2,
2448 3.5,&outtab,&status);
2449 if (status != VIR_OK)
2456 current_cat,&stdscat,&status);
2457 if (status == VIR_FATAL) {
2460 for (ii = 0; ii < ngood; ii++) {
2461 freetable(mstds[ii]);
2462 freepropertylist(pl[ii]);
2466 cpl_msg_error(fctid,
"Illumination correction fails");
2468 }
else if (status == VIR_WARN) {
2478 if (status == VIR_FATAL) {
2482 for (ii = 0; ii < ngood; ii++) {
2483 freetable(mstds[ii]);
2484 freepropertylist(pl[ii]);
2488 cpl_msg_error(fctid,
"%s",cpl_error_get_message());
2499 (void)
vircam_illum(ftmp,mstds,pl,ngood,filt,ps.tphottab,128,&illcor,
2500 &illcor_rms,&status);
2507 cpl_propertylist_update_float(ehu,
"ESO QC ILLUMCOR_RMS",illcor_rms);
2508 cpl_propertylist_set_comment(ehu,
"ESO QC ILLUMCOR_RMS",
2509 "RMS of illumination correction map");
2513 for (i = 0; i < ngood; i++) {
2514 freetable(mstds[i]);
2515 freepropertylist(pl[i]);
2546 static char *vircam_jmp_outfile(
const char *bname,
int ind,
int isfits) {
2557 nf += ((int)log10((
double)ind)+1);
2565 fname = cpl_malloc(nf);
2570 (void)snprintf(fname,nf,
"%s_%d.fits",bname,ind);
2572 (
void)snprintf(fname,nf,
"%s_%d",bname,ind);
2598 ps.master_dark = NULL;
2599 ps.master_twilight_flat = NULL;
2600 ps.master_conf = NULL;
2601 ps.master_sky = NULL;
2606 ps.readgain_file = NULL;
2607 ps.science_frames = NULL;
2608 ps.offset_skies = NULL;
2609 ps.product_frames_simple = NULL;
2610 ps.product_frames_simple_off = NULL;
2611 ps.product_frames_super = NULL;
2612 ps.product_frames_superc = NULL;
2613 ps.product_frame_stack = NULL;
2614 ps.product_frame_stackc = NULL;
2615 ps.product_frame_cat = NULL;
2616 ps.product_frame_illcor = NULL;
2617 ps.product_frame_sky = NULL;
2635 ps.offsky_fits = NULL;
2637 ps.ustep_sets = NULL;
2640 ps.dith_input = NULL;
2641 ps.dithc_input = NULL;
2642 ps.stack_frame = NULL;
2643 ps.stackc_frame = NULL;
2681 freetfits(ps.fchantab);
2682 freefitslist(ps.sci_fits,ps.nscience);
2683 freefitslist(ps.offsky_fits,ps.noffsets);
2685 for (i = 0; i < ps.nustep_sets; i++) {
2686 freespace(ps.ustep_sets[i].f);
2687 freefits(ps.ustep_sets[i].super);
2688 freefits(ps.ustep_sets[i].superc);
2690 freespace(ps.ustep_sets);
2692 freespace(ps.dith_input);
2694 freespace(ps.dithc_input);
2697 freefits(ps.stack_frame);
2698 freefits(ps.stackc_frame);
2699 freetfits(ps.outcat);
2700 freetable(ps.matchstds);
2701 freetfits(ps.illcor);
2702 freefits(ps.outsky);
2709 freespace(ps.labels);
2710 freeframe(ps.master_dark);
2711 freeframe(ps.master_twilight_flat);
2712 freeframe(ps.master_conf);
2713 freeframe(ps.master_sky);
2715 freeframe(ps.chantab);
2716 freeframe(ps.phottab);
2717 freeframe(ps.readgain_file);
2718 freetable(ps.tphottab);
2719 freeframeset(ps.science_frames);
2720 freeframeset(ps.offset_skies);
2721 freepropertylist(ps.phupaf);
2722 freespace(ps.product_frames_simple);
2723 freespace(ps.product_frames_super);
2724 freespace(ps.product_frames_superc);
2725 freespace(ps.product_frames_simple_off);
2726 freespace(ps.gaincors);
2727 freespace(ps.catpath);
2728 freespace(ps.catname);
2729 freespace(ps.catpath2);
2730 freespace(ps.catname2);