File: m_inpak90.F90

package info (click to toggle)
oasis3 3.mct%2Bdfsg.121022-15
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 61,868 kB
  • sloc: f90: 40,319; fortran: 5,859; ansic: 2,780; perl: 1,239; sh: 728; makefile: 706; xml: 278; awk: 25; csh: 7
file content (2049 lines) | stat: -rw-r--r-- 57,247 bytes parent folder | download | duplicates (10)
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
!-------------------------------------------------------------------------
!         NASA/GSFC, Data Assimilation Office, Code 910.3, GEOS/DAS      !
!-------------------------------------------------------------------------
! CVS m_inpak90.F90,v 1.8 2006-12-19 00:22:35 jacob Exp
! CVS MCT_2_8_0  
!-------------------------------------------------------------------------
!BOI
!
! !TITLE: Inpak 90 Documentation \\ Version 1.01
!
! !AUTHORS: Arlindo da Silva
!
! !AFFILIATION: Data Assimilation Office, NASA/GSFC, Greenbelt, MD 20771
!
! !DATE: June 20, 1996
!
! !INTRODUCTION: Package Overview
!
!      Inpak 90 is a Fortran (77/90) collection of 
!      routines/functions for accessing {\em Resource Files} 
!      in ASCII format. The package is optimized
!      for minimizing formatted I/O, performing all of its string
!      operations in memory using Fortran intrinsic functions.
!
! \subsection{Resource Files}
!
!      A {\em Resource File} is a text file consisting of variable
!     length lines (records), each possibly starting with a {\em label}
!     (or {\em key}), followed by some data. A simple resource file 
!     looks like this:
!
! \begin{verbatim}
! # Lines starting with # are comments which are
! # ignored during processing.
! my_file_names:         jan87.dat jan88.dat jan89.dat
! radius_of_the_earth:   6.37E6  # these are comments too
! constants:             3.1415   25
! my_favourite_colors:   green blue 022 # text & number are OK
! \end{verbatim}
!
!    In this example, {\tt my\_file\_names:} and {\tt constants:}
!    are labels, while {\tt jan87.dat, jan88.dat} and {\tt jan89.dat} are
!    data associated with label {\tt my\_file\_names:}.
!    Resource files can also contain simple tables of the form,
!
! \begin{verbatim}
! my_table_name::
!  1000     3000     263.0   
!   925     3000     263.0
!   850     3000     263.0
!   700     3000     269.0
!   500     3000     287.0
!   400     3000     295.8
!   300     3000     295.8    
! ::
! \end{verbatim}
!
! Resource files are random access, the particular order of the
! records are not important (except between ::s in a table definition).
!
!    \subsection{A Quick Stroll}
!
!    The first step is to load the ASCII resource (rc) file into
!    memory\footnote{See next section for a complete description
!    of parameters for each routine/function}:
!
! \begin{verbatim}
!       call i90_LoadF ( 'my_file.rc', iret )
! \end{verbatim}
!
!    The next step is to select the label (record) of interest, say
!
! \begin{verbatim}
!       call i90_label ( 'constants:', iret )
! \end{verbatim}
!
!  The 2 constants above can be retrieved with the following code
!  fragment:
! \begin{verbatim}
!       real    r
!       integer i
!       call i90_label ( 'constants:', iret )
!       r = i90_gfloat(iret)    ! results in r = 3.1415
!       i = i90_gint(iret)      ! results in i = 25
! \end{verbatim}
!
!  The file names above can be retrieved with the following
!  code fragment:
! \begin{verbatim}
!       character*20 fn1, fn2, fn3
!       integer      iret
!       call i90_label ( 'my_file_names:', iret )
!       call i90_Gtoken ( fn1, iret )  ! ==> fn1 = 'jan87.dat'
!       call i90_Gtoken ( fn2, iret )  ! ==> fn1 = 'jan88.dat'
!       call i90_Gtoken ( fn3, iret )  ! ==> fn1 = 'jan89.dat'
! \end{verbatim}
!
! To access the table above, the user first must use {\tt i90\_label()} to 
! locate the beginning of the table, e.g.,
!
! \begin{verbatim}
!       call i90_label ( 'my_table_name::', iret )
! \end{verbatim}
!
! Subsequently, {\tt i90\_gline()} can be used to gain access to each
! row of the table. Here is a code fragment to read the above
! table (7 rows, 3 columns):
!
! \begin{verbatim}
!       real          table(7,3)
!       character*20  word
!       integer       iret
!       call i90_label ( 'my_table_name::', iret )
!       do i = 1, 7
!          call i90_gline ( iret )
!          do j = 1, 3
!             table(i,j) = i90_gfloat ( iret )
!          end do                   
!       end do
! \end{verbatim}
!
! Get the idea?
! 
! \newpage
! \subsection{Main Routine/Functions}
!
! \begin{verbatim}
!  ------------------------------------------------------------------
!         Routine/Function                  Description
!  ------------------------------------------------------------------
!  I90_LoadF ( filen, iret )     loads resource file into memory
!  I90_Label ( label, iret )     selects a label (key)
!  I90_GLine ( iret )            selects next line (for tables)
!  I90_Gtoken ( word, iret )     get next token 
!  I90_Gfloat ( iret )           returns next float number (function)
!  I90_GInt ( iret )             returns next integer number (function)
!  i90_AtoF ( string, iret )     ASCII to float (function)
!  i90_AtoI ( string, iret )     ASCII to integer (function)
!  I90_Len ( string )            string length without trailing blanks
!  LabLin ( label )              similar to i90_label (no iret)
!  FltGet ( default )            returns next float number (function)
!  IntGet ( default )            returns next integer number (function)
!  ChrGet ( default )            returns next character (function)
!  TokGet ( string, default )    get next token
!  ------------------------------------------------------------------
! \end{verbatim}
!
! {\em Common Arguments:}
!
! \begin{verbatim}
! character*(*)      filen       file name
! integer            iret        error return code (0 is OK)
! character*(*)      label       label (key) to locate record
! character*(*)      word        blank delimited string
! character*(*)      string      a sequence of characters
! \end{verbatim}
!
! See the Prologues in the next section for additional details.
!
!
!    \subsection{Package History} 
!       Back in the 70s Eli Isaacson wrote IOPACK in Fortran
!       66.  In June of 1987 I wrote Inpak77 using
!       Fortran 77 string functions; Inpak 77 is a vastly
!       simplified IOPACK, but has its own goodies not found in
!       IOPACK.  Inpak 90 removes some obsolete functionality in
!       Inpak77, and parses the whole resource file in memory for
!       performance.  Despite its name, Inpak 90 compiles fine
!       under any modern Fortran 77 compiler.
!
!   \subsection{Bugs} 
!       Inpak 90 is not very gracious with error messages.  
!       The interactive functionality of Inpak77 has not been implemented.
!       The comment character \# cannot be escaped.
!
!  \subsection{Availability}
!
!   This software is available at 
! \begin{verbatim}
!         ftp://niteroi.gsfc.nasa.gov/pub/packages/i90/ 
! \end{verbatim}
!   There you will find the following files:
! \begin{verbatim}
! i90.f      Fortran 77/90 source code
! i90.h      Include file needed by i90.f
! ti90.f     Test code
! i90.ps     Postscript documentation
! \end{verbatim}
! An on-line version of this document is available at
! \begin{verbatim}
!        ftp://niteroi.gsfc.nasa.gov/www/packages/i90/i90.html
! \end{verbatim}
!
!EOI
!-------------------------------------------------------------------------
!~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
!       NASA/GSFC, Data Assimilation Office, Code 910.3, GEOS/DAS      !
!-----------------------------------------------------------------------
!
! !REVISION HISTORY:
! 	03Jul96 - J. Guo	- evolved to Fortran 90 module.  The
!		modifications include 1) additional subroutines to
!		dynamically manage the memory, 2) privatized most
!		entries, 3) included "i90.h" into the module source
!		with better initializations, 4) removed blockdata, 5)
!		used a portable opntext() call to avoid I/O portability
!		problems.
!
!		See I90_page() I90_Release(), and I90_LoadF() for
!		details.
!
!	05Aug98	- Jing Guo	-
!		  Removed i90_page() and its references.
!		  Added internal subroutines push_() and pop_().
!		  Modified i90_release().
!		  Added i90_fullrelease().
!		  Removed %loaded.  Check i90_depth instead.
!       06Aug98 - Todling       - made I90_gstr public
!	20Dec98 - Jing Guo	- replaced the description of I90_Gstr
!	28Sep99 - Jing Guo	- Merged with the MPI version with
!				  some addtional changes based on
!				  merging decisions.
!	12Oct99 - Larson/Guo	- Overloaded fltget() to new routines 
!                 getfltsp() and fltgetdp(), providing better support 
!                 for 32 and 64 bit platforms, respectively.
!_______________________________________________________________________

     module m_inpak90
     use m_stdio, only : stderr,stdout
     use m_realkinds, only: FP, SP, DP,kind_r8
     implicit none
     private
     public :: I90_LoadF   ! loads a resource file into memory
     public :: I90_allLoadF! loads/populates a resource file to all PEs
     public :: I90_Release ! Releases one cached resource file
     public :: I90_fullRelease ! Releases the whole stack
     public :: I90_Label   ! selects a label (key)
     public :: I90_GLine   ! selects the next line (for tables)
     public :: I90_Gtoken  ! gets the next token 
     public :: I90_Gstr    ! get a string upto to a "$" or EOL

     public :: I90_AtoF	! ASCII to float (function)
     public :: I90_AtoI	! ASCII to integer (function)

     public :: I90_Gfloat  ! returns next float number (function)
     public :: I90_GInt	! returns next integer number (function)

     public :: lablin,rdnext,fltget,intget,getwrd,str2rn,chrget,getstr
     public :: strget

     interface fltget; module procedure &
	  fltgetsp, &
	  fltgetdp
     end interface


