File: uves_utils.c

package info (click to toggle)
cpl-plugin-uves 6.1.3+dfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 23,128 kB
  • sloc: ansic: 171,056; sh: 4,359; python: 3,002; makefile: 1,322
file content (5353 lines) | stat: -rw-r--r-- 163,364 bytes parent folder | download
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
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
/*                                                                              *
 *   This file is part of the ESO UVES Pipeline                                 *
 *   Copyright (C) 2004,2005 European Southern Observatory                      *
 *                                                                              *
 *   This library is free software; you can redistribute it and/or modify       *
 *   it under the terms of the GNU General Public License as published by       *
 *   the Free Software Foundation; either version 2 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 General Public License for more details.                               *
 *                                                                              *
 *   You should have received a copy of the GNU General Public License          *
 *   along with this program; if not, write to the Free Software                *
 *   Foundation, 51 Franklin St, Fifth Floor, Boston, MA  02111-1307  USA       *
 *                                                                              */

/*
 * $Author: amodigli $
 * $Date: 2013-04-16 15:36:11 $
 * $Revision: 1.204 $
 * $Name: not supported by cvs2svn $
 */

#ifdef HAVE_CONFIG_H
#  include <config.h>
#endif

/*---------------------------------------------------------------------------*/
/**
 * @defgroup uves_utils  Utility functions
 *
 * This module contains various functions that are shared between multiple recipes
 */
/*---------------------------------------------------------------------------*/

/*-----------------------------------------------------------------------------
                            Includes
 ----------------------------------------------------------------------------*/
#include <uves_utils.h>
#include <uves_utils_cpl.h>
#include <irplib_ksigma_clip.h>
/*
 * System Headers
 */
#include <errno.h>
#include <uves.h>
#include <uves_extract_profile.h>
#include <uves_plot.h>
#include <uves_dfs.h>
#include <uves_pfits.h>
#include <uves_utils_wrappers.h>
#include <uves_wavecal_utils.h>
#include <uves_msg.h>
#include <uves_dump.h>
#include <uves_error.h>

#include <irplib_utils.h>

#include <cpl.h>
#include <uves_time.h> /* iso time */

#include <ctype.h>  /* tolower */
#include <stdbool.h>
#include <float.h>

/*-----------------------------------------------------------------------------
                            Defines
 ----------------------------------------------------------------------------*/
// The following macros are used to provide a fast
// and readable way to convert C-indexes to FORTRAN-indexes.
#define C_TO_FORTRAN_INDEXING(a) &a[-1]
#define FORTRAN_TO_C_INDEXING(a) &a[1]

/**@{*/
/*-----------------------------------------------------------------------------
                            Functions prototypes
 ----------------------------------------------------------------------------*/


static cpl_error_code 
uves_cosrout(cpl_image* ima,
             cpl_image** msk,
             const double ron, 
             const double gain,
             const int ns,
             const double sky,
             const double rc,
             cpl_image** flt,
             cpl_image** out);

static cpl_image * 
uves_gen_lowpass(const int xs, 
                  const int ys, 
                  const double sigma_x, 
                  const double sigma_y);

static cpl_error_code 
uves_find_next(cpl_image** msk,
               const int first_y,
               int* next_x,
	       int* next_y);

static cpl_error_code
uves_sort(const int kmax,float* inp, int* ord);

/*-----------------------------------------------------------------------------
                            Implementation
 ----------------------------------------------------------------------------*/
/**
   @brief    Check if an error has happened and returns error kind and location
   @param    val input value
   @return   0 if no error is detected,-1 else
 */
int uves_print_rec_status(const int val) {
    if(cpl_error_get_code() != CPL_ERROR_NONE) {
        uves_msg_error("Recipe status at %d",val);
        uves_msg_error("%s",(const char*) cpl_error_get_message());
        uves_msg_error("%s",(const char*) cpl_error_get_where());
        return -1;
    }
    return 0;
}

/*----------------------------------------------------------------------------*/
/**
 @brief  add EXTNAME to fits header of table missing it
 @param  fname      table filename
 @param  xname      extname value
 @param  x_num      extension id
 */
cpl_error_code
uves_table_add_extname(const char* fname,const char* xvalue,const int x_num)
{
	uves_propertylist* pl;
	uves_propertylist* xt;
	cpl_table* table;

	pl = uves_propertylist_load(fname,0);
	xt = uves_propertylist_load(fname,x_num);

	uves_pfits_set_extname(xt,xvalue);

	table=cpl_table_load(fname,x_num,1);
	cpl_table_save(table, (cpl_propertylist*) pl, (cpl_propertylist*) xt, fname,
			CPL_IO_DEFAULT);

	uves_propertylist_delete(pl);
	uves_propertylist_delete(xt);
	cpl_table_delete(table);
	return cpl_error_get_code();

}
/*---------------------------------------------------------------------------*/
/** 
 * @brief  Remove cosmic ray events on single ccd exposure and replace them by interpolation on neighbourhood pixels.
   @author P.Magain, M.Remy, Institut d'Astrophysique de Liege
           Ported to UVES pipe from MIDAS rcosmic.for
   @param  ima input image
   @param  flt median filter of input image
   @param  out output image
   @param  sky mean value of the sky background
   @param  ron Readout noise in ADU units.
   @param  gain Inverse gain factor (e-/ADU)
   @param  ns threshold for the detection of cosmic rays
   @param  nc critical ratio for discrimination of objects and cosmic rays
   @param  msk name of an optional frame containing the value 1 for cosmic
              rays and 0 for all other pixels
   @note   The detection threshold is in units of the theoretical noise
           sigma of each pixel; it's default value is 4. The default for
           `rc' is 2.

   @doc
         a) The algorithm works as follows:
         1. The input image is filtered in the following way:
         FILTER/MEDIAN inframe middumma 1,1,0.0 NA
         For Long-Slit spectra of extended sources, the algorithm may be
         more efficient if the median filter works only along the slit.
         2. The input image is compared with the filtered image. All pixels
         with an intensity I  greater than  Im+ns*sigma  are suspicious and
         may be cosmic rays (Im is the filtered intensity of a pixel and
         sigma is given by: sigma**2 = ron**2+I/gain).
         3. All suspicious pixels are grouped into sets of contiguous points.
         In each of these sets, the pixel with the maximum intensity Imax is
         selected. If (Imax-sky) is greater than rc*(Iaver-sky), Iaver being
         an average of the intensities of the first eight neighbours of that
         pixel, the whole set of points is considered as a cosmic ray event.
         4. The intensities of the pixels affected by cosmic rays are
         replaced by a median value calculated over the nearest neighbours of
         the group to which they belong.
         b) In many situations, rc is the most critical parameter and requires
         careful fine-tuning. If it is choosen too small, small sources such
         as stars may be affected. If rc is too large, the filter may not
         remove weak partical hits superimposed to reasonably well exposed
         extended sources.

 * @return newly allocated parametrlist or NULL on error
 */
/*---------------------------------------------------------------------------*/

cpl_error_code
uves_rcosmic(cpl_image* ima,
             cpl_image** flt,
             cpl_image** out,
             cpl_image** msk,
             const double sky,
             const double ron,
             const double gain,
             const int ns,
             const double rc)

{


/*


      PROGRAM RCOSMIC
      INTEGER*4 IAV,I
      INTEGER*4 STATUS,MADRID,SIZEX,IOMODE
      INTEGER*4 NAXIS,NPIX(2),IMNI,IMNO,IMNF,IMNC
      INTEGER*8 PNTRI,PNTRF,PNTRO,PNTRC
      INTEGER*4 KUN,KNUL
      CHARACTER*60 IMAGE,OBJET,COSMIC
      CHARACTER*72 IDENT1,IDENT2,IDENT3
      CHARACTER*48 CUNIT
      DOUBLE PRECISION START(2),STEP(2)
      REAL*4 SKY,GAIN,RON,NS,RC,PARAM(5),CUTS(2)
      INCLUDE 'MID_INCLUDE:ST_DEF.INC'
      COMMON/VMR/MADRID(1)
      INCLUDE 'MID_INCLUDE:ST_DAT.INC'
      DATA IDENT1 /' '/
      DATA IDENT2 /' '/
      DATA IDENT3 /'cosmic ray mask '/
      DATA CUNIT /' '/
      CALL STSPRO('RCOSMIC')
      CALL STKRDC('IN_A',1,1,60,IAV,IMAGE,KUN,KNUL,STATUS)
      CALL STIGET(IMAGE,D_R4_FORMAT,F_I_MODE,F_IMA_TYPE,
     1                2,NAXIS,NPIX,START,STEP
     1                ,IDENT1,CUNIT,PNTRI,IMNI,STATUS)

      CALL STKRDR('PARAMS',1,5,IAV,PARAM,KUN,KNUL,STATUS)
      CALL STIGET('middumma',D_R4_FORMAT,F_I_MODE,F_IMA_TYPE,
     1                2,NAXIS,NPIX,START,STEP
     1                ,IDENT2,CUNIT,PNTRF,IMNF,STATUS)
      SKY = PARAM(1)
      GAIN = PARAM(2)
      RON = PARAM(3)
      NS = PARAM(4)
      RC = PARAM(5)

*/


   check_nomsg(*flt=cpl_image_duplicate(ima));
   check_nomsg(uves_filter_image_median(flt,1,1,false));



/*

      CALL STKRDC('OUTIMA',1,1,60,IAV,OBJET,KUN,KNUL,STATUS)
      CALL STIPUT(OBJET,D_R4_FORMAT,F_O_MODE,F_IMA_TYPE,
     1                 NAXIS,NPIX,START,STEP
     1                ,IDENT1,CUNIT,PNTRO,IMNO,STATUS)

      SIZEX = 1
      DO I=1,NAXIS
         SIZEX = SIZEX*NPIX(I)
      ENDDO
      CALL STKRDC('COSMIC',1,1,60,IAV,COSMIC,KUN,KNUL,STATUS)
      IF (COSMIC(1:1).EQ.'+') THEN
            COSMIC = 'dummy_frame'
            IOMODE = F_X_MODE
      ELSE
            IOMODE = F_O_MODE
      ENDIF    
      CALL STIPUT(COSMIC,D_I2_FORMAT,IOMODE,F_IMA_TYPE
     1                 ,NAXIS,NPIX,START,STEP
     1                ,IDENT3,CUNIT,PNTRC,IMNC,STATUS)
      CALL COSROUT(MADRID(PNTRI),MADRID(PNTRC),NPIX(1),NPIX(2),
     1             RON,GAIN,NS,SKY,RC
     1            ,MADRID(PNTRF),MADRID(PNTRO))

      CUTS(1) = 0
      CUTS(2) = 1
      IF (IOMODE.EQ.F_O_MODE) 
     + CALL STDWRR(IMNC,'LHCUTS',CUTS,1,2,KUN,STATUS)
      CALL DSCUPT(IMNI,IMNO,' ',STATUS) 
      CALL STSEPI
      END


*/

   check_nomsg(uves_cosrout(ima,msk,ron,gain,ns,sky,rc,flt,out));
  cleanup:
  return CPL_ERROR_NONE;
}


/*---------------------------------------------------------------------------*/
/** 
 * @brief  Perform kappa-sigma clip.
   @author C. Izzo
   @param  values values to be checked
   @param  klow   kappa to clip too low level values
   @param  khigh  kappa to clip too high values
   @param  kiter  number of iterations

   @note   In first iteration a median is the reference value for robustness

 * @return 
 */
/*---------------------------------------------------------------------------*/
static double 
uves_ksigma_vector(cpl_vector *values,double klow, double khigh, int kiter)
{
    cpl_vector *accepted;
    double  mean  = 0.0;
    double  sigma = 0.0;
    double *data  = cpl_vector_get_data(values);
    int     n     = cpl_vector_get_size(values);
    int     ngood = n;
    int     count = 0;
    int     i;
 
    /*
     * At first iteration the mean is taken as the median, and the
     * standard deviation relative to this value is computed.
     */

    check_nomsg(mean = cpl_vector_get_median(values));

    for (i = 0; i < n; i++) {
        sigma += (mean - data[i]) * (mean - data[i]);
    }
    sigma = sqrt(sigma / (n - 1));

    while (kiter) {
        count = 0;
        for (i = 0; i < ngood; i++) {
            if (data[i]-mean < khigh*sigma && mean-data[i] < klow*sigma) {
                data[count] = data[i];
                ++count;
            }
        }

        if (count == 0) // This cannot happen at first iteration.
            break;      // So we can break: we have already computed a mean.

        /*
         * The mean must be computed even if no element was rejected
         * (count == ngood), because at first iteration median instead
         * of mean was computed.
         */

        check_nomsg(accepted = cpl_vector_wrap(count, data));
        check_nomsg(mean = cpl_vector_get_mean(accepted));
        if(count>1) {
           check_nomsg(sigma = cpl_vector_get_stdev(accepted));
        }
        check_nomsg(cpl_vector_unwrap(accepted));

        if (count == ngood) {
            break;
        }
        ngood = count;
        --kiter;
    }
  cleanup:

    return mean;
}


/**
 * @brief
 *   Stack images using k-sigma clipping
 *
 * @param imlist      List of images to stack
 * @param klow        Number of sigmas for rejection of lowest values
 * @param khigh       Number of sigmas for rejection of highest values
 * @param kiter       Max number of iterations
 *
 * @return Stacked image.
 *
 * At the first iteration the value of sigma is computed relatively to
 * the median value of all pixels at a given image position. For the
 * next iterations the sigma is computed in the standard way. If
 * at some iteration all points would be rejected, the mean computed
 * at the previous iteration is returned.
 */

cpl_image *
uves_ksigma_stack(const cpl_imagelist *imlist, double klow, double khigh, int kiter)
{
    int         ni, nx, ny, npix;
    cpl_image  *out_ima=NULL;
    cpl_imagelist  *loc_iml=NULL;
    double      *pout_ima=NULL;
    cpl_image  *image=NULL;
    const double     **data=NULL;
    double     *med=NULL;
    cpl_vector *time_line=NULL;
  
    double     *ptime_line=NULL;
    int         i, j;
   double mean_of_medians=0;

    passure(imlist != NULL, "Null input imagelist!");

    ni         = cpl_imagelist_get_size(imlist);
    loc_iml        = cpl_imagelist_duplicate(imlist);
    image      = cpl_imagelist_get(loc_iml, 0);
    nx         = cpl_image_get_size_x(image);
    ny         = cpl_image_get_size_y(image);
    npix       = nx * ny;

    out_ima    = cpl_image_new(nx, ny, CPL_TYPE_DOUBLE);
    pout_ima   = cpl_image_get_data_double(out_ima);

    time_line  = cpl_vector_new(ni);
   
    ptime_line = cpl_vector_get_data(time_line);

    data = cpl_calloc(sizeof(double *), ni);
    med  = cpl_calloc(sizeof(double), ni);

    for (i = 0; i < ni; i++) {
        image = cpl_imagelist_get(loc_iml, i);
        med[i]=cpl_image_get_median(image);
        cpl_image_subtract_scalar(image,med[i]);
        data[i] = cpl_image_get_data_double(image);
        mean_of_medians+=med[i];
    }
    mean_of_medians/=ni;

    for (i = 0; i < npix; i++) {
        for (j = 0; j < ni; j++) {
             ptime_line[j] = data[j][i];
         }
        check_nomsg(pout_ima[i] = uves_ksigma_vector(time_line, klow, khigh, kiter)); 
    }
 
    cpl_image_add_scalar(out_ima,mean_of_medians);

  cleanup:
    cpl_free(data);
    cpl_free(med);
    cpl_vector_delete(time_line);
    uves_free_imagelist(&loc_iml);

    return out_ima;

} 



/**
 * @brief
 *   Generates wave map
 * @param ima_sci (to get dimensions)
 * @param context recipe context
 * @param parameters input params
 * @param ordertable order table
 * @param linetable  line table
 * @param order_locations order traces polynomial description
 * @param dispersion_relation wavelength solution
 * @param first_abs_order min abs order 
 * @param last_abs_order  max abs order
 * @param slit_size  slit size in pixels
 * @param flats             List of flats to stack
 * @param ordertable        Input order table
 * @param order_locations   polynomial description of order locations
 *
 * @return wavemap image (that need to be deallocated from caller)
 * @doc Generates an image that at each point has an intensity equal to
 * the corresponding pixels
 * for each order
 *     for each x
 *         with y position on the order trace
 *         determines the wavelength corresponding to (x,y,m)  
 *         and set it to the corresponding map(x,y,m) pixel.
 *         We assume that point at the same x,m have same waveleng for each
 *         y along the extraction slit.
 *     endfor
 *  endfor
 *
 */
cpl_image *
uves_get_wave_map(
                  cpl_image * ima_sci,
		  const char *context,
                  const cpl_parameterlist *parameters,
		  const cpl_table *ordertable,
		  const cpl_table *linetable,
		  const polynomial* order_locations,
		  const polynomial *dispersion_relation,
		  const int first_abs_order,
		  const int last_abs_order,
		  const int slit_size)
{

  cpl_image* wave_map=NULL;
  double* pwmap=NULL;
  int ord_min=0;
  int ord_max=0;
  int i=0;
  int j=0;
  double xpos=0;
  double ypos=0;
  double wlen=0;
  
  int nx=0;
  int ny=0;
  int aord=0;
  int order=0;
  int jj=0;
  int norders=0;
  int hs=0;

  uves_msg("Creating wave map");
  /* set half slit size */
  hs=slit_size/2;

  /* get wave map size */ 
  nx = cpl_image_get_size_x(ima_sci);
  ny = cpl_image_get_size_y(ima_sci);
     
  /* get ord min-max */
  ord_min=cpl_table_get_column_min(ordertable,"Order");
  ord_max=cpl_table_get_column_max(ordertable,"Order");
  norders=ord_max-ord_min+1;

  check_nomsg(wave_map=cpl_image_new(nx,ny,CPL_TYPE_DOUBLE));
  pwmap=cpl_image_get_data_double(wave_map);

  for (order = 1; order <= norders; order++){
    /* wave solution need absolute order value */
    aord = uves_absolute_order(first_abs_order, last_abs_order, order);
    for (i=0;i<nx;i++) {
      xpos=(double)i;
      wlen=uves_polynomial_evaluate_2d(dispersion_relation,xpos,aord)/aord;
      ypos=uves_polynomial_evaluate_2d(order_locations,xpos,order);
      for (jj=-hs;jj<hs;jj++) {
	j=(int)(ypos+jj+0.5);
        /* check the point is on the detector */
	if( (j>0) && ( (j*nx+i)<nx*ny) ) {
	  pwmap[j*nx+i]=wlen;
	}
      }
    }
  }

  /*
  check_nomsg(cpl_image_save(wave_map,"wmap.fits",CPL_BPP_IEEE_FLOAT,NULL,
			     CPL_IO_DEFAULT));
  */
 cleanup:
  return wave_map;
}







/**
 * @brief
 *   Stack images using k-sigma clipping
 *
 * @param flats             List of flats to stack
 * @param ordertable        Input order table
 * @param order_locations   polynomial description of order locations
 *
 * @return Stacked image.
 *
 * The input list of flats is analized to compute
 * for each flat
 *     for each order
 *         The median flux on a number of windows of given X * Y size.
 *         The mean flux  of the values computed on each order is computed.
 *     endfor
 *     Finally the mean flux of all means is computed.
 *     The flat is normalized by the computed mean
 *  endfor
 */
cpl_image *
uves_flat_create_normalized_master2(cpl_imagelist * flats,
                                    const cpl_table *ordertable,
                                    const polynomial* order_locations,
                                    const cpl_image* mflat)
{

   cpl_imagelist* flats_norm=NULL;

   cpl_image* master_flat=NULL;
   /* cpl_image* img=NULL; */
   cpl_image* flat=NULL;
   cpl_image* flat_mflat=NULL;

   cpl_vector* vec_flux=NULL;
   double* pvec_flux=NULL;

   int ni=0;
   int i=0;
   int sx=0;
   int sy=0;
   int ord_min=0;
   int ord_max=0;
   int nord=0;
   int nsam=10;
   int x_space=10;
   int llx=0;
   int lly=0;
   int urx=0;
   int ury=0;
   int hbox_sx=0;
   int hbox_sy=0;
   int ord=0;
   int absord=0;
   int pos_x=0;
   int pos_y=0;
   double x=0;
   double y=0;
   double flux_median=0;
   double mean_explevel=0;
   /* double exptime=0; */
   int is=0;
   int k=0;

   ni=cpl_imagelist_get_size(flats);
   
   /* evaluate medain on many windows distribuited all over orders of flats */
   sx         = cpl_image_get_size_x(mflat);
   sy         = cpl_image_get_size_y(mflat);


   ord_min=cpl_table_get_column_min(ordertable,"Order");
   ord_max=cpl_table_get_column_max(ordertable,"Order");
   nord=ord_max-ord_min+1;

   hbox_sx=(int)((sx-2*x_space)/(2*nsam)+0.5);
   flats_norm=cpl_imagelist_new();
   for(i=0;i<ni;i++) {
   uves_free_vector(&vec_flux);
   vec_flux=cpl_vector_new(nord*nsam);
   pvec_flux=cpl_vector_get_data(vec_flux);
     uves_free_image(&flat_mflat);
     uves_free_image(&flat);
      check_nomsg(flat = cpl_image_duplicate(cpl_imagelist_get(flats, i)));
      /* normalize flats by master flat */
      flat_mflat=cpl_image_duplicate(flat);
      cpl_image_divide(flat_mflat,mflat);
      
      k=0;
      for(ord=0;ord<nord;ord++) {
         absord=ord+ord_min;
         pos_x=-hbox_sx;
         for(is=0;is<nsam;is++) {
            pos_x+=(2*hbox_sx+x_space);
            x=(int)(pos_x+0.5);

            check_nomsg(y=uves_polynomial_evaluate_2d(order_locations, 
                                                      x, absord));
            pos_y=(int)(y+0.5);

            check_nomsg(llx=uves_max_int(pos_x-hbox_sx,1));
            check_nomsg(lly=uves_max_int(pos_y-hbox_sy,1));
            check_nomsg(llx=uves_min_int(llx,sx));
            check_nomsg(lly=uves_min_int(lly,sy));

            check_nomsg(urx=uves_min_int(pos_x+hbox_sx,sx));
            check_nomsg(ury=uves_min_int(pos_y+hbox_sy,sy));
            check_nomsg(urx=uves_max_int(urx,1));
            check_nomsg(ury=uves_max_int(ury,1));

            check_nomsg(llx=uves_min_int(llx,urx));
            check_nomsg(lly=uves_min_int(lly,ury));

	    check_nomsg(pvec_flux[k]=0);

            check_nomsg(pvec_flux[k]=cpl_image_get_median_window(flat_mflat,llx,lly,urx,ury));

            k++;
         }

      }

      flux_median=cpl_vector_get_median(vec_flux);
      uves_msg("Flat %d normalize factor iter2: %g",i,flux_median);
      cpl_image_divide_scalar(flat,flux_median);
      cpl_imagelist_set(flats_norm,cpl_image_duplicate(flat),i);
      mean_explevel+=flux_median;
   }
   mean_explevel/=ni;
   
   check_nomsg(cpl_imagelist_multiply_scalar(flats_norm,mean_explevel));

   check( master_flat = cpl_imagelist_collapse_median_create(flats_norm),
          "Error computing median");




  cleanup:

   uves_free_imagelist(&flats_norm);
   uves_free_vector(&vec_flux);
   uves_free_image(&flat_mflat);
   uves_free_image(&flat);
   uves_check_rec_status(0);
   return master_flat;

}


