wrf-fire /WPS/ungrib/src/gribcode.F

Language Fortran 77 Lines 2136
MD5 Hash 878b75ab815647767790fbda17c533d2 Estimated Cost $35,845 (why?)
Repository git://github.com/jbeezley/wrf-fire.git View Raw File View Project SPDX
   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
!                                                                             !
!*****************************************************************************!
!                                                                             !
! This is a package of subroutines to read GRIB-formatted data.  It is still  !
! under continuous development. It will not be able to read every GRIB dataset!
! you give it, but it will read a good many.                                  !
!                                                                             !
!   Kevin W. Manning                                                          !
!   NCAR/MMM                                                                  !
!   Summer 1998, and continuing                                               !
!   SDG                                                                       !
!                                                                             !
!*****************************************************************************!
!                                                                             !
! The main user interfaces are:                                               !
!                                                                             !
!  SUBROUTINE GRIBGET(NUNIT, IERR)                                            !
!      Read a single GRIB record from UNIX file-descriptor NUNIT into array   !
!      GREC. No unpacking of any header or data values is performed.          !
!                                                                             !
!  SUBROUTINE GRIBREAD(NUNIT, DATA, NDATA, IERR)                              !
!      Read a single GRIB record from UNIX file-descriptor NUNIT, and unpack  !
!      all header and data values into the appropriate arrays.                !
!                                                                             !
!  SUBROUTINE GRIBHEADER                                                      !
!      Unpack the header of a GRIB record                                     !
!                                                                             !
!  SUBROUTINE GRIBDATA(DATARRAY, NDAT)                                        !
!      Unpack the data in a GRIB record into array DATARRAY                   !
!                                                                             !
!  SUBROUTINE GRIBPRINT(ISEC)                                                 !
!      Print the header information from GRIB section ISEC.                   !
!                                                                             !
!  SUBROUTINE GET_SEC1(KSEC1)                                                 !
!      Return the header information from Section 1.                          !
!                                                                             !
!  SUBROUTINE GET_SEC2(KSEC2)                                                 !
!      Return the header information from Section 2.                          !
!                                                                             !
!  SUBROUTINE GET_GRIDINFO(IGINFO, GINFO)                                     !
!      Return the grid information of the previously-unpacked GRIB header.    !
!                                                                             !
!                                                                             !
!*****************************************************************************!
!                                                                             !
!                                                                             !
! The following arrays have meanings as follows:                              !
!                                                                             !
!                                                                             !
! SEC0:    GRIB Header Section 0 information                                  !
!                                                                             !
!       1  :  Length of a complete GRIB record                                !
!       2  :  GRIB Edition number                                             !
!                                                                             !
!                                                                             !
! SEC1:    GRIB Header Section 1 information                                  !
!                                                                             !
!       1  :  Length of GRIB section 1 (bytes)                                !
!       2  :  Parameter Table Version number ????                             !
!       3  :  Center Identifier ????                                          !
!       4  :  Process Identifier ????                                         !
!       5  :  Grid ID number for pre-specified grids.                         !
!       6  :  Binary bitmap flag:                                             !
!       7  :  Parameter ID Number (ON388 Table 2)                             !
!       8  :  Level type (ON388 Table 3)                                      !
!       9  :  Level value, or top value of a layer                            !
!      10  :  Bottom value of a layer ( 0 if NA ??)                           !
!      11  :  Year (00-99)                                                    !
!      12  :  Month (01-12)                                                   !
!      13  :  Day of the month (01-31)                                        !
!      14  :  Hour (00-23)                                                    !
!      15  :  Minute (00-59)                                                  !
!      16  :  Forecast time unit: (ON388 Table 4)                             !
!      17  :  Time period 1:                                                  !
!      18  :  Time period 2:                                                  !
!      19  :  Time range indicator (ON833 Table 5)                            !
!      20  :  Number of ?? in an average ??                                   !
!      21  :  Number of ?? missing from average ??                            !
!      22  :  Century (Years 1999 and 2000 are century 20, 2001 is century 21)!
!      23  :  Sub-center identifier ??                                        !
!      24  :  Decimal scale factor for ???                                    !
!                                                                             !
!                                                                             !
!                                                                             !
!                                                                             !
!                                                                             !
! SEC2:    GRIB Header Section 2 information                                  !
!                                                                             !
!       1  :  Length of GRIB Section 2                                        !
!       2  :  Number of vertical-coordinate parameters ???                    !
!       3  :  Starting-point of the list of vertical-coordinate parameters ?? !
!       4  :  Data-representation type (i.e., grid type)  Table ???           !
!          :      0 = ??                                                      !
!          :      3 = Lambert-conformal grid.                                 !
!          :      5 = Polar-stereographic grid.                               !
!                                                                             !
!     if (SEC2(4) == 0) then            LATITUDE/LONGITUDE GRID               !
!                                                                             !
!         INFOGRID/GRIDINFO:                                                  !
!                                                                             !
!             1  :  I Dimension of the grid                                   !
!             2  :  J Dimension of the grid                                   !
!             3  :  Starting Latitude of the grid.                            !
!             4  :  Starting Longitude of the grid.                           !
!             5  :  Resolution and component flags.                           !
!             6  :  Ending latitude of the grid.                              !
!             7  :  Ending longitude of the grid.                             !
!             8  :  Longitudinal increment.                                   !
!             9  :  Latitudinal incriment.                                    !
!            10  :  Scanning mode (bit 3 from Table 8)                        !
!            21  :  Iscan sign (+1/-1) (bit 1 from Table 8)                   !
!            22  :  Jscan sign (+1/-1) (bit 2 from Table 8)                   !
!                                                                             !
!                                                                             !
!     elseif (SEC2(4) == 3) then        LAMBERT CONFORMAL GRID                !
!                                                                             !
!         INFOGRID/GRIDINFO:                                                  !
!                                                                             !
!             1  :  I Dimension of the grid                                   !
!             2  :  J Dimension of the grid                                   !
!             3  :  Starting Latitude of the grid.                            !
!             4  :  Starting Longitude of the grid.                           !
!             5  :  Resolution and component flags.                           !
!             6  :  Center longitude of the projection.                       !
!             7  :  Grid-spacing in the I direction                           !
!             8  :  Grid-spacing in the J direction                           !
!             9  :  Projection center                                         !
!            10  :  Scanning mode (bit 3 from Table 8)                        !
!            11  :  First TRUELAT value.                                      !
!            12  :  Second TRUELAT value.                                     !
!            13  :  Latitude of the southern pole ??                          !
!            14  :  Longitude of the southern pole ??                         !
!            21  :  Iscan sign (+1/-1) (bit 1 from Table 8)                   !
!            22  :  Jscan sign (+1/-1) (bit 2 from Table 8)                   !
!                                                                             !
!     if (SEC2(4) == 4) then            GAUSSIAN GRID                         !
!                                                                             !
!         INFOGRID/GRIDINFO:                                                  !
!                                                                             !
!             1  :  I Dimension of the grid                                   !
!             2  :  J Dimension of the grid                                   !
!             3  :  Starting Latitude of the grid.                            !
!             4  :  Starting Longitude of the grid.                           !
!             5  :  Resolution and component flags.                           !
!             6  :  Ending latitude of the grid.                              !
!             7  :  Ending longitude of the grid.                             !
!             8  :  Longitudinal increment.                                   !
!             9  :  Number of latitude circles between pole and equator       !
!            10  :  Scanning mode (bit 3 from Table 8)                        !
!            17  :  Original (stored) ending latitude                         !
!            18  :  Original (stored) starting latitude                       !
!            19  :  Approximate delta-latitude                                !
!            21  :  Iscan sign (+1/-1) (bit 1 from Table 8)                   !
!            22  :  Jscan sign (+1/-1) (bit 2 from Table 8)                   !
!                                                                             !
!                                                                             !
!     elseif (SEC2(4) == 5) then        POLAR STEREOGRAPHIC GRID              !
!                                                                             !
!         INFOGRID/GRIDINFO:                                                  !
!                                                                             !
!             1  :  I Dimension of the grid                                   !
!             2  :  J Dimension of the grid                                   !
!             3  :  Starting Latitude of the grid.                            !
!             4  :  Starting Longitude of the grid.                           !
!             5  :  Resolution and component flags.                           !
!             6  :  Center longitude of the projection.                       !
!             7  :  Grid-spacing in the I direction                           !
!             8  :  Grid-spacing in the J direction                           !
!             9  :  Projection center                                         !
!            10  :  Scanning mode (bit 3 from Table 8)                        !
!            21  :  Iscan sign (+1/-1) (bit 1 from Table 8)                   !
!            22  :  Jscan sign (+1/-1) (bit 2 from Table 8)                   !
!                                                                             !
!     elseif (SEC2(4) == 50) then       SPHERICAL HARMONIC COEFFICIENTS       !
!                                                                             !
!         INFOGRID/GRIDINFO:                                                  !
!                                                                             !
!             1  :  J-pentagonal resolution parameter                         !
!             2  :  K-pentagonal resolution parameter                         !
!             3  :  M-pentagonal resolution parameter                         !
!             4  :  Spectral representation type (ON388 Table 9)              !
!             5  :  Coefficient storage mode (ON388 Table 10)                 !
!                                                                             !
!     elseif (SEC2(4) == ?) then        ??                                    !
!                                                                             !
!                                                                             !
! SEC3:    GRIB Header Section 3 information:                                 !
! SEC4:    GRIB Header Section 4 information:                                 !
!
!
!
module module_grib
!
! Machine wordsize must be known for the various unpacking routines to work.
! Machine wordsize is set through CPP Directives.
! Use options -DBIT32 (for 32-bit word-size) or -DBIT64 (for 64-bit wordsize) 
! for the CPP pass of the compiler.
!
#if defined (BIT32)
  integer, parameter :: MWSIZE = 32 ! Machine word size in bits
#elif defined (BIT64)
  integer, parameter :: MWSIZE = 64 ! Machine word size in bits
#endif


! Array GREC holds a single packed GRIB record (header and all).
! Array BITMAP holds the bitmap (if a bitmap was used).
!
! For some reason, the cray does not like grec to be allocatable.
!
#if defined (CRAY) 
  integer, dimension(100000) :: grec
  integer, dimension(100000) :: bitmap
#else
  integer, allocatable, save, dimension(:) :: grec
  integer, allocatable, save, dimension(:) :: bitmap
#endif

! SEC0 holds the Section 0 header information
! SEC1 holds the Section 1 header information
! SEC2 holds the Section 2 header information
! SEC3 holds the Section 3 header information
! SEC4 holds the Section 4 header information
! XEC4 holds floating-point Section 4 header information

  integer, dimension(2) :: sec0
  integer, dimension(100) :: sec1
  integer, dimension(10) :: sec2
  integer, dimension(10) :: sec3
  integer, dimension(10) :: sec4
  real, dimension(1) :: xec4

  integer :: sevencount = 0

