| 1 |
#include <Python.h> |
| 2 |
|
| 3 |
#include <math.h> |
| 4 |
#include <stdlib.h> |
| 5 |
#include <string.h> |
| 6 |
#include <time.h> |
| 7 |
#include <float.h> |
| 8 |
#include <stdio.h> |
| 9 |
|
| 10 |
#ifndef NPY_NO_DEPRECATED_API |
| 11 |
#define NPY_NO_DEPRECATED_API NPY_1_21_API_VERSION |
| 12 |
#endif |
| 13 |
|
| 14 |
#pragma GCC diagnostic push |
| 15 |
#pragma GCC diagnostic ignored "-Wpedantic" |
| 16 |
#include <numpy/arrayobject.h> |
| 17 |
#include <numpy/ndarrayobject.h> |
| 18 |
#include <numpy/npy_math.h> |
| 19 |
#pragma GCC diagnostic pop |
| 20 |
|
| 21 |
#include "cdrizzleblot.h" |
| 22 |
#include "cdrizzlebox.h" |
| 23 |
#include "cdrizzlemap.h" |
| 24 |
#include "cdrizzleutil.h" |
| 25 |
#include "tests/drizzletest.h" |
| 26 |
|
| 27 |
static PyObject *gl_Error; |
| 28 |
FILE *driz_log_handle = NULL; |
| 29 |
|
| 30 |
static PyArrayObject * |
| 31 |
ensure_array(PyObject *obj, int npy_type, int min_depth, int max_depth, int *is_copy) |
| 32 |
{ |
| 33 |
if (PyArray_CheckExact(obj) && PyArray_IS_C_CONTIGUOUS((PyArrayObject *) obj) && |
| 34 |
PyArray_TYPE((PyArrayObject *) obj) == npy_type) { |
| 35 |
*is_copy = 0; |
| 36 |
return (PyArrayObject *) obj; |
| 37 |
} else { |
| 38 |
*is_copy = 1; |
| 39 |
|
| 40 |
#pragma GCC diagnostic push |
| 41 |
#pragma GCC diagnostic ignored "-Wpedantic" |
| 42 |
PyArray_Descr *dtype_descr = |
| 43 |
(PyArray_Descr *) ((void *) PyArray_DescrFromType((int) npy_type)); |
| 44 |
#pragma GCC diagnostic pop |
| 45 |
|
| 46 |
if (dtype_descr == NULL) { |
| 47 |
PyErr_SetString(PyExc_TypeError, "Invalid numpy type for array conversion."); |
| 48 |
*is_copy = 0; |
| 49 |
return NULL; |
| 50 |
} |
| 51 |
|
| 52 |
#pragma GCC diagnostic push |
| 53 |
#pragma GCC diagnostic ignored "-Wpedantic" |
| 54 |
return (PyArrayObject *) PyArray_FromAny( |
| 55 |
obj, dtype_descr, min_depth, max_depth, NPY_ARRAY_DEFAULT | NPY_ARRAY_ENSUREARRAY, |
| 56 |
NULL); |
| 57 |
#pragma GCC diagnostic pop |
| 58 |
} |
| 59 |
} |
| 60 |
|
| 61 |
static int |
| 62 |
process_array_list( |
| 63 |
PyObject *list, integer_t *nx, integer_t *ny, const char *name, PyArrayObject ***arrays, |
| 64 |
int *nmax, int allow_none, int *n_none, int **free_arrays, struct driz_error_t *error) |
| 65 |
{ |
| 66 |
npy_intp *ndim; |
| 67 |
int inx, iny; |
| 68 |
PyObject *list_elem = NULL; |
| 69 |
PyArrayObject **arr_list = NULL; |
| 70 |
PyArrayObject *arr = NULL; |
| 71 |
int i, at_least_one; |
| 72 |
int cpy; |
| 73 |
|
| 74 |
*arrays = NULL; |
| 75 |
*free_arrays = NULL; |
| 76 |
*nmax = 0; |
| 77 |
if (n_none) { |
| 78 |
*n_none = 0; |
| 79 |
} |
| 80 |
|
| 81 |
if (list == NULL || list == Py_None) { |
| 82 |
driz_error_set(error, PyExc_ValueError, "Array list '%s' is None or NULL.", name); |
| 83 |
return 1; |
| 84 |
} |
| 85 |
|
| 86 |
if (PyArray_CheckExact(list)) { |
| 87 |
if (!(arr_list = (PyArrayObject **) malloc(sizeof(PyArrayObject *)))) { |
| 88 |
driz_error_set(error, PyExc_MemoryError, "Memory allocation failed."); |
| 89 |
return 1; |
| 90 |
} |
| 91 |
if (!(*free_arrays = (int *) malloc(sizeof(int)))) { |
| 92 |
driz_error_set(error, PyExc_MemoryError, "Memory allocation failed."); |
| 93 |
return 1; |
| 94 |
} |
| 95 |
arr = ensure_array(list, NPY_FLOAT, 2, 2, &cpy); |
| 96 |
(*free_arrays)[0] = cpy; |
| 97 |
if (!arr) { |
| 98 |
driz_error_set(error, PyExc_ValueError, "Invalid '%s' array.", name); |
| 99 |
free(arr_list); |
| 100 |
return 1; |
| 101 |
} |
| 102 |
*arr_list = arr; |
| 103 |
*nmax = 1; |
| 104 |
*n_none = 0; |
| 105 |
ndim = PyArray_DIMS(arr); |
| 106 |
*nx = (int) ndim[1]; |
| 107 |
*ny = (int) ndim[0]; |
| 108 |
*arrays = arr_list; |
| 109 |
return 0; |
| 110 |
|
| 111 |
} else if (!PyList_Check(list) && !PyTuple_Check(list)) { |
| 112 |
driz_error_set( |
| 113 |
error, PyExc_TypeError, |
| 114 |
"Argument '%s' is not a list or a tuple of numpy.ndarray or None.", name); |
| 115 |
} |
| 116 |
|
| 117 |
at_least_one = 0; |
| 118 |
|
| 119 |
if (!(*nmax = PySequence_Size(list))) { |
| 120 |
return 0; |
| 121 |
} |
| 122 |
|
| 123 |
arr_list = (PyArrayObject **) calloc(*nmax, sizeof(PyArrayObject *)); |
| 124 |
if (!arr_list) { |
| 125 |
driz_error_set(error, PyExc_MemoryError, "Memory allocation failed."); |
| 126 |
return 1; |
| 127 |
} |
| 128 |
if (!(*free_arrays = (int *) calloc(*nmax, sizeof(int)))) { |
| 129 |
driz_error_set(error, PyExc_MemoryError, "Memory allocation failed."); |
| 130 |
return 1; |
| 131 |
} |
| 132 |
|
| 133 |
for (i = 0; i < *nmax; ++i) { |
| 134 |
if (!(list_elem = PySequence_GetItem(list, i))) { |
| 135 |
driz_error_set( |
| 136 |
error, PyExc_RuntimeError, "Error retrieving array %d from the '%s' list.", i, |
| 137 |
name); |
| 138 |
goto _exit_on_err; |
| 139 |
} |
| 140 |
|
| 141 |
if (list_elem == Py_None) { |
| 142 |
if (allow_none) { |
| 143 |
if (n_none) { |
| 144 |
(*n_none)++; |
| 145 |
} |
| 146 |
arr_list[i] = NULL; |
| 147 |
(*free_arrays)[i] = 0; |
| 148 |
Py_XDECREF(list_elem); |
| 149 |
continue; |
| 150 |
} else { |
| 151 |
*n_none = 1; |
| 152 |
driz_error_set( |
| 153 |
error, PyExc_ValueError, |
| 154 |
"Element %d of '%s' list is None which is not allowed.", i, name); |
| 155 |
goto _exit_on_err; |
| 156 |
} |
| 157 |
} else { |
| 158 |
arr = ensure_array(list_elem, NPY_FLOAT, 2, 2, &cpy); |
| 159 |
if (!arr) { |
| 160 |
driz_error_set( |
| 161 |
error, PyExc_ValueError, "Invalid array in '%s' at position %d.", name, i); |
| 162 |
goto _exit_on_err; |
| 163 |
} |
| 164 |
(*free_arrays)[i] = cpy; |
| 165 |
at_least_one = 1; |
| 166 |
} |
| 167 |
Py_XDECREF(list_elem); |
| 168 |
|
| 169 |
arr_list[i] = arr; |
| 170 |
|
| 171 |
if (*nx < 0) { |
| 172 |
ndim = PyArray_DIMS(arr); |
| 173 |
*nx = (int) ndim[1]; |
| 174 |
*ny = (int) ndim[0]; |
| 175 |
} else { |
| 176 |
ndim = PyArray_DIMS(arr); |
| 177 |
inx = (int) ndim[1]; |
| 178 |
iny = (int) ndim[0]; |
| 179 |
if ((*nx != inx) || (*ny != iny)) { |
| 180 |
driz_error_set( |
| 181 |
error, PyExc_ValueError, "Inconsistent image shape in the '%s' image list.", |
| 182 |
name); |
| 183 |
goto _exit_on_err; |
| 184 |
} |
| 185 |
} |
| 186 |
} |
| 187 |
|
| 188 |
if (!at_least_one && !allow_none) { |
| 189 |
free(arr_list); |
| 190 |
free(*free_arrays); |
| 191 |
arr_list = NULL; |
| 192 |
*nmax = 0; |
| 193 |
} |
| 194 |
|
| 195 |
*arrays = arr_list; |
| 196 |
return 0; |
| 197 |
|
| 198 |
_exit_on_err: |
| 199 |
|
| 200 |
Py_XDECREF(list_elem); |
| 201 |
if (arr_list) { |
| 202 |
for (i = 0; i < *nmax; ++i) { |
| 203 |
if (free_arrays && (*free_arrays)[i]) { |
| 204 |
Py_XDECREF(arr_list[i]); |
| 205 |
} |
| 206 |
} |
| 207 |
free(arr_list); |
| 208 |
} |
| 209 |
if (*free_arrays) { |
| 210 |
free(*free_arrays); |
| 211 |
} |
| 212 |
return 1; |
| 213 |
} |
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
static PyObject * |
| 219 |
tdriz(PyObject *self, PyObject *args, PyObject *keywords) |
| 220 |
{ |
| 221 |
(void) self; |
| 222 |
|
| 223 |
const char *kwlist[] = { |
| 224 |
"input", "weights", "pixmap", "output", "counts", "context", "input2", |
| 225 |
"output2", "dq", "outdq", "uniqid", "xmin", "xmax", "ymin", |
| 226 |
"ymax", "iscale", "pscale_ratio", "scale", "pixfrac", "kernel", "in_units", |
| 227 |
"expscale", "wtscale", "fillstr", "fillstr2", NULL}; |
| 228 |
|
| 229 |
|
| 230 |
PyObject *oimg, *owei, *pixmap, *oout, *owht, *ocon, *odq = NULL; |
| 231 |
PyObject *oimg2 = NULL, *oout2 = NULL, *ooutdq = NULL, *opscale_ratio = NULL, *oscale = NULL; |
| 232 |
int i, n_none; |
| 233 |
int nsq_args, nsq_arr = 0, nsq_arr_out = 0; |
| 234 |
int *free_arrays2 = NULL, *free_out_arrays2 = NULL; |
| 235 |
|
| 236 |
integer_t uniqid = 1; |
| 237 |
integer_t xmin = 0; |
| 238 |
integer_t xmax = 0; |
| 239 |
integer_t ymin = 0; |
| 240 |
integer_t ymax = 0; |
| 241 |
float iscale = 1.0f; |
| 242 |
float pscale_ratio = NPY_NANF; |
| 243 |
float scale = NPY_NANF; |
| 244 |
double pfract = 1.0; |
| 245 |
char *kernel_str = "square"; |
| 246 |
char *inun_str = "cps"; |
| 247 |
float expin = 1.0; |
| 248 |
float wtscl = 1.0; |
| 249 |
char *fillstr = "INDEF"; |
| 250 |
char *fillstr2 = "INDEF"; |
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
PyArrayObject *img = NULL, *wei = NULL, *out = NULL, *wht = NULL, *con = NULL, *map = NULL, |
| 255 |
*dq = NULL, *outdq = NULL; |
| 256 |
|
| 257 |
int free_img = 0, free_wei = 0, free_out = 0, free_wht = 0; |
| 258 |
int free_con = 0, free_map = 0, free_dq = 0, free_outdq = 0; |
| 259 |
|
| 260 |
PyArrayObject **img2_list = NULL, **out2_list = NULL; |
| 261 |
|
| 262 |
enum e_kernel_t kernel; |
| 263 |
enum e_unit_t inun; |
| 264 |
char *fillstr_end; |
| 265 |
bool_t do_fill, do_fill2; |
| 266 |
float fill_value, fill_value2; |
| 267 |
struct driz_error_t error; |
| 268 |
struct driz_param_t p; |
| 269 |
integer_t size[2]; |
| 270 |
integer_t nx, ny; |
| 271 |
integer_t inx, iny; |
| 272 |
integer_t onx, ony; |
| 273 |
npy_intp *ndim; |
| 274 |
char warn_msg[128]; |
| 275 |
|
| 276 |
driz_log_handle = driz_log_init(driz_log_handle); |
| 277 |
driz_log_message("starting tdriz"); |
| 278 |
driz_error_init(&error); |
| 279 |
|
| 280 |
if (!PyArg_ParseTupleAndKeywords( |
| 281 |
args, keywords, "OOOOOO|OOOOiiiiifOOdssffss:tdriz", (char **) kwlist, &oimg, &owei, |
| 282 |
&pixmap, &oout, &owht, &ocon, |
| 283 |
&oimg2, &oout2, &odq, &ooutdq, |
| 284 |
&uniqid, &xmin, &xmax, &ymin, &ymax, |
| 285 |
&iscale, &opscale_ratio, &oscale, |
| 286 |
&pfract, &kernel_str, &inun_str, |
| 287 |
&expin, &wtscl, &fillstr, &fillstr2) |
| 288 |
) { |
| 289 |
return NULL; |
| 290 |
} |
| 291 |
|
| 292 |
if (oscale != NULL && oscale != Py_None) { |
| 293 |
scale = (float) PyFloat_AsDouble(oscale); |
| 294 |
if (PyErr_Occurred()) { |
| 295 |
driz_error_set_message(&error, "Argument 'scale' is not a number."); |
| 296 |
goto _exit; |
| 297 |
} |
| 298 |
if (scale <= 0.0f || !isfinite(scale)) { |
| 299 |
driz_error_set_message(&error, "Argument 'scale' must be positive and finite."); |
| 300 |
goto _exit; |
| 301 |
} |
| 302 |
iscale = scale * scale; |
| 303 |
pscale_ratio = scale; |
| 304 |
if (py_warning( |
| 305 |
PyExc_DeprecationWarning, |
| 306 |
"Argument 'scale' has been deprecated since version 3.0 " |
| 307 |
"and it will be removed in a future release. " |
| 308 |
"Use 'iscale' and 'pscale_ratio' instead and set " |
| 309 |
"iscale=pscale_ratio**2 to achieve the same effect as with " |
| 310 |
"'scale'.") != 0) { |
| 311 |
goto _exit; |
| 312 |
} |
| 313 |
|
| 314 |
} else { |
| 315 |
if (iscale <= 0.0f || !isfinite(iscale)) { |
| 316 |
driz_error_set_message(&error, "Argument 'iscale' must be positive and finite."); |
| 317 |
goto _exit; |
| 318 |
} |
| 319 |
|
| 320 |
if (opscale_ratio != NULL && opscale_ratio != Py_None) { |
| 321 |
pscale_ratio = (float) PyFloat_AsDouble(opscale_ratio); |
| 322 |
if (PyErr_Occurred()) { |
| 323 |
driz_error_set_message(&error, "Argument 'pscale_ratio' is not a number."); |
| 324 |
goto _exit; |
| 325 |
} |
| 326 |
if (pscale_ratio <= 0.0f || !isfinite(pscale_ratio)) { |
| 327 |
driz_error_set_message( |
| 328 |
&error, "Argument 'pscale_ratio' must be positive and finite."); |
| 329 |
goto _exit; |
| 330 |
} |
| 331 |
} |
| 332 |
} |
| 333 |
|
| 334 |
|
| 335 |
img = ensure_array(oimg, NPY_FLOAT, 2, 2, &free_img); |
| 336 |
if (!img) { |
| 337 |
driz_error_set_message(&error, "Invalid input array"); |
| 338 |
goto _exit; |
| 339 |
} |
| 340 |
|
| 341 |
wei = ensure_array(owei, NPY_FLOAT, 2, 2, &free_wei); |
| 342 |
if (!wei) { |
| 343 |
driz_error_set_message(&error, "Invalid weights array"); |
| 344 |
goto _exit; |
| 345 |
} |
| 346 |
|
| 347 |
map = ensure_array(pixmap, NPY_DOUBLE, 3, 3, &free_map); |
| 348 |
if (!map) { |
| 349 |
driz_error_set_message(&error, "Invalid pixmap array"); |
| 350 |
goto _exit; |
| 351 |
} |
| 352 |
|
| 353 |
out = ensure_array(oout, NPY_FLOAT, 2, 2, &free_out); |
| 354 |
if (!out) { |
| 355 |
driz_error_set_message(&error, "Invalid output array"); |
| 356 |
goto _exit; |
| 357 |
} |
| 358 |
|
| 359 |
wht = ensure_array(owht, NPY_FLOAT, 2, 2, &free_wht); |
| 360 |
if (!wht) { |
| 361 |
driz_error_set_message(&error, "Invalid counts array"); |
| 362 |
goto _exit; |
| 363 |
} |
| 364 |
|
| 365 |
if (ocon == Py_None) { |
| 366 |
con = NULL; |
| 367 |
} else { |
| 368 |
con = ensure_array(ocon, NPY_INT32, 2, 2, &free_con); |
| 369 |
if (!con) { |
| 370 |
driz_error_set_message(&error, "Invalid context array"); |
| 371 |
goto _exit; |
| 372 |
} |
| 373 |
} |
| 374 |
|
| 375 |
if (odq == Py_None || odq == NULL) { |
| 376 |
dq = NULL; |
| 377 |
} else { |
| 378 |
dq = ensure_array(odq, NPY_UINT32, 2, 2, &free_dq); |
| 379 |
if (!dq) { |
| 380 |
driz_error_set_message(&error, "Invalid input DQ array"); |
| 381 |
goto _exit; |
| 382 |
} |
| 383 |
} |
| 384 |
|
| 385 |
if (ooutdq == Py_None || ooutdq == NULL) { |
| 386 |
if (dq != NULL) { |
| 387 |
driz_error_set_message(&error, "When 'dq' is provided, 'outdq' must also be provided."); |
| 388 |
goto _exit; |
| 389 |
} |
| 390 |
outdq = NULL; |
| 391 |
} else { |
| 392 |
outdq = ensure_array(ooutdq, NPY_UINT32, 2, 2, &free_outdq); |
| 393 |
if (!outdq) { |
| 394 |
driz_error_set_message(&error, "Invalid output DQ array"); |
| 395 |
goto _exit; |
| 396 |
} |
| 397 |
} |
| 398 |
|
| 399 |
|
| 400 |
if (fillstr == NULL || *fillstr == 0 || strncmp(fillstr, "INDEF", 6) == 0 || |
| 401 |
strncmp(fillstr, "indef", 6) == 0) { |
| 402 |
do_fill = 0; |
| 403 |
fill_value = 0.0; |
| 404 |
|
| 405 |
} else if (strncmp(fillstr, "NaN", 4) == 0 || strncmp(fillstr, "nan", 4) == 0) { |
| 406 |
do_fill = 1; |
| 407 |
fill_value = NPY_NANF; |
| 408 |
|
| 409 |
} else { |
| 410 |
do_fill = 1; |
| 411 |
#ifdef _WIN32 |
| 412 |
fill_value = atof(fillstr); |
| 413 |
#else |
| 414 |
fill_value = strtof(fillstr, &fillstr_end); |
| 415 |
if (fillstr == fillstr_end || *fillstr_end != '\0') { |
| 416 |
driz_error_set_message(&error, "Illegal fill value"); |
| 417 |
goto _exit; |
| 418 |
} |
| 419 |
#endif |
| 420 |
} |
| 421 |
|
| 422 |
if (fillstr2 == NULL || *fillstr2 == 0 || strncmp(fillstr2, "INDEF", 6) == 0 || |
| 423 |
strncmp(fillstr2, "indef", 6) == 0) { |
| 424 |
do_fill2 = 0; |
| 425 |
fill_value2 = 0.0; |
| 426 |
|
| 427 |
} else if (strncmp(fillstr2, "NaN", 4) == 0 || strncmp(fillstr2, "nan", 4) == 0) { |
| 428 |
do_fill2 = 1; |
| 429 |
fill_value2 = NPY_NANF; |
| 430 |
|
| 431 |
} else { |
| 432 |
do_fill2 = 1; |
| 433 |
#ifdef _WIN32 |
| 434 |
fill_value2 = atof(fillstr2); |
| 435 |
#else |
| 436 |
fill_value2 = strtof(fillstr2, &fillstr_end); |
| 437 |
if (fillstr2 == fillstr_end || *fillstr_end != '\0') { |
| 438 |
driz_error_set_message(&error, "Illegal fill value"); |
| 439 |
goto _exit; |
| 440 |
} |
| 441 |
#endif |
| 442 |
} |
| 443 |
|
| 444 |
|
| 445 |
ndim = PyArray_DIMS(img); |
| 446 |
inx = ndim[1]; |
| 447 |
iny = ndim[0]; |
| 448 |
|
| 449 |
ndim = PyArray_DIMS(map); |
| 450 |
size[0] = (integer_t) ndim[1]; |
| 451 |
size[1] = (integer_t) ndim[0]; |
| 452 |
|
| 453 |
if (size[0] != inx || size[1] != iny) { |
| 454 |
if (snprintf( |
| 455 |
warn_msg, 128, |
| 456 |
"Pixel map dimensions (%d, %d) != input dimensions " |
| 457 |
"(%d, %d).", |
| 458 |
size[0], size[1], inx, iny) < 1) { |
| 459 |
strcpy(warn_msg, "Pixel map dimensions != input dimensions."); |
| 460 |
} |
| 461 |
driz_error_set_message(&error, warn_msg); |
| 462 |
goto _exit; |
| 463 |
} |
| 464 |
if (ndim[2] != 2) { |
| 465 |
driz_error_set_message(&error, "Pixel map depth (3rd dim) must be 2."); |
| 466 |
goto _exit; |
| 467 |
} |
| 468 |
|
| 469 |
if (wei) { |
| 470 |
get_dimensions(wei, size); |
| 471 |
if (size[0] != inx || size[1] != iny) { |
| 472 |
if (snprintf( |
| 473 |
warn_msg, 128, |
| 474 |
"Weights array dimensions (%d, %d) != input " |
| 475 |
"dimensions (%d, %d).", |
| 476 |
size[0], size[1], inx, iny) < 1) { |
| 477 |
strcpy(warn_msg, "Weights array dimensions != input dimensions."); |
| 478 |
} |
| 479 |
driz_error_set_message(&error, warn_msg); |
| 480 |
goto _exit; |
| 481 |
} |
| 482 |
} |
| 483 |
|
| 484 |
|
| 485 |
ndim = PyArray_DIMS(out); |
| 486 |
onx = ndim[1]; |
| 487 |
ony = ndim[0]; |
| 488 |
|
| 489 |
get_dimensions(wht, size); |
| 490 |
if (size[0] != onx || size[1] != ony) { |
| 491 |
if (snprintf( |
| 492 |
warn_msg, 128, |
| 493 |
"Output weight dimensions (%d, %d) != output dimensions " |
| 494 |
"(%d, %d).", |
| 495 |
size[0], size[1], onx, ony) < 1) { |
| 496 |
strcpy(warn_msg, "Output weight dimensions != output dimensions."); |
| 497 |
} |
| 498 |
|
| 499 |
driz_error_set_message(&error, warn_msg); |
| 500 |
goto _exit; |
| 501 |
} |
| 502 |
|
| 503 |
if (con) { |
| 504 |
get_dimensions(con, size); |
| 505 |
if (size[0] != onx || size[1] != ony) { |
| 506 |
if (snprintf( |
| 507 |
warn_msg, 128, |
| 508 |
"Context dimensions (%d, %d) != output dimensions " |
| 509 |
"(%d, %d).", |
| 510 |
size[0], size[1], onx, ony) < 1) { |
| 511 |
strcpy(warn_msg, "Context dimensions != output dimensions."); |
| 512 |
} |
| 513 |
driz_error_set_message(&error, warn_msg); |
| 514 |
goto _exit; |
| 515 |
} |
| 516 |
} |
| 517 |
|
| 518 |
|
| 519 |
|
| 520 |
nsq_args = |
| 521 |
((int) (oimg2 != NULL && oimg2 != Py_None)) + ((int) (oout2 != NULL && oout2 != Py_None)); |
| 522 |
if (nsq_args == 2) { |
| 523 |
nx = inx; |
| 524 |
ny = iny; |
| 525 |
if (process_array_list( |
| 526 |
oimg2, &nx, &ny, "input2", &img2_list, &nsq_arr, 1, &n_none, &free_arrays2, |
| 527 |
&error)) { |
| 528 |
goto _exit; |
| 529 |
} |
| 530 |
if (n_none == nsq_arr && img2_list) { |
| 531 |
free(img2_list); |
| 532 |
nsq_arr = 0; |
| 533 |
} |
| 534 |
|
| 535 |
if (nsq_arr) { |
| 536 |
if (nx != inx || ny != iny) { |
| 537 |
driz_error_set_message( |
| 538 |
&error, "'input2' arrays must have the same " |
| 539 |
"dimensions as the 'input' array."); |
| 540 |
goto _exit; |
| 541 |
} |
| 542 |
|
| 543 |
nx = onx; |
| 544 |
ny = ony; |
| 545 |
if (process_array_list( |
| 546 |
oout2, &nx, &ny, "output2", &out2_list, &nsq_arr_out, 0, NULL, |
| 547 |
&free_out_arrays2, &error)) { |
| 548 |
goto _exit; |
| 549 |
} |
| 550 |
if (nx != onx || ny != ony) { |
| 551 |
driz_error_set_message( |
| 552 |
&error, "'output2' arrays must have the same " |
| 553 |
"dimensions as the 'output' array."); |
| 554 |
goto _exit; |
| 555 |
} |
| 556 |
|
| 557 |
if (nsq_arr != nsq_arr_out) { |
| 558 |
driz_error_set_message( |
| 559 |
&error, "The number of 'output2' arrays must match " |
| 560 |
"the number of 'input2' arrays."); |
| 561 |
goto _exit; |
| 562 |
} |
| 563 |
} |
| 564 |
} else if (nsq_args == 1) { |
| 565 |
driz_error_set_message( |
| 566 |
&error, "'input2' and 'output2' must both be either None, " |
| 567 |
"numpy.ndarray, or lists of numpy.ndarray of equal lengths."); |
| 568 |
goto _exit; |
| 569 |
} |
| 570 |
|
| 571 |
|
| 572 |
if (xmax == 0 || xmax >= inx) { |
| 573 |
xmax = inx - 1; |
| 574 |
} |
| 575 |
if (ymax == 0 || ymax >= iny) { |
| 576 |
ymax = iny - 1; |
| 577 |
} |
| 578 |
|
| 579 |
if (shrink_image_section(map, &xmin, &xmax, &ymin, &ymax)) { |
| 580 |
driz_error_set_message(&error, "No or too few valid pixels in the pixel map."); |
| 581 |
goto _exit; |
| 582 |
} |
| 583 |
|
| 584 |
|
| 585 |
if (kernel_str2enum(kernel_str, &kernel, &error) || unit_str2enum(inun_str, &inun, &error)) { |
| 586 |
goto _exit; |
| 587 |
} |
| 588 |
|
| 589 |
if (kernel == kernel_gaussian || kernel == kernel_lanczos2 || kernel == kernel_lanczos3) { |
| 590 |
if (snprintf(warn_msg, 128, "Kernel '%s' is not a flux-conserving kernel.", kernel_str) < |
| 591 |
1) { |
| 592 |
strcpy(warn_msg, "Selected kernel is not a flux-conserving kernel."); |
| 593 |
} |
| 594 |
PyErr_WarnEx(PyExc_Warning, warn_msg, 1); |
| 595 |
} |
| 596 |
|
| 597 |
if (pfract <= 0.001) { |
| 598 |
if (snprintf( |
| 599 |
warn_msg, 128, |
| 600 |
"Kernel reset to 'point' due to input 'pixfrac' " |
| 601 |
" being too small.") < 1) { |
| 602 |
strcpy( |
| 603 |
warn_msg, "Kernel reset to 'point' due to input 'pixfrac' " |
| 604 |
" being too small."); |
| 605 |
} |
| 606 |
PyErr_WarnEx(PyExc_Warning, warn_msg, 1); |
| 607 |
kernel_str2enum("point", &kernel, &error); |
| 608 |
} |
| 609 |
|
| 610 |
|
| 611 |
if (inun != unit_cps) { |
| 612 |
iscale /= expin; |
| 613 |
} |
| 614 |
|
| 615 |
|
| 616 |
driz_param_init(&p); |
| 617 |
|
| 618 |
p.data = img; |
| 619 |
p.dq = dq; |
| 620 |
p.weights = wei; |
| 621 |
p.pixmap = map; |
| 622 |
p.output_data = out; |
| 623 |
p.output_counts = wht; |
| 624 |
p.output_context = con; |
| 625 |
p.uuid = uniqid; |
| 626 |
p.xmin = xmin; |
| 627 |
p.ymin = ymin; |
| 628 |
p.xmax = xmax; |
| 629 |
p.ymax = ymax; |
| 630 |
p.iscale = iscale; |
| 631 |
p.pscale_ratio = pscale_ratio; |
| 632 |
p.pixel_fraction = pfract; |
| 633 |
p.kernel = kernel; |
| 634 |
p.in_units = inun; |
| 635 |
p.exposure_time = expin; |
| 636 |
p.weight_scale = wtscl; |
| 637 |
p.fill_value = fill_value; |
| 638 |
p.fill_value2 = fill_value2; |
| 639 |
p.in_nx = inx; |
| 640 |
p.in_ny = iny; |
| 641 |
p.out_nx = onx; |
| 642 |
p.out_ny = ony; |
| 643 |
p.data2 = img2_list; |
| 644 |
p.output_data2 = out2_list; |
| 645 |
p.ndata2 = nsq_arr; |
| 646 |
p.output_dq = outdq; |
| 647 |
p.error = &error; |
| 648 |
|
| 649 |
if (driz_error_check(&error, "xmin must be >= 0", p.xmin >= 0)) { |
| 650 |
goto _exit; |
| 651 |
} |
| 652 |
if (driz_error_check(&error, "ymin must be >= 0", p.ymin >= 0)) { |
| 653 |
goto _exit; |
| 654 |
} |
| 655 |
if (driz_error_check(&error, "xmax must be > xmin", p.xmax > p.xmin)) { |
| 656 |
goto _exit; |
| 657 |
} |
| 658 |
if (driz_error_check(&error, "ymax must be > ymin", p.ymax > p.ymin)) { |
| 659 |
goto _exit; |
| 660 |
} |
| 661 |
if (driz_error_check(&error, "exposure time must be > 0", p.exposure_time > 0.0f)) { |
| 662 |
goto _exit; |
| 663 |
} |
| 664 |
if (driz_error_check(&error, "weight scale must be > 0", p.weight_scale > 0.0f)) { |
| 665 |
goto _exit; |
| 666 |
} |
| 667 |
|
| 668 |
if (dobox(&p)) { |
| 669 |
goto _exit; |
| 670 |
} |
| 671 |
|
| 672 |
|
| 673 |
if (do_fill) { |
| 674 |
put_fill(&p, do_fill, do_fill2); |
| 675 |
} |
| 676 |
|
| 677 |
_exit: |
| 678 |
driz_log_message("ending tdriz"); |
| 679 |
driz_log_close(driz_log_handle); |
| 680 |
if (free_con) { |
| 681 |
Py_XDECREF(con); |
| 682 |
} |
| 683 |
if (free_img) { |
| 684 |
Py_XDECREF(img); |
| 685 |
} |
| 686 |
if (free_wei) { |
| 687 |
Py_XDECREF(wei); |
| 688 |
} |
| 689 |
if (free_out) { |
| 690 |
Py_XDECREF(out); |
| 691 |
} |
| 692 |
if (free_wht) { |
| 693 |
Py_XDECREF(wht); |
| 694 |
} |
| 695 |
if (free_map) { |
| 696 |
Py_XDECREF(map); |
| 697 |
} |
| 698 |
if (free_dq) { |
| 699 |
Py_XDECREF(dq); |
| 700 |
} |
| 701 |
if (free_outdq) { |
| 702 |
Py_XDECREF(outdq); |
| 703 |
} |
| 704 |
|
| 705 |
if (nsq_arr > 0 && img2_list) { |
| 706 |
for (i = 0; i < nsq_arr; ++i) { |
| 707 |
if (free_arrays2[i]) { |
| 708 |
Py_XDECREF(img2_list[i]); |
| 709 |
} |
| 710 |
} |
| 711 |
free(img2_list); |
| 712 |
free(free_arrays2); |
| 713 |
} |
| 714 |
|
| 715 |
if (nsq_arr_out > 0 && out2_list) { |
| 716 |
for (i = 0; i < nsq_arr_out; ++i) { |
| 717 |
if (free_out_arrays2[i]) { |
| 718 |
Py_XDECREF(out2_list[i]); |
| 719 |
} |
| 720 |
} |
| 721 |
free(out2_list); |
| 722 |
free(free_out_arrays2); |
| 723 |
} |
| 724 |
|
| 725 |
if (driz_error_is_set(&error)) { |
| 726 |
if (error.type == NULL) { |
| 727 |
error.type = PyExc_ValueError; |
| 728 |
} |
| 729 |
PyErr_SetString(error.type, driz_error_get_message(&error)); |
| 730 |
return NULL; |
| 731 |
} else { |
| 732 |
return Py_BuildValue("sii", "Callable C-based DRIZZLE Version 2.1.0", p.nmiss, p.nskip); |
| 733 |
} |
| 734 |
} |
| 735 |
|
| 736 |
|
| 737 |
|
| 738 |
|
| 739 |
|
| 740 |
static PyObject * |
| 741 |
tblot(PyObject *self, PyObject *args, PyObject *keywords) |
| 742 |
{ |
| 743 |
(void) self; |
| 744 |
|
| 745 |
const char *kwlist[] = {"source", "pixmap", "output", "xmin", "xmax", "ymin", |
| 746 |
"ymax", "iscale", "kscale", "scale", "interp", "exptime", |
| 747 |
"fillval", "misval", "sinscl", NULL}; |
| 748 |
|
| 749 |
|
| 750 |
PyObject *oimg, *pixmap, *oout; |
| 751 |
PyObject *oscale = NULL, *okscale = NULL, *oiscale = NULL; |
| 752 |
PyObject *oef = NULL, *omisval = NULL, *ofillval = NULL; |
| 753 |
long xmin = 0; |
| 754 |
long xmax = 0; |
| 755 |
long ymin = 0; |
| 756 |
long ymax = 0; |
| 757 |
float scale = 1.0f; |
| 758 |
float iscale = 1.0f; |
| 759 |
char *interp_str = "poly5"; |
| 760 |
float ef = 1.0f; |
| 761 |
float fillval; |
| 762 |
float sinscl = 1.0f; |
| 763 |
|
| 764 |
PyArrayObject *img = NULL, *out = NULL, *map = NULL; |
| 765 |
enum e_interp_t interp; |
| 766 |
int istat = 0; |
| 767 |
struct driz_error_t error; |
| 768 |
struct driz_param_t p; |
| 769 |
integer_t psize[2], osize[2]; |
| 770 |
char warn_msg[128]; |
| 771 |
int free_img = 0, free_out = 0, free_map = 0; |
| 772 |
|
| 773 |
driz_log_handle = driz_log_init(driz_log_handle); |
| 774 |
driz_log_message("starting tblot"); |
| 775 |
driz_error_init(&error); |
| 776 |
|
| 777 |
if (!PyArg_ParseTupleAndKeywords( |
| 778 |
args, keywords, "OOO|llllOOOsOOOf:tblot", (char **) kwlist, |
| 779 |
&oimg, &pixmap, &oout, |
| 780 |
&xmin, &xmax, &ymin, &ymax, |
| 781 |
&oiscale, &okscale, &oscale, &interp_str, &oef, |
| 782 |
&ofillval, &omisval, &sinscl) |
| 783 |
) { |
| 784 |
return NULL; |
| 785 |
} |
| 786 |
|
| 787 |
if (oscale != NULL && !Py_IsNone(oscale)) { |
| 788 |
scale = (float) PyFloat_AsDouble(oscale); |
| 789 |
|
| 790 |
if (PyErr_Occurred()) { |
| 791 |
driz_error_set_message(&error, "Argument 'scale' is not a number."); |
| 792 |
goto _exit; |
| 793 |
} |
| 794 |
|
| 795 |
if (scale <= 0.0f || !isfinite(scale)) { |
| 796 |
driz_error_set_message(&error, "Argument 'scale' must be positive and finite."); |
| 797 |
goto _exit; |
| 798 |
} |
| 799 |
iscale = 1.0 / (scale * scale); |
| 800 |
|
| 801 |
if (py_warning( |
| 802 |
PyExc_DeprecationWarning, "Argument 'scale' is deprecated, use 'iscale' " |
| 803 |
"instead and set it to 1.0 / (scale*scale).") != 0) { |
| 804 |
goto _exit; |
| 805 |
} |
| 806 |
|
| 807 |
} else { |
| 808 |
if (oiscale == NULL || Py_IsNone(oiscale)) { |
| 809 |
iscale = 1.0f; |
| 810 |
} else { |
| 811 |
iscale = (float) PyFloat_AsDouble(oiscale); |
| 812 |
|
| 813 |
if (PyErr_Occurred()) { |
| 814 |
driz_error_set_message(&error, "Argument 'iscale' is not a number."); |
| 815 |
goto _exit; |
| 816 |
} |
| 817 |
} |
| 818 |
if (iscale <= 0.0f || !isfinite(iscale)) { |
| 819 |
driz_error_set_message(&error, "Argument 'iscale' must be positive and finite."); |
| 820 |
goto _exit; |
| 821 |
} |
| 822 |
} |
| 823 |
|
| 824 |
if (okscale != NULL && !Py_IsNone(okscale)) { |
| 825 |
if (py_warning( |
| 826 |
PyExc_DeprecationWarning, |
| 827 |
"Argument 'kscale' has been deprecated and it will be " |
| 828 |
"removed in a future version. It is no longer used by " |
| 829 |
"the blotting algorithm and can be safely ignored.") != 0) { |
| 830 |
goto _exit; |
| 831 |
} |
| 832 |
} |
| 833 |
|
| 834 |
if (omisval != NULL && !Py_IsNone(omisval)) { |
| 835 |
if (py_warning( |
| 836 |
PyExc_DeprecationWarning, |
| 837 |
"Argument 'misval' has been deprecated and has been " |
| 838 |
"replaced by 'fillval' to achieve the same effect.") != 0) { |
| 839 |
goto _exit; |
| 840 |
} |
| 841 |
|
| 842 |
fillval = (float) PyFloat_AsDouble(omisval); |
| 843 |
|
| 844 |
if (ofillval != NULL && !Py_IsNone(ofillval)) { |
| 845 |
driz_error_set_message( |
| 846 |
&error, "Argument 'fillval' should not be set when 'misval' is set."); |
| 847 |
goto _exit; |
| 848 |
} |
| 849 |
} else { |
| 850 |
if (ofillval != NULL && !Py_IsNone(ofillval)) { |
| 851 |
fillval = (float) PyFloat_AsDouble(ofillval); |
| 852 |
|
| 853 |
if (PyErr_Occurred()) { |
| 854 |
driz_error_set_message(&error, "Argument 'fillval' is not a number."); |
| 855 |
goto _exit; |
| 856 |
} |
| 857 |
} else { |
| 858 |
fillval = 0.0f; |
| 859 |
} |
| 860 |
} |
| 861 |
|
| 862 |
if (oef != NULL && !Py_IsNone(oef)) { |
| 863 |
if (py_warning( |
| 864 |
PyExc_DeprecationWarning, "Argument 'exptime' has been deprecated and it will be " |
| 865 |
"removed in a future version. Use 'iscale' to achieve " |
| 866 |
"the same.") != 0) { |
| 867 |
goto _exit; |
| 868 |
} |
| 869 |
|
| 870 |
ef = (float) PyFloat_AsDouble(oef); |
| 871 |
|
| 872 |
if (PyErr_Occurred()) { |
| 873 |
driz_error_set_message(&error, "Argument 'exptime' is not a number."); |
| 874 |
goto _exit; |
| 875 |
} |
| 876 |
if (ef <= 0.0f || !isfinite(ef)) { |
| 877 |
driz_error_set_message(&error, "Argument 'exptime' must be positive and finite."); |
| 878 |
goto _exit; |
| 879 |
} |
| 880 |
} else { |
| 881 |
ef = 1.0f; |
| 882 |
} |
| 883 |
|
| 884 |
img = ensure_array(oimg, NPY_FLOAT, 2, 2, &free_img); |
| 885 |
if (!img) { |
| 886 |
driz_error_set_message(&error, "Invalid input array"); |
| 887 |
goto _exit; |
| 888 |
} |
| 889 |
|
| 890 |
map = ensure_array(pixmap, NPY_DOUBLE, 3, 3, &free_map); |
| 891 |
if (!map) { |
| 892 |
driz_error_set_message(&error, "Invalid pixmap array"); |
| 893 |
goto _exit; |
| 894 |
} |
| 895 |
|
| 896 |
out = ensure_array(oout, NPY_FLOAT, 2, 2, &free_out); |
| 897 |
if (!out) { |
| 898 |
driz_error_set_message(&error, "Invalid output array"); |
| 899 |
goto _exit; |
| 900 |
} |
| 901 |
|
| 902 |
if (interp_str2enum(interp_str, &interp, &error)) { |
| 903 |
goto _exit; |
| 904 |
} |
| 905 |
if (strncmp(interp_str, "sinc", 4) == 0) { |
| 906 |
if (py_warning( |
| 907 |
PyExc_DeprecationWarning, "The \"sinc\" interpolation is currently investigated for" |
| 908 |
"possible issues and its use is not recommended.") != 0) { |
| 909 |
goto _exit; |
| 910 |
} |
| 911 |
} |
| 912 |
|
| 913 |
get_dimensions(map, psize); |
| 914 |
get_dimensions(out, osize); |
| 915 |
|
| 916 |
if (psize[0] != osize[0] || psize[1] != osize[1]) { |
| 917 |
if (snprintf( |
| 918 |
warn_msg, 128, |
| 919 |
"Pixel map dimensions (%d, %d) != output dimensions " |
| 920 |
"(%d, %d).", |
| 921 |
psize[0], psize[1], osize[0], osize[1]) < 1) { |
| 922 |
strcpy(warn_msg, "Pixel map dimensions != output dimensions."); |
| 923 |
} |
| 924 |
driz_error_set_message(&error, warn_msg); |
| 925 |
goto _exit; |
| 926 |
} |
| 927 |
|
| 928 |
if (xmax == 0) { |
| 929 |
xmax = osize[0]; |
| 930 |
} |
| 931 |
if (ymax == 0) { |
| 932 |
ymax = osize[1]; |
| 933 |
} |
| 934 |
|
| 935 |
driz_param_init(&p); |
| 936 |
|
| 937 |
p.data = img; |
| 938 |
p.output_data = out; |
| 939 |
p.xmin = xmin; |
| 940 |
p.xmax = xmax; |
| 941 |
p.ymin = ymin; |
| 942 |
p.ymax = ymax; |
| 943 |
p.iscale = iscale; |
| 944 |
p.in_units = unit_cps; |
| 945 |
p.interpolation = interp; |
| 946 |
p.ef = ef; |
| 947 |
p.fill_value = fillval; |
| 948 |
p.sinscl = sinscl; |
| 949 |
p.pixmap = map; |
| 950 |
p.error = &error; |
| 951 |
|
| 952 |
if (driz_error_check(&error, "xmin must be >= 0", p.xmin >= 0)) { |
| 953 |
goto _exit; |
| 954 |
} |
| 955 |
if (driz_error_check(&error, "ymin must be >= 0", p.ymin >= 0)) { |
| 956 |
goto _exit; |
| 957 |
} |
| 958 |
if (driz_error_check(&error, "xmax must be > xmin", p.xmax > p.xmin)) { |
| 959 |
goto _exit; |
| 960 |
} |
| 961 |
if (driz_error_check(&error, "ymax must be > ymin", p.ymax > p.ymin)) { |
| 962 |
goto _exit; |
| 963 |
} |
| 964 |
if (driz_error_check(&error, "exposure time must be > 0", p.ef > 0.0)) { |
| 965 |
goto _exit; |
| 966 |
} |
| 967 |
|
| 968 |
if (doblot(&p)) { |
| 969 |
goto _exit; |
| 970 |
} |
| 971 |
|
| 972 |
_exit: |
| 973 |
driz_log_message("ending tblot"); |
| 974 |
driz_log_close(driz_log_handle); |
| 975 |
if (free_img) { |
| 976 |
Py_XDECREF(img); |
| 977 |
} |
| 978 |
if (free_out) { |
| 979 |
Py_XDECREF(out); |
| 980 |
} |
| 981 |
if (free_map) { |
| 982 |
Py_XDECREF(map); |
| 983 |
} |
| 984 |
|
| 985 |
if (driz_error_is_set(&error)) { |
| 986 |
if (strcmp(driz_error_get_message(&error), "<PYTHON>") != 0) { |
| 987 |
PyErr_SetString(PyExc_Exception, driz_error_get_message(&error)); |
| 988 |
} |
| 989 |
return NULL; |
| 990 |
} else { |
| 991 |
return Py_BuildValue("i", istat); |
| 992 |
} |
| 993 |
} |
| 994 |
|
| 995 |
|
| 996 |
|
| 997 |
|
| 998 |
|
| 999 |
static PyObject * |
| 1000 |
test_cdrizzle(PyObject *self, PyObject *args) |
| 1001 |
{ |
| 1002 |
(void) self; |
| 1003 |
|
| 1004 |
PyObject *data, *weights, *pixmap, *output_data, *output_counts, *output_context; |
| 1005 |
PyArrayObject *dat, *wei, *map, *odat, *ocnt, *ocon; |
| 1006 |
int argc = 1; |
| 1007 |
char *argv[] = {"utest_cdrizzle", NULL}; |
| 1008 |
int free_data = 0, free_wei = 0, free_map = 0; |
| 1009 |
int free_odat = 0, free_ocnt = 0, free_ocon = 0; |
| 1010 |
|
| 1011 |
if (!PyArg_ParseTuple( |
| 1012 |
args, "OOOOOO:test_cdrizzle", &data, &weights, &pixmap, &output_data, &output_counts, |
| 1013 |
&output_context)) { |
| 1014 |
return NULL; |
| 1015 |
} |
| 1016 |
|
| 1017 |
dat = ensure_array(data, NPY_FLOAT, 2, 2, &free_data); |
| 1018 |
if (!dat) { |
| 1019 |
return PyErr_Format(gl_Error, "Invalid data array."); |
| 1020 |
} |
| 1021 |
|
| 1022 |
wei = ensure_array(weights, NPY_FLOAT, 2, 2, &free_wei); |
| 1023 |
if (!wei) { |
| 1024 |
return PyErr_Format(gl_Error, "Invalid weghts array."); |
| 1025 |
} |
| 1026 |
|
| 1027 |
map = ensure_array(pixmap, NPY_DOUBLE, 2, 4, &free_map); |
| 1028 |
if (!map) { |
| 1029 |
return PyErr_Format(gl_Error, "Invalid pixmap."); |
| 1030 |
} |
| 1031 |
|
| 1032 |
odat = ensure_array(output_data, NPY_FLOAT, 2, 2, &free_odat); |
| 1033 |
if (!odat) { |
| 1034 |
return PyErr_Format(gl_Error, "Invalid output data array."); |
| 1035 |
} |
| 1036 |
|
| 1037 |
ocnt = ensure_array(output_counts, NPY_FLOAT, 2, 2, &free_ocnt); |
| 1038 |
if (!ocnt) { |
| 1039 |
return PyErr_Format(gl_Error, "Invalid output counts array."); |
| 1040 |
} |
| 1041 |
|
| 1042 |
ocon = ensure_array(output_context, NPY_INT32, 2, 2, &free_ocon); |
| 1043 |
if (!ocon) { |
| 1044 |
return PyErr_Format(gl_Error, "Invalid context array"); |
| 1045 |
} |
| 1046 |
|
| 1047 |
set_test_arrays(dat, wei, map, odat, ocnt, ocon); |
| 1048 |
utest_cdrizzle(argc, argv); |
| 1049 |
|
| 1050 |
if (free_data) { |
| 1051 |
Py_XDECREF(dat); |
| 1052 |
} |
| 1053 |
if (free_wei) { |
| 1054 |
Py_XDECREF(wei); |
| 1055 |
} |
| 1056 |
if (free_map) { |
| 1057 |
Py_XDECREF(map); |
| 1058 |
} |
| 1059 |
if (free_odat) { |
| 1060 |
Py_XDECREF(odat); |
| 1061 |
} |
| 1062 |
if (free_ocnt) { |
| 1063 |
Py_XDECREF(ocnt); |
| 1064 |
} |
| 1065 |
if (free_ocon) { |
| 1066 |
Py_XDECREF(ocon); |
| 1067 |
} |
| 1068 |
|
| 1069 |
return Py_BuildValue(""); |
| 1070 |
} |
| 1071 |
|
| 1072 |
static PyObject * |
| 1073 |
invert_pixmap_wrap(PyObject *self, PyObject *args) |
| 1074 |
{ |
| 1075 |
(void) self; |
| 1076 |
|
| 1077 |
PyObject *pixmap, *xyout, *bbox; |
| 1078 |
PyArrayObject *xyout_arr, *pixmap_arr, *bbox_arr; |
| 1079 |
struct driz_param_t par; |
| 1080 |
double *xy, *xyin; |
| 1081 |
npy_intp *ndim, xyin_dim = 2; |
| 1082 |
const double half = 0.5 - DBL_EPSILON; |
| 1083 |
int free_xyout = 0, free_pixmap = 0, free_bbox = 0; |
| 1084 |
|
| 1085 |
xyin = (double *) malloc(2 * sizeof(double)); |
| 1086 |
|
| 1087 |
if (!PyArg_ParseTuple(args, "OOO:invpixmap", &pixmap, &xyout, &bbox)) { |
| 1088 |
return NULL; |
| 1089 |
} |
| 1090 |
|
| 1091 |
xyout_arr = ensure_array(xyout, NPY_DOUBLE, 1, 1, &free_xyout); |
| 1092 |
if (!xyout_arr) { |
| 1093 |
return PyErr_Format(gl_Error, "Invalid xyout array."); |
| 1094 |
} |
| 1095 |
|
| 1096 |
pixmap_arr = ensure_array(pixmap, NPY_DOUBLE, 3, 3, &free_pixmap); |
| 1097 |
if (!pixmap_arr) { |
| 1098 |
return PyErr_Format(gl_Error, "Invalid pixmap."); |
| 1099 |
} |
| 1100 |
|
| 1101 |
par.pixmap = pixmap_arr; |
| 1102 |
ndim = PyArray_DIMS(pixmap_arr); |
| 1103 |
|
| 1104 |
if (bbox == Py_None) { |
| 1105 |
par.xmin = 0; |
| 1106 |
par.xmax = ndim[1] - 1; |
| 1107 |
par.ymin = 0; |
| 1108 |
par.ymax = ndim[0] - 1; |
| 1109 |
} else { |
| 1110 |
bbox_arr = ensure_array(bbox, NPY_DOUBLE, 2, 2, &free_bbox); |
| 1111 |
if (!bbox_arr) { |
| 1112 |
return PyErr_Format(gl_Error, "Invalid input bounding box."); |
| 1113 |
} |
| 1114 |
par.xmin = (integer_t) (*(double *) PyArray_GETPTR2(bbox_arr, 0, 0) - half); |
| 1115 |
par.xmax = (integer_t) (*(double *) PyArray_GETPTR2(bbox_arr, 0, 1) + half); |
| 1116 |
par.ymin = (integer_t) (*(double *) PyArray_GETPTR2(bbox_arr, 1, 0) - half); |
| 1117 |
par.ymax = (integer_t) (*(double *) PyArray_GETPTR2(bbox_arr, 1, 1) + half); |
| 1118 |
} |
| 1119 |
|
| 1120 |
xy = (double *) PyArray_DATA(xyout_arr); |
| 1121 |
|
| 1122 |
if (invert_pixmap(&par, xy[0], xy[1], &xyin[0], &xyin[1])) { |
| 1123 |
return Py_BuildValue(""); |
| 1124 |
} |
| 1125 |
|
| 1126 |
#pragma GCC diagnostic push |
| 1127 |
#pragma GCC diagnostic ignored "-Wpedantic" |
| 1128 |
PyArrayObject *arr = |
| 1129 |
(PyArrayObject *) PyArray_SimpleNewFromData(1, &xyin_dim, NPY_DOUBLE, xyin); |
| 1130 |
#pragma GCC diagnostic pop |
| 1131 |
|
| 1132 |
PyArray_ENABLEFLAGS(arr, NPY_ARRAY_OWNDATA); |
| 1133 |
if (free_xyout) { |
| 1134 |
Py_XDECREF(xyout_arr); |
| 1135 |
} |
| 1136 |
if (free_pixmap) { |
| 1137 |
Py_XDECREF(pixmap_arr); |
| 1138 |
} |
| 1139 |
if (free_bbox) { |
| 1140 |
Py_XDECREF(bbox_arr); |
| 1141 |
} |
| 1142 |
|
| 1143 |
return Py_BuildValue("N", arr); |
| 1144 |
} |
| 1145 |
|
| 1146 |
static PyObject * |
| 1147 |
clip_polygon_wrap(PyObject *self, PyObject *args) |
| 1148 |
{ |
| 1149 |
(void) self; |
| 1150 |
|
| 1151 |
int k; |
| 1152 |
PyObject *pin, *qin; |
| 1153 |
PyArrayObject *pin_arr, *qin_arr; |
| 1154 |
struct polygon p, q, pq; |
| 1155 |
PyObject *list, *tuple; |
| 1156 |
int free_pin = 0, free_qin = 0; |
| 1157 |
|
| 1158 |
if (!PyArg_ParseTuple(args, "OO:clip_polygon", &pin, &qin)) { |
| 1159 |
return NULL; |
| 1160 |
} |
| 1161 |
|
| 1162 |
pin_arr = ensure_array(pin, NPY_DOUBLE, 2, 2, &free_pin); |
| 1163 |
if (!pin_arr) { |
| 1164 |
return PyErr_Format(gl_Error, "Invalid P."); |
| 1165 |
} |
| 1166 |
|
| 1167 |
qin_arr = ensure_array(qin, NPY_DOUBLE, 2, 2, &free_qin); |
| 1168 |
if (!qin_arr) { |
| 1169 |
return PyErr_Format(gl_Error, "Invalid Q."); |
| 1170 |
} |
| 1171 |
|
| 1172 |
p.npv = PyArray_SHAPE(pin_arr)[0]; |
| 1173 |
for (k = 0; k < p.npv; ++k) { |
| 1174 |
p.v[k].x = *((double *) PyArray_GETPTR2(pin_arr, k, 0)); |
| 1175 |
p.v[k].y = *((double *) PyArray_GETPTR2(pin_arr, k, 1)); |
| 1176 |
} |
| 1177 |
|
| 1178 |
q.npv = PyArray_SHAPE(qin_arr)[0]; |
| 1179 |
for (k = 0; k < q.npv; ++k) { |
| 1180 |
q.v[k].x = *((double *) PyArray_GETPTR2(qin_arr, k, 0)); |
| 1181 |
q.v[k].y = *((double *) PyArray_GETPTR2(qin_arr, k, 1)); |
| 1182 |
} |
| 1183 |
|
| 1184 |
clip_polygon_to_window(&p, &q, &pq); |
| 1185 |
|
| 1186 |
list = PyList_New(pq.npv); |
| 1187 |
|
| 1188 |
for (k = 0; k < pq.npv; ++k) { |
| 1189 |
tuple = PyTuple_New(2); |
| 1190 |
PyTuple_SetItem(tuple, 0, PyFloat_FromDouble(pq.v[k].x)); |
| 1191 |
PyTuple_SetItem(tuple, 1, PyFloat_FromDouble(pq.v[k].y)); |
| 1192 |
PyList_SetItem(list, k, tuple); |
| 1193 |
} |
| 1194 |
if (free_pin) { |
| 1195 |
Py_XDECREF(pin_arr); |
| 1196 |
} |
| 1197 |
if (free_qin) { |
| 1198 |
Py_XDECREF(qin_arr); |
| 1199 |
} |
| 1200 |
|
| 1201 |
return Py_BuildValue("N", list); |
| 1202 |
} |
| 1203 |
|
| 1204 |
|
| 1205 |
|
| 1206 |
|
| 1207 |
#if defined(__GNUC__) |
| 1208 |
#pragma GCC diagnostic push |
| 1209 |
#pragma GCC diagnostic ignored "-Wmissing-field-initializers" |
| 1210 |
#elif defined(__clang__) |
| 1211 |
#pragma clang diagnostic push |
| 1212 |
#pragma clang diagnostic ignored "-Wcast-function-type-mismatch" |
| 1213 |
#endif |
| 1214 |
|
| 1215 |
static struct PyMethodDef cdrizzle_methods[] = { |
| 1216 |
{"tdriz", (PyCFunction) (void (*)(void)) tdriz, METH_VARARGS | METH_KEYWORDS, |
| 1217 |
"tdriz(image, weights, pixmap, output, counts, context, image2, " |
| 1218 |
"output2, dq, outdq, uniqid, xmin, xmax, ymin, ymax, iscale, " |
| 1219 |
"pscale_ratio, pixfrac, kernel, in_units, expscale, wtscale, fillstr, " |
| 1220 |
"fillstr2)"}, |
| 1221 |
{"tblot", (PyCFunction) (void (*)(void))(PyCFunctionWithKeywords) tblot, |
| 1222 |
METH_VARARGS | METH_KEYWORDS, |
| 1223 |
"tblot(image, pixmap, output, xmin, xmax, ymin, ymax, iscale, " |
| 1224 |
"interp, exptime, fillval, misval, sinscl)"}, |
| 1225 |
{"test_cdrizzle", (PyCFunction) test_cdrizzle, METH_VARARGS, |
| 1226 |
"test_cdrizzle(data, weights, pixmap, output_data, output_counts)"}, |
| 1227 |
{"invert_pixmap", (PyCFunction) invert_pixmap_wrap, METH_VARARGS, |
| 1228 |
"invert_pixmap(pixmap, xyout, bbox)"}, |
| 1229 |
{"clip_polygon", (PyCFunction) clip_polygon_wrap, METH_VARARGS, "clip_polygon(p, q)"}, |
| 1230 |
{NULL, NULL} |
| 1231 |
}; |
| 1232 |
#if defined(__GNUC__) |
| 1233 |
#pragma GCC diagnostic pop |
| 1234 |
#elif defined(__clang__) |
| 1235 |
#pragma clang diagnostic pop |
| 1236 |
#endif |
| 1237 |
|
| 1238 |
|
| 1239 |
|
| 1240 |
|
| 1241 |
#if PY_MAJOR_VERSION < 3 |
| 1242 |
PyMODINIT_FUNC |
| 1243 |
initcdrizzle(void) |
| 1244 |
{ |
| 1245 |
|
| 1246 |
(void) Py_InitModule("cdrizzle", cdrizzle_methods); |
| 1247 |
|
| 1248 |
|
| 1249 |
if (PyErr_Occurred()) { |
| 1250 |
Py_FatalError("can't initialize module cdrizzle"); |
| 1251 |
} |
| 1252 |
|
| 1253 |
import_array(); |
| 1254 |
} |
| 1255 |
|
| 1256 |
#else |
| 1257 |
static struct PyModuleDef moduledef = { |
| 1258 |
PyModuleDef_HEAD_INIT, "cdrizzle", NULL, -1, cdrizzle_methods, NULL, NULL, NULL, NULL}; |
| 1259 |
|
| 1260 |
PyMODINIT_FUNC |
| 1261 |
PyInit_cdrizzle(void) |
| 1262 |
{ |
| 1263 |
PyObject *m; |
| 1264 |
m = PyModule_Create(&moduledef); |
| 1265 |
|
| 1266 |
|
| 1267 |
if (PyErr_Occurred()) { |
| 1268 |
Py_FatalError("can't initialize module cdrizzle"); |
| 1269 |
} |
| 1270 |
|
| 1271 |
import_array(); |
| 1272 |
return m; |
| 1273 |
} |
| 1274 |
|
| 1275 |
#endif |