/**
 * @brief
 *   Stack images using k-sigma clipping
 *
 * @param flats             List of flats to stack
 * @param ordertable        Input order table
 * @param order_locations   polynomial description of order locations
 *
 * @return Stacked image.
 *
 * The input list of flats is analized to compute
 * for each flat
 *     for each order
 *         The median flux on a number of windows of given X * Y size.
 *         The mean flux  of the values computed on each order is computed.
 *     endfor
 *     Finally the mean flux of all means is computed.
 *     The flat is normalized by the computed mean
 *  endfor
 */

cpl_image *
uves_flat_create_normalized_master(cpl_imagelist * flats,
                                   const cpl_table *ordertable,
                                   const polynomial* order_locations,
				   const cpl_vector* gain_vals ,
				   double* fnoise)
{
   int         ni;
   cpl_image  *image=NULL;
   cpl_image* master_flat=NULL;
   cpl_imagelist* flats_norm=NULL;
   int   k=0;
   int ord_min=0;
   int ord_max=0;
   int nord=0;
   double flux_mean=0;
   int nsam=10;
   int x_space=10;
   int hbox_sx=0;
   int hbox_sy=10;
   int is=0;
   int pos_x=0;
   int pos_y=0;
   int llx=0;
   int lly=0;
   int urx=0;
   int ury=0;

   double x=0;
   double y=0;
   int sx=0;
   int sy=0;
   cpl_vector* vec_flux_ord=NULL;
   cpl_vector* vec_flux_sam=NULL;
   double* pvec_flux_ord=NULL;
   double* pvec_flux_sam=NULL;
   int absord=0;
   int ord=0;
   const double* pgain_vals=NULL;
   double fnoise_local=0;

   passure(flats != NULL, "Null input flats imagelist!");
   passure(order_locations != NULL, "Null input order locations polinomial!");

   ni         = cpl_imagelist_get_size(flats);

   image      = cpl_image_duplicate(cpl_imagelist_get(flats, 0));
   sx         = cpl_image_get_size_x(image);
   sy         = cpl_image_get_size_y(image);

   uves_free_image(&image);
   ord_min=cpl_table_get_column_min(ordertable,"Order");
   ord_max=cpl_table_get_column_max(ordertable,"Order");
   nord=ord_max-ord_min+1;
   vec_flux_ord=cpl_vector_new(nord);
   vec_flux_sam=cpl_vector_new(nsam);
   pvec_flux_ord=cpl_vector_get_data(vec_flux_ord);
   pvec_flux_sam=cpl_vector_get_data(vec_flux_sam);
   hbox_sx=(int)((sx-2*x_space)/(2*nsam)+0.5);
   flats_norm=cpl_imagelist_new();
   pgain_vals=cpl_vector_get_data_const(gain_vals);

   for(k=0;k<ni;k++) {
      uves_free_image(&image);
      image = cpl_image_duplicate(cpl_imagelist_get(flats, k));
      for(ord=0;ord<nord;ord++) {
         absord=ord+ord_min;
         pos_x=-hbox_sx;
         for(is=0;is<nsam;is++) {
            pos_x+=(2*hbox_sx+x_space);
            x=(int)(pos_x+0.5);

            check_nomsg(y=uves_polynomial_evaluate_2d(order_locations, 
                                                      x, absord));
            pos_y=(int)(y+0.5);

            llx=uves_max_int(pos_x-hbox_sx,1);
            lly=uves_max_int(pos_y-hbox_sy,1);
            llx=uves_min_int(llx,sx);
            lly=uves_min_int(lly,sy);

            urx=uves_min_int(pos_x+hbox_sx,sx);
            ury=uves_min_int(pos_y+hbox_sy,sy);
            urx=uves_max_int(urx,1);
            ury=uves_max_int(ury,1);

            llx=uves_min_int(llx,urx);
            lly=uves_min_int(lly,ury);

            check_nomsg(pvec_flux_sam[is]=cpl_image_get_median_window(image,llx,lly,urx,ury));

         }
         pvec_flux_ord[ord]=cpl_vector_get_mean(vec_flux_sam);
      }

      flux_mean=cpl_vector_get_mean(vec_flux_ord);
      uves_msg("Flat %d normalize factor inter1: %g",k,flux_mean);
      fnoise_local+=pgain_vals[k]*flux_mean;
      cpl_image_divide_scalar(image,flux_mean);
      cpl_imagelist_set(flats_norm,cpl_image_duplicate(image),k);
   }
   *fnoise=1./sqrt(fnoise_local);
   check( master_flat = cpl_imagelist_collapse_median_create(flats_norm),
          "Error computing median");
 
   uves_msg("FNOISE %g ",*fnoise);
  cleanup:

   uves_free_vector(&vec_flux_ord);
   uves_free_vector(&vec_flux_sam);
   uves_free_image(&image);
   uves_free_imagelist(&flats_norm);


   return master_flat;

}

/*---------------------------------------------------------------------------*/
/** 
 * @brief  Remove cosmic ray events on single ccd exposure and replace them by interpolation on neighbourhood pixels.
   @author P.Magain, M.Remy, Institut d'Astrophysique de Liege
           Ported to UVES pipe from MIDAS rcosmic.for
   @param  ima input image
   @param  flt median filter of input image
   @param  out output image
   @param  bkg mean value of the background
   @param  ron Readout noise in ADU units.
   @param  gain Inverse gain factor (e-/ADU)
   @param  ns threshold for the detection of cosmic rays
   @param  nc critical ratio for discrimination of objects and cosmic rays
   @param  msk name of an optional frame containing the value 1 for cosmic
              rays and 0 for all other pixels
   @note   The detection threshold is in units of the theoretical noise
           sigma of each pixel; it's default value is 4. The default for
           `rc' is 2.

   @doc


 * @return 
 */
/*---------------------------------------------------------------------------*/

static cpl_error_code 
uves_cosrout(cpl_image* ima,
             cpl_image** msk,
             const double ron, 
             const double gain,
             const int ns,
             const double sky,
             const double rc,
             cpl_image** flt,
             cpl_image** out)
{


/*

      SUBROUTINE COSROUT(AI,COSMIC,I_IMA,J_IMA,RON,GAIN,
     1                   NS,SKY,RC,AM,AO)
      INTEGER I_IMA,J_IMA,NUM
      INTEGER ORD(10000)
      INTEGER K,L
      INTEGER IDUMAX,JDUMAX,I1,I2,J1,II,JJ
      INTEGER I,J,IMAX,JMAX,IMIN,JMIN
      INTEGER FIRST(2),NEXT(2)
      INTEGER*2 COSMIC(I_IMA,J_IMA)
      REAL*4 VECTEUR(10000),FMAX,ASUM,RC
      REAL*4 AI(I_IMA,J_IMA),AO(I_IMA,J_IMA),AM(I_IMA,J_IMA)
      REAL*4 SIGMA,SKY,S1,S2
      REAL*4 RON,GAIN,NS,AMEDIAN

*/

  int sx=0;
  int sy=0;
  int i=0;
  int j=0;
  int k=1;
  int pix=0;
  int first[2];
  int next_x=0;
  int next_y=0;
  int i_min=0;
  int i_max=0;
  int j_min=0;
  int j_max=0;
  int idu_max=0;
  int jdu_max=0;
  int i1=0;
  int i2=0;
  int ii=0;
  int jj=0;
  int j1=0;
  int num=0;
  int l=0;
  int nmax=1e6;
  int ord[nmax];


  float* pi=NULL;
  float* po=NULL;
  float* pf=NULL;
  int* pm=NULL;
  float sigma=0;


  float vec[nmax];

  double f_max=0;
  double s1=0;
  double s2=0;
  double asum=0;
  double a_median=0;

  uves_msg_warning("sky=%g gain=%g ron=%g ns=%d rc=%g",sky,gain,ron,ns,rc);
  check_nomsg(sx=cpl_image_get_size_x(ima));
  check_nomsg(sy=cpl_image_get_size_y(ima));
  check_nomsg(pi=cpl_image_get_data_float(ima));
  //*flt=cpl_image_new(sx,sy,CPL_TYPE_FLOAT);
  *msk=cpl_image_new(sx,sy,CPL_TYPE_INT);

  check_nomsg(pf=cpl_image_get_data_float(*flt));
  check_nomsg(pm=cpl_image_get_data_int(*msk));

  check_nomsg(*out=cpl_image_duplicate(ima));
  check_nomsg(po=cpl_image_get_data_float(*out));

/*

      DO 10 J=1,J_IMA
      DO 5 I=1,I_IMA
      AO(I,J)=AI(I,J)
      COSMIC(I,J)= 0
    5 CONTINUE
   10 CONTINUE

C
C     La boucle suivante selectionne les pixels qui sont
C     significativ+ement au dessus de l'image filtree medianement.
C
C    The flowing loop selects the pixels that are much higher that the 
C    median filter image
C
C     COSMIC =-1 ----> candidate for cosmic
C            = 0 ----> not a cosmic
C            = 1 -----> a cosmic (at the end)
C            = 2 ----> member of the group
C            = 3 ----> member of a group which has been examined
C            = 4 ----> neighbourhood  of the group
      K=1
      DO 80 J=2,J_IMA-1
      DO 70 I=2,I_IMA-1
      SIGMA=SQRT(RON**2+AM(I,J)/GAIN)
      IF ((AI(I,J)-AM(I,J)).GE.(NS*SIGMA)) THEN
            COSMIC(I,J) = -1
            K = K+1
      ENDIF
   70 CONTINUE
   80 CONTINUE


*/


  uves_msg_warning("Set all pix to = -1 -> candidate for cosmic");
  k=1;
  for(j=1;j<sy-1;j++) {
    for(i=1;i<sx-1;i++) {
      pix=j*sx+i;
      sigma=sqrt(ron*ron+pf[pix]/gain);
      if ( (pi[pix]-pf[pix]) >= (ns*sigma) ) {
	pm[pix]=-1;
        k++;
      }
    }
  }


  /*

     La boucle suivante selectionne les pixels qui sont
     significativement au dessus de l'image filtree medianement.

     The flowing loop selects the pixels that are much higher that the 
     median filter image


     COSMIC =-1 ----> candidate for cosmic
            = 0 ----> not a cosmic
            = 1 -----> a cosmic (at the end)
            = 2 ----> member of the group
            = 3 ----> member of a group which has been examined
            = 4 ----> neighbourhood  of the group

  */


/*
  Ces pixels sont regroupes par ensembles connexes dans la boucle
  This pixels are gouped as grouped together if neibours
*/

  first[0]=1;
  first[1]=1;

 lab100:
  check_nomsg(uves_find_next(msk,first[1],&next_x, &next_y));

  if(next_x==-1) return CPL_ERROR_NONE;
  i=next_x;
  j=next_y;

  uves_msg_debug("p[%d,%d]=  2 -> member of the group",i,j);
  pix=j*sx+i;
  pm[pix]=2;

  i_min=i;
  i_max=i;
  j_min=j;
  j_max=j;
  idu_max=i;
  jdu_max=j;
  f_max=pi[pix];

 lab110:
  i1=0;
  i2=0;



/*
      FIRST(1) = 2
      FIRST(2) = 2
  100 CALL FINDNEXT(COSMIC,I_IMA,J_IMA,FIRST,NEXT)
      IF (NEXT(1).EQ.-1) RETURN
      I = NEXT(1)
      J = NEXT(2) 
      COSMIC(I,J) = 2
      IMIN = I
      IMAX = I 
      JMIN = J
      JMAX = J
      IDUMAX = I
      JDUMAX = J
      FMAX = AI(I,J)
  110 I1 = 0
      I2 = 0
      CONTINUE

*/

  for(l=0;l<2;l++) {
    for(k=0;k<2;k++) {
      ii=i+k-l;
      jj=j+k+l-3;
      pix=jj*sx+ii;
      if(pm[pix]==-1) {
	i1=ii;
	j1=jj;
	i_min=(i_min<ii) ? i_min: ii;
	i_max=(i_max>ii) ? i_max: ii;
	j_min=(j_min<jj) ? j_min: jj;
	j_max=(j_max>jj) ? j_max: jj;
        uves_msg_debug("p[%d,%d]= 2 -> member of the group",ii,jj);
	pm[pix]=2;
	if(pi[pix]>f_max) {
	  f_max=pi[pix];
	  idu_max=ii;
	  idu_max=jj;
	}
      } else if(pm[pix]==0) {
	pm[pix]=4;
        uves_msg_debug("p[%d,%d]= 4 -> neighbourhood  of the group",k,l);
      }
    }
  }


/*
      DO 125 L=1,2
          DO 115 K=1,2
               II = I+K-L
               JJ = J+K+L-3
               IF (COSMIC(II,JJ).EQ.-1) THEN
                   I1 = II
                   J1 = JJ  
                   IMIN = MIN(IMIN,II) 
                   IMAX = MAX(IMAX,II)
                   JMIN = MIN(JMIN,JJ)
                   JMAX = MAX(JMAX,JJ)
                   COSMIC(II,JJ) = 2
                   IF (AI(II,JJ).GT.FMAX) THEN
                         FMAX = AI(II,JJ)
                         IDUMAX = II
                         JDUMAX = JJ
                   ENDIF
                ELSE IF (COSMIC(II,JJ).EQ.0) THEN
                   COSMIC(II,JJ) = 4
                ENDIF
  115     CONTINUE 
  125 CONTINUE 

*/


  pix=j*sx+i;
  pm[pix]=3;
  uves_msg_debug("p[%d,%d]= 3 -> member of a group which has been examined",i,j);
  if(i1 != 0) {
    i=i1;
    j=j1;
    goto lab110;
  }


/*
      COSMIC(I,J) = 3
      IF (I1.NE.0) THEN
      I = I1
      J = J1
      GOTO 110
      ENDIF    
*/

  for(l=j_min;l<=j_max;l++){
    for(k=i_min;k<=i_max;k++){
      pix=l*sy+k;
      if(pm[pix] == 2) {
	i=k;
	j=l;
	goto lab110;
      }
    }
  }
  first[0] = next_x+1;
  first[1] = next_y; 


/*
      DO 140 L = JMIN,JMAX  
         DO 130 K = IMIN,IMAX
              IF (COSMIC(K,L).EQ.2) THEN
                 I = K
                 J = L
                 GOTO 110
              ENDIF
  130 CONTINUE
  140 CONTINUE   
      FIRST(1) = NEXT(1)+1
      FIRST(2) = NEXT(2) 

*/


  /*
  We start here the real work....
  1- decide if the pixel's group is a cosmic
  2-replace these values by another one
  */
  s1=pi[(jdu_max-1)*sx+idu_max-1]+
     pi[(jdu_max-1)*sx+idu_max+1]+
     pi[(jdu_max-1)*sx+idu_max]+
     pi[(jdu_max+1)*sx+idu_max];

  s2=pi[(jdu_max+1)*sy+idu_max-1]+
     pi[(jdu_max+1)*sy+idu_max+1]+
     pi[(jdu_max)*sy+idu_max-1]+
     pi[(jdu_max)*sy+idu_max+1];
  asum=(s1+s2)/8.-sky;


/*

C We start here the real work....
C 1- decide if the pixel's group is a cosmic
C 2-replace these values by another one
      
      S1 = AI(IDUMAX-1,JDUMAX-1) + 
     !     AI(IDUMAX+1,JDUMAX-1) +     
     !     AI(IDUMAX,JDUMAX-1)   +
     !     AI(IDUMAX,JDUMAX+1)

      S2 = AI(IDUMAX-1,JDUMAX+1) + 
     !     AI(IDUMAX+1,JDUMAX+1) +
     !     AI(IDUMAX-1,JDUMAX)   + 
     !     AI(IDUMAX+1,JDUMAX)
      ASUM = (S1+S2)/8.-SKY

*/

  if((f_max-sky) > rc*asum) {
    num=0;
    for( l = j_min-1; l <= j_max+1; l++) {
      for( k = i_min-1; k<= i_max+1;k++) {
	if(pm[l*sx+k]==4) {
	  vec[num]=pi[l*sx+k];
	  num++;
	}
      }
    }


/*

      IF ((FMAX-SKY).GT.RC*ASUM) THEN
         NUM = 1
         DO L = JMIN-1,JMAX+1
            DO K = IMIN-1,IMAX+1
               IF (COSMIC(K,L).EQ.4) THEN
                   VECTEUR(NUM) = AI(K,L)
                   NUM = NUM+1
               ENDIF    
            ENDDO
         ENDDO

*/

    uves_sort(num-1,vec,ord);
    a_median=vec[ord[(num-1)/2]];
    for(l = j_min-1; l <= j_max+1 ; l++){
      for(k = i_min-1 ; k <= i_max+1 ; k++){
	if(pm[l*sx+k] == 3) {
	   pm[l*sx+k]=1;
           uves_msg_debug("p[%d,%d]= 1 -> a cosmic (at the end)",k,l);

	   po[l*sx+k]=a_median;
	} else if (pm[l*sx+k] == 4) {
	   po[l*sx+k]=0;
	   po[l*sx+k]=a_median;//here we set to median instead than 0
	}
      }
    }


/*
         CALL SORT(NUM-1,VECTEUR,ORD)
         AMEDIAN = VECTEUR(ORD((NUM-1)/2))
         DO L = JMIN-1,JMAX+1
            DO K = IMIN-1,IMAX+1
               IF (COSMIC(K,L).EQ.3) THEN
                   COSMIC(K,L) = 1
                   AO(K,L) = AMEDIAN
               ELSE IF (COSMIC(K,L).EQ.4) THEN
                   COSMIC(K,L) = 0
               ENDIF
            ENDDO
         ENDDO
*/

  } else {
    for( l = j_min-1 ; l <= j_max+1 ; l++) {
      for( k = i_min-1 ; k <= i_max+1 ; k++) {
	if(pm[l*sx+k] != -1) {
           uves_msg_debug("p[%d,%d]= 0 -> not a cosmic",k,l);
	   pm[l*sx+k] = 0;
	}
      }
    }
  }


  if (next_x >0) goto lab100;


/*
      ELSE 
         DO L = JMIN-1,JMAX+1
            DO K = IMIN-1,IMAX+1
               IF (COSMIC(K,L).NE.-1) COSMIC(K,L) = 0
            ENDDO
          ENDDO
      ENDIF
        
      
 
      IF (NEXT(1).GT.0) GOTO 100
C
C
C
      RETURN
      END


*/


  cleanup:

  return CPL_ERROR_NONE;

}





static cpl_error_code 
uves_find_next(cpl_image** msk,
               const int first_y,
               int* next_x,
               int* next_y)
{
  int sx=cpl_image_get_size_x(*msk);
  int sy=cpl_image_get_size_y(*msk);
  int i=0;
  int j=0;
  int* pc=NULL;
  int pix=0;



  check_nomsg(pc=cpl_image_get_data_int(*msk));
  for(j=first_y;j<sy;j++) {
    for(i=1;i<sx;i++) {
      pix=j*sx+i;
      if(pc[pix]==-1) {
	*next_x=i;
	*next_y=j;
	return CPL_ERROR_NONE;
      }
    }
  }

  *next_x=-1;
  *next_y=-1;
  cleanup:
  return CPL_ERROR_NONE;

}

/*

      SUBROUTINE FINDNEXT(COSMIC,I_IMA,J_IMA,FIRST,NEXT)
      INTEGER I_IMA,J_IMA,FIRST(2),NEXT(2)
      INTEGER I,J
      INTEGER*2 COSMIC(I_IMA,J_IMA)
      DO J = FIRST(2), J_IMA
          DO I = 2, I_IMA
             IF (COSMIC(I,J).EQ.-1) THEN
                 NEXT(1) = I
                 NEXT(2) = J
                 RETURN
             ENDIF
          ENDDO
      ENDDO 
      NEXT(1) = -1
      NEXT(2) = -1
      RETURN
      END

*/


