File: ipol.c

package info (click to toggle)
spd 1.3.0-2
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 3,572 kB
  • sloc: ansic: 25,938; fortran: 10,483; sh: 1,032; makefile: 75
file content (1506 lines) | stat: -rw-r--r-- 51,407 bytes parent folder | download | duplicates (2)
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
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
/*
 *   Project: The SPD Image correction and azimuthal regrouping
 *                      http://forge.epn-campus.eu/projects/show/azimuthal
 *
 *   Copyright (C) 2005-2010 European Synchrotron Radiation Facility
 *                           Grenoble, France
 *
 *   Principal authors: P. Boesecke (boesecke@esrf.fr)
 *                      R. Wilcke (wilcke@esrf.fr)
 *
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU Lesser General Public License as published
 *   by the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU Lesser General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   and the GNU Lesser General Public License  along with this program.
 *   If not, see <http://www.gnu.org/licenses/>.
 */

# define IPOL_VERSION      "ipol : V1.7 Peter Boesecke 2010-09-29"
/*+++------------------------------------------------------------------------
NAME

   ipol --- interpolation routines

SYNOPSIS

   # include ipol.h

DESCRIPTION

   The module does not used externally defined routines and does not need
   to be linked to external libraries. 

HISTORY
  2005-12-04 V1.0 Extracted from SaxsRoutines V1.38 
  2005-12-09 V1.1 Array and dummy definitions from SaxsDefinitions.
                  Isum2ldw and Isum2ldwE: IPOL_ANTIALIASED
  2006-05-16 V1.2 Isum2ldw and Isum2ldwE: in vicinity to dummies *weight 
                  can be zero if IPOL_ANTIALIASED is set, even if cnt is not
                  zero. cnt is now explicitely set to 0 if *weight is 0.
  2007-02-26 V1.3 IpolRebin2 added
  2007-04-19 V1.4 code corrected to avoid compiler warnings with -Wall
  2008-05-25 V1.5 Isum2ldwE (calculation of varsum): if VarDat is NULL 
                  pvarval is not incremented and points always to 0,
                  unused variable pvarstart has been removed.
                  Isum2ldwEw (weighted sum), Isum2ldwE renamed to Isum2ldwEe.
                  Isum2ldwE uses Isum2ldwEe or Isum2ldwEw depending on
                  the switch variable IPolWeight.
  2008-05-27 V1.6 IPolMin: minimum coverage ratio of all non-dummy input 
                  pixels to the output pixel. 
                  If IPolMin*the output pixel area is larger than the sum
                  of all contributing input pixel areas, the calculated 
                  value is rejected (cnt=0). 

                  To do: the weight mode should also be used for rebinning,
                         probably better: pixels covering regions fully 
                         inside the output pixel should not be rejected, i.e.
                         that are contributing with 100%.
  2010-09-29 V1.7 IDX redefined using floor function

DESCRIPTION

----------------------------------------------------------------------------*/
/******************************************************************************
* Include Files                                                               *
******************************************************************************/

# include "ipol.h"

/******************************************************************************
* Private Macros                                                              *
******************************************************************************/

/******************************************************************************
* Private Variables                                                           *
******************************************************************************/

PRIVATE int IPolMode   = IPOL_NORMAL;
PRIVATE int IPolWeight = IPOL_EQUAL;
PRIVATE float IPolMin  = 0.5; // Minimum ratio of output to input pixel size

/******************************************************************************
* Routines                                                                    *
******************************************************************************/

/*---------------------------------------------------------------------------
NAME

  Ipolmode --- set/return interpolation mode 

DESCRIPTION
  Sets the interpolation mode and returns its new value:

    IPOL_NORMAL: normal, IPOL_ANTIALIASED: anti-aliased  

  If the input value is 0 only the current mode is returned.

---------------------------------------------------------------------------*/
int Ipolmode ( int mode )
{ switch (mode ) {
    case      IPOL_NORMAL: IPolMode = IPOL_NORMAL; break;
    case IPOL_ANTIALIASED: IPolMode = IPOL_ANTIALIASED; break;
  }
  return ( IPolMode );
} /* Ipolmode */

const char *Ipolmode2str ( int mode )
{ switch (mode ) {
    case      IPOL_NORMAL: return("normal");
    case IPOL_ANTIALIASED: return("antialiased");
    default: return("undefined");
  }
} /* Ipolmode2str */

/*---------------------------------------------------------------------------
NAME

  Ipolweight --- set/return weight method

DESCRIPTION
  Sets the method of weighting of data points and returns the new value:

    IPOL_EQUAL: each data point has equal weight, 
    IPOL_WEIGHTED: each point is weighted with its inverse variance 

  If the input value is 0 only the current method is returned.

---------------------------------------------------------------------------*/
int Ipolweight ( int method )
{ switch (method ) {
    case IPOL_EQUAL:    IPolWeight = IPOL_EQUAL; break;
    case IPOL_WEIGHTED: IPolWeight = IPOL_WEIGHTED; break;
  }
  return ( IPolWeight );
} /* Ipolweight */

const char *Ipolweight2str ( int method )
{ switch (method ) {
    case IPOL_EQUAL:    return("equal");
    case IPOL_WEIGHTED: return("weighted");
    default: return("undefined");
  }
} /* Ipolweight2str */

/*---------------------------------------------------------------------------
NAME

  Ipolmin --- set/return minimum ratio between input and output pixel area 

DESCRIPTION
  Sets the minimum accepted ratio between input and output pixel area
  If not used the default setting is 0.5. 

  If the input value is negative only the actually used value is returned.

---------------------------------------------------------------------------*/
float Ipolmin ( float minimum )
{ if (minimum>=0.0) IPolMin = minimum;
  return ( IPolMin );
} /* Ipolmin */

