This file is indexed.

/usr/include/casacore/images/Images/ImageRegrid.tcc is in casacore-dev 2.2.0-2.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

   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
//# ImageRegrid.cc: Regrids images
//# Copyright (C) 1996,1997,1998,1999,2000,2001,2002
//# Associated Universities, Inc. Washington DC, USA.
//#
//# This library is free software; you can redistribute it and/or modify it
//# under the terms of the GNU Library General Public License as published by
//# the Free Software Foundation; either version 2 of the License, or (at your
//# option) any later version.
//#
//# This library 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 Library General Public
//# License for more details.
//#
//# You should have received a copy of the GNU Library General Public License
//# along with this library; if not, write to the Free Software Foundation,
//# Inc., 675 Massachusetts Ave, Cambridge, MA 02139, USA.
//#
//# Correspondence concerning AIPS++ should be addressed as follows:
//#        Internet email: aips2-request@nrao.edu.
//#        Postal address: AIPS++ Project Office
//#                        National Radio Astronomy Observatory
//#                        520 Edgemont Road
//#                        Charlottesville, VA 22903-2475 USA
//#
//#
//# $Id$

#ifndef IMAGES_IMAGEREGRID_TCC
#define IMAGES_IMAGEREGRID_TCC

#include <casacore/images/Images/ImageRegrid.h>

#include <casacore/casa/Arrays/ArrayAccessor.h>
#include <casacore/casa/Exceptions/Error.h>
#include <casacore/coordinates/Coordinates/DirectionCoordinate.h>
#include <casacore/coordinates/Coordinates/LinearCoordinate.h>
#include <casacore/coordinates/Coordinates/SpectralCoordinate.h>
#include <casacore/images/Images/SubImage.h>
#include <casacore/images/Images/TempImage.h>
#include <casacore/lattices/Lattices/LatticeUtilities.h>
#include <casacore/measures/Measures/MCDirection.h>
#include <casacore/measures/Measures/MCFrequency.h>
#include <casacore/scimath/Mathematics/InterpolateArray1D.h>
#include <casacore/casa/System/ProgressMeter.h>

#include <casacore/casa/sstream.h>
#include <casacore/casa/fstream.h>

