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 #ifdef HAVE_CONFIG_H
00029 #include <config.h>
00030 #endif
00031
00032
00033
00034
00035 #include <cpl.h>
00036 #include "xsh_detmon.h"
00037 #include "xsh_detmon_lg.h"
00038
00039
00040
00041 #include "xsh_dfs.h"
00042 #include "xsh_drl.h"
00043 #include "xsh_msg.h"
00044 #include "xsh_error.h"
00045 #include "xsh_utils.h"
00046 #include <xsh_data_instrument.h>
00047 #include "xsh_parameters.h"
00048 #include "xsh_badpixelmap.h"
00049
00050
00051
00052 #define XSH_LINGAIN_ON_RAW(it) ((it) == 0 ? XSH_LINEARITY_UVB_ON : \
00053 (it) == 1 ? XSH_LINEARITY_VIS_ON : \
00054 (it) == 2 ? XSH_LINEARITY_NIR_ON : \
00055 "???")
00056
00057 #define XSH_LINGAIN_OFF_RAW(it) ((it) == 0 ? XSH_LINEARITY_UVB_OFF : \
00058 (it) == 1 ? XSH_LINEARITY_VIS_OFF : \
00059 (it) == 2 ? XSH_LINEARITY_NIR_OFF : \
00060 "???")
00061
00062 #define XSH_LINGAIN_DET_LIN_INFO(it) ((it) == 0 ? XSH_DET_LIN_INFO_UVB : \
00063 (it) == 1 ? XSH_DET_LIN_INFO_VIS : \
00064 (it) == 2 ? XSH_DET_LIN_INFO_NIR : \
00065 "???")
00066
00067 #define XSH_LINGAIN_GAIN_INFO(it) ((it) == 0 ? XSH_GAIN_INFO_UVB : \
00068 (it) == 1 ? XSH_GAIN_INFO_VIS : \
00069 (it) == 2 ? XSH_GAIN_INFO_NIR : \
00070 "???")
00071
00072 #define XSH_LINGAIN_COEFFS_CUBE(it) ((it) == 0 ? XSH_COEFFS_CUBE_UVB : \
00073 (it) == 1 ? XSH_COEFFS_CUBE_VIS : \
00074 (it) == 2 ? XSH_COEFFS_CUBE_NIR : \
00075 "???")
00076
00077 #define XSH_LINGAIN_BP_MAP_NL(it) ((it) == 0 ? XSH_BP_MAP_NL_UVB : \
00078 (it) == 1 ? XSH_BP_MAP_NL_VIS : \
00079 (it) == 2 ? XSH_BP_MAP_NL_NIR : \
00080 "???")
00081
00082 #define XSH_LINGAIN_RAW_BP_MAP_NL(it) ((it) == 0 ? XSH_RAW_BP_MAP_NL_UVB : \
00083 (it) == 1 ? XSH_RAW_BP_MAP_NL_VIS : \
00084 (it) == 2 ? XSH_RAW_BP_MAP_NL_NIR : \
00085 "???")
00086
00087 #define XSH_LINGAIN_AUTOCORR(it) ((it) == 0 ? XSH_AUTOCORR_UVB : \
00088 (it) == 1 ? XSH_AUTOCORR_VIS : \
00089 (it) == 2 ? XSH_AUTOCORR_NIR : \
00090 "???")
00091
00092 #define XSH_LINGAIN_DIFF_FLAT(it) ((it) == 0 ? XSH_DIFF_FLAT_UVB : \
00093 (it) == 1 ? XSH_DIFF_FLAT_VIS : \
00094 (it) == 2 ? XSH_DIFF_FLAT_NIR : \
00095 "???")
00096
00097 #define RECIPE_ID "xsh_lingain"
00098 #define RECIPE_AUTHOR "Lander de Bilbao,A.Modigliani"
00099 #define RECIPE_CONTACT "amodigli@eso.org"
00100
00101
00102 #define INSTREGEXP "ESO INS SETUP ID"
00103
00104 #define PAFREGEXP "^(" REGEXP "|" INSTREGEXP ")$"
00105 #define NIR TRUE
00106 #define OPT FALSE
00107 #define XSH_PIX2PIX CPL_TRUE
00108 #define XSH_EXTS_RED -1
00109 #define XSH_BPMBIN 1
00110 #define XSH_TOL 0.1
00111
00112
00113
00114 static int xsh_lingain_create(cpl_plugin *);
00115 static int xsh_lingain_exec(cpl_plugin *);
00116 static int xsh_lingain_destroy(cpl_plugin *);
00117 static int
00118 xsh_lingain(cpl_frameset * frameset,
00119 cpl_parameterlist * parlist);
00120
00121 cpl_error_code
00122 xsh_lingain_fill_parlist_default_opt(cpl_parameterlist * parlist);
00123 cpl_error_code
00124 xsh_lingain_fill_parlist_default_nir(cpl_parameterlist * parlist);
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137 static char xsh_lingain_description_short[] = "Computes detector's gain/linearity-map";
00138
00139 static char xsh_lingain_description[] =
00140 "This recipe computes detector linearity coefficients and gain\n\
00141 Input Frames : \n\
00142 arm = UVB-VIS: \n\
00143 - A set of n RAW linearity frames (Format=RAW, n >=8, Tag = LINEARITY_arm_ON)\n\
00144 - A set of n RAW bias frames (Format=RAW, n >=1, Tag = LINEARITY_arm_OFF)\n\
00145 arm = NIR:\n\
00146 - A set of n RAW linearity on frames (Format=RAW, n >=8, Tag = LINEARITY_arm_ON)\n\
00147 - A set of n RAW linearity off frames (Format=RAW, n >=8, Tag = LINEARITY_arm_OFF)\n\
00148 - [OPTIONAL] a static bad pixel map (PRO.CATG=BP_MAP_RP_arm) \n\
00149 Note: on and off frames are taken in pairs sequence: OFF-ON-ON-OFF, \n\
00150 at least 16 frames.\n\
00151 Products : \n\
00152 - A linearity map table, PRO.CATG = BP_MAP_LIN_NIR\n\
00153 - A gain table, PRO.CATG = GAIN_INFO\n\
00154 - A cube cointaining the linearity coefficients, PRO.CATG = COEFFS_CUBE_arm\n\
00155 - A linearity map image (RAW format), PRO.CATG = RAW_BP_MAP_NL_arm\n\
00156 - A linearity map image (PRE format), PRO.CATG = BP_MAP_NL_arm\n\
00157 - A master BP map image (PRE format), PRO.CATG = MASTER_BP_MAP_NLIN_arm equal \n\
00158 to the BP_MAP_NL_arm or combined via bitwise OR with BP_MAP_RP_arm if this is provided\n";
00159
00160
00161
00162
00163
00172
00173
00174 int cpl_plugin_get_info(cpl_pluginlist * list)
00175 {
00176 cpl_recipe *recipe = NULL;
00177 cpl_plugin *plugin = NULL;
00178
00179 recipe = cpl_calloc(1, sizeof(*recipe));
00180 if (recipe == NULL) {
00181 return -1;
00182 }
00183
00184 plugin = &recipe->interface;
00185
00186 cpl_plugin_init(plugin, CPL_PLUGIN_API,
00187 XSH_BINARY_VERSION,
00188 CPL_PLUGIN_TYPE_RECIPE,
00189 RECIPE_ID,
00190 xsh_lingain_description_short,
00191 xsh_lingain_description,
00192 RECIPE_AUTHOR,
00193 RECIPE_CONTACT,
00194 xsh_get_license(),
00195 xsh_lingain_create,
00196 xsh_lingain_exec,
00197 xsh_lingain_destroy);
00198
00199 cpl_pluginlist_append(list, plugin);
00200
00201 return (cpl_error_get_code() != CPL_ERROR_NONE);
00202 }
00203
00204
00214
00215
00216 static int xsh_lingain_create(cpl_plugin * plugin)
00217 {
00218 cpl_recipe *recipe = NULL;
00219
00220 xsh_init();
00221
00222
00223 assure(plugin != NULL, CPL_ERROR_NULL_INPUT, "Null plugin");
00224
00225
00226 assure(cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE,
00227 CPL_ERROR_TYPE_MISMATCH, "Plugin is not a recipe");
00228
00229 recipe = (cpl_recipe *) plugin;
00230
00231
00232 recipe->parameters = cpl_parameterlist_new();
00233 assure(recipe->parameters != NULL,
00234 CPL_ERROR_ILLEGAL_OUTPUT, "Memory allocation failed!");
00235
00236
00237 check( xsh_parameters_generic( RECIPE_ID, recipe->parameters ) ) ;
00238
00239 check(xsh_lingain_fill_parlist_default_opt(recipe->parameters));
00240
00241 cleanup:
00242 if (cpl_error_get_code() != CPL_ERROR_NONE) {
00243 xsh_error_dump(CPL_MSG_ERROR);
00244 return 1;
00245 } else {
00246 return 0;
00247 }
00248 }
00249
00250
00251
00257
00258
00259 static int xsh_lingain_exec(cpl_plugin * plugin)
00260 {
00261 cpl_recipe *recipe = NULL;
00262
00263
00264 assure(plugin != NULL, CPL_ERROR_NULL_INPUT, "Null plugin");
00265
00266
00267 assure(cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE,
00268 CPL_ERROR_TYPE_MISMATCH, "Plugin is not a recipe");
00269
00270 recipe = (cpl_recipe *) plugin;
00271
00272
00273 xsh_lingain(recipe->frames,recipe->parameters);
00274
00275 cleanup:
00276 if (cpl_error_get_code() != CPL_ERROR_NONE) {
00277 xsh_error_dump(CPL_MSG_ERROR);
00278 cpl_error_reset();
00279 return 1;
00280 } else {
00281 return 0;
00282 }
00283 }
00284
00285
00291
00292 static int xsh_lingain_destroy(cpl_plugin * plugin)
00293 {
00294 cpl_recipe *recipe = NULL;
00295
00296 xsh_error_reset();
00297
00298 assure(plugin != NULL, CPL_ERROR_NULL_INPUT, "Null plugin");
00299
00300
00301 assure(cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE,
00302 CPL_ERROR_TYPE_MISMATCH, "Plugin is not a recipe");
00303
00304 recipe = (cpl_recipe *) plugin;
00305
00306 xsh_free_parameterlist(&recipe->parameters);
00307
00308 cleanup:
00309 if (cpl_error_get_code() != CPL_ERROR_NONE) {
00310 return 1;
00311 } else {
00312 return 0;
00313 }
00314 }
00315
00316 static cpl_error_code
00317 xsh_params_set_defaults(cpl_parameterlist* pars,
00318 xsh_instrument* inst)
00319 {
00320 cpl_parameter* p=NULL;
00321
00322 if (xsh_instrument_get_arm(inst) == XSH_ARM_UVB){
00323 check(p=xsh_parameters_find(pars,RECIPE_ID,"kappa"));
00324 if(xsh_parameter_get_default_flag(p) == 0) {
00325
00326 cpl_parameter_set_double(p,5.);
00327 }
00328 }
00329
00330
00331 cleanup:
00332
00333 return cpl_error_get_code();
00334
00335 }
00336
00337
00338
00339
00340
00341
00342
00343
00344
00345
00346 static int
00347 xsh_lingain(cpl_frameset * frameset,
00348 cpl_parameterlist * parlist)
00349 {
00350
00351 const char* recipe_tags[6] = {XSH_LINEARITY_UVB_ON,XSH_LINEARITY_UVB_OFF,
00352 XSH_LINEARITY_VIS_ON,XSH_LINEARITY_VIS_OFF,
00353 XSH_LINEARITY_NIR_ON,XSH_LINEARITY_NIR_OFF};
00354 int recipe_tags_size = 6;
00355
00356 cpl_error_code error=0;
00357 int processed=0;
00358 cpl_parameterlist* plist=NULL;
00359 xsh_instrument* instrument=NULL;
00360 cpl_frameset* raws=NULL;
00361 cpl_frameset* calib=NULL;
00362 cpl_frameset* pros=NULL;
00363
00364 cpl_propertylist * lintbl = NULL;
00365 cpl_propertylist * gaintbl = NULL;
00366 cpl_propertylist * coeffscube = NULL;
00367 cpl_propertylist * bpm = NULL;
00368 cpl_propertylist * corr = NULL;
00369 cpl_propertylist * diff_flat =NULL;
00370 cpl_frame* bpmap_nl=NULL;
00371 cpl_image* ima=NULL;
00372 char name[256];
00373 cpl_propertylist* head=NULL;
00374 cpl_frame *bpmap = NULL;
00375 cpl_frame* coadd_bp_map=NULL;
00376 cpl_image* crox=NULL;
00377
00378
00379 char* bp_nl_tag=NULL;
00380 char* bp_nl_name=NULL;
00381 xsh_pre* pre_bp_nl = NULL;
00382 cpl_frame* bp_nl_frame = NULL;
00383
00384
00385
00386
00387
00388 check( xsh_begin( frameset, parlist, &instrument, &raws, &calib,
00389 recipe_tags, recipe_tags_size,
00390 RECIPE_ID, XSH_BINARY_VERSION,
00391 xsh_lingain_description_short ) ) ;
00392 if((bpmap=xsh_find_frame_with_tag(calib,XSH_BP_MAP_RP,instrument))==NULL) {
00393 xsh_msg("%s not provided",XSH_BP_MAP_RP);
00394 }
00395 check(plist=xsh_parameterlist_duplicate(parlist));
00396
00397 if ( xsh_instrument_get_arm(instrument) == XSH_ARM_NIR){
00398
00399
00400
00401 xsh_lingain_fill_parlist_default_nir(plist);
00402
00403 processed++;
00404
00405 lintbl=xsh_detmon_fill_prolist("DET_LIN_INFO_NIR","TYPE","TECH",CPL_FALSE);
00406
00407 gaintbl=xsh_detmon_fill_prolist("GAIN_INFO_NIR","TYPE","TECH",CPL_FALSE);
00408
00409 coeffscube=xsh_detmon_fill_prolist("COEFFS_CUBE_NIR","TYPE","TECH",CPL_FALSE);
00410
00411 bpm=xsh_detmon_fill_prolist("RAW_BP_MAP_NL_NIR","TYPE","TECH",CPL_FALSE);
00412
00413 corr=xsh_detmon_fill_prolist("AUTOCORR_NIR","TYPE","TECH",CPL_FALSE);
00414
00415 diff_flat=xsh_detmon_fill_prolist("DIFF_FLAT_NIR","TYPE","TECH",CPL_FALSE);
00416
00417 check(error = xsh_detmon_lg(raws,
00418 plist,
00419 XSH_LINEARITY_NIR_ON,
00420 XSH_LINEARITY_NIR_OFF,
00421 RECIPE_ID,
00422 "xsh",
00423 PAFREGEXP,
00424 lintbl,
00425 gaintbl,
00426 coeffscube,
00427 bpm,
00428 corr,
00429 diff_flat,
00430 PACKAGE "/" PACKAGE_VERSION,
00431 NULL, NULL, NIR));
00432
00433 } else if ( xsh_instrument_get_arm(instrument) == XSH_ARM_UVB){
00434
00435
00436
00437 xsh_lingain_fill_parlist_default_opt(plist);
00438 check(xsh_params_set_defaults(parlist,instrument));
00439
00440 processed++;
00441
00442 lintbl=xsh_detmon_fill_prolist("DET_LIN_INFO_UVB","TYPE","TECH",CPL_FALSE);
00443
00444 gaintbl=xsh_detmon_fill_prolist("GAIN_INFO_UVB","TYPE","TECH",CPL_FALSE);
00445
00446 coeffscube=xsh_detmon_fill_prolist("COEFFS_CUBE_UVB","TYPE","TECH",CPL_FALSE);
00447
00448 bpm=xsh_detmon_fill_prolist("RAW_BP_MAP_NL_UVB","TYPE","TECH",CPL_FALSE);
00449
00450 corr=xsh_detmon_fill_prolist("AUTOCORR_UVB","TYPE","TECH",CPL_FALSE);
00451
00452 diff_flat=xsh_detmon_fill_prolist("DIFF_FLAT_UVB","TYPE","TECH",CPL_FALSE);
00453 check(error = xsh_detmon_lg(raws,
00454 plist,
00455 XSH_LINEARITY_UVB_ON,
00456 XSH_LINEARITY_UVB_OFF,
00457 RECIPE_ID,
00458 "xsh",
00459 PAFREGEXP,
00460 lintbl,
00461 gaintbl,
00462 coeffscube,
00463 bpm,
00464 corr,
00465 diff_flat,
00466 PACKAGE "/" PACKAGE_VERSION,
00467 NULL, NULL, OPT));
00468
00469
00470
00471 } else if ( xsh_instrument_get_arm(instrument) == XSH_ARM_VIS) {
00472
00473
00474
00475 xsh_lingain_fill_parlist_default_opt(plist);
00476
00477
00478 processed++;
00479
00480 lintbl=xsh_detmon_fill_prolist("DET_LIN_INFO_VIS","TYPE","TECH",CPL_FALSE);
00481
00482 gaintbl=xsh_detmon_fill_prolist("GAIN_INFO_VIS","TYPE","TECH",CPL_FALSE);
00483
00484 coeffscube=xsh_detmon_fill_prolist("COEFFS_CUBE_VIS","TYPE","TECH",CPL_FALSE);
00485
00486 bpm=xsh_detmon_fill_prolist("RAW_BP_MAP_NL_VIS","TYPE","TECH",CPL_FALSE);
00487
00488 corr=xsh_detmon_fill_prolist("AUTOCORR_VIS","TYPE","TECH",CPL_FALSE);
00489
00490 diff_flat=xsh_detmon_fill_prolist("DIFF_FLAT_VIS","TYPE","TECH",CPL_FALSE);
00491 check(error = xsh_detmon_lg(raws,
00492 plist,
00493 XSH_LINEARITY_VIS_ON,
00494 XSH_LINEARITY_VIS_OFF,
00495 RECIPE_ID,
00496 "xsh",
00497 PAFREGEXP,
00498 lintbl,
00499 gaintbl,
00500 coeffscube,
00501 bpm,
00502 corr,
00503 diff_flat,
00504 PACKAGE "/" PACKAGE_VERSION,
00505 NULL, NULL, OPT));
00506
00507
00508
00509 }
00510
00511 pros=cpl_frameset_new();
00512 check(xsh_dfs_extract_pro_frames(raws,pros));
00513 check(xsh_frameset_merge(frameset,pros));
00514 xsh_free_frameset(&pros);
00515 check(bpmap_nl=xsh_find_frame_with_tag(frameset,XSH_RAW_BP_MAP_NL,instrument));
00516
00517
00518 check( pre_bp_nl = xsh_pre_create(bpmap_nl , NULL, NULL, instrument,0));
00519 bp_nl_tag=cpl_sprintf("%s_%s",XSH_BP_MAP_NL,
00520 xsh_instrument_arm_tostring( instrument ));
00521 bp_nl_name=cpl_sprintf("%s.fits",bp_nl_tag);
00522 check( bp_nl_frame = xsh_pre_save( pre_bp_nl, bp_nl_name,bp_nl_tag,1 ));
00523 cpl_frameset_insert(frameset, bp_nl_frame);
00524
00525
00526 check(sprintf(name,cpl_frame_get_filename(bp_nl_frame)));
00527 check(ima=cpl_image_load(name,CPL_TYPE_FLOAT,0,0));
00528 check(head=cpl_propertylist_load(name,0));
00529 cpl_propertylist_erase_regexp(head, "NPIXSAT",0);
00530 cpl_propertylist_erase_regexp(head, "FPIXSAT",0);
00531 check(xsh_bpmap_bitwise_to_flag(ima,QFLAG_NON_LINEAR_PIXEL));
00532
00533 if ( xsh_instrument_get_arm(instrument) == XSH_ARM_NIR) {
00534 check(crox=xsh_image_flag_bptype_with_crox(ima));
00535 check(cpl_image_save(crox,name,CPL_BPP_IEEE_FLOAT,head,CPL_IO_DEFAULT));
00536 xsh_free_image(&crox);
00537 } else {
00538 check(cpl_image_save(ima,name,CPL_BPP_IEEE_FLOAT,head,CPL_IO_DEFAULT));
00539 }
00540 xsh_free_image(&ima);
00541 xsh_free_propertylist(&head);
00542
00543
00544
00545
00546
00547
00548
00549
00550
00551
00552
00553
00554
00555
00556
00557
00558
00559
00560
00561
00562
00563
00564
00565
00566
00567
00568
00569
00570
00571
00572
00573 if(processed>0) {
00574
00575 cpl_ensure_code(!error, error);
00576 } else {
00577 xsh_msg("No data has been processed.");
00578 xsh_msg("Pls check if input frame set contains valid tags");
00579 }
00580
00581
00582 cleanup:
00583
00584 xsh_free_frame(&coadd_bp_map);
00585 xsh_free_frameset(&pros);
00586 xsh_free_image(&ima);
00587 xsh_free_propertylist(&head);
00588 xsh_free_propertylist(&lintbl);
00589 xsh_free_propertylist(&gaintbl);
00590 xsh_free_propertylist(&coeffscube);
00591 xsh_free_propertylist(&bpm);
00592 xsh_free_propertylist(&corr);
00593 xsh_free_propertylist(&diff_flat);
00594 xsh_pre_free(&pre_bp_nl);
00595 return CPL_ERROR_NONE;
00596 }
00597
00598
00599
00600
00601
00602
00603
00604
00605
00606
00607
00608
00609
00610
00611
00612
00613
00614
00615
00616
00617
00618
00619
00620
00621
00622
00623
00624
00625
00626
00627
00628
00629
00630
00631
00632
00633
00634
00635
00636
00637
00638
00639
00640
00641
00642
00643
00644
00645
00646
00647
00648
00649
00650
00651
00652
00653
00654
00655
00656
00657
00658 cpl_error_code
00659 xsh_lingain_fill_parlist_default_opt(cpl_parameterlist * parlist)
00660 {
00661
00662 cpl_error_code error =
00663 xsh_detmon_lg_fill_parlist_opt_default(parlist, RECIPE_ID, PACKAGE);
00664 cpl_parameter * p;
00665
00666 cpl_ensure_code(!error, error);
00667
00668
00669
00670 p = cpl_parameterlist_find(parlist, PACKAGE "." RECIPE_ID ".pix2pix");
00671
00672 cpl_ensure_code(p != NULL, CPL_ERROR_DATA_NOT_FOUND);
00673
00674 error = cpl_parameter_set_default_bool(p, XSH_PIX2PIX);
00675
00676
00677 p = cpl_parameterlist_find(parlist, PACKAGE "." RECIPE_ID ".tolerance");
00678
00679 cpl_ensure_code(p != NULL, CPL_ERROR_DATA_NOT_FOUND);
00680
00681 error = cpl_parameter_set_default_double(p, XSH_TOL);
00682
00683
00684 cpl_ensure_code(!error, error);
00685
00686 return CPL_ERROR_NONE;
00687 }
00688
00689
00690 cpl_error_code
00691 xsh_lingain_fill_parlist_default_nir(cpl_parameterlist * parlist)
00692 {
00693
00694
00695
00696 cpl_error_code error =
00697 xsh_detmon_lg_fill_parlist_nir_default(parlist,RECIPE_ID,
00698 PACKAGE_TARNAME);
00699 cpl_parameter * p;
00700
00701 cpl_ensure_code(!error, error);
00702
00703
00704 p = cpl_parameterlist_find(parlist, PACKAGE_TARNAME "." RECIPE_ID ".bpmbin");
00705
00706 cpl_ensure_code(p != NULL, CPL_ERROR_DATA_NOT_FOUND);
00707
00708 error = cpl_parameter_set_default_bool(p, XSH_BPMBIN);
00709
00710
00711 cpl_ensure_code(!error, error);
00712
00713
00714 return CPL_ERROR_NONE;
00715
00716 }
00717