X-shooter Pipeline Reference Manual 3.8.15
xsh_data_grid.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: 2013-04-26 10:44:44 $
23 * $Revision: 1.15 $
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_grid.h>
46#include <xsh_utils.h>
47#include <xsh_error.h>
48#include <xsh_msg.h>
49#include <xsh_pfits.h>
50#include <cpl.h>
51
52/*----------------------------------------------------------------------------
53 Function implementation
54 ----------------------------------------------------------------------------*/
55
56static int xsh_grid_point_compare(const void* one, const void* two){
57 xsh_grid_point** a = NULL;
58 xsh_grid_point** b = NULL;
59 int xa, ya, xb, yb;
60
61 a = (xsh_grid_point**) one;
62 b = (xsh_grid_point**) two;
63
64 xa = (*a)->x;
65 xb = (*b)->x;
66
67 ya = (*a)->y;
68 yb = (*b)->y;
69
70 if (ya < yb)
71 return -1;
72 else if ( (ya == yb) && (xa <= xb) ){
73 return -1;
74 }
75 else{
76 return 1;
77 }
78}
79
80
81/****************************************************************************/
86/****************************************************************************/
88{
89 int i = 0;
90
91 /* check input parameters */
93
94 xsh_msg( "Grid dump" ) ;
95 xsh_msg( "Size: %d", grid->size ) ;
96 xsh_msg( "Elts: %d", grid->idx ) ;
97 for(i =0 ; i<grid->idx ; i++ ) {
98 xsh_msg( "x %d y %d v %f", grid->list[i]->x, grid->list[i]->y ,
99 grid->list[i]->v) ;
100 }
101
102 cleanup:
103 return;
104}
105
106
107/****************************************************************************/
112/****************************************************************************/
113cpl_table* xsh_grid2table( xsh_grid* grid)
114{
115 int i = 0;
116 cpl_table* tab=NULL;
117 double* px=NULL;
118 double* py=NULL;
119 double* pi=NULL;
120 double* pe=NULL;
121 //double* pf=NULL;
122 //double* pr=NULL;
123
124 int nrows=0;
125
126 /* check input parameters */
128
129
130 nrows= grid->idx;
131 tab=cpl_table_new(nrows);
132 cpl_table_new_column(tab,"X",CPL_TYPE_DOUBLE);
133 cpl_table_new_column(tab,"Y",CPL_TYPE_DOUBLE);
134 cpl_table_new_column(tab,"INT",CPL_TYPE_DOUBLE);
135 cpl_table_new_column(tab,"ERR",CPL_TYPE_DOUBLE);
136
137 cpl_table_fill_column_window(tab,"X",0,nrows,-1);
138 cpl_table_fill_column_window(tab,"Y",0,nrows,-1);
139 cpl_table_fill_column_window(tab,"INT",0,nrows,-1);
140 cpl_table_fill_column_window(tab,"ERR",0,nrows,-1);
141
142 px=cpl_table_get_data_double(tab,"X");
143 py=cpl_table_get_data_double(tab,"Y");
144 pi=cpl_table_get_data_double(tab,"INT");
145 pe=cpl_table_get_data_double(tab,"ERR");
146
147 for (i = 0; i < nrows; i++) {
148 px[i] = grid->list[i]->x;
149 py[i] = grid->list[i]->y;
150 pi[i] = grid->list[i]->v;
151 pe[i] = grid->list[i]->errs;
152 }
153
154 cleanup:
155 return tab;
156}
157
158/****************************************************************************/
164/****************************************************************************/
166 xsh_grid* grid = NULL;
167
168 /* check input parameters */
170 XSH_CALLOC(grid,xsh_grid,1);
171
172 grid->size = size;
173 grid->idx = 0;
175
176 cleanup:
177 if(cpl_error_get_code() != CPL_ERROR_NONE){
178 xsh_grid_free(&grid);
179 }
180 return grid;
181}
182
183
184
185/****************************************************************************/
190/****************************************************************************/
192{
193 int i = 0;
194
195 if (grid && *grid){
196 if ( (*grid)->list ){
197 for (i=0; i< (*grid)->idx; i++) {
198 XSH_FREE( (*grid)->list[i]);
199 }
200 XSH_FREE( (*grid)->list);
201 }
202 XSH_FREE(*grid);
203 }
204}
205
206/****************************************************************************/
214/****************************************************************************/
215void xsh_grid_add(xsh_grid* grid, int x, int y, double data, double errs, int qual)
216{
217 xsh_grid_point* point = NULL;
218 /* check input parameters */
220 XSH_ASSURE_NOT_ILLEGAL(grid->idx < grid->size);
221
222 XSH_MALLOC(point,xsh_grid_point,1);
223
224 point->x = x;
225 point->y = y;
226 point->v = data;
227 point->errs = errs;
228 point->qual = qual;
229
230 grid->list[grid->idx] = point;
231 grid->idx++;
232 cleanup:
233 return;
234}
235
236
237/****************************************************************************/
242/****************************************************************************/
243
245{
246 /* check input parameters */
248
249 /* sort by Y and by X after */
250 qsort(grid->list,grid->idx, sizeof(xsh_grid_point*),
252 cleanup:
253 return;
254}
255
256/****************************************************************************/
263/****************************************************************************/
265{
266 xsh_grid_point* res = NULL;
267
268 /* check input parameters */
270 XSH_ASSURE_NOT_ILLEGAL( i < grid->idx);
271 res = grid->list[i];
272
273 cleanup:
274 return res;
275}
276
277/****************************************************************************/
283/****************************************************************************/
285{
286 int res = 0;
287
288 /* check input parameters */
290 res = grid->idx;
291
292 cleanup:
293 return res;
294}
void xsh_grid_add(xsh_grid *grid, int x, int y, double data, double errs, int qual)
add a point to a grid
xsh_grid * xsh_grid_create(int size)
Create a grid.
void xsh_grid_dump(xsh_grid *grid)
Dump main info about a grid.
Definition: xsh_data_grid.c:87
void xsh_grid_sort(xsh_grid *grid)
sort grid points
int xsh_grid_get_index(xsh_grid *grid)
get the number of elements in the grid
xsh_grid_point * xsh_grid_point_get(xsh_grid *grid, int i)
get x points from the grid
void xsh_grid_free(xsh_grid **grid)
Free a grid.
static int xsh_grid_point_compare(const void *one, const void *two)
Definition: xsh_data_grid.c:56
cpl_table * xsh_grid2table(xsh_grid *grid)
Dump main info about a grid.
#define XSH_ASSURE_NOT_ILLEGAL(cond)
Definition: xsh_error.h:107
#define XSH_ASSURE_NOT_NULL(pointer)
Definition: xsh_error.h:99
int size
int * y
int * x
#define xsh_msg(...)
Print a message on info level.
Definition: xsh_msg.h:121
xsh_grid_point ** list
Definition: xsh_data_grid.h:52
#define XSH_FREE(POINTER)
Definition: xsh_utils.h:92
#define XSH_MALLOC(POINTER, TYPE, SIZE)
Definition: xsh_utils.h:49
#define XSH_CALLOC(POINTER, TYPE, SIZE)
Definition: xsh_utils.h:56