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 <xsh_data_the_map.h>
00046 #include <xsh_utils.h>
00047 #include <xsh_utils_table.h>
00048 #include <xsh_error.h>
00049 #include <xsh_msg.h>
00050 #include <xsh_pfits.h>
00051 #include <cpl.h>
00052 #include <xsh_drl.h>
00053 #include <math.h>
00054
00055
00056
00057 static int xsh_the_map_lambda_compare(const void* one, const void* two){
00058 xsh_the_arcline** a = NULL;
00059 xsh_the_arcline** b = NULL;
00060 float la, lb;
00061
00062 a = (xsh_the_arcline**) one;
00063 b = (xsh_the_arcline**) two;
00064
00065 la = (*a)->wavelength;
00066 lb = (*b)->wavelength;
00067
00068 if (la <= lb)
00069 return -1;
00070 else
00071 return 1;
00072
00073
00074 }
00075
00076 static int xsh_the_map_lambda_order_slit_compare(const void* one,
00077 const void* two){
00078
00079 xsh_the_arcline** a = NULL;
00080 xsh_the_arcline** b = NULL;
00081 float la, lb;
00082 int oa, ob;
00083 float sa, sb;
00084
00085 a = (xsh_the_arcline**) one;
00086 b = (xsh_the_arcline**) two;
00087
00088 la = (*a)->wavelength;
00089 lb = (*b)->wavelength;
00090
00091 oa = (*a)->order;
00092 ob = (*b)->order;
00093
00094 sa = (*a)->slit_position;
00095 sb = (*b)->slit_position;
00096
00097 if ( (lb - la) > WAVELENGTH_PRECISION ){
00098 return -1;
00099 }
00100 else if ((lb - la) < WAVELENGTH_PRECISION ){
00101 return 1;
00102 }
00103 else {
00104 if (oa < ob) {
00105 return -1;
00106 }
00107 else if ( oa > ob) {
00108 return 1;
00109 }
00110 else{
00111 if (sa <= sb){
00112 return -1;
00113 }
00114 else{
00115 return 1;
00116 }
00117 }
00118 }
00119 }
00120
00126
00127 void xsh_the_map_lambda_sort(xsh_the_map* list)
00128 {
00129 qsort(list->list,list->size,sizeof(xsh_the_arcline*),
00130 xsh_the_map_lambda_compare);
00131 }
00132
00133 void xsh_the_map_lambda_order_slit_sort(xsh_the_map* list)
00134 {
00135 qsort(list->list,list->size,sizeof(xsh_the_arcline*),
00136 xsh_the_map_lambda_order_slit_compare);
00137 }
00138
00145
00146 int xsh_the_map_get_size(xsh_the_map* list)
00147 {
00148 int i=0;
00149
00150 XSH_ASSURE_NOT_NULL(list);
00151 i = list->size;
00152
00153 cleanup:
00154 return i;
00155 }
00156
00164
00165 double xsh_the_map_get_detx(xsh_the_map* list, int idx)
00166 {
00167 double res = 0.0;
00168
00169 XSH_ASSURE_NOT_NULL(list);
00170 XSH_ASSURE_NOT_ILLEGAL(idx >=0 && idx < list->size);
00171 XSH_ASSURE_NOT_NULL(list->list[idx]);
00172 res = list->list[idx]->detector_x;
00173
00174 cleanup:
00175 return res;
00176 }
00177
00185
00186 double xsh_the_map_get_dety(xsh_the_map* list, int idx)
00187 {
00188 double res = 0.0;
00189
00190 XSH_ASSURE_NOT_NULL(list);
00191 XSH_ASSURE_NOT_ILLEGAL(idx >=0 && idx < list->size);
00192 XSH_ASSURE_NOT_NULL(list->list[idx]);
00193
00194 res = list->list[idx]->detector_y;
00195
00196 cleanup:
00197 return res;
00198 }
00199
00200
00208
00209 float xsh_the_map_get_wavelength(xsh_the_map* list, int idx)
00210 {
00211 float res = 0.0;
00212
00213 XSH_ASSURE_NOT_NULL(list);
00214 XSH_ASSURE_NOT_ILLEGAL(idx >=0 && idx < list->size);
00215 XSH_ASSURE_NOT_NULL(list->list[idx]);
00216
00217 res = list->list[idx]->wavelength;
00218
00219 cleanup:
00220 return res;
00221 }
00222
00223
00231 int xsh_the_map_get_order(xsh_the_map* list, int idx)
00232 {
00233 int res = 0;
00234
00235 XSH_ASSURE_NOT_NULL(list);
00236 XSH_ASSURE_NOT_ILLEGAL(idx >=0 && idx < list->size);
00237 XSH_ASSURE_NOT_NULL(list->list[idx]);
00238
00239 res = list->list[idx]->order;
00240
00241 cleanup:
00242 return res;
00243 }
00244
00245
00246
00254 int xsh_the_map_get_slit_index(xsh_the_map* list, int idx)
00255 {
00256 int res = 0;
00257
00258 XSH_ASSURE_NOT_NULL(list);
00259 XSH_ASSURE_NOT_ILLEGAL(idx >=0 && idx < list->size);
00260 XSH_ASSURE_NOT_NULL(list->list[idx]);
00261
00262 res = list->list[idx]->slit_index;
00263
00264 cleanup:
00265 return res;
00266 }
00267
00268
00276 float xsh_the_map_get_slit_position( xsh_the_map* list, int idx)
00277 {
00278 float res = 0;
00279
00280 XSH_ASSURE_NOT_NULL(list);
00281 XSH_ASSURE_NOT_ILLEGAL(idx >=0 && idx < list->size);
00282 XSH_ASSURE_NOT_NULL(list->list[idx]);
00283
00284 res = list->list[idx]->slit_position;
00285
00286 cleanup:
00287 return res;
00288 }
00289
00290
00296
00297 void xsh_dump_the_map( xsh_the_map* the)
00298 {
00299 int i = 0;
00300
00301 assure(the != NULL,CPL_ERROR_NULL_INPUT,"Null the map");
00302
00303 xsh_msg( "THE_MAP Dump %d lines",the->size);
00304 for(i=0; i< the->size; i++) {
00305 xsh_msg(" Wavelength %f order %d slit_position %f detector_x %f \
00306 detector_y %f", the->list[i]->wavelength, the->list[i]->order,
00307 the->list[i]->slit_position, the->list[i]->detector_x,
00308 the->list[i]->detector_y);
00309 }
00310 xsh_msg( "END THE_MAP");
00311
00312 cleanup:
00313 return;
00314 }
00315
00316 void xsh_the_map_set_arcline( xsh_the_map* list, int idx, float wavelength,
00317 int order, int slit_index, float slit_position, double detx, double dety)
00318 {
00319 XSH_ASSURE_NOT_NULL( list);
00320
00321 XSH_ASSURE_NOT_ILLEGAL( idx >= 0);
00322 XSH_ASSURE_NOT_ILLEGAL( list->size > idx);
00323
00324 list->list[idx]->wavelength = wavelength;
00325 list->list[idx]->order = order;
00326 list->list[idx]->slit_index = slit_index;
00327 list->list[idx]->slit_position = slit_position;
00328 list->list[idx]->detector_x = detx;
00329 list->list[idx]->detector_y = dety;
00330
00331 cleanup :
00332 return;
00333 }
00334
00335
00345
00346 xsh_the_map* xsh_the_map_create( int size)
00347 {
00348 xsh_the_map* result = NULL;
00349 int i;
00350
00351 XSH_ASSURE_NOT_ILLEGAL( size > 0);
00352
00353
00354 XSH_CALLOC( result, xsh_the_map, 1);
00355
00356 result->size = size ;
00357
00358 XSH_CALLOC( result->list, xsh_the_arcline*, result->size);
00359
00360 for( i=0; i<size; i++){
00361 XSH_CALLOC( result->list[i], xsh_the_arcline,1);
00362 }
00363
00364 cleanup:
00365 if ( cpl_error_get_code() != CPL_ERROR_NONE){
00366 xsh_the_map_free( &result);
00367 }
00368 return result;
00369 }
00370
00371
00372
00384
00385 xsh_the_map* xsh_the_map_load( cpl_frame* frame)
00386 {
00387 cpl_table* table = NULL;
00388 const char* tablename = NULL;
00389 xsh_the_map* result = NULL;
00390 int i, k ;
00391 cpl_propertylist * header = NULL ;
00392 int full_size ;
00393 int real_size = 0 ;
00394
00395
00396 XSH_ASSURE_NOT_NULL( frame);
00397
00398
00399 check( tablename = cpl_frame_get_filename(frame));
00400
00401 XSH_TABLE_LOAD( table, tablename);
00402
00403 check_msg( header = cpl_propertylist_load( tablename, 1 ),
00404 "Can't load header from %s", tablename ) ;
00405
00406 xsh_msg_dbg_medium("Loading %s", tablename);
00407
00408
00409 XSH_CALLOC( result, xsh_the_map, 1);
00410
00411
00412
00413
00414 check (full_size = cpl_table_get_nrow( table));
00415
00416 xsh_msg_dbg_medium("size %d",full_size);
00417
00418 for( i = 0; i<full_size ; i++) {
00419 float lambda ;
00420
00421 check (xsh_get_table_value( table, XSH_THE_MAP_TABLE_COLNAME_WAVELENGTH,
00422 CPL_TYPE_FLOAT, i, &lambda));
00423 if ( lambda > WAVELENGTH_PRECISION ) real_size++ ;
00424 }
00425
00426 xsh_msg_dbg_medium("real size %d",real_size);
00427
00428 assure(real_size > 0, CPL_ERROR_ILLEGAL_INPUT,
00429 "no valid lambda found in the_map");
00430
00431
00432 result->size = real_size ;
00433
00434 XSH_CALLOC( result->list, xsh_the_arcline*, result->size);
00435
00436 for(i=0, k = 0 ; i<full_size; i++) {
00437 xsh_the_arcline * arc = NULL;
00438 float lambda = 0.0;
00439
00440 check(xsh_get_table_value( table, XSH_THE_MAP_TABLE_COLNAME_WAVELENGTH,
00441 CPL_TYPE_FLOAT, i, &lambda ));
00442
00443 if ( fabs(lambda) < WAVELENGTH_PRECISION || (lambda != lambda) ) continue;
00444 XSH_CALLOC( arc, xsh_the_arcline,1);
00445 arc->wavelength = lambda ;
00446 check(xsh_get_table_value(table, XSH_THE_MAP_TABLE_COLNAME_ORDER,
00447 CPL_TYPE_INT, i, &(arc->order)));
00448 check(xsh_get_table_value(table, XSH_THE_MAP_TABLE_COLNAME_SLITPOSITION,
00449 CPL_TYPE_FLOAT, i, &(arc->slit_position)));
00450 check(xsh_get_table_value(table, XSH_THE_MAP_TABLE_COLNAME_SLITINDEX,
00451 CPL_TYPE_INT, i, &(arc->slit_index)));
00452 check(xsh_get_table_value(table, XSH_THE_MAP_TABLE_COLNAME_DETECTORX,
00453 CPL_TYPE_DOUBLE, i, &(arc->detector_x)));
00454 check(xsh_get_table_value(table, XSH_THE_MAP_TABLE_COLNAME_DETECTORY,
00455 CPL_TYPE_DOUBLE, i, &(arc->detector_y)));
00456 result->list[k] = arc;
00457 k++ ;
00458 }
00459 check( result->header = cpl_propertylist_duplicate( header ) ) ;
00460 cleanup:
00461 XSH_TABLE_FREE( table);
00462 xsh_free_propertylist( &header ) ;
00463 return result;
00464 }
00465
00466
00467
00472
00473 void xsh_the_arcline_free(xsh_the_arcline** arc) {
00474 if( arc && *arc) {
00475 cpl_free(*arc);
00476 *arc = NULL;
00477 }
00478
00479 }
00480
00485
00486 void xsh_the_map_free(xsh_the_map** list)
00487 {
00488 int i = 0;
00489
00490 if (list && *list){
00491 if ((*list)->list){
00492 for(i=0; i < (*list)->size; i++) {
00493 xsh_the_arcline* arc = (*list)->list[i];
00494 xsh_the_arcline_free(&arc);
00495 }
00496 cpl_free((*list)->list);
00497 xsh_free_propertylist( &((*list)->header) ) ;
00498 (*list)->list = NULL;
00499 }
00500 cpl_free(*list);
00501 *list = NULL;
00502 }
00503 }
00504
00512
00513 cpl_frame* xsh_the_map_save(xsh_the_map* list,const char* filename)
00514 {
00515 cpl_table* table = NULL;
00516 cpl_frame * result = NULL ;
00517 int i=0;
00518
00519 XSH_ASSURE_NOT_NULL( list);
00520
00521
00522 check(table = cpl_table_new(XSH_THE_MAP_TABLE_NB_COL));
00523
00524
00525 check(
00526 cpl_table_new_column(table,XSH_THE_MAP_TABLE_COLNAME_WAVELENGTH,
00527 CPL_TYPE_FLOAT));
00528 check(
00529 cpl_table_set_column_unit ( table, XSH_THE_MAP_TABLE_COLNAME_WAVELENGTH,
00530 XSH_THE_MAP_TABLE_UNIT_WAVELENGTH));
00531 check(
00532 cpl_table_new_column(table,XSH_THE_MAP_TABLE_COLNAME_ORDER,
00533 CPL_TYPE_INT));
00534 check(
00535 cpl_table_set_column_unit ( table, XSH_THE_MAP_TABLE_COLNAME_ORDER,
00536 XSH_THE_MAP_TABLE_UNIT_ORDER));
00537 check(
00538 cpl_table_new_column(table,XSH_THE_MAP_TABLE_COLNAME_SLITPOSITION,
00539 CPL_TYPE_FLOAT));
00540 check(
00541 cpl_table_new_column(table,XSH_THE_MAP_TABLE_COLNAME_SLITINDEX,
00542 CPL_TYPE_INT));
00543 check(
00544 cpl_table_set_column_unit ( table, XSH_THE_MAP_TABLE_COLNAME_SLITPOSITION,
00545 XSH_THE_MAP_TABLE_UNIT_SLITPOSITION));
00546 check(
00547 cpl_table_new_column(table,XSH_THE_MAP_TABLE_COLNAME_DETECTORX,
00548 CPL_TYPE_DOUBLE));
00549 check(
00550 cpl_table_set_column_unit ( table, XSH_THE_MAP_TABLE_COLNAME_DETECTORX,
00551 XSH_THE_MAP_TABLE_UNIT_DETECTORX));
00552 check(
00553 cpl_table_new_column(table,XSH_THE_MAP_TABLE_COLNAME_DETECTORY,
00554 CPL_TYPE_DOUBLE));
00555 check(
00556 cpl_table_set_column_unit ( table, XSH_THE_MAP_TABLE_COLNAME_DETECTORY,
00557 XSH_THE_MAP_TABLE_UNIT_DETECTORY));
00558
00559 check(cpl_table_set_size(table,list->size));
00560
00561
00562 for(i=0;i<list->size;i++){
00563 check(cpl_table_set_float(table,XSH_THE_MAP_TABLE_COLNAME_WAVELENGTH,
00564 i,list->list[i]->wavelength));
00565 check(cpl_table_set_int(table,XSH_THE_MAP_TABLE_COLNAME_ORDER,
00566 i,list->list[i]->order));
00567 check(cpl_table_set_float(table,XSH_THE_MAP_TABLE_COLNAME_SLITPOSITION,
00568 i,list->list[i]->slit_position));
00569 check(cpl_table_set_int(table,XSH_THE_MAP_TABLE_COLNAME_SLITINDEX,
00570 i,list->list[i]->slit_index));
00571 check(cpl_table_set_double(table,XSH_THE_MAP_TABLE_COLNAME_DETECTORX,
00572 i,list->list[i]->detector_x));
00573 check(cpl_table_set_double(table,XSH_THE_MAP_TABLE_COLNAME_DETECTORY,
00574 i,list->list[i]->detector_y));
00575 }
00576
00577
00578 check(cpl_table_save(table, NULL,NULL,filename, CPL_IO_DEFAULT));
00579
00580
00581 check(result=xsh_frame_product(filename,
00582 "TAG",
00583 CPL_FRAME_TYPE_TABLE,
00584 CPL_FRAME_GROUP_PRODUCT,
00585 CPL_FRAME_LEVEL_TEMPORARY));
00586
00587 cleanup:
00588 XSH_TABLE_FREE( table);
00589 return result ;
00590 }
00591