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_data_order.h>
00049 #include <tests.h>
00050 #include <math.h>
00051
00052
00053
00054
00055 #define MODULE_ID "XSH_DETECT_CONTINUUM"
00056
00057
00058
00059
00060
00067
00068
00069
00070
00071
00072
00073
00074 static const double poly0_coeff[] = {
00075 280., 0.3, -0.0025 } ;
00076 static const double step = 40. ;
00077
00078 static int nx = 400, ny = 400 ;
00079 static int starty = 10, endy = 380 ;
00080 static int dimension = 1, degree = 2, width = 10 ;
00081 static int norder = 3 ;
00082 static double exptime = 1. ;
00083 static const char * img_name = "dtc_img.fits" ;
00084 static const char * tbl_name = "dtc_tbl.fits" ;
00085 static const char * dtc_pre_name = "dtc_img_pre.fits" ;
00086
00087 static void verify_order_table( cpl_frame * result )
00088 {
00089 const char *hname ;
00090 int nbad = 1 ;
00091 cpl_propertylist * header = NULL ;
00092 double residavg = 0. ;
00093
00094 xsh_msg( " ====== verify_order_tables" ) ;
00095
00096
00097
00098
00099 check( hname = cpl_frame_get_filename( result ) ) ;
00100 check( header = cpl_propertylist_load( hname, 0 ) ) ;
00101 check(xsh_get_property_value (header, QC_ORD_ORDERPOS_RESIDAVG,
00102 CPL_TYPE_DOUBLE, &residavg ) ) ;
00103 nbad = 0 ;
00104 assure( residavg < 1., CPL_ERROR_ILLEGAL_INPUT, "Error Too large" ) ;
00105 xsh_msg( " Orders Detected OK, RESIDAVG = %lf", residavg ) ;
00106
00107 cleanup:
00108 xsh_free_propertylist(&header);
00109 return ;
00110 }
00111
00112 int main(void)
00113 {
00114 xsh_instrument* instrument = NULL;
00115 xsh_order_list * list = NULL ;
00116 cpl_polynomial * poly0 = NULL ;
00117 cpl_polynomial * poly1 = NULL ;
00118 cpl_polynomial * poly2 = NULL ;
00119 cpl_image *image = NULL ;
00120 cpl_image *bias = NULL;
00121 cpl_frame * img_frame = NULL, * tbl_frame = NULL, * dtc_frame = NULL,
00122 * result_frame = NULL ;
00123 xsh_pre * img_pre = NULL ;
00124 xsh_clipping_param dcn_clipping ;
00125 xsh_detect_continuum_param detect_param;
00126
00127 XSH_INSTRCONFIG *iconfig ;
00128 cpl_propertylist * img_header=NULL ;
00129 cpl_frame* resid_frame=NULL;
00130
00131 TESTS_INIT(MODULE_ID);
00132 xsh_msg("detect_continuum");
00133 instrument = xsh_instrument_new() ;
00134 xsh_instrument_set_mode( instrument, XSH_MODE_SLIT ) ;
00135 xsh_instrument_set_arm( instrument, XSH_ARM_VIS ) ;
00136 xsh_instrument_set_lamp( instrument, XSH_LAMP_UNDEFINED ) ;
00137 xsh_instrument_set_recipe_id( instrument, "xsh_orderpos" ) ;
00138
00139 xsh_msg( " recipe_id: %s", instrument->recipe_id ) ;
00140
00141 iconfig = xsh_instrument_get_config( instrument ) ;
00142 iconfig->orders = norder ;
00143
00144 xsh_msg( "Create Order List with %d orders", norder ) ;
00145 check( list = create_order_list( norder, instrument ) ) ;
00146
00147 xsh_msg( "Create polynomials of degree %d", degree ) ;
00148 poly0 = cpl_polynomial_new( dimension ) ;
00149 poly1 = cpl_polynomial_new( dimension ) ;
00150 poly2 = cpl_polynomial_new( dimension ) ;
00151
00152
00153 {
00154 int i ;
00155 i = 0 ;
00156
00157 cpl_polynomial_set_coeff( poly0, &i, poly0_coeff[i] ) ;
00158 cpl_polynomial_set_coeff( poly1, &i, poly0_coeff[i] + step ) ;
00159 cpl_polynomial_set_coeff( poly2, &i, poly0_coeff[i] + 2*step ) ;
00160
00161 for( i = 1 ; i<= degree ; i++ ) {
00162 cpl_polynomial_set_coeff( poly0, &i, poly0_coeff[i] ) ;
00163 cpl_polynomial_set_coeff( poly1, &i, poly0_coeff[i] ) ;
00164 cpl_polynomial_set_coeff( poly2, &i, poly0_coeff[i] ) ;
00165 }
00166 }
00167
00168 xsh_msg( "Add to order list" ) ;
00169 add_to_order_list( list, 0, 1, poly0, width, starty, endy ) ;
00170 add_to_order_list( list, 1, 2, poly1, width, starty, endy ) ;
00171 add_to_order_list( list, 2, 3, poly2, width, starty, endy ) ;
00172
00173 xsh_order_list_dump( list, "orders.dmp" ) ;
00174
00175
00176 img_frame = cpl_frame_new() ;
00177 img_header = mkHeader( iconfig, nx, ny, exptime ) ;
00178
00179 check( image = create_order_image( list, nx, ny ) ) ;
00180 cpl_image_save( image, img_name, CPL_BPP_IEEE_DOUBLE, img_header,
00181 CPL_IO_DEFAULT);
00182
00183 cpl_frame_set_filename( img_frame, img_name ) ;
00184 cpl_frame_set_tag( img_frame, "ORDERDEF_VIS_D2" );
00185 cpl_frame_set_level( img_frame, CPL_FRAME_LEVEL_TEMPORARY);
00186 cpl_frame_set_group( img_frame, CPL_FRAME_GROUP_RAW ) ;
00187
00188
00189 bias = xsh_test_create_bias_image( "BIAS.fits" ,nx, ny, instrument);
00190 check( img_pre = xsh_pre_create( img_frame, NULL, bias, instrument,0));
00191 xsh_msg( "Saving PRE image \"%s\"", dtc_pre_name ) ;
00192 check_msg( dtc_frame = xsh_pre_save( img_pre, dtc_pre_name, "TEST",1 ),
00193 "Cant save pre structure" ) ;
00194 cpl_frame_set_filename( dtc_frame, dtc_pre_name ) ;
00195 cpl_frame_set_tag( dtc_frame, "ORDERDEF_UVB_D2" );
00196 cpl_frame_set_level( dtc_frame, CPL_FRAME_LEVEL_TEMPORARY);
00197 cpl_frame_set_group( dtc_frame, CPL_FRAME_GROUP_RAW ) ;
00198
00199
00200 check( tbl_frame = xsh_order_list_save( list, instrument, tbl_name,"ORDERDEF_UVB_D2",ny) ) ;
00201
00202
00203
00204 dcn_clipping.sigma = 2.5 ;
00205 dcn_clipping.niter = 5 ;
00206 dcn_clipping.frac = 0.7 ;
00207
00208
00209 dcn_clipping.res_max = 0.4 ;
00210
00211 detect_param.search_window = 30;
00212 detect_param.running_window = 7;
00213 detect_param.fit_window = 10;
00214 detect_param.poly_degree = 2;
00215 detect_param.poly_step = 2;
00216 detect_param.fit_threshold = 1.0;
00217
00218 xsh_debug_level_set( XSH_DEBUG_LEVEL_LOW ) ;
00219
00220 check (result_frame = xsh_detect_continuum( dtc_frame, tbl_frame, NULL,
00221 &detect_param,
00222 &dcn_clipping,
00223 instrument,&resid_frame) ) ;
00224
00225
00226 check( verify_order_table( result_frame ) ) ;
00227
00228 cleanup:
00229 xsh_free_propertylist(&img_header);
00230 xsh_free_polynomial(&poly0);
00231 xsh_free_polynomial(&poly1);
00232 xsh_free_polynomial(&poly2);
00233
00234 xsh_free_frame(&img_frame);
00235 xsh_free_frame(&dtc_frame);
00236 xsh_free_frame(&tbl_frame);
00237 xsh_free_frame(&result_frame);
00238
00239 xsh_free_frame(&resid_frame);
00240 xsh_order_list_free(&list);
00241 xsh_free_image(&image);
00242 xsh_free_image(&bias);
00243 xsh_pre_free(&img_pre);
00244 xsh_instrument_free(&instrument);
00245 if (cpl_error_get_code() != CPL_ERROR_NONE) {
00246 xsh_error_dump(CPL_MSG_ERROR);
00247 return 1;
00248 }
00249 else {
00250 return 0;
00251 }
00252
00253 }
00254