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
00037
00040
00041
00042
00043
00044 #include <xsh_drl.h>
00045 #include <xsh_pfits.h>
00046 #include <xsh_data_resid_tab.h>
00047 #include <xsh_data_instrument.h>
00048 #include <xsh_data_arclist.h>
00049 #include <xsh_data_spectralformat.h>
00050 #include <xsh_data_order.h>
00051 #include <xsh_data_wavesol.h>
00052 #include <xsh_data_the_map.h>
00053 #include <xsh_model_io.h>
00054 #include <xsh_model_utils.h>
00055 #include <xsh_error.h>
00056 #include <xsh_msg.h>
00057 #include <cpl.h>
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00090
00091 cpl_frame* xsh_flexcor( cpl_frame* afc_frame,
00092 cpl_frame *wave_tab_frame,
00093 cpl_frame *model_config_frame,
00094 cpl_frame *order_tab_frame,
00095 cpl_frame *attresidtab_frame,
00096 int afc_xmin, int afc_ymin,
00097 xsh_instrument *instr,
00098 cpl_frame **afc_order_tab_frame,
00099 cpl_frame **afc_model_config_frame)
00100 {
00101 char result_name[256];
00102 cpl_frame *result = NULL;
00103 cpl_frame *afc_order_tab_result = NULL;
00104 cpl_table *trace = NULL;
00105 float xshift = 0.0, yshift = 0.0;
00106 float T_effect_x = 0.0, T_effect_y = 0.0;
00107 xsh_resid_tab *attresid_tab = NULL;
00108 int residtab_size, iline;
00109 const char *tag = NULL;
00110 xsh_wavesol *wavesol = NULL;
00111 xsh_order_list *order_tab = NULL;
00112 double lambda, order, slit=0.0;
00113 double att_x, att_y;
00114 double cal_x, cal_y;
00115 double cal_no_T_corr_x, cal_no_T_corr_y;
00116 XSH_INSTRCONFIG *config = NULL;
00117 XSH_ARM arm = XSH_ARM_UNDEFINED;
00118 xsh_xs_3 config_model;
00119 xsh_xs_3 cfg_mod_no_T_corr;
00120
00121 cpl_table *model_tab = NULL;
00122 cpl_propertylist *model_header = NULL;
00123 cpl_propertylist *afc_header = NULL;
00124 cpl_vector *shiftx_vect = NULL;
00125 cpl_vector *shifty_vect = NULL;
00126 cpl_vector *T_effect_x_vect = NULL;
00127 cpl_vector *T_effect_y_vect = NULL;
00128 const char* afc_name=NULL;
00129 int found_temp=true;
00130 cpl_frame* MODEL_CONF_OPT_frame=NULL;
00131 cpl_propertylist* qc_head2copy=NULL;
00132
00133
00134
00135 XSH_ASSURE_NOT_NULL( attresidtab_frame);
00136 XSH_ASSURE_NOT_NULL( order_tab_frame);
00137 XSH_ASSURE_NOT_NULL( afc_order_tab_frame);
00138
00139
00140
00141 check( attresid_tab = xsh_resid_tab_load( attresidtab_frame));
00142 check( order_tab = xsh_order_list_load( order_tab_frame, instr));
00143 check( residtab_size = xsh_resid_tab_get_size( attresid_tab));
00144
00145 XSH_REGDEBUG( "RESID_TAB SIZE %d", residtab_size);
00146 check(afc_name=cpl_frame_get_filename(afc_frame));
00147 check(afc_header=cpl_propertylist_load(afc_name,0));
00148
00149 if ( wave_tab_frame != NULL){
00150 check( wavesol = xsh_wavesol_load( wave_tab_frame, instr));
00151 }
00152 else{
00153 XSH_ASSURE_NOT_NULL( model_config_frame);
00154 check( xsh_model_config_load_best( model_config_frame, &cfg_mod_no_T_corr));
00155 check(xsh_model_temperature_update_frame(&model_config_frame,afc_frame,
00156 instr,&found_temp));
00157 check( xsh_model_config_load_best( model_config_frame, &config_model));
00158 }
00159
00160 check( shiftx_vect = cpl_vector_new( residtab_size));
00161 check( shifty_vect = cpl_vector_new( residtab_size));
00162 check( T_effect_x_vect = cpl_vector_new( residtab_size));
00163 check( T_effect_y_vect = cpl_vector_new( residtab_size));
00164
00165 check(arm=xsh_instrument_get_arm(instr));
00166
00167 for( iline=0; iline < residtab_size; iline++){
00168
00169 lambda = attresid_tab->lambda[iline];
00170 order = attresid_tab->order[iline];
00171 att_x = attresid_tab->xgauss[iline];
00172 att_y = attresid_tab->ygauss[iline];
00173
00174 if ( wavesol != NULL){
00175 check( cal_x = xsh_wavesol_eval_polx( wavesol, lambda, order, slit));
00176 check( cal_y = xsh_wavesol_eval_poly( wavesol, lambda, order, slit));
00177 }
00178 else{
00179 check( xsh_model_get_xy( &config_model, instr, lambda, order, slit,
00180 &cal_x, &cal_y));
00181 check( xsh_model_get_xy( &cfg_mod_no_T_corr, instr, lambda, order, slit,
00182 &cal_no_T_corr_x, &cal_no_T_corr_y));
00183
00184
00185 attresid_tab->thpre_x[iline]=cal_x;
00186 attresid_tab->thpre_y[iline]=cal_y;
00187
00188 attresid_tab->slit_index[iline]=4;
00189
00190 T_effect_x=cal_x-cal_no_T_corr_x;
00191 T_effect_y=cal_y-cal_no_T_corr_y;
00192 xsh_msg(" T_effect_x %f T_effect_y %f", T_effect_x,T_effect_y);
00193 check( cpl_vector_set( T_effect_x_vect, iline, T_effect_x));
00194 check( cpl_vector_set( T_effect_y_vect, iline, T_effect_y));
00195 }
00196
00197 cal_x = cal_x-afc_xmin+1.0;
00198 cal_y =cal_y-afc_ymin+1.0;
00199
00200 xshift = att_x-cal_x;
00201 yshift = att_y-cal_y;
00202 xsh_msg("lambda %f order %f slit %f calx %f caly %f shiftx %f shifty %f TeffX %f TeffY %f",
00203 lambda,order,slit,cal_x, cal_y,xshift,yshift, T_effect_x,T_effect_y);
00204
00205 check( cpl_vector_set( shiftx_vect, iline, xshift));
00206 check( cpl_vector_set( shifty_vect, iline, yshift));
00207 }
00208
00209 check( xshift = cpl_vector_get_median( shiftx_vect));
00210 check( yshift = cpl_vector_get_median( shifty_vect));
00211 check( T_effect_x = cpl_vector_get_median( T_effect_x_vect));
00212 check( T_effect_y = cpl_vector_get_median( T_effect_y_vect));
00213
00214 xsh_msg("Measured shift IN X %f Y %f", xshift, yshift);
00215 if ( wavesol == NULL){
00216 xsh_msg("Temperature effect at (mean) reference line in X %f Y %f",
00217 T_effect_x,T_effect_y);
00218 }
00219
00220
00221 if ( wavesol != NULL){
00222 cpl_table* tab=NULL;
00223 cpl_propertylist* plist=NULL;
00224 check( xsh_wavesol_apply_shift( wavesol, xshift, yshift));
00225 check( trace = xsh_wavesol_trace( wavesol, &lambda, &order, &slit, 1));
00226 tag = XSH_GET_TAG_FROM_ARM( XSH_WAVE_TAB_AFC, instr);
00227 sprintf( result_name, "%s.fits", tag);
00228 check( result = xsh_wavesol_save( wavesol, trace, result_name, tag));
00229 tab=cpl_table_load(result_name,1,0);
00230 plist=cpl_propertylist_load(result_name,0);
00231 cpl_propertylist_append_double(plist,XSH_QC_AFC_XSHIFT,xshift);
00232 cpl_propertylist_append_double(plist,XSH_QC_AFC_YSHIFT,yshift);
00233 cpl_propertylist_set_comment(plist,XSH_QC_AFC_XSHIFT,XSH_QC_AFC_XSHIFT_C);
00234 cpl_propertylist_set_comment(plist,XSH_QC_AFC_YSHIFT,XSH_QC_AFC_YSHIFT_C);
00235 check( cpl_table_save(tab,plist, NULL, result_name,CPL_IO_DEFAULT));
00236 xsh_free_table(&tab);
00237 xsh_free_propertylist(&plist);
00238 }
00239 else{
00240 if (arm!=XSH_ARM_NIR) {
00241 xsh_model_offset( xshift, yshift, &config_model);
00242
00243
00244
00245 }
00246 else {
00247
00248
00249
00250 check(MODEL_CONF_OPT_frame=xsh_model_pipe_anneal( model_config_frame,
00251 attresidtab_frame,
00252 2000,
00253 1.0,
00254 9,
00255 5));
00256
00257
00258
00259
00260 check( xsh_model_config_load_best(model_config_frame, &config_model));
00261 }
00262
00263 xsh_msg("file name=%s",cpl_frame_get_filename(model_config_frame));
00264 qc_head2copy=cpl_propertylist_load_regexp(cpl_frame_get_filename(model_config_frame),0,"^ESO QC MODEL",0);
00265
00266 check( model_tab = xsh_model_io_output_cfg( &config_model));
00267 tag = XSH_GET_TAG_FROM_ARM( XSH_MOD_CFG_OPT_AFC, instr);
00268 sprintf( result_name, "%s.fits", tag);
00269 check( model_header = cpl_propertylist_new());
00270 check( xsh_pfits_set_pcatg( model_header, tag));
00271 cpl_propertylist_append_double(model_header,XSH_QC_AFC_XSHIFT,xshift);
00272 cpl_propertylist_append_double(model_header,XSH_QC_AFC_YSHIFT,yshift);
00273 cpl_propertylist_set_comment(model_header,XSH_QC_AFC_XSHIFT,XSH_QC_AFC_XSHIFT_C);
00274 cpl_propertylist_set_comment(model_header,XSH_QC_AFC_YSHIFT,XSH_QC_AFC_YSHIFT_C);
00275 cpl_propertylist_append(model_header,qc_head2copy);
00276 xsh_free_propertylist(&qc_head2copy);
00277 check( cpl_table_save( model_tab, model_header, NULL, result_name,
00278 CPL_IO_DEFAULT));
00279 check( *afc_model_config_frame = cpl_frame_new());
00280 check( cpl_frame_set_filename( *afc_model_config_frame, result_name));
00281 check( cpl_frame_set_tag( *afc_model_config_frame, tag));
00282 }
00283
00284 check (xsh_order_list_apply_shift( order_tab, xshift, yshift));
00285 tag = XSH_GET_TAG_FROM_MODE( XSH_ORDER_TAB_AFC, instr);
00286 sprintf( result_name, "%s.fits", tag);
00287
00288 check( config = xsh_instrument_get_config( instr));
00289 check( afc_order_tab_result = xsh_order_list_save( order_tab, instr,
00290 result_name, tag, config->ny));
00291
00292 *afc_order_tab_frame = afc_order_tab_result;
00293
00294 cleanup:
00295 if( cpl_error_get_code() != CPL_ERROR_NONE) {
00296 xsh_free_frame( &result);
00297 xsh_free_frame( &afc_order_tab_result);
00298
00299 }
00300 xsh_resid_tab_free( &attresid_tab);
00301 xsh_order_list_free( &order_tab);
00302 xsh_free_vector(&T_effect_x_vect);
00303 xsh_free_vector(&T_effect_y_vect);
00304 xsh_free_vector( &shiftx_vect);
00305 xsh_free_vector( &shifty_vect);
00306 xsh_free_table( &trace);
00307 xsh_free_table( &model_tab);
00308 xsh_free_propertylist( &model_header);
00309 xsh_free_propertylist( &afc_header);
00310 if (wavesol==NULL) {
00311 if (arm==XSH_ARM_NIR) {
00312 xsh_free_frame(&MODEL_CONF_OPT_frame);
00313 }
00314 }
00315 xsh_wavesol_free( &wavesol);
00316
00317 return result;
00318 }
00319
00320
00321
00337
00338 cpl_frame*
00339 xsh_afcthetab_create( cpl_frame *wave_tab_frame,
00340 cpl_frame *model_config_frame,
00341 int order,
00342 cpl_frame *spectralformat_frame,
00343 cpl_frame *arclines_frame,
00344 int xmin,
00345 int ymin,
00346 xsh_instrument *instr,
00347 const int clean_tmp)
00348 {
00349
00350 cpl_frame *result = NULL;
00351 xsh_arclist *arclist = NULL;
00352 xsh_wavesol *wavesol = NULL;
00353 float lambda, slit_position;
00354 double x,y;
00355 int slit_index;
00356 int size, iline, iorder, ithe=0;
00357 xsh_the_map* themap = NULL;
00358 int the_size=0;
00359 xsh_xs_3 config_model;
00360 cpl_vector* orders = NULL;
00361 int orders_size;
00362 xsh_spectralformat_list *spectrallist = NULL;
00363 char thename[256];
00364
00365
00366 XSH_ASSURE_NOT_NULL( arclines_frame);
00367 XSH_ASSURE_NOT_NULL( instr);
00368
00369 check( arclist = xsh_arclist_load( arclines_frame));
00370
00371 check( size = xsh_arclist_get_size( arclist));
00372
00373 xsh_msg("size %d", size);
00374
00375 if ( spectralformat_frame != NULL){
00376 check( spectrallist = xsh_spectralformat_list_load(
00377 spectralformat_frame, instr));
00378
00379 for( iline=0; iline < size; iline++){
00380 check( lambda = xsh_arclist_get_wavelength(arclist, iline));
00381 check( orders = xsh_spectralformat_list_get_orders(spectrallist,
00382 lambda));
00383 if (orders != NULL){
00384 check( orders_size = cpl_vector_get_size( orders));
00385 the_size += orders_size;
00386 }
00387 xsh_free_vector( &orders);
00388 }
00389 }
00390 else{
00391 the_size = 1;
00392 }
00393
00394
00395 xsh_msg("THE SIZE %d", the_size);
00396 check( themap = xsh_the_map_create( the_size));
00397
00398 if (wave_tab_frame != NULL){
00399 check( wavesol = xsh_wavesol_load( wave_tab_frame, instr));
00400 }
00401 else{
00402 XSH_ASSURE_NOT_NULL( model_config_frame);
00403 check( xsh_model_config_load_best( model_config_frame,
00404 &config_model));
00405 }
00406
00407 for( iline=0; iline < size; iline++){
00408 check( lambda = xsh_arclist_get_wavelength(arclist, iline));
00409 slit_index = 4;
00410 slit_position = 0;
00411 if ( spectrallist != NULL){
00412 check( orders = xsh_spectralformat_list_get_orders( spectrallist,
00413 lambda));
00414 }
00415 else{
00416 orders = cpl_vector_new(1);
00417 cpl_vector_set( orders, 0, order);
00418 }
00419 if (orders != NULL){
00420 check( orders_size = cpl_vector_get_size( orders));
00421
00422 for( iorder=0; iorder < orders_size; iorder++){
00423 int cur_order;
00424
00425 check( cur_order = cpl_vector_get( orders, iorder));
00426
00427 if (wavesol != NULL){
00428 check( x = xsh_wavesol_eval_polx( wavesol, lambda, cur_order,
00429 slit_position));
00430 check( y = xsh_wavesol_eval_poly( wavesol, lambda, cur_order,
00431 slit_position));
00432 }
00433 else{
00434 check( xsh_model_get_xy( &config_model, instr, lambda, cur_order,
00435 slit_position, &x, &y));
00436 }
00437 x = x-xmin+1.0;
00438 y = y-ymin+1.0;
00439 XSH_REGDEBUG("lambda %f order %d %f %f",lambda, cur_order, x, y);
00440 check( xsh_the_map_set_arcline( themap, ithe, lambda, cur_order,
00441 slit_index, slit_position, x, y));
00442 ithe++;
00443 }
00444 }
00445 xsh_free_vector( &orders);
00446 }
00447 sprintf( thename, "AFC_THEOTAB_%s.fits",
00448 xsh_instrument_arm_tostring( instr));
00449 check( result = xsh_the_map_save( themap, thename));
00450 if(clean_tmp) {
00451 xsh_add_temporary_file(thename);
00452 }
00453
00454 cleanup:
00455 if( cpl_error_get_code() != CPL_ERROR_NONE) {
00456 xsh_free_vector( &orders);
00457 xsh_free_frame( &result);
00458 }
00459 xsh_spectralformat_list_free( &spectrallist);
00460 xsh_arclist_free( &arclist);
00461 xsh_wavesol_free( &wavesol);
00462 xsh_the_map_free( &themap);
00463
00464 return result;
00465 }
00466