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
00032
00033
00038
00039
00043
00044
00045
00046
00047 #include <xsh_data_resid_tab.h>
00048 #include <xsh_utils.h>
00049 #include <xsh_error.h>
00050 #include <xsh_msg.h>
00051 #include <xsh_pfits.h>
00052 #include <cpl.h>
00053 #include <xsh_drl.h>
00054 #include <xsh_utils_table.h>
00055 #include <xsh_data_wavesol.h>
00056
00057
00058
00059
00060
00061
00083
00084 xsh_resid_tab*
00085 xsh_resid_tab_create(int size, double *lambda, double *order,
00086 double *slit, double* sn, int *slit_index,
00087 double *thpre_x, double *thpre_y,
00088 double*corr_x, double* corr_y,
00089 double *gaussian_norm,
00090 double *gaussian_fit_x, double *gaussian_fit_y,
00091 double *gaussian_sigma_x, double *gaussian_sigma_y,
00092 double *gaussian_fwhm_x, double *gaussian_fwhm_y, int* flag,
00093 xsh_wavesol *wavesol, int wavesol_type)
00094 {
00095 enum wavesol_type type = XSH_WAVESOL_GUESS;
00096 xsh_resid_tab* result = NULL;
00097 int i;
00098
00099
00100 XSH_ASSURE_NOT_ILLEGAL( size>=0);
00101 XSH_ASSURE_NOT_NULL( lambda);
00102 XSH_ASSURE_NOT_NULL( order);
00103 XSH_ASSURE_NOT_NULL( slit);
00104 XSH_ASSURE_NOT_NULL( slit_index);
00105 XSH_ASSURE_NOT_NULL( thpre_x);
00106 XSH_ASSURE_NOT_NULL( thpre_y);
00107 XSH_ASSURE_NOT_NULL( gaussian_fit_x);
00108 XSH_ASSURE_NOT_NULL( gaussian_fit_y);
00109 XSH_ASSURE_NOT_NULL( gaussian_sigma_x);
00110 XSH_ASSURE_NOT_NULL( gaussian_sigma_y);
00111 XSH_ASSURE_NOT_NULL( gaussian_fwhm_x);
00112 XSH_ASSURE_NOT_NULL( gaussian_fwhm_y);
00113 XSH_ASSURE_NOT_NULL( gaussian_norm);
00114
00115 XSH_CALLOC( result, xsh_resid_tab, 1);
00116
00117 XSH_CALLOC( result->lambda, double, size);
00118 XSH_CALLOC( result->order, double, size);
00119 XSH_CALLOC( result->slit, double, size);
00120 XSH_CALLOC( result->sn, double, size);
00121 XSH_CALLOC( result->slit_index, int, size);
00122 XSH_CALLOC( result->thpre_x, double, size);
00123 XSH_CALLOC( result->thpre_y, double, size);
00124 XSH_CALLOC( result->thcor_x, double, size);
00125 XSH_CALLOC( result->thcor_y, double, size);
00126 XSH_CALLOC( result->xgauss, double, size);
00127 XSH_CALLOC( result->ygauss, double, size);
00128 XSH_CALLOC( result->sig_xgauss, double, size);
00129 XSH_CALLOC( result->sig_ygauss, double, size);
00130 XSH_CALLOC( result->norm_gauss, double, size);
00131 XSH_CALLOC( result->fwhm_xgauss, double, size);
00132 XSH_CALLOC( result->fwhm_ygauss, double, size);
00133
00134 XSH_CALLOC( result->xpoly, double, size);
00135 XSH_CALLOC( result->ypoly, double, size);
00136 if(flag) {
00137 XSH_CALLOC( result->flag, int, size);
00138 }
00139 check (result->header = cpl_propertylist_new());
00140
00141 result->size = size;
00142 result->solution_type = wavesol_type;
00143
00144 if (wavesol != NULL) {
00145 check (type = xsh_wavesol_get_type( wavesol));
00146 }
00147 else{
00148 type = XSH_WAVESOL_UNDEFINED;
00149 }
00150
00151 for( i=0; i<size; i++){
00152 result->lambda[i] = lambda[i];
00153 result->order[i] = order[i];
00154 result->slit[i] = slit[i];
00155 if(sn != NULL) {
00156 result->sn[i] = sn[i];
00157 } else {
00158 result->sn[i]=0;
00159 }
00160 result->slit_index[i] = slit_index[i];
00161 result->thpre_x[i] = thpre_x[i];
00162 result->thpre_y[i] = thpre_y[i];
00163 result->thcor_x[i] = thpre_x[i]+corr_x[i];
00164 result->thcor_y[i] = thpre_y[i]+corr_y[i];
00165 result->xgauss[i] = gaussian_fit_x[i];
00166 result->ygauss[i] = gaussian_fit_y[i];
00167 result->sig_xgauss[i] = gaussian_sigma_x[i];
00168 result->sig_ygauss[i] = gaussian_sigma_y[i];
00169 result->fwhm_xgauss[i] = gaussian_fwhm_x[i];
00170 result->fwhm_ygauss[i] = gaussian_fwhm_y[i];
00171 result->norm_gauss[i] = gaussian_norm[i];
00172 if(flag) {
00173 result->flag[i] = flag[i];
00174 }
00175 if ( type == XSH_WAVESOL_2D){
00176 double polx, poly;
00177
00178 check( polx = xsh_wavesol_eval_polx( wavesol, lambda[i],
00179 order[i], slit[i]));
00180 check( poly = xsh_wavesol_eval_poly( wavesol, lambda[i],
00181 order[i], slit[i]));
00182 result->xpoly[i] = polx;
00183 result->ypoly[i] = poly;
00184 }
00185 else if ( type == XSH_WAVESOL_GUESS){
00186 check( result->xpoly[i] = xsh_wavesol_eval_polx( wavesol,
00187 lambda[i], order[i], slit[i])+result->thcor_x[i]);
00188 check( result->ypoly[i] = xsh_wavesol_eval_poly( wavesol,
00189 lambda[i], order[i], slit[i])+result->thcor_y[i]);
00190 }
00191 else{
00192 result->xpoly[i] = 0.0;
00193 result->ypoly[i] = 0.0;
00194 }
00195 }
00196
00197
00198
00199
00200
00201
00202
00203
00204 cleanup:
00205 if ( cpl_error_get_code() != CPL_ERROR_NONE){
00206 xsh_resid_tab_free( &result);
00207 }
00208 return result;
00209 }
00210
00211 cpl_frame*
00212 xsh_resid_tab_erase_flagged( cpl_frame* resid,const char* name)
00213 {
00214
00215 cpl_frame* result = NULL;
00216 cpl_table* tab = NULL;
00217 cpl_table* ext = NULL;
00218 const char* fname = NULL;
00219
00220 cpl_propertylist* header=NULL;
00221 int bad=0;
00222
00223 result=cpl_frame_duplicate(resid);
00224 check( fname = cpl_frame_get_filename( resid));
00225 tab=cpl_table_load(fname,1,0);
00226 header=cpl_propertylist_load(fname,0);
00227 bad=cpl_table_and_selected_int(tab,XSH_RESID_TAB_TABLE_COLNAME_FLAG,
00228 CPL_GREATER_THAN,0);
00229 cpl_table_erase_selected(tab);
00230 cpl_table_save(tab,header,NULL,name,CPL_IO_DEFAULT);
00231 cpl_frame_set_filename(result,name);
00232
00233 cleanup:
00234 xsh_free_table(&ext);
00235 xsh_free_table(&tab);
00236 xsh_free_propertylist(&header);
00237
00238 return result;
00239 }
00240
00250
00251
00252 xsh_resid_tab* xsh_resid_tab_load( cpl_frame* resid_tab_frame)
00253 {
00254 xsh_resid_tab* result = NULL;
00255 cpl_table* table = NULL;
00256 const char* tablename = NULL;
00257 int i = 0;
00258 int size = 0;
00259 double *lambda = NULL;
00260 double *order = NULL;
00261 double *slit = NULL;
00262 int *slit_index = NULL;
00263 double *thx = NULL;
00264 double *thy = NULL;
00265 double *thcorx = NULL;
00266 double *thcory = NULL;
00267 double *xgauss = NULL;
00268 double *ygauss = NULL;
00269 double *sig_xgauss = NULL;
00270 double *sig_ygauss = NULL;
00271 double *fwhm_xgauss = NULL;
00272 double *fwhm_ygauss = NULL;
00273 double *norm_gauss = NULL;
00274 double *xpoly = NULL;
00275 double *ypoly = NULL;
00276 double *thanneal_x = NULL;
00277 double *thanneal_y = NULL;
00278 int* flag=NULL;
00279 int solution_type = XSH_DETECT_ARCLINES_TYPE_POLY;
00280 const char* wavesol_type = NULL;
00281
00282 XSH_ASSURE_NOT_NULL( resid_tab_frame);
00283
00284
00285 check( tablename = cpl_frame_get_filename( resid_tab_frame));
00286
00287 XSH_TABLE_LOAD( table, tablename);
00288
00289 check( size = cpl_table_get_nrow(table));
00290
00291 XSH_CALLOC( result, xsh_resid_tab, 1);
00292 check (result->header = cpl_propertylist_load( tablename, 0));
00293 wavesol_type = xsh_pfits_get_wavesoltype( result->header);
00294 if (cpl_error_get_code() == CPL_ERROR_NONE){
00295 if ( strcmp(wavesol_type, XSH_WAVESOLTYPE_MODEL)==0){
00296 solution_type = XSH_DETECT_ARCLINES_TYPE_MODEL;
00297 }
00298 }
00299 else{
00300 xsh_msg("Can't read keyword %s", XSH_WAVESOLTYPE);
00301 cpl_error_reset();
00302 }
00303 result->size = size;
00304 result->solution_type = solution_type;
00305
00306 XSH_CALLOC( lambda, double, size);
00307 XSH_CALLOC( order, double, size);
00308 XSH_CALLOC( slit, double, size);
00309 XSH_CALLOC( slit_index, int, size);
00310 XSH_CALLOC( thx, double, size);
00311 XSH_CALLOC( thy, double, size);
00312 XSH_CALLOC( thcorx, double, size);
00313 XSH_CALLOC( thcory, double, size);
00314 XSH_CALLOC( xgauss, double, size);
00315 XSH_CALLOC( ygauss, double, size);
00316 XSH_CALLOC( sig_xgauss, double, size);
00317 XSH_CALLOC( sig_ygauss, double, size);
00318 XSH_CALLOC( fwhm_xgauss, double, size);
00319 XSH_CALLOC( fwhm_ygauss, double, size);
00320 XSH_CALLOC( norm_gauss, double, size);
00321 if (solution_type == XSH_DETECT_ARCLINES_TYPE_MODEL){
00322 XSH_CALLOC( thanneal_x, double, size);
00323 XSH_CALLOC( thanneal_y, double, size);
00324 }
00325 else{
00326 XSH_CALLOC( xpoly, double, size);
00327 XSH_CALLOC( ypoly, double, size);
00328 }
00329 XSH_CALLOC( flag, int, size);
00330
00331 for(i=0; i<size;i++){
00332 int orderv, slit_indexv;
00333 double lambdav, slitv, thxv, thyv;
00334 double thcorxv=0, thcoryv=0;
00335 double xgaussv, ygaussv, xpolyv, ypolyv;
00336 double sig_xgaussv, sig_ygaussv;
00337 double fwhm_xgaussv, fwhm_ygaussv;
00338 double norm_gaussv;
00339 int flagv;
00340
00341 check( xsh_get_table_value( table,
00342 XSH_RESID_TAB_TABLE_COLNAME_WAVELENGTH,
00343 CPL_TYPE_DOUBLE, i, &lambdav));
00344
00345 check( xsh_get_table_value( table,
00346 XSH_RESID_TAB_TABLE_COLNAME_ORDER,
00347 CPL_TYPE_INT, i, &orderv));
00348
00349 check( xsh_get_table_value( table,
00350 XSH_RESID_TAB_TABLE_COLNAME_SLITPOSITION,
00351 CPL_TYPE_DOUBLE, i, &slitv));
00352
00353 check( xsh_get_table_value( table,
00354 XSH_RESID_TAB_TABLE_COLNAME_SLITINDEX,
00355 CPL_TYPE_INT, i, &slit_indexv));
00356
00357 check( xsh_get_table_value( table,
00358 XSH_RESID_TAB_TABLE_COLNAME_XTHPRE,
00359 CPL_TYPE_DOUBLE, i, &thxv));
00360
00361 check( xsh_get_table_value( table,
00362 XSH_RESID_TAB_TABLE_COLNAME_YTHPRE,
00363 CPL_TYPE_DOUBLE, i, &thyv));
00364
00365
00366
00367
00368
00369
00370
00371
00372
00373
00374 check( xsh_get_table_value( table,
00375 XSH_RESID_TAB_TABLE_COLNAME_XGAUSS,
00376 CPL_TYPE_DOUBLE, i, &xgaussv));
00377
00378 check( xsh_get_table_value( table,
00379 XSH_RESID_TAB_TABLE_COLNAME_YGAUSS,
00380 CPL_TYPE_DOUBLE, i, &ygaussv));
00381
00382 check( xsh_get_table_value( table,
00383 XSH_RESID_TAB_TABLE_COLNAME_SIGMAXGAUSS,
00384 CPL_TYPE_DOUBLE, i, &sig_xgaussv));
00385
00386 check( xsh_get_table_value( table,
00387 XSH_RESID_TAB_TABLE_COLNAME_SIGMAYGAUSS,
00388 CPL_TYPE_DOUBLE, i, &sig_ygaussv));
00389
00390
00391 check( xsh_get_table_value( table,
00392 XSH_RESID_TAB_TABLE_COLNAME_FWHMXGAUSS,
00393 CPL_TYPE_DOUBLE, i, &fwhm_xgaussv));
00394
00395 check( xsh_get_table_value( table,
00396 XSH_RESID_TAB_TABLE_COLNAME_FWHMYGAUSS,
00397 CPL_TYPE_DOUBLE, i, &fwhm_ygaussv));
00398
00399 check( xsh_get_table_value( table,
00400 XSH_RESID_TAB_TABLE_COLNAME_NORMGAUSS,
00401 CPL_TYPE_DOUBLE, i, &norm_gaussv));
00402
00403 if (solution_type == XSH_DETECT_ARCLINES_TYPE_MODEL){
00404 check( xsh_get_table_value( table,
00405 XSH_RESID_TAB_TABLE_COLNAME_XTHANNEAL,
00406 CPL_TYPE_DOUBLE, i, &xpolyv));
00407 thanneal_x[i] = xpolyv;
00408 check( xsh_get_table_value( table,
00409 XSH_RESID_TAB_TABLE_COLNAME_YTHANNEAL,
00410 CPL_TYPE_DOUBLE, i, &ypolyv));
00411 thanneal_y[i] = ypolyv;
00412 }
00413 else{
00414 check( xsh_get_table_value( table,
00415 XSH_RESID_TAB_TABLE_COLNAME_XPOLY,
00416 CPL_TYPE_DOUBLE, i, &xpolyv));
00417 xpoly[i] = xpolyv;
00418 check( xsh_get_table_value( table,
00419 XSH_RESID_TAB_TABLE_COLNAME_YPOLY,
00420 CPL_TYPE_DOUBLE, i, &ypolyv));
00421 ypoly[i] = ypolyv;
00422 }
00423 if (cpl_table_has_column(table,XSH_RESID_TAB_TABLE_COLNAME_FLAG)) {
00424 check( xsh_get_table_value( table,
00425 XSH_RESID_TAB_TABLE_COLNAME_SLITINDEX,
00426 CPL_TYPE_INT, i, &flagv));
00427 }
00428 lambda[i] = lambdav;
00429 order[i] = (double) orderv;
00430 slit[i] = slitv;
00431 slit_index[i] = slit_indexv;
00432 thx[i] = thxv;
00433 thy[i] = thyv;
00434 thcorx[i] =thcorxv;
00435 thcory[i] = thcoryv;
00436 xgauss[i] = xgaussv;
00437 ygauss[i] = ygaussv;
00438 sig_xgauss[i] = sig_xgaussv;
00439 sig_ygauss[i] = sig_ygaussv;
00440 fwhm_xgauss[i] = fwhm_xgaussv;
00441 fwhm_ygauss[i] = fwhm_ygaussv;
00442 norm_gauss[i] = norm_gaussv;
00443 flag[i]=flagv;
00444 }
00445
00446 result->lambda = lambda;
00447 result->order = order;
00448 result->slit = slit;
00449 result->slit_index = slit_index;
00450 result->thpre_x = thx;
00451 result->thpre_y = thy;
00452 result->thcor_x = thcorx;
00453 result->thcor_y = thcory;
00454 result->xgauss = xgauss;
00455 result->ygauss = ygauss;
00456 result->sig_xgauss = sig_xgauss;
00457 result->sig_ygauss = sig_ygauss;
00458 result->fwhm_xgauss = fwhm_xgauss;
00459 result->fwhm_ygauss = fwhm_ygauss;
00460 result->norm_gauss = norm_gauss;
00461 if ( solution_type == XSH_DETECT_ARCLINES_TYPE_MODEL){
00462 result->thanneal_x = thanneal_x;
00463 result->thanneal_y = thanneal_y;
00464 }
00465 else{
00466 result->xpoly = xpoly;
00467 result->ypoly = ypoly;
00468 }
00469 result->flag = flag;
00470
00471 cleanup:
00472 if (cpl_error_get_code () != CPL_ERROR_NONE) {
00473 xsh_error_msg("can't load frame %s",
00474 cpl_frame_get_filename(resid_tab_frame));
00475 xsh_resid_tab_free(&result);
00476 }
00477 XSH_TABLE_FREE( table);
00478 return result;
00479 }
00480
00481
00489
00490 void xsh_resid_tab_free( xsh_resid_tab** resid) {
00491 if ( resid && *resid) {
00492 XSH_FREE( (*resid)->lambda);
00493 XSH_FREE( (*resid)->order);
00494 XSH_FREE( (*resid)->slit);
00495 XSH_FREE( (*resid)->slit_index);
00496 XSH_FREE( (*resid)->thpre_x);
00497 XSH_FREE( (*resid)->thpre_y);
00498 XSH_FREE( (*resid)->thcor_x);
00499 XSH_FREE( (*resid)->thcor_y);
00500 XSH_FREE( (*resid)->xgauss);
00501 XSH_FREE( (*resid)->ygauss);
00502 XSH_FREE( (*resid)->sig_xgauss);
00503 XSH_FREE( (*resid)->sig_ygauss);
00504 XSH_FREE( (*resid)->fwhm_xgauss);
00505 XSH_FREE( (*resid)->fwhm_ygauss);
00506 XSH_FREE( (*resid)->norm_gauss);
00507 XSH_FREE( (*resid)->xpoly);
00508 XSH_FREE( (*resid)->ypoly);
00509 XSH_FREE( (*resid)->thanneal_x);
00510 XSH_FREE( (*resid)->thanneal_y);
00511 XSH_FREE( (*resid)->flag);
00512 if( (*resid)->sn) XSH_FREE( (*resid)->sn) ;
00513 xsh_free_propertylist( &(*resid)->header);
00514 cpl_free(*resid);
00515 }
00516 *resid = NULL;
00517 }
00518
00519
00520
00529
00530 void xsh_resid_tab_log( xsh_resid_tab* resid, const char* filename)
00531 {
00532
00533 FILE* logfile = NULL;
00534 int i = 0;
00535
00536 XSH_ASSURE_NOT_NULL( resid);
00537 XSH_ASSURE_NOT_NULL( filename);
00538
00539 logfile = fopen(filename,"w");
00540
00541 if ( resid->solution_type == XSH_DETECT_ARCLINES_TYPE_POLY) {
00542 fprintf( logfile,
00543 "# lambda order slit thx, thy, gaussx, gaussy, xpoly, ypoly");
00544 for ( i=0; i< resid->size; i++){
00545 double lambda, order, slit;
00546
00547 lambda = resid->lambda[i];
00548 order = resid->order[i];
00549 slit = resid->slit[i];
00550 fprintf( logfile,
00551 "%.8lg %.8lg %.8lg %.8lg %.8lg %.8lg %.8lg %.8lg %.8lg\n",
00552 lambda, order, slit, resid->thpre_x[i], resid->thpre_y[i],
00553 resid->xgauss[i], resid->ygauss[i],
00554 resid->xpoly[i], resid->ypoly[i]
00555 );
00556 }
00557 }
00558 else{
00559 fprintf( logfile,
00560 "# lambda order slit thx, thy, gaussx, gaussy, thanneal_x, thanneal_y");
00561 for ( i=0; i< resid->size; i++){
00562 double lambda, order, slit;
00563
00564 lambda = resid->lambda[i];
00565 order = resid->order[i];
00566 slit = resid->slit[i];
00567 fprintf( logfile,
00568 "%.8lg %.8lg %.8lg %.8lg %.8lg %.8lg %.8lg %.8lg %.8lg\n",
00569 lambda, order, slit, resid->thpre_x[i], resid->thpre_y[i],
00570 resid->xgauss[i], resid->ygauss[i],
00571 resid->thanneal_x[i], resid->thanneal_y[i]
00572 );
00573 }
00574 }
00575 fclose( logfile);
00576 cleanup:
00577 return;
00578 }
00579
00580
00592
00593 cpl_frame* xsh_resid_tab_save( xsh_resid_tab* resid, const char* filename,
00594 xsh_instrument* instr,const char* tag)
00595 {
00596 cpl_frame *result = NULL ;
00597 cpl_table *table = NULL;
00598 cpl_propertylist *header = NULL;
00599 int i = 0;
00600
00601 double residx_min=0;
00602 double residx_max=0;
00603 double residx_rms=0;
00604
00605 double residy_min=0;
00606 double residy_max=0;
00607 double residy_rms=0;
00608
00609 XSH_ASSURE_NOT_NULL( resid);
00610 XSH_ASSURE_NOT_NULL( filename);
00611 XSH_ASSURE_NOT_NULL( instr);
00612
00613
00614 check( table = cpl_table_new( XSH_RESID_TAB_TABLE_NB_COL));
00615 header = resid->header;
00616
00617 if (resid->solution_type == XSH_DETECT_ARCLINES_TYPE_POLY){
00618 check( xsh_pfits_set_qc( header, &(resid->mean_diff_poly_fit_x),
00619 XSH_QC_FMTCHK_POLY_DIFFXAVG, instr));
00620 check( xsh_pfits_set_qc( header, &(resid->median_diff_poly_fit_x),
00621 XSH_QC_FMTCHK_POLY_DIFFXMED, instr));
00622 check( xsh_pfits_set_qc( header, &(resid->stdev_diff_poly_fit_x),
00623 XSH_QC_FMTCHK_POLY_DIFFXSTD, instr));
00624 check( xsh_pfits_set_qc( header, &(resid->mean_diff_poly_fit_y),
00625 XSH_QC_FMTCHK_POLY_DIFFYAVG, instr));
00626 check( xsh_pfits_set_qc( header, &(resid->median_diff_poly_fit_y),
00627 XSH_QC_FMTCHK_POLY_DIFFYMED, instr));
00628 check( xsh_pfits_set_qc( header, &(resid->stdev_diff_poly_fit_y),
00629 XSH_QC_FMTCHK_POLY_DIFFYSTD, instr));
00630 }
00631
00632
00633 XSH_TABLE_NEW_COL(table, XSH_RESID_TAB_TABLE_COLNAME_WAVELENGTH,
00634 XSH_RESID_TAB_TABLE_UNIT_WAVELENGTH, CPL_TYPE_DOUBLE);
00635
00636 XSH_TABLE_NEW_COL(table, XSH_RESID_TAB_TABLE_COLNAME_ORDER,
00637 XSH_RESID_TAB_TABLE_UNIT_ORDER, CPL_TYPE_INT);
00638
00639 XSH_TABLE_NEW_COL(table, XSH_RESID_TAB_TABLE_COLNAME_SLITPOSITION,
00640 XSH_RESID_TAB_TABLE_UNIT_SLITPOSITION, CPL_TYPE_DOUBLE);
00641
00642 XSH_TABLE_NEW_COL(table, XSH_RESID_TAB_TABLE_COLNAME_SLITINDEX,
00643 XSH_RESID_TAB_TABLE_UNIT_SLITINDEX, CPL_TYPE_INT);
00644
00645 XSH_TABLE_NEW_COL(table, XSH_RESID_TAB_TABLE_COLNAME_XTHPRE,
00646 XSH_RESID_TAB_TABLE_UNIT_XTHPRE, CPL_TYPE_DOUBLE);
00647
00648 XSH_TABLE_NEW_COL(table, XSH_RESID_TAB_TABLE_COLNAME_YTHPRE,
00649 XSH_RESID_TAB_TABLE_UNIT_YTHPRE, CPL_TYPE_DOUBLE);
00650
00651
00652
00653
00654
00655
00656
00657
00658
00659 XSH_TABLE_NEW_COL(table, XSH_RESID_TAB_TABLE_COLNAME_XGAUSS,
00660 XSH_RESID_TAB_TABLE_UNIT_XGAUSS, CPL_TYPE_DOUBLE);
00661
00662 XSH_TABLE_NEW_COL(table, XSH_RESID_TAB_TABLE_COLNAME_YGAUSS,
00663 XSH_RESID_TAB_TABLE_UNIT_YGAUSS, CPL_TYPE_DOUBLE);
00664
00665 XSH_TABLE_NEW_COL(table, XSH_RESID_TAB_TABLE_COLNAME_SIGMAXGAUSS,
00666 XSH_RESID_TAB_TABLE_UNIT_SIGMAXGAUSS, CPL_TYPE_DOUBLE);
00667
00668 XSH_TABLE_NEW_COL(table, XSH_RESID_TAB_TABLE_COLNAME_SIGMAYGAUSS,
00669 XSH_RESID_TAB_TABLE_UNIT_SIGMAYGAUSS, CPL_TYPE_DOUBLE);
00670
00671 XSH_TABLE_NEW_COL(table, XSH_RESID_TAB_TABLE_COLNAME_FWHMXGAUSS,
00672 XSH_RESID_TAB_TABLE_UNIT_SIGMAXGAUSS, CPL_TYPE_DOUBLE);
00673
00674 XSH_TABLE_NEW_COL(table, XSH_RESID_TAB_TABLE_COLNAME_FWHMYGAUSS,
00675 XSH_RESID_TAB_TABLE_UNIT_SIGMAYGAUSS, CPL_TYPE_DOUBLE);
00676
00677 XSH_TABLE_NEW_COL(table, XSH_RESID_TAB_TABLE_COLNAME_NORMGAUSS,
00678 XSH_RESID_TAB_TABLE_UNIT_NORMGAUSS, CPL_TYPE_DOUBLE);
00679
00680 XSH_TABLE_NEW_COL(table, XSH_RESID_TAB_TABLE_COLNAME_SN,
00681 XSH_RESID_TAB_TABLE_UNIT_SN, CPL_TYPE_DOUBLE);
00682
00683 XSH_TABLE_NEW_COL(table, XSH_RESID_TAB_TABLE_COLNAME_XPOLY,
00684 XSH_RESID_TAB_TABLE_UNIT_XPOLY, CPL_TYPE_DOUBLE);
00685
00686 XSH_TABLE_NEW_COL(table, XSH_RESID_TAB_TABLE_COLNAME_YPOLY,
00687 XSH_RESID_TAB_TABLE_UNIT_YPOLY, CPL_TYPE_DOUBLE);
00688
00689 XSH_TABLE_NEW_COL(table, XSH_RESID_TAB_TABLE_COLNAME_XTHANNEAL,
00690 XSH_RESID_TAB_TABLE_UNIT_XTHANNEAL, CPL_TYPE_DOUBLE);
00691
00692 XSH_TABLE_NEW_COL(table, XSH_RESID_TAB_TABLE_COLNAME_YTHANNEAL,
00693 XSH_RESID_TAB_TABLE_UNIT_YTHANNEAL, CPL_TYPE_DOUBLE);
00694
00695 XSH_TABLE_NEW_COL(table, XSH_RESID_TAB_TABLE_COLNAME_RESIDXPOLY,
00696 XSH_RESID_TAB_TABLE_UNIT_RESIDXPOLY, CPL_TYPE_DOUBLE);
00697
00698 XSH_TABLE_NEW_COL(table, XSH_RESID_TAB_TABLE_COLNAME_RESIDYPOLY,
00699 XSH_RESID_TAB_TABLE_UNIT_RESIDYPOLY, CPL_TYPE_DOUBLE);
00700
00701 XSH_TABLE_NEW_COL(table, XSH_RESID_TAB_TABLE_COLNAME_RESIDXMODEL,
00702 XSH_RESID_TAB_TABLE_UNIT_RESIDXMODEL, CPL_TYPE_DOUBLE);
00703
00704 XSH_TABLE_NEW_COL(table, XSH_RESID_TAB_TABLE_COLNAME_RESIDYMODEL,
00705 XSH_RESID_TAB_TABLE_UNIT_RESIDYMODEL, CPL_TYPE_DOUBLE);
00706
00707 if(resid->flag) {
00708
00709 XSH_TABLE_NEW_COL(table, XSH_RESID_TAB_TABLE_COLNAME_FLAG,
00710 XSH_RESID_TAB_TABLE_UNIT_FLAG, CPL_TYPE_INT);
00711
00712 }
00713 check(cpl_table_set_size( table, resid->size));
00714
00715
00716 for (i=0; i<resid->size; i++) {
00717 double lambda, order, slit,sn;
00718 int slit_index;
00719 double thpre_x, thpre_y;
00720 double thcor_x, thcor_y;
00721 double xgauss, ygauss;
00722 double xpoly, ypoly;
00723 double sig_xgauss, sig_ygauss;
00724 double fwhm_xgauss, fwhm_ygauss;
00725 double norm_gauss;
00726 int flag;
00727 lambda = resid->lambda[i];
00728 order = resid->order[i];
00729 slit = resid->slit[i];
00730 sn = resid->sn[i];
00731
00732 slit_index = resid->slit_index[i];
00733 thpre_x = resid->thpre_x[i];
00734 thpre_y = resid->thpre_y[i];
00735 thcor_x = resid->thcor_x[i];
00736 thcor_y = resid->thcor_y[i];
00737 xgauss = resid->xgauss[i];
00738 ygauss = resid->ygauss[i];
00739 sig_xgauss = resid->sig_xgauss[i];
00740 sig_ygauss = resid->sig_ygauss[i];
00741 fwhm_xgauss = resid->fwhm_xgauss[i];
00742 fwhm_ygauss = resid->fwhm_ygauss[i];
00743 norm_gauss = resid->norm_gauss[i];
00744 xpoly = resid->xpoly[i];
00745 ypoly = resid->ypoly[i];
00746 if(resid->flag) {
00747 flag = resid->flag[i];
00748 }
00749 check(cpl_table_set_double(table,XSH_RESID_TAB_TABLE_COLNAME_WAVELENGTH,
00750 i, lambda));
00751 check(cpl_table_set_double(table,XSH_RESID_TAB_TABLE_COLNAME_SN,
00752 i, sn));
00753
00754 check(cpl_table_set(table,XSH_RESID_TAB_TABLE_COLNAME_ORDER,
00755 i, order));
00756 check(cpl_table_set_double(table,XSH_RESID_TAB_TABLE_COLNAME_SLITPOSITION,
00757 i, slit));
00758 check(cpl_table_set_int(table,XSH_RESID_TAB_TABLE_COLNAME_SLITINDEX,
00759 i, slit_index));
00760 check(cpl_table_set_double(table,XSH_RESID_TAB_TABLE_COLNAME_XTHPRE,
00761 i, thpre_x));
00762 check(cpl_table_set_double(table,XSH_RESID_TAB_TABLE_COLNAME_YTHPRE,
00763 i, thpre_y));
00764
00765
00766
00767
00768
00769
00770
00771
00772 check(cpl_table_set_double(table,XSH_RESID_TAB_TABLE_COLNAME_XGAUSS,
00773 i, xgauss));
00774 check(cpl_table_set_double(table,XSH_RESID_TAB_TABLE_COLNAME_YGAUSS,
00775 i, ygauss));
00776 check(cpl_table_set_double(table,XSH_RESID_TAB_TABLE_COLNAME_SIGMAXGAUSS,
00777 i, sig_xgauss));
00778 check(cpl_table_set_double(table,XSH_RESID_TAB_TABLE_COLNAME_SIGMAYGAUSS,
00779 i, sig_ygauss));
00780
00781 check(cpl_table_set_double(table,XSH_RESID_TAB_TABLE_COLNAME_FWHMXGAUSS,
00782 i, fwhm_xgauss));
00783 check(cpl_table_set_double(table,XSH_RESID_TAB_TABLE_COLNAME_FWHMYGAUSS,
00784 i, fwhm_ygauss));
00785 check(cpl_table_set_double(table,XSH_RESID_TAB_TABLE_COLNAME_NORMGAUSS,
00786 i, norm_gauss));
00787
00788 if (resid->solution_type == XSH_DETECT_ARCLINES_TYPE_POLY){
00789 check(cpl_table_set_double(table,XSH_RESID_TAB_TABLE_COLNAME_XPOLY,
00790 i, xpoly));
00791 check(cpl_table_set_double(table,XSH_RESID_TAB_TABLE_COLNAME_YPOLY,
00792 i, ypoly));
00793 check(cpl_table_set_double(table,XSH_RESID_TAB_TABLE_COLNAME_RESIDXPOLY,
00794 i, xpoly-xgauss));
00795 check(cpl_table_set_double(table,XSH_RESID_TAB_TABLE_COLNAME_RESIDYPOLY,
00796 i, ypoly-ygauss));
00797 }
00798 if(resid->flag) {
00799
00800 check(cpl_table_set_int(table,XSH_RESID_TAB_TABLE_COLNAME_FLAG,
00801 i, flag));
00802 }
00803 }
00804
00805
00806 if (resid->solution_type == XSH_DETECT_ARCLINES_TYPE_POLY){
00807 check( xsh_pfits_set_wavesoltype( header, XSH_WAVESOLTYPE_POLYNOMIAL));
00808
00809 check(residx_min=cpl_table_get_column_min(table,XSH_RESID_TAB_TABLE_COLNAME_RESIDXPOLY));
00810 check(residx_max=cpl_table_get_column_max(table,XSH_RESID_TAB_TABLE_COLNAME_RESIDXPOLY));
00811 check(residx_rms=cpl_table_get_column_stdev(table,XSH_RESID_TAB_TABLE_COLNAME_RESIDXPOLY));
00812
00813 check(residy_min=cpl_table_get_column_min(table,XSH_RESID_TAB_TABLE_COLNAME_RESIDYPOLY));
00814 check(residy_max=cpl_table_get_column_max(table,XSH_RESID_TAB_TABLE_COLNAME_RESIDYPOLY));
00815 check(residy_rms=cpl_table_get_column_stdev(table,XSH_RESID_TAB_TABLE_COLNAME_RESIDYPOLY));
00816
00817 cpl_propertylist_append_double(header,"ESO QC POLY RESX_MIN",residx_min);
00818 cpl_propertylist_append_double(header,"ESO QC POLY RESX_MAX",residx_max);
00819 cpl_propertylist_append_double(header,"ESO QC POLY RESX_RMS",residx_rms);
00820
00821
00822 cpl_propertylist_append_double(header,"ESO QC POLY RESY_MIN",residy_min);
00823 cpl_propertylist_append_double(header,"ESO QC POLY RESY_MAX",residy_max);
00824 cpl_propertylist_append_double(header,"ESO QC POLY RESY_RMS",residy_rms);
00825
00826 }
00827 else{
00828 int size;
00829
00830 size = cpl_propertylist_get_size( header);
00831 XSH_REGDEBUG("write MODEL in header size %d", size);
00832 check( xsh_pfits_set_wavesoltype( header, XSH_WAVESOLTYPE_MODEL));
00833 size = cpl_propertylist_get_size( header);
00834 XSH_REGDEBUG("write MODEL in header new size %d", size);
00835
00836
00837
00838
00839 }
00840
00841
00842 XSH_REGDEBUG("save filename %s", filename);
00843 check( xsh_pfits_set_pcatg(header, tag));
00844 check( cpl_table_save(table, header,NULL,filename, CPL_IO_DEFAULT));
00845
00846
00847 check(result=xsh_frame_product(filename,
00848 tag,
00849 CPL_FRAME_TYPE_TABLE,
00850 CPL_FRAME_GROUP_PRODUCT,
00851 CPL_FRAME_LEVEL_TEMPORARY));
00852
00853
00854
00855
00856 cleanup:
00857
00858 if (cpl_error_get_code() != CPL_ERROR_NONE){
00859 xsh_free_frame(&result);
00860 }
00861 XSH_TABLE_FREE( table);
00862 return result ;
00863 }
00864
00865
00874 RESID_TAB_PROPERTY_GET ( size, int, 0)
00875
00876
00884 double * xsh_resid_tab_get_lambda_data(xsh_resid_tab *resid)
00885 {
00886 double * result = NULL ;
00887
00888 XSH_ASSURE_NOT_NULL( resid);
00889 result = resid->lambda ;
00890
00891 cleanup:
00892 return result;
00893 }
00894
00903 double * xsh_resid_tab_get_order_data(xsh_resid_tab *resid)
00904 {
00905 double * result = NULL ;
00906
00907 XSH_ASSURE_NOT_NULL( resid);
00908 result = resid->order ;
00909
00910 cleanup:
00911 return result;
00912 }
00913
00922 double * xsh_resid_tab_get_slitpos_data(xsh_resid_tab *resid)
00923 {
00924 double * result = NULL ;
00925
00926 XSH_ASSURE_NOT_NULL( resid);
00927 result = resid->slit ;
00928
00929 cleanup:
00930 return result;
00931 }
00932
00941 RESID_TAB_PROPERTY_GET ( slit_index, int*, NULL)
00942
00943
00951 double * xsh_resid_tab_get_thpre_x_data(xsh_resid_tab *resid)
00952 {
00953 double * result = NULL ;
00954
00955 XSH_ASSURE_NOT_NULL( resid);
00956 result = resid->thpre_x ;
00957
00958 cleanup:
00959 return result;
00960 }
00961
00970 double * xsh_resid_tab_get_thpre_y_data(xsh_resid_tab *resid)
00971 {
00972 double * result = NULL ;
00973
00974 XSH_ASSURE_NOT_NULL( resid);
00975 result = resid->thpre_y ;
00976
00977 cleanup:
00978 return result;
00979 }
00980
00989 double * xsh_resid_tab_get_xgauss_data(xsh_resid_tab *resid)
00990 {
00991 double * result = NULL ;
00992
00993 XSH_ASSURE_NOT_NULL( resid);
00994 result = resid->xgauss;
00995
00996 cleanup:
00997 return result;
00998 }
00999
01008 double * xsh_resid_tab_get_ygauss_data(xsh_resid_tab *resid)
01009 {
01010 double * result = NULL ;
01011
01012 XSH_ASSURE_NOT_NULL( resid);
01013 result = resid->ygauss ;
01014
01015 cleanup:
01016 return result;
01017 }
01018
01019
01020
01021 cpl_error_code
01022 xsh_frame_table_resid_merge(cpl_frame* self, cpl_frame* right,
01023 const int solution_type)
01024 {
01025 cpl_table* stab=NULL;
01026 cpl_table* rtab=NULL;
01027 cpl_propertylist* shead=NULL;
01028 cpl_propertylist* rhead=NULL;
01029
01030 const char* sname=NULL;
01031 const char* rname=NULL;
01032 const char* tag=NULL;
01033 int i=0;
01034 int srow=0;
01035 int rrow=0;
01036 double wtol=0.001;
01037
01038 double* pWaveSelf=NULL;
01039 int* pFlagSelf=NULL;
01040 int* pSlitIndexSelf=NULL;
01041 double* pXThAnnealSelf=NULL;
01042 double* pYThAnnealSelf=NULL;
01043 double* pXpolySelf=NULL;
01044 double* pYpolySelf=NULL;
01045
01046 double* pResidXpolySelf=NULL;
01047 double* pResidYpolySelf=NULL;
01048 double* pResidXmodelSelf=NULL;
01049 double* pResidYmodelSelf=NULL;
01050
01051
01052 double* pWaveRight=NULL;
01053 int* pSlitIndexRight=NULL;
01054 double* pXThAnnealRight=NULL;
01055 double* pYThAnnealRight=NULL;
01056 double* pXpolyRight=NULL;
01057 double* pYpolyRight=NULL;
01058
01059 double* pResidXpolyRight=NULL;
01060 double* pResidYpolyRight=NULL;
01061 double* pResidXmodelRight=NULL;
01062 double* pResidYmodelRight=NULL;
01063 double itol=0.001;
01064 int k=0;
01065
01066 sname=cpl_frame_get_filename(self);
01067 rname=cpl_frame_get_filename(right);
01068 tag=cpl_frame_get_filename(right);
01069
01070 shead=cpl_propertylist_load(sname,0);
01071 rhead=cpl_propertylist_load(rname,0);
01072
01073 stab=cpl_table_load(sname,1,0);
01074 rtab=cpl_table_load(rname,1,0);
01075 srow=cpl_table_get_nrow(stab);
01076 rrow=cpl_table_get_nrow(stab);
01077
01078 pWaveRight=cpl_table_get_data_double(rtab,XSH_RESID_TAB_TABLE_COLNAME_WAVELENGTH);
01079 pSlitIndexRight=cpl_table_get_data_int(rtab,XSH_RESID_TAB_TABLE_COLNAME_SLITINDEX);
01080 pXThAnnealRight=cpl_table_get_data_double(rtab,XSH_RESID_TAB_TABLE_COLNAME_XTHANNEAL);
01081 pYThAnnealRight=cpl_table_get_data_double(rtab,XSH_RESID_TAB_TABLE_COLNAME_YTHANNEAL);
01082
01083 pXpolyRight=cpl_table_get_data_double(rtab,XSH_RESID_TAB_TABLE_COLNAME_XPOLY);
01084 pYpolyRight=cpl_table_get_data_double(rtab,XSH_RESID_TAB_TABLE_COLNAME_YPOLY);
01085
01086 pResidXpolyRight=cpl_table_get_data_double(rtab,XSH_RESID_TAB_TABLE_COLNAME_RESIDXPOLY);
01087 pResidYpolyRight=cpl_table_get_data_double(rtab,XSH_RESID_TAB_TABLE_COLNAME_RESIDYPOLY);
01088 pResidXmodelRight=cpl_table_get_data_double(rtab,XSH_RESID_TAB_TABLE_COLNAME_RESIDXMODEL);
01089 pResidYmodelRight=cpl_table_get_data_double(rtab,XSH_RESID_TAB_TABLE_COLNAME_RESIDYMODEL);
01090
01091
01092 pWaveSelf=cpl_table_get_data_double(stab,XSH_RESID_TAB_TABLE_COLNAME_WAVELENGTH);
01093 pSlitIndexSelf=cpl_table_get_data_int(stab,XSH_RESID_TAB_TABLE_COLNAME_SLITINDEX);
01094 pFlagSelf=cpl_table_get_data_int(stab,XSH_RESID_TAB_TABLE_COLNAME_FLAG);
01095 pXThAnnealSelf=cpl_table_get_data_double(stab,XSH_RESID_TAB_TABLE_COLNAME_XTHANNEAL);
01096 pYThAnnealSelf=cpl_table_get_data_double(stab,XSH_RESID_TAB_TABLE_COLNAME_YTHANNEAL);
01097
01098 pXpolySelf=cpl_table_get_data_double(stab,XSH_RESID_TAB_TABLE_COLNAME_XPOLY);
01099 pYpolySelf=cpl_table_get_data_double(stab,XSH_RESID_TAB_TABLE_COLNAME_YPOLY);
01100
01101 pResidXpolySelf=cpl_table_get_data_double(stab,XSH_RESID_TAB_TABLE_COLNAME_RESIDXPOLY);
01102 pResidYpolySelf=cpl_table_get_data_double(stab,XSH_RESID_TAB_TABLE_COLNAME_RESIDYPOLY);
01103 pResidXmodelSelf=cpl_table_get_data_double(stab,XSH_RESID_TAB_TABLE_COLNAME_RESIDXMODEL);
01104 pResidYmodelSelf=cpl_table_get_data_double(stab,XSH_RESID_TAB_TABLE_COLNAME_RESIDYMODEL);
01105
01106
01107 for(i=0;i<srow;i++) {
01108
01109 for(k=0;k<rrow;k++) {
01110 if( (fabs(pWaveSelf[i]-pWaveRight[k]) < wtol) &&
01111 (fabs(pSlitIndexSelf[i]-pSlitIndexRight[k])< itol) &&
01112 pFlagSelf[i] == 0) {
01113 if (solution_type == XSH_DETECT_ARCLINES_TYPE_MODEL) {
01114 pXThAnnealSelf[i]=pXThAnnealRight[k];
01115 pYThAnnealSelf[i]=pYThAnnealRight[k];
01116 pResidXmodelSelf[i]=pResidXmodelRight[k];
01117 pResidYmodelSelf[i]=pResidYmodelRight[k];
01118 } else {
01119 pXpolySelf[i]=pXpolyRight[k];
01120 pYpolySelf[i]=pYpolyRight[k];
01121 pResidXpolySelf[i]=pResidXpolyRight[k];
01122 pResidYpolySelf[i]=pResidYpolyRight[k];
01123
01124 }
01125 }
01126 }
01127 }
01128
01129 check(cpl_table_save(stab,rhead,NULL,sname,CPL_IO_DEFAULT));
01130
01131
01132 cleanup:
01133 xsh_free_table(&stab);
01134 xsh_free_table(&rtab);
01135 xsh_free_propertylist(&shead);
01136 xsh_free_propertylist(&rhead);
01137
01138 return cpl_error_get_code();
01139
01140 }