!-----------------------------------------------------------------------
!
!	This part was originally in "i90.h", but included for module.
!

		! revised parameter table to fit Fortran 90 standard

  integer,   parameter :: LSZ      = 256

!ams
! On Linux with the Fujitsu compiler, I needed to reduce NBUF_MAX
!ams 
! integer,   parameter :: NBUF_MAX = 400*(LSZ) ! max size of buffer
! integer,   parameter :: NBUF_MAX = 200*(LSZ) ! max size of buffer
! Further reduction of NBUF_MAX was necessary for the Fujitsu VPP:
  integer,   parameter :: NBUF_MAX = 128*(LSZ)-1 ! Maximum buffer size
                                                 ! that works with the
                                                 ! Fujitsu-VPP platform.


  character, parameter :: BLK = achar(32)   ! blank (space)
  character, parameter :: TAB = achar(09)   ! TAB
  character, parameter :: EOL = achar(10)   ! end of line mark (newline)
  character, parameter :: EOB = achar(00)   ! end of buffer mark (null)
  character, parameter :: NULL= achar(00)   ! what it says

  type inpak90
		! May be easily paged for extentable file size (J.G.)

    integer :: nbuf                           ! actual size of buffer
    character(len=NBUF_MAX),pointer :: buffer ! hold the whole file?
    character(len=LSZ),     pointer :: this_line ! the current line

    integer :: next_line  ! index for next line on buffer

    type(inpak90),pointer :: last
  end type inpak90

  integer,parameter :: MALLSIZE_=10	! just an estimation

  character(len=*),parameter :: myname='MCT(MPEU)::m_inpak90'
!-----------------------------------------------------------------------

    integer,parameter :: i90_MXDEP = 4
    integer,save      :: i90_depth = 0
    type(inpak90),save,pointer :: i90_now

!-----------------------------------------------------------------------
      contains
!~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
!       NASA/GSFC, Data Assimilation Office, Code 910.3, GEOS/DAS      !
!BOP -------------------------------------------------------------------
!
! !IROUTINE: I90_allLoadF - populate a rooted database to all PEs
!
! !DESCRIPTION:
!
! !INTERFACE:

    subroutine I90_allLoadF(fname,root,comm,istat)
      use m_mpif90, only : MP_perr
      use m_mpif90, only : MP_comm_rank
      use m_mpif90, only : MP_CHARACTER
      use m_mpif90, only : MP_INTEGER
      use m_die, only : perr
      implicit none
      character(len=*),intent(in) :: fname
      integer,intent(in) :: root
      integer,intent(in) :: comm
      integer,intent(out) :: istat

! !REVISION HISTORY:
! 	28Jul98 - Jing Guo <guo@thunder> - initial prototype/prolog/code
!EOP ___________________________________________________________________

  character(len=*),parameter :: myname_=myname//'::I90_allLoadF'
  integer :: myID,ier

  istat=0

  call MP_comm_rank(comm,myID,ier)
  if(ier/=0) then
    call MP_perr(myname_,'MP_comm_rank()',ier)
    istat=ier
    return
  endif

  if(myID == root) then
    call i90_LoadF(fname,ier)
    if(ier /= 0) then
      call perr(myname_,'i90_LoadF("//trim(fname)//")',ier)
      istat=ier
      return
    endif
  else
    call push_(ier)
    if(ier /= 0) then
      call perr(myname_,'push_()',ier)
      istat=ier
      return
    endif
  endif

	! Initialize the buffer on all PEs

  call MPI_Bcast(i90_now%buffer,NBUF_MAX,MP_CHARACTER,root,comm,ier)
  if(ier /= 0) then
    call MP_perr(myname_,'MPI_Bcast(%buffer)',ier)
    istat=ier
    return
  endif

  call MPI_Bcast(i90_now%nbuf,1,MP_INTEGER,root,comm,ier)
  if(ier /= 0) then
    call MP_perr(myname_,'MPI_Bcast(%nbuf)',ier)
    istat=ier
    return
  endif

  i90_now%this_line=' '
  i90_now%next_line=0

end subroutine I90_allLoadF

