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 int nrows=0;
00121
00122
00123 XSH_ASSURE_NOT_NULL(grid);
00124
00125
00126 nrows= grid->idx;
00127 tab=cpl_table_new(nrows);
00128 cpl_table_new_column(tab,"X",CPL_TYPE_DOUBLE);
00129 cpl_table_new_column(tab,"Y",CPL_TYPE_DOUBLE);
00130 cpl_table_new_column(tab,"INT",CPL_TYPE_DOUBLE);
00131
00132 cpl_table_fill_column_window(tab,"X",0,nrows,-1);
00133 cpl_table_fill_column_window(tab,"Y",0,nrows,-1);
00134 cpl_table_fill_column_window(tab,"INT",0,nrows,-1);
00135
00136 px=cpl_table_get_data_double(tab,"X");
00137 py=cpl_table_get_data_double(tab,"Y");
00138 pi=cpl_table_get_data_double(tab,"INT");
00139
00140 for(i =0 ; i<nrows ; i++ ) {
00141 px[i]=grid->list[i]->x;
00142 py[i]=grid->list[i]->y;
00143 pi[i]=grid->list[i]->v;
00144 }
00145
00146 cleanup:
00147 return tab;
00148 }
00149
00150
00156
00157 xsh_grid* xsh_grid_create(int size){
00158 xsh_grid* grid = NULL;
00159
00160
00161 XSH_ASSURE_NOT_ILLEGAL(size > 0);
00162 XSH_CALLOC(grid,xsh_grid,1);
00163
00164 grid->size = size;
00165 grid->idx = 0;
00166 XSH_CALLOC(grid->list, xsh_grid_point*, size);
00167
00168 cleanup:
00169 if(cpl_error_get_code() != CPL_ERROR_NONE){
00170 xsh_grid_free(&grid);
00171 }
00172 return grid;
00173 }
00174
00175
00176
00177
00182
00183 void xsh_grid_free(xsh_grid** grid)
00184 {
00185 int i = 0;
00186
00187 if (grid && *grid){
00188 if ( (*grid)->list ){
00189 for (i=0; i<= (*grid)->idx; i++){
00190 XSH_FREE( (*grid)->list[i]);
00191 }
00192 XSH_FREE( (*grid)->list);
00193 }
00194 XSH_FREE(*grid);
00195 }
00196 }
00197
00198
00206
00207 void xsh_grid_add(xsh_grid* grid, int x, int y, double v)
00208 {
00209 xsh_grid_point* point = NULL;
00210
00211 XSH_ASSURE_NOT_NULL(grid);
00212 XSH_ASSURE_NOT_ILLEGAL(grid->idx < grid->size);
00213
00214 XSH_MALLOC(point,xsh_grid_point,1);
00215
00216 point->x = x;
00217 point->y = y;
00218 point->v = v;
00219
00220 grid->list[grid->idx] = point;
00221 grid->idx++;
00222 cleanup:
00223 return;
00224 }
00225
00226
00227
00232
00233
00234 void xsh_grid_sort(xsh_grid* grid)
00235 {
00236
00237 XSH_ASSURE_NOT_NULL(grid);
00238
00239
00240 qsort(grid->list,grid->idx, sizeof(xsh_grid_point*),
00241 xsh_grid_point_compare);
00242 cleanup:
00243 return;
00244 }
00245
00246
00253
00254 xsh_grid_point* xsh_grid_point_get(xsh_grid* grid, int i)
00255 {
00256 xsh_grid_point* res = NULL;
00257
00258
00259 XSH_ASSURE_NOT_NULL(grid);
00260 XSH_ASSURE_NOT_ILLEGAL( i < grid->idx);
00261 res = grid->list[i];
00262
00263 cleanup:
00264 return res;
00265 }
00266
00267
00273
00274 int xsh_grid_get_index(xsh_grid* grid)
00275 {
00276 int res = 0;
00277
00278
00279 XSH_ASSURE_NOT_NULL(grid);
00280 res = grid->idx;
00281
00282 cleanup:
00283 return res;
00284 }