/*---------------------------------------------------------------------------
NAME
 
  Isum2ldwEw --- Weighted pixel area integral with variance array
 
PURPOSE
  Like Isum2ldwEw but each point is additionally weighted with 1/variance.
 
DESCRIPTION
  The output value "sum" is the weighted area integral of all pixel values in 
  the region between (f1_1,f1_2) and (f3_1,f3_2). If one of the pixels lies 
  outside the range of the image or if it is a dummy it is ignored (wi=0):
  All pixels are weighted with 1/variance. 
  If at least one pixel of the sum has zero variance only the pixels with
  variance 0 will contribute to the sum according to the covered area.

  The output value "varsum" is the area integral of all variance values
  in the same region weighted with 1/variance. It is effectively the sum of
  all contributing pixels. If the variance value of at least one non-dummy 
  pixel in the region is negative "varsum" cannot be calculated and is set 
  to -1.0.
 
    area      = (f1_2-f1_1)*(f3_2,f3_1)
    sum       = vsign * Sum(Ii*wi/Ei)/Mean(1/Ei) 
                  (vsign * Sum(Ii*wi) if one of the Ei is 0)
    weight    = vsign * Sum(wi/Ei)/Mean(1/Ei) 
                  (vsign * Sum(wi) if one of the Ei is 0)
    varsum    = Sum(wi)/Mean(1/Ei) (Sum(Ei*wi) if one of the Ei is 0)
                  or -1.0 if it cannot be calculated
    varweight = Sum(wi/Ei)/Mean(1/Ei) (Sum(wi) if one of the Ei is 0)
    cnt       = number of contributing intensity values

           where Mean(1/Ei) = Sum(1/Ei)/cnt
 
    wi = overlap area of pixel i with [(f1_1,f1_2)..(f3_1,f3_2)]
 
  For non-zero weight, the averaged values are Sum(Ii*wi/Ei)/Sum(wi/Ei) and
  Sum(wi)/Sum(wi/Ei). vsign takes into account the direction of integration.
  If the integration along a single axis is done from positive to negative 
  coordinates vsign is negative, otherwise positive.

  The variance values are always positive, independent of the integration
  direction. If the variance sum varsum is negative it is invalid.
  If VarDat is NULL, sum, weight and varweight are calculated, varsum is 
  0.0 for all non-dummy pixels inside the array and negative for all pixels
  outside the array.

SYNOPSIS
 
  int Isum2ldwEw           ( float *Data, float *VarDat, int Dim_1, int Dim_2,
                             float Dummy, float DDummy,
                             float f1_1, float f1_2,  float f3_1, float f3_2,
                             float *sum, float *weight,
                             float *varsum, float *varweight);

  return value            (o)   : number of pixels contributing to the output
                                  value (0..4), if 0, no valid data point found.
  float Data[Dim_1,Dim_2]   (i) : input data array
  float VarDat[Dim_1,Dim_2] (i) : input variance array
  int Dim_1, Dim_2          (i) : dimension of array
  float Dummy, DDummy       (i) : dummy specification
  float f1_1, f1_2          (i) : index coordinate of lower left corner
  float f3_1, f3_2          (i) : index coordinate of upper right corner
  float *sum                (o) : area integral of data array (Data)
  float *weight             (o) : area of contributing pixels to sum
  float *varsum             (o) : area integral of variance array (VarDat)
                                  *varsum is set to -1.0 if one of the 
                                  contributing non-dummy pixels is negative.
  float *varweight          (o) : area of contributing pixels
 
---------------------------------------------------------------------------*/
int Isum2ldwEw             ( float *Data, float *VarDat, int Dim_1, int Dim_2,
                             float Dummy, float DDummy,
                             float f1_1, float f1_2,  float f3_1, float f3_2,
                             float *sum, float *weight, 
                             float *varsum, float *varweight)
{ // const float eps = IPOLEPS; // unused
  int cnt, varcnt;
  float *pval, *pstart;
  float *pvarval, varval, invvarval, wdvarval;
  float vsign=1.0, vvar=1.0;
  float w;
  float w1_1,w1_2,w3_1,w3_2;
  float null = 0.0;

  float weightv, sumv, varsumv;
  int cntv, varcntv;
  float suminvvar, meaninvvar;
  int varflag = 1; // set to 0 if one of the variances is 0
 
  int   i_1,  i_2;
  int   i1_1, i1_2, i3_1, i3_2;
  float Df_1, Df_2, Df_12;
  float Df_1A, Df_2A, Df_12A;
  float f_1A, f_2A;
  float tmp;
 
  Df_1  = f3_1-f1_1;
  Df_2  = f3_2-f1_2;
  Df_12 = Df_1*Df_2;
 
  /* sort range */
  if (Df_1<0) {tmp=f1_1;f1_1=f3_1;f3_1=tmp;vsign*=-1.0;Df_1=-Df_1;}
  if (Df_2<0) {tmp=f1_2;f1_2=f3_2;f3_2=tmp;vsign*=-1.0;Df_2=-Df_2;}
 
  *weight=0.0;
  *varweight=0.0;
  *sum=0.0;
  *varsum=-1.0;
  cnt=0;
  varcnt=0;

  weightv = 0.0;
  sumv = 0.0;
  varsumv = 0.0;
  cntv = 0;
  varcntv = 0;

  suminvvar = 0.0;
 
  if (!Data) return(cnt);    // return, if NULL pointer

  if (IPolMode == IPOL_ANTIALIASED) {
    /* Increase the integration range in both directions to 1. For
       renormalization of the results adjust vsign. */
    if (Df_1<=1.0) {
      Df_1A = 1.0;
      f_1A  = 0.5*(f1_1+f3_1);
      f1_1  =f_1A-0.5;f3_1=f_1A+0.5;
    } else Df_1A = Df_1;

    if (Df_2<=1.0) {
      Df_2A = 1.0;
      f_2A  = 0.5*(f1_2+f3_2);
      f1_2  =f_2A-0.5;f3_2=f_2A+0.5;
    } else Df_2A = Df_2;

    Df_12A = Df_1A*Df_2A;
    if (Df_12A == 0.0) return(cnt);

    tmp = Df_12/Df_12A;
    vvar  *= tmp;
    vsign *= tmp;
  }
 
  /* Add 0.5 to exclude negative array indices */
  f1_1+=0.5; f1_2+=0.5; f3_1+=0.5; f3_2+=0.5;
 
  // Lower left edge f1, w1_i is the overlap area of pixel p1
  // All w1_i are positive: 0<=w1_i<=1.0
  i1_1 = floor(f1_1);
  if (i1_1 >= 0 ) w1_1 = 1.0 - (f1_1 - i1_1);
    else {i1_1 = 0; w1_1 = 1.0; }
 
  i1_2 = floor(f1_2);
  if (i1_2 >= 0 ) w1_2 = 1.0 - (f1_2 - i1_2);
    else {i1_2 = 0; w1_2 = 1.0; }
 
  // upper right edge f3, w3_i is the overestimated area of pixel p3
  // All w3_i are negative or zero: -1.0<=w3_i<=0.0
  i3_1 = ceil(f3_1);
  if (i3_1<=Dim_1) w3_1 = f3_1 - i3_1;
    else { i3_1 = Dim_1; w3_1 = 0.0; }
 
  i3_2 = ceil(f3_2);
  if (i3_2<=Dim_2) w3_2 = f3_2 - i3_2;
    else { i3_2 = Dim_2; w3_2 = 0.0; }
 
  // Stop, if already the lower left pixel p1 lies outside the array.
  // or if upper right pixel lies outside the array.
  if ((i1_1>=Dim_1)||((i1_2>=Dim_2))||(i3_1<0)||(i3_2<0)) return(cnt);

  pvarval = &null; // if VarDat is NULL
  *varsum = 0.0;

  cntv = 0;
  suminvvar = 0.0;
  weightv = 0.0;
  sumv = 0.0;

  varcntv = 0;
  varsumv = 0.0; 

  varflag = 1; // set to 0 if one of the variances is 0
 
  // p1 (lower left pixel)
  pstart = pval = ABSPTR(Data,Dim_1,Dim_2,i1_1,i1_2);
  if (VarDat) pvarval = VarDat-Data+pval;
  w = w1_1*w1_2;
  if NODUMMY(*pval,Dummy,DDummy) {
    varval=*pvarval;
    if (varval==0.0) {
      cnt++; *weight+=w; 
      *sum+=*pval*w; 
      //*varsum+=varval*w;
      varcnt++; 
      varflag=0; 
    } else {
      if ((varflag)&&(varval>0)) {
        invvarval=1.0/varval;
        wdvarval=w*invvarval;
        cntv++; weightv+=wdvarval;
        sumv+=*pval*wdvarval; 
        suminvvar+=invvarval;
        varsumv+=w; varcntv++; 
      }
    }
  }

  // lower line between p1 and p2
  for (i_1=i1_1+1;i_1<i3_1;i_1++) {
    pval = NEXTCOL(pval,Dim_1,Dim_2);
    if (VarDat) pvarval = VarDat-Data+pval;
    w=w1_2;
    if NODUMMY(*pval,Dummy,DDummy) {
      varval=*pvarval;
      if (varval==0.0) {
        cnt++; *weight+=w;
        *sum+=*pval*w;
        //*varsum+=varval*w;
        varcnt++;
        varflag=0;
      } else {
        if ((varflag)&&(varval>0)) {
          invvarval=1.0/varval;
          wdvarval=w*invvarval;
          cntv++; weightv+=wdvarval;
          sumv+=*pval*wdvarval;
          suminvvar+=invvarval;
          varsumv+=w; varcntv++;
        }
      }
    }
  }
 
  // p2 (correct overestimation)
  w = w3_1*w1_2;
  if NODUMMY(*pval,Dummy,DDummy) {
    varval=*pvarval;
    if (varval==0.0) {
      cnt++; *weight+=w;
      *sum+=*pval*w;
      //*varsum+=varval*w;
      varcnt++;
      varflag=0;
    } else {
      if ((varflag)&&(varval>0)) {
        invvarval=1.0/varval;
        wdvarval=w*invvarval;
        cntv++; weightv+=wdvarval;
        sumv+=*pval*wdvarval;
        suminvvar+=invvarval;
        varsumv+=w; varcntv++;
      }
    }
  }

  for (i_2=i1_2+1;i_2<i3_2;i_2++) {
    // first column
    pstart = pval = NEXTROW(pstart,Dim_1,Dim_2);
    if (VarDat) pvarval = VarDat-Data+pval;
    w=w1_1;
    if NODUMMY(*pval,Dummy,DDummy) {
      varval=*pvarval;
      if (varval==0.0) {
        cnt++; *weight+=w;
        *sum+=*pval*w;
        //*varsum+=varval*w;
        varcnt++;
        varflag=0;
      } else {
        if ((varflag)&&(varval>0)) {
          invvarval=1.0/varval;
          wdvarval=w*invvarval;
          cntv++; weightv+=wdvarval;
          sumv+=*pval*wdvarval;
          suminvvar+=invvarval;
          varsumv+=w; varcntv++;
        }
      }
    }

    // line
    for (i_1=i1_1+1;i_1<i3_1;i_1++) {
      pval = NEXTCOL(pval,Dim_1,Dim_2);
      if (VarDat) pvarval = VarDat-Data+pval;
      // w=1.0
      if NODUMMY(*pval,Dummy,DDummy) {
        varval=*pvarval;
        if (varval==0.0) {
          cnt++; *weight+=1.0;
          *sum+=*pval;
          //*varsum+=varval;
          varcnt++;
          varflag=0;
        } else {
          if ((varflag)&&(varval>0)) {
            invvarval=1.0/varval;
            cntv++; weightv+=invvarval;
            sumv+=*pval*invvarval;
            suminvvar+=invvarval;
            varsumv+=1.0; varcntv++;
          }
        }
      }
    }
 
    // last column (correct overestimation)
    w=w3_1;
    if NODUMMY(*pval,Dummy,DDummy) {
      varval=*pvarval;
      if (varval==0.0) {
        cnt++; *weight+=w;
        *sum+=*pval*w;
        //*varsum+=varval*w;
        varcnt++;
        varflag=0;
      } else {
        if ((varflag)&&(varval>0)) {
          invvarval=1.0/varval;
          wdvarval=w*invvarval;
          cntv++; weightv+=wdvarval;
          sumv+=*pval*wdvarval;
          suminvvar+=invvarval;
          varsumv+=w; varcntv++;
        }
      }
    }
  }
 
  // p4 (correct overestimation)
  pval = pstart;
  if (VarDat) pvarval = VarDat-Data+pval;
  w = w1_1*w3_2;
  if NODUMMY(*pval,Dummy,DDummy) {
    varval=*pvarval;
    if (varval==0.0) {
      cnt++; *weight+=w;
      *sum+=*pval*w;
      //*varsum+=varval*w;
      varcnt++;
      varflag=0;
    } else {
      if ((varflag)&&(varval>0)) {
        invvarval=1.0/varval;
        wdvarval=w*invvarval;
        cntv++; weightv+=wdvarval;
        sumv+=*pval*wdvarval;
        suminvvar+=invvarval;
        varsumv+=w; varcntv++;
      }
    }
  }

  // upper line between p4 and p3 (correction)
  for (i_1=i1_1+1;i_1<i3_1;i_1++) {
    pval = NEXTCOL(pval,Dim_1,Dim_2);
    if (VarDat) pvarval = VarDat-Data+pval;
    w=w3_2;
    if NODUMMY(*pval,Dummy,DDummy) {
      varval=*pvarval;
      if (varval==0.0) {
        cnt++; *weight+=w;
        *sum+=*pval*w;
        //*varsum+=varval*w;
        varcnt++;
        varflag=0;
      } else {
        if ((varflag)&&(varval>0)) {  
          invvarval=1.0/varval;
          wdvarval=w*invvarval;
          cntv++; weightv+=wdvarval;
          sumv+=*pval*wdvarval;
          suminvvar+=invvarval;
          varsumv+=w; varcntv++;
        }
      }
    }
  }
 
  // p3 (correct underestimation)
  w = w3_1*w3_2;
  if NODUMMY(*pval,Dummy,DDummy) {
    varval=*pvarval;
    if (varval==0.0) {
      cnt++; *weight+=w;
      *sum+=*pval*w;
      //*varsum+=varval*w;
      varcnt++;
      varflag=0;
    } else {
      if ((varflag)&&(varval>0)) {  
        invvarval=1.0/varval;
        wdvarval=w*invvarval;
        cntv++; weightv+=wdvarval;
        sumv+=*pval*wdvarval;
        suminvvar+=invvarval;
        varsumv+=w; varcntv++;
      }
    }
  }

  if (varflag) {
    meaninvvar = suminvvar/cntv;
    cnt        = cntv;
    varcnt     = varcntv;
    *sum       = sumv/meaninvvar;
    *weight    = weightv/meaninvvar;
    *varsum    = varsumv/meaninvvar;
  }

  *sum      *= vsign;
  *weight   *= vsign;

  if (varcnt!=cnt) *varsum = -1.0; else *varsum *= vvar;

  *varweight = fabs(*weight);

  // *weight can apparently be zero when cnt is not if IPOL_ANTIALIASED is set
  if (fabs(*weight)<1e-32) cnt=0;
  else // reject pixels with less than IPolMin coverage
    if ( fabs(Df_12)*IPolMin > fabs(*weight) ) cnt=0;

  return(cnt);
 
} /* Isum2ldwEw */