! INFOGRID holds integer values defining the grid.
! GRIDINFO holds floating-point values definint the grid

  integer, dimension(40) :: infogrid
  real, dimension(40) :: gridinfo

  integer :: ied
  real, parameter :: pi = 3.1415926534
  real, parameter :: degran = pi/180.
  real, parameter :: raddeg = 1./degran

  real :: glat1, glon1, gclon, gtrue1, gtrue2, grrth, gx1, gy1, gkappa

contains
!
!=============================================================================!
!=============================================================================!
!=============================================================================!
!
  integer function gribsize(trec, ilen, ierr)
!-----------------------------------------------------------------------------!
! Return the size of a single GRIB record.                                    !
!                                                                             !
! Input:                                                                      !
!    TREC: At least a portion of the complete GRIB record.                    !
!    ILEN: The size of array TREC.                                            !
!                                                                             !
! Output                                                                      !
!    GRIBSIZE: The size of the full GRIB record                               !
!    IERR    : 0= no errors, 1 = read error
!                                                                             !
! Side Effects:                                                               !
!    * Module variable IED is set to the GRIB Edition number.                 !
!    * STOP, if not GRIB Edition 0 or 1                                      !
!                                                                             !
! Externals                                                                   !
!    GBYTE                                                                    !
!                                                                             !
!-----------------------------------------------------------------------------!
    implicit none
    integer :: ilen
    integer, dimension(ilen) :: trec
    integer :: isz0 = 32
    integer :: isz1 = 0
    integer :: isz2 = 0
    integer :: isz3 = 0
    integer :: isz4 = 0
    integer :: isz5 = 32
    integer :: iflag
    integer :: ierr
    character :: pname*132

    ierr = 0
! Unpack the GRIB Edition number, located in the eighth byte (bits 57-64)     !
! of array TREC.                                                              !

    call gbyte_g1(trec, ied, 56, 8)

! GRIB Edition 1 has the size of the whole GRIB record right up front.        !

    if (ied.eq.1) then
       ! Grib1
       ! Find the size of the whole GRIB record
       call gbyte_g1(trec, gribsize, 32, 24)

! GRIB Edition 0 does not include the total size, so we have to sum up        !
! the sizes of the individual sections                                        !

    elseif (ied.eq.0) then
       ! Grib old edition
       ! Find the size of section 1.
       call gbyte_g1(trec, isz1, isz0, 24)
       isz1 = isz1 * 8
       call gbyte_g1(trec, iflag, isz0+56, 8)
       if ((iflag.eq.128).or.(iflag.eq.192)) then ! section 2 is there
          ! Find the size of section 2.
          call gbyte_g1(trec, isz2, isz0+isz1, 24)
          isz2 = isz2 * 8
       endif
       if ((iflag.eq.64).or.(iflag.eq.192)) then ! Section 3 is there
          ! Find the size of section 3.
          call gbyte_g1(trec, isz3, isz0+isz1+isz2, 24)
          isz3 = isz3 * 8
       endif
       ! Find the size of section 4.
       call gbyte_g1(trec, isz4, isz0+isz1+isz2+isz3, 24)
       isz4 = isz4 * 8

       ! Total the sizes of sections 0 through 5.
       gribsize = (isz0+isz1+isz2+isz3+isz4+isz5) / 8

    elseif (ied.eq.2) then
       ! Grib2
       CALL getarg ( 0 , pname )
       write(*,'("*** stopping in gribcode ***\n")')
       write(*,'("\tI was expecting a Grib1 file, but this is a Grib2 file.")')
       if ( index(pname,'ungrib.exe') .ne. 0 ) then
         write(*,'("\tIt is possible this is because your GRIBFILE.XXX files")')
         write(*,'("\tare not all of the same type.")')
         write(*,'("\tWPS can handle both file types, but a separate ungrib")')
         write(*,'("\tjob must be run for each Grib type.\n")')
       else
         write(*,'("\tUse g2print on Grib2 files\n")')
       endif
       stop 'gribsize in gribcode'
    else
       write(*,'("Error trying to read grib edition number in gribsize.")')
       write(*,'("Possible corrupt grib file.")')
       write(6,*) 'Incorrect edition number  = ',ied
       write(6,*) 'Skipping the rest of the file and continuing.'
       ierr = 1
    endif
  end function gribsize
!
!=============================================================================!
!=============================================================================!
!=============================================================================!
!
  subroutine findgrib(nunit, isize, ierr)

!-----------------------------------------------------------------------------!
!                                                                             !
! Find the string "GRIB", which starts off a GRIB record.                     !
!                                                                             !
! Input:                                                                      !
!    NUNIT:  The C unit to read from.  This should already be opened.         !
!                                                                             !
! Output:                                                                     !
!    ISIZE:  The size in bytes of one complete GRIB Record                    !
!    IERR:   Error flag,                                                      !
!              0 : No error or end-of-file on reading                         !
!              1 : Hit the end of file                                        !
!              2 : Error on reading                                           !
!                                                                             !
! Side effects:                                                               !
!   * The pointer to C unit NUNIT is set to the beginning of the next         !
!     GRIB record.                                                            !
!   * The first time FINDGRIB is called, the integer GTEST is set to          !
!     a value equivalent to the string 'GRIB'                                 !
!                                                                             !
! Modules:                                                                    !
!     MODULE_GRIB                                                             !
!                                                                             !
! Externals:                                                                  !
!     BN_READ                                                                 !
!     BN_SEEK                                                                 !
!     GRIBSIZE                                                                !
!                                                                             !
!-----------------------------------------------------------------------------!
    implicit none
    integer, intent(in) :: nunit
    integer, intent(out) :: isize
    integer, intent(out) :: ierr

    integer, parameter :: LENTMP=100
    integer, dimension(lentmp) :: trec

    integer :: isz, itest, icnt

    integer, save :: gtest = 0

! Set the integer variable GTEST to hold the integer representation of the
! character string 'GRIB'.   This integer variable is later compared to
! integers we read from the GRIB file, to find the beginning of a GRIB record.

    if (gtest.eq.0) then
       if (mwsize.eq.32) then
          gtest = transfer('GRIB', gtest)
       elseif(mwsize.eq.64) then
          call gbyte_g1(char(0)//char(0)//char(0)//char(0)//'GRIB', gtest, 0, mwsize)
       endif
    endif
    ierr = 0
    icnt = 0

    LOOP : DO
! Read LENTMP bytes into holding array TREC.
       call bn_read(nunit, trec, lentmp, isz, ierr, 0)
       if (ierr.eq.1) then
          return
       elseif (ierr.eq.2) then
          write(*,'("Error reading GRIB: IERR = ", I2)') ierr
          return
       endif
! Reposition the file pointer back to where we started.
       call bn_seek(nunit, -isz, 0, 0)

! Compare the first four bytes of TREC with the string 'GRIB' stored in 
! integer variable GTEST.
       if (mwsize.eq.32) then
          if (trec(1) == gtest) exit LOOP
       elseif (mwsize.eq.64) then
          call gbyte_g1(trec, itest, 0, 32)
          if (itest == gtest) exit LOOP
       endif

! Advance the file pointer one byte.
       call bn_seek(nunit, 1, 0, 0)
       icnt = icnt + 1
       if ( icnt .gt. 100000) then       ! stop if we cannot find the GRIB string
         write(*,'("*** stopping in findgrib in gribcode ***\n")')
         write(*,'("\tI could not find the GRIB string in the input file")')
         write(*,'("\tafter testing the first 100,000 bytes.")')
         write(*,'("\tEither the file is corrupt or it is not a GRIB file.\n")')
         stop 'findgrib'
       endif

    ENDDO LOOP

!#if defined (DEC) || defined (ALPHA) || defined (alpha) || defined (LINUX)
#ifdef BYTESWAP
      call swap4(trec, isz)
#endif
    isize = gribsize(trec, isz, ierr)

  end subroutine findgrib
!
!=============================================================================!
!=============================================================================!
!=============================================================================!
!
  subroutine SGUP_NOBITMAP(datarray, ndat)
! Simple grid-point unpacking
    implicit none

    integer :: ndat
    real , dimension(ndat) :: datarray
    integer, dimension(ndat) :: IX
    real :: dfac, bfac
    integer :: iskip

    DFAC = 10.**(-sec1(24))
    BFAC = 2.**sec4(7)
    if (ied.eq.0) then
       iskip = 32 + sec1(1)*8 + sec2(1)*8 + sec3(1)*8 + 11*8
    elseif (ied.eq.1) then
       iskip = 64 + sec1(1)*8 + sec2(1)*8 + sec3(1)*8 + 11*8
    endif
! sec4(8) is the number of bits used per datum value.
! If sec4(8) = 255, assume they mean sec4(8) = 0
    if (sec4(8) == 255) then
       sec4(8) = 0
    endif
! If sec4(8) is 0, assume datarray is constant value of xec4(1)

    if (sec4(8).eq.0) then
       !!! HERE IS THE ORIGINAL NCAR CODE: 
       ! datarray = xec4(1)
       !!! HERE IS WHAT FSL CHANGED IT TO:
       datarray = DFAC*xec4(1)
       !!! because even though it is a constant value
       !!! your still need to scale by the decimal scale factor.
    else
       !!! FSL developers MOVED THE CALL TO gbytes FROM line 441 ABOVE 
       !!! BECAUSE IF sec4(8)==0 BEFORE gbytes IS CALLED, THE MASKS ARRAY
       !!! IN gbytes WILL BE INDEXED OUT OF BOUNDS. C HARROP 9/16/04
       call gbytes_g1(grec, IX, iskip, sec4(8), 0, ndat)
       datarray = DFAC * (xec4(1) + (IX*BFAC))
    endif
  end subroutine SGUP_NOBITMAP
!
!=============================================================================!
!=============================================================================!
!=============================================================================!
!

  subroutine SGUP_BITMAP(datarray, ndat)
! Simple grid-point unpacking, with a bitmap.
    implicit none

    integer :: ndat ! Number of data points in the final grid.
    real , dimension(ndat) :: datarray ! Array holding the final unpacked data.
    real :: dfac, bfac
    integer :: iskip, nbm, i, nn

    integer, allocatable, dimension(:) :: bmdat

! SEC4(1) : The number of bytes in the whole of GRIB Section 4.
! SEC4(6) : The number of unused bits at the end of GRIB Section 4.
! SEC4(8) : The number of bits per data value.

    datarray = -1.E30

! 1) There are fewer than NDAT data values, because a bitmap was used.  
!    Compute the number of data values (NBM).  There are 11 extra bytes
!    in the header section 4.  NBM equals the total number of data bits (not
!    counting the header bits), minus the number of unused buts, and then
!    divided by the number of bits per data value.