!~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
!       NASA/GSFC, Data Assimilation Office, Code 910.3, GEOS/DAS      !
!BOP -------------------------------------------------------------------
!
! !IROUTINE: push_ - push on a new layer of the internal file _i90_now_
!
! !DESCRIPTION:
!
! !INTERFACE:

    subroutine push_(ier)
      use m_die, only : perr
      use m_mall,only : mall_mci,mall_ci,mall_ison
      implicit none
      integer,intent(out) :: ier

! !REVISION HISTORY:
! 	05Aug98 - Jing Guo <guo@thunder> - initial prototype/prolog/code
!EOP ___________________________________________________________________

  character(len=*),parameter :: myname_=myname//'::push_'
  type(inpak90),pointer :: new

  if(i90_depth <= 0) nullify(i90_now)	! just an initialization

	! Too many levels

  if(i90_depth >= i90_MXDEP) then
	call perr(myname_,'(overflow)',i90_depth)
	ier=1
	return
  endif

  allocate(new,stat=ier)
	if(ier /= 0) then
	  call perr(myname_,'allocate(new)',ier)
	  return
	endif

	if(mall_ison()) call mall_ci(MALLSIZE_,myname)

  allocate(new%buffer,new%this_line,stat=ier)
	if(ier /= 0) then
	  call perr(myname_,'allocate(new%..)',ier)
	  return
	endif

	if(mall_ison()) then
	  call mall_mci(new%buffer,myname)
	  call mall_mci(new%this_line,myname)
	endif

  new%last => i90_now
  i90_now  => new
  nullify(new)

  i90_depth = i90_depth+1
end subroutine push_

!~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
!       NASA/GSFC, Data Assimilation Office, Code 910.3, GEOS/DAS      !
!BOP -------------------------------------------------------------------
!
! !IROUTINE: pop_ - pop off a layer of the internal file _i90_now_
!
! !DESCRIPTION:
!
! !INTERFACE:

    subroutine pop_(ier)
      use m_die, only : perr
      use m_mall,only : mall_mco,mall_co,mall_ison
      implicit none
      integer,intent(out) :: ier

! !REVISION HISTORY:
! 	05Aug98 - Jing Guo <guo@thunder> - initial prototype/prolog/code
!EOP ___________________________________________________________________

  character(len=*),parameter :: myname_=myname//'::pop_'
  type(inpak90),pointer :: old

  if(i90_depth <= 0) then
	call perr(myname_,'(underflow)',i90_depth)
	ier=1
	return
  endif

  old => i90_now%last

	if(mall_ison()) then
	  call mall_mco(i90_now%this_line,myname)
	  call mall_mco(i90_now%buffer,myname)
	endif

  deallocate(i90_now%buffer,i90_now%this_line,stat=ier)
	if(ier /= 0) then
	  call perr(myname_,'deallocate(new%..)',ier)
	  return
	endif

	if(mall_ison())  call mall_co(MALLSIZE_,myname)

  deallocate(i90_now,stat=ier)
	if(ier /= 0) then
	  call perr(myname_,'deallocate(new)',ier)
	  return
	endif

  i90_now => old
  nullify(old)

  i90_depth = i90_depth - 1
end subroutine pop_

!~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
!       NASA/GSFC, Data Assimilation Office, Code 910.3, GEOS/DAS      !
!-----------------------------------------------------------------------
!
! !ROUTINE: I90_Release - deallocate memory used to load a resource file
!
! !INTERFACE:
!
      subroutine I90_Release(stat)
	use m_die,only : perr,die
	implicit none
	integer,optional, intent(out) :: stat 
!
! !DESCRIPTION:
!
!	I90_Release() is used to pair I90_LoadF() to release the memory
!	used by I90_LoadF() for resourse data input.
!
! !SEE ALSO:
!
! !REVISION HISTORY:
! 	03Jul96 - J. Guo	- added to Arlindos inpak90 for its
!				  Fortran 90 revision.
!_______________________________________________________________________
  character(len=*),parameter :: myname_=myname//'::i90_Release'
  integer :: ier

  if(present(stat)) stat=0

  call pop_(ier)
  if(ier/=0) then
    call perr(myname_,'pop_()',ier)
    if(.not.present(stat)) call die(myname_)
    stat=ier
    return
  endif

 end subroutine I90_Release

!~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
!       NASA/GSFC, Data Assimilation Office, Code 910.3, GEOS/DAS      !
!BOP -------------------------------------------------------------------
!
! !IROUTINE: i90_fullRelease - releases the whole stack led by _i90_now_
!
! !DESCRIPTION:
!
! !INTERFACE:

    subroutine i90_fullRelease(ier)
      use m_die,only : perr
      implicit none
      integer,intent(out) :: ier

! !REVISION HISTORY:
! 	05Aug98 - Jing Guo <guo@thunder> - initial prototype/prolog/code
!EOP ___________________________________________________________________

  character(len=*),parameter :: myname_=myname//'::i90_fullRelease'

  do while(i90_depth > 0)
    call pop_(ier)
    if(ier /= 0) then
      call perr(myname_,'pop_()',ier)
      return
    endif
  end do
  ier=0

end subroutine i90_fullRelease
!=======================================================================
      subroutine I90_LoadF ( filen, iret )
	use m_ioutil, only : luavail,opntext,clstext
	use m_die, only : perr
      implicit NONE

!-------------------------------------------------------------------------
!         NASA/GSFC, Data Assimilation Office, Code 910.3, GEOS/DAS      !
!-------------------------------------------------------------------------
!BOP
!
! !ROUTINE:  I90_LoadF() --- Loads resource file into memory.
! 
! !DESCRIPTION: 
!
!  Reads resource file, strips out comments, translate TABs into
!  blanks, and loads the modified file contents into memory.
!  Must be called only once for each resource file.
!
! !CALLING SEQUENCE: 
!
!     call i90_LoadF ( filen, iret )
!
! !INPUT PARAMETERS: 
!
      character*(*) filen            ! file name

! !OUTPUT PARAMETERS:

      integer       iret             ! Return code:
                                     !   0    no error
                                     ! -98    coult not get unit number
                                     !        (strange!)
                                     ! -98    talk to a wizzard
                                     ! -99    out of memory: increase
                                     !        NBUF_MAX in 'i90.h'
                                     ! other  iostat from open statement.
!
! !BUGS:  
!
!  It does not perform dynamic allocation, mostly to keep vanilla f77
!  compatibility. Overall amount of static memory is small (~100K
!  for default NBUF_MAX = 400*256).
!
! !SEE ALSO: 
!
!  i90_label()   selects a label (key)
!
! !FILES USED:  
!
!  File name supplied on input. The file is opened, read and then closed.
!
! !REVISION HISTORY: 
!
!  19Jun96   da Silva   Original code.
!
!EOP
!-------------------------------------------------------------------------
      integer         lu, ios, loop, ls, ptr
      character*256   line
      character(len=*), parameter :: myname_ = myname//'::i90_loadf'

		! Check to make sure there is not too many levels
		! of the stacked resource files

  if(i90_depth >= i90_MXDEP) then
	call perr(myname_,'(overflow)',i90_depth)
	iret=1
	return
  endif

