Debian Codegraph @

MontageLib/FixNaN/montageFixNaN.c

raw symbols SCIP
1 /* mFixNaN.c
2
3 Version Developer Date Change
4 ------- --------------- ------- -----------------------
5 2.2 John Good 08Sep15 fits_read_pix() incorrect null value
6 2.1 John Good 07Oct07 Bug fix: set fstatus value
7 2.0 John Good 11Aug05 Change the code to do a line at a time
8 (to mimimize memory usage) and the allow
9 for no output image (just a count of pixel
10 that would change) when the output file
11 name is "-".
12 1.0 John Good 16Sep04 Baseline code
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 /* mFixNaN */
52 /* */
53 /* Montage is a set of general reprojection / coordinate-transform / */
54 /* mosaicking programs. Any number of input images can be merged into */
55 /* an output FITS file. The attributes of the input are read from the */
56 /* input files; the attributes of the output are read a combination of */
57 /* the command line and a FITS header template file. */
58 /* */
59 /* This module, mFixNaN, converts NaNs found in the image to some */
60 /* other value (given by the user) or ranges of values to NaNs */
61 /* */
62 /* char *input_file FITS file to fix */
63 /* char *output_file Fixed FITS file */
64 /* */
65 /* int haveVal This flag indicates that NaNs should be */
66 /* converted to a different value. */
67 /* */
68 /* double NaNvalue The value associated with the above flag. */
69 /* This is for situations where the NaNs in the */
70 /* image are incorrectly used. */
71 /* */
72 /* int nMinMax The next five arguments define a set of value */
73 /* ranges which will be converted to NaNs. This */
74 /* one is the count of these ranges. */
75 /* */
76 /* double *minblank The 'ranges' can either have min/max values */
77 /* or can be just an upper (max) value (i.e. */
78 /* with a min of -Infinity) or a lower (min) */
79 /* value (i.e. with a max of +Infinity). This */
80 /* array of values are the minumums. */
81 /* */
82 /* int *ismin This array is a set of booleans indicating */
83 /* whether each min value above is part of a */
84 /* min/max range (0) or a standalone lower limit */
85 /* (1, in which case the corresponding max is */
86 /* ignored). */
87 /* */
88 /* double *maxblank Max values for min/max ranges or standalone */
89 /* upper limits. */
90 /* */
91 /* int *ismax Booleans indicating whether each max value is */
92 /* part of a min/max range (0) or a standalone */
93 /* upper limit (1). */
94 /* */
95 /* int boundaries Some projections (e.g. Aitoff) have regions */
96 /* of the image that are non-physical but which */
97 /* may have non-blank values (e.g. 0 instead of */
98 /* NaN). If this flag is on, check for that and */
99 /* convert those pixels to proper NaNs. */
100 /* */
101 /* int debug Debugging output level */
102 /* */
103 /* */
104 /* The 'NaNvalue' and ranges can be used together: The ranges are used */
105 /* to logically convert some values to NaN and then the NaNvalue is used */
106 /* to convert those and any other naturally occuring NaNs to the */
107 /* the NaNvalue. However, this is unlikely to be of much use in */
108 /* practice. */
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 /* Make a NaN value to use setting blank pixels */
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 /* Initialize return structure */
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 /* Read the input image */
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 /* Create the output FITS files */
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 /* Copy all the header keywords */
248 /* from the input to the output */
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 /* Modify BITPIX to be -64 */
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 /* Allocate memory for the input/output image pixels */
282 /* (same size as the input image) */
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 /* Loop over the input lines */
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 /* Read a line from the input file */
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 /* For each input pixel */
355 /************************/
356
357 // If boundary checking is on (e.g. Aitoff pixels in the
358 // corners) or not-a-number and we have a replacement value
359
360 for (i=0; i<input.naxes[0]; ++i)
361 {
362 // First convert the "boundary" values if desired
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 // Now process the data
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 // If in one of the "fix" ranges
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 // Otherwise, good data
462
463 else
464 outbuffer[i] = inbuffer[i];
465 }
466
467
468 /***************************/
469 /* Write the output buffer */
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 /* Close the FITS files */
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 /* Open a FITS file pair and extract the */
543 /* pertinent header information. */
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 /* Print out general errors */
602 /* */
603 /******************************/
604
605 void mFixNaN_printError(char *msg)
606 {
607 strcpy(montage_msgstr, msg);
608 return;
609 }
610
611
612
613 /***********************************/
614 /* */
615 /* Print out FITS library errors */
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