X-shooter Pipeline Reference Manual 3.8.15
xsh_data_localization.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: 2011-12-09 10:09:26 $
23 * $Revision: 1.18 $
24 */
25
26#ifdef HAVE_CONFIG_H
27#include <config.h>
28#endif
29
30/*---------------------------------------------------------------------------*/
35/*---------------------------------------------------------------------------*/
36
40/*-----------------------------------------------------------------------------
41 Includes
42 ----------------------------------------------------------------------------*/
43
44#include <math.h>
45#include <xsh_dfs.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
53/*----------------------------------------------------------------------------
54 Function implementation
55 ----------------------------------------------------------------------------*/
56
57/*---------------------------------------------------------------------------*/
64/*---------------------------------------------------------------------------*/
66{
67 xsh_localization* result = NULL;
68
69 XSH_CALLOC(result, xsh_localization, 1);
71
72 cleanup:
73 if (cpl_error_get_code() != CPL_ERROR_NONE) {
74 xsh_localization_free( &result);
75 }
76 return result;
77}
78/*---------------------------------------------------------------------------*/
79
80/*---------------------------------------------------------------------------*/
90/*---------------------------------------------------------------------------*/
92 cpl_table* table = NULL;
93 cpl_propertylist* header = NULL;
94 const char* tablename = NULL;
95 xsh_localization *result = NULL;
96 int pol_degree = 0;
97 cpl_size k;
98
99 /* check input parameters */
100 XSH_ASSURE_NOT_NULL( frame);
101
102 /* get table filename */
103 check( tablename = cpl_frame_get_filename( frame));
104
105 XSH_TABLE_LOAD( table, tablename);
106
107 check( result = xsh_localization_create());
108
109 check( header = cpl_propertylist_load( tablename,0));
110 check(cpl_propertylist_append( result->header, header));
111
112 /* Get the polynomial degree */
114 CPL_TYPE_INT, 0, &pol_degree);
115 result->pol_degree = pol_degree ;
116
117 check( result->edguppoly = cpl_polynomial_new( 1));
118 check( result->cenpoly = cpl_polynomial_new( 1));
119 check( result->edglopoly = cpl_polynomial_new( 1));
120
121 for( k = 0 ; k <= pol_degree ; k++ ) {
122 char colname[32];
123 float coef ;
124
125 sprintf(colname, "%s%"CPL_SIZE_FORMAT, XSH_LOCALIZATION_TABLE_COLNAME_CENTER, k);
126 check( xsh_get_table_value( table, colname, CPL_TYPE_FLOAT, 0, &coef));
127 check( cpl_polynomial_set_coeff( result->cenpoly, &k, coef));
128
129 sprintf(colname, "%s%"CPL_SIZE_FORMAT, XSH_LOCALIZATION_TABLE_COLNAME_EDGLO, k);
130 check( xsh_get_table_value(table, colname, CPL_TYPE_FLOAT, 0, &coef));
131 check( cpl_polynomial_set_coeff( result->edglopoly, &k,coef));
132
133 sprintf(colname, "%s%"CPL_SIZE_FORMAT, XSH_LOCALIZATION_TABLE_COLNAME_EDGUP, k);
134 check( xsh_get_table_value( table, colname, CPL_TYPE_FLOAT, 0, &coef));
135 check( cpl_polynomial_set_coeff( result->edguppoly, &k, coef));
136 }
137
138 cleanup:
139 if (cpl_error_get_code() != CPL_ERROR_NONE) {
140 xsh_error_msg("can't load frame %s",cpl_frame_get_filename(frame));
141 xsh_localization_free(&result);
142 }
143 xsh_free_propertylist( &header);
144 XSH_TABLE_FREE( table);
145 return result;
146}
147/*---------------------------------------------------------------------------*/
148
149/*---------------------------------------------------------------------------*/
156/*---------------------------------------------------------------------------*/
158{
159
160 if (list && *list){
161 /* free the list */
162 xsh_free_polynomial(&(*list)->cenpoly);
163 xsh_free_polynomial(&(*list)->edguppoly);
164 xsh_free_polynomial(&(*list)->edglopoly);
165 xsh_free_propertylist(&((*list)->header));
166 cpl_free(*list);
167 *list = NULL;
168 }
169}
170/*---------------------------------------------------------------------------*/
171
172
173/*---------------------------------------------------------------------------*/
182/*---------------------------------------------------------------------------*/
184{
185 cpl_propertylist *res = NULL;
186
187 XSH_ASSURE_NOT_NULL( list);
188 res = list->header;
189 cleanup:
190 return res;
191}
192/*---------------------------------------------------------------------------*/
193
194/*---------------------------------------------------------------------------*/
208/*---------------------------------------------------------------------------*/
210 const char* filename,
212{
213 cpl_table* table = NULL;
214 cpl_frame * result = NULL ;
215 cpl_size k ;
216 int pol_degree ;
217 char colname[32] ;
218 const char * tag = NULL ;
219
220 /* check input parameters */
222 XSH_ASSURE_NOT_NULL(filename);
223
224 pol_degree = list->pol_degree;
225
226 XSH_ASSURE_NOT_ILLEGAL( pol_degree >= 0 ) ;
227
228 /* create a table */
229 check(table = cpl_table_new( 1));
230
231 /* Create as many column as needed, according to the polynomial degree */
232 for( k = 0 ; k<= pol_degree ; k++ ) {
233
234 sprintf(colname, "%s%"CPL_SIZE_FORMAT, XSH_LOCALIZATION_TABLE_COLNAME_CENTER, k);
235 check(
236 cpl_table_new_column(table, colname, CPL_TYPE_FLOAT));
237 sprintf(colname, "%s%"CPL_SIZE_FORMAT, XSH_LOCALIZATION_TABLE_COLNAME_EDGUP, k);
238 check(
239 cpl_table_new_column(table, colname, CPL_TYPE_FLOAT));
240 sprintf(colname, "%s%"CPL_SIZE_FORMAT, XSH_LOCALIZATION_TABLE_COLNAME_EDGLO, k);
241 check(
242 cpl_table_new_column(table, colname, CPL_TYPE_FLOAT));
243 }
244 check( cpl_table_new_column( table, XSH_LOCALIZATION_TABLE_DEGPOL,
245 CPL_TYPE_INT ) ) ;
246
247
248 for( k = 0 ; k <= pol_degree ; k++ ) {
249 double coef ;
250
251 /* Get and Write coef from center polynomial */
252 check(coef = cpl_polynomial_get_coeff( list->cenpoly, &k));
253 sprintf(colname, "%s%"CPL_SIZE_FORMAT, XSH_LOCALIZATION_TABLE_COLNAME_CENTER, k);
254 check(cpl_table_set( table, colname, 0, coef));
255
256 /* Get and Write coef from upper polynomial */
257 check(coef = cpl_polynomial_get_coeff( list->edguppoly, &k));
258 sprintf(colname, "%s%"CPL_SIZE_FORMAT, XSH_LOCALIZATION_TABLE_COLNAME_EDGUP, k);
259 check(cpl_table_set( table, colname, 0, coef));
260
261 /* Get and Write coef from lower polynomial */
262 check(coef = cpl_polynomial_get_coeff( list->edglopoly, &k));
263 sprintf(colname, "%s%"CPL_SIZE_FORMAT, XSH_LOCALIZATION_TABLE_COLNAME_EDGLO, k);
264 check(cpl_table_set( table, colname, 0, coef));
265 }
266
267 check( cpl_table_set(table, XSH_LOCALIZATION_TABLE_DEGPOL, 0,
268 pol_degree));
269 /* create fits file */
270 check( cpl_table_save( table, list->header, NULL, filename, CPL_IO_DEFAULT));
271 /* Create the frame */
273 check(result=xsh_frame_product(filename,
274 tag,
275 CPL_FRAME_TYPE_TABLE,
276 CPL_FRAME_GROUP_PRODUCT,
277 CPL_FRAME_LEVEL_TEMPORARY));
278
279 cleanup:
280 XSH_TABLE_FREE(table);
281 return result ;
282}
283
static xsh_instrument * instrument
xsh_localization * xsh_localization_load(cpl_frame *frame)
Load a localization list from a frame.
cpl_frame * xsh_localization_save(xsh_localization *list, const char *filename, xsh_instrument *instrument)
save a localization to a frame
void xsh_localization_free(xsh_localization **list)
free memory associated to a localization_list
cpl_propertylist * xsh_localization_get_header(xsh_localization *list)
get header of the table
xsh_localization * xsh_localization_create(void)
Create an empty localization list.
#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
void xsh_free_polynomial(cpl_polynomial **p)
Deallocate a polynomial and set the pointer to NULL.
Definition: xsh_utils.c:2194
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
cpl_polynomial * cenpoly
cpl_polynomial * edguppoly
cpl_polynomial * edglopoly
#define XSH_LOCALIZATION_TABLE_DEGPOL
#define XSH_LOCALIZATION_TABLE_COLNAME_EDGLO
#define XSH_LOCALIZATION_TABLE_COLNAME_EDGUP
#define XSH_LOCALIZATION_TABLE_COLNAME_CENTER
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 XSH_LOCALIZATION
Definition: xsh_dfs.h:600
#define XSH_GET_TAG_FROM_ARM(TAG, instr)
Definition: xsh_dfs.h:1548
#define XSH_NEW_PROPERTYLIST(POINTER)
Definition: xsh_utils.h:70
#define XSH_CALLOC(POINTER, TYPE, SIZE)
Definition: xsh_utils.h:56
#define XSH_TABLE_LOAD(TABLE, NAME)
#define XSH_TABLE_FREE(TABLE)