/*---------------------------------------------------------------------------
NAME
 
  Isum2ldwEe--- Pixel area integral with variance array
 
PURPOSE
  Like Isum2ldw but additionally with calculation of variance array.
 
DESCRIPTION
  The output value "sum" is the area integral of all pixel values in the region
  between (f1_1,f1_2) and (f3_1,f3_2). If one of the pixels lies outside the
  range of the image or if it is a dummy it is ignored (wi=0):

  The output value "varsum" is the area integral of all variance values
  in the same region. If the variance value of at least one non-dummy pixel in
  the region is negative "varsum" cannot be calculated and is set to -1.0.
 
    area      = (f1_2-f1_1)*(f3_2,f3_1)
    sum       = vsign * Sum(Ii*wi)
    weight    = vsign * Sum(wi)
    varsum    = Sum(Ei*wi) or -1.0 if it cannot be calculated
    varweight = Sum(wi)
    cnt       = number of contributing intensity values
 
    wi = overlap area of pixel i with [(f1_1,f1_2)..(f3_1,f3_2)]
 
  For non-zero weight, the averaged values are Sum(Ii*wi)/Sum(wi) and
  Sum(Ei*wi)/Sum(wi). vsign takes into account the direction of integration.
  If the integration along a single axis is done from positive to negative 
  coordinates vsign is negative, otherwise positive.

  The variance values are always positive, independent of the integration
  direction. If the variance sum varsum is negative it is invalid.
  If VarDat is NULL, sum, weight and varweight are calculated, varsum is 
  0.0 for all non-dummy pixels inside the array and negative for all pixels
  outside the array.

SYNOPSIS
 
  int Isum2ldwEe           ( float *Data, float *VarDat, int Dim_1, int Dim_2,
                             float Dummy, float DDummy,
                             float f1_1, float f1_2,  float f3_1, float f3_2,
                             float *sum, float *weight,
                             float *varsum, float *varweight);

  return value            (o)   : number of pixels contributing to the output
                                  value (0..4), if 0, no valid data point found.
  float Data[Dim_1,Dim_2]   (i) : input data array
  float VarDat[Dim_1,Dim_2] (i) : input variance array
  int Dim_1, Dim_2          (i) : dimension of array
  float Dummy, DDummy       (i) : dummy specification
  float f1_1, f1_2          (i) : index coordinate of lower left corner
  float f3_1, f3_2          (i) : index coordinate of upper right corner
  float *sum                (o) : area integral of data array (Data)
  float *weight             (o) : area of contributing pixels to sum
  float *varsum             (o) : area integral of variance array (VarDat)
                                  *varsum is set to -1.0 if one of the 
                                  contributing non-dummy pixels is negative.
  float *varweight          (o) : area of contributing pixels
 
---------------------------------------------------------------------------*/
int Isum2ldwEe             ( float *Data, float *VarDat, int Dim_1, int Dim_2,
                             float Dummy, float DDummy,
                             float f1_1, float f1_2,  float f3_1, float f3_2,
                             float *sum, float *weight, 
                             float *varsum, float *varweight)
{ // const float eps = IPOLEPS; // unused
  int cnt, varcnt;
  float *pval, *pstart;
  float *pvarval;
  float vsign=1.0, vvar=1.0;
  float w;
  float w1_1,w1_2,w3_1,w3_2;
  float null = 0.0;
 
  int   i_1,  i_2;
  int   i1_1, i1_2, i3_1, i3_2;
  float Df_1, Df_2, Df_12;
  float Df_1A, Df_2A, Df_12A;
  float f_1A, f_2A;
  float tmp;
 
  Df_1  = f3_1-f1_1;
  Df_2  = f3_2-f1_2;
  Df_12 = Df_1*Df_2;
 
  /* sort range */
  if (Df_1<0) {tmp=f1_1;f1_1=f3_1;f3_1=tmp;vsign*=-1.0;Df_1=-Df_1;}
  if (Df_2<0) {tmp=f1_2;f1_2=f3_2;f3_2=tmp;vsign*=-1.0;Df_2=-Df_2;}
 
  *weight=0.0;
  *varweight=0.0;
  *sum=0.0;
  *varsum=-1.0;
  cnt=0;
  varcnt=0;
 
  if (!Data) return(cnt);    // return, if NULL pointer

  if (IPolMode == IPOL_ANTIALIASED) {
    /* Increase the integration range in both directions to 1. For
       renormalization of the results adjust vsign. */
    if (Df_1<=1.0) {
      Df_1A = 1.0;
      f_1A  = 0.5*(f1_1+f3_1);
      f1_1  =f_1A-0.5;f3_1=f_1A+0.5;
    } else Df_1A = Df_1;

    if (Df_2<=1.0) {
      Df_2A = 1.0;
      f_2A  = 0.5*(f1_2+f3_2);
      f1_2  =f_2A-0.5;f3_2=f_2A+0.5;
    } else Df_2A = Df_2;

    Df_12A = Df_1A*Df_2A;
    if (Df_12A == 0.0) return(cnt);

    tmp = Df_12/Df_12A;
    vvar  *= tmp;
    vsign *= tmp;
  }
 
  /* Add 0.5 to exclude negative array indices */
  f1_1+=0.5; f1_2+=0.5; f3_1+=0.5; f3_2+=0.5;
 
  // Lower left edge f1, w1_i is the overlap area of pixel p1
  // All w1_i are positive: 0<=w1_i<=1.0
  i1_1 = floor(f1_1);
  if (i1_1 >= 0 ) w1_1 = 1.0 - (f1_1 - i1_1);
    else {i1_1 = 0; w1_1 = 1.0; }
 
  i1_2 = floor(f1_2);
  if (i1_2 >= 0 ) w1_2 = 1.0 - (f1_2 - i1_2);
    else {i1_2 = 0; w1_2 = 1.0; }
 
  // upper right edge f3, w3_i is the overestimated area of pixel p3
  // All w3_i are negative or zero: -1.0<=w3_i<=0.0
  i3_1 = ceil(f3_1);
  if (i3_1<=Dim_1) w3_1 = f3_1 - i3_1;
    else { i3_1 = Dim_1; w3_1 = 0.0; }
 
  i3_2 = ceil(f3_2);
  if (i3_2<=Dim_2) w3_2 = f3_2 - i3_2;
    else { i3_2 = Dim_2; w3_2 = 0.0; }
 
  // Stop, if already the lower left pixel p1 lies outside the array.
  // or if upper right pixel lies outside the array.
  if ((i1_1>=Dim_1)||((i1_2>=Dim_2))||(i3_1<0)||(i3_2<0)) return(cnt);

  pvarval = &null; // if VarDat is NULL
  *varsum = 0.0;
 
  // p1 (lower left pixel)
  pstart = pval = ABSPTR(Data,Dim_1,Dim_2,i1_1,i1_2);
  if (VarDat) pvarval = VarDat-Data+pval;
  w = w1_1*w1_2;
  if NODUMMY(*pval,Dummy,DDummy) { 
    cnt++; *weight+=w; 
    *sum+=*pval*w; 
    if (*pvarval>=0) { *varsum+=*pvarval*w; varcnt++; }
  }
  // lower line between p1 and p2
  for (i_1=i1_1+1;i_1<i3_1;i_1++) {
    pval = NEXTCOL(pval,Dim_1,Dim_2);
    if (VarDat) pvarval = VarDat-Data+pval;
    if NODUMMY(*pval,Dummy,DDummy) { 
      cnt++; *weight+=w1_2; 
      *sum+=*pval*w1_2; 
      if (*pvarval>=0) { *varsum+=*pvarval*w1_2; varcnt++; } 
    }
  }
 
  // p2 (correct overestimation)
  w = w3_1*w1_2;
  if NODUMMY(*pval,Dummy,DDummy) { 
    cnt++; *weight+=w; 
    *sum+=*pval*w; 
    if (*pvarval>=0) { *varsum+=*pvarval*w; varcnt++; } 
  }
 
  for (i_2=i1_2+1;i_2<i3_2;i_2++) {
    // first column
    pstart = pval = NEXTROW(pstart,Dim_1,Dim_2);
    if (VarDat) pvarval = VarDat-Data+pval;
    if NODUMMY(*pval,Dummy,DDummy) { 
      cnt++; *weight+=w1_1; 
      *sum+=*pval*w1_1; 
      if (*pvarval>=0) { *varsum+=*pvarval*w1_1; varcnt++; } 
    }
 
    // line
    for (i_1=i1_1+1;i_1<i3_1;i_1++) {
      pval = NEXTCOL(pval,Dim_1,Dim_2);
      if (VarDat) pvarval = VarDat-Data+pval;
      if NODUMMY(*pval,Dummy,DDummy) { 
        cnt++; *weight+=1.0; 
        *sum+=*pval; 
        if (*pvarval>=0) { *varsum+=*pvarval; varcnt++; }
      }
    }
 
    // last column (correct overestimation)
    if NODUMMY(*pval,Dummy,DDummy) { 
      cnt++; *weight+=w3_1; 
      *sum+=*pval*w3_1; 
      if (*pvarval>=0) { *varsum+=*pvarval*w3_1; varcnt++; }
    }
  }
 
  // p4 (correct overestimation)
  pval = pstart;
  if (VarDat) pvarval = VarDat-Data+pval;
  w = w1_1*w3_2;
  if NODUMMY(*pval,Dummy,DDummy) { 
    cnt++; *weight+=w; 
    *sum+=*pval*w; 
    if (*pvarval>=0) { *varsum+=*pvarval*w; varcnt++; }
  }
 
  // upper line between p4 and p3 (correction)
  for (i_1=i1_1+1;i_1<i3_1;i_1++) {
    pval = NEXTCOL(pval,Dim_1,Dim_2);
    if (VarDat) pvarval = VarDat-Data+pval;
    if NODUMMY(*pval,Dummy,DDummy) { 
      cnt++; *weight+=w3_2; 
      *sum+=*pval*w3_2; 
      if (*pvarval>=0) { *varsum+=*pvarval*w3_2; varcnt++; }
    }
  }
 
  // p3 (correct underestimation)
  w = w3_1*w3_2;
  if NODUMMY(*pval,Dummy,DDummy) { 
    cnt++; *weight+=w; 
    *sum+=*pval*w; 
    if (*pvarval>=0) { *varsum+=*pvarval*w; varcnt++; }
  }

  *sum      *= vsign;
  *weight   *= vsign;

  if (varcnt!=cnt) *varsum = -1.0; else *varsum *= vvar;

  *varweight = fabs(*weight);

  // *weight can apparently be zero when cnt is not if IPOL_ANTIALIASED is set
  if (fabs(*weight)<1e-32) cnt=0;
  else // reject pixels with less than IPolMin coverage
    if ( fabs(Df_12)*IPolMin > fabs(*weight) ) cnt=0;

  return(cnt);
 
} /* Isum2ldwEe*/

