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 <math.h>
00044 #include <xsh_data_spectralformat.h>
00045 #include <xsh_utils.h>
00046 #include <xsh_error.h>
00047 #include <xsh_msg.h>
00048 #include <xsh_pfits.h>
00049 #include <cpl.h>
00050 #include <xsh_utils_table.h>
00051 #include <xsh_drl.h>
00052
00053
00054
00055
00056
00057
00066
00067 void xsh_spectralformat_list_dump( xsh_spectralformat_list* list,
00068 const char * fname )
00069 {
00070 int i ;
00071 FILE * fout ;
00072
00073 if ( fname == NULL ) fout = stdout ;
00074 else fout = fopen( fname, "w" ) ;
00075
00076 for( i = 0 ; i<list->size ; i++ ) {
00077 fprintf( fout, "Order: %d, Lambda Min: %f,Lambda Max: %f\n",
00078 list->list[i].absorder, list->list[i].lambda_min,
00079 list->list[i].lambda_max ) ;
00080 }
00081 if ( fname != NULL ) fclose( fout ) ;
00082 }
00083
00084
00085
00086
00096
00097 xsh_spectralformat_list* xsh_spectralformat_list_create( int size,
00098 xsh_instrument* instr)
00099 {
00100 xsh_spectralformat_list* result = NULL;
00101
00102
00103 XSH_ASSURE_NOT_ILLEGAL( size > 0);
00104
00105 XSH_CALLOC( result, xsh_spectralformat_list, 1);
00106 result->size = size;
00107
00108 result->instrument = instr;
00109 XSH_CALLOC(result->list, xsh_spectralformat, result->size);
00110 XSH_NEW_PROPERTYLIST(result->header);
00111
00112 cleanup:
00113 if (cpl_error_get_code() != CPL_ERROR_NONE) {
00114 xsh_spectralformat_list_free(&result);
00115 }
00116 return result;
00117 }
00118
00119
00120
00131
00132 xsh_spectralformat_list* xsh_spectralformat_list_load(cpl_frame* frame,
00133 xsh_instrument* instr)
00134 {
00135 cpl_table* table = NULL;
00136 cpl_propertylist* header = NULL;
00137 const char* tablename = NULL;
00138 xsh_spectralformat_list * result = NULL;
00139 int i = 0 ;
00140 double value ;
00141 int size;
00142
00143
00144 XSH_ASSURE_NOT_NULL( frame);
00145 XSH_ASSURE_NOT_NULL( instr);
00146
00147
00148 check( tablename = cpl_frame_get_filename( frame));
00149
00150 XSH_TABLE_LOAD( table, tablename);
00151
00152 check( size = cpl_table_get_nrow( table));
00153 check( result = xsh_spectralformat_list_create( size, instr));
00154
00155 check( header = cpl_propertylist_load(tablename,1));
00156 check( cpl_propertylist_append(result->header, header));
00157
00158 for(i=0;i<result->size;i++){
00159 const char * lamp ;
00160
00161
00162 check( xsh_get_table_value( table, XSH_SPECTRALFORMAT_TABLE_COLNAME_ORDER,
00163 CPL_TYPE_INT, i, &(result->list[i].absorder)));
00164
00165 check( lamp = cpl_table_get_string(table,
00166 XSH_SPECTRALFORMAT_TABLE_COLNAME_LAMP, i));
00167 strcpy( result->list[i].lamp, lamp);
00168
00169 check( value = cpl_table_get( table,
00170 XSH_SPECTRALFORMAT_TABLE_COLNAME_WLMINFUL, i, NULL));
00171 result->list[i].lambda_min_full = (float)value;
00172
00173 check( value = cpl_table_get( table,
00174 XSH_SPECTRALFORMAT_TABLE_COLNAME_WLMAXFUL, i, NULL));
00175 result->list[i].lambda_max_full = (float)value;
00176
00177 check( value = cpl_table_get( table,
00178 XSH_SPECTRALFORMAT_TABLE_COLNAME_WLMIN, i, NULL));
00179 result->list[i].lambda_min = (float)value;
00180
00181 check( value = cpl_table_get( table,
00182 XSH_SPECTRALFORMAT_TABLE_COLNAME_WLMAX, i, NULL));
00183 result->list[i].lambda_max = (float)value;
00184
00185 check( value = cpl_table_get( table,
00186 XSH_SPECTRALFORMAT_TABLE_COLNAME_FLSR, i, NULL));
00187 result->list[i].flsr = (float)value;
00188
00189 check( value = cpl_table_get( table,
00190 XSH_SPECTRALFORMAT_TABLE_COLNAME_UFSR, i, NULL));
00191 result->list[i].ufsr = (float)value;
00192
00193
00194 if(cpl_table_has_column(table,XSH_SPECTRALFORMAT_TABLE_COLNAME_XMIN)) {
00195 check( value = cpl_table_get( table,
00196 XSH_SPECTRALFORMAT_TABLE_COLNAME_XMIN, i, NULL));
00197 result->list[i].xmin = (float)value;
00198
00199 check( value = cpl_table_get( table,
00200 XSH_SPECTRALFORMAT_TABLE_COLNAME_XMAX, i, NULL));
00201 result->list[i].xmax = (float)value;
00202
00203
00204 check( value = cpl_table_get( table,
00205 XSH_SPECTRALFORMAT_TABLE_COLNAME_YMIN, i, NULL));
00206 result->list[i].ymin = (float)value;
00207
00208 check( value = cpl_table_get( table,
00209 XSH_SPECTRALFORMAT_TABLE_COLNAME_YMAX, i, NULL));
00210 result->list[i].ymax = (float)value;
00211
00212 }
00213 }
00214 cleanup:
00215 if (cpl_error_get_code () != CPL_ERROR_NONE) {
00216 xsh_error_msg( "can't load frame %s",cpl_frame_get_filename(frame));
00217 xsh_spectralformat_list_free( &result);
00218 }
00219 xsh_free_propertylist( &header);
00220 XSH_TABLE_FREE( table);
00221 return result;
00222 }
00223
00224
00225
00232
00233 void xsh_spectralformat_list_free( xsh_spectralformat_list** list)
00234 {
00235 if ( list && *list){
00236
00237 if ( ( *list)->list){
00238 cpl_free( (*list)->list);
00239 }
00240 xsh_free_propertylist( &((*list)->header));
00241 cpl_free( *list);
00242 *list = NULL;
00243 }
00244 }
00245
00246
00247
00248
00249
00258
00259 cpl_propertylist* xsh_spectralformat_list_get_header(
00260 xsh_spectralformat_list* list)
00261 {
00262 cpl_propertylist *res = NULL;
00263
00264 XSH_ASSURE_NOT_NULL(list);
00265 res = list->header;
00266 cleanup:
00267 return res;
00268 }
00269
00270
00271
00282
00283 float xsh_spectralformat_list_get_lambda_min( xsh_spectralformat_list *list,
00284 int absorder)
00285 {
00286 int i ;
00287 float res = 0. ;
00288
00289 XSH_ASSURE_NOT_NULL(list);
00290
00291 for( i = 0; i<list->size; i++){
00292 if ( list->list[i].absorder == absorder){
00293 res = list->list[i].lambda_min ;
00294 break ;
00295 }
00296 }
00297 cleanup:
00298 return res ;
00299 }
00300
00301
00302
00303
00304 const char * xsh_spectralformat_list_get_lamp( xsh_spectralformat_list * list,
00305 int absorder )
00306 {
00307 int i ;
00308 const char *res = NULL;
00309
00310 XSH_ASSURE_NOT_NULL( list);
00311
00312 for( i = 0 ; i<list->size ; i++ )
00313 if ( list->list[i].absorder == absorder ) {
00314 res = list->list[i].lamp ;
00315 break ;
00316 }
00317
00318 cleanup:
00319 return res ;
00320 }
00321
00322
00323
00332
00333 float xsh_spectralformat_list_get_lambda_max( xsh_spectralformat_list * list,
00334 int absorder )
00335 {
00336 int i ;
00337 float res = 0. ;
00338
00339 XSH_ASSURE_NOT_NULL(list);
00340 for( i = 0 ; i<list->size ; i++ )
00341 if ( list->list[i].absorder == absorder ) {
00342 res = list->list[i].lambda_max ;
00343 break ;
00344 }
00345
00346 cleanup:
00347 return res ;
00348 }
00349
00350
00351
00362
00363 cpl_vector* xsh_spectralformat_list_get_orders(
00364 xsh_spectralformat_list *list, float lambda)
00365 {
00366 cpl_vector* result = NULL;
00367 int i;
00368 int size = 0;
00369 int order_tab[20];
00370
00371 XSH_ASSURE_NOT_NULL(list);
00372
00373 for( i = 0 ; i<list->size ; i++ ){
00374 float min=0.0, max=0.0;
00375
00376 min = list->list[i].lambda_min_full;
00377 max = list->list[i].lambda_max_full;
00378
00379 xsh_msg_dbg_high( "search lambda %f in [%f,%f]", lambda, min, max);
00380
00381 if (lambda >= min && lambda <= max){
00382 order_tab[size] = list->list[i].absorder;
00383 size++;
00384 }
00385 }
00386
00387 if (size > 0){
00388 check( result = cpl_vector_new( size));
00389 for( i=0; i< size; i++){
00390 check( cpl_vector_set( result, i, order_tab[i]));
00391 }
00392 }
00393 cleanup:
00394 if ( cpl_error_get_code() != CPL_ERROR_NONE){
00395 xsh_free_vector( &result);
00396 }
00397 return result;
00398 }
00399
00400 void xsh_spectralformat_check_wlimit( xsh_spectralformat_list *spectralformat,
00401 xsh_order_list* orderlist, xsh_wavesol *wavesol,
00402 xsh_xs_3 *model, xsh_instrument *instr)
00403 {
00404 int iorder = 0;
00405
00406 XSH_ASSURE_NOT_NULL( spectralformat);
00407 XSH_ASSURE_NOT_NULL( orderlist);
00408
00409 for( iorder=0; iorder < spectralformat->size; iorder++){
00410 double absorder = 0;
00411 double slit = 0.0;
00412 double wmin, wmax;
00413 double ymin, ymax;
00414 double y_wmin, y_wmax;
00415
00416 absorder = (double)spectralformat->list[iorder].absorder;
00417 wmin = spectralformat->list[iorder].lambda_min;
00418 wmax = spectralformat->list[iorder].lambda_max;
00419
00420 ymin = (double)orderlist->list[iorder].starty;
00421 ymax = (double)orderlist->list[iorder].endy;
00422
00423 if (wavesol != NULL){
00424 check( y_wmin = xsh_wavesol_eval_poly( wavesol, wmin, absorder,
00425 slit));
00426 check( y_wmax = xsh_wavesol_eval_poly( wavesol, wmax, absorder,
00427 slit));
00428 }
00429 else{
00430 double x;
00431
00432 check( xsh_model_get_xy( model, instr, wmin, absorder, slit,
00433 &x, &y_wmin));
00434 check( xsh_model_get_xy( model, instr, wmax, absorder, slit,
00435 &x, &y_wmax));
00436 }
00437 if ( y_wmin < ymin || y_wmin > ymax){
00438 xsh_msg_warning("For order %f at wmin %f : y %f not in [%f,%f]",
00439 absorder, wmin, y_wmin, ymin, ymax);
00440 }
00441 if ( y_wmax > ymax || y_wmax < ymin){
00442 xsh_msg_warning("For order %f at wmax %f : y %f not in [%f,%f]",
00443 absorder, wmax, y_wmax, ymin, ymax);
00444 }
00445
00446
00447 }
00448
00449 cleanup:
00450 return;
00451 }
00452