X-shooter Pipeline Reference Manual 3.8.15
xsh_data_the_map.c
Go to the documentation of this file.
1/* *
2 * This file is part of the ESO X-shooter Pipeline *
3 * Copyright (C) 2006 European Southern Observatory *
4 * *
5 * This library is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the Free Software *
17 * Foundation, 51 Franklin St, Fifth Floor, Boston, MA 02111-1307 USA *
18 * */
19
20/*
21 * $Author: amodigli $
22 * $Date: 2012-12-17 16:57:15 $
23 * $Revision: 1.27 $
24 * $Name: not supported by cvs2svn $
25 */
26
27#ifdef HAVE_CONFIG_H
28#include <config.h>
29#endif
30
31/*---------------------------------------------------------------------------*/
36/*---------------------------------------------------------------------------*/
37
41/*-----------------------------------------------------------------------------
42 Includes
43 ----------------------------------------------------------------------------*/
44
45#include <xsh_data_the_map.h>
46#include <xsh_utils.h>
47#include <xsh_utils_table.h>
48#include <xsh_error.h>
49#include <xsh_msg.h>
50#include <xsh_pfits.h>
51#include <cpl.h>
52#include <xsh_drl.h>
53#include <math.h>
54/*----------------------------------------------------------------------------
55 Function implementation
56 ----------------------------------------------------------------------------*/
57static int xsh_the_map_lambda_compare(const void* one, const void* two){
58 xsh_the_arcline** a = NULL;
59 xsh_the_arcline** b = NULL;
60 float la, lb;
61
62 a = (xsh_the_arcline**) one;
63 b = (xsh_the_arcline**) two;
64
65 la = (*a)->wavelength;
66 lb = (*b)->wavelength;
67
68 if (la <= lb)
69 return -1;
70 else
71 return 1;
72
73
74}
75
76static int xsh_the_map_lambda_order_slit_compare(const void* one,
77 const void* two){
78
79 xsh_the_arcline** a = NULL;
80 xsh_the_arcline** b = NULL;
81 float la, lb;
82 int oa, ob;
83 float sa, sb;
84
85 a = (xsh_the_arcline**) one;
86 b = (xsh_the_arcline**) two;
87
88 la = (*a)->wavelength;
89 lb = (*b)->wavelength;
90
91 oa = (*a)->order;
92 ob = (*b)->order;
93
94 sa = (*a)->slit_position;
95 sb = (*b)->slit_position;
96
97 if ( (lb - la) > WAVELENGTH_PRECISION ){
98 return -1;
99 }
100 else if ((lb - la) < WAVELENGTH_PRECISION ){
101 return 1;
102 }
103 else {
104 if (oa < ob) {
105 return -1;
106 }
107 else if ( oa > ob) {
108 return 1;
109 }
110 else{
111 if (sa <= sb){
112 return -1;
113 }
114 else{
115 return 1;
116 }
117 }
118 }
119}
120/*---------------------------------------------------------------------------*/
126/*---------------------------------------------------------------------------*/
128{
129 qsort(list->list,list->size,sizeof(xsh_the_arcline*),
131}
132
134{
135 qsort(list->list,list->size,sizeof(xsh_the_arcline*),
137}
138/*---------------------------------------------------------------------------*/
145/*---------------------------------------------------------------------------*/
147{
148 int i=0;
149
151 i = list->size;
152
153 cleanup:
154 return i;
155}
156/*---------------------------------------------------------------------------*/
164/*---------------------------------------------------------------------------*/
165double xsh_the_map_get_detx(xsh_the_map* list, int idx)
166{
167 double res = 0.0;
168
170 XSH_ASSURE_NOT_ILLEGAL(idx >=0 && idx < list->size);
171 XSH_ASSURE_NOT_NULL(list->list[idx]);
172 res = list->list[idx]->detector_x;
173
174 cleanup:
175 return res;
176}
177/*---------------------------------------------------------------------------*/
185/*---------------------------------------------------------------------------*/
186double xsh_the_map_get_dety(xsh_the_map* list, int idx)
187{
188 double res = 0.0;
189
191 XSH_ASSURE_NOT_ILLEGAL(idx >=0 && idx < list->size);
192 XSH_ASSURE_NOT_NULL(list->list[idx]);
193
194 res = list->list[idx]->detector_y;
195
196 cleanup:
197 return res;
198}
199
200/*---------------------------------------------------------------------------*/
208/*---------------------------------------------------------------------------*/
210{
211 float res = 0.0;
212
214 XSH_ASSURE_NOT_ILLEGAL(idx >=0 && idx < list->size);
215 XSH_ASSURE_NOT_NULL(list->list[idx]);
216
217 res = list->list[idx]->wavelength;
218
219 cleanup:
220 return res;
221}
222
223/*---------------------------------------------------------------------------*/
232{
233 int res = 0;
234
236 XSH_ASSURE_NOT_ILLEGAL(idx >=0 && idx < list->size);
237 XSH_ASSURE_NOT_NULL(list->list[idx]);
238
239 res = list->list[idx]->order;
240
241 cleanup:
242 return res;
243}
244
245
246/*---------------------------------------------------------------------------*/
255{
256 int res = 0;
257
259 XSH_ASSURE_NOT_ILLEGAL(idx >=0 && idx < list->size);
260 XSH_ASSURE_NOT_NULL(list->list[idx]);
261
262 res = list->list[idx]->slit_index;
263
264 cleanup:
265 return res;
266}
267
268/*---------------------------------------------------------------------------*/
277{
278 float res = 0;
279
281 XSH_ASSURE_NOT_ILLEGAL(idx >=0 && idx < list->size);
282 XSH_ASSURE_NOT_NULL(list->list[idx]);
283
284 res = list->list[idx]->slit_position;
285
286 cleanup:
287 return res;
288}
289
290/*---------------------------------------------------------------------------*/
296/*---------------------------------------------------------------------------*/
298{
299 int i = 0;
300
301 assure(the != NULL,CPL_ERROR_NULL_INPUT,"Null the map");
302
303 xsh_msg( "THE_MAP Dump %d lines",the->size);
304 for(i=0; i< the->size; i++) {
305 xsh_msg(" Wavelength %f order %d slit_position %f detector_x %f \
306 detector_y %f", the->list[i]->wavelength, the->list[i]->order,
307 the->list[i]->slit_position, the->list[i]->detector_x,
308 the->list[i]->detector_y);
309 }
310 xsh_msg( "END THE_MAP");
311
312 cleanup:
313 return;
314}
315
316void xsh_the_map_set_arcline( xsh_the_map* list, int idx, float wavelength,
317 int order, int slit_index, float slit_position, double detx, double dety)
318{
319 XSH_ASSURE_NOT_NULL( list);
320
321 XSH_ASSURE_NOT_ILLEGAL( idx >= 0);
322 XSH_ASSURE_NOT_ILLEGAL( list->size > idx);
323
324 list->list[idx]->wavelength = wavelength;
325 list->list[idx]->order = order;
326 list->list[idx]->slit_index = slit_index;
327 list->list[idx]->slit_position = slit_position;
328 list->list[idx]->detector_x = detx;
329 list->list[idx]->detector_y = dety;
330
331 cleanup :
332 return;
333}
334
335/*---------------------------------------------------------------------------*/
345/*---------------------------------------------------------------------------*/
347{
348 xsh_the_map* result = NULL;
349 int i;
350
352
353 /* allocate memory */
354 XSH_CALLOC( result, xsh_the_map, 1);
355
356 result->size = size ;
357
358 XSH_CALLOC( result->list, xsh_the_arcline*, result->size);
359
360 for( i=0; i<size; i++){
361 XSH_CALLOC( result->list[i], xsh_the_arcline,1);
362 }
363
364 cleanup:
365 if ( cpl_error_get_code() != CPL_ERROR_NONE){
366 xsh_the_map_free( &result);
367 }
368 return result;
369}
370/*---------------------------------------------------------------------------*/
371
372/*---------------------------------------------------------------------------*/
384/*---------------------------------------------------------------------------*/
385xsh_the_map* xsh_the_map_load( cpl_frame* frame)
386{
387 cpl_table* table = NULL;
388 const char* tablename = NULL;
389 xsh_the_map* result = NULL;
390 int i, k ;
391 cpl_propertylist * header = NULL ;
392 int full_size ;
393 int real_size = 0 ;
394
395 /* verify input */
396 XSH_ASSURE_NOT_NULL( frame);
397
398 /* get table filename */
399 check( tablename = cpl_frame_get_filename(frame));
400
401 XSH_TABLE_LOAD( table, tablename);
402
403 check_msg( header = cpl_propertylist_load( tablename, 1 ),
404 "Can't load header from %s", tablename ) ;
405
406 xsh_msg_dbg_medium("Loading %s", tablename);
407
408 /* allocate memory */
409 XSH_CALLOC( result, xsh_the_map, 1);
410
411 /*
412 First round to get the real number of good lines (wavelenght != 0.
413 */
414 check (full_size = cpl_table_get_nrow( table));
415
416 xsh_msg_dbg_medium("size %d",full_size);
417
418 for( i = 0; i<full_size ; i++) {
419 float lambda ;
420
422 CPL_TYPE_FLOAT, i, &lambda));
423 if ( lambda > WAVELENGTH_PRECISION ) real_size++ ;
424 }
425 /* Check */
426 xsh_msg_dbg_medium("real size %d",real_size);
427
428 assure(real_size > 0, CPL_ERROR_ILLEGAL_INPUT,
429 "no valid lambda found in the_map");
430
431 /* init structure */
432 result->size = real_size ;
433
434 XSH_CALLOC( result->list, xsh_the_arcline*, result->size);
435
436 for(i=0, k = 0 ; i<full_size; i++) {
437 xsh_the_arcline * arc = NULL;
438 float lambda = 0.0;
439
441 CPL_TYPE_FLOAT, i, &lambda ));
442 /* for incorrect line detection (nan) and 0. wavelength */
443 if ( fabs(lambda) < WAVELENGTH_PRECISION || (lambda != lambda) ) continue;
445 arc->wavelength = lambda ;
447 CPL_TYPE_INT, i, &(arc->order)));
449 CPL_TYPE_FLOAT, i, &(arc->slit_position)));
451 CPL_TYPE_INT, i, &(arc->slit_index)));
453 CPL_TYPE_DOUBLE, i, &(arc->detector_x)));
455 CPL_TYPE_DOUBLE, i, &(arc->detector_y)));
456 result->list[k] = arc;
457 k++ ;
458 }
459 check( result->header = cpl_propertylist_duplicate( header ) ) ;
460 cleanup:
461 XSH_TABLE_FREE( table);
462 xsh_free_propertylist( &header ) ;
463 return result;
464}
465
466
467/*---------------------------------------------------------------------------*/
472/*---------------------------------------------------------------------------*/
474 if( arc && *arc) {
475 cpl_free(*arc);
476 *arc = NULL;
477 }
478
479}
480/*---------------------------------------------------------------------------*/
485/*---------------------------------------------------------------------------*/
487{
488 int i = 0;
489
490 if (list && *list){
491 if ((*list)->list){
492 for(i=0; i < (*list)->size; i++) {
493 xsh_the_arcline* arc = (*list)->list[i];
495 }
496 cpl_free((*list)->list);
497 xsh_free_propertylist( &((*list)->header) ) ;
498 (*list)->list = NULL;
499 }
500 cpl_free(*list);
501 *list = NULL;
502 }
503}
504/*---------------------------------------------------------------------------*/
512/*---------------------------------------------------------------------------*/
513cpl_frame* xsh_the_map_save(xsh_the_map* list,const char* filename)
514{
515 cpl_table* table = NULL;
516 cpl_frame * result = NULL ;
517 int i=0;
518
519 XSH_ASSURE_NOT_NULL( list);
520
521 /* create a table */
522 check(table = cpl_table_new(XSH_THE_MAP_TABLE_NB_COL));
523
524 /* create column names */
525 check(
526 cpl_table_new_column(table,XSH_THE_MAP_TABLE_COLNAME_WAVELENGTH,
527 CPL_TYPE_FLOAT));
528 check(
529 cpl_table_set_column_unit ( table, XSH_THE_MAP_TABLE_COLNAME_WAVELENGTH,
531 check(
532 cpl_table_new_column(table,XSH_THE_MAP_TABLE_COLNAME_ORDER,
533 CPL_TYPE_INT));
534 check(
535 cpl_table_set_column_unit ( table, XSH_THE_MAP_TABLE_COLNAME_ORDER,
537 check(
538 cpl_table_new_column(table,XSH_THE_MAP_TABLE_COLNAME_SLITPOSITION,
539 CPL_TYPE_FLOAT));
540 check(
541 cpl_table_new_column(table,XSH_THE_MAP_TABLE_COLNAME_SLITINDEX,
542 CPL_TYPE_INT));
543 check(
544 cpl_table_set_column_unit ( table, XSH_THE_MAP_TABLE_COLNAME_SLITPOSITION,
546 check(
547 cpl_table_new_column(table,XSH_THE_MAP_TABLE_COLNAME_DETECTORX,
548 CPL_TYPE_DOUBLE));
549 check(
550 cpl_table_set_column_unit ( table, XSH_THE_MAP_TABLE_COLNAME_DETECTORX,
552 check(
553 cpl_table_new_column(table,XSH_THE_MAP_TABLE_COLNAME_DETECTORY,
554 CPL_TYPE_DOUBLE));
555 check(
556 cpl_table_set_column_unit ( table, XSH_THE_MAP_TABLE_COLNAME_DETECTORY,
558
559 check(cpl_table_set_size(table,list->size));
560
561 /* insert data */
562 for(i=0;i<list->size;i++){
563 check(cpl_table_set_float(table,XSH_THE_MAP_TABLE_COLNAME_WAVELENGTH,
564 i,list->list[i]->wavelength));
565 check(cpl_table_set_int(table,XSH_THE_MAP_TABLE_COLNAME_ORDER,
566 i,list->list[i]->order));
567 check(cpl_table_set_float(table,XSH_THE_MAP_TABLE_COLNAME_SLITPOSITION,
568 i,list->list[i]->slit_position));
569 check(cpl_table_set_int(table,XSH_THE_MAP_TABLE_COLNAME_SLITINDEX,
570 i,list->list[i]->slit_index));
571 check(cpl_table_set_double(table,XSH_THE_MAP_TABLE_COLNAME_DETECTORX,
572 i,list->list[i]->detector_x));
573 check(cpl_table_set_double(table,XSH_THE_MAP_TABLE_COLNAME_DETECTORY,
574 i,list->list[i]->detector_y));
575 }
576
577 /* create fits file */
578 check(cpl_table_save(table, NULL,NULL,filename, CPL_IO_DEFAULT));
579
580 /* Create the frame */
581 check(result=xsh_frame_product(filename,
582 "TAG",
583 CPL_FRAME_TYPE_TABLE,
584 CPL_FRAME_GROUP_PRODUCT,
585 CPL_FRAME_LEVEL_TEMPORARY));
586
587 cleanup:
588 XSH_TABLE_FREE( table);
589 return result ;
590}
591
void xsh_the_map_free(xsh_the_map **list)
free memory associated to a the_map
double xsh_the_map_get_detx(xsh_the_map *list, int idx)
get detx of the map list
float xsh_the_map_get_slit_position(xsh_the_map *list, int idx)
get slit position of the map list
static int xsh_the_map_lambda_compare(const void *one, const void *two)
int xsh_the_map_get_slit_index(xsh_the_map *list, int idx)
get slit position of the map list
void xsh_dump_the_map(xsh_the_map *the)
Dump main info about the_map.
double xsh_the_map_get_dety(xsh_the_map *list, int idx)
get dety of the map list
void xsh_the_map_set_arcline(xsh_the_map *list, int idx, float wavelength, int order, int slit_index, float slit_position, double detx, double dety)
void xsh_the_map_lambda_sort(xsh_the_map *list)
sort the_map arcline list by increasing lambda
int xsh_the_map_get_order(xsh_the_map *list, int idx)
get order of the map list
int xsh_the_map_get_size(xsh_the_map *list)
get size of the map list
static int xsh_the_map_lambda_order_slit_compare(const void *one, const void *two)
void xsh_the_arcline_free(xsh_the_arcline **arc)
free memory associated to a the_arcline
void xsh_the_map_lambda_order_slit_sort(xsh_the_map *list)
xsh_the_map * xsh_the_map_create(int size)
Create an empty theoretical map.
float xsh_the_map_get_wavelength(xsh_the_map *list, int idx)
get wavelength of the map list
xsh_the_map * xsh_the_map_load(cpl_frame *frame)
load a theoretical map frame in the_map structure. Suppress spurious entries in the THE MAP (marked w...
cpl_frame * xsh_the_map_save(xsh_the_map *list, const char *filename)
save a the_map to a frame
#define XSH_ASSURE_NOT_ILLEGAL(cond)
Definition: xsh_error.h:107
#define assure(CONDITION, ERROR_CODE,...)
Definition: xsh_error.h:54
#define check(COMMAND)
Definition: xsh_error.h:71
#define check_msg(COMMAND,...)
Definition: xsh_error.h:62
#define XSH_ASSURE_NOT_NULL(pointer)
Definition: xsh_error.h:99
int size
#define xsh_msg_dbg_medium(...)
Definition: xsh_msg.h:44
#define xsh_msg(...)
Print a message on info level.
Definition: xsh_msg.h:121
void xsh_free_propertylist(cpl_propertylist **p)
Deallocate a property list and set the pointer to NULL.
Definition: xsh_utils.c:2179
cpl_error_code xsh_get_table_value(const cpl_table *table, const char *colname, cpl_type coltype, int i, void *result)
Read a table value from a fits table.
cpl_propertylist * header
xsh_the_arcline ** list
#define XSH_THE_MAP_TABLE_NB_COL
#define XSH_THE_MAP_TABLE_UNIT_SLITPOSITION
#define XSH_THE_MAP_TABLE_COLNAME_SLITINDEX
#define XSH_THE_MAP_TABLE_UNIT_WAVELENGTH
#define XSH_THE_MAP_TABLE_COLNAME_WAVELENGTH
#define XSH_THE_MAP_TABLE_UNIT_DETECTORX
#define XSH_THE_MAP_TABLE_UNIT_DETECTORY
#define XSH_THE_MAP_TABLE_COLNAME_DETECTORX
#define XSH_THE_MAP_TABLE_COLNAME_SLITPOSITION
#define XSH_THE_MAP_TABLE_COLNAME_ORDER
#define XSH_THE_MAP_TABLE_UNIT_ORDER
#define XSH_THE_MAP_TABLE_COLNAME_DETECTORY
int order
Definition: xsh_detmon_lg.c:80
cpl_frame * xsh_frame_product(const char *fname, const char *tag, cpl_frame_type type, cpl_frame_group group, cpl_frame_level level)
Creates a frame with given characteristics.
Definition: xsh_dfs.c:930
#define WAVELENGTH_PRECISION
Definition: xsh_drl.h:190
#define XSH_CALLOC(POINTER, TYPE, SIZE)
Definition: xsh_utils.h:56
#define XSH_TABLE_LOAD(TABLE, NAME)
#define XSH_TABLE_FREE(TABLE)