X-shooter Pipeline Reference Manual 3.8.15
xsh_create_order_table.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-08 18:43:31 $
23 * $Revision: 1.22 $
24*/
25
26#ifdef HAVE_CONFIG_H
27#include <config.h>
28#endif
29
30/*---------------------------------------------------------------------------*/
39#include <xsh_drl.h>
40#include <xsh_pfits.h>
41#include <xsh_utils.h>
42#include <xsh_data_order.h>
43#include <xsh_data_arclist.h>
44#include <xsh_error.h>
45#include <xsh_utils.h>
46#include <xsh_msg.h>
47#include <xsh_data_pre.h>
48#include <xsh_data_order.h>
49#include <xsh_data_resid_tab.h>
50#include <cpl.h>
51
52
53static int
54data_order_compare (const void *one, const void *two)
55{
56 xsh_sort_data* a = NULL;
57 xsh_sort_data* b = NULL;
58 double* da = NULL, *db = NULL;
59
60 a = (xsh_sort_data *) one;
61 b = (xsh_sort_data *) two;
62
63 da = (double*)(a->data);
64 db = (double*)(b->data);
65
66 if ((*da) <= (*db))
67 return -1;
68 else
69 return 1;
70}
71
72
73
74cpl_frame* xsh_create_order_table( cpl_frame *in_frame,
75 cpl_frame* spectralformat_frame,
76 cpl_frame *resid_frame,
77 cpl_frame *arclines,
81{
82 xsh_pre *pre = NULL;
83 cpl_frame *result = NULL ;
84 xsh_resid_tab *resid_tab = NULL ;
85 xsh_order_list *list = NULL ;
86 xsh_arclist *arclist = NULL;
87 cpl_propertylist *header = NULL ;
88 double *vlambda = NULL;
89 // double *thpre_x = NULL, *thpre_y = NULL;
90 double *xgauss = NULL, *ygauss = NULL, *vorder = NULL ;
91 double *pos_x = NULL, *pos_y = NULL ;
92 int size, i, nlinecat;
93 //int nlineclean;
94 int *sort = NULL;
95 const char *model_date = NULL;
96 const char *tag = NULL;
97 char fname[256];
98 xsh_spectralformat_list *spectralformat_list = NULL;
99 cpl_table* spc_fmt_tab=NULL;
100 FILE* debug_out = NULL;
101
102 xsh_msg("Creating the Order Table");
103 XSH_ASSURE_NOT_NULL( in_frame);
104 XSH_ASSURE_NOT_NULL( resid_frame);
105 XSH_ASSURE_NOT_NULL( arclines);
109 XSH_ASSURE_NOT_NULL( spectralformat_frame);
110
111 check( pre = xsh_pre_load( in_frame, instrument));
112 check( resid_tab = xsh_resid_tab_load( resid_frame));
113 check( arclist = xsh_arclist_load( arclines));
114 check( spectralformat_list = xsh_spectralformat_list_load(
115 spectralformat_frame, instrument));
116 check( nlinecat = xsh_arclist_get_size( arclist));
117 //check( nlineclean = xsh_arclist_get_size( arclist));
118 check( model_date = xsh_pfits_get_date( resid_tab->header));
119
120 /* Extract from resid table lambda, x, y, dx, dy */
121 check( size = xsh_resid_tab_get_size( resid_tab));
122 xsh_msg_dbg_high ( " Resid Table Size: %d", size);
123
124 check( vlambda = xsh_resid_tab_get_lambda_data( resid_tab ) ) ;
125 check( vorder = xsh_resid_tab_get_order_data( resid_tab ) ) ;
126 //check( thpre_x = xsh_resid_tab_get_thpre_x_data( resid_tab ) ) ;
127 //check( thpre_y = xsh_resid_tab_get_thpre_y_data( resid_tab ) ) ;
128 check( xgauss = xsh_resid_tab_get_xgauss_data( resid_tab ) ) ;
129 check( ygauss = xsh_resid_tab_get_ygauss_data( resid_tab ) ) ;
130
131 /* Calculate pos_x & pos_y from theoretical and diff */
132 XSH_CALLOC( pos_x, double, size);
133 XSH_CALLOC( pos_y, double, size);
134 for( i = 0 ; i<size ; i++ ) {
135 *(pos_x+i) = *(xgauss+i) ;
136 *(pos_y+i) = *(ygauss+i) ;
137 }
138
139 check( sort = xsh_sort( vorder, size, sizeof(double),
141 check(xsh_reindex( vorder, sort, size));
142 check(xsh_reindex( pos_x, sort, size));
143 check(xsh_reindex( pos_y, sort, size));
144 check(xsh_reindex( vlambda, sort, size));
145
146 /* init */
148 xsh_msg_dbg_medium("Number of orders expected : %d",list->size);
149
150 xsh_order_list_fit(list, size, vorder, pos_x, pos_y, da->ordertab_deg_y);
151 xsh_msg("size=%d",size);
152
153 /* update starty and endy */
154 for( i=0; i < list->size; i++){
155 list->list[i].starty = 1;
156 list->list[i].endy = pre->ny;
157 }
158 spc_fmt_tab=cpl_table_load(cpl_frame_get_filename(spectralformat_frame),1,0);
159 if(cpl_table_has_column(spc_fmt_tab,XSH_SPECTRALFORMAT_TABLE_COLNAME_XMIN)) {
160 for( i=0; i < list->size; i++){
161 list->list[i].starty = spectralformat_list->list[i].ymin;
162 list->list[i].endy = spectralformat_list->list[i].ymax;
163 }
164 }
165 xsh_free_table(&spc_fmt_tab);
166 xsh_spectralformat_list_free(&spectralformat_list);
167 xsh_msg( " Save the produced order table" ) ;
168
169 if ( cpl_error_get_code() == CPL_ERROR_NONE){
170 /* save guess order table */
171 check(header = xsh_order_list_get_header(list));
172 check(xsh_pfits_set_qc_nlinecat(header,nlinecat));
173 xsh_msg("create order tab. nlinecat=%d",nlinecat);
175/* check(xsh_pfits_set_qc_nlinecat_clean(header,nlineclean));*/
176 check(xsh_pfits_set_qc(header, (void*)model_date,
178
180 sprintf(fname,"%s%s",tag,".fits");
181 check(result = xsh_order_list_save(list,instrument,fname,tag,pre->ny));
182 //xsh_add_temporary_file( fname ) ;
183
184
185 /* REGDEBUG */
187 int dy,dor;
188
189 debug_out = fopen("predict_cen_points.log","w");
190 for(dy=0;dy< size; dy++){
191 fprintf(debug_out,"%f %f\n",pos_x[dy],pos_y[dy]);
192 }
193
194 fclose(debug_out);
195 debug_out = fopen("predict_cen.log","w");
196
197 for(dor = 0; dor < list->size; dor++){
198 for(dy=0;dy<pre->ny;dy++){
199 float dx = 0;
200
201 check( dx = cpl_polynomial_eval_1d(list->list[dor].cenpoly,dy,NULL));
202 fprintf(debug_out,"%f %d\n",dx,dy);
203 }
204 }
205 fclose(debug_out);
206 }
207 /* ENDREGDEBUG */
208 }
209 else{
210 if (da->mode_iterative){
211 cpl_error_reset();
212 xsh_msg("********** Not enough orders to produce ORDER_TABLE");
213 }
214 }
215
216 cleanup:
217 if(debug_out!=NULL) {
218 fclose(debug_out);
219 }
220 xsh_pre_free( &pre);
221 xsh_resid_tab_free( &resid_tab);
222 xsh_arclist_free( &arclist);
223 xsh_order_list_free( &list);
224 xsh_spectralformat_list_free(&spectralformat_list);
225 XSH_FREE( pos_x);
226 XSH_FREE( pos_y);
227 XSH_FREE( sort);
228 xsh_spectralformat_list_free(&spectralformat_list);
229 return result ;
230}
231
static xsh_instrument * instrument
void xsh_arclist_free(xsh_arclist **list)
free memory associated to a arclist
int xsh_arclist_get_size(xsh_arclist *list)
get size of arcline list
xsh_arclist * xsh_arclist_load(cpl_frame *frame)
load an arcline list frame in arclist structure
xsh_order_list * xsh_order_list_create(xsh_instrument *instr)
create an empty order list
cpl_frame * xsh_order_list_save(xsh_order_list *order_list, xsh_instrument *instrument, const char *filename, const char *tag, const int ny)
Save an order list to a frame.
cpl_propertylist * xsh_order_list_get_header(xsh_order_list *list)
get header of the table
void xsh_order_list_fit(xsh_order_list *list, int size, double *order, double *posx, double *posy, int deg_poly)
fit the polynomial solution of given points
void xsh_order_list_free(xsh_order_list **list)
free memory associated to an order_list
xsh_pre * xsh_pre_load(cpl_frame *frame, xsh_instrument *instr)
Load a xsh_pre structure from a frame.
Definition: xsh_data_pre.c:849
void xsh_pre_free(xsh_pre **pre)
Free a xsh_pre structure.
Definition: xsh_data_pre.c:823
double * xsh_resid_tab_get_lambda_data(xsh_resid_tab *resid)
Get the size of the residual tab.
double * xsh_resid_tab_get_ygauss_data(xsh_resid_tab *resid)
Get the ygauss array of the residual tab.
void xsh_resid_tab_free(xsh_resid_tab **resid)
Free memory associated to a resid_tab.
xsh_resid_tab * xsh_resid_tab_load(cpl_frame *resid_tab_frame)
Load a residual tab from a frame.
double * xsh_resid_tab_get_order_data(xsh_resid_tab *resid)
Get the Orders array of the residual tab.
double * xsh_resid_tab_get_xgauss_data(xsh_resid_tab *resid)
Get the xgauss array of the residual tab.
xsh_spectralformat_list * xsh_spectralformat_list_load(cpl_frame *frame, xsh_instrument *instr)
Load a spectralformat list from a frame.
void xsh_spectralformat_list_free(xsh_spectralformat_list **list)
Free memory associated to an spactralformat_list.
#define check(COMMAND)
Definition: xsh_error.h:71
#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
#define xsh_msg_dbg_high(...)
Definition: xsh_msg.h:40
const char * xsh_pfits_get_date(const cpl_propertylist *plist)
find out the DATE
Definition: xsh_pfits.c:1726
void xsh_pfits_set_qc_nlinecat(cpl_propertylist *plist, double value)
Definition: xsh_pfits_qc.c:587
void xsh_pfits_set_qc_nlinefound(cpl_propertylist *plist, double value)
Definition: xsh_pfits_qc.c:626
void xsh_pfits_set_qc(cpl_propertylist *plist, void *value, const char *kw, xsh_instrument *instrument)
int xsh_debug_level_get(void)
get debug level
Definition: xsh_utils.c:3142
int * xsh_sort(void *base, size_t nmemb, size_t size, int(*compar)(const void *, const void *))
Sort an array and give is index table.
Definition: xsh_utils.c:1966
void xsh_reindex(double *data, int *idx, int size)
TO BE DESCRIBED.
Definition: xsh_utils.c:2008
void xsh_free_table(cpl_table **t)
Deallocate a table and set the pointer to NULL.
Definition: xsh_utils.c:2133
xsh_order * list
cpl_polynomial * cenpoly
cpl_propertylist * header
void * data
Definition: xsh_utils.h:120
static int data_order_compare(const void *one, const void *two)
cpl_frame * xsh_create_order_table(cpl_frame *in_frame, cpl_frame *spectralformat_frame, cpl_frame *resid_frame, cpl_frame *arclines, xsh_detect_arclines_param *da, xsh_clipping_param *dac, xsh_instrument *instrument)
int xsh_resid_tab_get_size(xsh_resid_tab *resid)
#define XSH_SPECTRALFORMAT_TABLE_COLNAME_XMIN
#define XSH_ORDER_TAB_GUESS
Definition: xsh_dfs.h:552
#define XSH_GET_TAG_FROM_ARM(TAG, instr)
Definition: xsh_dfs.h:1548
#define XSH_QC_MODEL_FMTCHK_DATE
#define XSH_FREE(POINTER)
Definition: xsh_utils.h:92
@ XSH_DEBUG_LEVEL_MEDIUM
Definition: xsh_utils.h:138
#define XSH_CALLOC(POINTER, TYPE, SIZE)
Definition: xsh_utils.h:56