namespace casacore { //# NAMESPACE CASACORE - BEGIN

template<class T>
ImageRegrid<T>::ImageRegrid()
: itsShowLevel(0),
  itsDisableConversions(False),
  itsNotify(False)
{;}

template<class T>
ImageRegrid<T>::ImageRegrid(const ImageRegrid& other)  
: itsShowLevel(other.itsShowLevel),
  itsDisableConversions(other.itsDisableConversions),
  itsNotify(other.itsNotify)
{;}


template<class T>
ImageRegrid<T>::~ImageRegrid()
{;}

template<class T>
ImageRegrid<T>& ImageRegrid<T>::operator=(const ImageRegrid& other) 
{
  if (this != &other) {
    itsShowLevel = other.itsShowLevel;
    itsDisableConversions = other.itsDisableConversions;
    itsNotify = other.itsNotify;
  }
  return *this;
}

template<class T>
void ImageRegrid<T>::regrid(
	ImageInterface<T>& outImage,
	typename Interpolate2D::Method method,
	const IPosition& outPixelAxesU,
	const ImageInterface<T>& inImage,
	Bool replicate, uInt decimate, Bool showProgress,
	Bool forceRegrid, Bool verbose
) {
	LogIO os(LogOrigin("ImageRegrid", __func__, WHERE));
	Timer t0;
	IPosition outShape = outImage.shape();
	IPosition inShape = inImage.shape();
	const uInt nDim = inImage.ndim();
	ThrowIf(
		nDim != outImage.ndim(),
		"The input and output images must have the same "
		"number of axes"
	);
	if (inImage.imageInfo().hasMultipleBeams()) {
		ThrowIf(
			inImage.coordinates().hasSpectralAxis()
			&& anyTrue(
				outPixelAxesU.asVector()
				== inImage.coordinates().spectralAxisNumber()
			),
			"This image has multiple beams. The spectral axis cannot be regridded"
		);
		ThrowIf(
			inImage.coordinates().hasPolarizationCoordinate()
			&& anyTrue(
				outPixelAxesU.asVector()
				== inImage.coordinates().polarizationAxisNumber()
			),
			"This image has multiple beams. The polarization axis cannot be regridded"
		);
	}
	const Bool outIsMasked = outImage.isMasked() && outImage.hasPixelMask()
			&& outImage.pixelMask().isWritable();
	const CoordinateSystem& inCoords = inImage.coordinates();
	CoordinateSystem outCoords = outImage.coordinates();
	IPosition outPixelAxes = outPixelAxesU;

	// Find world and pixel axis maps

	Vector<Int> pixelAxisMap1, pixelAxisMap2;
	findMaps (nDim, pixelAxisMap1, pixelAxisMap2, inCoords, outCoords);

	// Check user pixel axes specifications

	_checkAxes(outPixelAxes, inShape, outShape, pixelAxisMap1, outCoords, verbose);

	{
		// warn if necessary, CAS-5110
		Vector<Int> dirAxes = outCoords.directionAxesNumbers();
		Bool regridDirAxis = False;
		for (uInt i=0; i<outPixelAxes.size(); i++) {
			for (uInt j=0; j<dirAxes.size(); j++) {
				if (outPixelAxes[i] == dirAxes[j]) {
					regridDirAxis = True;
					break;
				}
			}
			if (regridDirAxis) {
				break;
			}
		}
		const ImageInfo info = inImage.imageInfo();
		if (regridDirAxis && info.hasBeam()) {
			const DirectionCoordinate dc = inImage.coordinates().directionCoordinate();
			Vector<Double> inc = dc.increment();
			Vector<String> units = dc.worldAxisUnits();
			Quantity inpix = min(Quantity(inc[0], units[0]), Quantity(inc[1], units[1]));
			Quantity inbeam = info.hasSingleBeam()
				? info.restoringBeam().getMinor()
				: info.getBeamSet().getSmallestMinorAxisBeam().getMinor();
			const DirectionCoordinate dcout = outImage.coordinates().directionCoordinate();
			inc = dcout.increment();
			units = dcout.worldAxisUnits();
			Quantity outpix = min(Quantity(inc[0], units[0]), Quantity(inc[1], units[1]));
			if (
				(
					method == Interpolate2D::NEAREST && inbeam/inpix < Quantity(5, "")
					&& outpix/inpix > Quantity(0.5, "")
				)
				|| (
					method == Interpolate2D::LINEAR && inbeam/inpix < Quantity(3, "")
					&& outpix/inpix > Quantity(0.75, "")
				)
				|| (
					method == Interpolate2D::CUBIC && inbeam/inpix < Quantity(3, "")
					&& outpix/inpix > Quantity(1, "")
				)
			) {
				LogIO log;
				log << LogOrigin("ImageRegrid", __func__) << LogIO::WARN
					<< "You are regridding an image whose beam is not well sampled by the "
					<< "pixel size.  Total flux can be lost when regridding such "
					<< "images, especially when the new pixel size is larger than "
					<< "the old pixel size. It is recommended to check the total "
					<< "flux of your input and output image, and if necessary "
					<< "rebin the input to have smaller pixels." << LogIO::POST;
			}

		}
	}
	const uInt nOutRegridPixelAxes = outPixelAxes.nelements();
	if (itsShowLevel>0) {
		cerr << "outPixelAxes = " << outPixelAxes << endl;
	}

	// Set output shape.  This shape is incremental, for each regridding
	// pass it incrementally changes from the input shape to the output shape
	// We account here for different pixel axis orders

	IPosition outShape2(nDim);
	for (uInt paOut=0; paOut<nDim; paOut++) {
		outShape2(paOut) = inShape(pixelAxisMap1[paOut]);
	}

	// Specify input and output lattices for each regridding pass

	MaskedLattice<T>* inPtr = 0;
	CoordinateSystem inCoords2(inCoords);
	MaskedLattice<T>* outPtr = 0;
	MaskedLattice<T>* finalOutPtr = &outImage;
	Vector<Bool> doneOutPixelAxes(outCoords.nPixelAxes(), True);
	for (uInt i=0; i<nOutRegridPixelAxes; i++) {
		doneOutPixelAxes(outPixelAxes[i]) = False;
	}
	// Loop over specified pixel axes of output image

	Bool first = True;
	for (uInt i=0; i<nOutRegridPixelAxes; i++) {
		if (!doneOutPixelAxes(outPixelAxes[i])) {

			// Set input and output images for this pass. The new  input must be the last
			// output image.  We end up with at least one temporary image. Could
			// probably improve this.
			if (first) {
				inPtr = inImage.cloneML();
				first = False;
			}
			else {

				// If inPtr == 0, it means that in the previous pass, the coordinate
				// and shape information for the regrid axis was identical (in==out)
				// so we didn't
				// actually need to regrid, but just swap in and out pointers.

				if (inPtr) delete inPtr;
				inPtr = outPtr;
				outPtr = 0;
			}
			// Regrid one Coordinate, pertaining to this axis. If the axis
			// belongs to a DirectionCoordinate or 2-axis LinearCoordinate,
			// it will also regrid the other axis.  After the first pass,
			// the output image is in the final order.
			_regridOneCoordinate (
				os, outShape2, doneOutPixelAxes,
				finalOutPtr, inPtr, outPtr, outCoords,
				inCoords2, outPixelAxes[i], inImage,
				outShape, replicate, decimate, outIsMasked,
				showProgress, forceRegrid, method, verbose
			);
			// The input CS for the next pass is now the present output
			// CS, except that we overwrite the coordinates not yet regridded
			inCoords2 = outCoords;
			for (uInt j=0; j<doneOutPixelAxes.nelements(); j++) {
				// For every pixel axis that has not yet been regridded, put back
				// the original coordinate (so that its other axes can be regridded)
				if (!doneOutPixelAxes(j)) {
					Int inCoord2, inAxisInCoord2;
					inCoords2.findPixelAxis(inCoord2, inAxisInCoord2, j);
					Int inCoord, inAxisInCoord;
					inCoords.findPixelAxis(inCoord, inAxisInCoord,
							pixelAxisMap1[j]);
					// We might end up replacing the same coordinate more than
					// once with this algorithm
					inCoords2.replaceCoordinate(
						inCoords.coordinate(inCoord),
						inCoord2
					);
				}
			}
		}
	}
	delete inPtr;
	inPtr = 0;
	if (itsShowLevel>0) {
		cerr << "Function regrid took " << t0.all() << endl;
	}
}

template<class T>
void ImageRegrid<T>::_regridOneCoordinate (LogIO& os, IPosition& outShape2,
		Vector<Bool>& doneOutPixelAxes,
		MaskedLattice<T>* &finalOutPtr,
		MaskedLattice<T>* &inPtr,
		MaskedLattice<T>* &outPtr,
		CoordinateSystem& outCoords,
		const CoordinateSystem& inCoords,
		Int outPixelAxis,
		const ImageInterface<T>& inImage,
		const IPosition& outShape,
		Bool replicate, uInt decimate,
		Bool outIsMasked, Bool showProgress,
		Bool forceRegrid,
		typename Interpolate2D::Method method,
		Bool verbose)
		{
	Timer t0;
	Double s0 = 0.0;
	// Find world and pixel axis maps

	Vector<Int> pixelAxisMap1, pixelAxisMap2;
	findMaps (inImage.ndim(), pixelAxisMap1, pixelAxisMap2, inCoords, outCoords);

	// Find equivalent world axis
	Int outWorldAxis = outCoords.pixelAxisToWorldAxis(outPixelAxis);
	Int outCoordinate, outAxisInCoordinate;
	Int inCoordinate, inAxisInCoordinate;
	outCoords.findPixelAxis(outCoordinate, outAxisInCoordinate, outPixelAxis);
	Coordinate::Type type = outCoords.type(outCoordinate);

	// Find Coordinate in input image.

	Int inPixelAxis = pixelAxisMap1[outPixelAxis];
	Int inWorldAxis = inCoords.pixelAxisToWorldAxis(inPixelAxis);
	inCoords.findPixelAxis(inCoordinate, inAxisInCoordinate, inPixelAxis);
	if (inCoordinate==-1 || inAxisInCoordinate==-1) {
		ostringstream oss1;
		ostringstream oss2;
		oss1 << outCoords.showType(outCoordinate);
		oss2 << outPixelAxis+1;
		String msg = String("Output axis (") + String(oss2) +
				String(") of coordinate type ") + String(oss1) +
				String("does not have a coordinate in the input "
						"CoordinateSystem");
		ThrowCc(msg);
	}

	// Where are the input and output pixel axes for this  coordinate ?
	// DIrectionCoordinates and LinearCoordinates (holding two axes) are
	// done in one pass.  Others are done axis by axis which is flawed
	// when those axes are coupled (e.g. other axes of LC)

	Vector<Int> outPixelAxes = outCoords.pixelAxes(outCoordinate);
	Vector<Int>  inPixelAxes = inCoords.pixelAxes(inCoordinate);
	Int maxMemoryInMB = 0;

	// Work out if we can do a 2-D regrid. Either a DC or a 2-axis LC
	// (maybe extend to two axes of N-axis LC in the future)

	if ( (type==Coordinate::DIRECTION) ||
			(type==Coordinate::LINEAR &&
					outPixelAxes.nelements()==2 &&
					inPixelAxes.nelements()==2) ) {
		// We will do two pixel axes in this pass

		doneOutPixelAxes(outPixelAxes[0]) = True;
		doneOutPixelAxes(outPixelAxes[1]) = True;

		// Update the incremental output image shape.

		outShape2(outPixelAxes[0]) = outShape(outPixelAxes[0]);
		outShape2(outPixelAxes[1]) = outShape(outPixelAxes[1]);
		ThrowIf(
			outShape2(outPixelAxes[0])==1 && outShape2(outPixelAxes[1])==1,
			"You cannot regrid the Coordinate as it is "
			"of shape [1,1]"
		);
		const IPosition inShape = inPtr->shape();
		Bool shapeDiff =
				(outShape2(outPixelAxes[0]) != inShape(inPixelAxes(0))) ||
				(outShape2(outPixelAxes[1]) != inShape(inPixelAxes(1)));
		// See if we really need to regrid this axis. If the coordinates and shape are the
		// same there is nothing to do apart from swap in and out pointers
		// or copy on last pass

		const Coordinate& cIn = inCoords.coordinate(inCoordinate);
		const Coordinate& cOut = outCoords.coordinate(outCoordinate);
		Bool regridIt = shapeDiff || forceRegrid || !(cIn.near(cOut));
		Bool lastPass = allEQ(doneOutPixelAxes, True);
		//
		if (!regridIt) {
			if (verbose) {
				os << "Input and output shape/coordinate information for "
					<< Coordinate::typeToString(cIn.type())
					<< " axes equal - no regridding needed" << LogIO::POST;
			}
			if (lastPass) {

				// Can't avoid this copy

				LatticeUtilities::copyDataAndMask (os, *finalOutPtr, *inPtr);
			} else {
				outPtr = inPtr;
				inPtr = 0;
			}
			return;
		}
		// Deal with pointers

		if (lastPass) {
			outPtr = finalOutPtr;
		} else {
			outPtr = new TempImage<T>(TiledShape(outShape2), outCoords,
					maxMemoryInMB);
			if (outIsMasked) {
				String maskName("mask0");
				TempImage<T>* tmpPtr = dynamic_cast<TempImage<T>*>(outPtr);
				tmpPtr->makeMask(maskName, True, True, False);
			}
		}
		//
		regridTwoAxisCoordinate  (os, *outPtr, *inPtr, inImage.units(),
				inCoords, outCoords, inCoordinate,
				outCoordinate, inPixelAxes, outPixelAxes,
				pixelAxisMap1, pixelAxisMap2, method,
				replicate, decimate, showProgress);
	} else {

		// Note that will do one pixel axis in this pass

		doneOutPixelAxes(outPixelAxes(outAxisInCoordinate)) = True;

		// Update the incremental output image shape.

		outShape2(outPixelAxes(outAxisInCoordinate)) =
				outShape(outPixelAxes(outAxisInCoordinate));
		const IPosition inShape = inPtr->shape();
		Bool shapeDiff = (outShape2(outPixelAxes(0)) != inShape(inPixelAxes(0)));

		// Get Coordinates.  Set world axis units for input and output
		// coordinates for this pixel
		// axis to be the same.  We can only do this via the
		// CoordinateSystem (or casting)
		// or by breaking polymorphism like we did for DirectionCoordinate.
		// Ho hum.

		Vector<String> inUnits = inCoords.worldAxisUnits();
		Vector<String> outUnits = outCoords.worldAxisUnits();
		outUnits(outWorldAxis) = inUnits(inWorldAxis);
		ThrowIf(
			!outCoords.setWorldAxisUnits(outUnits),
			"Failed to set output CoordinateSystem units"
		);
		const Coordinate& inCoord = inCoords.coordinate(inCoordinate);
		const Coordinate& outCoord = outCoords.coordinate(outCoordinate);

		// See if we really need to regrid this axis.
		//  If the coordinates are the same
		// there is nothing to do apart from swap in and out pointers or
		// copy on last pass

		IPosition t(1, outAxisInCoordinate);
		IPosition excludeAxes = IPosition::otherAxes(outCoord.nPixelAxes(), t);
		Bool regridIt = shapeDiff ||
				forceRegrid || !(inCoord.near(outCoord, excludeAxes.asVector()));
		Bool lastPass = allEQ(doneOutPixelAxes, True);
		//
		if (!regridIt) {
			if (verbose) {
				os << "Input and output shape/coordinate information for "
					<< Coordinate::typeToString(inCoord.type())
					<< " axis equal - no regridding needed" << LogIO::POST;
			}
			if (lastPass) {

				// Can't avoid this copy

				LatticeUtilities::copyDataAndMask (os, *finalOutPtr, *inPtr);
			} else {
				outPtr = inPtr;
				inPtr = 0;
			}
			return;
		}

		// Deal with pointers

		if (lastPass) {
			outPtr = finalOutPtr;
		} else {
			outPtr = new TempImage<T>(TiledShape(outShape2), outCoords,
					maxMemoryInMB);
			if (outIsMasked) {
				String maskName("mask0");
				TempImage<T>* tmpPtr = dynamic_cast<TempImage<T>*>(outPtr);
				tmpPtr->makeMask(maskName, True, True, True, True);
			}
		}

		// Possibly make Frequency reference conversion machine.
		//  We could use the internal
		// machine layer inside the SpectralCoordinate, but making
		// the machine explicitly
		// this way is more general because it allows the ObsInfos
		// and SpectralCoordinates
		// to be different.

		Bool madeIt = False;
		MFrequency::Convert machine;
		if (!itsDisableConversions && type==Coordinate::SPECTRAL) {
			madeIt = CoordinateUtil::makeFrequencyMachine(os, machine,
					inCoordinate, outCoordinate, inCoords, outCoords);
		}

		// Regrid

		if (itsShowLevel>0) {
			cerr << "usemachine=" << madeIt << endl;
		}
		regrid1D (*outPtr, *inPtr, inCoord, outCoord, inPixelAxes,
				outPixelAxes, inAxisInCoordinate, outAxisInCoordinate,
				pixelAxisMap2, method, machine, replicate,
				madeIt, showProgress);
	}
	//
	if (itsShowLevel > 0) {
		s0 += t0.all();
		cerr << "   Function regridOneCoordinate took " << s0 << endl;
	}
}


template<class T>
Bool ImageRegrid<T>::insert (ImageInterface<T>& outImage,
                             const Vector<Double>& outPixel,
                             const ImageInterface<T>& inImage) 
{
   LogIO os(LogOrigin("ImageRegrid", "insert(...)", WHERE));
//
   ThrowIf(
		   outImage.ndim()!=inImage.ndim(),
		   "The input and output images must have the same number of dimensions"
	);
//
   const CoordinateSystem& inCoords = inImage.coordinates();
   const CoordinateSystem& outCoords = outImage.coordinates();
   const uInt nPixelAxes = inCoords.nPixelAxes();
   AlwaysAssert(outImage.shape().nelements()==nPixelAxes,AipsError);
//
   Bool locateByRefPix = outPixel.nelements()==0;
   if (!locateByRefPix) {
      AlwaysAssert(outPixel.nelements()==nPixelAxes,AipsError);
   }
//
   const IPosition& inShape = inImage.shape();
   const IPosition& outShape = outImage.shape();
//
   const Vector<Double>& inRefPix = inCoords.referencePixel();
   const Vector<Double>& outRefPix = outCoords.referencePixel();

// Where are the output blc/trc after placing the input image
// No trimming yet

   IPosition outBlc(nPixelAxes), outTrc(nPixelAxes);
   IPosition inBlc(nPixelAxes), inTrc(nPixelAxes);
   Int coordinate, axisInCoordinate;
   for (uInt i=0; i<nPixelAxes; i++) {
      outCoords.findPixelAxis(coordinate, axisInCoordinate, i);
      if (coordinate==-1 || axisInCoordinate==-1) {
         ostringstream oss;
         oss << "Pixel axis " << i <<
	   " has been removed from the output CoordinateSystem" << endl;
         ThrowCc(String(oss));
      }
      Coordinate::Type type = outCoords.type(coordinate);
      ThrowIf(
    		  type==Coordinate::STOKES && outShape(i)!=inShape(i),
    		  "It is not possible to change the shape of the Stokes axis"
      );
//
      if (locateByRefPix) {
         outBlc(i) = static_cast<Int>(outRefPix(i) - inRefPix(i) + 0.5);
      } else {
         outBlc(i) = static_cast<Int>(outPixel(i) + 0.5);
      }
      outTrc(i) = outBlc(i) + inShape(i) - 1;
//
      inBlc(i) = 0;
      inTrc(i) = inShape(i) - 1;
   }
   if (itsShowLevel>0) {
      cerr << "inBlc, inTrc = " << inBlc << inTrc << endl;
      cerr << "outBlc, outTrc = " << outBlc << outTrc << endl;
   }

// Does the input miss the output entirely ?

   Bool missedIt = True;
   for (uInt i=0; i<nPixelAxes; i++) {
      if ( (outTrc(i)>=0 && outTrc(i)<outShape(i)) ||
           (outBlc(i)>=0 && outTrc(i)<outShape(i)) ||
           (outBlc(i)>=0 && outBlc(i)<outShape(i)) ) {
         missedIt = False;
         break;
      }
   }
   if (itsShowLevel>0) {
      cerr << "missedIt = " << missedIt << endl;
   }
   if (missedIt) return False;

// Now trim blc/trc

   for (uInt i=0; i<nPixelAxes; i++) {
      Int t = outBlc(i);
      outBlc(i) = max(0,outBlc(i));
      Int d = t - outBlc(i);
      inBlc(i) -= d;
//
      t = outTrc(i);
      outTrc(i) = std::min(outShape(i)-1, outTrc(i));
      d = t - outTrc(i);
      inTrc(i) -= d;
   }

   if (itsShowLevel>0) {
      cerr << "After trimming " << endl;
      cerr << "inBlc, inTrc = " << inBlc << inTrc << endl;
      cerr << "outBlc, outTrc = " << outBlc << outTrc << endl;
   }

// Copy the relevant portion

   Slicer inBox(inBlc, inTrc, Slicer::endIsLast);
   Slicer outBox(outBlc, outTrc, Slicer::endIsLast);
   SubImage<T> inSub(inImage, inBox);
   SubImage<T> outSub(outImage, outBox, True);
//
   const Bool outIsMasked = outImage.isMasked() && outImage.hasPixelMask() &&
                            outImage.pixelMask().isWritable();
   if (outIsMasked) {
      LatticeUtilities::copyDataAndMask(os, outSub, inSub);
   } else {
      outSub.copyData(inSub);
   }
//
   return True;
}

template<class T> CoordinateSystem ImageRegrid<T>::makeCoordinateSystem(
	LogIO& os, std::set<Coordinate::Type>& coordsToBeRegridded,
	const CoordinateSystem& cSysTo, const CoordinateSystem& cSysFrom,
	const IPosition& outPixelAxes, const IPosition& inShape,
	Bool giveStokesWarning
) {
	coordsToBeRegridded.clear();
	os << LogOrigin("ImageRegrid<T>", __func__, WHERE);
	const uInt nCoordsFrom = cSysFrom.nCoordinates();
	const uInt nPixelAxesFrom = cSysFrom.nPixelAxes();
	ThrowIf(
		inShape.nelements() > 0 && inShape.nelements() != nPixelAxesFrom,
		"Inconsistent size and csysFrom"
	);

	// Create output CS.  Copy the output ObsInfo over first.

	CoordinateSystem cSysOut(cSysFrom);

	// If specified axes are empty, set to all

	IPosition outPixelAxes2 = outPixelAxes.nelements() == 0
		? IPosition::makeAxisPath(nPixelAxesFrom)
		: outPixelAxes;

	// Loop over coordinates in the From CS

	for (uInt i=0; i<nCoordsFrom; i++) {
		Coordinate::Type typeFrom = cSysFrom.type(i);
		Bool regridIt = False;

		// Stokes is never regridded
		if (typeFrom == Coordinate::STOKES) {
			if (outPixelAxes.size() > 0 && giveStokesWarning) {
				os << LogIO::WARN << "A stokes coordinate cannot be regridded, ignoring" << LogIO::POST;
			}
			continue;
		}
		Vector<Int> pixelAxes = cSysFrom.pixelAxes(i);
		for (uInt k=0; k<pixelAxes.nelements(); k++) {
			if (
				inShape.nelements() == 0
				|| (inShape.nelements() > 0 && inShape[pixelAxes[k]] > 1)
			) {
				for (uInt j=0; j<outPixelAxes2.nelements(); j++) {
					if (pixelAxes[k] == outPixelAxes2(j)) {
						regridIt = True;
					}
				}
			}
		}

		// We are regridding some axis from this coordinate.
		// Replace it from 'To' if we can find it.

		if (regridIt) {

			// Trouble with multiple Coordinates of this type here.

			Int iCoordTo = cSysTo.findCoordinate(typeFrom, -1);
			if (iCoordTo < 0) {
				os << LogIO::WARN << Coordinate::typeToString(typeFrom) << " coordinate is not present "
					<< " in the output coordinate system, so it cannot be regridded"
					<< LogIO::POST;
			}
			else {
				ThrowIf(
					cSysTo.pixelAxes(iCoordTo).nelements()
					!= cSysFrom.pixelAxes(i).nelements(),
					"Wrong number of pixel axes in 'To' CoordinateSystem for "
					"coordinate of type " + cSysTo.showType(iCoordTo)
				);
				cSysOut.replaceCoordinate (cSysTo.coordinate(iCoordTo), i);
				coordsToBeRegridded.insert(typeFrom);
			}
		}
	}
	return cSysOut;
}


template<class T>
void ImageRegrid<T>::regridTwoAxisCoordinate (
	LogIO& os, MaskedLattice<T>& outLattice,
	const MaskedLattice<T>& inLattice,
	const Unit& imageUnit, const CoordinateSystem& inCoords,
	const CoordinateSystem& outCoords,
	Int inCoordinate, Int outCoordinate,
	const Vector<Int> inPixelAxes,
	const Vector<Int> outPixelAxes,
	const Vector<Int> pixelAxisMap1,
	const Vector<Int> pixelAxisMap2,
	typename Interpolate2D::Method method,
	Bool replicate, uInt decimate, Bool showProgress) {
	// Compute output coordinate, find region around this coordinate
	// in input, interpolate. Any output mask is overwritten

	Timer t0, t1, t2, t3, t4;
	Double s0 = 0.0;
	Double s1 = 0.0;
	Double s2 = 0.0;
	Double s3 = 0.0;
	Double s4 = 0.0;
	AlwaysAssert(inPixelAxes.nelements()==2, AipsError);
	AlwaysAssert(outPixelAxes.nelements()==2, AipsError);
	Bool inIsMasked = inLattice.isMasked();
	Bool outIsMasked = outLattice.isMasked() && outLattice.hasPixelMask() &&
			outLattice.pixelMask().isWritable();
	const IPosition inShape = inLattice.shape();
	const IPosition outShape = outLattice.shape();
	const uInt nDim = inLattice.ndim();
	if (itsShowLevel>0) {
		cerr << "Replicate = " << replicate << endl;
		cerr << "inPixelAxes = " << inPixelAxes << endl;
		cerr << "outPixelAxes = " << outPixelAxes << endl;
		cerr << "inIsMasked " << inIsMasked << endl;
		cerr << "outIsMasked " << outIsMasked << endl;
	}
	if (itsShowLevel>0) {
		if (method==Interpolate2D::NEAREST) {
			cerr << "Method is nearest" << endl;
		} else if (method==Interpolate2D::LINEAR) {
			cerr << "Method is linear" << endl;
		} else if (method==Interpolate2D::CUBIC) {
			cerr << "Method is cubic" << endl;
		}
	}
	// We iterate through the output image by tile.  We iterate through
	// each tile by matrix holding the Direction Coordinate axes (in
	// some order).  We have a matrix(i=1:nrows,j=1:ncols).
	//
	// pixelAxisMap1(i) says where pixel axis i in the output image is
	//			 in the input  image
	// pixelAxisMap2(i) says where pixel axis i in the
	//			  input image is in the output image
	// pixelAxes(0)    says where Lon  is in image (in or out)
	// pixelAxes(1)    says where Lat  is in image (in or out)
	// xOutAxis        is the first direction axis in the output image
	//				 (associated with i)
	// yOutAxis        is the second direction axis in the output image
	//				 (associated with j)
	// xInCorrAxis     is the corresponding axis to xOutAxis in the input image
	// yInCorrAxis     is the corresponding axis to yOutAxis in the input image
	// xInAxis         is the first direction axis in the input image
	// yInAxis         is the second direction axis in the input image
	// xOutCorrAxis    is the corresponding axis to xInAxis in the output image
	// yOutCorrAxis    is the corresponding axis to yInAxis in the output image

	// Example:
	//   Regrid ra/dec axes with input order ra/dec/freq and output order
	// 			freq/dec/ra
	//
	//   input  image shape = [20, 30, 40] (ra/dec/freq)
	//   output image shape = [40, 90, 60] (freq/dec/ra) - we are making ra/dec
	//				 shape 3x input
	//
	//   outPixelAxes = [2,1] = [lon,lat]
	//   The cursor matrix is of shape [nrow,ncol] = [90,60]
	//       xOutAxis = 1   (dec)
	//       yOutAxis = 2   (ra)
	//    xInCorrAxis = 1   (dec)
	//    yInCorrAxis = 0   (ra)
	//        xInAxis = 0   (ra)
	//        yInAxis = 1   (dec)
	//   xOutCorrAxis = 2   (ra)
	//   yOutCorrAxis = 1   (dec)

	const uInt xOutAxis = min(outPixelAxes(0), outPixelAxes(1));
	const uInt yOutAxis = max(outPixelAxes(0), outPixelAxes(1));
	uInt xInCorrAxis = pixelAxisMap1[xOutAxis];
	uInt yInCorrAxis = pixelAxisMap1[yOutAxis];
	const uInt xInAxis = min(inPixelAxes(0), inPixelAxes(1));
	const uInt yInAxis = max(inPixelAxes(0), inPixelAxes(1));
	uInt xOutCorrAxis = pixelAxisMap2[xInAxis];
	uInt yOutCorrAxis = pixelAxisMap2[yInAxis];
	// Make navigator and iterator for output data and mask.  It is vital that
	// the "niceShape" is the same for both iterators.  Because the mask and
	// lattice are both TempLattices, one might be on disk, one in core.
	// Hence we pick one nice shape and use it on both iterators, although
	// it may be suboptimal for one of the lattices.

	//IPosition niceShape = outLattice.niceCursorShape();
	// Temporary fix for AIT/SIN regriding for full sky images
	// Hold a plane in memory
	IPosition niceShape=outLattice.shape();
	niceShape=1;
	niceShape(xOutAxis)=outLattice.shape()(xOutAxis);
	niceShape(yOutAxis)=outLattice.shape()(yOutAxis);

	LatticeStepper outStepper(outShape, niceShape, LatticeStepper::RESIZE);

	LatticeIterator<T> outIter(outLattice, outStepper);

	if (itsShowLevel>0) {
		cerr << "xOutAxis, yOutAxis = " << xOutAxis << ", " << yOutAxis << endl;
		cerr << "xInCorrAxis, yInCoorrAxis = " << xInCorrAxis << ", " <<
				yInCorrAxis << endl;
		cerr << "xInAxis, yInAxis = " << xInAxis << ", " << yInAxis << endl;
		cerr << "xOutCorrAxis, yOutCoorrAxis = " << xOutCorrAxis << ", " <<
				yOutCorrAxis << endl;
		//
		cerr << "cursor shape = " << niceShape << endl;
		cerr << "shape in, shape out" << inShape << outShape << endl;
	}

	// Deal with mask.  Stepper will make a reference copy of the mask

	LatticeIterator<Bool>* outMaskIterPtr = 0;
	if (outIsMasked) {
		Lattice<Bool>& outMask = outLattice.pixelMask();
        outMaskIterPtr = new LatticeIterator<Bool>(outMask, outStepper);
	}
	// These tell us which chunk of input data we need to service each
	// iteration through the output image

	IPosition inChunkBlc(nDim);
	IPosition inChunkTrc(nDim);

	// These tell us which 2D piece of inChunk to read.
	// This is  what we regrid from.

	IPosition inChunkBlc2D(nDim);
	IPosition inChunkTrc2D(nDim);

	// Coordinate conversion vectors

	Vector<Double> world(2), inPixel(2), outPixel(2);
	Vector<Double> pixelScale(nDim);
	pixelScale = 1.0;
	pixelScale(xInAxis) = Float(outShape(xOutCorrAxis)) /
			Float(inShape(xInAxis));
	pixelScale(yInAxis) = Float(outShape(yOutCorrAxis)) /
			Float(inShape(yInAxis));
	if (itsShowLevel > 0) {
		cerr << "pixelScale = " << pixelScale << endl;
	}

	// 2D interpolator

	Interpolate2D interp(method);
	// Various things needed along the way

	Vector<Double> pix2DPos2(2);
	IPosition outPos4, outPos3, outPos2, inPos;
	Double minInX, minInY, maxInX, maxInY;

	// Generate full plane of coordinates mapping each output
	// pixel to an input pixel

	IPosition shapeGrid(3, outShape(xOutAxis), outShape(yOutAxis), 2);
	its2DCoordinateGrid.resize(shapeGrid);
	its2DCoordinateGridMask.resize(outShape(xOutAxis), outShape(yOutAxis));
	Bool allFailed = False;
	Bool missedIt = False;

	// Either generate the coordinate grid or use what the user has supplied
	t1.mark();
	if (itsUser2DCoordinateGrid.nelements() > 0 &&
			itsUser2DCoordinateGridMask.nelements() > 0) {
		if (itsNotify) {
			os << "Using user set DirectionCoordinate grid" << LogIO::POST;
		}
		//
		{
			IPosition shp1 = its2DCoordinateGrid.shape();
			IPosition shp2 = itsUser2DCoordinateGrid.shape();
			if (shp1.isEqual(shp2)) {
				its2DCoordinateGrid = itsUser2DCoordinateGrid;
			} else {
				os << "User set 2D coordinate grid has the wrong shape" <<
						LogIO::POST;
			}
		}
		{
			IPosition shp1 = its2DCoordinateGridMask.shape();
			IPosition shp2 = itsUser2DCoordinateGridMask.shape();
			if (shp1.isEqual(shp2)) {
				its2DCoordinateGridMask = itsUser2DCoordinateGridMask;
			} else {
				os << "User set 2D coordinate grid mask has the wrong shape" <<
						LogIO::POST;
			}
		}
		//
		allFailed = False;
		missedIt = False;
	}
	else {
		allFailed = False;
		missedIt = True;
		IPosition outPosFull(outLattice.ndim(),0);
		if (replicate) {
			make2DCoordinateGrid (its2DCoordinateGrid, minInX, minInY, maxInX,
					maxInY,
					pixelScale, xInAxis, yInAxis, xOutAxis, yOutAxis,
					xInCorrAxis, yInCorrAxis, xOutCorrAxis,
					yOutCorrAxis,
					outPosFull, outShape);
			missedIt = False;
			allFailed = False;
			its2DCoordinateGridMask.set(True);
		}
		else {
			make2DCoordinateGrid (os, allFailed, missedIt, minInX, minInY, maxInX,
					maxInY,
					its2DCoordinateGrid, its2DCoordinateGridMask,
					inCoords, outCoords, inCoordinate, outCoordinate,
					xInAxis, yInAxis, xOutAxis,
					yOutAxis,
					inPixelAxes, outPixelAxes, inShape, outPosFull,
					outShape, decimate);
		}
	}
	s1 += t1.all();
	if (missedIt || allFailed) {
		outLattice.set(0.0);
		if (outIsMasked) {
			outLattice.pixelMask().set(False);
		}
        delete outMaskIterPtr;
        return;
	}
	// Progress meter

	ProgressMeter* pProgressMeter = 0;
	if (showProgress) {
		Double nMin = 0.0;
		Double nMax = Double(outLattice.shape().product());
		ostringstream oss;
		oss << "Axes " << outPixelAxes + 1 << " : Pixels Regridded";
		pProgressMeter = new ProgressMeter(nMin, nMax, String(oss),
				String("Regridding"),
				String(""), String(""),
				True,
				max(1,Int(nMax/20)));
	}
	// Find scale factor for Jy/pixel images

	Double scale = findScaleFactor(imageUnit, inCoords, outCoords,
			inCoordinate, outCoordinate, os);

	// Iterate through output image

	t2.mark();
	Double iPix = 0.0;
	Int i2;
	for (outIter.reset(); !outIter.atEnd(); outIter++) {
		const IPosition& outCursorShape = outIter.cursorShape();
		const IPosition& outPos = outIter.position();
		if (itsShowLevel>0) {
			cerr << endl;
			cerr << "Output lattice iterator position = " <<  outPos << endl;
			cerr << "Shape of cursor = " << outIter.cursor().shape() << endl;
		}

		// Now get a chunk of input data which we will access over and over
		// as we interpolate it.

		missedIt = True;
		allFailed = True;
		t3.mark();
		findXYExtent (missedIt, allFailed, minInX, minInY, maxInX, maxInY,
				its2DCoordinateGrid,
				its2DCoordinateGridMask, xInAxis, yInAxis, xOutAxis,
				yOutAxis, outPos,
				outCursorShape, inShape);
		s3 += t3.all();
		if (itsShowLevel>0) {
			cerr << "missedIt, allFailed, minInX, maxInX, minInY, maxInY = " <<
					missedIt << ", " << allFailed << ", " <<
					minInX << ", " << maxInX << ", " <<  minInY << ", " <<
					maxInY << endl;
		}
		if (missedIt || allFailed) {
			outIter.rwCursor().set(0.0);
			if (outIsMasked) outMaskIterPtr->rwCursor().set(False);
			if (showProgress) {
				pProgressMeter->update(iPix);
				iPix += Double(outCursorShape.product());
			}
		}
		else {
			// For the non-regrid axes, the input and output shapes,
			// and hence positions, are the same.
			// pixelAxisMap2(i) says where pixel axis i in the input
			// image is in the output image.

			for (uInt k=0; k<nDim; k++) {
				inChunkBlc(k) = outPos[pixelAxisMap2[k]];
				inChunkTrc(k) = outIter.endPosition()(pixelAxisMap2[k]);
			}

			// Now overwrite the blc/trc for the regrid axes.
			// The interpolation schemes (Interpolate2D) use
			// a small grid about the pixel of interest I happen
			// to know that allowing 3
			// pixels on either side is enough.
			// If this should change, the interpolation
			// would return False at the edges
			i2 = static_cast<Int>(floor(minInX)) - 3;
			inChunkBlc(xInAxis) = max(0,i2);
			i2 = static_cast<Int>(floor(minInY)) - 3;
			inChunkBlc(yInAxis) = max(0,i2);
			i2 = static_cast<Int>(ceil(maxInX)) + 3;
			inChunkTrc(xInAxis) = min(inShape(xInAxis)-1,i2);
			i2 = static_cast<Int>(ceil(maxInY)) + 3;
			inChunkTrc(yInAxis) = min(inShape(yInAxis)-1,i2);
			IPosition inChunkShape = inChunkTrc - inChunkBlc + 1;
			if (itsShowLevel>0) {
				cerr << "inChunkShape = " << inChunkShape << endl;
				cerr << "inChunkBlc, inChunkTrc " << inChunkBlc << inChunkTrc <<
						endl;
			}

			// Get the input data and mask

			Array<T> inDataChunk = inLattice.getSlice(inChunkBlc, inChunkShape);
			Array<Bool>* inMaskChunkPtr = 0;
			if (inIsMasked) {
				inMaskChunkPtr =
						new Array<Bool>(inLattice.getMaskSlice(inChunkBlc,
								inChunkShape));
			}
			// Iterate through the output cursor by Matrices,
			// each holding a Direction plane.
			// This gets us just a few percent speed up over
			// iterating through pixel by pixel.

			ArrayLattice<T> outCursor(outIter.rwCursor());    // reference copy
			t4.mark();
			ThrowIf(
				inChunkShape(xInAxis)==1 && inChunkShape(yInAxis)==1,
				"Cannot regrid degenerate DirectionCoordinate plane"
			);
			ThrowIf(
				inChunkShape(xInAxis)==1 || inChunkShape(yInAxis)==1,
				"Cannot yet handle DirectionCoordinate plane with one "
				"degenerate axis"
			);
			regrid2DMatrix(outCursor, outMaskIterPtr, interp, pProgressMeter,
					iPix, nDim,
					xInAxis, yInAxis, xOutAxis, yOutAxis, scale,
					inIsMasked, outIsMasked,
					outPos, outCursorShape, inChunkShape, inChunkBlc,
					pixelAxisMap2,
					inDataChunk, inMaskChunkPtr, its2DCoordinateGrid,
					its2DCoordinateGridMask);


			s4 += t4.all();
		}
		if (outIsMasked) {
			(*outMaskIterPtr)++;
		}
	}
	if (outIsMasked) delete outMaskIterPtr;
	if (showProgress) delete pProgressMeter;
	if (itsShowLevel > 0) {
		s2 += t2.all();
		s0 += t0.all();
		cerr << "         Function regrid2DMatrix took "  << s4 << endl;
		cerr << "         Function findXYExtent took "  << s3 << endl;
		cerr << "      Iterating and regridding took "  << s2 << endl;
		if (replicate) {
			cerr << "      Function make2DCoordinateGrid (replication) took " <<
					s1 << endl;
		} else {
			cerr << "      Function make2DCoordinateGrid (regridding)  took " <<
					s1 << endl;
		}
		cerr << "    Function regrid2D took " << s0  << endl;
	}
}



template<class T>
void ImageRegrid<T>::make2DCoordinateGrid(LogIO& os, Bool& allFailed, Bool&missedIt,
					  Double& minInX, Double& minInY, 
					  Double& maxInX, Double& maxInY,  
					  Cube<Double>& in2DPos,
					  Matrix<Bool>& succeed,
					  const CoordinateSystem& inCoords,
					  const CoordinateSystem& outCoords,
                                          Int inCoordinate, Int outCoordinate,
					  uInt xInAxis, uInt yInAxis,
					  uInt xOutAxis, uInt yOutAxis,
					  const IPosition& inPixelAxes,
					  const IPosition& outPixelAxes,
					  const IPosition& inShape,
					  const IPosition& outPos,
					  const IPosition& outCursorShape,
					  uInt decimate) {
//
// in2DPos says where the output pixel (i,j) is located in the input image
//
  Vector<Double> world(2), inPixel(2), outPixel(2);
  minInX =  100000000.0;
  minInY =  100000000.0;
  maxInX = -100000000.0;
  maxInY = -100000000.0;
  allFailed = True;
  Bool ok1=False, ok2=False;
  MVDirection inMVD, outMVD;
//
  uInt ni = outCursorShape(xOutAxis);
  uInt nj = outCursorShape(yOutAxis);
  
// Where in the Direction Coordinates are X and Y ?
// pixelAxes(0) says where Lon is in DirectionCoordinate
// pixelAxes(1) says where Lat is in DirectionCoordinate
// The X axis is always the direction axis that appears first in the image
//
  uInt inXIdx = 0;         // [x,y] = [lon,lat]
  uInt inYIdx = 1;
  if (inPixelAxes(0)==Int(yInAxis)) {
    inXIdx = 1;            // [x,y] = [lat,lon]
    inYIdx = 0;
  };
//
  uInt outXIdx = 0;         
  uInt outYIdx = 1;
  if (outPixelAxes(0)==Int(yOutAxis)) {
    outXIdx = 1;         
    outYIdx = 0;
  };
//
  Matrix<Bool> doneIt(ni,nj);
  doneIt = False;
  //
  if (itsShowLevel > 0) {                 
    cerr << "inXIdx, inYIdx = " << inXIdx << ", " << inYIdx << endl;
    cerr << "outXIdx, outYIdx = " << outXIdx << ", " << outYIdx << endl;
  }
//
  uInt nOutI = 0;
  uInt nOutJ = 0;
  uInt iInc = 1;
  uInt jInc = 1;
  if (decimate > 1) {
    Int nOut = ni / decimate;
    if (nOut <= 1) {
      cerr << "Illegal decimation factor for X; setting to unity" << endl;
      nOut = ni;
    }   
    iInc = ni / (nOut- 1);
    //
    nOut = nj / decimate;
    if (nOut <= 1) {
      cerr << "Illegal decimation factor for Y; setting to unity" << endl;
      nOut = nj;
    }   
    jInc = nj / (nOut - 1);
    if (iInc < 1) {
      cerr << "Illegal decimation increment computed for X; "
	"setting to unity" << endl;
      iInc = 1;
    }
    if (jInc < 1) {
      cerr << "Illegal decimation increment computed for Y; "
	"setting to unity" << endl;
      jInc = 1;
    }
    //
    if (iInc==1 && jInc==1) decimate = 0;
    else {
      for (uInt j=0; j<nj; j+=jInc,nOutJ++) {;}
      for (uInt i=0; i<ni; i+=iInc,nOutI++) {;}
    };
    //
    if (itsShowLevel > 0) {                 
      cerr << "decimate, nOutI, nOutJ = " << decimate << ", " << nOutI <<
	", " << nOutJ << endl;
      cerr << "iInc, jInc = " << iInc << ", " << jInc << endl;
    }
  }
//
  Matrix<Double> iInPos2D(nOutI,nOutJ);
  Matrix<Double> jInPos2D(nOutI,nOutJ);
  Matrix<Bool> ijInMask2D(nOutI,nOutJ);

// Are we dealing with a DirectionCoordinate or LinearCoordinate ?

  Bool isDir = inCoords.type(inCoordinate)==Coordinate::DIRECTION && 
                outCoords.type(outCoordinate)==Coordinate::DIRECTION;
  DirectionCoordinate inDir, outDir;
  LinearCoordinate inLin, outLin;
  Bool useMachine = False;
  MDirection::Convert machine;
  if (isDir) {
      inDir = inCoords.directionCoordinate(inCoordinate);
      outDir = outCoords.directionCoordinate(outCoordinate);

// Set units to degrees

      Vector<String> units(2);
      units.set("deg");
      ThrowIf(
    	!inDir.setWorldAxisUnits(units),
        "Failed to set input DirectionCoordinate units to degrees"
    	);
      ThrowIf(
    	!outDir.setWorldAxisUnits(units),
        "Failed to set output DirectionCoordinate units to degrees"
       );

// Possibly make Direction reference conversion machine.  We could use the internal
// machine layer inside the DirectionCoordinate, but making the machine explicitly
// this way is more general because it allows the ObsInfo to be different.

      if (!itsDisableConversions) {
         useMachine = CoordinateUtil::makeDirectionMachine(os, machine, inDir, outDir, 
                                                           inCoords.obsInfo(),
                                                           outCoords.obsInfo());
      }
  } else {
     inLin = inCoords.linearCoordinate(inCoordinate);
     outLin = outCoords.linearCoordinate(outCoordinate);

// Set units to same thing

      const Vector<String>& units = inLin.worldAxisUnits().copy();
      ThrowIf(
    	!outLin.setWorldAxisUnits(units),
         "Failed to set output and input LinearCoordinate axis units the same"
    	);
  }
//
  if (itsShowLevel>0) {
     cerr << "usemachine=" << useMachine << endl;
  }

// If decimating, compute a sparse grid of coordinates and then
// interpolate the others.
// Otherwise, compute all coordinates (very expensive)
// This approach is going to cause pixels along the right and top edges
// to be masked as the coarse grid is unlikely to finish exactly
// on the lattice edge

  Timer t0;
  uInt ii = 0;
  uInt jj = 0;
  for (uInt j=0; j<nj; j+=jInc,jj++) {
	  ii = 0;
	  for (uInt i=0; i<ni; i+=iInc,ii++) {
		  outPixel(outXIdx) = i + outPos[xOutAxis];
		  outPixel(outYIdx) = j + outPos[yOutAxis];

		  // Do coordinate conversions (outpixel to world to inpixel)
		  // for the axes of interest

		  if (useMachine) {                             // must be Direction
			  ok1 = outDir.toWorld(outMVD, outPixel);
			  ok2 = False;
			  if (ok1) {
				  inMVD = machine(outMVD).getValue();
				  ok2 = inDir.toPixel(inPixel, inMVD);
			  };
		  } else {
			  if (isDir) {
				  ok1 = outDir.toWorld(world, outPixel);
				  ok2 = False;
				  if (ok1) ok2 = inDir.toPixel(inPixel, world);
			  } else {
				  ok1 = outLin.toWorld(world, outPixel);
				  ok2 = False;
				  if (ok1) ok2 = inLin.toPixel(inPixel, world);
			  }
		  };
		  //
		  if (!ok1 || !ok2) {
			  succeed(i,j) = False;
			  if (decimate>1) ijInMask2D(ii,jj) = False;
		  } else {

			  // This gives the 2D input pixel coordinate (relative to
			  // the start of the full Lattice)
			  // to find the interpolated result at.  (,,0) pertains to
			  // inX and (,,1) to inY
			  in2DPos(i,j,0) = inPixel(inXIdx);
			  in2DPos(i,j,1) = inPixel(inYIdx);
			  allFailed = False;
			  succeed(i,j) = True;
			  //
			  if (decimate <= 1) {
				  minInX = min(minInX,inPixel(inXIdx));
				  minInY = min(minInY,inPixel(inYIdx));
				  maxInX = max(maxInX,inPixel(inXIdx));
				  maxInY = max(maxInY,inPixel(inYIdx));
			  } else {
				  iInPos2D(ii,jj) = inPixel(inXIdx);
				  jInPos2D(ii,jj) = inPixel(inYIdx);
				  ijInMask2D(ii,jj) = True;
			  };
		  };
	  };
  };
  if (itsShowLevel > 0) {
    cerr << "nII, nJJ= " << ii << ", " << jj << endl;
    cerr << "Sparse grid took " << t0.all() << endl;
  };
  
  // Bi-linear Interpolation of x and y coordinates in the sparse grid 
  // The coordinates that were already computed get interpolated as 
  // well (should be identical).
  if (decimate > 1) {
    Timer t1;
    //
    Interpolate2D interp(Interpolate2D::LINEAR);
    Vector<Double> pos(2);
    Double resultI=0.0, resultJ=0.0;

    ArrayAccessor<Bool, Axis<0> > sucp0;
    ArrayAccessor<Bool, Axis<1> > sucp1(succeed);
    ArrayAccessor<Double, Axis<0> > in2Dp0;
    ArrayAccessor<Double, Axis<1> > in2Dp1(in2DPos);
    for (uInt j=0; j<nj; j++) {
      pos[1] = Double(j) / Double(jInc); 
      sucp0 = sucp1;
      in2Dp0 = in2Dp1;
      for (uInt i=0; i<ni; i++) {
	pos[0] = Double(i) / Double(iInc); 
	ok1 = interp.interp(resultI, resultJ, pos,
			    iInPos2D, jInPos2D, ijInMask2D);
	if (ok1) {
	  *in2Dp0 = resultI;
	  in2Dp0.next(AxisN(2)) = resultJ;
	  *sucp0 = True;
	  allFailed = False;
	  //
	  minInX = minInX < resultI ? minInX : resultI;
	  minInY = minInY < resultJ ? minInY : resultJ;
	  maxInX = maxInX > resultI ? maxInX : resultI;
	  maxInY = maxInY > resultJ ? maxInY : resultJ;
	} else *sucp0 = False;
	sucp0++;
	in2Dp0++;
      };
      sucp1++;
      in2Dp1++;
    };
    if (itsShowLevel > 0) {
      cerr << "Interpolated grid took " << t1.all() << endl;
    };
  };
  
  
  // Does the output map to anywhere on the input ?
  
  missedIt = False;
  if (!allFailed) {
    Double ijMin = -0.5;
    Double iMax = inShape(xInAxis) - 0.5;
    Double jMax = inShape(yInAxis) - 0.5;
    //
    missedIt  = (minInX<ijMin && maxInX<ijMin)  ||
      (minInX>iMax && maxInX>iMax) ||
      (minInY<ijMin && maxInY<ijMin)  ||
      (minInY>jMax && maxInY>jMax);
  }
  if (itsShowLevel>0) {
    cerr << "allFailed, missedIt  = " << allFailed << ", " << missedIt <<
      endl;
  }
}


template<class T>
void ImageRegrid<T>::make2DCoordinateGrid (Cube<Double>& in2DPos,
                                           Double& minInX, Double& minInY, 
                                           Double& maxInX, Double& maxInY,  
                                           const Vector<Double>& pixelScale,
                                           uInt xInAxis, uInt yInAxis,
                                           uInt xOutAxis, uInt yOutAxis,
                                           uInt, uInt,
                                           uInt xOutCorrAxis, uInt yOutCorrAxis,
                                           const IPosition& outPos, 
                                           const IPosition& outCursorShape)

{
   Double oX = -0.5 + (1.0/2/pixelScale(xInAxis));
   Double oY = -0.5 + (1.0/2/pixelScale(yInAxis));
//
   uInt ni = outCursorShape(xOutAxis);
   uInt nj = outCursorShape(yOutAxis);
//
   if (xOutAxis == xOutCorrAxis) {                      

// First output Direction axis corresponds to the first input Direction axis

      Double t0 = (outPos[xOutCorrAxis] / pixelScale(xInAxis)) + oX;
      Double t1 = (outPos[yOutCorrAxis] / pixelScale(yInAxis)) + oY;
//
      for (uInt j=0; j<nj; j++) {
         for (uInt i=0; i<ni; i++) {
           in2DPos(i,j,0) = Double(i) / pixelScale(xInAxis) + t0;
           in2DPos(i,j,1) = Double(j) / pixelScale(yInAxis) + t1;
         }
      }  
   } else if (xOutAxis == yOutCorrAxis) {

// First output Direction axis corresponds to the second input Direction axis

      Double t0 = (outPos[yOutCorrAxis] / pixelScale(xInAxis)) + oX;
      Double t1 = (outPos[xOutCorrAxis] / pixelScale(yInAxis)) + oY;
//
      for (uInt j=0; j<nj; j++) {
         for (uInt i=0; i<ni; i++) {
           in2DPos(i,j,0) = Double(j) / pixelScale(xInAxis) + t0;
           in2DPos(i,j,1) = Double(i) / pixelScale(yInAxis) + t1;
         }
      }  
   } else {
      throw(AipsError("Big trouble in make2CoordinateGrid"));
   }
//
   minInX = in2DPos(0,0,0);
   minInY = in2DPos(0,0,1);
   maxInX = in2DPos(ni-1,nj-1,0);
   maxInY = in2DPos(ni-1,nj-1,1);
}


template<class T>
void ImageRegrid<T>::findXYExtent (Bool& missedIt, Bool& allFailed,
                                   Double& minInX, Double& minInY, 
                                   Double& maxInX, Double& maxInY,  
                                   Cube<Double>& in2DPos,
                                   const Matrix<Bool>& succeed,
                                   uInt xInAxis, uInt yInAxis,
                                   uInt xOutAxis, uInt yOutAxis,
                                   const IPosition& outPos,
                                   const IPosition& outCursorShape,
                                   const IPosition& inShape) 
//
// Finds the blc and trc (absolute pixel coordinates) of the INPUT image 
// for the OUTPUT chunk being regridded.
//
{
   uInt ni = outCursorShape(xOutAxis);
   uInt nj = outCursorShape(yOutAxis);
               
// outPos is the BLC of this chunk in the output lattice
 
   uInt iOff = outPos[xOutAxis];
   uInt jOff = outPos[yOutAxis];
//
   IPosition blc(2);
   blc(0) = iOff;
   blc(1) = jOff;
   IPosition trc(2);
   trc(0) = iOff + ni - 1;
   trc(1) = jOff + nj - 1;
   IPosition minPos, maxPos;
//
   IPosition s = succeed.shape();
   if (blc(0)==0 && blc(1)==0 && trc(0)==(s(0)-1) && trc(1)==(s(1)-1)) {
// Short cut if we are going to use the full matrix

       allFailed = minmax (minInX, maxInX, minInY, maxInY, 
                           in2DPos.xyPlane(0), in2DPos.xyPlane(1), succeed);

   } else {
// Pull out the relevant piece

      allFailed = minmax (minInX, maxInX, minInY, maxInY, 
                          in2DPos.xyPlane(0)(blc,trc), 
                          in2DPos.xyPlane(1)(blc,trc), succeed(blc,trc));
   }
//
   if (!allFailed) {
      Double ijMin = -0.5;
      Double iMax = inShape(xInAxis) - 0.5;
      Double jMax = inShape(yInAxis) - 0.5;
      missedIt  = (minInX<ijMin && maxInX<ijMin)  ||
                  (minInX>iMax && maxInX>iMax) ||
                  (minInY<ijMin && maxInY<ijMin)  ||
                  (minInY>jMax && maxInY>jMax);
   } else {
      missedIt = True;
   }
}

template<class T>
void ImageRegrid<T>::regrid2DMatrix(Lattice<T>& outCursor, 
                                    LatticeIterator<Bool>*& outMaskIterPtr,
                                    const Interpolate2D& interp,
                                    ProgressMeter*& pProgressMeter,
                                    Double& iPix,
                                    uInt nDim, 
                                    uInt xInAxis, uInt yInAxis, 
                                    uInt xOutAxis, uInt yOutAxis, 
                                    Double scale,
                                    Bool inIsMasked, Bool outIsMasked,
                                    const IPosition& outPos,
                                    const IPosition& outCursorShape,
                                    const IPosition& inChunkShape,
                                    const IPosition& inChunkBlc,
                                    const IPosition& pixelAxisMap2,
                                    Array<T>& inDataChunk,
                                    Array<Bool>*& inMaskChunkPtr,
                                    const Cube<Double>& pix2DPos,
                                    const Matrix<Bool>& succeed) {
  // 
  // Iterate through a stack of DirectionCoordinate planes and interpolate them
  //
  // Setup Navigator and tell it which axes are the Direction ones in case
  // of other degenerate axes
  
  IPosition axisPath;
  IPosition outCursorAxes(2, xOutAxis, yOutAxis);
  IPosition outCursorIterShape(2, outCursorShape(xOutAxis),
			       outCursorShape(yOutAxis));
  LatticeStepper outCursorIterStepper(outCursor.shape(), outCursorIterShape,
				      outCursorAxes, axisPath);
  LatticeIterator<T> outCursorIter(outCursor, outCursorIterStepper);
  //
  LatticeIterator<Bool>* outMaskCursorIterPtr = 0;
  Lattice<Bool>* outMaskCursorPtr = 0;
  if (outIsMasked) {
    outMaskCursorPtr = new ArrayLattice<Bool>(outMaskIterPtr->rwCursor());
    outMaskCursorIterPtr = new LatticeIterator<Bool>(*outMaskCursorPtr,
						     outCursorIterStepper);
  }
  //
  IPosition inChunkBlc2D(nDim, 0);
  IPosition inChunkTrc2D(nDim);
  inChunkTrc2D = inChunkShape - 1;
  //
  IPosition inChunk2DShape(2);
  inChunk2DShape[0] = inChunkTrc2D[xInAxis] - inChunkBlc2D[xInAxis] + 1;
  inChunk2DShape[1] = inChunkTrc2D[yInAxis] - inChunkBlc2D[yInAxis] + 1;
  //
  Vector<Double> pix2DPos2(2);
  IPosition outPos3;
  Bool interpOK;
  T result(0);
  //
  for (outCursorIter.reset(); !outCursorIter.atEnd(); outCursorIter++) {
    
    // outCursorIter.position is the location of the BLC of the current matrix
    // within the current
    // cursor (tile) of data. outPos3 is the location of the BLC of the
    // current matrix within
    // the full lattice
    outPos3 = outPos + outCursorIter.position();
    
    // Fish out the 2D piece of the inChunk relevant to this plane of the cursor
    for (uInt k=0; k<nDim; k++) {
      if (k!=xInAxis&& k!=yInAxis) {
	inChunkBlc2D[k] = outPos3[pixelAxisMap2[k]] - inChunkBlc[k];
	inChunkTrc2D[k] = inChunkBlc2D[k];
      }; 
    };
    //
    const Matrix<T>& inDataChunk2D =
      inDataChunk(inChunkBlc2D, inChunkTrc2D).reform(inChunk2DShape);
    Matrix<Bool>* inMaskChunk2DPtr = 0;
    if (inIsMasked) {
      inMaskChunk2DPtr = 
	new Matrix<Bool>((*inMaskChunkPtr)
			 (inChunkBlc2D, inChunkTrc2D).
			 reform(inChunk2DShape));
    };

    // Now work through each output pixel in the data Matrix and do the
    // interpolation
    uInt nCol = outCursorIter.matrixCursor().ncolumn();
    uInt nRow = outCursorIter.matrixCursor().nrow();
    Matrix<T> &outMCursor = outCursorIter.rwMatrixCursor();
    Matrix<Bool> *outMaskMCursor = 0;
    if (outIsMasked) {
      outMaskMCursor = &(outMaskCursorIterPtr->rwMatrixCursor());
    };
    
    ArrayAccessor<Bool, Axis<0> > sucp0;
    ArrayAccessor<Bool, Axis<1> > sucp1(succeed);
    ArrayAccessor<T, Axis<0> > outMp0;
    ArrayAccessor<T, Axis<1> > outMp1(outMCursor);
    ArrayAccessor<Bool, Axis<0> > outMaskMp0;
    ArrayAccessor<Bool, Axis<1> > outMaskMp1;
    if (outIsMasked) outMaskMp1.init(*outMaskMCursor);
    uInt dpix2DPos = &pix2DPos(0,0,1) - &pix2DPos(0,0,0);

    for (uInt j=0; j<nCol; j++) {
      if (outIsMasked) outMaskMp0 = outMaskMp1;
      sucp0 = sucp1;
      outMp0 = outMp1;
      for (uInt i=0; i<nRow; i++) {
	if (! *sucp0) {
	  *outMp0 = 0.0;
	  if (outIsMasked) *outMaskMp0 = False;
	} else {
	  
	  // Now do the interpolation. pix2DPos(i,j,) is the absolute input
	  // pixel coordinate in the input lattice for the
	  // current output pixel.
	  uInt ii = outPos3[xOutAxis] + i;
	  uInt jj = outPos3[yOutAxis] + j;
	  const Double *pix2Dp = &pix2DPos(ii,jj,0);
	  pix2DPos2[0] = *pix2Dp - inChunkBlc[xInAxis];
	  pix2DPos2[1] = *(pix2Dp + dpix2DPos) - inChunkBlc[yInAxis];
	  if (inIsMasked) {                     
	    interpOK = interp.interp(result, pix2DPos2, inDataChunk2D,
				     *inMaskChunk2DPtr);
	  } else {
	    interpOK = interp.interp(result, pix2DPos2, inDataChunk2D);
	  };
	  if (interpOK) {
	    *outMp0 = scale * result;
	    if (outIsMasked) *outMaskMp0 = True; 
	  } else {
	    *outMp0 = 0.0;
	    if (outIsMasked) *outMaskMp0 = False; 
	  };
	};
	sucp0++;
	outMp0++;
	outMaskMp0++;
      };
      sucp1++;
      outMp1++;
      outMaskMp1++;
    };
    //
    if (pProgressMeter) {
      pProgressMeter->update(iPix); 
      iPix += nCol*nRow;
    };
    //
    if (outIsMasked) (*outMaskCursorIterPtr)++;
    if (inIsMasked) delete inMaskChunk2DPtr;
  };
  //
  if (inIsMasked) delete inMaskChunkPtr;
  if (outIsMasked) {
    delete outMaskCursorIterPtr;
    delete outMaskCursorPtr;
  };
}

template<class T>
void ImageRegrid<T>::regrid1D (MaskedLattice<T>& outLattice,
                               const MaskedLattice<T>& inLattice,
                               const Coordinate& inCoord,
                               const Coordinate& outCoord,
                               const Vector<Int>& inPixelAxes,
                               const Vector<Int>& outPixelAxes,
                               Int inAxisInCoordinate,
                               Int outAxisInCoordinate,
                               const Vector<Int> pixelAxisMap,
                               typename Interpolate2D::Method method,
                               MFrequency::Convert& machine,
                               Bool replicate,
                               Bool useMachine, Bool showProgress)

//
// Any output mask is overwritten
//
{
   const Bool inIsMasked = inLattice.isMasked();
   const Bool outIsMasked = outLattice.isMasked() &&
     outLattice.hasPixelMask() &&
     outLattice.pixelMask().isWritable();
//
   if (itsShowLevel>0) {
      cerr << "inIsMasked = " << inIsMasked << endl;
      cerr << "outIsMasked = " << outIsMasked << endl;
   }
//
   const IPosition& inShape = inLattice.shape();
   const IPosition& outShape = outLattice.shape();
   const uInt nDim = inLattice.ndim();
   const Int inPixelAxis = inPixelAxes(inAxisInCoordinate);
   const Int outPixelAxis = outPixelAxes(outAxisInCoordinate);

// Generate vector of pixel coordinates

   const uInt nLine = outShape(outPixelAxis);
   Vector<Bool> failed(nLine);
   Block<Float> outX(nLine);
   Bool allFailed = False;
   Bool allGood = True;
//
   if (replicate) {
      Float pixelScale = Float(outShape(outPixelAxis)) /
	Float(inShape(inPixelAxis));
      make1DCoordinateGrid (outX, pixelScale);
   } else {
      make1DCoordinateGrid (outX, failed, allFailed, allGood,
                            inCoord, outCoord, inAxisInCoordinate,
                            outAxisInCoordinate, machine, useMachine);
   }

// Short cut if all conversions cactus

   if (allFailed) {
      outLattice.set(0.0);
      if (outIsMasked) {
         Lattice<Bool>& outMask = outLattice.pixelMask();
         outMask.set(False);
      }
      return;
   }

// Generate vector of input X values for interpolator

   const uInt nIn = inShape(inPixelAxis);
   Block<Float> inX(nIn);
   if (itsShowLevel>0) cerr << "inX = ";
   for (uInt i=0; i<nIn; i++) {
      inX[i] = Float(i);
      if (itsShowLevel>0) cerr << inX[i] << ",";
   }
   if (itsShowLevel>0) cerr << endl;
//
   if (itsShowLevel>0) cerr << "outX = ";
   for (uInt i=0; i<outX.nelements(); i++) {
      if (itsShowLevel>0) cerr << outX[i] << ",";
   }
   if (itsShowLevel>0) cerr << endl;
//

// Make navigator and iterator for output data and mask. It is vital that
// the "niceShape" is the same for both iterators.  Because the mask and 
// lattice are both TempLattices, one might be on disk, one in core.
// Hence we pick one nice shape and use it on both iterators

   IPosition niceShape = outLattice.niceCursorShape();
   TiledLineStepper outStepper(outShape, niceShape, outPixelAxis);
   LatticeIterator<T> outIter(outLattice, outStepper);
//
   LatticeIterator<Bool>* outMaskIterPtr = 0;
   if (outIsMasked) {
      Lattice<Bool>& outMask = outLattice.pixelMask();    
      TiledLineStepper outMaskStepper(outShape, niceShape, outPixelAxis);
      outMaskIterPtr = new LatticeIterator<Bool>(outMask, outMaskStepper);
   }
//
   IPosition inSubShape(nDim,1);
   IPosition inPos(nDim);
   inSubShape(inPixelAxis) = inShape(inPixelAxis);
//
   if (itsShowLevel>0) {
      cerr << "in, out pixel axis = " << inPixelAxis << ", " << outPixelAxis <<
	endl;  
      cerr << "shape in, shape out" << inShape << outShape << endl;  
      cerr << "inSubShape=" << inSubShape << endl;
   }

// Set interpolator method

   InterpolateArray1D<Float,Float>::InterpolationMethod method1D =
        InterpolateArray1D<Float,Float>::linear;
   if (method==Interpolate2D::NEAREST) {
      method1D = InterpolateArray1D<Float,Float>::nearestNeighbour;
      if (itsShowLevel>0) {
         cerr << "Method = nearest" << endl;
      }
   } else if (method==Interpolate2D::LINEAR) {
      method1D = InterpolateArray1D<Float,Float>::linear;
      if (itsShowLevel>0) {
         cerr << "Method = linear" << endl;
      }
   } else if (method==Interpolate2D::CUBIC) {
      method1D = InterpolateArray1D<Float,Float>::spline;
      if (itsShowLevel>0) {
         cerr << "Method = cubic spline" << endl;
      }
   }

// Progress meter

   ProgressMeter* pProgressMeter = 0;
   if (showProgress) {
     Double nMin = 0.0;
     Double nMax = Double(outLattice.shape().product()) /
       Double(outIter.cursorShape().product());
     ostringstream oss;
     oss << "Axis " << outPixelAxis + 1 << " : Lines Regridded";
     pProgressMeter = new ProgressMeter(nMin, nMax, String(oss),
                                        String("Regridding"),
                                        String(""), String(""),
                                        True, max(1,Int(nMax/20)));
   }

// Iterate through output image by line

   Bool goodIsTrue = True;
   Bool extrapolate = False;
   Vector<Bool> dummyOutMask(nLine);
   for (outIter.reset(); !outIter.atEnd(); outIter++) {
      const IPosition& outPos = outIter.position();
      if (itsShowLevel>1) {
         cerr << endl;
         cerr << "Output lattice iterator position = " <<  outPos << endl;
         cerr << "Output lattice iterator cursor shape = " << 
	   outIter.cursorShape()<< endl;
      }

// Get input vector of data and mask

      for (uInt i=0; i<nDim; i++) {
         inPos[i] = outPos[pixelAxisMap[i]];
      }
      const Vector<T>& inY = inLattice.getSlice(inPos, inSubShape, True);
      const Vector<Bool>& inMask = inLattice.getMaskSlice(inPos, inSubShape,
							  True);

      if (itsShowLevel>1) {
         cerr << "inPos=" << inPos << endl;
         cerr << "inY=" << inY << endl;
         cerr << "inY=" << inMask << endl;
      }
//
      if (allGood) {
         if (outIsMasked) {
            InterpolateArray1D<Float,T>::interpolate(outIter.rwVectorCursor(),
                                                     outMaskIterPtr->
						     rwVectorCursor(),
                                                     outX, inX, inY, inMask,
                                                     method1D, goodIsTrue,
						     extrapolate);
         } else {
            InterpolateArray1D<Float,T>::interpolate(outIter.rwVectorCursor(),
                                                     dummyOutMask,
                                                     outX, inX, inY, inMask,
                                                     method1D, goodIsTrue,
						     extrapolate);
         }
      } else {

// AND the coordinate conversion success vector and the input mask

         if (outIsMasked) {
            InterpolateArray1D<Float,T>::interpolate(outIter.rwVectorCursor(),
                                                     outMaskIterPtr->
						     rwVectorCursor(),
                                                     outX, inX, inY, 
                                                    (failed && inMask),
                                                     method1D, goodIsTrue,
						     extrapolate);
         } else {
            InterpolateArray1D<Float,T>::interpolate(outIter.rwVectorCursor(),
                                                     dummyOutMask,
                                                     outX, inX, inY, 
                                                    (failed && inMask),
                                                     method1D, goodIsTrue,
						     extrapolate);
         }
      }
//
      if (itsShowLevel>1) {
         cerr << "outY = " << outIter.rwVectorCursor() << endl;
         if (outIsMasked) cerr << "outMask = " <<
			    outMaskIterPtr->rwVectorCursor() << endl;
      }
//
      if (outIsMasked) (*outMaskIterPtr)++;
      if (showProgress) pProgressMeter->update(Double(outIter.nsteps()));
   } 
//
   if (outIsMasked) delete outMaskIterPtr;
   if (showProgress) delete pProgressMeter;
}



template<class T>
void ImageRegrid<T>::make1DCoordinateGrid (Block<Float>& outX,
                                           Vector<Bool>& failed,
                                           Bool& allFailed,
                                           Bool& allGood,
                                           const Coordinate& inCoord,
                                           const Coordinate& outCoord,
                                           Int inAxisInCoordinate,
                                           Int outAxisInCoordinate,
                                           MFrequency::Convert& machine,
                                           Bool useMachine)
{
// Precompute vector of output coordinates to interpolate data at

   Double outPixel2, inPixel2;
   Vector<Double> world, inPixel;
   Vector<Double> outPixel = outCoord.referencePixel().copy();
//
   const uInt nLine = outX.nelements();
   failed.resize(nLine);
   allFailed = True;
   allGood = True;
   Bool ok1 = False;
   Bool ok2 = False;
   MFrequency inMVF, outMVF;
//
   if (useMachine) {

// If we are going to Stoke up the MFrequency machine it means
// we have a SpectralCoordinate; cast to it

      const SpectralCoordinate& inSpecCoord =
	dynamic_cast<const SpectralCoordinate&>(inCoord);
      const SpectralCoordinate& outSpecCoord =
	dynamic_cast<const SpectralCoordinate&>(outCoord);
//
      for (uInt i=0; i<nLine; i++) {

// Fill Coordinate pixel locations
//
         outPixel2 = i;
         ok1 = outSpecCoord.toWorld(outMVF, outPixel2);
         if (ok1) {
            inMVF = machine(outMVF).getValue();
            ok2 = inSpecCoord.toPixel(inPixel2, inMVF);
         } 
//
         if (!ok1 || !ok2) {
            failed(i) = True;
            allGood = False;
         } else {

// This one ok

            outX[i] = inPixel2;
            failed(i) = False;
            allFailed = False;
         }
      }
   } else {
      for (uInt i=0; i<nLine; i++) {

// Fill Coordinate pixel locations
//
         outPixel(outAxisInCoordinate) = i;
         ok1 = outCoord.toWorld(world, outPixel);
         if (ok1) ok2 = inCoord.toPixel(inPixel, world);
//
         if (!ok1 || !ok2) {
            failed(i) = True;
            allGood = False;
         } else {

// This one ok

            outX[i] = inPixel(inAxisInCoordinate);
            failed(i) = False;
            allFailed = False;
         }
      }
   }
//
   if (itsShowLevel>0) {
      cerr << "allFailed=" << allFailed << endl;
      cerr << "allGood =" << allGood << endl;
   }
   if (itsShowLevel>1) {
       cerr << "failed = " << failed << endl;
       cerr << "outX=";
       for (uInt i=0;i<nLine;i++) {
          cerr << outX[i] << ", ";
       }
       cerr << endl;
   }
}



template<class T>
void ImageRegrid<T>::make1DCoordinateGrid (Block<Float>& outX,
                                           Float pixelScale) const
{
   Float oX = -0.5 + (1.0/2/pixelScale);
   const uInt nLine = outX.nelements();
   for (uInt i=0; i<nLine; i++) {
      outX[i]  = (Float(i) / pixelScale) + oX;
   }
}


template<class T>
void ImageRegrid<T>::_checkAxes(IPosition& outPixelAxes,
                               const IPosition& inShape,
                               const IPosition& outShape,
                               const Vector<Int>& pixelAxisMap1,
                               const CoordinateSystem& outCoords,
                               Bool verbose)
{
   LogIO os(LogOrigin("ImageRegrid", __func__, WHERE));
   ThrowIf(inShape.nelements()==0, "The input shape is illegal");
   ThrowIf(
		   outShape.nelements()==0,
		   "The output shape is illegal"
   );
   Int n1 = outPixelAxes.nelements();
   const Int nOut = outShape.nelements();
   ThrowIf(
		   n1 > nOut,
      "You have specified more pixel axes than there are dimensions"
   );

// Fill in all axes if null pixelAxes given

   if (n1==0) {
      outPixelAxes = IPosition::makeAxisPath(nOut);
      n1 = outPixelAxes.nelements();
   }

// Check for Stokes and discard

   Int outCoordinate, outAxisInCoordinate;
   Int j = 0;
   for (Int i=0; i<n1; i++) {

// Find pixel axis in output coordinates if not yet done

      outCoords.findPixelAxis(outCoordinate, outAxisInCoordinate, 
                              outPixelAxes(i));
      if (outCoordinate==-1 || outAxisInCoordinate==-1) {
         ostringstream oss;
         oss << "Pixel axis " << outPixelAxes(i)+1 << 
                " has been removed from the output CoordinateSystem" << endl;
         ThrowCc(String(oss));
      }

// Find out the coordinate type and don't allow Stokes
 
      Coordinate::Type type = outCoords.type(outCoordinate);

      if (type==Coordinate::STOKES) {
         os << LogIO::POST << "The Stokes axis cannot be regridded "
	   "- removing from list" << endl;
      } else {
         Bool ok = True;
         if (outShape(outPixelAxes(i))==1) {

// We can (will be able to) handle DirectionCoordinates which
// are degenerate in one axis.  The test for this is in Regrid2d
// Otherwise, regridding a one-pixel axis is useless.

            if (type!=Coordinate::DIRECTION) {
            	if (verbose) {
            		os << "Cannot regrid zero-based axis " << outPixelAxes(i)
            			<< " because it is of unit length - removing from list"
            			<< LogIO::POST;
            	}
              ok = False;
            }
         } 
//
         if (ok) {
            outPixelAxes(j) = outPixelAxes(i);
            j++;
         }
      }
   }
   outPixelAxes.resize(j,True);
   n1 = outPixelAxes.nelements();

// Check for range

   Vector<Bool> found(nOut, False);
   for (Int i=0; i<n1; i++) {
      ThrowIf(
    	outPixelAxes(i)<0 || outPixelAxes(i)>=nOut,
         "Pixel axes are out of range"
      );
//
      ThrowIf(
    		  found(outPixelAxes(i)),
    		  "Specified pixel axes " + String::toString( outPixelAxes+1)
            	+ " are not unique"
      );
         found(outPixelAxes(i)) = True;
   }
// CHeck non-regriddded axis shapes are ok

   for (Int i=0; i<nOut; i++) {
      Bool foundIt = False;
      for (Int j=0;j<n1; j++) {
         if (outPixelAxes(j)==i) {
            foundIt = True;
            break;
         }
      }

// pixelAxisMap1(i) says where pixel axis i in the output image
// is in the input image

      if (!foundIt && outShape(i) != inShape(pixelAxisMap1[i])) {
    	  ostringstream oss;
    	  oss << "Any axis not being regridded must have the same "
    	     << "input and output shapes. Output axis " << i
    	     << ", which corresponds to input axis "
    	     << pixelAxisMap1[i] << ", has a length of " << outShape(i)
    	     << ", whereas the corresponding input axis has length "
    	     << inShape(pixelAxisMap1[i]);
    	  ThrowCc(oss.str());
      }  
   }
}


template<class T>
void ImageRegrid<T>::findMaps (uInt nDim, 
                               Vector<Int>& pixelAxisMap1,
                               Vector<Int>& pixelAxisMap2,
                               const CoordinateSystem& inCoords,
                               const CoordinateSystem& outCoords) const
{

// Find mapping between CoordinateSystems
//
// worldAxisMap(i) is the location of world axis i (from the supplied
// coordinate system, cSys, in the current coordinate system. 
// worldAxisTranspose(i) is the location of world axis i (from the current
// coordinate system) in the supplied coordinate system, cSys.  

   Vector<Int> worldAxisTranspose, worldAxisMap;
   Vector<Bool> worldRefChange;
   if (!outCoords.worldMap(worldAxisMap, worldAxisTranspose,
                           worldRefChange, inCoords)) {
      throw(AipsError(inCoords.errorMessage()));
   }

// pixelAxisMap1(i) says where pixel axis i in the output coordinate system
// is in the input coordinate system
// pixelAxisMap2(i) says where pixel axis i in the input coordinate system
// is in the output coordinate system

   pixelAxisMap1.resize(nDim);
   pixelAxisMap2.resize(nDim);
   for (uInt paOut=0; paOut<nDim; paOut++) {
      Int waOut = outCoords.pixelAxisToWorldAxis(paOut);
      Int waIn = worldAxisTranspose(waOut);
      Int paIn = inCoords.worldAxisToPixelAxis(waIn);      
//
      pixelAxisMap1[paOut] = paIn;
      pixelAxisMap2[paIn] = paOut;
   }
//
   if (itsShowLevel>0) {
      cerr << "worldmap, worldtranspose, refChange = " <<
             worldAxisMap << worldAxisTranspose << worldRefChange << endl;
      cerr << "pixelaxismap{1,2} = " << pixelAxisMap1 << pixelAxisMap2 << endl;
   }
}   



template<class T>
Double ImageRegrid<T>::findScaleFactor(const Unit& units, 
                                       const CoordinateSystem& inCoords, 
                                       const CoordinateSystem& outCoords,
                                       Int inCoordinate, Int outCoordinate,
                                       LogIO& os) const
{
   Double fac = 1.0;
   String t = units.getName();
   t.upcase();
   if (t==String("JY/PIXEL")) {

// Set units to the same thing

      if (inCoords.type(inCoordinate)==Coordinate::DIRECTION) {
         DirectionCoordinate inDir = inCoords.directionCoordinate(inCoordinate);
         DirectionCoordinate outDir = outCoords.directionCoordinate(outCoordinate);
//
         Vector<String> units(2);
         units.set("deg");
//
         inDir.setWorldAxisUnits(units);
         outDir.setWorldAxisUnits(units);
//
         const Vector<Double>& incIn = inDir.increment();
         const Vector<Double>& incOut = outDir.increment();
//
         fac = abs(incOut(0)*incOut(1) / incIn(0) / incIn(1));
         os << "Applying Jy/pixel scale factor of " << fac << endl;
      } else if (inCoords.type(inCoordinate)==Coordinate::LINEAR) {
         LinearCoordinate inLin = inCoords.linearCoordinate(inCoordinate);
         LinearCoordinate outLin = outCoords.linearCoordinate(outCoordinate);
//
         const Vector<String>& units = inLin.worldAxisUnits().copy();
         ThrowIf(
        	!outLin.setWorldAxisUnits(units),
            "Failed to set output and input LinearCoordinate axis units the same"
         );
//
         const Vector<Double>& incIn = inLin.increment();
         const Vector<Double>& incOut = outLin.increment();
//
         fac = abs(incOut(0)*incOut(1) / incIn(0) / incIn(1));
         os << "Applying Jy/pixel scale factor of " << fac << endl;
     }
   }
//
   return fac;
}

template<class T>
Bool ImageRegrid<T>::minmax(Double &minX, Double &maxX, Double &minY,
			    Double &maxY,
                            const Array<Double> &xData, 
                            const Array<Double> &yData, 
                            const Array<Bool> &mask) {                                 
  minX = 1.0e30;
  maxX = -1.0e30;
  minY = 1.0e30;
  maxY = -1.0e30;
  Array<Bool>::const_iterator pMask = mask.begin();
  Array<Double>::const_iterator pXend = xData.end();
  for (Array<Double>::const_iterator pX = xData.begin(), pY = yData.begin();
       pX != pXend; ++pX, ++pY, ++pMask) {
    if (*pMask) {
      minX = minX < *pX ? minX : *pX;
      maxX = maxX > *pX ? maxX : *pX;
      minY = minY < *pY ? minY : *pY;
      maxY = maxY > *pY ? maxY : *pY;
    };
  };
  return (maxX < minX);
}

template<class T>
void ImageRegrid<T>::get2DCoordinateGrid (Cube<Double> &grid,
					  Matrix<Bool> &gridMask) const
{
   grid = its2DCoordinateGrid;
   gridMask = its2DCoordinateGridMask;
}


template<class T>
void ImageRegrid<T>::set2DCoordinateGrid (const Cube<Double> &grid, 
                                          const Matrix<Bool> &gridMask,
                                          Bool)
{
   itsUser2DCoordinateGrid.resize();
   itsUser2DCoordinateGrid = grid;
   itsUser2DCoordinateGridMask.resize();
   itsUser2DCoordinateGridMask = gridMask;
}



} //# NAMESPACE CASACORE - END


#endif