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 #ifdef HAVE_CONFIG_H
00028 #include <config.h>
00029 #endif
00030
00031
00036
00037
00041
00042
00043
00044 #include <string.h>
00045 #include <stdio.h>
00046 #include <xsh_data_pre.h>
00047 #include <xsh_dump.h>
00048 #include <xsh_utils.h>
00049 #include <xsh_error.h>
00050 #include <xsh_msg.h>
00051 #include <xsh_pfits.h>
00052 #include <xsh_dfs.h>
00053 #include <math.h>
00054 #include <time.h>
00055 #include <cpl.h>
00056
00057
00058
00059
00060 static cpl_error_code
00061 xsh_preoverscan_corr(cpl_frame* raw,
00062 const int corr_mode,double* cor_val)
00063 {
00064
00065 const char* fname=NULL;
00066 cpl_image* raw_ima=NULL;
00067 cpl_image* tmp_ima=NULL;
00068
00069 cpl_propertylist* header=NULL;
00070 int ovscx=0;
00071 int ovscy=0;
00072 int prscx=0;
00073 int prscy=0;
00074 int naxis1=0;
00075 int naxis2=0;
00076 double tmp_val=0;
00077
00078
00079 float* ptmp=NULL;
00080 int i=0;
00081 int j=0;
00082 char cor_fname[256];
00083 xsh_msg("Apply pre-overscan correction");
00084 fname=cpl_frame_get_filename(raw);
00085 sprintf(cor_fname,"PREOVER_COR_%s",xsh_get_basename(fname));
00086
00087 header=cpl_propertylist_load(fname,0);
00088
00089 check(raw_ima=cpl_image_load(fname,XSH_PRE_DATA_TYPE, 0, 0));
00090
00091 prscx=xsh_pfits_get_prscx(header);
00092 prscy=xsh_pfits_get_prscy(header);
00093
00094 ovscx=xsh_pfits_get_ovscx(header);
00095 ovscy=xsh_pfits_get_ovscy(header);
00096 naxis1=xsh_pfits_get_naxis1(header);
00097 naxis2=xsh_pfits_get_naxis2(header);
00098 tmp_ima=cpl_image_new(naxis1,naxis2,XSH_PRE_DATA_TYPE);
00099
00100 ptmp=cpl_image_get_data_float(tmp_ima);
00101
00102 switch(corr_mode) {
00103
00104 case 0:
00105 {
00106 *cor_val=0;
00107 break;
00108 }
00109 case 1:
00110 {
00111
00112 *cor_val=cpl_image_get_median_window(raw_ima,
00113 naxis1-ovscx,1,naxis1,naxis2);
00114 xsh_msg("cor_val=%g",*cor_val);
00115 break;
00116 }
00117 case 2:
00118 {
00119
00120 *cor_val=cpl_image_get_median_window(raw_ima,
00121 1,1,ovscx,naxis2);
00122
00123 break;
00124 }
00125 case 3:
00126 {
00127
00128 *cor_val=cpl_image_get_median_window(raw_ima,
00129 naxis1-ovscx,1,naxis1,naxis2);
00130 *cor_val+=cpl_image_get_median_window(raw_ima,
00131 1,1,ovscx,naxis2);
00132 *cor_val*=0.5;
00133
00134 break;
00135 }
00136 case 4:
00137 {
00138
00139
00140
00141
00142
00143
00144
00145 for(j=0;j<naxis2;j++) {
00146 tmp_val=cpl_image_get_median_window(raw_ima,
00147 naxis1-ovscx,j+1,naxis1,j+1);
00148 for(i=0;i<naxis1;i++) {
00149 ptmp[j*naxis1+i]=tmp_val;
00150 }
00151 }
00152 break;
00153 }
00154 case 5:
00155 {
00156
00157 for(j=0;j<naxis2;j++) {
00158 tmp_val=cpl_image_get_median_window(raw_ima,
00159 naxis1-ovscx,j+1,naxis1,j+1);
00160 for(i=0;i<naxis1;i++) {
00161 ptmp[j*naxis1+i]=tmp_val;
00162 }
00163 }
00164 break;
00165 }
00166 case 6:
00167 {
00168
00169
00170
00171 for(j=0;j<naxis2;j++) {
00172 tmp_val=cpl_image_get_median_window(raw_ima,
00173 1,j+1,ovscx,j+1);
00174
00175 tmp_val+=cpl_image_get_median_window(raw_ima,
00176 1,j+1,ovscx,j+1);
00177 for(i=0;i<naxis1;i++) {
00178 ptmp[j*naxis1+i]=tmp_val;
00179 }
00180 }
00181 break;
00182 }
00183 case 7:
00184 {
00185
00186
00187 break;
00188 }
00189 case 8:
00190 {
00191
00192
00193 break;
00194 }
00195 case 9:
00196 {
00197
00198
00199 break;
00200 }
00201 default:
00202 xsh_msg_error("Not supported");
00203 break;
00204 }
00205
00206 if(corr_mode<4) {
00207 cpl_image_subtract_scalar(raw_ima,*cor_val);
00208 } else {
00209 *cor_val=cpl_image_get_median_window(tmp_ima,1,1,naxis1,naxis2);
00210 cpl_image_subtract(raw_ima,tmp_ima);
00211 }
00212 xsh_msg("overscan correction %g",*cor_val);
00213
00214
00215 check(cpl_image_save(raw_ima,cor_fname,CPL_BPP_IEEE_FLOAT,header,CPL_IO_DEFAULT));
00216 cpl_frame_set_filename(raw,cor_fname);
00217
00218
00219 cleanup:
00220
00221 xsh_free_image(&raw_ima);
00222 xsh_free_image(&tmp_ima);
00223 xsh_free_propertylist(&header);
00224
00225 return cpl_error_get_code();
00226 }
00227
00228
00229 static double
00230 xsh_compute_ron_nir(const double dit)
00231 {
00232 double ron=0;
00233 const int nsamples=10;
00234 int i=0;
00235 double dit_min=0;
00236 double dit_max=0;
00237 double ron_min=0;
00238 double ron_max=0;
00239 double slope=0;
00240 int i_min=0;
00241
00242
00243 static double dit_samples[10]={2,4,8,16,32,64,128,256,400,1024};
00244
00245
00246 static double ron_samples[10]={21.3,18.4,14.8,11.7,9.0,7.3,6.3,6.2,7.0,9.0};
00247
00248 for(i=0;i<nsamples;i++){
00249 if(dit_samples[i] < dit) i_min=i;
00250 }
00251
00252 dit_min=dit_samples[i_min];
00253 ron_min=ron_samples[i_min];
00254
00255 dit_max=dit_samples[i_min+1];
00256 ron_max=ron_samples[i_min+1];
00257 slope=(ron_max-ron_min)/(dit_max-dit_min);
00258
00259 ron=ron_min+slope*(dit-dit_min);
00260
00261 return ron;
00262
00263 }
00264
00300
00301 static void xsh_pre_init(xsh_pre* pre, cpl_propertylist* header,
00302 xsh_instrument * instrument)
00303 {
00304 XSH_ARM arm;
00305 double dit;
00306
00307 XSH_ASSURE_NOT_NULL( pre);
00308 XSH_ASSURE_NOT_NULL( header);
00309 XSH_ASSURE_NOT_NULL( pre->instrument);
00310
00311
00312 pre->data_header = header;
00313
00314
00315 arm = xsh_pfits_get_arm( pre->data_header);
00316 if ( cpl_error_get_code() != CPL_ERROR_NONE){
00317 xsh_msg_warning("No ARM keyword found put default");
00318 xsh_error_reset();
00319 check( xsh_pfits_set_arm(pre->data_header, instrument));
00320 }
00321
00322
00323 check( pre->naxis1 = xsh_pfits_get_naxis1(pre->data_header));
00324 check( pre->naxis2 = xsh_pfits_get_naxis2(pre->data_header));
00325 if(cpl_propertylist_has(pre->data_header,"EXPTIME")) {
00326 check( pre->exptime = xsh_pfits_get_exptime(pre->data_header));
00327 }
00328 if( xsh_instrument_get_arm(pre->instrument) == XSH_ARM_NIR){
00329 check(dit= xsh_pfits_get_dit(pre->data_header)) ;
00330
00331
00332 pre->ron=xsh_compute_ron_nir(dit);
00333
00334 pre->conad=2.12;
00335 pre->gain=1./pre->conad;
00336 }else {
00337 check(pre->ron = xsh_pfits_get_ron(pre->data_header));
00338 check(pre->conad = xsh_pfits_get_conad(pre->data_header));
00339 check(pre->gain = xsh_pfits_get_det_gain(pre->data_header));
00340 }
00341 if( xsh_instrument_get_arm(pre->instrument) == XSH_ARM_NIR){
00342 int chip_ny;
00343
00344
00345 pre->binx = 1;
00346 pre->biny = 1;
00347
00348 check(pre->pszx = xsh_pfits_get_det_pxspace(
00349 pre->data_header));
00350
00351
00352 pre->pszx = pre->pszx * 1000000 ;
00353 pre->pszy = pre->pszx;
00354
00355 check( chip_ny = xsh_pfits_get_chip_ny (pre->data_header));
00356 pre->cutx = XSH_NIR_DEFAULT_CUT;
00357
00358
00359
00360
00361
00362 pre->cuty = XSH_NIR_DEFAULT_CUT_Y;
00363 pre->cutmx = XSH_NIR_DEFAULT_CUT_X;
00364 #if 0
00365 pre->cutmy = (chip_ny / 2) - XSH_NIR_DEFAULT_CUT_Y ;
00366 #else
00367
00368 pre->cutmy = XSH_NIR_DEFAULT_CUT_Y ;
00369 #endif
00370
00371 }
00372 else {
00373 pre->binx = xsh_pfits_get_binx( pre->data_header);
00374 pre->biny = xsh_pfits_get_biny( pre->data_header);
00375 if ( cpl_error_get_code() != CPL_ERROR_NONE){
00376 xsh_msg("WARNING : No binning keywords found. Force binning to 1x1");
00377 pre->binx = 1;
00378 pre->biny = 1;
00379 xsh_error_reset();
00380 }
00381
00382 check(pre->pszx = xsh_pfits_get_pszx(pre->data_header));
00383 check(pre->pszy = xsh_pfits_get_pszy(pre->data_header));
00384
00385 pre->pszx = pre->pszx * pre->binx;
00386 pre->pszy = pre->pszy * pre->biny;
00387
00388
00389
00390
00391
00392
00393
00394 pre->cutmx = xsh_pfits_get_ovscx(pre->data_header);
00395 pre->cutx = xsh_pfits_get_prscx(pre->data_header);
00396 pre->cutmy = xsh_pfits_get_ovscy(pre->data_header);
00397 pre->cuty = xsh_pfits_get_prscy(pre->data_header);
00398 xsh_error_reset();
00399 }
00400
00401 instrument->binx = pre->binx ;
00402 instrument->biny = pre->biny ;
00403
00404 cleanup:
00405
00406 return;
00407 }
00408
00409
00431
00432 xsh_pre* xsh_pre_create(cpl_frame* raw,cpl_frame* bpmap, cpl_image* bias_data,
00433 xsh_instrument* instr,const int pre_overscan_corr)
00434 {
00435 xsh_pre* result = NULL;
00436 const char* framename = NULL;
00437 const char* frame_tag = NULL;
00438 int nextensions = 0;
00439 double median_le = 0.0, median_ri = 0.0, median_up = 0.0, median_do = 0.0;
00440 double stdev_le = 0.0, stdev_ri = 0.0,stdev_up = 0.0, stdev_do = 0.0;
00441 cpl_image *image = NULL;
00442 float* errs = NULL;
00443 float* data = NULL;
00444 int i = 0;
00445 const char *bpfilename = NULL;
00446 cpl_propertylist* bplist = NULL, * data_header = NULL;
00447 int bpnaxis1 = 0,bpnaxis2 =0;
00448 float* pdata=NULL;
00449 int nsat=0;
00450 double frac_sat=0;
00451
00452 float sat_thresh=65535;
00453 double cor_val=0;
00454
00455
00456 XSH_ASSURE_NOT_NULL( raw);
00457 XSH_ASSURE_NOT_NULL( instr);
00458
00459
00460 XSH_CALLOC( result, xsh_pre,1);
00461
00462
00463 result->instrument = instr;
00464 XSH_NEW_PROPERTYLIST( result->errs_header);
00465 check( xsh_pfits_set_extname ( result->errs_header, "ERRS"));
00466 XSH_NEW_PROPERTYLIST( result->qual_header);
00467 check( xsh_pfits_set_extname ( result->qual_header, "QUAL"));
00468
00469
00470 check( framename = cpl_frame_get_filename( raw));
00471 check( result->group = cpl_frame_get_group( raw));
00472 check( frame_tag = cpl_frame_get_tag( raw));
00473
00474
00475 check(nextensions = cpl_frame_get_nextensions(raw));
00476
00477
00478 XSH_ASSURE_NOT_ILLEGAL(nextensions == 0);
00479
00480
00481 check_msg(data_header = cpl_propertylist_load(framename, 0),
00482 "Could not load header of file '%s'", framename);
00483
00484 if(pre_overscan_corr>0) {
00485 check(xsh_preoverscan_corr(raw,pre_overscan_corr,&cor_val));
00486 check( framename = cpl_frame_get_filename( raw));
00487 }
00488 check(xsh_pre_init(result,data_header, instr));
00489
00490
00491 if (strstr( frame_tag, "AFC") != NULL){
00492 check( xsh_pfits_set_dpr_tech ( data_header, XSH_DPR_TECH_SINGLE_PINHOLE));
00493 if ( xsh_instrument_get_arm(instr) != XSH_ARM_NIR){
00494 result->cutmx = 0;
00495 result->cutmy = 0;
00496 result->cutx = 0;
00497 result->cuty = 0;
00498 }
00499 }
00500
00501 result->nx = result->naxis1 - result->cutmx - result->cutx;
00502 result->ny = result->naxis2 - result->cutmy - result->cuty;
00503
00504
00505 if( xsh_instrument_get_arm(instr) == XSH_ARM_NIR){
00506
00507 XSH_ASSURE_NOT_ILLEGAL(result->nx > 0 && result->ny > 0);
00508
00509
00510 check(image = cpl_image_load(framename, XSH_PRE_DATA_TYPE, 0, 0));
00511
00512
00513
00514
00515
00516
00517 xsh_msg_dbg_medium("extracting region [%d,%d:%d,%d]",
00518 result->cutx+1, result->cuty+1,
00519 result->naxis1-XSH_NIR_DEFAULT_CUT,
00520 result->naxis2-result->cutmy-XSH_NIR_DEFAULT_CUT);
00521
00522 check( result->data = cpl_image_extract( image,
00523 result->cutx+1, result->cuty+1,
00524 result->naxis1-XSH_NIR_DEFAULT_CUT,
00525 result->naxis2-result->cutmy-XSH_NIR_DEFAULT_CUT));
00526
00527 check(cpl_image_turn(result->data,1));
00528 result->nx = cpl_image_get_size_x( result->data ) ;
00529 result->ny = cpl_image_get_size_y( result->data ) ;
00530 xsh_msg_dbg_low( "***** NX,NY: %d,%d", result->nx, result->ny ) ;
00531
00532
00533 check(result->errs = cpl_image_duplicate(result->data));
00534 check(errs = cpl_image_get_data_float(result->errs));
00535 check(data = cpl_image_get_data_float(result->data));
00536
00537 for(i = 0; i < result->nx*result->ny; i++) {
00538
00539 errs[i] = sqrt(fabs(data[i] * result->conad)
00540 + result->ron * result->ron) / result->conad;
00541 }
00542
00543
00544 pdata=cpl_image_get_data_float(result->data);
00545 for(i=0;i<result->nx*result->ny;i++) {
00546 if(pdata[i]>=(sat_thresh-cor_val) || pdata[i]==-cor_val) nsat++;
00547 }
00548 frac_sat=(double)nsat/(result->nx*result->ny);
00549
00550
00551 check(xsh_pfits_set_nsat(result->data_header, nsat));
00552 check(xsh_pfits_set_frac_sat(result->data_header, frac_sat));
00553
00554 }
00555
00556 else {
00557 XSH_ASSURE_NOT_ILLEGAL(result->nx > 0 && result->ny > 0);
00558
00559 xsh_msg("framename=%s",framename);
00560 check(image = cpl_image_load(framename, XSH_PRE_DATA_TYPE, 0, 0));
00561
00562
00563 if (result->cutx > 0) {
00564 check(median_le = cpl_image_get_median_window(image, 1, 1,
00565 result->cutx, result->naxis2));
00566 check(stdev_le = cpl_image_get_stdev_window(image, 1, 1,
00567 result->cutx, result->naxis2));
00568 }
00569 if (result->cuty > 0) {
00570 check(median_do = cpl_image_get_median_window(image, 1, 1,
00571 result->naxis1, result->cuty));
00572 check(stdev_do = cpl_image_get_stdev_window(image, 1, 1,
00573 result->naxis1, result->cuty));
00574 }
00575 if (result->cutmx > 0) {
00576 check(median_ri =cpl_image_get_median_window(image,
00577 result->naxis1 - result->cutmx + 1, 1, result->naxis1,
00578 result->naxis2));
00579 check(stdev_ri = cpl_image_get_stdev_window(image,
00580 result->naxis1 - result->cutmx + 1, 1, result->naxis1,
00581 result->naxis2));
00582 }
00583 if (result->cutmy > 0) {
00584 check(median_up =cpl_image_get_median_window(image, 1,
00585 result->naxis2 - result->cutmy + 1, result->naxis1, result->naxis2));
00586 check(stdev_ri = cpl_image_get_stdev_window(image, 1,
00587 result->naxis2 - result->cutmy + 1, result->naxis1, result->naxis2));
00588 }
00589
00590
00591 check(xsh_pfits_set_bias_left_median(result->data_header, median_le));
00592 check(xsh_pfits_set_bias_right_median(result->data_header, median_ri));
00593 check(xsh_pfits_set_bias_left_stdev(result->data_header, stdev_le));
00594 check(xsh_pfits_set_bias_right_stdev(result->data_header, stdev_ri));
00595 check(xsh_pfits_set_bias_up_median(result->data_header, median_up));
00596 check(xsh_pfits_set_bias_down_median(result->data_header, median_do));
00597 check(xsh_pfits_set_bias_up_stdev(result->data_header, stdev_up));
00598 check(xsh_pfits_set_bias_down_stdev(result->data_header, stdev_do));
00599
00600
00601 check(result->data = cpl_image_extract(image, result->cutx + 1,
00602 result->cuty + 1, result->naxis1 - result->cutmx, result->naxis2 -
00603 result->cutmy));
00604
00605
00606 pdata=cpl_image_get_data_float(result->data);
00607 for(i=0;i<result->nx*result->ny;i++) {
00608 if(pdata[i]>=(sat_thresh-cor_val) || pdata[i]==-cor_val) nsat++;
00609 }
00610 frac_sat=(double)nsat/(result->nx*result->ny);
00611
00612 if( xsh_instrument_get_arm(instr) == XSH_ARM_UVB){
00613
00614 check( cpl_image_turn(result->data,2));
00615 }
00616 check(xsh_pfits_set_nsat(result->data_header, nsat));
00617 check(xsh_pfits_set_frac_sat(result->data_header, frac_sat));
00618
00619
00620 check( result->errs = cpl_image_duplicate(result->data));
00621 check( errs = cpl_image_get_data_float(result->errs));
00622 check( data = cpl_image_get_data_float(result->data));
00623
00624
00625 if ( (strcmp(frame_tag, XSH_BIAS_UVB) == 0) ||
00626 (strcmp(frame_tag, XSH_BIAS_VIS) == 0) ){
00627
00628
00629 float ron_div_conad = result->ron / result->conad;
00630
00631 for(i = 0; i < result->nx*result->ny; i++) {
00632 errs[i] = ron_div_conad;
00633 }
00634
00635 }
00636 else if ( (strcmp(frame_tag, XSH_RAW_IMA_SLIT_UVB) == 0) ||
00637 (strcmp(frame_tag, XSH_RAW_IMA_SLIT_VIS) == 0) ){
00638
00639
00640 float ron_div_conad = result->ron / result->conad;
00641
00642 for(i = 0; i < result->nx*result->ny; i++) {
00643 errs[i] = ron_div_conad;
00644 }
00645
00646 }
00647
00648 else if ( (strcmp(frame_tag, XSH_LINEARITY_UVB) == 0) ||
00649 (strcmp(frame_tag, XSH_LINEARITY_VIS) == 0) ||
00650 (strcmp(frame_tag, XSH_RAW_BP_MAP_NL_UVB) == 0) ||
00651 (strcmp(frame_tag, XSH_RAW_BP_MAP_NL_VIS) == 0) ||
00652 (strcmp(frame_tag, XSH_BP_MAP_NL_UVB) == 0) ||
00653 (strcmp(frame_tag, XSH_BP_MAP_NL_VIS) == 0) ){
00654
00655 for(i = 0; i < result->nx*result->ny; i++) {
00656 errs[i] = 0;
00657 }
00658 }
00659 else {
00660
00661 int bias_x, bias_y;
00662 float* mbias_data = NULL;
00663
00664 if ( bias_data == (cpl_image *)-1 ) {
00665 xsh_msg( "No BIAS, just a Test" ) ;
00666 for ( i = 0 ; i < result->nx*result->ny; i++) {
00667 errs[i] = 0;
00668 }
00669 }
00670 else {
00671 XSH_ASSURE_NOT_NULL( bias_data);
00672 check( bias_x = cpl_image_get_size_x( bias_data));
00673 check( bias_y = cpl_image_get_size_y( bias_data));
00674 xsh_msg_dbg_medium("bias_x=%d nx=%d bias_y=%d ny=%d",
00675 bias_x,result->nx,bias_y,result->ny);
00676 XSH_ASSURE_NOT_ILLEGAL_MSG( bias_x == result->nx,
00677 "Master bias X size different from raw frame X size. Input image frames must match in arm and bin!");
00678 XSH_ASSURE_NOT_ILLEGAL_MSG( bias_y == result->ny,
00679 "Master bias Y size different from raw frame Y size. Input image frames must match in arm and bin!");
00680
00681 if(pre_overscan_corr==0) {
00682 check( mbias_data = cpl_image_get_data_float( bias_data));
00683
00684 for(i = 0; i < result->nx*result->ny; i++) {
00685
00686 errs[i] = sqrt( fabs( data[i] - mbias_data[i])
00687 * result->conad + result->ron * result->ron)
00688 / result->conad;
00689 }
00690 } else {
00691 for(i = 0; i < result->nx*result->ny; i++) {
00692
00693 errs[i] = sqrt( fabs( data[i])
00694 * result->conad + result->ron * result->ron)
00695 / result->conad;
00696 }
00697 }
00698 }
00699 }
00700 }
00701
00702
00703
00704 if (bpmap == NULL) {
00705 xsh_msg("No input BP map provided");
00706 check(result->qual = cpl_image_new(result->nx, result->ny,
00707 XSH_PRE_QUAL_TYPE));
00708
00709 }
00710 else {
00711 check(bpfilename = cpl_frame_get_filename(bpmap));
00712 xsh_msg("Use Bad pixel map file %s\n",bpfilename);
00713 check(bplist = cpl_propertylist_load(bpfilename, 0));
00714
00715 check(bpnaxis1 = xsh_pfits_get_naxis1(bplist));
00716 check(bpnaxis2 = xsh_pfits_get_naxis2(bplist));
00717 assure(bpnaxis1 == result->nx && bpnaxis2 == result->ny,
00718 CPL_ERROR_ILLEGAL_INPUT,"qual image %d %d must have same size of data\
00719 image %d %d",bpnaxis1,bpnaxis2,result->nx,result->ny);
00720 check(result->qual = cpl_image_load(bpfilename, XSH_PRE_QUAL_TYPE, 0, 0));
00721 }
00722
00723 cleanup:
00724 if (cpl_error_get_code () != CPL_ERROR_NONE) {
00725 xsh_pre_free (&result);
00726 result = NULL;
00727 }
00728 xsh_free_image(&image);
00729 xsh_free_propertylist(&bplist);
00730
00731 return result;
00732 }
00733
00734
00740
00741 void xsh_pre_free(xsh_pre** pre)
00742 {
00743 if (pre && *pre) {
00744 xsh_free_image(&((*pre)->data));
00745 xsh_free_propertylist(&((*pre)->data_header));
00746 xsh_free_image(&((*pre)->errs));
00747 xsh_free_propertylist(&((*pre)->errs_header));
00748 xsh_free_image(&((*pre)->qual));
00749 xsh_free_propertylist(&((*pre)->qual_header));
00750 cpl_free(*pre);
00751 *pre = NULL;
00752 }
00753 }
00754
00755
00766
00767 xsh_pre* xsh_pre_load (cpl_frame * frame, xsh_instrument* instr)
00768 {
00769
00770 xsh_pre * result = NULL;
00771
00772 const char *filename = NULL;
00773 int extension = 0;
00774 cpl_propertylist* data_header = NULL;
00775
00776
00777
00778 XSH_ASSURE_NOT_NULL(frame);
00779 XSH_ASSURE_NOT_NULL(instr);
00780 XSH_ASSURE_NOT_NULL(cpl_frame_get_tag (frame));
00781 check (filename = cpl_frame_get_filename (frame));
00782
00783 XSH_CALLOC(result, xsh_pre,1);
00784
00785 result->instrument = instr;
00786 check(result->group = cpl_frame_get_group(frame));
00787
00788
00789 XSH_ASSURE_NOT_ILLEGAL(cpl_frame_get_nextensions(frame) == 2);
00790
00791
00792 extension = 0;
00793 check_msg (data_header = cpl_propertylist_load (filename,extension),
00794 "Cannot read the FITS header from '%s' extension %d",
00795 filename, extension);
00796 check(xsh_pre_init(result,data_header, instr));
00797
00798 result->nx = result->naxis1;
00799 result->ny = result->naxis2;
00800
00801
00802 check_msg (result->data = cpl_image_load (filename,XSH_PRE_DATA_TYPE,0,0),
00803 "Error loading image from %s extension 0", filename);
00804
00805
00806 check_msg (result->errs_header = cpl_propertylist_load (filename,1),
00807 "Cannot read the FITS header from '%s' extension 1",filename);
00808 assure(strcmp(xsh_pfits_get_extname(result->errs_header),"ERRS") == 0,
00809 CPL_ERROR_ILLEGAL_INPUT,"extension 1 must be a errs extension");
00810 check_msg (result->errs = cpl_image_load (filename, XSH_PRE_ERRS_TYPE,0,1),
00811 "Error loading image from %s extension 1", filename);
00812
00813 check_msg (result->qual_header = cpl_propertylist_load (filename,2),
00814 "Cannot read the FITS header from '%s' extension 2",filename);
00815 assure(strcmp("QUAL",xsh_pfits_get_extname (result->qual_header)) == 0,
00816 CPL_ERROR_ILLEGAL_INPUT,"extension 2 must be a qual extension");
00817 check_msg (result->qual = cpl_image_load (filename, XSH_PRE_QUAL_TYPE,0,2),
00818 "Error loading image from %s extension 2", filename);
00819
00820
00821
00822
00823
00824
00825
00826
00827
00828
00829
00830
00831
00832
00833
00834
00835
00836
00837
00838
00839
00840
00841
00842
00843
00844
00845
00846
00847
00848
00849
00850
00851
00852
00853
00854 cleanup:
00855 if (cpl_error_get_code () != CPL_ERROR_NONE) {
00856 xsh_error_msg("can't load frame %s",cpl_frame_get_filename(frame));
00857 xsh_pre_free(&result);
00858 }
00859 return result;
00860 }
00861
00862
00868
00869 xsh_pre * xsh_pre_duplicate (const xsh_pre * pre)
00870 {
00871 xsh_pre *result = NULL;
00872
00873 assure(pre != NULL, CPL_ERROR_NULL_INPUT, "Null PRE");
00874
00875
00876 result = (xsh_pre*)(cpl_malloc (sizeof (xsh_pre)));
00877 assure (result != NULL, CPL_ERROR_ILLEGAL_OUTPUT,
00878 "Memory allocation failed!");
00879
00880
00881 result->data = NULL;
00882 result->data_header = NULL;
00883 result->errs = NULL;
00884 result->errs_header = NULL;
00885 result->qual = NULL;
00886 result->qual_header = NULL;
00887 result->instrument = pre->instrument;
00888
00889
00890 check(result->nx = xsh_pre_get_nx(pre));
00891 check(result->ny = xsh_pre_get_ny(pre));
00892 check(result->group = xsh_pre_get_group(pre));
00893 result->pszx = pre->pszx;
00894 result->pszy = pre->pszy;
00895 result->gain = pre->gain;
00896 result->exptime = pre->exptime;
00897
00898 check_msg(result->data_header = cpl_propertylist_duplicate(
00899 pre->data_header),"can't copy data header");
00900 check_msg(result->data = cpl_image_duplicate(pre->data),
00901 "can't copy data image");
00902
00903
00904 check_msg(result->errs_header = cpl_propertylist_duplicate(
00905 pre->errs_header),"can't copy errs header");
00906 check_msg(result->errs = cpl_image_duplicate(pre->errs),
00907 "can't copy errs image");
00908
00909
00910 check_msg(result->qual_header = cpl_propertylist_duplicate(
00911 pre->qual_header),"can't copy qual header");
00912
00913 check_msg(result->qual = cpl_image_duplicate(pre->qual),
00914 "can't copy qual image");
00915
00916 cleanup:
00917 if (cpl_error_get_code () != CPL_ERROR_NONE) {
00918 xsh_pre_free(&result);
00919 result = NULL;
00920 }
00921 return result;
00922 }
00923
00924
00933
00934 cpl_frame* xsh_pre_save (const xsh_pre * pre,
00935 const char *filename,
00936 const char *tag,
00937 int temp )
00938 {
00939 cpl_frame *product_frame = NULL;
00940
00941 XSH_ASSURE_NOT_NULL(pre);
00942 XSH_ASSURE_NOT_NULL(filename);
00943
00944
00945 check( xsh_pfits_set_pcatg( pre->data_header,tag));
00946 check_msg (cpl_image_save (pre->data, filename, XSH_PRE_DATA_BPP,
00947 pre->data_header, CPL_IO_DEFAULT),
00948 "Could not save data to %s extension 0", filename);
00949 check_msg (cpl_image_save (pre->errs, filename, XSH_PRE_ERRS_BPP,
00950 pre->errs_header, CPL_IO_EXTEND),
00951 "Could not save errs to %s extension 1", filename);
00952 check_msg (cpl_image_save (pre->qual, filename, XSH_PRE_QUAL_BPP,
00953 pre->qual_header, CPL_IO_EXTEND),
00954 "Could not save qual to %s extension 2", filename);
00955
00956 product_frame = cpl_frame_new ();
00957 XSH_ASSURE_NOT_NULL( product_frame);
00958
00959 check ((cpl_frame_set_filename (product_frame, filename),
00960 cpl_frame_set_tag(product_frame,tag),
00961 cpl_frame_set_type (product_frame, CPL_FRAME_TYPE_IMAGE),
00962 cpl_frame_set_group (product_frame, xsh_pre_get_group(pre)) )) ;
00963
00964 if ( temp != 0 ) {
00965 check( cpl_frame_set_level(product_frame,
00966 CPL_FRAME_LEVEL_TEMPORARY));
00967 xsh_add_temporary_file( filename ) ;
00968 }
00969
00970 cleanup:
00971 if (cpl_error_get_code () != CPL_ERROR_NONE) {
00972 xsh_free_frame(&product_frame);
00973 product_frame = NULL;
00974 }
00975 return product_frame;
00976 }
00977
00978
00990
00991
00992 cpl_frame*
00993 xsh_pre_save_product(xsh_pre *pre,
00994 cpl_frame* frame,
00995 cpl_frameset* frameset,
00996 const cpl_parameterlist* parameters,
00997 const char* recipe_id,
00998 const char *prefix)
00999 {
01000 cpl_frame* result = NULL;
01001 char filename[256];
01002 time_t now ;
01003 char* date = NULL;
01004 const char* tag=NULL;
01005
01006 XSH_ASSURE_NOT_NULL( pre);
01007 XSH_ASSURE_NOT_NULL( frame);
01008 XSH_ASSURE_NOT_NULL( parameters);
01009 XSH_ASSURE_NOT_NULL( recipe_id);
01010 XSH_ASSURE_NOT_NULL( pre->instrument);
01011
01012
01013 check( cpl_frame_set_group (frame, CPL_FRAME_GROUP_PRODUCT));
01014 #if defined CPL_VERSION_CODE && CPL_VERSION_CODE >= CPL_VERSION(4, 8, 0)
01015 check (cpl_dfs_setup_product_header (pre->data_header,
01016 frame, frameset, parameters,
01017 recipe_id, pre->instrument->pipeline_id,
01018 pre->instrument->dictionary,NULL));
01019 #else
01020 check (cpl_dfs_setup_product_header (pre->data_header,
01021 frame, frameset, parameters,
01022 recipe_id, pre->instrument->pipeline_id,
01023 pre->instrument->dictionary));
01024 #endif
01025
01026 if ( prefix == NULL ) {
01027 sprintf(filename,"PRODUCT_%s",cpl_frame_get_filename (frame));
01028 }
01029 else if ( xsh_time_stamp_get() ) {
01030 time( &now ) ;
01031 date = xsh_sdate_utc(&now);
01032 sprintf( filename, "%s_%dx%d_%s.fits", prefix, pre->binx, pre->biny,
01033 date);
01034 }
01035 else
01036 sprintf( filename, "%s_%dx%d.fits", prefix, pre->binx, pre->biny );
01037 check(tag=xsh_pfits_get_pcatg(pre->data_header));
01038 check(result = xsh_pre_save(pre, filename, tag,0));
01039
01040 cpl_frame_set_type (result, CPL_FRAME_TYPE_IMAGE),
01041 cpl_frame_set_group (result, CPL_FRAME_GROUP_PRODUCT),
01042 cpl_frame_set_level (result, CPL_FRAME_LEVEL_FINAL);
01043 cleanup:
01044 if (cpl_error_get_code () != CPL_ERROR_NONE) {
01045 xsh_free_frame(&result);
01046 result = NULL;
01047 }
01048 XSH_FREE( date ) ;
01049
01050 return result;
01051 }
01052
01053
01066
01067 xsh_pre* xsh_pre_new (int nx, int ny)
01068 {
01069 xsh_pre *result = NULL;
01070
01071 assure (nx > 0 && ny > 0, CPL_ERROR_ILLEGAL_INPUT,
01072 "Illegal image size: %dx%d", nx, ny);
01073
01074 XSH_CALLOC( result, xsh_pre, 1);
01075
01076 result->nx = nx;
01077 result->ny = ny;
01078
01079 check( result->data = cpl_image_new (nx, ny, XSH_PRE_DATA_TYPE));
01080 check( result->errs = cpl_image_new (nx, ny, XSH_PRE_ERRS_TYPE));
01081 check( result->qual = cpl_image_new (nx, ny, XSH_PRE_QUAL_TYPE));
01082 check( result->data_header = cpl_propertylist_new());
01083 check( result->errs_header = cpl_propertylist_new());
01084 check( result->qual_header = cpl_propertylist_new());
01085
01086 cleanup:
01087 if (cpl_error_get_code () != CPL_ERROR_NONE) {
01088 xsh_pre_free ( &result);
01089 result = NULL;
01090 }
01091 return result;
01092 }
01093
01094
01100
01101 int xsh_pre_get_nx (const xsh_pre * pre)
01102 {
01103 int result = 0;
01104
01105 XSH_ASSURE_NOT_NULL( pre );
01106 result = pre->nx;
01107
01108 cleanup:
01109 return result;
01110 }
01111
01112
01118
01119 int xsh_pre_get_ny (const xsh_pre * pre)
01120 {
01121 int result = 0;
01122
01123 XSH_ASSURE_NOT_NULL( pre );
01124 result = pre->ny;
01125
01126 cleanup:
01127 return result;
01128 }
01129
01130
01131
01137
01138 int xsh_pre_get_binx (const xsh_pre * pre)
01139 {
01140 int result = 0;
01141
01142 XSH_ASSURE_NOT_NULL( pre );
01143 result = pre->binx;
01144
01145 cleanup:
01146 return result;
01147 }
01148
01149
01155
01156 int xsh_pre_get_biny (const xsh_pre * pre)
01157 {
01158 int result = 0;
01159
01160 XSH_ASSURE_NOT_NULL( pre );
01161 result = pre->biny;
01162
01163 cleanup:
01164 return result;
01165 }
01166
01167
01173
01174 float xsh_pre_get_pszx (const xsh_pre * pre)
01175 {
01176 float result = 0.0;
01177 assure (pre != NULL, CPL_ERROR_NULL_INPUT, "Null PRE");
01178 result = pre->pszx;
01179
01180 cleanup:
01181 return result;
01182 }
01183
01184
01193
01194
01195 void xsh_pre_from_raw_get(xsh_pre* pre, double raw_x, double raw_y,
01196 double* x, double* y)
01197 {
01198
01199 XSH_ASSURE_NOT_NULL(pre);
01200 XSH_ASSURE_NOT_NULL(x);
01201 XSH_ASSURE_NOT_NULL(y);
01202
01203
01204
01205
01206
01207
01208 if( xsh_instrument_get_arm(pre->instrument) == XSH_ARM_NIR){
01210 *x = raw_y ;
01211 *y = pre->ny + pre->cutx - raw_x;
01212 }
01213 else{
01214 *x = raw_x - pre->cutx;
01215 *y = raw_y - pre->cuty;
01216 }
01217
01218 cleanup:
01219 return;
01220 }
01221
01222
01228
01229 float xsh_pre_get_pszy (const xsh_pre * pre)
01230 {
01231 float result = 0.0;
01232
01233 XSH_ASSURE_NOT_NULL(pre);
01234 result = pre->pszy;
01235
01236 cleanup:
01237 return result;
01238 }
01239
01240
01246
01247 float xsh_pre_get_gain (const xsh_pre * pre)
01248 {
01249 float result = 0.0;
01250
01251 XSH_ASSURE_NOT_NULL(pre);
01252 result = pre->gain;
01253
01254 cleanup:
01255 return result;
01256 }
01257
01263
01264 cpl_frame_group xsh_pre_get_group (const xsh_pre * pre)
01265 {
01266 cpl_frame_group result = CPL_FRAME_GROUP_NONE;
01267
01268 XSH_ASSURE_NOT_NULL(pre);
01269 result = pre->group;
01270
01271 cleanup:
01272 return result;
01273 }
01274
01280
01281 cpl_mask* xsh_pre_get_bpmap (const xsh_pre * pre)
01282 {
01283 int* qual = NULL;
01284 cpl_mask* mask = NULL;
01285 cpl_binary* binary = NULL;
01286 int i = 0;
01287
01288 XSH_ASSURE_NOT_NULL(pre);
01289
01290 check(mask = cpl_image_get_bpm(pre->qual));
01291 check(qual = cpl_image_get_data_int(pre->qual));
01292 check(binary = cpl_mask_get_data(mask));
01293
01294 for (i = 0; i < pre->nx* pre->ny; i++) {
01295 if (qual[i] >= XSH_GOOD_PIXEL_LEVEL) {
01296 binary[i] = CPL_BINARY_1;
01297 }
01298 }
01299
01300 cleanup:
01301 return mask;
01302 }
01303
01304
01324
01325
01326 void xsh_pre_add (xsh_pre* self, const xsh_pre * right)
01327 {
01328 float* errs1 = NULL;
01329 float* errs2 = NULL;
01330 int i = 0;
01331 cpl_mask* mask = NULL;
01332 cpl_binary* binary =NULL;
01333 int* qual = NULL;
01334
01335 XSH_ASSURE_NOT_NULL(self);
01336 XSH_ASSURE_NOT_NULL(right);
01337
01338
01339 assure (xsh_pre_get_nx (self) == xsh_pre_get_nx (right) &&
01340 xsh_pre_get_ny (self) == xsh_pre_get_ny (right),
01341 CPL_ERROR_INCOMPATIBLE_INPUT,
01342 "Image sizes don't match: %dx%d and %dx%d",
01343 xsh_pre_get_nx (self), xsh_pre_get_ny (self),
01344 xsh_pre_get_nx (right), xsh_pre_get_ny (right));
01345
01346
01347 check (cpl_image_add(self->data, right->data));
01348
01349
01350 check(errs1 = cpl_image_get_data_float(self->errs));
01351 check(errs2 = cpl_image_get_data_float(right->errs));
01352
01353 for(i=0;i< self->nx*self->ny; i++){
01354 errs1[i] = sqrt(pow(errs1[i],2)+pow(errs2[i],2));
01355 }
01356
01357
01358
01359
01360 if( (xsh_pre_get_group(right) == CPL_FRAME_GROUP_CALIB) ){
01361
01362 check( mask = xsh_pre_get_bpmap(right));
01363 check(binary = cpl_mask_get_data(mask));
01364 check(qual = cpl_image_get_data_int(self->qual));
01365 for(i=0;i<self->nx*self->ny;i++){
01366 if (binary[i]){
01367 qual[i] |= QFLAG_CALIB_FILE_DEFECT;
01368 }
01369 }
01370 }
01371
01372 else {
01373
01374
01375
01376
01377 xsh_badpixelmap_or( self, right ) ;
01378 }
01379 cleanup:
01380 return;
01381 }
01382
01383
01400 cpl_frameset* xsh_pre_frameset_subtract_frame( cpl_frameset *set,
01401 cpl_frame *sub,
01402 const char* spec,
01403 xsh_instrument *instr )
01404 {
01405 int i=0;
01406 int size=0;
01407
01408 cpl_frame* frm=NULL;
01409 cpl_frame* cor=NULL;
01410 cpl_frameset* result=NULL;
01411 char filename[80];
01412 const char * name=NULL;
01413 size=cpl_frameset_get_size(set);
01414 result=cpl_frameset_new();
01415 for(i=0;i<size;i++) {
01416 frm=cpl_frameset_get_frame(set,i);
01417 name=cpl_frame_get_filename(frm);
01418 sprintf(filename,"SUB_%s_%d_%s",spec,i,name);
01419 cor=xsh_pre_frame_subtract(frm,sub,filename,instr);
01420 cpl_frameset_insert(result,cor);
01421 }
01422
01423 return result;
01424
01425 }
01426
01427
01444 cpl_frame* xsh_pre_frame_subtract( cpl_frame *one,
01445 cpl_frame *two,
01446 const char *filename,
01447 xsh_instrument *instr )
01448 {
01449 xsh_pre * pre1 = NULL ;
01450 xsh_pre * pre2 = NULL ;
01451 xsh_pre * pre_result = NULL ;
01452 cpl_frame * result = NULL ;
01453 const char* tag=NULL;
01454
01455
01456 XSH_ASSURE_NOT_NULL ( one);
01457 XSH_ASSURE_NOT_NULL ( two);
01458 XSH_ASSURE_NOT_NULL ( filename);
01459 XSH_ASSURE_NOT_NULL ( instr);
01460
01461
01462 check ( pre1 = xsh_pre_load( one, instr));
01463 check ( pre2 = xsh_pre_load( two, instr));
01464
01465
01466 check( pre_result = xsh_pre_duplicate( pre1));
01467 check ( xsh_pre_subtract( pre_result, pre2));
01468
01469 check(tag=cpl_frame_get_tag(one));
01470
01471 check( result = xsh_pre_save( pre_result, filename,tag,1));
01472
01473
01474 check ((cpl_frame_set_filename (result, filename),
01475 cpl_frame_set_type (result, CPL_FRAME_TYPE_IMAGE),
01476 cpl_frame_set_group (result, xsh_pre_get_group(pre_result)),
01477 cpl_frame_set_level (result, CPL_FRAME_LEVEL_TEMPORARY),
01478 cpl_frame_set_tag( result,tag)));
01479 xsh_add_temporary_file( filename ) ;
01480
01481 cleanup:
01482 xsh_pre_free( &pre1);
01483 xsh_pre_free( &pre2);
01484 xsh_pre_free( &pre_result);
01485 return result;
01486 }
01487
01488
01501
01502
01503 void xsh_pre_subtract (xsh_pre* self, const xsh_pre * right)
01504 {
01505
01506 float* errs1 = NULL;
01507 float* errs2 = NULL;
01508 int i = 0;
01509 cpl_mask* mask = NULL;
01510 cpl_binary* binary =NULL;
01511 int* qual = NULL;
01512
01513
01514 XSH_ASSURE_NOT_NULL( self);
01515 XSH_ASSURE_NOT_NULL( right);
01516
01517
01518 assure (xsh_pre_get_nx (self) == xsh_pre_get_nx (right) &&
01519 xsh_pre_get_ny (self) == xsh_pre_get_ny (right),
01520 CPL_ERROR_INCOMPATIBLE_INPUT,
01521 "Image sizes don't match: %dx%d and %dx%d",
01522 xsh_pre_get_nx (self), xsh_pre_get_ny (self),
01523 xsh_pre_get_nx (right), xsh_pre_get_ny (right));
01524
01525
01526 check ( cpl_image_subtract( self->data, right->data));
01527
01528
01529 check( errs1 = cpl_image_get_data_float( self->errs));
01530 check( errs2 = cpl_image_get_data_float( right->errs));
01531
01532 for(i=0;i< self->nx*self->ny; i++){
01533 errs1[i] = sqrt(pow(errs1[i],2)+pow(errs2[i],2));
01534 }
01535
01536
01537 if( (xsh_pre_get_group(right) == CPL_FRAME_GROUP_CALIB) ){
01538 check( mask = xsh_pre_get_bpmap(right));
01539 check(binary = cpl_mask_get_data(mask));
01540 check(qual = cpl_image_get_data_int(self->qual));
01541 for(i=0;i<self->nx*self->ny;i++){
01542 if (binary[i]){
01543 qual[i] |= QFLAG_CALIB_FILE_DEFECT;
01544 }
01545 }
01546 }
01547 else {
01548 xsh_badpixelmap_or( self, right ) ;
01549 }
01550
01551 cleanup:
01552 return ;
01553 }
01554
01555
01575
01576 void xsh_pre_divide (xsh_pre* self, const xsh_pre * right, double threshold)
01577 {
01578
01579 float *errs1 = NULL;
01580 float *errs2 = NULL;
01581 float *data1 = NULL;
01582 float *data2 = NULL;
01583 int *qual1 = NULL;
01584 int *qual2 = NULL;
01585 int i = 0;
01586
01587 assure (self != NULL, CPL_ERROR_NULL_INPUT, "Null image!");
01588 assure (right != NULL, CPL_ERROR_NULL_INPUT, "Null image!");
01589
01590
01591 assure (xsh_pre_get_nx (self) == xsh_pre_get_nx (right) &&
01592 xsh_pre_get_ny (self) == xsh_pre_get_ny (right),
01593 CPL_ERROR_INCOMPATIBLE_INPUT,
01594 "Image sizes don't match: %dx%d and %dx%d",
01595 xsh_pre_get_nx (self), xsh_pre_get_ny (self),
01596 xsh_pre_get_nx (right), xsh_pre_get_ny (right));
01597
01598
01599
01600 check(data1 = cpl_image_get_data_float(self->data));
01601 check(data2 = cpl_image_get_data_float(right->data));
01602 check(errs1 = cpl_image_get_data_float(self->errs));
01603 check(errs2 = cpl_image_get_data_float(right->errs));
01604 check(qual1 = cpl_image_get_data_int(self->qual));
01605 check(qual2 = cpl_image_get_data_int(right->qual));
01606
01607 for(i=0;i< self->nx*self->ny; i++){
01608
01609 if ( qual2[i] > XSH_GOOD_PIXEL_LEVEL ) {
01610 if( (xsh_pre_get_group(right) == CPL_FRAME_GROUP_CALIB) ){
01611 qual1[i] |= QFLAG_CALIB_FILE_DEFECT;
01612 }
01613 else {
01614 qual1[i] |= qual2[i];
01615 }
01616 }
01617 else if ( fabs(data2[i]) < threshold){
01618 qual1[i] |= QFLAG_DIVISOR_ZERO;
01619 errs1[i] = 1;
01620 data1[i] = 0;
01621 }
01622 else {
01623 double d1 = 0.0, d2 = 0.0;
01624 double e1 = 0.0, e2 =0.0;
01625 double error = 0.0;
01626
01627 d1 = data1[i];
01628 d2 = data2[i];
01629 e1 = errs1[i];
01630 e2 = errs2[i];
01631 error = fabs( 1.0/d2) * sqrt( (e1*e1) + (e2*e2*d1*d1)/(d2*d2) );
01632 errs1[i] = (float) error;
01633 data1[i] = (float)(d1/d2);
01634 }
01635 }
01636
01637 cleanup:
01638 return;
01639 }
01640
01641
01661
01662 void xsh_pre_multiply (xsh_pre* self, const xsh_pre * right, double threshold)
01663 {
01664
01665 float *errs1 = NULL;
01666 float *errs2 = NULL;
01667 float *data1 = NULL;
01668 float *data2 = NULL;
01669 int *qual1 = NULL;
01670 int *qual2 = NULL;
01671 int i = 0;
01672
01673 assure (self != NULL, CPL_ERROR_NULL_INPUT, "Null image!");
01674 assure (right != NULL, CPL_ERROR_NULL_INPUT, "Null image!");
01675
01676
01677 assure (xsh_pre_get_nx (self) == xsh_pre_get_nx (right) &&
01678 xsh_pre_get_ny (self) == xsh_pre_get_ny (right),
01679 CPL_ERROR_INCOMPATIBLE_INPUT,
01680 "Image sizes don't match: %dx%d and %dx%d",
01681 xsh_pre_get_nx (self), xsh_pre_get_ny (self),
01682 xsh_pre_get_nx (right), xsh_pre_get_ny (right));
01683
01684
01685
01686 check(data1 = cpl_image_get_data_float(self->data));
01687 check(data2 = cpl_image_get_data_float(right->data));
01688 check(errs1 = cpl_image_get_data_float(self->errs));
01689 check(errs2 = cpl_image_get_data_float(right->errs));
01690 check(qual1 = cpl_image_get_data_int(self->qual));
01691 check(qual2 = cpl_image_get_data_int(right->qual));
01692
01693 for(i=0;i< self->nx*self->ny; i++){
01694
01695 if ( qual2[i] > XSH_GOOD_PIXEL_LEVEL ) {
01696 if( (xsh_pre_get_group(right) == CPL_FRAME_GROUP_CALIB) ){
01697 qual1[i] |= QFLAG_CALIB_FILE_DEFECT;
01698 }
01699 else {
01700 qual1[i] |= qual2[i];
01701 }
01702 }
01703 else if ( fabs(data2[i]) > threshold){
01704 qual1[i] |= QFLAG_OUTSIDE_DATA_RANGE;
01705 errs1[i] = 1;
01706 data1[i] = 0;
01707 }
01708 else {
01709 double d1 = 0.0, d2 = 0.0;
01710 double e1 = 0.0, e2 =0.0;
01711 double error = 0.0;
01712
01713 d1 = data1[i];
01714 d2 = data2[i];
01715 e1 = errs1[i];
01716 e2 = errs2[i];
01717 error = sqrt( d2*d2*e1*e1 + d1*d1*e2*e2 );
01718 errs1[i] = (float) error;
01719 data1[i] = (float)(d1*d2);
01720 }
01721 }
01722
01723 cleanup:
01724 return;
01725 }
01726
01727
01728
01733
01734 void xsh_pre_normalize( xsh_pre* self)
01735 {
01736 double val1, val2, ct1, ct2;
01737 int i, rej, i1, i2;
01738
01739 XSH_ASSURE_NOT_NULL(self);
01740 i = 0;
01741
01742 do {
01743 i++;
01744 check(val1 = cpl_image_get(self->data, i, 1, &rej));
01745 }
01746 while (val1 == 0 || rej == 1);
01747 i1 = i;
01748
01749 do {
01750 i++;
01751 check(val2 = cpl_image_get(self->data, i, 1, &rej));
01752 }
01753 while (val2 == 0 || rej == 1);
01754 i2 = i;
01755
01756 check(cpl_image_normalise( self->data, CPL_NORM_MEAN));
01757
01758 check(ct1 = val1/ cpl_image_get(self->data, i1, 1, &rej));
01759 check(ct2 = val2/ cpl_image_get(self->data, i2, 1, &rej));
01760
01761 xsh_msg("normalize estimate pixel (%d,1) : %f",i1,ct1);
01762 xsh_msg("normalize estimate pixel (%d,1): %f",i2,ct2);
01763
01764 check( cpl_image_divide_scalar( self->errs, ct1));
01765
01766 cleanup:
01767 return;
01768 }
01769
01779 void xsh_pre_median_mean_stdev( xsh_pre * preFrame, double * mean,
01780 double * median, double * stdev )
01781 {
01782 int i, ng, nx, ny ;
01783 float * ppix = NULL;
01784 int * pbad = NULL;
01785 cpl_vector * ptemp = NULL;
01786
01787 XSH_ASSURE_NOT_NULL( preFrame ) ;
01788 XSH_ASSURE_NOT_NULL( mean ) ;
01789 XSH_ASSURE_NOT_NULL( median ) ;
01790 XSH_ASSURE_NOT_NULL( stdev ) ;
01791
01792 nx = xsh_pre_get_nx (preFrame);
01793 assure (nx != 0, cpl_error_get_code (), "Cant get X size");
01794 ny = xsh_pre_get_ny (preFrame);
01795 assure (ny != 0, cpl_error_get_code (), "Cant get Y size");
01796
01797 check( ptemp = cpl_vector_new( nx*ny )) ;
01798 check( ppix = cpl_image_get_data_float( preFrame->data ) ) ;
01799 check( pbad = cpl_image_get_data_int( preFrame->qual ) ) ;
01800
01801 for( ng = 0, i = 0 ; i < nx*ny ; i++ ) {
01802 if ( pbad[i] <= XSH_GOOD_PIXEL_LEVEL ){
01803 check( cpl_vector_set( ptemp, ng, (double)(ppix[i]) )) ;
01804 ng++ ;
01805 }
01806 }
01807
01808
01809 check( cpl_vector_set_size( ptemp, ng ) );
01810 check( *mean = cpl_vector_get_mean( ptemp ) );
01811 check( *stdev = cpl_vector_get_stdev( ptemp ) );
01812 check( *median = cpl_vector_get_median( ptemp ) );
01813
01814 cleanup:
01815 if (cpl_error_get_code() != CPL_ERROR_NONE){
01816 *mean = 0.0;
01817 *stdev = 0.0;
01818 *median = 0.0;
01819 }
01820 xsh_free_vector( &ptemp ) ;
01821 return;
01822 }
01823
01837 void xsh_pre_median_mean_stdev_window( xsh_pre * preFrame, double * mean,
01838 double * median, double * stdev,
01839 const int llx, const int lly,
01840 const int urx, const int ury )
01841 {
01842 int i, ng, nx, ny ;
01843 int vx,vy;
01844 float * ppix = NULL;
01845 int * pbad = NULL;
01846 cpl_vector * ptemp = NULL;
01847 int j;
01848
01849 XSH_ASSURE_NOT_NULL( preFrame ) ;
01850 XSH_ASSURE_NOT_NULL( mean ) ;
01851 XSH_ASSURE_NOT_NULL( median ) ;
01852 XSH_ASSURE_NOT_NULL( stdev ) ;
01853
01854 nx = xsh_pre_get_nx (preFrame);
01855 assure (nx != 0, cpl_error_get_code (), "Cant get X size");
01856 ny = xsh_pre_get_ny (preFrame);
01857 assure (ny != 0, cpl_error_get_code (), "Cant get Y size");
01858
01859 vx=urx-llx+1;
01860 vy=ury-lly+1;
01861
01862 check( ptemp = cpl_vector_new( vx*vy )) ;
01863 check( ppix = cpl_image_get_data_float( preFrame->data ) ) ;
01864 check( pbad = cpl_image_get_data_int( preFrame->qual ) ) ;
01865
01866 ng=0;
01867
01868 for( j = lly-1 ; j < ury ; j++ ) {
01869 for( i = llx-1 ; i < urx ; i++ ) {
01870
01871 if ( pbad[i] <= XSH_GOOD_PIXEL_LEVEL ){
01872 check( cpl_vector_set( ptemp, ng, (double)(ppix[j*nx+i]) )) ;
01873 ng++ ;
01874 }
01875 }
01876 }
01877
01878 cpl_table* tab=NULL;
01879 cpl_table* ext=NULL;
01880 int kappa=5;
01881 int niter1=2;
01882 int niter2=4;
01883
01884 tab=cpl_table_new(ng);
01885 cpl_table_wrap_double(tab,cpl_vector_get_data(ptemp),"VAL");
01886
01887
01888 *median=cpl_table_get_column_median(tab,"VAL");
01889 *mean=cpl_table_get_column_mean(tab,"VAL");
01890 *stdev=cpl_table_get_column_stdev(tab,"VAL");
01891 for(i=0;i<niter1;i++) {
01892
01893 cpl_table_and_selected_double(tab,"VAL",CPL_LESS_THAN,
01894 *median+kappa*(*stdev));
01895 cpl_table_and_selected_double(tab,"VAL",CPL_GREATER_THAN,
01896 *median-kappa*(*stdev));
01897 xsh_free_table(&ext);
01898 ext=cpl_table_extract_selected(tab);
01899 *median=cpl_table_get_column_median(ext,"VAL");
01900 *mean=cpl_table_get_column_mean(ext,"VAL");
01901 *stdev=cpl_table_get_column_stdev(ext,"VAL");
01902
01903 }
01904
01905
01906 for(i=0;i<niter2;i++) {
01907 cpl_table_and_selected_double(tab,"VAL",CPL_LESS_THAN,
01908 *mean+kappa*(*stdev));
01909 cpl_table_and_selected_double(tab,"VAL",CPL_GREATER_THAN,
01910 *mean-kappa*(*stdev));
01911 xsh_free_table(&ext);
01912 ext=cpl_table_extract_selected(tab);
01913 *median=cpl_table_get_column_median(ext,"VAL");
01914 *mean=cpl_table_get_column_mean(ext,"VAL");
01915 *stdev=cpl_table_get_column_stdev(ext,"VAL");
01916
01917 }
01918 xsh_free_table(&ext);
01919 ext=cpl_table_extract_selected(tab);
01920
01921 cpl_table_unwrap(tab,"VAL");
01922 *median=cpl_table_get_column_median(ext,"VAL");
01923 *mean=cpl_table_get_column_mean(ext,"VAL");
01924 *stdev=cpl_table_get_column_stdev(ext,"VAL");
01925
01926
01927
01928
01929
01930
01931
01932
01933
01934
01935
01936
01937
01938
01939
01940
01941
01942
01943 cleanup:
01944 if (cpl_error_get_code() != CPL_ERROR_NONE){
01945 *mean = 0.0;
01946 *stdev = 0.0;
01947 *median = 0.0;
01948 }
01949 xsh_free_vector( &ptemp ) ;
01950 xsh_free_table(&tab);
01951 xsh_free_table(&ext);
01952 return;
01953 }
01954
01955
01976
01977 int xsh_pre_window_best_median_flux_pos( xsh_pre* pre, int xcen, int ycen,
01978 int search_window_hsize, int running_median_hsize, int* xadj, int* yadj)
01979 {
01980 int search_win_xmin, search_win_xmax, search_win_ymin, search_win_ymax;
01981 float* data = NULL;
01982 int* qual = NULL;
01983 double* median_tab = NULL;
01984 int median_box_size = 0;
01985 double max_flux = -99999;
01986 int ibox, jbox;
01987 int xpos = -1, ypos = -1;
01988 int result= 0;
01989
01990 XSH_ASSURE_NOT_NULL( pre);
01991 XSH_ASSURE_NOT_NULL( xadj);
01992 XSH_ASSURE_NOT_NULL( yadj);
01993 XSH_CMP_INT( xcen, >=, 0, "Check central x position",);
01994 XSH_CMP_INT( xcen, <, pre->nx, "Check central x position",);
01995 XSH_CMP_INT( ycen, >=, 0, "Check central x position",);
01996 XSH_CMP_INT( ycen, <, pre->ny, "Check central x position",);
01997
01998 median_box_size = 1+running_median_hsize*2;
01999
02000 search_win_xmin = xcen-search_window_hsize;
02001 search_win_xmax = xcen+search_window_hsize;
02002 search_win_ymin = ycen-search_window_hsize;
02003 search_win_ymax = ycen+search_window_hsize;
02004
02005 if ( search_win_xmin < 0){
02006 search_win_xmin = 0;
02007 }
02008 if ( search_win_xmax >= pre->nx){
02009 search_win_xmax = pre->nx-1;
02010 }
02011 if ( search_win_ymin < 0){
02012 search_win_ymin = 0;
02013 }
02014 if ( search_win_ymax >= pre->ny){
02015 search_win_ymax = pre->ny-1;
02016 }
02017
02018 check( data = cpl_image_get_data_float( pre->data));
02019 check( qual = cpl_image_get_data_int( pre->qual));
02020 XSH_CALLOC(median_tab, double, median_box_size* median_box_size);
02021
02022 for (jbox=search_win_ymin; jbox <= (search_win_ymax+1-median_box_size);
02023 jbox++){
02024 for (ibox=search_win_xmin; ibox <= (search_win_xmax+1-median_box_size);
02025 ibox++){
02026 double med_flux = 0.0;
02027 int xc,yc;
02028
02029 med_flux = xsh_pre_data_window_median_flux_pa( pre,
02030 ibox, jbox, median_box_size, median_box_size, median_tab);
02031
02032 if ( cpl_error_get_code() == CPL_ERROR_NONE){
02033 xc = ibox+running_median_hsize;
02034 yc = jbox+running_median_hsize;
02035
02036 if ( (max_flux) < med_flux &&
02037 (qual[xc+yc*pre->nx] <= XSH_GOOD_PIXEL_LEVEL) ){
02038 max_flux = med_flux;
02039 xpos = xc;
02040 ypos = yc;
02041 }
02042 }
02043 else{
02044 xsh_error_reset();
02045 }
02046 }
02047 }
02048 if (xpos < 0 || ypos < 0){
02049 xsh_msg_dbg_high( "No valid pixels in the search box");
02050 result = 1;
02051 }
02052 else{
02053 *xadj = xpos;
02054 *yadj = ypos;
02055 }
02056
02057 cleanup:
02058 XSH_FREE( median_tab);
02059 return result;
02060 }
02061
02062
02073
02074 double xsh_pre_data_window_median_flux_pa(xsh_pre* pre, int x, int y,
02075 int size_x, int size_y, double* tab )
02076 {
02077 double medflux = 0.0;
02078 cpl_vector* v = NULL;
02079 float* data = NULL;
02080 int* qual = NULL;
02081 int ix = 0, iy = 0;
02082 int size = 0;
02083
02084
02085 XSH_ASSURE_NOT_NULL(pre);
02086 XSH_ASSURE_NOT_ILLEGAL(x >=0 && x < pre->nx);
02087 XSH_ASSURE_NOT_ILLEGAL(y >= 0 && y < pre->ny);
02088 XSH_ASSURE_NOT_ILLEGAL(size_x > 0 && (x+ size_x));
02089 XSH_ASSURE_NOT_ILLEGAL(size_y > 0 && (y+ size_y));
02090 XSH_ASSURE_NOT_NULL(tab);
02091
02092 if ( (x+size_x) >= pre->nx){
02093 size_x = pre->nx-x;
02094 }
02095 if ( (y+size_y) >= pre->ny){
02096 size_y = pre->ny-y;
02097 }
02098
02099
02100 check(data = cpl_image_get_data_float(pre->data));
02101
02102 check(qual = cpl_image_get_data_int(pre->qual));
02103
02104 for( iy=0; iy< size_y; iy++){
02105 for( ix=0; ix<size_x; ix++){
02106 if (qual[x+ix+(y+iy)*pre->nx] <= XSH_GOOD_PIXEL_LEVEL) {
02107 tab[size] = data[x+ix+(y+iy)*pre->nx];
02108 size++;
02109 }
02110 }
02111 }
02112 #if 0
02113
02114
02115
02116
02117
02118 XSH_ASSURE_NOT_ILLEGAL( ((float)size/(float)(size_x*size_y)) >
02119 XSH_WINDOW_MEDIAN_FRAC);
02120 #else
02121
02122
02123
02124
02125
02126
02127
02128
02129
02130 if ( ((float)size/(float)(size_x*size_y)) <= XSH_WINDOW_MEDIAN_FRAC ) {
02131 xsh_msg_warning( "Not enough good pixels in sub-window: %d/%d [%.2f < %.2f]", size,
02132 size_x*size_y, (float)size/(float)(size_x*size_y),
02133 XSH_WINDOW_MEDIAN_FRAC ) ;
02134 xsh_msg_warning( " Use Minimum/Median = 0." ) ;
02135 goto cleanup ;
02136 }
02137 #endif
02138
02139
02140 check(v = cpl_vector_wrap(size, tab));
02141 check( medflux = cpl_vector_get_median(v));
02142
02143 cleanup:
02144 xsh_unwrap_vector(&v);
02145 return medflux;
02146 }
02147
02148
02162
02163 double xsh_pre_data_window_sample_flux_pa(xsh_pre* pre, int x, int y,
02164 int size_x, int size_y, double* tab,
02165 const char* method,
02166 double min_window_frac )
02167 {
02168 double flux = 0.0;
02169 cpl_vector* v = NULL;
02170 float* data = NULL;
02171 int* qual = NULL;
02172 int ix = 0, iy = 0;
02173 int size = 0;
02174
02175
02176 XSH_ASSURE_NOT_NULL(pre);
02177 XSH_ASSURE_NOT_ILLEGAL(x >=1 && x <= pre->nx);
02178 XSH_ASSURE_NOT_ILLEGAL(y >= 1 && y <= pre->ny);
02179 XSH_ASSURE_NOT_ILLEGAL(size_x > 0 && (x+ size_x));
02180 XSH_ASSURE_NOT_ILLEGAL(size_y > 0 && (y+ size_y));
02181 XSH_ASSURE_NOT_NULL(tab);
02182
02183 x = x-1;
02184 y = y-1;
02185
02186 if ( (x+size_x) >= pre->nx){
02187 size_x = pre->nx-x;
02188 }
02189 if ( (y+size_y) >= pre->ny){
02190 size_y = pre->ny-y;
02191 }
02192
02193
02194 check(data = cpl_image_get_data_float(pre->data));
02195
02196 check(qual = cpl_image_get_data_int(pre->qual));
02197
02198 for( iy=0; iy< size_y; iy++){
02199 for( ix=0; ix<size_x; ix++){
02200 if (qual[x+ix+(y+iy)*pre->nx] <= XSH_GOOD_PIXEL_LEVEL) {
02201 tab[size] = data[x+ix+(y+iy)*pre->nx];
02202 size++;
02203 }
02204 }
02205 }
02206
02207 #if 0
02208
02209
02210
02211
02212
02213 XSH_ASSURE_NOT_ILLEGAL( ((float)size/(float)(size_x*size_y)) >
02214 XSH_WINDOW_MEDIAN_FRAC);
02215 #else
02216
02217
02218
02219
02220
02221
02222
02223
02224
02225 if ( ( (float)size/(float)(size_x*size_y)) <= min_window_frac ) {
02226 xsh_msg_warning( "Not enough good pixels in sub-window: %d/%d [%.2f < %.2f]", size,
02227 size_x*size_y, (float)size/(float)(size_x*size_y),
02228 min_window_frac ) ;
02229 xsh_msg_warning( " Use Minimum/Median = 0." ) ;
02230 goto cleanup ;
02231 }
02232 #endif
02233
02234
02235 check(v = cpl_vector_wrap(size, tab));
02236 if(strcmp(method,"minimum")==0) {
02237 check( flux = cpl_vector_get_min(v));
02238 } else if (strcmp(method,"median")==0) {
02239 check( flux = cpl_vector_get_median(v));
02240 } else if (strcmp(method,"poly")==0) {
02241 check( flux = cpl_vector_get_median(v));
02242 } else {
02243 flux=0;
02244 }
02245
02246 cleanup:
02247 xsh_unwrap_vector(&v);
02248 return flux;
02249 }
02250
02251
02252
02262
02263 double xsh_pre_data_window_median_flux(xsh_pre* pre, int x, int y,
02264 int size_x, int size_y)
02265 {
02266 double* data = NULL;
02267 double medflux = 0.0;
02268
02269 XSH_MALLOC(data,double,size_x*size_y);
02270 check(medflux =
02271 xsh_pre_data_window_median_flux_pa(pre, x, y, size_x, size_y, data));
02272
02273 cleanup:
02274 XSH_FREE(data);
02275 return medflux;
02276 }
02277
02283
02284 const cpl_propertylist *
02285 xsh_pre_get_header_const (const xsh_pre * pre)
02286 {
02287 cpl_propertylist *header = NULL;
02288
02289 XSH_ASSURE_NOT_NULL(pre);
02290 header = pre->data_header;
02291
02292 cleanup:
02293 return header;
02294 }
02295
02296
02302
02303 cpl_propertylist *
02304 xsh_pre_get_header (xsh_pre * pre)
02305 {
02306 XSH_ASSURE_NOT_NULL(pre);
02307
02308 cleanup:
02309 return (cpl_propertylist *) xsh_pre_get_header_const (pre);
02310 }
02311
02312
02313
02319
02320 const cpl_image* xsh_pre_get_data_const (const xsh_pre * pre)
02321 {
02322 cpl_image *data = NULL;
02323
02324 XSH_ASSURE_NOT_NULL(pre);
02325 data = pre->data;
02326
02327 cleanup:
02328 return data;
02329 }
02330
02331
02337
02338 cpl_image* xsh_pre_get_data (xsh_pre * pre)
02339 {
02340 return (cpl_image *) xsh_pre_get_data_const (pre);
02341 }
02342
02343
02349
02350 const cpl_image * xsh_pre_get_errs_const (const xsh_pre * pre)
02351 {
02352 cpl_image *errs = NULL;
02353
02354 XSH_ASSURE_NOT_NULL(pre);
02355 errs = pre->errs;
02356
02357 cleanup:
02358 return errs;
02359 }
02360
02361
02367
02368 cpl_image* xsh_pre_get_errs (xsh_pre * pre)
02369 {
02370 return (cpl_image *) xsh_pre_get_errs_const (pre);
02371 }
02372
02373
02379
02380 const cpl_image* xsh_pre_get_qual_const (const xsh_pre * pre)
02381 {
02382 cpl_image *qual = NULL;
02383
02384 XSH_ASSURE_NOT_NULL(pre);
02385 qual = pre->qual;
02386
02387 cleanup:
02388 return qual;
02389 }
02390
02391
02397 cpl_image* xsh_pre_get_qual (xsh_pre * pre)
02398 {
02399 return (cpl_image *) xsh_pre_get_qual_const (pre);
02400 }
02401
02402
02408
02409 const double* xsh_pre_get_data_buffer_const (const xsh_pre * pre)
02410 {
02411 double *buffer = NULL;
02412
02413 XSH_ASSURE_NOT_NULL(pre);
02414
02415
02416
02417 passure (cpl_image_get_type (pre->data) == CPL_TYPE_DOUBLE, " ");
02418 buffer = (double *) cpl_image_get_data (pre->data);
02419
02420 cleanup:
02421 return buffer;
02422 }
02423
02424
02430
02431 double* xsh_pre_get_data_buffer (xsh_pre * pre)
02432 {
02433 return (double *) xsh_pre_get_data_buffer_const (pre);
02434 }
02435
02436
02442
02443 const double* xsh_pre_get_errs_buffer_const (const xsh_pre * pre)
02444 {
02445 double *buffer = NULL;
02446
02447 XSH_ASSURE_NOT_NULL(pre);
02448 passure (cpl_image_get_type (pre->errs) == CPL_TYPE_DOUBLE, " ");
02449 buffer = (double *) cpl_image_get_data (pre->errs);
02450
02451 cleanup:
02452 return buffer;
02453 }
02454
02455
02461
02462 double* xsh_pre_get_errs_buffer (xsh_pre * pre)
02463 {
02464 return (double *) xsh_pre_get_errs_buffer_const(pre);
02465 }
02466
02467
02475
02476 void
02477 xsh_pre_dump (const xsh_pre * pre, FILE * stream)
02478 {
02479 cpl_stats *stats = NULL;
02480
02481 if (pre == NULL) {
02482 fprintf (stream, "Null");
02483 }
02484 else {
02485
02486 stats = cpl_stats_new_from_image (pre->data, CPL_STATS_ALL);
02487
02488 cpl_stats_dump (stats, CPL_STATS_ALL, stream);
02489
02490
02491
02492 fflush (stream);
02493 }
02494
02495 xsh_free_stats (&stats);
02496 return;
02497 }
02498
02499
02500
02506 void xsh_pre_multiply_scalar (const xsh_pre * pre, double x)
02507 {
02508
02509 XSH_ASSURE_NOT_NULL( pre);
02510
02511
02512 check (cpl_image_multiply_scalar(pre->data, x));
02513
02514
02515 check (cpl_image_multiply_scalar(pre->errs, fabs(x)));
02516
02517 cleanup:
02518 return;
02519 }
02520
02526 void xsh_pre_divide_scalar (const xsh_pre * pre, double x)
02527 {
02528
02529 XSH_ASSURE_NOT_NULL( pre);
02530 XSH_ASSURE_NOT_ILLEGAL( x != 0);
02531
02532
02533 check (cpl_image_divide_scalar(pre->data, x));
02534
02535
02536 check (cpl_image_divide_scalar(pre->errs, fabs(x)));
02537
02538 cleanup:
02539 return;
02540 }
02541
02548 void xsh_pre_add_scalar (const xsh_pre * pre, double x)
02549 {
02550
02551 XSH_ASSURE_NOT_NULL( pre);
02552 XSH_ASSURE_NOT_ILLEGAL( x != 0);
02553
02554
02555 check (cpl_image_add_scalar(pre->data, x));
02556
02557 cleanup:
02558 return;
02559 }
02560
02561
02567 void xsh_pre_subtract_scalar (const xsh_pre * pre, double x)
02568 {
02569
02570 XSH_ASSURE_NOT_NULL( pre);
02571 XSH_ASSURE_NOT_ILLEGAL( x != 0);
02572
02573
02574 check (cpl_image_subtract_scalar(pre->data, x));
02575
02576 cleanup:
02577 return;
02578 }
02579
02586 void xsh_pre_multiply_image(const xsh_pre *pre, cpl_image* img)
02587 {
02588 cpl_image *abs = NULL;
02589
02590 XSH_ASSURE_NOT_NULL( pre);
02591 XSH_ASSURE_NOT_NULL( img);
02592
02593
02594 check ( cpl_image_multiply(pre->data, img));
02595 check( abs = cpl_image_abs_create( img));
02596 check ( cpl_image_multiply(pre->errs, abs));
02597
02598 cleanup:
02599 xsh_free_image( &abs);
02600 return;
02601 }
02602
02603
02610 cpl_image* xsh_pre_abs (const xsh_pre * pre)
02611 {
02612
02613 cpl_image *result = NULL;
02614 int *result_data = NULL;
02615 float *data = NULL;
02616 int i, size;
02617
02618 XSH_ASSURE_NOT_NULL( pre);
02619
02620
02621 check ( data = cpl_image_get_data_float( pre->data));
02622 check( result = cpl_image_new( pre->nx, pre->ny, CPL_TYPE_INT));
02623 check( result_data = cpl_image_get_data_int( result));
02624
02625 size = pre->nx*pre->ny;
02626
02627 for(i =0; i < size; i++){
02628 if ( data[i] < 0){
02629 result_data[i] = -1;
02630 data[i] = -data[i];
02631 }
02632 else{
02633 result_data[i] = 1;
02634 }
02635 }
02636
02637 cleanup:
02638 return result;
02639 }
02640
02641
02650
02651
02652 cpl_frame*
02653 xsh_frameset_average_pre(cpl_frameset *set,
02654 xsh_instrument* instr,
02655 const char* tag)
02656 {
02657 cpl_frame* result=NULL;
02658 cpl_frame* frame=NULL;
02659 cpl_image* data=NULL;
02660 cpl_image* errs=NULL;
02661 cpl_image* qual=NULL;
02662
02663 xsh_pre* pre=NULL;
02664 cpl_imagelist* iml_data=NULL;
02665 cpl_imagelist* iml_errs=NULL;
02666 cpl_imagelist* iml_qual=NULL;
02667
02668 cpl_propertylist* plist=NULL;
02669 char name_o[80];
02670 const char* name=NULL;
02671 int i=0;
02672 int size=0;
02673 int nx=0;
02674 int ny=0;
02675
02676 size=cpl_frameset_get_size(set);
02677 iml_data=cpl_imagelist_new();
02678 iml_errs=cpl_imagelist_new();
02679 iml_qual=cpl_imagelist_new();
02680
02681 for(i=0;i<size;i++) {
02682 frame=cpl_frameset_get_frame(set,i);
02683 pre=xsh_pre_load(frame,instr);
02684 nx=pre->nx;
02685 ny=pre->ny;
02686 cpl_imagelist_set(iml_data,cpl_image_duplicate(pre->data),i);
02687 cpl_imagelist_set(iml_errs,cpl_image_duplicate(pre->errs),i);
02688 cpl_imagelist_set(iml_qual,cpl_image_duplicate(pre->qual),i);
02689 xsh_pre_free(&pre);
02690 }
02691
02692
02693 pre=xsh_pre_load(cpl_frameset_get_frame(set,0),instr);
02694 pre->data=cpl_imagelist_collapse_create(iml_data);
02695 pre->errs=cpl_imagelist_collapse_create(iml_errs);
02696 pre->qual=cpl_imagelist_collapse_create(iml_qual);
02697
02698 cpl_image_divide_scalar(pre->data,size);
02699 cpl_image_divide_scalar(pre->errs,size);
02700 cpl_image_divide_scalar(pre->qual,size);
02701
02702 frame=cpl_frameset_get_frame(set,0);
02703 name=cpl_frame_get_filename(frame);
02704 plist=cpl_propertylist_load(name,0);
02705
02706 sprintf(name_o,"%s.fits",tag);
02707 check(xsh_pre_save(pre,name_o,tag,0));
02708
02709 result=xsh_frame_product(name_o,tag,CPL_FRAME_TYPE_IMAGE,
02710 CPL_FRAME_GROUP_PRODUCT,CPL_FRAME_LEVEL_FINAL);
02711
02712 cleanup:
02713 xsh_free_image(&data);
02714 xsh_free_image(&errs);
02715 xsh_free_image(&qual);
02716
02717 xsh_free_imagelist(&iml_data);
02718 xsh_free_imagelist(&iml_errs);
02719 xsh_free_imagelist(&iml_qual);
02720
02721 xsh_pre_free(&pre);
02722 xsh_free_propertylist(&plist);
02723
02724 return result;
02725 }
02726
02734 void xsh_pre_turn( xsh_pre * pre, int rot )
02735 {
02736 cpl_image * img = NULL ;
02737
02738 XSH_ASSURE_NOT_NULL( pre ) ;
02739
02740 check( img = xsh_pre_get_data( pre ) ) ;
02741 check( cpl_image_turn( img, rot ) ) ;
02742
02743 check( img = xsh_pre_get_errs( pre ) ) ;
02744 check( cpl_image_turn( img, rot ) ) ;
02745
02746 check( img = xsh_pre_get_qual( pre ) ) ;
02747 check( cpl_image_turn( img, rot ) ) ;
02748
02749 cleanup:
02750 return ;
02751 }
02752
02767 void xsh_pre_extract( xsh_pre *pre, int xmin, int ymin, int xmax, int ymax)
02768 {
02769 cpl_image * img = NULL ;
02770 cpl_image * ext_img = NULL ;
02771
02772 XSH_ASSURE_NOT_NULL( pre);
02773
02774 check( img = xsh_pre_get_data( pre));
02775
02776 XSH_ASSURE_NOT_NULL( img);
02777
02778 check( ext_img = cpl_image_extract( img, xmin, ymin, xmax, ymax));
02779 xsh_free_image( &img);
02780 pre->data = ext_img;
02781
02782 check( img = xsh_pre_get_errs( pre));
02783 check( ext_img = cpl_image_extract( img, xmin, ymin, xmax, ymax));
02784 xsh_free_image( &img);
02785 pre->errs = ext_img;
02786
02787 check( img = xsh_pre_get_qual( pre));
02788 check( ext_img = cpl_image_extract( img, xmin, ymin, xmax, ymax));
02789 xsh_free_image( &img);
02790 pre->qual = ext_img;
02791
02792 cleanup:
02793 return;
02794 }
02803 void xsh_pre_flip( xsh_pre * pre, int angle )
02804 {
02805 cpl_image * img = NULL ;
02806
02807 XSH_ASSURE_NOT_NULL( pre ) ;
02808
02809 check( img = xsh_pre_get_data( pre ) ) ;
02810 check( cpl_image_flip( img, angle ) ) ;
02811
02812 check( img = xsh_pre_get_errs( pre ) ) ;
02813 check( cpl_image_flip( img, angle ) ) ;
02814
02815 check( img = xsh_pre_get_qual( pre ) ) ;
02816 check( cpl_image_flip( img, angle ) ) ;
02817
02818 cleanup:
02819 return ;
02820 }