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 <stdio.h>
00035 #include <cpl.h>
00036 #include <math.h>
00037
00038 #include "vircam_utils.h"
00039 #include "vircam_mask.h"
00040 #include "vircam_dfs.h"
00041 #include "vircam_mods.h"
00042 #include "vircam_stats.h"
00043 #include "vircam_fits.h"
00044 #include "vircam_pfits.h"
00045 #include "vircam_channel.h"
00046 #include "vircam_paf.h"
00047 #include "vircam_wcsutils.h"
00048
00049
00050
00051 #define MEANTWI 1
00052 #define CONFMAP 2
00053 #define RATIMG 4
00054 #define STATS_TAB 8
00055
00056
00057
00058 static int vircam_twilight_flat_combine_create(cpl_plugin *) ;
00059 static int vircam_twilight_flat_combine_exec(cpl_plugin *) ;
00060 static int vircam_twilight_flat_combine_destroy(cpl_plugin *) ;
00061 static int vircam_twilight_flat_combine(cpl_parameterlist *, cpl_frameset *) ;
00062 static int vircam_twilight_flat_combine_save(cpl_frameset *framelist,
00063 cpl_parameterlist *parlist);
00064 static void vircam_twilight_flat_combine_dummy_products(void);
00065 static void vircam_twilight_flat_combine_normal(int jext);
00066 static int vircam_twilight_flat_combine_lastbit(int jext,
00067 cpl_frameset *framelist,
00068 cpl_parameterlist *parlist);
00069 static void vircam_twilight_flat_combine_init(void);
00070 static void vircam_twilight_flat_combine_tidy(int level);
00071
00072
00073
00074 static struct {
00075
00076
00077
00078 float lthr;
00079 float hthr;
00080 int combtype;
00081 int scaletype;
00082 int xrej;
00083 float thresh;
00084 int ncells;
00085 int extenum;
00086
00087
00088
00089 float flatrms;
00090 float flatratio_med;
00091 float flatratio_rms;
00092
00093 } vircam_twilight_flat_combine_config;
00094
00095
00096 static struct {
00097 vir_fits **good;
00098 int ngood;
00099 int *labels;
00100 cpl_frameset *twilightlist;
00101 cpl_frame *master_dark;
00102 cpl_frame *master_twilight_flat;
00103 vir_mask *master_mask;
00104 cpl_frame *chantab;
00105
00106 cpl_image *outimage;
00107 cpl_image *outconf;
00108 vir_fits **twilights;
00109 int ntwilights;
00110 cpl_propertylist *drs;
00111 cpl_propertylist *drs2;
00112 unsigned char *rejmask;
00113 unsigned char *rejplus;
00114 vir_fits *mfimage;
00115 cpl_image *ratioimg;
00116 cpl_table *ratioimstats;
00117 vir_tfits *ctable;
00118 vir_fits *mdark;
00119 cpl_propertylist *phupaf;
00120 } ps;
00121
00122 static int isfirst;
00123 static cpl_frame *product_frame_mean_twi = NULL;
00124 static cpl_frame *product_frame_conf = NULL;
00125 static cpl_frame *product_frame_ratioimg = NULL;
00126 static cpl_frame *product_frame_ratioimg_stats = NULL;
00127 static int we_expect;
00128 static int we_get;
00129
00130 static char vircam_twilight_flat_combine_description[] =
00131 "vircam_twilight_flat_combine -- VIRCAM twilight flat combine recipe.\n\n"
00132 "Combine a list of twilight flat frames into a mean frame. Optionally\n"
00133 "compare the output frame to a master twilight flat frame\n\n"
00134 "The program accepts the following files in the SOF:\n\n"
00135 " Tag Description\n"
00136 " -----------------------------------------------------------------------\n"
00137 " %-21s A list of raw twilight flat images\n"
00138 " %-21s A master dark frame\n"
00139 " %-21s Optional reference twilight flat frame\n"
00140 " %-21s Optional channel table or\n"
00141 " %-21s Optional initial channel table\n"
00142 " %-21s Optional master bad pixel map or\n"
00143 " %-21s Optional master confidence map\n"
00144 "If no master twilight flat is made available, then no comparison will be\n"
00145 "done. This means there will be no output ratio image. If a master twilight\n"
00146 "is available, but no channel table is, then a ratio image will be formed\n"
00147 "but no stats will be written."
00148 "\n";
00149
00280
00281
00282
00290
00291
00292 int cpl_plugin_get_info(cpl_pluginlist *list) {
00293 cpl_recipe *recipe = cpl_calloc(1,sizeof(*recipe));
00294 cpl_plugin *plugin = &recipe->interface;
00295 char alldesc[SZ_ALLDESC];
00296 (void)snprintf(alldesc,SZ_ALLDESC,vircam_twilight_flat_combine_description,
00297 VIRCAM_TWI_RAW,VIRCAM_CAL_DARK,VIRCAM_REF_TWILIGHT_FLAT,
00298 VIRCAM_CAL_CHANTAB,VIRCAM_CAL_CHANTAB_INIT,VIRCAM_CAL_BPM,
00299 VIRCAM_CAL_CONF);
00300
00301 cpl_plugin_init(plugin,
00302 CPL_PLUGIN_API,
00303 VIRCAM_BINARY_VERSION,
00304 CPL_PLUGIN_TYPE_RECIPE,
00305 "vircam_twilight_flat_combine",
00306 "VIRCAM twilight combination recipe",
00307 alldesc,
00308 "Jim Lewis",
00309 "jrl@ast.cam.ac.uk",
00310 vircam_get_license(),
00311 vircam_twilight_flat_combine_create,
00312 vircam_twilight_flat_combine_exec,
00313 vircam_twilight_flat_combine_destroy);
00314
00315 cpl_pluginlist_append(list,plugin);
00316
00317 return(0);
00318 }
00319
00320
00329
00330
00331 static int vircam_twilight_flat_combine_create(cpl_plugin *plugin) {
00332 cpl_recipe *recipe;
00333 cpl_parameter *p;
00334
00335
00336
00337 if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
00338 recipe = (cpl_recipe *)plugin;
00339 else
00340 return(-1);
00341
00342
00343
00344 recipe->parameters = cpl_parameterlist_new();
00345
00346
00347
00348 p = cpl_parameter_new_value("vircam.vircam_twilight_flat_combine.lthr",
00349 CPL_TYPE_DOUBLE,
00350 "Low rejection threshold for underexpsed images",
00351 "vircam.vircam_twilight_flat_combine",
00352 0.0);
00353 cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI,"lthr");
00354 cpl_parameterlist_append(recipe->parameters,p);
00355
00356
00357
00358 p = cpl_parameter_new_value("vircam.vircam_twilight_flat_combine.hthr",
00359 CPL_TYPE_DOUBLE,
00360 "High rejection threshold for overexposed images",
00361 "vircam.vircam_twilight_flat_combine",
00362 65535.0);
00363 cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI,"hthr");
00364 cpl_parameterlist_append(recipe->parameters,p);
00365
00366
00367
00368 p = cpl_parameter_new_range("vircam.vircam_twilight_flat_combine.combtype",
00369 CPL_TYPE_INT,
00370 "1 == Median,\n 2 == Mean",
00371 "vircam.vircam_twilight_flat_combine",
00372 2,1,2);
00373 cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI,"combtype");
00374 cpl_parameterlist_append(recipe->parameters,p);
00375
00376
00377
00378 p = cpl_parameter_new_range("vircam.vircam_twilight_flat_combine.scaletype",
00379 CPL_TYPE_INT,
00380 "0 == none,\n 1 == additive offset,\n 2 == multiplicative offset,\n 3 == exposure time scaling + additive offset",
00381 "vircam.vircam_twilight_flat_combine",
00382 2,0,3);
00383 cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI,"scaletype");
00384 cpl_parameterlist_append(recipe->parameters,p);
00385
00386
00387
00388 p = cpl_parameter_new_value("vircam.vircam_twilight_flat_combine.xrej",
00389 CPL_TYPE_BOOL,
00390 "True if using extra rejection cycle",
00391 "vircam.vircam_twilight_flat_combine",
00392 TRUE);
00393 cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI,"xrej");
00394 cpl_parameterlist_append(recipe->parameters,p);
00395
00396
00397
00398 p = cpl_parameter_new_value("vircam.vircam_twilight_flat_combine.thresh",
00399 CPL_TYPE_DOUBLE,
00400 "Rejection threshold in sigma above background",
00401 "vircam.vircam_twilight_flat_combine",5.0);
00402 cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI,"thresh");
00403 cpl_parameterlist_append(recipe->parameters,p);
00404
00405
00406
00407 p = cpl_parameter_new_enum("vircam.vircam_twilight_flat_combine.ncells",
00408 CPL_TYPE_INT,
00409 "Number of cells for data channel stats",
00410 "vircam.vircam_twilight_flat_combine",8,7,1,2,4,
00411 8,16,32,64);
00412 cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI,"ncells");
00413 cpl_parameterlist_append(recipe->parameters,p);
00414
00415
00416
00417 p = cpl_parameter_new_range("vircam.vircam_twilight_flat_combine.extenum",
00418 CPL_TYPE_INT,
00419 "Extension number to be done, 0 == all",
00420 "vircam.vircam_twilight_flat_combine",
00421 1,0,16);
00422 cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI,"ext");
00423 cpl_parameterlist_append(recipe->parameters,p);
00424
00425
00426
00427 return(0);
00428 }
00429
00430
00431
00437
00438
00439 static int vircam_twilight_flat_combine_exec(cpl_plugin *plugin) {
00440 cpl_recipe *recipe;
00441
00442
00443
00444 if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
00445 recipe = (cpl_recipe *)plugin;
00446 else
00447 return(-1);
00448
00449 return(vircam_twilight_flat_combine(recipe->parameters,recipe->frames));
00450 }
00451
00452
00458
00459
00460 static int vircam_twilight_flat_combine_destroy(cpl_plugin *plugin) {
00461 cpl_recipe *recipe ;
00462
00463
00464
00465 if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
00466 recipe = (cpl_recipe *)plugin;
00467 else
00468 return(-1);
00469
00470 cpl_parameterlist_delete(recipe->parameters);
00471 return(0);
00472 }
00473
00474
00481
00482
00483 static int vircam_twilight_flat_combine(cpl_parameterlist *parlist,
00484 cpl_frameset *framelist) {
00485 const char *fctid="vircam_twilight_flat_combine";
00486 int nlab,j,jst,jfn,retval,status,live,nx,ny,ndit;
00487 long i;
00488 cpl_parameter *p;
00489 vir_fits *ff;
00490 cpl_propertylist *pp;
00491
00492
00493
00494 if (framelist == NULL || cpl_frameset_get_size(framelist) <= 0) {
00495 cpl_msg_error(fctid,"Input framelist NULL or has no input data\n");
00496 return(-1);
00497 }
00498
00499
00500
00501 vircam_twilight_flat_combine_init();
00502 we_expect = MEANTWI + CONFMAP;
00503
00504
00505
00506 p = cpl_parameterlist_find(parlist,"vircam.vircam_twilight_flat_combine.lthr");
00507 vircam_twilight_flat_combine_config.lthr =
00508 (float)cpl_parameter_get_double(p);
00509 p = cpl_parameterlist_find(parlist,
00510 "vircam.vircam_twilight_flat_combine.hthr");
00511 vircam_twilight_flat_combine_config.hthr =
00512 (float)cpl_parameter_get_double(p);
00513 p = cpl_parameterlist_find(parlist,
00514 "vircam.vircam_twilight_flat_combine.combtype");
00515 vircam_twilight_flat_combine_config.combtype = cpl_parameter_get_int(p);
00516 p = cpl_parameterlist_find(parlist,
00517 "vircam.vircam_twilight_flat_combine.scaletype");
00518 vircam_twilight_flat_combine_config.scaletype = cpl_parameter_get_int(p);
00519 p = cpl_parameterlist_find(parlist,
00520 "vircam.vircam_twilight_flat_combine.xrej");
00521 vircam_twilight_flat_combine_config.xrej = cpl_parameter_get_bool(p);
00522 p = cpl_parameterlist_find(parlist,
00523 "vircam.vircam_twilight_flat_combine.thresh");
00524 vircam_twilight_flat_combine_config.thresh =
00525 (float)cpl_parameter_get_double(p);
00526 p = cpl_parameterlist_find(parlist,
00527 "vircam.vircam_twilight_flat_combine.ncells");
00528 vircam_twilight_flat_combine_config.ncells = cpl_parameter_get_int(p);
00529 p = cpl_parameterlist_find(parlist,
00530 "vircam.vircam_twilight_flat_combine.extenum");
00531 vircam_twilight_flat_combine_config.extenum = cpl_parameter_get_int(p);
00532
00533
00534
00535 if (vircam_dfs_set_groups(framelist) != VIR_OK) {
00536 cpl_msg_error(fctid,"Cannot identify RAW and CALIB frames");
00537 vircam_twilight_flat_combine_tidy(2);
00538 return(-1);
00539 }
00540
00541
00542
00543 if ((ps.labels = cpl_frameset_labelise(framelist,vircam_compare_tags,
00544 &nlab)) == NULL) {
00545 cpl_msg_error(fctid,"Cannot labelise the input frames");
00546 vircam_twilight_flat_combine_tidy(2);
00547 return(-1);
00548 }
00549 if ((ps.twilightlist = vircam_frameset_subgroup(framelist,ps.labels,nlab,
00550 VIRCAM_TWI_RAW)) == NULL) {
00551 cpl_msg_error(fctid,"Cannot find twilight frames in input frameset");
00552 vircam_twilight_flat_combine_tidy(2);
00553 return(-1);
00554 }
00555 ps.ntwilights = cpl_frameset_get_size(ps.twilightlist);
00556
00557
00558
00559 if ((ps.master_dark = vircam_frameset_subgroup_1(framelist,ps.labels,nlab,
00560 VIRCAM_CAL_DARK)) == NULL) {
00561 cpl_msg_error(fctid,"No master dark found");
00562 vircam_twilight_flat_combine_tidy(2);
00563 return(-1);
00564 }
00565
00566
00567
00568 if ((ps.master_twilight_flat = vircam_frameset_subgroup_1(framelist,
00569 ps.labels,nlab,
00570 VIRCAM_REF_TWILIGHT_FLAT)) == NULL)
00571 cpl_msg_info(fctid,"No master twilight flat found -- no ratio image will be formed");
00572 else
00573 we_expect |= RATIMG;
00574
00575
00576
00577
00578 ps.master_mask = vircam_mask_define(framelist,ps.labels,nlab);
00579
00580
00581
00582 if ((ps.chantab = vircam_frameset_subgroup_1(framelist,ps.labels,nlab,
00583 VIRCAM_CAL_CHANTAB)) == NULL) {
00584 if ((ps.chantab = vircam_frameset_subgroup_1(framelist,ps.labels,nlab,
00585 VIRCAM_CAL_CHANTAB_INIT)) == NULL) {
00586 cpl_msg_info(fctid,"No channel table found -- no ratio image stats and no linearisation will be done");
00587 } else {
00588 cpl_msg_info(fctid,"Channel table is labelled INIT -- no linearisation will be done");
00589 if (we_expect & RATIMG)
00590 we_expect |= STATS_TAB;
00591 }
00592 } else if (we_expect & RATIMG) {
00593 we_expect |= STATS_TAB;
00594 }
00595
00596
00597
00598 pp = cpl_propertylist_load(cpl_frame_get_filename(cpl_frameset_get_frame(ps.twilightlist,0)),0);
00599 if (vircam_pfits_get_ndit(pp,&ndit) != VIR_OK) {
00600 cpl_msg_error(fctid,"No value for NDIT available");
00601 freepropertylist(pp);
00602 vircam_twilight_flat_combine_tidy(2);
00603 return(-1);
00604 }
00605 cpl_propertylist_delete(pp);
00606
00607
00608
00609
00610
00611 vircam_exten_range(vircam_twilight_flat_combine_config.extenum,
00612 (const cpl_frame *)cpl_frameset_get_frame(ps.twilightlist,0),
00613 &jst,&jfn);
00614 if (jst == -1 || jfn == -1) {
00615 cpl_msg_error(fctid,"Unable to continue");
00616 vircam_twilight_flat_combine_tidy(2);
00617 return(-1);
00618 }
00619
00620
00621
00622 ps.good = cpl_malloc(ps.ntwilights*sizeof(vir_fits *));
00623
00624
00625
00626 for (j = jst; j <= jfn; j++) {
00627 status = VIR_OK;
00628 we_get = 0;
00629 isfirst = (j == jst);
00630
00631
00632
00633 ps.twilights = vircam_fits_load_list(ps.twilightlist,CPL_TYPE_FLOAT,j);
00634 if (ps.twilights == NULL) {
00635 cpl_msg_info(fctid,"Extension %d twilights wouldn't load",j);
00636 retval = vircam_twilight_flat_combine_lastbit(j,framelist,parlist);
00637 if (retval != 0)
00638 return(-1);
00639 continue;
00640 }
00641
00642
00643
00644 ps.ngood = 0;
00645 for (i = 0; i < ps.ntwilights; i++) {
00646 ff = ps.twilights[i];
00647 vircam_pfits_get_detlive(vircam_fits_get_ehu(ff),&live);
00648 if (! live) {
00649 cpl_msg_info(fctid,"Detector flagged dead %s",
00650 vircam_fits_get_fullname(ff));
00651 vircam_fits_set_error(ff,VIR_FATAL);
00652 } else {
00653 ps.good[ps.ngood] = ff;
00654 ps.ngood += 1;
00655 }
00656 }
00657
00658
00659
00660
00661 if (ps.ngood == 0) {
00662 cpl_msg_info(fctid,"All images flagged bad for this extension");
00663 retval = vircam_twilight_flat_combine_lastbit(j,framelist,parlist);
00664 if (retval != 0)
00665 return(-1);
00666 continue;
00667 }
00668
00669
00670
00671 vircam_overexp(ps.good,&(ps.ngood),
00672 vircam_twilight_flat_combine_config.lthr,
00673 vircam_twilight_flat_combine_config.hthr,0);
00674
00675
00676
00677
00678 if (ps.ngood == 0) {
00679 cpl_msg_info(fctid,"All images either under or overexposed");
00680 retval = vircam_twilight_flat_combine_lastbit(j,framelist,parlist);
00681 if (retval != 0)
00682 return(-1);
00683 continue;
00684 }
00685
00686
00687
00688 nx = cpl_image_get_size_x(vircam_fits_get_image(ps.good[0]));
00689 ny = cpl_image_get_size_y(vircam_fits_get_image(ps.good[0]));
00690 if (vircam_mask_load(ps.master_mask,j,nx,ny) == VIR_FATAL) {
00691 cpl_msg_info(fctid,"Unable to load mask image %s[%d]",
00692 vircam_mask_get_filename(ps.master_mask),j);
00693 cpl_msg_info(fctid,"Forcing all pixels to be good from now on");
00694 vircam_mask_force(ps.master_mask,nx,ny);
00695 }
00696
00697
00698
00699
00700 ps.mdark = vircam_fits_load(ps.master_dark,CPL_TYPE_FLOAT,j);
00701 if (ps.mdark == NULL) {
00702 cpl_msg_info(fctid,"Can't load master dark for extension %d",j);
00703 retval = vircam_twilight_flat_combine_lastbit(j,framelist,parlist);
00704 if (retval != 0)
00705 return(-1);
00706 continue;
00707 } else if (vircam_is_dummy(vircam_fits_get_ehu(ps.mdark))) {
00708 cpl_msg_info(fctid,"Can't master dark extension %d is a dummy",j);
00709 retval = vircam_twilight_flat_combine_lastbit(j,framelist,parlist);
00710 if (retval != 0)
00711 return(-1);
00712 continue;
00713 }
00714
00715
00716
00717 cpl_msg_info(fctid,"Dark correcting extension %d\n",j);
00718 for (i = 0; i < ps.ngood; i++)
00719 vircam_darkcor((ps.good)[i],ps.mdark,1.0,&status);
00720
00721
00722
00723
00724 if (ps.chantab != NULL) {
00725 ps.ctable = vircam_tfits_load(ps.chantab,j);
00726 if (ps.ctable == NULL) {
00727 cpl_msg_info(fctid,"Channel table extension %d won't load",j);
00728 } else if (vircam_chantab_verify(vircam_tfits_get_table(ps.ctable)) != VIR_OK) {
00729 cpl_msg_info(fctid,"Channel table extension %d has errors",j);
00730 freetfits(ps.ctable);
00731 } else {
00732 pp = cpl_propertylist_load(cpl_frame_get_filename(ps.chantab),
00733 j);
00734 if (vircam_is_dummy(pp)) {
00735 cpl_msg_info(fctid,
00736 "Channel table extensions %d is a dummy",j);
00737 freetfits(ps.ctable);
00738 }
00739 freepropertylist(pp);
00740 }
00741 } else
00742 ps.ctable = NULL;
00743
00744
00745
00746
00747 if (ps.ctable != NULL) {
00748 cpl_msg_info(fctid,"Linearising extension %d\n",j);
00749 for (i = 0; i < ps.ngood; i++)
00750 (void)vircam_lincor((ps.good)[i],ps.ctable,1,ndit,&status);
00751 }
00752
00753
00754
00755 cpl_msg_info(fctid,"Doing combination for extension %d\n",j);
00756 (void)vircam_imcombine(ps.good,ps.ngood,
00757 vircam_twilight_flat_combine_config.combtype,
00758 vircam_twilight_flat_combine_config.scaletype,
00759 vircam_twilight_flat_combine_config.xrej,
00760 vircam_twilight_flat_combine_config.thresh,
00761 &(ps.outimage),&(ps.rejmask),&(ps.rejplus),
00762 &(ps.drs),&status);
00763
00764
00765
00766
00767 if (status == VIR_OK) {
00768 we_get |= MEANTWI;
00769 vircam_twilight_flat_combine_normal(j);
00770 } else {
00771 cpl_msg_info(fctid,"A processing step failed");
00772 }
00773
00774
00775
00776 retval = vircam_twilight_flat_combine_lastbit(j,framelist,parlist);
00777 if (retval != 0)
00778 return(-1);
00779
00780 }
00781 vircam_twilight_flat_combine_tidy(2);
00782 return(0);
00783 }
00784
00785
00786
00793
00794
00795 static int vircam_twilight_flat_combine_save(cpl_frameset *framelist,
00796 cpl_parameterlist *parlist) {
00797 cpl_propertylist *plist,*elist,*p,*pafprop;
00798 int status;
00799 const char *fctid = "vircam_twilight_flat_combine_save";
00800 const char *outfile = "twilightcomb.fits";
00801 const char *outdiff = "twilightratio.fits";
00802 const char *outdimst = "twilightratiotab.fits";
00803 const char *outconf = "twilightconf.fits";
00804 const char *outfilepaf = "twilightcomb";
00805 const char *outdiffpaf = "twilightratio";
00806 const char *recipeid = "vircam_twilight_flat_combine";
00807
00808
00809
00810
00811 if (isfirst) {
00812
00813
00814
00815 product_frame_mean_twi = cpl_frame_new();
00816 cpl_frame_set_filename(product_frame_mean_twi,outfile);
00817 cpl_frame_set_tag(product_frame_mean_twi,VIRCAM_PRO_TWILIGHT_FLAT);
00818 cpl_frame_set_type(product_frame_mean_twi,CPL_FRAME_TYPE_IMAGE);
00819 cpl_frame_set_group(product_frame_mean_twi,CPL_FRAME_GROUP_PRODUCT);
00820 cpl_frame_set_level(product_frame_mean_twi,CPL_FRAME_LEVEL_FINAL);
00821
00822
00823
00824 plist = vircam_fits_get_phu(ps.twilights[0]);
00825 ps.phupaf = vircam_paf_phu_items(plist);
00826 vircam_dfs_set_product_primary_header(plist,product_frame_mean_twi,
00827 framelist,parlist,
00828 (char *)recipeid,
00829 "PRO-1.15");
00830
00831
00832
00833 if (cpl_image_save(NULL,outfile,CPL_BPP_8_UNSIGNED,plist,
00834 CPL_IO_DEFAULT) != CPL_ERROR_NONE) {
00835 cpl_msg_error(fctid,"Cannot save product PHU");
00836 cpl_frame_delete(product_frame_mean_twi);
00837 return(-1);
00838 }
00839 cpl_frameset_insert(framelist,product_frame_mean_twi);
00840
00841
00842
00843
00844 product_frame_conf = cpl_frame_new();
00845 cpl_frame_set_filename(product_frame_conf,outconf);
00846 cpl_frame_set_tag(product_frame_conf,VIRCAM_PRO_CONF);
00847 cpl_frame_set_type(product_frame_conf,CPL_FRAME_TYPE_IMAGE);
00848 cpl_frame_set_group(product_frame_conf,CPL_FRAME_GROUP_PRODUCT);
00849 cpl_frame_set_level(product_frame_conf,CPL_FRAME_LEVEL_FINAL);
00850
00851
00852
00853 plist = vircam_fits_get_phu(ps.twilights[0]);
00854 vircam_dfs_set_product_primary_header(plist,product_frame_conf,
00855 framelist,parlist,
00856 (char *)recipeid,
00857 "PRO-1.15");
00858
00859
00860
00861 if (cpl_image_save(NULL,outconf,CPL_BPP_8_UNSIGNED,plist,
00862 CPL_IO_DEFAULT) != CPL_ERROR_NONE) {
00863 cpl_msg_error(fctid,"Cannot save product PHU");
00864 cpl_frame_delete(product_frame_conf);
00865 return(-1);
00866 }
00867 cpl_frameset_insert(framelist,product_frame_conf);
00868
00869
00870
00871 if (we_expect & RATIMG) {
00872 product_frame_ratioimg = cpl_frame_new();
00873 cpl_frame_set_filename(product_frame_ratioimg,outdiff);
00874 cpl_frame_set_tag(product_frame_ratioimg,
00875 VIRCAM_PRO_RATIOIMG_TWILIGHT_FLAT);
00876 cpl_frame_set_type(product_frame_ratioimg,CPL_FRAME_TYPE_IMAGE);
00877 cpl_frame_set_group(product_frame_ratioimg,
00878 CPL_FRAME_GROUP_PRODUCT);
00879 cpl_frame_set_level(product_frame_ratioimg,CPL_FRAME_LEVEL_FINAL);
00880
00881
00882
00883 plist = vircam_fits_get_phu(ps.twilights[0]);
00884 vircam_dfs_set_product_primary_header(plist,product_frame_ratioimg,
00885 framelist,parlist,
00886 (char *)recipeid,
00887 "PRO-1.15");
00888
00889
00890
00891 if (cpl_image_save(NULL,outdiff,CPL_BPP_8_UNSIGNED,plist,
00892 CPL_IO_DEFAULT) != CPL_ERROR_NONE) {
00893 cpl_msg_error(fctid,"Cannot save product PHU");
00894 cpl_frame_delete(product_frame_ratioimg);
00895 return(-1);
00896 }
00897 cpl_frameset_insert(framelist,product_frame_ratioimg);
00898 }
00899
00900
00901
00902
00903 if (we_expect & STATS_TAB) {
00904 product_frame_ratioimg_stats = cpl_frame_new();
00905 cpl_frame_set_filename(product_frame_ratioimg_stats,outdimst);
00906 cpl_frame_set_tag(product_frame_ratioimg_stats,
00907 VIRCAM_PRO_RATIOIMG_TWILIGHT_FLAT_STATS);
00908 cpl_frame_set_type(product_frame_ratioimg_stats,
00909 CPL_FRAME_TYPE_TABLE);
00910 cpl_frame_set_group(product_frame_ratioimg_stats,
00911 CPL_FRAME_GROUP_PRODUCT);
00912 cpl_frame_set_level(product_frame_ratioimg_stats,
00913 CPL_FRAME_LEVEL_FINAL);
00914
00915
00916
00917 plist = vircam_fits_get_phu(ps.twilights[0]);
00918 vircam_dfs_set_product_primary_header(plist,
00919 product_frame_ratioimg_stats,
00920 framelist,parlist,
00921 (char *)recipeid,
00922 "PRO-1.15");
00923
00924
00925
00926 elist = vircam_fits_get_ehu(ps.twilights[0]);
00927 p = cpl_propertylist_duplicate(elist);
00928 vircam_merge_propertylists(p,ps.drs);
00929 if (! (we_get & STATS_TAB))
00930 vircam_dummy_property(p);
00931 vircam_dfs_set_product_exten_header(p,product_frame_ratioimg_stats,
00932 framelist,parlist,
00933 (char *)recipeid,
00934 "PRO-1.15");
00935 status = VIR_OK;
00936 vircam_removewcs(p,&status);
00937
00938
00939
00940 if (cpl_table_save(ps.ratioimstats,plist,p,outdimst,
00941 CPL_IO_DEFAULT) != CPL_ERROR_NONE) {
00942 cpl_msg_error(fctid,"Cannot save product table extension");
00943 cpl_propertylist_delete(p);
00944 return(-1);
00945 }
00946 cpl_propertylist_delete(p);
00947 cpl_frameset_insert(framelist,product_frame_ratioimg_stats);
00948 }
00949 }
00950
00951
00952
00953 plist = vircam_fits_get_ehu(ps.twilights[0]);
00954 vircam_merge_propertylists(plist,ps.drs);
00955
00956
00957
00958 p = cpl_propertylist_duplicate(plist);
00959 if (! (we_get & MEANTWI))
00960 vircam_dummy_property(p);
00961 vircam_dfs_set_product_exten_header(p,product_frame_mean_twi,framelist,
00962 parlist,(char *)recipeid,
00963 "PRO-1.15");
00964
00965
00966
00967 cpl_propertylist_update_float(p,"ESO QC FLATRMS",
00968 vircam_twilight_flat_combine_config.flatrms);
00969 cpl_propertylist_set_comment(p,"ESO QC FLATRMS","RMS of output flat");
00970 if (cpl_image_save(ps.outimage,outfile,CPL_BPP_IEEE_FLOAT,p,
00971 CPL_IO_EXTEND) != CPL_ERROR_NONE) {
00972 cpl_msg_error(fctid,"Cannot save product image extension");
00973 cpl_propertylist_delete(p);
00974 return(-1);
00975 }
00976
00977
00978
00979 pafprop = vircam_paf_req_items(p);
00980 vircam_merge_propertylists(pafprop,ps.phupaf);
00981 vircam_paf_append(pafprop,vircam_fits_get_phu(ps.twilights[0]),
00982 "ESO INS FILT1 NAME");
00983 if (vircam_paf_print((char *)outfilepaf,
00984 "VIRCAM/vircam_twilight_flat_combine",
00985 "QC file",pafprop) != VIR_OK)
00986 cpl_msg_warning(fctid,"Unable to save PAF for mean twilight");
00987 cpl_propertylist_delete(pafprop);
00988 cpl_propertylist_delete(p);
00989
00990
00991
00992 if (we_expect & RATIMG) {
00993 p = cpl_propertylist_duplicate(plist);
00994 if (! (we_get & RATIMG))
00995 vircam_dummy_property(p);
00996 cpl_propertylist_update_float(p,"ESO QC FLATRATIO_MED",
00997 vircam_twilight_flat_combine_config.flatratio_med);
00998 cpl_propertylist_set_comment(p,"ESO QC FLATRATIO_MED",
00999 "Median of ratio map");
01000 cpl_propertylist_update_float(p,"ESO QC FLATRATIO_RMS",
01001 vircam_twilight_flat_combine_config.flatratio_rms);
01002 cpl_propertylist_set_comment(p,"ESO QC FLATRATIO_RMS",
01003 "RMS of ratio map");
01004 vircam_dfs_set_product_exten_header(p,product_frame_ratioimg,
01005 framelist,parlist,
01006 (char *)recipeid,
01007 "PRO-1.15");
01008 if (cpl_image_save(ps.ratioimg,outdiff,CPL_BPP_IEEE_FLOAT,p,
01009 CPL_IO_EXTEND) != CPL_ERROR_NONE) {
01010 cpl_propertylist_delete(p);
01011 cpl_msg_error(fctid,"Cannot save product image extension");
01012 return(-1);
01013 }
01014
01015
01016
01017 pafprop = vircam_paf_req_items(p);
01018 vircam_merge_propertylists(pafprop,ps.phupaf);
01019 vircam_paf_append(pafprop,vircam_fits_get_phu(ps.twilights[0]),
01020 "ESO INS FILT1 NAME");
01021 if (vircam_paf_print((char *)outdiffpaf,
01022 "VIRCAM/vircam_twilight_flat_combine",
01023 "QC file",pafprop) != VIR_OK)
01024 cpl_msg_warning(fctid,"Unable to save PAF for twilight ratio image");
01025 cpl_propertylist_delete(pafprop);
01026 cpl_propertylist_delete(p);
01027 }
01028
01029
01030
01031 if (! isfirst && (we_expect & STATS_TAB)) {
01032 p = cpl_propertylist_duplicate(plist);
01033 if (! (we_get & STATS_TAB))
01034 vircam_dummy_property(p);
01035 vircam_dfs_set_product_exten_header(p,product_frame_ratioimg_stats,
01036 framelist,parlist,
01037 (char *)recipeid,
01038 "PRO-1.15");
01039 status = VIR_OK;
01040 vircam_removewcs(p,&status);
01041 if (cpl_table_save(ps.ratioimstats,NULL,p,outdimst,CPL_IO_EXTEND)
01042 != CPL_ERROR_NONE) {
01043 cpl_msg_error(fctid,"Cannot save product table extension");
01044 cpl_propertylist_delete(p);
01045 return(-1);
01046 }
01047 cpl_propertylist_delete(p);
01048 }
01049
01050
01051
01052 vircam_merge_propertylists(plist,ps.drs2);
01053 p = cpl_propertylist_duplicate(plist);
01054 if (! (we_get & CONFMAP))
01055 vircam_dummy_property(p);
01056
01057
01058
01059 vircam_dfs_set_product_exten_header(p,product_frame_conf,framelist,
01060 parlist,(char *)recipeid,
01061 "PRO-1.15");
01062 if (cpl_image_save(ps.outconf,outconf,CPL_BPP_16_SIGNED,p,
01063 CPL_IO_EXTEND) != CPL_ERROR_NONE) {
01064 cpl_msg_error(fctid,"Cannot save product image extension");
01065 cpl_propertylist_delete(p);
01066 return(-1);
01067 }
01068 cpl_propertylist_delete(p);
01069
01070
01071
01072 return(0);
01073 }
01074
01075
01079
01080
01081 static void vircam_twilight_flat_combine_dummy_products(void) {
01082
01083
01084
01085 if (we_get == we_expect)
01086 return;
01087
01088
01089
01090 if (! (we_get & MEANTWI)) {
01091 ps.outimage = vircam_dummy_image(ps.twilights[0]);
01092 vircam_twilight_flat_combine_config.flatrms = 0.0;
01093 }
01094
01095
01096
01097 if (! (we_get & CONFMAP)) {
01098 ps.outconf = vircam_dummy_image(ps.twilights[0]);
01099 vircam_twilight_flat_combine_config.flatrms = 0.0;
01100 }
01101
01102
01103
01104 if ((we_expect & RATIMG) && ! (we_get & RATIMG)) {
01105 vircam_twilight_flat_combine_config.flatratio_med = 0.0;
01106 vircam_twilight_flat_combine_config.flatratio_rms = 0.0;
01107 ps.ratioimg = vircam_dummy_image(ps.twilights[0]);
01108 }
01109
01110
01111
01112 if ((we_expect & STATS_TAB) && ! (we_get & STATS_TAB))
01113 ps.ratioimstats = vircam_create_diffimg_stats(0);
01114
01115 return;
01116 }
01117
01118
01123
01124
01125 static void vircam_twilight_flat_combine_normal(int jext) {
01126 int nx,ny,ncells,status;
01127 long npi;
01128 unsigned char *bpm;
01129 float *idata,med,sig,gdiff,grms;
01130 const char *fctid="vircam_twilight_flat_combine_normal";
01131
01132
01133
01134 nx = cpl_image_get_size_x(ps.outimage);
01135 ny = cpl_image_get_size_y(ps.outimage);
01136 npi = nx*ny;
01137 vircam_mask_load(ps.master_mask,jext,nx,ny);
01138 bpm = vircam_mask_get_data(ps.master_mask);
01139
01140
01141
01142 status = VIR_OK;
01143 (void)vircam_mkconf(ps.outimage,(char *)"None Available",ps.master_mask,
01144 &(ps.outconf),&(ps.drs2),&status);
01145 if (status == VIR_OK)
01146 we_get |= CONFMAP;
01147 else {
01148 cpl_msg_info(fctid,"Confidence map creation failed extension %d",jext);
01149 status = VIR_OK;
01150 }
01151
01152
01153
01154 idata = cpl_image_get_data(ps.outimage);
01155 vircam_medsig(idata,bpm,npi,&med,&sig);
01156
01157
01158
01159 cpl_propertylist_update_float(ps.drs,"ESO DRS MEDFLAT",med);
01160 cpl_propertylist_set_comment(ps.drs,"ESO DRS MEDFLAT",
01161 "Median value before normalisation");
01162 cpl_image_divide_scalar(ps.outimage,med);
01163 vircam_medmad(idata,bpm,npi,&med,&sig);
01164 sig *= 1.48;
01165 vircam_twilight_flat_combine_config.flatrms = sig;
01166
01167
01168
01169 if (ps.master_twilight_flat != NULL) {
01170 ps.mfimage = vircam_fits_load(ps.master_twilight_flat,CPL_TYPE_FLOAT,jext);
01171 if (ps.mfimage == NULL) {
01172 cpl_msg_info(fctid,"Master twilight extension %d won't load",jext);
01173 } else if (vircam_is_dummy(vircam_fits_get_ehu(ps.mfimage))) {
01174 cpl_msg_info(fctid,"Master twilight extension %d is a dummy",jext);
01175 freefits(ps.mfimage);
01176 }
01177 } else
01178 ps.mfimage = NULL;
01179
01180
01181
01182
01183
01184
01185
01186
01187 vircam_twilight_flat_combine_config.flatratio_med = 0.0;
01188 vircam_twilight_flat_combine_config.flatratio_rms = 0.0;
01189 ncells = vircam_twilight_flat_combine_config.ncells;
01190 vircam_difference_image(vircam_fits_get_image(ps.mfimage),ps.outimage,bpm,
01191 vircam_tfits_get_table(ps.ctable),ncells,2,
01192 &gdiff,&grms,&(ps.ratioimg),
01193 &(ps.ratioimstats));
01194 vircam_mask_clear(ps.master_mask);
01195 vircam_twilight_flat_combine_config.flatratio_med = gdiff;
01196 vircam_twilight_flat_combine_config.flatratio_rms = grms;
01197 if (ps.ratioimg != NULL)
01198 we_get |= RATIMG;
01199 if (ps.ratioimstats != NULL)
01200 we_get |= STATS_TAB;
01201 return;
01202 }
01203
01204
01212
01213
01214
01215 static int vircam_twilight_flat_combine_lastbit(int jext,
01216 cpl_frameset *framelist,
01217 cpl_parameterlist *parlist) {
01218 int retval;
01219 const char *fctid="vircam_twilight_flat_combine_lastbit";
01220
01221
01222
01223 vircam_twilight_flat_combine_dummy_products();
01224
01225
01226
01227 cpl_msg_info(fctid,"Saving products for extension %d",jext);
01228 retval = vircam_twilight_flat_combine_save(framelist,parlist);
01229 if (retval != 0) {
01230 vircam_twilight_flat_combine_tidy(2);
01231 return(-1);
01232 }
01233
01234
01235
01236 vircam_twilight_flat_combine_tidy(1);
01237 return(0);
01238 }
01239
01240
01244
01245
01246 static void vircam_twilight_flat_combine_init(void) {
01247 ps.labels = NULL;
01248 ps.twilightlist = NULL;
01249 ps.twilights = NULL;
01250 ps.good = NULL;
01251 ps.master_dark = NULL;
01252 ps.master_twilight_flat = NULL;
01253 ps.master_mask = NULL;
01254 ps.chantab = NULL;
01255 ps.ctable = NULL;
01256 ps.outimage = NULL;
01257 ps.outconf = NULL;
01258 ps.drs = NULL;
01259 ps.drs2 = NULL;
01260 ps.rejmask = NULL;
01261 ps.rejplus = NULL;
01262 ps.mfimage = NULL;
01263 ps.ratioimg = NULL;
01264 ps.ratioimstats = NULL;
01265 ps.phupaf = NULL;
01266 }
01267
01268
01272
01273
01274 static void vircam_twilight_flat_combine_tidy(int level) {
01275 freeimage(ps.outimage);
01276 freeimage(ps.outconf);
01277 freefitslist(ps.twilights,ps.ntwilights);
01278 freepropertylist(ps.drs);
01279 freepropertylist(ps.drs2);
01280 freespace(ps.rejmask);
01281 freespace(ps.rejplus);
01282 freefits(ps.mfimage);
01283 freeimage(ps.ratioimg);
01284 freetable(ps.ratioimstats);
01285 freetfits(ps.ctable);
01286 freefits(ps.mdark);
01287 if (level == 1)
01288 return;
01289
01290 freespace(ps.good);
01291 freespace(ps.labels);
01292 freeframeset(ps.twilightlist);
01293 freeframe(ps.master_dark);
01294 freeframe(ps.master_twilight_flat);
01295 freemask(ps.master_mask);
01296 freeframe(ps.chantab);
01297 freepropertylist(ps.phupaf);
01298 }
01299
01302
01303
01304
01305
01306
01307
01308
01309
01310
01311
01312
01313
01314
01315
01316
01317
01318
01319
01320
01321
01322
01323
01324
01325
01326
01327
01328
01329
01330
01331
01332
01333
01334
01335
01336
01337
01338
01339
01340
01341
01342
01343
01344
01345
01346
01347
01348
01349
01350
01351
01352
01353
01354
01355
01356
01357
01358
01359
01360
01361
01362
01363
01364
01365
01366
01367
01368
01369
01370
01371
01372
01373
01374
01375
01376
01377
01378
01379
01380
01381
01382
01383
01384
01385
01386
01387
01388
01389
01390
01391
01392
01393
01394
01395
01396
01397
01398
01399
01400
01401
01402
01403
01404
01405
01406
01407
01408
01409
01410
01411
01412
01413
01414
01415
01416
01417
01418
01419
01420
01421
01422
01423
01424
01425
01426
01427
01428
01429
01430
01431
01432