1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
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
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
|
/*
The following code was written by Richard White at STScI and made
available for use in CFITSIO in July 1999.
*/
# include <stdio.h>
# include <stdlib.h>
# include <math.h>
#include "fitsio2.h"
/* nearest integer function */
# define NINT(x) ((x >= 0.) ? (int) (x + 0.5) : (int) (x - 0.5))
# define SORT_CUTOFF 100 /* used by xMedian */
# define NELEM 5 /* used by xMedian */
#define NULL_VALUE -2147483647 /* value used to represent undefined pixels */
#define N_RESERVED_VALUES 1 /* number of reserved values, starting with */
/* and including NULL_VALUE. These values */
/* may not be used to represent the quantized */
/* and scaled floating point pixel values */
/* factor to convert from median deviation to rms */
# define MEDIAN_TO_RMS 1.4826
/* more than this many standard deviations from the mean is an outlier */
# define SIGMA_CLIP 5.
# define NITER 3 /* number of sigma-clipping iterations */
static float xMedian (float [], int);
static void InsertionSort (float x[], int);
static int FqCompare (const void *, const void *);
static void FqMean (float [], int, double *, double *);
/*---------------------------------------------------------------------------*/
/* this routine used to be called 'quantize' (WDP) */
int fits_quantize_float (float fdata[], int nx, float in_null_value,
int noise_bits, int idata[], double *bscale,
double *bzero, int *iminval, int *imaxval) {
/* arguments:
float fdata[] i: array of image pixels to be compressed
int nx i: length of fdata array
float in_null_value i: value used to represent undefined pixels in fdata
int noise_bits i: quantization level (number of bits)
int idata[] o: values of fdata after applying bzero and bscale
double bscale o: scale factor
double bzero o: zero offset
int iminval o: minimum quantized value that is returned
int imaxval o: maximum quantized value that is returned
The function value will be one if the input fdata were copied to idata;
in this case the parameters bscale and bzero can be used to convert back to
nearly the original floating point values: fdata ~= idata * bscale + bzero.
If the function value is zero, the data were not copied to idata.
*/
float *diff; /* difference array */
int ndiff; /* size of diff array */
int intflag; /* true if data are really integer */
int i, j, iter; /* loop indices */
int anynulls = 0; /* set if fdata contains any null values */
int nshift;
int first_nonnull = 0;
double mean, stdev; /* mean and RMS of differences */
double minval = 0., maxval = 0.; /* min & max of fdata */
double delta; /* bscale, 1 in idata = delta in fdata */
double zeropt; /* bzero */
double median; /* median of diff array */
double temp;
if (nx <= 1) {
*bscale = 1.;
*bzero = 0.;
return (0);
}
*iminval = INT32_MAX;
*imaxval = INT32_MIN;
/* Check to see if data are "floating point integer." */
/* This also catches the case where all the pixels are null */
intflag = 1; /* initial value */
for (i = 0; i < nx; i++) {
if (fdata[i] == in_null_value) {
idata[i] = NULL_VALUE;
anynulls = 1;
}
else if (fdata[i] > INT32_MAX ||
fdata[i] < NULL_VALUE + N_RESERVED_VALUES) {
intflag = 0; /* not integer */
break;
}
else {
idata[i] = (int)(fdata[i] + 0.5);
*iminval = minvalue(idata[i], *iminval);
*imaxval = maxvalue(idata[i], *imaxval);
if (idata[i] != fdata[i]) {
intflag = 0; /* not integer */
break;
}
}
}
if (intflag) { /* data are "floating point integer" */
if (anynulls) {
/* Shift the range of values so they lie close to NULL_VALUE. */
/* This will make the compression more efficient. */
nshift = *iminval - NULL_VALUE - N_RESERVED_VALUES;
for (i = 0; i < nx; i++) {
if (idata[i] != NULL_VALUE) {
idata[i] -= nshift;
}
}
*iminval = *iminval - nshift;
*imaxval = *imaxval - nshift;
*bscale = 1.;
*bzero = (double) nshift;
}
else {
/* there were no null values, so no need to shift the range */
*bscale = 1.;
*bzero = 0.;
}
return (1);
}
/* data are not "floating point integer"; need to quantize them */
/* find first non-null pixel, and initialize min and max values */
for (i = 0; i < nx; i++) {
if (fdata[i] != in_null_value) {
minval = fdata[i];
maxval = fdata[i];
first_nonnull = i;
break;
}
}
/* allocate temporary buffer for differences */
ndiff = nx - first_nonnull - 1;
if ((diff = malloc (ndiff * sizeof (float))) == NULL) {
ffpmsg("Out of memory in 'fits_quantize_float'.");
return (0);
}
/* calc ABS difference between successive non-null pixels */
j = first_nonnull;
ndiff = 0;
for (i = j + 1 ; i < nx; i++) {
if (fdata[i] != in_null_value) {
diff[ndiff] = fabs (fdata[i] - fdata[j]);
j = i;
ndiff++;
minval = minvalue(minval, fdata[i]);
maxval = maxvalue(maxval, fdata[i]);
}
}
/* check if there were any null values */
if (ndiff + 1 == nx)
anynulls = 0;
else
anynulls = 1;
/* use median of absolute deviations */
median = xMedian (diff, ndiff);
stdev = median * MEDIAN_TO_RMS;
/* substitute sigma-clipping if median is zero */
if (stdev == 0.0) {
/* calculate differences between non-null pixels */
j = first_nonnull;
ndiff = 0;
for (i = j + 1 ; i < nx; i++) {
if (fdata[i] != in_null_value) {
diff[ndiff] = fdata[i] - fdata[j];
j = i;
ndiff++;
}
}
FqMean (diff, ndiff, &mean, &stdev);
for (iter = 0; iter < NITER; iter++) {
j = 0;
for (i = 0; i < ndiff; i++) {
if (fabs (diff[i] - mean) < SIGMA_CLIP * stdev) {
if (j < i)
diff[j] = diff[i];
j++;
}
}
if (j == ndiff)
break;
ndiff = j;
FqMean (diff, ndiff, &mean, &stdev);
}
}
free (diff);
delta = stdev / pow (2., (double)noise_bits);
if (delta == 0. && ndiff > 0)
return (0); /* Zero variance in differences! Don't quantize. */
/* check that the range of quantized levels is not > range of int */
if ((maxval - minval) / delta > 2. * 2147483647. - N_RESERVED_VALUES )
return (0); /* don't quantize */
if (!anynulls) { /* don't have to check for nulls */
/* return all positive values, if possible since some */
/* compression algorithms either only work for positive integers, */
/* or are more efficient. */
if ((maxval - minval) / delta < 2147483647. - N_RESERVED_VALUES )
{
zeropt = minval;
}
else
{
/* center the quantized levels around zero */
zeropt = (minval + maxval) / 2.;
}
for (i = 0; i < nx; i++) {
temp = (fdata[i] - zeropt) / delta;
idata[i] = NINT (temp);
}
}
else {
/* data contains null values; shift the range to be */
/* close to the value used to represent null values */
zeropt = minval - delta * (NULL_VALUE + N_RESERVED_VALUES);
for (i = 0; i < nx; i++) {
if (fdata[i] != in_null_value) {
temp = (fdata[i] - zeropt) / delta;
idata[i] = NINT (temp);
}
else
idata[i] = NULL_VALUE;
}
}
/* calc min and max values */
temp = (minval - zeropt) / delta;
*iminval = NINT (temp);
temp = (maxval - zeropt) / delta;
*imaxval = NINT (temp);
*bscale = delta;
*bzero = zeropt;
return (1); /* yes, data have been quantized */
}
/*---------------------------------------------------------------------------*/
int fits_quantize_double (double fdata[], int nx, double in_null_value,
int noise_bits, int idata[], double *bscale,
double *bzero, int *iminval, int *imaxval) {
/* arguments:
double fdata[] i: array of image pixels to be compressed
int nx i: length of fdata array
double in_null_value i: value used to represent undefined pixels in fdata
int noise_bits i: quantization level (number of bits)
int idata[] o: values of fdata after applying bzero and bscale
double bscale o: scale factor
double bzero o: zero offset
int imaxval o: maximum quantized value that is returned
int iminval o: minimum quantized value that is returned
The function value will be one if the input fdata were copied to idata;
in this case the parameters bscale and bzero can be used to convert back to
nearly the original floating point values: fdata ~= idata * bscale + bzero.
If the function value is zero, the data were not copied to idata.
*/
float *diff; /* difference array */
int ndiff; /* size of diff array */
int intflag; /* true if data are really integer */
int i, j, iter; /* loop indices */
int anynulls = 0; /* set if fdata contains any null values */
int nshift;
int first_nonnull = 0;
double mean, stdev; /* mean and RMS of differences */
double minval = 0., maxval = 0.; /* min & max of fdata */
double delta; /* bscale, 1 in idata = delta in fdata */
double zeropt; /* bzero */
double median; /* median of diff array */
double temp;
if (nx <= 1) {
*bscale = 1.;
*bzero = 0.;
return (0);
}
*iminval = INT32_MAX;
*imaxval = INT32_MIN;
/* Check to see if data are "floating point integer." */
/* This also catches the case where all the pixels are null */
intflag = 1; /* initial value */
for (i = 0; i < nx; i++) {
if (fdata[i] == in_null_value) {
idata[i] = NULL_VALUE;
anynulls = 1;
}
else if (fdata[i] > INT32_MAX ||
fdata[i] < NULL_VALUE + N_RESERVED_VALUES) {
intflag = 0; /* not integer */
break;
}
else {
idata[i] = (int)(fdata[i] + 0.5);
*iminval = minvalue(idata[i], *iminval);
*imaxval = maxvalue(idata[i], *imaxval);
if (idata[i] != fdata[i]) {
intflag = 0; /* not integer */
break;
}
}
}
if (intflag) { /* data are "floating point integer" */
if (anynulls) {
/* Shift the range of values so they lie close to NULL_VALUE. */
/* This will make the compression more efficient. */
nshift = *iminval - NULL_VALUE - N_RESERVED_VALUES;
for (i = 0; i < nx; i++) {
if (idata[i] != NULL_VALUE) {
idata[i] -= nshift;
}
}
*iminval = *iminval - nshift;
*imaxval = *imaxval - nshift;
*bscale = 1.;
*bzero = (double) nshift;
}
else {
/* there were no null values, so no need to shift the range */
*bscale = 1.;
*bzero = 0.;
}
return (1);
}
/* data are not "floating point integer"; need to quantize them */
/* find first non-null pixel, and initialize min and max values */
for (i = 0; i < nx; i++) {
if (fdata[i] != in_null_value) {
minval = fdata[i];
maxval = fdata[i];
first_nonnull = i;
break;
}
}
/* allocate temporary buffer for differences */
ndiff = nx - first_nonnull - 1;
if ((diff = malloc (ndiff * sizeof (float))) == NULL) {
ffpmsg("Out of memory in 'fits_quantize_double'.");
return (0);
}
/* calc ABS difference between successive non-null pixels */
j = first_nonnull;
ndiff = 0;
for (i = j + 1 ; i < nx; i++) {
if (fdata[i] != in_null_value) {
diff[ndiff] = fabs (fdata[i] - fdata[j]);
j = i;
ndiff++;
minval = minvalue(minval, fdata[i]);
maxval = maxvalue(maxval, fdata[i]);
}
}
/* check if there were any null values */
if (ndiff + 1 == nx)
anynulls = 0;
else
anynulls = 1;
/* use median of absolute deviations */
median = xMedian (diff, ndiff);
stdev = median * MEDIAN_TO_RMS;
/* substitute sigma-clipping if median is zero */
if (stdev == 0.0) {
/* calculate differences between non-null pixels */
j = first_nonnull;
ndiff = 0;
for (i = j + 1 ; i < nx; i++) {
if (fdata[i] != in_null_value) {
diff[ndiff] = fdata[i] - fdata[j];
j = i;
ndiff++;
}
}
FqMean (diff, ndiff, &mean, &stdev);
for (iter = 0; iter < NITER; iter++) {
j = 0;
for (i = 0; i < ndiff; i++) {
if (fabs (diff[i] - mean) < SIGMA_CLIP * stdev) {
if (j < i)
diff[j] = diff[i];
j++;
}
}
if (j == ndiff)
break;
ndiff = j;
FqMean (diff, ndiff, &mean, &stdev);
}
}
free (diff);
delta = stdev / pow (2., (double)noise_bits);
if (delta == 0. && ndiff > 0)
return (0); /* Zero variance in differences! Don't quantize. */
/* check that the range of quantized levels is not > range of int */
if ((maxval - minval) / delta > 2. * 2147483647 - N_RESERVED_VALUES )
return (0); /* don't quantize */
if (!anynulls) { /* don't have to check for nulls */
/* center the quantized levels around zero */
zeropt = (minval + maxval) / 2.;
for (i = 0; i < nx; i++) {
temp = (fdata[i] - zeropt) / delta;
idata[i] = NINT (temp);
}
}
else {
/* data contains null values; shift the range to be */
/* close to the value used to represent null values */
zeropt = minval - delta * (NULL_VALUE + N_RESERVED_VALUES);
for (i = 0; i < nx; i++) {
if (fdata[i] != in_null_value) {
temp = (fdata[i] - zeropt) / delta;
idata[i] = NINT (temp);
}
else
idata[i] = NULL_VALUE;
}
}
/* calc min and max values */
temp = (minval - zeropt) / delta;
*iminval = NINT (temp);
temp = (maxval - zeropt) / delta;
*imaxval = NINT (temp);
*bscale = delta;
*bzero = zeropt;
return (1); /* yes, data have been quantized */
}
/*---------------------------------------------------------------------------*/
/* This computes the mean and standard deviation. */
static void FqMean (float diff[], int ndiff, double *mean, double *stdev) {
int i;
double sum, sumsq;
double m; /* mean */
double xn; /* = ndiff */
double temp;
if (ndiff < 2) {
if (ndiff < 1)
*mean = 0.;
else
*mean = diff[0];
*stdev = 0.;
return;
}
xn = (double)ndiff;
sum = 0.;
sumsq = 0.;
for (i = 0; i < ndiff; i++) {
sum += diff[i];
sumsq += (diff[i] * diff[i]);
}
m = sum / xn;
*mean = m;
temp = (sumsq / xn - m*m) * xn;
if (temp <= 0)
*stdev = 0.;
else
*stdev = sqrt (temp / (xn-1.));
}
/*---------------------------------------------------------------------------*/
/* This returns an approximation to the median.
The input array will be clobbered.
*/
static float xMedian (float x[], int n) {
/* arguments:
float x[] io: the array (will be scrambled and possibly modified)
int n i: number of elements in x (modified locally)
*/
int i, j;
int next_n;
int npix;
int done;
float median = 0.;
if (n < 1) {
ffpmsg("xMedian: no data");
return (0.);
}
if (n == 1)
return (x[0]);
if (n == 2)
return ((x[0] + x[1]) / 2.);
done = 0;
while (!done) {
if (n < SORT_CUTOFF) {
qsort (x, n, sizeof (float), FqCompare);
if (n / 2 * 2 == n)
median = (x[n/2-1] + x[n/2]) / 2.;
else
median = x[n/2];
return (median);
}
/* ignore trailing groups of less than three elements */
next_n = (n + NELEM-3) / NELEM;
for (j = 0; j < next_n; j++) {
i = j * NELEM;
npix = minvalue (NELEM, n - j*NELEM);
InsertionSort (&x[i], npix);
switch (npix) {
case 1:
median = x[i];
break;
case 2:
median = (x[i] + x[i+1]) / 2.;
break;
case 3:
median = x[i+1];
break;
case 4:
median = (x[i+1] + x[i+2]) / 2.;
break;
case 5: /* NELEM = 5 */
median = x[i+2];
break;
default:
ffpmsg("npix should be 1..5");
}
x[j] = median;
}
if (next_n <= 1)
done = 1;
else
n = next_n;
}
return (x[0]);
}
/*---------------------------------------------------------------------------*/
static void InsertionSort (float x[], int n) {
float a;
int i, j;
for (j = 1; j < n; j++) {
a = x[j];
i = j - 1;
while (i >= 0 && x[i] > a) {
x[i+1] = x[i];
i--;
}
x[i+1] = a;
}
}
/*---------------------------------------------------------------------------*/
static int FqCompare (const void *vp, const void *vq) {
const float *p = vp;
const float *q = vq;
if (*p > *q)
return (1);
else if (*p < *q)
return (-1);
else
return (0);
}
|