/*---------------------------------------------------------------------------
NAME

  Isum2ldwE --- Pixel area integral with variance array
 
PURPOSE
  Use Isum2ldwEe or Isum2ldwEw depending on IPolWeight.
 
DESCRIPTION
  See Isum2ldwEe and Isum2ldwEw.
---------------------------------------------------------------------------*/
int Isum2ldwE              ( float *Data, float *VarDat, int Dim_1, int Dim_2,
                             float Dummy, float DDummy,
                             float f1_1, float f1_2,  float f3_1, float f3_2,
                             float *sum, float *weight,
                             float *varsum, float *varweight)
{ int cnt=0;
  switch (IPolWeight) {
    case IPOL_EQUAL:
      cnt = Isum2ldwEe   ( Data, VarDat, Dim_1, Dim_2,
                             Dummy, DDummy, f1_1, f1_2,  f3_1, f3_2,
                             sum, weight, varsum, varweight); break;
    case IPOL_WEIGHTED:
      cnt = Isum2ldwEw   ( Data, VarDat, Dim_1, Dim_2,
                             Dummy, DDummy, f1_1, f1_2,  f3_1, f3_2,
                             sum, weight, varsum, varweight); break;
  }
  return( cnt );
} // Isum2ldwE

/*---------------------------------------------------------------------------
NAME

  Isum2ldw --- Pixel area integral

PURPOSE
  Area integral of the array Data between index coordinate (f1_1,f1_2) and 
  index coordinate (f3_1,f3_2). Dummy values and array limits are checked. 
  The contributing area is returned in weight. The intensity value per
  pixel area is sum divided by weight. The return value of the function is the 
  number of contributing pixels. 
  A call with (f3_1,f3_2) = (f1_1+1,f1_2+1) corresponds to a call to Ipol2ldw
  with (f_1,f_2) = (f1_1+0.5,f1_2+0.5). See also Ipol2ldw.

  If the interpolation mode is IPOL_ANTIALIASED (default), the minimum side 
  length of an integration area is 1. Smaller side lengths are increased to 1 
  and the results are renomalized to match the original side lengths. 

DESCRIPTION
  The output value "sum" is the area integral of all pixel values in the region 
  between (f1_1,f1_2) and (f3_1,f3_2). If one of the pixels lies outside the 
  range of the image or if it is a dummy it is ignored (wi=0):
 
    sum    = Sum(Ii*wi)
    weight = Sum(wi)
    cnt    = number of contributing intensity values
 
    wi = overlap area of pixel i with [(f1_1,f1_2)..(f3_1,f3_2)] 
 
  For non-zero weight, the averaged value is Sum(Ii*wi)/Sum(wi).
 
              --------------------- --------------------- ---------------------
   f3_2     - |             ******| |*******************| |**************     |
              |             ******|.|*******************|.|**************     |
              |        p4- -*-*-*-|.|-*-*-*-*-*-*-*-*-*-|.|-*-*-*-*p3****     |
              |         |   ******|.|*******************|.|*********|****     |
              |             ******| |*******************| |**************     |
              --------------------- --------------------- ---------------------
                       ...                   ...                   ...
              --------------------- --------------------- ---------------------
              |             ******| |*******************| |*********|****     |
  f = ***     |         |   ******|.|*******************|.|**************     |
              |             ******|.|*******************|.|*********|****     |
              |         |   ******|.|*******************|.|**************     |
              |             ******| |*******************| |*********|****     |
              --------------------- --------------------- ---------------------
                       ...                   ...                   ...
              --------------------- --------------------- ---------------------
   f1_2     - |             ******| |*******************| |**************     |
              |         |         |.|                   |.|         |         |
              |        p1- - - - -|.|- - - - - - - - - -|.|- - - - p2         |
              |                   |.|                   |.|                   |
              |                   | |                   | |                   |
              --------------------- --------------------- ---------------------
                            |                                           |
                          f1_1                                        f3_1

 
SYNOPSIS
 
  int Isum2ldw ( float *Data, int Dim_1, int Dim_2, float Dummy, float DDummy,
                 float f1_1, float f1_2,  float f3_1, float f3_2,
                 float *sum, float *weight);
 
  return value            (o)   : number of pixels contributing to the output
                                  value (0..4), if 0, no valid data point found.
  float Data[Dim_1,Dim_2] (i)   : input array
  int Dim_1, Dim_2        (i)   : dimension of array
  float Dummy, DDummy     (i)   : dummy specification
  float f1_1, f1_2        (i)   : index coordinate of lower left corner
  float f3_1, f3_2        (i)   : index coordinate of upper right corner 
  float *sum              (o)   : area integral of Data array
  float *weight           (o)   : area of contributing pixels 
  
---------------------------------------------------------------------------*/
int Isum2ldw               ( float *Data, int Dim_1, int Dim_2,
                             float Dummy, float DDummy,
                             float f1_1, float f1_2,  float f3_1, float f3_2,
                             float *sum, float *weight)
{ // const float eps = IPOLEPS; // unused
  int cnt=0;
  float *pval, *pstart;
  float vsign=1.0;
  float w;
  float w1_1,w1_2,w3_1,w3_2;
 
  int   i_1,  i_2;
  int   i1_1, i1_2, i3_1, i3_2;
  float Df_1, Df_2, Df_12;
  float Df_1A, Df_2A, Df_12A;
  float f_1A, f_2A;
  float tmp;

  Df_1  = f3_1-f1_1;
  Df_2  = f3_2-f1_2;
  Df_12 = Df_1*Df_2;

  /* use faster routine Ipol2ldw, if area size is 1x1 */
  if ((Df_1==1.0)&&(Df_2==1.0)) 
   return(Ipol2ldw(Data,Dim_1,Dim_2,Dummy,DDummy,f1_1+0.5,f1_2+0.5,sum,weight));

  /* sort range */
  if (Df_1<0) {tmp=f1_1;f1_1=f3_1;f3_1=tmp;vsign*=-1.0;Df_1=-Df_1;}
  if (Df_2<0) {tmp=f1_2;f1_2=f3_2;f3_2=tmp;vsign*=-1.0;Df_2=-Df_2;}

  *weight=0.0;
  *sum=0.0;
  cnt=0;

  if (!Data) return(cnt); // return, if NULL pointer
 
  if (IPolMode == IPOL_ANTIALIASED) {
    /* Increase the integration range in both directions to 1. For 
       renormalization of the results adjust vsign. */ 
    if (Df_1<=1.0) {
      Df_1A = 1.0;
      f_1A  = 0.5*(f1_1+f3_1);
      f1_1  =f_1A-0.5;f3_1=f_1A+0.5;
    } else Df_1A = Df_1;

    if (Df_2<=1.0) {
      Df_2A = 1.0;
      f_2A  = 0.5*(f1_2+f3_2);
      f1_2  =f_2A-0.5;f3_2=f_2A+0.5;
    } else Df_2A = Df_2;

    Df_12A = Df_1A*Df_2A;

    if (Df_12A == 0.0) return(cnt);

    vsign*=Df_12/Df_12A;
  }

  /* Add 0.5 to exclude negative array indices */
  f1_1+=0.5; f1_2+=0.5; f3_1+=0.5; f3_2+=0.5;

  // Lower left edge f1, w1_i is the overlap area of pixel p1
  // All w1_i are positive: 0<=w1_i<=1.0
  i1_1 = floor(f1_1);
  if (i1_1 >= 0 ) w1_1 = 1.0 - (f1_1 - i1_1);
    else {i1_1 = 0; w1_1 = 1.0; } 

  i1_2 = floor(f1_2);
  if (i1_2 >= 0 ) w1_2 = 1.0 - (f1_2 - i1_2);
    else {i1_2 = 0; w1_2 = 1.0; }

  // upper right edge f3, w3_i is the overestimated area of pixel p3
  // All w3_i are negative or zero: -1.0<=w3_i<=0.0
  i3_1 = ceil(f3_1); 
  if (i3_1<=Dim_1) w3_1 = f3_1 - i3_1; 
    else { i3_1 = Dim_1; w3_1 = 0.0; }

  i3_2 = ceil(f3_2);
  if (i3_2<=Dim_2) w3_2 = f3_2 - i3_2;
    else { i3_2 = Dim_2; w3_2 = 0.0; }

  // Stop, if already the lower left pixel p1 lies outside the array.
  // or if upper right pixel lies outside the array.
  if ((i1_1>=Dim_1)||((i1_2>=Dim_2))||(i3_1<0)||(i3_2<0)) return(cnt);

  // p1 (lower left pixel)
  pstart = pval = ABSPTR(Data,Dim_1,Dim_2,i1_1,i1_2);
  w = w1_1*w1_2;
  if NODUMMY(*pval,Dummy,DDummy) { cnt++; *weight+=w; *sum+=*pval*w; }

  // lower line between p1 and p2
  for (i_1=i1_1+1;i_1<i3_1;i_1++) {
    pval = NEXTCOL(pval,Dim_1,Dim_2);
    if NODUMMY(*pval,Dummy,DDummy) { cnt++; *weight+=w1_2; *sum+=*pval*w1_2; }
  }

  // p2 (correct overestimation)
  w = w3_1*w1_2;
  if NODUMMY(*pval,Dummy,DDummy) { cnt++; *weight+=w; *sum+=*pval*w; }

  for (i_2=i1_2+1;i_2<i3_2;i_2++) {
    // first column
    pstart = pval = NEXTROW(pstart,Dim_1,Dim_2);
    if NODUMMY(*pval,Dummy,DDummy) { cnt++; *weight+=w1_1; *sum+=*pval*w1_1; }

    // line
    for (i_1=i1_1+1;i_1<i3_1;i_1++) {
      pval = NEXTCOL(pval,Dim_1,Dim_2);
      if NODUMMY(*pval,Dummy,DDummy) { cnt++; *weight+=1.0; *sum+=*pval; }
    }

    // last column (correct overestimation)
    if NODUMMY(*pval,Dummy,DDummy) { cnt++; *weight+=w3_1; *sum+=*pval*w3_1; }
  }

  // p4 (correct overestimation)
  pval = pstart;
  w = w1_1*w3_2;
  if NODUMMY(*pval,Dummy,DDummy) { cnt++; *weight+=w; *sum+=*pval*w; }

  // upper line between p4 and p3 (correction)
  for (i_1=i1_1+1;i_1<i3_1;i_1++) {
    pval = NEXTCOL(pval,Dim_1,Dim_2);
    if NODUMMY(*pval,Dummy,DDummy) { cnt++; *weight+=w3_2; *sum+=*pval*w3_2; }
  }

  // p3 (correct underestimation)
  w = w3_1*w3_2;
  if NODUMMY(*pval,Dummy,DDummy) { cnt++; *weight+=w; *sum+=*pval*w; }

  *sum    *= vsign;
  *weight *= vsign;

  // *weight can apparently be zero when cnt is not if IPOL_ANTIALIASED is set
  if (fabs(*weight)<1e-32) cnt=0;
  else // reject pixels with less than IPolMin coverage
    if ( fabs(Df_12)*IPolMin > fabs(*weight) ) cnt=0;

  return(cnt);

} /* Isum2ldw */

