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
00036
00037
00041
00042
00043
00044
00045 #include <math.h>
00046 #include <xsh_data_order.h>
00047 #include <xsh_utils.h>
00048 #include <xsh_error.h>
00049 #include <xsh_utils_wrappers.h>
00050 #include <xsh_msg.h>
00051 #include <xsh_pfits.h>
00052 #include <xsh_dfs.h>
00053 #include <cpl.h>
00054 #include <xsh_utils_table.h>
00055 #include <xsh_data_instrument.h>
00056 #include <xsh_data_spectralformat.h>
00057
00058
00059
00060
00067 void xsh_order_list_dump( xsh_order_list* list, const char * fname )
00068 {
00069 int i ;
00070 FILE * fout ;
00071
00072 if ( fname == NULL ) fout = stdout ;
00073 else fout = fopen( fname, "w" ) ;
00074 fprintf( fout, "Found %d orders\n", list->size ) ;
00075
00076 for( i = 0 ; i<list->size ; i++ ) {
00077 int degree, j ;
00078
00079 fprintf( fout, "order: %d, ", list->list[i].absorder ) ;
00080 fprintf( fout, "Starty: %d, Endy: %d\n", list->list[i].starty,
00081 list->list[i].endy ) ;
00082 degree = cpl_polynomial_get_degree( list->list[i].cenpoly ) ;
00083 for( j = 0 ; j<= degree ; j++ )
00084 fprintf( fout, " %lf ",
00085 cpl_polynomial_get_coeff( list->list[i].cenpoly, &j ) ) ;
00086 for( j = 0 ; j<= degree ; j++ )
00087 fprintf( fout, "%lf ",
00088 cpl_polynomial_get_coeff( list->list[i].edglopoly, &j ) ) ;
00089 for( j = 0 ; j<= degree ; j++ )
00090 fprintf( fout, " %lf ",
00091 cpl_polynomial_get_coeff( list->list[i].edguppoly, &j ) ) ;
00092 fprintf( fout, "\n" ) ;
00093 }
00094 if ( fname != NULL ) fclose( fout ) ;
00095 }
00096
00104 void xsh_order_list_verify( xsh_order_list * list, int ny )
00105 {
00106 int i ;
00107
00108
00109
00110
00111 for( i = 0 ; i<list->size ; i++ )
00112 if ( list->list[i].endy <= list->list[i].starty ) {
00113 list->list[i].endy = ny;
00114 list->list[i].starty = 1;
00115 }
00116
00117 return ;
00118 }
00119
00120
00128
00129 xsh_order_list* xsh_order_list_new( int size)
00130 {
00131 xsh_order_list* result = NULL;
00132
00133 XSH_ASSURE_NOT_ILLEGAL( size > 0);
00134 XSH_CALLOC(result,xsh_order_list,1);
00135 result->size = size;
00136 XSH_CALLOC(result->list, xsh_order, result->size);
00137 XSH_NEW_PROPERTYLIST(result->header);
00138
00139 cleanup:
00140 if (cpl_error_get_code() != CPL_ERROR_NONE) {
00141 xsh_order_list_free(&result);
00142 }
00143 return result;
00144 }
00145
00146
00153
00154 xsh_order_list* xsh_order_list_create(xsh_instrument* instr)
00155 {
00156 xsh_order_list* result = NULL;
00157 int size;
00158 XSH_INSTRCONFIG * config = NULL;
00159
00160
00161 XSH_ASSURE_NOT_NULL(instr);
00162 check( config = xsh_instrument_get_config( instr ) ) ;
00163 XSH_ASSURE_NOT_NULL( config ) ;
00164 size = config->orders;
00165 check( result = xsh_order_list_new( size));
00166 result->instrument = instr;
00167 result->absorder_min = config->order_min;
00168 result->absorder_max = config->order_max;
00169 result->bin_x = xsh_instrument_get_binx( instr ) ;
00170 result->bin_y = xsh_instrument_get_biny( instr ) ;
00171
00172 cleanup:
00173 if (cpl_error_get_code() != CPL_ERROR_NONE) {
00174 xsh_order_list_free(&result);
00175 }
00176 return result;
00177 }
00178
00179
00188
00189 xsh_order_list* xsh_order_list_load(cpl_frame* frame, xsh_instrument* instr)
00190 {
00191 cpl_table* table = NULL;
00192 cpl_propertylist* header = NULL;
00193 const char* tablename = NULL;
00194 xsh_order_list* result = NULL;
00195 int i = 0, k = 0, pol_degree = 0;
00196
00197
00198 XSH_ASSURE_NOT_NULL( frame);
00199 XSH_ASSURE_NOT_NULL( instr);
00200
00201
00202 check( tablename = cpl_frame_get_filename(frame));
00203
00204 XSH_TABLE_LOAD( table, tablename);
00205
00206 check(result = xsh_order_list_create(instr));
00207 check(header = cpl_propertylist_load(tablename,0));
00208 check(cpl_propertylist_append(result->header, header));
00209
00210 XSH_ASSURE_NOT_ILLEGAL(result->size == cpl_table_get_nrow(table));
00211
00212 for(i=0;i<result->size;i++){
00213
00214 check(xsh_get_table_value(table, XSH_ORDER_TABLE_COLNAME_ORDER,
00215 CPL_TYPE_INT, i, &(result->list[i].order)));
00216 check(xsh_get_table_value(table, XSH_ORDER_TABLE_COLNAME_ABSORDER,
00217 CPL_TYPE_INT, i, &(result->list[i].absorder)));
00218
00219 xsh_get_table_value(table, XSH_ORDER_TABLE_DEGY,
00220 CPL_TYPE_INT, i, &pol_degree);
00221 if ( cpl_error_get_code() != CPL_ERROR_NONE ) {
00222 pol_degree = 2 ;
00223 cpl_error_reset() ;
00224 }
00225
00226 result->list[i].pol_degree = pol_degree ;
00227
00228 check(result->list[i].edguppoly = cpl_polynomial_new(1));
00229 check(result->list[i].cenpoly = cpl_polynomial_new(1));
00230 check(result->list[i].edglopoly = cpl_polynomial_new(1));
00231
00232 check(result->list[i].slicuppoly = cpl_polynomial_new(1));
00233 check(result->list[i].sliclopoly = cpl_polynomial_new(1));
00234
00235 for( k = 0 ; k <= pol_degree ; k++ ) {
00236 char colname[32] ;
00237 float coef ;
00238
00239 sprintf( colname, "%s%d", XSH_ORDER_TABLE_COLNAME_CENTER, k ) ;
00240 check(xsh_get_table_value(table, colname, CPL_TYPE_FLOAT, i, &coef));
00241 check(cpl_polynomial_set_coeff(result->list[i].cenpoly,
00242 &k,coef));
00243
00244 sprintf( colname, "%s%d", XSH_ORDER_TABLE_COLNAME_EDGLO, k ) ;
00245 check(xsh_get_table_value(table, colname, CPL_TYPE_FLOAT, i, &coef));
00246 check(cpl_polynomial_set_coeff(result->list[i].edglopoly,
00247 &k,coef));
00248
00249 sprintf( colname, "%s%d", XSH_ORDER_TABLE_COLNAME_EDGUP, k ) ;
00250 check(xsh_get_table_value(table, colname, CPL_TYPE_FLOAT, i, &coef));
00251 check(cpl_polynomial_set_coeff(result->list[i].edguppoly,
00252 &k,coef));
00253
00254
00255 sprintf( colname, "%s%d", XSH_ORDER_TABLE_COLNAME_SLICUP, k ) ;
00256 if ( cpl_table_has_column( table, colname ) == 1 ) {
00257 check(xsh_get_table_value(table, colname, CPL_TYPE_FLOAT, i, &coef));
00258 check(cpl_polynomial_set_coeff(result->list[i].slicuppoly,
00259 &k,coef));
00260 }
00261 else check(cpl_polynomial_set_coeff(result->list[i].slicuppoly,
00262 &k, 0.));
00263
00264 sprintf( colname, "%s%d", XSH_ORDER_TABLE_COLNAME_SLICLO, k ) ;
00265 if ( cpl_table_has_column( table, colname ) == 1 ) {
00266 check(xsh_get_table_value(table, colname, CPL_TYPE_FLOAT, i, &coef));
00267 check(cpl_polynomial_set_coeff(result->list[i].sliclopoly,
00268 &k,coef));
00269 }
00270 else check(cpl_polynomial_set_coeff(result->list[i].sliclopoly,
00271 &k, 0.));
00272
00273 }
00274
00275
00276 check(xsh_get_table_value(table, XSH_ORDER_TABLE_COLNAME_STARTY,
00277 CPL_TYPE_INT, i, &result->list[i].starty));
00278 check(xsh_get_table_value(table, XSH_ORDER_TABLE_COLNAME_ENDY,
00279 CPL_TYPE_INT, i, &result->list[i].endy));
00280
00281
00282
00283
00284
00285
00286
00287
00288
00289
00290
00291
00292
00293
00294
00295
00296
00297
00298
00299
00300
00301
00302
00303
00304
00305
00306
00307
00308
00309
00310
00311
00312
00313
00314
00315
00316
00317
00318 }
00319
00320 result->bin_x = xsh_instrument_get_binx( instr ) ;
00321 result->bin_y = xsh_instrument_get_biny( instr ) ;
00322 xsh_msg_dbg_medium( "Order Table Load, Binning: %d x %d", result->bin_x,
00323 result->bin_y);
00324
00325 cleanup:
00326 if (cpl_error_get_code () != CPL_ERROR_NONE) {
00327 xsh_error_msg("can't load frame %s",cpl_frame_get_filename(frame));
00328 xsh_order_list_free(&result);
00329 }
00330 xsh_free_propertylist(&header);
00331 XSH_TABLE_FREE( table);
00332 return result;
00333 }
00334
00335
00345
00346
00347 void xsh_order_list_fit(xsh_order_list *list, int size, double* order,
00348 double* posx, double* posy, int deg_poly)
00349 {
00350 int ordersize, i, nborder, nb_keep_order;
00351 cpl_vector *vx = NULL, *vy = NULL;
00352
00353 XSH_ASSURE_NOT_NULL( list);
00354 XSH_ASSURE_NOT_NULL( order);
00355 XSH_ASSURE_NOT_NULL( posx);
00356 XSH_ASSURE_NOT_NULL( posy);
00357 XSH_ASSURE_NOT_ILLEGAL( deg_poly >= 0);
00358
00359 ordersize = 0;
00360 nborder = 0;
00361 nb_keep_order = 0;
00362 xsh_msg("Fit order traces");
00363 xsh_msg_dbg_high("List size=%d",size);
00364 xsh_msg_dbg_high("Fit a polynomial of degree %d by order",deg_poly);
00365 xsh_msg_dbg_high("Search from order %d to %d", list->absorder_min,
00366 list->absorder_max);
00367 for(i=1; i <= size; i++) {
00368 if ( i < size && fabs(order[i-1] - order[i] ) < 0.0001) {
00369 ordersize++;
00370 }
00371 else {
00372 int absorder = order[i-1];
00373
00374 if( (absorder >= list->absorder_min) &&
00375 (absorder <= list->absorder_max) ){
00376
00377 ordersize++;
00378 check( vx = cpl_vector_wrap( ordersize, &(posx[i-ordersize])));
00379 check( vy = cpl_vector_wrap( ordersize, &(posy[i-ordersize])));
00380 xsh_msg_dbg_low("%d) absorder %lg nbpoints %d",
00381 nborder+1, order[i-1],ordersize);
00382 XSH_ASSURE_NOT_ILLEGAL_MSG(ordersize > deg_poly,
00383 "You must have more points to fit correctly this order");
00384 check( list->list[nb_keep_order].cenpoly =
00385 xsh_polynomial_fit_1d_create(vy, vx, deg_poly,NULL));
00386 list->list[nb_keep_order].order = nborder;
00387 list->list[nb_keep_order].absorder = (int)(order[i-1]);
00388
00389 check( xsh_unwrap_vector(&vx));
00390 check( xsh_unwrap_vector(&vy));
00391 nb_keep_order++;
00392 }
00393 else{
00394 xsh_msg("WARNING skipping absorder %d because is not in range",
00395 absorder);
00396 }
00397 nborder++;
00398 ordersize = 0;
00399 }
00400 }
00401 XSH_ASSURE_NOT_ILLEGAL( list->size == nb_keep_order);
00402 cleanup:
00403 xsh_unwrap_vector(&vx);
00404 xsh_unwrap_vector(&vy);
00405 return;
00406 }
00407
00408
00413
00414 void xsh_order_list_free(xsh_order_list** list)
00415 {
00416 int i = 0;
00417
00418 if (list && *list){
00419
00420 for (i = 0; i < (*list)->size; i++){
00421 xsh_free_polynomial(&(*list)->list[i].cenpoly);
00422 xsh_free_polynomial(&(*list)->list[i].edguppoly);
00423 xsh_free_polynomial(&(*list)->list[i].edglopoly);
00424 xsh_free_polynomial(&(*list)->list[i].slicuppoly);
00425 xsh_free_polynomial(&(*list)->list[i].sliclopoly);
00426 xsh_free_polynomial(&(*list)->list[i].blazepoly);
00427 }
00428 if ((*list)->list){
00429 cpl_free( (*list)->list);
00430 }
00431 xsh_free_propertylist(&((*list)->header));
00432 cpl_free( *list);
00433 *list = NULL;
00434 }
00435 }
00436
00437
00438
00439
00440
00441
00442
00443
00444
00445
00446
00447
00448
00449 int xsh_order_list_get_index_by_absorder( xsh_order_list* list,
00450 double absorder)
00451 {
00452 int idx = 0;
00453 int size = 0;
00454
00455 XSH_ASSURE_NOT_NULL( list);
00456 size = list->size;
00457 while (idx < size && (list->list[idx].absorder != absorder)){
00458 idx++;
00459 }
00460 XSH_ASSURE_NOT_ILLEGAL( idx < size);
00461
00462 cleanup:
00463 return idx;
00464
00465 }
00466
00472
00473 cpl_propertylist* xsh_order_list_get_header(xsh_order_list* list)
00474 {
00475 cpl_propertylist * res = NULL;
00476
00477 XSH_ASSURE_NOT_NULL(list);
00478 res = list->header;
00479 cleanup:
00480 return res;
00481 }
00482
00489
00490 int xsh_order_list_get_starty(xsh_order_list* list, int i)
00491 {
00492 int res = 0;
00493
00494 XSH_ASSURE_NOT_NULL(list);
00495 XSH_ASSURE_NOT_ILLEGAL(i >= 0 && i< list->size);
00496 res = floor( convert_data_to_bin( list->list[i].starty, list->bin_y)+0.5);
00497
00498 cleanup:
00499 return res;
00500 }
00501
00502
00509
00510 int xsh_order_list_get_endy(xsh_order_list* list, int i)
00511 {
00512 int res = 100000000;
00513
00514 XSH_ASSURE_NOT_NULL(list);
00515 XSH_ASSURE_NOT_ILLEGAL(i >= 0 && i< list->size);
00516 res = floor(convert_data_to_bin( (double)list->list[i].endy,
00517 list->bin_y)+0.5);
00518 cleanup:
00519 return res;
00520 }
00521
00522 int xsh_order_list_get_order( xsh_order_list * list, int absorder )
00523 {
00524 int i = 0 ;
00525
00526 XSH_ASSURE_NOT_NULL(list);
00527
00528 for( i = 0 ; i<list->size ; i++ )
00529 if ( list->list[i].absorder == absorder ) return i ;
00530
00531 return -1 ;
00532 cleanup:
00533 return -1 ;
00534 }
00535
00536
00537
00538
00539
00540
00541
00542
00543
00544
00545
00546
00547
00548 xsh_order_list* xsh_order_list_merge( xsh_order_list* lista,
00549 xsh_order_list* listb)
00550 {
00551 xsh_order_list* result = NULL;
00552 int size = 0;
00553 int i,j;
00554
00555 XSH_ASSURE_NOT_NULL( lista);
00556 XSH_ASSURE_NOT_NULL( listb);
00557
00558 size = lista->size+listb->size;
00559 check( result = xsh_order_list_new( size));
00560
00561 for( i=0; i< lista->size; i++){
00562 result->list[i].order = i;
00563 result->list[i].absorder = lista->list[i].absorder;
00564 result->list[i].starty = lista->list[i].starty;
00565 result->list[i].endy = lista->list[i].endy;
00566 result->list[i].cenpoly = cpl_polynomial_duplicate(
00567 lista->list[i].cenpoly);
00568 result->list[i].edguppoly = cpl_polynomial_duplicate(
00569 lista->list[i].edguppoly);
00570 result->list[i].edglopoly = cpl_polynomial_duplicate(
00571 lista->list[i].edglopoly);
00572 result->list[i].slicuppoly = cpl_polynomial_duplicate(
00573 lista->list[i].slicuppoly);
00574 result->list[i].sliclopoly = cpl_polynomial_duplicate(
00575 lista->list[i].sliclopoly);
00576 }
00577
00578 for( i=0; i< listb->size; i++){
00579 j= lista->size+i;
00580 result->list[j].order = i;
00581 result->list[j].absorder = listb->list[i].absorder;
00582 result->list[j].starty = listb->list[i].starty;
00583 result->list[j].endy = listb->list[i].endy;
00584 result->list[j].cenpoly = cpl_polynomial_duplicate(
00585 listb->list[i].cenpoly);
00586 result->list[j].edguppoly = cpl_polynomial_duplicate(
00587 listb->list[i].edguppoly);
00588 result->list[j].edglopoly = cpl_polynomial_duplicate(
00589 listb->list[i].edglopoly);
00590 result->list[j].slicuppoly = cpl_polynomial_duplicate(
00591 listb->list[i].slicuppoly);
00592 result->list[j].sliclopoly = cpl_polynomial_duplicate(
00593 listb->list[i].sliclopoly);
00594 }
00595
00596 cleanup:
00597 if (cpl_error_get_code() != CPL_ERROR_NONE) {
00598 xsh_order_list_free(&result);
00599 }
00600 return result;
00601 }
00602
00603
00604
00613
00614 void xsh_order_list_set_bin_x( xsh_order_list* list, int bin)
00615 {
00616 XSH_ASSURE_NOT_NULL( list);
00617 list->bin_x = bin;
00618
00619 cleanup:
00620 return;
00621 }
00622
00623
00632
00633 void xsh_order_list_set_bin_y( xsh_order_list* list, int bin)
00634 {
00635 XSH_ASSURE_NOT_NULL( list);
00636 list->bin_y = bin;
00637
00638 cleanup:
00639 return;
00640 }
00641
00642
00654
00655 double xsh_order_list_eval( xsh_order_list* list, cpl_polynomial *poly,
00656 double y)
00657 {
00658 double result=0;
00659 double y_no_bin;
00660 double x_no_bin;
00661
00662 XSH_ASSURE_NOT_NULL( list);
00663 XSH_ASSURE_NOT_NULL( poly);
00664
00665 y_no_bin = convert_bin_to_data( y, list->bin_y);
00666 check( x_no_bin = cpl_polynomial_eval_1d( poly, y_no_bin, NULL));
00667 result = convert_data_to_bin( x_no_bin, list->bin_x);
00668
00669 cleanup:
00670 return result;
00671 }
00672
00673
00687
00688 int xsh_order_list_eval_int( xsh_order_list* list, cpl_polynomial *poly,
00689 double y)
00690 {
00691 double res = 0.0;
00692 int result=0;
00693
00694 XSH_ASSURE_NOT_NULL( list);
00695 XSH_ASSURE_NOT_NULL( poly);
00696
00697 check( res = xsh_order_list_eval( list, poly, y));
00698 result = floor( res+0.5);
00699
00700 cleanup:
00701 return result;
00702 }
00703
00704
00705
00717
00718 void xsh_order_list_apply_shift( xsh_order_list *list, double xshift,
00719 double yshift)
00720 {
00721 cpl_polynomial *p = NULL;
00722 double coef;
00723 int pows = 0;
00724 int i;
00725
00726 XSH_ASSURE_NOT_NULL( list);
00727
00728 for( i=0; i< list->size; i++){
00729 p = list->list[i].cenpoly;
00730 check( coef = cpl_polynomial_get_coeff( p, &pows));
00731 coef += xshift;
00732 check( cpl_polynomial_set_coeff( p, &pows, coef));
00733
00734 p = list->list[i].edguppoly;
00735 if ( p != NULL){
00736 check( coef = cpl_polynomial_get_coeff( p, &pows));
00737 coef += xshift;
00738 check( cpl_polynomial_set_coeff( p, &pows, coef));
00739 }
00740 p = list->list[i].edglopoly;
00741 if ( p != NULL){
00742 check( coef = cpl_polynomial_get_coeff( p, &pows));
00743 coef += xshift;
00744 check( cpl_polynomial_set_coeff( p, &pows, coef));
00745 }
00746 p = list->list[i].sliclopoly;
00747 if ( p != NULL){
00748 check( coef = cpl_polynomial_get_coeff( p, &pows));
00749 coef += xshift;
00750 check( cpl_polynomial_set_coeff( p, &pows, coef));
00751 }
00752 p = list->list[i].slicuppoly;
00753 if ( p != NULL){
00754 check( coef = cpl_polynomial_get_coeff( p, &pows));
00755 coef += xshift;
00756 check( cpl_polynomial_set_coeff( p, &pows, coef));
00757 }
00758 }
00759
00760 cleanup:
00761 return;
00762 }
00763
00764
00765
00783
00784 cpl_frame*
00785 xsh_order_list_save( xsh_order_list* order_list, xsh_instrument* instrument,
00786 const char* filename, const char* tag,const int ny)
00787 {
00788 cpl_table* table = NULL;
00789 cpl_table* tabqc = NULL;
00790 cpl_frame * result = NULL ;
00791 int i=0;
00792 int k ;
00793 int pol_degree ;
00794 char colname[32] ;
00795 int PointStep = 8 ;
00796
00797 int nraw;
00798 int* porder=NULL;
00799 int* pabsorder=NULL;
00800 double* pcenterx=NULL;
00801 double* pcentery=NULL;
00802 double* pedgeupx=NULL;
00803 double* pedgelox=NULL;
00804 double* pslicupx=NULL;
00805 double* psliclox=NULL;
00806 int iorder=0;
00807 int iy=0;
00808 cpl_table *tab_ext = NULL;
00809 int norder=0;
00810
00811 XSH_ASSURE_NOT_NULL( order_list);
00812 XSH_ASSURE_NOT_NULL( filename);
00813 XSH_ASSURE_NOT_NULL( tag);
00814 XSH_ASSURE_NOT_NULL( instrument);
00815
00816
00817 check( pol_degree = cpl_polynomial_get_degree(
00818 order_list->list[0].cenpoly));
00819 XSH_ASSURE_NOT_ILLEGAL( pol_degree > 0);
00820
00821
00822 check(table = cpl_table_new( order_list->size));
00823
00824
00825 check(
00826 cpl_table_new_column( table, XSH_ORDER_TABLE_COLNAME_ORDER,
00827 CPL_TYPE_INT));
00828 check(
00829 cpl_table_new_column( table, XSH_ORDER_TABLE_COLNAME_ABSORDER,
00830 CPL_TYPE_INT));
00831
00832
00833 for( k = 0 ; k<= pol_degree ; k++ ) {
00834
00835 sprintf( colname, "%s%d", XSH_ORDER_TABLE_COLNAME_CENTER, k ) ;
00836 check(
00837 cpl_table_new_column(table, colname, CPL_TYPE_FLOAT));
00838 sprintf( colname, "%s%d", XSH_ORDER_TABLE_COLNAME_EDGUP, k ) ;
00839 check(
00840 cpl_table_new_column(table, colname, CPL_TYPE_FLOAT));
00841 sprintf( colname, "%s%d", XSH_ORDER_TABLE_COLNAME_EDGLO, k ) ;
00842 check(
00843 cpl_table_new_column(table, colname, CPL_TYPE_FLOAT));
00844
00845 sprintf( colname, "%s%d", XSH_ORDER_TABLE_COLNAME_SLICUP, k ) ;
00846 check(
00847 cpl_table_new_column(table, colname, CPL_TYPE_FLOAT));
00848
00849 sprintf( colname, "%s%d", XSH_ORDER_TABLE_COLNAME_SLICLO, k ) ;
00850 check(
00851 cpl_table_new_column(table, colname, CPL_TYPE_FLOAT));
00852 }
00853 check( cpl_table_new_column( table, XSH_ORDER_TABLE_DEGY, CPL_TYPE_INT ) ) ;
00854
00855 check(
00856 cpl_table_new_column(table,XSH_ORDER_TABLE_COLNAME_STARTY,
00857 CPL_TYPE_INT));
00858 check(
00859 cpl_table_new_column(table,XSH_ORDER_TABLE_COLNAME_ENDY,
00860 CPL_TYPE_INT));
00861 check(cpl_table_set_size(table,order_list->size));
00862
00863
00864
00865 for(i=0;i<order_list->size;i++){
00866 check(cpl_table_set_int(table,XSH_ORDER_TABLE_COLNAME_ORDER,
00867 i,order_list->list[i].order));
00868 check(cpl_table_set_int(table,XSH_ORDER_TABLE_COLNAME_ABSORDER,
00869 i,order_list->list[i].absorder));
00870 check(cpl_table_set_int(table,XSH_ORDER_TABLE_COLNAME_STARTY,
00871 i,order_list->list[i].starty));
00872 check(cpl_table_set_int(table,XSH_ORDER_TABLE_COLNAME_ENDY,
00873 i,order_list->list[i].endy));
00874
00875 for( k = 0 ; k <= pol_degree ; k++ ) {
00876 double coef ;
00877
00878
00879 check(coef = cpl_polynomial_get_coeff(order_list->list[i].cenpoly,&k));
00880 sprintf( colname, "%s%d", XSH_ORDER_TABLE_COLNAME_CENTER, k ) ;
00881 check(cpl_table_set(table, colname,i,coef));
00882
00883
00884 if ( order_list->list[i].edguppoly != NULL ) {
00885 check(coef = cpl_polynomial_get_coeff(order_list->list[i].edguppoly,&k));
00886 }
00887 else coef = 0.0 ;
00888 sprintf( colname, "%s%d", XSH_ORDER_TABLE_COLNAME_EDGUP, k ) ;
00889 check(cpl_table_set(table, colname,i,coef));
00890
00891
00892 if ( order_list->list[i].edglopoly != NULL ) {
00893 check(coef = cpl_polynomial_get_coeff(order_list->list[i].edglopoly,&k));
00894 }
00895 else coef = 0.0;
00896 sprintf( colname, "%s%d", XSH_ORDER_TABLE_COLNAME_EDGLO, k ) ;
00897 check(cpl_table_set(table, colname,i,coef));
00898
00899
00900 if ( order_list->list[i].sliclopoly != NULL ) {
00901 check(coef = cpl_polynomial_get_coeff(order_list->list[i].sliclopoly,&k));
00902 }
00903 else coef = 0.0 ;
00904 sprintf( colname, "%s%d", XSH_ORDER_TABLE_COLNAME_SLICLO, k ) ;
00905 check(cpl_table_set(table, colname, i, coef));
00906
00907
00908 if ( order_list->list[i].slicuppoly != NULL ) {
00909 check(coef = cpl_polynomial_get_coeff(order_list->list[i].slicuppoly,&k));
00910 }
00911 else coef = 0.0 ;
00912 sprintf( colname, "%s%d", XSH_ORDER_TABLE_COLNAME_SLICUP, k ) ;
00913 check(cpl_table_set(table, colname, i, coef));
00914
00915 }
00916 check( cpl_table_set(table, XSH_ORDER_TABLE_DEGY, i, pol_degree ) ) ;
00917 }
00918
00919
00920
00921 nraw = order_list->size*(ny/PointStep+0.5);
00922
00923 check( tabqc = cpl_table_new(nraw));
00924 cpl_table_new_column(tabqc,XSH_ORDER_TABLE_COLNAME_ORDER,CPL_TYPE_INT);
00925 cpl_table_new_column(tabqc,XSH_ORDER_TABLE_COLNAME_ABSORDER,CPL_TYPE_INT);
00926 cpl_table_new_column(tabqc,XSH_ORDER_TABLE_COLNAME_CENTERX,CPL_TYPE_DOUBLE);
00927 cpl_table_new_column(tabqc,XSH_ORDER_TABLE_COLNAME_CENTERY,CPL_TYPE_DOUBLE);
00928
00929 cpl_table_fill_column_window(tabqc,XSH_ORDER_TABLE_COLNAME_ORDER,0,nraw,-1);
00930 cpl_table_fill_column_window(tabqc,XSH_ORDER_TABLE_COLNAME_ABSORDER,0,nraw,-1);
00931 cpl_table_fill_column_window(tabqc,XSH_ORDER_TABLE_COLNAME_CENTERX,0,nraw,-1);
00932 cpl_table_fill_column_window(tabqc,XSH_ORDER_TABLE_COLNAME_CENTERY,0,nraw,-1);
00933
00934 porder=cpl_table_get_data_int(tabqc,XSH_ORDER_TABLE_COLNAME_ORDER);
00935 pabsorder=cpl_table_get_data_int(tabqc,XSH_ORDER_TABLE_COLNAME_ABSORDER);
00936 pcenterx=cpl_table_get_data_double(tabqc,XSH_ORDER_TABLE_COLNAME_CENTERX);
00937 pcentery=cpl_table_get_data_double(tabqc,XSH_ORDER_TABLE_COLNAME_CENTERY);
00938
00939 if ( XSH_CMP_TAG_LAMP( tag, XSH_ORDER_TAB_EDGES)){
00940 cpl_table_new_column(tabqc,XSH_ORDER_TABLE_COLNAME_EDGLOX,CPL_TYPE_DOUBLE);
00941 cpl_table_new_column(tabqc,XSH_ORDER_TABLE_COLNAME_EDGUPX,CPL_TYPE_DOUBLE);
00942 cpl_table_fill_column_window(tabqc,XSH_ORDER_TABLE_COLNAME_EDGLOX,0,nraw,-1);
00943 cpl_table_fill_column_window(tabqc,XSH_ORDER_TABLE_COLNAME_EDGUPX,0,nraw,-1);
00944 pedgelox=cpl_table_get_data_double(tabqc,XSH_ORDER_TABLE_COLNAME_EDGLOX);
00945 pedgeupx=cpl_table_get_data_double(tabqc,XSH_ORDER_TABLE_COLNAME_EDGUPX);
00946
00947 if ( order_list->list[0].slicuppoly != NULL ) {
00948 cpl_table_new_column(tabqc,XSH_ORDER_TABLE_COLNAME_SLICLOX,CPL_TYPE_DOUBLE);
00949 cpl_table_new_column(tabqc,XSH_ORDER_TABLE_COLNAME_SLICUPX,CPL_TYPE_DOUBLE);
00950
00951 cpl_table_fill_column_window(tabqc,XSH_ORDER_TABLE_COLNAME_SLICLOX,0,nraw,-1);
00952 cpl_table_fill_column_window(tabqc,XSH_ORDER_TABLE_COLNAME_SLICUPX,0,nraw,-1);
00953
00954 psliclox=cpl_table_get_data_double(tabqc,XSH_ORDER_TABLE_COLNAME_SLICLOX);
00955 pslicupx=cpl_table_get_data_double(tabqc,XSH_ORDER_TABLE_COLNAME_SLICUPX);
00956 }
00957 }
00958
00959
00960 k=0;
00961 for( iorder=0; iorder< order_list->size; iorder++){
00962 int starty, endy;
00963
00964 xsh_msg_dbg_high(" Produce solution for order %d",
00965 order_list->list[iorder].absorder);
00966
00967 check( starty = order_list->list[iorder].starty);
00968 check( endy = order_list->list[iorder].endy);
00969 #if 0
00970 if ( starty == -1 && endy == -1 ) {
00971 xsh_msg( " **** Order %d Discarded", order_list->list[iorder].absorder);
00972 continue ;
00973 }
00974 #endif
00975 if (starty == 0){
00976 xsh_msg("Warning starty equal zero, put starty ==> 1");
00977 starty = 1;
00978 }
00979 if ( endy == 0){
00980 xsh_msg("Warning starty equal zero, put endy ==> %d",ny);
00981 endy = ny;
00982 }
00983
00984 for( iy=starty; iy<=endy; iy= iy+PointStep){
00985 float dx;
00986
00987 check( dx = cpl_polynomial_eval_1d( order_list->list[iorder].cenpoly,
00988 iy, NULL));
00989 porder[k]=iorder;
00990 pabsorder[k]=order_list->list[iorder].absorder;
00991 pcenterx[k]=dx;
00992 pcentery[k]=iy;
00993
00994 if ( XSH_CMP_TAG_LAMP( tag, XSH_ORDER_TAB_EDGES)){
00995 check(dx=cpl_polynomial_eval_1d(order_list->list[iorder].edglopoly,
00996 iy, NULL ) ) ;
00997 pedgelox[k]=dx;
00998 check(dx=cpl_polynomial_eval_1d(order_list->list[iorder].edguppoly,
00999 iy, NULL ) ) ;
01000 pedgeupx[k]=dx;
01001
01002 if ( order_list->list[iorder].slicuppoly != NULL ) {
01003 check(dx=cpl_polynomial_eval_1d(order_list->list[iorder].slicuppoly,
01004 iy, NULL ) ) ;
01005 pslicupx[k]=dx;
01006 }
01007 if ( order_list->list[iorder].sliclopoly != NULL ) {
01008 check(dx=cpl_polynomial_eval_1d(order_list->list[iorder].sliclopoly,
01009 iy, NULL ) ) ;
01010 psliclox[k]=dx;
01011 }
01012 }
01013 k++;
01014 }
01015 if(starty!=-999) {
01016
01017
01018
01019
01020 norder++;
01021 }
01022 }
01023
01024 check( cpl_table_and_selected_int( tabqc, XSH_ORDER_TABLE_COLNAME_ORDER,
01025 CPL_GREATER_THAN, -1));
01026
01027 tab_ext = cpl_table_extract_selected( tabqc);
01028
01029
01030 check( xsh_pfits_set_pcatg( order_list->header, tag));
01031 if (strstr(tag, "EDGE") != NULL) {
01032 cpl_propertylist_append_int(order_list->header,XSH_QC_ORD_EDGE_NDET, norder);
01033 cpl_propertylist_set_comment(order_list->header,XSH_QC_ORD_EDGE_NDET,
01034 "number of detected order edges");
01035 }
01036 check( cpl_table_save(table, order_list->header, NULL, filename,
01037 CPL_IO_DEFAULT));
01038 check( cpl_table_save(tab_ext, NULL, NULL, filename,
01039 CPL_IO_EXTEND));
01040
01041
01042 check( result = xsh_frame_product( filename, tag, CPL_FRAME_TYPE_TABLE,
01043 CPL_FRAME_GROUP_PRODUCT, CPL_FRAME_LEVEL_TEMPORARY));
01044
01045 cleanup:
01046 xsh_free_table( &tab_ext);
01047 xsh_free_table( &tabqc);
01048 xsh_free_table( &table);
01049 return result;
01050 }
01051
01052
01053 void xsh_order_split_qth_d2( cpl_frame* order_tab_frame,
01054 cpl_frame* spectralformat_frame, cpl_frame** qth_order_tab_frame,
01055 cpl_frame** d2_order_tab_frame, xsh_instrument *instr)
01056 {
01057 cpl_table *order_tab = NULL;
01058 cpl_table *order_tab_ext = NULL;
01059 const char* order_tab_name = NULL;
01060 int order_tab_size;
01061 cpl_table *order_tab_qth = NULL;
01062 cpl_table *order_tab_d2 = NULL;
01063 cpl_table *order_tab_ext_qth = NULL;
01064 cpl_table *order_tab_ext_d2 = NULL;
01065 xsh_spectralformat_list *spectralformat = NULL;
01066 cpl_propertylist *header = NULL;
01067 int i=0;
01068
01069 XSH_ASSURE_NOT_NULL( order_tab_frame);
01070 XSH_ASSURE_NOT_NULL( spectralformat_frame);
01071 XSH_ASSURE_NOT_NULL( qth_order_tab_frame);
01072 XSH_ASSURE_NOT_NULL( d2_order_tab_frame);
01073 XSH_ASSURE_NOT_NULL( instr);
01074
01075 check( spectralformat = xsh_spectralformat_list_load( spectralformat_frame,
01076 instr));
01077 check( order_tab_name = cpl_frame_get_filename( order_tab_frame));
01078 XSH_TABLE_LOAD( order_tab, order_tab_name);
01079 check(order_tab_ext=cpl_table_load(order_tab_name,2,0));
01080 check( header = cpl_propertylist_load( order_tab_name, 0));
01081 check( order_tab_size = cpl_table_get_nrow( order_tab));
01082 for (i=0; i< order_tab_size; i++){
01083 int absorder;
01084 const char* lamp = NULL;
01085
01086 check( xsh_get_table_value( order_tab, XSH_ORDER_TABLE_COLNAME_ABSORDER,
01087 CPL_TYPE_INT, i, &absorder));
01088 check( lamp = xsh_spectralformat_list_get_lamp( spectralformat,
01089 absorder));
01090 XSH_ASSURE_NOT_NULL( lamp);
01091 if ( strcmp(lamp,"QTH")==0){
01092 check( cpl_table_select_row( order_tab, i));
01093 }
01094 else{
01095 check( cpl_table_unselect_row( order_tab, i));
01096 }
01097 }
01098
01099 check( order_tab_qth = cpl_table_extract_selected( order_tab));
01100 check( cpl_table_not_selected( order_tab));
01101 check( order_tab_d2 = cpl_table_extract_selected( order_tab));
01102
01103 cpl_table_and_selected_int(order_tab_ext,"ABSORDER",CPL_GREATER_THAN,XSH_ORDER_MIN_UVB_D2-1);
01104 check( order_tab_ext_d2 = cpl_table_extract_selected( order_tab_ext));
01105 cpl_table_select_all(order_tab_ext);
01106
01107 cpl_table_and_selected_int(order_tab_ext,"ABSORDER",CPL_LESS_THAN,XSH_ORDER_MAX_UVB_QTH+1);
01108 check( order_tab_ext_qth = cpl_table_extract_selected( order_tab_ext));
01109
01110
01111 check( cpl_table_save( order_tab_qth, header, NULL, "ORDER_TAB_CENTR_QTH_UVB.fits", CPL_IO_DEFAULT));
01112 check( cpl_table_save( order_tab_ext_qth, header, NULL, "ORDER_TAB_CENTR_QTH_UVB.fits", CPL_IO_EXTEND));
01113 check( cpl_table_save( order_tab_d2, header, NULL, "ORDER_TAB_CENTR_D2_UVB.fits", CPL_IO_DEFAULT));
01114 check( cpl_table_save( order_tab_ext_d2, header, NULL, "ORDER_TAB_CENTR_D2_UVB.fits", CPL_IO_EXTEND));
01115
01116 *qth_order_tab_frame = cpl_frame_new();
01117 cpl_frame_set_filename( *qth_order_tab_frame, "ORDER_TAB_CENTR_QTH_UVB.fits");
01118 *d2_order_tab_frame = cpl_frame_new();
01119 cpl_frame_set_filename( *d2_order_tab_frame, "ORDER_TAB_CENTR_D2_UVB.fits");
01120 cleanup:
01121 xsh_spectralformat_list_free( &spectralformat);
01122 xsh_free_propertylist( &header);
01123 xsh_free_table( &order_tab);
01124 xsh_free_table( &order_tab_qth);
01125 xsh_free_table( &order_tab_d2);
01126 xsh_free_table( &order_tab_ext);
01127 xsh_free_table( &order_tab_ext_d2);
01128 xsh_free_table( &order_tab_ext_qth);
01129
01130 return;
01131 }