X-shooter Pipeline Reference Manual 3.8.15
xsh_data_arclist.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-06-21 07:40:22 $
23 * $Revision: 1.25 $
24 * $Name: not supported by cvs2svn $
25 */
26
27#ifdef HAVE_CONFIG_H
28#include <config.h>
29#endif
30
31/*---------------------------------------------------------------------------*/
39/*---------------------------------------------------------------------------*/
40
44/*-----------------------------------------------------------------------------
45 Includes
46 ----------------------------------------------------------------------------*/
47#include <xsh_data_arclist.h>
48#include <xsh_utils.h>
49#include <xsh_error.h>
50#include <xsh_msg.h>
51#include <xsh_pfits.h>
52#include <cpl.h>
53#include <xsh_utils_table.h>
54#include <math.h>
55#include <xsh_drl.h>
56/*----------------------------------------------------------------------------
57 Function implementation
58 ----------------------------------------------------------------------------*/
59static int xsh_arclist_lambda_compare(const void* one, const void* two){
60 xsh_arcline** a = NULL;
61 xsh_arcline** b = NULL;
62 float la, lb;
63
64 a = (xsh_arcline**) one;
65 b = (xsh_arcline**) two;
66
67 la = (*a)->wavelength;
68 lb = (*b)->wavelength;
69
70 if (la <= lb)
71 return -1;
72 else
73 return 1;
74
75
76}
77
78
79/*---------------------------------------------------------------------------*/
86/*---------------------------------------------------------------------------*/
88 qsort(list->list,list->size,sizeof(xsh_arcline*),
90}
91
92/*---------------------------------------------------------------------------*/
99/*---------------------------------------------------------------------------*/
101 int i=0;
102
104 i = list->size;
105
106 cleanup:
107 return i;
108}
109
110
111/*---------------------------------------------------------------------------*/
118/*---------------------------------------------------------------------------*/
120 int i=0;
121
123 i = list->nbrejected;
124
125 cleanup:
126 return i;
127}
128
129/*---------------------------------------------------------------------------*/
137/*---------------------------------------------------------------------------*/
139{
140 float f = 0.0;
141
143 XSH_ASSURE_NOT_ILLEGAL(idx >= 0 && idx < list->size);
144 f = list->list[idx]->wavelength;
145
146 cleanup:
147 return f;
148}
149
150/*---------------------------------------------------------------------------*/
157/*---------------------------------------------------------------------------*/
158void xsh_arclist_reject(xsh_arclist* list, int idx)
159{
161 XSH_ASSURE_NOT_ILLEGAL(idx >= 0 && idx < list->size);
162
163 if (list->rejected[idx] == 0){
164 list->rejected[idx] = 1;
165 list->nbrejected++;
166 }
167
168 cleanup:
169 return;
170
171}
172
173/*---------------------------------------------------------------------------*/
180/*---------------------------------------------------------------------------*/
182{
184 XSH_ASSURE_NOT_ILLEGAL(idx >= 0 && idx < list->size);
185
186 if (list->rejected[idx] == 1){
187 list->rejected[idx] = 0;
188 list->nbrejected--;
189 }
190
191 cleanup:
192 return;
193
194}
195/*---------------------------------------------------------------------------*/
204/*---------------------------------------------------------------------------*/
206{
207 int res = 0;
208
210 XSH_ASSURE_NOT_ILLEGAL(idx >= 0 && idx < list->size);
211
212 res = list->rejected[idx];
213 cleanup:
214 return res;
215}
216
217/*---------------------------------------------------------------------------*/
223/*---------------------------------------------------------------------------*/
224cpl_propertylist* xsh_arclist_get_header(xsh_arclist* list)
225{
226 cpl_propertylist * res = NULL;
227
229 res = list->header;
230 cleanup:
231 return res;
232}
233
234/*---------------------------------------------------------------------------*/
240/*---------------------------------------------------------------------------*/
242{
243 int i = 0;
244
246
247 xsh_msg( "ARCLINE_LIST Dump %d lines",list->size);
248
249 for(i=0; i< list->size; i++) {
250 const char* name = list->list[i]->name;
251 const char* comment = list->list[i]->comment;
252 if (name == NULL) name ="";
253 if (comment == NULL) comment ="";
254 xsh_msg(" Wavelength %f name %s flux %d comment %s",
255 list->list[i]->wavelength, name,
256 list->list[i]->flux, comment);
257 }
258 xsh_msg( "END ARCLINE_LIST");
259
260 cleanup:
261 return;
262}
263
264/*---------------------------------------------------------------------------*/
271/*---------------------------------------------------------------------------*/
272xsh_arclist* xsh_arclist_load(cpl_frame* frame){
273 cpl_table* table = NULL;
274 const char* tablename = NULL;
275 xsh_arclist* result = NULL;
276 int i = 0;
277
278 /* verify input */
279 XSH_ASSURE_NOT_NULL( frame);
280
281 /* get table filename */
282 check(tablename = cpl_frame_get_filename(frame));
283
284 XSH_TABLE_LOAD( table, tablename);
285
286 /* allocate memory */
287 XSH_CALLOC(result,xsh_arclist,1);
288
289 /* init structure */
290 check(result->size = cpl_table_get_nrow(table));
291 XSH_CALLOC(result->list, xsh_arcline*, result->size);
292 XSH_CALLOC(result->rejected, int, result->size);
293 result->nbrejected = 0;
294
295 check(result->header = cpl_propertylist_load(tablename, 0));
296
297 /* load table data */
298 for(i=0;i<result->size;i++){
299 const char* name ="";
300 const char* comment ="";
301 xsh_arcline* arc = (xsh_arcline*)cpl_malloc(sizeof(xsh_arcline));
302
304 CPL_TYPE_FLOAT, i, &(arc->wavelength)));
306 CPL_TYPE_STRING, i, &name));
308 CPL_TYPE_INT, i, &(arc->flux)));
310 CPL_TYPE_STRING, i, &comment));
311 if (name != NULL) {
312 arc->name = xsh_stringdup(name);
313 }
314 else {
315 arc->name = NULL;
316 }
317 if (comment != NULL) {
318 arc->comment = xsh_stringdup(comment);
319 }
320 else {
321 arc->comment = NULL;
322 }
323 result->list[i] = arc;
324 }
325
326 cleanup:
327 if (cpl_error_get_code () != CPL_ERROR_NONE) {
328 xsh_error_msg("can't load frame %s",cpl_frame_get_filename(frame));
329 xsh_arclist_free(&result);
330 }
331 XSH_TABLE_FREE( table);
332 return result;
333}
334
335/*---------------------------------------------------------------------------*/
340/*---------------------------------------------------------------------------*/
342{
343 if (arc && (*arc)){
344 if ((*arc)->name != NULL) {
345 cpl_free((*arc)->name);
346 }
347 if ((*arc)->comment != NULL) {
348 cpl_free((*arc)->comment);
349 }
350 cpl_free(*arc);
351 *arc = NULL;
352 }
353}
354
355/*---------------------------------------------------------------------------*/
360/*---------------------------------------------------------------------------*/
362{
363 int i;
364
365 if (list && *list) {
366 if ((*list)->list) {
367 for (i=0; i< (*list)->size; i++) {
368 xsh_arcline* arc = (*list)->list[i];
369 xsh_arcline_free(&arc);
370 }
371 cpl_free((*list)->list);
372 xsh_free_propertylist(&((*list)->header));
373 }
374 XSH_FREE((*list)->rejected);
375 cpl_free(*list);
376 *list = NULL;
377 }
378}
379
380/*---------------------------------------------------------------------------*/
392/*---------------------------------------------------------------------------*/
393void xsh_arclist_clean_from_list( xsh_arclist* list, double* lambda, int size)
394{
395 int i=0, j=0;
396 int nrejected=0;
397 XSH_ASSURE_NOT_NULL( list);
398 XSH_ASSURE_NOT_NULL( lambda);
399
400 for( i=0; i< list->size; i++){
401 int find = 0;
402 double arc_lambda = 0.0;
403
404 check( arc_lambda = xsh_arclist_get_wavelength( list, i));
405
406 for ( j = 0 ; j<size ; j++ ) {
407 if ( fabs( arc_lambda-lambda[j]) <= WAVELENGTH_PRECISION ) {
408 find = 1 ;
409 break ;
410 }
411 }
412 if ( find == 0 ) {
413 check( xsh_arclist_reject( list, i));
414 nrejected++;
415 }
416
417
418 }
419 XSH_REGDEBUG("cleanarclines list size %d rejected %d (%d)", list->size,
420 list->nbrejected, size );
421
422 check( xsh_arclist_clean( list));
423 cleanup:
424 return;
425}
426
427
428void xsh_arclist_clean_from_list_not_flagged( xsh_arclist* list, double* lambda, int* flag, int size)
429{
430 int i=0, j=0;
431 int nrejected=0;
432 XSH_ASSURE_NOT_NULL( list);
433 XSH_ASSURE_NOT_NULL( lambda);
434
435 for( i=0; i< list->size; i++){
436 int find = 0;
437 double arc_lambda = 0.0;
438
439 check( arc_lambda = xsh_arclist_get_wavelength( list, i));
440 for ( j = 0 ; j<size ; j++ ) {
441 if ( fabs( arc_lambda-lambda[j]) <= WAVELENGTH_PRECISION &&
442 (flag[j] == 0) ) {
443 find = 1 ;
444 break ;
445 }
446 }
447 if ( find == 0 ) {
448 check( xsh_arclist_reject( list, i));
449 //xsh_msg("reject lambda[%d]=%g",i,arc_lambda);
450 nrejected++;
451 }
452
453 }
454
455 XSH_REGDEBUG("cleanarclines list size %d rejected %d (%d)", list->size,
456 list->nbrejected, size );
457 check( xsh_arclist_clean( list));
458
459
460 cleanup:
461 return;
462}
463
464
466{
467 int i, j;
468
470
471 j = 0;
472 for(i=0;i<list->size;i++)
473 {
474 if(xsh_arclist_is_rejected(list,i)){
475 xsh_arcline_free(&list->list[i]);
476 }
477 else{
478 list->list[j] = list->list[i];
479 list->rejected[j] = 0;
480 j++;
481 }
482 }
483 list->size = j;
484 list->nbrejected = 0;
485
486 cleanup:
487 return;
488}
489/*---------------------------------------------------------------------------*/
498/*---------------------------------------------------------------------------*/
499cpl_frame*
500xsh_arclist_save(xsh_arclist* list,const char* filename,const char* tag)
501{
502 cpl_table* table = NULL;
503 cpl_frame * result = NULL ;
504 int i=0;
505
507 XSH_ASSURE_NOT_NULL(filename);
508
509 /* create a table */
510 check(table = cpl_table_new(XSH_ARCLIST_TABLE_NB_COL));
511
512 /* create column names */
513 check(
514 cpl_table_new_column(table,XSH_ARCLIST_TABLE_COLNAME_WAVELENGTH,
515 CPL_TYPE_FLOAT));
516 check(
517 cpl_table_set_column_unit ( table, XSH_ARCLIST_TABLE_COLNAME_WAVELENGTH,
519 check(
520 cpl_table_new_column(table,XSH_ARCLIST_TABLE_COLNAME_NAME,
521 CPL_TYPE_STRING));
522 check(
523 cpl_table_set_column_unit ( table, XSH_ARCLIST_TABLE_COLNAME_NAME,
525
526 check(
527 cpl_table_new_column(table,XSH_ARCLIST_TABLE_COLNAME_FLUX,
528 CPL_TYPE_INT));
529 check(
530 cpl_table_set_column_unit ( table, XSH_ARCLIST_TABLE_COLNAME_FLUX,
532
533 check(
534 cpl_table_new_column(table,XSH_ARCLIST_TABLE_COLNAME_COMMENT,
535 CPL_TYPE_STRING));
536 check(
537 cpl_table_set_column_unit ( table, XSH_ARCLIST_TABLE_COLNAME_COMMENT,
539
540 check(cpl_table_set_size(table,list->size));
541
542 /* insert data */
543 for(i=0;i<list->size;i++){
544 check(cpl_table_set_float(table,XSH_ARCLIST_TABLE_COLNAME_WAVELENGTH,
545 i,list->list[i]->wavelength));
546 check(cpl_table_set_string(table,XSH_ARCLIST_TABLE_COLNAME_NAME,
547 i,list->list[i]->name));
548 check(cpl_table_set_int(table,XSH_ARCLIST_TABLE_COLNAME_FLUX,
549 i,list->list[i]->flux));
550 check(cpl_table_set_string(table,XSH_ARCLIST_TABLE_COLNAME_COMMENT,
551 i,list->list[i]->comment));
552 }
553
554 /* create fits file */
555 check(cpl_table_save(table, list->header, NULL, filename, CPL_IO_DEFAULT));
556 //check( xsh_add_temporary_file( filename));
557 /* Create the frame */
558 check(result=xsh_frame_product(filename,tag,
559 CPL_FRAME_TYPE_TABLE,
560 CPL_FRAME_GROUP_PRODUCT,
561 CPL_FRAME_LEVEL_TEMPORARY));
562
563 cleanup:
564 XSH_TABLE_FREE( table);
565 return result ;
566}
567
void xsh_arclist_lambda_sort(xsh_arclist *list)
sort arcline list by increasing lambda
void xsh_dump_arclist(xsh_arclist *list)
Dump main info about an arcline_list.
void xsh_arclist_clean_from_list_not_flagged(xsh_arclist *list, double *lambda, int *flag, int size)
static int xsh_arclist_lambda_compare(const void *one, const void *two)
float xsh_arclist_get_wavelength(xsh_arclist *list, int idx)
get wavelength of a line in the arcline list
void xsh_arclist_clean(xsh_arclist *list)
void xsh_arclist_clean_from_list(xsh_arclist *list, double *lambda, int size)
Clean an arclist according to a list of valid lambda.
int xsh_arclist_get_nbrejected(xsh_arclist *list)
get nb lines rejected in arcline list
void xsh_arclist_free(xsh_arclist **list)
free memory associated to a arclist
void xsh_arclist_restore(xsh_arclist *list, int idx)
restore a line from the list
int xsh_arclist_get_size(xsh_arclist *list)
get size of arcline list
void xsh_arclist_reject(xsh_arclist *list, int idx)
reject a line from the list
xsh_arclist * xsh_arclist_load(cpl_frame *frame)
load an arcline list frame in arclist structure
void xsh_arcline_free(xsh_arcline **arc)
free memory associated to a arcline
int xsh_arclist_is_rejected(xsh_arclist *list, int idx)
give if a line is rejected
cpl_propertylist * xsh_arclist_get_header(xsh_arclist *list)
get header of the table
cpl_frame * xsh_arclist_save(xsh_arclist *list, const char *filename, const char *tag)
save a arclist to a frame
#define XSH_REGDEBUG(...)
Definition: xsh_error.h:132
#define XSH_ASSURE_NOT_ILLEGAL(cond)
Definition: xsh_error.h:107
#define check(COMMAND)
Definition: xsh_error.h:71
#define xsh_error_msg(...)
Definition: xsh_error.h:94
#define XSH_ASSURE_NOT_NULL(pointer)
Definition: xsh_error.h:99
int size
#define xsh_msg(...)
Print a message on info level.
Definition: xsh_msg.h:121
char * xsh_stringdup(const char *s)
String duplication.
Definition: xsh_utils.c:1658
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_arcline ** list
#define XSH_ARCLIST_TABLE_COLNAME_WAVELENGTH
#define XSH_ARCLIST_TABLE_UNIT_WAVELENGTH
#define XSH_ARCLIST_TABLE_COLNAME_NAME
#define XSH_ARCLIST_TABLE_UNIT_NAME
#define XSH_ARCLIST_TABLE_COLNAME_FLUX
#define XSH_ARCLIST_TABLE_UNIT_FLUX
#define XSH_ARCLIST_TABLE_NB_COL
#define XSH_ARCLIST_TABLE_COLNAME_COMMENT
#define XSH_ARCLIST_TABLE_UNIT_COMMENT
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_FREE(POINTER)
Definition: xsh_utils.h:92
#define XSH_CALLOC(POINTER, TYPE, SIZE)
Definition: xsh_utils.h:56
#define XSH_TABLE_LOAD(TABLE, NAME)
#define XSH_TABLE_FREE(TABLE)