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