| 1 |
#include <assert.h> |
| 2 |
#define _USE_MATH_DEFINES |
| 3 |
#include <math.h> |
| 4 |
#include <stdarg.h> |
| 5 |
#include <stdio.h> |
| 6 |
#include <stdlib.h> |
| 7 |
#include <string.h> |
| 8 |
|
| 9 |
#ifndef NPY_NO_DEPRECATED_API |
| 10 |
#define NPY_NO_DEPRECATED_API NPY_1_21_API_VERSION |
| 11 |
#endif |
| 12 |
#define NO_IMPORT_ARRAY |
| 13 |
#define PY_ARRAY_UNIQUE_SYMBOL cdrizzle_util_api |
| 14 |
|
| 15 |
#pragma GCC diagnostic push |
| 16 |
#pragma GCC diagnostic ignored "-Wpedantic" |
| 17 |
#include <numpy/npy_math.h> |
| 18 |
#include <numpy/arrayobject.h> |
| 19 |
#pragma GCC diagnostic pop |
| 20 |
|
| 21 |
#include "cdrizzlemap.h" |
| 22 |
#include "cdrizzleutil.h" |
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
void |
| 29 |
driz_error_init(struct driz_error_t *error) { |
| 30 |
assert(error); |
| 31 |
error->type = NULL; |
| 32 |
error->last_message[0] = 0; |
| 33 |
} |
| 34 |
|
| 35 |
int |
| 36 |
driz_error_check(struct driz_error_t *error, const char *message, int test) { |
| 37 |
if (!test) { |
| 38 |
driz_error_set_message(error, message); |
| 39 |
return 1; |
| 40 |
} |
| 41 |
|
| 42 |
return 0; |
| 43 |
} |
| 44 |
|
| 45 |
void |
| 46 |
driz_error_set_message(struct driz_error_t *error, const char *message) { |
| 47 |
assert(error); |
| 48 |
assert(message); |
| 49 |
|
| 50 |
strncpy(error->last_message, message, MAX_DRIZ_ERROR_LEN - 1); |
| 51 |
error->last_message[MAX_DRIZ_ERROR_LEN - 1] = '\0'; |
| 52 |
} |
| 53 |
|
| 54 |
void |
| 55 |
driz_error_format_message(struct driz_error_t *error, const char *format, ...) { |
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
va_list argp; |
| 60 |
|
| 61 |
assert(error); |
| 62 |
assert(format); |
| 63 |
|
| 64 |
va_start(argp, format); |
| 65 |
(void)vsnprintf(error->last_message, MAX_DRIZ_ERROR_LEN - 1, format, argp); |
| 66 |
va_end(argp); |
| 67 |
error->last_message[MAX_DRIZ_ERROR_LEN - 1] = '\0'; |
| 68 |
} |
| 69 |
|
| 70 |
void |
| 71 |
driz_error_set(struct driz_error_t *error, PyObject *type, const char *format, |
| 72 |
...) { |
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
va_list argp; |
| 77 |
|
| 78 |
assert(error); |
| 79 |
assert(format); |
| 80 |
|
| 81 |
va_start(argp, format); |
| 82 |
(void)vsnprintf(error->last_message, MAX_DRIZ_ERROR_LEN - 1, format, argp); |
| 83 |
va_end(argp); |
| 84 |
|
| 85 |
error->last_message[MAX_DRIZ_ERROR_LEN - 1] = '\0'; |
| 86 |
error->type = type; |
| 87 |
} |
| 88 |
|
| 89 |
const char * |
| 90 |
driz_error_get_message(struct driz_error_t *error) { |
| 91 |
assert(error); |
| 92 |
|
| 93 |
return error->last_message; |
| 94 |
} |
| 95 |
|
| 96 |
int |
| 97 |
driz_error_is_set(struct driz_error_t *error) { |
| 98 |
assert(error); |
| 99 |
|
| 100 |
return error->last_message[0] != 0; |
| 101 |
} |
| 102 |
|
| 103 |
void |
| 104 |
driz_error_unset(struct driz_error_t *error) { |
| 105 |
assert(error); |
| 106 |
|
| 107 |
driz_error_init(error); |
| 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 |
int |
| 150 |
py_warning(PyObject *warning_type, const char *format, ...) { |
| 151 |
char warn_msg[MAX_DRIZ_ERROR_LEN]; |
| 152 |
va_list argp; |
| 153 |
va_start(argp, format); |
| 154 |
if (vsnprintf(warn_msg, MAX_DRIZ_ERROR_LEN - 1, format, argp) < 1) { |
| 155 |
strncpy(warn_msg, "Warning message formatting error.", |
| 156 |
MAX_DRIZ_ERROR_LEN - 1); |
| 157 |
} |
| 158 |
va_end(argp); |
| 159 |
|
| 160 |
if (!warning_type) { |
| 161 |
warning_type = PyExc_Warning; |
| 162 |
} |
| 163 |
|
| 164 |
warn_msg[MAX_DRIZ_ERROR_LEN - 1] = '\0'; |
| 165 |
|
| 166 |
return PyErr_WarnEx(warning_type, warn_msg, 1); |
| 167 |
} |
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
void |
| 173 |
driz_param_dump(struct driz_param_t *p) { |
| 174 |
assert(p); |
| 175 |
|
| 176 |
printf("DRIZZLING PARAMETERS:\n"); |
| 177 |
printf(" kernel: %s\n", kernel_enum2str(p->kernel)); |
| 178 |
printf(" pixel_fraction: %f\n", p->pixel_fraction); |
| 179 |
printf(" exposure_time: %f\n", p->exposure_time); |
| 180 |
printf(" weight_scale: %f\n", p->weight_scale); |
| 181 |
printf(" fill_value: %f\n", p->fill_value); |
| 182 |
printf(" fill_value2: %f\n", p->fill_value2); |
| 183 |
printf(" do_fill: %s\n", bool2str(p->do_fill)); |
| 184 |
printf(" do_fill2: %s\n", bool2str(p->do_fill2)); |
| 185 |
printf(" in_units: %s\n", unit_enum2str(p->in_units)); |
| 186 |
printf(" out_units: %s\n", unit_enum2str(p->out_units)); |
| 187 |
printf(" iscale: %f\n", p->iscale); |
| 188 |
printf(" pscale_ratio: %f\n", p->pscale_ratio); |
| 189 |
} |
| 190 |
|
| 191 |
void |
| 192 |
driz_param_init(struct driz_param_t *p) { |
| 193 |
assert(p); |
| 194 |
|
| 195 |
|
| 196 |
p->kernel = kernel_square; |
| 197 |
p->pixel_fraction = 1.0; |
| 198 |
|
| 199 |
|
| 200 |
p->exposure_time = 1.0f; |
| 201 |
|
| 202 |
|
| 203 |
p->weight_scale = 1.0f; |
| 204 |
|
| 205 |
|
| 206 |
p->fill_value = 0.0f; |
| 207 |
p->fill_value2 = 0.0f; |
| 208 |
p->do_fill = 0; |
| 209 |
p->do_fill2 = 0; |
| 210 |
|
| 211 |
|
| 212 |
p->in_units = unit_counts; |
| 213 |
p->out_units = unit_counts; |
| 214 |
|
| 215 |
p->iscale = 1.0f; |
| 216 |
p->pscale_ratio = NPY_NANF; |
| 217 |
|
| 218 |
|
| 219 |
p->data = NULL; |
| 220 |
p->data2 = NULL; |
| 221 |
p->dq = NULL; |
| 222 |
p->weights = NULL; |
| 223 |
p->pixmap = NULL; |
| 224 |
p->ndata2 = 0; |
| 225 |
|
| 226 |
|
| 227 |
p->in_nx = -1; |
| 228 |
p->in_ny = -1; |
| 229 |
p->out_nx = -1; |
| 230 |
p->out_ny = -1; |
| 231 |
|
| 232 |
|
| 233 |
p->output_data = NULL; |
| 234 |
p->output_data2 = NULL; |
| 235 |
p->output_counts = NULL; |
| 236 |
p->output_context = NULL; |
| 237 |
p->output_dq = NULL; |
| 238 |
|
| 239 |
p->nmiss = 0; |
| 240 |
p->nskip = 0; |
| 241 |
p->error = NULL; |
| 242 |
} |
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
static const char *kernel_string_table[] = { |
| 248 |
"square", "gaussian", "point", "turbo", "lanczos2", "lanczos3", NULL}; |
| 249 |
|
| 250 |
static const char *unit_string_table[] = {"counts", "cps", NULL}; |
| 251 |
|
| 252 |
static const char *interp_string_table[] = { |
| 253 |
"nearest", "linear", "poly3", "poly5", "spline3", |
| 254 |
"sinc", "lsinc", "lan3", "lan5", NULL}; |
| 255 |
|
| 256 |
static const char *bool_string_table[] = {"FALSE", "TRUE", NULL}; |
| 257 |
|
| 258 |
static int |
| 259 |
str2enum(const char *s, const char *table[], int *result, |
| 260 |
struct driz_error_t *error) { |
| 261 |
const char **it = table; |
| 262 |
|
| 263 |
assert(s); |
| 264 |
assert(table); |
| 265 |
assert(result); |
| 266 |
assert(error); |
| 267 |
|
| 268 |
while (*it != NULL) { |
| 269 |
if (strncmp(s, *it, 32) == 0) { |
| 270 |
*result = it - table; |
| 271 |
return 0; |
| 272 |
} |
| 273 |
++it; |
| 274 |
} |
| 275 |
|
| 276 |
return 1; |
| 277 |
} |
| 278 |
|
| 279 |
int |
| 280 |
kernel_str2enum(const char *s, enum e_kernel_t *result, |
| 281 |
struct driz_error_t *error) { |
| 282 |
if (str2enum(s, kernel_string_table, (int *)result, error)) { |
| 283 |
driz_error_format_message(error, "Unknown kernel type '%s'", s); |
| 284 |
return 1; |
| 285 |
} |
| 286 |
|
| 287 |
return 0; |
| 288 |
} |
| 289 |
|
| 290 |
int |
| 291 |
unit_str2enum(const char *s, enum e_unit_t *result, |
| 292 |
struct driz_error_t *error) { |
| 293 |
if (str2enum(s, unit_string_table, (int *)result, error)) { |
| 294 |
driz_error_format_message(error, "Unknown unit type '%s'", s); |
| 295 |
return 1; |
| 296 |
} |
| 297 |
|
| 298 |
return 0; |
| 299 |
} |
| 300 |
|
| 301 |
int |
| 302 |
interp_str2enum(const char *s, enum e_interp_t *result, |
| 303 |
struct driz_error_t *error) { |
| 304 |
if (str2enum(s, interp_string_table, (int *)result, error)) { |
| 305 |
driz_error_format_message(error, "Unknown interp type '%s'", s); |
| 306 |
return 1; |
| 307 |
} |
| 308 |
|
| 309 |
return 0; |
| 310 |
} |
| 311 |
|
| 312 |
const char * |
| 313 |
kernel_enum2str(enum e_kernel_t value) { |
| 314 |
assert(value >= 0 && value < kernel_LAST); |
| 315 |
|
| 316 |
return kernel_string_table[value]; |
| 317 |
} |
| 318 |
|
| 319 |
const char * |
| 320 |
unit_enum2str(enum e_unit_t value) { |
| 321 |
assert(value >= 0 && value < 2); |
| 322 |
|
| 323 |
return unit_string_table[value]; |
| 324 |
} |
| 325 |
|
| 326 |
const char * |
| 327 |
interp_enum2str(enum e_interp_t value) { |
| 328 |
assert(value >= 0 && value < interp_LAST); |
| 329 |
|
| 330 |
return interp_string_table[value]; |
| 331 |
} |
| 332 |
|
| 333 |
const char * |
| 334 |
bool2str(bool_t value) { |
| 335 |
return bool_string_table[value ? 1 : 0]; |
| 336 |
} |
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
void |
| 342 |
create_lanczos_lut(const int kernel_order, const size_t npix, const double del, |
| 343 |
double *lanczos_lut) { |
| 344 |
double order = (double)kernel_order; |
| 345 |
double poff, x, r; |
| 346 |
|
| 347 |
assert(lanczos_lut); |
| 348 |
|
| 349 |
|
| 350 |
lanczos_lut[0] = 1.0; |
| 351 |
|
| 352 |
for (size_t i = 1; i < npix; ++i) { |
| 353 |
x = (double)i * del; |
| 354 |
if (x <= order) { |
| 355 |
poff = M_PI * x; |
| 356 |
r = poff / order; |
| 357 |
lanczos_lut[i] = sin(poff) * sin(r) / (poff * r); |
| 358 |
} else { |
| 359 |
lanczos_lut[i] = 0.0; |
| 360 |
} |
| 361 |
} |
| 362 |
} |
| 363 |
|
| 364 |
void |
| 365 |
put_fill(struct driz_param_t *p, int fill, int fill2) { |
| 366 |
integer_t i, j, k, osize[2], osize2[2], csize[2]; |
| 367 |
|
| 368 |
assert(p); |
| 369 |
if (!p->output_data2) { |
| 370 |
fill2 = 0; |
| 371 |
} |
| 372 |
if (!fill && !fill2) { |
| 373 |
return; |
| 374 |
} |
| 375 |
|
| 376 |
get_dimensions(p->output_data, osize); |
| 377 |
get_dimensions(p->output_counts, csize); |
| 378 |
if (osize[0] != csize[0] || osize[1] != csize[1]) { |
| 379 |
driz_error_set(p->error, PyExc_ValueError, |
| 380 |
"Mismatch between output_data and output_counts " |
| 381 |
"array size."); |
| 382 |
return; |
| 383 |
} |
| 384 |
|
| 385 |
if (fill2) { |
| 386 |
for (k = 0; k < p->ndata2; ++k) { |
| 387 |
get_dimensions(p->output_data2[k], osize2); |
| 388 |
if ((osize2[0] != osize[0]) || (osize2[1] != osize[1])) { |
| 389 |
driz_error_set(p->error, PyExc_ValueError, |
| 390 |
"Mismatch between output_data and output_data2 " |
| 391 |
"array size."); |
| 392 |
return; |
| 393 |
} |
| 394 |
} |
| 395 |
} |
| 396 |
|
| 397 |
if (fill && !fill2) { |
| 398 |
for (j = 0; j < osize[1]; ++j) { |
| 399 |
for (i = 0; i < osize[0]; ++i) { |
| 400 |
if (get_pixel(p->output_counts, i, j) == 0.0) { |
| 401 |
set_pixel(p->output_data, i, j, p->fill_value); |
| 402 |
} |
| 403 |
} |
| 404 |
} |
| 405 |
} else if (!fill && fill2) { |
| 406 |
for (j = 0; j < osize[1]; ++j) { |
| 407 |
for (i = 0; i < osize[0]; ++i) { |
| 408 |
if (get_pixel(p->output_counts, i, j) == 0.0) { |
| 409 |
for (k = 0; k < p->ndata2; ++k) { |
| 410 |
set_pixel(p->output_data2[k], i, j, p->fill_value2); |
| 411 |
} |
| 412 |
} |
| 413 |
} |
| 414 |
} |
| 415 |
} else { |
| 416 |
for (j = 0; j < osize[1]; ++j) { |
| 417 |
for (i = 0; i < osize[0]; ++i) { |
| 418 |
if (get_pixel(p->output_counts, i, j) == 0.0) { |
| 419 |
set_pixel(p->output_data, i, j, p->fill_value); |
| 420 |
if (p->output_data2) { |
| 421 |
for (k = 0; k < p->ndata2; ++k) { |
| 422 |
set_pixel(p->output_data2[k], i, j, p->fill_value2); |
| 423 |
} |
| 424 |
} |
| 425 |
} |
| 426 |
} |
| 427 |
} |
| 428 |
} |
| 429 |
} |
| 430 |
|
| 431 |
double |
| 432 |
mgf2(double lambda) { |
| 433 |
double sig, sig2; |
| 434 |
|
| 435 |
sig = 1.0e7 / lambda; |
| 436 |
sig2 = sig * sig; |
| 437 |
|
| 438 |
return sqrt(1.0 + 2.590355e10 / (5.312993e10 - sig2) + |
| 439 |
4.4543708e9 / (11.17083e9 - sig2) + |
| 440 |
4.0838897e5 / (1.766361e5 - sig2)); |
| 441 |
} |