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 #ifdef HAVE_CONFIG_H
00030 #include <config.h>
00031 #endif
00032
00039
00047
00050
00051
00052
00053
00054
00055
00056
00057
00058 #include <xsh_error.h>
00059
00060 #include <xsh_utils.h>
00061 #include <xsh_msg.h>
00062
00063 #include <xsh_dfs.h>
00064
00065 #include <xsh_drl.h>
00066
00067 #include <xsh_compute_linearity.h>
00068 #include <xsh_detmon_lg.h>
00069 #include <xsh_pfits.h>
00070 #include <xsh_paf_save.h>
00071
00072
00073 #include <cpl.h>
00074
00075
00076
00077
00078
00079 #define RECIPE_ID "xsh_linear"
00080 #define RECIPE_AUTHOR "L.Guglielmi,R.Haigron,P.Goldoni,F.Royer"
00081 #define RECIPE_CONTACT "laurent.guglielmi@apc.univ-paris7.fr"
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092 static int create(cpl_plugin *);
00093 static int exec(cpl_plugin *);
00094 static int destroy(cpl_plugin *);
00095
00096
00097 static void xsh_linear(cpl_parameterlist *, cpl_frameset *);
00098 static void xsh_gain(cpl_parameterlist* parameters,
00099 cpl_frameset* frameset,
00100 xsh_instrument* instr);
00101 static cpl_vector*
00102 xsh_get_exptimes(cpl_frameset* set);
00103
00104
00105
00106
00107 static char xsh_linear_description_short[] =
00108 "Create the linearity bad pixel mask";
00109
00110 static char xsh_linear_description[] =
00111 "This recipe creates a bad pixel mask from several (at least 3x3)\n\
00112 LINEARITY frames in increasing exposure times\n\
00113 Input Frames:\n\
00114 Raw Frames (Tag = LINEARITY_arm)\n\
00115 Prepare PRE structures\n\
00116 Group frames by exposure time\n\
00117 Remove cosmic rays in each group and create\n\
00118 the median frame\n\
00119 Finally compute the linearity bad pixel mask. Linearity is obtained by\n\
00120 a polynomial fit (degree 3)\n\
00121 Product:\n\
00122 Bad Pixel Map, PRO.CATG = BADPIXEL_MAP_arm\n" ;
00123
00124
00125
00126
00127
00136
00137
00138 int cpl_plugin_get_info(cpl_pluginlist *list) {
00139 cpl_recipe *recipe = NULL;
00140 cpl_plugin *plugin = NULL;
00141
00142 recipe = cpl_calloc(1, sizeof(*recipe));
00143 if ( recipe == NULL ){
00144 return -1;
00145 }
00146
00147 plugin = &recipe->interface ;
00148
00149 cpl_plugin_init(plugin,
00150 CPL_PLUGIN_API,
00151 XSH_BINARY_VERSION,
00152 CPL_PLUGIN_TYPE_RECIPE,
00153 RECIPE_ID,
00154 xsh_linear_description_short,
00155 xsh_linear_description,
00156 RECIPE_AUTHOR,
00157 RECIPE_CONTACT,
00158 xsh_get_license(),
00159 create,
00160 exec,
00161 destroy);
00162
00163 cpl_pluginlist_append(list, plugin);
00164
00165 return (cpl_error_get_code() != CPL_ERROR_NONE);
00166 }
00167
00168
00178
00179
00180 static int create(cpl_plugin *plugin){
00181 cpl_recipe *recipe = NULL;
00182 cpl_parameter* p =NULL;
00183
00184
00185 xsh_init();
00186
00187
00188 assure( plugin != NULL, CPL_ERROR_NULL_INPUT, "Null plugin");
00189
00190
00191 assure( cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE,
00192 CPL_ERROR_TYPE_MISMATCH,
00193 "Plugin is not a recipe");
00194
00195 recipe = (cpl_recipe *)plugin;
00196
00197
00198 recipe->parameters = cpl_parameterlist_new();
00199 assure( recipe->parameters != NULL,
00200 CPL_ERROR_ILLEGAL_OUTPUT,
00201 "Memory allocation failed!");
00202
00203
00204
00205 check( xsh_parameters_generic( RECIPE_ID, recipe->parameters ) ) ;
00206
00207
00208
00209 p = cpl_parameter_new_value("xsh.xsh_linear.crh_clip_kappa",
00210 CPL_TYPE_DOUBLE,
00211 "multiple of sigma in sigma clipping",
00212 "xsh.xsh_linear",5.0);
00213
00214 cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI,"crh_clip_kappa");
00215 cpl_parameterlist_append(recipe->parameters,p);
00216
00217
00218 p = cpl_parameter_new_value("xsh.xsh_linear.crh_clip_niter",
00219 CPL_TYPE_INT,"number of iterations in sigma clipping","xsh.xsh_linear",2);
00220 cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI,"crh_clip_niter");
00221 cpl_parameterlist_append(recipe->parameters,p);
00222
00223
00224 p = cpl_parameter_new_value("xsh.xsh_linear.crh_clip_frac",
00225 CPL_TYPE_DOUBLE,"minimal fractions of points accepted \
00226 / total in sigma clipping",
00227 "xsh.xsh_linear",0.7);
00228 cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI,"crh_clip_frac");
00229 cpl_parameterlist_append(recipe->parameters,p);
00230
00231
00232 p = cpl_parameter_new_value("xsh.xsh_linear.lin_clip_kappa",
00233 CPL_TYPE_DOUBLE,
00234 "multiple of sigma in sigma clipping",
00235 "xsh.xsh_linear",10.0);
00236 cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI,"lin_clip_kappa");
00237 cpl_parameterlist_append(recipe->parameters,p);
00238
00239 p = cpl_parameter_new_value("xsh.xsh_linear.lin_clip_niter",
00240 CPL_TYPE_INT,"number of iterations in sigma clipping","xsh.xsh_linear",2);
00241 cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI,"lin_clip_niter");
00242 cpl_parameterlist_append(recipe->parameters,p);
00243
00244 p = cpl_parameter_new_value("xsh.xsh_linear.lin_clip_frac",
00245 CPL_TYPE_DOUBLE,"Minimum fraction of bad pixels allowed",
00246 "xsh.xsh_linear",0.7);
00247 cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI,"lin_clip_frac");
00248 cpl_parameterlist_append(recipe->parameters,p);
00249
00250 p = cpl_parameter_new_value("xsh.xsh_linear.lin_clip_diff",
00251 CPL_TYPE_DOUBLE,"Minimum relative change in sigma for sigma clipping",
00252 "xsh.xsh_linear",0.7);
00253 cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI,"lin_clip_diff");
00254 cpl_parameterlist_append(recipe->parameters,p);
00255
00256
00257 p = cpl_parameter_new_value("xsh.xsh_linear.exp_toler",
00258 CPL_TYPE_DOUBLE,"Tolerance for equal exposures times in seconds",
00259 "xsh.xsh_linear", 0.001);
00260 cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI,"exp_toler");
00261 cpl_parameterlist_append(recipe->parameters,p);
00262
00263
00264
00265
00266 p = cpl_parameter_new_value("xsh.xsh_linear.compute_gain",
00267 CPL_TYPE_BOOL,
00268 "Compute gain",
00269 "xsh.xsh_linear",CPL_FALSE);
00270
00271
00272 cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI,"compute_gain");
00273 cpl_parameterlist_append(recipe->parameters,p);
00274
00275
00276 p = cpl_parameter_new_value("xsh.xsh_linear.llx",
00277 CPL_TYPE_INT,"Detector lower left x pix",
00278 "xsh.xsh_linear", 1);
00279 cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI,"llx");
00280 cpl_parameterlist_append(recipe->parameters,p);
00281
00282
00283 p = cpl_parameter_new_value("xsh.xsh_linear.lly",
00284 CPL_TYPE_INT,"Detector lower left y pix",
00285 "xsh.xsh_linear", 1);
00286 cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI,"lly");
00287 cpl_parameterlist_append(recipe->parameters,p);
00288
00289
00290
00291 p = cpl_parameter_new_value("xsh.xsh_linear.urx",
00292 CPL_TYPE_INT,"Detector upper right x pix",
00293 "xsh.xsh_linear", 2148);
00294 cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI,"urx");
00295 cpl_parameterlist_append(recipe->parameters,p);
00296
00297
00298
00299 p = cpl_parameter_new_value("xsh.xsh_linear.ury",
00300 CPL_TYPE_INT,"Detector upper right y pix",
00301 "xsh.xsh_linear", 3000);
00302 cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI,"ury");
00303 cpl_parameterlist_append(recipe->parameters,p);
00304
00305
00306
00307 p = cpl_parameter_new_value("xsh.xsh_linear.kappa",
00308 CPL_TYPE_DOUBLE,"Value in kappa-sigma clip",
00309 "xsh.xsh_linear", 3.);
00310 cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI,"kappa");
00311 cpl_parameterlist_append(recipe->parameters,p);
00312
00313
00314
00315
00316 p = cpl_parameter_new_value("xsh.xsh_linear.nclip",
00317 CPL_TYPE_INT,"Number of kappa-sigma clip iterations",
00318 "xsh.xsh_linear", 5);
00319 cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI,"nclip");
00320 cpl_parameterlist_append(recipe->parameters,p);
00321
00322
00323
00324 p = cpl_parameter_new_value("xsh.xsh_linear.shiftx",
00325 CPL_TYPE_INT,"Pixel X shift applied in autocorrelation. "
00326 "Must be > 0.",
00327 "xsh.xsh_linear", 26);
00328 cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI,"shiftx");
00329 cpl_parameterlist_append(recipe->parameters,p);
00330
00331
00332
00333 p = cpl_parameter_new_value("xsh.xsh_linear.shifty",
00334 CPL_TYPE_INT,"Pixel Y shift applied in autocorrelation. "
00335 "Must be > 0.",
00336 "xsh.xsh_linear", 26);
00337 cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI,"shifty");
00338 cpl_parameterlist_append(recipe->parameters,p);
00339
00340
00341
00342
00343 p = cpl_parameter_new_value("xsh.xsh_linear.autocorr",
00344 CPL_TYPE_BOOL,"Compute autocor?",
00345 "xsh.xsh_linear",CPL_FALSE);
00346 cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI,"autocorr");
00347 cpl_parameterlist_append(recipe->parameters,p);
00348
00349
00350
00351
00352
00353
00354
00355 cleanup:
00356 if ( cpl_error_get_code() != CPL_ERROR_NONE ){
00357 xsh_error_dump(CPL_MSG_ERROR);
00358 return 1;
00359 }
00360 else {
00361 return 0;
00362 }
00363 }
00364
00365
00371
00372
00373 static int exec(cpl_plugin *plugin) {
00374 cpl_recipe *recipe = NULL;
00375
00376
00377 assure( plugin != NULL, CPL_ERROR_NULL_INPUT, "Null plugin" );
00378
00379
00380 assure( cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE,
00381 CPL_ERROR_TYPE_MISMATCH, "Plugin is not a recipe");
00382
00383 recipe = (cpl_recipe *)plugin;
00384
00385
00386 xsh_linear(recipe->parameters, recipe->frames);
00387
00388 cleanup:
00389 if ( cpl_error_get_code() != CPL_ERROR_NONE ) {
00390 xsh_error_dump(CPL_MSG_ERROR);
00391 return 1;
00392 }
00393 else {
00394 return 0;
00395 }
00396 }
00397
00398
00404
00405 static int
00406 destroy(cpl_plugin *plugin)
00407 {
00408 cpl_recipe *recipe = NULL;
00409
00410
00411 assure( plugin != NULL, CPL_ERROR_NULL_INPUT, "Null plugin" );
00412
00413
00414 assure( cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE,
00415 CPL_ERROR_TYPE_MISMATCH, "Plugin is not a recipe");
00416
00417 recipe = (cpl_recipe *)plugin;
00418
00419 xsh_free_parameterlist(&recipe->parameters);
00420
00421 cleanup:
00422 if (cpl_error_get_code() != CPL_ERROR_NONE)
00423 {
00424 return 1;
00425 }
00426 else
00427 {
00428 return 0;
00429 }
00430 }
00431
00432
00440
00441 static void xsh_linear(cpl_parameterlist* parameters, cpl_frameset* frameset){
00442
00443 const char* recipe_tags[1] = {XSH_LINEARITY};
00444 int recipe_tags_size = 1;
00445
00446 cpl_parameter* param = NULL;
00447 cpl_frameset* raws = NULL;
00448 cpl_frameset* calib = NULL;
00449 cpl_frame* bpmap = NULL;
00450 xsh_instrument* instrument = NULL;
00451
00452
00453 xsh_clipping_param crh_clipping = {
00454 0.0, 0, 0.0, 0.0, 0.3
00455 } ;
00456
00457
00458 xsh_clipping_param lin_clipping = {
00459 0.0, 0, 0.0, 0.0, 0.3
00460 } ;
00461
00462
00463 double exp_toler ;
00464
00465 int nbLinGroups ;
00466 int nRawFrames ;
00467 cpl_frameset ** rawGrp = NULL;
00468 cpl_frameset ** pgrp = NULL;
00469 cpl_frameset * medSet = NULL;
00470 cpl_frameset * nirSet = NULL ;
00471 cpl_imagelist * crh_list=NULL;
00472
00473 char *final_prefix = NULL ;
00474 int gain_switch=false;
00475 cpl_image* crh_ima=NULL;
00476
00477
00478 int i = 0, minfr = 999999 ;
00479 char * crh_name;
00480
00481
00482 cpl_frame *medFrame =NULL;
00483 char *ftag =NULL;
00484 char str[16] ;
00485 int pre_overscan_corr=0;
00486
00487
00488
00489 param = cpl_parameterlist_find(parameters,"xsh.xsh_linear.crh_clip_kappa");
00490 crh_clipping.sigma = cpl_parameter_get_double(param);
00491
00492 param = cpl_parameterlist_find(parameters,"xsh.xsh_linear.crh_clip_niter");
00493 crh_clipping.niter = cpl_parameter_get_int(param);
00494
00495 param = cpl_parameterlist_find(parameters,"xsh.xsh_linear.crh_clip_frac");
00496 crh_clipping.frac = cpl_parameter_get_double(param);
00497
00498
00499 param = cpl_parameterlist_find(parameters,"xsh.xsh_linear.lin_clip_kappa");
00500 lin_clipping.sigma = cpl_parameter_get_double(param);
00501
00502 param = cpl_parameterlist_find(parameters,"xsh.xsh_linear.lin_clip_niter");
00503 lin_clipping.niter = cpl_parameter_get_int(param);
00504
00505 param = cpl_parameterlist_find(parameters,"xsh.xsh_linear.lin_clip_frac");
00506 lin_clipping.frac = cpl_parameter_get_double(param);
00507
00508 param = cpl_parameterlist_find(parameters,"xsh.xsh_linear.lin_clip_diff");
00509 lin_clipping.diff = cpl_parameter_get_double(param);
00510
00511
00512 param = cpl_parameterlist_find(parameters,"xsh.xsh_linear.exp_toler");
00513 exp_toler = cpl_parameter_get_double(param);
00514
00515
00516
00517
00518
00519
00520
00521 check( xsh_begin( frameset, parameters, &instrument, &raws, &calib,
00522 recipe_tags, recipe_tags_size,
00523 RECIPE_ID, XSH_BINARY_VERSION,
00524 xsh_linear_description_short ) ) ;
00525
00526 check(param=cpl_parameterlist_find(parameters,
00527 "xsh.xsh_linear.compute_gain"));
00528 check(gain_switch = cpl_parameter_get_bool(param));
00529
00530 if(gain_switch) {
00531 check(xsh_gain(parameters,frameset,instrument));
00532 }
00533
00534
00535
00536
00537 bpmap = xsh_find_bpmap(calib) ;
00538
00539
00540
00541 xsh_msg("Calling the xsh_prepare function");
00542
00543 check_msg(xsh_prepare(raws,bpmap, NULL, XSH_LINEARITY, instrument,
00544 pre_overscan_corr),
00545 "Error with linear");
00546
00547 xsh_msg( "Working on Arm %s", xsh_instrument_arm_tostring( instrument) ) ;
00548 final_prefix = xsh_stringcat_any( "LINEAR_BPMAP_",
00549 xsh_instrument_arm_tostring( instrument),
00550 "" ) ;
00551 assure( final_prefix != NULL, cpl_error_get_code(),
00552 "Cant allocate memory for final prefix" ) ;
00553
00554 nRawFrames = cpl_frameset_get_size( raws ) ;
00555 xsh_msg( "Found %d LINEARITY Raw files", nRawFrames ) ;
00556
00557 if ( bpmap == NULL )
00558 xsh_msg( "No Bad Pixel Map" ) ;
00559 else
00560 xsh_msg( "Found BadPixel Map Frame: %s",
00561 cpl_frame_get_filename( bpmap ) ) ;
00562
00563
00564 xsh_msg( "Calling xsh_linear_group_by_exptime" ) ;
00565 xsh_msg( "exp_toler: %lf", exp_toler ) ;
00566
00567
00568
00569
00570
00571 rawGrp = cpl_malloc( nRawFrames*sizeof( cpl_frameset *) ) ;
00572
00573 nbLinGroups = xsh_linear_group_by_exptime( raws, instrument, exp_toler,
00574 rawGrp ) ;
00575
00576 xsh_msg( "Nb of groups: %d", nbLinGroups ) ;
00577
00578
00579 assure( nbLinGroups >= 3, CPL_ERROR_ILLEGAL_INPUT,
00580 "Not enough EXPTIME Groups, %d < 3", nbLinGroups ) ;
00581 pgrp = rawGrp ;
00582
00583 for( i = 0 ; i<nbLinGroups ; i++, pgrp++ ) {
00584 int nfr = cpl_frameset_get_size( *pgrp ) ;
00585 if ( nfr < minfr ) minfr = nfr ;
00586 xsh_msg( "Frameset #%d - %d frames", i, nfr ) ;
00587 }
00588
00589
00590
00591
00592
00593
00594 xsh_msg( "Calling xsh_remove_crh_multiple" ) ;
00595 xsh_msg( " CRH parameters: Sigma %lf, Niteration %d, Fraction %lf",
00596 crh_clipping.sigma, crh_clipping.niter, crh_clipping.frac ) ;
00597
00598 medSet = cpl_frameset_new() ;
00599 crh_list=cpl_imagelist_new();
00600
00601 for( i=0, pgrp = rawGrp ; i<nbLinGroups ; i++, pgrp++ ) {
00602
00603 sprintf( str, "%02d", i ) ;
00604 ftag = xsh_stringcat_any( "remove_crh_",
00605 xsh_instrument_arm_tostring( instrument),
00606 str, "", NULL ) ;
00607 crh_name= xsh_stringcat_any( "crh_map_",
00608 xsh_instrument_arm_tostring( instrument),
00609 str, ".fits", NULL ) ;
00610 xsh_free_frame(&medFrame);
00611 check_msg( medFrame = xsh_remove_crh_multiple( *pgrp,
00612 ftag,
00613 &crh_clipping,
00614 instrument,
00615 &crh_list,&crh_ima ),
00616 "Error in xsh_remove_crh" ) ;
00617
00618 cpl_frameset_insert( medSet, cpl_frame_duplicate(medFrame) ) ;
00619 cpl_free( ftag ) ;
00620 xsh_msg("saving file %d",i);
00621 cpl_imagelist_save(crh_list,"crh_list.fits",CPL_BPP_IEEE_FLOAT,
00622 NULL,CPL_IO_DEFAULT);
00623 xsh_add_temporary_file("crh_list.fits");
00624 cpl_image_save (crh_ima,crh_name, CPL_BPP_32_SIGNED,
00625 NULL, CPL_IO_DEFAULT);
00626 xsh_add_temporary_file(crh_name);
00627 cpl_free(crh_name ) ;
00628 xsh_free_imagelist(&crh_list);
00629 xsh_free_image(&crh_ima);
00630 }
00631 xsh_free_imagelist(&crh_list);
00632
00633
00634
00635
00636
00637
00638
00639 if ( instrument->arm == XSH_ARM_NIR ) {
00640
00641 nirSet = xsh_subtract_on_off( medSet, instrument ) ;
00642 assure( nirSet != NULL, cpl_error_get_code(),
00643 "Error in subtract" ) ;
00644
00645 check( bpmap = xsh_compute_linearity( nirSet, instrument,
00646 &lin_clipping ) ) ;
00647 }
00648 else {
00649
00650 bpmap = xsh_compute_linearity( medSet, instrument,
00651 &lin_clipping ) ;
00652 assure( bpmap != NULL, cpl_error_get_code(),
00653 "Error in compute_linearity" ) ;
00654 }
00655
00656
00657 xsh_msg( "Bad Pixel Map created" ) ;
00658 xsh_msg( "Saving final product" ) ;
00659
00660
00661
00662 check( xsh_add_product_bpmap( bpmap, frameset, parameters, RECIPE_ID,
00663 instrument, final_prefix ) ) ;
00664
00665
00666
00667 cleanup:
00668 xsh_end( RECIPE_ID, frameset, parameters ) ;
00669
00670 xsh_instrument_free( &instrument ) ;
00671 xsh_free_frame( &bpmap ) ;
00672 xsh_free_frameset( &raws ) ;
00673 xsh_free_frameset( &calib ) ;
00674 cpl_free( rawGrp ) ;
00675 xsh_free_frame(&medFrame);
00676 xsh_free_frameset( &medSet ) ;
00677 xsh_free_frameset( &nirSet ) ;
00678 cpl_free( final_prefix ) ;
00679 xsh_free_imagelist(&crh_list);
00680
00681 return;
00682 }
00683
00684
00685
00693
00694 static void
00695 xsh_gain(cpl_parameterlist* parameters,
00696 cpl_frameset* frameset,
00697 xsh_instrument* instr){
00698
00699
00700 cpl_frameset* set_flats=NULL;
00701 cpl_frameset* set_biases=NULL;
00702 cpl_table* gain_tbl=NULL;
00703 cpl_parameter* p=NULL;
00704 cpl_imagelist* diff_imlist=NULL;
00705 cpl_imagelist* autocorr_imlist=NULL;
00706 cpl_propertylist* qclist=NULL;
00707 cpl_imagelist* iml_flats=NULL;
00708 cpl_imagelist* iml_biases=NULL;
00709 cpl_image* img=NULL;
00710 cpl_frame* frm=NULL;
00711 cpl_frame* result=NULL;
00712
00713 unsigned mode_collapse_noautocorr=IRPLIB_GAIN_PTC |
00714 IRPLIB_GAIN_WITH_RESCALE |
00715 IRPLIB_GAIN_COLLAPSE;
00716
00717
00718
00719 unsigned mode_collapse_autocorr=IRPLIB_GAIN_PTC |
00720 IRPLIB_GAIN_WITH_RESCALE |
00721 IRPLIB_GAIN_COLLAPSE |
00722 IRPLIB_GAIN_WITH_AUTOCORR;
00723
00724
00725
00726 unsigned mode_nocollapse_noautocorr=IRPLIB_GAIN_PTC |
00727 IRPLIB_GAIN_WITH_RESCALE |
00728 IRPLIB_GAIN_NO_COLLAPSE;
00729
00730
00731 unsigned mode_nocollapse_autocorr=IRPLIB_GAIN_PTC |
00732 IRPLIB_GAIN_WITH_RESCALE |
00733 IRPLIB_GAIN_NO_COLLAPSE |
00734 IRPLIB_GAIN_WITH_AUTOCORR;
00735
00736
00737 unsigned mode=0;
00738 const char* tag_on=NULL;
00739 const char* tag_off=NULL;
00740 cpl_vector* exptimes=NULL;
00741 int llx=0;
00742 int lly=0;
00743 int urx=0;
00744 int ury=0;
00745 double kappa=0;
00746 int nclip=3;
00747 int shiftx=0;
00748 int shifty=0;
00749 int auto_corr=0;
00750 const char* name=NULL;
00751 double mjd_obs=0;
00752
00753 int i=0;
00754 const char* name_o="gain_table.fits";
00755 const char* name_diff_o="diff.fits";
00756 const char* name_autocorr_o="autocorr.fits";
00757 const char* pro_catg=XSH_GET_TAG_FROM_ARM(XSH_GAIN,instr);
00758 const char* file=NULL;
00759 int ext=0;
00760 int nbiases=0;
00761 int nflats=0;
00762 int sx=0;
00763 int sy=0;
00764
00765 cpl_propertylist* plist=NULL;
00766 int ext_min=0;
00767 int ext_max=1;
00768 double tolerance=0;
00769
00770
00771
00772
00773
00774 check(p = cpl_parameterlist_find(parameters,"xsh.xsh_linear.llx"));
00775 check(llx = cpl_parameter_get_int(p));
00776
00777 check(p = cpl_parameterlist_find(parameters,"xsh.xsh_linear.lly"));
00778 check(lly = cpl_parameter_get_int(p));
00779
00780 check(p = cpl_parameterlist_find(parameters,"xsh.xsh_linear.urx"));
00781 check(urx = cpl_parameter_get_int(p));
00782
00783 check(p = cpl_parameterlist_find(parameters,"xsh.xsh_linear.ury"));
00784 check(ury = cpl_parameter_get_int(p));
00785
00786 check(p = cpl_parameterlist_find(parameters,"xsh.xsh_linear.kappa"));
00787 check(kappa = cpl_parameter_get_double(p));
00788
00789 check(p = cpl_parameterlist_find(parameters,"xsh.xsh_linear.nclip"));
00790 check(nclip = cpl_parameter_get_int(p));
00791
00792 check(p = cpl_parameterlist_find(parameters,"xsh.xsh_linear.shiftx"));
00793 check(shiftx = cpl_parameter_get_int(p));
00794
00795 check(p = cpl_parameterlist_find(parameters,"xsh.xsh_linear.shifty"));
00796 check(shifty = cpl_parameter_get_int(p));
00797
00798 check(p = cpl_parameterlist_find(parameters,"xsh.xsh_linear.autocorr"));
00799 check(auto_corr = cpl_parameter_get_bool(p));
00800
00801 check(p = cpl_parameterlist_find(parameters,"xsh.xsh_linear.exp_toler"));
00802 check(tolerance = cpl_parameter_get_double(p));
00803
00804 if(shiftx < 1) {
00805 shiftx=1;
00806 xsh_msg_warning("Autocorrelation X shift must be >1. Set it to 1.");
00807 }
00808
00809 if(shifty < 1) {
00810 shifty=1;
00811 xsh_msg_warning("Autocorrelation Y shift must be >1. Set it to 1.");
00812 }
00813
00814 if(xsh_instrument_get_arm(instr) == XSH_ARM_UVB) {
00815 tag_on ="LINEARITY_UVB";
00816 tag_off="BIAS_UVB";
00817
00818 if(auto_corr==0) {
00819 mode=mode_collapse_noautocorr|IRPLIB_GAIN_OPT;
00820 } else {
00821 mode=mode_collapse_autocorr|IRPLIB_GAIN_OPT;
00822 }
00823
00824 } else if (xsh_instrument_get_arm(instr) == XSH_ARM_VIS) {
00825 tag_on ="LINEARITY_VIS";
00826 tag_off="BIAS_VIS";
00827
00828 if(auto_corr==0) {
00829 mode=mode_collapse_noautocorr|IRPLIB_GAIN_OPT;
00830 } else {
00831 mode=mode_collapse_autocorr|IRPLIB_GAIN_OPT;
00832 }
00833
00834 } else if (xsh_instrument_get_arm(instr) == XSH_ARM_NIR) {
00835 tag_on ="LINEARITY_NIR_ON";
00836 tag_off="LINEARITY_NIR_OFF";
00837
00838 if(auto_corr==0) {
00839 mode=mode_nocollapse_noautocorr|IRPLIB_GAIN_NIR;
00840 } else {
00841 mode=mode_nocollapse_autocorr|IRPLIB_GAIN_NIR;
00842 }
00843
00844 } else {
00845 xsh_msg_error("instr %d not supported. Exit.",
00846 xsh_instrument_get_arm(instr));
00847 }
00848 xsh_msg("tag_on=%s tag_off=%s",tag_on,tag_off);
00849
00850
00851
00852 check(set_flats= xsh_frameset_extract((cpl_frameset*) frameset,tag_on));
00853 nflats=cpl_frameset_get_size(set_flats);
00854
00855
00856
00857
00858 check(set_biases= xsh_frameset_extract((cpl_frameset*) frameset,tag_off));
00859 check(exptimes=xsh_get_exptimes(set_flats));
00860 check(nbiases=cpl_frameset_get_size(set_biases));
00861 check(name=cpl_frame_get_filename(cpl_frameset_get_first(set_flats)));
00862 check(plist=cpl_propertylist_load(name,0));
00863 check(mjd_obs=xsh_pfits_get_mjdobs(plist));
00864 xsh_free_propertylist(&plist);
00865
00866 if(xsh_instrument_get_arm(instr) == XSH_ARM_VIS) {
00867
00868 if(mjd_obs> 53095.4) {
00869 ext_min=1;
00870 ext_max=3;
00871 }
00872 }
00873
00874
00875 for(ext=ext_min;ext<ext_max;ext++) {
00876 xsh_msg("Processing extention %d",ext);
00877
00878 xsh_free_imagelist(&iml_flats);
00879 iml_flats=cpl_imagelist_new();
00880 for(i=0;i<nflats;i++) {
00881 check(frm=cpl_frameset_get_frame(set_flats,i));
00882 check(file=cpl_frame_get_filename(frm));
00883 check(img=cpl_image_load(file,CPL_TYPE_FLOAT,0,ext));
00884 check(cpl_imagelist_set(iml_flats,cpl_image_duplicate(img),i));
00885 xsh_free_image(&img);
00886 }
00887
00888
00889 xsh_free_imagelist(&iml_biases);
00890 iml_biases=cpl_imagelist_new();
00891 for(i=0;i<nbiases;i++) {
00892 check(frm=cpl_frameset_get_frame(set_biases,i));
00893 check(file=cpl_frame_get_filename(frm));
00894 check(img=cpl_image_load(file,CPL_TYPE_FLOAT,0,ext));
00895 check(cpl_imagelist_set(iml_biases,cpl_image_duplicate(img),i));
00896 xsh_free_image(&img);
00897 }
00898
00899 xsh_free_propertylist(&qclist);
00900 check(qclist=cpl_propertylist_new());
00901
00902 xsh_free_table(&gain_tbl);
00903
00904 sx=cpl_image_get_size_x(cpl_imagelist_get(iml_flats,0));
00905 sy=cpl_image_get_size_y(cpl_imagelist_get(iml_flats,0));
00906
00907 urx=(urx<sx) ? urx : sx;
00908 ury=(ury<sy) ? ury : sy;
00909
00910 llx=(llx>1) ? llx : 1;
00911 lly=(lly>1) ? lly : 1;
00912 check(gain_tbl=xsh_detmon_gain(iml_flats,iml_biases,exptimes,tolerance,
00913 llx,lly,urx,ury,
00914 kappa,nclip,
00915 shiftx,shifty,qclist,mode,
00916 &diff_imlist,&autocorr_imlist));
00917
00918
00919 if(ext==ext_min) {
00920
00921 check(cpl_table_save(gain_tbl,qclist,NULL,name_o,CPL_IO_DEFAULT));
00922 if(auto_corr==1) {
00923 cpl_imagelist_save(diff_imlist,name_diff_o,CPL_BPP_IEEE_FLOAT,
00924 NULL,CPL_IO_DEFAULT);
00925
00926 cpl_imagelist_save(autocorr_imlist,name_autocorr_o,
00927 CPL_BPP_IEEE_FLOAT,NULL,CPL_IO_DEFAULT);
00928
00929 }
00930 } else {
00931 check(cpl_table_save(gain_tbl,qclist,NULL,name_o,CPL_IO_EXTEND));
00932
00933 if(auto_corr==1) {
00934 cpl_imagelist_save(diff_imlist,name_diff_o,CPL_BPP_IEEE_FLOAT,
00935 NULL,CPL_IO_EXTEND);
00936
00937 cpl_imagelist_save(autocorr_imlist,name_autocorr_o,
00938 CPL_BPP_IEEE_FLOAT,NULL,CPL_IO_EXTEND);
00939
00940 }
00941
00942
00943 }
00944
00945 }
00946
00947
00948
00949
00950 xsh_free_frame(&result);
00951 check(result=cpl_frame_new());
00952
00953
00954 cpl_frame_set_filename(result,name_o);
00955 cpl_frame_set_tag(result,pro_catg);
00956 cpl_frame_set_type(result,CPL_FRAME_TYPE_TABLE);
00957 cpl_frame_set_group(result, CPL_FRAME_GROUP_PRODUCT);
00958 cpl_frame_set_level(result, CPL_FRAME_LEVEL_FINAL);
00959
00960
00961 check(xsh_add_product_table(cpl_frame_duplicate(result),
00962 frameset,parameters,RECIPE_ID,instr));
00963
00964
00965
00966
00967
00968 cleanup:
00969 xsh_free_propertylist(&plist);
00970 cpl_free(exptimes); exptimes=NULL;
00971 xsh_free_frame(&result);
00972 xsh_free_frameset(&set_flats);
00973 xsh_free_frameset(&set_biases);
00974 xsh_free_propertylist(&qclist);
00975 xsh_free_imagelist(&iml_biases);
00976 xsh_free_imagelist(&iml_flats);
00977
00978 return;
00979 }
00980
00981
00982
00988
00989 static cpl_vector*
00990 xsh_get_exptimes(cpl_frameset* set)
00991 {
00992
00993 cpl_vector* times=NULL;
00994 int n=0;
00995 int i=0;
00996 cpl_propertylist* plist=NULL;
00997 cpl_frame* frm=NULL;
00998
00999
01000 n=cpl_frameset_get_size(set);
01001 times=cpl_vector_new(n);
01002
01003 for(i=0;i<n;i++) {
01004 check(frm=cpl_frameset_get_frame(set,i));
01005 check(plist=cpl_propertylist_load(cpl_frame_get_filename(frm),0));
01006 cpl_vector_set(times,i,xsh_pfits_get_exptime(plist));
01007
01008 xsh_free_propertylist(&plist);
01009 }
01010
01011 cleanup:
01012
01013
01014 return times;
01015
01016 }
01017
01018
01019