!     Open file
!     ---------     
!      lu = i90_lua()

      lu = luavail()	! a more portable version
      if ( lu .lt. 0 ) then
         iret = -97
         return
      end if

	! A open through an interface to avoid portability problems.
	! (J.G.)

      call opntext(lu,filen,'old',ios)
      if ( ios .ne. 0 ) then
	 write(stderr,'(2a,i5)') myname_,': opntext() error, ios =',ios
         iret = ios
         return
      end if

	! Create a dynamic page to store the file.  It might be expanded
	! to allocate memory on requests (a link list) (J.G.)

	! Changed from page_() to push_(), to allow multiple (stacked)
	! inpak90 buffers.  J.G.

      call push_(ios)	! to create buffer space
      if ( ios .ne. 0 ) then
	 write(stderr,'(2a,i5)') myname_,': push_() error, ios =',ios
         iret = ios
         return
      end if

!     Read to end of file
!     -------------------
      i90_now%buffer(1:1) = EOL
      ptr = 2                         ! next buffer position
      do loop = 1, NBUF_MAX

!        Read next line
!        --------------
         read(lu,'(a)', end=11) line  ! read next line
         call i90_trim ( line )      ! remove trailing blanks
         call i90_pad ( line )        ! Pad with # from end of line

!        A non-empty line
!        ----------------
         ls = index(line,'#' ) - 1    ! line length
         if ( ls .gt. 0 ) then
              if ( (ptr+ls) .gt. NBUF_MAX ) then
                 iret = -99
                 return
              end if
              i90_now%buffer(ptr:ptr+ls) = line(1:ls) // EOL
              ptr = ptr + ls + 1
         end if

      end do

      iret = -98 ! good chance i90_now%buffer is not big enough 
      return

 11   continue

!     All done
!     --------
!      close(lu)
	call clstext(lu,ios)
	if(ios /= 0) then
	  iret=-99
	  return
	endif
      i90_now%buffer(ptr:ptr) = EOB
      i90_now%nbuf = ptr
      i90_now%this_line=' '
      i90_now%next_line=0
      iret = 0

      return
      end subroutine I90_LoadF

            
!...................................................................

      subroutine i90_label ( label, iret )

      implicit NONE

!-------------------------------------------------------------------------
!         NASA/GSFC, Data Assimilation Office, Code 910.3, GEOS/DAS      !
!-------------------------------------------------------------------------
!BOP
!
! !ROUTINE:  I90_Label() --- Selects a label (record).
! 
! !DESCRIPTION: 
!
!  Once the buffer has been loaded with {\tt i90\_loadf()}, this routine
!  selects a given ``line'' (record/table) associated with ``label''. 
!  Think of ``label'' as a resource name or data base ``key''.
!
! !CALLING SEQUENCE: 
!           
!     call i90_Label ( label, iret )
!
! !INPUT PARAMETERS: 
!
      character(len=*),intent(in) :: label            ! input label

! !OUTPUT PARAMETERS:

      integer       iret             ! Return code:
                                     !   0    no error
                                     !  -1    buffer not loaded
                                     !  -2    could not find label   
!
! !SEE ALSO: 
!
!  i90_loadf()    load file into buffer
!  i90_gtoken()   get next token
!  i90_gline()    get next line (for tables)
!  atof()         convert word (string) to float
!  atoi()         convert word (string) to integer
!  
! !REVISION HISTORY: 
!
!  19Jun96   da Silva   Original code.
!  19Jan01   Jay Larson <larson@mcs.anl.gov> - introduced CHARACTER 
!            variable EOL_label, which is used to circumvent pgf90
!            problems with passing concatenated characters as an argument
!            to a function.
!
!EOP
!-------------------------------------------------------------------------

      integer i, j

      character(len=(len(label)+len(EOL))) :: EOL_label

!	Make sure that a buffer is defined (JG)
!	----------------------------------
	if(i90_depth <= 0) then
	  iret = -1
	  return
	endif

!     Determine whether label exists
!     ------------------------------     
      EOL_label = EOL // label
      i = index ( i90_now%buffer(1:i90_now%nbuf), EOL_label ) + 1
      if ( i .le. 1 ) then
           i90_now%this_line = BLK // EOL
           iret = -2
           return
      end if

!     Extract the line associated with this label
!     -------------------------------------------
      i = i + len ( label )
      j = i + index(i90_now%buffer(i:i90_now%nbuf),EOL) - 2
      i90_now%this_line = i90_now%buffer(i:j) // BLK // EOL

      i90_now%next_line = j + 2

      iret = 0

      return
      end subroutine i90_label

!...................................................................

      subroutine i90_gline ( iret )

      implicit NONE

!-------------------------------------------------------------------------
!         NASA/GSFC, Data Assimilation Office, Code 910.3, GEOS/DAS      !
!-------------------------------------------------------------------------
!BOP
!
! !ROUTINE:  I90_GLine() --- Selects next line.
! 
! !DESCRIPTION: 
!
!     Selects next line, irrespective of of label. If the next line starts
! with :: (end of table mark), then it lets the user know. This sequential
! access of the buffer is useful to assess tables, a concept introduced
! in Inpak 77 by Jing Guo. A table is a construct like this:
!
! \begin{verbatim}
! my_table_name::
!  1000     3000     263.0   
!   925     3000     263.0
!   850     3000     263.0
!   700     3000     269.0
!   500     3000     287.0
!   400     3000     295.8
!   300     3000     295.8    
! ::
! \end{verbatim}
!
! To access this table, the user first must use {\tt i90\_label()} to 
! locate the beginning of the table, e.g.,
!
! \begin{verbatim}
!       call i90_label ( 'my_table_name::', iret )
! \end{verbatim}
!
! Subsequently, {\tt i90\_gline()} can be used to gain acess to each
! row of the table. Here is a code fragment to read the above
! table (7 rows, 3 columns):
!
! \begin{verbatim}
!       real          table(7,3)
!       character*20  word
!       integer       iret
!       call i90_label ( 'my_table_name::', iret )
!       do i = 1, 7
!          call i90_gline ( iret )
!          do j = 1, 3
!             table(i,j) = fltget ( 0. )
!          end do                   
!       end do
! \end{verbatim}
!
!  For simplicity we have assumed that the dimensions of table were
!  known. It is relatively simple to infer the table dimensions
!  by manipulating ``iret''.
!
! !CALLING SEQUENCE: 
!
!     call i90_gline ( iret )
!
! !INPUT PARAMETERS: 
!
!     None.
!
! !OUTPUT PARAMETERS:
!
      integer       iret             ! Return code:
                                     !   0    no error
                                     !  -1    end of buffer reached
                                     !  +1    end of table  reached

! !SEE ALSO: 
!
!  i90_label()    selects a line (record/table)
!
! !REVISION HISTORY: 
!
!  10feb95   Guo        Wrote rdnext(), Inpak 77 extension.
!  19Jun96   da Silva   Original code with functionality of rdnext()
!
!EOP
!-------------------------------------------------------------------------

      integer i, j

