00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027 #ifdef HAVE_CONFIG_H
00028 #include <config.h>
00029 #endif
00030
00031
00036
00037
00041
00042
00043
00044
00045 #include <xsh_data_grid.h>
00046 #include <xsh_utils.h>
00047 #include <xsh_error.h>
00048 #include <xsh_msg.h>
00049 #include <xsh_pfits.h>
00050 #include <cpl.h>
00051
00052
00053
00054
00055
00056 static int xsh_grid_point_compare(const void* one, const void* two){
00057 xsh_grid_point** a = NULL;
00058 xsh_grid_point** b = NULL;
00059 int xa, ya, xb, yb;
00060
00061 a = (xsh_grid_point**) one;
00062 b = (xsh_grid_point**) two;
00063
00064 xa = (*a)->x;
00065 xb = (*b)->x;
00066
00067 ya = (*a)->y;
00068 yb = (*b)->y;
00069
00070 if (ya < yb)
00071 return -1;
00072 else if ( (ya == yb) && (xa <= xb) ){
00073 return -1;
00074 }
00075 else{
00076 return 1;
00077 }
00078 }
00079
00080
00081
00086
00087 void xsh_grid_dump( xsh_grid* grid)
00088 {
00089 int i = 0;
00090
00091
00092 XSH_ASSURE_NOT_NULL(grid);
00093
00094 xsh_msg( "Grid dump" ) ;
00095 xsh_msg( "Size: %d", grid->size ) ;
00096 xsh_msg( "Elts: %d", grid->idx ) ;
00097 for(i =0 ; i<grid->idx ; i++ ) {
00098 xsh_msg( "x %d y %d v %f", grid->list[i]->x, grid->list[i]->y ,
00099 grid->list[i]->v) ;
00100 }
00101
00102 cleanup:
00103 return;
00104 }
00105
00106
00107
00112
00113 cpl_table* xsh_grid2table( xsh_grid* grid)
00114 {
00115 int i = 0;
00116 cpl_table* tab=NULL;
00117 double* px=NULL;
00118 double* py=NULL;
00119 double* pi=NULL;
00120 double* pe=NULL;
00121
00122
00123
00124 int nrows=0;
00125
00126
00127 XSH_ASSURE_NOT_NULL(grid);
00128
00129
00130 nrows= grid->idx;
00131 tab=cpl_table_new(nrows);
00132 cpl_table_new_column(tab,"X",CPL_TYPE_DOUBLE);
00133 cpl_table_new_column(tab,"Y",CPL_TYPE_DOUBLE);
00134 cpl_table_new_column(tab,"INT",CPL_TYPE_DOUBLE);
00135 cpl_table_new_column(tab,"ERR",CPL_TYPE_DOUBLE);
00136
00137 cpl_table_fill_column_window(tab,"X",0,nrows,-1);
00138 cpl_table_fill_column_window(tab,"Y",0,nrows,-1);
00139 cpl_table_fill_column_window(tab,"INT",0,nrows,-1);
00140 cpl_table_fill_column_window(tab,"ERR",0,nrows,-1);
00141
00142 px=cpl_table_get_data_double(tab,"X");
00143 py=cpl_table_get_data_double(tab,"Y");
00144 pi=cpl_table_get_data_double(tab,"INT");
00145 pe=cpl_table_get_data_double(tab,"ERR");
00146
00147 for (i = 0; i < nrows; i++) {
00148 px[i] = grid->list[i]->x;
00149 py[i] = grid->list[i]->y;
00150 pi[i] = grid->list[i]->v;
00151 pe[i] = grid->list[i]->errs;
00152 }
00153
00154 cleanup:
00155 return tab;
00156 }
00157
00158
00164
00165 xsh_grid* xsh_grid_create(int size){
00166 xsh_grid* grid = NULL;
00167
00168
00169 XSH_ASSURE_NOT_ILLEGAL(size > 0);
00170 XSH_CALLOC(grid,xsh_grid,1);
00171
00172 grid->size = size;
00173 grid->idx = 0;
00174 XSH_CALLOC(grid->list, xsh_grid_point*, size);
00175
00176 cleanup:
00177 if(cpl_error_get_code() != CPL_ERROR_NONE){
00178 xsh_grid_free(&grid);
00179 }
00180 return grid;
00181 }
00182
00183
00184
00185
00190
00191 void xsh_grid_free(xsh_grid** grid)
00192 {
00193 int i = 0;
00194
00195 if (grid && *grid){
00196 if ( (*grid)->list ){
00197 for (i=0; i<= (*grid)->idx; i++){
00198 XSH_FREE( (*grid)->list[i]);
00199 }
00200 XSH_FREE( (*grid)->list);
00201 }
00202 XSH_FREE(*grid);
00203 }
00204 }
00205
00206
00214
00215 void xsh_grid_add(xsh_grid* grid, int x, int y, double data, double errs, int qual)
00216 {
00217 xsh_grid_point* point = NULL;
00218
00219 XSH_ASSURE_NOT_NULL(grid);
00220 XSH_ASSURE_NOT_ILLEGAL(grid->idx < grid->size);
00221
00222 XSH_MALLOC(point,xsh_grid_point,1);
00223
00224 point->x = x;
00225 point->y = y;
00226 point->v = data;
00227 point->errs = errs;
00228 point->qual = qual;
00229
00230 grid->list[grid->idx] = point;
00231 grid->idx++;
00232 cleanup:
00233 return;
00234 }
00235
00236
00237
00242
00243
00244 void xsh_grid_sort(xsh_grid* grid)
00245 {
00246
00247 XSH_ASSURE_NOT_NULL(grid);
00248
00249
00250 qsort(grid->list,grid->idx, sizeof(xsh_grid_point*),
00251 xsh_grid_point_compare);
00252 cleanup:
00253 return;
00254 }
00255
00256
00263
00264 xsh_grid_point* xsh_grid_point_get(xsh_grid* grid, int i)
00265 {
00266 xsh_grid_point* res = NULL;
00267
00268
00269 XSH_ASSURE_NOT_NULL(grid);
00270 XSH_ASSURE_NOT_ILLEGAL( i < grid->idx);
00271 res = grid->list[i];
00272
00273 cleanup:
00274 return res;
00275 }
00276
00277
00283
00284 int xsh_grid_get_index(xsh_grid* grid)
00285 {
00286 int res = 0;
00287
00288
00289 XSH_ASSURE_NOT_NULL(grid);
00290 res = grid->idx;
00291
00292 cleanup:
00293 return res;
00294 }