//Be carefull with F77 and C indexing
static cpl_error_code
uves_sort(const int kmax,float* inp, int* ord)
{
  int k=0;
  int j=0;
  int l=0;
  float f=0;
  int i_min=0;
  int i_max=0;
  int i=0;

  for(k=0;k<kmax;k++) {
    ord[k]=k;
  }

  if(inp[0]>inp[1]) {
    ord[0]=1;
    ord[1]=0;
  }

  for(j=2;j<kmax;j++) {
    f=inp[j];
    l=inp[j-1];

/*
      SUBROUTINE SORT(KMAX,INP,ORD)
      INTEGER KMAX,IMIN,IMAX,I,J,K,L
      INTEGER ORD(10000)
      REAL*4 INP(10000),F
      DO 4100 J=1,KMAX
      ORD(J)=J
 4100 CONTINUE
      IF (INP(1).GT.INP(2)) THEN 
             ORD(1)=2
             ORD(2)=1
      END IF
      DO 4400 J=3,KMAX
      F=INP(J)
      L=ORD(J-1)
*/

  if(inp[l]<=f) goto lab4400;
    l=ord[0];
    i_min=0;
    if(f<=inp[l]) goto lab4250;
    i_max=j-1;
  lab4200:
    i=(i_min+i_max)/2;
    l=ord[i];

/*
      IF (INP(L).LE.F) GO TO 4400
      L=ORD(1)
      IMIN=1
      IF (F.LE.INP(L)) GO TO 4250
      IMAX=J-1
 4200 I=(IMIN+IMAX)/2
      L=ORD(I)
*/

    if(inp[l]<f) {
      i_min=i;
    } else {
      i_max=i;
    }
    if(i_max>(i_min+1)) goto lab4200;
    i_min=i_max;
  lab4250:
    for(k=j-2;k>=i_min;k--) {
      ord[k+1]=ord[k];
    }
    ord[i_min]=j;
  lab4400:
    return CPL_ERROR_NONE;
  }
    return CPL_ERROR_NONE;
}

/*
      IF (INP(L).LT.F) THEN
              IMIN=I
              ELSE
              IMAX=I
      END IF
      IF (IMAX.GT.(IMIN+1)) GO TO 4200
      IMIN=IMAX
 4250 DO 4300 K=J-1,IMIN,-1
      ORD(K+1)=ORD(K)
 4300 CONTINUE
      ORD(IMIN)=J
 4400 CONTINUE
      RETURN
      END
*/

/*---------------------------------------------------------------------------*/
/** 
 * @brief  Extract frames with given tag from frameset
 * @param  pin input parameterlist 
 * @return newly allocated parametrlist or NULL on error
 */
/*---------------------------------------------------------------------------*/

cpl_parameterlist* 
uves_parameterlist_duplicate(const cpl_parameterlist* pin){

   cpl_parameter* p=NULL;
   cpl_parameterlist* pout=NULL;

   pout=cpl_parameterlist_new();
   p=cpl_parameterlist_get_first((cpl_parameterlist*)pin);
   while (p != NULL)
   {
      cpl_parameterlist_append(pout,p);
      p=cpl_parameterlist_get_next((cpl_parameterlist*)pin);
   }
   return pout;

}
/**
 * @brief
 *   Convert all lowercase characters in a string into uppercase
 *   characters.
 *
 * @param s  The string to convert.
 *
 * @return Returns a pointer to the converted string.
 *
 * Walks through the given string and turns lowercase characters into
 * uppercase characters using @b toupper().
 *
 * @see uves_string_tolower()
 */


const char*
uves_string_toupper(char* s)
{

    char *t = s;

    if( s == NULL) { 
       cpl_error_set(cpl_func,CPL_ERROR_NULL_INPUT); 
       return NULL;
    };
    while (*t) {
        *t = toupper(*t);
        t++;
    }

    return s;

}

/**
 * @brief
 *   Convert all uppercase characters in a string into lowercase
 *   characters.
 *
 * @param s  The string to convert.
 *
 * @return Returns a pointer to the converted string.
 *
 * Walks through the given string and turns uppercase characters into
 * lowercase characters using @b tolower().
 *
 * @see uves_string_tolower()
 */

const char*
uves_string_tolower(char* s)
{

    char *t = s;

    if( s == NULL) { 
       cpl_error_set(cpl_func,CPL_ERROR_NULL_INPUT); 
       return NULL;
    };
    while (*t) {
        *t = tolower(*t);
        t++;
    }

    return s;

}




/*----------------------------------------------------------------------------*/
/** 
 * @brief  Extract frames with given tag from frameset
 * @param  frames      frame set
 * @param  tag         to search for
 * @return newly allocated, possibly empty, frameset, or NULL on error
 */
/*----------------------------------------------------------------------------*/
cpl_frameset *
uves_frameset_extract(const cpl_frameset *frames,
                      const char *tag)
{
    cpl_frameset *subset = NULL;
    const cpl_frame *f;



    assure( frames != NULL, CPL_ERROR_ILLEGAL_INPUT, "Null frameset" );
    assure( tag    != NULL, CPL_ERROR_ILLEGAL_INPUT, "Null tag" );
    
    subset = cpl_frameset_new();

    for (f = cpl_frameset_find_const(frames, tag);
         f != NULL;
         f = cpl_frameset_find_const(frames, NULL)) {

        cpl_frameset_insert(subset, cpl_frame_duplicate(f));
    }

 cleanup:
    return subset;
}

/*----------------------------------------------------------------------------*/
/**
  @brief   Calculate x to the y'th
  @param   x
  @param   y   The exponent. May be positive or zero or negative.

  @note This function is (very much, on some platforms) faster
        than pow() from math.h when y is an integer

 */
/*----------------------------------------------------------------------------*/
double
uves_pow_int(double x, int y)
{
    double result = 1.0;

    /* Invariant is:   result * x ^ y   */
    

    while(y != 0)
    {
        if (y % 2 == 0)
        {
            x *= x;
            y /= 2;
        }
        else
        {
            if (y > 0)
            {
                result *= x;
                y -= 1;            
            }
            else
            {
                result /= x;
                y += 1;            
            }
        }
    }
    
    return result;
}






/*----------------------------------------------------------------------------*/
/**
   @brief    Get UVES library version number
   @param    major        (output) If non-null, the major version number
   @param    minor        (output) If non-null, the minor version number
   @param    micro        (output) If non-null, the micro version number
   @return   CPL_ERROR_NONE iff okay

*/
/*----------------------------------------------------------------------------*/
cpl_error_code
uves_get_version(int *major, int *minor, int *micro)
{
    /* Macros are defined in config.h */
    if (major != NULL) *major = UVES_MAJOR_VERSION;
    if (minor != NULL) *minor = UVES_MINOR_VERSION;
    if (micro != NULL) *micro = UVES_MICRO_VERSION;

    return cpl_error_get_code();
}


/*----------------------------------------------------------------------------*/
/**
   @brief    Get UVES library binary version number
   @return   Binary version number

*/
/*----------------------------------------------------------------------------*/
int
uves_get_version_binary(void)
{
    return UVES_BINARY_VERSION;
}


/*----------------------------------------------------------------------------*/
/**
   @brief    Get the pipeline copyright and license
   @return   The copyright and license string

   The function returns a pointer to the statically allocated license string.
   This string should not be modified using the returned pointer.
*/
/*----------------------------------------------------------------------------*/
const char *
uves_get_license(void)
{
    return
    "This file is part of the ESO UVES Instrument Pipeline\n"
    "Copyright (C) 2004,2005,2006 European Southern Observatory\n"
    "\n"
    "This program is free software; you can redistribute it and/or modify\n"
    "it under the terms of the GNU General Public License as published by\n"
    "the Free Software Foundation; either version 2 of the License, or\n"
    "(at your option) any later version.\n"
    "\n"
    "This program is distributed in the hope that it will be useful,\n"
    "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
    "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n"
        "GNU General Public License for more details.\n"
        "\n"
        "You should have received a copy of the GNU General Public License\n"
        "along with this program; if not, write to the Free Software\n"
        "Foundation, 51 Franklin St, Fifth Floor, Boston, \n"
        "MA  02111-1307  USA" ;

    /* Note that long strings are unsupported in C89 */
}

/*----------------------------------------------------------------------------*/
/**
   @brief    Check compile time and runtime library versions
   @return      CPL_ERROR_NONE iff OK

   The function returns error if CPL and QFITS version are not up to date.
   Running with versions older than required will cause subtle bugs.

   This function should be called from "make check" to verify an installation
*/
/*----------------------------------------------------------------------------*/
/* To change requirements, just edit these numbers */
#define REQ_CPL_MAJOR 3
#define REQ_CPL_MINOR 1
#define REQ_CPL_MICRO 0

#define REQ_QF_MAJOR 6
#define REQ_QF_MINOR 2
#define REQ_QF_MICRO 0

void
uves_check_version(void)
{
#ifdef CPL_VERSION_CODE
#if CPL_VERSION_CODE >= CPL_VERSION(REQ_CPL_MAJOR, REQ_CPL_MINOR, REQ_CPL_MICRO)
    uves_msg_debug("Compile time CPL version code was %d "
                   "(version %d-%d-%d, code %d required)",
                   CPL_VERSION_CODE, REQ_CPL_MAJOR, REQ_CPL_MINOR, REQ_CPL_MICRO,
                   CPL_VERSION(REQ_CPL_MAJOR, REQ_CPL_MINOR, REQ_CPL_MICRO));
#else
#error CPL version too old
#endif
#else  /* ifdef CPL_VERSION_CODE */
#error CPL_VERSION_CODE not defined. CPL version too old
#endif

    if (cpl_version_get_major() < REQ_CPL_MAJOR ||
    (cpl_version_get_major() == REQ_CPL_MAJOR && 
     (int) cpl_version_get_minor() < REQ_CPL_MINOR) || /* cast suppresses warning
                                                              about comparing unsigned < 0 */
    (cpl_version_get_major() == REQ_CPL_MAJOR &&
     cpl_version_get_minor() == REQ_CPL_MINOR && 
     (int) cpl_version_get_micro() < REQ_CPL_MICRO)
    )
    {
        uves_msg_warning("CPL version %s (%d.%d.%d) (detected) is not supported. "
                 "Please update to CPL version %d.%d.%d or later", 
                 cpl_version_get_version(),
                 cpl_version_get_major(),
                 cpl_version_get_minor(),
                 cpl_version_get_micro(),
                 REQ_CPL_MAJOR,
                 REQ_CPL_MINOR,
                 REQ_CPL_MICRO);
    }
    else
    {
        uves_msg_debug("Runtime CPL version %s (%d.%d.%d) detected (%d.%d.%d or later required)",
               cpl_version_get_version(),
               cpl_version_get_major(),
               cpl_version_get_minor(),
               cpl_version_get_micro(),
               REQ_CPL_MAJOR,
               REQ_CPL_MINOR,
               REQ_CPL_MICRO);
    }

    {
    const char *qfts_v = " ";
    char *suffix;
    
    long qfts_major;
    long qfts_minor;
    long qfts_micro;

    qfts_v = qfits_version();

    assure( qfts_v != NULL, CPL_ERROR_ILLEGAL_INPUT,
        "Error reading qfits version");

    /* Parse    "X.[...]" */
    qfts_major = strtol(qfts_v, &suffix, 10);
    assure( suffix != NULL && suffix[0] == '.' && suffix[1] != '\0', 
        CPL_ERROR_ILLEGAL_INPUT, 
        "Error parsing version string '%s'. "
        "Format 'X.Y.Z' expected", qfts_v);

    /* Parse    "Y.[...]" */
    qfts_minor = strtol(suffix+1, &suffix, 10);
    assure( suffix != NULL && suffix[0] == '.' && suffix[1] != '\0', 
        CPL_ERROR_ILLEGAL_INPUT,
        "Error parsing version string '%s'. "
        "Format 'X.Y.Z' expected", qfts_v);

    /* Parse    "Z" */
    qfts_micro = strtol(suffix+1, &suffix, 10);

    /* If qfits version is earlier than required ... */
    if (qfts_major < REQ_QF_MAJOR ||
        (qfts_major == REQ_QF_MAJOR && qfts_minor  < REQ_QF_MINOR) ||
        (qfts_major == REQ_QF_MAJOR && qfts_minor == REQ_QF_MINOR && 
         qfts_micro < REQ_QF_MICRO)
        )
        {
        uves_msg_warning("qfits version %s (detected) is not supported. "
                 "Please update to qfits version %d.%d.%d or later", 
                 qfts_v,
                 REQ_QF_MAJOR,
                 REQ_QF_MINOR,
                 REQ_QF_MICRO);
        }
    else
        {
        uves_msg_debug("qfits version %ld.%ld.%ld detected "
                   "(%d.%d.%d or later required)", 
                   qfts_major, qfts_minor, qfts_micro,
                   REQ_QF_MAJOR,
                   REQ_QF_MINOR,
                   REQ_QF_MICRO);
        }
    }
    
  cleanup:
    return;
}

/*----------------------------------------------------------------------------*/
/**
   @brief    Recipe termination
   @param       recipe_id      Name of calling recipe
   @param       frames         The output frame set
   @return       CPL_ERROR_NONE iff OK

   This function is called at the end of a recipe. The output frame set is 
   printed, and the number of warnings produced by the recipe (using uves_msg_warning())
   is summarized.
   See also @c uves_initialize().
*/
/*----------------------------------------------------------------------------*/
cpl_error_code
uves_end(const char *recipe_id, const cpl_frameset *frames)
{
    cpl_frameset *products = NULL;
    const cpl_frame *f;
    int warnings = uves_msg_get_warnings();

    recipe_id = recipe_id; /* Suppress warning about unused variable,
                  perhaps we the recipe_id later, so
                  keep it in the interface. */


    /* Print (only) output frames */

    products = cpl_frameset_new();
    assure_mem( products );
    int i=0;
    int nfrm=0;
    nfrm=cpl_frameset_get_size(frames);
    for (i=0;i<nfrm;i++)
    {
        f=cpl_frameset_get_frame_const(frames,i);
        if (cpl_frame_get_group(f) == CPL_FRAME_GROUP_PRODUCT)
        {
            check_nomsg(
            cpl_frameset_insert(products, cpl_frame_duplicate(f)));
        }
    }

/* Don't do this. EsoRex should.
   uves_msg_low("Output frames");
   check( uves_print_cpl_frameset(products),
   "Could not print output frames");
*/

    /* Summarize warnings, if any */
    if( warnings > 0)
    {
        uves_msg_warning("Recipe produced %d warning%s (excluding this one)",
                 uves_msg_get_warnings(),
                 /* Plural? */ (warnings > 1) ? "s" : "");
    }

  cleanup:
    uves_free_frameset(&products);
    return cpl_error_get_code();    
}

/*----------------------------------------------------------------------------*/
/**
   @brief   Recipe initialization
   @param  frames         The input frame set
   @param  parlist        The input parameter list
   @param  recipe_id      Name of the recipe, e.g. @em uves_mbias
   @param  short_descr    A short description of what the recipe does
   @param  frames         The input frame set
   @return  current time in ISO 8601 format

   This function takes care of all the mandatory tasks that are common for every recipe
   before the beginning of the data reduction. This function
   - initializes error handling (by resetting the error state),
   - initializes messaging (thereby setting the severity level),
   - initializes plotting (by reading the plotting commands from the parameterlist
     and passing these to the plotting module),
   - makes sure that the CPL and qfits libraries are up to date,
     and prints a warning message if not,
   - defines the group of all input frames (see @c uves_dfs_set_groups()), and
   - prints the input frame set.
*/
/*----------------------------------------------------------------------------*/
char *
uves_initialize(cpl_frameset *frames, const cpl_parameterlist *parlist, 
        const char *recipe_id, const char *short_descr)
{
    char *recipe_string = NULL;
    char *stars = NULL;     /* A string of stars */
    char *spaces1 = NULL;
    char *spaces2 = NULL;
    char *spaces3 = NULL;
    char *spaces4 = NULL;
    char *start_time = NULL;

    start_time = uves_sprintf("%s", uves_get_datetime_iso8601());

    check( uves_check_version(), "Library validation failed");

    /* Now read parameters and set specified message level */
    {
    const char *plotter_command;
    int msglevel;
    
    /* Read parameters using context = recipe_id */

        if (0) /* disabled */
            check( uves_get_parameter(parlist, NULL, "uves", "msginfolevel", 
                                      CPL_TYPE_INT, &msglevel),
                   "Could not read parameter");
        else
            {
                msglevel = -1; /* max verbosity */
            }
    uves_msg_set_level(msglevel);
    check( uves_get_parameter(parlist, NULL, "uves", "plotter",
                  CPL_TYPE_STRING, &plotter_command), "Could not read parameter");
    
    /* Initialize plotting */
    check( uves_plot_initialize(plotter_command), 
           "Could not initialize plotting");
    }    

    /* Print 
     *************************
     ***   PACAGE_STRING   ***
     *** Recipe: recipe_id ***
     *************************
     */
    recipe_string = uves_sprintf("Recipe: %s", recipe_id);
    {
    int field = uves_max_int(strlen(PACKAGE_STRING), strlen(recipe_string));
    int nstars = 3+1 + field + 1+3;
    int nspaces1, nspaces2, nspaces3, nspaces4;
    int i;
    
    /* ' ' padding */
    nspaces1 = (field - strlen(PACKAGE_STRING)) / 2; 
    nspaces2 = field - strlen(PACKAGE_STRING) - nspaces1;

    nspaces3 = (field - strlen(recipe_string)) / 2;
    nspaces4 = field - strlen(recipe_string) - nspaces3;

    spaces1 = cpl_calloc(nspaces1 + 1, sizeof(char)); 
    spaces2 = cpl_calloc(nspaces2 + 1, sizeof(char));
    spaces3 = cpl_calloc(nspaces3 + 1, sizeof(char)); 
    spaces4 = cpl_calloc(nspaces4 + 1, sizeof(char));
    for (i = 0; i < nspaces1; i++) spaces1[i] = ' ';
    for (i = 0; i < nspaces2; i++) spaces2[i] = ' ';
    for (i = 0; i < nspaces3; i++) spaces3[i] = ' ';
    for (i = 0; i < nspaces4; i++) spaces4[i] = ' ';

    stars = cpl_calloc(nstars + 1, sizeof(char));
    for (i = 0; i < nstars; i++) stars[i] = '*';
    
    uves_msg("%s", stars);
    uves_msg("*** %s%s%s ***", spaces1, PACKAGE_STRING, spaces2);
    uves_msg("*** %s%s%s ***", spaces3, recipe_string, spaces4);
    uves_msg("%s", stars);
    }

    uves_msg("This recipe %c%s", tolower(short_descr[0]), short_descr+1);

    if (cpl_frameset_is_empty(frames)) {
        uves_msg_debug("Guvf cvcryvar unf ernpurq vgf uvtu dhnyvgl qhr na npgvir "
                       "hfre pbzzhavgl naq gur erfcbafvoyr naq vqrnyvfgvp jbex bs "
                       "vaqvivqhny cvcryvar qrirybcref, naq qrfcvgr orvat 'onfrq ba' "
                       "PCY juvpu vf n cvrpr bs cbyvgvpny penc");
    }

    /* Set group (RAW/CALIB) of input frames */
    /* This is mandatory for the later call of 
       cpl_dfs_setup_product_header */
    check( uves_dfs_set_groups(frames), "Could not classify input frames");

    /* Print input frames */
    uves_msg_low("Input frames");
    check( uves_print_cpl_frameset(frames), "Could not print input frames" );

  cleanup:
    cpl_free(recipe_string);
    cpl_free(stars);
    cpl_free(spaces1);
    cpl_free(spaces2);
    cpl_free(spaces3);
    cpl_free(spaces4);
    return start_time;
}