!	Make sure that a buffer is defined (JG)
!	----------------------------------
	if(i90_depth <= 0) then
	  iret = -1
	  return
	endif

      if ( i90_now%next_line .ge. i90_now%nbuf ) then
         iret = -1
         return
      end if

      i = i90_now%next_line
      j = i + index(i90_now%buffer(i:i90_now%nbuf),EOL) - 2
      i90_now%this_line = i90_now%buffer(i:j) // BLK // EOL
      
      if ( i90_now%this_line(1:2) .eq. '::' ) then
           iret = 1                        ! end of table
           i90_now%next_line = i90_now%nbuf + 1
           return
      end if

      i90_now%next_line = j + 2
      iret = 0

      return
      end subroutine i90_gline

!...................................................................
            
      subroutine i90_GToken ( token, iret )

      implicit NONE

!-------------------------------------------------------------------------
!         NASA/GSFC, Data Assimilation Office, Code 910.3, GEOS/DAS      !
!-------------------------------------------------------------------------
!BOP
!
! !ROUTINE:  I90_GToken()  --- Gets next token.
! 
! !DESCRIPTION: 
!
!  Get next token from current line. The current line is defined by a
!  call to {\tt i90\_label()}. Tokens are sequences of characters (including
!  blanks) which may be enclosed by single or double quotes. 
!  If no quotes are present, the token from the current position to the next
!  blank of TAB is returned.
!  
!  {\em Examples of valid token:}
!
!  \begin{verbatim}
!               single_token "second token on line"
!               "this is a token"
!               'Another example of a token'
!               'this is how you get a " inside a token'
!               "this is how you get a ' inside a token"
!               This is valid too   # the line ends before the #
!  \end{verbatim}
!  The last line has 4 valid tokens: {\tt This, is, valid} and {\tt too}.
!  
!  {\em Invalid string constructs:}
!
!  \begin{verbatim}
!               cannot handle mixed quotes (i.e. single/double)
!               'escaping like this \' is not implemented'
!               'this # will not work because of the #'
!  \end{verbatim}
!  The \# character is reserved for comments and cannot be included
!  inside quotation marks.
!
! !CALLING SEQUENCE: 
!
!     call i90_GToken ( token, iret )
!
! !INPUT PARAMETERS: 
!
!     None.
!
! !OUTPUT PARAMETERS:
!
      character*(*) token            ! Next token from current line
      integer       iret             ! Return code:
                                     !   0    no error
                                     !  -1    either nothing left
                                     !        on line or mismatched
                                     !        quotation marks.

! !BUGS:  
!
!     Standard Unix escaping is not implemented at the moment.
!     
!
! !SEE ALSO: 
!
!  i90_label()    selects a line (record/table)
!  i90_gline()    get next line (for tables)
!  atof()         convert word (string) to float
!  atoi()         convert word (string) to integer
!  
!
! !REVISION HISTORY: 
!
!  19Jun96   da Silva   Original code.
!
!EOP
!-------------------------------------------------------------------------

      character*1   ch
      integer       ib, ie

!	Make sure that a buffer is defined (JG)
!	----------------------------------
	if(i90_depth <= 0) then
	  iret = -1
	  return
	endif

      call i90_trim ( i90_now%this_line )
     
      ch = i90_now%this_line(1:1)
      if ( ch .eq. '"' .or. ch .eq. "'" ) then
           ib = 2
           ie = index ( i90_now%this_line(ib:), ch ) 
      else
           ib = 1
           ie = min(index(i90_now%this_line,BLK),	&
		    index(i90_now%this_line,EOL)) - 1

      end if

      if ( ie .lt. ib ) then
           token = BLK
           iret = -1
           return
      else
		! Get the token, and shift the rest of %this_line to
		! the left

           token = i90_now%this_line(ib:ie) 
           i90_now%this_line = i90_now%this_line(ie+2:)
           iret = 0
      end if

      return
      end subroutine i90_gtoken
!...................................................................
      subroutine i90_gstr ( string, iret )

      implicit NONE

!-------------------------------------------------------------------------
!         NASA/GSFC, Data Assimilation Office, Code 910.3, GEOS/DAS      !
!-------------------------------------------------------------------------
!
! !ROUTINE:  I90\_GStr()
! 
! !DESCRIPTION: 
!
!  Get next string from current line. The current line is defined by a
!  call to {\tt i90\_label()}. Strings are sequence of characters (including
!  blanks) enclosed by single or double quotes. If no quotes
!  are present, the string from the current position to the end of 
!  the line is returned.
!
!  NOTE: This routine is defined differently from \verb"i90_GTolen()",
!	 where a {\sl token} is white-space delimited, but this routine
!	 will try to fetch a string either terminated by a "$" or by the
!	 end of the line.
!
!  {\em Examples of valid strings:}
!
!  \begin{verbatim}
!               "this is a string"
!               'Another example of string'
!               'this is how you get a " inside a string'
!               "this is how you get a ' inside a string"
!               This is valid too   # the line ends before the #
!
!  \end{verbatim}
!  
!  {\em Invalid string constructs:}
!
!  \begin{verbatim}
!               cannot handle mixed quotes
!               'escaping like this \' is not implemented'
!  \end{verbatim}
!
!  {\em Obsolete feature (for Inpak 77 compatibility):}
!
!  \begin{verbatim}
!               the string ends after a $ this is another string
!  \end{verbatim}
!
! !CALLING SEQUENCE: 
!
!  \begin{verbatim}
!     call i90_Gstr ( string, iret )
!  \end{verbatim}
!
! !INPUT PARAMETERS: 
!
      character*(*) string           ! A NULL (char(0)) delimited string.

! !OUTPUT PARAMETERS:
!
      integer       iret             ! Return code:
                                     !   0    no error
                                     !  -1    either nothing left
                                     !        on line or mismatched
                                     !        quotation marks.

! !BUGS:  
!
!     Standard Unix escaping is not implemented at the moment.
!     No way to tell sintax error from end of line (same iret).
!     
!
! !SEE ALSO: 
!
!  i90_label()    selects a line (record/table)
!  i90_gtoken()   get next token
!  i90_gline()    get next line (for tables)
!  atof()         convert word (string) to float
!  atoi()         convert word (string) to integer
!  
!
! !REVISION HISTORY: 
!
!  19Jun96   da Silva   Original code.
!  01Oct96   Jing Guo	Removed the null terminitor
!
!-------------------------------------------------------------------------

      character*1   ch
      integer       ib, ie

!	Make sure that a buffer is defined (JG)
!	----------------------------------
	if(i90_depth <= 0) then
	  iret = -1
	  return
	endif

      call i90_trim ( i90_now%this_line )
     
      ch = i90_now%this_line(1:1)
      if ( ch .eq. '"' .or. ch .eq. "'" ) then
           ib = 2
           ie = index ( i90_now%this_line(ib:), ch ) 
      else
           ib = 1
           ie = index(i90_now%this_line,'$')-1  ! undocumented feature!
           if ( ie .lt. 1 ) ie = index(i90_now%this_line,EOL)-2
      end if

      if ( ie .lt. ib ) then
