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_TYPE_UCHAR,plist,
00143 CPL_IO_DEFAULT) != CPL_ERROR_NONE) {
00144 cpl_msg_error(fctid,"Cannot save product PHU");
00145 cpl_frame_delete(product_frame);
00146 freespace(fname);
00147 return(-1);
00148 }
00149 cpl_frameset_insert(framelist,product_frame);
00150 ps.product_frames_simple[i] = product_frame;
00151 }
00152
00153
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_TYPE_FLOAT,plist,
00167 CPL_IO_EXTEND) != CPL_ERROR_NONE) {
00168 cpl_msg_error(fctid,"Cannot save product image extension");
00169 freespace(fname);
00170 return(-1);
00171 }
00172
00173
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_TYPE_UCHAR,plist,
00251 CPL_IO_DEFAULT) != CPL_ERROR_NONE) {
00252 cpl_msg_error(fctid,"Cannot save product PHU");
00253 cpl_frame_delete(product_frame);
00254 freespace(fname);
00255 return(-1);
00256 }
00257 cpl_frameset_insert(framelist,product_frame);
00258 ps.product_frames_simple_off[i] = product_frame;
00259 }
00260
00261
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_TYPE_FLOAT,plist,
00275 CPL_IO_EXTEND) != CPL_ERROR_NONE) {
00276 cpl_msg_error(fctid,"Cannot save product image extension");
00277 freespace(fname);
00278 return(-1);
00279 }
00280
00281
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_TYPE_UCHAR,p,
00360 CPL_IO_DEFAULT) != CPL_ERROR_NONE) {
00361 cpl_msg_error(fctid,"Cannot save product PHU");
00362 cpl_frame_delete(product_frame);
00363 freespace(fname);
00364 return(-1);
00365 }
00366 cpl_frameset_insert(framelist,product_frame);
00367 }
00368
00369
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_TYPE_FLOAT,p,CPL_IO_EXTEND) !=
00381 CPL_ERROR_NONE) {
00382 cpl_msg_error(fctid,"Cannot save product image extension");
00383 freespace(fname);
00384 return(-1);
00385 }
00386
00387
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_TYPE_UCHAR,plist,
00508 CPL_IO_DEFAULT) != CPL_ERROR_NONE) {
00509 cpl_msg_error(fctid,"Cannot save product PHU");
00510 cpl_frame_delete(product_frame);
00511 freespace(fname);
00512 freespace(fnamec);
00513 return(-1);
00514 }
00515 cpl_frameset_insert(framelist,product_frame);
00516 ps.product_frames_super[i] = product_frame;
00517
00518
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_TYPE_UCHAR,plist,
00548 CPL_IO_DEFAULT) != CPL_ERROR_NONE) {
00549 cpl_msg_error(fctid,"Cannot save product PHU");
00550 cpl_frame_delete(product_frame);
00551 freespace(fname);
00552 freespace(fnamec);
00553 return(-1);
00554 }
00555 cpl_frameset_insert(framelist,product_frame);
00556 ps.product_frames_superc[i] = product_frame;
00557 }
00558
00559
00560
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_TYPE_FLOAT,p,CPL_IO_EXTEND) !=
00573 CPL_ERROR_NONE) {
00574 cpl_msg_error(fctid,"Cannot save product image extension");
00575 freespace(fname);
00576 freespace(fnamec);
00577 return(-1);
00578 }
00579 cpl_propertylist_delete(p);
00580
00581
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_TYPE_SHORT,p,CPL_IO_EXTEND) !=
00594 CPL_ERROR_NONE) {
00595 cpl_msg_error(fctid,"Cannot save confidence map image extension");
00596 freespace(fname);
00597 freespace(fnamec);
00598 return(-1);
00599 }
00600 cpl_propertylist_delete(p);
00601
00602
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_TYPE_UCHAR,plist,
00698 CPL_IO_DEFAULT) != CPL_ERROR_NONE) {
00699 cpl_msg_error(fctid,"Cannot save product PHU");
00700 cpl_frame_delete(product_frame);
00701 freespace(fname);
00702 freespace(fnamec);
00703 return(-1);
00704 }
00705 cpl_frameset_insert(framelist,product_frame);
00706
00707
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_TYPE_UCHAR,plist,
00737 CPL_IO_DEFAULT) != CPL_ERROR_NONE) {
00738 cpl_msg_error(fctid,"Cannot save product PHU");
00739 cpl_frame_delete(product_frame);
00740 freespace(fname);
00741 freespace(fnamec);
00742 return(-1);
00743 }
00744 cpl_frameset_insert(framelist,product_frame);
00745 }
00746
00747
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_TYPE_FLOAT,p,CPL_IO_EXTEND) !=
00761 CPL_ERROR_NONE) {
00762 cpl_msg_error(fctid,"Cannot save product image extension");
00763 freespace(fname);
00764 freespace(fnamec);
00765 return(-1);
00766 }
00767
00768
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_TYPE_SHORT,p,CPL_IO_EXTEND) !=
00806 CPL_ERROR_NONE) {
00807 cpl_msg_error(fctid,"Cannot save product image extension");
00808 freespace(fname);
00809 freespace(fnamec);
00810 freepropertylist(p);
00811 return(-1);
00812 }
00813 cpl_propertylist_delete(p);
00814
00815
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,
01350 "Microstep sequence %" CPL_SIZE_FORMAT " has no input",
01351 (cpl_size)(ps.ustep_sets[i].ustep_number));
01352 ps.ustep_sets[i].status = VIR_FATAL;
01353 } else if (ps.ustep_sets[i].ngood != ps.ustep_sets[i].nustep) {
01354 cpl_msg_warning(fctid,"Microstep sequence %" CPL_SIZE_FORMAT " incomplete",
01355 (cpl_size)(ps.ustep_sets[i].ustep_number));
01356 ps.ustep_sets[i].status = VIR_WARN;
01357 }
01358 }
01359 }
01360
01361
01362
01378
01379
01380 extern void vircam_jmp_interleave(void) {
01381 int i,refset,k,nstep,status,nk;
01382 const int *d;
01383 long npts;
01384 float val;
01385 double refx,refy,refra,refdec,x,y;
01386 cpl_image *fi,*outimage,*outconf;
01387 cpl_propertylist *plist;
01388 vir_fits *ff,**tmp;
01389 cpl_wcs *wcs;
01390 const char *fctid = "vircam_jmp_interleave";
01391
01392
01393
01394 vircam_jmp_ustep_seq();
01395
01396
01397
01398 ps.dith_input = cpl_malloc(ps.nustep_sets*sizeof(vir_fits*));
01399 ps.dithc_input = cpl_malloc(ps.nustep_sets*sizeof(vir_fits*));
01400
01401
01402
01403 ps.ndith = 0;
01404 for (i = 0; i < ps.nustep_sets; i++) {
01405 if (ps.ustep_sets[i].status == VIR_FATAL) {
01406 outimage = vircam_dummy_image(ps.ustep_sets[i].f[0]);
01407 outconf = vircam_dummy_image(ps.fconf);
01408 ff = vircam_fits_wrap(outimage,ps.ustep_sets[i].f[0],NULL,NULL);
01409 ps.dith_input[ps.ndith] = ff;
01410 vircam_fits_set_error(ff,VIR_FATAL);
01411 ps.ustep_sets[i].super = ff;
01412 ff = vircam_fits_wrap(outconf,ps.ustep_sets[i].f[0],NULL,NULL);
01413 ps.dithc_input[ps.ndith++] = ff;
01414 vircam_fits_set_error(ff,VIR_FATAL);
01415 ps.ustep_sets[i].superc = ff;
01416 continue;
01417 }
01418
01419
01420
01421
01422
01423 refset = 0;
01424 refx = 0.0;
01425 refy = 0.0;
01426 for (k = 0; k < ps.ustep_sets[i].nframes; k++) {
01427 ff = ps.ustep_sets[i].f[k];
01428 if (vircam_fits_get_status(ff) == VIR_FATAL)
01429 continue;
01430 wcs = cpl_wcs_new_from_propertylist(vircam_fits_get_ehu(ff));
01431 if (wcs == NULL) {
01432 cpl_msg_error(fctid,"Unable to open WCS structure %s",
01433 vircam_fits_get_fullname(ff));
01434 vircam_fits_set_error(ff,VIR_FATAL);
01435 continue;
01436 }
01437
01438
01439
01440 fi = vircam_fits_get_image(ff);
01441 npts = vircam_getnpts(fi);
01442 val = vircam_med(cpl_image_get_data_float(fi),NULL,npts);
01443 cpl_propertylist_update_float(vircam_fits_get_ehu(ff),
01444 "ESO DRS BACKMED",val);
01445
01446
01447
01448 if (refset == 0) {
01449 refset = 1;
01450 d = cpl_array_get_data_int_const(cpl_wcs_get_image_dims(wcs));
01451 refx = 0.5*(double)d[0];
01452 refy = 0.5*(double)d[1];
01453 vircam_xytoradec(wcs,refx,refy,&refra,&refdec);
01454 cpl_propertylist_update_double(vircam_fits_get_ehu(ff),
01455 "ESO DRS XOFFMICRO",0.0);
01456 cpl_propertylist_update_double(vircam_fits_get_ehu(ff),
01457 "ESO DRS YOFFMICRO",0.0);
01458 cpl_wcs_delete(wcs);
01459 continue;
01460 }
01461
01462
01463
01464
01465 vircam_radectoxy(wcs,refra,refdec,&x,&y);
01466 x = refx - x;
01467 y = refy - y;
01468 cpl_propertylist_update_double(vircam_fits_get_ehu(ff),
01469 "ESO DRS XOFFMICRO",x);
01470 cpl_propertylist_update_double(vircam_fits_get_ehu(ff),
01471 "ESO DRS YOFFMICRO",y);
01472 cpl_wcs_delete(wcs);
01473 }
01474
01475
01476
01477 tmp = cpl_malloc(ps.ustep_sets[i].nframes*sizeof(vir_fits *));
01478 nk = 0;
01479 for (k = 0; k < ps.ustep_sets[i].nframes; k++) {
01480 ff = ps.ustep_sets[i].f[k];
01481 if (vircam_fits_get_status(ff) != VIR_FATAL)
01482 tmp[nk++] = ff;
01483 }
01484 if (nk < ps.ustep_sets[i].nframes) {
01485 cpl_msg_error(fctid,"A frame in this ustep sequence failed");
01486 ps.ustep_sets[i].status = VIR_WARN;
01487 }
01488
01489
01490
01491 status = VIR_OK;
01492 nstep = (int)sqrt((double)(ps.ustep_sets[i].nustep));
01493 (void)vircam_interleave(tmp,nk,&(ps.fconf),1,nstep,&plist,&outimage,
01494 &outconf,&status);
01495 freespace(tmp);
01496 if (status != VIR_OK) {
01497 cpl_msg_error(fctid,
01498 "Interleaving failed for ugroup %" CPL_SIZE_FORMAT " extn %" CPL_SIZE_FORMAT,
01499 (cpl_size)(ps.ustep_sets[i].ustep_number),
01500 (cpl_size)(vircam_fits_get_nexten(ps.ustep_sets[i].f[0])));
01501 freepropertylist(plist);
01502 freeimage(outimage);
01503 freeimage(outconf);
01504 outimage = vircam_dummy_image(ps.ustep_sets[i].f[0]);
01505 outconf = vircam_dummy_image(ps.fconf);
01506 ff = vircam_fits_wrap(outimage,ps.ustep_sets[i].f[0],NULL,NULL);
01507 vircam_fits_set_error(ff,VIR_FATAL);
01508 ps.ustep_sets[i].super = ff;
01509 ff = vircam_fits_wrap(outconf,ps.ustep_sets[i].f[0],NULL,NULL);
01510 vircam_fits_set_error(ff,VIR_FATAL);
01511 ps.ustep_sets[i].superc = ff;
01512 }
01513
01514
01515
01516
01517 ps.dith_input[ps.ndith] = vircam_fits_wrap(outimage,
01518 ps.ustep_sets[i].f[0],NULL,
01519 plist);
01520 ps.dithc_input[ps.ndith++] = vircam_fits_wrap(outconf,
01521 ps.ustep_sets[i].f[0],
01522 NULL,plist);
01523 ps.ndithc = ps.ndith;
01524 ps.ustep_sets[i].super = ps.dith_input[ps.ndith - 1];
01525 ps.ustep_sets[i].superc = ps.dithc_input[ps.ndithc - 1];
01526 freepropertylist(plist);
01527 }
01528 }
01529
01530
01550
01551
01552 extern void vircam_jmp_dither_offsets(void) {
01553 int status,i,ustepnum,nmatch,refset;
01554 float *xoffs,*yoffs,xoff,yoff,filtfwhm;
01555 cpl_wcs *wcsref,*wcs;
01556 vir_fits *ff,*ffc;
01557 vir_tfits *catref,*outcat;
01558 cpl_table *cr,*oc,*outxy;
01559 const char *fctid = "vircam_jmp_dither_offsets";
01560 const double maxoffset = 2048;
01561
01562
01563
01564
01565
01566
01567 if (ps.ndith == 0)
01568 return;
01569
01570
01571
01572 if (ps.ndith == 1) {
01573 cpl_propertylist_update_double(vircam_fits_get_ehu(ps.dith_input[0]),
01574 "ESO DRS XOFFDITHER",(double)0.0);
01575 cpl_propertylist_update_double(vircam_fits_get_ehu(ps.dith_input[0]),
01576 "ESO DRS YOFFDITHER",(double)0.0);
01577 return;
01578 }
01579
01580
01581
01582 status = VIR_OK;
01583 xoffs = cpl_malloc(ps.ndith*sizeof(float));
01584 yoffs = cpl_malloc(ps.ndith*sizeof(float));
01585
01586
01587
01588 refset = 0;
01589 wcsref = NULL;
01590 for (i = 0; i < ps.ndith; i++) {
01591 ff = ps.dith_input[i];
01592 if (vircam_fits_get_status(ff) == VIR_FATAL)
01593 continue;
01594 wcs = cpl_wcs_new_from_propertylist(vircam_fits_get_ehu(ff));
01595 (void)vircam_pfits_get_ustepnum(vircam_fits_get_phu(ff),&ustepnum);
01596
01597
01598
01599
01600 if (wcs == NULL) {
01601 cpl_msg_warning(fctid,
01602 "Unable to get WCS for ustep %" CPL_SIZE_FORMAT,
01603 (cpl_size)ustepnum);
01604 xoffs[i] = 0.0;
01605 yoffs[i] = 0.0;
01606 vircam_fits_set_error(ff,VIR_WARN);
01607 continue;
01608 }
01609
01610
01611
01612
01613 if (! refset) {
01614 xoffs[i] = 0.0;
01615 yoffs[i] = 0.0;
01616 refset = 1;
01617 wcsref = wcs;
01618 continue;
01619 }
01620
01621
01622
01623
01624 (void)vircam_diffxywcs(wcs,wcsref,&xoff,&yoff,&status);
01625
01626
01627
01628 if (status != VIR_OK) {
01629 xoffs[i] = 0.0;
01630 yoffs[i] = 0.0;
01631 cpl_msg_warning(fctid,
01632 "Unable to WCS difference for %" CPL_SIZE_FORMAT,
01633 (cpl_size)ustepnum);
01634 } else if (fabs((double)xoff) > maxoffset ||
01635 fabs((double)yoff) > maxoffset) {
01636 cpl_msg_warning(fctid,
01637 "WCS offsets for %" CPL_SIZE_FORMAT " are > %g: %g %g -- ignoring",
01638 (cpl_size)ustepnum,maxoffset,xoff,yoff);
01639 xoffs[i] = 0.0;
01640 yoffs[i] = 0.0;
01641 } else {
01642 xoffs[i] = xoff;
01643 yoffs[i] = yoff;
01644 }
01645 cpl_wcs_delete(wcs);
01646 }
01647 if (wcsref != NULL)
01648 cpl_wcs_delete(wcsref);
01649
01650
01651
01652 catref = NULL;
01653 cr = NULL;
01654 filtfwhm = (interlv ? 3.5 : 2.0);
01655 for (i = 0; i < ps.ndith; i++) {
01656 ff = ps.dith_input[i];
01657 if (vircam_fits_get_status(ff) == VIR_FATAL)
01658 continue;
01659 status = VIR_OK;
01660 if (ps.ndithc != 1)
01661 ffc = ps.dithc_input[i];
01662 else
01663 ffc = ps.dithc_input[0];
01664 (void)vircam_pfits_get_ustepnum(vircam_fits_get_phu(ff),&ustepnum);
01665 outcat = NULL;
01666 (void)vircam_imcore(ff,ffc,25,5.0,0,3.5,64,1,filtfwhm,&outcat,
01667 &status);
01668
01669
01670
01671
01672
01673 if (status != VIR_OK && vircam_fits_get_status(ff) != VIR_OK) {
01674 vircam_fits_set_error(ff,VIR_FATAL);
01675 freetfits(outcat);
01676 cpl_msg_error(fctid,
01677 "Unable to get offsets for %" CPL_SIZE_FORMAT,
01678 (cpl_size)ustepnum);
01679 continue;
01680
01681
01682
01683
01684 } else if (status != VIR_OK && vircam_fits_get_status(ff) == VIR_OK) {
01685 vircam_fits_set_error(ff,VIR_WARN);
01686 freetfits(outcat);
01687 cpl_msg_error(fctid,
01688 "Unable to get object offset for %" CPL_SIZE_FORMAT ". Going with WCS offset",
01689 (cpl_size)ustepnum);
01690 continue;
01691
01692
01693
01694
01695
01696 } else {
01697 oc = vircam_tfits_get_table(outcat);
01698 cpl_table_add_scalar(oc,"X_coordinate",(double)xoffs[i]);
01699 cpl_table_add_scalar(oc,"Y_coordinate",(double)yoffs[i]);
01700 if (catref == NULL) {
01701 catref = outcat;
01702 cr = oc;
01703 continue;
01704 }
01705
01706
01707
01708 (void)vircam_matchxy(oc,cr,100.0,&xoff,&yoff,&nmatch,&outxy,&status);
01709 freetable(outxy);
01710 freetfits(outcat);
01711
01712
01713
01714
01715
01716 if ((nmatch == 0 || status == VIR_FATAL) &&
01717 vircam_fits_get_status(ff) != VIR_OK) {
01718 xoff = 0.0;
01719 yoff = 0.0;
01720 vircam_fits_set_error(ff,VIR_FATAL);
01721 cpl_msg_error(fctid,
01722 "Unable to match stars for %" CPL_SIZE_FORMAT,
01723 (cpl_size)ustepnum);
01724
01725
01726
01727
01728
01729 } else if ((nmatch == 0 || status == VIR_FATAL) &&
01730 vircam_fits_get_status(ff) == VIR_OK) {
01731 vircam_fits_set_error(ff,VIR_WARN);
01732 xoff = 0.0;
01733 yoff = 0.0;
01734 cpl_msg_warning(fctid,
01735 "Unable to match stars for %" CPL_SIZE_FORMAT ". Going with WCS offsets",
01736 (cpl_size)ustepnum);
01737 }
01738
01739
01740
01741 xoffs[i] += xoff;
01742 yoffs[i] += yoff;
01743 }
01744 }
01745
01746
01747
01748 for (i = 0; i < ps.ndith; i++) {
01749 if (vircam_fits_get_status(ps.dith_input[i]) == VIR_FATAL)
01750 continue;
01751 ff = ps.dith_input[i];
01752 cpl_propertylist_update_double(vircam_fits_get_ehu(ff),
01753 "ESO DRS XOFFDITHER",(double)xoffs[i]);
01754 cpl_propertylist_update_double(vircam_fits_get_ehu(ff),
01755 "ESO DRS YOFFDITHER",(double)yoffs[i]);
01756 }
01757
01758
01759
01760 freespace(xoffs);
01761 freespace(yoffs);
01762 freetfits(catref);
01763 }
01764
01765
01781
01782
01783 extern void vircam_jmp_dither_images(void) {
01784 int status,ngood,i,n;
01785 vir_fits **d,**dc,*ff;
01786 cpl_propertylist *dither_ehu,*dither_phu;
01787 cpl_image *outdither,*outditherc;
01788 const char *fctid = "vircam_jmp_dither_images";
01789
01790
01791
01792 ngood = 0;
01793 for (i = 0; i < ps.ndith; i++)
01794 if (vircam_fits_get_status(ps.dith_input[i]) != VIR_FATAL)
01795 ngood++;
01796
01797
01798
01799
01800 if (ngood == 0) {
01801 cpl_msg_error(fctid,"No good input images for jittering");
01802
01803
01804 outdither = vircam_dummy_image(ps.sci_fits[0]);
01805 outditherc = vircam_dummy_image(ps.dithc_input[0]);
01806 ff = vircam_fits_wrap(outdither,ps.sci_fits[0],NULL,NULL);
01807 ps.stack_frame = ff;
01808 vircam_fits_set_error(ff,VIR_FATAL);
01809 ff = vircam_fits_wrap(outditherc,ps.dithc_input[0],NULL,NULL);
01810 vircam_fits_set_error(ff,VIR_FATAL);
01811 ps.stackc_frame = ff;
01812 return;
01813 }
01814
01815
01816
01817 d = cpl_malloc(ngood*sizeof(vir_fits *));
01818 dc = cpl_malloc(ngood*sizeof(vir_fits *));
01819 n = 0;
01820 for (i = 0; i < ps.ndith; i++) {
01821 if (vircam_fits_get_status(ps.dith_input[i]) != VIR_FATAL) {
01822 d[n] = ps.dith_input[i];
01823 if (ps.ndithc != 1)
01824 dc[n++] = ps.dithc_input[i];
01825 else
01826 dc[n++] = ps.dithc_input[0];
01827 }
01828 }
01829
01830
01831
01832 status = VIR_OK;
01833 (void)vircam_imdither(d,dc,ngood,ngood,5.0,5.0,&dither_ehu,&outdither,
01834 &outditherc,&status);
01835 dither_phu = cpl_propertylist_duplicate(vircam_fits_get_phu(d[0]));
01836 if (status != VIR_OK) {
01837 freeimage(outdither);
01838 freeimage(outditherc);
01839 cpl_msg_error(fctid,"Error jittering to output");
01840 } else {
01841 ps.stack_frame = vircam_fits_wrap(outdither,d[0],dither_phu,
01842 dither_ehu);
01843 ps.stackc_frame = vircam_fits_wrap(outditherc,dc[0],dither_phu,
01844 dither_ehu);
01845 }
01846
01847
01848
01849 freepropertylist(dither_phu);
01850 freepropertylist(dither_ehu);
01851 freespace(d);
01852 freespace(dc);
01853 }
01854
01855
01870
01871
01872 extern void vircam_jmp_catalogue(void) {
01873 const char *fctid = "vircam_jmp_catalogue";
01874 int status;
01875 vir_tfits *outtab;
01876 float filtfwhm;
01877
01878
01879
01880 if (ps.stack_frame == NULL || vircam_fits_get_status(ps.stack_frame) == VIR_FATAL) {
01881 cpl_msg_error(fctid,"No stack image available. No catalogue generated");
01882 return;
01883 }
01884
01885
01886
01887 status = VIR_OK;
01888 filtfwhm = (interlv ? 3.5 : 2);
01889 (void)vircam_imcore(ps.stack_frame,ps.stackc_frame,
01890 vircam_jmp_config.ipix,
01891 vircam_jmp_config.threshold,
01892 vircam_jmp_config.icrowd,
01893 vircam_jmp_config.rcore,
01894 vircam_jmp_config.nbsize,2,filtfwhm,
01895 &outtab,&status);
01896
01897
01898
01899
01900
01901 if (status != VIR_OK) {
01902 cpl_msg_error(fctid,"Error generating catalogue");
01903 freetfits(outtab);
01904 vircam_fits_set_error(ps.stack_frame,VIR_FATAL);
01905 } else {
01906 ps.outcat = outtab;
01907 }
01908
01909 return;
01910 }
01911
01912
01928
01929
01930 extern void vircam_jmp_matched_stds(void) {
01931 int status,ncat,nstd,level;
01932 const char *fctid = "vircam_jmp_matched_stds";
01933 cpl_table *stdscat,*newstds,*tmp,*tmp2,*cat;
01934 cpl_propertylist *p;
01935
01936
01937
01938 status = VIR_OK;
01939
01940
01941
01942 if (ps.outcat == NULL) {
01943 cpl_msg_error(fctid,"No input catalogue found");
01944 return;
01945 }
01946
01947
01948
01949 (void)vircam_getstds(vircam_fits_get_ehu(ps.stack_frame),1,
01950 current_catpath,current_cat,&stdscat,&status);
01951 if (status != VIR_OK) {
01952 freetable(stdscat);
01953 cpl_msg_error(fctid,"Failed to find any standards");
01954 return;
01955 }
01956
01957
01958
01959 (void)cpl_table_or_selected_float(stdscat,"j_msig",CPL_LESS_THAN,0.2);
01960 (void)cpl_table_and_selected_float(stdscat,"k_msig",CPL_LESS_THAN,0.2);
01961 newstds = cpl_table_extract_selected(stdscat);
01962 nstd = (int)cpl_table_get_nrow(newstds);
01963
01964
01965
01966
01967
01968
01969 cat = vircam_tfits_get_table(ps.outcat);
01970 ncat = (int)cpl_table_get_nrow(cat);
01971 if (ncat > 500 && ncat > 2.0*nstd) {
01972 tmp = cpl_table_duplicate(cat);
01973 (void)cpl_table_or_selected_float(tmp,"Ellipticity",CPL_LESS_THAN,0.5);
01974 tmp2 = cpl_table_extract_selected(tmp);
01975 ncat = (int)cpl_table_get_nrow(tmp2);
01976 freetable(tmp);
01977 p = cpl_propertylist_new();
01978 cpl_propertylist_append_bool(p,"Aper_flux_3",TRUE);
01979 cpl_table_sort(tmp2,(const cpl_propertylist *)p);
01980 cpl_propertylist_delete(p);
01981 level = min(ncat,max(1,min(5000,max(500,2*nstd))));
01982 tmp = cpl_table_extract(tmp2,1,(cpl_size)level);
01983 freetable(tmp2);
01984 ncat = (int)cpl_table_get_nrow(tmp);
01985 cat = tmp;
01986 } else {
01987 tmp = NULL;
01988 }
01989
01990
01991
01992 (void)vircam_matchstds(cat,newstds,300.0,&(ps.matchstds),&status);
01993 freetable(stdscat);
01994 freetable(newstds);
01995 freetable(tmp);
01996 if (status != VIR_OK) {
01997 freetable(ps.matchstds);
01998 cpl_msg_error(fctid,"Failed to match standards to catalogue");
01999 return;
02000 }
02001 }
02002
02003
02020
02021
02022 extern void vircam_jmp_wcsfit(void) {
02023 int status,n,i;
02024 const char *fctid = "vircam_jmp_wcsfit";
02025 float *ra,*dec,*x,*y;
02026 double r,d;
02027 cpl_table *cat;
02028 cpl_wcs *wcs;
02029
02030
02031
02032 status = VIR_OK;
02033
02034
02035
02036 if (ps.matchstds == NULL) {
02037 cpl_msg_error(fctid,"No input matched standards catalogue found");
02038 return;
02039 }
02040
02041
02042
02043 (void)vircam_platesol(vircam_fits_get_ehu(ps.stack_frame),
02044 vircam_tfits_get_ehu(ps.outcat),ps.matchstds,
02045 6,1,&status);
02046 if (status != VIR_OK) {
02047 cpl_msg_error(fctid,"Failed to fit WCS");
02048 return;
02049 }
02050
02051
02052
02053 cat = vircam_tfits_get_table(ps.outcat);
02054 n = (int)cpl_table_get_nrow(cat);
02055 wcs = cpl_wcs_new_from_propertylist(vircam_fits_get_ehu(ps.stack_frame));
02056 if (wcs == NULL) {
02057 cpl_msg_error(fctid,"Failed to fill RA and Dec in catalogue");
02058 return;
02059 }
02060 x = cpl_table_get_data_float(cat,"X_coordinate");
02061 y = cpl_table_get_data_float(cat,"Y_coordinate");
02062 ra = cpl_table_get_data_float(cat,"RA");
02063 dec = cpl_table_get_data_float(cat,"DEC");
02064 for (i = 0; i < n; i++) {
02065 vircam_xytoradec(wcs,x[i],y[i],&r,&d);
02066 ra[i] = (float)r;
02067 dec[i] = (float)d;
02068 }
02069 cpl_wcs_delete(wcs);
02070 }
02071
02072
02088
02089
02090 extern void vircam_jmp_photcal(void) {
02091 int status;
02092 const char *fctid = "vircam_jmp_photcal";
02093 char filt[32];
02094 cpl_propertylist *pl;
02095
02096
02097
02098 status = VIR_OK;
02099
02100
02101
02102 if (ps.matchstds == NULL || (int)cpl_table_get_nrow(ps.matchstds) == 0) {
02103 cpl_msg_error(fctid,"No input matched standards catalogue found");
02104 return;
02105 }
02106
02107
02108
02109 if (vircam_pfits_get_filter(vircam_fits_get_phu(ps.stack_frame),filt) != VIR_OK) {
02110 cpl_msg_error(fctid,"No filter name in stack header");
02111 return;
02112 }
02113
02114
02115
02116 pl = vircam_tfits_get_ehu(ps.outcat);
02117 (void)vircam_photcal(&(ps.stack_frame),&(ps.matchstds),&pl,1,filt,
02118 ps.tphottab,&status);
02119 if (status != VIR_OK) {
02120 cpl_msg_error(fctid,"Failed to fit photometric zeropoint");
02121 return;
02122 }
02123 }
02124
02125
02142
02143
02144 extern void vircam_jmp_bpm2conf(void) {
02145 cpl_image *im;
02146 int i,n,*data;
02147
02148
02149
02150 im = vircam_fits_get_image(ps.fconf);
02151 n = (int)cpl_image_get_size_x(im)*(int)cpl_image_get_size_y(im);
02152 data = cpl_image_get_data_int(im);
02153
02154
02155
02156 for (i = 0; i < n; i++)
02157 data[i] = (data[i] == 1 ? 0 : 100);
02158
02159 }
02160
02161
02178
02179
02180 extern void vircam_jmp_skycor(void) {
02181 int i,ngood,status,nfrms;
02182 long npts;
02183 vir_fits **ftmp,*ff,**frms;
02184 const char *fctid = "vircam_jmp_skycor";
02185 unsigned char *rejmask,*rejplus;
02186 cpl_propertylist *drs;
02187 cpl_image *skyimg,*fim;
02188 float *data,med,sig;
02189
02190
02191
02192 if (offsky == 0 || offsky == 1) {
02193
02194
02195
02196 if (offsky == 1) {
02197 nfrms = ps.noffsets;
02198 frms = ps.offsky_fits;
02199 } else {
02200 nfrms = ps.nscience;
02201 frms = ps.sci_fits;
02202 }
02203
02204
02205
02206 ftmp = cpl_malloc(nfrms*sizeof(vir_fits *));
02207 ngood = 0;
02208 for (i = 0; i < nfrms; i++) {
02209 ff = frms[i];
02210 if (vircam_fits_get_status(ff) != VIR_FATAL)
02211 ftmp[ngood++] = ff;
02212 }
02213
02214
02215
02216 if (ngood == 0) {
02217 freespace(ftmp);
02218 cpl_msg_error(fctid,"Sky correction impossible. No good sky frames available");
02219 return;
02220 }
02221
02222
02223
02224 status = VIR_OK;
02225 (void)vircam_imcombine(ftmp,ngood,1,1,1,2.0,&skyimg,&rejmask,&rejplus,
02226 &drs,&status);
02227 freespace(rejmask);
02228 freespace(rejplus);
02229 if (offsky == 0)
02230 freepropertylist(drs);
02231 freespace(ftmp);
02232 } else {
02233 skyimg = vircam_fits_get_image(ps.fsky);
02234 }
02235
02236
02237
02238 data = cpl_image_get_data_float(skyimg);
02239 npts = (long)cpl_image_get_size_x(skyimg)*(long)cpl_image_get_size_y(skyimg);
02240 vircam_qmedsig(data,NULL,npts,5.0,3,-1000.0,65535.0,&med,&sig);
02241 for (i = 0; i < npts; i++)
02242 data[i] -= med;
02243
02244
02245
02246 if (offsky == 1) {
02247 ps.outsky = vircam_fits_wrap(skyimg,frms[0],NULL,NULL);
02248 vircam_merge_propertylists(vircam_fits_get_ehu(ps.outsky),drs);
02249 freepropertylist(drs);
02250 drs = vircam_fits_get_ehu(ps.outsky);
02251 cpl_propertylist_update_float(drs,"ESO QC SKYMED",med);
02252 cpl_propertylist_set_comment(drs,"ESO QC SKYMED",
02253 "Median sky background");
02254 cpl_propertylist_update_float(drs,"ESO QC SKYSIG",sig);
02255 cpl_propertylist_set_comment(drs,"ESO QC SKYSIG",
02256 "Sky background sigma");
02257 }
02258
02259
02260
02261
02262 for (i = 0; i < ps.nscience; i++) {
02263 ff = ps.sci_fits[i];
02264 fim = vircam_fits_get_image(ff);
02265 if (vircam_fits_get_status(ff) != VIR_FATAL) {
02266 drs = vircam_fits_get_ehu(ff);
02267 cpl_image_subtract(fim,skyimg);
02268 if (offsky == 0) {
02269 cpl_propertylist_update_string(drs,"ESO DRS SKYCOR","internal");
02270 } else if (offsky == 1) {
02271 cpl_propertylist_update_string(drs,"ESO DRS SKYCOR","offsky");
02272 } else if (offsky == -1) {
02273 cpl_propertylist_update_string(drs,"ESO DRS SKYCOR",
02274 vircam_fits_get_fullname(ps.fsky));
02275 }
02276 cpl_propertylist_set_comment(drs,"ESO DRS SKYCOR",
02277 "Sky correction method");
02278 }
02279 }
02280 for (i = 0; i < ps.noffsets; i++) {
02281 ff = ps.offsky_fits[i];
02282 fim = vircam_fits_get_image(ff);
02283 if (vircam_fits_get_status(ff) != VIR_FATAL) {
02284 drs = vircam_fits_get_ehu(ff);
02285 cpl_image_subtract(fim,skyimg);
02286 if (offsky == 0) {
02287 cpl_propertylist_update_string(drs,"ESO DRS SKYCOR","internal");
02288 } else if (offsky == 1) {
02289 cpl_propertylist_update_string(drs,"ESO DRS SKYCOR","offsky");
02290 } else if (offsky == -1) {
02291 cpl_propertylist_update_string(drs,"ESO DRS SKYCOR",
02292 vircam_fits_get_fullname(ps.fsky));
02293 }
02294 cpl_propertylist_set_comment(drs,"ESO DRS SKYCOR",
02295 "Image has been sky corrected");
02296 }
02297 }
02298
02299
02300
02301 if (offsky == 0)
02302 cpl_image_delete(skyimg);
02303 else if (offsky == 1)
02304 cpl_image_add_scalar(skyimg,med);
02305 return;
02306 }
02307
02308
02324
02325
02326 extern void vircam_jmp_get_readnoise_gain(int jext, float *readnoise,
02327 float *gain) {
02328 cpl_propertylist *p_rg;
02329 const char *fctid = "vircam_jmp_get_readnoise_gain";
02330
02331
02332
02333 p_rg = cpl_propertylist_load(cpl_frame_get_filename(ps.readgain_file),
02334 (cpl_size)jext);
02335
02336
02337
02338 switch (cpl_propertylist_get_type(p_rg,"ESO QC READNOISE")) {
02339 case CPL_TYPE_FLOAT:
02340 *readnoise = cpl_propertylist_get_float(p_rg,"ESO QC READNOISE");
02341 break;
02342 case CPL_TYPE_DOUBLE:
02343 *readnoise = (float)cpl_propertylist_get_double(p_rg,
02344 "ESO QC READNOISE");
02345 break;
02346 default:
02347 cpl_error_reset();
02348 *readnoise = 25.0;
02349 cpl_msg_error(fctid,"Unable to get READNOISE estimate, guessing %g\n",
02350 *readnoise);
02351 }
02352
02353
02354
02355 switch (cpl_propertylist_get_type(p_rg,"ESO QC CONAD")) {
02356 case CPL_TYPE_FLOAT:
02357 *gain = cpl_propertylist_get_float(p_rg,"ESO QC CONAD");
02358 break;
02359 case CPL_TYPE_DOUBLE:
02360 *gain = (float)cpl_propertylist_get_double(p_rg,"ESO QC CONAD");
02361 break;
02362 default:
02363 cpl_error_reset();
02364 *gain = 1.0;
02365 cpl_msg_error(fctid,"Unable to get GAIN estimate, guessing %g\n",
02366 *gain);
02367 }
02368 cpl_propertylist_delete(p_rg);
02369 }
02370
02371
02386
02387
02388 extern void vircam_jmp_illum(void) {
02389 int ngood,i,status,ii;
02390 float illcor_rms;
02391 vir_fits **ftmp,*ff,*ffc;
02392 char filt[32];
02393 cpl_table **mstds,*stdscat,*ms,*illcor,*ot;
02394 vir_tfits *outtab;
02395 cpl_propertylist **pl,*phu,*ehu;
02396 const char *fctid = "vircam_jmp_illum";
02397
02398
02399
02400 ps.illcor = NULL;
02401
02402
02403
02404 ftmp = cpl_malloc(ps.nscience*sizeof(vir_fits *));
02405 ngood = 0;
02406 for (i = 0; i < ps.nscience; i++) {
02407 ff = ps.sci_fits[i];
02408 if (vircam_fits_get_status(ff) != VIR_FATAL)
02409 ftmp[ngood++] = ff;
02410 }
02411
02412
02413
02414 if (ngood == 0) {
02415 freespace(ftmp);
02416 cpl_msg_error(fctid,"Illumination correction impossible. No good science frames available");
02417 return;
02418 }
02419
02420
02421
02422 if (vircam_pfits_get_filter(vircam_fits_get_phu(ftmp[0]),filt) != VIR_OK) {
02423 cpl_msg_error(fctid,"No filter name in stack header");
02424 freespace(ftmp);
02425 return;
02426 }
02427
02428
02429
02430
02431 mstds = cpl_malloc(ngood*sizeof(cpl_table *));
02432 for (i = 0; i < ngood; i++)
02433 mstds[i] = NULL;
02434 pl = cpl_malloc(ngood*sizeof(cpl_propertylist *));
02435 for (i = 0; i < ngood; i++)
02436 pl[i] = NULL;
02437
02438
02439
02440
02441 ffc = ps.fconf;
02442 for (i = 0; i < ngood; i++) {
02443 status = VIR_OK;
02444 ff = ftmp[i];
02445 (void)vircam_imcore(ff,ffc,vircam_jmp_config.ipix,
02446 1.5*vircam_jmp_config.threshold,0,
02447 vircam_jmp_config.rcore,vircam_jmp_config.nbsize,2,
02448 3.5,&outtab,&status);
02449 if (status != VIR_OK)
02450 cpl_error_reset();
02451 pl[i] = cpl_propertylist_duplicate(vircam_tfits_get_ehu(outtab));
02452
02453
02454
02455 (void)vircam_getstds(vircam_fits_get_ehu(ff),1,current_catpath,
02456 current_cat,&stdscat,&status);
02457 if (status == VIR_FATAL) {
02458 freetfits(outtab);
02459 freespace(ftmp);
02460 for (ii = 0; ii < ngood; ii++) {
02461 freetable(mstds[ii]);
02462 freepropertylist(pl[ii]);
02463 }
02464 freespace(mstds);
02465 freespace(pl);
02466 cpl_msg_error(fctid,"Illumination correction fails");
02467 return;
02468 } else if (status == VIR_WARN) {
02469 cpl_error_reset();
02470 freetfits(outtab);
02471 continue;
02472 }
02473
02474
02475
02476 ot = vircam_tfits_get_table(outtab);
02477 (void)vircam_matchstds(ot,stdscat,300.0,&ms,&status);
02478 if (status == VIR_FATAL) {
02479 freetable(stdscat);
02480 freetfits(outtab);
02481 freespace(ftmp);
02482 for (ii = 0; ii < ngood; ii++) {
02483 freetable(mstds[ii]);
02484 freepropertylist(pl[ii]);
02485 }
02486 freespace(mstds);
02487 freespace(pl);
02488 cpl_msg_error(fctid,"%s",cpl_error_get_message());
02489 return;
02490 }
02491 mstds[i] = ms;
02492 freetfits(outtab);
02493 freetable(stdscat);
02494 }
02495
02496
02497
02498 status = VIR_OK;
02499 (void)vircam_illum(ftmp,mstds,pl,ngood,filt,ps.tphottab,128,&illcor,
02500 &illcor_rms,&status);
02501
02502
02503
02504 phu = cpl_propertylist_duplicate(vircam_fits_get_phu(ftmp[0]));
02505 ehu = cpl_propertylist_duplicate(vircam_fits_get_ehu(ftmp[0]));
02506 ps.illcor = vircam_tfits_wrap(illcor,NULL,phu,ehu);
02507 cpl_propertylist_update_float(ehu,"ESO QC ILLUMCOR_RMS",illcor_rms);
02508 cpl_propertylist_set_comment(ehu,"ESO QC ILLUMCOR_RMS",
02509 "RMS of illumination correction map");
02510
02511
02512
02513 for (i = 0; i < ngood; i++) {
02514 freetable(mstds[i]);
02515 freepropertylist(pl[i]);
02516 }
02517 freespace(mstds);
02518 freespace(pl);
02519 freespace(ftmp);
02520 }
02521
02522
02544
02545
02546 static char *vircam_jmp_outfile(const char *bname, int ind, int isfits) {
02547 int nf;
02548 char *fname;
02549
02550
02551
02552
02553 nf = strlen(bname);
02554 if (ind == 0)
02555 nf++;
02556 else
02557 nf += ((int)log10((double)ind)+1);
02558 if (isfits)
02559 nf += 7;
02560 else
02561 nf += 2;
02562
02563
02564
02565 fname = cpl_malloc(nf);
02566
02567
02568
02569 if (isfits)
02570 (void)snprintf(fname,nf,"%s_%d.fits",bname,ind);
02571 else
02572 (void)snprintf(fname,nf,"%s_%d",bname,ind);
02573 return(fname);
02574 }
02575
02576
02591
02592
02593 extern void vircam_jmp_init(void) {
02594
02595
02596
02597 ps.labels = NULL;
02598 ps.master_dark = NULL;
02599 ps.master_twilight_flat = NULL;
02600 ps.master_conf = NULL;
02601 ps.master_sky = NULL;
02602 ps.mask = NULL;
02603 ps.chantab = NULL;
02604 ps.phottab = NULL;
02605 ps.tphottab = NULL;
02606 ps.readgain_file = NULL;
02607 ps.science_frames = NULL;
02608 ps.offset_skies = NULL;
02609 ps.product_frames_simple = NULL;
02610 ps.product_frames_simple_off = NULL;
02611 ps.product_frames_super = NULL;
02612 ps.product_frames_superc = NULL;
02613 ps.product_frame_stack = NULL;
02614 ps.product_frame_stackc = NULL;
02615 ps.product_frame_cat = NULL;
02616 ps.product_frame_illcor = NULL;
02617 ps.product_frame_sky = NULL;
02618 ps.phupaf = NULL;
02619 ps.gaincors = NULL;
02620 ps.catpath = NULL;
02621 ps.catname = NULL;
02622 ps.catpath2 = NULL;
02623 ps.catname2 = NULL;
02624
02625
02626
02627 ps.fdark = NULL;
02628 ps.fflat = NULL;
02629 ps.fconf = NULL;
02630 ps.fsky = NULL;
02631 ps.fchantab = NULL;
02632 ps.nscience = 0;
02633 ps.sci_fits = NULL;
02634 ps.noffsets = 0;
02635 ps.offsky_fits = NULL;
02636 ps.nustep_sets = 0;
02637 ps.ustep_sets = NULL;
02638 ps.ndith = 0;
02639 ps.ndithc = 0;
02640 ps.dith_input = NULL;
02641 ps.dithc_input = NULL;
02642 ps.stack_frame = NULL;
02643 ps.stackc_frame = NULL;
02644 ps.outcat = NULL;
02645 ps.outsky = NULL;
02646 ps.illcor = NULL;
02647 }
02648
02649
02670
02671
02672 extern void vircam_jmp_tidy(int level) {
02673 int i;
02674
02675
02676
02677 freefits(ps.fdark);
02678 freefits(ps.fflat);
02679 freefits(ps.fconf);
02680 freefits(ps.fsky);
02681 freetfits(ps.fchantab);
02682 freefitslist(ps.sci_fits,ps.nscience);
02683 freefitslist(ps.offsky_fits,ps.noffsets);
02684 ps.nscience = 0;
02685 for (i = 0; i < ps.nustep_sets; i++) {
02686 freespace(ps.ustep_sets[i].f);
02687 freefits(ps.ustep_sets[i].super);
02688 freefits(ps.ustep_sets[i].superc);
02689 }
02690 freespace(ps.ustep_sets);
02691 ps.nustep_sets = 0;
02692 freespace(ps.dith_input);
02693 ps.ndith = 0;
02694 freespace(ps.dithc_input);
02695 ps.ndithc = 0;
02696
02697 freefits(ps.stack_frame);
02698 freefits(ps.stackc_frame);
02699 freetfits(ps.outcat);
02700 freetable(ps.matchstds);
02701 freetfits(ps.illcor);
02702 freefits(ps.outsky);
02703
02704 if (level == 1)
02705 return;
02706
02707
02708
02709 freespace(ps.labels);
02710 freeframe(ps.master_dark);
02711 freeframe(ps.master_twilight_flat);
02712 freeframe(ps.master_conf);
02713 freeframe(ps.master_sky);
02714 freemask(ps.mask);
02715 freeframe(ps.chantab);
02716 freeframe(ps.phottab);
02717 freeframe(ps.readgain_file);
02718 freetable(ps.tphottab);
02719 freeframeset(ps.science_frames);
02720 freeframeset(ps.offset_skies);
02721 freepropertylist(ps.phupaf);
02722 freespace(ps.product_frames_simple);
02723 freespace(ps.product_frames_super);
02724 freespace(ps.product_frames_superc);
02725 freespace(ps.product_frames_simple_off);
02726 freespace(ps.gaincors);
02727 freespace(ps.catpath);
02728 freespace(ps.catname);
02729 freespace(ps.catpath2);
02730 freespace(ps.catname2);
02731 }
02732
02735
02736
02737
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
02918
02919
02920
02921
02922
02923
02924
02925
02926
02927
02928
02929
02930
02931
02932