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