/*----------------------------------------------------------------------------*/
/**
   @brief    Optimally average images
   @param    image1      First image
   @param    noise1      Noise (one sigma) of first image
   @param    image2      Second image
   @param    noise2      Noise (one sigma) of second image
   @param    noise       (Output) Combined noise image. This may not be NULL.
   @return   The optimally combined image

   For each pixel the resultant flux is computed as 
   x =  (x1 / sigma_1^2 + x2 / sigma_2^2) 
      / ( 1 / sigma_1^2 +  1 / sigma_2^2)

   and the combined noise is

   1/sigma^2 = 1 / sigma_1^2 + 1 / sigma_2^2.

   Bad pixels are properly propagated (i.e. a resulting pixel is
   marked bad if both inputs are bad ; if only one input is good,
   this input is used as the output and the 'bad' input is ignored).

   @note If it turns out to be necessary, this function can be optimized
   by using 'image1_data[i]'
   rather than 'cpl_image_get(image1, x, y, &pis_rejected1)', i.e.
   avoid multiplication for each pixel

*/
/*----------------------------------------------------------------------------*/
cpl_image *
uves_average_images(const cpl_image *image1, const cpl_image *noise1,
            const cpl_image *image2, const cpl_image *noise2,
            cpl_image **noise)
{
    cpl_image *result = NULL;
    cpl_size nx, ny; 
    int x, y;

    /* Check input */
    assure( image1 != NULL, CPL_ERROR_NULL_INPUT, "Null image");
    assure( image2 != NULL, CPL_ERROR_NULL_INPUT, "Null image");
    assure( noise1 != NULL, CPL_ERROR_NULL_INPUT, "Null image");
    assure( noise2 != NULL, CPL_ERROR_NULL_INPUT, "Null image");
    assure( noise  != NULL, CPL_ERROR_NULL_INPUT, "Null image");

    assure( cpl_image_get_min(noise1) > 0, CPL_ERROR_ILLEGAL_INPUT,
        "Noise must be everywhere positive, minimum = %e", cpl_image_get_min(noise1));
    assure( cpl_image_get_min(noise2) > 0, CPL_ERROR_ILLEGAL_INPUT,
        "Noise must be everywhere positive, minimum = %e", cpl_image_get_min(noise2));
    
    nx = cpl_image_get_size_x(image1);
    ny = cpl_image_get_size_y(image1);

    assure( nx == cpl_image_get_size_x(image2), CPL_ERROR_INCOMPATIBLE_INPUT, 
        "Size mismatch %" CPL_SIZE_FORMAT " != %" CPL_SIZE_FORMAT "",
        nx,   cpl_image_get_size_x(image2));
    assure( nx == cpl_image_get_size_x(noise1), CPL_ERROR_INCOMPATIBLE_INPUT, 
        "Size mismatch %" CPL_SIZE_FORMAT " != %" CPL_SIZE_FORMAT "",
        nx,   cpl_image_get_size_x(noise1));
    assure( nx == cpl_image_get_size_x(noise2), CPL_ERROR_INCOMPATIBLE_INPUT,
        "Size mismatch %" CPL_SIZE_FORMAT " != %" CPL_SIZE_FORMAT "",
        nx,   cpl_image_get_size_x(noise2));
    assure( ny == cpl_image_get_size_y(image2), CPL_ERROR_INCOMPATIBLE_INPUT,
        "Size mismatch %" CPL_SIZE_FORMAT " != %" CPL_SIZE_FORMAT "",
        ny,   cpl_image_get_size_y(image2));
    assure( ny == cpl_image_get_size_y(noise1), CPL_ERROR_INCOMPATIBLE_INPUT,
        "Size mismatch %" CPL_SIZE_FORMAT " != %" CPL_SIZE_FORMAT "",
        ny,   cpl_image_get_size_y(noise1));
    assure( ny == cpl_image_get_size_y(noise2), CPL_ERROR_INCOMPATIBLE_INPUT,
        "Size mismatch %" CPL_SIZE_FORMAT " != %" CPL_SIZE_FORMAT "",
        ny,   cpl_image_get_size_y(noise2));
    
    result = cpl_image_new(nx, ny, CPL_TYPE_DOUBLE);
    *noise = cpl_image_new(nx, ny, CPL_TYPE_DOUBLE);

    /* Do the calculation */
    for (y = 1; y <= ny; y++)
    {
        for (x = 1; x <= nx; x++)
        {
            double flux1, flux2;
            double sigma1, sigma2;
            int pis_rejected1, noise_rejected1;
            int pis_rejected2, noise_rejected2;

            flux1  = cpl_image_get(image1, x, y, &pis_rejected1);
            flux2  = cpl_image_get(image2, x, y, &pis_rejected2);
            sigma1 = cpl_image_get(noise1, x, y, &noise_rejected1);
            sigma2 = cpl_image_get(noise2, x, y, &noise_rejected2);

            pis_rejected1 = pis_rejected1 || noise_rejected1;
            pis_rejected2 = pis_rejected2 || noise_rejected2;
            
            if (pis_rejected1 && pis_rejected2)
            {
                cpl_image_reject(result, x, y);
                cpl_image_reject(*noise, x, y);
            }
            else
            {
                /* At least one good pixel */

                double flux, sigma;
                
                if (pis_rejected1 && !pis_rejected2)
                {
                    flux = flux2;
                    sigma = sigma2;
                }
                else if (!pis_rejected1 && pis_rejected2)
                {
                    flux = flux1;
                    sigma = sigma1;
                }
                else
                {
                    /* Both pixels are good */
                    sigma =
                    1 / (sigma1*sigma1) +
                    1 / (sigma2*sigma2);
                    
                    flux = flux1/(sigma1*sigma1) + flux2/(sigma2*sigma2);
                    flux /= sigma;
                    
                    sigma = sqrt(sigma);
                }
                
                cpl_image_set(result, x, y, flux);
                cpl_image_set(*noise, x, y, sigma);
            }
        }
    }
    
  cleanup:
    if (cpl_error_get_code() != CPL_ERROR_NONE) 
    {
        uves_free_image(&result);
    }
    return result;
}

/*----------------------------------------------------------------------------*/
/**
   @brief    Initialize image header
   @param    ctype1   Value of CTYPE1 keyword
   @param    ctype2   Value of CTYPE2 keyword
   @param    bunit     Value of BUNIT keyword
   @param    crval1    Value of CRVAL1 keyword
   @param    crval2    Value of CRVAL2 keyword
   @param    crpix1    Value of CRPIX1 keyword
   @param    crpix2    Value of CRPIX2 keyword
   @param    cdelt1   Value of CDELT1 keyword
   @param    cdelt2   Value of CDELT2 keyword

   @return   Header containing the specified FITS keywords
*/
/*----------------------------------------------------------------------------*/
uves_propertylist *
uves_initialize_image_header(const char *ctype1, const char *ctype2, 
                             const char *cunit1, const char *cunit2,
                             const char *bunit,const double bscale,
                 double crval1, double crval2,
                 double crpix1, double crpix2,
                 double cdelt1, double cdelt2)
{
    uves_propertylist *header = NULL;  /* Result */

    header = uves_propertylist_new();

    check( uves_pfits_set_ctype1(header, ctype1), "Error writing keyword");
    check( uves_pfits_set_ctype2(header, ctype2), "Error writing keyword");
    check( uves_pfits_set_cunit1(header, cunit1), "Error writing keyword");
    if(cunit2 != NULL) {
      check( uves_pfits_set_cunit2(header, cunit2), "Error writing keyword");
    }
    check( uves_pfits_set_bunit (header, bunit ), "Error writing keyword");
    if(bscale) {
       check( uves_pfits_set_bscale (header, bscale ), "Error writing keyword");
    }
    check( uves_pfits_set_crval1(header, crval1), "Error writing keyword");
    check( uves_pfits_set_crval2(header, crval2), "Error writing keyword");
    check( uves_pfits_set_crpix1(header, crpix1), "Error writing keyword");
    check( uves_pfits_set_crpix2(header, crpix2), "Error writing keyword");
    check( uves_pfits_set_cdelt1(header, cdelt1), "Error writing keyword");
    check( uves_pfits_set_cdelt2(header, cdelt2), "Error writing keyword");
    
  cleanup:
    return header;
}

/*----------------------------------------------------------------------------*/
/**
   @brief    Create noise image
   @param    image              Input image
   @param    image_header       Input image header
   @param    ncom               Number of combined frames
   @param    chip               CCD chip
   @return   The newly allocated noise image, or NULL on error.

   The noise image is calculated as a combination of read-out noise, discretization
   noise and photonic noise.
   
   The noise decreases accordingly if the number of combined frames,
   @em ncom, is more than one. Those frames are assumed to have been median stacked.

   (See source code for the exact error propagation formulas).

*/
/*----------------------------------------------------------------------------*/
cpl_image *
uves_define_noise(const cpl_image *image, 
                  const uves_propertylist *image_header,
                  int ncom, enum uves_chip chip)
{
    /*
          \/  __
           \_(__)_...
    */

    cpl_image *noise = NULL;      /* Result */

    /* cpl_image *in_med = NULL;     Median filtered input image */

    double ron;                   /* Read-out noise in ADU */
    double gain;
    int nx, ny, i;
    double *noise_data;
    const double *image_data;
    bool has_bnoise=false;
    bool has_dnoise=false;
    double bnoise=0;
    double dnoise=0;
    double dtime=0;
    double bnoise2=0;
    double dnoise2=0;
    double exptime=0;
    double exptime2=0;
    double tot_noise2=0;
    double var_bias_dark=0;

    /* Read, check input parameters */
    assure( ncom >= 1, CPL_ERROR_ILLEGAL_INPUT, "Number of combined frames = %d", ncom);
    
    check( ron = uves_pfits_get_ron_adu(image_header, chip),
       "Could not read read-out noise");
    
    check( gain = uves_pfits_get_gain(image_header, chip),
       "Could not read gain factor");
    assure( gain > 0, CPL_ERROR_ILLEGAL_INPUT, "Non-positive gain: %e", gain);

    nx = cpl_image_get_size_x(image);
    ny = cpl_image_get_size_y(image);

    /* For efficiency reasons, use pointers to image data buffers */
    /* The following check is too strict. It can be avoided to solve PIPE-4893
    assure(cpl_image_count_rejected(image) == 0, 
       CPL_ERROR_UNSUPPORTED_MODE, "Input image contains bad pixels");
       */
    assure(cpl_image_get_type(image) == CPL_TYPE_DOUBLE,
       CPL_ERROR_UNSUPPORTED_MODE, 
       "Input image is of type %s. double expected", 
       uves_tostring_cpl_type(cpl_image_get_type(image)));

    noise = cpl_image_new(nx, ny, CPL_TYPE_DOUBLE);
    assure_mem( noise );

    noise_data = cpl_image_get_data_double(noise);

    image_data = cpl_image_get_data_double_const(image);


    if(image_header != NULL) {
       has_bnoise=uves_propertylist_contains(image_header,UVES_BNOISE);
       has_dnoise=uves_propertylist_contains(image_header,UVES_DNOISE);
    }

    if(has_bnoise) {
       bnoise=uves_propertylist_get_double(image_header,UVES_BNOISE);
       bnoise2=bnoise*bnoise;
    }

    if(has_dnoise) {
       dnoise=uves_propertylist_get_double(image_header,UVES_DNOISE);
       dnoise2=dnoise*dnoise;
       dtime=uves_propertylist_get_double(image_header,UVES_DTIME);
       exptime=uves_pfits_get_exptime(image_header);
       exptime2=exptime*exptime/dtime/dtime;
    }
    var_bias_dark=bnoise2+dnoise2*exptime2;
    uves_msg_debug("bnoise=%g dnoise=%g sci exptime=%g dark exptime=%g",
	     bnoise,dnoise,exptime,dtime);

    /* Apply 3x3 median filter to get rid of isolated hot/cold pixels */

    /* This filter is disabled, as there is often structure on the scale
       of 1 pixel (e.g. UVES_ORDER_FLAT frames). Smoothing out this
       structure *does* result in worse fits to the data.

       in_med = cpl_image_duplicate(image);
       assure( in_med != NULL, CPL_ERROR_ILLEGAL_OUTPUT, "Image duplication failed");
       
       uves_msg_low("Applying 3x3 median filter");
       
       check( uves_filter_image_median(&in_med, 1, 1), "Could not filter image");
       image_data = cpl_image_get_data_double(in_med);
       
       uves_msg_low("Setting pixel flux uncertainty");
    */

    /* We assume median stacked input (master flat, master dark, ...) */
    double gain2=gain*gain;
        
    double quant_var = uves_max_double(0, (1 - gain2)/12.0);
    /* Quant. error =
     * sqrt((g^2-1)/12)
     */
    double flux_var_adu=0;
    double ron2=ron*ron;
    /* https://en.wikipedia.org/wiki/Median#The_sample_median */
    double median_factor = (ncom > 2) ? 2.0 / M_PI * (2 * ncom + 3) / (2 * ncom + 1) : 1.0;

    double inv_ncom_median_factor = 1/(ncom*median_factor);
    for (i = 0; i < nx*ny; i++)
    {
         
        /* Slow: flux = cpl_image_get(image, x, y, &pis_rejected); */
        /* Slow: flux = image_data[(x-1) + (y-1) * nx]; */
        flux_var_adu =  uves_max_double(image_data[i],0)*gain;
        
        /* For a number, N, of averaged or median stacked "identical" frames
         * (gaussian distribution assumed), the combined noise is
         *
         *  sigma_N = sigma / sqrt(N*f)
         *
         *  where (to a good approximation)
         *        f ~= { 1    , N = 1
         *             { 2/pi , N > 1
         *
         *  (i.e. the resulting uncertainty is
         *   larger than for average stacked inputs where f = 1)
         */
        
        /* Slow: cpl_image_set(noise, x, y, ... ); */
        /* Slow: noise_data[(x-1) + (y-1)*nx] = 
                 sqrt((ron*ron + quant_var + sigma_adu*sigma_adu) /
              ((MIDAS) ? 1 : ncom * median_factor)); */

        
      tot_noise2=(( ron2 + quant_var + flux_var_adu )*inv_ncom_median_factor)+
         var_bias_dark;

      /*
      tot_noise2=(( ron2 + quant_var + flux_var_adu )*std_err_median_asymptotic_approx);
      */
        noise_data[i] = sqrt(tot_noise2);
    }

  cleanup:
    /* uves_free_image(&in_med); */
    if (cpl_error_get_code() != CPL_ERROR_NONE)
    {
        uves_free_image(&noise);
    }

    return noise;
}


/*----------------------------------------------------------------------------*/
/**
   @brief    Subtract bias
   @param    image                The image to be de-biased
   @param    master_bias          The bias image to subtract
   @return   CPL_ERROR_NONE iff okay

   Negative values are set to zero.

*/
/*----------------------------------------------------------------------------*/
cpl_error_code
uves_subtract_bias(cpl_image *image, const cpl_image *master_bias)
{
    passure ( image != NULL, " ");
    passure ( master_bias != NULL, " ");

    check( cpl_image_subtract(image, master_bias),
       "Error subtracting bias");

    /* Due to different bad column correction in image/master_bias,
       it might happen that the image has become negative after 
       subtracting the bias. Disallow that. */

#if 0
    /* No, for backwards compatibility, allow negative values.
     * MIDAS has an inconsistent logic on this matter.
     * For master dark frames, the thresholding *is* applied,
     * but not for science frames. Therefore we have to
     * apply thresholding on a case-by-case base (i.e. from
     * the caller).
     */
    check( cpl_image_threshold(image, 
                   0, DBL_MAX,     /* Interval */
                   0, DBL_MAX),    /* New values */
       "Error thresholding image");
#endif

  cleanup:
    return cpl_error_get_code();
}
/*----------------------------------------------------------------------------*/
/**
   @brief    Subtract dark
   @param    image                The image to be dark-subtracted
   @param    image_header         The image header
   @param    master_dark          The dark image to subtract
   @param    mdark_header          The master dark header
   @return   CPL_ERROR_NONE iff okay

   Before subtracting the dark frame, it is normalized to the same exposure
   time as the input image. Exposure times are read from the provided headers.

*/
/*----------------------------------------------------------------------------*/
cpl_error_code
uves_subtract_dark(cpl_image *image, const uves_propertylist *image_header,
           const cpl_image *master_dark,
           const uves_propertylist *mdark_header)
{
    cpl_image *normalized_mdark = NULL;
    double image_exptime = 0.0;
    double mdark_exptime = 0.0;

    passure ( image != NULL, " ");
    passure ( image_header != NULL, " ");
    passure ( master_dark != NULL, " ");
    passure ( mdark_header != NULL, " ");

    /* Normalize mdark to same exposure time as input image, then subtract*/
    check( image_exptime = uves_pfits_get_uit(image_header),
       "Error reading input image exposure time");

    check( mdark_exptime = uves_pfits_get_uit(mdark_header),
       "Error reading master dark exposure time");
    
    uves_msg("Rescaling master dark from %f s to %f s exposure time", 
         mdark_exptime, image_exptime);
    
    check( normalized_mdark = 
       cpl_image_multiply_scalar_create(master_dark,
                        image_exptime / mdark_exptime),
       "Error normalizing master dark");
    
    check( cpl_image_subtract(image, normalized_mdark), 
       "Error subtracting master dark");

    uves_msg_warning("noise rescaled master dark %g",cpl_image_get_stdev(normalized_mdark));


  cleanup:
    uves_free_image(&normalized_mdark);
    return cpl_error_get_code();
}

/*----------------------------------------------------------------------------*/
/**
   @brief    Get the absolute order number
   @param    first_abs_order   Absolute order number of row number 1 in the 
                               spectrum image
   @param    last_abs_order    Absolute order number of the highest row in the 
                               spectrum image
   @param    relative_order    Relative order number (row number in spectrum image)
   @return   The absolute order number

   This function converts from row number in a spectrum image to
   absolute (physical) order number.
   
*/
/*----------------------------------------------------------------------------*/
int uves_absolute_order(int first_abs_order, int last_abs_order, int relative_order)
{
    return (first_abs_order +
        (relative_order-1)*((last_abs_order > first_abs_order) ? 1 : -1));
}

/*----------------------------------------------------------------------------*/
/**
  @brief    Get average with iterative rejection
  @param    t           table, rows with outliers are removed
  @param    column      with data values 
  @param    residual2   name of temporary column used to store the squared
                        residuals (this column must not already exist)
  @param    kappa       rejection parameter
  @return   robust mean

  Outliers are rejected using robust estimation. The final average value is
  computed using the arithmetic mean, which has lower error than the median.

**/
/*----------------------------------------------------------------------------*/
double
uves_average_reject(cpl_table *t,
                    const char *column,
                    const char *residual2,
                    double kappa)
{
    double mean = 0, median, sigma2;
    int rejected;
    
    do {
        /* Robust estimation */
      check_nomsg(median = cpl_table_get_column_median(t, column));

        /* Create column
           residual2 = (column - median)^2   */
      check_nomsg(cpl_table_duplicate_column(t, residual2, t, column));
      check_nomsg(cpl_table_subtract_scalar(t, residual2, median));
      check_nomsg(cpl_table_multiply_columns(t, residual2, residual2));

        /* For a Gaussian distribution:
         * sigma    ~= median(|residual|) / 0.6744
         * sigma^2  ~= median(residual^2) / 0.6744^2  
         */

      check_nomsg(sigma2 = cpl_table_get_column_median(t, residual2) / (0.6744 * 0.6744));

        /* Reject values where
           residual^2 > (kappa*sigma)^2
        */
    check_nomsg( rejected = uves_erase_table_rows(t, residual2,
                                                      CPL_GREATER_THAN,
                                                      kappa*kappa*sigma2));
        
    check_nomsg(cpl_table_erase_column(t, residual2));

    } while (rejected > 0);

    check_nomsg(mean  = cpl_table_get_column_mean(t, column));
    
  cleanup:
    return mean;
}