!         string = NULL
           iret = -1
           return
      else
           string = i90_now%this_line(ib:ie) ! // NULL
           i90_now%this_line = i90_now%this_line(ie+2:)
           iret = 0
      end if

      return
      end subroutine i90_gstr

!...................................................................

      real(FP) function i90_GFloat( iret )

      implicit NONE

!-------------------------------------------------------------------------
!         NASA/GSFC, Data Assimilation Office, Code 910.3, GEOS/DAS      !
!-------------------------------------------------------------------------
!BOP
!
! !ROUTINE: i90_GFloat() --- Returns next float number.
! 
! !DESCRIPTION: 
!
!  Returns next float (real number) from the current line.
!  If an error occurs a zero value is returned.
!
! !CALLING SEQUENCE: 
!
!      real  rnumber
!      rnumber = i90_gfloat ( default )
!
! !OUTPUT PARAMETERS: 
!
      integer,intent(out) :: iret    ! Return code:
                                     !   0    no error
                                     !  -1    either nothing left
                                     !        on line or mismatched
                                     !        quotation marks.
                                     !  -2    parsing error

!
! !REVISION HISTORY: 
!
!  19Jun96   da Silva   Original code.
!
!EOP
!-------------------------------------------------------------------------

      character*256 token
      integer       ios
      real(FP)      x

!	Make sure that a buffer is defined (JG)
!	----------------------------------
	if(i90_depth <= 0) then
	  iret = -1
	  return
	endif

      call i90_gtoken ( token, iret )
      if ( iret .eq. 0 ) then
           read(token,*,iostat=ios) x	! Does it require an extension?
           if ( ios .ne. 0 ) iret = -2
      end if
      if ( iret .ne. 0 ) x = 0.
      i90_GFloat = x

      return
      end function i90_GFloat

!...................................................................

      integer function I90_GInt ( iret )

      implicit NONE

!-------------------------------------------------------------------------
!         NASA/GSFC, Data Assimilation Office, Code 910.3, GEOS/DAS      !
!-------------------------------------------------------------------------
!BOP
!
! !ROUTINE: I90_GInt() --- Returns next integer number.
! 
! !DESCRIPTION: 
!
!  Returns next integer number from the current line.
!  If an error occurs a zero value is returned.
!
! !CALLING SEQUENCE: 
!
!      integer number
!      number = i90_gint ( default )
!
! !OUTPUT PARAMETERS: 
!
      integer       iret             ! Return code:
                                     !   0    no error
                                     !  -1    either nothing left
                                     !        on line or mismatched
                                     !        quotation marks.
                                     !  -2    parsing error

!
! !REVISION HISTORY: 
!
!  19Jun96   da Silva   Original code.
!  24may00   da Silva   delcared x as real*8 in case this module is compiled
!                       with real*4
!
!EOP
!-------------------------------------------------------------------------

      character*256 token
      real(kind_r8) x
      integer       ios

!	Make sure that a buffer is defined (JG)
!	----------------------------------
	if(i90_depth <= 0) then
	  iret = -1
	  return
	endif

      call i90_gtoken ( token, iret )
      if ( iret .eq. 0 ) then
           read(token,*,iostat=ios) x
           if ( ios .ne. 0 ) iret = -2
      end if
      if ( iret .ne. 0 ) x = 0
      i90_gint = nint(x)

      return
      end function i90_gint
      
!...................................................................

      real(FP) function i90_AtoF( string, iret )

      implicit NONE

!-------------------------------------------------------------------------
!         NASA/GSFC, Data Assimilation Office, Code 910.3, GEOS/DAS      !
!-------------------------------------------------------------------------
!BOP
!
! !ROUTINE:  i90_AtoF() --- Translates ASCII (string) to float.
! 
! !DESCRIPTION: 
!
!     Converts string to real number. Same as obsolete {\tt str2rn()}.
!
! !CALLING SEQUENCE: 
!
!     real  rnumber
!     rnumber = i90_atof ( string, iret )
!
! !INPUT PARAMETERS: 
!
      character(len=*),intent(in) :: string           ! a string

! !OUTPUT PARAMETERS:
!
      integer,intent(out) :: iret    ! Return code:
                                     !   0    no error
                                     !  -1    could not convert, probably
                                     !        string is not a number

!
! !REVISION HISTORY: 
!
!  19Jun96   da Silva   Original code.
!
!EOP
!-------------------------------------------------------------------------

      read(string,*,end=11,err=11) i90_AtoF
      iret = 0
      return
 11   iret = -1
      return
      end function i90_AtoF

!...................................................................

      integer function i90_atoi ( string, iret )

      implicit NONE

!-------------------------------------------------------------------------
!         NASA/GSFC, Data Assimilation Office, Code 910.3, GEOS/DAS      !
!-------------------------------------------------------------------------
!BOP
!
! !ROUTINE:  I90_AtoI() --- Translates ASCII (strings) to integer.
! 
! !DESCRIPTION: 
!
!     Converts string to integer number.
!
! !CALLING SEQUENCE: 
!
!     integer number
!     number = i90_atoi ( string, iret )
!
! !INPUT PARAMETERS: 
!
      character*(*) string           ! a string

! !OUTPUT PARAMETERS:
!
      integer       iret             ! Return code:
                                     !   0    no error
                                     !  -1    could not convert, probably
                                     !        string is not a number

!
! !REVISION HISTORY: 
!
!  19Jun96   da Silva   Original code.
!
!EOP
!-------------------------------------------------------------------------

      read(string,*,end=11,err=11) i90_atoi
      iret = 0
      return
 11   iret = -1
      return
      end function i90_atoi

!...................................................................

      integer function i90_Len ( string )

      implicit NONE

!-------------------------------------------------------------------------
!         NASA/GSFC, Data Assimilation Office, Code 910.3, GEOS/DAS      !
!-------------------------------------------------------------------------
!BOP
!
! !ROUTINE:  I90_Len() --- Returns length of string.
! 
! !DESCRIPTION: 
!
!  Returns the length of a string excluding trailing blanks.
!  It follows that 
!  \begin{verbatim}
!              i90_len(string) .le. len(string),
!  \end{verbatim}
!  where {\tt len} is the intrinsic string length function.  
!  Example:
!  \begin{verbatim}
!         ls = len('abc  ')       ! results in ls = 5
!         ls = i90_len ('abc  ')  ! results in ls = 3
!  \end{verbatim}
!
! !CALLING SEQUENCE: 
!
!       integer ls
!       ls = i90_len ( string )
!
! !INPUT PARAMETERS: 
!
         character*(*)   string     ! a string
!
! !OUTPUT PARAMETERS:
!
!        The length of the string, excluding trailing blanks.
!
! !REVISION HISTORY: 
!
!  01Apr94   Guo        Original code (a.k.a. luavail())
!  19Jun96   da Silva   Minor modification + prologue.
!
!EOP
!-------------------------------------------------------------------------

      integer ls, i, l
      ls = len(string)
      do i = ls, 1, -1
         l = i
         if ( string(i:i) .ne. BLK ) go to 11
      end do
      l = l - 1
 11   continue
      i90_len = l
      return
      end function i90_len
      
