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
00031
00032
00033
00034
00035
00036
00037 #ifdef HAVE_CONFIG_H
00038 # include <config.h>
00039 #endif
00040
00041
00042
00043
00044 #include "sinfo_new_lamp_flats.h"
00045 #include "sinfo_flat_ini_by_cpl.h"
00046 #include "sinfo_pro_save.h"
00047 #include "sinfo_pro_types.h"
00048 #include "sinfo_functions.h"
00049 #include "sinfo_new_cube_ops.h"
00050 #include "sinfo_error.h"
00051 #include "sinfo_utils_wrappers.h"
00052 #include "sinfo_image_ops.h"
00053 #include "sinfo_utilities.h"
00054 #include "sinfo_globals.h"
00055
00056
00057
00058
00059 static int
00060 new_qc_get_cnt(cpl_frameset* on_set, cpl_frameset* of_set, flat_config* cfg);
00061 static int
00062 new_lamp_flats_det_ncounts(cpl_frameset* raw, flat_config* config);
00063
00064 static struct {
00065
00066 double avg_on;
00067 double std_on;
00068 double avg_of;
00069 double std_of;
00070 double avg_di;
00071 double std_di;
00072 double nsat_on;
00073 double noise_on;
00074 double noise_of;
00075 double flux_on;
00076 double nsat;
00077
00078 } qc_lampflat;
00079
00080
00081
00082
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112 int
00113 sinfo_new_lamp_flats (const char* plugin_id,
00114 cpl_parameterlist* config,
00115 cpl_frameset* sof,
00116 cpl_frameset* ref_set)
00117 {
00118 flat_config * cfg =NULL;
00119 cpl_imagelist* list_object=NULL;
00120 cpl_imagelist* list_dither_object=NULL ;
00121 cpl_imagelist* list_sky=NULL ;
00122 cpl_imagelist* list_dither_sky=NULL;
00123 cpl_image ** im=NULL ;
00124 cpl_image * norm_dith =NULL;
00125 cpl_image * im_obj =NULL;
00126 cpl_image * int_im =NULL;
00127 cpl_image * int_im_dith =NULL;
00128 cpl_image * im_sky =NULL;
00129 cpl_image * im_dither =NULL;
00130 cpl_image * im_obj_sub =NULL;
00131 cpl_image * im_dither_sub =NULL;
00132 cpl_image * im_dither_sky =NULL;
00133 cpl_image ** imMed=NULL ;
00134 cpl_image * colImage =NULL;
00135 cpl_image * mask_im =NULL;
00136 cpl_image * norm =NULL;
00137 cpl_image * compImage =NULL;
00138 cpl_image * maskImage =NULL;
00139 cpl_image * threshIm =NULL;
00140
00141 char name[MAX_NAME_SIZE];
00142
00143 int typ;
00144 Stats * stats =NULL;
00145 int i = 0;
00146 int* n=NULL;
00147 int nob =0;
00148 int nsky = 0;
00149 int nobjdith = 0;
00150 int nskydith = 0;
00151 int n_badpixels =0;
00152 int pos =0;
00153
00154 float** slit_edges=NULL;
00155 float local_clean_mean =0.;
00156 float clean_stdev =0.;
00157 float mean_factor =0.;
00158 float val_x=0;
00159 float val_y=0;
00160
00161 char outNameDither[MAX_NAME_SIZE];
00162 char name_list[MAX_NAME_SIZE];
00163 char tbl_slitpos_name[MAX_NAME_SIZE];
00164
00165 cpl_table* tbl_slitpos=NULL;
00166 int* status=NULL;
00167
00168 int n_im_med=0;
00169 cpl_frameset* raw=NULL;
00170
00171 cpl_table* qclog_tbl=NULL;
00172 int naxis1=0;
00173 int naxis2=0;
00174 double fpn_stdev1=0;
00175 double fpn_stdev2=0;
00176
00177 int no=0;
00178 float lo_cut=0;
00179 float hi_cut=0;
00180
00181
00182
00183
00184
00185
00186
00187
00188 cknull_nomsg(raw=cpl_frameset_new());
00189
00190 cknull(cfg = sinfo_parse_cpl_input_flat(config,sof,&raw),
00191 "could not parse cpl input!");
00192
00193 if (cfg->interpolInd == 1) {
00194 if(sinfo_is_fits_file(cfg->mask) != 1) {
00195 sinfo_msg_error("Input file %s is not FITS",cfg->mask);
00196 goto cleanup;
00197 }
00198 if (sinfo_is_fits_file(cfg->slitposList) != 1) {
00199 sinfo_msg_error("Input file %s is not FITS",cfg->slitposList);
00200 goto cleanup;
00201 }
00202 }
00203
00204
00205
00206
00207
00208
00209
00210 sinfo_msg("Takes clean mean of several images");
00211
00212 cknull(list_object = cpl_imagelist_new (),"could not allocate memory");
00213
00214 if (cfg->contains_dither == 1) {
00215 cknull(list_dither_object=cpl_imagelist_new(),"could not allocate memory");
00216 }
00217
00218 if (cfg->contains_sky == 1) {
00219 cknull(list_sky=cpl_imagelist_new(),"could not allocate memory");
00220 }
00221
00222 if (cfg->contains_dither == 1 && cfg->nditheroff > 0) {
00223 cknull(list_dither_sky=cpl_imagelist_new(),"could not allocate memory");
00224 }
00225
00226 if (cfg->contains_dither == 0 && cfg->nditheroff > 0){
00227 sinfo_msg_error("please use non-dithered off-frames, remove the 2!");
00228 goto cleanup;
00229 }
00230
00231
00232
00233 im = (cpl_image**) cpl_calloc (cfg -> nframes, sizeof(cpl_image*));
00234
00235 for (i=0; i< cfg->nframes; i++) {
00236 strcpy(name,cfg->framelist[i]);
00237 if(sinfo_is_fits_file(name) != 1) {
00238 sinfo_msg_error("PP Input file %s %d is not FITS",name,i);
00239 goto cleanup;
00240 }
00241 im[i]=cpl_image_load(name,CPL_TYPE_FLOAT,0,0);
00242
00243 }
00244
00245 for (i=0; i< cfg->nframes; i++) {
00246 typ = cfg->frametype[i];
00247 pos = cfg->frameposition[i];
00248 cknull(im[i],"could not load image %d",i);
00249 if (pos == 2) {
00250 if (typ == 1) {
00251 cpl_imagelist_set( list_object, cpl_image_duplicate(im[i]), nob );
00252 nob = nob + 1;
00253 } else {
00254 cpl_imagelist_set( list_sky, cpl_image_duplicate(im[i]), nsky );
00255 nsky = nsky + 1 ;
00256 }
00257 } else {
00258 if (typ == 1) {
00259 cpl_imagelist_set(list_dither_object,
00260 cpl_image_duplicate(im[i]), nobjdith );
00261 nobjdith = nobjdith + 1;
00262 } else {
00263 cpl_imagelist_set( list_dither_sky,
00264 cpl_image_duplicate(im[i]), nskydith );
00265 nskydith = nskydith + 1 ;
00266 }
00267 }
00268 }
00269
00270
00271 if (nob != cfg->nobj || cfg->noff != nsky ||
00272 nobjdith != cfg->nditherobj || nskydith != cfg->nditheroff) {
00273 sinfo_msg_error("something is wrong with the number of "
00274 "the different types of frames");
00275 goto cleanup;
00276 }
00277
00278
00279 sinfo_msg("Creates and fills cubes with the different image lists");
00280 cknull(list_object,"could not create data cube!");
00281
00282 if (cfg->contains_dither == 1) {
00283 cknull(list_dither_object,"could not create data cube!");
00284 }
00285 if (cfg->contains_sky == 1 && nsky > 0) {
00286 cknull(list_sky,"could not create data cube!");
00287 }
00288
00289 if (cfg->contains_dither == 1 && nskydith > 0) {
00290 cknull(list_dither_sky,"could not create data cube!");
00291 }
00292
00293
00294
00295 sinfo_msg("Takes the average of the different cubes");
00296 if (cfg->loReject*cfg->nobj < 1. && cfg->hiReject *cfg->nobj < 1.) {
00297 cknull(im_obj = sinfo_new_average_cube_to_image(list_object ),
00298 "sinfo_averageCubeToImage failed" );
00299 } else {
00300
00301 no=cpl_imagelist_get_size(list_object);
00302 lo_cut=(floor)(cfg->loReject*no+0.5);
00303 hi_cut=(floor)(cfg->hiReject*no+0.5);
00304 cknull(im_obj=cpl_imagelist_collapse_minmax_create(list_object,
00305 lo_cut,
00306 hi_cut),
00307 "sinfo_average_with_rejection failed" );
00308 }
00309 sinfo_free_imagelist(&list_object);
00310
00311 if (cfg->contains_sky == 1) {
00312 if (cfg->loReject * nsky < 1. && cfg->hiReject * nsky < 1.) {
00313 cknull(im_sky = sinfo_new_average_cube_to_image(list_sky ),
00314 "sinfo_new_average_cube_to_image failed" );
00315 } else {
00316
00317 no=cpl_imagelist_get_size(list_sky);
00318 lo_cut=(floor)(cfg->loReject*no+0.5);
00319 hi_cut=(floor)(cfg->hiReject*no+0.5);
00320 cknull(im_sky=cpl_imagelist_collapse_minmax_create(list_sky,lo_cut,hi_cut),
00321 "sinfo_average_with_rejection failed" );
00322 }
00323 sinfo_free_imagelist(&list_sky);
00324 }
00325
00326 if (cfg->contains_dither == 1) {
00327 if (cfg->loReject*nobjdith < 1. && cfg->hiReject * nobjdith < 1.) {
00328 cknull(im_dither = sinfo_new_average_cube_to_image(list_dither_object ),
00329 "sinfo_new_average_cube_to_image failed" );
00330 } else {
00331
00332
00333 no=cpl_imagelist_get_size(list_dither_object);
00334 lo_cut=(floor)(cfg->loReject*no+0.5);
00335 hi_cut=(floor)(cfg->hiReject*no+0.5);
00336 cknull(im_dither=cpl_imagelist_collapse_minmax_create(list_dither_object,
00337 lo_cut,hi_cut),
00338 "sinfo_average_with_rejection failed" );
00339 }
00340 sinfo_free_imagelist(&list_dither_object);
00341 }
00342
00343 if (cfg->contains_dither == 1 && nskydith > 0 ) {
00344 if (cfg->loReject*nskydith < 1. && cfg->hiReject*nskydith < 1.) {
00345 cknull(im_dither_sky = sinfo_new_average_cube_to_image(list_dither_sky ),
00346 "sinfo_new_average_cube_to_image failed" );
00347 } else {
00348 no=cpl_imagelist_get_size(list_dither_sky);
00349 lo_cut=(floor)(cfg->loReject*no+0.5);
00350 hi_cut=(floor)(cfg->hiReject*no+0.5);
00351 cknull(im_dither_sky=cpl_imagelist_collapse_minmax_create(list_dither_sky,
00352 lo_cut,hi_cut),
00353 "new_average_with_rejection failed" );
00354 }
00355 sinfo_free_imagelist(&list_dither_sky);
00356 }
00357
00358
00359
00360
00361
00362
00363
00364
00365
00366 sinfo_msg("Subtracts the resulting off-frame (sky) from the on-frame");
00367 if (cfg->contains_sky == 1) {
00368 cknull(im_obj_sub = cpl_image_subtract_create(im_obj, im_sky),
00369 "could not sinfo_sub_image");
00370 sinfo_free_image(&im_obj);
00371 if (((cfg->contains_dither == 1) && (nskydith > 0)) ||
00372 (cfg->contains_dither == 0)) {
00373 sinfo_free_image(&im_sky);
00374 }
00375 im_obj = im_obj_sub;
00376 }
00377
00378 if (cfg->contains_dither == 1 && nskydith > 0) {
00379 cknull(im_dither_sub=cpl_image_subtract_create(im_dither, im_dither_sky),
00380 "could not sinfo_sub_image");
00381 sinfo_free_image(&im_dither);
00382 sinfo_free_image(&im_dither_sky);
00383 im_dither = im_dither_sub;
00384 } else if (cfg->contains_dither == 1 &&
00385 nskydith == 0 &&
00386 cfg->contains_sky == 1) {
00387 cknull(im_dither_sub = cpl_image_subtract_create(im_dither, im_sky),
00388 "could not sinfo_sub_image");
00389 sinfo_free_image(&im_dither);
00390 sinfo_free_image(&im_sky);
00391 im_dither = im_dither_sub;
00392 }
00393
00394
00395
00396
00397
00398
00399
00400
00401 sinfo_msg("Generating a static bad pixel mask");
00402 n_im_med = cfg->iterations+1;
00403
00404 imMed=(cpl_image**) cpl_calloc(n_im_med, sizeof(cpl_image*));
00405
00406 if (cfg->badInd == 1) {
00407 sinfo_msg("removes the intensity tilt from every column and");
00408 sinfo_msg("computes the standard deviation on a rectangular zone");
00409
00410
00411 cknull(colImage = sinfo_new_col_tilt( im_obj, cfg->sigmaFactor ),
00412 "sinfo_colTilt failed" );
00413
00414 cknull(stats = sinfo_new_image_stats_on_rectangle(colImage,
00415 cfg->badLoReject,
00416 cfg->badHiReject,
00417 cfg->llx,
00418 cfg->lly,
00419 cfg->urx,
00420 cfg->ury),
00421 "sinfo_get_image_stats_on_vig failed\n");
00422
00423 local_clean_mean = stats->cleanmean;
00424 clean_stdev = stats->cleanstdev;
00425
00426
00427
00428 if (cfg->threshInd == 1) {
00429 cknull(threshIm = sinfo_new_thresh_image(colImage,
00430 local_clean_mean-mean_factor*clean_stdev,
00431 local_clean_mean+mean_factor*clean_stdev),
00432 " sinfo_threshImage failed\n" );
00433 }
00434 if (cfg->threshInd == 0) {
00435 threshIm = colImage;
00436 }
00437
00438
00439
00440
00441
00442
00443
00444 cknull(imMed[0]= sinfo_new_median_image(threshIm,-cfg->factor*clean_stdev),
00445 " sinfo_medianImage failed" );
00446
00447
00448
00449
00450 for (i=1; i< cfg->iterations+1; i++) {
00451 cknull(imMed[i]=sinfo_new_median_image(imMed[i-1],
00452 -cfg->factor*clean_stdev),
00453 "sinfo_medianImage failed" );
00454 }
00455
00456
00457 cknull(compImage=sinfo_new_compare_images(threshIm,
00458 imMed[cfg->iterations],
00459 im_obj),
00460 "sinfo_compareImages failed" );
00461
00462
00463 n = (int*)cpl_calloc(1,sizeof(int));
00464 cknull(maskImage = sinfo_new_promote_image_to_mask( compImage, n ),
00465 "error in sinfo_promoteImageToMask" );
00466
00467
00468 n_badpixels = n[0];
00469 sinfo_msg("No of bad pixels: %d", n_badpixels);
00470
00471 cknull_nomsg(qclog_tbl = sinfo_qclog_init());
00472 ck0_nomsg(sinfo_qclog_add_int(qclog_tbl,"QC BP-MAP NBADPIX",n_badpixels,
00473 "No of bad pixels","%d"));
00474
00475 ck0(sinfo_pro_save_ima(maskImage,ref_set,sof,cfg->maskname,
00476 PRO_BP_MAP,qclog_tbl,plugin_id,config),
00477 "cannot save ima %s", cfg->maskname);
00478
00479
00480
00481 sinfo_free_table(&qclog_tbl);
00482
00483 sinfo_new_del_Stats(stats);
00484 sinfo_free_int(&n);
00485 sinfo_free_image(&threshIm);
00486 if (cfg->threshInd == 1) {
00487 sinfo_free_image(&colImage);
00488 }
00489 sinfo_free_image(&compImage);
00490 sinfo_free_image(&maskImage);
00491
00492 for (i=0; i< cfg->iterations+1; i++) {
00493 sinfo_free_image(&imMed[i]);
00494 }
00495
00496 }
00497
00498 cpl_free(imMed);
00499 imMed=NULL;
00500
00501
00502
00503
00504
00505
00506
00507 sinfo_msg("Creates a Master flat field");
00508 if (cfg->interpolInd == 1) {
00509 cknull(mask_im = cpl_image_load(cfg->mask,CPL_TYPE_FLOAT,0,0),
00510 "could not load static bad pixel mask" );
00511
00512
00513
00514
00515 slit_edges = (float **) cpl_calloc( 32, sizeof (float*) ) ;
00516 for ( i = 0 ; i < 32 ; i++ )
00517 {
00518 slit_edges[i] = (float *) cpl_calloc( 2, sizeof (float)) ;
00519 }
00520
00521 if(sinfo_is_fits_file(cfg->slitposList) !=1 ) {
00522 sinfo_msg_error("Input file %s is not FITS", cfg->slitposList);
00523 goto cleanup;
00524 }
00525 strcpy(tbl_slitpos_name,cfg->slitposList);
00526 check(tbl_slitpos = cpl_table_load(tbl_slitpos_name,1,0),
00527 "error loading tbl %s",tbl_slitpos_name);
00528
00529 for (i =0 ; i< 32; i++){
00530 val_x=cpl_table_get_double(tbl_slitpos,"pos1",i,status);
00531 val_y=cpl_table_get_double(tbl_slitpos,"pos2",i,status);
00532 slit_edges[i][0]=val_x;
00533 slit_edges[i][1]=val_y;
00534 }
00535 sinfo_free_table(&tbl_slitpos);
00536
00537 cknull(int_im = sinfo_interpol_source_image (im_obj, mask_im,
00538 cfg->maxRad, slit_edges),
00539 "could not carry out sinfo_interpolSourceImage" );
00540
00541 sinfo_free_image(&im_obj);
00542 cknull(norm = sinfo_new_normalize_to_central_pixel(int_im),
00543 "could not normalize flatfield" );
00544 sinfo_free_image(&int_im);
00545 im_obj = norm;
00546
00547 if (cfg->contains_dither == 1) {
00548 cknull(int_im_dith = sinfo_interpol_source_image(im_dither,
00549 mask_im,
00550 cfg->maxRad,
00551 slit_edges),
00552 "could not carry out sinfo_interpolSourceImage" );
00553
00554 cpl_image_delete(im_dither);
00555 cknull(norm_dith = sinfo_new_normalize_to_central_pixel(int_im_dith),
00556 "could not normalize flatfield" );
00557 sinfo_free_image(&int_im_dith);
00558 im_dither = norm_dith;
00559 }
00560
00561 for ( i = 0 ; i < 32 ; i++ )
00562 {
00563 cpl_free( slit_edges[i] );
00564 }
00565 cpl_free( slit_edges ) ;
00566 sinfo_free_image(&mask_im);
00567
00568 }
00569
00570 if (cfg->interpolInd != 1) {
00571 cknull(norm = sinfo_new_normalize_to_central_pixel(im_obj),
00572 "could not normalize flatfield" );
00573 sinfo_free_image(&im_obj);
00574 im_obj = norm;
00575
00576 if (cfg->contains_dither == 1) {
00577 cknull(norm_dith = sinfo_new_normalize_to_central_pixel(im_dither),
00578 "could not normalize flatfield" );
00579 sinfo_free_image(&im_dither);
00580 im_dither = norm_dith;
00581 }
00582 }
00583
00584 naxis1=cpl_image_get_size_x(im_obj);
00585 naxis2=cpl_image_get_size_y(im_obj);
00586
00587
00588 if(cfg->qc_fpn_xmin1 < 1) {
00589 sinfo_msg_error("qc_ron_xmin < 1");
00590 goto cleanup;
00591 }
00592
00593 if(cfg->qc_fpn_xmax1 > naxis1) {
00594 sinfo_msg_error("qc_ron_xmax < %d",naxis1);
00595 goto cleanup;
00596 }
00597
00598 if(cfg->qc_fpn_ymin1 < 1) {
00599 sinfo_msg_error("qc_ron_ymin < 1");
00600 goto cleanup;
00601 }
00602
00603 if(cfg->qc_fpn_ymax1 > naxis2) {
00604 sinfo_msg_error("qc_ron_ymax < %d",naxis2);
00605 goto cleanup;
00606 }
00607 fpn_stdev1 = cpl_image_get_stdev_window(im_obj,
00608 cfg->qc_fpn_xmin1,
00609 cfg->qc_fpn_ymin1,
00610 cfg->qc_fpn_xmax1,
00611 cfg->qc_fpn_ymax1);
00612
00613
00614 if(cfg->qc_fpn_xmin2 < 1) {
00615 sinfo_msg_error("qc_ron_xmin < %d",1);
00616 goto cleanup;
00617 }
00618
00619
00620 if(cfg->qc_fpn_xmax2 > naxis1) {
00621 sinfo_msg_error("qc_ron_xmax < %d",naxis1);
00622 goto cleanup;
00623 }
00624
00625 if(cfg->qc_fpn_ymin2 < 1) {
00626 sinfo_msg_error("qc_ron_ymin < 1");
00627 goto cleanup;
00628 }
00629
00630 if(cfg->qc_fpn_ymax2 > naxis2) {
00631 sinfo_msg_error("qc_ron_ymax < %d",naxis2);
00632 goto cleanup;
00633 }
00634 fpn_stdev2 = cpl_image_get_stdev_window(im_obj,
00635 cfg->qc_fpn_xmin2,
00636 cfg->qc_fpn_ymin2,
00637 cfg->qc_fpn_xmax2,
00638 cfg->qc_fpn_ymax2);
00639
00640
00641
00642 ck0(new_lamp_flats_det_ncounts(raw,cfg),"error computing number of counts");
00643 cknull_nomsg(qclog_tbl = sinfo_qclog_init());
00644 ck0_nomsg(sinfo_qclog_add_double(qclog_tbl,"QC SPECFLAT NCNTSAVG",
00645 qc_lampflat.avg_di,"Average counts","%g"));
00646 ck0_nomsg(sinfo_qclog_add_double(qclog_tbl,"QC SPECFLAT NCNTSSTD",
00647 qc_lampflat.std_di,"Stdev counts","%g"));
00648 ck0_nomsg(sinfo_qclog_add_double(qclog_tbl,"QC SPECFLAT OFFFLUX",
00649 qc_lampflat.avg_of,
00650 "Average flux off frames","%g"));
00651
00652 ck0_nomsg(sinfo_qclog_add_double(qclog_tbl,
00653 "QC LFLAT FPN1",
00654 fpn_stdev1,
00655 "Fixed Pattern Noise of combined frames",
00656 "%f"));
00657
00658 ck0_nomsg(sinfo_qclog_add_double(qclog_tbl,
00659 "QC LFLAT FPN2",
00660 fpn_stdev2,
00661 "Fixed Pattern Noise of combined frames",
00662 "%f"));
00663
00664 ck0(sinfo_pro_save_ima(im_obj,ref_set,sof,cfg->outName,
00665 PRO_MASTER_FLAT_LAMP,qclog_tbl,plugin_id,config),
00666 "cannot save ima %s", cfg->outName);
00667
00668 sinfo_free_table(&qclog_tbl);
00669 sinfo_free_image(&im_obj);
00670
00671
00672 if (cfg->contains_dither == 1) {
00673
00674 if (strstr(cfg->outName, ".fits" ) != NULL ) {
00675
00676 snprintf(name_list, MAX_NAME_SIZE-1,"%s%s",
00677 sinfo_new_get_rootname(cfg->outName),
00678 "_dither");
00679 strcpy(outNameDither,name_list);
00680 strcat(outNameDither,strstr(cfg->outName,".fits"));
00681
00682 } else {
00683 strcpy(outNameDither,cfg->outName);
00684 strcat(outNameDither,"_dither");
00685 }
00686
00687
00688 naxis1=cpl_image_get_size_x(im_dither);
00689 naxis2=cpl_image_get_size_y(im_dither);
00690
00691
00692 if(cfg->qc_fpn_xmin1 < 1) {
00693 sinfo_msg_error("qc_ron_xmin1 < 1");
00694 goto cleanup;
00695 }
00696
00697 if(cfg->qc_fpn_xmax1 > naxis1) {
00698 sinfo_msg_error("qc_ron_xmax1 < %d",naxis1);
00699 goto cleanup;
00700 }
00701
00702 if(cfg->qc_fpn_ymin1 < 1) {
00703 sinfo_msg_error("qc_ron_ymin1 < 1");
00704 goto cleanup;
00705 }
00706
00707 if(cfg->qc_fpn_ymax1 > naxis2) {
00708 sinfo_msg_error("qc_ron_ymax1 < %d",naxis2);
00709 goto cleanup;
00710 }
00711
00712
00713 fpn_stdev1 = cpl_image_get_stdev_window(im_dither,
00714 cfg->qc_fpn_xmin1,
00715 cfg->qc_fpn_ymin1,
00716 cfg->qc_fpn_xmax1,
00717 cfg->qc_fpn_ymax1);
00718
00719 if(cfg->qc_fpn_xmin2 < 1) {
00720 sinfo_msg_error("qc_ron_xmin2 < 1");
00721 goto cleanup;
00722 }
00723
00724 if(cfg->qc_fpn_xmax2 > naxis1) {
00725 sinfo_msg_error("qc_ron_xmax2 < %d",naxis1);
00726 goto cleanup;
00727 }
00728
00729 if(cfg->qc_fpn_ymin2 < 1) {
00730 sinfo_msg_error("qc_ron_ymin2 < 1");
00731 goto cleanup;
00732 }
00733
00734 if(cfg->qc_fpn_ymax2 > naxis2) {
00735 sinfo_msg_error("qc_ron_ymax2 < %d",naxis2);
00736 goto cleanup;
00737 }
00738
00739 fpn_stdev2 = cpl_image_get_stdev_window(im_dither,
00740 cfg->qc_fpn_xmin2,
00741 cfg->qc_fpn_ymin2,
00742 cfg->qc_fpn_xmax2,
00743 cfg->qc_fpn_ymax2);
00744
00745
00746 ck0(new_lamp_flats_det_ncounts(raw,cfg),"error computing ncounts");
00747 cknull_nomsg(qclog_tbl = sinfo_qclog_init());
00748 ck0_nomsg(sinfo_qclog_add_double(qclog_tbl,"QC SPECFLAT NCNTSAVG",
00749 qc_lampflat.avg_di,"Average counts","%g"));
00750
00751 ck0_nomsg(sinfo_qclog_add_double(qclog_tbl,"QC SPECFLAT NCNTSSTD",
00752 qc_lampflat.std_di,"Stdev counts","%g"));
00753
00754 ck0_nomsg(sinfo_qclog_add_double(qclog_tbl,"QC SPECFLAT OFFFLUX",
00755 qc_lampflat.avg_of,"Average flux off frames","%g"));
00756
00757 ck0_nomsg(sinfo_qclog_add_double(qclog_tbl,"QC LFLAT FPN1",fpn_stdev1,
00758 "Fixed Pattern Noise of combined frames","%f"));
00759
00760 ck0_nomsg(sinfo_qclog_add_double(qclog_tbl,"QC LFLAT FPN2",fpn_stdev2,
00761 "Fixed Pattern Noise of combined frames","%f"));
00762
00763
00764 ck0(sinfo_pro_save_ima(im_dither,ref_set,sof,outNameDither,
00765 PRO_MASTER_FLAT_LAMP,qclog_tbl,plugin_id,config),
00766 "cannot save ima %s", outNameDither);
00767
00768 sinfo_free_table(&qclog_tbl);
00769 sinfo_free_image(&im_dither);
00770
00771 }
00772
00773
00774
00775 sinfo_free_image_array(&im,cfg->nframes);
00776 sinfo_free_frameset(&raw);
00777 sinfo_flat_free(&cfg);
00778 return 0;
00779
00780 cleanup:
00781
00782
00783
00784 if(slit_edges != NULL) {
00785 for ( i = 0 ; i < 32 ; i++ )
00786 {
00787 if(slit_edges[i] != NULL) {
00788 cpl_free( slit_edges[i] );
00789 }
00790 slit_edges[i]=NULL;
00791 }
00792 cpl_free( slit_edges ) ;
00793 }
00794 sinfo_free_image(&mask_im);
00795 sinfo_free_table(&qclog_tbl);
00796 if(stats) {
00797 sinfo_new_del_Stats(stats);
00798 }
00799 sinfo_free_int(&n);
00800 sinfo_free_image(&threshIm);
00801 sinfo_free_image(&maskImage);
00802 if(imMed != NULL) sinfo_free_image_array(&imMed,cfg->iterations);
00803 if(stats!= NULL) {
00804 sinfo_new_del_Stats(stats);
00805 stats=NULL;
00806 }
00807 sinfo_free_image(&compImage);
00808 sinfo_free_image(&colImage);
00809 sinfo_free_imagelist(&list_dither_object);
00810 sinfo_free_imagelist(&list_dither_sky);
00811 if(list_sky != NULL) {
00812 sinfo_free_imagelist(&list_sky);
00813 }
00814 sinfo_free_imagelist(&list_object);
00815 sinfo_free_image(&im_dither);
00816 sinfo_free_image(&im_dither_sky);
00817 sinfo_free_image(&im_obj);
00818 sinfo_free_image(&im_sky);
00819 if(im != NULL) sinfo_free_image_array(&im,cfg->nframes);
00820 sinfo_free_frameset(&raw);
00821 if(cfg != NULL) {
00822 sinfo_flat_free(&cfg);
00823 }
00824 return -1;
00825
00826 }
00827
00828 static int
00829 new_lamp_flats_det_ncounts(cpl_frameset* raw, flat_config* cfg)
00830 {
00831 int i=0;
00832 int j=0;
00833
00834 int nraw=0;
00835 int non=0;
00836 int noff=0;
00837
00838 double mjd_on=0;
00839 double mjd_of=0;
00840 double mjd_of_frm=0;
00841
00842 char filename[MAX_NAME_SIZE];
00843
00844 cpl_frame* frm=NULL;
00845 cpl_frame* frm_dup=NULL;
00846 cpl_frame* on_frm=NULL;
00847 cpl_frame* of_frm=NULL;
00848 cpl_frame* tmp_of_frm=NULL;
00849
00850
00851 cpl_frameset* on_set=NULL;
00852 cpl_frameset* of_set=NULL;
00853 cpl_frameset* wrk_set=NULL;
00854
00855 on_set=cpl_frameset_new();
00856 of_set=cpl_frameset_new();
00857
00858 nraw = cpl_frameset_get_size(raw);
00859
00860 for (i=0; i< nraw; i++) {
00861 frm = cpl_frameset_get_frame(raw,i);
00862 frm_dup = cpl_frame_duplicate(frm);
00863 if(sinfo_frame_is_on(frm) == 1) {
00864 cpl_frameset_insert(on_set,frm_dup);
00865 non++;
00866 } else {
00867 cpl_frameset_insert(of_set,frm_dup);
00868 noff++;
00869 }
00870 }
00871
00872
00873 if (non == noff) {
00874 new_qc_get_cnt(on_set,of_set,cfg);
00875
00876 } else if (non == 0) {
00877 sinfo_msg("non == 0");
00878 sinfo_msg_warning("QC SPECFLAT NCNTAVG/NCTNTSTD/OFFFLUX=0 ");
00879
00880 } else if ( noff == 0 ) {
00881 sinfo_msg("noff == 0");
00882 sinfo_msg_warning("QC SPECFLAT NCNTAVG/NCTNTSTD/OFFFLUX=0 ");
00883
00884 } else {
00885
00886 sinfo_msg_warning("non != noff, => QC SPECFLAT NCNTAVG/NCTNTSTD/OFFFLUX=0 ");
00887
00888 for (i=0;i<non;i++) {
00889 wrk_set=cpl_frameset_new();
00890 on_frm=cpl_frameset_get_frame(on_set,i);
00891 mjd_on=sinfo_get_mjd_obs(on_frm);
00892 of_frm=cpl_frameset_get_frame(of_set,0);
00893 mjd_of=sinfo_get_mjd_obs(of_frm);
00894 strcpy(filename,cpl_frame_get_filename(of_frm));
00895 for (j=1;j<noff;j++) {
00896 tmp_of_frm = cpl_frameset_get_frame(of_set,j);
00897 mjd_of_frm = sinfo_get_mjd_obs(tmp_of_frm);
00898
00899 if(1000.*(mjd_of_frm-mjd_on)*(mjd_of_frm-mjd_on) <
00900 1000.*(mjd_of- mjd_on)*(mjd_of- mjd_on) ) {
00901 mjd_of=mjd_of_frm;
00902 of_frm=cpl_frame_duplicate(tmp_of_frm);
00903 }
00904 }
00905 strcpy(filename,cpl_frame_get_filename(of_frm));
00906 frm_dup=cpl_frame_duplicate(of_frm);
00907 cpl_frameset_insert(wrk_set,frm_dup);
00908 strcpy(filename,cpl_frame_get_filename(of_frm));
00909 }
00910
00911 new_qc_get_cnt(on_set,wrk_set,cfg);
00912
00913 }
00914
00915 cpl_frameset_delete(wrk_set);
00916 cpl_frameset_delete(on_set);
00917 cpl_frameset_delete(of_set);
00918 return 0;
00919
00920
00921 }
00922
00923 static int
00924 new_qc_get_cnt(cpl_frameset* on_set, cpl_frameset* of_set, flat_config* cfg)
00925 {
00926
00927 int i=0;
00928 int nsat=0;
00929 int non=0;
00930 int nof=0;
00931 int nfr=0;
00932
00933 char name[MAX_NAME_SIZE];
00934 cpl_vector* vec_on=NULL;
00935 cpl_vector* vec_of=NULL;
00936 cpl_vector* vec_di=NULL;
00937 cpl_vector* vec_nsat=NULL;
00938 cpl_frame* on_frm=NULL;
00939 cpl_frame* of_frm=NULL;
00940
00941 cpl_image* dif_ima=NULL;
00942 cpl_image* on_ima=NULL;
00943 cpl_image* of_ima=NULL;
00944 cpl_image* tmp_ima=NULL;
00945
00946 double med=0;
00947 non = cpl_frameset_get_size(on_set);
00948 nof = cpl_frameset_get_size(of_set);
00949 nfr = (non <= nof) ? non : nof;
00950 vec_on = cpl_vector_new(nfr);
00951 vec_of = cpl_vector_new(nfr);
00952 vec_di = cpl_vector_new(nfr);
00953 vec_nsat = cpl_vector_new(nfr);
00954
00955
00956 for (i=0; i< nfr; i++) {
00957 on_frm = cpl_frameset_get_frame(on_set,i);
00958 strcpy(name,cpl_frame_get_filename(on_frm));
00959 on_ima = cpl_image_load(name,CPL_TYPE_FLOAT,0,0);
00960 med= cpl_image_get_median(on_ima);
00961 cpl_vector_set(vec_on,i,med);
00962
00963 tmp_ima = cpl_image_duplicate(on_ima);
00964 cpl_image_threshold(tmp_ima,SINFO_DBL_MIN,
00965 cfg->qc_thresh_max,0,1);
00966 nsat=cpl_image_get_flux(tmp_ima);
00967 cpl_vector_set(vec_nsat,i,nsat);
00968
00969
00970 of_frm = cpl_frameset_get_frame(of_set,i);
00971 strcpy(name,cpl_frame_get_filename(of_frm));
00972 of_ima = cpl_image_load(name,CPL_TYPE_FLOAT,0,0);
00973 med= cpl_image_get_median(of_ima);
00974 cpl_vector_set(vec_of,i,med);
00975 dif_ima = cpl_image_subtract_create(on_ima,of_ima);
00976 med= cpl_image_get_median(dif_ima);
00977 cpl_vector_set(vec_di,i,med);
00978
00979 cpl_image_delete(on_ima);
00980 cpl_image_delete(of_ima);
00981 cpl_image_delete(dif_ima);
00982 cpl_image_delete(tmp_ima);
00983 }
00984 qc_lampflat.avg_on=cpl_vector_get_mean(vec_on);
00985 qc_lampflat.avg_of=cpl_vector_get_mean(vec_of);
00986 qc_lampflat.avg_di=cpl_vector_get_mean(vec_di);
00987 if(nfr > 1 ) {
00988 qc_lampflat.std_on=cpl_vector_get_stdev(vec_on);
00989 qc_lampflat.std_of=cpl_vector_get_stdev(vec_of);
00990 qc_lampflat.std_di=cpl_vector_get_stdev(vec_di);
00991 }
00992 qc_lampflat.nsat=cpl_vector_get_mean(vec_nsat);
00993 cpl_vector_delete(vec_on);
00994 cpl_vector_delete(vec_of);
00995 cpl_vector_delete(vec_di);
00996 cpl_vector_delete(vec_nsat);
00997
00998
00999
01000
01001
01002
01003
01004
01005
01006 return 0;
01007 }
01008