/*----------------------------------------------------------------------------*/
/**
   @brief    Fit a 1d polynomial to two table columns
   @param    t             Table
   @param    X             Name of table column containing independent variable
   @param    Y             Name of table column containing dependent variable
   @param    sigmaY        Uncertainty of dependent variable. 
                           If NULL, constant uncertainties are assumed.
   @param    degree        Degree of polynomial fit
   @param    polynomial_fit Name of column to add
   @param    residual_square Name of column to add
   @param    mean_squared_error Mean squared error of the residuals
   @param    kappa         If positive, the value of kappa used in a kappa sigma-clipping.
   Ignored if negative. 

   @return   Fitted polynomial

   This function fits column @em Y as function of @em X. Both columns must have type
   CPL_TYPE_DOUBLE or CPL_TYPE_INT.

   If non-NULL the columns specified by the parameters @em polynomial_fit 
   and @em residual_square are added to the table (containing the fitted value
   and the squared residual for each point). If any of these columns already exist,
   an error is set.

   If @em kappa is positive, a kappa-sigma clipping is performed (iteratively, 
   until there are no points with residuals worse than kappa*sigma). The
   rejected points (rows) are physically removed from the table.

   Note that rows with invalid values are not handled properly (the garbage values
   are used for the fitting). Therefore the input table should not have invalid
   rows.
*/
/*----------------------------------------------------------------------------*/
polynomial *
uves_polynomial_regression_1d(cpl_table *t,
                  const char *X, const char *Y, const char *sigmaY, 
                  int degree, 
                  const char *polynomial_fit, const char *residual_square,
                  double *mean_squared_error, double kappa)
{
    int N;
    int total_rejected = 0;  /* Rejected in kappa sigma clipping */
    int rejected = 0;
    double mse;                  /* local mean squared error */
    double *x;
    double *y;
    double *sy;
    polynomial *result = NULL;
    cpl_vector *vx = NULL;
    cpl_vector *vy = NULL;
    cpl_vector *vsy = NULL;
    cpl_type type;

    /* Check input */
    assure( t != NULL, CPL_ERROR_NULL_INPUT, "Null table");
    assure( X != NULL, CPL_ERROR_NULL_INPUT, "Null column name");
    assure( Y != NULL, CPL_ERROR_NULL_INPUT, "Null column name");
    assure( cpl_table_has_column(t, X), CPL_ERROR_ILLEGAL_INPUT, "No such column: %s", X);
    assure( cpl_table_has_column(t, Y), CPL_ERROR_ILLEGAL_INPUT, "No such column: %s", Y);
    assure( sigmaY == NULL || cpl_table_has_column(t, sigmaY) , CPL_ERROR_ILLEGAL_INPUT,
        "No such column: %s", sigmaY);

    assure( polynomial_fit == NULL || !cpl_table_has_column(t, polynomial_fit),
        CPL_ERROR_ILLEGAL_INPUT, "Column '%s' already present", polynomial_fit);

    assure( residual_square == NULL || !cpl_table_has_column(t, residual_square), 
        CPL_ERROR_ILLEGAL_INPUT, "Column '%s' already present", residual_square);
    
    /* Check column types */
    type = cpl_table_get_column_type(t, Y);
    assure( type == CPL_TYPE_DOUBLE || type == CPL_TYPE_INT, CPL_ERROR_INVALID_TYPE, 
        "Input column '%s' has wrong type (%s)", Y, uves_tostring_cpl_type(type));
    type = cpl_table_get_column_type(t, X);
    assure( type == CPL_TYPE_DOUBLE || type == CPL_TYPE_INT, CPL_ERROR_INVALID_TYPE,
        "Input column '%s' has wrong type (%s)", X, uves_tostring_cpl_type(type));
    if (sigmaY != NULL)
    {
        type = cpl_table_get_column_type(t, sigmaY);
        assure( type == CPL_TYPE_INT || type == CPL_TYPE_DOUBLE,
            CPL_ERROR_INVALID_TYPE, 
            "Input column '%s' has wrong type (%s)", 
            sigmaY, uves_tostring_cpl_type(type));
    }

    check( cpl_table_cast_column(t, X, "_X_double", CPL_TYPE_DOUBLE),
       "Could not cast table column '%s' to double", X);
    check( cpl_table_cast_column(t, Y, "_Y_double", CPL_TYPE_DOUBLE),
       "Could not cast table column '%s' to double", Y);
    if (sigmaY != NULL)
    {
        check( cpl_table_cast_column(t, sigmaY, "_sY_double", CPL_TYPE_DOUBLE), 
           "Could not cast table column '%s' to double", sigmaY);
    } 
    

    total_rejected = 0;
    rejected = 0;
    check( cpl_table_new_column(t, "_residual_square", CPL_TYPE_DOUBLE), 
       "Could not create column");
    do{
    check( (N = cpl_table_get_nrow(t),
        x = cpl_table_get_data_double(t, "_X_double"),
        y = cpl_table_get_data_double(t, "_Y_double")),
           "Could not read table data");
    
    if (sigmaY != NULL) 
        {
        check( sy = cpl_table_get_data_double(t,  "_sY_double"),
               "Could not read table data");
        } 
    else 
        {
        sy = NULL;
        }
  
    assure( N > 0, CPL_ERROR_ILLEGAL_INPUT, "Empty table. "
            "No points to fit in poly 1d regression. At least 2 needed");

    assure( N > degree, CPL_ERROR_ILLEGAL_INPUT, "%d points to fit in poly 1d "
           "regression of degree %d. At least %d needed.",
            N,degree,degree+1);

    /* Wrap vectors */
    uves_unwrap_vector(&vx);
    uves_unwrap_vector(&vy);
    
    vx = cpl_vector_wrap(N, x);
    vy = cpl_vector_wrap(N, y);
       
    if (sy != NULL)
        {
        uves_unwrap_vector(&vsy);
        vsy = cpl_vector_wrap(N, sy);
        }
    else
        {
        vsy = NULL;
        }
     
    /* Fit! */
    uves_polynomial_delete(&result);
    check( result = uves_polynomial_fit_1d(vx, vy, vsy, degree, &mse), 
           "Could not fit polynomial");
    
    /* If requested, calculate residuals and perform kappa-sigma clipping */
    if (kappa > 0)
        {
        double sigma2;   /* sigma squared */
        int i;
        
        for (i = 0; i < N; i++)
            {
            double xval, yval, yfit;
            
            check(( xval = cpl_table_get_double(t, "_X_double", i, NULL),
                yval = cpl_table_get_double(t, "_Y_double" ,i, NULL),
                yfit = uves_polynomial_evaluate_1d(result, xval),
    
                cpl_table_set_double(t, "_residual_square", i, 
                             (yfit-yval)*(yfit-yval))),
                "Could not evaluate polynomial");
            }
        
        /* For robustness, estimate sigma as (third quartile) / 0.6744
         * (68% is within 1 sigma, 50% is within 3rd quartile, so sigma is > 3rd quartile)
         * The third quartile is estimated as the median of the absolute residuals,
         * so  sigma    ~= median(|residual|) / 0.6744  , i.e.
         *     sigma^2  ~= median(residual^2) / 0.6744^2  
         */
        sigma2 = cpl_table_get_column_median(t, "_residual_square") / (0.6744 * 0.6744);

        /* Remove points with residual^2 > kappa^2 * sigma^2 */
        check( rejected = uves_erase_table_rows(t, "_residual_square", 
                            CPL_GREATER_THAN, kappa*kappa*sigma2),
               "Could not remove outlier points");
        
        uves_msg_debug("%d of %d points rejected in kappa-sigma clipping. rms=%f",
                   rejected, N, sqrt(mse));
        
        /* Update */
        total_rejected += rejected;
        N = cpl_table_get_nrow(t);
        }
    
} while (rejected > 0);
    
    cpl_table_erase_column(t,  "_residual_square");    
    
    if (kappa > 0)
    {    
        uves_msg_debug("%d of %d points (%f %%) rejected in kappa-sigma clipping",
              total_rejected,
              N + total_rejected,
              (100.0*total_rejected)/(N + total_rejected)
        );
    }
    
    if (mean_squared_error != NULL) *mean_squared_error = mse;
    
    /* Add the fitted values to table if requested */
    if (polynomial_fit != NULL || residual_square != NULL)
    {
        int i;
        
        check( cpl_table_new_column(t, "_polynomial_fit", CPL_TYPE_DOUBLE), 
           "Could not create column");
        for (i = 0; i < N; i++){
        double xval;
        double yfit;
        
        check((
              xval = cpl_table_get_double(t, "_X_double", i, NULL),
              yfit = uves_polynomial_evaluate_1d(result, xval),
              cpl_table_set_double(t, "_polynomial_fit", i, yfit)),
              "Could not evaluate polynomial");
        }
        
        /* Add residual^2  =  (Polynomial fit  -  Y)^2  if requested */
        if (residual_square != NULL)
        {
            check(( cpl_table_duplicate_column(t, residual_square,     /* RS := PF */
                               t, "_polynomial_fit"),
                cpl_table_subtract_columns(t, residual_square, Y), /* RS := RS - Y */
                cpl_table_multiply_columns(t, residual_square, residual_square)),
                                                                               /* RS := RS^2 */
                "Could not calculate Residual of fit");
        }
        
        /* Keep the polynomial_fit column if requested */
        if (polynomial_fit != NULL)
        {
            cpl_table_name_column(t, "_polynomial_fit", polynomial_fit);
        }
        else
        {
            cpl_table_erase_column(t, "_polynomial_fit");
        }
    }
    
    check(( cpl_table_erase_column(t, "_X_double"),
        cpl_table_erase_column(t, "_Y_double")),
      "Could not delete temporary columns");
    
    if (sigmaY != NULL) 
    {
        check( cpl_table_erase_column(t, "_sY_double"), 
           "Could not delete temporary column");
    } 
    
  cleanup:
    uves_unwrap_vector(&vx);
    uves_unwrap_vector(&vy);
    uves_unwrap_vector(&vsy);
    if (cpl_error_get_code() != CPL_ERROR_NONE)
    {
        uves_polynomial_delete(&result);
    }
    
    return result;
}


/*----------------------------------------------------------------------------*/
/**
   @brief    Fit a 2d polynomial to three table columns
   @param    t             The table
   @param    X1             Name of table column containing 1st independent variable
   @param    X2             Name of table column containing 2nd independent variable
   @param    Y              Name of table column containing dependent variable
   @param    sigmaY         Uncertainty of dependent variable. If NULL, constant 
                            uncertainties are assumed.
   @param    degree1        Degree of polynomial fit (1st variable)
   @param    degree2        Degree of polynomial fit (2nd variable)
   @param    polynomial_fit  If non-NULL, name of column to add. The fitted value.
   @param    residual_square If non-NULL, name of column to add. The squared residual of the fit.
   @param    variance_fit    If non-NULL, name of column to add. Variance of the fitted value.
   @param    mean_squared_error (out) Mean squared error of the residuals. May be NULL.
   @param    red_chisq          (out) Reduced chi square of the fit. May be NULL.
   @param    variance        (out) Variance of the fit-polynomial (which is in itself a
   polynomial; see also @c uves_polynomial_fit_2d() ). May be NULL.
   @param    kappa           If positive, the value of kappa used in a kappa sigma-clipping.
                             Ignored if negative. 
   @param    min_reject      Minimum number of outliers worth rejecting. Stop iterating (for
                             efficiency) if less than this relative number of outliers
                             (e.g. 0.001) are detected. Negative to disable
   @return   Fitted polynomial

   This function fits column @em Y (must be of type CPL_TYPE_DOUBLE) as function 
   of @em X1 (CPL_TYPE_DOUBLE or CPL_TYPE_INT) and @em X2 (CPL_TYPE_DOUBLE or CPL_TYPE_INT).
   The column @em sigmaY contains the Y-uncertainties. If NULL, constant uncertainty 
   equal to 1 is assumed.

   If non-NULL the columns specified by the parameters @em polynomial_fit, @em residual_square
   and @em variance_fit are added to the table (containing the fitted value, the squared
   residual and the variance of the fitted value, for each point).
   
   If non-NULL, the @em mean_squared_error and @em red_chisq (reduced chi square) are
   calculated.

   If non-NULL the parameter @em variance will contain the polynomial that defines
   the variance of the fit (i.e. as function of @em x1 and @em x2 ).

   To calculate variances or reduced chi square, the parameter @em sigmaY must be non-NULL.

   If @em kappa is positive, a kappa-sigma clipping is performed (iteratively, until there
   are no points with residuals worse than kappa*sigma). The rejected points (rows) 
   are physically removed from the table.

   Also see @c uves_polynomial_regression_1d() .
*/
/*----------------------------------------------------------------------------*/

polynomial *
uves_polynomial_regression_2d(cpl_table *t,
                  const char *X1, const char *X2, const char *Y, 
                  const char *sigmaY,
                  int degree1, int degree2,
                  const char *polynomial_fit, const char *residual_square, 
                  const char *variance_fit,
                  double *mse, double *red_chisq,
                  polynomial **variance, double kappa,
                              double min_reject)
{
    int N;
    int rejected;
    int total_rejected;
    double *x1;
    double *x2;
    double *y;
    double *res;
    double *sy;
    polynomial *p = NULL;               /* Result */
    polynomial *variance_local = NULL;
    cpl_vector *vx1 = NULL;
    cpl_vector *vx2 = NULL;
    cpl_bivector *vx = NULL;
    cpl_vector *vy = NULL;
    cpl_vector *vsy= NULL;
    cpl_type type;

    /* Check input */
    assure( t != NULL, CPL_ERROR_NULL_INPUT, "Null table");
    N  = cpl_table_get_nrow(t);
    assure( N > 0, CPL_ERROR_ILLEGAL_INPUT, "The table with column to compute regression has 0 rows!");
    assure( N > 8, CPL_ERROR_ILLEGAL_INPUT, "For poly regression you need at least 9 points. The table with column to compute regression has %d rows!",N);

    assure( cpl_table_has_column(t, X1), CPL_ERROR_ILLEGAL_INPUT, "No such column: %s", X1);
    assure( cpl_table_has_column(t, X2), CPL_ERROR_ILLEGAL_INPUT, "No such column: %s", X2);
    assure( cpl_table_has_column(t, Y) , CPL_ERROR_ILLEGAL_INPUT, "No such column: %s", Y);
    assure( (variance == NULL && variance_fit == NULL) || sigmaY != NULL,
        CPL_ERROR_INCOMPATIBLE_INPUT, "Cannot calculate variances without sigmaY");
    if (sigmaY != NULL)
    {
        assure( cpl_table_has_column(t, sigmaY) , CPL_ERROR_ILLEGAL_INPUT, 
            "No such column: %s", sigmaY);
    }
    if (polynomial_fit != NULL)
    {
        assure( !cpl_table_has_column(t, polynomial_fit) , CPL_ERROR_ILLEGAL_INPUT,
            "Table already has '%s' column", polynomial_fit);
    }
    if (residual_square != NULL)
    {
        assure( !cpl_table_has_column(t, residual_square), CPL_ERROR_ILLEGAL_INPUT, 
            "Table already has '%s' column", residual_square);
    }
    if (variance_fit != NULL)
    {
        assure( !cpl_table_has_column(t, variance_fit) , CPL_ERROR_ILLEGAL_INPUT,
            "Table already has '%s' column", variance_fit);
    }

    /* Check column types */
    type = cpl_table_get_column_type(t, X1);
    assure( type == CPL_TYPE_INT || type == CPL_TYPE_DOUBLE, CPL_ERROR_INVALID_TYPE,
        "Input column '%s' has wrong type (%s)", X1, uves_tostring_cpl_type(type));
    type = cpl_table_get_column_type(t, X2);
    assure( type == CPL_TYPE_INT || type == CPL_TYPE_DOUBLE, CPL_ERROR_INVALID_TYPE,
        "Input column '%s' has wrong type (%s)", X2, uves_tostring_cpl_type(type));
    type = cpl_table_get_column_type(t, Y);
    assure( type == CPL_TYPE_INT || type == CPL_TYPE_DOUBLE, CPL_ERROR_INVALID_TYPE,
        "Input column '%s' has wrong type (%s)", Y, uves_tostring_cpl_type(type));
    if (sigmaY != NULL)
    {
        type = cpl_table_get_column_type(t, sigmaY);
        assure( type == CPL_TYPE_INT || type == CPL_TYPE_DOUBLE, CPL_ERROR_INVALID_TYPE,
            "Input column '%s' has wrong type (%s)", 
            sigmaY, uves_tostring_cpl_type(type));
    }

    /* In the case that these temporary columns already exist, a run-time error will occur */
    check( cpl_table_cast_column(t, X1    , "_X1_double", CPL_TYPE_DOUBLE), 
       "Could not cast table column to double");
    check( cpl_table_cast_column(t, X2    , "_X2_double", CPL_TYPE_DOUBLE),
       "Could not cast table column to double");
    check( cpl_table_cast_column(t,  Y    ,  "_Y_double", CPL_TYPE_DOUBLE), 
       "Could not cast table column to double");
    if (sigmaY != NULL)
    {
        check( cpl_table_cast_column(t, sigmaY, "_sY_double", CPL_TYPE_DOUBLE), 
           "Could not cast table column to double");
    }
    
    total_rejected = 0;
    rejected = 0;
    check( cpl_table_new_column(t, "_residual_square", CPL_TYPE_DOUBLE), 
       "Could not create column");

    do {
        /* WARNING!!! Code duplication (see below). Be careful
           when updating */
    check(( N  = cpl_table_get_nrow(t),
        x1 = cpl_table_get_data_double(t, "_X1_double"),
        x2 = cpl_table_get_data_double(t, "_X2_double"),
        y  = cpl_table_get_data_double(t, "_Y_double"),
                res= cpl_table_get_data_double(t, "_residual_square")),
          "Could not read table data");
    
    if (sigmaY != NULL) 
        {
        check (sy = cpl_table_get_data_double(t,  "_sY_double"),
               "Could not read table data");
        }
    else 
        {
        sy = NULL;
        }

    assure( N > 0, CPL_ERROR_ILLEGAL_INPUT, "Empty table");
    
    /* Wrap vectors */
    uves_unwrap_vector(&vx1);
    uves_unwrap_vector(&vx2);
    uves_unwrap_vector(&vy);

    vx1 = cpl_vector_wrap(N, x1);
    vx2 = cpl_vector_wrap(N, x2);
    vy  = cpl_vector_wrap(N, y);
    if (sy != NULL)
        {
        uves_unwrap_vector(&vsy);
        vsy = cpl_vector_wrap(N, sy);
        }
    else
        {
        vsy = NULL;
        }
    
    /* Wrap up the bi-vector */
    uves_unwrap_bivector_vectors(&vx);
    vx = cpl_bivector_wrap_vectors(vx1, vx2);
  
    /* Fit! */
    uves_polynomial_delete(&p);
        check( p =  uves_polynomial_fit_2d(vx, vy, vsy, degree1, degree2, 
                                           NULL, NULL, NULL),
               "Could not fit polynomial");

    /* If requested, calculate residuals and perform kappa-sigma clipping */
    if (kappa > 0)
        {
        double sigma2;   /* sigma squared */
        int i;

                cpl_table_fill_column_window_double(t, "_residual_square", 0, 
                                                    cpl_table_get_nrow(t), 0.0);

        for (i = 0; i < N; i++)
            {
                        double yval, yfit;

                        yval  = y[i];
                        yfit  = uves_polynomial_evaluate_2d(p, x1[i], x2[i]);
                        res[i] = (yfit-y[i])*(yfit-y[i]);
            }
        
        /* For robustness, estimate sigma as (third quartile) / 0.6744
         * (68% is within 1 sigma, 50% is within 3rd quartile, so sigma is > 3rd quartile)
         * The third quartile is estimated as the median of the absolute residuals,
         * so  sigma    ~= median(|residual|) / 0.6744  , i.e.
         *     sigma^2  ~= median(residual^2) / 0.6744^2  
         */
        sigma2 = cpl_table_get_column_median(t, "_residual_square") / (0.6744 * 0.6744);
                             

        /* Remove points with residual^2 > kappa^2 * sigma^2 */
        check( rejected = uves_erase_table_rows(t, "_residual_square", 
                            CPL_GREATER_THAN, kappa*kappa*sigma2),
               "Could not remove outlier points");
        /* Note! All pointers to table data are now invalid! */


        uves_msg_debug("%d of %d points rejected in kappa-sigma clipping. rms=%f", 
                   rejected, N, sqrt(sigma2));
        
        /* Update */
        total_rejected += rejected;
        N = cpl_table_get_nrow(t);
        }
        
    /* Stop also if there are too few points left to make the fit.
     * Needed number of points = (degree1+1)(degree2+1) coefficients
     *      plus one extra point for chi^2 computation.   */
    } while (rejected > 0 && rejected > min_reject*(N+rejected) &&
             N >= (degree1 + 1)*(degree2 + 1) + 1);
    
    if (kappa > 0)
    {    
        uves_msg_debug("%d of %d points (%f %%) rejected in kappa-sigma clipping",
                total_rejected,
                N + total_rejected,
                (100.0*total_rejected)/(N + total_rejected)
        );
    }
       
    /* Final fit */
    {
        /* Need to convert to vector again. */

        /* WARNING!!! Code duplication (see above). Be careful
           when updating */
    check(( N  = cpl_table_get_nrow(t),
        x1 = cpl_table_get_data_double(t, "_X1_double"),
        x2 = cpl_table_get_data_double(t, "_X2_double"),
        y  = cpl_table_get_data_double(t, "_Y_double"),
                res= cpl_table_get_data_double(t, "_residual_square")),
          "Could not read table data");
    
    if (sigmaY != NULL) 
        {
        check (sy = cpl_table_get_data_double(t,  "_sY_double"),
               "Could not read table data");
        }
    else 
        {
        sy = NULL;
        }

    assure( N > 0, CPL_ERROR_ILLEGAL_INPUT, "Empty table");
    
    /* Wrap vectors */
    uves_unwrap_vector(&vx1);
    uves_unwrap_vector(&vx2);
    uves_unwrap_vector(&vy);

    vx1 = cpl_vector_wrap(N, x1);
    vx2 = cpl_vector_wrap(N, x2);
    vy  = cpl_vector_wrap(N, y);
    if (sy != NULL)
        {
        uves_unwrap_vector(&vsy);
        vsy = cpl_vector_wrap(N, sy);
        }
    else
        {
        vsy = NULL;
        }
    
    /* Wrap up the bi-vector */
    uves_unwrap_bivector_vectors(&vx);
    vx = cpl_bivector_wrap_vectors(vx1, vx2);
    }

    uves_polynomial_delete(&p);
    if (variance_fit != NULL || variance != NULL)
        {
            /* If requested, also compute variance */
            check( p = uves_polynomial_fit_2d(vx, vy, vsy, degree1, degree2,
                                              mse, red_chisq, &variance_local),
                   "Could not fit polynomial");
        }
    else
        {
            check( p = uves_polynomial_fit_2d(vx, vy, vsy, degree1, degree2, 
                                              mse, red_chisq, NULL),
                   "Could not fit polynomial");
        }

    cpl_table_erase_column(t,  "_residual_square");
    
    /* Add the fitted values to table as requested */
    if (polynomial_fit != NULL || residual_square != NULL)
    {
        int i;
            double *pf;
        
        check( cpl_table_new_column(t, "_polynomial_fit", CPL_TYPE_DOUBLE), 
           "Could not create column");

            cpl_table_fill_column_window_double(t, "_polynomial_fit", 0, 
                                                cpl_table_get_nrow(t), 0.0);

            x1 = cpl_table_get_data_double(t, "_X1_double");
            x2 = cpl_table_get_data_double(t, "_X2_double");
            pf = cpl_table_get_data_double(t, "_polynomial_fit");

        for (i = 0; i < N; i++){
#if 0        
        double x1val, x2val, yfit;

        check(( x1val = cpl_table_get_double(t, "_X1_double", i, NULL),
            x2val = cpl_table_get_double(t, "_X2_double", i, NULL),
            yfit  = uves_polynomial_evaluate_2d(p, x1val, x2val),
            
            cpl_table_set_double(t, "_polynomial_fit", i, yfit)),
            "Could not evaluate polynomial");

#else
                pf[i] = uves_polynomial_evaluate_2d(p, x1[i], x2[i]);
#endif
        }
        
        /* Add residual^2  =  (Polynomial fit  -  Y)^2  if requested */
        if (residual_square != NULL)
        {
            check(( cpl_table_duplicate_column(t, residual_square,     /* RS := PF */
                               t, "_polynomial_fit"),
                cpl_table_subtract_columns(t, residual_square, Y), /* RS := RS - Y */
                cpl_table_multiply_columns(t, residual_square, residual_square)),
                                                                   /* RS := RS^2 */
               "Could not calculate Residual of fit");
        }
        
        /* Keep the polynomial_fit column if requested */
        if (polynomial_fit != NULL)
        {
            cpl_table_name_column(t, "_polynomial_fit", polynomial_fit);
        }
        else
        {
            cpl_table_erase_column(t, "_polynomial_fit");
        }
    }
    
    /* Add variance of poly_fit if requested */
    if (variance_fit != NULL)
    {
        int i;
            double *vf;

        check( cpl_table_new_column(t, variance_fit, CPL_TYPE_DOUBLE), 
           "Could not create column");
            
            cpl_table_fill_column_window_double(t, variance_fit, 0,
                                                cpl_table_get_nrow(t), 0.0);

            x1 = cpl_table_get_data_double(t, "_X1_double");
            x2 = cpl_table_get_data_double(t, "_X2_double");
            vf = cpl_table_get_data_double(t, variance_fit);

        for (i = 0; i < N; i++)
        {
#if 0
            double x1val, x2val, yfit_variance;
            check(( x1val         = cpl_table_get_double(t, "_X1_double", i, NULL),
                x2val         = cpl_table_get_double(t, "_X2_double", i, NULL),
                yfit_variance = uves_polynomial_evaluate_2d(variance_local, 
                                    x1val, x2val),
                
                cpl_table_set_double(t, variance_fit, i, yfit_variance)),
               "Could not evaluate polynomial");
#else
                    vf[i] = uves_polynomial_evaluate_2d(variance_local, x1[i], x2[i]);
#endif

        }
    }
    
    
    check(( cpl_table_erase_column(t, "_X1_double"),
        cpl_table_erase_column(t, "_X2_double"),
        cpl_table_erase_column(t,  "_Y_double")),
      "Could not delete temporary columns");
      
    if (sigmaY != NULL) 
    {
        check( cpl_table_erase_column(t, "_sY_double"),
           "Could not delete temporary column");
    }
    
  cleanup:
    uves_unwrap_bivector_vectors(&vx);
    uves_unwrap_vector(&vx1);
    uves_unwrap_vector(&vx2);
    uves_unwrap_vector(&vy);
    uves_unwrap_vector(&vsy);
    /* Delete 'variance_local', or return through 'variance' parameter */
    if (variance != NULL)
    {
        *variance = variance_local;
    }
    else
    {
        uves_polynomial_delete(&variance_local);
    }
    if (cpl_error_get_code() != CPL_ERROR_NONE)
    {
        uves_polynomial_delete(&p);
    }

    return p;
}

