X-shooter Pipeline Reference Manual 3.8.15
xsh_data_order_resid_tab.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:56:39 $
23 * $Revision: 1.13 $
24 */
25
26#ifdef HAVE_CONFIG_H
27#include <config.h>
28#endif
29
30/*---------------------------------------------------------------------------*/
35/*---------------------------------------------------------------------------*/
36#define XSH_RESIDX_THRESHOLD 0.1
40/*-----------------------------------------------------------------------------
41 Includes
42 ----------------------------------------------------------------------------*/
43#include <xsh_data_instrument.h>
45#include <xsh_utils.h>
46#include <xsh_error.h>
47#include <xsh_msg.h>
48#include <xsh_pfits.h>
49#include <cpl.h>
50#include <xsh_drl.h>
51#include <xsh_utils_table.h>
52
53/*----------------------------------------------------------------------------
54 Function implementation
55 ----------------------------------------------------------------------------*/
56
57
77/*---------------------------------------------------------------------------*/
79 double * posx, double * posy,
80 double * resx, double* polx )
81{
82 xsh_resid_order_tab* result = NULL;
83 int i;
84
85 /* verify input */
87 XSH_ASSURE_NOT_NULL( orders);
92
94
95 XSH_CALLOC( result->order, int, size);
96 XSH_CALLOC( result->pos_x, double, size);
97 XSH_CALLOC( result->pos_y, double, size);
98 XSH_CALLOC( result->res_x, double, size);
99 XSH_CALLOC( result->pol_x, double, size);
100
101
102 check (result->header = cpl_propertylist_new());
103
104 result->size = size;
105 xsh_msg_dbg_low( " xsh_resid_order_create( %d )", result->size ) ;
106
107 for( i=0; i<size; i++) {
108 result->order[i] = orders[i];
109 result->pos_x[i] = posx[i];
110 result->pos_y[i] = posy[i];
111 result->res_x[i] = resx[i];
112 result->pol_x[i] = polx[i];
113 }
114
115 cleanup:
116 if ( cpl_error_get_code() != CPL_ERROR_NONE){
117 xsh_resid_order_free( &result);
118 }
119 return result;
120}
121
122/*---------------------------------------------------------------------------*/
132/*---------------------------------------------------------------------------*/
133
134xsh_resid_order_tab * xsh_resid_order_load( cpl_frame * resid_tab_frame)
135{
136 xsh_resid_order_tab * result = NULL;
137 cpl_table * table = NULL;
138 const char * tablename = NULL;
139 int i = 0;
140 int size = 0;
141
142 /* verify input */
143 XSH_ASSURE_NOT_NULL( resid_tab_frame);
144
145 /* get table */
146 check( tablename = cpl_frame_get_filename( resid_tab_frame));
147
148 XSH_TABLE_LOAD( table, tablename);
149
150 check( size = cpl_table_get_nrow(table));
151
152 XSH_CALLOC( result, xsh_resid_order_tab, 1);
153 check (result->header = cpl_propertylist_load( tablename, 0));
154 result->size = size;
155
156 XSH_CALLOC( result->order, int, size);
157 XSH_CALLOC( result->pos_x, double, size);
158 XSH_CALLOC( result->pos_y, double, size);
159 XSH_CALLOC( result->res_x, double, size);
160 XSH_CALLOC( result->pol_x, double, size);
161
162 for(i=0; i<size;i++){
163
164 check( xsh_get_table_value( table,
166 CPL_TYPE_INT, i, &result->order[i]));
167
170 CPL_TYPE_DOUBLE, i, &result->pos_x[i]));
171
174 CPL_TYPE_DOUBLE, i, &result->pos_y[i]));
175
178 CPL_TYPE_DOUBLE, i, &result->res_x[i]));
179
182 CPL_TYPE_DOUBLE, i, &result->pol_x[i]));
183 }
184
185 cleanup:
186 if (cpl_error_get_code () != CPL_ERROR_NONE) {
187 xsh_error_msg("can't load frame %s",
188 cpl_frame_get_filename(resid_tab_frame));
189 xsh_resid_order_free(&result);
190 }
191 XSH_TABLE_FREE( table);
192 return result;
193}
194
195/*---------------------------------------------------------------------------*/
203/*---------------------------------------------------------------------------*/
205 if ( resid && *resid) {
206 XSH_FREE( (*resid)->order);
207 XSH_FREE( (*resid)->pos_x);
208 XSH_FREE( (*resid)->pos_y);
209 XSH_FREE( (*resid)->res_x);
210 XSH_FREE( (*resid)->pol_x);
211
212 xsh_free_propertylist( &(*resid)->header);
213
214 cpl_free(*resid);
215 *resid = NULL;
216 }
217
218}
219
220
221/*---------------------------------------------------------------------------*/
239/*---------------------------------------------------------------------------*/
241 const char* filename,
243 ORDERPOS_QC_PARAM* ord_qc_param,
244 const char* tag)
245{
246 cpl_frame *result = NULL ;
247 cpl_table *table = NULL;
248 cpl_propertylist *header = NULL;
249 int i = 0;
250 //int ord_min=0;
251 //int ord_max=0;
252 cpl_table* sel=NULL;
253
254 XSH_ASSURE_NOT_NULL( resid);
255 XSH_ASSURE_NOT_NULL( filename);
257
258 xsh_msg_dbg_high( " xsh_resid_order_save, size = %d", resid->size ) ;
259
260 /* create a table */
261 check( table = cpl_table_new( XSH_RESID_ORDER_TABLE_NB_COL));
262 header = resid->header;
263 /* copy some QC parameters */
264
265 // To Be Done !
266
267 check( xsh_pfits_set_qc( header, &resid->residmin,
269 instrument ) ) ;
270 check( xsh_pfits_set_qc( header, &resid->residmax,
272 instrument ) ) ;
273 check( xsh_pfits_set_qc( header, &resid->residavg,
275 instrument ) ) ;
276 check( xsh_pfits_set_qc( header, &resid->residrms,
278 instrument ) ) ;
279
280 /* create columns */
283
285 XSH_RESID_ORDER_TABLE_UNIT_POSX, CPL_TYPE_DOUBLE);
286
288 XSH_RESID_ORDER_TABLE_UNIT_POSY, CPL_TYPE_DOUBLE);
289
291 XSH_RESID_ORDER_TABLE_UNIT_RESX, CPL_TYPE_DOUBLE);
292
294 XSH_RESID_ORDER_TABLE_UNIT_POLX, CPL_TYPE_DOUBLE);
295
296 check(cpl_table_set_size( table, resid->size));
297
298 /* insert data */
299 for (i=0; i<resid->size; i++) {
300 check(cpl_table_set_int(table, XSH_RESID_ORDER_TABLE_COLNAME_ORDER,
301 i, resid->order[i]));
302 check(cpl_table_set_double(table,XSH_RESID_ORDER_TABLE_COLNAME_POSX,
303 i, resid->pos_x[i]));
304 check(cpl_table_set_double(table,XSH_RESID_ORDER_TABLE_COLNAME_POSY,
305 i, resid->pos_y[i]));
306 check(cpl_table_set_double(table,XSH_RESID_ORDER_TABLE_COLNAME_RESX,
307 i, resid->res_x[i]));
308 check(cpl_table_set_double(table,XSH_RESID_ORDER_TABLE_COLNAME_POLX,
309 i, resid->pol_x[i]));
310 }
311
312
313 /*
314 Setup more QC parameters
315 */
316 //check(ord_max=cpl_table_get_column_max(table,XSH_RESID_ORDER_TABLE_COLNAME_ORDER));
317 //check(ord_min=cpl_table_get_column_min(table,XSH_RESID_ORDER_TABLE_COLNAME_ORDER));
318
319 ord_qc_param->nposall = resid->size ;
320
321 check(ord_qc_param->npossel=cpl_table_and_selected_double(table,
323
324 check(ord_qc_param->npossel=cpl_table_and_selected_double(table,
326
327
328 check(ord_qc_param->npossel=cpl_table_and_selected_double(table,
329 XSH_RESID_ORDER_TABLE_COLNAME_RESX,CPL_LESS_THAN,2*resid->residrms));
330 check(ord_qc_param->npossel=cpl_table_and_selected_double(table,
331 XSH_RESID_ORDER_TABLE_COLNAME_RESX,CPL_GREATER_THAN,-2*resid->residrms));
332 check(sel=cpl_table_extract_selected(table));
333
334
335 check(resid->residavg_sel =
336 cpl_table_get_column_mean(sel,XSH_RESID_ORDER_TABLE_COLNAME_RESX));
337
338 check(resid->residmax_sel =
339 cpl_table_get_column_max(sel,XSH_RESID_ORDER_TABLE_COLNAME_RESX));
340
341 check(resid->residmin_sel =
342 cpl_table_get_column_min(sel,XSH_RESID_ORDER_TABLE_COLNAME_RESX));
343
344 check(resid->residrms_sel =
345 cpl_table_get_column_stdev(sel,XSH_RESID_ORDER_TABLE_COLNAME_RESX));
346
347 xsh_msg_dbg_high("after selection: avg=%g med=%g rms=%g min=%g max=%g",
348 resid->residavg_sel,
349 cpl_table_get_column_median(sel,XSH_RESID_ORDER_TABLE_COLNAME_RESX),
350 resid->residrms_sel,
351 resid->residmin_sel,
352 resid->residmax_sel);
353
354
355 check( xsh_pfits_set_qc( header, &resid->residmin_sel,
357 instrument ) ) ;
358 check( xsh_pfits_set_qc( header, &resid->residmax_sel,
360 instrument ) ) ;
361 check( xsh_pfits_set_qc( header, &resid->residavg_sel,
363 instrument ) ) ;
364 check( xsh_pfits_set_qc( header, &resid->residrms_sel,
366 instrument ) ) ;
367
368
369 /* create fits file */
370 check( xsh_pfits_set_pcatg(header, tag));
371 check( cpl_table_save(table, header,NULL,filename, CPL_IO_DEFAULT));
372
373
374 /* Create the frame */
375 check(result=xsh_frame_product(filename,
376 tag,
377 CPL_FRAME_TYPE_TABLE,
378 CPL_FRAME_GROUP_PRODUCT,
379 CPL_FRAME_LEVEL_TEMPORARY));
380 //check (xsh_add_temporary_file( filename));
381
382 cleanup:
383
384 if (cpl_error_get_code() != CPL_ERROR_NONE){
385 xsh_free_frame(&result);
386 }
387 XSH_TABLE_FREE( table);
388 XSH_TABLE_FREE( sel);
389 return result ;
390}
391
392
393
394
395
396
397
398
399
400/*---------------------------------------------------------------------------*/
418/*---------------------------------------------------------------------------*/
419cpl_table *
422 ORDERPOS_QC_PARAM* ord_qc_param)
423{
424 cpl_table *table = NULL;
425 int i = 0;
426 //int ord_min=0;
427 //int ord_max=0;
428 cpl_table* sel=NULL;
429 int* porder=NULL;
430 double* pposx=NULL;
431 double* pposy=NULL;
432 double* presx=NULL;
433 double* ppolx=NULL;
434
435 XSH_ASSURE_NOT_NULL( resid);
437
438 xsh_msg( " xsh_resid_order_save, size = %d", resid->size ) ;
439
440 /* create a table */
441 check( table = cpl_table_new( XSH_RESID_ORDER_TABLE_NB_COL));
442
443 /* create columns */
446
448 XSH_RESID_ORDER_TABLE_UNIT_POSX, CPL_TYPE_DOUBLE);
449
451 XSH_RESID_ORDER_TABLE_UNIT_POSY, CPL_TYPE_DOUBLE);
452
454 XSH_RESID_ORDER_TABLE_UNIT_RESX, CPL_TYPE_DOUBLE);
455
457 XSH_RESID_ORDER_TABLE_UNIT_POLX, CPL_TYPE_DOUBLE);
458
459 check(cpl_table_set_size( table, resid->size));
460
461
462 // insert data:
463 // using pointers is faster, but remember to initialize columns!
464 cpl_table_fill_column_window(table,XSH_RESID_ORDER_TABLE_COLNAME_ORDER,
465 0,resid->size,-1);
466
467 cpl_table_fill_column_window(table,XSH_RESID_ORDER_TABLE_COLNAME_POSX,
468 0,resid->size,-1);
469
470 cpl_table_fill_column_window(table,XSH_RESID_ORDER_TABLE_COLNAME_POSY,
471 0,resid->size,-1);
472
473 cpl_table_fill_column_window(table,XSH_RESID_ORDER_TABLE_COLNAME_RESX,
474 0,resid->size,-1);
475
476 cpl_table_fill_column_window(table,XSH_RESID_ORDER_TABLE_COLNAME_POLX,
477 0,resid->size,-1);
478 porder=cpl_table_get_data_int(table,XSH_RESID_ORDER_TABLE_COLNAME_ORDER);
479 pposx=cpl_table_get_data_double(table,XSH_RESID_ORDER_TABLE_COLNAME_POSX);
480 pposy=cpl_table_get_data_double(table,XSH_RESID_ORDER_TABLE_COLNAME_POSY);
481 presx=cpl_table_get_data_double(table,XSH_RESID_ORDER_TABLE_COLNAME_RESX);
482 ppolx=cpl_table_get_data_double(table,XSH_RESID_ORDER_TABLE_COLNAME_POLX);
483
484
485
486 for (i=0; i<resid->size; i++) {
487 check(porder[i]=resid->order[i]);
488 check(pposx[i]=resid->pos_x[i]);
489 check(pposy[i]=resid->pos_y[i]);
490 check(presx[i]=resid->res_x[i]);
491 check(ppolx[i]=resid->pol_x[i]);
492 }
493
494
495 /*
496 Setup more QC parameters
497 */
498 //check(ord_max=cpl_table_get_column_max(table,XSH_RESID_ORDER_TABLE_COLNAME_ORDER));
499 //check(ord_min=cpl_table_get_column_min(table,XSH_RESID_ORDER_TABLE_COLNAME_ORDER));
500
501 ord_qc_param->nposall = resid->size ;
502
503 check(ord_qc_param->npossel=cpl_table_and_selected_double(table,
505
506
507 check(ord_qc_param->npossel=cpl_table_and_selected_double(table,
508 XSH_RESID_ORDER_TABLE_COLNAME_RESX,CPL_LESS_THAN,2*resid->residrms));
509 check(sel=cpl_table_extract_selected(table));
510
511
512 check(resid->residavg_sel =
513 cpl_table_get_column_mean(sel,XSH_RESID_ORDER_TABLE_COLNAME_RESX));
514
515 check(resid->residmax_sel =
516 cpl_table_get_column_max(sel,XSH_RESID_ORDER_TABLE_COLNAME_RESX));
517
518 check(resid->residmin_sel =
519 cpl_table_get_column_min(sel,XSH_RESID_ORDER_TABLE_COLNAME_RESX));
520
521 check(resid->residrms_sel =
522 cpl_table_get_column_stdev(sel,XSH_RESID_ORDER_TABLE_COLNAME_RESX));
523
524 xsh_msg("after selection: avg=%g med=%g rms=%g min=%g max=%g",
525 resid->residavg_sel,
526 cpl_table_get_column_median(sel,XSH_RESID_ORDER_TABLE_COLNAME_RESX),
527 resid->residrms_sel,
528 resid->residmin_sel,
529 resid->residmax_sel);
530
531
532 cleanup:
533
534 XSH_TABLE_FREE( sel);
535 return table ;
536}
537
538
539
540
541
542
543
544
static xsh_instrument * instrument
xsh_resid_order_tab * xsh_resid_order_create(int size, int *orders, double *posx, double *posy, double *resx, double *polx)
Create a residual tab structure.
void xsh_resid_order_free(xsh_resid_order_tab **resid)
Free memory associated to a resid_tab.
cpl_table * xsh_resid_order_2tab(xsh_resid_order_tab *resid, xsh_instrument *instrument, ORDERPOS_QC_PARAM *ord_qc_param)
convert a residual tab structure to a cpl_table
xsh_resid_order_tab * xsh_resid_order_load(cpl_frame *resid_tab_frame)
Load a residual tab from a frame.
cpl_frame * xsh_resid_order_save(xsh_resid_order_tab *resid, const char *filename, xsh_instrument *instrument, ORDERPOS_QC_PARAM *ord_qc_param, const char *tag)
Save a residual tab to a frame.
#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
#define xsh_msg_dbg_low(...)
Definition: xsh_msg.h:48
#define xsh_msg_dbg_high(...)
Definition: xsh_msg.h:40
void xsh_pfits_set_pcatg(cpl_propertylist *plist, const char *value)
Write the PCATG value.
Definition: xsh_pfits.c:1008
void xsh_pfits_set_qc(cpl_propertylist *plist, void *value, const char *kw, xsh_instrument *instrument)
void xsh_free_frame(cpl_frame **f)
Deallocate a frame and set the pointer to NULL.
Definition: xsh_utils.c:2269
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.
#define XSH_RESIDX_THRESHOLD
#define XSH_RESID_ORDER_TABLE_UNIT_ORDER
#define XSH_RESID_ORDER_TABLE_UNIT_POSX
#define XSH_RESID_ORDER_TABLE_COLNAME_RESX
#define XSH_RESID_ORDER_TABLE_COLNAME_POSY
#define XSH_RESID_ORDER_TABLE_COLNAME_POLX
#define XSH_RESID_ORDER_TABLE_UNIT_RESX
#define XSH_RESID_ORDER_TABLE_UNIT_POLX
#define XSH_RESID_ORDER_TABLE_UNIT_POSY
#define XSH_RESID_ORDER_TABLE_COLNAME_ORDER
#define XSH_RESID_ORDER_TABLE_NB_COL
#define XSH_RESID_ORDER_TABLE_COLNAME_POSX
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 QC_ORD_ORDERPOS_RESIDMAX
Definition: xsh_pfits_qc.h:175
#define QC_ORD_ORDERPOS_RESIDAVG
Definition: xsh_pfits_qc.h:176
#define QC_ORD_ORDERPOS_RESIDMIN_SEL
Definition: xsh_pfits_qc.h:180
#define QC_ORD_ORDERPOS_RESIDMIN
Definition: xsh_pfits_qc.h:174
#define QC_ORD_ORDERPOS_RESIDAVG_SEL
Definition: xsh_pfits_qc.h:182
#define QC_ORD_ORDERPOS_RESIDRMS_SEL
Definition: xsh_pfits_qc.h:183
#define QC_ORD_ORDERPOS_RESIDRMS
Definition: xsh_pfits_qc.h:177
#define QC_ORD_ORDERPOS_RESIDMAX_SEL
Definition: xsh_pfits_qc.h:181
#define XSH_TABLE_NEW_COL(TABLE, NAME, UNIT, TYPE)
Definition: xsh_utils.h:113
#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)