! If sec4(8) is 0, assume datarray is constant value of xec4(1)

    if (sec4(8).eq.0) then
       where(bitmap(1:ndat).eq.1) datarray = xec4(1)
       return
    endif
    nbm = ((sec4(1)-11)*8-sec4(6))/sec4(8)
    allocate(bmdat(nbm))

! Compute the parameters involved with packing
    DFAC = 10.**(-sec1(24))
    BFAC = 2.**sec4(7)

! Set ISKIP to the beginning of the data.
    if (ied.eq.0) then
       iskip = 32 + sec1(1)*8 + sec2(1)*8 + sec3(1)*8 + 11*8
    elseif (ied.eq.1) then
       iskip = 64 + sec1(1)*8 + sec2(1)*8 + sec3(1)*8 + 11*8
    endif

! Read the data from the GREC array
    call gbytes_g1(grec, bmdat, iskip, sec4(8), 0, nbm)
! sec4(8) is the number of bits used per datum value.
! If sec4(8) = 255, assume they mean sec4(8) = 0
    if (sec4(8) == 255) sec4(8) = 0

! Unpack the data according to packing parameters DFAC, BFAC, and XEC4(1), 
! and masked by the bitmap BITMAP.
       nn = 0
       do i = 1, ndat
          if (bitmap(i).eq.1) then
             nn = nn + 1
             datarray(i) = DFAC * (xec4(1) + (bmdat(nn)*BFAC))
          endif
       enddo

! Deallocate the scratch BMDAT array
    deallocate(bmdat)

  end subroutine SGUP_BITMAP
!
!=============================================================================!
!=============================================================================!
!=============================================================================!
!

  subroutine CSHUP(pdata, ndat)
! ECMWFs unpacking of ECMWFs Complex Spherical Harmonic packing
! Adapted from ECMWFs GRIBEX package.
    implicit none

    integer :: ndat
    real , dimension(ndat) :: pdata
    integer, dimension(ndat+500) :: IX

    integer :: iskip, isign
    integer :: N1, IPOWER, J, K, M, nval
    real :: zscale, zref

    integer :: ic, jm, iuc, il2, inum, jn
    integer :: inext, ilast, ioff, jrow, index, i, jcol
    real :: bval
    integer, allocatable, dimension(:) :: iexp, imant
    real , dimension(0:400) :: factor
    real :: power
    integer :: N

    index = -1

    if (ied.eq.0) then
       iskip = 32 + sec1(1)*8 + sec2(1)*8 + sec3(1)*8 + 11*8
    elseif(ied.eq.1) then
       iskip = 64 + sec1(1)*8 + sec2(1)*8 + sec3(1)*8 + 11*8
    endif

    call gbyte_g1(grec,N1,iskip,16)
    iskip = iskip + 16

    call gbyte_g1(grec,ipower,iskip,16)
    iskip = iskip + 16
    if (ipower.ge.32768) ipower = 32768-ipower

! Unpack the resolution parameters for the initial (small) truncation:
    call gbyte_g1(grec,J,iskip,8)
    iskip = iskip + 8
    call gbyte_g1(grec,K,iskip,8)
    iskip = iskip + 8
    call gbyte_g1(grec,M,iskip,8)
    iskip = iskip + 8

    zscale = 2.**sec4(7)

    iskip = N1*8

    nval = NDAT - (J+1)*(J+2)

    call gbytes_g1(grec, ix, iskip, sec4(8), 0, nval)
! sec4(8) is the number of bits used per datum value.
! If sec4(8) = 255, assume they mean sec4(8) = 0
    if (sec4(8) == 255) sec4(8) = 0

    pdata(1:nval) = (float(ix(1:nval))*zscale)+xec4(1)

    IUC = NDAT+1
    IC  = NVAL+1
    DO JM=INFOGRID(1),0,-1
       IL2=MAX(JM,J+1)
       INUM=2*(INFOGRID(1)-IL2+1)
       pdata(iuc-inum:iuc-1) = pdata(ic-inum:ic-1)
       iuc = iuc - inum
       ic = ic - inum
       IUC = IUC-MAX((IL2-JM)*2,0)
    ENDDO

    if (ied.eq.0) then
       iskip = 32 + sec1(1)*8 + sec2(1)*8 + sec3(1)*8 + 11*8
    elseif (ied.eq.1) then
       iskip = 64 + sec1(1)*8 + sec2(1)*8 + sec3(1)*8 + 18*8
    endif

    allocate(iexp(802))
    allocate(imant(802))
    ilast=j+1
    do jrow=1,ilast
       inext = 2*(ilast-jrow+1)
       ! extract all the exponents
       call gbytes_g1(grec, iexp, iskip, 8, 24, inext)
       ! extract all the mantissas
       ioff = 8
       call gbytes_g1(grec, imant, iskip+8, 24, 8, inext)
       iskip = iskip + inext*32

       ! Build the real values from mantissas and exponents
       bval = 2.**(-24)
       i = 0
       do jcol = jrow, infogrid(1)+1
          index = index + 2
          if (ilast.ge.jcol) then
             i = i + 1
             if ((iexp(i).eq.128.or.iexp(i).eq.0).and.(imant(i).eq.0)) then
                pdata(i) = 0
             else
                if (iexp(i).ge.128) then
                   iexp(i) = iexp(i) - 128
                   isign = -1
                else
                   isign = 1
                endif
                pdata(index) = isign*bval*IMANT(i)*16.**(IEXP(i)-64)
                i = i + 1
                if (iexp(i).ge.128) then
                   iexp(i) = iexp(i) - 128
                   isign = -1
                else
                   isign = 1
                endif
                pdata(index+1) = isign*bval*IMANT(i)*16.**(IEXP(i)-64)
             endif
          endif
       enddo
    enddo

    !Apply power scaling:

    if (ipower.ne.0) then
       power = float(ipower) / 1000.0
       factor(0) = 1.0
       do n = 1 , infogrid(1)
          if( ipower .ne. 1000 ) then
             factor(n) = 1.0 / (n * (n+1) )**power
          else
             factor(n) = 1.0 / (n * (n + 1))
          endif
       enddo
       INDEX = -1
       DO M = 0 , J-1
          DO N = M , INFOGRID(1)
             INDEX = INDEX + 2
             IF ( N .GE. J ) THEN
                PDATA(INDEX:INDEX+1) = PDATA(INDEX:INDEX+1) * FACTOR(N)
             ENDIF
          ENDDO
       ENDDO
       DO M = J , INFOGRID(1)
          DO N = M , INFOGRID(1)
             INDEX = INDEX + 2
             PDATA(INDEX:INDEX+1)   = PDATA(INDEX:INDEX+1)   * FACTOR(N)
          ENDDO
       ENDDO
    endif

  end subroutine CSHUP
!
!=============================================================================!
!=============================================================================!
!=============================================================================!
!
!
!
! Trigonometric functions which deal with degrees, rather than radians:
!
  real function sind(theta)
    real :: theta
    sind = sin(theta*degran)
  end function sind
  real function cosd(theta)
    real :: theta
    cosd = cos(theta*degran)
  end function cosd
  real function tand(theta)
    real :: theta
    tand = tan(theta*degran)
  end function tand
  real function atand(x)
    real :: x
    atand = atan(x)*raddeg
  end function atand
  real function atan2d(x,y)
    real :: x,y
    atan2d = atan2(x,y)*raddeg
  end function atan2d
  real function asind(x)
    real :: x
    asind = asin(x)*raddeg
  end function asind
  real function acosd(x)
    real :: x
    acosd = acos(x)*raddeg
  end function acosd

!
!=============================================================================!
!=============================================================================!
!=============================================================================!
!
end module module_grib
!
!=============================================================================!
!=============================================================================!
!=============================================================================!
!
subroutine gribget(nunit, ierr)
  use module_grib
!-----------------------------------------------------------------------------!
!                                                                             !
! Read a single GRIB record, with no unpacking of any header or data fields.  !
!                                                                             !
! Input:                                                                      !
!     NUNIT:  C unit number to read from.  This should already be open.       !
!                                                                             !
! Output:                                                                     !
!     IERR: Error flag, Non-zero means there was a problem with the read.     !
!                                                                             !
! Side Effects:                                                               !
!        The array GREC is allocated, and filled with one GRIB record.        !
!        The C unit pointer is moved to the end of the GRIB record just read. !
!                                                                             !
! Modules:                                                                    !
!       MODULE_GRIB                                                           !
!                                                                             !
! Externals:                                                                  !
!       FINDGRIB                                                              !
!       BN_READ                                                               !
!                                                                             !
!-----------------------------------------------------------------------------!

  implicit none

  integer :: nunit
  integer :: ierr
  integer :: isz, isize

! Position the file pointer at the beginning of the GRIB record.
  call findgrib(nunit, isize, ierr)
  if (ierr.ne.0) return

! Allocate the GREC array to be able to hold the data

#if defined (CRAY)
#else
  allocate(grec((isize+(mwsize/8-1))/(mwsize/8)))
#endif

! Read the full GRIB record.

  call bn_read(nunit, grec, isize, isz, ierr, 1)

!#if defined (DEC) || defined (ALPHA) || defined (alpha) || defined (LINUX)
#ifdef BYTESWAP
      call swap4(grec, isz)
#endif


end subroutine gribget
!
!=============================================================================!
!=============================================================================!
!=============================================================================!
!
subroutine gribread(nunit, data, ndata, debug_level, ierr)
!-----------------------------------------------------------------------------!
! Read one grib record, unpack the header and data information.               !
!                                                                             !
! Input:                                                                      !
!    NUNIT:  C Unit to read from.                                             !
!    NDATA:  Size of array DATA (Should be >= NDAT as computed herein.)       !
!                                                                             !
! Output:                                                                     !
!    DATA:  The unpacked data array                                           !
!    IERR:  Error flag, non-zero means there was a problem.                   !
!                                                                             !
! Side Effects:                                                               !
!    * Header arrays SEC0, SEC1, SEC2, SEC3, SEC4, XEC4, INFOGRID and         !
!      INFOGRID are filled.                                                   !
!    * The BITMAP array is filled.                                            !
!    * The C unit pointer is advanced to the end of the GRIB record.          !
!                                                                             !
! Modules:                                                                    !
!      MODULE_GRIB                                                            !
!                                                                             !
! Externals:                                                                  !
!      GRIBGET                                                                !
!      GRIBHEADER                                                             !
!      GRIBDATA                                                               !
!                                                                             !
!-----------------------------------------------------------------------------!
  use module_grib

  implicit none

  integer :: nunit
  integer :: debug_level
  integer :: ierr
  real, allocatable, dimension(:) :: datarray
  integer :: ndata
  real, dimension(ndata) :: data

  integer :: ni, nj

  ierr = 0

  call gribget(nunit, ierr)
  if (ierr.ne.0) return