/*----------------------------------------------------------------------------*/
/**
   @brief    Fit a 2d polynomial to three table columns
   @param    t              The table
   @param    X1             Name of table column containing 1st independent variable
   @param    X2             Name of table column containing 2nd independent variable
   @param    Y              Name of table column containing dependent variable
   @param    sigmaY         Uncertainty of dependent variable. If NULL, constant
                            uncertainties are assumed.
   @param    polynomial_fit  If non-NULL, name of column to add. The fitted value.
   @param    residual_square If non-NULL, name of column to add. The squared residual of the fit.
   @param    variance_fit    If non-NULL, name of column to add. Variance of the fitted value.
   @param    mean_squared_error (out) Mean squared error of the residuals. May be NULL.
   @param    red_chisq          (out) Reduced chi square of the fit. May be NULL.
   @param    variance        (out) Variance of the fit-polynomial (which is in itself a
                             polynomial; see also @c uves_polynomial_fit_2d() ). May be NULL.
   @param    kappa           If positive, the value of kappa used in a kappa sigma-clipping.
                             Ignored if negative. 
   @param    maxdeg1         Maximum degree of 1st independent variable
   @param    maxdeg2         Maximum degree of 2nd independent variable
   @param    min_rms         Minimum RMS to aim for. Stop iterating (for efficiency) if
                             this precision is achieved. Set to negative to disable.
   @param    min_reject      Minimum number of outliers worth rejecting. Stop iterating (for
                             efficiency) if less than this relative number of outliers
                             (e.g. 0.001) are detected. Negative to disable
   @param    verbose         print messages at info level (true) or debug level (false)
   @param    min_val         minimum allowed value. A fit producing a value lower
                             than this number will be rejected. Set to NULL to disable
   @param    max_val         maximum allowed value
   @param    npos            size of positions array
   @param    positions       positions where the fitted solution must be inside the
                             limits given by min_val / max_val (for the solution
                 to be accepted)
   @return   Fitted polynomial

   This function is like @c uves_polynomial_regression_2d() except it automatically
   selects the best polynomial degrees based on the RMS of the fit.

   @note that when kappa-sigma clipping is involved, it becomes rather
   complicated to decide which are the optimal degrees, and for that
   reason this function is not expected to give good results for all
   possible kinds of input data.
*/
/*----------------------------------------------------------------------------*/

polynomial *
uves_polynomial_regression_2d_autodegree(cpl_table *t,
                     const char *X1, const char *X2, const char *Y,
                     const char *sigmaY,
                     const char *polynomial_fit,
                     const char *residual_square, 
                     const char *variance_fit,
                     double *mean_squared_error, double *red_chisq,
                     polynomial **variance, double kappa,
                     int maxdeg1, int maxdeg2, double min_rms,
                                         double min_reject,
                                         bool verbose,
                     const double *min_val,
                     const double *max_val,
                     int npos, double positions[][2])
{
    int deg1 = 0;               /* Current degrees                                  */
    int deg2 = 0;               /* Current degrees                                  */
    int i;

    double **mse = NULL;
    bool adjust1 = true;      /* Flags indicating if DEFPOL1/DEFPOL2 should be adjusted */
    bool adjust2 = true;      /*   (or held constant)            */
    bool finished = false;

    const char *y_unit;
    cpl_table *temp = NULL;
    polynomial *bivariate_fit = NULL;   /* Result */

    assure( (min_val == NULL && max_val == NULL) || positions != NULL,
        CPL_ERROR_NULL_INPUT,
        "Missing positions array");    

    check_nomsg( y_unit = cpl_table_get_column_unit(t, Y));
    if (y_unit == NULL)
    {
        y_unit = "";
    }

    assure(maxdeg1 >= 1 && maxdeg2 >= 1, CPL_ERROR_ILLEGAL_INPUT, 
       "Illegal max. degrees: (%d, %d)",
       maxdeg1, maxdeg2);

    mse = cpl_calloc(maxdeg1+1, sizeof(double *));
    assure_mem(mse);
    for (i = 0; i < maxdeg1+1; i++)
    {
        int j;
        mse[i] = cpl_calloc(maxdeg2+1, sizeof(double));
        assure_mem(mse);

        for (j = 0; j < maxdeg2+1; j++)
        {
            mse[i][j] = -1;
        }
    }

    temp = cpl_table_duplicate(t);
    assure_mem(temp);

    uves_polynomial_delete(&bivariate_fit);
    check( bivariate_fit = uves_polynomial_regression_2d(temp,
                             X1, X2, Y, sigmaY,
                             deg1,
                             deg2,
                             NULL, NULL, NULL,  /* new columns  */
                             &mse[deg1][deg2], NULL, /* chi^2/N */
                             NULL,              /* variance pol.*/
                             kappa, min_reject),
       "Error fitting polynomial");
    if (verbose)
        uves_msg_low("(%d, %d)-degree: RMS = %.3g %s (%" CPL_SIZE_FORMAT "/%" CPL_SIZE_FORMAT " outliers)",
                     deg1, deg2, sqrt(mse[deg1][deg2]), y_unit,
                     cpl_table_get_nrow(t) - cpl_table_get_nrow(temp),
                     cpl_table_get_nrow(t));
    else
        uves_msg_debug("(%d, %d)-degree: RMS = %.3g %s (%" CPL_SIZE_FORMAT "/%" CPL_SIZE_FORMAT " outliers)",
                       deg1, deg2, sqrt(mse[deg1][deg2]), y_unit,
                     cpl_table_get_nrow(t) - cpl_table_get_nrow(temp),
                     cpl_table_get_nrow(t));
    /* Find best values of deg1, deg2 less than or equal to 8,8
       (the fitting algorithm is unstable after this point, anyway) */
    do
    {
        int new_deg1, new_deg2;
        double m;

        finished = true;

        adjust1 = adjust1 && (deg1 + 2 <= maxdeg1);
        adjust2 = adjust2 && (deg2 + 2 <= maxdeg2);
        
        /* Try the new degrees

                              (d1+1, d2  ) (d1+2, d2)
                       (d1, d2+1) (d1+1, d2+1)
                       (d1, d2+2)

           in the following order:

                                     1            3
                          1          2
                          3

               (i.e. only move to '3' if positions '1' and '2' were no better, etc.)
        */
        for (new_deg1 = deg1; new_deg1 <= deg1+2; new_deg1++)
        for (new_deg2 = deg2; new_deg2 <= deg2+2; new_deg2++)
            if ( (
                 (new_deg1 == deg1+1 && new_deg2 == deg2   && adjust1) ||
                 (new_deg1 == deg1+2 && new_deg2 == deg2   && adjust1) ||
                 (new_deg1 == deg1   && new_deg2 == deg2+1 && adjust2) ||
                 (new_deg1 == deg1   && new_deg2 == deg2+2 && adjust2) ||
                 (new_deg1 == deg1+1 && new_deg2 == deg2+1 && adjust1 && adjust2)
                 )
             && mse[new_deg1][new_deg2] < 0)
            {
                int rejected = 0;

                uves_free_table(&temp);
                temp = cpl_table_duplicate(t);
                assure_mem(temp);

                uves_polynomial_delete(&bivariate_fit);
                bivariate_fit = uves_polynomial_regression_2d(temp,
                                      X1, X2, Y, sigmaY,
                                      new_deg1,
                                      new_deg2,
                                      NULL, NULL, NULL,
                                      &(mse[new_deg1]
                                        [new_deg2]),
                                      NULL,
                                      NULL,
                                      kappa, min_reject);

                if (cpl_error_get_code() == CPL_ERROR_SINGULAR_MATRIX)
                {
                    uves_error_reset();

                                    if (verbose)
                                        uves_msg_low("(%d, %d)-degree: Singular matrix", 
                         new_deg1, new_deg2);
                                    else
                                        uves_msg_debug("(%d, %d)-degree: Singular matrix", 
                         new_deg1, new_deg2);
                    
                    mse[new_deg1][new_deg2] = DBL_MAX/2; 
                }
                else
                {
                    assure( cpl_error_get_code() == CPL_ERROR_NONE,
                        cpl_error_get_code(),
                        "Error fitting (%d, %d)-degree polynomial", 
                        new_deg1, new_deg2 );
                    
                    rejected = cpl_table_get_nrow(t) - cpl_table_get_nrow(temp);
                
                                    if (verbose)
                                        uves_msg_low("(%d,%d)-degree: RMS = %.3g %s (%d/%" CPL_SIZE_FORMAT " outliers)",
                                                     new_deg1, new_deg2, sqrt(mse[new_deg1][new_deg2]), y_unit,
                                                     rejected, cpl_table_get_nrow(t));
                                    else
                                        uves_msg_debug("(%d,%d)-degree: RMS = %.3g %s (%d/%" CPL_SIZE_FORMAT " outliers)",
                                                     new_deg1, new_deg2, sqrt(mse[new_deg1][new_deg2]), y_unit,
                                                     rejected, cpl_table_get_nrow(t));

                    /* Reject if fit produced bad values */
                    if (min_val != NULL || max_val != NULL)
                    {
                        for (i = 0; i < npos; i++)
                        {
                            double val = uves_polynomial_evaluate_2d(
                            bivariate_fit,
                            positions[i][0], positions[i][1]);
                            if (min_val != NULL && val < *min_val)
                            {
                                uves_msg_debug("Bad fit: %f < %f", 
                                       val,
                                       *min_val);
                                mse[new_deg1][new_deg2] = DBL_MAX/2; 
                                /* A large number, even if we add a bit */
                            }
                            if (max_val != NULL && val > *max_val)
                            {
                                uves_msg_debug("Bad fit: %f > %f", 
                                       val,
                                       *max_val);
                                mse[new_deg1][new_deg2] = DBL_MAX/2; 
                            }
                        }
                    }
                
                    /* For robustness, make sure that we don't accept a solution that
                       rejected too many points (say, 80%)
                    */
                    if (rejected >= (4*cpl_table_get_nrow(t))/5)
                    {
                        mse[new_deg1][new_deg2] = DBL_MAX/2; 
                    }
                    
                }/* if fit succeeded */
            }
        
        /* If fit is significantly better (say, 10% improvement in MSE) in either direction, 
         * (in (degree,degree)-space) then move in that direction.
         *
         * First try to move one step horizontal/vertical, 
         * otherwise try to move diagonally (i.e. increase both degrees),
         * otherwise move two steps horizontal/vertical
         *
         */
        m = mse[deg1][deg2];

        if      (adjust1                              
             && (m - mse[deg1+1][deg2])/m > 0.1
             && (!adjust2 || mse[deg1+1][deg2] <= mse[deg1][deg2+1])
             /* The condition is read like this:
            if 
            - we are trying to move right, and
            - this is this is a better place than the current, and
                - this is better than moving down */
        )
        {
            deg1++;
            finished = false;
        }
        else if (adjust2 &&
             (m - mse[deg1][deg2+1])/m > 0.1
             && (!adjust1 || mse[deg1+1][deg2] > mse[deg1][deg2+1])
        )
        {
            deg2++;
            finished = false;
        }
        else if (adjust1 && adjust2 && (m - mse[deg1+1][deg2+1])/m > 0.1)
        {
            deg1++;
            deg2++;
            finished = false;
        }
        else if (adjust1
             && (m - mse[deg1+2][deg2])/m > 0.1
             && (!adjust2 || mse[deg1+2][deg2] <= mse[deg1][deg2+2])
        )
        {
            deg1 += 2;
            finished = false;
        }
        else if (adjust2 
             && (m - mse[deg1][deg2+2])/m > 0.1
             && (!adjust1 || mse[deg1+2][deg2] < mse[deg1][deg2+2]))
        {
            deg2 += 2;
            finished = false;
        }

        /* For efficiency, stop if rms reached min_rms */   
        finished = finished || (sqrt(mse[deg1][deg2]) < min_rms);

    } while (!finished);

    uves_polynomial_delete(&bivariate_fit);
    check( bivariate_fit = uves_polynomial_regression_2d(t,
                             X1, X2, Y, sigmaY,
                             deg1,
                             deg2,
                             polynomial_fit, residual_square, 
                             variance_fit,
                             mean_squared_error, red_chisq,
                             variance, kappa, min_reject),
       "Error fitting (%d, %d)-degree polynomial", deg1, deg2);

    if (verbose)
        uves_msg_low("Using degree (%d, %d), RMS = %.3g %s", deg1, deg2, 
                     sqrt(mse[deg1][deg2]), y_unit);
    else
        uves_msg_debug("Using degree (%d, %d), RMS = %.3g %s", deg1, deg2, 
                     sqrt(mse[deg1][deg2]), y_unit);
    
  cleanup:
    if (mse != NULL)
    {
        for (i = 0; i < maxdeg1+1; i++)
        {
            if (mse[i] != NULL)
            {
                cpl_free(mse[i]);
            }
        }
        cpl_free(mse);
    }
    uves_free_table(&temp);
    
    return bivariate_fit;    
}

/*----------------------------------------------------------------------------*/
/**
   @brief          Remove named prefix from string
   @param s       string
   @param prefix   the prefix to remove
   @return  s without prefix, or NULL on error. This points to a position in 
   the string @em s, and therefore must not be deallocated.

   The function fails if @em prefix is not a prefix of @em s.
*/
/*----------------------------------------------------------------------------*/
const char *
uves_remove_string_prefix(const char *s, const char *prefix)
{
    const char *result = NULL;
    unsigned int prefix_length;

    assure( s != NULL, CPL_ERROR_NULL_INPUT, "Null string");
    assure( prefix != NULL, CPL_ERROR_NULL_INPUT, "Null string");

    prefix_length = strlen(prefix);

    assure( strlen(s) >= prefix_length &&
        strncmp(s, prefix, prefix_length) == 0,
        CPL_ERROR_INCOMPATIBLE_INPUT, "'%s' is not a prefix of '%s'",
        prefix, s);
    
    result = s + prefix_length;
    
  cleanup:
    return result;
}


/*----------------------------------------------------------------------------*/
/**
   @brief  Pseudo-random gaussian distributed number
   @return Pseudo-random gasssian value with mean zero, stdev 1, based
   on C's rand()

   It is probably a good idea to call srand() before using this function, for
   reasons of reproducability.
*/
/*----------------------------------------------------------------------------*/

double uves_gaussrand(void)
{
    static double V1, V2, S;
    static int phase = 0;
    double X;
    
    if(phase == 0) {
    do {
        double U1 = (double)rand() / RAND_MAX;
        double U2 = (double)rand() / RAND_MAX;
        
        V1 = 2 * U1 - 1;
        V2 = 2 * U2 - 1;
        S = V1 * V1 + V2 * V2;
    } while(S >= 1 || S == 0);
    
    X = V1 * sqrt(-2 * log(S) / S);
    } else
    X = V2 * sqrt(-2 * log(S) / S);
    
    phase = 1 - phase;
    
    return X;
}

/*----------------------------------------------------------------------------*/
/**
   @brief        Spline interpolation based on Hermite polynomials
   @param xp     x-value to interpolate
   @param t      Table containing the columns to interpolate
   @param column_x  Column of x-values
   @param column_y  Column of y-values
   @param istart    (input/output) initial row (set to 0 to search all row)
      
   @return The interpolated value.
*/
/*----------------------------------------------------------------------------*/

double uves_spline_hermite_table( double xp, const cpl_table *t, const char *column_x, 
                const char *column_y, int *istart )
{
    double result = 0;
    int n;

    const double *x, *y;
    
    check( x = cpl_table_get_data_double_const(t, column_x),
       "Error reading column '%s'", column_x);
    check( y = cpl_table_get_data_double_const(t, column_y),
       "Error reading column '%s'", column_y);

    n = cpl_table_get_nrow(t);

    result = uves_spline_hermite(xp, x, y, n, istart);

  cleanup:
    return result;
}

/*----------------------------------------------------------------------------*/
/**
   @brief        Spline interpolation based on Hermite polynomials
   @param xp     x-value to interpolate
   @param x      x-values
   @param y      y-values
   @param n      array length
   @param istart    (input/output) initial row (set to 0 to search all row)
      
   @return The interpolated value.

   The x column must be sorted (ascending or descending) and all x column
   values must be different.

   Adopted from: Cristian Levin - ESO La Silla, 1-Apr-1991
*/
/*----------------------------------------------------------------------------*/
double uves_spline_hermite( double xp, const double *x, const double *y, int n, int *istart )
{
    double yp1, yp2, yp = 0;
    double xpi, xpi1, l1, l2, lp1, lp2;
    int i;

    if ( x[0] <= x[n-1] && (xp < x[0] || xp > x[n-1]) )    return 0.0;
    if ( x[0] >  x[n-1] && (xp > x[0] || xp < x[n-1]) )    return 0.0;

    if ( x[0] <= x[n-1] )
    {
        for ( i = (*istart)+1; i <= n && xp >= x[i-1]; i++ )
        ;
    }
    else
    {
        for ( i = (*istart)+1; i <= n && xp <= x[i-1]; i++ )
        ;
    }

    *istart = i;
    i--;
    
    lp1 = 1.0 / (x[i-1] - x[i]);
    lp2 = -lp1;

    if ( i == 1 )
    {
        yp1 = (y[1] - y[0]) / (x[1] - x[0]);
    }
    else
    {
        yp1 = (y[i] - y[i-2]) / (x[i] - x[i-2]);
    }

    if ( i >= n - 1 )
    {
        yp2 = (y[n-1] - y[n-2]) / (x[n-1] - x[n-2]);
    }
    else
    {
        yp2 = (y[i+1] - y[i-1]) / (x[i+1] - x[i-1]);
    }

    xpi1 = xp - x[i];
    xpi  = xp - x[i-1];
    l1   = xpi1*lp1;
    l2   = xpi*lp2;

    yp = y[i-1]*(1 - 2.0*lp1*xpi)*l1*l1 + 
         y[i]*(1 - 2.0*lp2*xpi1)*l2*l2 + 
         yp1*xpi*l1*l1 + yp2*xpi1*l2*l2;

    return yp;
}

/*----------------------------------------------------------------------------*/
/**
   @brief    Natural cubic-spline interpolation.
   @param xp      x-value to interpolate
   @param x      x-array [1..n], must be sorted ascending
   @param y      y-array [1..n] 
   @param y2      y2-array [1..n] (2-nd derivatives)
   @param n       array size
   @param kstart  Start search index. Contains on output index
                  of largest x less than xp.

   @return the interpolated value

**/
/*----------------------------------------------------------------------------*/

double uves_spline_cubic( double xp, double *x, float *y, float *y2, int n, int *kstart )
{
    int klo, khi, k;
    double a, b, h, yp = 0;

    assure_nomsg( x  != NULL, CPL_ERROR_NULL_INPUT);
    assure_nomsg( y  != NULL, CPL_ERROR_NULL_INPUT);
    assure_nomsg( y2 != NULL, CPL_ERROR_NULL_INPUT);
    assure_nomsg( kstart != NULL, CPL_ERROR_NULL_INPUT);

    klo = *kstart;
    khi = n;

    if ( xp < x[1] || xp > x[n] )
    {
        return 0.0;
    }
    else if ( xp == x[1] )
    {
        return(y[1]);
    }
    
    for ( k = klo; k < n && xp > x[k]; k++ )
    ;

    klo = *kstart = k-1;
    khi = k;

    h = x[khi] - x[klo];
    assure( h != 0.0, CPL_ERROR_DIVISION_BY_ZERO,
        "Empty x-value range: xlo = %e ; xhi = %e", x[khi], x[klo]);

    a = (x[khi] - xp) / h;
    b = (xp - x[klo]) / h;

    yp = a*y[klo] + b*y[khi] + ((a*a*a - a)*y2[klo] + (b*b*b - b)*y2[khi])*
     (h*h) / 6.0;

  cleanup:
    return yp;
}