/*---------------------------------------------------------------------------
NAME

  Ipol2ldw --- Linear two dimensional interpolation

PURPOSE
  Linear two dimensional interpolation including dummies with limit checks
  Ipol2ldw calculates the weighted sum and the sum of the weights of the 
  contributing pixels. The array limits are checked. To calculate the 
  interpolated value, sum must be devided by weight. The function returns 
  the number of contributing pixels.  See also Ipol2ld.
  The result corresponds to Isum2ldw(...,f_1-0.5,f_2-0.5,f_1+0.5,f_2+0.5,...).

DESCRIPTION
  The output value "sum" is the weighted sum of the intensities of the 4 
  closest pixels p1 to p4 around pixel f. Pixel f is located at (f_1,f_2). 
  The values of the pixels p1=Data[i_1,i_2], p2=Data[i_1+1,i_2], 
  p3=Data[i_1+1,i_2+1] and p4=Data[I_1,i_2+1] are multiplied by the overlap 
  areas wi of each pixel with the pixel f and added. The overlap areas w1 
  to w4 are calculated from the displacement r_1 and r_2 of f from p1. 
  If one of the 4 pixels lies outside the range of the image or if it is 
  a dummy it is ignored (wi=0): 

    sum    = Sum(Ii*wi)
    weight = Sum(wi)
    cnt    = number of used intensity values

    w1 = (1-r_1)*(1-r_2)
    w2 = r_1*(1-r_2)
    w3 = r_1*r_2
    w4 = (1-r_1)*r_2

  with r_1 = f_1 - floor(f_1) and r_2 = f_2 - floor(f_2)

  For non-zero weight, the averaged value is Sum(Ii*wi)/Sum(wi).

                 -----------------------------------------
                 |             //////|**************     |
                 |             //////|**************     |
    w4 = ///     |        p4- -/-/-/-|-*-*-*-*p3****     |  w3 = ***
        f_2  ----|         |   //////|***f*****|****     |
             ^   |             //////|**************     |
             |   -----------------------------------------
    w1 = +++ |r_2|             ++++++|\\\\\\\\\\\\\\     |  w2 = \\\
             v   |         |         |         |         |
  floor(f_2)-----|        p1- - - - -|- - - - p2         |
                 |                   |                   |
                 |                   |                   |
                 -----------------------------------------
                           |      r_1    |
                           |<----------->|
                           |
                       floor(f_1)       f_1

SYNOPSIS

  int Ipol2ldw (float *Data, int Dim_1, int Dim_2, float Dummy, float DDummy,
                float f_1, float f_2, float *sum, float *weight)

  return value            (o)   : number of pixels contributing to the output
                                  value (0..4), if 0, no valid data point found.
  float Data[Dim_1,Dim_2] (i)   : input array
  int Dim_1, Dim_2        (i)   : dimension of array
  float Dummy, DDummy     (i)   : dummy specification
  float f_1, f_2          (i)   : program array indices (interpolation point)
  float *sum              (o)   : weighted sum of interpolated pixels 
  float *weight           (o)   : weight of sum 
                                  minimum 0.0: no valid pixel found
                                  maximum 1.0: interpolation between 1 to 4 
                                               distance weighted pixels

---------------------------------------------------------------------------*/
int Ipol2ldw (float *Data, int Dim_1, int Dim_2, float Dummy, float DDummy,
              float f_1, float f_2, float *sum, float *weight )
{ const float eps = IPOLEPS;
  int cnt;
  float *pval1, *pval2, *pval3, *pval4;
  float w1, w2, w3, w4;

  int   i_1, i_2;
  float r_1, r_2;

  /* calculate integer indices and rest */
  IDX(f_1,i_1,r_1);
  IDX(f_2,i_2,r_2);

  *weight=0.0;
  *sum=0.0;
  cnt=0;

  if (!Data) return(cnt); // return, if NULL pointer

  pval1 = ABSPTR(Data,Dim_1,Dim_2,i_1,i_2);

  /* General check */
  if ( (i_1>=0) && (i_2>=0) && (i_1<Dim_1-1) && (i_2<Dim_2-1) )
    /* all 4 points inside the array */
    if (r_1<eps) {
      if (r_2<eps) {
        if NODUMMY(*pval1,Dummy,DDummy) {
          cnt++; *weight += 1.0; *sum = *pval1; /* no interpolation */
          } /* if NODUMMY */
        } else {
        if NODUMMY(*pval1,Dummy,DDummy) {
          cnt++;w1=1.0-r_2; *weight+=w1; *sum+=*pval1*w1;
          } /* if NODUMMY */
        pval4 = NEXTROW(pval1,Dim_1,Dim_2);
        if NODUMMY(*pval4,Dummy,DDummy) {
          cnt++;w4=r_2; *weight+=w4; *sum+=*pval4*w4;
          } /* if NODUMMY */
        } /* if (r_2<eps) */
      } else {
      if (r_2<eps) {
        if NODUMMY(*pval1,Dummy,DDummy) {
          cnt++; w1=(1.0-r_1); *weight+=w1; *sum = *pval1*w1;
          } /* if NODUMMY */
        pval2 = NEXTCOL(pval1,Dim_1,Dim_2);
        if NODUMMY(*pval2,Dummy,DDummy) {
          cnt++; w2=r_1; *weight+=w2; *sum+=*pval2*w2;
          } /* if NODUMMY */
        } else {
        if NODUMMY(*pval1,Dummy,DDummy) {
          cnt++;w1=(1.0-r_1)*(1.0-r_2); *weight+=w1; *sum+=*pval1*w1;
          } /* if NODUMMY */
        pval2 = NEXTCOL(pval1,Dim_1,Dim_2);
        if NODUMMY(*pval2,Dummy,DDummy) {
          cnt++;w2=r_1*(1.0-r_2); *weight+=w2; *sum+=*pval2*w2;
          } /* if NODUMMY */
        pval4 = NEXTROW(pval1,Dim_1,Dim_2);
        if NODUMMY(*pval4,Dummy,DDummy) {
          cnt++;w4=(1.0-r_1)*r_2; *weight+=w4; *sum+=*pval4*w4;
          } /* if NODUMMY */
        pval3 = NEXTCOLROW(pval1,Dim_1,Dim_2);
        if NODUMMY(*pval3,Dummy,DDummy) {
          cnt++;w3=r_1*r_2; *weight+=w3; *sum+=*pval3*w3;
          } /* if NODUMMY */
        } /* (r_2<eps) */
      } /* if (r_1 ... */
   else if ( (i_1>=-1) && (i_2>=-1) && (i_1<Dim_1) && (i_2<Dim_2) ) {
    /* some of the 4 points are inside the array */
    if (r_1<eps) {
      if (r_2<eps) {
        if ( (i_1>=0) && (i_2>=0) ) /* pval1 OK? */
          if NODUMMY(*pval1,Dummy,DDummy) {
            cnt++; *weight += 1.0; *sum = *pval1; /* no interpolation */
            } /* if NODUMMY */

        } else {
        if ( (i_1>=0) && (i_2>=0) ) /* pval1 OK? */
          if NODUMMY(*pval1,Dummy,DDummy) {
            cnt++;w1=1.0-r_2; *weight+=w1; *sum+=*pval1*w1;
            } /* if NODUMMY */

        if ( (i_1>=0) && (i_2<Dim_2-1) ) { 
          pval4 = NEXTROW(pval1,Dim_1,Dim_2);
          if NODUMMY(*pval4,Dummy,DDummy) {
            cnt++;w4=r_2; *weight+=w4; *sum+=*pval4*w4;
            } /* if NODUMMY */
          } /* pval4 */

        } /* if (r_2<eps) */
      } else {
      if (r_2<eps) {
        if ( (i_1>=0) && (i_2>=0) ) /* pval1 OK? */
          if NODUMMY(*pval1,Dummy,DDummy) {
            cnt++; w1=(1.0-r_1); *weight+=w1; *sum = *pval1*w1;
            } /* if NODUMMY */

        if ( (i_2>=0) && (i_1<Dim_1-1) ) { 
          pval2 = NEXTCOL(pval1,Dim_1,Dim_2);
          if NODUMMY(*pval2,Dummy,DDummy) {
            cnt++; w2=r_1; *weight+=w2; *sum+=*pval2*w2;
            } /* if NODUMMY */
          } /* pval2 */

        } else {
        if ( (i_1>=0) && (i_2>=0) ) /* pval1 OK? */
          if NODUMMY(*pval1,Dummy,DDummy) {
            cnt++;w1=(1.0-r_1)*(1.0-r_2); *weight+=w1; *sum+=*pval1*w1;
            } /* if NODUMMY */

        if ( (i_2>=0) && (i_1<Dim_1-1) ) {
          pval2 = NEXTCOL(pval1,Dim_1,Dim_2);
          if NODUMMY(*pval2,Dummy,DDummy) {
            cnt++;w2=r_1*(1.0-r_2); *weight+=w2; *sum+=*pval2*w2;
            } /* if NODUMMY */
          } /* pval2 */

        if ( (i_1>=0) && (i_2<Dim_2-1) ) { 
          pval4 = NEXTROW(pval1,Dim_1,Dim_2);
          if NODUMMY(*pval4,Dummy,DDummy) {
            cnt++;w4=(1.0-r_1)*r_2; *weight+=w4; *sum+=*pval4*w4;
            } /* if NODUMMY */
          } /* pval4 */

        if ( (i_1<Dim_1-1) && (i_2<Dim_2-1) ) { 
          pval3 = NEXTCOLROW(pval1,Dim_1,Dim_2);
          if NODUMMY(*pval3,Dummy,DDummy) {
            cnt++;w3=r_1*r_2; *weight+=w3; *sum+=*pval3*w3;
            } /* if NODUMMY */
          } /* pval3 */

        } /* (r_2<eps) */
      } /* if (r_1 ... */
    }

  // reject pixels with less than IPolMin coverage
  if ( IPolMin > fabs(*weight) ) cnt=0;

  return(cnt);

} /* Ipol2ldw */

