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