/*----------------------------------------------------------------------------*/
/**
   @brief    Determine if a table is sorted
   @param    t        Table
   @param    column   Column name, type must be double
   @param    reverse  Flag indicating to check for ascending (false) or
                      descending (true)
                      sort order
   @return   Non-zero iff table is sorted according to the specified column
*/
/*----------------------------------------------------------------------------*/
bool
uves_table_is_sorted_double(const cpl_table *t, const char *column, const bool reverse)
{
    bool is_sorted = true;       /* ... until proven false */
    int i;
    int N;
    double previous, current;    /* column values */

    passure(t != NULL, " ");
    passure(cpl_table_has_column(t, column), "No column '%s'", column);
    passure(cpl_table_get_column_type(t, column) == CPL_TYPE_DOUBLE, " ");
    
    N = cpl_table_get_nrow(t);

    if (N > 1) 
    {
        previous = cpl_table_get_double(t, column, 0, NULL);
        
        for(i = 1; i < N && is_sorted; i++)
        {
            current = cpl_table_get_double(t, column, i, NULL);
            if (!reverse)
            {
                /* Check for ascending */
                is_sorted = is_sorted && ( current >= previous );
            }
            else
            {
                /* Check for descending */
                is_sorted = is_sorted && ( current <= previous );
            }
            
            previous = current;
        }
    }
    else
    {
        /* 0 or 1 rows. Table is sorted */        
    }
    
  cleanup:
    return is_sorted;
}

/*----------------------------------------------------------------------------*/
/**
   @brief   Create the table that describes fibre traces
   @return  The table which must be deallocated with @c cpl_table_delete() .

*/
/*----------------------------------------------------------------------------*/
cpl_table *
uves_ordertable_traces_new(void)
{
    cpl_table *result = NULL;
    
    check((
          result = cpl_table_new(0),
          cpl_table_new_column(result, "TraceID"  , CPL_TYPE_INT),
          cpl_table_new_column(result, "Offset"   , CPL_TYPE_DOUBLE),
          cpl_table_new_column(result, "Tracemask", CPL_TYPE_INT)),
    "Error creating table");
    
  cleanup:
    return result;
}

/*----------------------------------------------------------------------------*/
/**
   @brief   Add a trace
   @param   traces        The table containing information on the
                          position of fibre traces (FLAMES/UVES)
   @param   fibre_ID      The fibre ID number
   @param   fibre_offset  Offset of this fibre
   @param   fibre_mask    0 if fibre is disabled, 1 if it is enabled
   @return  CPL_ERROR_NONE iff okay
*/
/*----------------------------------------------------------------------------*/
cpl_error_code
uves_ordertable_traces_add(cpl_table *traces, 
               int fibre_ID, double fibre_offset, int fibre_mask)
{
    int size;

    assure( traces != NULL, CPL_ERROR_NULL_INPUT, "Null table!");
    
    /* Write to new table row */
    check((
          size = cpl_table_get_nrow(traces),
          cpl_table_set_size  (traces, size+1),
          cpl_table_set_int   (traces, "TraceID"  , size, fibre_ID),
          cpl_table_set_double(traces, "Offset"   , size, fibre_offset),
          cpl_table_set_int   (traces, "Tracemask", size, fibre_mask)),
      "Error updating table");

  cleanup:
    return cpl_error_get_code();
}


/*----------------------------------------------------------------------------*/
/**
   @brief   Remove column units from a table
   @param   tname filename of the table containing units to be removed (FLAMES/UVES)
   @return  CPL_ERROR_NONE iff okay
*/
/*----------------------------------------------------------------------------*/
cpl_error_code
uves_tablename_remove_units(const char* tname)
{
   cpl_table* tab=NULL;
   uves_propertylist* head=NULL;
   tab=cpl_table_load(tname,1,0);
   head=uves_propertylist_load(tname,0);
   uves_table_remove_units(&tab);
   check_nomsg(uves_table_save(tab,head,NULL,tname,CPL_IO_DEFAULT));

  cleanup:
   uves_free_table(&tab);
   uves_free_propertylist(&head);
   return cpl_error_get_code();
}



/*----------------------------------------------------------------------------*/
/**
   @brief   Unify column units in tables
   @param   name2 of the table with reference units 
   @param   name1 of the table with new units 
   @return  CPL_ERROR_NONE iff okay
*/
/*----------------------------------------------------------------------------*/
cpl_error_code
uves_tablenames_unify_units(const char* tname2, const char* tname1)
{
   cpl_table* tab1=NULL;
   cpl_table* tab2=NULL;
   uves_propertylist* head2=NULL;

   tab1=cpl_table_load(tname1,1,0);

   tab2=cpl_table_load(tname2,1,0);
   head2=uves_propertylist_load(tname2,0);

   uves_table_unify_units(&tab2,&tab1);
   check_nomsg(uves_table_save(tab2,head2,NULL,tname2,CPL_IO_DEFAULT));

  cleanup:
   uves_free_table(&tab1);
   uves_free_table(&tab2);
   uves_free_propertylist(&head2);
   return cpl_error_get_code();

}



/*----------------------------------------------------------------------------*/
/**
   @brief   Remove column units from a table
   @param   tab The table containing units to be removed (FLAMES/UVES)
   @return  CPL_ERROR_NONE iff okay
*/
/*----------------------------------------------------------------------------*/
cpl_error_code
uves_table_remove_units(cpl_table **table)
{
    int ncols;
    const char* colname=NULL;
    int i=0;
    cpl_array *names=NULL;

    assure( *table != NULL, CPL_ERROR_NULL_INPUT, "Null input table!");
    ncols = cpl_table_get_ncol(*table);
    names = cpl_table_get_column_names(*table);
    for(i=0;i<ncols;i++) {
       colname=cpl_array_get_string(names, i);
       cpl_table_set_column_unit(*table,colname,NULL);
    }

  cleanup:
    uves_free_array(&names);

    return cpl_error_get_code();
}



/*----------------------------------------------------------------------------*/
/**
   @brief   Unify column units of table2 to table1
   @param   table2 The table with reference units 
   @param   table1 The table with new units 
   @return  CPL_ERROR_NONE iff okay
*/
/*----------------------------------------------------------------------------*/
cpl_error_code
uves_table_unify_units(cpl_table **table2,  cpl_table **table1)
{
    int ncols1;
    int ncols2;
    const char* colname=NULL;
    const char* unit1=NULL;

    int i=0;
    cpl_array *names=NULL;

    assure( table1 != NULL, CPL_ERROR_NULL_INPUT, "Null input table!");
    assure( *table2 != NULL, CPL_ERROR_NULL_INPUT, "Null input table!");
    ncols1 = cpl_table_get_ncol(*table1);
    ncols2 = cpl_table_get_ncol(*table2);
    assure( ncols1 == ncols2, CPL_ERROR_NULL_INPUT, 
            "n columns (tab1) != n columns (tab2)");

    names = cpl_table_get_column_names(*table1);
    for(i=0;i<ncols1;i++) {
       colname=cpl_array_get_string(names, i);
       unit1=cpl_table_get_column_unit(*table1,colname);
       cpl_table_set_column_unit(*table2,colname,unit1);
    }

  cleanup:
    uves_free_array(&names);

    return cpl_error_get_code();
}

/*
 * modified on 2006/04/19
 *  jmlarsen:  float[5] -> const double[]
 *             changed mapping of indices to parameters
 *             Normalized the profile to 1 and changed meaning
 *             of (a[3], a[2]) to (integrated flux, stdev)
 *             Disabled debugging messages
 *
 * modified on 2005/07/29 to make dydapar a FORTRAN array
 * (indiced from 1 to N instead of 0 to N-1).
 * This allows the array to be passed to C functions expecting
 * FORTRAN-like arrays.
 *
 * modified on 2005/08/02 to make the function prototype ANSI
 * compliant (so it can be used with the levmar library).
 *
 * modified on 2005/08/16. The function now expects C-indexed
 * arrays as parameters (to allow proper integration). However, the
 * arrays are still converted to FORTRAN-indexed arrays internally.
 */

/**
@brief This subroutine gives the value of the Moffat (beta=4)+ linear 
    functions at     pixel of coordinates x estimated at one point 
@param x  input pixel coordinates
@param a  profile coefficients
@param y  Moffat profile values
@param dyda errors associated to y


*/
static void fmoffa_i(float x,const double a[],double *y,double dyda[])

 
     /*     int na;*/
{
  double fac=0, fac2=0, fac4= 0, fac4i=0, arg=0, arg2=0;
  double a2i=0, m = 0, p = 0, dif =0;
  double sqrt5 = 2.23606797749979;

  *y=0.0;
//  a2i = 1.0/a[2];
  a2i = 1.0/(a[2]*sqrt5);

  dif=x-a[1];
  arg=dif*a2i;
  arg2=arg*arg;

  fac=1.0+arg2;
  fac2=fac*fac;
  fac4=fac2*fac2;
  fac4i = 1.0/fac4;
  
//  m = a[1]*fac4i;
  m = a[3]*fac4i * a2i*16/(5.0*M_PI);
  *y = m + a[4]*(1.0+dif*a[5]);  
  p = 8.0*m/fac*arg*a2i;

  dyda[3] = m/a[3];
  dyda[2] = p*dif/a[2] - m/a[2];

//  dyda[3]=fac4i;
  dyda[1]=p-a[4]*a[5];
//  dyda[2]=p*dif*a2i;
  dyda[4]=1.0+dif*a[5];
  dyda[5]=a[4]*dif;


#if 0
  {
     int i = 0, npar=5 ;
     printf("fmoffat_i \n");
     for (i = 1;i<=npar;i++) printf("a[%1i] %f :\n",i,a[i]);
     
     printf("fmoffat_i ");
     for (i = 1;i<=npar;i++) printf("%i %f :",i,dyda[i]);
     printf("\n");
  }
#endif
  
}

/**
@brief Moffat profile
@param x  input pixel coordinates
@param a  profile coefficients
@param y  Moffat profile values
@param dyda errors associated to y
@note

        This subroutine  gives  the value  of  the  Moffat (beta =  4,
    fixed)+ linear functions  at pixel of coordinates x integrated
    over  this pixel; the integration  is done  using the npoint=3
    Gauss-Legendre  integration formula. The weights and abscissae
    are modified to take into account the range of integration (in
    pratice, the values are divided by 2) .
    


*/
static void fmoffa_c(float x,const double a[],double *y,double dyda[])/*,na)*/
//void fmoffa_c(x,a,y, dyda)


//     float x,*a,*y,*dyda;
/*int na;*/
{
  int npoint = 3;
  double const xgl[3] = {-0.387298334621,0.,0.387298334621};
  double const wgl[3] = {.2777777777778,0.444444444444,0.2777777777778};
  int i=0;
  int j=0;
  int npar = 5;
  double xmod = 0;
  double dydapar[5]; /* = {0.,0.,0.,0.,0.,};*/
  double ypar;


  // Convert C-indexed arrays to FORTRAN-indexed arrays
  a    = C_TO_FORTRAN_INDEXING(a);
  dyda = C_TO_FORTRAN_INDEXING(dyda);

  *y = 0.0;
  for (i = 1;i<=npar;i++) dyda[i] = 0.;
  /*  printf("fmoffat_c ");
  for (i = 1;i<=npar;i++) printf("%i %f :",i,a[i]);*/
  /*for (i = 0;i<3;i++) printf("%i %f %f:",i,xgl[i],wgl[i]);*/
  /*  printf("\n");*/
  for (j=0; j < npoint; j++) 
      {
      xmod = x+xgl[j];

      fmoffa_i(xmod,a,&ypar,&dydapar[-1]);
      
      *y = *y + ypar*wgl[j];
      
      for (i = 1; i <= npar; i++)
          {
          dyda[i] = dyda[i] + dydapar[i-1]*wgl[j] ;
          }

     /*      if (j == 2) 
    for (i = 1;i<=npar;i++) 
      {
        dyda[i] = dydapar[i];
      };
     */
    }

#if 0
      printf("fmoffat_c ");
      for (i = 1;i<=npar;i++) printf("%i %f %f: \n",i,a[i],dyda[i]);
      printf("\n");
#endif
}

/*----------------------------------------------------------------------------*/
/**
   @brief   Evaluate a Moffat
   @param   x             The evaluation point
   @param   a             The parameters defining the gaussian
   @param   result        The function value
   @return  0 iff okay.
*/
/*----------------------------------------------------------------------------*/
int
uves_moffat(const double x[], const double a[], double *result)
{
    double dyda[5];

    fmoffa_c(x[0], a, result, dyda);

    return 0;
}

/*----------------------------------------------------------------------------*/
/**
   @brief   Evaluate Moffat derivative
   @param   x             The evaluation point
   @param   a             The parameters defining the gaussian
   @param   result        The function value
   @return  0 iff okay.
*/
/*----------------------------------------------------------------------------*/
int
uves_moffat_derivative(const double x[], const double a[], double result[])
{
    double y;

    fmoffa_c(x[0], a, &y, result);

    return 0;
}

/*----------------------------------------------------------------------------*/
/**
   @brief   Evaluate a gaussian
   @param   x             The evaluation point
   @param   a             The parameters defining the gaussian
   @param   result        The function value

   @return  0 iff okay.

   This function computes

   @em a3 +  @em a2 / sqrt(2 pi @em a1^2) *
   exp( -(@em x0 - @em a0)^2/(2 @em a1^2)).
  
   where @em a0, ..., @em a3 are the first four elements of @em a, and @em
   x0 is the first element of @em x .

   The function never fails.

*/
/*----------------------------------------------------------------------------*/

int
uves_gauss(const double x[], const double a[], double *result)
{
    double my    = a[0];
    double sigma = a[1];

    if (sigma == 0)
    {
        /* Dirac's delta function */
        if (x[0] == my)
        {
            *result = DBL_MAX;
        }
        else
        {
            *result = 0;
        }
        return 0;
    }
    else
    {
        double A     = a[2];
        double B     = a[3];
        
        *result = B    +
        A/(sqrt(2*M_PI*sigma*sigma)) *
        exp(- (x[0] - my)*(x[0] - my)
            / (2*sigma*sigma));
    }
    
    return 0;
}

/*----------------------------------------------------------------------------*/
/**
   @brief   Evaluate the derivatives of a gaussian
   @param   x             The evaluation point
   @param   a             The parameters defining the gaussian
   @param   result        The derivatives wrt to parameters

   @return  0 iff okay.

   This function computes the partial derivatives of
   @em f(@em x0,@em a) =
   @em a3 +  @em a2 / sqrt(2 pi @em a1^2) *
   exp( -(@em x0 - @em a0)^2/(2 @em a1^2))
   with respect to @em a0, ..., @em a3.
   On successful evaluation, the i'th element of the @em result vector
   contains df/da_i.

   The function never returns failure.

*/
/*----------------------------------------------------------------------------*/

int
uves_gauss_derivative(const double x[], const double a[], double result[])
{
    double my    = a[0];
    double sigma = a[1];
    double A     = a[2];
    /* a[3] not used */

    double factor;
   
    /* f(x) = B + A/sqrt(2 pi s^2) exp(-(x-my)^2/2s^2)
     *
     * df/d(my) = A/sqrt(2 pi s^2) exp(-(x-my)^2/2s^2) * (x-my)  / s^2
     *          = A * fac. * (x-my)  / s^2
     * df/ds    = A/sqrt(2 pi s^2) exp(-(x-my)^2/2s^2) * ((x-my)^2/s^3 - 1/s)
     *          = A * fac. * ((x-my)^2 / s^2 - 1) / s
     * df/dA    = 1/sqrt(2 pi s^2) exp(-(x-my)^2/2s^2)
     *          = fac.
     * df/dB    = 1
     */
    
    if (sigma == 0)
    {
        /* Derivative of Dirac's delta function */
        result[0] = 0;
        result[1] = 0;
        result[2] = 0;
        result[3] = 0;
        return 0;
    }

    factor = exp( -(x[0] - my)*(x[0] - my)/(2*sigma*sigma) )
    / (sqrt(2*M_PI*sigma*sigma));

    result[0] = A * factor * (x[0]-my) / (sigma*sigma);
    result[1] = A * factor * ((x[0]-my)*(x[0]-my) / (sigma*sigma) - 1) / sigma;
    result[2] = factor;
    result[3] = 1;

    return 0;
}

/*----------------------------------------------------------------------------*/
/**
   @brief   Evaluate a gaussian with linear background
   @param   x             The evaluation point
   @param   a             The parameters defining the gaussian
   @param   result        The function value

   @return  0 iff okay.

   This function computes

   @em a3 + a4*(@em x0 - @em a0)  +
   @em a2 / sqrt(2 pi @em a1^2) *
   exp( -(@em x0 - @em a0)^2/(2 @em a1^2)).
  
   where @em a0, ..., @em a4 are the first five elements of @em a, and @em
   x0 is the first element of @em x .

   The function never fails.

*/
/*----------------------------------------------------------------------------*/

int
uves_gauss_linear(const double x[], const double a[], double *result)
{
    double my    = a[0];
    double sigma = a[1];

    if (sigma == 0)
    {
        /* Dirac's delta function */
        if (x[0] == my)
        {
            *result = DBL_MAX;
        }
        else
        {
            *result = 0;
        }
        return 0;
    }
    else
    {
        double A     = a[2];
        double B     = a[3];
        double C     = a[4];
        
        *result = B    + C*(x[0] - my) +
        A/(sqrt(2*M_PI*sigma*sigma)) *
        exp(- (x[0] - my)*(x[0] - my)
            / (2*sigma*sigma));
    }
    
    return 0;
}

/*----------------------------------------------------------------------------*/
/**
   @brief   Evaluate the derivatives of a gaussian with linear background
   @param   x             The evaluation point
   @param   a             The parameters defining the gaussian
   @param   result        The derivatives wrt to parameters

   @return  0 iff okay.

   This function computes the partial derivatives of

   @em f(@em x0,@em a) =
   @em a3 + a4*(x0 - @em a0) +
   @em a2 / sqrt(2 pi @em a1^2) *
   exp( -(@em x0 - @em a0)^2/(2 @em a1^2))

   with respect to @em a0, ..., @em a4.
   On successful evaluation, the i'th element of the @em result vector
   contains df/da_i.

   The function never returns failure.

*/
/*----------------------------------------------------------------------------*/

int
uves_gauss_linear_derivative(const double x[], const double a[], double result[])
{
    double my    = a[0];
    double sigma = a[1];
    double A     = a[2];
    /* a[3] not used */
    double C     = a[4];

    double factor;
   
    /* f(x) = B + C(x-my) + A/sqrt(2 pi s^2) exp(-(x-my)^2/2s^2)
     *
     * df/d(my) = A/sqrt(2 pi s^2) exp(-(x-my)^2/2s^2) * (x-my)  / s^2
     *          = A * fac. * (x-my)  / s^2   - C
     * df/ds    = A/sqrt(2 pi s^2) exp(-(x-my)^2/2s^2) * ((x-my)^2/s^3 - 1/s)
     *          = A * fac. * ((x-my)^2 / s^2 - 1) / s
     * df/dA    = 1/sqrt(2 pi s^2) exp(-(x-my)^2/2s^2)
     *          = fac.
     * df/dB    = 1
     *
     * df/dC    = x-my
     */
    
    if (sigma == 0)
    {
        /* Derivative of Dirac's delta function */
        result[0] = -C;
        result[1] = 0;
        result[2] = 0;
        result[3] = 0;
        result[4] = x[0];
        return 0;
    }

    factor = exp( -(x[0] - my)*(x[0] - my)/(2*sigma*sigma) )
    / (sqrt(2*M_PI*sigma*sigma));

    result[0] = A * factor * (x[0]-my) / (sigma*sigma);
    result[1] = A * factor * ((x[0]-my)*(x[0]-my) / (sigma*sigma) - 1) / sigma;
    result[2] = factor;
    result[3] = 1;
    result[4] = x[0] - my;

    return 0;
}