/*---------------------------------------------------------------------------
NAME

  Ipol2ld --- Interpolation including dummies with limit checks

PURPOSE
  Linear two dimensional interpolation including dummies with limit checks
  Ipol2ld returns the interpolated value. To calculate the sum and the weight
  of the interpolated pixels use Ipol2ldw.

METHOD
  See Ipol2ldw. Ipol2ld calculates only the interpolated value and returns
  the number of interpolated pixels.
---------------------------------------------------------------------------*/
int Ipol2ld (float *Data, int Dim_1, int Dim_2, float Dummy, float DDummy,
             float f_1, float f_2, float *value) 
{ float weight;
  int cnt;

  cnt = Ipol2ldw(Data, Dim_1, Dim_2, Dummy, DDummy,f_1, f_2, value, &weight);
  if (cnt) *value = *value/weight;

  return ( cnt );

} /* Ipol2ld */

int Ipol2d (float *Data, int Dim_1, int Dim_2, float Dummy, float DDummy,
            float f_1, float f_2, float *value)
{  return(Ipol2ld (Data, Dim_1, Dim_2, Dummy, DDummy,
            f_1, f_2, value));
} /* Ipol2d */

/*---------------------------------------------------------------------------
NAME

  Ipol2 --- Two dimensional interpolation, no limit checks, no dummy checks

SYNOPSIS
  void Ipol2 (float *Data, int Dim_1, int Dim_2, 
             float f_1, float f_2, float *value)

PURPOSE
  Linear two dimensional interpolation 
  no limit checks,
  no dummy checks

METHOD
  The point (f_1, f_2) is linearly interpolated between the 4 closest
  program array elements val1=Data[i_1,i_2], val2=Data[i_1+1,I_2],
  val3=Data[i_1+1,i_2+1] and val4=Data[I_1,i_2+1]. If a float index
  is an integer number 0, 1, 2, 3 etc. no interpolation is done in the
  corresponding direction.

ARGUMENTS

  return value            (o)   : number of pixels contributing to the output
                                  value (0..4), if 0, only dummies found.
  float Data[Dim_1,Dim_2] (i)   : input array
  int Dim_1, Dim_2        (i)   : dimension of array
  float f_1, f_2          (i)   : program array indices (interpolation point)
  float *value            (o)   : interpolated value

2 HISTORY
  28-Apr-1995 PB from Ipol2d

---------------------------------------------------------------------------*/
void Ipol2 (float *Data, int Dim_1, int Dim_2,
            float f_1, float f_2, float *value)
{ const float eps = IPOLEPS;
  int cnt;
  float *pval1, *pval2, *pval3, *pval4;
  float w1, w2, w3, w4;
  float weight;

  int   i_1, i_2;
  float r_1, r_2;

  /* calculate integer indices and rest */
  IDX(f_1,i_1,r_1);
  IDX(f_2,i_2,r_2);

  weight=0.0;
  *value=0.0;
  cnt=0;

  if (!Data) return; // return, if NULL pointer

  pval1 = ABSPTR(Data,Dim_1,Dim_2,i_1,i_2);

  if (r_1<eps) {
     if (r_2<eps) {
         cnt++; *value = *pval1;  /* no interpolation */
       } else {
         cnt++;w1=1.0-r_2; weight+=w1; *value+=*pval1*w1;
       pval4 = NEXTROW(pval1,Dim_1,Dim_2);
         cnt++;w4=r_2; weight+=w4; *value+=*pval4*w4;
       if (cnt) *value = *value/weight;
       } /* if (r_2<eps) */
     } else {
     if (r_2<eps) {
         cnt++; w1=(1-r_1); weight+=w1; *value = *pval1*w1;
       pval2 = NEXTCOL(pval1,Dim_1,Dim_2);
         cnt++; w2=r_1; weight+=w2; *value+=*pval2*w2;
       if (cnt) *value = *value/weight;
       } else {
         cnt++;w1=(1.0-r_1)*(1.0-r_2); weight+=w1; *value+=*pval1*w1;
       pval2 = NEXTCOL(pval1,Dim_1,Dim_2);
         cnt++;w2=r_1*(1.0-r_2); weight+=w2; *value+=*pval2*w2;
       pval4 = NEXTROW(pval1,Dim_1,Dim_2);
         cnt++;w4=(1.0-r_1)*r_2; weight+=w4; *value+=*pval4*w4;
       pval3 = NEXTCOLROW(pval1,Dim_1,Dim_2);
         cnt++;w3=r_1*r_2; weight+=w3; *value+=*pval3*w3;
       if (cnt) *value = *value/weight;
       } /* (r_2<eps) */
     } /* if (r_1 ... */

} /* Ipol2 */

