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 #ifdef HAVE_CONFIG_H
00026 # include <config.h>
00027 #endif
00028
00029
00035
00038
00039
00040
00041
00042
00043 #include <cpl.h>
00044 #include <xsh_data_instrument.h>
00045 #include <xsh_pfits.h>
00046 #include <xsh_msg.h>
00047 #include <xsh_utils.h>
00048 #include <xsh_utils_image.h>
00049 #include <xsh_data_order.h>
00050 #include <tests.h>
00051 #include <math.h>
00052
00053 #include <xsh_cpl_size.h>
00054
00055
00056
00057
00058 #define MODULE_ID "XSH_FLUX_CONSERVATION"
00059
00060
00061
00062
00063
00070
00071
00072
00073
00074
00075
00076
00077 static const double poly0_coeff[] = {
00078 280., 0.3, -0.0025 } ;
00079 static const double step = 40. ;
00080
00081 static int nx = 400, ny = 400 ;
00082 static int starty = 10, endy = 380 ;
00083 static int dimension = 1, degree = 2, width = 10 ;
00084 static int norder = 3 ;
00085 static double exptime = 1. ;
00086 static const char * img_name = "dtc_img.fits" ;
00087 static const char * tbl_name = "dtc_tbl.fits" ;
00088 static const char * dtc_pre_name = "dtc_img_pre.fits" ;
00089
00090 static double
00091 derivative_x(const double a1, const double a2, const double x){
00092
00093 return a1+2*a2*x;
00094
00095 }
00096
00097 static double
00098 derivative_y(const double a1, const double a2, const double y){
00099
00100 return a1+2*a2*y;
00101
00102 }
00103 static void verify_order_table( cpl_frame * result )
00104 {
00105 const char *hname ;
00106 int nbad = 1 ;
00107 cpl_propertylist * header = NULL ;
00108 double residavg = 0. ;
00109
00110 xsh_msg( " ====== verify_order_tables" ) ;
00111
00112
00113
00114
00115 check( hname = cpl_frame_get_filename( result ) ) ;
00116 check( header = cpl_propertylist_load( hname, 0 ) ) ;
00117 check(xsh_get_property_value (header, QC_ORD_ORDERPOS_RESIDAVG,
00118 CPL_TYPE_DOUBLE, &residavg ) ) ;
00119 nbad = 0 ;
00120 assure( residavg < 1., CPL_ERROR_ILLEGAL_INPUT, "Error Too large" ) ;
00121 xsh_msg( " Orders Detected OK, RESIDAVG = %lf", residavg ) ;
00122
00123 cleanup:
00124 xsh_free_propertylist(&header);
00125 return ;
00126 }
00127
00128 int main(void)
00129 {
00130 xsh_instrument* instrument = NULL;
00131 xsh_order_list * list = NULL ;
00132 cpl_polynomial * poly0 = NULL ;
00133 cpl_polynomial * poly1 = NULL ;
00134 cpl_polynomial * poly2 = NULL ;
00135 cpl_image *image = NULL ;
00136 cpl_image *bias = NULL;
00137 cpl_frame * img_frame = NULL, * tbl_frame = NULL, * dtc_frame = NULL,
00138 * result_frame = NULL ;
00139 xsh_pre * img_pre = NULL ;
00140 xsh_clipping_param dcn_clipping ;
00141 xsh_detect_continuum_param detect_param;
00142
00143 XSH_INSTRCONFIG *iconfig ;
00144 cpl_propertylist * img_header ;
00145
00146
00147 cpl_vector* xprofile=NULL;
00148 cpl_vector* yprofile=NULL;
00149 cpl_polynomial* poly_u=NULL;
00150 cpl_polynomial* poly_v=NULL;
00151 const int dim=2;
00152 cpl_size pows[dim];
00153 cpl_image* warped=NULL;
00154 cpl_image* wone=NULL;
00155 cpl_image* one=NULL;
00156 cpl_image* dXdx=NULL;
00157 cpl_image* dYdy=NULL;
00158 cpl_image* dXdy=NULL;
00159 cpl_image* dYdx=NULL;
00160 cpl_image* corr=NULL;
00161
00162 cpl_vector* sup=NULL;
00163 double* psup=NULL;
00164 cpl_vector* inf=NULL;
00165 double* pinf=NULL;
00166
00167 float* pdXdx=NULL;
00168 float* pdYdy=NULL;
00169 float* pdXdy=NULL;
00170 float* pdYdx=NULL;
00171 float* pcor=NULL;
00172
00173 int j=0;
00174 int i=0;
00175 cpl_size deg=0;
00176 double f_org=0;
00177 double f_cor=0;
00178 double f_add=0;
00179 double f_bad=0;
00180 double f_end=0;
00181 double add=0.1;
00182 cpl_frame* resid_frame=NULL;
00183
00184 TESTS_INIT(MODULE_ID);
00185 xsh_msg("detect_continuum");
00186 instrument = xsh_instrument_new() ;
00187 xsh_instrument_set_mode( instrument, XSH_MODE_SLIT ) ;
00188 xsh_instrument_set_arm( instrument, XSH_ARM_VIS ) ;
00189 xsh_instrument_set_lamp( instrument, XSH_LAMP_UNDEFINED ) ;
00190 xsh_instrument_set_recipe_id( instrument, "xsh_orderpos" ) ;
00191
00192 xsh_msg( " recipe_id: %s", instrument->recipe_id ) ;
00193
00194 iconfig = xsh_instrument_get_config( instrument ) ;
00195 iconfig->orders = norder ;
00196
00197 xsh_msg( "Create Order List with %d orders", norder ) ;
00198 check( list = create_order_list( norder, instrument ) ) ;
00199
00200 xsh_msg( "Create polynomials of degree %d", degree ) ;
00201 poly0 = cpl_polynomial_new( dimension ) ;
00202 poly1 = cpl_polynomial_new( dimension ) ;
00203 poly2 = cpl_polynomial_new( dimension ) ;
00204
00205
00206 {
00207 deg = 0 ;
00208
00209 cpl_polynomial_set_coeff( poly0, °, poly0_coeff[deg] ) ;
00210 cpl_polynomial_set_coeff( poly1, °, poly0_coeff[deg] + step ) ;
00211 cpl_polynomial_set_coeff( poly2, °, poly0_coeff[deg] + 2*step ) ;
00212
00213 for( deg = 1 ; deg<= degree ; deg++ ) {
00214 cpl_polynomial_set_coeff( poly0, °, poly0_coeff[deg] ) ;
00215 cpl_polynomial_set_coeff( poly1, °, poly0_coeff[deg] ) ;
00216 cpl_polynomial_set_coeff( poly2, °, poly0_coeff[deg] ) ;
00217 }
00218 }
00219
00220 xsh_msg( "Add to order list" ) ;
00221 add_to_order_list( list, 0, 1, poly0, width, starty, endy ) ;
00222 add_to_order_list( list, 1, 2, poly1, width, starty, endy ) ;
00223 add_to_order_list( list, 2, 3, poly2, width, starty, endy ) ;
00224
00225 xsh_order_list_dump( list, "orders.dmp" ) ;
00226
00227
00228 img_frame = cpl_frame_new() ;
00229 img_header = mkHeader( iconfig, nx, ny, exptime ) ;
00230
00231 check( image = create_order_image( list, nx, ny ) ) ;
00232 cpl_image_save( image, img_name, CPL_BPP_IEEE_DOUBLE, img_header,
00233 CPL_IO_DEFAULT);
00234
00235 cpl_frame_set_filename( img_frame, img_name ) ;
00236 cpl_frame_set_tag( img_frame, "ORDERDEF_VIS_D2" );
00237 cpl_frame_set_level( img_frame, CPL_FRAME_LEVEL_TEMPORARY);
00238 cpl_frame_set_group( img_frame, CPL_FRAME_GROUP_RAW ) ;
00239
00240
00241 bias = xsh_test_create_bias_image( "BIAS.fits" ,nx, ny, instrument);
00242 check( img_pre = xsh_pre_create( img_frame, NULL, bias, instrument,0,CPL_FALSE));
00243 xsh_msg( "Saving PRE image \"%s\"", dtc_pre_name ) ;
00244 check_msg( dtc_frame = xsh_pre_save( img_pre, dtc_pre_name, "TEST",1 ),
00245 "Cant save pre structure" ) ;
00246 cpl_frame_set_filename( dtc_frame, dtc_pre_name ) ;
00247 cpl_frame_set_tag( dtc_frame, "ORDERDEF_UVB_D2" );
00248 cpl_frame_set_level( dtc_frame, CPL_FRAME_LEVEL_TEMPORARY);
00249 cpl_frame_set_group( dtc_frame, CPL_FRAME_GROUP_RAW ) ;
00250
00251
00252 check( tbl_frame = xsh_order_list_save( list, instrument, tbl_name,"ORDERDEF_UVB_D2",ny) ) ;
00253
00254
00255
00256 poly_u=cpl_polynomial_new(2);
00257 poly_v=cpl_polynomial_new(2);
00258
00259
00260 pows[0]=0;
00261 pows[1]=0;
00262 cpl_polynomial_set_coeff(poly_v,pows,poly0_coeff[0]);
00263
00264
00265 pows[0]=0;
00266 pows[1]=1;
00267 cpl_polynomial_set_coeff(poly_v,pows,-poly0_coeff[1]);
00268
00269
00270 pows[0]=0;
00271 pows[1]=2;
00272 cpl_polynomial_set_coeff(poly_v,pows,-poly0_coeff[2]);
00273
00274
00275 pows[0]=0;
00276 pows[1]=1;
00277 cpl_polynomial_set_coeff(poly_u,pows,1.);
00278 cpl_polynomial_dump(poly_u,stdout);
00279 cpl_polynomial_dump(poly_v,stdout);
00280
00281 check(warped=cpl_image_new(nx,ny,CPL_TYPE_FLOAT));
00282 check(one=cpl_image_new(nx,ny,CPL_TYPE_FLOAT));
00283 check(cpl_image_add_scalar(one,1.));
00284 check(xprofile=cpl_vector_new(nx));
00285 check(yprofile=cpl_vector_new(ny));
00286 cpl_vector_fill_kernel_profile(xprofile, CPL_KERNEL_DEFAULT,
00287 CPL_KERNEL_DEF_WIDTH);
00288 cpl_vector_fill_kernel_profile(yprofile, CPL_KERNEL_DEFAULT,
00289 CPL_KERNEL_DEF_WIDTH);
00290
00291
00292
00293
00294
00295 xsh_free_vector(&xprofile);
00296 xsh_free_vector(&yprofile);
00297
00298 xsh_free_image(&warped);
00299 check(warped=xsh_warp_image_generic(img_pre->data,CPL_KERNEL_TANH,poly_u,poly_v));
00300 check(wone=xsh_warp_image_generic(one,CPL_KERNEL_TANH,poly_u,poly_v));
00301 check(cpl_image_divide(warped,wone));
00302 xsh_free_image(&wone);
00303
00304 check(f_org=cpl_image_get_flux(img_pre->data));
00305 check(f_bad=cpl_image_get_flux_window(warped,1,140,nx,216));
00306
00307 xsh_msg("Flux PRE frame: %g",f_org);
00308 xsh_msg("Flux warp frame: %g",f_bad);
00309
00310
00311 check(dXdx=cpl_image_new(nx,ny,CPL_TYPE_FLOAT));
00312 check(dYdy=cpl_image_new(nx,ny,CPL_TYPE_FLOAT));
00313 check(dXdy=cpl_image_new(nx,ny,CPL_TYPE_FLOAT));
00314 check(dYdx=cpl_image_new(nx,ny,CPL_TYPE_FLOAT));
00315 check(corr=cpl_image_new(nx,ny,CPL_TYPE_FLOAT));
00316
00317 check(pdXdx=cpl_image_get_data_float(dXdx));
00318 check(pdYdy=cpl_image_get_data_float(dYdy));
00319 check(pdXdy=cpl_image_get_data_float(dXdy));
00320 check(pdYdx=cpl_image_get_data_float(dYdx));
00321 check(pcor=cpl_image_get_data_float(corr));
00322
00323 check(sup=cpl_vector_new(2));
00324 check(psup=cpl_vector_get_data(sup));
00325 check(inf=cpl_vector_new(2));
00326 check(pinf=cpl_vector_get_data(inf));
00327
00328
00329 for(j=1;j<ny-1;j++) {
00330 for(i=1;i<nx-1;i++) {
00331 psup[0]=(double)(i+1);
00332 psup[1]=(double)j;
00333
00334 pinf[0]=(double)(i-1);
00335 pinf[1]=(double)j;
00336
00337 pdXdx[i+j*nx]=1.;
00338
00339
00340
00341
00342
00343
00344
00345
00346
00347
00348
00349 pdYdx[i+j*nx]=derivative_x(poly0_coeff[1],poly0_coeff[2],(double)i);
00350
00351 psup[0]=(double)i;
00352 psup[1]=(double)(j+1);
00353
00354 pinf[0]=(double)i;
00355 pinf[1]=(double)(j-1);
00356
00357
00358
00359
00360
00361 pdXdy[i+j*nx]=0.;
00362
00363 pdYdy[i+j*nx]=derivative_y(poly0_coeff[1],poly0_coeff[2],(double)j);
00364
00365
00366
00367
00368
00369
00370
00371
00372
00373
00374
00375
00376
00377
00378
00379 pcor[i+j*nx]=fabs(pdXdx[i+j*nx]*pdYdy[i+j*nx]-
00380 pdXdy[i+j*nx]*pdYdx[i+j*nx]);
00381
00382 }
00383 }
00384
00385 check(cpl_image_save(warped,"order_warped.fits",CPL_BPP_IEEE_FLOAT,NULL,
00386 CPL_IO_DEFAULT));
00387 check(cpl_image_save(wone,"wone.fits",CPL_BPP_IEEE_FLOAT,NULL,
00388 CPL_IO_DEFAULT));
00389 check(cpl_image_save(corr,"corr.fits",CPL_BPP_IEEE_FLOAT,NULL,
00390 CPL_IO_DEFAULT));
00391 cpl_image_add_scalar(corr,add);
00392 check(cpl_image_divide(warped,corr));
00393 f_cor=cpl_image_get_flux_window(corr,1,140,nx,216);
00394
00395 f_add=add*nx*ny;
00396 xsh_msg("Flux add frame: %g",f_add);
00397 f_end=cpl_image_get_flux_window(warped,1,140,nx,216);
00398
00399
00400 xsh_msg("Flux corrected frame: %g",f_end);
00401 xsh_msg("Flux predicted frame: %g",f_end*(f_add+f_cor)/f_cor);
00402
00403
00404 check(cpl_image_save(warped,"corrected.fits",CPL_BPP_IEEE_FLOAT,NULL,
00405 CPL_IO_DEFAULT));
00406
00407
00408 dcn_clipping.sigma = 2.5 ;
00409 dcn_clipping.niter = 5 ;
00410 dcn_clipping.frac = 0.7 ;
00411
00412
00413 dcn_clipping.res_max = 0.4 ;
00414
00415 detect_param.search_window = 30;
00416 detect_param.running_window = 7;
00417 detect_param.fit_window = 10;
00418 detect_param.poly_degree = 2;
00419 detect_param.poly_step = 2;
00420 detect_param.fit_threshold = 1.0;
00421
00422 xsh_debug_level_set( XSH_DEBUG_LEVEL_LOW ) ;
00423
00424 check (result_frame = xsh_detect_continuum( dtc_frame, tbl_frame, NULL,
00425 &detect_param,
00426 &dcn_clipping,
00427 instrument,&resid_frame) ) ;
00428
00429
00430 check( verify_order_table( result_frame ) ) ;
00431
00432 cleanup:
00433 xsh_free_frame(&resid_frame);
00434 xsh_free_propertylist(&img_header);
00435 xsh_free_polynomial(&poly0);
00436 xsh_free_polynomial(&poly1);
00437 xsh_free_polynomial(&poly2);
00438 xsh_free_polynomial(&poly_u);
00439 xsh_free_polynomial(&poly_v);
00440
00441 xsh_free_frame(&img_frame);
00442 xsh_free_frame(&dtc_frame);
00443 xsh_free_frame(&tbl_frame);
00444 xsh_free_frame(&result_frame);
00445 xsh_order_list_free(&list);
00446 xsh_free_image(&image);
00447 xsh_free_image(&bias);
00448 xsh_free_image(&warped);
00449 xsh_free_image(&wone);
00450 xsh_free_image(&one);
00451
00452 xsh_free_image(&dXdx);
00453 xsh_free_image(&dYdy);
00454 xsh_free_image(&dXdy);
00455 xsh_free_image(&dYdx);
00456 xsh_free_image(&corr);
00457
00458
00459 xsh_free_vector(&sup);
00460 xsh_free_vector(&inf);
00461 xsh_pre_free(&img_pre);
00462 xsh_free_vector(&xprofile);
00463 xsh_free_vector(&yprofile);
00464 xsh_instrument_free(&instrument);
00465 if (cpl_error_get_code() != CPL_ERROR_NONE) {
00466 xsh_error_dump(CPL_MSG_ERROR);
00467 return 1;
00468 }
00469 else {
00470 return 0;
00471 }
00472
00473 }
00474