File: xsh_data_grid.c

package info (click to toggle)
cpl-plugin-xshoo 2.8.4%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 18,168 kB
  • sloc: ansic: 146,236; sh: 11,433; python: 2,342; makefile: 1,024
file content (295 lines) | stat: -rw-r--r-- 8,823 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
/*                                                                           *
 *   This file is part of the ESO X-shooter Pipeline                         *
 *   Copyright (C) 2006 European Southern Observatory                        *
 *                                                                           *
 *   This library is free software; you can redistribute it and/or modify    *
 *   it under the terms of the GNU General Public License as published by    *
 *   the Free Software Foundation; either version 2 of the License, or       *
 *   (at your option) any later version.                                     *
 *                                                                           *
 *   This program is distributed in the hope that it will be useful,         *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of          *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the           *
 *   GNU General Public License for more details.                            *
 *                                                                           *
 *   You should have received a copy of the GNU General Public License       *
 *   along with this program; if not, write to the Free Software             *
 *   Foundation, 51 Franklin St, Fifth Floor, Boston, MA  02111-1307  USA    *
 *                                                                           */

/*
 * $Author: amodigli $
 * $Date: 2013-04-26 10:44:44 $
 * $Revision: 1.15 $
 * $Name: not supported by cvs2svn $
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

/*---------------------------------------------------------------------------*/
/**
 * @defgroup xsh_data_grid Grid
 * @ingroup data_handling
 */
/*---------------------------------------------------------------------------*/

/**@{*/


/*-----------------------------------------------------------------------------
                                 Includes
 ----------------------------------------------------------------------------*/

#include <xsh_data_grid.h>
#include <xsh_utils.h>
#include <xsh_error.h>
#include <xsh_msg.h>
#include <xsh_pfits.h>
#include <cpl.h>

/*----------------------------------------------------------------------------
                                 Function implementation
 ----------------------------------------------------------------------------*/

static int xsh_grid_point_compare(const void* one, const void* two){
  xsh_grid_point** a = NULL;
  xsh_grid_point** b = NULL;
  int xa, ya, xb, yb;
                                                                                                                                                             
  a = (xsh_grid_point**) one;
  b = (xsh_grid_point**) two;
                                                                                                                                                             
  xa = (*a)->x;
  xb = (*b)->x;

  ya = (*a)->y;
  yb = (*b)->y;
                                                                                                                                                             
  if (ya < yb)
    return -1;
  else if ( (ya == yb) && (xa <= xb) ){
    return -1;
  }
  else{
    return 1;
  }                                                                                                                                                          
}


/****************************************************************************/
/** 
 * @brief Dump main info about a grid
 * @param grid pointer
 */
/****************************************************************************/
void xsh_grid_dump( xsh_grid* grid)
{
  int i = 0;

  /* check input parameters */
  XSH_ASSURE_NOT_NULL(grid);

  xsh_msg( "Grid  dump" ) ;
  xsh_msg( "Size: %d", grid->size ) ;
  xsh_msg( "Elts: %d", grid->idx ) ;
  for(i =0  ; i<grid->idx ; i++ ) {
    xsh_msg( "x %d y %d v %f", grid->list[i]->x, grid->list[i]->y ,
      grid->list[i]->v) ;
  }
  
  cleanup:
    return;
}


/****************************************************************************/
/** 
 * @brief Dump main info about a grid
 * @param grid pointer
 */
/****************************************************************************/
cpl_table* xsh_grid2table( xsh_grid* grid)
{
  int i = 0;
  cpl_table* tab=NULL;
  double* px=NULL;
  double* py=NULL;
  double* pi=NULL;
  double* pe=NULL;
  //double* pf=NULL;
  //double* pr=NULL;

  int nrows=0;

  /* check input parameters */
  XSH_ASSURE_NOT_NULL(grid);


  nrows= grid->idx;
  tab=cpl_table_new(nrows);
  cpl_table_new_column(tab,"X",CPL_TYPE_DOUBLE);
  cpl_table_new_column(tab,"Y",CPL_TYPE_DOUBLE);
  cpl_table_new_column(tab,"INT",CPL_TYPE_DOUBLE);
  cpl_table_new_column(tab,"ERR",CPL_TYPE_DOUBLE);

  cpl_table_fill_column_window(tab,"X",0,nrows,-1);
  cpl_table_fill_column_window(tab,"Y",0,nrows,-1);
  cpl_table_fill_column_window(tab,"INT",0,nrows,-1);
  cpl_table_fill_column_window(tab,"ERR",0,nrows,-1);

  px=cpl_table_get_data_double(tab,"X");
  py=cpl_table_get_data_double(tab,"Y");
  pi=cpl_table_get_data_double(tab,"INT");
  pe=cpl_table_get_data_double(tab,"ERR");

  for (i = 0; i < nrows; i++) {
    px[i] = grid->list[i]->x;
    py[i] = grid->list[i]->y;
    pi[i] = grid->list[i]->v;
    pe[i] = grid->list[i]->errs;
  }

  cleanup:
    return tab;
}