/*---------------------------------------------------------------------------
NAME

  IpolRebin2 --- Two dimensional rebinning of a float array.

SYNOPSIS
  void IpolRebin2 ( float *Data, int Dim_1, int Dim_2,
                    float * DataOut, int * OutDim_1, int * OutDim_2,
                    float Dummy, float DDummy, int Bin_1, int Bin_2, int average )


PURPOSE
  Two dimensional rebinning of a float array. The result is written to the
  output array. The new dimensions are returned.

ARGUMENTS

  float Data[Dim_1,Dim_2]    (i) : input array with dimension Dim_1, Dim_2
  int Dim_1, Dim_2           (i) : dimensions of input array
  float *DataOut             (o) : output array (needs to be allocated!)
  int *pOutDim_1, *pOutDim_2 (o) : pointer to output dimensions 
                                   (need to be allocated!)
  float Dummy, DDummy        (i) : Dummy values of input and output arrays 
  int Bin_1, Bin_2           (i) : Binning factors (>=1) 
  int Average                (i) : flag: sum (0) or average (!0) pixel values

  Data and DataOut can be identical. If they are not identical, they must not
  overlap. The output variables *pOutDim_1 and *pOutDim_2 must have been 
  allocated. They can be identical to Dim_1 and Dim_2.

HISTORY
  2007-02-26 PB from RebinFloat2d

---------------------------------------------------------------------------*/
void IpolRebin2 ( float *Data, int Dim_1, int Dim_2,
                  float * DataOut, int * pOutDim_1, int * pOutDim_2,
                  float Dummy, float DDummy, int Bin_1, int Bin_2, int Average )
{ register long int j_1, j_2, i_1, i_2;
  float * pin, * pout;
  float value, sum, count;
  int bin_1, bin_2;
  int dim_1, dim_2;

  dim_1 = Dim_1;
  dim_2 = Dim_2;

  if (pOutDim_1!=NULL) *pOutDim_1=dim_1;
  if (pOutDim_2!=NULL) *pOutDim_2=dim_2;

  bin_1 = Bin_1>1?Bin_1:1; // max(1,bin_1)
  bin_2 = Bin_2>1?Bin_2:1; // max(1,bin_2)

  if ((bin_1>1) || (bin_2>1) || (DataOut!=Data)) {
    pout = DataOut;
    if (pout!=NULL) {
      for (j_2=0;j_2<=dim_2-bin_2;j_2+=bin_2)
        for (j_1=0;j_1<=dim_1-bin_1;j_1+=bin_1) {
          sum = 0.0; count = 0.0;
          for (i_2=j_2;i_2<j_2+bin_2;i_2++) {
            pin = ABSPTR(Data,dim_1,dim_2,j_1,i_2);
            for (i_1=j_1;i_1<j_1+bin_1;i_1++) {
              value = *(pin++);
              if NODUMMY(value,Dummy,DDummy)
                { sum += value; count += 1.0; }
            }
          }
          if (count>0.0) *(pout++) = Average?sum/count:sum;
          else *(pout++) = Dummy;
        }
      dim_1 /= bin_1; dim_2 /= bin_2;
      if (pOutDim_1!=NULL) *pOutDim_1=dim_1;
      if (pOutDim_2!=NULL) *pOutDim_2=dim_2;
    }
  }

} /* IpolRebin2 */


/*****************************************************************************/