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
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 if ( wavesol != NULL){
00221 check( xsh_wavesol_apply_shift( wavesol, xshift, yshift));
00222 check( trace = xsh_wavesol_trace( wavesol, &lambda, &order, &slit, 1));
00223 tag = XSH_GET_TAG_FROM_ARM( XSH_WAVE_TAB_AFC, instr);
00224 sprintf( result_name, "%s.fits", tag);
00225 check( result = xsh_wavesol_save( wavesol, trace, result_name, tag));
00226 }
00227 else{
00228 if (arm!=XSH_ARM_NIR) {
00229 xsh_model_offset( xshift, yshift, &config_model);
00230
00231
00232
00233 }
00234 else {
00235
00236
00237 check(MODEL_CONF_OPT_frame=xsh_model_pipe_anneal( model_config_frame,
00238 attresidtab_frame,
00239 2000,
00240 1.0,
00241 9,
00242 5));
00243
00244
00245
00246 check( xsh_model_config_load_best( MODEL_CONF_OPT_frame, &config_model));
00247 }
00248
00249 check( model_tab = xsh_model_io_output_cfg( &config_model));
00250 tag = XSH_GET_TAG_FROM_ARM( XSH_MOD_CFG_OPT_AFC, instr);
00251 sprintf( result_name, "%s.fits", tag);
00252 check( model_header = cpl_propertylist_new());
00253 check( xsh_pfits_set_pcatg( model_header, tag));
00254 check( cpl_table_save( model_tab, model_header, NULL, result_name,
00255 CPL_IO_DEFAULT));
00256 check( *afc_model_config_frame = cpl_frame_new());
00257 check( cpl_frame_set_filename( *afc_model_config_frame, result_name));
00258 check( cpl_frame_set_tag( *afc_model_config_frame, tag));
00259 }
00260 check (xsh_order_list_apply_shift( order_tab, xshift, yshift));
00261 tag = XSH_GET_TAG_FROM_MODE( XSH_ORDER_TAB_AFC, instr);
00262 sprintf( result_name, "%s.fits", tag);
00263
00264 check( config = xsh_instrument_get_config( instr));
00265 check( afc_order_tab_result = xsh_order_list_save( order_tab, instr,
00266 result_name, tag, config->ny));
00267
00268 *afc_order_tab_frame = afc_order_tab_result;
00269
00270 cleanup:
00271 if( cpl_error_get_code() != CPL_ERROR_NONE) {
00272 xsh_free_frame( &result);
00273 xsh_free_frame( &afc_order_tab_result);
00274
00275 }
00276 xsh_resid_tab_free( &attresid_tab);
00277 xsh_wavesol_free( &wavesol);
00278 xsh_order_list_free( &order_tab);
00279 xsh_free_vector( &shiftx_vect);
00280 xsh_free_vector( &shifty_vect);
00281 xsh_free_table( &trace);
00282 xsh_free_table( &model_tab);
00283 xsh_free_propertylist( &model_header);
00284 xsh_free_propertylist( &afc_header);
00285 if (arm!=XSH_ARM_NIR && wavesol==NULL) {
00286 xsh_free_frame(&MODEL_CONF_OPT_frame);
00287 }
00288
00289 return result;
00290 }
00291
00292
00293
00309
00310 cpl_frame* xsh_afcthetab_create( cpl_frame *wave_tab_frame,
00311 cpl_frame *model_config_frame, int order,
00312 cpl_frame *spectralformat_frame, cpl_frame *arclines_frame,
00313 int xmin, int ymin, xsh_instrument *instr)
00314 {
00315
00316 cpl_frame *result = NULL;
00317 xsh_arclist *arclist = NULL;
00318 xsh_wavesol *wavesol = NULL;
00319 float lambda, slit_position;
00320 double x,y;
00321 int slit_index;
00322 int size, iline, iorder, ithe=0;
00323 xsh_the_map* themap = NULL;
00324 int the_size=0;
00325 xsh_xs_3 config_model;
00326 cpl_vector* orders = NULL;
00327 int orders_size;
00328 xsh_spectralformat_list *spectrallist = NULL;
00329 char thename[256];
00330
00331
00332 XSH_ASSURE_NOT_NULL( arclines_frame);
00333 XSH_ASSURE_NOT_NULL( instr);
00334
00335 check( arclist = xsh_arclist_load( arclines_frame));
00336
00337 check( size = xsh_arclist_get_size( arclist));
00338
00339 xsh_msg("size %d", size);
00340
00341 if ( spectralformat_frame != NULL){
00342 check( spectrallist = xsh_spectralformat_list_load(
00343 spectralformat_frame, instr));
00344
00345 for( iline=0; iline < size; iline++){
00346 check( lambda = xsh_arclist_get_wavelength(arclist, iline));
00347 slit_index = 4;
00348 slit_position = 0;
00349 check( orders = xsh_spectralformat_list_get_orders(spectrallist,
00350 lambda));
00351 if (orders != NULL){
00352 check( orders_size = cpl_vector_get_size( orders));
00353 the_size += orders_size;
00354 }
00355 xsh_free_vector( &orders);
00356 }
00357 }
00358 else{
00359 the_size = 1;
00360 }
00361
00362
00363 xsh_msg("THE SIZE %d", the_size);
00364 check( themap = xsh_the_map_create( the_size));
00365
00366 if (wave_tab_frame != NULL){
00367 check( wavesol = xsh_wavesol_load( wave_tab_frame, instr));
00368 }
00369 else{
00370 XSH_ASSURE_NOT_NULL( model_config_frame);
00371 check( xsh_model_config_load_best( model_config_frame,
00372 &config_model));
00373 }
00374
00375 for( iline=0; iline < size; iline++){
00376 check( lambda = xsh_arclist_get_wavelength(arclist, iline));
00377 slit_index = 4;
00378 slit_position = 0;
00379 if ( spectrallist != NULL){
00380 check( orders = xsh_spectralformat_list_get_orders( spectrallist,
00381 lambda));
00382 }
00383 else{
00384 orders = cpl_vector_new(1);
00385 cpl_vector_set( orders, 0, order);
00386 }
00387 if (orders != NULL){
00388 check( orders_size = cpl_vector_get_size( orders));
00389
00390 for( iorder=0; iorder < orders_size; iorder++){
00391 int cur_order;
00392
00393 check( cur_order = cpl_vector_get( orders, iorder));
00394
00395 if (wavesol != NULL){
00396 check( x = xsh_wavesol_eval_polx( wavesol, lambda, cur_order,
00397 slit_position));
00398 check( y = xsh_wavesol_eval_poly( wavesol, lambda, cur_order,
00399 slit_position));
00400 }
00401 else{
00402 check( xsh_model_get_xy( &config_model, instr, lambda, cur_order,
00403 slit_position, &x, &y));
00404 }
00405 x = x-xmin+1.0;
00406 y = y-ymin+1.0;
00407 XSH_REGDEBUG("lambda %f order %d %f %f",lambda, cur_order, x, y);
00408 check( xsh_the_map_set_arcline( themap, ithe, lambda, cur_order,
00409 slit_index, slit_position, x, y));
00410 ithe++;
00411 }
00412 }
00413 xsh_free_vector( &orders);
00414 }
00415 sprintf( thename, "AFC_THEOTAB_%s.fits",
00416 xsh_instrument_arm_tostring( instr));
00417 check( result = xsh_the_map_save( themap, thename));
00418
00419 cleanup:
00420 if( cpl_error_get_code() != CPL_ERROR_NONE) {
00421 xsh_free_vector( &orders);
00422 xsh_free_frame( &result);
00423 }
00424 xsh_spectralformat_list_free( &spectrallist);
00425 xsh_arclist_free( &arclist);
00426 xsh_wavesol_free( &wavesol);
00427 xsh_the_map_free( &themap);
00428
00429 return result;
00430 }
00431