/****************************************************************************/
/** 
 * @brief Create a grid
 * @param size size of the grid
 * @return the grid structure
 */
/****************************************************************************/
xsh_grid* xsh_grid_create(int size){
  xsh_grid* grid = NULL;

  /* check input parameters */
  XSH_ASSURE_NOT_ILLEGAL(size > 0);
  XSH_CALLOC(grid,xsh_grid,1);
  
  grid->size = size;
  grid->idx = 0;
  XSH_CALLOC(grid->list, xsh_grid_point*, size);

  cleanup:
    if(cpl_error_get_code() != CPL_ERROR_NONE){
      xsh_grid_free(&grid);
    }
    return grid;
}



/****************************************************************************/
/** 
 * @brief Free a grid
 * @param grid the grid pointer
 */
/****************************************************************************/
void xsh_grid_free(xsh_grid** grid)
{
  int i = 0;

  if (grid && *grid){
    if ( (*grid)->list ){
      for (i=0; i< (*grid)->idx; i++) {
        XSH_FREE( (*grid)->list[i]);
      }
      XSH_FREE( (*grid)->list);
    }
    XSH_FREE(*grid);
  }
}

/****************************************************************************/
/** 
 * @brief add a point to a grid
 * @param grid The grid
 * @param x x coordinate of the point
 * @param y y coordinate of the point
 * @param v value of the point
 */
/****************************************************************************/
void xsh_grid_add(xsh_grid* grid, int x, int y, double data, double errs, int qual)
{
  xsh_grid_point* point = NULL;
  /* check input parameters */
  XSH_ASSURE_NOT_NULL(grid);
  XSH_ASSURE_NOT_ILLEGAL(grid->idx < grid->size);

  XSH_MALLOC(point,xsh_grid_point,1);
  
  point->x = x;
  point->y = y;
  point->v = data;
  point->errs = errs;
  point->qual = qual;

  grid->list[grid->idx] = point;
  grid->idx++;
  cleanup:
    return;
}


/****************************************************************************/
/**
 * @brief sort grid points
 * @param grid The grid
 */
/****************************************************************************/

void xsh_grid_sort(xsh_grid* grid)
{
  /* check input parameters */
  XSH_ASSURE_NOT_NULL(grid);
                                                                                                                                                           
  /* sort by Y and by X after */
  qsort(grid->list,grid->idx, sizeof(xsh_grid_point*),
    xsh_grid_point_compare);
  cleanup:
    return;
}

/****************************************************************************/
/** 
 * @brief get x points from the grid
 * @param grid the grid pointer
 * @param i grid index point
 * @return the x points
 */
/****************************************************************************/
xsh_grid_point* xsh_grid_point_get(xsh_grid* grid, int i)
{
  xsh_grid_point* res = NULL;

  /* check input parameters */
  XSH_ASSURE_NOT_NULL(grid);
  XSH_ASSURE_NOT_ILLEGAL( i < grid->idx);
  res = grid->list[i];

  cleanup:
    return res;
}

/****************************************************************************/
/** 
 * @brief get the number of elements in the grid
 * @param grid the grid pointer
 * @return the number of elements in the grid
 */
/****************************************************************************/
int xsh_grid_get_index(xsh_grid* grid)
{
  int res = 0;

  /* check input parameters */
  XSH_ASSURE_NOT_NULL(grid);
  res = grid->idx;

  cleanup:
    return res;
}
/**@}*/