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 #ifdef HAVE_CONFIG_H
00027 #include <config.h>
00028 #endif
00029
00030
00035
00036
00040
00041
00042
00043 #include <string.h>
00044 #include <math.h>
00045 #include <xsh_utils_table.h>
00046 #include <xsh_parameters.h>
00047 #include <xsh_data_pre.h>
00048 #include <xsh_data_order.h>
00049 #include <xsh_data_dispersol.h>
00050 #include <xsh_utils_wrappers.h>
00051 #include <xsh_data_wavemap.h>
00052 #include <xsh_error.h>
00053 #include <xsh_msg.h>
00054 #include <xsh_pfits.h>
00055 #include <xsh_dfs.h>
00056 #include <math.h>
00057 #include <gsl/gsl_multifit.h>
00058 #include <cpl.h>
00059
00060
00061
00062
00069 void
00070 xsh_wavemap_list_dump( xsh_wavemap_list * list,
00071 const char * fname )
00072 {
00073 int i ;
00074 FILE * fout = NULL;
00075
00076 XSH_ASSURE_NOT_NULL( list ) ;
00077 if ( fname == NULL ) fout = stdout ;
00078 else fout = fopen( fname, "w" ) ;
00079 XSH_ASSURE_NOT_NULL( fout ) ;
00080
00081 fprintf( fout, "Wavemap List. Nb of orders: %d\n", list->size ) ;
00082 for( i = 0 ; i<list->size ; i++ ) {
00083 fprintf( fout, " Entry %2d: Order %d, Ndata: %d\n",
00084 i, list->list[i].order, list->list[i].sky_size);
00085 }
00086
00087 cleanup:
00088 if ( fname != NULL && fout != NULL ) fclose( fout ) ;
00089 return ;
00090 }
00091
00092
00093
00100
00101 xsh_wavemap_list*
00102 xsh_wavemap_list_create( xsh_instrument* instr)
00103 {
00104 xsh_wavemap_list* result = NULL;
00105 XSH_INSTRCONFIG * config = NULL;
00106 XSH_ARM xsh_arm;
00107 int i, size = 0;
00108
00109
00110 XSH_ASSURE_NOT_NULL( instr);
00111
00112
00113 xsh_arm = xsh_instrument_get_arm( instr) ;
00114 check( config = xsh_instrument_get_config( instr));
00115 size = config->orders;
00116
00117 XSH_CALLOC(result, xsh_wavemap_list, 1 );
00118
00119 result->size = size;
00120
00121 XSH_ASSURE_NOT_ILLEGAL(result->size > 0);
00122 result->instrument = instr;
00123 XSH_CALLOC(result->list, xsh_wavemap, result->size);
00124 XSH_NEW_PROPERTYLIST(result->header);
00125
00126
00127 for( i = 0 ; i<result->size ; i++ ) {
00128 result->list[i].tcheb_pol_lambda = NULL ;
00129 }
00130
00131 cleanup:
00132 if (cpl_error_get_code() != CPL_ERROR_NONE) {
00133 xsh_wavemap_list_free(&result);
00134 }
00135 return result;
00136 }
00137
00147
00148 void
00149 xsh_wavemap_list_set_max_size( xsh_wavemap_list * list, int idx,
00150 int absorder, int max_size)
00151 {
00152 xsh_wavemap * pwavemap = NULL ;
00153
00154
00155 XSH_ASSURE_NOT_NULL(list) ;
00156 XSH_ASSURE_NOT_ILLEGAL( idx >= 0 && idx < list->size && max_size > 0);
00157 pwavemap = &list->list[idx] ;
00158 XSH_ASSURE_NOT_NULL( pwavemap);
00159
00160 pwavemap->order = absorder;
00161 pwavemap->sky_size = max_size;
00162 pwavemap->object_size = max_size;
00163 XSH_CALLOC( pwavemap->sky, wavemap_item, max_size);
00164 XSH_CALLOC( pwavemap->object, wavemap_item, max_size);
00165
00166 cleanup:
00167 return ;
00168 }
00169
00170
00175
00176 void
00177 xsh_wavemap_list_free(xsh_wavemap_list** list)
00178 {
00179 int i = 0;
00180 xsh_wavemap_list *plist = NULL;
00181
00182 if (list != NULL && *list != NULL ) {
00183 plist = *list ;
00184
00185 for (i = 0; i < plist->size; i++) {
00186 xsh_wavemap *pr = &(plist->list[i]) ;
00187
00188 xsh_msg_dbg_high( "Freeing order index %d", i ) ;
00189 if ( pr == NULL ) continue ;
00190 xsh_msg_dbg_high( " Abs Order: %d", pr->order ) ;
00191 cpl_free( pr->sky);
00192 cpl_free( pr->object);
00193 if (pr->pol_lambda != NULL){
00194 xsh_free_polynomial( &(pr->pol_lambda));
00195 }
00196 if (pr->pol_slit != NULL){
00197 xsh_free_polynomial( &(pr->pol_slit));
00198 }
00199 xsh_free_polynomial( &(pr->tcheb_pol_lambda));
00200 }
00201 if ((*list)->list){
00202 cpl_free((*list)->list);
00203 }
00204 xsh_free_propertylist(&((*list)->header));
00205 cpl_free(*list);
00206 *list = NULL;
00207 }
00208 }
00209
00210 static void
00211 lambda_fit( double * lambda, double * xpos, double * ypos,
00212 int size, xsh_wavemap_list * wmap,
00213 xsh_dispersol_param * dispsol_param, int order)
00214 {
00215 double xmin, xmax, ymin, ymax, lmin, lmax;
00216 double *tcheb_lambda = NULL, *tcheb_xpos = NULL, *tcheb_ypos = NULL;
00217 int nbcoefs, xdeg, ydeg;
00218 cpl_vector *tcheb_coef_x = NULL;
00219 cpl_vector *tcheb_coef_y = NULL;
00220 int i, j, k ;
00221 double chisq;
00222 gsl_matrix *X = NULL, *cov = NULL;
00223 gsl_vector *y = NULL, *c = NULL;
00224 gsl_multifit_linear_workspace *work = NULL;
00225 cpl_polynomial* result = NULL;
00226 int pows[3];
00227
00228 XSH_ASSURE_NOT_NULL( lambda);
00229 XSH_ASSURE_NOT_NULL( xpos);
00230 XSH_ASSURE_NOT_NULL( ypos);
00231 XSH_ASSURE_NOT_NULL( wmap);
00232 XSH_ASSURE_NOT_NULL( dispsol_param);
00233 XSH_ASSURE_NOT_ILLEGAL( size > 0);
00234 XSH_ASSURE_NOT_ILLEGAL( order >= 0 && order < wmap->size);
00235
00236 xsh_msg_dbg_low( " Entering lambda_fit. Order %d, size: %d",
00237 wmap->list[order].order, size);
00238 xdeg = dispsol_param->deg_x;
00239 ydeg = dispsol_param->deg_y;
00240 nbcoefs = (xdeg+1)*(ydeg+1) ;
00241
00242 XSH_ASSURE_NOT_ILLEGAL( size >= nbcoefs);
00243
00244 wmap->degx = xdeg ;
00245 wmap->degy = ydeg ;
00246
00247
00248 check(xsh_tools_min_max(size, lambda, &lmin, &lmax));
00249 check(xsh_tools_min_max(size, xpos, &xmin, &xmax));
00250 check(xsh_tools_min_max(size, ypos, &ymin, &ymax));
00251
00252 wmap->list[order].xmin = xmin ;
00253 wmap->list[order].xmax = xmax ;
00254 wmap->list[order].ymin = ymin ;
00255 wmap->list[order].ymax = ymax ;
00256 wmap->list[order].lambda_min = lmin ;
00257 wmap->list[order].lambda_max = lmax ;
00258
00259 xsh_msg_dbg_medium(
00260 " Min,Max. Lambda: %.6lf,%.6lf - X: %.6lf,%.6lf - Y: %.6lf,%.6lf",
00261 lmin, lmax, xmin, xmax, ymin, ymax);
00262
00263
00264 XSH_CALLOC(tcheb_lambda, double, size);
00265 XSH_CALLOC(tcheb_xpos, double, size);
00266 XSH_CALLOC(tcheb_ypos, double, size);
00267
00268 check(xsh_tools_tchebitchev_transform_tab(size, lambda, lmin, lmax,
00269 tcheb_lambda));
00270 check(xsh_tools_tchebitchev_transform_tab(size, xpos, xmin, xmax,
00271 tcheb_xpos));
00272 check(xsh_tools_tchebitchev_transform_tab(size, ypos, ymin, ymax,
00273 tcheb_ypos));
00274 X = gsl_matrix_alloc( size, nbcoefs);
00275 y = gsl_vector_alloc( size);
00276 c = gsl_vector_alloc( nbcoefs);
00277 cov = gsl_matrix_alloc( nbcoefs, nbcoefs);
00278
00279 for (i = 0; i < size; i++) {
00280 check( tcheb_coef_x = xsh_tools_tchebitchev_poly_eval(
00281 xdeg, tcheb_xpos[i]));
00282 check( tcheb_coef_y = xsh_tools_tchebitchev_poly_eval(
00283 ydeg, tcheb_ypos[i]));
00284
00285 for(j = 0; j < dispsol_param->deg_x +1 ; j++) {
00286 for(k = 0; k < dispsol_param->deg_y +1 ; k++){
00287 double vx, vy;
00288
00289 check( vx = cpl_vector_get( tcheb_coef_x, j));
00290 check( vy = cpl_vector_get( tcheb_coef_y, k));
00291 gsl_matrix_set (X, i, k+j*(xdeg+1), vx*vy );
00292 }
00293 }
00294 gsl_vector_set (y, i, tcheb_lambda[i]);
00295 xsh_free_vector(&tcheb_coef_x);
00296 xsh_free_vector(&tcheb_coef_y);
00297
00298 }
00299 xsh_msg_dbg_medium( " GSL Initialized" ) ;
00300
00301
00302 XSH_ASSURE_NOT_ILLEGAL_MSG( size >nbcoefs,
00303 "Not enough Points vs Number of Coeffs" ) ;
00304 work = gsl_multifit_linear_alloc(size, nbcoefs);
00305 gsl_multifit_linear(X, y, c, cov, &chisq, work);
00306
00307 xsh_msg_dbg_medium( " GSL Done" ) ;
00308
00309 wmap->list[order].tcheb_pol_lambda = cpl_polynomial_new( 2);
00310
00311 result = wmap->list[order].tcheb_pol_lambda ;
00312
00313 for(i = 0; i < (xdeg +1) ; i++){
00314 for(j = 0; j < (ydeg +1) ; j++){
00315 pows[0] = j;
00316 pows[1] = i;
00317 cpl_polynomial_set_coeff(result, pows,
00318 gsl_vector_get(c, j+i*(ydeg+1)));
00319 }
00320 }
00321 xsh_msg_dbg_medium( " GSL Finished" ) ;
00322
00323 gsl_multifit_linear_free (work);
00324 gsl_matrix_free (X);
00325 gsl_vector_free (y);
00326 gsl_vector_free (c);
00327 gsl_matrix_free (cov);
00328
00329 cleanup:
00330 XSH_FREE( tcheb_lambda);
00331 XSH_FREE( tcheb_xpos);
00332 XSH_FREE( tcheb_ypos);
00333 return ;
00334 }
00335
00336
00352
00353
00354
00355
00356 void
00357 xsh_wavemap_list_compute_poly( double * vlambda,
00358 double * vslit,
00359 double * xpos,
00360 double * ypos,
00361 int nitems,
00362 double * orders,
00363 xsh_dispersol_param *dispsol_param,
00364 xsh_wavemap_list *wmap)
00365 {
00366
00367
00368
00369
00370 int i, ordersize, nborder ;
00371 double *vx = NULL, *vy = NULL, *vl = NULL, *vs = NULL;
00372
00373
00374 xsh_msg( "Entering xsh_wavemap_compute" ) ;
00375
00376 XSH_ASSURE_NOT_NULL( vlambda);
00377 XSH_ASSURE_NOT_NULL( vslit);
00378 XSH_ASSURE_NOT_NULL( xpos);
00379 XSH_ASSURE_NOT_NULL( ypos);
00380 XSH_ASSURE_NOT_NULL( orders);
00381 XSH_ASSURE_NOT_NULL( wmap);
00382 XSH_ASSURE_NOT_NULL( dispsol_param);
00383 XSH_ASSURE_NOT_ILLEGAL( nitems > 0);
00384 XSH_ASSURE_NOT_ILLEGAL( wmap->size);
00385
00386 xsh_msg( " X degree = %d, Y degree = %d", dispsol_param->deg_x,
00387 dispsol_param->deg_y) ;
00388
00389
00390 ordersize = 0;
00391 nborder = 0;
00392
00393 wmap->degx = dispsol_param->deg_x;
00394 wmap->degy = dispsol_param->deg_y;
00395
00396 xsh_msg("Compute POLY for lambda");
00397 XSH_REGDEBUG( "nitems %d ", nitems);
00398
00399 for( i = 1 ; i<=nitems ; i++ ) {
00400
00401 if ( i < nitems && orders[i-1] == orders[i] ) {
00402 ordersize++;
00403 }
00404 else {
00405 cpl_vector* posx = NULL;
00406 cpl_vector* posy = NULL;
00407 cpl_vector* lvalues = NULL;
00408 cpl_vector* svalues = NULL;
00409 cpl_bivector *positions = NULL;
00410 double mse =0.0;
00411
00412 ordersize++;
00413 XSH_MALLOC( vx, double, ordersize);
00414 memcpy( vx, &(xpos[i-ordersize]), ordersize*sizeof(double));
00415
00416 XSH_MALLOC( vy, double, ordersize);
00417 memcpy( vy, &(ypos[i-ordersize]), ordersize*sizeof(double));
00418
00419 XSH_MALLOC( vl, double, ordersize);
00420 memcpy( vl, &(vlambda[i-ordersize]), ordersize*sizeof(double) ) ;
00421
00422 XSH_MALLOC( vs, double, ordersize);
00423 memcpy( vs, &( vslit[i-ordersize]), ordersize*sizeof(double) ) ;
00424 wmap->list[nborder].sky_size = ordersize ;
00425
00426
00427 wmap->list[nborder].order = orders[i-1] ;
00428 xsh_msg_dbg_high( "Order: %d, Size: %d", wmap->list[nborder].order,
00429 ordersize );
00430
00431 posx = cpl_vector_wrap( ordersize, vx);
00432 posy = cpl_vector_wrap( ordersize, vy);
00433
00434 positions = cpl_bivector_wrap_vectors( posx, posy);
00435 lvalues = cpl_vector_wrap( ordersize, vl);
00436 svalues = cpl_vector_wrap( ordersize, vs);
00437
00438 check(wmap->list[nborder].pol_lambda =
00439 xsh_polynomial_fit_2d_create( positions, lvalues, dispsol_param->deg_x,
00440 &mse));
00441
00442 check(wmap->list[nborder].pol_slit =
00443 xsh_polynomial_fit_2d_create( positions, svalues, dispsol_param->deg_x,
00444 &mse));
00445
00446 cpl_bivector_unwrap_vectors( positions);
00447
00448 cpl_vector_unwrap( posx);
00449 cpl_vector_unwrap( posy);
00450 cpl_vector_unwrap( lvalues);
00451 cpl_vector_unwrap( svalues);
00452
00453 XSH_FREE( vx);
00454 XSH_FREE( vy);
00455 XSH_FREE( vl);
00456 XSH_FREE( vs);
00457 nborder++ ;
00458 ordersize = 0 ;
00459
00460 }
00461 }
00462
00463 cleanup:
00464 XSH_FREE( vx);
00465 XSH_FREE( vy);
00466 XSH_FREE( vl);
00467 XSH_FREE( vs);
00468 return ;
00469 }
00470
00471
00485
00486 void
00487 xsh_wavemap_list_compute( double * vlambda,
00488 double * xpos,
00489 double * ypos,
00490 int nitems,
00491 double * orders,
00492 xsh_dispersol_param * dispsol_param,
00493 xsh_wavemap_list * wmap )
00494 {
00495 int i, ordersize, nborder ;
00496 double *vx = NULL, *vy = NULL, *vl = NULL;
00497
00498 xsh_msg( "Entering xsh_wavemap_compute" ) ;
00499
00500 XSH_ASSURE_NOT_NULL( vlambda ) ;
00501 XSH_ASSURE_NOT_NULL( xpos ) ;
00502 XSH_ASSURE_NOT_NULL( ypos ) ;
00503 XSH_ASSURE_NOT_NULL( orders ) ;
00504 XSH_ASSURE_NOT_NULL( wmap ) ;
00505 XSH_ASSURE_NOT_NULL( dispsol_param ) ;
00506 XSH_ASSURE_NOT_ILLEGAL( nitems > 0 ) ;
00507 XSH_ASSURE_NOT_ILLEGAL( wmap->size ) ;
00508
00509 xsh_msg( " X degree = %d, Y degree = %d", dispsol_param->deg_x,
00510 dispsol_param->deg_y) ;
00511
00512
00513
00514
00515
00516 ordersize = 0;
00517 nborder = 0;
00518
00519 XSH_REGDEBUG( "nitems %d ", nitems);
00520
00521 for( i = 1 ; i<=nitems ; i++ ) {
00522
00523 if ( i < nitems && orders[i-1] == orders[i] ) {
00524 ordersize++;
00525 }
00526 else {
00527 ordersize++;
00528 XSH_MALLOC( vx, double, ordersize);
00529 memcpy( vx, &(xpos[i-ordersize]), ordersize*sizeof(double));
00530
00531 XSH_MALLOC( vy, double, ordersize);
00532 memcpy( vy, &(ypos[i-ordersize]), ordersize*sizeof(double));
00533
00534 XSH_MALLOC( vl, double, ordersize);
00535 memcpy( vl, &(vlambda[i-ordersize]), ordersize*sizeof(double) ) ;
00536
00537 wmap->list[nborder].sky_size = ordersize;
00538
00539 wmap->list[nborder].order = orders[i-1] ;
00540 xsh_msg_dbg_high( "Order: %d, Size: %d", wmap->list[nborder].order,
00541 ordersize );
00542
00543 check( lambda_fit( vl, vx, vy, ordersize, wmap, dispsol_param, nborder ) ) ;
00544 XSH_FREE( vx ) ;
00545 XSH_FREE( vy ) ;
00546 XSH_FREE( vl ) ;
00547 nborder++ ;
00548 ordersize = 0 ;
00549 }
00550 }
00551
00552 cleanup:
00553 XSH_FREE( vx ) ;
00554 XSH_FREE( vy ) ;
00555 XSH_FREE( vl ) ;
00556 return ;
00557 }
00558
00559
00569
00570
00571 static double
00572 xsh_wavemap_list_eval_lambda( xsh_wavemap_list * wmap,
00573 double x, double y, int ord )
00574 {
00575 double tcheb_x = 0.0, tcheb_y = 0.0;
00576 double lambda = -1.;
00577 double res=0.0;
00578 int i, j ;
00579 int pows[2] ;
00580 int degx, degy ;
00581 cpl_vector *tcheb_coef_x = NULL;
00582 cpl_vector *tcheb_coef_y = NULL;
00583
00584 XSH_ASSURE_NOT_NULL( wmap);
00585 degx = wmap->degx;
00586 degy = wmap->degy;
00587 XSH_ASSURE_NOT_ILLEGAL( degx >0 && degy > 0);
00588
00589 xsh_msg_dbg_high( " eval_lambda: degx: %d, min: %lf, max: %lf",
00590 degx, wmap->list[ord].xmin,
00591 wmap->list[ord].xmax ) ;
00592
00593 if ( wmap->list[ord].xmin >= wmap->list[ord].xmax ||
00594 wmap->list[ord].lambda_min >= wmap->list[ord].lambda_max ||
00595 wmap->list[ord].ymin >= wmap->list[ord].ymax ) {
00596 return 0. ;
00597 }
00598
00599 check( tcheb_x = xsh_tools_tchebitchev_transform( x, wmap->list[ord].xmin,
00600 wmap->list[ord].xmax));
00601 check( tcheb_y = xsh_tools_tchebitchev_transform( y,
00602 wmap->list[ord].ymin, wmap->list[ord].ymax));
00603 check( tcheb_coef_x = xsh_tools_tchebitchev_poly_eval(
00604 degx, tcheb_x));
00605 check( tcheb_coef_y = xsh_tools_tchebitchev_poly_eval(
00606 degy, tcheb_y));
00607
00608 for ( i=0; i < degx+1; i++) {
00609 for( j=0; j < degy+1; j++) {
00610 double vx, vy;
00611 double coef;
00612
00613 check( vx = cpl_vector_get( tcheb_coef_x, i));
00614 check( vy = cpl_vector_get( tcheb_coef_y, j));
00615 pows[0] = j;
00616 pows[1] = i;
00617 check( coef = cpl_polynomial_get_coeff( wmap->list[ord].tcheb_pol_lambda,
00618 pows));
00619 res += coef*vx*vy ;
00620 }
00621 }
00622 check( lambda = xsh_tools_tchebitchev_reverse_transform( res,
00623 wmap->list[ord].lambda_min, wmap->list[ord].lambda_max));
00624
00625 cleanup:
00626 xsh_free_vector( &tcheb_coef_x);
00627 xsh_free_vector( &tcheb_coef_y);
00628 return lambda ;
00629 }
00630
00643 cpl_frame *
00644 xsh_wavemap_list_save( xsh_wavemap_list * wmap,
00645 cpl_frame * order_frame,
00646 xsh_pre * pre,
00647 xsh_instrument * instr,
00648 const char * prefix)
00649 {
00650 cpl_frame * result = NULL ;
00651 int order, x, y ;
00652 int start_y, end_y ;
00653 int xmin, xmax ;
00654 int nx, ny ;
00655 double lambda ;
00656 cpl_image * wimg = NULL ;
00657 double * dimg = NULL ;
00658 cpl_propertylist * wheader = NULL ;
00659 char * final_name = NULL;
00660 xsh_order_list * order_list = NULL ;
00661
00662 XSH_ASSURE_NOT_NULL( wmap ) ;
00663 XSH_ASSURE_NOT_NULL( order_frame ) ;
00664 XSH_ASSURE_NOT_NULL( pre ) ;
00665 XSH_ASSURE_NOT_NULL( prefix ) ;
00666 XSH_ASSURE_NOT_NULL( instr ) ;
00667
00668 final_name = xsh_stringcat_any( prefix, ".fits", NULL ) ;
00669 xsh_msg( "Entering xsh_wavemap_save, file \"%s\"", final_name ) ;
00670
00671
00672
00673
00674
00675
00676
00677
00678 check(order_list=xsh_order_list_load(order_frame, instr )) ;
00679
00680
00681
00682
00683
00684 nx = pre->nx ;
00685 ny = pre->ny ;
00686 xsh_msg( "Image size:%d,%d", nx, ny ) ;
00687
00688 check( wimg = cpl_image_new( nx, ny, CPL_TYPE_DOUBLE ) ) ;
00689 check( dimg = cpl_image_get_data_double( wimg ) ) ;
00690
00691 for( order = 0 ; order < wmap->size ; order++ ) {
00692 int count = 0 ;
00693
00694
00695 if ( wmap->list[order].tcheb_pol_lambda == NULL ) {
00696 xsh_msg( "Order %d: NULL Polynome", order ) ;
00697 continue ;
00698 }
00699 if ( wmap->list[order].sky_size <= 0 ) {
00700 xsh_msg( "NOT ENOUGH DATA FOR ORDER %d",
00701 order_list->list[order].absorder ) ;
00702 continue ;
00703 }
00704
00705 start_y = xsh_order_list_get_starty( order_list, order ) ;
00706 end_y = xsh_order_list_get_endy( order_list, order ) ;
00707 xsh_msg_dbg_low( " Order %d, Ymin: %d, Ymax: %d",
00708 order, start_y, end_y ) ;
00709
00710 for ( y = start_y ; y < end_y ; y++ ) {
00711 double dxmin, dxmax ;
00712
00713
00714 check( dxmin = cpl_polynomial_eval_1d(order_list->list[order].edglopoly,
00715 (double)y, NULL ) ) ;
00716 check( dxmax = cpl_polynomial_eval_1d(order_list->list[order].edguppoly,
00717 (double)y, NULL ) ) ;
00718 xmin = floor(dxmin) ;
00719 xmax = ceil(dxmax) ;
00720 for (x = xmin ; x<xmax ; x++ ) {
00721 count++ ;
00722
00723 lambda = xsh_wavemap_list_eval_lambda( wmap, (double)x,
00724 (double)y, order);
00725 if ( cpl_error_get_code() != CPL_ERROR_NONE){
00726 lambda =0.0;
00727 xsh_error_reset();
00728 }
00729
00730 *(dimg + x + (y*nx)) = (float)lambda ;
00731 }
00732 }
00733 xsh_msg_dbg_high( " %d points to calculate", count ) ;
00734 }
00735
00736
00737 check( wheader = cpl_propertylist_duplicate( pre->data_header ) ) ;
00738
00739 check( cpl_image_save( wimg, final_name, CPL_BPP_IEEE_FLOAT,
00740 wheader, CPL_IO_DEFAULT ) ) ;
00741 check(result=xsh_frame_product(final_name,
00742 "TAG",
00743 CPL_FRAME_TYPE_IMAGE,
00744 CPL_FRAME_GROUP_PRODUCT,
00745 CPL_FRAME_LEVEL_TEMPORARY));
00746
00747 cleanup:
00748 xsh_order_list_free( &order_list);
00749 XSH_FREE( final_name ) ;
00750 xsh_free_image ( &wimg ) ;
00751 xsh_free_propertylist( &wheader);
00752
00753 return result ;
00754 }
00755
00768 cpl_frame *
00769 xsh_wavemap_list_save2( xsh_wavemap_list * wmap,
00770 xsh_order_list * order_list,
00771 xsh_pre * pre,
00772 xsh_instrument * instr,
00773 const char * prefix)
00774 {
00775 cpl_frame * result = NULL ;
00776 int order, x, y ;
00777 int start_y, end_y ;
00778 int xmin, xmax ;
00779 int nx, ny ;
00780 double lambda ;
00781 cpl_image * wimg = NULL ;
00782 double * dimg = NULL ;
00783 cpl_propertylist * wheader = NULL ;
00784 char * final_name = NULL;
00785
00786 XSH_ASSURE_NOT_NULL( wmap ) ;
00787 XSH_ASSURE_NOT_NULL( order_list ) ;
00788 XSH_ASSURE_NOT_NULL( pre ) ;
00789 XSH_ASSURE_NOT_NULL( prefix ) ;
00790 XSH_ASSURE_NOT_NULL( instr ) ;
00791
00792 final_name = xsh_stringcat_any( prefix, ".fits", NULL ) ;
00793 xsh_msg( "Entering xsh_wavemap_save, file \"%s\"", final_name ) ;
00794
00795
00796
00797
00798
00799
00800
00801
00802
00803
00804
00805
00806
00807
00808 nx = pre->nx ;
00809 ny = pre->ny ;
00810 xsh_msg( "Image size:%d,%d", nx, ny ) ;
00811
00812 check( wimg = cpl_image_new( nx, ny, CPL_TYPE_DOUBLE ) ) ;
00813 check( dimg = cpl_image_get_data_double( wimg ) ) ;
00814
00815 for( order = 0 ; order < wmap->size ; order++ ) {
00816 int count = 0 ;
00817
00818
00819 if ( wmap->list[order].tcheb_pol_lambda == NULL ) {
00820 xsh_msg( "Order %d: NULL Polynome", order ) ;
00821 continue ;
00822 }
00823 if ( wmap->list[order].sky_size <= 0 ) {
00824 xsh_msg( "NOT ENOUGH DATA FOR ORDER %d",
00825 order_list->list[order].absorder ) ;
00826 continue ;
00827 }
00828
00829 start_y = xsh_order_list_get_starty( order_list, order ) ;
00830 end_y = xsh_order_list_get_endy( order_list, order ) ;
00831 xsh_msg_dbg_low( " Order %d, Ymin: %d, Ymax: %d",
00832 order, start_y, end_y ) ;
00833
00834 for ( y = start_y ; y < end_y ; y++ ) {
00835 double dxmin, dxmax ;
00836
00837
00838 check( dxmin = cpl_polynomial_eval_1d(order_list->list[order].edglopoly,
00839 (double)y, NULL ) ) ;
00840 check( dxmax = cpl_polynomial_eval_1d(order_list->list[order].edguppoly,
00841 (double)y, NULL ) ) ;
00842 xmin = floor(dxmin) ;
00843 xmax = ceil(dxmax) ;
00844 for (x = xmin ; x<xmax ; x++ ) {
00845 count++ ;
00846
00847 lambda = xsh_wavemap_list_eval_lambda( wmap, (double)x,
00848 (double)y, order);
00849 if ( cpl_error_get_code() != CPL_ERROR_NONE){
00850 lambda =0.0;
00851 xsh_error_reset();
00852 }
00853
00854 *(dimg + x + (y*nx)) = (float)lambda ;
00855 }
00856 }
00857 xsh_msg_dbg_high( " %d points to calculate", count ) ;
00858 }
00859
00860
00861 check( wheader = cpl_propertylist_duplicate( pre->data_header ) ) ;
00862
00863 check( cpl_image_save( wimg, final_name, CPL_BPP_IEEE_FLOAT,
00864 wheader, CPL_IO_DEFAULT ) ) ;
00865 check(result=xsh_frame_product(final_name,
00866 "TAG",
00867 CPL_FRAME_TYPE_IMAGE,
00868 CPL_FRAME_GROUP_PRODUCT,
00869 CPL_FRAME_LEVEL_TEMPORARY));
00870
00871 cleanup:
00872 xsh_order_list_free( &order_list);
00873 XSH_FREE( final_name ) ;
00874 xsh_free_image ( &wimg ) ;
00875 xsh_free_propertylist( &wheader);
00876
00877 return result ;
00878 }
00879
00880
00881
00895
00896 cpl_frame*
00897 xsh_wavemap_list_save_poly( xsh_wavemap_list* wmap,
00898 cpl_frame *order_frame,
00899 xsh_pre *pre,
00900 xsh_instrument *instr,
00901 const char *prefix,
00902 cpl_frame **dispersol_frame,
00903 cpl_frame **slitmap_frame)
00904 {
00905 cpl_frame *result = NULL;
00906 const char *slit_tag;
00907 int order;
00908 xsh_dispersol_list *dispersol_list = NULL;
00909
00910 XSH_ASSURE_NOT_NULL( wmap);
00911 XSH_ASSURE_NOT_NULL( order_frame);
00912 XSH_ASSURE_NOT_NULL( prefix);
00913 XSH_ASSURE_NOT_NULL( dispersol_frame);
00914 XSH_ASSURE_NOT_NULL( instr);
00915
00916
00917
00918 check( dispersol_list = xsh_dispersol_list_new( wmap->size,
00919 wmap->degx, wmap->degy, instr));
00920
00921 for( order = 0 ; order < wmap->size ; order++ ) {
00922
00923 check( xsh_dispersol_list_add( dispersol_list, order,
00924 wmap->list[order].order, wmap->list[order].pol_lambda,
00925 wmap->list[order].pol_slit));
00926 wmap->list[order].pol_lambda = NULL;
00927 wmap->list[order].pol_slit = NULL;
00928 }
00929
00930 if (pre != NULL){
00931
00932 check( result = xsh_dispersol_list_to_wavemap( dispersol_list,
00933 order_frame, pre,
00934 instr,prefix));
00935
00936 slit_tag = XSH_GET_TAG_FROM_ARM( XSH_SLIT_MAP_POLY, instr);
00937
00938
00939 check (*slitmap_frame = xsh_dispersol_list_to_slitmap( dispersol_list,
00940 order_frame, pre,
00941 instr, slit_tag));
00942 }
00943
00944
00945 check( *dispersol_frame = xsh_dispersol_list_save( dispersol_list, instr));
00946
00947
00948 cleanup:
00949 if (cpl_error_get_code() != CPL_ERROR_NONE){
00950 xsh_free_frame( &result);
00951 }
00952 xsh_dispersol_list_free( &dispersol_list);
00953
00954
00955 return result ;
00956 }
00957