!...................................................................

      integer function I90_Lua()

      implicit NONE

!-------------------------------------------------------------------------
!         NASA/GSFC, Data Assimilation Office, Code 910.3, GEOS/DAS      !
!-------------------------------------------------------------------------
!BOP
!
! !ROUTINE:  I90_Lua() --- Returns available logical unit number.
! 
! !DESCRIPTION: 
!
!  Look for an available (not opened) Fortran logical unit for i/o.
!
! !CALLING SEQUENCE: 
!
!       integer lu
!       lu = i90_lua()
!
! !INPUT PARAMETERS: 
!
!       None.
!
! !OUTPUT PARAMETERS:
!
!       The desired unit number if positive, -1 if unsucessful.
!
! !REVISION HISTORY: 
!
!  01Apr94   Guo        Original code (a.k.a. luavail())
!  19Jun96   da Silva   Minor modification + prologue.
!
!EOP
!-------------------------------------------------------------------------


      integer lu,ios
      logical opnd
      lu=7
      inquire(unit=lu,opened=opnd,iostat=ios)
      do while(ios.eq.0.and.opnd)
         lu=lu+1
         inquire(unit=lu,opened=opnd,iostat=ios)
      end do
      if(ios.ne.0) lu=-1
      i90_lua=lu
      return
      end function i90_lua
      
!...................................................................

      subroutine i90_pad ( string )

      implicit NONE