! Unpack the header information

  call gribheader(debug_level,ierr)

! Determine the size of the data array from the information in the header, 
! and allocate the array DATARRAY to hold that data.

  if (sec2(4).ne.50) then
     ni = infogrid(1)
     nj = infogrid(2)
     allocate(datarray(ni*nj))
  else
     ni = (infogrid(1)+1) * (infogrid(1)+2)
     nj = 1
     allocate(datarray(ni*nj))
  endif

! Unpack the data from the GRIB record, and fill the array DATARRAY.

  call gribdata(datarray, ni*nj) 

  data(1:ni*nj) = datarray(1:ni*nj)
#if defined (CRAY)
#else
  deallocate(grec, datarray)
#endif

end subroutine gribread
!
!=============================================================================!
!=============================================================================!
!=============================================================================!
!
subroutine get_sec1(ksec1)
! Return the GRIB Section 1 header information, which has already been
! unpacked by subroutine GRIBHEADER.
  use module_grib
  integer, dimension(100) :: ksec1
  ksec1 = sec1
end subroutine get_sec1
!
!=============================================================================!
!=============================================================================!
!=============================================================================!
!
subroutine get_sec2(ksec2)
! Return the GRIB Section 2 header information, which has already been
! unpacked by subroutine GRIBHEADER.
  use module_grib
  integer, dimension(10) :: ksec2
  ksec2 = sec2
end subroutine get_sec2
!
!=============================================================================!
!=============================================================================!
!=============================================================================!
!
subroutine get_gridinfo(iginfo, ginfo)
  use module_grib
  integer, dimension(40) :: iginfo
  real, dimension(40) :: ginfo
  iginfo = infogrid
  ginfo = gridinfo