/*----------------------------------------------------------------------------*/
/**
   @brief   Reconstruct echelle image from spectrum
   @param   pos             position iterator
   @param   chip            CCD chip  (for header)
   @param   spectrum        object spectrum
   @param   sky             sky spectrum
   @param   cosmic_image    if non-NULL, image of cosmic rays. Values > 0 mark
                            CR hits
   @param   image_noise     (output) error bars
   @param   image_header    (output) describing the output image
   @return  simulated image
*/
/*----------------------------------------------------------------------------*/
cpl_image *
uves_create_image(uves_iterate_position *pos, enum uves_chip chip,
                  const cpl_image *spectrum, const cpl_image *sky,
                  const cpl_image *cosmic_image,
                  const uves_extract_profile *profile,
                  cpl_image **image_noise, uves_propertylist **image_header)
{
    cpl_image *image = NULL;

    cpl_binary *bpm = NULL;
    bool loop_y = false;

    double ron = 3;
    double gain = 1.0; //fixme
    bool new_format = true;

    image        = cpl_image_new(pos->nx, pos->ny, CPL_TYPE_DOUBLE);
    assure_mem( image );
    if (image_noise != NULL) {
        *image_noise = cpl_image_new(pos->nx, pos->ny, CPL_TYPE_DOUBLE);
        assure_mem( *image_noise );
        cpl_image_add_scalar(*image_noise, 0.01); /* To avoid non-positive values */
    }

    if (image_header != NULL) {
        *image_header = uves_propertylist_new();
      
        uves_propertylist_append_double(*image_header, UVES_MJDOBS, 60000);
        uves_propertylist_append_double(*image_header, UVES_RON(new_format, chip), ron);
        uves_propertylist_append_double(*image_header, UVES_GAIN(new_format, chip), gain);
    }

    for (uves_iterate_set_first(pos,
                                1, pos->nx,
                                pos->minorder, pos->maxorder,
                                bpm,
                                loop_y);
         !uves_iterate_finished(pos); 
         uves_iterate_increment(pos)) {
      
        /* Manual loop over y */
        uves_extract_profile_set(profile, pos, NULL);
        for (pos->y = pos->ylow; pos->y <= pos->yhigh; pos->y++) {

            /* Get empirical and model profile */
            double flux, sky_flux;
            int bad;
            int spectrum_row = pos->order - pos->minorder + 1;
            double noise;
            double prof = uves_extract_profile_evaluate(profile, pos);
          
            if (sky != NULL)
                {
                    sky_flux = cpl_image_get(sky, pos->x, spectrum_row, &bad)/pos->sg.length;
                }
            else
                {
                    sky_flux = 0;
                }

            flux = cpl_image_get(spectrum, pos->x, spectrum_row, &bad) * prof + sky_flux;
          
            //fixme: check this formula
            noise = sqrt(gain)*sqrt(ron*ron/(gain*gain) + sky_flux/gain + flux/gain);
//          uves_msg_error("%f", prof);
            cpl_image_set(image, pos->x, pos->y, 
                          flux);
            if (image_noise != NULL) cpl_image_set(*image_noise, pos->x, pos->y, noise);
          
        }
    }

    if (cosmic_image != NULL) {
        double cr_val = 2*cpl_image_get_max(image);
        /* assign high pixel value to CR pixels */
        
        loop_y = true;
        
        for (uves_iterate_set_first(pos,
                                    1, pos->nx,
                                    pos->minorder, pos->maxorder,
                                    bpm,
                                    loop_y);
             !uves_iterate_finished(pos); 
             uves_iterate_increment(pos)) {
            
            int is_rejected;
            if (cpl_image_get(cosmic_image, pos->x, pos->y, &is_rejected) > 0) {
                cpl_image_set(image, pos->x, pos->y, cr_val);
            }
        }
    }
    
  cleanup:
    return image;
}

void 
uves_frameset_dump(cpl_frameset* set)
{

  cpl_frame* frm=NULL;
  int sz=0;
  int i=0;

  cknull(set,"Null input frameset");
  check_nomsg(sz=cpl_frameset_get_size(set));
  for(i=0;i<sz;i++) {
      frm=cpl_frameset_get_frame(set,i);
    uves_msg("frame %d tag %s filename %s group %d",
	     i,
             cpl_frame_get_tag(frm),
             cpl_frame_get_filename(frm),
             cpl_frame_get_group(frm));

  }

  cleanup:

  return ;
}




/*-------------------------------------------------------------------------*/
/**
  @name        uves_image_smooth_x
  @memo        Smooth an image using a simple mean.
  @param       inp        Image to shift.
  @return      1 newly allocated image.
  @see         uves_spline_hermite
  @doc

  This function interpolate an image using hermite splines.

  The returned image is a newly allocated object, it must be deallocated
  using uves_image_delete().
 */
/*--------------------------------------------------------------------------*/

cpl_image *
uves_image_smooth_x(cpl_image * inp, const int r)
{

  /*
   @param xp     x-value to interpolate
   @param x      x-values
   @param y      y-values
   @param n      array length
   @param istart    (input/output) initial row (set to 0 to search all row)

  */
  float* pinp=NULL;
  float* pout=NULL;
  int sx=0;
  int sy=0;
  int i=0;
  int j=0;
  int k=0;

  cpl_image* out=NULL;

  cknull(inp,"Null in put image, exit");
  check_nomsg(out=cpl_image_duplicate(inp));
  check_nomsg(sx=cpl_image_get_size_x(inp));
  check_nomsg(sy=cpl_image_get_size_y(inp));
  check_nomsg(pinp=cpl_image_get_data_float(inp));
  check_nomsg(pout=cpl_image_get_data_float(out));
  for(j=0;j<sy;j++) {
    for(i=r;i<sx-r;i++) {
      for(k=-r;k<r;k++) {
	pout[j*sx+i]+=pinp[j*sx+i+k];
      }
      pout[j*sx+i]/=2*r;
    }
  }

 cleanup:

  if(cpl_error_get_code() != CPL_ERROR_NONE) {
    return NULL;
  } else {
    return out;

  }

}





/*-------------------------------------------------------------------------*/
/**
  @name        uves_image_smooth_x
  @memo        Smooth an image using a simple mean.
  @param       inp        Image to shift.
  @return      1 newly allocated image.
  @see         uves_spline_hermite
  @doc

  This function interpolate an image using hermite splines.

  The returned image is a newly allocated object, it must be deallocated
  using uves_image_delete().
 */
/*--------------------------------------------------------------------------*/

cpl_image *
uves_image_smooth_y(cpl_image * inp, const int r)
{

  /*
   @param xp     x-value to interpolate
   @param x      x-values
   @param y      y-values
   @param n      array length
   @param istart    (input/output) initial row (set to 0 to search all row)

  */
  float* pinp=NULL;
  float* pout=NULL;
  int sx=0;
  int sy=0;
  int i=0;
  int j=0;
  int k=0;

  cpl_image* out=NULL;

  cknull(inp,"Null in put image, exit");
  check_nomsg(out=cpl_image_duplicate(inp));
  check_nomsg(sx=cpl_image_get_size_x(inp));
  check_nomsg(sy=cpl_image_get_size_y(inp));
  check_nomsg(pinp=cpl_image_get_data_float(inp));
  check_nomsg(pout=cpl_image_get_data_float(out));
  for(j=r;j<sy-r;j++) {
    for(i=0;i<sx;i++) {
      for(k=-r;k<r;k++) {
	pout[j*sx+i]+=pinp[(j+k)*sx+i];
      }
      pout[j*sx+i]/=2*r;
    }
  }

 cleanup:

  if(cpl_error_get_code() != CPL_ERROR_NONE) {
    return NULL;
  } else {
    return out;

  }

}


/*-------------------------------------------------------------------------*/
/**
  @name        uves_image_smooth_mean_x
  @memo        Smooth an image using a simple mean.
  @param       inp        Image to shift.
  @return      1 newly allocated image.
  @see         uves_spline_hermite
  @doc

  This function interpolate an image using hermite splines.

  The returned image is a newly allocated object, it must be deallocated
  using uves_image_delete().
 */
/*--------------------------------------------------------------------------*/

cpl_image *
uves_image_smooth_mean_x(cpl_image * inp, const int r)
{

  /*
   @param xp     x-value to interpolate
   @param x      x-values
   @param y      y-values
   @param n      array length
   @param istart    (input/output) initial row (set to 0 to search all row)

  */
  float* pinp=NULL;
  float* pout=NULL;
  int sx=0;
  int sy=0;
  int i=0;
  int j=0;
  int k=0;

  cpl_image* out=NULL;

  cknull(inp,"Null in put image, exit");
  check_nomsg(out=cpl_image_duplicate(inp));
  check_nomsg(sx=cpl_image_get_size_x(inp));
  check_nomsg(sy=cpl_image_get_size_y(inp));
  check_nomsg(pinp=cpl_image_get_data_float(inp));
  check_nomsg(pout=cpl_image_get_data_float(out));
  for(j=0;j<sy;j++) {
    for(i=r;i<sx-r;i++) {
      for(k=-r;k<r;k++) {
	pout[j*sx+i]+=pinp[j*sx+i+k];
      }
      pout[j*sx+i]/=2*r;
    }
  }

 cleanup:

  if(cpl_error_get_code() != CPL_ERROR_NONE) {
    return NULL;
  } else {
    return out;

  }

}


/*-------------------------------------------------------------------------*/
/**
  @name        uves_image_smooth_median_x
  @memo        Smooth an image using a simple mean.
  @param       inp        Image to shift.
  @return      1 newly allocated image.
  @see         uves_spline_hermite
  @doc

  This function interpolate an image using hermite splines.

  The returned image is a newly allocated object, it must be deallocated
  using uves_image_delete().
 */
/*--------------------------------------------------------------------------*/

cpl_image *
uves_image_smooth_median_x(cpl_image * inp, const int r)
{

  /*
   @param xp     x-value to interpolate
   @param x      x-values
   @param y      y-values
   @param n      array length
   @param istart    (input/output) initial row (set to 0 to search all row)

  */
  float* pout=NULL;
  int sx=0;
  int sy=0;
  int i=0;
  int j=0;

  cpl_image* out=NULL;


  cknull(inp,"Null in put image, exit");
  check_nomsg(out=cpl_image_duplicate(inp));
  check_nomsg(sx=cpl_image_get_size_x(inp));
  check_nomsg(sy=cpl_image_get_size_y(inp));
  check_nomsg(pout=cpl_image_get_data_float(out));

  for(j=1;j<sy;j++) {
    for(i=1+r;i<sx-r;i++) {
      pout[j*sx+i]=(float)cpl_image_get_median_window(inp,i,j,i+r,j);
    }
  }

 cleanup:

  if(cpl_error_get_code() != CPL_ERROR_NONE) {
    return NULL;
  } else {
    return out;

  }

}

/*-------------------------------------------------------------------------*/
/**
  @name        uves_image_smooth_fft
  @memo        Smooth an image using a FFT.
  @param       inp  Image to filter
  @param       fx  Image to filter
  @return      1 newly allocated image.
  @doc
  This function applies a lowpass spatial filter of frequency fy along Y.

  The returned image is a newly allocated object, it must be deallocated
  using uves_free_image().
 */
/*--------------------------------------------------------------------------*/

cpl_image *
uves_image_smooth_fft(cpl_image * inp, const int fx)
{

  int sx=0;
  int sy=0;

  cpl_image* out=NULL;
  cpl_image* im_re=NULL;
  cpl_image* im_im=NULL;
  cpl_image* ifft_re=NULL;
  cpl_image* ifft_im=NULL;
  cpl_image* filter=NULL; 

  int sigma_x=fx;
  int sigma_y=0;

  cknull(inp,"Null in put image, exit");
  check_nomsg(im_re = cpl_image_cast(inp, CPL_TYPE_DOUBLE));
  check_nomsg(im_im = cpl_image_cast(inp, CPL_TYPE_DOUBLE));

  // Compute FFT
  check_nomsg(cpl_image_fft(im_re,im_im,CPL_FFT_DEFAULT));

  check_nomsg(sx=cpl_image_get_size_x(inp));
  check_nomsg(sy=cpl_image_get_size_y(inp));
  sigma_x=sx;

  //Generates filter image
  check_nomsg(filter = uves_gen_lowpass(sx,sy,sigma_x,sigma_y));

  //Apply filter
  cpl_image_multiply(im_re,filter);
  cpl_image_multiply(im_im,filter);

  uves_free_image(&filter);

  check_nomsg(ifft_re = cpl_image_duplicate(im_re));
  check_nomsg(ifft_im = cpl_image_duplicate(im_im));

  uves_free_image(&im_re);
  uves_free_image(&im_im);

  //Computes FFT-INVERSE
  check_nomsg(cpl_image_fft(ifft_re,ifft_im,CPL_FFT_INVERSE));
  check_nomsg(out = cpl_image_cast(ifft_re, CPL_TYPE_FLOAT));

 cleanup:

  uves_free_image(&ifft_re);
  uves_free_image(&ifft_im);
  uves_free_image(&filter);
  uves_free_image(&im_re);
  uves_free_image(&im_im);

  if(cpl_error_get_code() != CPL_ERROR_NONE) {
    return NULL;
  } else {
    return out;
  }

}

/*-------------------------------------------------------------------------*/
/**
  @brief	Computes kappa-sigma clean mean (free bad pixels) for each input image of the input imagelist.
  @param	iml      input imagelist
  @param	kappa    value for kappa-sigma clip
  @return	vector with computed values for each image of the list

  The returned vector must be deallocated.
 */
/*--------------------------------------------------------------------------*/
cpl_vector * 
uves_imagelist_get_clean_mean_levels(cpl_imagelist* iml, double kappa)
{

   cpl_image* img=NULL;
   int size=0;
   int i=0;
   cpl_vector* values=NULL;
   double* pval=NULL;
   double mean=0;
   double stdev=0;
  
   check_nomsg(size=cpl_imagelist_get_size(iml));
   check_nomsg(values=cpl_vector_new(size));
   pval=cpl_vector_get_data(values);
   for(i=0;i<size;i++) {
      img=cpl_imagelist_get(iml,i);
      irplib_ksigma_clip(img,1,1,
                         cpl_image_get_size_x(img),
                         cpl_image_get_size_y(img),
                         5,kappa,1.e-5,&mean,&stdev);
      uves_msg("Ima %d mean level: %g",i+1,mean);
      pval[i]=mean;
   }

  cleanup:

   return values;
}


/*-------------------------------------------------------------------------*/
/**
  @brief	Subtract from input imagelist values specified in input vector.
  @param	iml      input imagelist
  @param	values   value to be subtracted
  @return	corrected imagelist

  The returned vector must be deallocated.
 */
/*--------------------------------------------------------------------------*/
cpl_error_code
uves_imagelist_subtract_values(cpl_imagelist** iml, cpl_vector* values)
{

   cpl_image* img=NULL;
   int size=0;
   int i=0;
   double* pval=NULL;
  
   check_nomsg(size=cpl_imagelist_get_size(*iml));
   pval=cpl_vector_get_data(values);
   for(i=0;i<size;i++) {
      img=cpl_imagelist_get(*iml,i);
      cpl_image_subtract_scalar(img,pval[i]);
      cpl_imagelist_set(*iml,img,i);
   }

  cleanup:

   return cpl_error_get_code();
}


/*-------------------------------------------------------------------------*/
/**
  @brief	Generate a low pass filter for FFT convolution .
  @param	xs	x size of the generated image.
  @param	ys	y size of the generated image.
  @param	sigma_x	Sigma for the gaussian distribution.
  @param	sigma_y      Sigma for the gaussian distribution.
  @return	1 newly allocated image.

  This function generates an image of a 2d gaussian, modified in such
  a way that the different quadrants have a quadrants of the gaussian
  in the corner. This image is suitable for FFT convolution.
  Copied from eclipse, src/iproc/generate.c

  The returned image must be deallocated.
 */
/*--------------------------------------------------------------------------*/
static cpl_image * 
uves_gen_lowpass(const int xs, 
                  const int ys, 
                  const double sigma_x, 
                  const double sigma_y)
{

    int i= 0.0;
    int j= 0.0;
    int hlx= 0.0;
    int hly = 0.0;
    double x= 0.0;
    double y= 0.0;
    double gaussval= 0.0;
    double inv_sigma_x=1./sigma_x;
    double inv_sigma_y=1./sigma_y;

    float *data;

    cpl_image 	*lowpass_image=NULL;


    lowpass_image = cpl_image_new (xs, ys, CPL_TYPE_FLOAT);
    if (lowpass_image == NULL) {
        uves_msg_error("Cannot generate lowpass filter <%s>",
                        cpl_error_get_message());
        return NULL;
    }

    hlx = xs/2;
    hly = ys/2;

    data = cpl_image_get_data_float(lowpass_image);
		
/* Given an image with pixels 0<=i<N, 0<=j<M then the convolution image
   has the following properties:

   ima[0][0] = 1
   ima[i][0] = ima[N-i][0] = exp (-0.5 * (i/sig_i)^2)   1<=i<N/2
   ima[0][j] = ima[0][M-j] = exp (-0.5 * (j/sig_j)^2)   1<=j<M/2
   ima[i][j] = ima[N-i][j] = ima[i][M-j] = ima[N-i][M-j] 
             = exp (-0.5 * ((i/sig_i)^2 + (j/sig_j)^2)) 
*/

    data[0] = 1.0;

    /* first row */
    for (i=1 ; i<=hlx ; i++) {
        x = i * inv_sigma_x;
        gaussval = exp(-0.5*x*x);
        data[i] = gaussval;
        data[xs-i] = gaussval;
    }

    for (j=1; j<=hly ; j++) {
        y = j * inv_sigma_y;
      /* first column */
        data[j*xs] = exp(-0.5*y*y);
        data[(ys-j)*xs] = exp(-0.5*y*y);

        for (i=1 ; i<=hlx ; i++) {
	/* Use internal symetries */
            x = i * inv_sigma_x;
            gaussval = exp (-0.5*(x*x+y*y));
            data[j*xs+i] = gaussval;
            data[(j+1)*xs-i] = gaussval;
            data[(ys-j)*xs+i] = gaussval;
            data[(ys+1-j)*xs-i] = gaussval;

        }
    }

    /* FIXME: for the moment, reset errno which is coming from exp()
            in first for-loop at i=348. This is causing cfitsio to
            fail when loading an extension image (bug in cfitsio too).
    */
    if(errno != 0)
        errno = 0;
    
    return lowpass_image;
}
/*-------------------------------------------------------------------------*/
/**
  @brief	Flag blemishes in a flat image
  @param	flat input image
  @param	head input header
  @return	output flag image or NULL 

 */
/*--------------------------------------------------------------------------*/
cpl_image*
uves_image_mflat_detect_blemishes(const cpl_image* flat, 
                                  const uves_propertylist* head)
{

   cpl_image* result=NULL;
   cpl_image* diff=NULL;
   cpl_image* flat_smooth=NULL;
   cpl_array* val=NULL;
   cpl_matrix* mx=NULL;

   int binx=0;
   int biny=0;
   int sx=0;
   int sy=0;
   int size=0;
   int i=0;
   int j=0;
   int k=0;
   int niter=3;
   int filter_width_x=7;
   int filter_width_y=7;

   double mean=0;
   double stdev=0;
   double stdev_x_4=0;

   double med_flat=0;

   double* pres=NULL;
   const double* pima=NULL;
   double* pval=NULL;
   double* pdif=NULL;
   int npixs=0;

   /* check input is valid */
   passure( flat !=NULL , "NULL input flat ");
   passure( head !=NULL , "NULL input head ");
  
   /* get image and bin sizes */
   sx=cpl_image_get_size_x(flat);
   sy=cpl_image_get_size_y(flat);
   npixs=sx*sy;

   binx=uves_pfits_get_binx(head);
   biny=uves_pfits_get_biny(head);

   /* set proper x/y filter width. Start values are 3 */
   if (binx>1) filter_width_x=5;
   if (biny>1) filter_width_y=5;


   /* create residuals image from smoothed flat */
   check_nomsg(mx=cpl_matrix_new(filter_width_x,filter_width_y));
  
  for(j=0; j< filter_width_y; j++){
    for(i=0; i< filter_width_x; i++){
      cpl_matrix_set( mx, i,j,1.0);
    }
  }
  
   check_nomsg(diff=cpl_image_duplicate(flat));

   check_nomsg(flat_smooth=uves_image_filter_median(flat,mx));
   /*
   check_nomsg(cpl_image_save(flat_smooth,"flat_smooth.fits",
			      CPL_BPP_IEEE_FLOAT,NULL,CPL_IO_DEFAULT));
   */
   check_nomsg(cpl_image_subtract(diff,flat_smooth));
   /*
   check_nomsg(cpl_image_save(diff,"diff.fits",
			      CPL_BPP_IEEE_FLOAT,NULL,CPL_IO_DEFAULT));
   */  
   /* compute median of flat */
   check_nomsg(med_flat=cpl_image_get_median(flat));

   /* prepare array of flat pixel values greater than the median */
   val=cpl_array_new(npixs,CPL_TYPE_DOUBLE);
   check_nomsg(cpl_array_fill_window_double(val,0,npixs,0));
   check_nomsg(pval=cpl_array_get_data_double(val));
   check_nomsg(pima=cpl_image_get_data_double_const(flat));
   check_nomsg(pdif=cpl_image_get_data_double(diff));
   k=0;
   for(i=0;i<npixs;i++) {
     if(pima[i]>med_flat) {
        pval[k]=pdif[i]; 
        k++;
     } 
   }   

   check_nomsg(cpl_array_set_size(val,k));
   pval=cpl_array_get_data_double(val);
   /* computes 4 sigma clip mean of values */
   check_nomsg(mean=cpl_array_get_mean(val));
   check_nomsg(stdev=cpl_array_get_stdev(val));
   stdev_x_4=stdev*4.;
   check_nomsg(size=cpl_array_get_size(val));

   for(i=0;i<niter;i++) {
     for(k=0;k<size;k++) {
       if(fabs(pval[k]-mean)>stdev_x_4) {
	 cpl_array_set_invalid(val,k);
       }
     }
     mean=cpl_array_get_mean(val);
     stdev=cpl_array_get_stdev(val);
     stdev_x_4=stdev*4.;
   }

   /* compute absolute value of difference image */
   result=cpl_image_new(sx,sy,CPL_TYPE_DOUBLE);
   pres=cpl_image_get_data_double(result);
   for(i=0;i<npixs;i++) {
     if(fabs(pdif[i])<stdev_x_4) {
       pres[i]=1.;
     }
   }

   /* save result to debug */
   /*
   check_nomsg(cpl_image_save(result,"blemish.fits",CPL_BPP_IEEE_FLOAT,NULL,
			CPL_IO_DEFAULT));
   */

 cleanup:
   uves_free_array(&val);
   uves_free_image(&diff);
   uves_free_image(&flat_smooth);
   uves_free_matrix(&mx);
   return result;
}


/**@}*/