| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
#include <stdio.h> |
| 17 |
#include <stdlib.h> |
| 18 |
#include <unistd.h> |
| 19 |
#include <string.h> |
| 20 |
#include <strings.h> |
| 21 |
#include <sys/types.h> |
| 22 |
#include <time.h> |
| 23 |
#include <math.h> |
| 24 |
#include <fitsio.h> |
| 25 |
#include <wcs.h> |
| 26 |
|
| 27 |
#include <mFixNaN.h> |
| 28 |
#include <montage.h> |
| 29 |
|
| 30 |
#define MAXSTR 256 |
| 31 |
#define MAXFILE 256 |
| 32 |
|
| 33 |
static struct |
| 34 |
{ |
| 35 |
fitsfile *fptr; |
| 36 |
long naxes[2]; |
| 37 |
double crpix1, crpix2; |
| 38 |
} |
| 39 |
input, output; |
| 40 |
|
| 41 |
static time_t currtime, start; |
| 42 |
|
| 43 |
static struct WorldCoor *wcs; |
| 44 |
|
| 45 |
static char montage_msgstr[1024]; |
| 46 |
static char montage_json [1024]; |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
struct mFixNaNReturn *mFixNaN(char *input_file, char *output_file, int haveVal, double NaNvalue, int nMinMax, |
| 113 |
double *minblank, int *ismin, double *maxblank, int *ismax, int boundaries, int debug) |
| 114 |
{ |
| 115 |
int i, j, k, countRange, countNaN, bcount, status; |
| 116 |
int inRange, offscl, nullcnt; |
| 117 |
long fpixel[4], nelements; |
| 118 |
|
| 119 |
double *inbuffer; |
| 120 |
double *outbuffer; |
| 121 |
double xi, yj; |
| 122 |
double xproj, yproj; |
| 123 |
double lon, lat; |
| 124 |
|
| 125 |
char *checkHdr; |
| 126 |
|
| 127 |
struct mFixNaNReturn *returnStruct; |
| 128 |
|
| 129 |
FILE* fd; |
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
union |
| 137 |
{ |
| 138 |
double d; |
| 139 |
char c[8]; |
| 140 |
} |
| 141 |
value; |
| 142 |
|
| 143 |
double nan; |
| 144 |
|
| 145 |
for(i=0; i<8; ++i) |
| 146 |
value.c[i] = (char)255; |
| 147 |
|
| 148 |
nan = value.d; |
| 149 |
|
| 150 |
fd = fopen("/tmp/jcg.debug", "w+"); |
| 151 |
|
| 152 |
fprintf(fd, "XXX> nMinMax: [%d]\n", nMinMax); |
| 153 |
|
| 154 |
for(i=0; i<nMinMax; ++i) |
| 155 |
fprintf(fd, "XXX> [%-g](%d) -> [%-g](%d)\n", minblank[i], ismin[i], maxblank[i], ismax[i]); |
| 156 |
|
| 157 |
fflush(fd); |
| 158 |
fclose(fd); |
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
returnStruct = (struct mFixNaNReturn *)malloc(sizeof(struct mFixNaNReturn)); |
| 166 |
|
| 167 |
memset((void *)returnStruct, 0, sizeof(returnStruct)); |
| 168 |
|
| 169 |
|
| 170 |
returnStruct->status = 1; |
| 171 |
|
| 172 |
strcpy(returnStruct->msg, ""); |
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
time(&currtime); |
| 180 |
start = currtime; |
| 181 |
|
| 182 |
|
| 183 |
checkHdr = montage_checkHdr(input_file, 0, 0); |
| 184 |
|
| 185 |
if(checkHdr) |
| 186 |
{ |
| 187 |
strcpy(returnStruct->msg, checkHdr); |
| 188 |
return returnStruct; |
| 189 |
} |
| 190 |
|
| 191 |
if(mFixNaN_readFits(input_file, boundaries) > 0) |
| 192 |
{ |
| 193 |
strcpy(returnStruct->msg, montage_msgstr); |
| 194 |
return returnStruct; |
| 195 |
} |
| 196 |
|
| 197 |
|
| 198 |
if(debug >= 1) |
| 199 |
{ |
| 200 |
printf("\ninput.naxes[0] = %ld\n", input.naxes[0]); |
| 201 |
printf("input.naxes[1] = %ld\n", input.naxes[1]); |
| 202 |
printf("input.crpix1 = %-g\n", input.crpix1); |
| 203 |
printf("input.crpix2 = %-g\n", input.crpix2); |
| 204 |
|
| 205 |
fflush(stdout); |
| 206 |
} |
| 207 |
|
| 208 |
output.naxes[0] = input.naxes[0]; |
| 209 |
output.naxes[1] = input.naxes[1]; |
| 210 |
output.crpix1 = input.crpix1; |
| 211 |
output.crpix2 = input.crpix2; |
| 212 |
|
| 213 |
if(debug >= 1) |
| 214 |
{ |
| 215 |
printf("\noutput.naxes[0] = %ld\n", output.naxes[0]); |
| 216 |
printf("output.naxes[1] = %ld\n", output.naxes[1]); |
| 217 |
printf("output.crpix1 = %-g\n", output.crpix1); |
| 218 |
printf("output.crpix2 = %-g\n", input.crpix2); |
| 219 |
|
| 220 |
fflush(stdout); |
| 221 |
} |
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
remove(output_file); |
| 230 |
|
| 231 |
status = 0; |
| 232 |
if(fits_create_file(&output.fptr, output_file, &status)) |
| 233 |
{ |
| 234 |
mFixNaN_printFitsError(status); |
| 235 |
strcpy(returnStruct->msg, montage_msgstr); |
| 236 |
return returnStruct; |
| 237 |
} |
| 238 |
|
| 239 |
if(debug >= 1) |
| 240 |
{ |
| 241 |
printf("\nFITS output file created (not yet populated)\n"); |
| 242 |
fflush(stdout); |
| 243 |
} |
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
status = 0; |
| 252 |
if(fits_copy_header(input.fptr, output.fptr, &status)) |
| 253 |
{ |
| 254 |
mFixNaN_printFitsError(status); |
| 255 |
strcpy(returnStruct->msg, montage_msgstr); |
| 256 |
return returnStruct; |
| 257 |
} |
| 258 |
|
| 259 |
if(debug >= 1) |
| 260 |
{ |
| 261 |
printf("Header keywords copied to FITS output file\n\n"); |
| 262 |
fflush(stdout); |
| 263 |
} |
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
status = 0; |
| 271 |
if(fits_update_key_lng(output.fptr, "BITPIX", -64, |
| 272 |
(char *)NULL, &status)) |
| 273 |
{ |
| 274 |
mFixNaN_printFitsError(status); |
| 275 |
strcpy(returnStruct->msg, montage_msgstr); |
| 276 |
return returnStruct; |
| 277 |
} |
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
outbuffer = (double *)malloc(output.naxes[0] * sizeof(double)); |
| 286 |
|
| 287 |
if(debug >= 1) |
| 288 |
{ |
| 289 |
printf("%ld bytes allocated for row of output image pixels\n", |
| 290 |
output.naxes[0] * sizeof(double)); |
| 291 |
fflush(stdout); |
| 292 |
} |
| 293 |
|
| 294 |
inbuffer = (double *)malloc(input.naxes[0] * sizeof(double)); |
| 295 |
|
| 296 |
if(debug >= 1) |
| 297 |
{ |
| 298 |
printf("%ld bytes allocated for row of input image pixels\n", |
| 299 |
input.naxes[0] * sizeof(double)); |
| 300 |
fflush(stdout); |
| 301 |
} |
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
fpixel[0] = 1; |
| 309 |
fpixel[1] = 1; |
| 310 |
fpixel[2] = 1; |
| 311 |
fpixel[3] = 1; |
| 312 |
|
| 313 |
nelements = input.naxes[0]; |
| 314 |
|
| 315 |
countRange = 0; |
| 316 |
countNaN = 0; |
| 317 |
bcount = 0; |
| 318 |
|
| 319 |
for (j=0; j<input.naxes[1]; ++j) |
| 320 |
{ |
| 321 |
if(debug >= 2) |
| 322 |
{ |
| 323 |
if(debug >= 3) |
| 324 |
printf("\n"); |
| 325 |
|
| 326 |
printf("\rProcessing input row %5d [So far rangeCount=%d, nanCount=%d, boundaryCount=%d]", |
| 327 |
j, countRange, countNaN, bcount); |
| 328 |
|
| 329 |
if(debug >= 3) |
| 330 |
printf("\n"); |
| 331 |
|
| 332 |
fflush(stdout); |
| 333 |
} |
| 334 |
|
| 335 |
for (i=0; i<output.naxes[0]; ++i) |
| 336 |
outbuffer[i] = 0.; |
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
status = 0; |
| 344 |
if(fits_read_pix(input.fptr, TDOUBLE, fpixel, nelements, &nan, |
| 345 |
inbuffer, &nullcnt, &status)) |
| 346 |
{ |
| 347 |
mFixNaN_printFitsError(status); |
| 348 |
strcpy(returnStruct->msg, montage_msgstr); |
| 349 |
return returnStruct; |
| 350 |
} |
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
for (i=0; i<input.naxes[0]; ++i) |
| 361 |
{ |
| 362 |
|
| 363 |
|
| 364 |
if(boundaries) |
| 365 |
{ |
| 366 |
xi = i; |
| 367 |
yj = j; |
| 368 |
|
| 369 |
pix2wcs(wcs, xi, yj, &lon, &lat); |
| 370 |
|
| 371 |
offscl = wcs->offscl; |
| 372 |
|
| 373 |
if(!offscl) |
| 374 |
wcs2pix(wcs, lon, lat, &xproj, &yproj, &offscl); |
| 375 |
|
| 376 |
if(offscl || fabs(xi-xproj) > 0.01 || fabs(yj-yproj) > 0.01) |
| 377 |
{ |
| 378 |
inbuffer[i] = nan; |
| 379 |
++bcount; |
| 380 |
} |
| 381 |
} |
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
if((haveVal == 1) && mNaN(inbuffer[i])) |
| 387 |
{ |
| 388 |
++countNaN; |
| 389 |
|
| 390 |
outbuffer[i] = NaNvalue; |
| 391 |
|
| 392 |
if(debug >= 3) |
| 393 |
{ |
| 394 |
printf("pixel[%d][%d] converted to %-g\n", j, i, NaNvalue); |
| 395 |
fflush(stdout); |
| 396 |
} |
| 397 |
} |
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
else if(nMinMax) |
| 403 |
{ |
| 404 |
inRange = 0; |
| 405 |
|
| 406 |
for(k=0; k<nMinMax; ++k) |
| 407 |
{ |
| 408 |
if(ismin[k] && !ismax[k] && inbuffer[i] >= minblank[k]) |
| 409 |
{ |
| 410 |
inRange = 1; |
| 411 |
break; |
| 412 |
} |
| 413 |
else if(!ismin[k] && ismax[k] && inbuffer[i] <= maxblank[k]) |
| 414 |
{ |
| 415 |
inRange = 1; |
| 416 |
break; |
| 417 |
} |
| 418 |
else if(inbuffer[i] >= minblank[k] |
| 419 |
&& inbuffer[i] <= maxblank[k]) |
| 420 |
{ |
| 421 |
inRange = 1; |
| 422 |
break; |
| 423 |
} |
| 424 |
} |
| 425 |
|
| 426 |
if(inRange) |
| 427 |
{ |
| 428 |
++countRange; |
| 429 |
|
| 430 |
if(haveVal) |
| 431 |
{ |
| 432 |
++countNaN; |
| 433 |
|
| 434 |
outbuffer[i] = NaNvalue; |
| 435 |
|
| 436 |
if(debug >= 3) |
| 437 |
{ |
| 438 |
printf("pixel[%d][%d] converted to NaN -> %-g\n", j, i, NaNvalue); |
| 439 |
fflush(stdout); |
| 440 |
} |
| 441 |
} |
| 442 |
else |
| 443 |
{ |
| 444 |
++countNaN; |
| 445 |
|
| 446 |
outbuffer[i] = nan; |
| 447 |
|
| 448 |
if(debug >= 3) |
| 449 |
{ |
| 450 |
printf("pixel[%d][%d] converted to NaN\n", j, i); |
| 451 |
fflush(stdout); |
| 452 |
} |
| 453 |
} |
| 454 |
} |
| 455 |
|
| 456 |
else |
| 457 |
outbuffer[i] = inbuffer[i]; |
| 458 |
} |
| 459 |
|
| 460 |
|
| 461 |
|
| 462 |
|
| 463 |
else |
| 464 |
outbuffer[i] = inbuffer[i]; |
| 465 |
} |
| 466 |
|
| 467 |
|
| 468 |
|
| 469 |
|
| 470 |
|
| 471 |
|
| 472 |
status = 0; |
| 473 |
if (fits_write_pix(output.fptr, TDOUBLE, fpixel, nelements, |
| 474 |
outbuffer, &status)) |
| 475 |
{ |
| 476 |
mFixNaN_printFitsError(status); |
| 477 |
strcpy(returnStruct->msg, montage_msgstr); |
| 478 |
return returnStruct; |
| 479 |
} |
| 480 |
|
| 481 |
++fpixel[1]; |
| 482 |
} |
| 483 |
|
| 484 |
if(debug >= 1) |
| 485 |
{ |
| 486 |
time(&currtime); |
| 487 |
printf("\nDone copying data (%d seconds)\n", |
| 488 |
(int)(currtime - start)); |
| 489 |
fflush(stdout); |
| 490 |
} |
| 491 |
|
| 492 |
|
| 493 |
|
| 494 |
|
| 495 |
|
| 496 |
|
| 497 |
status = 0; |
| 498 |
if(fits_close_file(input.fptr, &status)) |
| 499 |
{ |
| 500 |
mFixNaN_printFitsError(status); |
| 501 |
strcpy(returnStruct->msg, montage_msgstr); |
| 502 |
return returnStruct; |
| 503 |
} |
| 504 |
|
| 505 |
status = 0; |
| 506 |
if(fits_close_file(output.fptr, &status)) |
| 507 |
{ |
| 508 |
mFixNaN_printFitsError(status); |
| 509 |
strcpy(returnStruct->msg, montage_msgstr); |
| 510 |
return returnStruct; |
| 511 |
} |
| 512 |
|
| 513 |
if(debug >= 1) |
| 514 |
{ |
| 515 |
time(&currtime); |
| 516 |
printf("Done (%d seconds total)\n", (int)(currtime - start)); |
| 517 |
fflush(stdout); |
| 518 |
} |
| 519 |
|
| 520 |
free(inbuffer); |
| 521 |
free(outbuffer); |
| 522 |
|
| 523 |
|
| 524 |
sprintf(montage_msgstr, "rangeCount=%d, nanCount=%d, boundaryCount=%d", countRange, countNaN, bcount); |
| 525 |
sprintf(montage_json, "{\"rangeCount\":%d, \"nanCount\":%d, \"boundaryCount\":%d}", countRange, countNaN, bcount); |
| 526 |
|
| 527 |
returnStruct -> status = 0; |
| 528 |
|
| 529 |
strcpy(returnStruct->msg, montage_msgstr); |
| 530 |
strcpy(returnStruct->json, montage_json); |
| 531 |
|
| 532 |
returnStruct->rangeCount = countRange; |
| 533 |
returnStruct->nanCount = countNaN; |
| 534 |
returnStruct->boundaryCount = bcount; |
| 535 |
|
| 536 |
return returnStruct; |
| 537 |
} |
| 538 |
|
| 539 |
|
| 540 |
|
| 541 |
|
| 542 |
|
| 543 |
|
| 544 |
|
| 545 |
|
| 546 |
|
| 547 |
int mFixNaN_readFits(char *fluxfile, int boundaryFlag) |
| 548 |
{ |
| 549 |
int status, nfound; |
| 550 |
long naxes[2]; |
| 551 |
double crpix[2]; |
| 552 |
char errstr[MAXSTR]; |
| 553 |
char *header; |
| 554 |
|
| 555 |
status = 0; |
| 556 |
if(fits_open_file(&input.fptr, fluxfile, READONLY, &status)) |
| 557 |
{ |
| 558 |
sprintf(errstr, "Image file %s missing or invalid FITS", fluxfile); |
| 559 |
mFixNaN_printError(errstr); |
| 560 |
return 1; |
| 561 |
} |
| 562 |
|
| 563 |
status = 0; |
| 564 |
if(fits_read_keys_lng(input.fptr, "NAXIS", 1, 2, naxes, &nfound, &status)) |
| 565 |
{ |
| 566 |
mFixNaN_printFitsError(status); |
| 567 |
return 1; |
| 568 |
} |
| 569 |
|
| 570 |
input.naxes[0] = naxes[0]; |
| 571 |
input.naxes[1] = naxes[1]; |
| 572 |
|
| 573 |
status = 0; |
| 574 |
if(fits_read_keys_dbl(input.fptr, "CRPIX", 1, 2, crpix, &nfound, &status)) |
| 575 |
{ |
| 576 |
mFixNaN_printFitsError(status); |
| 577 |
return 1; |
| 578 |
} |
| 579 |
|
| 580 |
input.crpix1 = crpix[0]; |
| 581 |
input.crpix2 = crpix[1]; |
| 582 |
|
| 583 |
if(boundaryFlag) |
| 584 |
{ |
| 585 |
if(fits_get_image_wcs_keys(input.fptr, &header, &status)) |
| 586 |
{ |
| 587 |
mFixNaN_printFitsError(status); |
| 588 |
return 1; |
| 589 |
} |
| 590 |
|
| 591 |
wcs = wcsinit(header); |
| 592 |
} |
| 593 |
|
| 594 |
return 0; |
| 595 |
} |
| 596 |
|
| 597 |
|
| 598 |
|
| 599 |
|
| 600 |
|
| 601 |
|
| 602 |
|
| 603 |
|
| 604 |
|
| 605 |
void mFixNaN_printError(char *msg) |
| 606 |
{ |
| 607 |
strcpy(montage_msgstr, msg); |
| 608 |
return; |
| 609 |
} |
| 610 |
|
| 611 |
|
| 612 |
|
| 613 |
|
| 614 |
|
| 615 |
|
| 616 |
|
| 617 |
|
| 618 |
|
| 619 |
void mFixNaN_printFitsError(int status) |
| 620 |
{ |
| 621 |
char status_str[FLEN_STATUS]; |
| 622 |
|
| 623 |
fits_get_errstatus(status, status_str); |
| 624 |
|
| 625 |
strcpy(montage_msgstr, status_str); |
| 626 |
|
| 627 |
return; |
| 628 |
} |
| 629 |
|