!-------------------------------------------------------------------------
!         NASA/GSFC, Data Assimilation Office, Code 910.3, GEOS/DAS      !
!-------------------------------------------------------------------------
!BOP
!
! !ROUTINE:  I90_Pad() --- Pad strings.
! 
! !DESCRIPTION: 
!
!     Pads from the right with the comment character (\#). It also
!  replaces TABs with blanks for convenience. This is a low level
!  i90 routine.
!
! !CALLING SEQUENCE: 
!
!      call i90_pad ( string )
!
! !INPUT PARAMETERS: 
!
       character*256 string       ! input string

! !OUTPUT PARAMETERS:            ! modified string
!
!      character*256 string
!
! !BUGS:  
!
!      It alters TABs even inside strings.
!
!
! !REVISION HISTORY: 
!
!  19Jun96   da Silva   Original code.
!
!EOP
!-------------------------------------------------------------------------

      integer i

!     Pad end of string with #
!     ------------------------
      do i = 256, 1, -1 
         if ( string(i:i) .ne. ' ' .and.	&
	        string(i:i) .ne. '$' ) go to 11
         string(i:i) = '#'
      end do
 11   continue

!     Replace TABs with blanks
!     -------------------------
      do i = 1, 256
         if ( string(i:i) .eq. TAB ) string(i:i) = BLK
         if ( string(i:i) .eq. '#' ) go to 21
      end do
 21   continue

      return
      end subroutine i90_pad
         
!...................................................................

      subroutine I90_Trim ( string )

      implicit NONE

!-------------------------------------------------------------------------
!         NASA/GSFC, Data Assimilation Office, Code 910.3, GEOS/DAS      !
!-------------------------------------------------------------------------
!BOP
!
! !ROUTINE:  I90_Trim() - Removes leading blanks from strings.
!
! !DESCRIPTION: 
!
!    Removes blanks and TABS from begenning of string. 
!    This is a low level i90 routine.
! 
! !CALLING SEQUENCE: 
!
!     call i90_Trim ( string )
!
! !INPUT PARAMETERS: 
!
      character*256 string    ! the input string
!
! !OUTPUT PARAMETERS:
!
!     character*256 string    ! the modified string
!
!
! !REVISION HISTORY: 
!
!  19Jun96   da Silva   Original code.
!
!EOP
!-------------------------------------------------------------------------

      integer     ib, i

!     Get rid of leading blanks
!     -------------------------
      ib = 1
      do i = 1, 255
         if ( string(i:i) .ne. ' ' .and.	&
	        string(i:i) .ne. TAB ) go to 21
         ib = ib + 1
      end do
 21   continue

!     String without trailling blanks
!     -------------------------------
      string = string(ib:)

      return
      end subroutine i90_trim


!==========================================================================


!                        -----------------------------
!                        Inpak 77 Upward Compatibility
!                        -----------------------------


      subroutine lablin ( label )

      implicit NONE

!-------------------------------------------------------------------------
!         NASA/GSFC, Data Assimilation Office, Code 910.3, GEOS/DAS      !
!-------------------------------------------------------------------------
!BOP
!
! !ROUTINE:  Lablin() --- Selects a Label (Inpak 77)
! 
! !DESCRIPTION: 
!
!    Selects a given ``line'' (record/table) associated with ``label''. 
!    Similar to {\tt i90\_label()}, but prints a message to {\tt stdout}
!    if it cannot locate the label. Kept for Inpak 77 upward compatibility.
!
! !CALLING SEQUENCE: 
!
!     call lablin ( label )
!
! !INPUT PARAMETERS: 

      character(len=*),intent(in) :: label   ! string with label name
!
! !OUTPUT PARAMETERS:
!
!    None.
!
! !REVISION HISTORY: 
!
!  19Jun96   da Silva   Original code.
!
!EOP
!-------------------------------------------------------------------------

      integer       iret

      call i90_label ( label, iret )
      if ( iret .ne. 0 ) then
	write(stderr,'(2a)') 'i90/lablin: cannot find label ', label
      endif

      end subroutine lablin
      
!...................................................................

      real(SP) function fltgetsp ( default )

      implicit NONE

!-------------------------------------------------------------------------
!         NASA/GSFC, Data Assimilation Office, Code 910.3, GEOS/DAS      !
!-------------------------------------------------------------------------
!BOP
!
! !ROUTINE: FltGetsp() --- Returns next float (Inpak 77, single precision)
! 
! !DESCRIPTION: 
!
!  Returns next float (real number, single precision) from the current 
!  line, or a default value if it fails to obtain the desired number.
!  Kept for Inpak 77 upward compatibility.
!
! !CALLING SEQUENCE: 
!
!      real  rnumber, default
!      rnumber = fltgetsp ( default )
!
! !INPUT PARAMETERS: 
!
      real(SP), intent(IN) ::    default       ! default value.

!
! !REVISION HISTORY: 
!
!  19Jun96   da Silva   Original code.
!  12Oct99   Guo/Larson - Built from original FltGet() function.
!
!EOP
!-------------------------------------------------------------------------

      character*256 token
      real(FP)      x
      integer       iret

      call i90_gtoken ( token, iret )
      if ( iret .eq. 0 ) then
           read(token,*,iostat=iret) x
      end if
      if ( iret .ne. 0 ) x = default
      !print *, x
      fltgetsp = x

      return
      end function fltgetsp
      
!...................................................................

      real(DP) function fltgetdp ( default )

      implicit NONE

!-------------------------------------------------------------------------
!         NASA/GSFC, Data Assimilation Office, Code 910.3, GEOS/DAS      !
!-------------------------------------------------------------------------
!BOP
!
! !ROUTINE: FltGetdp() --- Returns next float (Inpak 77)
! 
! !DESCRIPTION: 
!
!  Returns next float (real number) from the current line, or a 
!  default value (double precision) if it fails to obtain the desired 
!  number.  Kept for Inpak 77 upward compatibility.
!
! !CALLING SEQUENCE: 
!
!      real(DP) :: default
!      real :: rnumber 
!      rnumber = FltGetdp(default)
!
! !INPUT PARAMETERS: 
!
      real(DP), intent(IN) ::    default       ! default value.

!
! !REVISION HISTORY: 
!
!  19Jun96   da Silva   Original code.
!  12Oct99   Guo/Larson - Built from original FltGet() function.
!
!EOP
!-------------------------------------------------------------------------

      character*256 token
      real(FP)          x
      integer       iret

      call i90_gtoken ( token, iret )
      if ( iret .eq. 0 ) then
           read(token,*,iostat=iret) x
      end if
      if ( iret .ne. 0 ) x = default
      !print *, x
      fltgetdp = x

      return
      end function fltgetdp
      
!...................................................................

      integer function intget ( default )

      implicit NONE

!-------------------------------------------------------------------------
!         NASA/GSFC, Data Assimilation Office, Code 910.3, GEOS/DAS      !
!-------------------------------------------------------------------------
!BOP
!
! !ROUTINE: IntGet() --- Returns next integer (Inpak 77). 
! 
! !DESCRIPTION: 
!
!  Returns next integer number from the current line, or a default 
!  value if it fails to obtain the desired number.
!  Kept for Inpak 77 upward compatibility.
!
! !CALLING SEQUENCE: 
!
!      integer number, default
!      number = intget ( default )
!
! !INPUT PARAMETERS: 
!
       integer     default       ! default value.

!
! !REVISION HISTORY: 
!
!  19Jun96   da Silva   Original code.
!
!EOP
!-------------------------------------------------------------------------

      character*256 token
      real(FP)      x
      integer       iret

      call i90_gtoken ( token, iret )
      if ( iret .eq. 0 ) then
           read(token,*,iostat=iret) x
      end if
      if ( iret .ne. 0 ) x = default
      intget = nint(x)
      !print *, intget

      return
      end function intget
      
!...................................................................

      character(len=1) function chrget ( default )

      implicit NONE

!-------------------------------------------------------------------------
!         NASA/GSFC, Data Assimilation Office, Code 910.3, GEOS/DAS      !
!-------------------------------------------------------------------------
!BOP
!
! !ROUTINE: ChrGet() --- Returns next character (Inpak 77).
! 
! !DESCRIPTION: 
!
!  Returns next non-blank character from the current line, or a default 
!  character if it fails for whatever reason.
!  Kept for Inpak 77 upward compatibility.
!
! !CALLING SEQUENCE: 
!
!     character*1 ch, default
!     ch = chrget ( default )
!
! !INPUT PARAMETERS: 
!
      character*1    default       ! default value.

!
! !REVISION HISTORY: 
!
!  19Jun96   da Silva   Original code.
!
!EOP
!-------------------------------------------------------------------------

      character*256 token
      integer       iret

      call i90_gtoken ( token, iret )
      if ( iret .ne. 0 ) then
           chrget = default
      else
           chrget = token(1:1)
      end if
      !print *, chrget

      return
      end function chrget

!...................................................................

      subroutine TokGet ( token, default )

      implicit NONE


!-------------------------------------------------------------------------
!         NASA/GSFC, Data Assimilation Office, Code 910.3, GEOS/DAS      !
!-------------------------------------------------------------------------
!BOP
!
! !ROUTINE: TokGet() --- Gets next token (Inpakk 77 like).
! 
! !DESCRIPTION: 
!
!  Returns next token from the current line, or a default 
!  word if it fails for whatever reason.
!
! !CALLING SEQUENCE: 
!
!      call TokGet ( token, default )
!
! !INPUT PARAMETERS: 
!
       character*(*) default     ! default token

! !OUTPUT PARAMETERS:
!
       character*(*) token       ! desired token
!
! !REVISION HISTORY: 
!
!  19Jun96   da Silva   Original code.
!
!EOP
!-------------------------------------------------------------------------

      integer       iret

      call i90_GToken ( token, iret )
      if ( iret .ne. 0 ) then
           token = default
      end if
      !print *, token

      return
      end subroutine tokget
      
!====================================================================

!                          --------------------------
!                          Obsolete Inpak 77 Routines
!                              (Not Documented)
!                          --------------------------

!...................................................................

      subroutine iniin()
      print *, 		&
           'i90: iniin() is obsolete, use i90_loadf() instead!'
      return
      end subroutine iniin
   

!...................................................................

      subroutine iunits ( mifans, moftrm, moferr, miftrm )
      integer mifans, moftrm, moferr, miftrm 
      print *, 		&
           'i90: iunits() is obsolete, use i90_loadf() instead!'
      return
      end subroutine iunits
   
!...................................................................

      subroutine getstr ( iret, string )
      implicit NONE
      character*(*) string 
      integer       iret  !, ls
      call i90_gstr ( string, iret )
      return
      end subroutine getstr

!...................................................................

      subroutine getwrd ( iret, word )
      implicit NONE
      character*(*) word
      integer       iret
      call i90_gtoken ( word, iret )
      return
      end subroutine getwrd

!...................................................................

      subroutine rdnext ( iret )
      implicit NONE
      integer iret
      call i90_gline ( iret )
      return
      end subroutine rdnext

!...................................................................
            
      real(FP) function str2rn ( string, iret )
      implicit NONE
      character*(*) string
      integer iret
      read(string,*,end=11,err=11) str2rn
      iret = 0
      return
 11   iret = 1
      return
      end function str2rn

!...................................................................

      subroutine strget ( string, default )

      implicit NONE

!-------------------------------------------------------------------------
!         NASA/GSFC, Data Assimilation Office, Code 910.3, GEOS/DAS      !
!-------------------------------------------------------------------------
!
! !ROUTINE: StrGet()
! 
! !DESCRIPTION: 
!
!  Returns next string on the current line, or a default 
!  string if it fails for whatever reason. Similar to {\tt i90\_gstr()}.
!  Kept for Inpak 77 upward compatibility.
!
!  NOTE: This is an obsolete routine. The notion of "string" used
!        here is not conventional. Please use routine {\tt TokGet()}
!        instead.
!
! !CALLING SEQUENCE: 
!
!      call strget ( string, default )
!
! !INPUT PARAMETERS: 
!
       character*(*) default      ! default string

! !OUTPUT PARAMETERS:

       character*(*) string       ! desired string

!
! !REVISION HISTORY: 
!
!  19Jun96   da Silva   Original code.
!  01Oct96   Jing Guo   Removed the null terminitor
!
!-------------------------------------------------------------------------

      integer iret

      call i90_gstr ( string, iret )
      if ( iret .ne. 0 ) then
           string = default
      end if

      return
      end subroutine strget
      

end module m_inpak90