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
00028
00029
00030
00031 #include "drms.h"
00032 #include "serverdefs.h"
00033 #include "drms_types.h"
00034 #include "drms_record.h"
00035 #include "drms_names.h"
00036 #include "drms_env.h"
00037 #include "drms_network.h"
00038 #include "drms_keyword.h"
00039 #include "astro.h"
00040
00041 #define MAX_RANK (2)
00042
00043
00044 static void Ccker(double *u, double s) {
00045 double s2, s3;
00046
00047 s2= s * s;
00048 s3= s2 * s;
00049 u[0] = s2 - 0.5 * (s3 + s);
00050 u[1] = 1.5*s3 - 2.5*s2 + 1.0;
00051 u[2] = -1.5*s3 + 2.0*s2 + 0.5*s;
00052 u[3] = 0.5 * (s3 - s2);
00053 }
00054
00055
00056 float Ccint2(float *f, int nx, int ny, double x, double y) {
00057 double ux[4], uy[4];
00058 double sum;
00059 int ix, iy, ix1, iy1, i, j;
00060
00061 if (x < 1. || x >= (float)(nx-2) || y < 1. || y >= (float)(ny-2))
00062 return F_NAN;
00063
00064 ix = (int)x;
00065 Ccker (ux, x - (double)ix);
00066 iy = (int)y;
00067 Ccker (uy, y - (double)iy);
00068
00069 ix1 = ix - 1;
00070 iy1 = iy - 1;
00071 sum = 0.;
00072 for (i = 0; i < 4; i++)
00073 for (j = 0; j < 4; j++)
00074 sum = sum + f[(iy1+i) * nx + ix1 + j] * uy[i] * ux[j];
00075 return (float)sum;
00076 }
00077
00078 double Ccint2d(double *f, int nx, int ny, double x, double y) {
00079 double ux[4], uy[4];
00080 double sum;
00081 int ix, iy, ix1, iy1, i, j;
00082
00083 if (x < 1. || x >= (float)(nx-2) || y < 1. || y >= (float)(ny-2))
00084 return D_NAN;
00085
00086 ix = (int)x;
00087 Ccker (ux, x - (double)ix);
00088 iy = (int)y;
00089 Ccker (uy, y - (double)iy);
00090
00091 ix1 = ix - 1;
00092 iy1 = iy - 1;
00093 sum = 0.;
00094 for (i = 0; i < 4; i++)
00095 for (j = 0; j < 4; j++)
00096 sum = sum + f[(iy1+i) * nx + ix1 + j] * uy[i] * ux[j];
00097 return sum;
00098 }
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113 float Linint2(float *f, int nx, int ny, double x, double y) {
00114
00115 double p0, p1, q0, q1;
00116 int ilow, jlow;
00117 float *fptr;
00118 double u;
00119
00120 ilow = (int)floor (x);
00121 jlow = (int)floor (y);
00122 if (ilow < 0) ilow = 0;
00123 if (ilow+1 >= nx) ilow -= 1;
00124 if (jlow < 0) jlow = 0;
00125 if (jlow+1 >= ny) jlow -= 1;
00126 p1 = x - ilow;
00127 p0 = 1.0 - p1;
00128 q1 = y - jlow;
00129 q0 = 1.0 - q1;
00130
00131 u = 0.0;
00132 fptr = f + jlow*nx + ilow;
00133 u += p0 * q0 * *fptr++;
00134 u += p1 * q0 * *fptr;
00135 fptr += nx - 1;
00136 u += p0 * q1 * *fptr++;
00137 u += p1 * q1 * *fptr;
00138 return (float)u;
00139 }
00140
00141 double Linint2d(double *f, int nx, int ny, double x, double y) {
00142
00143 double p0, p1, q0, q1;
00144 int ilow, jlow;
00145 double *fptr;
00146 double u;
00147
00148 ilow = (int)floor (x);
00149 jlow = (int)floor (y);
00150 if (ilow < 0) ilow = 0;
00151 if (ilow+1 >= nx) ilow -= 1;
00152 if (jlow < 0) jlow = 0;
00153 if (jlow+1 >= ny) jlow -= 1;
00154 p1 = x - ilow;
00155 p0 = 1.0 - p1;
00156 q1 = y - jlow;
00157 q0 = 1.0 - q1;
00158
00159 u = 0.0;
00160 fptr = f + jlow*nx + ilow;
00161 u += p0 * q0 * *fptr++;
00162 u += p1 * q0 * *fptr;
00163 fptr += nx - 1;
00164 u += p0 * q1 * *fptr++;
00165 u += p1 * q1 * *fptr;
00166 return u;
00167 }
00168
00169 int Regrid(DRMS_Array_t **dataArr, int *new_length, LIBASTRO_Interpolation_t scheme) {
00170
00171
00172
00173
00174
00175
00176
00177
00178
00179
00180
00181 LIBASTRO_Error_t error = kLIBASTRO_Success;
00182
00183 void *new;
00184 double step[MAX_RANK], start[MAX_RANK];
00185 double x, y;
00186 int orange[MAX_RANK], nrange[MAX_RANK];
00187 int col, row, n;
00188 int nsize = 1;
00189 int rank = 0;
00190
00191 int *dims = NULL;
00192 DRMS_Type_t saveType;
00193 DRMS_Array_t *segArrConv = NULL;
00194 DRMS_Array_t *inputArr = NULL;
00195
00196 int status = 0;
00197 int bDataConverted = 0;
00198
00199 if (*dataArr == NULL)
00200 {
00201 error = kLIBASTRO_BadData;
00202 }
00203 else
00204 {
00205 dims = (*dataArr)->axis;
00206 rank = (*dataArr)->naxis;
00207
00208 if (rank != 2) return kLIBASTRO_BadDimensionality;
00209
00210 for (n = 0; n < rank; n++) {
00211 orange[n] = dims[n];
00212 nrange[n] = new_length[n];
00213 nsize *= nrange[n];
00214 step[n] = (double) orange[n] / (double) nrange[n];
00215 start[n] = -0.5 + 0.5 * step[n];
00216 }
00217
00218
00219
00220
00221 saveType = (*dataArr)->type;
00222
00223 if (saveType == DRMS_TYPE_INT || saveType == DRMS_TYPE_LONGLONG)
00224 {
00225
00226 segArrConv = drms_array_convert(DRMS_TYPE_DOUBLE,
00227 (*dataArr)->bzero,
00228 (*dataArr)->bscale,
00229 (*dataArr));
00230
00231 bDataConverted = 1;
00232 }
00233 else if (saveType == DRMS_TYPE_SHORT || saveType == DRMS_TYPE_CHAR)
00234 {
00235 segArrConv = drms_array_convert(DRMS_TYPE_FLOAT,
00236 (*dataArr)->bzero,
00237 (*dataArr)->bscale,
00238 (*dataArr));
00239 bDataConverted = 1;
00240 }
00241
00242 if (bDataConverted)
00243 {
00244 inputArr = segArrConv;
00245 }
00246 else
00247 {
00248 inputArr = *dataArr;
00249 }
00250
00251 if (bDataConverted && segArrConv == NULL)
00252 {
00253 error = kLIBASTRO_CouldntCreateData;
00254 }
00255 else
00256 {
00257 y = start[1];
00258
00259 if (inputArr->type == DRMS_TYPE_DOUBLE)
00260 new = malloc(nsize * sizeof (double));
00261 else
00262 new = malloc(nsize * sizeof (float));
00263
00264 for (row = 0, n = 0; row < nrange[1]; row++) {
00265 x = start[0];
00266 for (col = 0; col < nrange[0]; col++) {
00267 if (inputArr->type == DRMS_TYPE_DOUBLE)
00268 {
00269 if (scheme == kLIBASTRO_InterCubic)
00270 {
00271 ((double *)new)[n] = Ccint2d((double *)inputArr->data,
00272 orange[0],
00273 orange[1],
00274 x,
00275 y);
00276 }
00277 else
00278 {
00279 ((double *)new)[n] = Linint2d((double *)inputArr->data,
00280 orange[0],
00281 orange[1],
00282 x,
00283 y);
00284 }
00285 }
00286 else
00287 {
00288 if (scheme == kLIBASTRO_InterCubic)
00289 {
00290 ((float *)new)[n] = Ccint2((float *)inputArr->data,
00291 orange[0],
00292 orange[1],
00293 x,
00294 y);
00295 }
00296 else
00297 {
00298 ((float *)new)[n] = Linint2((float *)inputArr->data,
00299 orange[0],
00300 orange[1],
00301 x,
00302 y);
00303 }
00304 }
00305
00306
00307 x += step[0];
00308 n++;
00309 }
00310 y += step[1];
00311 }
00312
00313 DRMS_Array_t *segArray = drms_array_create(inputArr->type,
00314 rank,
00315 new_length,
00316 new,
00317 &status);
00318
00319 if (status != DRMS_SUCCESS)
00320 {
00321 error = kLIBASTRO_CouldntCreateData;
00322 }
00323
00324 segArray->bzero = inputArr->bzero;
00325 segArray->bscale = inputArr->bscale;
00326
00327 if (error == kLIBASTRO_Success)
00328 {
00329
00330
00331 if (bDataConverted)
00332 {
00333 drms_array_convert_inplace(saveType,
00334 inputArr->bzero,
00335 inputArr->bscale,
00336 segArray);
00337 }
00338
00339 if (*dataArr)
00340 {
00341 drms_free_array(*dataArr);
00342 *dataArr = segArray;
00343 }
00344 }
00345
00346 drms_free_array(segArrConv);
00347 }
00348 }
00349
00350 return error;
00351 }
00352
00353 float Imaginterp(DRMS_Segment_t *img, double x, double y) {
00354
00355
00356
00357
00358
00359
00360
00361
00362
00363
00364
00365
00366
00367
00368
00369
00370
00371
00372
00373
00374
00375
00376
00377
00378 double xs, ys;
00379 int cols, rows, mdim;
00380 int status;
00381
00382 cols = img->axis[0];
00383 rows = img->axis[1];
00384
00385 mdim = (cols > rows) ? cols : rows;
00386 xs = 0.5 * (x + 1.0) * mdim - 0.5;
00387 ys = 0.5 * (y + 1.0) * mdim - 0.5;
00388
00389
00390 DRMS_Array_t *segArr = drms_segment_read(img, DRMS_TYPE_FLOAT, &status);
00391
00392 if (status == DRMS_SUCCESS)
00393 {
00394 return Ccint2(segArr->data, cols, rows, xs, ys);
00395 }
00396 else
00397 {
00398 return DRMS_MISSING_FLOAT;
00399 }
00400 }