end subroutine get_gridinfo
!
!=============================================================================!
!=============================================================================!
!=============================================================================!
!
subroutine gribprint(isec)
  use module_grib
  implicit none
  integer :: isec
  integer :: ou = 6
  character(len=12) :: string = ',t45,":",i8)'
  character(len=15) :: rstring = ',t45,":",f12.5)'

  if (isec.eq.0) then
     write(*,'(/,"GRIB SECTION 0:")')
     write(ou,'(5x,"Grib Length"'//string) sec0(1)
     write(ou,'(5x,"Grib Edition"'//string) sec0(2)
  else if (isec.eq.1) then
     write(*,'(/,"GRIB SECTION 1:")')
     write(ou,'(5x,"Length of PDS"'//string) sec1(1)
     write(ou,'(5x,"Parameter Table Version"'//string) sec1(2)
     write(ou,'(5x,"Center ID"'//string) sec1(3)
     write(ou,'(5x,"Process ID"'//string) sec1(4)
     write(ou,'(5x,"Grid ID"'//string) sec1(5)
     if (sec1(25) == 1) then
        write(ou,'(5x,"Is there a Grid Desc. Section (GDS)?",t45,":     Yes")')
     else if (sec1(25) == 0) then
        write(ou,'(5x,"Is there a Grid Desc. Section (GDS)?",t45,":      No")')
     else
        print*, 'Unrecognized sec1(25): ', sec1(25)
     endif
     if (sec1(26) == 1) then
        write(ou,'(5x,"Is there a Bit Map Section (BMS)?",t45,":     Yes")')
     else if (sec1(26) == 0) then
        write(ou,'(5x,"Is there a Bit Map Section (BMS)?",t45,":      No")')
     else
        print*, 'Unrecognized sec1(26): ', sec1(26)
     endif
     write(ou,'(5x,"Parameter"'//string) sec1(7)
     write(ou,'(5x,"Level type"'//string) sec1(8)
     if ( (sec1(8) == 101) .or. (sec1(8) == 104) .or. (sec1(8) == 106) .or. &
          (sec1(8) == 108) .or. (sec1(8) == 110) .or. (sec1(8) == 112) .or. &
          (sec1(8) == 114) .or. (sec1(8) == 116) .or. (sec1(8) == 120) .or. &
          (sec1(8) == 121) .or. (sec1(8) == 128) .or. (sec1(8) == 141) ) then
        write(ou,'(5x,"Hgt, pres, etc. of layer top "'//string) sec1(9)
        write(ou,'(5x,"Hgt, pres, etc. of layer bottom "'//string) sec1(10)
     else
        write(ou,'(5x,"Height, pressure, etc "'//string) sec1(9)
     endif
     write(ou,'(5x,"Year"'//string) sec1(11)
     write(ou,'(5x,"Month"'//string) sec1(12)
     write(ou,'(5x,"Day"'//string) sec1(13)
     write(ou,'(5x,"Hour"'//string) sec1(14)
     write(ou,'(5x,"Minute"'//string) sec1(15)
     write(ou,'(5x,"Forecast time unit"'//string) sec1(16)
     write(ou,'(5x,"P1"'//string) sec1(17)
     write(ou,'(5x,"P2"'//string) sec1(18)
     write(ou,'(5x,"Time Range Indicator"'//string) sec1(19)
     write(ou,'(5x,"Number in Ave?"'//string) sec1(20)
     write(ou,'(5x,"Number missing from ave?"'//string) sec1(21)
     write(ou,'(5x,"Century"'//string) sec1(22)
     write(ou,'(5x,"Sub-center"'//string) sec1(23)
     write(ou,'(5x,"Decimal scale factor"'//string) sec1(24)
  elseif ((isec.eq.2) .and. ((sec1(6).eq.128).or.(sec1(6).eq.192))) then
     write(*,'(/,"GRIB SECTION 2:")')
     write(ou,'(5x,"Length of GRID Desc. Section"'//string) sec2(1)
     if ((sec2(2) /= 0).or.(sec2(3) /= 0) .or. (sec2(4) /= 0)) then
        write(ou,'(5x,"Number of V. Coordinate Parms"'//string) sec2(2)
        write(ou,'(5x,"List Starting point"'//string) sec2(3)
        write(ou,'(5x,"Data Representation type"'//string) sec2(4)
     endif

     if (sec2(4).eq.0) then
        write(ou,'(5x,"Cylindrical Equidistant Grid")')
        write(ou,'(10x,"NI"'//string) infogrid(1)
        write(ou,'(10x,"NJ"'//string) infogrid(2)
        write(ou,'(10x,"Lat 1"'//rstring) gridinfo(3)
        write(ou,'(10x,"Lon 1"'//rstring) gridinfo(4)
        write(ou,'(10x,"Resolution and Component:", t45,":",B8.8)') infogrid(5)
        write(ou,'(10x,"Lat NI"'//string) infogrid(6)
        write(ou,'(10x,"Lon NJ"'//string) infogrid(7)
        write(ou,'(10x,"Delta-Lon"'//string) infogrid(8)
        write(ou,'(10x,"Delta-Lat"'//string) infogrid(9)
        write(ou,'(10x,"Scanning mode"'//string) infogrid(10)
        write(ou,'(10x,"I-Scanning increment"'//string) infogrid(21)
        write(ou,'(10x,"J-Scanning increment"'//string) infogrid(22)

     else if (sec2(4).eq.1) then
        write(ou,'(5x,"Mercator Grid")')
        write(ou,'(10x,"NI"'//string) infogrid(1)
        write(ou,'(10x,"NJ"'//string) infogrid(2)
        write(ou,'(10x,"Lat 1"'//rstring) gridinfo(3)
        write(ou,'(10x,"Lon 1"'//rstring) gridinfo(4)
        write(ou,'(10x,"Resolution and Component",t45,":", B8.8)') infogrid(5)
        write(ou,'(10x,"Lat NI"'//rstring) gridinfo(6)
        write(ou,'(10x,"Lon NJ"'//rstring) gridinfo(7)
        write(ou,'(10x,"Dx"'//rstring) gridinfo(8)
        write(ou,'(10x,"Dy"'//rstring) gridinfo(9)
        write(ou,'(10x,"Scanning mode"'//string) infogrid(10)
        write(ou,'(10x,"Latin"'//rstring) gridinfo(11)
        write(ou,'(10x,"I-Scanning increment"'//string) infogrid(21)
        write(ou,'(10x,"J-Scanning increment"'//string) infogrid(22)

     else if (sec2(4).eq.4) then
        write(ou,'(5x,"Gaussian Grid")')
        write(ou,'(10x,"NI"'//string) infogrid(1)
        write(ou,'(10x,"NJ"'//string) infogrid(2)
        write(ou,'(10x,"Original (stored) Lat 1"'//rstring) gridinfo(18)
        write(ou,'(10x,"Lat 1"'//rstring) gridinfo(3)
        write(ou,'(10x,"Lon 1"'//rstring) gridinfo(4)
        write(ou,'(10x,"Resolution and Component",t45,":", B8.8)') infogrid(5)
        write(ou,'(10x,"Original (stored) Lat NI"'//rstring) gridinfo(17)
        write(ou,'(10x,"Lat NI"'//rstring) gridinfo(6)
        write(ou,'(10x,"Lon NJ"'//rstring) gridinfo(7)
        write(ou,'(10x,"Delta-Lon"'//rstring) gridinfo(8)
        write(ou,'(10x,"Delta-Lat"'//rstring) gridinfo(19)
        write(ou,'(10x,"Number of lats (pole - eq)"'//string) infogrid(9)
        write(ou,'(10x,"Scanning mode"'//string) infogrid(10)
        write(ou,'(10x,"I-Scanning increment"'//string) infogrid(21)
        write(ou,'(10x,"J-Scanning increment"'//string) infogrid(22)
     elseif (sec2(4).eq.3) then
        write(ou,'(5x,"Lambert Conformal Grid")')
        write(ou,'(10x,"NI"'//string) infogrid(1)
        write(ou,'(10x,"NJ"'//string) infogrid(2)
        write(ou,'(10x,"Lat 1"'//string) infogrid(3)
        write(ou,'(10x,"Lon 1"'//string) infogrid(4)
        write(ou,'(10x,"Resolution and Component",t45,":", B8.8)') infogrid(5)
        write(ou,'(10x,"Lov"'//string) infogrid(6)
        write(ou,'(10x,"Dx"'//string) infogrid(7)
        write(ou,'(10x,"Dy"'//string) infogrid(8)
        write(ou,'(10x,"Projection center"'//string) infogrid(9)
        write(ou,'(10x,"Scanning mode"'//string) infogrid(10)
        write(ou,'(10x,"I-Scanning increment"'//string) infogrid(21)
        write(ou,'(10x,"J-Scanning increment"'//string) infogrid(22)
        write(ou,'(10x,"Latin 1"'//string) infogrid(11)
        write(ou,'(10x,"Latin 2"'//string) infogrid(12)
        write(ou,'(10x,"Lat of southern pole"'//string) infogrid(13)
        write(ou,'(10x,"Lon of southern pole"'//string) infogrid(14)
     elseif (sec2(4).eq.5) then
        write(ou,'(5x,"Polar Stereographic Grid")')
        write(ou,'(10x,"NI"'//string) infogrid(1)
        write(ou,'(10x,"NJ"'//string) infogrid(2)
        write(ou,'(10x,"Lat 1"'//string) infogrid(3)
        write(ou,'(10x,"Lon 1"'//string) infogrid(4)
        write(ou,'(10x,"Resolution and Component", t45,":",B8.8)') infogrid(5)
        write(ou,'(10x,"Lov"'//string) infogrid(6)
        write(ou,'(10x,"Dx"'//string) infogrid(7)
        write(ou,'(10x,"Dy"'//string) infogrid(8)
        write(ou,'(10x,"Projection center"'//string) infogrid(9)
        write(ou,'(10x,"Scanning mode"'//string) infogrid(10)
        write(ou,'(10x,"I-Scanning increment"'//string) infogrid(21)
        write(ou,'(10x,"J-Scanning increment"'//string) infogrid(22)
     elseif (sec2(4).eq.50) then
        write(ou,'(5x,"Spherical harmonic components")')
        write(ou,'(10x,"J-Pentagonal resolution parm:"'//string) infogrid(1)
        write(ou,'(10x,"K-Pentagonal resolution parm:"'//string) infogrid(2)
        write(ou,'(10x,"M-Pentagonal resolution parm:"'//string) infogrid(3)
        write(ou,'(10x,"Representation type"'//string) infogrid(4)
        write(ou,'(10x,"Coefficient storage mode"'//string) infogrid(5)
     endif
  elseif ((isec.eq.3) .and. (sec1(26).eq.1)) then
     write(*,'(/,"GRIB SECTION 3:")')
     write(ou,'(5x,"Length of bit map section"'//string) sec3(1)
     write(ou,'(5x,"Number of unused bits"'//string) sec3(2)
     write(ou,'(5x,"Numeric"'//string) sec3(3)

  elseif (isec.eq.4) then
     write(*,'(/,"GRIB SECTION 4:")')
     write(ou,'(5x,"Length of BDS"'//string) sec4(1)
     write(ou,'(5x,"0/1: grid-point or sph. harm. data"'//string) sec4(2)
     write(ou,'(5x,"0/1: simple or complex packing"'//string) sec4(3)
     write(ou,'(5x,"0/1: floating or integer"'//string) sec4(4)
     write(ou,'(5x,"0/1: No addl flags or addl flags"'//string) sec4(5)
     write(ou,'(5x,"Unused bits"'//string) sec4(6)
     write(ou,'(5x,"Binary Scale Factor"'//string) sec4(7)
     write(ou,'(5x,"Reference Value", t45, ":", F18.8)') xec4(1)
     write(ou,'(5x,"Number of bits for packing"'//string) sec4(8)
  endif

end subroutine gribprint
!
!=============================================================================!
!=============================================================================!
!=============================================================================!
!
subroutine get_bitmap(bm8, ndat)
  use module_grib
  integer, dimension(ndat) :: bm8
  if ((sec1(6).eq.64).or.(sec1(6).eq.192)) then
     bm8 = bitmap
  else
     bm8 = 1
  endif
end subroutine get_bitmap
!
!=============================================================================!
!=============================================================================!
!=============================================================================!
!
subroutine gribxyll(x, y, xlat, xlon)
  use module_grib
  implicit none

  real , intent(in) :: x, y
  real , intent(out) :: xlat, xlon

  real :: r, xkm, ykm, y1
  integer :: iscan, jscan

  if (sec2(4).eq.0) then ! Cylindrical equidistant grid

     xlat = gridinfo(3) + gridinfo(9)*(y-1.)
     xlon = gridinfo(4) + gridinfo(8)*(x-1.)

  elseif (sec2(4) == 1) then      ! Mercator grid
     r = grrth*cosd(gtrue1)
     xkm = (x-1.)*gridinfo(8)
     ykm = (y-1.)*gridinfo(9)
     xlon = gridinfo(4) + (xkm/r)*(180./pi)
     y1 = r*alog((1.+sind(gridinfo(3)))/cosd(gridinfo(3)))/gridinfo(9)
     xlat = 90. - 2. * atan(exp(-gridinfo(9)*(y+y1-1.)/r))*180./pi

  elseif (sec2(4) == 3) then      ! Lambert Conformal grid
     gclon = gridinfo(6)
     r = sqrt((x-1.+gx1)**2 + (y-1+gy1)**2)
     xlat = 90. - 2.*atand(tand(45.-gtrue1/2.)* &
          ((r*gkappa*gridinfo(7))/(grrth*sind(90.-gtrue1)))**(1./gkappa))
     xlon = atan2d((x-1.+gx1),-(y-1.+gy1))/gkappa + gclon

  elseif (sec2(4) == 5) then  ! Polar Stereographic grid
     gclon = gridinfo(6)
     r = sqrt((x-1.+gx1)**2 + (y-1+gy1)**2)
     xlat = 90. - 2.*atan2d((r*gridinfo(7)),(grrth*(1.+sind(gtrue1))))
     xlon = atan2d((x-1.+gx1),-(y-1.+gy1)) + gclon

  elseif (sec2(4) == 4) then ! Gaussian grid

     xlon = gridinfo(4) + gridinfo(8)*(x-1.)
     xlat = gridinfo(3) + gridinfo(19)*(y-1.)

  else
     write(*,'("Unrecognized projection:", I10)') sec2(4)
     write(*,'("STOP in GRIBXYLL")')
     stop
  endif

end subroutine gribxyll
!
!=============================================================================!
!=============================================================================!
!=============================================================================!
!
subroutine gribllxy(xlat, xlon, x, y)
  use module_grib
  implicit none
  real , intent(in) :: xlat, xlon
  real , intent(out) :: x, y

  real :: r, y1

  if (sec2(4) == 0) then      ! Cylindrical Equidistant grid

     x = 1. + (xlon-gridinfo(4)) / gridinfo(9)
     y = 1. + (xlat-gridinfo(3)) / gridinfo(8)

  else if (sec2(4) == 1) then      ! Mercator grid

     r = grrth*cosd(gtrue1)
     x = 1.+( (r/gridinfo(8)) * (xlon-gridinfo(4)) * (pi/180.) )
     y1 = (r/gridinfo(9))*alog((1.+sind(gridinfo(3)))/cosd(gridinfo(3)))
     y = 1. + ((r/gridinfo(9))*alog((1.+sind(xlat))/cosd(xlat)))-y1

  else if (sec2(4) == 3) then      ! Lambert Conformal grid
     gclon = gridinfo(6)
     r = grrth/(gridinfo(7)*gkappa)*sind(90.-gtrue1) * &
          (tand(45.-xlat/2.)/tand(45.-gtrue1/2.)) ** gkappa
     x =  r*sind(gkappa*(xlon-gclon)) - gx1 + 1.
     y = -r*cosd(gkappa*(xlon-gclon)) - gy1 + 1.

  elseif (sec2(4) == 5) then  ! Polar Stereographic grid
     gclon = gridinfo(6)
     r = grrth/gridinfo(7) * tand((90.-xlat)/2.) * (1.+sind(gtrue1))
     x = ( r * sind(xlon-gclon)) - gx1 + 1.
     y = (-r * cosd(xlon-gclon)) - gy1 + 1.

  else
     write(*,'("Unrecognized projection:", I10)') sec2(4)
     write(*,'("STOP in GRIBLLXY")')
     stop
  endif

end subroutine gribllxy
!
!=============================================================================!
!=============================================================================!
!=============================================================================!
!
subroutine glccone (fsplat,ssplat,sign,confac)
  use module_grib
  implicit none
  real, intent(in) :: fsplat,ssplat
  integer, intent(in) :: sign
  real, intent(out) :: confac
  if (abs(fsplat-ssplat).lt.1.E-3) then
     confac = sind(fsplat)
  else
     confac = log10(cosd(fsplat))-log10(cosd(ssplat))
     confac = confac/(log10(tand(45.-float(sign)*fsplat/2.))- &
          log10(tand(45.-float(sign)*ssplat/2.)))
  endif
end subroutine glccone
!
!=============================================================================!
!=============================================================================!
!=============================================================================!
!
!=============================================================================!
!=============================================================================!
!=============================================================================!
!
subroutine gribheader(debug_level,ierr)
!
! IERR non-zero means there was a problem unpacking the grib header.
!
  use module_grib
  implicit none
  integer :: debug_level
  integer :: ierr

  integer, parameter :: nsec1 = 24

  integer, dimension(nsec1) :: &
       iw1=(/3,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,1,1,1,2/)
  integer :: icount, iskip, ibts, nbm, nbz, i9skip, i17skip

  integer :: iman, ichar, isign, iscan

  integer,  allocatable, dimension(:) :: bm8

  real :: r
  integer :: isvns
  integer :: gsvns = 0

  if (gsvns.eq.0) then
     if (mwsize.eq.32) then
        gsvns = transfer('7777', gsvns)
     elseif(mwsize.eq.64) then
        call gbyte_g1(char(0)//char(0)//char(0)//char(0)//'7777', gsvns, 0, mwsize)
     endif
  endif

! Section 0:
  sec0(2) = ied
  if (ied.eq.1) then
     call gbyte_g1(grec, sec0(1), 32, 24)
     iskip = 64
  elseif (ied.eq.0) then
     sec0(1) = gribsize(grec,200, ierr)
     iskip = 32
  endif

! Section 1:
  i9skip = iskip + 80
  i17skip = iskip + 144
  do icount = 1, nsec1 - ((1-ied)*6)
     ibts = iw1(icount)*8
     call gbyte_g1(grec, sec1(icount), iskip, ibts)
     iskip = iskip + ibts
  enddo
  if (ied.eq.0) sec1(22) = 20
  ! Sec1 indices 9 and 10 might actually be one value, not two.
  ! If this is the case, reread sec1(9), and set sec1(10) to zero:
  if ( (sec1(8) == 101) .or. (sec1(8) == 104) .or. (sec1(8) == 106) .or. &
       (sec1(8) == 108) .or. (sec1(8) == 110) .or. (sec1(8) == 112) .or. &
       (sec1(8) == 114) .or. (sec1(8) == 116) .or. (sec1(8) == 120) .or. &
       (sec1(8) == 121) .or. (sec1(8) == 128) .or. (sec1(8) == 141) .or. &
       (sec1(8) == 236) ) then
     ! No action here.
  else
     call gbyte_g1(grec, sec1(9), i9skip, 16)
     sec1(10) = 0.
  endif

  if (sec1(24).ge.32768) sec1(24) = 32768-sec1(24)

  ! If the TIME/RANGE INDICATOR (sec1(19)) indicates that the time P1
  ! is spread over two bytes, then recompute sec1(17) and set sec1(18)
  ! to zero.
  if (sec1(19) == 10) then
     call gbyte_g1(grec, sec1(17), i17skip, 16)
     sec1(18) = 0
  endif

  ! Pull out single bits from sec1(6) for the GDS and BMS flags:
  sec1(25) = sec1(6)/128
  sec1(26) = mod(sec1(6)/64,2)

! Section 2:
! if ((sec1(6) == 128) .or. (sec1(6) == 192)) then
  if (sec1(25) == 1) then

     if (ied.eq.0) then
        iskip = 32 + sec1(1)*8
     elseif (ied.eq.1) then
        iskip = 64 + sec1(1)*8
     endif
     call gbyte_g1(grec, sec2(1), iskip, 24)
     iskip = iskip + 24
     call gbytes_g1(grec, sec2(2), iskip, 8, 0, 3)
     iskip = iskip + 8*3

     if (sec2(4) == 0) then
        ! Lat/Lon Grid:
        call gbytes_g1(grec, infogrid(1), iskip, 16, 0, 2)        
        iskip = iskip + 32
        call gbytes_g1(grec, infogrid(3), iskip, 24, 0, 2)
        iskip = iskip + 48
        call gbyte_g1(grec, infogrid(5), iskip, 8)
        iskip = iskip + 8
        call gbytes_g1(grec, infogrid(6), iskip, 24, 0, 2)
        iskip = iskip + 48
        call gbytes_g1(grec, infogrid(8), iskip, 16, 0, 2)        
        iskip = iskip + 32
        call gbyte_g1(grec, infogrid(21), iskip, 1)
        infogrid(21) = 1-(infogrid(21)*2)
        iskip = iskip + 1
        call gbyte_g1(grec, infogrid(22), iskip, 1)
        infogrid(22) = (infogrid(22)*2)-1
        iskip = iskip + 1
        call gbyte_g1(grec, infogrid(10), iskip, 1)
        iskip = iskip + 1
        iskip = iskip + 5
        call gbyte_g1(grec, infogrid(11), iskip, 32)
        iskip = iskip + 32

!MGD        if ( debug_level .gt. 100 ) then
!MGD        print *, "lat/lon grib grid info", infogrid(1), infogrid(3), &
!MGD        infogrid(5), infogrid(6), infogrid(8), infogrid(21), &
!MGD        infogrid(22), infogrid(10), infogrid(11), infogrid(8)
!MGD        end if

        infogrid(8) = infogrid(8) * infogrid(21)
        infogrid(9) = infogrid(9) * infogrid(22)

        gridinfo(1) = float(infogrid(1))
        gridinfo(2) = float(infogrid(2))
        if (infogrid(3).ge.8388608) infogrid(3) = 8388608 - infogrid(3)
        if (infogrid(4).ge.8388608) infogrid(4) = 8388608 - infogrid(4)
        gridinfo(3) = float(infogrid(3))*0.001
        gridinfo(4) = infogrid(4) * 0.001
        if (infogrid(6).ge.8388608) infogrid(6) = 8388608 - infogrid(6)
        if (infogrid(7).ge.8388608) infogrid(7) = 8388608 - infogrid(7)
        gridinfo(6) = infogrid(6) * 0.001
        gridinfo(7) = infogrid(7) * 0.001
        gridinfo(8) = infogrid(8) * 0.001
        gridinfo(9) = infogrid(9) * 0.001
        gridinfo(21) = float(infogrid(21))
        gridinfo(22) = float(infogrid(22))
     elseif (sec2(4) == 1) then ! Mercator grid
        ! Number of points in X and Y
        call gbytes_g1(grec, infogrid(1), iskip, 16, 0, 2)
        iskip = iskip + 32
        ! Starting lat and lon
        call gbytes_g1(grec, infogrid(3), iskip, 24, 0, 2)
        iskip = iskip + 48
        ! "Resolution and component flags"
        call gbyte_g1(grec, infogrid(5), iskip, 8)
        iskip = iskip + 8
        ! Ending lat and lon
        call gbytes_g1(grec, infogrid(6), iskip, 24, 0, 2)
        iskip = iskip + 48
        ! Truelat, 3 bytes
        call gbyte_g1(grec, infogrid(11), iskip, 24)
        iskip = iskip + 24
        ! "Reserved", i.e., skip a byte
        iskip = iskip + 8
        ! Scanning mode flags, first three bits of the next byte
        ! and skip the last five bits.
        call gbyte_g1(grec, infogrid(21), iskip, 1)
        infogrid(21) = 1-(infogrid(21)*2)
        iskip = iskip + 1
        call gbyte_g1(grec, infogrid(22), iskip, 1)
        infogrid(22) = (infogrid(22)*2)-1
        iskip = iskip + 1
        call gbyte_g1(grec, infogrid(10), iskip, 1)
        iskip = iskip + 1
        iskip = iskip + 5
        ! Grid increment in X and Y
        call gbytes_g1(grec, infogrid(8), iskip, 24, 0, 2)
        iskip = iskip + 48
        ! Done reading map specifications.
        ! Now do various conversions:

        gridinfo(1) = float(infogrid(1)) ! ok
        gridinfo(2) = float(infogrid(2)) ! ok

        if (infogrid(3) .ge.8388608) infogrid(3)  = 8388608 - infogrid(3)
        if (infogrid(4) .ge.8388608) infogrid(4)  = 8388608 - infogrid(4)
        if (infogrid(6) .ge.8388608) infogrid(6)  = 8388608 - infogrid(6)
        if (infogrid(7) .ge.8388608) infogrid(7)  = 8388608 - infogrid(7)
        if (infogrid(11).ge.8388608) infogrid(11) = 8388608 - infogrid(11)
        gridinfo(3)  = infogrid(3)  * 0.001
        gridinfo(4)  = infogrid(4)  * 0.001
        gridinfo(6)  = infogrid(6)  * 0.001
        gridinfo(7)  = infogrid(7)  * 0.001
        gridinfo(8)  = infogrid(8)  * 0.001
        gridinfo(9)  = infogrid(9)  * 0.001
        gridinfo(11) = infogrid(11) * 0.001

        gridinfo(21) = infogrid(21)
        gridinfo(22) = infogrid(22)

        gridinfo(20) = 6370.949
        grrth = gridinfo(20)
        gtrue1 = gridinfo(11)

     elseif (sec2(4) == 3) then
        if (ied.eq.0) then
           print '(//,"*** Despair ***"//)'
           stop
        endif
! Lambert Conformal:
        call gbytes_g1(grec, infogrid(1), iskip, 16, 0, 2)        
        iskip = iskip + 32
        call gbytes_g1(grec, infogrid(3), iskip, 24, 0, 2)
        iskip = iskip + 48
        if (infogrid(3).ge.8388608) infogrid(3) = 8388608 - infogrid(3)
        if (infogrid(4).ge.8388608) infogrid(4) = 8388608 - infogrid(4)
        call gbyte_g1(grec, infogrid(5), iskip, 8)
        iskip = iskip + 8
        call gbytes_g1(grec, infogrid(6), iskip, 24, 0, 3)
        if (infogrid(6).ge.8388608) infogrid(6) = 8388608 - infogrid(6)
        iskip = iskip + 72
        call gbyte_g1(grec, infogrid(9), iskip, 8)
        iskip = iskip + 8
        call gbyte_g1(grec, infogrid(21), iskip, 1)
        infogrid(21) = 1-(infogrid(21)*2)
        iskip = iskip + 1
        call gbyte_g1(grec, infogrid(22), iskip, 1)
        infogrid(22) = (infogrid(22)*2)-1
        iskip = iskip + 1
        call gbyte_g1(grec, infogrid(10), iskip, 1)
        iskip = iskip + 1
        iskip = iskip + 5
        call gbytes_g1(grec, infogrid(11), iskip, 24, 0, 4)
        if (infogrid(11).ge.8388608) infogrid(11) = 8388608 - infogrid(11)
        if (infogrid(12).ge.8388608) infogrid(12) = 8388608 - infogrid(12)
        if (infogrid(13).ge.8388608) infogrid(13) = 8388608 - infogrid(13)
        if (infogrid(14).ge.8388608) infogrid(14) = 8388608 - infogrid(14)
        iskip = iskip + 96
        call gbyte_g1(grec, infogrid(15), iskip, 16)
        iskip = iskip + 16

        infogrid(7) = infogrid(7) * infogrid(21)
        infogrid(8) = infogrid(8) * infogrid(22)


        gridinfo(1) = float(infogrid(1))
        gridinfo(2) = float(infogrid(2))
        gridinfo(3) = infogrid(3) * 0.001
        gridinfo(4) = infogrid(4) * 0.001
        gridinfo(6) = infogrid(6) * 0.001
        gridinfo(7) = infogrid(7) * 0.001
        gridinfo(8) = infogrid(8) * 0.001
        gridinfo(9) = infogrid(9) * 0.001
        gridinfo(11) = infogrid(11) * 0.001
        gridinfo(12) = infogrid(12) * 0.001
        gridinfo(13) = infogrid(13) * 0.001
        gridinfo(14) = infogrid(14) * 0.001

        gridinfo(20) = 6370
        ! a priori knowledge:
        if (sec1(5).eq.212) then
           gridinfo(3) = 12.190
           gridinfo(4) = -133.459
           gridinfo(7) = 40.63525
           gridinfo(8) = 40.63525
           gridinfo(20) = 6370
        endif

!=============================================================================!
! More a priori knowledge:                                                    !
! Correct some bad lat/lon numbers coded into some RUC headers.               !
!                                                                             !
        if (sec1(3) == 59) then  ! If FSL
           if (sec1(4) == 86) then  ! and RUC
              if (sec1(5) == 255) then 
           ! Check to correct bad lat/lon numbers.
                 if (infogrid(3) == 16909) then
                    infogrid(3) = 16281
                    gridinfo(3) = 16.281
                 endif
                 if (infogrid(4) == 236809) then
                    infogrid(4) = 2338622
                    gridinfo(4) = 233.8622
                 endif
              endif
           endif
        endif
!=============================================================================!


        gridinfo(21) = float(infogrid(21))
        gridinfo(22) = float(infogrid(22))

        ! Map parameters
        glat1 = gridinfo(3)
        glon1 = gridinfo(4)
        gclon = gridinfo(6)
        if (gclon.gt.180.) gclon = -(360.-gclon)
        if ((gclon<0).and.(glon1>180)) glon1 = glon1-360.  
        gtrue1 = gridinfo(11)
        gtrue2 = gridinfo(12)
        grrth = gridinfo(20)
        call glccone(gtrue1, gtrue2, 1, gkappa)
        r = grrth/(gridinfo(7)*gkappa)*sind(90.-gtrue1) * &
             (tand(45.-glat1/2.)/tand(45.-gtrue1/2.)) ** gkappa
        gx1 =  r*sind(gkappa*(glon1-gclon))
        gy1 = -r*cosd(gkappa*(glon1-gclon))

     elseif (sec2(4) == 4) then
        ! Gaussian Grid:
        call gbytes_g1(grec, infogrid(1), iskip, 16, 0, 2)        
        iskip = iskip + 32
        call gbytes_g1(grec, infogrid(3), iskip, 24, 0, 2)
        iskip = iskip + 48
        call gbyte_g1(grec, infogrid(5), iskip, 8)
        iskip = iskip + 8
        call gbytes_g1(grec, infogrid(6), iskip, 24, 0, 2)
        iskip = iskip + 48
        call gbytes_g1(grec, infogrid(8), iskip, 16, 0, 2)        
        iskip = iskip + 32
        call gbyte_g1(grec, infogrid(21), iskip, 1)
        infogrid(21) = 1-(infogrid(21)*2)
        iskip = iskip + 1
        call gbyte_g1(grec, infogrid(22), iskip, 1)
        infogrid(22) = (infogrid(22)*2)-1
        iskip = iskip + 1
        call gbyte_g1(grec, infogrid(10), iskip, 1)
        iskip = iskip + 1
        iskip = iskip + 5
        call gbyte_g1(grec, infogrid(11), iskip, 32)
        iskip = iskip + 32

        infogrid(8) = infogrid(8) * infogrid(21)

        gridinfo(1) = float(infogrid(1))
        gridinfo(2) = float(infogrid(2))
        if (infogrid(3).ge.8388608) infogrid(3) = 8388608 - infogrid(3)
        if (infogrid(4).ge.8388608) infogrid(4) = 8388608 - infogrid(4)
        gridinfo(3) = float(infogrid(3))*0.001
        gridinfo(4) = infogrid(4) * 0.001
        if (infogrid(6).ge.8388608) infogrid(6) = 8388608 - infogrid(6)
        if (infogrid(7).ge.8388608) infogrid(7) = 8388608 - infogrid(7)
        gridinfo(6) = infogrid(6) * 0.001
        gridinfo(7) = infogrid(7) * 0.001
        gridinfo(8) = infogrid(8) * 0.001
        gridinfo(21) = float(infogrid(21))
        gridinfo(22) = float(infogrid(22))

        ! Compute an approximate delta-latitude and starting latitude.
        ! Replace the stored value of starting latitude with approximate one.
        gridinfo(18) = gridinfo(3)
        infogrid(18) = infogrid(3)
        gridinfo(17) = gridinfo(6)
        infogrid(17) = infogrid(6)
!        call griblgg(infogrid(2), gridinfo(3), gridinfo(19))
!        infogrid(19) = nint(gridinfo(19)*1000.)
!        infogrid(3) = nint(gridinfo(3)*1000.)
        gridinfo(6) = -gridinfo(3)
        infogrid(6) = -infogrid(3)

     elseif (sec2(4) == 5) then
! Polar Stereographic Grid
        if (ied.eq.0) then
           print '(//,"*** Despair ***"//)'
           stop
        endif
        call gbytes_g1(grec, infogrid(1), iskip, 16, 0, 2)  ! NX and NY
        iskip = iskip + 32
        call gbytes_g1(grec, infogrid(3), iskip, 24, 0, 2)  ! LAT1 and LON1
        iskip = iskip + 48
        call gbyte_g1(grec, infogrid(5), iskip, 8) ! Resolution and Component
        iskip = iskip + 8
        call gbytes_g1(grec, infogrid(6), iskip, 24, 0, 3) ! LOV, DX, and DY
        iskip = iskip + 72
        call gbyte_g1(grec, infogrid(9), iskip, 8) ! Projection center flag
        iskip = iskip + 8
        call gbyte_g1(grec, infogrid(21), iskip, 1)
        infogrid(21) = 1-(infogrid(21)*2)
        iskip = iskip + 1
        call gbyte_g1(grec, infogrid(22), iskip, 1)
        infogrid(22) = (infogrid(22)*2)-1
        iskip = iskip + 1
        call gbyte_g1(grec, infogrid(10), iskip, 1)
        iskip = iskip + 1
        iskip = iskip + 5
!         call gbyte_g1(grec, infogrid(11), iskip, 32) ! Set to 0 (reserved)
        iskip = iskip + 32

        if (infogrid(3).ge.8388608) infogrid(3) = 8388608 - infogrid(3)
        if (infogrid(4).ge.8388608) infogrid(4) = 8388608 - infogrid(4)
        if (infogrid(6).ge.8388608) infogrid(6) = 8388608 - infogrid(6)


        infogrid(7) = infogrid(7) * infogrid(21)
        infogrid(8) = infogrid(8) * infogrid(22)

        gridinfo(1) = float(infogrid(1))
        gridinfo(2) = float(infogrid(2))
        gridinfo(3) = infogrid(3) * 0.001
        gridinfo(4) = infogrid(4) * 0.001
        gridinfo(6) = infogrid(6) * 0.001
        gridinfo(7) = infogrid(7) * 0.001
        gridinfo(8) = infogrid(8) * 0.001

        gridinfo(20) = 6370

        ! a priori knowledge:
        if (sec1(5).eq.240) then
           gridinfo(3) = 22.7736
           gridinfo(4) = -120.376
           gridinfo(7) = 4.7625
           gridinfo(8) = 4.7625
           gridinfo(20) = 6370
        endif

        ! Map parameters
        glat1 = gridinfo(3)
        glon1 = gridinfo(4)
        gclon = gridinfo(6)
        if (gclon.gt.180.) gclon = -(360.-gclon)
        ! GRIB edition 1 Polar Stereographic grids are true at 60 degrees
        ! Which hemisphere depends on infogrid(9), the "Projection Center Flag"
        grrth = gridinfo(20)
        if (infogrid(9) > 127) then
           gtrue1 = -60. 
           r = grrth/gridinfo(7) * tand((-90.-glat1)/2.) * (1.+sind(-gtrue1))
           gx1 = -r * sind(glon1-gridinfo(6))
           gy1 = -r * cosd(glon1-gridinfo(6)) 
        else
           gtrue1 = 60. 
           r = grrth/gridinfo(7) * tand((90.-glat1)/2.) * (1.+sind(gtrue1))
           gx1 = r * sind(glon1-gridinfo(6))
           gy1 = -r * cosd(glon1-gridinfo(6))
        endif

        gridinfo(21) = float(infogrid(21))
        gridinfo(22) = float(infogrid(22))

     elseif (sec2(4) == 50) then
! Spherical harmonic coefficients
        if (ied.eq.0) then
           print '(//,"*** Despair ***"//)'
           stop
        endif
        call gbytes_g1(grec, infogrid(1), iskip, 16, 0, 3)
        iskip = iskip + 48
        call gbytes_g1(grec, infogrid(4), iskip, 8, 0, 2)
        iskip = iskip + 16

        iskip = iskip + 18*8

     else
        call gribprint(0)
        call gribprint(1)
        call gribprint(2)
        call gribprint(3)
        call gribprint(4)
        write(*,'("Unrecognized grid: ", i8)') sec2(4)
        write(*,'("This grid is not currently supported.")')
        write(*,'("Write your own program to put the data to the intermediate format")')
        stop
     endif

  endif

! Section 3
  if ((sec1(6).eq.64).or.(sec1(6).eq.192)) then
     if (ied.eq.0) then
        print '(//,"*** Despair ***"//)'
        stop
     endif

     if (ied.eq.0) then
        iskip = 32 + sec1(1)*8 + sec2(1)*8
     elseif (ied.eq.1) then
        iskip = 64 + sec1(1)*8 + sec2(1)*8
     endif
     call gbyte_g1(grec, sec3(1), iskip, 24)
     iskip = iskip + 24
     call gbyte_g1(grec, sec3(2), iskip, 8)
     iskip = iskip + 8
     call gbyte_g1(grec, sec3(3), iskip, 16)
     iskip = iskip + 16

#if defined (CRAY)
#else
     allocate(bitmap((sec3(1)-6)*8))
#endif
     allocate(bm8((sec3(1)-6)*8))
     call gbytes_g1(grec, bm8, iskip, 1, 0, (sec3(1)-6)*8)
     bitmap(1:size(bm8)) = bm8(1:size(bm8))
     deallocate(bm8)
     iskip = iskip + sec3(1)-6
  else
     sec3 = 0
  endif

! Section 4
  if ((sec1(6).eq.128).or.(sec1(6).eq.192)) then
     if (ied.eq.0) then
        iskip = 32 + sec1(1)*8 + sec2(1)*8 + sec3(1)*8
     elseif (ied.eq.1) then
        iskip = 64 + sec1(1)*8 + sec2(1)*8 + sec3(1)*8
     endif
     call gbyte_g1(grec, sec4(1), iskip, 24)
     if (sec4(1) > (sec0(1) - sec1(1) - sec2(1) - sec3(1) - 4)) then
        write(*,'(/,"*** I have good reason to believe that this GRIB record is")')
        write(*,'("*** corrupted or miscoded.",/)')
        ierr = 1
        return
     endif
     iskip = iskip + 24
     call gbytes_g1(grec, sec4(2), iskip, 1,0,4)
     iskip = iskip + 4
     call gbyte_g1(grec, sec4(6), iskip, 4)
     iskip = iskip + 4
! Get the binary scale factor
     call gbyte_g1(grec, isign, iskip, 1)
     iskip = iskip + 1
     call gbyte_g1(grec, sec4(7), iskip, 15)
     iskip = iskip + 15
     sec4(7) = sec4(7) * (-2*isign+1)
! Get the reference value:
     call gbyte_g1(grec, isign, iskip, 1)
     iskip = iskip + 1
     isign = -2*isign+1
     call gbyte_g1(grec, ichar, iskip, 7)
     iskip = iskip + 7
     call gbyte_g1(grec, iman, iskip, 24)
     iskip = iskip + 24
     if ( iman .ne. 0 ) then
       xec4(1) = float(isign) * (2.**(-24)) * float(iman) *  &
          (16.**(ichar-64))
     else
       xec4(1) = 0.
     endif

     call gbyte_g1(grec,sec4(8), iskip, 8)
! sec4(8) is the number of bits used per datum value.
! If sec4(8) = 255, assume they mean sec4(8) = 0
     if (sec4(8) == 255) sec4(8) = 0
     iskip = iskip + 8
  endif

! Section 5
  call gbyte_g1(grec, isvns, ((sec0(1)-4)*8), 32)
  if (isvns.ne.gsvns) then
     write(*, '("End-of-record mark (7777) not found", 2I10)') isvns
     write(*, '("Sec0(1) = ", I8, i2)') sec0(1), sevencount
     sevencount = sevencount + 1
     if (sevencount > 10) then
        write(*,'(//," *** Found more than 10 consecutive bad GRIB records")')
        write(*,'(" *** Let''s just stop now.",//)')
        write(*,'(" Perhaps the analysis file should have been converted",/,&
             &" from COS-Blocked format?",//)')
        stop
     endif
  else
     sevencount = 0
  endif

  ierr = 0

end subroutine gribheader
!
!=============================================================================!
!=============================================================================!
!=============================================================================!
!
  subroutine gribdata(datarray, ndat) 

!-----------------------------------------------------------------------------!
!                                                                             !
! Read and unpack the data from a GRIB record.                                !
!                                                                             !
! Input:                                                                      !
!    NDAT:  The size of the data array we expect to unpack.                   !
!                                                                             !
! Output:                                                                     !
!    DATARRAY:  The unpacked data from the GRIB record                        !
!                                                                             !
! Side Effects:                                                               !
!    STOP if it cannot unpack the data.                                       !
!                                                                             !
! Externals:                                                                  !
!     SGUP_BITMAP                                                             !
!     SGUP_NOBITMAP                                                           !
!     CSHUP                                                                   !
!                                                                             !
! Modules:                                                                    !
!     MODULE_GRIB                                                             !
!                                                                             !
!-----------------------------------------------------------------------------!
    use module_grib

    implicit none

    integer :: ndat
    real , dimension(ndat) :: datarray
    integer, dimension(ndat) :: IX

    integer :: iskip, nbm

    if (sec4(2) == 0) then ! Grid-point data
       if (sec4(3).eq.0) then ! Simple unpacking
          if ((sec1(6).eq.64).or.(sec1(6).eq.192)) then ! There is a bitmap
             call SGUP_BITMAP(datarray, ndat)
          else
             call SGUP_NOBITMAP(datarray, ndat)
          endif
       else
          write(*,'(//,"***** No complex unpacking of gridpoint data.")')
          write(*,'("***** Option not yet available.",//)')
!         write(*,'("***** Complain to mesouser@ucar.edu",//)')
          stop
       endif
    else
       if (sec4(3).eq.0) then ! Simple unpacking
          write(*,'(//,"***** No simple unpacking of spherical-harmonic coefficients.")')
          write(*,'("***** Option not yet available.",//)')
!         write(*,'("***** Complain to mesouser@ucar.edu",//)')
          stop
       elseif (sec4(3).eq.1) then
          call CSHUP(datarray, ndat)
       endif
    endif

end subroutine gribdata

subroutine deallogrib
! Deallocates a couple of arrays that may be allocated.
  use module_grib
#if defined (CRAY)
#else
  if (allocated(grec)) deallocate(grec)
  if (allocated(bitmap)) deallocate(bitmap)
#endif
end subroutine deallogrib

SUBROUTINE gribLGG( NLAT, startlat, deltalat )


  implicit none
!
!  LGGAUS finds the Gaussian latitudes by finding the roots of the
!  ordinary Legendre polynomial of degree NLAT using Newtons
!  iteration method.
!
!  On entry:
  integer NLAT ! the number of latitudes (degree of the polynomial)
!
!  On exit: for each Gaussian latitude

  double precision, dimension(NLAT) :: LATG ! Latitude

! Approximations to a regular latitude grid:
  real :: deltalat
  real :: startlat

!-----------------------------------------------------------------------

  integer :: iskip = 15
  double precision :: sum1 = 0.
  double precision :: sum2 = 0.
  double precision :: sum3 = 0.
  double precision :: sum4 = 0.
  double precision :: xn

  integer, save :: SAVE_NLAT = -99
  real, save :: save_deltalat = -99.
  real, save :: save_startlat = -99.

  double precision, dimension(nlat) ::  COSC, SINC
  double precision, parameter :: PI = 3.141592653589793
!
!    -convergence criterion for iteration of cos latitude
  double precision, parameter :: XLIM  = 1.0E-14
  integer :: nzero, i, j
  double precision :: fi, fi1, a, b, g, gm, gp, gt, delta, c, d

  if (nlat == save_nlat) then
     deltalat = save_deltalat
     startlat = save_startlat
     return
  endif
!
!    -the number of zeros between pole and equator
  NZERO = NLAT/2
!
!    -set first guess for cos(colat)
  DO I=1,NZERO
     COSC(I) = SIN( (I-0.5)*PI/NLAT + PI*0.5 )
  ENDDO
!
!    -constants for determining the derivative of the polynomial
  FI  = NLAT
  FI1 = FI+1.0
  A   = FI*FI1 / SQRT(4.0*FI1*FI1-1.0)
  B   = FI1*FI / SQRT(4.0*FI*FI-1.0)
!
!    -loop over latitudes, iterating the search for each root
  DO I=1,NZERO
     J=0
!
!       -determine the value of the ordinary Legendre polynomial for
!       -the current guess root
     LOOP30 : DO 
        CALL LGORD( G, COSC(I), NLAT )
!
!       -determine the derivative of the polynomial at this point
        CALL LGORD( GM, COSC(I), NLAT-1 )
        CALL LGORD( GP, COSC(I), NLAT+1 )
        GT = (COSC(I)*COSC(I)-1.0) / (A*GP-B*GM)
!
!       -update the estimate of the root
        DELTA   = G*GT
        COSC(I) = COSC(I) - DELTA
!
!       -if convergence criterion has not been met, keep trying
        J = J+1
        IF( ABS(DELTA).LE.XLIM ) EXIT LOOP30
     ENDDO LOOP30
  ENDDO
!
!  Determine the sin(colat)
  SINC(1:NZERO) = SIN(ACOS(COSC(1:NZERO)))
!
!    -if NLAT is odd, set values at the equator
  IF( MOD(NLAT,2) .NE. 0 ) THEN
     I       = NZERO+1
     SINC(I) = 1.0
     latg(i) = 0.
  END IF

! Set the latitudes.

  latg(1:NZERO) = dacos(sinc(1:NZERO)) * 180. / pi

! Determine the southern hemisphere values by symmetry
  do i = 1, nzero
     latg(nlat-nzero+i) = -latg(nzero+1-i)
  enddo


! Now that we have the true values, find some approximate values.

  xn = float(nlat-iskip*2)
  do i = iskip+1, nlat-iskip
     sum1 = sum1 + latg(i)*float(i)
     sum2 = sum2 + float(i)
     sum3 = sum3 + latg(i)
     sum4 = sum4 + float(i)**2
  enddo

  b = (xn*sum1 - sum2*sum3) / (xn*sum4 - sum2**2)
  a = (sum3 - b * sum2) / xn

  deltalat = sngl(b)
  startlat = sngl(a + b)

  save_nlat = nlat
  save_deltalat = deltalat
  save_startlat = startlat

contains
  SUBROUTINE LGORD( F, COSC, N )
    implicit none
!
!  LGORD calculates the value of an ordinary Legendre polynomial at a
!  latitude.
!
!  On entry:
!     COSC - cos(colatitude)
!     N      - the degree of the polynomial
!
!  On exit:
!     F      - the value of the Legendre polynomial of degree N at
!              latitude asin(COSC)
    double precision :: s1, c4, a, b, fk, f, cosc, colat, c1, fn, ang
    integer :: n, k

!------------------------------------------------------------------------

    colat = acos(cosc)
    c1 = sqrt(2.0)
    do k=1,n
       c1 = c1 * sqrt( 1.0 - 1.0/(4*k*k) )
    enddo
    fn = n
    ang= fn * colat
    s1 = 0.0
    c4 = 1.0
    a  =-1.0
    b  = 0.0
    do k=0,n,2
       if (k.eq.n) c4 = 0.5 * c4
       s1 = s1 + c4 * cos(ang)
       a  = a + 2.0
       b  = b + 1.0
       fk = k
       ang= colat * (fn-fk-2.0)
       c4 = ( a * (fn-b+1.0) / ( b * (fn+fn-a) ) ) * c4
    enddo
    f = s1 * c1
  end subroutine lgord

END SUBROUTINE GRIBLGG

SUBROUTINE REORDER_IT (a, nx, ny, dx, dy, iorder)

      use module_debug

      implicit none
      integer :: nx, ny, iorder
      integer :: i, j, k, m
      real :: dx, dy
      real, dimension(nx*ny) :: a, z

      if (iorder .eq. 0 .and. dx .gt. 0. .and. dy .lt. 0) return
      k = 0
      call mprintf(.true.,DEBUG, &
        "Reordering GRIB array : dx = %f  , dy = %f  , iorder = %i",  &
	 f1=dx,f2=dy,i1=iorder)
      if (iorder .eq. 0 ) then
	if ( dx .lt. 0 .and. dy .lt. 0. ) then
	  do j = 1, ny
	  do i = nx, 1, -1
	    k = k + 1
	    m = i * j
	    z(k) = a(m)
	  enddo
	  enddo
        else if ( dx .lt. 0 .and. dy .gt. 0. ) then
	  do j = ny, 1, -1
	  do i = nx, 1, -1
	    k = k + 1
	    m = i * j
	    z(k) = a(m)
	  enddo
	  enddo
        else if ( dx .gt. 0 .and. dy .gt. 0. ) then
	  do j = ny, 1, -1
	  do i = 1, nx
	    k = k + 1
	    m = i * j
	    z(k) = a(m)
	  enddo
	  enddo
        endif
      else
	if ( dx .gt. 0 .and. dy .lt. 0. ) then
	  do i = 1, nx
	  do j = 1, ny
	    k = k + 1
	    m = i * j
	    z(k) = a(m)
	  enddo
	  enddo
        else if ( dx .lt. 0 .and. dy .lt. 0. ) then
	  do i = nx, 1, -1
	  do j = 1, ny
	    k = k + 1
	    m = i * j
	    z(k) = a(m)
	  enddo
	  enddo
        else if ( dx .lt. 0 .and. dy .lt. 0. ) then
	  do i = nx, 1, -1
	  do j = ny, 1, -1
	    k = k + 1
	    m = i * j
	    z(k) = a(m)
	  enddo
	  enddo
        else if ( dx .gt. 0 .and. dy .gt. 0. ) then
	  do i = 1, nx
	  do j = ny, 1, -1
	    k = k + 1
	    m = i * j
	    z(k) = a(m)
	  enddo
	  enddo
        endif
      endif
!  now put it back in the 1-d array and reset the dx and dy
      do k = 1, nx*ny
        a(k) = z(k)
      enddo
      dx = abs ( dx)
      dy = -1 * abs(dy)
      return
END SUBROUTINE REORDER_IT
Back to Top